. Copyright (C) 2010 Ermal Luçi All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* pfSense_BUILDER_BINARIES: /usr/bin/killall /bin/pgrep /bin/sh /usr/local/sbin/dhcpd /usr/local/sbin/igmpproxy pfSense_BUILDER_BINARIES: /sbin/ifconfig /usr/sbin/arp /sbin/ifconfig /usr/local/sbin/dnsmasq pfSense_BUILDER_BINARIES: /usr/sbin/bsnmpd /sbin/route /usr/local/sbin/olsrd pfSense_BUILDER_BINARIES: /usr/local/sbin/miniupnpd /usr/sbin/rtadvd pfSense_MODULE: utils */ /* implement ipv6 route advertising deamon */ function services_rtadvd_configure() { global $config, $g; if(isset($config['system']['developerspew'])) { $mt = microtime(); echo "services_rtadvd_configure() being called $mt\n"; } if($g['services_dhcp_server_enable'] == false) return; if(is_process_running("rtadvd")) { mwexec("killall -9 rtadvd", true); } /* DHCP enabled on any interfaces? */ if (!is_dhcp_server_enabled()) return 0; if (!is_array($config['dhcpdv6'])) $config['dhcpdv6'] = array(); $dhcpdv6cfg = $config['dhcpdv6']; $Iflist = get_configured_interface_list(); /* write rtadvd.conf */ $fd = fopen("{$g['varetc_path']}/rtadvd.conf", "w"); if (!$fd) { printf("Error: cannot open rtadvd.conf in services_rtadvd_configure().\n"); return 1; } /* raflags, other o, managed=64 m, stateful=128, both=192 */ $rtadvdconf = "# Automatically Generated, do not edit\n"; $rtadvdconf = << $dhcpv6ifconf) { $rtadvdnum++; /* It appears we can not advertise the gateway IP (carp) * rtadvd[44205]: IPV6_JOIN_GROUP(link) on vip1: Can't assign requested address if($dhcpv6ifconf['gateway'] <> "") { $dhcpv6if = find_carp_interface($dhcpv6ifconf['gateway']); } */ $realif = get_real_interface($dhcpv6if); $rtadvdifs[] = $realif; $ifcfgipv6 = get_interface_ipv6($dhcpv6if); $ifcfgsnv6 = get_interface_subnetv6($dhcpv6if); $subnetv6 = gen_subnetv6($ifcfgipv6, $ifcfgsnv6); $subnetmaskv6 = gen_subnet_mask($ifcfgsnv6); $rtadvdconf .= "{$realif}:\\\n"; $rtadvdconf .= "\t:addr=\"{$subnetv6}\":\\\n"; $rtadvdconf .= "\t:prefixlen#{$ifcfgsnv6}:\\\n"; switch($dhcpv6ifconf['mode']) { case "managed": $rtadvdconf .= "\t:raflags=\"m\":\\\n"; break; case "assist": $rtadvdconf .= "\t:raflags=\"o\":\\\n"; break; default: $rtadvdconf .= "\t:raflags#0:\\\n"; break; } $rtadvdconf .= "\t:tc=ether:\n"; $rtadvdconf .= "\n\n"; } fwrite($fd, $rtadvdconf); fclose($fd); if(count($rtadvdifs) > 0) { mwexec("/usr/sbin/rtadvd -c {$g['varetc_path']}/rtadvd.conf " . join(" ", $rtadvdifs)); } return 0; } function services_dhcpd_configure() { global $config, $g; if($g['services_dhcp_server_enable'] == false) return; if(isset($config['system']['developerspew'])) { $mt = microtime(); echo "services_dhcpd_configure($if) being called $mt\n"; } /* kill any running dhcpd */ if(is_process_running("dhcpd")) { mwexec("killall dhcpd", true); } /* DHCP enabled on any interfaces? */ if (!is_dhcp_server_enabled()) return 0; /* if OLSRD is enabled, allow WAN to house DHCP. */ if($config['installedpackages']['olsrd']) foreach($config['installedpackages']['olsrd']['config'] as $olsrd) if($olsrd['enable']) $is_olsr_enabled = true; /* configure DHCPD chroot */ $fd = fopen("{$g['tmp_path']}/dhcpd.sh","w"); $status = `mount | grep "{$g['dhcpd_chroot_path']}/dev"`; fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}\n"); fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/dev\n"); fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/etc\n"); fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/usr/local/sbin\n"); fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/var/db\n"); fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/var/run\n"); fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/usr\n"); fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/lib\n"); fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/run\n"); fwrite($fd, "chown -R dhcpd:_dhcp {$g['dhcpd_chroot_path']}/*\n"); fwrite($fd, "cp /lib/libc.so.* {$g['dhcpd_chroot_path']}/lib/\n"); fwrite($fd, "cp /usr/local/sbin/dhcpd {$g['dhcpd_chroot_path']}/usr/local/sbin/\n"); fwrite($fd, "chmod a+rx {$g['dhcpd_chroot_path']}/usr/local/sbin/dhcpd\n"); if(!trim($status)) fwrite($fd, "mount -t devfs devfs {$g['dhcpd_chroot_path']}/dev\n"); fclose($fd); mwexec("/bin/sh {$g['tmp_path']}/dhcpd.sh"); if ($g['booting']) { if ($g['platform'] != "pfSense") { /* restore the leases, if we have them */ if (file_exists("{$g['cf_conf_path']}/dhcpleases.tgz")) { $dhcprestore = ""; $dhcpreturn = ""; exec("cd /;LANG=C /usr/bin/tar -xzf {$g['cf_conf_path']}/dhcpleases.tgz 2>&1", $dhcprestore, $dhcpreturn); $dhcprestore = implode(" ", $dhcprestore); if($dhcpreturn <> 0) { log_error("DHCP leases restore failed exited with $dhcpreturn, the error is: $dhcprestore\n"); } } if (file_exists("{$g['cf_conf_path']}/dhcp6leases.tgz")) { $dhcprestore = ""; $dhcpreturn = ""; exec("cd /;LANG=C /usr/bin/tar -xzf {$g['cf_conf_path']}/dhcp6leases.tgz 2>&1", $dhcprestore, $dhcpreturn); $dhcprestore = implode(" ", $dhcprestore); if($dhcpreturn <> 0) { log_error("DHCP leases v6 restore failed exited with $dhcpreturn, the error is: $dhcprestore\n"); } } } } $syscfg = $config['system']; if (!is_array($config['dhcpd'])) $config['dhcpd'] = array(); if (!is_array($config['dhcpdv6'])) $config['dhcpdv6'] = array(); $dhcpdcfg = $config['dhcpd']; $dhcpdv6cfg = $config['dhcpdv6']; $Iflist = get_configured_interface_list(); if ($g['booting']) echo "Starting DHCP service..."; else sleep(1); /* write dhcpd.conf */ $fd = fopen("{$g['dhcpd_chroot_path']}/etc/dhcpd.conf", "w"); $fdv6 = fopen("{$g['dhcpd_chroot_path']}/etc/dhcpdv6.conf", "w"); if ((!$fd) || (! $fdv6)) { printf("Error: cannot open dhcpd.conf or dhcpdv6.conf in services_dhcpd_configure().\n"); return 1; } $custoptions = ""; foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) { if(is_array($dhcpifconf['numberoptions']) && is_array($dhcpifconf['numberoptions']['item'])) { foreach($dhcpifconf['numberoptions']['item'] as $itemidx => $item) { if(!empty($item['type'])) $itemtype = $item['type']; else $itemtype = "text"; $custoptions .= "option custom-{$dhcpif}-{$itemidx} code {$item['number']} = {$itemtype};\n"; } } } $custoptionsv6 = ""; foreach ($dhcpdv6cfg as $dhcpv6if => $dhcpv6ifconf) { if(is_array($dhcpv6ifconf['numberoptions']) && is_array($dhcpv6ifconf['numberoptions']['item'])) { foreach($dhcpv6ifconf['numberoptions']['item'] as $itemv6idx => $itemv6) { $custoptionsv6 .= "option custom-{$dhcpv6if}-{$itemv6idx} code {$itemv6['number']} = text;\n"; } } } $dhcpdconf = << $dhcpifconf) { interfaces_staticarp_configure($dhcpif); if (!isset($dhcpifconf['enable'])) continue; if($dhcpifconf['failover_peerip'] <> "") { $int = guess_interface_from_ip($dhcpifconf['failover_peerip']); $intip = find_interface_ip($int); $real_dhcpif = convert_friendly_interface_to_real_interface_name($dhcpif); /* * yep, failover peer is defined. * does it match up to a defined vip? */ $skew = 110; $a_vip = &$config['virtualip']['vip']; if(is_array($a_vip)) { foreach ($a_vip as $vipent) { if($int == $real_dhcpif) { /* this is the interface! */ if(is_numeric($vipent['advskew']) && ($vipent['advskew'] < "20")) $skew = 0; } } } else { log_error("Warning! DHCP Failover setup and no CARP virtual IP's defined!"); } if($skew > 10) { $type = "secondary"; $dhcpdconf_pri = "mclt 600;\n"; $my_port = "520"; $peer_port = "519"; } else { $my_port = "519"; $peer_port = "520"; $type = "primary"; $dhcpdconf_pri = "split 128;\n"; $dhcpdconf_pri .= " mclt 600;\n"; } $dhcpdconf .= << $dhcpv6ifconf) { interfaces_staticarp_configure($dhcpv6if); if (!isset($dhcpv6ifconf['enable'])) continue; if($dhcpv6ifconf['failover_peerip'] <> "") { $intv6 = guess_interface_from_ip($dhcpv6ifconf['failover_peerip']); $intipv6 = find_interface_ipv6($intv6); $real_dhcpv6if = convert_friendly_interface_to_real_interface_name($dhcpv6if); /* * yep, failover peer is defined. * does it match up to a defined vip? */ $skew = 110; $a_vip = &$config['virtualip']['vip']; if(is_array($a_vip)) { foreach ($a_vip as $vipent) { if($intv6 == $real_dhcpv6if) { /* this is the interface! */ if(is_numeric($vipent['advskew']) && ($vipent['advskew'] < "20")) $skew = 0; } } } else { log_error("Warning! DHCPv6 Failover setup and no CARP virtual IPv6's defined!"); } if($skew > 10) { $typev6 = "secondary"; $dhcpdv6conf_pri = "mclt 600;\n"; $my_portv6 = "520"; $peer_portv6 = "519"; } else { $my_portv6 = "519"; $peer_portv6 = "520"; $typev6 = "primary"; $dhcpdv6conf_pri = "split 128;\n"; $dhcpdv6conf_pri .= " mclt 600;\n"; } $dhcpdv6conf .= << $dhcpifconf) { $ifcfg = $config['interfaces'][$dhcpif]; if (!isset($dhcpifconf['enable']) || !isset($Iflist[$dhcpif])) continue; $ifcfgip = get_interface_ip($dhcpif); $ifcfgsn = get_interface_subnet($dhcpif); $subnet = gen_subnet($ifcfgip, $ifcfgsn); $subnetmask = gen_subnet_mask($ifcfgsn); if (!is_ipaddr($subnet)) continue; if($is_olsr_enabled == true) if($dhcpifconf['netmask']) $subnetmask = gen_subnet_mask($dhcpifconf['netmask']); $dnscfg = ""; if ($dhcpifconf['domain']) { $dnscfg .= " option domain-name \"{$dhcpifconf['domain']}\";\n"; } if($dhcpifconf['domainsearchlist'] <> "") { $dnscfg .= " option domain-search-list \"{$dhcpifconf['domainsearchlist']}\";\n"; } if (isset($dhcpifconf['ddnsupdate'])) { if($dhcpifconf['ddnsdomain'] <> "") { $dnscfg .= " ddns-domainname \"{$dhcpifconf['ddnsdomain']}\";\n"; } $dnscfg .= " ddns-update-style interim;\n"; } if (is_array($dhcpifconf['dnsserver']) && ($dhcpifconf['dnsserver'][0])) { $dnscfg .= " option domain-name-servers " . join(",", $dhcpifconf['dnsserver']) . ";"; } else if (isset($config['dnsmasq']['enable'])) { $dnscfg .= " option domain-name-servers {$ifcfgip};"; } else if (is_array($syscfg['dnsserver']) && ($syscfg['dnsserver'][0])) { $dnscfg .= " option domain-name-servers " . join(",", $syscfg['dnsserver']) . ";"; } $dhcpdconf .= "subnet {$subnet} netmask {$subnetmask} {\n"; $dhcpdconf .= " pool {\n"; /* is failover dns setup? */ if (is_array($dhcpifconf['dnsserver']) && $dhcpifconf['dnsserver'][0] <> "") { $dhcpdconf .= " option domain-name-servers {$dhcpifconf['dnsserver'][0]}"; if($dhcpifconf['dnsserver'][1] <> "") $dhcpdconf .= ",{$dhcpifconf['dnsserver'][1]}"; $dhcpdconf .= ";\n"; } if($dhcpifconf['failover_peerip'] <> "") $dhcpdconf .= " deny dynamic bootp clients;\n"; if (isset($dhcpifconf['denyunknown'])) $dhcpdconf .= " deny unknown clients;\n"; if ($dhcpifconf['gateway']) $routers = $dhcpifconf['gateway']; else $routers = $ifcfgip; if($dhcpifconf['failover_peerip'] <> "") { $dhcpdconf .= " failover peer \"dhcp{$dhcpnum}\";\n"; $dhcpnum++; } $dhcpdconf .= << "") $dhcpdconf .= " option tftp-server-name \"{$dhcpifconf['tftp']}\";\n"; // Handle option, number rowhelper values $dhcpdconf .= "\n"; if($dhcpifconf['numberoptions']['item']) { foreach($dhcpifconf['numberoptions']['item'] as $itemidx => $item) { if(empty($item['type']) || $item['type'] == "text") $dhcpdconf .= " option custom-{$dhcpif}-{$itemidx} \"{$item['value']}\";\n"; else $dhcpdconf .= " option custom-{$dhcpif}-{$itemidx} {$item['value']};\n"; } } // ldap-server if ($dhcpifconf['ldap'] <> "") $dhcpdconf .= " option ldap-server \"{$dhcpifconf['ldap']}\";\n"; // net boot information if(isset($dhcpifconf['netboot'])) { if (($dhcpifconf['next-server'] <> "") && ($dhcpifconf['filename'] <> "")) { $dhcpdconf .= " next-server {$dhcpifconf['next-server']};\n"; $dhcpdconf .= " filename \"{$dhcpifconf['filename']}\";\n"; } if ($dhcpifconf['rootpath'] <> "") { $dhcpdconf .= " option root-path \"{$dhcpifconf['rootpath']}\";\n"; } } $dhcpdconf .= << $dhcpv6ifconf) { $ifcfgv6 = $config['interfaces'][$dhcpv6if]; if (!isset($dhcpv6ifconf['enable']) || !isset($Iflist[$dhcpv6if])) continue; $ifcfgipv6 = get_interface_ipv6($dhcpv6if); $ifcfgsnv6 = get_interface_subnetv6($dhcpv6if); $subnetv6 = gen_subnetv6($ifcfgipv6, $ifcfgsnv6); $subnetmaskv6 = gen_subnet_mask($ifcfgsnv6); if($is_olsr_enabled == true) if($dhcpv6ifconf['netmask']) $subnetmask = gen_subnet_mask($dhcpv6ifconf['netmask']); $dnscfgv6 = ""; if ($dhcpv6ifconf['domain']) { $dnscfgv6 .= " option domain-name \"{$dhcpv6ifconf['domain']}\";\n"; } if($dhcpv6ifconf['domainsearchlist'] <> "") { $dnscfgv6 .= " option domain-search-list \"{$dhcpifconf['domainsearchlist']}\";\n"; } if (isset($dhcpv6ifconf['ddnsupdate'])) { if($dhcpv6ifconf['ddnsdomain'] <> "") { $dnscfgv6 .= " ddns-domainname \"{$dhcpv6ifconf['ddnsdomain']}\";\n"; } $dnscfgv6 .= " ddns-update-style interim;\n"; } if (is_array($dhcpv6ifconf['dnsserver']) && ($dhcpv6ifconf['dnsserver'][0])) { $dnscfgv6 .= " option dhcp6.name-servers " . join(",", $dhcpv6ifconf['dnsserver']) . ";"; } else if (isset($config['dnsmasq']['enable'])) { $dnscfgv6 .= " option dhcp6.name-servers {$ifcfgipv6};"; } else if (is_array($syscfg['dnsserver']) && ($syscfg['dnsserver'][0])) { $dns_arrv6 = array(); foreach($syscfg['dnsserver'] as $dnsserver) { if(is_ipaddrv6($dnsserver)) { $dns_arrv6[] = $dnsserver; } } if(!empty($dns_arrv6)) $dnscfgv6 .= " option dhcp6.name-servers " . join(",", $dns_arrv6) . ";"; } $subnet6 = Net_IPv6::compress(gen_subnetv6($ifcfgipv6, $ifcfgsnv6)); $dhcpdv6conf .= "subnet6 {$subnet6}/{$ifcfgsnv6} {\n"; /* is failover dns setup? */ if (is_array($dhcpv6ifconf['dnsserver']) && $dhcpv6ifconf['dnsserver'][0] <> "") { $dhcpdv6conf .= " option dhcp6.name-servers {$dhcpv6ifconf['dnsserver'][0]}"; if($dhcpv6ifconf['dnsserver'][1] <> "") $dhcpdv6conf .= ",{$dhcpv6ifconf['dnsserver'][1]}"; $dhcpdv6conf .= ";\n"; } if($dhcpv6ifconf['failover_peerip'] <> "") $dhcpdv6conf .= " deny dynamic bootp clients;\n"; if (isset($dhcpv6ifconf['denyunknown'])) $dhcpdv6conf .= " deny unknown clients;\n"; if ($dhcpv6ifconf['gateway']) $routersv6 = $dhcpv6ifconf['gateway']; else $routersv6 = $ifcfgipv6; if($dhcpv6ifconf['failover_peerip'] <> "") { $dhcpdv6conf .= " failover peer \"dhcpv6{$dhcpv6num}\";\n"; $dhcpv6num++; } $dhcpdv6conf .= << "") $dhcpdv6conf .= " option tftp-server-name \"{$dhcpv6ifconf['tftp']}\";\n"; // Handle option, number rowhelper values $dhcpdv6conf .= "\n"; if($dhcpv6ifconf['numberoptions']['item']) { foreach($dhcpv6ifconf['numberoptions']['item'] as $itemv6idx => $itemv6) { $dhcpdv6conf .= " option custom-{$dhcpv6if}-{$itemv6idx} \"{$itemv6['value']}\";\n"; } } // ldap-server if ($dhcpv6ifconf['ldap'] <> "") $dhcpdv6conf .= " option ldap-server \"{$dhcpv6ifconf['ldap']}\";\n"; // net boot information if(isset($dhcpv6ifconf['netboot'])) { if (($dhcpv6ifconf['next-server'] <> "") && ($dhcpv6ifconf['filename'] <> "")) { $dhcpdv6conf .= " next-server {$dhcpv6ifconf['next-server']};\n"; $dhcpdv6conf .= " filename \"{$dhcpv6ifconf['filename']}\";\n"; } if ($dhcpv6ifconf['rootpath'] <> "") { $dhcpdv6conf .= " option root-path \"{$dhcpv6ifconf['rootpath']}\";\n"; } } $dhcpdv6conf .= << "unmanaged") { $realif = escapeshellcmd(get_real_interface($dhcpv6if)); $dhcpdv6ifs[] = $realif; exec("/sbin/ifconfig {$realif} |awk '/ether/ {print $2}'", $mac); $v6address = generate_ipv6_from_mac($mac[0]); /* Create link local address for bridges */ if(stristr("$realif", "bridge")) { mwexec("/sbin/ifconfig {$realif} inet6 {$v6address}"); } } } fwrite($fd, $dhcpdconf); fclose($fd); fwrite($fdv6, $dhcpdv6conf); fclose($fdv6); /* create an empty leases database */ touch("{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases"); touch("{$g['varrun_path']}/dhcpd.pid"); /* create an empty leases v6 database */ touch("{$g['dhcpd_chroot_path']}/var/db/dhcpd6.leases"); touch("{$g['varrun_path']}/dhcpdv6.pid"); /* fire up dhcpd in a chroot */ if(count($dhcpdifs) > 0) { mwexec("/usr/local/sbin/dhcpd -user dhcpd -group _dhcp -chroot {$g['dhcpd_chroot_path']} -cf /etc/dhcpd.conf " . join(" ", $dhcpdifs)); } if(count($dhcpdv6ifs) > 0) { mwexec("/usr/local/sbin/dhcpd -6 -user dhcpd -group _dhcp -chroot {$g['dhcpd_chroot_path']} -cf /etc/dhcpdv6.conf " . join(" ", $dhcpdv6ifs)); mwexec("/usr/sbin/rtadvd " . join(" ", $dhcpdv6ifs)); } /* start ipv6 route advertising if required */ services_rtadvd_configure(); if ($g['booting']) { print "done.\n"; } return 0; } function services_igmpproxy_configure() { global $config, $g; /* kill any running igmpproxy */ killbyname("igmpproxy"); if (!is_array($config['igmpproxy']['igmpentry'])) return 1; $iflist = get_configured_interface_list(); $igmpconf = << "") { $item = explode(" ", $igmpcf['address']); foreach($item as $iww) $igmpconf .= "altnet {$iww}\n"; } $igmpconf .= "\n"; } foreach ($iflist as $ifn) { $realif = get_real_interface($ifn); $igmpconf .= "phyint {$realif} disabled\n"; } $igmpconf .= "\n"; $igmpfl = fopen($g['tmp_path'] . "/igmpproxy.conf", "w"); if (!$igmpfl) { log_error("Could not write Igmpproxy configuration file!"); return; } fwrite($igmpfl, $igmpconf); fclose($igmpfl); mwexec("/usr/local/sbin/igmpproxy -c " . $g['tmp_path'] . "/igmpproxy.conf"); log_error("Started Igmpproxy service sucsesfully."); return 0; } function interfaces_staticarp_configure($if) { global $config, $g; if(isset($config['system']['developerspew'])) { $mt = microtime(); echo "interfaces_staticarp_configure($if) being called $mt\n"; } $ifcfg = $config['interfaces'][$if]; if (empty($if) || empty($ifcfg['if'])) return 0; /* Enable staticarp, if enabled */ if(isset($config['dhcpd'][$if]['staticarp'])) { mwexec("/sbin/ifconfig " . escapeshellarg($ifcfg['if']) . " staticarp " ); mwexec("/usr/sbin/arp -d -i " . escapeshellarg($ifcfg['if']) . " -a > /dev/null 2>&1 "); if (is_array($config['dhcpd'][$if]['staticmap'])) { foreach ($config['dhcpd'][$if]['staticmap'] as $arpent) { mwexec("/usr/sbin/arp -s " . escapeshellarg($arpent['ipaddr']) . " " . escapeshellarg($arpent['mac'])); } } } else { mwexec("/sbin/ifconfig " . escapeshellarg($ifcfg['if']) . " -staticarp " ); mwexec("/usr/sbin/arp -d -i " . escapeshellarg($ifcfg['if']) . " -a > /dev/null 2>&1 "); } return 0; } function services_dhcrelay_configure() { global $config, $g; if(isset($config['system']['developerspew'])) { $mt = microtime(); echo "services_dhcrelay_configure() being called $mt\n"; } /* kill any running dhcrelay */ killbypid("{$g['varrun_path']}/dhcrelay.pid"); $dhcrelaycfg =& $config['dhcrelay']; /* DHCPRelay enabled on any interfaces? */ if (!isset($dhcrelaycfg['enable'])) return 0; if ($g['booting']) echo "Starting DHCP relay service..."; else sleep(1); $iflist = get_configured_interface_list(); $dhcifaces = explode(",", $dhcrelaycfg['interface']); foreach ($dhcifaces as $dhcrelayif) { if (!isset($iflist[$dhcrelayif]) || link_interface_to_bridge($dhcrelayif)) continue; if (is_ipaddr(get_interface_ip($dhcrelayif))) $dhcrelayifs[] = get_real_interface($dhcrelayif); } /* * In order for the relay to work, it needs to be active * on the interface in which the destination server sits. */ $srvips = explode(",", $dhcrelaycfg['server']); foreach ($srvips as $srcidx => $srvip) { unset($destif); foreach ($iflist as $ifname) { $subnet = get_interface_ip($ifname); if (!is_ipaddr($subnet)) continue; $subnet .= "/" . get_interface_subnet($ifname); if (ip_in_subnet($srvip, $subnet)) { $destif = get_real_interface($ifname); break; } } if (!isset($destif)) { if (is_array($config['staticroutes']['route'])) { foreach ($config['staticroutes']['route'] as $rtent) { if (ip_in_subnet($srvip, $rtent['network'])) { $a_gateways = return_gateways_array(true); $destif = $a_gateways[$rtent['gateway']]['interface']; break; } } } } if (!isset($destif)) { /* Create a array from the existing route table */ exec("/usr/bin/netstat -rnWf inet", $route_str); array_shift($route_str); array_shift($route_str); array_shift($route_str); array_shift($route_str); $route_arr = array(); foreach($route_str as $routeline) { $items = preg_split("/[ ]+/i", $routeline); if (ip_in_subnet($srvip, $items[0])) { $destif = trim($items[2]); break; } } } if (!isset($destif)) { if (is_array($config['gateways']['gateway_item'])) { foreach ($config['gateways']['gateway_item'] as $gateway) { if (isset($gateway['defaultgw'])) { $a_gateways = return_gateways_array(true); $destif = $a_gateways[$rtent['gateway']]['interface']; break; } } } else $destif = get_real_interface("wan"); } if (!empty($destif)) $dhcrelayifs[] = $destif; } $dhcrelayifs = array_unique($dhcrelayifs); /* fire up dhcrelay */ if (empty($dhcrelayifs)) { log_error("No suitable interface found for running dhcrelay!"); return; /* XXX */ } $cmd = "/usr/local/sbin/dhcrelay -i " . implode(" -i ", $dhcrelayifs); if (isset($dhcrelaycfg['agentoption'])) $cmd .= " -a -m replace"; $cmd .= " " . implode(" ", $srvips); mwexec($cmd); return 0; } function services_dyndns_configure_client($conf) { if (!isset($conf['enable'])) return; /* load up the dyndns.class */ require_once("dyndns.class"); log_error("DynDns: Running updatedns()"); $dns = new updatedns($dnsService = $conf['type'], $dnsHost = $conf['host'], $dnsUser = $conf['username'], $dnsPass = $conf['password'], $dnsWilcard = $conf['wildcard'], $dnsMX = $conf['mx'], $dnsIf = "{$conf['interface']}"); } function services_dyndns_configure($int = "") { global $config, $g; if(isset($config['system']['developerspew'])) { $mt = microtime(); echo "services_dyndns_configure() being called $mt\n"; } $dyndnscfg = $config['dyndnses']['dyndns']; if (is_array($dyndnscfg)) { if ($g['booting']) echo "Starting DynDNS clients..."; foreach ($dyndnscfg as $dyndns) { if (!empty($int) && $int != $dyndns['interface']) continue; services_dyndns_configure_client($dyndns); sleep(1); } if ($g['booting']) echo "done.\n"; } return 0; } function services_dnsmasq_configure() { global $config, $g; $return = 0; if(isset($config['system']['developerspew'])) { $mt = microtime(); echo "services_dnsmasq_configure() being called $mt\n"; } /* kill any running dnsmasq */ sigkillbypid("{$g['varrun_path']}/dnsmasq.pid", "TERM"); if (isset($config['dnsmasq']['enable'])) { if ($g['booting']) echo "Starting DNS forwarder..."; else sleep(1); /* generate hosts file */ if(system_hosts_generate()!=0) $return = 1; $args = ""; if (isset($config['dnsmasq']['regdhcp'])) { $args .= " --dhcp-hostsfile={$g['varetc_path']}/hosts "; } /* Setup forwarded domains */ if (isset($config['dnsmasq']['domainoverrides']) && is_array($config['dnsmasq']['domainoverrides'])) { foreach($config['dnsmasq']['domainoverrides'] as $override) { $args .= ' --server=/' . $override['domain'] . '/' . $override['ip']; } } /* Allow DNS Rebind for forwarded domains */ if (isset($config['dnsmasq']['domainoverrides']) && is_array($config['dnsmasq']['domainoverrides'])) { if(!isset($config['system']['webgui']['nodnsrebindcheck'])) { foreach($config['dnsmasq']['domainoverrides'] as $override) { $args .= ' --rebind-domain-ok=/' . $override['domain'] . '/ '; } } } if(!isset($config['system']['webgui']['nodnsrebindcheck'])) $dns_rebind = "--rebind-localhost-ok --stop-dns-rebind"; /* run dnsmasq */ mwexec("/usr/local/sbin/dnsmasq --local-ttl 1 --all-servers {$dns_rebind} --dns-forward-max=5000 --cache-size=10000 {$args}"); if ($g['booting']) echo "done.\n"; } if (!$g['booting']) { if(services_dhcpd_configure()!=0) $return = 1; } return $return; } function services_snmpd_configure() { global $config, $g; if(isset($config['system']['developerspew'])) { $mt = microtime(); echo "services_snmpd_configure() being called $mt\n"; } /* kill any running snmpd */ sigkillbypid("{$g['varrun_path']}/snmpd.pid", "TERM"); sleep(2); if(is_process_running("bsnmpd")) mwexec("/usr/bin/killall bsnmpd", true); if (isset($config['snmpd']['enable'])) { if ($g['booting']) echo "Starting SNMP daemon... "; /* generate snmpd.conf */ $fd = fopen("{$g['varetc_path']}/snmpd.conf", "w"); if (!$fd) { printf("Error: cannot open snmpd.conf in services_snmpd_configure().\n"); return 1; } $snmpdconf = << $dnsupdate) { if (!isset($dnsupdate['enable'])) continue; if (!empty($int) && $int != $dnsupdate['interface']) continue; /* determine interface name */ $if = get_real_interface($dnsupdate['interface']); $wanip = get_interface_ip($dnsupdate['interface']); if ($wanip) { $keyname = $dnsupdate['keyname']; /* trailing dot */ if (substr($keyname, -1) != ".") $keyname .= "."; $hostname = $dnsupdate['host']; /* trailing dot */ if (substr($hostname, -1) != ".") $hostname .= "."; /* write private key file this is dumb - public and private keys are the same for HMAC-MD5, but nsupdate insists on having both */ $fd = fopen("{$g['varetc_path']}/K{$i}{$keyname}+157+00000.private", "w"); $privkey .= << "on") return; $fd = fopen("{$g['varetc_path']}/olsr.conf", "w"); if($olsrd['announcedynamicroute'] or $olsrd['enableannounce'] == "on") { $enableannounce .= "\nHna4\n"; $enableannounce .= "{\n"; if($olsrd['announcedynamicroute']) $enableannounce .= "\t{$olsrd['announcedynamicroute']}\n"; if($olsrd['enableannounce'] == "on") $enableannounce .= "0.0.0.0 0.0.0.0"; $enableannounce .= "\n}\n"; } else { $enableannounce = ""; } $olsr .= <<0 # # defaults to 1 MprCoverage 3 # Example plugin entry with parameters: EODA; if($olsrd['enablehttpinfo'] == "on") { $olsr .= << 0) mwexec('killall miniupnpd 2>/dev/null', true); mwexec('/sbin/pfctl -aminiupnpd -Fr 2>&1 >/dev/null'); mwexec('/sbin/pfctl -aminiupnpd -Fn 2>&1 >/dev/null'); break; case "restart": upnp_action('stop'); upnp_action('start'); break; } } function upnp_start() { global $config; if(!isset($config['installedpackages']['miniupnpd']['config'])) return; if($config['installedpackages']['miniupnpd']['config'][0]['enable']) { echo "Starting UPnP service... "; require_once('/usr/local/pkg/miniupnpd.inc'); sync_package_miniupnpd(); echo "done.\n"; } } function install_cron_job($command, $active=false, $minute="0", $hour="*", $monthday="*", $month="*", $weekday="*", $who="root") { global $config, $g; $is_installed = false; if(!$config['cron']['item']) return; $x=0; foreach($config['cron']['item'] as $item) { if(strstr($item['command'], $command)) { $is_installed = true; break; } $x++; } if($active) { $cron_item = array(); $cron_item['minute'] = $minute; $cron_item['hour'] = $hour; $cron_item['mday'] = $monthday; $cron_item['month'] = $month; $cron_item['wday'] = $weekday; $cron_item['who'] = $who; $cron_item['command'] = $command; if(!$is_installed) { $config['cron']['item'][] = $cron_item; write_config("Installed cron job for {$command}"); } else { $config['cron']['item'][$x] = $cron_item; write_config("Updated cron job for {$command}"); } } else { if(($is_installed == true) && ($x > 0)) { unset($config['cron']['item'][$x]); write_config("Remvoed cron job for {$command}"); } } configure_cron(); } ?>