$lastseen)
if($t <> "")
$dns_server_master[] = $t;
$lastseen = $t;
}
return $dns_server_master;
}
/****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 = get_real_interface($interface);
if($int <> "")
$interface = $int;
$int_family = preg_split("/[0-9]+/", $interface);
$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;
if(does_interface_exist($interface))
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("/sbin/ifconfig {$interface} polling 2>/dev/null");
} else {
mwexec("/sbin/ifconfig {$interface} -polling 2>/dev/null");
}
}
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 polling
* 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");
mwexec("/sbin/sysctl kern.polling.idle_poll=1");
} 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/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;
}
/*
* 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;
}
/*
* 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 $carpinterface | grep carp`);
foreach($carp_query as $int) {
if(stristr($int, "MASTER"))
return "MASTER";
if(stristr($int, "BACKUP"))
return "BACKUP";
if(stristr($int, "INIT"))
return "INIT";
}
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;
}
/*
* 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);
}
/*
* 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;
}
/****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) {
if (!is_string($haystack))
return;
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;
}
/*
* 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')){
global $g;
function php_check_syntax($code_to_check, &$errormessage){
return false;
$fout = fopen("{$g['tmp_path']}/codetocheck.php","w");
$code = $_POST['content'];
$code = str_replace("", "", $code);
fwrite($fout, "\n");
fclose($fout);
$command = "/usr/local/bin/php -l {$g['tmp_path']}/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;
}
/*
* 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;
$xmlrpc_base_url = isset($config['system']['altpkgrepo']['enable']) ? $config['system']['altpkgrepo']['xmlrpcbaseurl'] : $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);
// If the ALT PKG Repo has a username/password set, use it.
if($config['system']['altpkgrepo']['username'] &&
$config['system']['altpkgrepo']['password']) {
$username = $config['system']['altpkgrepo']['username'];
$password = $config['system']['altpkgrepo']['password'];
$cli->setCredentials($username, $password);
}
$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
}
/****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;
/* XXX: Use locks?! */
if (file_exists("{$g['tmp_path']}/reloading_all")) {
log_error("WARNING: Recursive call to interfaces sync!");
return;
}
touch("{$g['tmp_path']}/reloading_all");
if($g['debug'])
log_error("reload_interfaces_sync() is starting.");
/* parse config.xml again */
$config = parse_config(true);
/* enable routing */
system_routing_enable();
if($g['debug'])
log_error("Enabling system routing");
if($g['debug'])
log_error("Cleaning up Interfaces");
/* set up interfaces */
interfaces_configure();
/* remove reloading_all trigger */
if($g['debug'])
log_error("Removing {$g['tmp_path']}/reloading_all");
/* start devd back up */
mwexec("/bin/rm {$g['tmp_path']}/reload*");
}
/****f* pfsense-utils/reload_all
* NAME
* reload_all - triggers a reload of all settings
* * INPUTS
* none
* RESULT
* none
******/
function reload_all() {
global $g;
touch("{$g['tmp_path']}/reload_all");
}
/****f* pfsense-utils/reload_interfaces
* NAME
* reload_interfaces - triggers a reload of all interfaces
* INPUTS
* none
* RESULT
* none
******/
function reload_interfaces() {
global $g;
touch("{$g['tmp_path']}/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;
/* XXX: Use locks?! */
if (file_exists("{$g['tmp_path']}/reloading_all")) {
log_error("WARNING: Recursive call to reload all sync!");
return;
}
touch("{$g['tmp_path']}/reloading_all");
/* 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();
/* enable routing */
system_routing_enable();
/* set up interfaces */
interfaces_configure();
/* start dyndns service */
services_dyndns_configure();
/* configure cron service */
configure_cron();
/* start the NTP client */
system_ntp_configure();
/* sync pw database */
conf_mount_rw();
unlink_if_exists("/etc/spwd.db.tmp");
mwexec("/usr/sbin/pwd_mkdb -d /etc/ /etc/master.passwd");
conf_mount_ro();
/* restart sshd */
@touch("{$g['tmp_path']}/start_sshd");
/* restart webConfigurator if needed */
touch("{$g['tmp_path']}/restart_webgui");
mwexec("/bin/rm {$g['tmp_path']}/reload*");
}
function auto_login() {
global $config;
if(isset($config['system']['disableconsolemenu']))
return;
$gettytab = file_get_contents("/etc/gettytab");
$getty_split = split("\n", $gettytab);
conf_mount_rw();
$fd = false;
while (!$fd) {
$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") or stristr($tty, "ttyu0")) {
if(isset($config['system']['enableserial'])) {
fwrite($fd, "ttyu0 \"/usr/libexec/getty bootupcli\" dialup on secure\n");
} else {
fwrite($fd, "ttyu0 \"/usr/libexec/getty bootupcli\" dialup off secure\n");
}
} else {
fwrite($fd, $tty . "\n");
}
}
fclose($fd);
auto_login();
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;
}
/* 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;
}
/* Compute the total uptime from the ppp uptime log file in the conf directory */
/* Written by: gnoahb@gmail.com */
function get_ppp_uptime($serialport){
if (file_exists("/conf/ppp-up.".$serialport . ".log")){
$saved_time = file_get_contents("/conf/ppp-up." . $serialport . ".log");
$uptime_data = explode("\n",$saved_time);
$sec=0;
$min=0;
$hrs=0;
foreach($uptime_data as $upt) {
$time = substr($upt, 1 + strpos($upt, " "));
if ($time != "00:00:00"){
$hms = explode(":",$time);
$hrs += $hms[0];
$min += $hms[1];
$sec += $hms[2];
}
}
if ($sec != 0){
$min += floor($sec/60);
$sec %= 60;
}
if($min !=0){
$hrs += floor($min/60);
$min %= 60;
}
$total_time = $hrs.":".$min.":".$sec;
return $total_time;
} else {
$total_time = "No session history data found!";
return $total_time;
}
}
//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'];
if($config['interfaces'][$if]['serialport'])
$ifinfo['hwif'] = get_real_interface($if);
$ifinfo['if'] = get_real_interface($if);
$found = true;
break;
}
}
if ($found == false)
return;
/* run netstat to determine link info */
unset($linkinfo);
$chkif = $ifinfo['if'];
exec("/usr/bin/netstat -I {$chkif} -nWb -f link", $linkinfo);
$linkinfo = preg_split("/\s+/", $linkinfo[1]);
$ifinfotmp = pfSense_get_interface_addresses($chkif);
$ifinfo['status'] = $ifinfotmp['status'];
$ifinfo['macaddr'] = $ifinfotmp['macaddr'];
$ifinfo['ipaddr'] = $ifinfotmp['ipaddr'];
$ifinfo['subnet'] = $ifinfotmp['subnet'];
if (isset($ififnotmp['link0']))
$link0 = "down";
if (preg_match("/^enc|^tun|^ppp|^pptp|^ovpn/i", $chkif)) {
$ifinfo['inpkts'] = $linkinfo[3];
$ifinfo['outpkts'] = $linkinfo[6];
} else {
$ifinfo['inerrs'] = $linkinfo[5];
$ifinfo['outerrs'] = $linkinfo[9];
$ifinfo['collisions'] = $linkinfo[11];
}
/* Use pfctl for non wrapping 64 bit counters */
/* Pass */
exec("/sbin/pfctl -vvsI -i {$chkif}", $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 = "";
switch ($config['interfaces'][$ifdescr]['ipaddr']) {
/* DHCP? -> see if dhclient is up */
case "dhcp":
case "carpdev-dhcp":
/* see if dhclient is up */
if (find_dhclient_process($ifinfo['if']) <> "")
$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;
/* PPP interface? -> get uptime for this session and cumulative uptime from the persistant log file in conf */
case "":
if ($config['interfaces'][$if]['serialport']){
$dev = $config['interfaces'][$if]['serialport'];
if (file_exists("/dev/{$dev}")){
$ifinfo['ppplink'] = $dev;
if (file_exists("/var/run/{$dev}.sock") && file_exists("/var/run/{$dev}.if")){
$ifinfo['ppp_uptime'] = `/usr/sbin/pppctl /var/run/$dev.sock show physical | grep 'Connect time' | cut -c 15-`;
} //else if (`/sbin/ifconfig
$ifinfo['missing_device'] = 0;
}
else{
$ifinfo['ppplink'] = $dev . " device not present! Is the modem attached to the system?";
$ifinfo['missing_device'] = 1;
}
// Calculate cumulative uptime for PPP link. Useful for connections that have per minute/hour contracts so you don't go over!
$ifinfo['ppp_uptime_accumulated'] = get_ppp_uptime($dev);
}
break;
default:
break;
}
if ($ifinfo['status'] == "up") {
/* try to determine media with ifconfig */
unset($ifconfiginfo);
exec("/sbin/ifconfig " . $ifinfo['if'], $ifconfiginfo);
$wifconfiginfo = array();
if(is_interface_wireless($ifdescr)) {
exec("/sbin/ifconfig {$ifinfo['if']} list sta", $wifconfiginfo);
array_shift($wifconfiginfo);
}
$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($ifinfo['status'] == "running")
$ifinfo['status'] = "up";
}
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];
}
}
foreach($wifconfiginfo as $ici) {
$elements = preg_split("/[ ]+/i", $ici);
if ($elements[0] != "") {
$ifinfo['bssid'] = $elements[0];
}
if ($elements[3] != "") {
$ifinfo['rate'] = $elements[3];
}
if ($elements[4] != "") {
$ifinfo['rssi'] = $elements[4];
}
}
/* 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 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;
}
/* 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)) {
if($g['debug']) {
log_error("DNSCACHE: Found old IP {$oldcontents} and new IP {$contents}");
}
return ($oldcontents);
} else {
return false;
}
}
/*
* load_glxsb() - Load the glxsb crypto module if enabled in config.
*/
function load_glxsb() {
global $config, $g;
$is_loaded = `/sbin/kldstat | /usr/bin/grep -c glxsb`;
if (isset($config['system']['glxsb_enable']) && ($is_loaded == 0)) {
mwexec("/sbin/kldload glxsb");
}
}
/****f* pfsense-utils/isvm
* NAME
* isvm
* INPUTS
* none
* RESULT
* returns true if machine is running under a virtual environment
******/
function isvm() {
$virtualenvs = array("vmware", "parallels", "qemu", "bochs", "plex86");
$bios_vendor = strtolower(`/bin/kenv | /usr/bin/grep "bios.vendor" | /usr/bin/cut -d"=" -f2`);
if(in_array($bios_vendor, $virtualenvs))
return true;
else
return false;
}
function get_freebsd_version() {
$version = trim(`/usr/bin/uname -r | /usr/bin/cut -d'.' -f1`);
return $version;
}
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;
}
/*
* 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();
}
/*
* 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();
}
/*
* 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}%";
}
}
/* Split() is being DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged. */
if(!function_exists("split")) {
function split($seperator, $haystack, $limit = null) {
return preg_split($seperator, $haystack, $limit);
}
}
function update_alias_names_upon_change($section, $subsection, $fielda, $fieldb, $new_alias_name, $origname) {
global $g, $config, $pconfig, $debug;
if(!$origname)
return;
if($debug) $fd = fopen("{$g['tmp_path']}/print_r", "a");
if($debug) fwrite($fd, print_r($pconfig, true));
if($fieldb) {
if($debug) fwrite($fd, "fieldb exists\n");
for ($i = 0; isset($config["$section"]["$subsection"][$i]["$fielda"]); $i++) {
if($debug) fwrite($fd, "$i\n");
if($config["$section"]["$subsection"][$i]["$fielda"]["$fieldb"] == $origname) {
if($debug) fwrite($fd, "Setting old alias value $origname to $new_alias_name\n");
$config["$section"]["$subsection"][$i]["$fielda"]["$fieldb"] = $new_alias_name;
}
}
} else {
if($debug) fwrite($fd, "fieldb does not exist\n");
for ($i = 0; isset($config["$section"]["$subsection"][$i]["$fielda"]); $i++) {
if($config["$section"]["$subsection"][$i]["$fielda"] == $origname) {
$config["$section"]["$subsection"][$i]["$fielda"] = $new_alias_name;
if($debug) fwrite($fd, "Setting old alias value $origname to $new_alias_name\n");
}
}
}
if($debug) fclose($fd);
}
?>