X-Git-Url: https://vcs.fsf.org/?p=squirrelmail.git;a=blobdiff_plain;f=plugins%2Ffilters%2Ffilters.php;h=8b017c968bbfa85e21d79677a218523ed721fd54;hp=1550fa541d299dd9b79a4affe52ca55408d8db99;hb=18b078e93c1fa217a36cc5f5749fc3d3568f079a;hpb=4eec80a7296fd32006c010387d8ead6cbc2847d8 diff --git a/plugins/filters/filters.php b/plugins/filters/filters.php index 1550fa54..8b017c96 100644 --- a/plugins/filters/filters.php +++ b/plugins/filters/filters.php @@ -25,6 +25,144 @@ * $Id$ */ +function filters_SaveCache () { + global $data_dir, $SpamFilters_DNScache; + + if (file_exists($data_dir . "/dnscache")) { + $fp = fopen($data_dir . "/dnscache", "r"); + } else { + $fp = false; + } + if ($fp) { + flock($fp,LOCK_EX); + } else { + $fp = fopen($data_dir . "/dnscache", "w+"); + fclose($fp); + $fp = fopen($data_dir . "/dnscache", "r"); + flock($fp,LOCK_EX); + } + $fp1=fopen($data_dir . "/dnscache", "w+"); + + foreach ($SpamFilters_DNScache as $Key=> $Value) { + $tstr = $Key . ',' . $Value['L'] . ',' . $Value['T'] . "\n"; + fputs ($fp1, $tstr); + } + fclose($fp1); + flock($fp,LOCK_UN); + fclose($fp); +} + + +function filters_LoadCache () { + global $data_dir, $SpamFilters_DNScache; + + if (file_exists($data_dir . "/dnscache")) { + $SpamFilters_DNScache = array(); + if ($fp = fopen ($data_dir . "/dnscache", "r")) { + flock($fp,LOCK_SH); + while ($data=fgetcsv($fp,1024)) { + if ($data[2] > time()) { + $SpamFilters_DNScache[$data[0]]['L'] = $data[1]; + $SpamFilters_DNScache[$data[0]]['T'] = $data[2]; + } + } + + flock($fp,LOCK_UN); + } + } +} + +function filters_bulkquery($filters_spam_scan, $filters, $read) { + global $SpamFilters_YourHop, $attachment_dir, $username, + $SpamFilters_DNScache, $SpamFilters_BulkQuery, + $SpamFilters_CacheTTL; + + $IPs = array(); + $i = 0; + while ($i < count($read)) { + // EIMS will give funky results + $Chunks = explode(' ', $read[$i]); + if ($Chunks[0] != '*') { + $i ++; + continue; + } + $MsgNum = $Chunks[1]; + + $i ++; + + // Look through all of the Received headers for IP addresses + // Stop when I get ")" on a line + // Stop if I get "*" on a line (don't advance) + // and above all, stop if $i is bigger than the total # of lines + while (($i < count($read)) && + ($read[$i][0] != ')' && $read[$i][0] != '*' && + $read[$i][0] != "\n")) { + // Check to see if this line is the right "Received from" line + // to check + if (is_int(strpos($read[$i], $SpamFilters_YourHop))) { + $read[$i] = ereg_replace('[^0-9\.]', ' ', $read[$i]); + $elements = explode(' ', $read[$i]); + foreach ($elements as $value) { + if ($value != '' && + ereg('[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}', + $value, $regs)) { + $Chunks = explode('.', $value); + $IP = $Chunks[3] . '.' . $Chunks[2] . '.' . + $Chunks[1] . '.' . $Chunks[0]; + foreach ($filters as $key => $value) { + if ($filters[$key]['enabled'] && + $filters[$key]['dns']) { + if (strlen($SpamFilters_DNScache[$IP.'.'.$filters[$key]['dns']]) == 0) { + $IPs[$IP] = true; + break; + } + } + } + // If we've checked one IP and YourHop is + // just a space + if ($SpamFilters_YourHop == ' ') { + break; // don't check any more + } + } + } + } + $i ++; + } + } + + if (count($IPs) > 0) { + $rbls = array(); + foreach ($filters as $key => $value) { + if ($filters[$key]['enabled']) { + if ($filters[$key]['dns']) { + $rbls[$filters[$key]['dns']] = true; + } + } + } + + $bqfil = $attachment_dir . $username . "-bq.in"; + $fp = fopen($bqfil, "w"); + fputs ($fp, $SpamFilters_CacheTTL . "\n"); + foreach ($rbls as $key => $value) { + fputs ($fp, "." . $key . "\n"); + } + fputs ($fp, "----------\n"); + foreach ($IPs as $key => $value) { + fputs ($fp, $key . "\n"); + } + fclose ($fp); + $bqout = array(); + exec ($SpamFilters_BulkQuery . " < " . $bqfil, $bqout); + foreach ($bqout as $value) { + $Chunks = explode(',', $value); + $SpamFilters_DNScache[$Chunks[0]]['L'] = $Chunks[1]; + $SpamFilters_DNScache[$Chunks[0]]['T'] = $Chunks[2] + time(); + } + unlink($bqfil); + } +} + + function filters_sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message) { global $color, $squirrelmail_language, $imap_general_debug; @@ -145,8 +283,10 @@ function start_filters() { function user_filters($imap_stream) { + global $data_dir, $username; $filters = load_filters(); if (! $filters) return; + $filters_user_scan = getPref($data_dir, $username, 'filters_user_scan'); sqimap_mailbox_select($imap_stream, 'INBOX'); @@ -159,16 +299,16 @@ function user_filters($imap_stream) { * and the other for CC. */ filter_search_and_delete($imap_stream, 'TO', - $filters[$i]['what'], $filters[$i]['folder']); + $filters[$i]['what'], $filters[$i]['folder'], $filters_user_scan); filter_search_and_delete($imap_stream, 'CC', - $filters[$i]['what'], $filters[$i]['folder']); + $filters[$i]['what'], $filters[$i]['folder'], $filters_user_scan); } else { /* * If it's a normal TO, CC, SUBJECT, or FROM, then handle it * normally. */ filter_search_and_delete($imap_stream, $filters[$i]['where'], - $filters[$i]['what'], $filters[$i]['folder']); + $filters[$i]['what'], $filters[$i]['folder'], $filters_user_scan); } } // Clean out the mailbox whether or not auto_expunge is on @@ -176,10 +316,30 @@ function user_filters($imap_stream) { sqimap_mailbox_expunge($imap_stream, 'INBOX'); } -function filter_search_and_delete($imap, $where, $what, $where_to) { - // Don't mess with the " characters on this fputs line! - fputs ($imap, 'a001 SEARCH ALL ' . $where . ' "' . addslashes($what) . - "\"\r\n"); +function filter_search_and_delete($imap, $where, $what, $where_to, $user_scan) { + global $languages, $squirrelmail_language, $allow_charset_search; + if ($user_scan == 'new') { + $category = 'UNSEEN'; + } else { + $category = 'ALL'; + } + + if ($allow_charset_search && isset($languages[$squirrelmail_language]['CHARSET']) && + $languages[$squirrelmail_language]['CHARSET']) { + $search_str = "SEARCH CHARSET " + . strtoupper($languages[$squirrelmail_language]['CHARSET']) + . ' ' . $category . ' '; + } else { + $search_str = 'SEARCH CHARSET US-ASCII ' . $category . ' '; + } + if ($where == "Header") { + $what = explode(':', $what); + $where = trim($where . ' ' . $what[0]); + $what = addslashes(trim($what[1])); + } + $search_str .= $where . ' {' . strlen($what) . "}\r\n" . $what . "\r\n"; + + fputs ($imap, "a001 $search_str"); $read = filters_sqimap_read_data ($imap, 'a001', true, $response, $message); // This may have problems with EIMS due to it being goofy @@ -203,11 +363,17 @@ function spam_filters($imap_stream) { global $data_dir, $username; global $SpamFilters_YourHop; global $SpamFilters_DNScache; + global $SpamFilters_SharedCache; + global $SpamFilters_BulkQuery; $filters_spam_scan = getPref($data_dir, $username, 'filters_spam_scan'); $filters_spam_folder = getPref($data_dir, $username, 'filters_spam_folder'); $filters = load_spam_filters(); + if ($SpamFilters_SharedCache) { + filters_LoadCache(); + } + $run = 0; foreach ($filters as $Key=> $Value) { @@ -225,16 +391,46 @@ function spam_filters($imap_stream) { // Ask for a big list of all "Received" headers in the inbox with // flags for each message. Kinda big. - fputs($imap_stream, 'A3999 FETCH 1:* (FLAGS BODY.PEEK[HEADER.FIELDS ' . - "(RECEIVED)])\r\n"); - - $read = filters_sqimap_read_data ($imap_stream, 'A3999', true, + //fputs($imap_stream, 'A3999 FETCH 1:* (FLAGS BODY.PEEK[HEADER.FIELDS ' . + // "(RECEIVED)])\r\n"); + $sid = sqimap_session_id(); + if ($filters_spam_scan != 'new') { + fputs($imap_stream, $sid.' FETCH 1:* (FLAGS BODY.PEEK[HEADER.FIELDS ' . + "(RECEIVED)])\r\n"); + } else { + fputs ($imap_stream, $sid.' SEARCH UNSEEN' . "\r\n"); + $read = filters_sqimap_read_data ($imap_stream, $sid, true, + $response, $message); + $sid = sqimap_session_id(); + if ($response != 'OK' || trim($read[0]) == '* SEARCH') { + fputs($imap_stream, + $sid.' FETCH 1:* (FLAGS BODY.PEEK[HEADER.FIELDS ' . + "(RECEIVED)])\r\n"); + } else { + $read[0] = trim($read[0]); + $i = 0; + $imap_query = $sid.' FETCH '; + $Chunks = explode(' ', $read[0]); + for ($i=2; $i < (count($Chunks)-1) ; $i++) { + $imap_query .= $Chunks[$i].','; + } + $imap_query .= $Chunks[count($Chunks)-1]; + $imap_query .= ' (FLAGS BODY.PEEK[HEADER.FIELDS '; + $imap_query .= "(RECEIVED)])\r\n"; + fputs($imap_stream, $imap_query); + } + } + + $read = filters_sqimap_read_data ($imap_stream, $sid, true, $response, $message); - if ($response != 'OK') { return; } + if (strlen($SpamFilters_BulkQuery) > 0) { + filters_bulkquery($filters_spam_scan, $filters, $read); + } + $i = 0; while ($i < count($read)) { // EIMS will give funky results @@ -248,14 +444,6 @@ function spam_filters($imap_stream) { $IPs = array(); $i ++; $IsSpam = 0; - $Scan = 1; - - // Check for normal IMAP servers - if ($filters_spam_scan == 'new') { - if (is_int(strpos($Chunks[4], '\Seen'))) { - $Scan = 0; - } - } // Look through all of the Received headers for IP addresses // Stop when I get ")" on a line @@ -269,25 +457,23 @@ function spam_filters($imap_stream) { if (is_int(strpos($read[$i], $SpamFilters_YourHop))) { // short-circuit and skip work if we don't scan this one - if ($Scan) { - $read[$i] = ereg_replace('[^0-9\.]', ' ', $read[$i]); - $elements = explode(' ', $read[$i]); - foreach ($elements as $value) { - if ($value != '' && - ereg('[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}', - $value, $regs)) { - $Chunks = explode('.', $value); - if (filters_spam_check_site($Chunks[0], - $Chunks[1], $Chunks[2], $Chunks[3], - $filters)) { - $IsSpam ++; - break; // no sense in checking more IPs - } - // If we've checked one IP and YourHop is - // just a space - if ($SpamFilters_YourHop == ' ') { - break; // don't check any more - } + $read[$i] = ereg_replace('[^0-9\.]', ' ', $read[$i]); + $elements = explode(' ', $read[$i]); + foreach ($elements as $value) { + if ($value != '' && + ereg('[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}', + $value, $regs)) { + $Chunks = explode('.', $value); + if (filters_spam_check_site($Chunks[0], + $Chunks[1], $Chunks[2], $Chunks[3], + $filters)) { + $IsSpam ++; + break; // no sense in checking more IPs + } + // If we've checked one IP and YourHop is + // just a space + if ($SpamFilters_YourHop == ' ') { + break; // don't check any more } } } @@ -309,24 +495,30 @@ function spam_filters($imap_stream) { sqimap_mailbox_expunge($imap_stream, 'INBOX'); - session_register('SpamFilters_DNScache'); + if ($SpamFilters_SharedCache) { + filters_SaveCache(); + } else { + session_register('SpamFilters_DNScache'); + } } // Does the loop through each enabled filter for the specified IP address. // IP format: $a.$b.$c.$d function filters_spam_check_site($a, $b, $c, $d, &$filters) { - global $SpamFilters_DNScache; + global $SpamFilters_DNScache, $SpamFilters_CacheTTL; foreach ($filters as $key => $value) { if ($filters[$key]['enabled']) { if ($filters[$key]['dns']) { $filter_revip = $d . '.' . $c . '.' . $b . '.' . $a . '.' . $filters[$key]['dns']; - if (strlen($SpamFilters_DNScache[$filter_revip]) == 0) { - $SpamFilters_DNScache[$filter_revip] = - gethostbyname($filter_revip); + if (strlen($SpamFilters_DNScache[$filter_revip]['L']) == 0) { + $SpamFilters_DNScache[$filter_revip]['L'] = + gethostbyname($filter_revip); + $SpamFilters_DNScache[$filter_revip]['T'] = + time() + $SpamFilters_CacheTTL; } - if ($SpamFilters_DNScache[$filter_revip] == + if ($SpamFilters_DNScache[$filter_revip]['L'] == $filters[$key]['result']) { return 1; } @@ -338,6 +530,7 @@ function filters_spam_check_site($a, $b, $c, $d, &$filters) { function load_filters() { global $data_dir, $username; + $filters = array(); for ($i=0; $fltr = getPref($data_dir, $username, 'filter' . $i); $i++) { $ary = explode(',', $fltr); @@ -473,22 +666,6 @@ function load_spam_filters() { $filters['ORDB']['comment'] = _("FREE - ORDB was born when ORBS went off the air. It seems to have fewer false positives than ORBS did though."); - $filters['ORBZ Inputs']['prefname'] = 'filters_spam_orbz'; - $filters['ORBZ Inputs']['name'] = 'ORBZ Inputs List'; - $filters['ORBZ Inputs']['link'] = 'http://www.orbz.org/'; - $filters['ORBZ Inputs']['dns'] = 'inputs.orbz.org'; - $filters['ORBZ Inputs']['result'] = '127.0.0.2'; - $filters['ORBZ Inputs']['comment'] = - _("FREE - Another ORBS replacement (just the INPUTS database used here)."); - - $filters['ORBZ Outputs']['prefname'] = 'filters_spam_orbz_outputs'; - $filters['ORBZ Outputs']['name'] = 'ORBZ Outputs List'; - $filters['ORBZ Outputs']['link'] = 'http://www.orbz.org/'; - $filters['ORBZ Outputs']['dns'] = 'outputs.orbz.org'; - $filters['ORBZ Outputs']['result'] = '127.0.0.2'; - $filters['ORBZ Outputs']['comment'] = - _("FREE - Another ORBS replacement (just the OUTPUTS database used here)."); - $filters['FiveTen Direct']['prefname'] = 'filters_spam_fiveten_src'; $filters['FiveTen Direct']['name'] = 'Five-Ten-sg.com Direct SPAM Sources'; $filters['FiveTen Direct']['link'] = 'http://www.five-ten-sg.com/blackhole.php'; @@ -673,6 +850,30 @@ function load_spam_filters() { $filters['NJABL DUL']['comment'] = _("FREE, for now - Not Just Another Blacklist - Dial-up IPs."); + $filters['Conf DSBL.ORG Relay']['prefname'] = 'filters_spam_dsbl_conf_ss'; + $filters['Conf DSBL.ORG Relay']['name'] = 'DSBL.org Confirmed Relay List'; + $filters['Conf DSBL.ORG Relay']['link'] = 'http://www.dsbl.org/'; + $filters['Conf DSBL.ORG Relay']['dns'] = 'list.dsbl.org'; + $filters['Conf DSBL.ORG Relay']['result'] = '127.0.0.2'; + $filters['Conf DSBL.ORG Relay']['comment'] = + _("FREE - Distributed Sender Boycott List - Confirmed Relays"); + + $filters['Conf DSBL.ORG Multi-Stage']['prefname'] = 'filters_spam_dsbl_conf_ms'; + $filters['Conf DSBL.ORG Multi-Stage']['name'] = 'DSBL.org Confirmed Multi-Stage Relay List'; + $filters['Conf DSBL.ORG Multi-Stage']['link'] = 'http://www.dsbl.org/'; + $filters['Conf DSBL.ORG Multi-Stage']['dns'] = 'multihop.dsbl.org'; + $filters['Conf DSBL.ORG Multi-Stage']['result'] = '127.0.0.2'; + $filters['Conf DSBL.ORG Multi-Stage']['comment'] = + _("FREE - Distributed Sender Boycott List - Confirmed Multi-stage Relays"); + + $filters['UN-Conf DSBL.ORG']['prefname'] = 'filters_spam_dsbl_unc'; + $filters['UN-Conf DSBL.ORG']['name'] = 'DSBL.org UN-Confirmed Relay List'; + $filters['UN-Conf DSBL.ORG']['link'] = 'http://www.dsbl.org/'; + $filters['UN-Conf DSBL.ORG']['dns'] = 'unconfirmed.dsbl.org'; + $filters['UN-Conf DSBL.ORG']['result'] = '127.0.0.2'; + $filters['UN-Conf DSBL.ORG']['comment'] = + _("FREE - Distributed Sender Boycott List - UN-Confirmed Relays"); + foreach ($filters as $Key => $Value) { $filters[$Key]['enabled'] = getPref($data_dir, $username, $filters[$Key]['prefname']); @@ -707,7 +908,10 @@ function filter_swap($id1, $id2) { /* This update the filter rules when renaming or deleting folders */ -function update_for_folder ($old_folder, $action, $new_folder = 'INBOX') { +function update_for_folder ($args) { + $old_folder = $args[0]; + $new_folder = $args[2]; + $action = $args[1]; global $plugins, $data_dir, $username; $filters = array(); $filters = load_filters();