. 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. */ /* include all configuration functions */ require_once("functions.inc"); function load_balancer_use_sticky() { global $config, $g; if (isset ($config['system']['lb_use_sticky'])) touch("/var/etc/use_pf_pool__stickyaddr"); else unlink_if_exists("/var/etc/use_pf_pool__stickyaddr"); } function services_dhcpd_configure() { global $config, $g; if(isset($config['system']['developerspew'])) { $mt = microtime(); echo "services_dhcpd_configure($if) being called $mt\n"; } /* 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("/tmp/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 /tmp/dhcpd.sh"); /* kill any running dhcpd */ if(is_process_running("dhcpd")) mwexec("killall dhcpd", true); $syscfg = $config['system']; $dhcpdcfg = $config['dhcpd']; /* DHCP enabled on any interfaces? */ $dhcpdenable = false; if(is_array($dhcpdcfg)) 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; } if (!$dhcpdenable) return 0; if ($g['booting']) echo "Starting DHCP service..."; else sleep(1); /* write dhcpd.conf */ $fd = fopen("{$g['dhcpd_chroot_path']}/etc/dhcpd.conf", "w"); if (!$fd) { printf("Error: cannot open dhcpd.conf in services_dhcpd_configure().\n"); return 1; } $dhcpdconf = << $dhcpifconf) { if($dhcpifconf['failover_peerip'] <> "") { /* * 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) { $int = guess_interface_from_ip($dhcpifconf['failover_peerip']); $intip = find_interface_ip($int); $real_dhcpif = convert_friendly_interface_to_real_interface_name($dhcpif); if($int == $real_dhcpif) { /* this is the interface! */ if($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 .= << $dhcpifconf) { $ifcfg = $config['interfaces'][$dhcpif]; if (!isset($dhcpifconf['enable']) || ($ifcfg['ipaddr'] == "dhcp") || (($dhcpif != "lan") && (!isset($ifcfg['enable']) || !$ifcfg['if'] || $ifcfg['bridge']))) continue; if($dhcpif == "lan" && $ifcfg['bridge']) log_error("NOTE: DHCP Server on LAN is enabled."); $subnet = gen_subnet($ifcfg['ipaddr'], $ifcfg['subnet']); $subnetmask = gen_subnet_mask($ifcfg['subnet']); 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 (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 " . $ifcfg['ipaddr'] . ";"; } 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 = $ifcfg['ipaddr']; if($dhcpifconf['failover_peerip'] <> "") { $dhcpdconf .= " failover peer \"dhcp{$dhcpnum}\";\n"; $dhcpnum++; } $dhcpdconf .= << "") && ($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 .= << /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'])); log_error("/usr/sbin/arp -s " . escapeshellarg($arpent['ipaddr']) . " " . escapeshellarg($arpent['mac'])); } } } else { mwexec("/sbin/ifconfig " . escapeshellarg($ifcfg['if']) . " -staticarp " ); mwexec("/usr/sbin/arp -da > /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? */ $dhcrelayenable = false; if(is_array($dhcrelaycfg)) { foreach ($dhcrelaycfg as $dhcrelayif => $dhcrelayifconf) { if (isset($dhcrelayifconf['enable']) && (($dhcrelayif == "lan") || (isset($config['interfaces'][$dhcrelayif]['enable']) && $config['interfaces'][$dhcrelayif]['if'] && (!$config['interfaces'][$dhcrelayif]['bridge'])))) $dhcrelayenable = true; } } if (!$dhcrelayenable) return 0; if ($g['booting']) echo "Starting DHCP relay service..."; else sleep(1); $dhcrelayifs = array(); foreach ($dhcrelaycfg as $dhcrelayif => $dhcrelayifconf) { $ifcfg = $config['interfaces'][$dhcrelayif]; if (!isset($dhcrelayifconf['enable']) || (($dhcrelayif != "lan") && (!isset($ifcfg['enable']) || !$ifcfg['if'] || $ifcfg['bridge']))) continue; $dhcrelayifs[] = $ifcfg['if']; } /* In order for the relay to work, it needs to be active on the interface in which the destination server sits */ foreach ($config['interfaces'] as $ifname) { $subnet = $ifname['ipaddr'] . "/" . $ifname['subnet']; if (ip_in_subnet($dhcrelaycfg['server'],$subnet)) $destif = $ifname['if']; } if (!isset($destif)) $destif = $config['interfaces']['wan']['if']; $dhcrelayifs[] = $destif; $dhcrelayifs = array_unique($dhcrelayifs); /* fire up dhcrelay */ $cmd = "/usr/local/sbin/dhcrelay -i " . join(" -i ", $dhcrelayifs); if (isset($dhcrelaycfg['agentoption'])) $cmd .= " -a -m replace"; $cmd .= " {$dhcrelaycfg['server']}"; mwexec($cmd); if (!$g['booting']) { /* set the reload filter dity flag */ touch("{$g['tmp_path']}/filter_dirty"); } return 0; } function services_dyndns_reset() { global $config, $g; if(isset($config['system']['developerspew'])) { $mt = microtime(); echo "services_dyndns_reset() being called $mt\n"; } if (file_exists("{$g['vardb_path']}/ez-ipupdate.cache")) { conf_mount_rw(); unlink("{$g['vardb_path']}/ez-ipupdate.cache"); conf_mount_ro(); } if (file_exists("{$g['conf_path']}/ez-ipupdate.cache")) { conf_mount_rw(); unlink("{$g['conf_path']}/ez-ipupdate.cache"); conf_mount_ro(); } if (file_exists("{$g['conf_path']}/dyndns.cache")) { conf_mount_rw(); unlink("{$g['conf_path']}/dyndns.cache"); conf_mount_ro(); } return 0; } function services_dyndns_configure() { global $config, $g; if(isset($config['system']['developerspew'])) { $mt = microtime(); echo "services_dyndns_configure() being called $mt\n"; } $dyndnscfg = $config['dyndns']; $wancfg = $config['interfaces']['wan']; if (isset($dyndnscfg['enable'])) { if ($g['booting']) { echo "Starting DynDNS client..."; if(isset($config['system']['use_old_dyndns'])) { echo " [Using ez-ipupdate] "; services_dyndns_configure_old(); return; } } else { sleep(1); if(isset($config['system']['use_old_dyndns'])) { services_dyndns_configure_old(); return; } } /* load up the dyndns.class */ require_once("dyndns.class"); log_error("DynDns: Running updatedns()"); /* determine WAN interface name */ $wanif = get_real_wan_interface(); /* get ip */ $ip = find_interface_ip($wanif); $dns = new updatedns($dnsService = $config['dyndns']['type'], $dnsHost = $config['dyndns']['host'], $dnsUser = $config['dyndns']['username'], $dnsPass = $config['dyndns']['password'], $dnsWilcard = $config['dyndns']['wildcard'], $dnsMX = $config['dyndns']['mx']); if ($g['booting']) echo "done.\n"; } return 0; } function services_dyndns_configure_old() { global $config, $g; if(isset($config['system']['developerspew'])) { $mt = microtime(); echo "services_dyndns_configure_old() being called $mt\n"; } /* kill any running ez-ipupdate */ /* ez-ipupdate needs SIGQUIT instead of SIGTERM */ sigkillbypid("{$g['varrun_path']}/ez-ipupdate.pid", "QUIT"); $dyndnscfg = $config['dyndns']; $wancfg = $config['interfaces']['wan']; if (isset($dyndnscfg['enable'])) { if ($g['booting']) echo "Starting DynDNS client..."; else sleep(1); /* determine WAN interface name */ $wanif = get_real_wan_interface(); /* write ez-ipupdate.conf */ $fd = fopen("{$g['varetc_path']}/ez-ipupdate.conf", "w"); if (!$fd) { printf("Error: cannot open ez-ipupdate.conf in services_dyndns_configure().\n"); return 1; } $ezipupdateconf = << $paents) { if ($paif == "wan" && !(is_ipaddr($config['interfaces']['wan']['ipaddr']) || ($config['interfaces']['wan']['ipaddr'] == "dhcp") || ($config['interfaces']['wan']['ipaddr'] == "bigpond"))) continue; $args = $config['interfaces'][$paif]['if'] . " auto"; foreach ($paents as $paent) { if (isset($paent['subnet'])) $args .= " " . escapeshellarg("{$paent['subnet']}/{$paent['subnet_bits']}"); else if (isset($paent['range'])) $args .= " " . escapeshellarg($paent['range']['from'] . "-" . $paent['range']['to']); } mwexec_bg("/usr/local/sbin/choparp " . $args); } } } function services_dnsupdate_process() { global $config, $g; if(isset($config['system']['developerspew'])) { $mt = microtime(); echo "services_dnsupdate_process() being called $mt\n"; } /* Dynamic DNS updating active? */ if (isset($config['dnsupdate']['enable'])) { $wanip = get_current_wan_address(); if ($wanip) { $keyname = $config['dnsupdate']['keyname']; /* trailing dot */ if (substr($keyname, -1) != ".") $keyname .= "."; $hostname = $config['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{$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, $g; if($config['installedpackages']['miniupnpd']['config'][0]['enable']) { if($g['booting']) { echo "Starting UPnP service..."; include('/usr/local/pkg/miniupnpd.inc'); sync_package_miniupnpd(); echo "done.\n"; } else { upnp_action('start'); } } } ?>