@@ -25,17 +25,17 @@ discard block |
||
| 25 | 25 | |
| 26 | 26 | /* Conf */ |
| 27 | 27 | if (!isset($_GET['conf'])) { |
| 28 | - syslog(LOG_ALERT, |
|
| 29 | - sprintf('%s: you must insert the config file name as a GET parameter, such as %s?conf=listEmail.conf', |
|
| 30 | - $user, $_SERVER['SCRIPT_NAME']) ); |
|
| 31 | - exit(254); |
|
| 28 | + syslog(LOG_ALERT, |
|
| 29 | + sprintf('%s: you must insert the config file name as a GET parameter, such as %s?conf=listEmail.conf', |
|
| 30 | + $user, $_SERVER['SCRIPT_NAME']) ); |
|
| 31 | + exit(254); |
|
| 32 | 32 | } |
| 33 | 33 | $fileconf = $_GET['conf']; |
| 34 | 34 | if ( !file_exists(dirname(__FILE__) . '/../' . $fileconf) ) { |
| 35 | - syslog(LOG_ALERT, |
|
| 36 | - sprintf('%s: the configuration file <%s> doesn\'t exist.', |
|
| 37 | - $user, $fileconf )); |
|
| 38 | - exit(254); |
|
| 35 | + syslog(LOG_ALERT, |
|
| 36 | + sprintf('%s: the configuration file <%s> doesn\'t exist.', |
|
| 37 | + $user, $fileconf )); |
|
| 38 | + exit(254); |
|
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | closelog(); |
@@ -63,8 +63,8 @@ discard block |
||
| 63 | 63 | |
| 64 | 64 | /* check you select a blocklist */ |
| 65 | 65 | if ( !$tables["$typedesc"]['bl'] ) { |
| 66 | - syslog(LOG_EMERG,"$user: <$typedesc> is not a blocklist. Are you stupid? Do you want to whitelist a spammer? I refuse to continue."); |
|
| 67 | - exit (254); |
|
| 66 | + syslog(LOG_EMERG,"$user: <$typedesc> is not a blocklist. Are you stupid? Do you want to whitelist a spammer? I refuse to continue."); |
|
| 67 | + exit (254); |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | |
@@ -98,15 +98,15 @@ discard block |
||
| 98 | 98 | if (preg_match_all('/^https?\:\/\/(?<splunkhost>[\w\.\-]+)\:8000\/app\/(?<splunkapp>[\w\.\-]+)\/\@go\?sid=(?<job>[\w\.\-\d]+)$/', |
| 99 | 99 | $webhook['results_link'], $out, PREG_PATTERN_ORDER) === FALSE) { |
| 100 | 100 | syslog(LOG_ALERT, |
| 101 | - sprintf('%s: unexpected error: can\'t parse the results link returned by webhook (<%s>).', |
|
| 101 | + sprintf('%s: unexpected error: can\'t parse the results link returned by webhook (<%s>).', |
|
| 102 | 102 | $user, $webhook['results_link']) ); |
| 103 | 103 | return 255; |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | if ( $webhook['app'] != $out['splunkapp'][0] ) { |
| 107 | 107 | syslog(LOG_ALERT, |
| 108 | - sprintf('%s: unexpected error: the APP returned by webhook (<%s>) doesn\'t match the app (<%s>) in result link.', |
|
| 109 | - $user, $webhook['app'], $out['splunkapp'][0] ) ); |
|
| 108 | + sprintf('%s: unexpected error: the APP returned by webhook (<%s>) doesn\'t match the app (<%s>) in result link.', |
|
| 109 | + $user, $webhook['app'], $out['splunkapp'][0] ) ); |
|
| 110 | 110 | return 255; |
| 111 | 111 | } |
| 112 | 112 | |
@@ -140,57 +140,57 @@ discard block |
||
| 140 | 140 | |
| 141 | 141 | $nr = count ($results); |
| 142 | 142 | for ($i=1; $i<$nr; $i++) { /* We skip first header line (i=0) */ |
| 143 | - $data = str_getcsv($results[$i], ','); |
|
| 144 | - $thisVal = $data[1]; |
|
| 145 | - unset($data[1]); |
|
| 146 | - $data = array_values($data); |
|
| 147 | - if ( !in_array($thisVal,array_keys($tolist)) ) |
|
| 148 | - $tolist["$thisVal"] = $data; |
|
| 149 | - else if ($data[3]>$tolist[$thisVal][3]) |
|
| 150 | - $tolist["$thisVal"] = $data; |
|
| 143 | + $data = str_getcsv($results[$i], ','); |
|
| 144 | + $thisVal = $data[1]; |
|
| 145 | + unset($data[1]); |
|
| 146 | + $data = array_values($data); |
|
| 147 | + if ( !in_array($thisVal,array_keys($tolist)) ) |
|
| 148 | + $tolist["$thisVal"] = $data; |
|
| 149 | + else if ($data[3]>$tolist[$thisVal][3]) |
|
| 150 | + $tolist["$thisVal"] = $data; |
|
| 151 | 151 | } |
| 152 | 152 | |
| 153 | 153 | /* Make MYSQL connection */ |
| 154 | 154 | |
| 155 | 155 | $mysqli = new mysqli($dbhost, $userdb, $pwd, $db, $dbport); |
| 156 | 156 | if ($mysqli->connect_error) { |
| 157 | - syslog (LOG_EMERG, $user.': Connect Error (' . $mysqli->connect_errno . ') ' |
|
| 158 | - . $mysqli->connect_error); |
|
| 159 | - exit (254); |
|
| 157 | + syslog (LOG_EMERG, $user.': Connect Error (' . $mysqli->connect_errno . ') ' |
|
| 158 | + . $mysqli->connect_error); |
|
| 159 | + exit (254); |
|
| 160 | 160 | |
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | syslog(LOG_INFO, $user.': Successfully mysql connected to ' . $mysqli->host_info) ; |
| 164 | 164 | |
| 165 | 165 | foreach ( array_keys($tolist) as $value) { |
| 166 | - $quantity = $conf['quantity']; |
|
| 167 | - $reason = 'On ['.$tolist["$value"][0]."] <$value> sent ".$tolist["$value"][1].' messages to '.$tolist["$value"][2].' recipients.'; |
|
| 168 | - if ( $tolist["$value"][3] >= $threshold ) { |
|
| 169 | - if ( searchAndList ($mysqli,$user,$tables,$typedesc,$value,$unit,$quantity,$reason) ) { |
|
| 170 | - syslog (LOG_INFO, "$user: ".'Listing reason: '.$reason); |
|
| 171 | - /* Send a email to domain admin if you list an email */ |
|
| 172 | - if ( ( $tables["$typedesc"]['field'] == 'email' ) OR ( $tables["$typedesc"]['field'] == 'username' ) ) { |
|
| 173 | - /* Sometime uid are in the form of <user>@<domain> ... */ |
|
| 174 | - if ( strpos($value, '@') !== FALSE ) { |
|
| 175 | - $domain = substr(strrchr($value, '@'), 1); |
|
| 176 | - if ( strpos($domain, '@') === FALSE ) { |
|
| 177 | - $recip = emailToNotify($domainNotify_file,$domain); |
|
| 178 | - $subject = sprintf('%s <%s> is now blocked because exceedes limits on outgoing emails', |
|
| 179 | - $tables["$typedesc"]['field'], $value); |
|
| 180 | - if (!empty($recip)) |
|
| 181 | - if ( sendEmailWarn($tplfile,'[email protected]',$recip, |
|
| 182 | - $subject,$value,"$quantity $unit",$reason) ) |
|
| 183 | - syslog(LOG_INFO, "$user: \"$recip\" was notified about the \"$value\" abuse."); |
|
| 184 | - } |
|
| 185 | - else syslog(LOG_ERR,"$user: <$domain> contains the '@' char. Notification cannot be sent."); |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - } |
|
| 189 | - } |
|
| 190 | - else { |
|
| 191 | - $reason .= " But it has NOT been listed because it doesn't apply to the trigger condition."; |
|
| 192 | - syslog (LOG_INFO, "$user: ".$reason); |
|
| 193 | - } |
|
| 166 | + $quantity = $conf['quantity']; |
|
| 167 | + $reason = 'On ['.$tolist["$value"][0]."] <$value> sent ".$tolist["$value"][1].' messages to '.$tolist["$value"][2].' recipients.'; |
|
| 168 | + if ( $tolist["$value"][3] >= $threshold ) { |
|
| 169 | + if ( searchAndList ($mysqli,$user,$tables,$typedesc,$value,$unit,$quantity,$reason) ) { |
|
| 170 | + syslog (LOG_INFO, "$user: ".'Listing reason: '.$reason); |
|
| 171 | + /* Send a email to domain admin if you list an email */ |
|
| 172 | + if ( ( $tables["$typedesc"]['field'] == 'email' ) OR ( $tables["$typedesc"]['field'] == 'username' ) ) { |
|
| 173 | + /* Sometime uid are in the form of <user>@<domain> ... */ |
|
| 174 | + if ( strpos($value, '@') !== FALSE ) { |
|
| 175 | + $domain = substr(strrchr($value, '@'), 1); |
|
| 176 | + if ( strpos($domain, '@') === FALSE ) { |
|
| 177 | + $recip = emailToNotify($domainNotify_file,$domain); |
|
| 178 | + $subject = sprintf('%s <%s> is now blocked because exceedes limits on outgoing emails', |
|
| 179 | + $tables["$typedesc"]['field'], $value); |
|
| 180 | + if (!empty($recip)) |
|
| 181 | + if ( sendEmailWarn($tplfile,'[email protected]',$recip, |
|
| 182 | + $subject,$value,"$quantity $unit",$reason) ) |
|
| 183 | + syslog(LOG_INFO, "$user: \"$recip\" was notified about the \"$value\" abuse."); |
|
| 184 | + } |
|
| 185 | + else syslog(LOG_ERR,"$user: <$domain> contains the '@' char. Notification cannot be sent."); |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + } |
|
| 189 | + } |
|
| 190 | + else { |
|
| 191 | + $reason .= " But it has NOT been listed because it doesn't apply to the trigger condition."; |
|
| 192 | + syslog (LOG_INFO, "$user: ".$reason); |
|
| 193 | + } |
|
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | /* Close connection */ |
@@ -4,18 +4,18 @@ discard block |
||
| 4 | 4 | |
| 5 | 5 | function username() { |
| 6 | 6 | if (isset ($_SERVER['REMOTE_USER'])) $user = $_SERVER['REMOTE_USER']; |
| 7 | - else if (isset ($_SERVER['USER'])) $user = $_SERVER['USER']; |
|
| 8 | - else $user='unknown'; |
|
| 7 | + else if (isset ($_SERVER['USER'])) $user = $_SERVER['USER']; |
|
| 8 | + else $user='unknown'; |
|
| 9 | 9 | return $user; |
| 10 | 10 | } |
| 11 | 11 | |
| 12 | 12 | |
| 13 | 13 | function myConnect($host, $user, $pass, $db, $port, $tablelist, $typedesc, $loguser) { |
| 14 | - $db = ( $tablelist["$typedesc"]['milter'] ) ? $tablelist["$typedesc"]['name'] : $db; |
|
| 14 | + $db = ( $tablelist["$typedesc"]['milter'] ) ? $tablelist["$typedesc"]['name'] : $db; |
|
| 15 | 15 | $mysqli = new mysqli($host, $user, $pass, $db, $port); |
| 16 | - if ($mysqli->connect_error) { |
|
| 17 | - syslog (LOG_EMERG, $loguser.': Connect Error to DB <'.$db.'> (' . $mysqli->connect_errno . ') ' |
|
| 18 | - . $mysqli->connect_error); |
|
| 16 | + if ($mysqli->connect_error) { |
|
| 17 | + syslog (LOG_EMERG, $loguser.': Connect Error to DB <'.$db.'> (' . $mysqli->connect_errno . ') ' |
|
| 18 | + . $mysqli->connect_error); |
|
| 19 | 19 | return FALSE; |
| 20 | 20 | } |
| 21 | 21 | syslog(LOG_INFO, $loguser.': Successfully MySQL connected at DB <'.$db.'> to ' . $mysqli->host_info) ; |
@@ -56,7 +56,7 @@ discard block |
||
| 56 | 56 | } |
| 57 | 57 | } |
| 58 | 58 | list($sub['net'],$sub['mask'])=explode('/',$value); |
| 59 | - $query= sprintf("INSERT INTO `$table` ( |
|
| 59 | + $query= sprintf("INSERT INTO `$table` ( |
|
| 60 | 60 | `$type` , |
| 61 | 61 | `netmask`, |
| 62 | 62 | `date` , |
@@ -69,10 +69,10 @@ discard block |
||
| 69 | 69 | INET_ATON( '%s' ) , INET_ATON( '%s' ) , |
| 70 | 70 | CURRENT_TIMESTAMP , TIMESTAMPADD(%s,%d,CURRENT_TIMESTAMP), '1', '%s', '%s' |
| 71 | 71 | )" ,$sub['net'],$sub['mask'],$expUnit,$expQ,$user,$myreason); |
| 72 | - break; |
|
| 72 | + break; |
|
| 73 | 73 | |
| 74 | 74 | default: |
| 75 | - $query= sprintf("INSERT INTO `$table` ( |
|
| 75 | + $query= sprintf("INSERT INTO `$table` ( |
|
| 76 | 76 | `$type` , |
| 77 | 77 | `date` , |
| 78 | 78 | `exp` , |
@@ -87,8 +87,8 @@ discard block |
||
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | if ($myconn->query($query) === TRUE) { |
| 90 | - syslog(LOG_INFO, "$user: $type <$value> successfully listed on <$table> for $expQ $expUnit."); |
|
| 91 | - $result=TRUE; |
|
| 90 | + syslog(LOG_INFO, "$user: $type <$value> successfully listed on <$table> for $expQ $expUnit."); |
|
| 91 | + $result=TRUE; |
|
| 92 | 92 | } |
| 93 | 93 | else syslog(LOG_ERR, "$user: Error: ".$myconn->error); |
| 94 | 94 | return $result; |
@@ -106,9 +106,9 @@ discard block |
||
| 106 | 106 | $nlist = '`nlist` + 1'; |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | - switch ($type) { |
|
| 109 | + switch ($type) { |
|
| 110 | 110 | case 'ip': |
| 111 | - $query= sprintf("UPDATE `$table` SET |
|
| 111 | + $query= sprintf("UPDATE `$table` SET |
|
| 112 | 112 | `active` = '1', |
| 113 | 113 | `user` = '%s', |
| 114 | 114 | `exp` = TIMESTAMPADD(%s,%d,%s), |
@@ -116,9 +116,9 @@ discard block |
||
| 116 | 116 | `reason` = '%s' |
| 117 | 117 | WHERE `$table`.`$type` = INET_ATON('%s') LIMIT 1" ,$user,$expUnit,$expQ,$exptime,$nlist,$myreason,$value); |
| 118 | 118 | break; |
| 119 | - case 'network': |
|
| 119 | + case 'network': |
|
| 120 | 120 | list($sub['net'],$sub['mask'])=explode('/',$value); |
| 121 | - $query= sprintf("UPDATE `$table` SET |
|
| 121 | + $query= sprintf("UPDATE `$table` SET |
|
| 122 | 122 | `active` = '1', |
| 123 | 123 | `user` = '%s', |
| 124 | 124 | `exp` = TIMESTAMPADD(%s,%d,%s), |
@@ -127,7 +127,7 @@ discard block |
||
| 127 | 127 | WHERE (`$table`.`$type` = INET_ATON('%s') AND `$table`.`netmask` = INET_ATON('%s')) LIMIT 1" ,$user,$expUnit,$expQ,$exptime,$nlist,$myreason,$sub['net'],$sub['mask']); |
| 128 | 128 | break; |
| 129 | 129 | default: |
| 130 | - $query= sprintf("UPDATE `$table` SET |
|
| 130 | + $query= sprintf("UPDATE `$table` SET |
|
| 131 | 131 | `active` = '1', |
| 132 | 132 | `user` = '%s', |
| 133 | 133 | `exp` = TIMESTAMPADD(%s,%d,%s), |
@@ -136,18 +136,18 @@ discard block |
||
| 136 | 136 | WHERE `$table`.`$type` = '%s' LIMIT 1" ,$user,$expUnit,$expQ,$exptime,$nlist,$myreason,$value); |
| 137 | 137 | } |
| 138 | 138 | |
| 139 | - if ($myconn->query($query) === TRUE) { |
|
| 140 | - syslog(LOG_INFO, "$user: relist $type <$value> on <$table> for $expQ $expUnit from $exptime."); |
|
| 139 | + if ($myconn->query($query) === TRUE) { |
|
| 140 | + syslog(LOG_INFO, "$user: relist $type <$value> on <$table> for $expQ $expUnit from $exptime."); |
|
| 141 | 141 | $result=TRUE; |
| 142 | - } |
|
| 143 | - else syslog (LOG_ERR, "$user: Error: ". $myconn->error); |
|
| 142 | + } |
|
| 143 | + else syslog (LOG_ERR, "$user: Error: ". $myconn->error); |
|
| 144 | 144 | return $result; |
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | function remove ($myconn,$user,$value,$type,$table) { |
| 148 | 148 | |
| 149 | - switch ($type) { |
|
| 150 | - case 'ip': |
|
| 149 | + switch ($type) { |
|
| 150 | + case 'ip': |
|
| 151 | 151 | $query = sprintf("DELETE FROM `$table` WHERE |
| 152 | 152 | `$table`.`$type` = INET_ATON('%s') LIMIT 1", $value); |
| 153 | 153 | break; |
@@ -163,18 +163,18 @@ discard block |
||
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | |
| 166 | - if ($return=$myconn->query($query) === TRUE) |
|
| 167 | - syslog(LOG_INFO, "$user: permanently DELETED $type <$value> from <$table>."); |
|
| 168 | - else syslog(LOG_ERR, "$user: Error: ". $myconn->error); |
|
| 166 | + if ($return=$myconn->query($query) === TRUE) |
|
| 167 | + syslog(LOG_INFO, "$user: permanently DELETED $type <$value> from <$table>."); |
|
| 168 | + else syslog(LOG_ERR, "$user: Error: ". $myconn->error); |
|
| 169 | 169 | |
| 170 | - return $return; |
|
| 170 | + return $return; |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | |
| 174 | 174 | function changestatus ($myconn,$user,$value,$status,$type,$table) { |
| 175 | 175 | |
| 176 | 176 | switch ($type) { |
| 177 | - case 'ip': |
|
| 177 | + case 'ip': |
|
| 178 | 178 | $query= sprintf("UPDATE `$table` SET `active` = '$status', `user` = '%s' WHERE `$table`.`$type` = INET_ATON('%s') LIMIT 1" ,$user, $value); |
| 179 | 179 | break; |
| 180 | 180 | case 'network': |
@@ -185,16 +185,16 @@ discard block |
||
| 185 | 185 | $query= sprintf("UPDATE `$table` SET `active` = '$status', `user` = '%s' WHERE `$table`.`$type` = '%s' LIMIT 1" ,$user, $value); |
| 186 | 186 | } |
| 187 | 187 | |
| 188 | - if ($return=$myconn->query($query) === TRUE) { |
|
| 189 | - syslog(LOG_INFO, "$user: change status of $type <$value>. The status is now <$status>"); |
|
| 190 | - } |
|
| 191 | - else syslog(LOG_ERR, "$user: Error: ". $myconn->error); |
|
| 188 | + if ($return=$myconn->query($query) === TRUE) { |
|
| 189 | + syslog(LOG_INFO, "$user: change status of $type <$value>. The status is now <$status>"); |
|
| 190 | + } |
|
| 191 | + else syslog(LOG_ERR, "$user: Error: ". $myconn->error); |
|
| 192 | 192 | return $return; |
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | |
| 196 | 196 | function expire ($myconn,$user,$tables,$expireTime) { |
| 197 | - $return=TRUE; |
|
| 197 | + $return=TRUE; |
|
| 198 | 198 | $log=array(); |
| 199 | 199 | $desc = array_keys($tables); |
| 200 | 200 | foreach ($desc as $tdesc) { |
@@ -204,11 +204,11 @@ discard block |
||
| 204 | 204 | /* END OF QUERY */ |
| 205 | 205 | $log[0] = 'expired for'; |
| 206 | 206 | $log[1] = 'disabled for'; |
| 207 | - if ($myconn->multi_query($query)) { |
|
| 207 | + if ($myconn->multi_query($query)) { |
|
| 208 | 208 | $j = 0; |
| 209 | 209 | do { |
| 210 | - $numdel = $myconn->affected_rows; |
|
| 211 | - syslog(LOG_INFO, "Expire job - <$user> Permanently DELETED $numdel records ".$log[$j]." $expireTime YEARS from <".$tables["$tdesc"]['name'].'>.'); |
|
| 210 | + $numdel = $myconn->affected_rows; |
|
| 211 | + syslog(LOG_INFO, "Expire job - <$user> Permanently DELETED $numdel records ".$log[$j]." $expireTime YEARS from <".$tables["$tdesc"]['name'].'>.'); |
|
| 212 | 212 | $j++; |
| 213 | 213 | |
| 214 | 214 | } while ($myconn->next_result()); |
@@ -220,7 +220,7 @@ discard block |
||
| 220 | 220 | } |
| 221 | 221 | if ( !($return) ) syslog(LOG_EMERG, 'End of Expire job with error. See above logs. SQL Connection terminated'); |
| 222 | 222 | else syslog(LOG_INFO, 'Successfully End of Expire job. SQL Connection successfully terminated.'); |
| 223 | - return $return; |
|
| 223 | + return $return; |
|
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | |
@@ -293,7 +293,7 @@ discard block |
||
| 293 | 293 | if ( $entry->num_rows ) { |
| 294 | 294 | if ( $entry->num_rows == 1 ) { |
| 295 | 295 | $riga = $entry->fetch_array(MYSQLI_ASSOC); |
| 296 | - if (isListed($riga)) { |
|
| 296 | + if (isListed($riga)) { |
|
| 297 | 297 | $warn = "<$value> is already present in <$listdep> list!"; |
| 298 | 298 | $entry->free(); |
| 299 | 299 | return FALSE; |
@@ -310,7 +310,7 @@ discard block |
||
| 310 | 310 | function searchentry ($myconn,$value,$tablelist) { |
| 311 | 311 | /* Make a MYSQL query and return result */ |
| 312 | 312 | |
| 313 | - $type = $tablelist['field']; |
|
| 313 | + $type = $tablelist['field']; |
|
| 314 | 314 | |
| 315 | 315 | if ( $tablelist['milter'] ) { |
| 316 | 316 | $table = milterTable($type); |
@@ -334,7 +334,7 @@ discard block |
||
| 334 | 334 | case 'ip': |
| 335 | 335 | $query = sprintf('SELECT * FROM ( |
| 336 | 336 | SELECT *, GROUP_CONCAT(milt.name) as miltnames FROM `%s` LEFT JOIN milt ON (%s.idmilt=milt.id)' . |
| 337 | - 'WHERE `ip` = INET_ATON(\'%s\') |
|
| 337 | + 'WHERE `ip` = INET_ATON(\'%s\') |
|
| 338 | 338 | ) AS val WHERE val.ip IS NOT null', $table, $table, $value); |
| 339 | 339 | break; |
| 340 | 340 | default: |
@@ -345,33 +345,33 @@ discard block |
||
| 345 | 345 | } |
| 346 | 346 | |
| 347 | 347 | else { |
| 348 | - $table = $tablelist['name']; |
|
| 349 | - if ($value == 'ALL') $query = 'select * from '.$table; |
|
| 350 | - else { |
|
| 351 | - switch ($type) { |
|
| 352 | - case 'ip': |
|
| 353 | - $query= "select * from $table where $type = INET_ATON('$value')"; |
|
| 354 | - break; |
|
| 355 | - case 'network': |
|
| 356 | - list($sub['net'],$sub['mask'])=explode('/',$value); |
|
| 357 | - $query= sprintf('select * from `%s` |
|
| 348 | + $table = $tablelist['name']; |
|
| 349 | + if ($value == 'ALL') $query = 'select * from '.$table; |
|
| 350 | + else { |
|
| 351 | + switch ($type) { |
|
| 352 | + case 'ip': |
|
| 353 | + $query= "select * from $table where $type = INET_ATON('$value')"; |
|
| 354 | + break; |
|
| 355 | + case 'network': |
|
| 356 | + list($sub['net'],$sub['mask'])=explode('/',$value); |
|
| 357 | + $query= sprintf('select * from `%s` |
|
| 358 | 358 | WHERE ( |
| 359 | 359 | inet_aton(\'%s\') >= network AND |
| 360 | 360 | ( inet_aton(\'%s\') | ( inet_aton(\'%s\') ^ (power(2,32)-1) ) ) |
| 361 | 361 | <= network | ( netmask ^ (power(2,32)-1) ) |
| 362 | 362 | )', $table, $sub['net'], $sub['net'], $sub['mask']); |
| 363 | 363 | ; |
| 364 | - break; |
|
| 365 | - default: |
|
| 366 | - $query= "select * from $table where $type = '$value'"; |
|
| 367 | - } |
|
| 368 | - } |
|
| 364 | + break; |
|
| 365 | + default: |
|
| 366 | + $query= "select * from $table where $type = '$value'"; |
|
| 367 | + } |
|
| 368 | + } |
|
| 369 | 369 | } |
| 370 | 370 | |
| 371 | 371 | $result = $myconn->query($query); |
| 372 | 372 | if($result === false) |
| 373 | 373 | syslog(LOG_EMERG, "ALERT: Query <$query> failed: ".$myconn->error); |
| 374 | - return $result; |
|
| 374 | + return $result; |
|
| 375 | 375 | } |
| 376 | 376 | |
| 377 | 377 | function countListed ($myconn,$table) { |
@@ -386,14 +386,14 @@ discard block |
||
| 386 | 386 | |
| 387 | 387 | |
| 388 | 388 | function isFull($myconn,$typedesc,$alltables) { |
| 389 | - if (isset($alltables["$typedesc"]['limit'])) { |
|
| 389 | + if (isset($alltables["$typedesc"]['limit'])) { |
|
| 390 | 390 | if ( $alltables["$typedesc"]['milter'] ) |
| 391 | 391 | $tab = 'net'; |
| 392 | 392 | else |
| 393 | 393 | $tab = $alltables["$typedesc"]['name']; |
| 394 | - if ( countListed($myconn,$tab) >= $alltables["$typedesc"]['limit'] ) |
|
| 395 | - return TRUE; |
|
| 396 | - } |
|
| 394 | + if ( countListed($myconn,$tab) >= $alltables["$typedesc"]['limit'] ) |
|
| 395 | + return TRUE; |
|
| 396 | + } |
|
| 397 | 397 | return FALSE; |
| 398 | 398 | } |
| 399 | 399 | |
@@ -413,14 +413,14 @@ discard block |
||
| 413 | 413 | if ($result) { |
| 414 | 414 | printf("<pre>Your request for $type <$value> returned %d items.\n</pre>", $result->num_rows); |
| 415 | 415 | |
| 416 | - /* Check for limit in number of listed items */ |
|
| 416 | + /* Check for limit in number of listed items */ |
|
| 417 | 417 | $full = isFull($myconn,$typedesc,$tables); |
| 418 | 418 | if ($full) print '<p>'.htmlspecialchars("$typedesc has reached maximum value of ".$tables["$typedesc"]['limit'].' listed items.').'</p>'; |
| 419 | 419 | |
| 420 | 420 | if ($result->num_rows) { |
| 421 | 421 | print $tabhtm; |
| 422 | 422 | $i=0; |
| 423 | - while ($riga = $result->fetch_array(MYSQLI_ASSOC)) { |
|
| 423 | + while ($riga = $result->fetch_array(MYSQLI_ASSOC)) { |
|
| 424 | 424 | if (isListed($riga)) { |
| 425 | 425 | if ($tables["$typedesc"]['bl']) $listed='Listed'; |
| 426 | 426 | else $listed='WhiteListed'; |
@@ -446,7 +446,7 @@ discard block |
||
| 446 | 446 | printf ("<tr id=id$i><td id='status$listed'>%s</td><td id='status$listed'>%s</td><td id='status$listed'>%s</td><td id='status$listed'>%s</td><td id='status$listed'>%s</td><td id='status$listed'>%s</td><td id='status$listed'>%s</td><td id='status$listed'>%s</td><td>%s</td></tr>\n", |
| 447 | 447 | $element, $riga['date'], $riga['datemod'], $riga['exp'], $riga['active'], $riga['nlist'], $riga['user'],htmlspecialchars($riga['reason']),ask($myconn,$i,$listed,$tables,$typedesc,$element,$full,$user,$adm)); |
| 448 | 448 | $i++; |
| 449 | - } |
|
| 449 | + } |
|
| 450 | 450 | print '</tbody></table>'; |
| 451 | 451 | } |
| 452 | 452 | else { |
@@ -466,15 +466,15 @@ discard block |
||
| 466 | 466 | |
| 467 | 467 | function sendEmailWarn($tplf,$from,$to,$sbj,$emailListed,$intervalToExpire,$detail) { |
| 468 | 468 | $now = time(); |
| 469 | - setlocale (LC_TIME, 'it_IT'); |
|
| 470 | - $date = date("r",$now); |
|
| 469 | + setlocale (LC_TIME, 'it_IT'); |
|
| 470 | + $date = date("r",$now); |
|
| 471 | 471 | $messageID = md5(uniqid($now,1)) . '@' . gethostname(); |
| 472 | 472 | $mua = 'PHP/' . phpversion(); |
| 473 | 473 | |
| 474 | 474 | /* Parsing headers */ |
| 475 | 475 | if (!file_exists($tplf['header'])) { |
| 476 | - syslog(LOG_ERR, 'Sending email... template file <'.$tplf['header'].'> not found!'); |
|
| 477 | - exit; |
|
| 476 | + syslog(LOG_ERR, 'Sending email... template file <'.$tplf['header'].'> not found!'); |
|
| 477 | + exit; |
|
| 478 | 478 | } |
| 479 | 479 | |
| 480 | 480 | $head_tmpl = file_get_contents($tplf['header']); |
@@ -483,35 +483,35 @@ discard block |
||
| 483 | 483 | $headers = str_replace($arr_tpl_vars, $arr_tpl_data, $head_tmpl); |
| 484 | 484 | $headers = preg_replace( '/\r|\n/', "\r\n", $headers ); |
| 485 | 485 | |
| 486 | - /* Parsing body */ |
|
| 486 | + /* Parsing body */ |
|
| 487 | 487 | |
| 488 | - if (!file_exists($tplf['body'])) { |
|
| 489 | - syslog(LOG_ERR, 'Sending email... template file <'.$tplf['body'].'> not found!'); |
|
| 490 | - exit; |
|
| 491 | - } |
|
| 488 | + if (!file_exists($tplf['body'])) { |
|
| 489 | + syslog(LOG_ERR, 'Sending email... template file <'.$tplf['body'].'> not found!'); |
|
| 490 | + exit; |
|
| 491 | + } |
|
| 492 | 492 | |
| 493 | - $body_tmpl = file_get_contents($tplf['body']); |
|
| 494 | - $arr_tpl_vars = array('{emailListed}','{expInterval}','{reason}'); |
|
| 495 | - $arr_tpl_data = array($emailListed,$intervalToExpire,$detail); |
|
| 496 | - $body = str_replace($arr_tpl_vars, $arr_tpl_data, $body_tmpl); |
|
| 497 | - $body = preg_replace( "/\r|\n/", "\r\n", $body ); |
|
| 493 | + $body_tmpl = file_get_contents($tplf['body']); |
|
| 494 | + $arr_tpl_vars = array('{emailListed}','{expInterval}','{reason}'); |
|
| 495 | + $arr_tpl_data = array($emailListed,$intervalToExpire,$detail); |
|
| 496 | + $body = str_replace($arr_tpl_vars, $arr_tpl_data, $body_tmpl); |
|
| 497 | + $body = preg_replace( "/\r|\n/", "\r\n", $body ); |
|
| 498 | 498 | $body = wordwrap ( $body, 75 , "\r\n" ); |
| 499 | 499 | |
| 500 | 500 | /* Send the mail! */ |
| 501 | - if ( strlen(ini_get("safe_mode"))< 1) { |
|
| 502 | - $old_mailfrom = ini_get("sendmail_from"); |
|
| 503 | - ini_set("sendmail_from", $from); |
|
| 504 | - $params = sprintf("-oi -f %s", '<>'); |
|
| 505 | - if (!(mail($to,$sbj, $body,$headers,$params))) $flag=FALSE; |
|
| 506 | - else $flag=TRUE; |
|
| 507 | - if (isset($old_mailfrom)) |
|
| 508 | - ini_set("sendmail_from", $old_mailfrom); |
|
| 509 | - } |
|
| 510 | - else { |
|
| 511 | - if (!(mail($to,$sbj, $body,$headers))) $flag=FALSE; |
|
| 512 | - else $flag=TRUE; |
|
| 513 | - } |
|
| 514 | - return $flag; |
|
| 501 | + if ( strlen(ini_get("safe_mode"))< 1) { |
|
| 502 | + $old_mailfrom = ini_get("sendmail_from"); |
|
| 503 | + ini_set("sendmail_from", $from); |
|
| 504 | + $params = sprintf("-oi -f %s", '<>'); |
|
| 505 | + if (!(mail($to,$sbj, $body,$headers,$params))) $flag=FALSE; |
|
| 506 | + else $flag=TRUE; |
|
| 507 | + if (isset($old_mailfrom)) |
|
| 508 | + ini_set("sendmail_from", $old_mailfrom); |
|
| 509 | + } |
|
| 510 | + else { |
|
| 511 | + if (!(mail($to,$sbj, $body,$headers))) $flag=FALSE; |
|
| 512 | + else $flag=TRUE; |
|
| 513 | + } |
|
| 514 | + return $flag; |
|
| 515 | 515 | } |
| 516 | 516 | |
| 517 | 517 | function emailToNotify($notify_file,$dom) { |
@@ -525,62 +525,62 @@ discard block |
||
| 525 | 525 | function searchAndList ($myconn,$loguser,$tables,$typedesc,$value,$unit,&$quantity,&$reason) { |
| 526 | 526 | |
| 527 | 527 | /* Search and list value */ |
| 528 | - $type = $tables["$typedesc"]['field']; |
|
| 529 | - $table = $tables["$typedesc"]['name']; |
|
| 530 | - $result = searchentry ($myconn,$value,$tables["$typedesc"]); |
|
| 531 | - |
|
| 532 | - /* Manage abnormal conditions */ |
|
| 533 | - /* Value already present in db more than once. This is absurd. Panic! */ |
|
| 534 | - if ($result->num_rows > 1) { |
|
| 535 | - syslog(LOG_EMERG,"$loguser: PANIC! Select for $type '$value' returned ". $result->num_rows ." items instead of one. Abnormal. Contact a sysadmin or a developer."); |
|
| 536 | - $result->free(); |
|
| 537 | - return FALSE; |
|
| 538 | - } |
|
| 528 | + $type = $tables["$typedesc"]['field']; |
|
| 529 | + $table = $tables["$typedesc"]['name']; |
|
| 530 | + $result = searchentry ($myconn,$value,$tables["$typedesc"]); |
|
| 531 | + |
|
| 532 | + /* Manage abnormal conditions */ |
|
| 533 | + /* Value already present in db more than once. This is absurd. Panic! */ |
|
| 534 | + if ($result->num_rows > 1) { |
|
| 535 | + syslog(LOG_EMERG,"$loguser: PANIC! Select for $type '$value' returned ". $result->num_rows ." items instead of one. Abnormal. Contact a sysadmin or a developer."); |
|
| 536 | + $result->free(); |
|
| 537 | + return FALSE; |
|
| 538 | + } |
|
| 539 | 539 | |
| 540 | - /* Value already present in db or not present: to list anyway */ |
|
| 541 | - if ($result->num_rows >= 0) { |
|
| 542 | - /* First, check for limit in number of listed items */ |
|
| 543 | - if (isFull($myconn,$typedesc,$tables)) { |
|
| 544 | - syslog(LOG_EMERG,"$loguser: $typedesc has reached maximum value of ".$tables["$typedesc"]['limit'].' listed items. Abnormal exit.'); |
|
| 545 | - $result->free(); |
|
| 546 | - return FALSE; |
|
| 547 | - } |
|
| 548 | - /* Second, check if the (re)list would be consistent now */ |
|
| 549 | - if (! consistentListing($myconn,$tables,$typedesc,$value,$whynot) ) { |
|
| 550 | - syslog(LOG_ERR, $loguser.': '.$whynot); |
|
| 551 | - $result->free(); |
|
| 552 | - return FALSE; |
|
| 553 | - } |
|
| 554 | - } |
|
| 555 | - /* End of abnormal conditions */ |
|
| 540 | + /* Value already present in db or not present: to list anyway */ |
|
| 541 | + if ($result->num_rows >= 0) { |
|
| 542 | + /* First, check for limit in number of listed items */ |
|
| 543 | + if (isFull($myconn,$typedesc,$tables)) { |
|
| 544 | + syslog(LOG_EMERG,"$loguser: $typedesc has reached maximum value of ".$tables["$typedesc"]['limit'].' listed items. Abnormal exit.'); |
|
| 545 | + $result->free(); |
|
| 546 | + return FALSE; |
|
| 547 | + } |
|
| 548 | + /* Second, check if the (re)list would be consistent now */ |
|
| 549 | + if (! consistentListing($myconn,$tables,$typedesc,$value,$whynot) ) { |
|
| 550 | + syslog(LOG_ERR, $loguser.': '.$whynot); |
|
| 551 | + $result->free(); |
|
| 552 | + return FALSE; |
|
| 553 | + } |
|
| 554 | + } |
|
| 555 | + /* End of abnormal conditions */ |
|
| 556 | 556 | |
| 557 | 557 | |
| 558 | - /* Finally, here I can list the value! */ |
|
| 558 | + /* Finally, here I can list the value! */ |
|
| 559 | 559 | $thisentry = $result->fetch_array(MYSQLI_ASSOC); |
| 560 | - switch ($result->num_rows) { |
|
| 561 | - /* Relist value if already present */ |
|
| 562 | - case 1: |
|
| 563 | - if ( isListed($thisentry) ) { |
|
| 560 | + switch ($result->num_rows) { |
|
| 561 | + /* Relist value if already present */ |
|
| 562 | + case 1: |
|
| 563 | + if ( isListed($thisentry) ) { |
|
| 564 | 564 | /* Entry already listed */ |
| 565 | 565 | $expdate = $thisentry['exp']; |
| 566 | 566 | $reason = sprintf('%s. Already listed. Adding 1 DAY to previous expire date.', |
| 567 | 567 | $reason); |
| 568 | 568 | $quantity = 1; |
| 569 | 569 | $unit = 'DAY'; |
| 570 | - } |
|
| 570 | + } |
|
| 571 | 571 | else { |
| 572 | - /* Entry delisted */ |
|
| 572 | + /* Entry delisted */ |
|
| 573 | 573 | $quantity *= $thisentry['nlist']; |
| 574 | 574 | $expdate = 0; /* This forces expiration from CURRENT_TIMESTAMP */ |
| 575 | 575 | } |
| 576 | 576 | $result->free(); |
| 577 | - return relist ($myconn,$loguser,$value,$type,$table,$unit,$quantity,$reason, $expdate); |
|
| 577 | + return relist ($myconn,$loguser,$value,$type,$table,$unit,$quantity,$reason, $expdate); |
|
| 578 | 578 | |
| 579 | - /* First time list value */ |
|
| 580 | - case 0: |
|
| 581 | - $result->free(); |
|
| 582 | - return addtolist ($myconn,$loguser,$value,$tables["$typedesc"],$unit,$quantity,$reason,$_); |
|
| 583 | - } |
|
| 579 | + /* First time list value */ |
|
| 580 | + case 0: |
|
| 581 | + $result->free(); |
|
| 582 | + return addtolist ($myconn,$loguser,$value,$tables["$typedesc"],$unit,$quantity,$reason,$_); |
|
| 583 | + } |
|
| 584 | 584 | } |
| 585 | 585 | |
| 586 | 586 | |
@@ -613,7 +613,7 @@ discard block |
||
| 613 | 613 | return FALSE; |
| 614 | 614 | } |
| 615 | 615 | $result = searchentry ($myconn,'ALL',$tabletype); |
| 616 | - if ($result->num_rows) { |
|
| 616 | + if ($result->num_rows) { |
|
| 617 | 617 | while ($row = $result->fetch_array(MYSQLI_ASSOC)) { |
| 618 | 618 | $thisNet = long2ip($row['network']).'/'.long2ip($row['netmask']); |
| 619 | 619 | if ( isIn($thisNet, $net) ) { |
@@ -633,7 +633,7 @@ discard block |
||
| 633 | 633 | /* For miltermap */ |
| 634 | 634 | function checkMilterConf($table) { |
| 635 | 635 | if (isset($table['milter'])) { |
| 636 | - if ($table['milter'] === TRUE) { |
|
| 636 | + if ($table['milter'] === TRUE) { |
|
| 637 | 637 | switch ( $table['field'] ) { |
| 638 | 638 | case 'network': |
| 639 | 639 | case 'ip': |
@@ -656,15 +656,15 @@ discard block |
||
| 656 | 656 | |
| 657 | 657 | function milterTable($t) { |
| 658 | 658 | /* Return the milter object table for type t or FALSE on error */ |
| 659 | - switch ($t) { |
|
| 660 | - case 'network': |
|
| 661 | - return 'net'; |
|
| 662 | - case 'ip': |
|
| 663 | - return 'ips'; |
|
| 664 | - default: |
|
| 659 | + switch ($t) { |
|
| 660 | + case 'network': |
|
| 661 | + return 'net'; |
|
| 662 | + case 'ip': |
|
| 663 | + return 'ips'; |
|
| 664 | + default: |
|
| 665 | 665 | syslog(LOG_EMERG, "ALERT: type <$t> not allowed in configuration. "); |
| 666 | - return FALSE; |
|
| 667 | - } |
|
| 666 | + return FALSE; |
|
| 667 | + } |
|
| 668 | 668 | } |
| 669 | 669 | |
| 670 | 670 | |
@@ -672,9 +672,9 @@ discard block |
||
| 672 | 672 | $milters=array(); |
| 673 | 673 | $query = 'SELECT `name` FROM `config`'; |
| 674 | 674 | |
| 675 | - $result = $myconn->query($query); |
|
| 676 | - if($result === false) { |
|
| 677 | - syslog(LOG_EMERG, "$loguser: ALERT: Query <$query> failed: ".$myconn->error); |
|
| 675 | + $result = $myconn->query($query); |
|
| 676 | + if($result === false) { |
|
| 677 | + syslog(LOG_EMERG, "$loguser: ALERT: Query <$query> failed: ".$myconn->error); |
|
| 678 | 678 | return FALSE; |
| 679 | 679 | } |
| 680 | 680 | if ($result->num_rows) { |
@@ -744,21 +744,21 @@ discard block |
||
| 744 | 744 | |
| 745 | 745 | function curl_get($url, array $get = NULL, array $options = array(), $loguser) |
| 746 | 746 | { |
| 747 | - $defaults = array( |
|
| 748 | - CURLOPT_URL => $url. (strpos($url, '?') === FALSE ? '?' : ''). http_build_query($get), |
|
| 749 | - CURLOPT_HEADER => 0, |
|
| 750 | - CURLOPT_RETURNTRANSFER => TRUE, |
|
| 751 | - CURLOPT_TIMEOUT => 4 |
|
| 752 | - ); |
|
| 753 | - |
|
| 754 | - $ch = curl_init(); |
|
| 755 | - curl_setopt_array($ch, ($options + $defaults)); |
|
| 756 | - if( ! $result = curl_exec($ch)) |
|
| 757 | - { |
|
| 758 | - syslog(LOG_ERR, sprintf('%s: CURL Error: <%s>', $loguser, curl_error($ch))); |
|
| 759 | - } |
|
| 760 | - curl_close($ch); |
|
| 761 | - return $result; |
|
| 747 | + $defaults = array( |
|
| 748 | + CURLOPT_URL => $url. (strpos($url, '?') === FALSE ? '?' : ''). http_build_query($get), |
|
| 749 | + CURLOPT_HEADER => 0, |
|
| 750 | + CURLOPT_RETURNTRANSFER => TRUE, |
|
| 751 | + CURLOPT_TIMEOUT => 4 |
|
| 752 | + ); |
|
| 753 | + |
|
| 754 | + $ch = curl_init(); |
|
| 755 | + curl_setopt_array($ch, ($options + $defaults)); |
|
| 756 | + if( ! $result = curl_exec($ch)) |
|
| 757 | + { |
|
| 758 | + syslog(LOG_ERR, sprintf('%s: CURL Error: <%s>', $loguser, curl_error($ch))); |
|
| 759 | + } |
|
| 760 | + curl_close($ch); |
|
| 761 | + return $result; |
|
| 762 | 762 | } |
| 763 | 763 | |
| 764 | 764 | /* |