$lastseen) if($t <> "") $dns_server_master[] = $t; $lastseen = $t; } return $dns_server_master; } /****f* pfsense-utils/log_error * NAME * log_error - Sends a string to syslog. * INPUTS * $error - string containing the syslog message. * RESULT * null ******/ function log_error($error) { global $g; $page = $_SERVER['SCRIPT_NAME']; syslog(LOG_WARNING, "$page: $error"); if ($g['debug']) syslog(LOG_WARNING, var_dump(debug_backtrace())); return; } /****f* pfsense-utils/return_dir_as_array * NAME * return_dir_as_array - Return a directory's contents as an array. * INPUTS * $dir - string containing the path to the desired directory. * RESULT * $dir_array - array containing the directory's contents. This array will be empty if the path specified is invalid. ******/ function return_dir_as_array($dir) { $dir_array = array(); if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { $canadd = 0; if($file == ".") $canadd = 1; if($file == "..") $canadd = 1; if($canadd == 0) array_push($dir_array, $file); } closedir($dh); } } return $dir_array; } /****f* pfsense-utils/enable_hardware_offloading * NAME * enable_hardware_offloading - Enable a NIC's supported hardware features. * INPUTS * $interface - string containing the physical interface to work on. * RESULT * null * NOTES * This function only supports the fxp driver's loadable microcode. ******/ function enable_hardware_offloading($interface) { global $g, $config; if(stristr($interface,"lnc")) return; /* translate wan, lan, opt -> real interface if needed */ $int = filter_translate_type_to_real_interface($interface); if($int <> "") $interface = $int; $int_family = preg_split("/[0-9]+/", $int); $options = strtolower(`/sbin/ifconfig -m {$interface} | grep capabilities`); $supported_ints = array('fxp'); if (in_array($int_family, $supported_ints)) { if(isset($config['system']['do_not_use_nic_microcode'])) continue; mwexec("/sbin/ifconfig {$interface} link0"); } /* skip vlans for checksumming and polling */ if(stristr($interface, "vlan")) return; if($config['system']['disablechecksumoffloading']) { if(stristr($options, "txcsum") == true) mwexec("/sbin/ifconfig {$interface} -txcsum 2>/dev/null"); if(stristr($options, "rxcsum") == true) mwexec("/sbin/ifconfig {$interface} -rxcsum 2>/dev/null"); } else { if(stristr($options, "txcsum") == true) mwexec("/sbin/ifconfig {$interface} txcsum 2>/dev/null"); if(stristr($options, "rxcsum") == true) mwexec("/sbin/ifconfig {$interface} rxcsum 2>/dev/null"); } /* if the NIC supports polling *AND* it is enabled in the GUI */ if(interface_supports_polling($interface)) { $polling = isset($config['system']['polling']); if($polling) { mwexec("sysctl kern.polling.enable=1"); mwexec("/sbin/ifconfig {$interface} polling 2>/dev/null"); } else { mwexec("sysctl kern.polling.enable=0"); } } return; } /****f* pfsense-utils/interface_supports_polling * NAME * checks to see if an interface supports polling according to man polling * INPUTS * * RESULT * true or false * NOTES * ******/ function interface_supports_polling($iface) { $pattern = '/([a-z].*)[0-9]/'; preg_match($pattern, $iface, $iface2); $interface=$iface2[1]; $supported_ints = array("bge", "dc", "em", "fwe", "fwip", "fxp", "ixgb", "nfe", "vge", "re", "rl", "sf", "sis", "ste", "stge", "vge", "vr", "xl"); if(in_array($interface, $supported_ints)) return true; return false; } /****f* pfsense-utils/is_alias_inuse * NAME * checks to see if an alias is currently in use by a rule * INPUTS * * RESULT * true or false * NOTES * ******/ function is_alias_inuse($alias) { global $g, $config; if($alias == "") return false; /* loop through firewall rules looking for alias in use */ if(is_array($config['filter']['rule'])) foreach($config['filter']['rule'] as $rule) { if($rule['source']['address']) if($rule['source']['address'] == $alias) return true; if($rule['destination']['address']) if($rule['destination']['address'] == $alias) return true; } /* loop through nat rules looking for alias in use */ if(is_array($config['nat']['rule'])) foreach($config['nat']['rule'] as $rule) { if($rule['target'] == $alias) return true; if($rule['external-address'] == $alias) return true; } return false; } /****f* pfsense-utils/is_schedule_inuse * NAME * checks to see if a schedule is currently in use by a rule * INPUTS * * RESULT * true or false * NOTES * ******/ function is_schedule_inuse($schedule) { global $g, $config; if($schedule == "") return false; /* loop through firewall rules looking for schedule in use */ if(is_array($config['filter']['rule'])) foreach($config['filter']['rule'] as $rule) { if($rule['sched'] == $schedule) return true; } return false; } /****f* pfsense-utils/setup_polling_defaults * NAME * sets up sysctls for pollingS * INPUTS * * RESULT * null * NOTES * ******/ function setup_polling_defaults() { global $g, $config; if($config['system']['polling_each_burst']) mwexec("sysctl kern.polling.each_burst={$config['system']['polling_each_burst']}"); if($config['system']['polling_burst_max']) mwexec("sysctl kern.polling.burst_max={$config['system']['polling_burst_max']}"); if($config['system']['polling_user_frac']) mwexec("sysctl kern.polling.user_frac={$config['system']['polling_user_frac']}"); } /****f* pfsense-utils/setup_polling * NAME * sets up polling * INPUTS * * RESULT * null * NOTES * ******/ function setup_polling() { global $g, $config; setup_polling_defaults(); $supported_ints = array('bge', 'dc', 'em', 'fwe', 'fwip', 'fxp', 'ixgb', 'ste', 'nge', 're', 'rl', 'sf', 'sis', 'ste', 'vge', 'vr', 'xl'); /* if list */ $iflist = get_configured_interface_list(); foreach ($iflist as $ifent => $ifname) { $real_interface = convert_friendly_interface_to_real_interface_name($ifname); $ifdevice = substr($real_interface, 0, -1); if(!in_array($ifdevice, $supported_ints)) { continue; } if(isset($config['system']['polling'])) { mwexec("/sbin/ifconfig {$real_interface} polling"); } else { mwexec("/sbin/ifconfig {$real_interface} -polling"); } } } /****f* pfsense-utils/setup_microcode * NAME * enumerates all interfaces and calls enable_hardware_offloading which * enables a NIC's supported hardware features. * INPUTS * * RESULT * null * NOTES * This function only supports the fxp driver's loadable microcode. ******/ function setup_microcode() { /* if list */ $ifdescrs = get_configured_interface_list(); foreach($ifdescrs as $if) enable_hardware_offloading($if); } /****f* pfsense-utils/return_filename_as_array * NAME * return_filename_as_array - Return a file's contents as an array. * INPUTS * $filename - string containing the path to the desired file. * $strip - array of characters to strip - default is '#'. * RESULT * $file - array containing the file's contents. * NOTES * This function strips lines starting with '#' and leading/trailing whitespace by default. ******/ function return_filename_as_array($filename, $strip = array('#')) { if(file_exists($filename)) $file = file($filename); if(is_array($file)) { foreach($file as $line) $line = trim($line); foreach($strip as $tostrip) $file = preg_grep("/^{$tostrip}/", $file, PREG_GREP_INVERT); } return $file; } /****f* pfsense-utils/file_put_contents * NAME * file_put_contents - Wrapper for file_put_contents if it doesn't exist * RESULT * none ******/ if(!function_exists("file_put_contents")) { function file_put_contents($filename, $data) { $fd = fopen($filename,"w"); fwrite($fd, $data); fclose($fd); } } /****f* pfsense-utils/get_carp_status * NAME * get_carp_status - Return whether CARP is enabled or disabled. * RESULT * boolean - true if CARP is enabled, false if otherwise. ******/ function get_carp_status() { /* grab the current status of carp */ $status = `/sbin/sysctl net.inet.carp.allow | cut -d" " -f2`; if(intval($status) == "0") return false; return true; } /****f* pfsense-utils/is_carp_defined * NAME * is_carp_defined - Return whether CARP is detected in the kernel. * RESULT * boolean - true if CARP is detected, false otherwise. ******/ function is_carp_defined() { /* is carp compiled into the kernel and userland? */ $command = "/sbin/sysctl -a | grep carp"; $fd = popen($command . " 2>&1 ", "r"); if(!$fd) { log_error("Warning, could not execute command {$command}"); return 0; } while(!feof($fd)) { $tmp .= fread($fd,49); } fclose($fd); if($tmp == "") return false; else return true; } /****f* pfsense-utils/find_number_of_created_carp_interfaces * NAME * find_number_of_created_carp_interfaces - Return the number of CARP interfaces. * RESULT * $tmp - Number of currently created CARP interfaces. ******/ function find_number_of_created_carp_interfaces($flush = false) { global $carp_interface_count_cache; if (!isset($carp_interface_count_cache) or $flush) { $command = "/sbin/ifconfig | /usr/bin/grep \"carp*:\" | /usr/bin/wc -l"; $fd = popen($command . " 2>&1 ", "r"); if(!$fd) { log_error("Warning, could not execute command {$command}"); return 0; } while(!feof($fd)) { $tmp .= fread($fd,49); } fclose($fd); $carp_interface_count_cache = intval($tmp); } return $carp_interface_count_cache; } function link_carp_interface_to_parent($interface) { global $config; if($interface == "") return; /* if list */ $ifdescrs = get_configured_interface_list(); $carp_int = $interface; $carp_ip = find_interface_ip($interface); $carp_subnet = find_virtual_ip_netmask($carp_ip); $starting_ip = gen_subnet("{$carp_ip}", "{$carp_subnet}"); $carp_ints = ""; foreach ($ifdescrs as $ifdescr => $ifname) { if(interface_has_gateway($ifname)) { $interfaceip = get_interface_ip($ifname); $subnet_bits = get_interface_subnet($ifname); $subnet_ip = gen_subnet("{$interfaceip}", "{$subnet_bits}"); if(ip_in_subnet($carp_ip, "{$subnet_ip}/{$subnet_bits}")) { return $ifname; } } } return $carp_ints; } /****f* pfsense-utils/link_ip_to_carp_interface * NAME * link_ip_to_carp_interface - Find where a CARP interface links to. * INPUTS * $ip * RESULT * $carp_ints ******/ function link_ip_to_carp_interface($ip) { global $config; if($ip == "") return; /* if list */ $ifdescrs = get_configured_interface_list(); $ft = split("\.", $ip); $ft_ip = $ft[0] . "." . $ft[1] . "." . $ft[2] . "."; $carp_ints = ""; $num_carp_ints = find_number_of_created_carp_interfaces(); foreach ($ifdescrs as $ifdescr => $ifname) { for($x=0; $x<$num_carp_ints; $x++) { $carp_int = "carp{$x}"; $carp_ip = find_interface_ip($carp_int); $carp_subnet = find_virtual_ip_netmask($carp_ip); $starting_ip = gen_subnet("{$carp_ip}", "{$carp_subnet}"); if(ip_in_subnet($ip, "{$starting_ip}/{$carp_subnet}")) if(!stristr($carp_ints, $carp_int)) $carp_ints .= " " . $carp_int; } } return $carp_ints; } /****f* pfsense-utils/find_virtual_ip_netmask * NAME * find_virtual_ip_netmask - Finds a virtual ip's subnet mask' * INPUTS * $ip - ip address to locate subnet mask of * RESULT * String containing the command's result. * NOTES * This function returns the command's stdout and stderr. ******/ function find_virtual_ip_netmask($ip) { global $config; foreach($config['virtualip']['vip'] as $vip) { if($ip == $vip['subnet']) return $vip['subnet_bits']; } } /****f* pfsense-utils/exec_command * NAME * exec_command - Execute a command and return a string of the result. * INPUTS * $command - String of the command to be executed. * RESULT * String containing the command's result. * NOTES * This function returns the command's stdout and stderr. ******/ function exec_command($command) { $output = array(); exec($command . ' 2>&1 ', $output); return(implode("\n", $output)); } /* * convert_ip_to_network_format($ip, $subnet): converts an ip address to network form */ function convert_ip_to_network_format($ip, $subnet) { $ipsplit = split('[.]', $ip); $string = $ipsplit[0] . "." . $ipsplit[1] . "." . $ipsplit[2] . ".0/" . $subnet; return $string; } /* * filter_translate_type_to_real_interface($interface): * returns the real hardware interface name for a friendly interface. ie: wan */ function filter_translate_type_to_real_interface($interface) { global $config; if($config['interfaces'][$interface]['if'] <> "") { return $config['interfaces'][$interface]['if']; } else { return $interface; } } /* * get_carp_interface_status($carpinterface): returns the status of a carp ip */ function get_carp_interface_status($carpinterface) { /* basically cache the contents of ifconfig statement to speed up this routine */ global $carp_query; if($carp_query == "") $carp_query = split("\n", `/sbin/ifconfig | /usr/bin/grep carp`); $found_interface = 0; foreach($carp_query as $int) { if($found_interface == 1) { if(stristr($int, "MASTER")) return "MASTER"; if(stristr($int, "BACKUP")) return "BACKUP"; if(stristr($int, "INIT")) return "INIT"; return false; } if(stristr($int, $carpinterface) == true) $found_interface=1; } return; } /* * get_pfsync_interface_status($pfsyncinterface): returns the status of a pfsync */ function get_pfsync_interface_status($pfsyncinterface) { $result = does_interface_exist($pfsyncinterface); if($result <> true) return; $status = exec_command("/sbin/ifconfig {$pfsyncinterface} | /usr/bin/grep \"pfsync:\" | /usr/bin/cut -d\" \" -f5"); return $status; } /* * find_carp_interface($ip): return the carp interface where an ip is defined */ function find_carp_interface($ip) { global $find_carp_ifconfig; if($find_carp_ifconfig == "") { $find_carp_ifconfig = array(); $num_carp_ints = find_number_of_created_carp_interfaces(); for($x=0; $x<$num_carp_ints; $x++) { $find_carp_ifconfig[$x] = exec_command("/sbin/ifconfig carp{$x}"); } } $carps = 0; foreach($find_carp_ifconfig as $fci) { if(stristr($fci, $ip . " ") == true) return "carp{$carps}"; $carps++; } } /* * add_rule_to_anchor($anchor, $rule): adds the specified rule to an anchor */ function add_rule_to_anchor($anchor, $rule, $label) { mwexec("echo " . $rule . " | /sbin/pfctl -a " . $anchor . ":" . $label . " -f -"); } /* * remove_text_from_file * remove $text from file $file */ function remove_text_from_file($file, $text) { global $fd_log; if($fd_log) fwrite($fd_log, "Adding needed text items:\n"); $filecontents = file_get_contents($file); $textTMP = str_replace($text, "", $filecontents); $text = $textTMP; if($fd_log) fwrite($fd_log, $text); $fd = fopen($file, "w"); fwrite($fd, $text); fclose($fd); } /* * add_text_to_file($file, $text): adds $text to $file. * replaces the text if it already exists. */ function add_text_to_file($file, $text, $replace = false) { if(file_exists($file) and is_writable($file)) { $filecontents = file($file); $fout = fopen($file, "w"); $filecontents = array_map('rtrim', $filecontents); array_push($filecontents, $text); if ($replace) $filecontents = array_unique($filecontents); $file_text = implode("\n", $filecontents); fwrite($fout, $file_text); fclose($fout); return true; } else { return false; } } /* * after_sync_bump_adv_skew(): create skew values by 1S */ function after_sync_bump_adv_skew() { global $config, $g; $processed_skew = 1; $a_vip = &$config['virtualip']['vip']; foreach ($a_vip as $vipent) { if($vipent['advskew'] <> "") { $processed_skew = 1; $vipent['advskew'] = $vipent['advskew']+1; } } if($processed_skew == 1) write_config("After synch increase advertising skew"); } /* * get_filename_from_url($url): converts a url to its filename. */ function get_filename_from_url($url) { return basename($url); } /* * update_output_window: update bottom textarea dynamically. */ function update_output_window($text) { global $pkg_interface; $log = ereg_replace("\n", "\\n", $text); if($pkg_interface == "console") { /* too chatty */ } else { echo "\n"; } /* ensure that contents are written out */ ob_flush(); } /* * get_dir: return an array of $dir */ function get_dir($dir) { $dir_array = array(); $d = dir($dir); while (false !== ($entry = $d->read())) { array_push($dir_array, $entry); } $d->close(); return $dir_array; } /* * update_output_window: update top textarea dynamically. */ function update_status($status) { global $pkg_interface; if($pkg_interface == "console") { echo $status . "\n"; } else { echo "\n"; } /* ensure that contents are written out */ ob_flush(); } /* * exec_command_and_return_text_array: execute command and return output */ function exec_command_and_return_text_array($command) { $fd = popen($command . " 2>&1 ", "r"); while(!feof($fd)) { $tmp .= fread($fd,49); } fclose($fd); $temp_array = split("\n", $tmp); return $temp_array; } /* * exec_command_and_return_text: execute command and return output */ function exec_command_and_return_text($command) { return exec_command($command); } /* * exec_command_and_return_output: execute command and update output window dynamically */ function execute_command_return_output($command) { global $fd_log, $pkg_interface; $fd = popen($command . " 2>&1 ", "r"); if($pkg_interface <> "console") { echo "\n"; } $counter = 0; $counter2 = 0; while(!feof($fd)) { $tmp = fread($fd, 50); $tmp1 = ereg_replace("\n","\\n", $tmp); $text = ereg_replace("\"","'", $tmp1); $lasttext = ""; if($lasttext == "..") { $text = ""; $lasttext = ""; $counter=$counter-2; } else { $lasttext .= $text; } if($counter > 51) { $counter = 0; $extrabreak = "\\n"; } else { $extrabreak = ""; $counter++; } if($counter2 > 600) { if($pkg_interface <> "console") { echo "\n"; } $counter2 = 0; } else $counter2++; if($pkg_interface <> "console") { echo "\n"; } } fclose($fd); } /* * update_progress_bar($percent): updates the javascript driven progress bar. */ function update_progress_bar($percent) { global $pkg_interface; if($percent > 100) $percent = 1; if($pkg_interface <> "console") { echo "\n"; } else { echo " {$percent}%"; } } /****f* pfsense-utils/WakeOnLan * NAME * WakeOnLan - Wake a machine up using the wake on lan format/protocol * RESULT * true/false - true if the operation was successful ******/ function WakeOnLan($addr, $mac) { $addr_byte = explode(':', $mac); $hw_addr = ''; for ($a=0; $a < 6; $a++) $hw_addr .= chr(hexdec($addr_byte[$a])); $msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255); for ($a = 1; $a <= 16; $a++) $msg .= $hw_addr; // send it to the broadcast address using UDP $s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); if ($s == false) { log_error("Error creating socket!"); log_error("Error code is '".socket_last_error($s)."' - " . socket_strerror(socket_last_error($s))); } else { // setting a broadcast option to socket: $opt_ret = socket_set_option($s, 1, 6, TRUE); if($opt_ret < 0) log_error("setsockopt() failed, error: " . strerror($opt_ret)); $e = socket_sendto($s, $msg, strlen($msg), 0, $addr, 2050); socket_close($s); log_error("Magic Packet sent ({$e}) to {$addr} MAC={$mac}"); return true; } return false; } /* * gather_altq_queue_stats(): gather altq queue stats and return an array that * is queuename|qlength|measured_packets * NOTE: this command takes 5 seconds to run */ function gather_altq_queue_stats($dont_return_root_queues) { exec("/sbin/pfctl -vvsq", $stats_array); $queue_stats = array(); foreach ($stats_array as $stats_line) { $match_array = ""; if (preg_match_all("/queue\s+(\w+)\s+/",$stats_line,$match_array)) $queue_name = $match_array[1][0]; if (preg_match_all("/measured:\s+.*packets\/s\,\s(.*)\s+\]/",$stats_line,$match_array)) $speed = $match_array[1][0]; if (preg_match_all("/borrows:\s+(.*)/",$stats_line,$match_array)) $borrows = $match_array[1][0]; if (preg_match_all("/suspends:\s+(.*)/",$stats_line,$match_array)) $suspends = $match_array[1][0]; if (preg_match_all("/dropped pkts:\s+(.*)/",$stats_line,$match_array)) $drops = $match_array[1][0]; if (preg_match_all("/measured:\s+(.*)packets/",$stats_line,$match_array)) { $measured = $match_array[1][0]; if($dont_return_root_queues == true) if(stristr($queue_name,"root_") == false) array_push($queue_stats, "{$queue_name}|{$speed}|{$measured}|{$borrows}|{$suspends}|{$drops}"); } } return $queue_stats; } /* * reverse_strrchr($haystack, $needle): Return everything in $haystack up to the *last* instance of $needle. * Useful for finding paths and stripping file extensions. */ function reverse_strrchr($haystack, $needle) { return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) +1 ) : false; } /* * backup_config_section($section): returns as an xml file string of * the configuration section */ function backup_config_section($section) { global $config; $new_section = &$config[$section]; /* generate configuration XML */ $xmlconfig = dump_xml_config($new_section, $section); $xmlconfig = str_replace("", "", $xmlconfig); return $xmlconfig; } /* * backup_vip_config_section($section): returns as an xml file string of * the configuration section */ function backup_vip_config_section() { global $config; $new_section = &$config['virtualip']; foreach($new_section['vip'] as $section) { if($section['mode'] == "proxyarp") { unset($section); } if($section['advskew'] <> "") { $section_val = intval($section['advskew']); $section_val=$section_val+100; if($section_val > 255) $section_val = 255; $section['advskew'] = $section_val; } $temp['vip'][] = $section; } return $temp; } /* * restore_config_section($section, new_contents): restore a configuration section, * and write the configuration out * to disk/cf. */ function restore_config_section($section, $new_contents) { global $config, $g; conf_mount_rw(); $fout = fopen("{$g['tmp_path']}/tmpxml","w"); fwrite($fout, $new_contents); fclose($fout); $section_xml = parse_xml_config($g['tmp_path'] . "/tmpxml", $section); $config[$section] = &$section_xml; unlink($g['tmp_path'] . "/tmpxml"); write_config("Restored {$section} of config file (maybe from CARP partner)"); conf_mount_ro(); return; } /* * merge_config_section($section, new_contents): restore a configuration section, * and write the configuration out * to disk/cf. But preserve the prior * structure if needed */ function merge_config_section($section, $new_contents) { global $config; conf_mount_rw(); $fname = get_tmp_filename(); $fout = fopen($fname, "w"); fwrite($fout, $new_contents); fclose($fout); $section_xml = parse_xml_config($fname, $section); $config[$section] = $section_xml; unlink($fname); write_config("Restored {$section} of config file (maybe from CARP partner)"); conf_mount_ro(); return; } /* * http_post($server, $port, $url, $vars): does an http post to a web server * posting the vars array. * written by nf@bigpond.net.au */ function http_post($server, $port, $url, $vars) { $user_agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)"; $urlencoded = ""; while (list($key,$value) = each($vars)) $urlencoded.= urlencode($key) . "=" . urlencode($value) . "&"; $urlencoded = substr($urlencoded,0,-1); $content_length = strlen($urlencoded); $headers = "POST $url HTTP/1.1 Accept: */* Accept-Language: en-au Content-Type: application/x-www-form-urlencoded User-Agent: $user_agent Host: $server Connection: Keep-Alive Cache-Control: no-cache Content-Length: $content_length "; $errno = ""; $errstr = ""; $fp = fsockopen($server, $port, $errno, $errstr); if (!$fp) { return false; } fputs($fp, $headers); fputs($fp, $urlencoded); $ret = ""; while (!feof($fp)) $ret.= fgets($fp, 1024); fclose($fp); return $ret; } /* * php_check_syntax($code_tocheck, $errormessage): checks $code_to_check for errors */ if (!function_exists('php_check_syntax')){ function php_check_syntax($code_to_check, &$errormessage){ return false; $fout = fopen("/tmp/codetocheck.php","w"); $code = $_POST['content']; $code = str_replace("", "", $code); fwrite($fout, "\n"); fclose($fout); $command = "/usr/local/bin/php -l /tmp/codetocheck.php"; $output = exec_command($command); if (stristr($output, "Errors parsing") == false) { echo "false\n"; $errormessage = ''; return(false); } else { $errormessage = $output; return(true); } } } /* * php_check_filename_syntax($filename, $errormessage): checks the file $filename for errors */ if (!function_exists('php_check_syntax')){ function php_check_syntax($code_to_check, &$errormessage){ return false; $command = "/usr/local/bin/php -l " . $code_to_check; $output = exec_command($command); if (stristr($output, "Errors parsing") == false) { echo "false\n"; $errormessage = ''; return(false); } else { $errormessage = $output; return(true); } } } /* * rmdir_recursive($path,$follow_links=false) * Recursively remove a directory tree (rm -rf path) * This is for directories _only_ */ function rmdir_recursive($path,$follow_links=false) { $to_do = glob($path); if(!is_array($to_do)) $to_do = array($to_do); foreach($to_do as $workingdir) { // Handle wildcards by foreaching. if(file_exists($workingdir)) { if(is_dir($workingdir)) { $dir = opendir($workingdir); while ($entry = readdir($dir)) { if (is_file("$workingdir/$entry") || ((!$follow_links) && is_link("$workingdir/$entry"))) unlink("$workingdir/$entry"); elseif (is_dir("$workingdir/$entry") && $entry!='.' && $entry!='..') rmdir_recursive("$workingdir/$entry"); } closedir($dir); rmdir($workingdir); } elseif (is_file($workingdir)) { unlink($workingdir); } } } return; } /* * get_memory() * returns an array listing the amount of * memory installed in the hardware * [0]real and [1]available */ function get_memory() { if(file_exists("/var/log/dmesg.boot")) { $mem = `cat /var/log/dmesg.boot | grep memory`; $matches = ""; if (preg_match_all("/real memory = .* \((.*) MB/", $mem, $matches)) $real = $matches[1]; if (preg_match_all("/avail memory = .* \((.*) MB/", $mem, $matches)) $avail = $matches[1]; return array($real[0],$avail[0]); } else { $mem = `dmesg -a`; $matches = ""; if (preg_match_all("/real memory = .* \((.*) MB/", $mem, $matches)) $real = $matches[1]; if (preg_match_all("/avail memory = .* \((.*) MB/", $mem, $matches)) $avail = $matches[1]; return array($real[0],$avail[0]); } } /* * safe_mkdir($path, $mode = 0755) * create directory if it doesn't already exist and isn't a file! */ function safe_mkdir($path, $mode=0755) { global $g; if (!is_file($path) && !is_dir($path)) { return @mkdir($path, $mode); } else { return false; } } /* * make_dirs($path, $mode = 0755) * create directory tree recursively (mkdir -p) */ function make_dirs($path, $mode = 0755) { $base = ''; foreach (explode('/', $path) as $dir) { $base .= "/$dir"; if (!is_dir($base)) { if (!@mkdir($base, $mode)) return false; } } return true; } /* * call_pfsense_method(): Call a method exposed by the pfsense.com XMLRPC server. */ function call_pfsense_method($method, $params, $timeout = 0) { global $g, $config; $ip = gethostbyname($g['product_website']); if($ip == $g['product_website']) return false; global $g, $config; $xmlrpc_base_url = $g['xmlrpcbaseurl']; $xmlrpc_path = $g['xmlrpcpath']; $msg = new XML_RPC_Message($method, array(XML_RPC_Encode($params))); $cli = new XML_RPC_Client($xmlrpc_path, $xmlrpc_base_url); $resp = $cli->send($msg, $timeout); if(!$resp) { log_error("XMLRPC communication error: " . $cli->errstr); return false; } elseif($resp->faultCode()) { log_error("XMLRPC request failed with error " . $resp->faultCode() . ": " . $resp->faultString()); return false; } else { return XML_RPC_Decode($resp->value()); } } /* * check_firmware_version(): Check whether the current firmware installed is the most recently released. */ function check_firmware_version($tocheck = "all", $return_php = true) { global $g, $config; $ip = gethostbyname($g['product_website']); if($ip == $g['product_website']) return false; $rawparams = array("firmware" => array("version" => trim(file_get_contents('/etc/version'))), "kernel" => array("version" => trim(file_get_contents('/etc/version_kernel'))), "base" => array("version" => trim(file_get_contents('/etc/version_base'))), "platform" => trim(file_get_contents('/etc/platform')) ); if($tocheck == "all") { $params = $rawparams; } else { foreach($tocheck as $check) { $params['check'] = $rawparams['check']; $params['platform'] = $rawparams['platform']; } } if($config['system']['firmware']['branch']) { $params['branch'] = $config['system']['firmware']['branch']; } if(!$versions = call_pfsense_method('pfsense.get_firmware_version', $params)) { return false; } else { $versions["current"] = $params; } return $versions; } function get_disk_info() { $diskout = ""; exec("/bin/df -h | /usr/bin/grep -w '/' | /usr/bin/awk '{ print $2, $3, $4, $5 }'", $diskout); return explode(' ', $diskout[0]); // $size, $used, $avail, $cap } function run_plugins($directory) { global $config, $g; /* process packager manager custom rules */ $files = return_dir_as_array($directory); if($files <> "") { foreach ($files as $file) { if($file) { $text = file_get_contents($directory . $file); if($text) { if(stristr($file, ".sh") == true) { mwexec($directory . $file . " start"); } else { if(!stristr($file,"CVS")) { if($g['booting'] == true) echo "\t{$file}... "; require_once($directory . $file); } } } } } } } /****f* pfsense-utils/display_top_tabs * NAME * display_top_tabs - display tabs with rounded edges * INPUTS * $text - array of tabs * RESULT * null ******/ function display_top_tabs(& $tab_array) { global $HTTP_SERVER_VARS; global $config; global $g; /* does the user have access to this tab? * master user has access to everything. * if the user does not have access, simply * unset the tab item. */ $tab_temp = array (); foreach ($tab_array as $ta) if(isAllowedPage($ta[2])) $tab_temp[] = $ta; /* // FIXME : if the checks are not good enough // in isAllowedPage, it needs to be // fixed instead of kludging here // TODO: humm what shall we do with pkg_edit.php and pkg.php? if ((strpos($link, "pkg.php")) !== false || (strpos($link, "pkg_edit.php")) !== false) { $pos_equal = strpos($link, "="); $pos_xmlsuffix = strpos($link, ".xml"); // do we match an absolute url including ?xml= foo if(!isAllowedPage($link, $allowed)) $link = substr($link, $pos_equal +1, ($pos_xmlsuffix - $pos_equal +3)); } // next check - what if the basename contains a query string? if ((strpos($link, "?")) !== false) { $pos_qmark = strpos($link, "?"); $link = substr($link, 0, $pos_qmark); } $authorized_text = print_r($allowed, true); if(is_array($authorized)) if (in_array(basename($link), $authorized)) */ unset ($tab_array); $tab_array = & $tab_temp; $tab_active_bg = "#EEEEEE"; $tab_inactive_bg = "#777777"; $nifty_tabs_corners = "#FFF"; $font_color = "white"; /* if tabcontrols.php exist for a theme, allow it to be overriden */ $themename = $config['theme']; $filename = "/usr/local/www/themes/{$themename}/tabcontrols.php"; if(file_exists($filename)) { $eval_code = file_get_contents($filename); eval($eval_code); } $tabcharcount = 0; foreach ($tab_array as $ta) $tabcharcount = $tabcharcount + strlen($ta[0]); // If the character count of the tab names is > 670 // then show a select item dropdown menubox. if($tabcharcount > 82) { echo "Currently viewing: "; echo "\n
"; } else { echo "\n"; } else { echo " | \n"; } $tabscounter++; } echo " |
{$ta[0]}"; echo " "; echo " | \n"; } else { echo ""; echo "{$ta[0]} "; echo " | \n"; } } echo "
\n"; } else { echo " | \n"; } $tabscounter++; } echo " |
"; echo " | \n"; echo "|
{$text} | \n"; else echo "\n"; echo " |
$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; } } ?>