\n";
foreach ($tab_array as $ta) {
if ($ta[1] == true) {
echo "
\n";
} else {
echo "
\n";
}
$tabscounter++;
}
echo "
\n";
echo "
\n";
echo "";
}
/****f* pfsense-utils/display_topbar
* NAME
* display_topbar - top a table off with rounded edges
* INPUTS
* $text - (optional) Text to include in bar
* RESULT
* null
******/
function display_topbar($text = "", $bg_color="#990000", $replace_color="#FFFFFF", $rounding_style="smooth") {
echo "
\n";
echo "
\n";
echo "
";
echo "
\n";
echo "
\n";
echo "
\n";
if ($text != "")
echo "
{$text}
\n";
else
echo "
\n";
echo "
\n";
echo "
";
echo "";
}
/****f* pfsense-utils/generate_random_mac_address
* NAME
* generate_random_mac - generates a random mac address
* INPUTS
* none
* RESULT
* $mac - a random mac address
******/
function generate_random_mac_address() {
$mac = "02";
for($x=0; $x<5; $x++)
$mac .= ":" . dechex(rand(16, 255));
return $mac;
}
/****f* pfsense-utils/strncpy
* NAME
* strncpy - copy strings
* INPUTS
* &$dst, $src, $length
* RESULT
* none
******/
function strncpy(&$dst, $src, $length) {
if (strlen($src) > $length) {
$dst = substr($src, 0, $length);
} else {
$dst = $src;
}
}
/****f* pfsense-utils/reload_interfaces_sync
* NAME
* reload_interfaces - reload all interfaces
* INPUTS
* none
* RESULT
* none
******/
function reload_interfaces_sync() {
global $config, $g;
$shutdown_webgui_needed = false;
touch("{$g['tmp_path']}/reloading_all");
if($g['debug'])
log_error("reload_interfaces_sync() is starting.");
if(file_exists("{$g['tmp_path']}/config.cache"))
unlink("{$g['tmp_path']}/config.cache");
/* parse config.xml again */
$config = parse_config(true);
$wan_if = $config['interfaces']['wan']['if'];
if (isset($config['interfaces']['lan']))
$lan_if = $config['interfaces']['lan']['if'];
else
$lan_if = "";
if($g['debug'])
log_error("Cleaning up Interfaces");
/* if list */
$iflist = get_configured_interface_list(true);
foreach ($iflist as $ifent => $ifname) {
$ifname_real = convert_friendly_interface_to_real_interface_name($ifname);
if(stristr($ifname, "lo0") == true)
continue;
/* do not process wan interface, its mandatory */
if(stristr($ifname, "$wan_if") == true)
continue;
/* do not process lan interface, its mandatory */
if(stristr($ifname, "$lan_if") == true)
continue;
if($g['debug'])
log_error("Downing and deleting $ifname_real - $ifname");
mwexec("/sbin/ifconfig {$ifname_real} down");
mwexec("/sbin/ifconfig {$ifname_real} delete");
}
/* set up interfaces */
interfaces_configure();
/* set up static routes */
if($g['debug'])
log_error("Configuring system Routing");
system_routing_configure();
/* enable routing */
if($g['debug'])
log_error("Enabling system routing");
system_routing_enable();
/* setup captive portal if needed */
if($g['debug'])
log_error("Configuring Captive portal");
captiveportal_configure();
/* restart webConfigurator if needed */
if($shutdown_webgui_needed == true)
touch("/tmp/restart_webgui");
/* start devd back up */
mwexec("/bin/rm /tmp/reload*");
/* remove reloading_all trigger */
if($g['debug'])
log_error("Removing {$g['tmp_path']}/reloading_all");
unlink_if_exists("{$g['tmp_path']}/reloading_all");
}
/****f* pfsense-utils/reload_all
* NAME
* reload_all - triggers a reload of all settings
* * INPUTS
* none
* RESULT
* none
******/
function reload_all() {
touch("/tmp/reload_all");
}
/****f* pfsense-utils/reload_interfaces
* NAME
* reload_interfaces - triggers a reload of all interfaces
* INPUTS
* none
* RESULT
* none
******/
function reload_interfaces() {
touch("/tmp/reload_interfaces");
}
/****f* pfsense-utils/reload_all_sync
* NAME
* reload_all - reload all settings
* * INPUTS
* none
* RESULT
* none
******/
function reload_all_sync() {
global $config, $g;
$g['booting'] = false;
touch("{$g['tmp_path']}/reloading_all");
$shutdown_webgui_needed = false;
if(file_exists("{$g['tmp_path']}/config.cache"))
unlink("{$g['tmp_path']}/config.cache");
/* parse config.xml again */
$config = parse_config(true);
/* set up our timezone */
system_timezone_configure();
/* set up our hostname */
system_hostname_configure();
/* make hosts file */
system_hosts_generate();
/* generate resolv.conf */
system_resolvconf_generate();
/* Set up our loopback interface */
interfaces_loopback_configure();
$wan_if = $config['interfaces']['wan']['if'];
if (isset($config['interfaces']['lan']))
$lan_if = $config['interfaces']['lan']['if'];
else
$lan_if = "";
/* if list */
$iflist = get_configured_interface_list();
foreach ($iflist as $ifent => $ifname) {
$ifname_real = convert_friendly_interface_to_real_interface_name($ifname);
if(stristr($ifname, "lo0") == true)
continue;
/* do not process wan interface, its mandatory */
if($wan_if == $ifname_real)
continue;
/* do not process lan interface, its mandatory */
if($lan_if == $ifname_real)
continue;
mwexec("/sbin/ifconfig {$ifname_real} down");
mwexec("/sbin/ifconfig {$ifname_real} delete");
}
/* set up interfaces */
interfaces_configure();
/* set up static routes */
system_routing_configure();
/* enable routing */
system_routing_enable();
/* ensure passwords are sync'd */
// system_password_configure();
/* start dnsmasq service */
services_dnsmasq_configure();
/* start dyndns service */
services_dyndns_configure();
/* start DHCP service */
services_dhcpd_configure();
/* configure cron service */
configure_cron();
/* start the NTP client */
system_ntp_configure();
/* start ftp proxy helpers if they are enabled */
system_start_ftp_helpers();
/* start the captive portal */
captiveportal_configure();
/* reload the filter */
filter_configure_sync();
/* sync pw database */
conf_mount_rw();
mwexec("/usr/sbin/pwd_mkdb -d /etc/ /etc/master.passwd");
conf_mount_ro();
/* restart sshd */
touch("/tmp/start_sshd");
/* restart webConfigurator if needed */
if($shutdown_webgui_needed == true)
touch("/tmp/restart_webgui");
mwexec("/bin/rm /tmp/reload*");
unlink_if_exists("{$g['tmp_path']}/reloading_all");
}
function auto_login($status) {
$gettytab = file_get_contents("/etc/gettytab");
$getty_split = split("\n", $gettytab);
conf_mount_rw();
$fd = fopen("/etc/gettytab", "w");
foreach($getty_split as $gs) {
if(stristr($gs, ":ht:np:sp#115200") ) {
if($status == true) {
fwrite($fd, " :ht:np:sp#115200:al=root:\n");
} else {
fwrite($fd, " :ht:np:sp#115200:\n");
}
} else {
fwrite($fd, "{$gs}\n");
}
}
fclose($fd);
conf_mount_ro();
}
function setup_serial_port() {
global $g, $config;
conf_mount_rw();
/* serial console - write out /boot.config */
if(file_exists("/boot.config"))
$boot_config = file_get_contents("/boot.config");
else
$boot_config = "";
if($g['platform'] <> "cdrom") {
$boot_config_split = split("\n", $boot_config);
$fd = fopen("/boot.config","w");
if($fd) {
foreach($boot_config_split as $bcs) {
if(stristr($bcs, "-D")) {
/* DONT WRITE OUT, WE'LL DO IT LATER */
} else {
if($bcs <> "")
fwrite($fd, "{$bcs}\n");
}
}
if(isset($config['system']['enableserial'])) {
fwrite($fd, "-D");
}
fclose($fd);
}
/* serial console - write out /boot/loader.conf */
$boot_config = file_get_contents("/boot/loader.conf");
$boot_config_split = split("\n", $boot_config);
$fd = fopen("/boot/loader.conf","w");
if($fd) {
foreach($boot_config_split as $bcs) {
if(stristr($bcs, "console")) {
/* DONT WRITE OUT, WE'LL DO IT LATER */
} else {
if($bcs <> "")
fwrite($fd, "{$bcs}\n");
}
}
if(isset($config['system']['enableserial'])) {
fwrite($fd, "console=\"comconsole\"\n");
}
fclose($fd);
}
}
$ttys = file_get_contents("/etc/ttys");
$ttys_split = split("\n", $ttys);
$fd = fopen("/etc/ttys", "w");
foreach($ttys_split as $tty) {
if(stristr($tty, "ttyd0")) {
if(isset($config['system']['enableserial'])) {
fwrite($fd, "ttyd0 \"/usr/libexec/getty bootupcli\" dialup on secure\n");
} else {
fwrite($fd, "ttyd0 \"/usr/libexec/getty bootupcli\" dialup off secure\n");
}
} else {
fwrite($fd, $tty . "\n");
}
}
fclose($fd);
if(isset($config['system']['disableconsolemenu'])) {
auto_login(false);
} else {
auto_login(true);
}
conf_mount_ro();
return;
}
function print_value_list($list, $count = 10, $separator = ",") {
$list = implode($separator, array_slice($list, 0, $count));
if(count($list) < $count) {
$list .= ".";
} else {
$list .= "...";
}
return $list;
}
function update_filter_reload_status($text) {
global $g;
$fd = fopen("{$g['varrun_path']}/filter_reload_status", "w");
fwrite($fd, $text);
fclose($fd);
}
function get_interface_gateway($interface) {
global $config, $g;
$iflist = get_configured_interface_with_descr();
/*
* XXX: This is silly at first, but we may be called with the interface
* descr for no apparent reason!!!
*/
foreach ($iflist as $ifent => $ifdesc) {
if ($ifent == $interface || $ifdesc == $interface) {
$interface = $ifent;
break;
}
}
$gw = NULL;
$gwcfg = $config['interfaces'][$interface];
if (is_ipaddr($gwcfg['gateway']))
$gw = $gwcfg['gateway'];
else if (!empty($gwcfg['gateway']))
$gw = lookup_gateway_ip_by_name($gwcfg['gateway']);
// for dynamic interfaces we handle them through the $interface_router file.
if (!is_ipaddr($gw)) {
$realif = get_real_interface($interface);
if (file_exists("{$g['tmp_path']}/{$realif}_router")) {
$gw = file_get_contents("{$g['tmp_path']}/{$realif}_router");
$gw = rtrim($gw);
}
}
/* return gateway */
return $gw;
}
/* DHCP enabled on any interfaces? */
function is_dhcp_server_enabled()
{
global $config;
$dhcpdenable = false;
if (!is_array($config['dhcpd']))
return false;
$Iflist = get_configured_interface_list();
foreach ($config['dhcpd'] as $dhcpif => $dhcpifconf) {
if (isset($dhcpifconf['enable']) && isset($Iflist[$dhcpif])) {
$dhcpdenable = true;
break;
}
}
return $dhcpdenable;
}
/* return outside interfaces with a gateway */
function get_interfaces_with_gateway() {
global $config;
$ints = array();
$vfaces = array(
'bridge.?*',
'ppp.?*',
'sl.?*',
'gif.?*',
'faith.?*',
'lo.?*',
'ng.?*',
'vlan.?*',
'pflog.?*',
'pfsync.?*',
'enc.?*',
'tun.?*',
'carp.?*'
);
$ifdescrs = get_interface_list("active","physical",$vfaces);
/* loop interfaces, check config for outbound */
foreach ($ifdescrs as $ifdescr => $ifname) {
$friendly = $ifname['friendly'];
switch ($config['interfaces'][$friendly]['ipaddr']) {
case "dhcp":
case "carpdev-dhcp":
case "pppoe":
case "pptp":
$ints[] = $friendly;
break;
default:
if ($config['interfaces'][$friendly]['pointtopoint'])
$ints[] = $friendly;
else if ($config['interfaces'][$friendly]['gateway'] <> "")
$ints[] = $friendly;
break;
}
}
return $ints;
}
/* return true if interface has a gateway */
function interface_has_gateway($friendly) {
$friendly = strtolower($friendly);
if(in_array($friendly, get_interfaces_with_gateway())) {
return true;
} else {
/* extra check for real interface names if it falls through */
$friendly = convert_real_interface_to_friendly_interface_name($friendly);
return(in_array($friendly, get_interfaces_with_gateway()));
}
}
/****f* pfsense-utils/isAjax
* NAME
* isAjax - reports if the request is driven from prototype
* INPUTS
* none
* RESULT
* true/false
******/
function isAjax() {
return isset ($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
}
//returns interface information
function get_interface_info($ifdescr) {
global $config, $linkinfo, $netstatrninfo;
$ifinfo = array();
/* if list */
$iflist = get_configured_interface_with_descr(false,true);
$found = false;
foreach ($iflist as $if => $ifname) {
if ($ifdescr == $if || $ifdescr == $ifname) {
$ifinfo['hwif'] = $config['interfaces'][$if]['if'];
$ifinfo['if'] = get_real_interface($if);
$found = true;
break;
}
}
if ($found == false)
return;
/* run netstat to determine link info */
unset($linkinfo);
if ($ifinfo['if'] != $ifinfo['hwif'])
$chkif = $ifinfo['hwif'];
else
$chkif = $ifinfo['if'];
exec("/usr/bin/netstat -I {$chkif} -nWb -f link", $linkinfo);
$linkinfo = preg_split("/\s+/", $linkinfo[1]);
if ("{$chkif}*" == $linkinfo[0])
$ifinfo['status'] = "down";
else if ($chkif == $linkinfo[0])
$ifinfo['status'] = "up";
else
$ifinfo['status'] = "down";
if (preg_match("/^enc|^tun|^ppp|^pptp|^ovpn/i", $ifinfo['if'])) {
$ifinfo['inpkts'] = $linkinfo[3];
$ifinfo['outpkts'] = $linkinfo[6];
} else {
$ifinfo['macaddr'] = $linkinfo[3];
$ifinfo['inerrs'] = $linkinfo[5];
$ifinfo['outerrs'] = $linkinfo[8];
$ifinfo['collisions'] = $linkinfo[10];
}
/* Use pfctl for non wrapping 64 bit counters */
/* Pass */
exec("/sbin/pfctl -vvsI -i {$ifinfo['if']}", $pfctlstats);
$pf_in4_pass = preg_split("/ +/ ", $pfctlstats[3]);
$pf_out4_pass = preg_split("/ +/", $pfctlstats[5]);
$in4_pass = $pf_in4_pass[5];
$out4_pass = $pf_out4_pass[5];
$in4_pass_packets = $pf_in4_pass[3];
$out4_pass_packets = $pf_out4_pass[3];
$ifinfo['inbytespass'] = $in4_pass;
$ifinfo['outbytespass'] = $out4_pass;
$ifinfo['inpktspass'] = $in4_pass_packets;
$ifinfo['outpktspass'] = $out4_pass_packets;
/* Block */
$pf_in4_block = preg_split("/ +/", $pfctlstats[4]);
$pf_out4_block = preg_split("/ +/", $pfctlstats[6]);
$in4_block = $pf_in4_block[5];
$out4_block = $pf_out4_block[5];
$in4_block_packets = $pf_in4_block[3];
$out4_block_packets = $pf_out4_block[3];
$ifinfo['inbytesblock'] = $in4_block;
$ifinfo['outbytesblock'] = $out4_block;
$ifinfo['inpktsblock'] = $in4_block_packets;
$ifinfo['outpktsblock'] = $out4_block_packets;
$ifinfo['inbytes'] = $in4_pass + $in4_block;
$ifinfo['outbytes'] = $out4_pass + $out4_block;
$ifinfo['inpkts'] = $in4_pass_packets + $in4_block_packets;
$ifinfo['outpkts'] = $in4_pass_packets + $out4_block_packets;
$ifconfiginfo = "";
unset($ifconfiginfo, $link0);
exec("/sbin/ifconfig " . $ifinfo['if'], $ifconfiginfo);
foreach ($ifconfiginfo as $ici) {
if (preg_match("/inet (\S+)/", $ici, $matches)) {
$ifinfo['ipaddr'] = $matches[1];
}
if (preg_match("/netmask (\S+)/", $ici, $matches)) {
if (preg_match("/^0x/", $matches[1])) {
$ifinfo['subnet'] = long2ip(hexdec($matches[1]));
}
}
if (strpos($ici, 'LINK0') !== false) {
$link0 = "down";
}
}
switch ($config['interfaces'][$if]['ipaddr']) {
/* DHCP? -> see if dhclient is up */
case "dhcp":
/* see if dhclient is up */
if (is_dhcp_running($ifinfo['if']) == true)
$ifinfo['dhcplink'] = "up";
else
$ifinfo['dhcplink'] = "down";
break;
case "carpdev-dhcp":
/* see if dhclient is up */
if (is_dhcp_running($ifinfo['if']) == true)
$ifinfo['dhcplink'] = "up";
else
$ifinfo['dhcplink'] = "down";
break;
/* PPPoE interface? -> get status from virtual interface */
case "pppoe":
unset($linkinfo);
exec("/usr/bin/netstat -I " . $ifinfo['if'] . " -nWb -f link", $linkinfo);
$linkinfo = preg_split("/\s+/", $linkinfo[1]);
if ("{$ifinfo['if']}*" == $linkinfo[0])
$ifinfo['pppoelink'] = "down";
else if ($ifinfo['if'] == $linkinfo[0] && !isset($link0))
/* get PPPoE link status for dial on demand */
$ifinfo['pppoelink'] = "up";
else
$ifinfo['pppoelink'] = "down";
break;
/* PPTP interface? -> get status from virtual interface */
case "pptp":
unset($linkinfo);
exec("/usr/bin/netstat -I " . $ifinfo['if'] . " -nWb -f link", $linkinfo);
$linkinfo = preg_split("/\s+/", $linkinfo[1]);
if ("{$ifinfo['if']}*" == $linkinfo[0])
$ifinfo['pptplink'] = "down";
else if ($ifinfo['if'] == $linkinfo[0] && !isset($link0))
/* get PPTP link status for dial on demand */
$ifinfo['pptplink'] = "up";
else
$ifinfo['pptplink'] = "down";
break;
default:
break;
}
if ($ifinfo['status'] == "up") {
/* try to determine media with ifconfig */
unset($ifconfiginfo);
exec("/sbin/ifconfig " . $ifinfo['hwif'], $ifconfiginfo);
$matches = "";
foreach ($ifconfiginfo as $ici) {
/* don't list media/speed for wireless cards, as it always
displays 2 Mbps even though clients can connect at 11 Mbps */
if (preg_match("/media: .*? \((.*?)\)/", $ici, $matches)) {
$ifinfo['media'] = $matches[1];
} else if (preg_match("/media: Ethernet (.*)/", $ici, $matches)) {
$ifinfo['media'] = $matches[1];
} else if (preg_match("/media: IEEE 802.11 Wireless Ethernet (.*)/", $ici, $matches)) {
$ifinfo['media'] = $matches[1];
}
if (preg_match("/status: (.*)$/", $ici, $matches)) {
if ($matches[1] != "active")
$ifinfo['status'] = $matches[1];
}
if (preg_match("/channel (\S*)/", $ici, $matches)) {
$ifinfo['channel'] = $matches[1];
}
if (preg_match("/ssid (\".*?\"|\S*)/", $ici, $matches)) {
if ($matches[1][0] == '"')
$ifinfo['ssid'] = substr($matches[1], 1, -1);
else
$ifinfo['ssid'] = $matches[1];
}
}
/* lookup the gateway */
if (interface_has_gateway($if))
$ifinfo['gateway'] = get_interface_gateway($if);
}
$bridge = "";
$bridge = link_interface_to_bridge($ifdescr);
if($bridge) {
$bridge_text = `/sbin/ifconfig {$bridge}`;
if(stristr($bridge_text, "blocking") <> false) {
$ifinfo['bridge'] = "blocking - check for ethernet loops";
$ifinfo['bridgeint'] = $bridge;
} else if(stristr($bridge_text, "learning") <> false) {
$ifinfo['bridge'] = "learning";
$ifinfo['bridgeint'] = $bridge;
} else if(stristr($bridge_text, "forwarding") <> false) {
$ifinfo['bridge'] = "forwarding";
$ifinfo['bridgeint'] = $bridge;
}
}
return $ifinfo;
}
//returns cpu speed of processor. Good for determining capabilities of machine
function get_cpu_speed() {
return exec("sysctl hw.clockrate | awk '{ print $2 }'");
}
/* check if the wan interface is up
* Wait for a maximum of 10 seconds
* If the interface is up before then continue
*/
function is_wan_interface_up($interface) {
global $g;
global $config;
$i = 0;
while($i < 10) {
if(get_interface_gateway($interface)) {
return true;
} else {
sleep(1);
}
$i++;
}
return false;
}
function add_hostname_to_watch($hostname) {
if(!is_dir("/var/db/dnscache")) {
mkdir("/var/db/dnscache");
}
if((is_fqdn($hostname)) && (!is_ipaddr($hostname))) {
$domrecords = array();
$domips = array();
exec("host -t A $hostname", $domrecords, $rethost);
if($rethost == 0) {
foreach($domrecords as $domr) {
$doml = explode(" ", $domr);
$domip = $doml[3];
/* fill array with domain ip addresses */
if(is_ipaddr($domip)) {
$domips[] = $domip;
}
}
}
sort($domips);
$contents = "";
if(! empty($domips)) {
foreach($domips as $ip) {
$contents .= "$ip\n";
}
}
file_put_contents("/var/db/dnscache/$hostname", $contents);
}
}
function find_dns_aliases() {
global $config, $g;
foreach((array) $config['aliases']['alias'] as $alias) {
$alias_value = $alias['address'];
$alias_name = $alias['name'];
if(stristr($alias_value, " ")) {
$alias_split = split(" ", $alias_value);
foreach($alias_split as $as) {
if(is_fqdn($as))
add_hostname_to_watch($as);
}
} else {
if(is_fqdn($alias_value))
add_hostname_to_watch($alias_value);
}
}
}
function is_fqdn($fqdn) {
$hostname = false;
if(preg_match("/[-A-Z0-9\.]+\.[-A-Z0-9\.]+/i", $fqdn)) {
$hostname = true;
}
if(preg_match("/\.\./", $fqdn)) {
$hostname = false;
}
if(preg_match("/^\./i", $fqdn)) {
$hostname = false;
}
if(preg_match("/\//i", $fqdn)) {
$hostname = false;
}
return($hostname);
}
function pfsense_default_state_size() {
/* get system memory amount */
$memory = get_memory();
$avail = $memory[0];
/* Be cautious and only allocate 10% of system memory to the state table */
$max_states = (int) ($avail/10)*1000;
return $max_states;
}
function lookup_gateway_ip_by_name($name) {
global $config;
if(is_array($config['gateways'])) {
foreach($config['gateways']['gateway_item'] as $gateway) {
if($gateway['name'] == $name) {
$gatewayip = $gateway['gateway'];
//$interfacegw = $gateway['interface'];
return($gatewayip);
}
}
} else {
return(false);
}
}
function lookup_gateway_monitor_ip_by_name($name) {
global $config;
$gateways_arr = return_gateways_array();
foreach($gateways_arr as $gateway) {
if($gateway['name'] == "$name") {
$monitorip = $gateway['monitor'];
if($monitorip == "")
$monitorip = $gateway['gateway'];
return($monitorip);
}
}
return(false);
}
function lookup_gateway_interface_by_name($name) {
global $config;
$gateways_arr = return_gateways_array();
foreach($gateways_arr as $gateway) {
if($gateway['name'] == "$name") {
$gatewayip = $gateway['gateway'];
$interfacegw = $gateway['interface'];
return($interfacegw);
}
}
return(false);
}
/****f* pfsense-utils/safe_write_file
* NAME
* safe_write_file - Write a file out atomically
* DESCRIPTION
* safe_write_file() Writes a file out atomically by first writing to a
* temporary file of the same name but ending with the pid of the current
* process, them renaming the temporary file over the original.
* INPUTS
* $filename - string containing the filename of the file to write
* $content - string containing the file content to write to file
* $force_binary - boolean denoting whether we should force binary
* mode writing.
* RESULT
* boolean - true if successful, false if not
******/
function safe_write_file($file, $content, $force_binary) {
$tmp_file = $file . "." . getmypid();
$write_mode = $force_binary ? "wb" : "w";
$fd = fopen($tmp_file, $write_mode);
if (!$fd) {
// Unable to open temporary file for writing
return false;
}
if (!fwrite($fd, $content)) {
// Unable to write to temporary file
fclose($fd);
return false;
}
fclose($fd);
if (!rename($tmp_file, $file)) {
// Unable to move temporary file to original
unlink($tmp_file);
return false;
}
return true;
}
function rule_popup($src,$srcport,$dst,$dstport){
global $config;
$aliases_array = array();
if($config['aliases']['alias'] <> "" and is_array($config['aliases']['alias']))
{
$span_begin = "";
$alias_src_span_begin = "";
$alias_src_span_end = "";
$alias_src_port_span_begin = "";
$alias_src_port_span_end = "";
$alias_dst_span_begin = "";
$alias_dst_span_end = "";
$alias_dst_port_span_begin = "";
$alias_dst_port_span_end = "";
$alias_content_text = "";
foreach($config['aliases']['alias'] as $alias_name)
{
$alias_addresses = explode (" ", $alias_name['address']);
$alias_details = explode ("||", $alias_name['detail']);
$alias_objects_with_details = "";
$counter = 0;
foreach($alias_addresses as $alias_ports_address)
{
$alias_objects_with_details .= $alias_addresses[$counter];
$alias_detail_default = strpos ($alias_details[$counter],"Entry added");
if ($alias_details[$counter] != "" && $alias_detail_default === False){
$alias_objects_with_details .=" - " . $alias_details[$counter];
}
$alias_objects_with_details .= " ";
$counter++;
}
//max character length for caption field
$maxlength = 60;
$alias_descr_substr = $alias_name['descr'];
$alias_content_text = htmlspecialchars($alias_objects_with_details);
$alias_caption = htmlspecialchars($alias_descr_substr . ":");
$strlength = strlen ($alias_caption);
if ($strlength >= $maxlength)
$alias_caption = substr($alias_caption, 0, $maxlength) . "...";
$span_begin = "$alias_caption
$alias_content_text
', 'trail', true, 'delay', 0, 'fade', 'both', 'fadeMax', 93, 'styleClass', 'niceTitle');\" onmouseout=\"this.style.color = ''; domTT_mouseout(this, event);\">";
if ($alias_name['name'] == $src)
{
$alias_src_span_begin = $span_begin;
}
if ($alias_name['name'] == $srcport)
{
$alias_src_port_span_begin = $span_begin;
}
if ($alias_name['name'] == $dst)
{
$alias_dst_span_begin = $span_begin;
}
if ($alias_name['name'] == $dstport)
{
$alias_dst_port_span_begin = $span_begin;
}
}
$descriptions = array ();
$descriptions['src'] = $alias_src_span_begin;
$descriptions['srcport'] = $alias_src_port_span_begin;
$descriptions['dst'] = $alias_dst_span_begin;
$descriptions['dstport'] = $alias_dst_port_span_begin;
return $descriptions;
}
}
function download_file_with_progress_bar($url_file, $destination_file, $readbody = 'read_body') {
global $ch, $fout, $file_size, $downloaded;
$file_size = 1;
$downloaded = 1;
/* open destination file */
$fout = fopen($destination_file, "wb");
/*
* Originally by Author: Keyvan Minoukadeh
* Modified by Scott Ullrich to return Content-Length size
*/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_file);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
curl_setopt($ch, CURLOPT_WRITEFUNCTION, $readbody);
curl_setopt($ch, CURLOPT_NOPROGRESS, '1');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, '5');
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($fout)
fclose($fout);
curl_close($ch);
return ($http_code == 200) ? true : $http_code;
}
function read_header($ch, $string) {
global $file_size, $fout;
$length = strlen($string);
$regs = "";
ereg("(Content-Length:) (.*)", $string, $regs);
if($regs[2] <> "") {
$file_size = intval($regs[2]);
}
ob_flush();
return $length;
}
function read_body($ch, $string) {
global $fout, $file_size, $downloaded, $sendto, $static_status, $static_output, $lastseen;
$length = strlen($string);
$downloaded += intval($length);
$downloadProgress = round(100 * (1 - $downloaded / $file_size), 0);
$downloadProgress = 100 - $downloadProgress;
if($lastseen <> $downloadProgress and $downloadProgress < 101) {
if($sendto == "status") {
$tostatus = $static_status . $downloadProgress . "%";
update_status($tostatus);
} else {
$tooutput = $static_output . $downloadProgress . "%";
update_output_window($tooutput);
}
update_progress_bar($downloadProgress);
$lastseen = $downloadProgress;
}
if($fout)
fwrite($fout, $string);
ob_flush();
return $length;
}
/* Compare the current hostname DNS to the DNS cache we made
* if it has changed we return the old records
* if no change we return true */
function compare_hostname_to_dnscache($hostname) {
if(!is_dir("/var/db/dnscache")) {
mkdir("/var/db/dnscache");
}
$hostname = trim($hostname);
if(is_readable("/var/db/dnscache/{$hostname}")) {
$oldcontents = file_get_contents("/var/db/dnscache/{$hostname}");
} else {
$oldcontents = "";
}
if((is_fqdn($hostname)) && (!is_ipaddr($hostname))) {
$domrecords = array();
$domips = array();
exec("host -t A $hostname", $domrecords, $rethost);
if($rethost == 0) {
foreach($domrecords as $domr) {
$doml = explode(" ", $domr);
$domip = $doml[3];
/* fill array with domain ip addresses */
if(is_ipaddr($domip)) {
$domips[] = $domip;
}
}
}
sort($domips);
$contents = "";
if(! empty($domips)) {
foreach($domips as $ip) {
$contents .= "$ip\n";
}
}
}
if(trim($oldcontents) != trim($contents)) {
log_error("DNSCACHE: Found old IP {$oldcontents} and new IP {$contents}");
return ($oldcontents);
} else {
return false;
}
}
?>