\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, $debug;
$shutdown_webgui_needed = false;
touch("{$g['tmp_path']}/reloading_all");
if($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'];
$lan_if = $config['interfaces']['lan']['if'];
if($debug)
log_error("Cleaning up Interfaces");
/* build an array of interfaces to work with */
$iflist = array("lan" => "LAN", "wan" => "WAN");
for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++)
$iflist['opt' . $i] = "opt{$i}";
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($debug)
log_error("Downing and deleting $ifname_real - $ifname");
mwexec("/sbin/ifconfig {$ifname_real} down");
mwexec("/sbin/ifconfig {$ifname_real} delete");
}
/* set up VLAN virtual interfaces */
if($debug)
log_error("Configuring VLANS");
interfaces_vlan_configure();
/* set up LAN interface */
if($debug)
log_error("Configuring LAN");
interfaces_lan_configure();
/* set up WAN interface */
if($debug)
log_error("Configuring WAN");
interfaces_wan_configure();
/* set up Optional interfaces */
if($debug)
log_error("Configuring optional interfaces");
interfaces_optional_configure();
/* set up static routes */
if($debug)
log_error("Configuring system Routing");
system_routing_configure();
/* enable routing */
if($debug)
log_error("Enabling system routing");
system_routing_enable();
/* setup captive portal if needed */
if($debug)
log_error("Configuring Captive portal");
captiveportal_configure();
/* bring up carp interfaces */
if($debug)
log_error("Configuring CARP");
interfaces_carp_configure();
/* bring up carp interfaces*/
if($debug)
log_error("Bringing up CARP interfaces");
interfaces_carp_bring_up_final();
/* 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($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/sync_webgui_passwords
* NAME
* sync_webgui_passwords - syncs webgui and ssh passwords
* INPUTS
* none
* RESULT
* none
******/
function sync_webgui_passwords() {
global $config, $g, $groupindex, $userindex;
conf_mount_rw();
$fd = fopen("{$g['varrun_path']}/htpasswd", "w");
if (!$fd) {
log_error("Error: cannot open htpasswd in sync_webgui_passwords().\n");
return 1;
}
/* loop through custom users and add "virtual" entries */
if ($config['system']['user']) {
foreach ($config['system']['user'] as $user)
fwrite($fd, "{$user['name']}:{$user['password']}\n");
}
fclose($fd);
chmod("{$g['varrun_path']}/htpasswd", 0600);
if ($config['system']['user']) {
$root =& getUNIXRoot();
$crypted_pw = &$root['password'];
}
if (!$crypted_pw) {
log_error("Error: cannot determine root pwd in sync_webgui_passwords().\nRoot user struct follows:\n");
if(!$root)
log_error("Unable to determine root user" . print_r($root, true));
log_error("Testing whether your system has the necessary users... ");
empty($config['system']['user']) ? log_error("users are missing.\n") : log_error("users found.\n");
return 1;
}
if(file_exists("/etc/pwd.db.tmp"))
unlink("/etc/pwd.db.tmp");
mwexec("/usr/sbin/pwd_mkdb -d /etc -p /etc/master.passwd");
mwexec("/usr/sbin/pwd_mkdb -p /etc/master.passwd");
/* sync root */
$fd = popen("/usr/sbin/pw usermod -n root -H 0", "w");
fwrite($fd, $crypted_pw);
pclose($fd);
mwexec("/usr/sbin/pw usermod -n root -s /bin/sh");
/* and again for shell users */
/* only users with hasShellAccess() == true should be synced */
if($config['system']['user']) {
$home_base = $g['platform'] == "pfSense" ? "/home" : "/var/home";
if(! is_dir($home_base))
mkdir($home_base, 0755);
foreach($config['system']['user'] as $user) {
if (hasShellAccess($user['name']) || isAllowedToCopyFiles($user['name'])) {
$home = hasShellAccess($user['name']) ? "{$home_base}/{$user['name']}" : "{$home_base}/scponly";
$shell = isAllowedToCopyFiles($user['name']) ? "/usr/local/bin/scponly" : "/etc/rc.initial";
if (isAllowedToCopyFiles($user['name']))
$user['groupname'] = "scponly";
$fd = popen("/usr/sbin/pw groupshow -n {$user['groupname']} 2>&1", "r");
$pwread = fgets($fd, 4096);
pclose($fd);
if (strpos($pwread, "unknown group") !== false) {
$groupname = $user['groupname'];
$group = $config['system']['group'][$groupindex[$groupname]];
if (isset($group) && is_array($group)) {
$fd = popen("/usr/sbin/pw groupadd -g {$group['gid']} -n {$group['name']}", "r");
pclose($fd);
} elseif (isAllowedToCopyFiles($user['name'])) {
$fd = popen("/usr/sbin/pw groupadd -g 100 -n scponly", "r");
pclose($fd);
}
}
$fd = popen("/usr/sbin/pw usershow -n {$user['name']} 2>&1", "r");
$pwread = fgets($fd, 4096);
pclose($fd);
isSystemAdmin($user['name']) ? $group = "wheel" : $group = "staff";
if (strpos($pwread, "no such user") === false) {
$fd = popen("/usr/sbin/pw usermod -n {$user['name']} -g ${user['groupname']} -G {$group} -H 0", "w");
fwrite($fd, $user['password']);
pclose($fd);
} else {
$fd = popen("/usr/sbin/pw useradd -u {$user['uid']} -n {$user['name']} -c '{$user['fullname']}' -g ${user['groupname']} -G {$group} -H 0", "w");
fwrite($fd, $user['password']);
pclose($fd);
}
/* common user related operations */
mwexec("/usr/sbin/pw usermod -n {$user['name']} -s {$shell}");
if(! is_dir($home)) mkdir($home, 0755);
mwexec("/usr/sbin/pw usermod -n {$user['name']} -d {$home} -m");
if (isAllowedToCopyFiles($user['name'])) {
mwexec("/usr/sbin/pw usermod -n {$user['name']} -g scponly");
}
if (file_exists("{$home_base}/scponly"))
mwexec("chmod 0660 {$home_base}/scponly");
if(isset($config['system']['ssh']['sshdkeyonly']) && ! isAllowedToCopyFiles($user['name'])) {
create_authorized_keys($user['name'], $home);
}
}
}
}
mwexec("/usr/sbin/pwd_mkdb -d /etc -p /etc/master.passwd");
mwexec("/usr/sbin/pwd_mkdb -p /etc/master.passwd");
conf_mount_ro();
}
/****f* pfsense-utils/cleanup_opt_interfaces_after_removal
* NAME
* cleanup_opt_interfaces_after_removal - renumber interfaces after removing
* * INPUTS
* optional interface number
* RESULT
* none
******/
function cleanup_opt_interfaces_after_removal($opt_interface_num) {
/* move all the interfaces up. for example:
* opt1 --> opt1
* opt2 --> delete
* opt3 --> opt2
* opt4 --> opt3
*/
global $g, $config;
config_lock();
conf_mount_rw();
unlink_if_exists("{$g['tmp_path']}/config.cache");
$config_file = file_get_contents("/cf/conf/config.xml");
/* loop through and reassign deleted items */
for ($i = 500; isset ($config['interfaces']['opt' . $i]); $i--) {
if ($i < $opt_interface_num)
break;
if ($i == $opt_interface_num) {
/* item should be deleted */
str_replace("opt" . $i, "optXXXX", $config_file);
}
}
/* loop through and reassign optional items */
for ($i = 500; isset ($config['interfaces']['opt' . $i]); $i--) {
if ($i < $opt_interface_num)
break;
/* replace opt$i with $i -1 */
str_replace("opt" . $i, "opt" . ($i -1), $config_file);
}
$fd = fopen("/cf/conf/config.xml", "w");
fwrite($fd, $config_file);
fclose($fd);
$config = parse_config(true);
/* loop through and delete old rules */
$num_rules = count($config['filter']['rule']);
for($x = $num_rules; $x > 0; $x--) {
if($config['filter']['rule'][$x])
if($config['filter']['rule'][$x]['interface'] == "optXXXX")
unset($config['filter']['rule'][$x]['interface']);
}
$num_rules = count($config['nat']['advancedoutbound']['rule']);
for($x = $num_rules; $x > 0; $x--) {
if($config['nat']['advancedoutbound']['rule'][$x])
if($config['nat']['advancedoutbound']['rule'][$x]['interface'] == "optXXXX")
unset($config['nat']['advancedoutbound']['rule'][$x]['interface']);
}
$num_rules = count($config['nat']['rule']);
for($x = $num_rules; $x > 0; $x--) {
if($config['nat']['rule'][$x])
if($config['nat']['rule'][$x]['interface'] == "optXXXX")
unset($config['nat']['rule'][$x]['interface']);
}
conf_mount_ro();
config_unlock();
return true;
}
/****f* pfsense-utils/get_number_of_wan_netgraph_interfaces_needed
* NAME
* get_number_of_wan_netgraph_interfaces_needed - returns the
* amount of netgraph interfaces needed for system wans
* * INPUTS
* none
* RESULT
* number of needed netgraph (ng) interfaces
******/
function get_number_of_wan_netgraph_interfaces_needed() {
global $config, $g;
/* build an array of interfaces to work with */
$iflist = array("wan" => "WAN");
for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++)
$iflist['opt' . $i] = $config['interfaces']['opt' . $i]['descr'];
$ng_interfaces_needed = 0;
foreach ($iflist as $ifent => $ifname) {
if($config['interfaces'][$ifname]['ipaddr'] == "pppoe") {
$ng_interfaces_needed++;
}
}
return $ng_interfaces_needed;
}
function get_netgaph_interface_assignment($friendly_interface) {
global $config, $g;
/* build an array of interfaces to work with */
$iflist = array("wan" => "WAN");
for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++)
$iflist['opt' . $i] = $config['interfaces']['opt' . $i]['descr'];
$ng_interfaces_needed = 0;
$ng_interfaces_number = 0;
foreach ($iflist as $ifent => $ifname) {
if($config['interfaces'][$ifname]['ipaddr'] == "pppoe") {
$ng_interfaces_number++;
}
if($friendly_interface == $ifname)
break;
}
return $ng_interfaces_number;
}
/****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();
/* delete all old interface information */
$iflist = split(" ", str_replace("\n", "", `/sbin/ifconfig -l`));
$wan_if = $config['interfaces']['wan']['if'];
$lan_if = $config['interfaces']['lan']['if'];
/* build an array of interfaces to work with */
$iflist = array("lan" => "LAN", "wan" => "WAN");
for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++)
$iflist['opt' . $i] = "opt{$i}";
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 VLAN virtual interfaces */
interfaces_vlan_configure();
/* set up LAN interface */
interfaces_lan_configure();
/* set up WAN interface */
interfaces_wan_configure();
/* set up Optional interfaces */
interfaces_optional_configure();
/* bring up carp interfaces */
interfaces_carp_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();
/* bring up carp interfaces*/
interfaces_carp_bring_up_final();
/* 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 convert_friendly_interface_to_friendly_descr($interface) {
global $config;
/* attempt to resolve interface to friendly descr */
if($config['interfaces'][$interface]['descr'])
return $config['interfaces'][$interface]['descr'];
$tmp = convert_real_interface_to_friendly_descr($interface);
/* could not resolve, return back what was passed */
return $interface;
}
function convert_real_interface_to_friendly_descr($interface) {
global $config;
if($interface == $config['interfaces']['wan']['if'])
return "wan";
if($interface == $config['interfaces']['lan']['if'])
return "lan";
/* attempt to resolve interface to friendly descr */
$friendly_int = convert_real_interface_to_friendly_interface_name($interface);
if($config['interfaces'][$friendly_int]['descr'])
return $config['interfaces'][$friendly_int]['descr'];
/* could not resolve, return back what was passed */
return $interface;
}
function enable_rrd_graphing() {
global $config, $g;
if($g['booting'])
echo "Generating RRD graphs...";
$rrddbpath = "/var/db/rrd/";
$rrdgraphpath = "/usr/local/www/rrd";
$traffic = "-traffic.rrd";
$packets = "-packets.rrd";
$states = "-states.rrd";
$quality = "-quality.rrd";
$wireless = "-wireless.rrd";
$queues = "-queues.rrd";
$queuesdrop = "-queuesdrop.rrd";
$spamd = "-spamd.rrd";
$proc = "-processor.rrd";
$rrdtool = "/usr/local/bin/rrdtool";
$netstat = "/usr/bin/netstat";
$awk = "/usr/bin/awk";
$tar = "/usr/bin/tar";
$pfctl = "/sbin/pfctl";
$php = "/usr/local/bin/php";
$top = "/usr/bin/top";
$spamd_gather = "/usr/local/bin/spamd_gather_stats.php";
$ifconfig = "/sbin/ifconfig";
$rrdtrafficinterval = 60;
$rrdqualityinterval = 60;
$rrdwirelessinterval = 60;
$rrdqueuesinterval = 60;
$rrdqueuesdropinterval = 60;
$rrdpacketsinterval = 60;
$rrdstatesinterval = 60;
$rrdspamdinterval = 60;
$rrdlbpoolinterval = 60;
$rrdprocinterval = 60;
$trafficvalid = $rrdtrafficinterval * 2;
$qualityvalid = $rrdqualityinterval * 2;
$wirelessvalid = $rrdwirelessinterval * 2;
$queuesvalid = $rrdqueuesinterval * 2;
$queuesdropvalid = $rrdqueuesdropinterval * 2;
$packetsvalid = $rrdpacketsinterval * 2;
$statesvalid = $rrdstatesinterval*2;
$spamdvalid = $rrdspamdinterval * 2;
$lbpoolvalid = $rrdlbpoolinterval * 2;
$procvalid = $rrdlbpoolinterval * 2;
/* Asume GigE for now */
$downstream = 125000000;
$upstream = 125000000;
$rrdrestore = "";
$rrdreturn = "";
if (isset ($config['rrd']['enable'])) {
/* create directory if needed */
if (!is_dir("$rrddbpath")) {
mkdir("$rrddbpath", 0755);
}
if ($g['booting']) {
if ($g['platform'] != "pfSense") {
/* restore the databases, if we have one */
if (file_exists("{$g['cf_conf_path']}/rrd.tgz")) {
exec("cd /;LANG=C /usr/bin/tar -xzf {$g['cf_conf_path']}/rrd.tgz", $rrdrestore, $rrdreturn);
if((int)$rrdrestore <> 0) {
log_error("RRD restore failed exited with $rrdreturn, the error is: $rrdrestore[0]\n");
}
}
}
}
/* db update script */
$rrdupdatesh = "#!/bin/sh\n";
$rrdupdatesh .= "\n";
$rrdupdatesh .= "counter=1\n";
$rrdupdatesh .= "while [ \"\$counter\" -ne 0 ]\n";
$rrdupdatesh .= "do\n";
$rrdupdatesh .= "";
$i = 0;
$vfaces = array (
"vlan.?*",
"enc.?*"
);
$ifdescrs = get_interface_list(true, true, $vfaces);
$ifdescrs['enc0']['friendly'] = "ipsec";
$ifdescrs['enc0']['descr'] = "IPSEC";
$ifdescrs['enc0']['up'] = true;
foreach ($ifdescrs as $realif => $ifdescr) {
$ifname = $ifdescr['friendly'];
$state = $ifdescr['up'];
/* skip interfaces that do not have a friendly name */
if ("$ifname" == "") {
continue;
}
/* or are down */
if (!$state) {
continue;
}
/* TRAFFIC, set up the rrd file */
if (!file_exists("$rrddbpath$ifname$traffic")) {
/* create rrd file if it does not exist */
log_error("Create RRD database $rrddbpath$ifname$traffic");
$rrdcreate = "$rrdtool create $rrddbpath$ifname$traffic --step $rrdtrafficinterval ";
$rrdcreate .= "DS:in:COUNTER:$trafficvalid:0:$downstream ";
$rrdcreate .= "DS:out:COUNTER:$trafficvalid:0:$upstream ";
$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:720:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:1440:1500 ";
$rrdcreateoutput = array();
$rrdcreatereturn = "";
$rrdcreatel = exec("$rrdcreate 2>&1", $rrdcreateoutput, $rrdcreatereturn);
if ($rrdcreatereturn != 0) {
log_error("RRD create failed exited with $rrdcreatereturn, the
error is: $rrdcreateoutput[0]\n");
}
}
/* enter UNKNOWN values in the RRD so it knows we rebooted. */
if($g['booting']) {
exec("$rrdtool update $rrddbpath$ifname$traffic N:U:U");
}
$rrdupdatesh .= "\n";
$rrdupdatesh .= "# polling traffic for interface $ifname $realif \n";
$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$traffic N:\\\n";
if(! is_macaddr(get_interface_mac($realif))) {
$rrdupdatesh .= "`$netstat -nbf link -I {$realif} | $awk '{getline 2;print \$6 \":\" \$9}'`\n";
} else {
$rrdupdatesh .= "`$netstat -nbf link -I {$realif} | $awk '{getline 2;print \$7 \":\" \$10}'`\n";
}
/* PACKETS, set up the rrd file */
if (!file_exists("$rrddbpath$ifname$packets")) {
/* create rrd file if it does not exist */
log_error("Create RRD database $rrddbpath$ifname$packets");
$rrdcreate = "$rrdtool create $rrddbpath$ifname$packets --step $rrdpacketsinterval ";
$rrdcreate .= "DS:in:COUNTER:$packetsvalid:0:$downstream ";
$rrdcreate .= "DS:out:COUNTER:$packetsvalid:0:$upstream ";
$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:720:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:1440:1500 ";
$rrdcreatel = exec("$rrdcreate 2>&1", $rrdcreateoutput, $rrdcreatereturn);
if ($rrdcreatereturn != 0) {
log_error("RRD create failed exited with $rrdcreatereturn, the
error is: $rrdcreateoutput[0]\n");
}
}
/* enter UNKNOWN values in the RRD so it knows we rebooted. */
if($g['booting']) {
exec("$rrdtool update $rrddbpath$ifname$packets N:U:U");
}
$rrdupdatesh .= "\n";
$rrdupdatesh .= "# polling packets for interface $ifname $realif \n";
$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$packets N:\\\n";
if(! is_macaddr(get_interface_mac($realif))) {
$rrdupdatesh .= "`$netstat -nbf link -I {$realif} | $awk '{getline 2;print \$4 \":\" \$7}'`\n";
} else {
$rrdupdatesh .= "`$netstat -nbf link -I {$realif} | $awk '{getline 2;print \$5 \":\" \$8}'`\n";
}
/* if an alternative gateway is defined, use it */
if ($config['interfaces'][$ifname]['use_rrd_gateway'] <> "") {
$gatewayip = get_interface_gateway($ifname);
$monitorip = $config['interfaces'][$ifname]['use_rrd_gateway'];
mwexec("/sbin/route add -host {$monitorip} {$gatewayip} 1> /dev/null 2>&1");
} else {
$monitorip = get_interface_gateway($ifname);
}
$numpings = 5;
$btick = '`';
if($monitorip <> "") {
/* QUALITY, create link quality database */
if (!file_exists("$rrddbpath$ifname$quality")) {
/* create rrd file if it does not exist */
log_error("Create RRD database $rrddbpath$ifname$quality");
$rrdcreate = "$rrdtool create $rrddbpath$ifname$quality --step $rrdqualityinterval ";
$rrdcreate .= "DS:loss:GAUGE:$qualityvalid:0:100 ";
$rrdcreate .= "DS:roundtrip:GAUGE:$qualityvalid:0:10000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:720:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:1440:1500 ";
$rrdcreatel = exec("$rrdcreate 2>&1", $rrdcreateoutput, $rrdcreatereturn);
if ($rrdcreatereturn != 0) {
log_error("RRD create failed exited with $rrdcreatereturn, the error is: $rrdcreateoutput[0]\n");
}
}
/* enter UNKNOWN values in the RRD so it knows we rebooted. */
if($g['booting']) {
exec("$rrdtool update $rrddbpath$ifname$quality N:U:U");
}
/* the ping test function. We call this on the last line */
$rrdupdatesh .= "get_quality_stats_{$ifname} () {\n";
$rrdupdatesh .= " packetloss_{$ifname}=100\n";
$rrdupdatesh .= " roundtrip_{$ifname}=0\n";
$rrdupdatesh .= " local out_{$ifname}\n";
$rrdupdatesh .= " out_{$ifname}=$btick ping -c $numpings -q $monitorip $btick\n";
$rrdupdatesh .= " if [ $? -eq 0 ]; then\n";
$rrdupdatesh .= " packetloss_{$ifname}=$btick echo \$out_{$ifname} | cut -f18 -d' ' | cut -c -1 $btick\n";
$rrdupdatesh .= " roundtrip_{$ifname}=$btick echo \$out_{$ifname} | cut -f24 -d' ' | cut -f2 -d'/' $btick\n";
$rrdupdatesh .= " fi\n";
$rrdupdatesh .= " $rrdtool update $rrddbpath$ifname$quality N:\$packetloss_{$ifname}:\$roundtrip_{$ifname}\n";
$rrdupdatesh .= "}\n\n";
$rrdupdatesh .= "get_quality_stats_{$ifname} &\n\n";
}
/* WIRELESS, set up the rrd file */
if($config['interfaces'][$ifname]['wireless']['mode'] == "bss") {
if (!file_exists("$rrddbpath$ifname$wireless")) {
/* create rrd file if it does not exist */
log_error("Create RRD database $rrddbpath$ifname$wireless");
$rrdcreate = "$rrdtool create $rrddbpath$ifname$wireless --step $rrdwirelessinterval ";
$rrdcreate .= "DS:snr:GAUGE:$wirelessvalid:0:1000 ";
$rrdcreate .= "DS:rate:GAUGE:$wirelessvalid:0:1000 ";
$rrdcreate .= "DS:channel:GAUGE:$wirelessvalid:0:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:720:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:1440:1500 ";
$rrdcreateoutput = array();
$rrdcreatereturn = "";
$rrdcreatel = exec("$rrdcreate 2>&1", $rrdcreateoutput, $rrdcreatereturn);
if ($rrdcreatereturn != 0) {
log_error("RRD create failed exited with $rrdcreatereturn, the
error is: $rrdcreateoutput[0]\n");
}
}
/* enter UNKNOWN values in the RRD so it knows we rebooted. */
if($g['booting']) {
exec("$rrdtool update $rrddbpath$ifname$wireless N:U:U:U");
}
$rrdupdatesh .= "\n";
$rrdupdatesh .= "# polling wireless for interface $ifname $realif \n";
$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$wireless N:\\\n";
$rrdupdatesh .= "`$ifconfig {$realif} list ap| $awk 'gsub(\"M\", \"\") {getline 2;print substr(\$5, 0, length(\$5)-2) \":\" $4 \":\" $3}'`\n";
}
/* WAN interface only statistics */
if ("$ifname" == "wan") {
/* QUEUES, set up the queues databases */
if (!is_array($config['shaper']['queue'])) {
$config['shaper']['queue'] = array ();
}
$a_queues = & $config['shaper']['queue'];
if (isset ($config['shaper']['enable'])) {
if (!file_exists("$rrddbpath$ifname$queues")) {
/* create rrd file if it does not exist */
log_error("Create RRD database $rrddbpath$ifname$queues");
$rrdcreate = "$rrdtool create $rrddbpath$ifname$queues --step $rrdqueuesinterval ";
/* loop list of shaper queues */
$q = 0;
foreach ($a_queues as $queue) {
$name = $queue['name'];
$rrdcreate .= "DS:$name:COUNTER:$queuesvalid:0:$downstream ";
}
$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:720:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:1440:1500 ";
$rrdcreatel = exec("$rrdcreate 2>&1", $rrdcreateoutput, $rrdcreatereturn);
if ($rrdcreatereturn != 0) {
log_error("RRD create failed exited with $rrdcreatereturn, the
error is: $rrdcreateoutput[0]\n");
}
}
if (!file_exists("$rrddbpath$ifname$queuesdrop")) {
/* create rrd file if it does not exist */
log_error("Create RRD database $rrddbpath$ifname$queuesdrop");
$rrdcreate = "$rrdtool create $rrddbpath$ifname$queuesdrop --step $rrdqueuesdropinterval ";
/* loop list of shaper queues */
$q = 0;
foreach ($a_queues as $queue) {
$name = $queue['name'];
$rrdcreate .= "DS:$name:COUNTER:$queuesdropvalid:0:$downstream ";
}
$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:720:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:1440:1500 ";
$rrdcreatel = exec("$rrdcreate 2>&1", $rrdcreateoutput, $rrdcreatereturn);
if ($rrdcreatereturn != 0) {
log_error("RRD create failed exited with $rrdcreatereturn, the error is: $rrdcreateoutput[0]\n");
}
}
if($g['booting']) {
$rrdqcommand = "-t ";
$rrducommand = "N";
$q = 0;
foreach ($a_queues as $queue) {
if($q == 0) {
$rrdqcommand .= "{$queue['name']}";
} else {
$rrdqcommand .= ":{$queue['name']}";
}
$q++;
$rrducommand .= ":U";
}
exec("$rrdtool update $rrddbpath$ifname$queues $rrdqcommand $rrducommand");
exec("$rrdtool update $rrddbpath$ifname$queuesdrop $rrdqcommand $rrducommand");
}
/* awk function to gather shaper data */
/* yes, it's special */
$rrdupdatesh .= "` pfctl -vsq | awk 'BEGIN {printf \"$rrdtool update $rrddbpath$ifname$queues \" } ";
$rrdupdatesh .= "{ ";
$rrdupdatesh .= "if ((\$1 == \"queue\") && ( \$2 ~ /^q/ )) { ";
$rrdupdatesh .= "dsname = dsname \":\" \$2 ; ";
$rrdupdatesh .= "q=1; ";
$rrdupdatesh .= "} ";
$rrdupdatesh .= "else if ((\$4 == \"bytes:\") && ( q == 1 ) ) { ";
$rrdupdatesh .= "dsdata = dsdata \":\" \$5 ; ";
$rrdupdatesh .= "q=0; ";
$rrdupdatesh .= "} ";
$rrdupdatesh .= "} END { ";
$rrdupdatesh .= "dsname = substr(dsname,2); ";
$rrdupdatesh .= "dsdata = substr(dsdata,2); ";
$rrdupdatesh .= "printf \"-t \" dsname \" N:\" dsdata }' ";
$rrdupdatesh .= "dsname=\"\" dsdata=\"\"`\n\n";
$rrdupdatesh .= "` pfctl -vsq | awk 'BEGIN {printf \"$rrdtool update $rrddbpath$ifname$queuesdrop \" } ";
$rrdupdatesh .= "{ ";
$rrdupdatesh .= "if ((\$1 == \"queue\") && ( \$2 ~ /^q/ )) { ";
$rrdupdatesh .= "dsname = dsname \":\" \$2 ; ";
$rrdupdatesh .= "q=1; ";
$rrdupdatesh .= "} ";
$rrdupdatesh .= "else if ((\$4 == \"bytes:\") && ( q == 1 ) ) { ";
$rrdupdatesh .= "dsdata = dsdata \":\" \$8 ; ";
$rrdupdatesh .= "q=0; ";
$rrdupdatesh .= "} ";
$rrdupdatesh .= "} END { ";
$rrdupdatesh .= "dsname = substr(dsname,2); ";
$rrdupdatesh .= "dsdata = substr(dsdata,2); ";
$rrdupdatesh .= "printf \"-t \" dsname \" N:\" dsdata }' ";
$rrdupdatesh .= "dsname=\"\" dsdata=\"\"`\n\n";
}
}
}
$i++;
/* System only statistics */
$ifname = "system";
/* STATES, create pf states database */
if(! file_exists("$rrddbpath$ifname$states")) {
/* create rrd file if it does not exist */
log_error("Create RRD database $rrddbpath$ifname$states");
$rrdcreate = "$rrdtool create $rrddbpath$ifname$states --step $rrdstatesinterval ";
$rrdcreate .= "DS:pfrate:GAUGE:$statesvalid:0:10000000 ";
$rrdcreate .= "DS:pfstates:GAUGE:$statesvalid:0:10000000 ";
$rrdcreate .= "DS:pfnat:GAUGE:$statesvalid:0:10000000 ";
$rrdcreate .= "DS:srcip:GAUGE:$statesvalid:0:10000000 ";
$rrdcreate .= "DS:dstip:GAUGE:$statesvalid:0:10000000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:720:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:1440:1500 ";
$rrdcreatel = exec("$rrdcreate 2>&1", $rrdcreateoutput, $rrdcreatereturn);
if($rrdcreatereturn != 0) {
log_error("RRD create failed exited with $rrdcreatereturn, the
error is: $rrdcreateoutput[0]\n");
}
}
/* enter UNKNOWN values in the RRD so it knows we rebooted. */
if($g['booting']) {
exec("$rrdtool update $rrddbpath$ifname$states N:U:U:U:U:U");
}
/* the pf states gathering function. */
$rrdupdatesh .= "\n";
$rrdupdatesh .= "pfctl_si_out=\"` $pfctl -si > /tmp/pfctl_si_out `\"\n";
$rrdupdatesh .= "pfctl_ss_out=\"` $pfctl -ss > /tmp/pfctl_ss_out`\"\n";
$rrdupdatesh .= "pfrate=\"` cat /tmp/pfctl_si_out | egrep \"inserts|removals\" | awk '{ pfrate = \$3 + pfrate } {print pfrate}'|tail -1 `\"\n";
$rrdupdatesh .= "pfstates=\"` cat /tmp/pfctl_ss_out | egrep -v \"<\\-.*?<\\-|\\->.*?\\->\" | wc -l|sed 's/ //g'`\"\n";
$rrdupdatesh .= "pfnat=\"` cat /tmp/pfctl_ss_out | egrep '<\\-.*?<\\-|\\->.*?\\->' | wc -l|sed 's/ //g' `\"\n";
$rrdupdatesh .= "srcip=\"` cat /tmp/pfctl_ss_out | egrep -v '<\\-.*?<\\-|\\->.*?\\->' | grep '\\->' | awk '{print \$3}' | awk -F: '{print \$1}' | sort -u|wc -l|sed 's/ //g' `\"\n";
$rrdupdatesh .= "dstip=\"` cat /tmp/pfctl_ss_out | egrep -v '<\\-.*?<\\-|\\->.*?\\->' | grep '<\\-' | awk '{print \$3}' | awk -F: '{print \$1}' | sort -u|wc -l|sed 's/ //g' `\"\n";
$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$states N:\$pfrate:\$pfstates:\$pfnat:\$srcip:\$dstip\n\n";
/* End pf states statistics */
/* CPU, create CPU statistics database */
if(! file_exists("$rrddbpath$ifname$proc")) {
/* create rrd file if it does not exist */
log_error("Create RRD database $rrddbpath$ifname$proc");
$rrdcreate = "$rrdtool create $rrddbpath$ifname$proc --step $rrdprocinterval ";
$rrdcreate .= "DS:user:GAUGE:$procvalid:0:10000000 ";
$rrdcreate .= "DS:nice:GAUGE:$procvalid:0:10000000 ";
$rrdcreate .= "DS:system:GAUGE:$procvalid:0:10000000 ";
$rrdcreate .= "DS:interrupt:GAUGE:$procvalid:0:10000000 ";
$rrdcreate .= "DS:processes:GAUGE:$procvalid:0:10000000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:720:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:1440:1500 ";
$rrdcreatel = exec("$rrdcreate 2>&1", $rrdcreateoutput, $rrdcreatereturn);
if($rrdcreatereturn != 0) {
log_error("RRD create failed exited with $rrdcreatereturn, the
error is: $rrdcreateoutput[0]\n");
}
}
/* enter UNKNOWN values in the RRD so it knows we rebooted. */
if($g['booting']) {
exec("$rrdtool update $rrddbpath$ifname$proc N:U:U:U:U:U");
}
/* the CPU stats gathering function. */
$rrdupdatesh .= "`$top -d 2 -s 1 0 | $awk '{gsub(/%/, \"\")} BEGIN { ";
$rrdupdatesh .= "printf \"$rrdtool update $rrddbpath$ifname$proc \" } ";
$rrdupdatesh .= "{ if ( \$2 == \"processes:\" ) { ";
$rrdupdatesh .= "processes = \$1; ";
$rrdupdatesh .= "} ";
$rrdupdatesh .= "else if ( \$1 == \"CPU\" ) { ";
$rrdupdatesh .= "user = \$3; ";
$rrdupdatesh .= "nice = \$5; ";
$rrdupdatesh .= "sys = \$7; ";
$rrdupdatesh .= "interrupt = \$9; ";
$rrdupdatesh .= "} ";
$rrdupdatesh .= "} END { ";
$rrdupdatesh .= "printf \"N:\"user\":\"nice\":\"sys\":\"interrupt\":\"processes ";
$rrdupdatesh .= "}'`\n\n";
/* End CPU statistics */
/* SPAMD, set up the spamd rrd file */
if (isset($config['installedpackages']['spamdsettings']) &&
isset ($config['installedpackages']['spamdsettings']['config'][0]['enablerrd'])) {
/* set up the spamd rrd file */
if (!file_exists("$rrddbpath$ifname$spamd")) {
/* create rrd file if it does not exist */
log_error("Create RRD database $rrddbpath$ifname$spamd");
$rrdcreate = "$rrdtool create $rrddbpath$ifname$spamd --step $rrdspamdinterval ";
$rrdcreate .= "DS:conn:GAUGE:$spamdvalid:0:10000 ";
$rrdcreate .= "DS:time:GAUGE:$spamdvalid:0:86400 ";
$rrdcreate .= "RRA:MIN:0.5:1:1000 ";
$rrdcreate .= "RRA:MIN:0.5:5:1000 ";
$rrdcreate .= "RRA:MIN:0.5:60:1000 ";
$rrdcreate .= "RRA:MIN:0.5:720:1000 ";
$rrdcreate .= "RRA:MIN:0.5:1440:1500 ";
$rrdcreate .= "RRA:AVERAGE:0.5:1:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:5:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:720:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:1440:1500 ";
$rrdcreate .= "RRA:MAX:0.5:1:1000 ";
$rrdcreate .= "RRA:MAX:0.5:5:1000 ";
$rrdcreate .= "RRA:MAX:0.5:60:1000 ";
$rrdcreate .= "RRA:MAX:0.5:720:1000 ";
$rrdcreate .= "RRA:MAX:0.5:1440:1500 ";
$rrdcreatel = exec("$rrdcreate 2>&1", $rrdcreateoutput, $rrdcreatereturn);
if ($rrdcreatereturn != 0) {
log_error("RRD create failed exited with $rrdcreatereturn, the
error is: $rrdcreateoutput[0]\n");
}
}
$rrdupdatesh .= "\n";
$rrdupdatesh .= "# polling spamd for connections and tarpitness \n";
$rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$spamd \\\n";
$rrdupdatesh .= "`$php -q $spamd_gather`\n";
}
/* End System statistics */
$rrdupdatesh .= "sleep 60\n";
$rrdupdatesh .= "done\n";
log_error("Creating rrd update script");
/* write the rrd update script */
$updaterrdscript = "{$g['vardb_path']}/rrd/updaterrd.sh";
$fd = fopen("$updaterrdscript", "w");
fwrite($fd, "$rrdupdatesh");
fclose($fd);
/* kill off traffic collectors */
kill_traffic_collector();
/* start traffic collector */
mwexec_bg("/usr/bin/nice -n20 /bin/sh $updaterrdscript");
} else {
/* kill off traffic collectors */
kill_traffic_collector();
}
if($g['booting'])
echo "done.\n";
}
function kill_traffic_collector() {
mwexec("ps awwwux | grep '/[u]pdaterrd.sh' | awk '{print $2}' | xargs kill");
}
function update_filter_reload_status($text) {
global $g;
config_lock();
$fd = fopen("{$g['varrun_path']}/filter_reload_status", "w");
fwrite($fd, $text);
fclose($fd);
config_unlock();
}
function get_interface_gateway($interface) {
global $config, $g;
$interface = strtolower($interface);
/* if we are dhclient, obtain the gateway from the tmp file, otherwise
* grab the address from the configuration file.
*/
$tmpif = convert_real_interface_to_friendly_interface_name($interface);
if($tmpif <> $interface)
$interface = $tmpif;
$realif = $config['interfaces'][$interface]['if'];
if(file_exists("{$g['tmp_path']}/{$realif}_router")) {
$gw = file_get_contents("{$g['tmp_path']}/{$realif}_router");
$gw = rtrim($gw);
} else {
if(is_array($config['gateways'])) {
foreach($config['gateways']['gateway_item'] as $gateway) {
if($gateway['name'] == $gateway['name']) {
$gatewayip = $gateway['gateway'];
$interfacegw = $gateway['interface'];
}
}
$gw = $gatewayip;
} else {
$gw = $config['interfaces'][$interface]['gateway'];
}
}
/* if wan is requested, return it */
if($interface == "wan")
return str_replace("\n", "", `route -n get default | grep gateway | awk '{ print $2 }'`);
/* return gateway */
return $gw;
}
function is_dhcp_server_enabled() {
/* DHCP enabled on any interfaces? */
global $config, $g;
$dhcpdcfg = $config['dhcpd'];
$dhcpdenable = false;
foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
if (isset ($dhcpifconf['enable']) && (($dhcpif == "lan") || (isset ($config['interfaces'][$dhcpif]['enable']) && $config['interfaces'][$dhcpif]['if'] && (!$config['interfaces'][$dhcpif]['bridge']))))
$dhcpdenable = true;
if (isset ($dhcpifconf['enable']) && (($dhcpif == "wan") || (isset ($config['interfaces'][$dhcpif]['enable']) && $config['interfaces'][$dhcpif]['if'] && (!$config['interfaces'][$dhcpif]['bridge']))))
$dhcpdenable = true;
}
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'];
if ($config['interfaces'][$friendly]['ipaddr'] == "dhcp") {
$ints[] = $friendly;
continue;
}
if ($config['interfaces'][$friendly]['ipaddr'] == "pppoe") {
$ints[] = $friendly;
continue;
}
if ($config['interfaces'][$friendly]['ipaddr'] == "pptp") {
$ints[] = $friendly;
continue;
}
if ($config['interfaces'][$friendly]['gateway'] <> "") {
$ints[] = $friendly;
continue;
}
}
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();
/* find out interface name */
$ifinfo['hwif'] = $config['interfaces'][$ifdescr]['if'];
if ($ifdescr == "wan")
$ifinfo['if'] = get_real_wan_interface();
else
$ifinfo['if'] = $ifinfo['hwif'];
/* run netstat to determine link info */
unset($linkinfo);
exec("/usr/bin/netstat -I " . $ifinfo['hwif'] . " -nWb -f link", $linkinfo);
$linkinfo = preg_split("/\s+/", $linkinfo[1]);
if (preg_match("/\*$/", $linkinfo[0])) {
$ifinfo['status'] = "down";
} else {
$ifinfo['status'] = "up";
}
if (!strstr($ifinfo['if'],'tun')) {
$ifinfo['macaddr'] = $linkinfo[3];
$ifinfo['inpkts'] = $linkinfo[4];
$ifinfo['inerrs'] = $linkinfo[5];
$ifinfo['inbytes'] = $linkinfo[6];
$ifinfo['outpkts'] = $linkinfo[7];
$ifinfo['outerrs'] = $linkinfo[8];
$ifinfo['outbytes'] = $linkinfo[9];
$ifinfo['collisions'] = $linkinfo[10];
} else {
$ifinfo['inpkts'] = $linkinfo[3];
$ifinfo['inbytes'] = $linkinfo[5];
$ifinfo['outpkts'] = $linkinfo[6];
$ifinfo['outbytes'] = $linkinfo[8];
}
/* DHCP? -> see if dhclient is up */
if (($ifdescr == "wan") && ($config['interfaces']['wan']['ipaddr'] == "dhcp")) {
/* see if dhclient is up */
if (is_dhcp_running("wan") == true)
$ifinfo['dhcplink'] = "up";
else
$ifinfo['dhcplink'] = "down";
}
/* loop through optional interfaces looking to see if they are dhcp */
for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
$ifdescrs['opt' . $j] = $config['interfaces']['opt' . $j]['descr'];
if (($ifdescr == "opt{$j}") && ($config['interfaces']['opt' . $j]['ipaddr'] == "dhcp")) {
/* see if dhclient is up */
if (is_dhcp_running("opt{$j}") == true)
$ifinfo['dhcplink'] = "up";
else
$ifinfo['dhcplink'] = "down";
}
}
/* PPPoE interface? -> get status from virtual interface */
if (($ifdescr == "wan") && ($config['interfaces']['wan']['ipaddr'] == "pppoe")) {
unset($linkinfo);
exec("/usr/bin/netstat -I " . $ifinfo['if'] . " -nWb -f link", $linkinfo);
$linkinfo = preg_split("/\s+/", $linkinfo[1]);
if (preg_match("/\*$/", $linkinfo[0])) {
$ifinfo['pppoelink'] = "down";
} else {
/* get PPPoE link status for dial on demand */
$ifconfiginfo = "";
unset($ifconfiginfo);
exec("/sbin/ifconfig " . $ifinfo['if'], $ifconfiginfo);
$ifinfo['pppoelink'] = "up";
foreach ($ifconfiginfo as $ici) {
if (strpos($ici, 'LINK0') !== false)
$ifinfo['pppoelink'] = "down";
}
}
}
/* PPTP interface? -> get status from virtual interface */
if (($ifdescr == "wan") && ($config['interfaces']['wan']['ipaddr'] == "pptp")) {
unset($linkinfo);
exec("/usr/bin/netstat -I " . $ifinfo['if'] . " -nWb -f link", $linkinfo);
$linkinfo = preg_split("/\s+/", $linkinfo[1]);
if (preg_match("/\*$/", $linkinfo[0])) {
$ifinfo['pptplink'] = "down";
} else {
/* get PPTP link status for dial on demand */
unset($ifconfiginfo);
exec("/sbin/ifconfig " . $ifinfo['if'], $ifconfiginfo);
$ifinfo['pptplink'] = "up";
foreach ($ifconfiginfo as $ici) {
if (strpos($ici, 'LINK0') !== false)
$ifinfo['pptplink'] = "down";
}
}
}
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];
}
}
if ($ifinfo['pppoelink'] != "down" && $ifinfo['pptplink'] != "down") {
/* try to determine IP address and netmask with ifconfig */
unset($ifconfiginfo);
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 ($ifdescr == "wan") {
/* run netstat to determine the default gateway */
unset($netstatrninfo);
exec("/usr/bin/netstat -rnf inet", $netstatrninfo);
foreach ($netstatrninfo as $nsr) {
if (preg_match("/^default\s*(\S+)/", $nsr, $matches)) {
$ifinfo['gateway'] = $matches[1];
}
}
} else {
/* deterimine interface gateway */
$int = convert_friendly_interface_to_real_interface_name($ifdescr);
$gw = get_interface_gateway($int);
if($gw)
$ifinfo['gateway'] = $gw;
}
}
}
$bridge = "";
$int = "";
$int = convert_friendly_interface_to_real_interface_name($ifdescr);
$bridge = link_int_to_bridge_interface($int);
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)) {
$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_interface_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($interfacegw);
}
}
} else {
return(false);
}
}
?>