/dev/null /usr/bin/killall spamlogd 2>/dev/null /usr/bin/killall spamd 2>/dev/null sleep 3 if [ `/sbin/mount | /usr/bin/grep '[f]descfs' | /usr/bin/wc -l` -gt 0 ]; then /sbin/umount /dev/fd fi EOF; log_error("[spamd] Writing rc_file"); write_rcfile($rc); log_error("[spamd] Installing cron job"); install_cron_job("/usr/bin/nice -n20 /usr/local/sbin/spamd-setup -d -D {$spamd_setup_opts}", true, "*/120"); log_error("[spamd] Setting up spamd.conf symlink"); unlink_if_exists("{$localpath}/etc/spamd/spamd.conf"); symlink("/etc/spamd.conf", "{$localpath}/etc/spamd/spamd.conf"); conf_mount_ro(); log_error("[spamd] (Re)starting spamd"); if (is_service_running("spamd")) { restart_service("spamd"); } else { start_service("spamd"); } log_error("[spamd] Reconfiguring filter"); filter_configure(); log_error("[spamd] Package setup completed"); } function spamd_enabled() { global $config, $spamd_enabled; $spamd_enabled = false; if (is_array($config['installedpackages']['spamdsettings'])) { $spamdsettings = $config['installedpackages']['spamdsettings']['config'][0]; } else { $spamdsettings = array(); } if ($spamdsettings['spamd_enable'] == "on") { $spamd_enabled = true; } return $spamd_enabled; } function sync_package_spamd_whitelist() { global $config; conf_mount_rw(); /* write out ip to the whitelist db */ $fd = fopen("/var/db/whitelist.txt","w"); if ($config['installedpackages']['spamdwhitelist']['config'] != "") { foreach ($config['installedpackages']['spamdwhitelist']['config'] as $spamd) { fwrite($fd, $spamd['ip'] . "\n"); } } fclose($fd); /* signal a reload of all files */ mwexec("/usr/bin/killall -HUP spamlogd"); mwexec("/sbin/pfctl -t spamd-white -T add {$spamd['ip']}"); conf_mount_ro(); } function spamd_generate_rules($type) { global $config; // do not install any rules if disabled if (!spamd_enabled()) { return; } $natrules = ""; switch($type) { case 'nat': $natrules .= "\n# spam table \n"; $natrules .= "table persist\n"; $natrules .= "table persist\n"; $natrules .= "table persist\n"; if (file_exists("/var/db/whitelist.txt")) { $natrules .= "table persist file \"/var/db/whitelist.txt\"\n"; } if (is_array($config['installedpackages']['spamdsettings']['config'])) { $spamdconfig = $config['installedpackages']['spamdsettings']['config']['0']; } else { $spamdconfig = $config['installedpackages']['spamdsettings']['config']; } if ($spamdconfig) { $nextmta = $spamdconfig['next_mta']; $spamdbinds = explode(',', $spamdconfig['spamdbinds_array']); if (is_array($spamdbinds)) { foreach ($spamdbinds as $interface) { $wanif = get_real_interface($interface); if (!isset($wanif)) { continue; } $natrules .= "rdr pass on {$wanif} proto tcp from to port smtp -> 127.0.0.1 port spamd\n"; $natrules .= "rdr pass on {$wanif} proto tcp from to port smtp -> 127.0.0.1 port spamd\n"; $natrules .= "rdr pass on {$wanif} proto tcp from ! to port smtp -> 127.0.0.1 port spamd\n"; if ($nextmta != "") { $natrules .= "rdr pass on {$wanif} proto tcp from to port smtp -> {$nextmta} port smtp\n"; } } } } $natrules .= "\n"; break; } return $natrules; } function remove_spaces($string) { $string = str_replace(" ", "", $string); return $string; } function sync_spamd_config_to_backup() { global $config; if (is_array($config['installedpackages']['carpsettings']['config'])) { foreach ($config['installedpackages']['carpsettings']['config'] as $carp) { if ($carp['synchronizetoip'] != "") { $synctoip = $carp['synchronizetoip']; $password = $carp['password']; if ($config['system']['username']) { $username = $config['system']['username']; } else { $username = "admin"; } } } } if ($synctoip and $password) { if ($config['system']['webgui']['protocol'] != "") { $synchronizetoip = $config['system']['webgui']['protocol']; $synchronizetoip .= "://"; } $port = $config['system']['webgui']['port']; /* if port is empty lets rely on the protocol selection */ if ($port == "") { if ($config['system']['webgui']['protocol'] == "http") { $port = "80"; } else { $port = "443"; } } $params = array(XML_RPC_encode($password),XML_RPC_encode($xml)); /* create files to sync array */ $filetosync = array("/var/db/spamd", "/var/db/whitelist.txt"); /* loop through files to sync list and sync them up */ foreach ($filetosync as $f2s) { $f2c_contents = file_get_contents($f2s); xmlrpc_sync_file($url, $password, $f2s, $f2c_contents, $port); } /* signal remote process config reload */ xmlrpc_exec_shell($url, $password, "/usr/bin/killall -HUP spamlogd", $port); xmlrpc_exec_shell($url, $password, "/usr/bin/killall -HUP spamd", $port); } } function custom_php_install_command() { global $config; system("touch /var/db/whitelist.txt"); system("touch /var/db/blacklist.txt"); exec("/usr/sbin/pw usermod _spamd -g proxy -G _spamd,proxy"); sync_package_spamd(); } function custom_php_deinstall_command() { global $config; unlink_if_exists("/usr/local/pkg/pf/spamd_rules.php"); install_cron_job("/usr/bin/nice -n20 /usr/local/sbin/spamd-setup", false); /* clean up user/groups */ if (exec("/usr/sbin/pw groupshow proxy | /usr/bin/grep _spamd")) { exec ("/usr/sbin/pw groupmod proxy -d _spamd"); } if (exec("/usr/sbin/pw usershow _spamd")) { exec("/usr/sbin/pw userdel _spamd"); } if (exec("/usr/sbin/pw groupshow _spamd")) { exec("/usr/sbin/pw groupdel _spamd"); } /* unmount fdescfs if needed */ if (trim(shell_exec("/sbin/mount | /usr/bin/grep '[f]descfs' | /usr/bin/wc -l")) != 0) { log_error("[spamd] Unmounting fdescfs."); mwexec("/sbin/umount /dev/fd"); } unlink_if_exists("/var/db/spamd"); filter_configure(); } function spamd_validate_input($post, &$input_errors) { if (!empty($post["next_mta"])) { if (!is_ipaddrv4($post['next_mta'])) { $input_errors[] = "'NextMTA' is not a valid IPv4 address"; } } if (isset($post['maxblack']) && $post['maxblack'] != "") { if (!is_numericint($post['maxblack']) || $post['maxblack'] < 1) { $input_errors[] = "'Maximum Blacklisted Connections' must be a positive integer or empty."; } } if (isset($post['maxcon']) && $post['maxcon'] != "") { if (!is_numericint($post['maxcon']) || $post['maxcon'] < 1) { $input_errors[] = "'Max Concurrent Connections' must be a positive integer or empty."; } } if (!empty($post['maxblack']) && is_numericint($post['maxblack']) && !empty($post['maxcon']) && is_numericint($post['maxcon'])) { if ($post['maxblack'] > $post['maxcon']) { $input_errors[] = "'Maximum Blacklisted Connections' must not be higher than 'Max Concurrent Connections'."; } } if (isset($post['passtime']) && $post['passtime'] != "") { if (!is_numericint($post['passtime']) || $post['passtime'] < 1) { $input_errors[] = "'Passtime' must be a positive integer or empty."; } } if (isset($post['greyexp']) && $post['greyexp'] != "") { if (!is_numericint($post['greyexp']) || $post['greyexp'] < 1) { $input_errors[] = "'Grey Expiration' must be a positive integer or empty."; } } if (isset($post['whiteexp']) && $post['whiteexp'] != "") { if (!is_numericint($post['whiteexp']) || $post['whiteexp'] < 1) { $input_errors[] = "'White Exp' must be a positive integer or empty."; } } if (isset($post['stuttersecs']) && $post['stuttersecs'] != "") { if (!is_numericint($post['stuttersecs']) || $post['stuttersecs'] < 1) { $input_errors[] = "'Stutter Secs' must be a positive integer or empty."; } } if (isset($post['delaysecs']) && $post['delaysecs'] != "") { if (!is_numericint($post['delaysecs']) || $post['delaysecs'] < 1) { $input_errors[] = "'Delay Secs' must be a positive integer or empty."; } } if (isset($post['window']) && $post['window'] != "") { if (!is_numericint($post['window']) || $post['window'] < 1) { $input_errors[] = "'Window Size' must be a positive integer or empty."; } } } ?>