All rights reserved. Based on FreeNAS (http://www.freenas.org) Copyright (C) 2005-2006 Olivier Cochard-Labbé . All rights reserved. Based on m0n0wall (http://m0n0.ch/wall) Copyright (C) 2003-2006 Manuel Kasper . 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. */ /* ========================================================================== */ define ("FTP_BACKEND_PAM", "pam"); define ("FTP_BACKEND_PLAINTEXT", "plaintext"); define ("NFS_SERVICE_PORTS", "111 2049"); $freenas_config =& $config['installedpackages']['freenas']['config'][0]; /* Stop samba, Generate the samba configuration * file and start samba Return 0 if OK, 1 or * error code if error */ function services_samba_configure() { global $freenas_config, $g; /* kill any running samba */ killbyname("smbd"); killbyname("winbindd"); killbyname("nmbd"); if (isset($freenas_config['samba']['enable'])) { if ($g['booting']) { echo "Starting Samba... "; } /* make sure any of the required dirs exist */ if (! file_exists("{$g['varetc_path']}/private")) { mkdir("{$g['varetc_path']}/private"); } if (! file_exists("{$g['varlog_path']}/samba")) { mkdir("{$g['varlog_path']}/samba"); } /* generate smb.conf */ $fd = fopen("{$g['varetc_path']}/smb.conf", "w"); if (!$fd) { printf("Error: cannot open smb.conf in services_samba_configure().\n"); return 1; } /* If no share configured, exit */ if (!is_array($freenas_config['mounts']['mount'])) { return 0; } $gen_date = date("F j, Y, g:i a"); $sambaconf = << "") { $pfnetworks = array(); foreach (explode(",", $networks) as $netel) { list($network,$subnet) = explode('/', $netel); $subnet = gen_subnet_mask($subnet); $pfnetworks[] = $network; $tmp_nfsconf .= "{$nfsconf} -network {$network} -mask {$subnet}\n"; } $nfsconf = $tmp_nfsconf; } else { $nfsconf .= "\n"; } } fwrite($fd, $nfsconf); fclose($fd); $nfsdbindto = $freenas_config['nfs']['bindto'] = "(ANY)" ? " -a" : " -h {$freenas_config['nfs']['bindto']}"; $rpcbbindto = $freenas_config['nfs']['bindto'] = "(ANY)" ? "" : " -h {$freenas_config['nfs']['bindto']}"; $servetcp = isset($freenas_config['nfs']['servetcp']) ? " -t" : ""; $serveudp = isset($freenas_config['nfs']['serveudp']) ? " -u" : ""; /* run rpcbind, nfsd and mountd */ mwexec("/usr/sbin/rpcbind{$rpcbbindto}"); mwexec("/usr/sbin/nfsd{$nfsdbindto}{$serveudp}{$servetcp} -n 4"); mwexec("/usr/sbin/mountd -r {$g['varetc_path']}/exports"); mwexec("/usr/sbin/rpc.lockd"); mwexec("/usr/sbin/rpc.statd"); if ($g['booting']) { echo "done\n"; } } else { services_remove_transparency_for("nfs"); }// end if return 0; } function services_remove_transparency_for($whom) { $service_result = mwexec ("pfctl -a \"passin-package-freenas-{$whom}\" -F rules"); if($service_result <> 0) { file_notice("FREENAS", "There were error(s) flushing the exclude table", "FREENAS", ""); } } function services_setup_transparency_for($whom, $networks) { global $config, $freenas_config; if ($whom == "" || $networks == "") { return; } $generatedLANSubnet = gen_subnet($config['interfaces']['lan']['ipaddr'], $config['interfaces']['lan']['subnet']); $aliases = ""; /* stolen from filter.inc */ $real_wanif = get_real_wan_interface(); $wanip = find_interface_ip($real_wanif); $wan_aliases = " " . link_ip_to_carp_interface($wanip); if (link_int_to_bridge_interface("wan")) $wan_aliases .= " " . link_int_to_bridge_interface("wan"); if ($config['interfaces']['wan']['ipaddr'] == "pppoe" or $config['interfaces']['wan']['ipaddr'] == "pptp") { $aliases .= "ng0 = \"{ {$config['interfaces']['wan']['if']} {$real_wanif} }\" \n"; $aliases .= "wan = \"{ {$config['interfaces']['wan']['if']} {$wan_aliases} ng0 }\"\n"; } else { $aliases .= "wan = \"{ {$real_wanif} {$wan_aliases} }\"\n"; } $trans_file = fopen("/tmp/freenas-{$whom}.rules","w"); fwrite($trans_file, "{$aliases}\n"); switch ($whom) { case "nfs": $nfs_ports = explode(" ", NFS_SERVICE_PORTS); foreach ($nfs_ports as $port) { fwrite($trans_file, "pass in quick on \$wan proto { tcp udp } from { {$networks} } to {$freenas_config['nfs']['bindto']} port = {$port} keep state label \"FreeNAS related rule for {$whom}-{$port}\"\n"); } break; } fclose($trans_file); $service_result = mwexec ("pfctl -a \"passin-package-freenas-{$whom}\" -f /tmp/freenas-{$whom}.rules"); if($service_result <> 0) { file_notice("FREENAS", "There were error(s) loading the transparency rules", "FREENAS", ""); } //add_trans_table($whom); } function services_ftpd_configure() { global $freenas_config, $g; // services_vsftpd_configure(); // services_pureftpd_configure(); services_wzdftpd_configure(); } function services_wzdftpd_configure() { global $freenas_config, $config, $g; /* kill any WZDFTPD */ killbyname("wzdftpd"); if (isset($freenas_config['ftp']['enable'])) { if ($g['booting']) { echo "Starting FTP server... "; } /* create missing dirs if necessary */ if (! file_exists("/var/log/wzdftpd")) { mkdir("/var/log/wzdftpd"); } if (! file_exists("/usr/local/etc/wzdftpd")) { mkdir("/usr/local/etc/wzdftpd"); } /* generate wzd.cfg */ chmod("/usr/local/etc/wzdftpd/wzd.cfg", 0600); $fd = fopen("/usr/local/etc/wzdftpd/wzd.cfg", "w"); if (!$fd) { printf("Error: cannot open vsftpd.conf in services_vsftpd_configure().\n"); return 1; } $ftpconf = << 0) { $cron_min = $cron_min . "," . $minutev; } else { $cron_min = $minutev; } } // end foreach } else { $cron_min = "*"; } // end if if ($freenas_config['rsyncclient']['all_hours'] == "0") { foreach ($freenas_config['rsyncclient']['hour'] as $hourv) { if (strlen($cron_hour) > 0) { $cron_hour = $cron_hour . "," . $hourv; } else { $cron_hour = $hourv; } } // end foreach } else { $cron_hour = "*"; } // end if if ($freenas_config['rsyncclient']['all_days'] == "0") { foreach ($freenas_config['rsyncclient']['day'] as $dayv) { if (strlen($cron_day) > 0) { $cron_day = $cron_day . "," . $dayv; } else { $cron_day = $dayv; } } // end foreach } else { $cron_day = "*"; } // end if if ($freenas_config['rsyncclient']['all_months'] == "0") { foreach ($freenas_config['rsyncclient']['month'] as $monthv) { if (strlen($cron_month) > 0) { $cron_month = $cron_month . "," . $monthv; } else { $cron_month = $monthv; } } // end foreach } else { $cron_month = "*"; } // end if if ($freenas_config['rsyncclient']['all_weekday'] == "0") { foreach ($freenas_config['rsyncclient']['weekday'] as $weekdayv) { if (strlen($cron_weekday) > 0) { $cron_weekday = $cron_weekday . "," . $weekdayv; } else { $cron_weekday = $weekdayv; } } // end foreach } else { $cron_weekday = "*"; } // end if $cron_item = array(); $cron_item['minute'] = "{$cron_min}"; $cron_item['hour'] = "{$cron_hour}"; $cron_item['mday'] = "{$cron_day}"; $cron_item['month'] = "{$cron_month}"; $cron_item['wday'] = "{$cron_weekday}"; $cron_item['who'] = "root"; $cron_item['command'] = "{$g['varrun_path']}/sync.sh"; $config['cron']['item'][] = $cron_item; if ($g['booting']) { echo "done\n"; } } // end if /* Erase all variable used previously */ unset ($cron_min, $cron_hour, $cron_day, $cron_month, $cron_weekday); /* ======================================================================= */ /* = configure crontab item for rsync local part = */ /* ======================================================================= */ if (isset($freenas_config['rsync_local']['enable'])) { if ($g['booting']) { echo "Configuring cron for local rsync... "; } if ($freenas_config['rsync_local']['all_mins'] == "0") { foreach ($freenas_config['rsync_local']['minute'] as $minutev) { if (strlen($cron_min) > 0) { $cron_min = $cron_min . "," . $minutev; } else { $cron_min = $minutev; } } // end foreach } else { $cron_min = "*"; } // end if if ($freenas_config['rsync_local']['all_hours'] == "0") { foreach ($freenas_config['rsync_local']['hour'] as $hourv) { if (strlen($cron_hour) > 0) { $cron_hour = $cron_hour . "," . $hourv; } else { $cron_hour = $hourv; } } // end foreach } else { $cron_hour = "*"; } // end if if ($freenas_config['rsync_local']['all_days'] == "0") { foreach ($freenas_config['rsync_local']['day'] as $dayv) { if (strlen($cron_day) > 0) { $cron_day = $cron_day . "," . $dayv; } else { $cron_day = $dayv; } } // end foreach } else { $cron_day = "*"; } // end if if ($freenas_config['rsync_local']['all_months'] == "0") { foreach ($freenas_config['rsync_local']['month'] as $monthv) { if (strlen($cron_month) > 0) { $cron_month = $cron_month . "," . $monthv; } else { $cron_month = $monthv; } } // end foreach } else { $cron_month = "*"; } // end if if ($freenas_config['rsync_local']['all_weekday'] == "0") { foreach ($freenas_config['rsync_local']['weekday'] as $weekdayv) { if (strlen($cron_weekday) > 0) { $cron_weekday = $cron_weekday . "," . $weekdayv; } else { $cron_weekday = $weekdayv; } } // end foreach } else { $cron_weekday = "*"; } $cron_item = array(); $cron_item['minute'] = "{$cron_min}"; $cron_item['hour'] = "{$cron_hour}"; $cron_item['mday'] = "{$cron_day}"; $cron_item['month'] = "{$cron_month}"; $cron_item['wday'] = "{$cron_weekday}"; $cron_item['who'] = "root"; $cron_item['command'] = "{$g['varrun_path']}/sync.sh"; $config['cron']['item'][] = $cron_item; if ($g['booting']) { echo "done\n"; } } /* Erase all variable used previously */ unset ($cron_min, $cron_hour, $cron_day, $cron_month, $cron_weekday); /* ======================================================================= */ /* = configure crontab item shutdown part = */ /* ======================================================================= */ if (isset($freenas_config['shutdown']['enable'])) { if ($g['booting']) { echo "Configuring cron for shutdown... "; } if ($freenas_config['shutdown']['all_mins'] == "0") { foreach ($freenas_config['shutdown']['minute'] as $minutev) { if (strlen($cron_min) > 0) { $cron_min = $cron_min . "," . $minutev; } else { $cron_min = $minutev; } } // end foreach } else { $cron_min = "*"; } // end if if ($freenas_config['shutdown']['all_hours'] == "0") { foreach ($freenas_config['shutdown']['hour'] as $hourv) { if (strlen($cron_hour) > 0) { $cron_hour = $cron_hour . "," . $hourv; } else { $cron_hour = $hourv; } } // end foreach } else { $cron_hour = "*"; } // end if if ($freenas_config['shutdown']['all_days'] == "0") { foreach ($freenas_config['shutdown']['day'] as $dayv) { if (strlen($cron_day) > 0) { $cron_day = $cron_day . "," . $dayv; } else { $cron_day = $dayv; } } // end foreach } else { $cron_day = "*"; } // end if if ($freenas_config['shutdown']['all_months'] == "0") { foreach ($freenas_config['shutdown']['month'] as $monthv) { if (strlen($cron_month) > 0) { $cron_month = $cron_month . "," . $monthv; } else { $cron_month = $monthv; } } // end foreach } else { $cron_month = "*"; } // end if if ($freenas_config['shutdown']['all_weekday'] == "0") { foreach ($freenas_config['shutdown']['weekday'] as $weekdayv) { if (strlen($cron_weekday) > 0) { $cron_weekday = $cron_weekday . "," . $weekdayv; } else { $cron_weekday = $weekdayv; } } // end foreach } else { $cron_weekday = "*"; } // end if $cron_item = array(); $cron_item['minute'] = "{$cron_min}"; $cron_item['hour'] = "{$cron_hour}"; $cron_item['mday'] = "{$cron_day}"; $cron_item['month'] = "{$cron_month}"; $cron_item['wday'] = "{$cron_weekday}"; $cron_item['who'] = "root"; $cron_item['command'] = "{$g['varrun_path']}/sync.sh"; $config['cron']['item'][] = $cron_item; if ($g['booting']) { echo "done\n"; } } // end if /* call the pfSense configure_cron function */ configure_cron(); /* run cron */ mwexec("/usr/sbin/cron -s"); return 0; } function services_rsyncclient_configure() { global $freenas_config, $g; // Generate a shell script that is used by cron // Generate crontab works if (isset($freenas_config['rsyncclient']['enable'])) { if ($g['booting']) { echo "Generating synchronizing cron script... "; } /* generate /var/run/sync.sh script */ $fd = fopen("{$g['varrun_path']}/sync.sh", "w"); if (!$fd) { printf("Error: cannot open /var/run/sync.sh in services_rsyncclient_configure().\n"); return 1; } $syncscript = << $sharev) { $syncscript .= << @SERVICE_DESC@ on %h @TYPE@ @PORT@ @TXT_RECORDS@ EOD; /* kill any running dbus daemon */ sigkillbypid("{$g['varrun_path']}/dbus/dbus.pid", "TERM"); unlink_if_exists("{$g['varrun_path']}/dbus/dbus.pid"); /* kill any running avahi daemons */ mwexec("/usr/local/sbin/avahi-daemon -k"); /* simply return if zeroconf isn't enabled */ if (isset($config['system']['zeroconf_disable'])) { return 0; } /* configure Avahi and it's dependencies */ if (! file_exists("/usr/local/etc/avahi/services")) { mwexec("mkdir -p /usr/local/etc/avahi/services"); } /* add HTTP service type */ $webproto = $config['system']['webgui']['protocol']; if (isset($config['system']['webgui']['port'])) { $webservice = str_replace("@PORT@", $config['system']['webgui']['port'], $service_template); $webservice = str_replace("@TYPE@", "_{$webproto}._tcp", $webservice); $webservice = str_replace("@SERVICE_DESC@", "pfSense webGUI", $webservice); $webservice = str_replace("@TXT_RECORDS@", "", $webservice); file_put_contents("/usr/local/etc/avahi/services/http.service", $webservice); } else { $webservice = str_replace("@PORT@", "80", $service_template); $webservice = str_replace("@TYPE@", "_{$webproto}._tcp", $webservice); $webservice = str_replace("@SERVICE_DESC@", "pfSense webGUI", $webservice); $webservice = str_replace("@TXT_RECORDS@", "", $webservice); file_put_contents("/usr/local/etc/avahi/services/http.service", $webservice); } // end if /* afp is announcing itself */ if (isset($freenas_config['afp']['enable'])) { /* NOP */ } /* add rsync service type */ if (isset($freenas_config['rsyncd']['enable'])) { $rsyncservice = str_replace("@PORT@", $freenas_config['rsyncd']['port'], $service_template); $rsyncservice = str_replace("@TYPE@", "_rsync._tcp", $rsyncservice); $rsyncservice = str_replace("@SERVICE_DESC@", "Remote Sync Daemon", $rsyncservice); $rsyncservice = str_replace("@TXT_RECORDS@", "", $rsyncservice); file_put_contents("/usr/local/etc/avahi/services/rsync.service", $rsyncservice); } // end if /* add ntp service type */ if ($config['ntpd']['mode'] == "server") { $ntpdservice = str_replace("@PORT@", "123", $service_template); $ntpdservice = str_replace("@TYPE@", "_ntp._udp", $ntpdservice); $ntpdservice = str_replace("@SERVICE_DESC@", "Network Time Daemon", $ntpdservice); $ntpdservice = str_replace("@TXT_RECORDS@", "", $ntpdservice); file_put_contents("/usr/local/etc/avahi/services/ntpd.service", $ntpdservice); } // end if if (isset($config['system']['ssh']['enable'])) { $sshservice = str_replace("@PORT@", "22", $service_template); $sshservice = str_replace("@TYPE@", "_ssh._tcp", $sshservice); $sshservice = str_replace("@SERVICE_DESC@", "Remote Terminal", $sshservice); $sshservice = str_replace("@TXT_RECORDS@", "", $sshservice); file_put_contents("/usr/local/etc/avahi/services/ssh.service", $sshservice); /* do the same for sftp, cause we do enable it by dfeault */ $sftpservice = str_replace("@PORT@", "22", $service_template); $sftpservice = str_replace("@TYPE@", "_ssh._tcp", $sftpservice); $sftpservice = str_replace("@SERVICE_DESC@", "Remote Terminal", $sftpservice); $sftpservice = str_replace("@TXT_RECORDS@", "", $sftpservice); file_put_contents("/usr/local/etc/avahi/services/ssh-sftp.service", $sftpservice); } // end if /* we are using wzdftpd, which has the ability to announce itself */ if (isset($freenas_config['ftp']['enable'])) { /* NOP */ } if (isset($freenas_config['samba']['enable'])) { $cifsservice = str_replace("@PORT@", "139", $service_template); $cifsservice = str_replace("@TYPE@", "_smb._tcp", $cifsservice); $cifsservice = str_replace("@SERVICE_DESC@", "Samba Server", $cifsservice); $cifsservice = str_replace("@TXT_RECORDS@", "", $cifsservice); file_put_contents("/usr/local/etc/avahi/services/cifs.service", $cifsservice); } if (isset($freenas_config['nfs']['enable'])) { /* If no share configured, exit */ if (!is_array($freenas_config['mounts']['mount'])) { break; } $a_mount = &$freenas_config['mounts']['mount']; foreach ($a_mount as $mount) { $sharename = str_replace(" ", "", $mount['sharename']); $sharename = strtolower($sharename); $nfsservice = str_replace("@PORT@", "2049", $service_template); $nfsservice = str_replace("@TYPE@", "_nfs._tcp", $nfsservice); $nfsservice = str_replace("@SERVICE_DESC@", "NFS Mount: {$mount['sharename']}", $nfsservice); $nfsservice = str_replace("@TXT_RECORDS@", "path=/mnt/{$mount['sharename']}", $nfsservice); file_put_contents("/usr/local/etc/avahi/services/{$sharename}_nfs.service", $nfsservice); } // end foreach } // end if /* run dbus */ mwexec("/usr/local/bin/dbus-daemon --system"); /* run avahi */ mwexec("/usr/local/sbin/avahi-daemon -D"); if ($g['booting']) { echo "done\n"; } return 0; } /* this function is currently not used */ function services_howl_configure() { global $freenas_config, $config, $g; /* kill any mDNSResponder */ killbyname("mDNSResponder"); if (isset($config['system']['zeroconf_disable'])) { return 0; } $fd = fopen("{$g['varetc_path']}/mDNSResponder.conf", "w"); if (!$fd) { printf("Error: cannot open mDNSResponder.conf in services_howl_configure().\n"); return 1; } if ($config['system']['webgui']['port']) { $mDNSResponder = <<