function BuildRules() { $unix = new unix(); $sock = new sockets(); if (system_is_overloaded(basename(__FILE__))) { system_admin_events("Overloaded system, aborting...", __FUNCTION__, __FILE__, __LINE__, "fetchmail"); die; } $EnableFetchmailScheduler = $sock->GET_INFO("EnableFetchmailScheduler"); $EnablePostfixMultiInstance = $sock->GET_INFO("EnablePostfixMultiInstance"); if (!is_numeric($EnableFetchmailScheduler)) { $EnableFetchmailScheduler = 0; } if (!is_numeric($EnablePostfixMultiInstance)) { $EnablePostfixMultiInstance = 0; } $DenyFetchMailWriteConf = $sock->GET_INFO("DenyFetchMailWriteConf"); if (!is_numeric($DenyFetchMailWriteConf)) { $DenyFetchMailWriteConf = 0; } if (!isset($GLOBALS["FetchMailGLobalDropDelivered"])) { $sock = new sockets(); $GLOBALS["FetchMailGLobalDropDelivered"] = $sock->GET_INFO("FetchMailGLobalDropDelivered"); if (!is_numeric($GLOBALS["FetchMailGLobalDropDelivered"])) { $GLOBALS["FetchMailGLobalDropDelivered"] = 0; } } if ($DenyFetchMailWriteConf == 1) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} saving configuration denied (DenyFetchMailWriteConf)\n"; return true; } @file_put_contents("/proc/sys/net/ipv4/tcp_timestamps", "0"); if ($EnableFetchmailScheduler == 1) { BuildRules_schedule(); return; } foreach (glob("/etc/cron.d/fetchmail*") as $filename) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} removing {$filename}..\n"; @unlink($filename); } $fetch = new fetchmail(); $l[] = "set logfile /var/log/fetchmail.log"; $l[] = "set daemon {$fetch->FetchmailPoolingTime}"; $l[] = "set postmaster \"{$fetch->FetchmailDaemonPostmaster}\""; $l[] = "set idfile \"/var/log/fetchmail.id\""; $l[] = ""; $sql = "SELECT * FROM fetchmail_rules WHERE enabled=1"; $q = new mysql(); $results = $q->QUERY_SQL($sql, "artica_backup"); if (!$q->ok) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} saving configuration file FAILED\n"; return false; } echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} building " . mysql_num_rows($results) . " rules...\n"; $array = array(); while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) { $ID = $ligne["ID"]; $pattern = build_line($ligne); $l[] = $pattern; $GLOBALS["FETCHMAIL_RULES_ID"][$ID] = $pattern; } if ($GLOBALS["SINGLE_DEBUG"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} single-debug, aborting nex step\n"; return; } if ($EnablePostfixMultiInstance == 1) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} postfix multiple instances enabled (" . count($GLOBALS["multi_smtp"]) . ") hostnames\n"; @unlink("/etc/artica-postfix/fetchmail.schedules"); if (is_array($GLOBALS["multi_smtp"])) { if ($GLOBALS["DEBUG"]) { print_r($GLOBALS["multi_smtp"]); } while (list($hostname, $rules) = each($GLOBALS["multi_smtp"])) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} {$hostname} save rules...\n"; @file_put_contents("/etc/postfix-{$hostname}/fetchmail.rc", @implode("\n", $rules)); @chmod("/etc/postfix-{$hostname}/fetchmail.rc", 0600); $schedule[] = multi_build_schedule($hostname); if (!is_fetchmailset($hostname)) { $restart = true; } else { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} {$hostname} already scheduled...\n"; } } if ($restart) { @file_put_contents("/etc/artica-postfix/fetchmail.schedules", @implode("\n", $schedule)); system("/etc/init.d/artica-postfix restart fcron"); } } return; } if (is_file("/etc/fetchmail.perso")) { $l[] = "# fetchmail.perso content"; $l[] = "# Save a configuration file in /etc/fetchmail.perso"; $l[] = @file_get_contents("/etc/fetchmail.perso"); } if (is_array($l)) { $conf = implode("\n", $l); echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} building /etc/fetchmailrc " . count($l) . " lines\n"; } else { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} building /etc/fetchmailrc 0 lines\n"; $conf = null; } @file_put_contents("/etc/fetchmailrc", $conf); shell_exec("/bin/chmod 600 /etc/fetchmailrc"); echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} saving /etc/fetchmailrc configuration file done\n"; build_monit(); if ($GLOBALS["RELOAD"]) { if ($EnablePostfixMultiInstance == 0) { reload(); } } }
function BuildRules() { $sock = new sockets(); $EnablePostfixMultiInstance = $sock->GET_INFO("EnablePostfixMultiInstance"); $fetch = new fetchmail(); $l[] = "set logfile /var/log/fetchmail.log"; $l[] = "set daemon {$fetch->FetchmailPoolingTime}"; $l[] = "set postmaster \"{$fetch->FetchmailDaemonPostmaster}\""; $l[] = "set idfile \"/var/log/fetchmail.id\""; $l[] = ""; $sql = "SELECT * FROM fetchmail_rules WHERE enabled=1"; $q = new mysql(); $results = $q->QUERY_SQL($sql, "artica_backup"); if (!$q->ok) { echo "Starting......: fetchmail saving configuration file FAILED\n"; return false; } $array = array(); while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) { $ligne["poll"] = trim($ligne["poll"]); if ($ligne["poll"] == null) { continue; } if ($ligne["proto"] == null) { continue; } if ($ligne["uid"] == null) { continue; } $user = new user($ligne["uid"]); $ligne["is"] = $user->mail; $smtphost = null; $sslfingerprint = null; $fetchall = null; $timeout = null; $port = null; $aka = null; $folder = null; $tracepolls = null; $interval = null; $keep = null; $fetchall = null; $sslcertck = null; if ($ligne["proto"] == "httpp") { $ligne["proto"] = "pop3"; } if (trim($ligne["port"]) > 0) { $port = "port {$ligne["port"]}"; } if (trim($ligne["aka"]) != null) { $aka = "\n\taka {$ligne["aka"]}"; } if ($ligne["ssl"] == 1) { $ssl = "\n\tssl\n\tsslproto ''"; } if ($ligne["timeout"] > 0) { $timeout = "\n\ttimeout {$ligne["timeout"]}"; } if ($ligne["folder"] != null) { $folder = "\n\tfolder {$ligne["folder"]}"; } if ($ligne["tracepolls"] == 1) { $tracepolls = "\n\ttracepolls"; } if ($ligne["interval"] > 0) { $interval = "\n\\tinterval {$ligne["interval"]}"; } if ($ligne["keep"] == 1) { $keep = "\n\tkeep "; } if ($ligne["nokeep"] == 1) { $keep = "\n\tnokeep"; } if ($ligne["multidrop"] == 1) { $ligne["is"] = "*"; } if ($ligne["fetchall"] == 1) { $fetchall = "\n\tfetchall"; } if (strlen(trim($ligne["sslfingerprint"])) > 10) { $sslfingerprint = "\n\tsslfingerprint '{$ligne["sslfingerprint"]}'"; } if ($ligne["sslcertck"] == 1) { $sslcertck = "\n\tsslcertck"; } if ($EnablePostfixMultiInstance == 1) { if ($GLOBALS["DEBUG"]) { echo "multiple instances::poll={$ligne["poll"]} smtp_host={$ligne["smtp_host"]}\n"; } if (strlen(trim($ligne["smtp_host"])) == 0) { continue; } $smtphost = "\n\tsmtphost " . multi_get_smtp_ip($ligne["smtp_host"]); } if (trim($ssl) == null) { $ssl = "\n\tsslproto ssl23\n\tno ssl"; } $pattern = "poll {$ligne["poll"]}{$tracepolls}\n\tproto {$ligne["proto"]} {$port}\n\tuser \"{$ligne["user"]}\"\n\tpass {$ligne["pass"]}\n\tis {$ligne["is"]}{$aka}{$folder}{$ssl}{$fetchall}{$interval}{$timeout}{$keep}{$multidrop}{$sslfingerprint}{$sslcertck}{$smtphost}\n\n"; if ($GLOBALS["DEBUG"]) { echo "{$pattern}\n"; } $multi_smtp[$ligne["smtp_host"]][] = $pattern; $l[] = $pattern; } if ($EnablePostfixMultiInstance == 1) { echo "Starting......: fetchmail postfix multiple instances enabled (" . count($multi_smtp) . ") hostnames\n"; @unlink("/etc/artica-postfix/fetchmail.schedules"); if (is_array($multi_smtp)) { if ($GLOBALS["DEBUG"]) { print_r($multi_smtp); } while (list($hostname, $rules) = each($multi_smtp)) { echo "Starting......: fetchmail {$hostname} save rules...\n"; @file_put_contents("/etc/postfix-{$hostname}/fetchmail.rc", @implode("\n", $rules)); @chmod("/etc/postfix-{$hostname}/fetchmail.rc", 0600); $schedule[] = multi_build_schedule($hostname); if (!is_fetchmailset($hostname)) { $restart = true; } else { echo "Starting......: fetchmail {$hostname} already scheduled...\n"; } } if ($restart) { @file_put_contents("/etc/artica-postfix/fetchmail.schedules", @implode("\n", $schedule)); system("/etc/init.d/artica-postfix restart fcron"); } } return; } if (is_array($l)) { $conf = implode("\n", $l); } else { $conf = null; } @file_put_contents("/etc/fetchmailrc", $conf); @chmod("/etc/fetchmailrc", 600); echo "Starting......: fetchmail saving configuration file done\n"; }