-
Notifications
You must be signed in to change notification settings - Fork 8
/
EximDatabaseLogger.php
361 lines (345 loc) · 11.4 KB
/
EximDatabaseLogger.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php // $Header$
// based on eximstats
error_reporting(E_ALL);
define('EMAIL_REGEX', '[_[:alnum:]-]+(\.[_[:alnum:]-]+)*@[[:alnum:]-]+(\.[[:alnum:]-]+)*(\.([[:alpha:]]){2,4})');
define('LOG_TYPE_RECEIVED', 'RECEIVED');
define('LOG_TYPE_DELIVERED', 'DELIVERED');
define('LOG_TYPE_BOUNCED', 'BOUNCED');
define('LOG_TYPE_BLACKLISTED', 'BLACKLISTED');
define('LOG_TYPE_SPAM', 'SPAM');
define('LOG_TYPE_MALWARE', 'MALWARE');
define('LOG_TYPE_MIME_ERROR', 'MIME_ERROR');
define('LOG_TYPE_BAD_ATTACHMENT', 'BAD_ATTACHMENT');
define('LOG_TYPE_SENDER_VERIFY_FAIL', 'REJECT_INVALID_SENDER');
define('LOG_TYPE_SENDER_VERIFY_TEMP_FAIL', 'TEMP_REJECT_INVALID_SENDER');
define('LOG_TYPE_OTHER_TEMP_REJECT', 'OTHER_TEMP_REJECT');
define('HOSTNAME_FUNCTION', '/bin/hostname --fqdn');
/**
*
* Logs entries from Exim's mainlog to a database
*
* @author Christian G. Warden <[email protected]>
* @copyright (c) 2004 by The Postica Corporation
* @version $Id$
*/
class EximDatabaseLogger {
var $conn;
var $mtaId;
/**
* Ignore records whose timestamp is before this time.
* @var integer
* @access public
*/
var $startTime;
/**
* Ignore records whose timestamp is less than the maximum timestamp for this mta.
* @var integer
* @access public
*/
var $ignoreOldEntries;
/**
*
* Set timestamp, before which, records should be ignored.
*
* @access public
*/
function setStartTime($timestamp) {
$this->startTime = $timestamp;
}
/**
*
* Takes a database resource an optionally the id of the mta this log is from
*
* @return EximDatabaseLogger
* @access public
*/
function EximDatabaseLogger(&$conn, $mtaId = null) {
define_syslog_variables();
openlog('exidblog', LOG_PERROR | LOG_PID, LOG_MAIL);
$this->conn =& $conn;
if (! is_null($mtaId)) {
$this->mtaId = $mtaId;
} else {
$hostname = chop(exec(HOSTNAME_FUNCTION));
$res = mysql_query(sprintf(
"SELECT id FROM mta WHERE name = '%s'",
mysql_real_escape_string($hostname)));
if ($res === false) {
syslog(LOG_ERR, 'failed to get mta id: ' . mysql_error());
die();
}
if (mysql_num_rows($res) != 1) {
syslog(LOG_ERR, 'Failed to find record for mta: ' . $hostname);
die();
}
list($this->mtaId) = mysql_fetch_array($res);
if ($this->mtaId === false) {
syslog(LOG_ERR, 'failed to get mta field: ' . mysql_error());
die();
}
mysql_free_result($res) || die('failed to free result');
}
}
/**
*
* Takes a file handle and read until
*
* @return void
* @access public
*/
function parse($handle) {
if ($this->ignoreOldEntries) {
$res = mysql_query(sprintf('
SELECT
UNIX_TIMESTAMP(MAX(timestamp))
FROM
log
WHERE
mta = %d',
$this->mtaId));
if ($res === false) {
syslog(LOG_ERR, 'query failed: ' . mysql_error());
die();
}
list($this->startTime) = mysql_fetch_array($res) or die('failed to get start time: ' . mysql_error());
mysql_free_result($res) || die('failed to free result');
}
$fakeRejects = array();
do {
$line = fgets($handle);
if (strlen($line) == 0) {
break;
}
if (strlen($line) < 38 ||
!preg_match('/^(\d{4})\-(\d\d)-(\d\d)\s(\d\d):(\d\d):(\d\d)( [-+]\d\d\d\d)?/', $line, $matches)) {
continue;
}
$timestamp = mktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
if ($timestamp <= $this->startTime) {
echo 'skipping line. timestamp ' . date('Y-m-d G:i:s', $timestamp) . ' before ' . date('Y-m-d G:i:s', $this->startTime) . "\n";
continue;
}
$extra = !empty($matches[7]) ? 6 : 0;
$id = substr($line, 20 + $extra, 16);
$flag = substr($line, 37 + $extra, 2);
$remoteHost = '';
$remoteHostDomain = '';
$ip = '';
$senderEmail = '';
$senderEmailDomain = '';
$recipient = '';
$recipientDomain = '';
$recipients = array();
$headerSubject = '';
$headerMessageID = '';
$logType = '';
$additionalData = '';
if (preg_match('/\sH=(\S+)/', $line, $matches)) {
$remoteHost = $matches[1];
if (preg_match('/\sH=.*?(\s\[([^]]+)\])/', $line, $matches)) {
$ip = $matches[2];
} else {
$ip = '';
}
if ($remoteHost[0] != '[' && preg_match('/^(\(?)[^\.]+\.([^\.]+\..*)/', $remoteHost, $matches)) {
$remoteHostDomain = $matches[1] . $matches[2];
}
}
if ($flag == '<=') {
if (isset($fakeRejects[$id])) {
unset($fakeRejects[$id]);
continue;
}
$line = substr($line, 40 + $extra);
# sender email
$senderEmail = preg_match('/^(\S+)/', $line, $matches) ? $matches[1] : '';
$senderEmailDomain = preg_match('/^\S*?\@(\S+)/', $line, $matches) ? $matches[1] : '';
$pattern = '/from <[^>]*?> for(( ((' . EMAIL_REGEX . ')|([-\w]+)))+)$/';
preg_match($pattern, $line, $matches);
$recipient = ltrim($matches[1]);
$recipients = explode(' ', $recipient);
$headerMessageID = preg_match('/\sid=(\S+)/', $line, $matches) ? $matches[1] : '';
$headerSubject = preg_match('/\sT="(.*)" from </', $line, $matches) ? $matches[1] : '';
// additionalData is authenticated sender
$additionalData = preg_match('/ A=(cram_md5|spa|login|plain):(\S+) S=/', $line, $matches) ? $matches[2] : '';
$logType = LOG_TYPE_RECEIVED;
} elseif ($flag == '=>' || $flag == '->') {
$router = preg_match('/\sR=(\S+)/', $line, $matches) ? $matches[1] : '';
switch ($router) {
case 'discard_spam':
case 'cache':
continue 2;
break;
}
$line = substr($line, 40 + $extra);
$senderEmail = preg_match('/\sF=<(\S+)>/', $line, $matches) ? $matches[1] : '<>';
$senderEmailDomain = ltrim(strstr($senderEmail, '@'), '@');
$recipients[] = preg_match('/^(\S+)/', $line, $matches) ? $matches[1] : '';
$additionalData = preg_match('/\sC="(.*)"$/', $line, $matches) ? $matches[1] : '';
$logType = LOG_TYPE_DELIVERED;
} elseif ($flag == '**') {
$line = substr($line, 40 + $extra);
$senderEmail = preg_match('/\sF=<(\S+)>/', $line, $matches) ? $matches[1] : '<>';
$senderEmailDomain = ltrim(strstr($senderEmail, '@'), '@');
$recipients[] = preg_match('/^(\S+)/', $line, $matches) ? $matches[1] : '';
$router = preg_match('/\sR=(\S+)/', $line, $matches) ? $matches[1] : '';
$logType = LOG_TYPE_BOUNCED;
// what caused the bounce?
switch ($router) {
}
} elseif (($flag == 'FR' && preg_match('/^(\S+) \(([^)]*)\)/', substr($line, 40 + $extra), $matches)) ||
($flag == 'H=' && preg_match('/rejected after DATA: (\S+) \(([^)]*)\)/', $line, $matches))) {
// rejected in data acl
switch ($matches[1]) {
case 'SPAM':
$logType = LOG_TYPE_SPAM;
break;
case 'MALWARE':
$logType = LOG_TYPE_MALWARE;
break;
case 'MIME_ERROR':
$logType = LOG_TYPE_MIME_ERROR;
break;
case 'BAD_ATTACHMENT':
$logType = LOG_TYPE_BAD_ATTACHMENT;
break;
}
$additionalData = $matches[2];
$senderEmail = preg_match('/\sF=<(\S+)>/', $line, $matches) ? $matches[1] : '<>';
$senderEmailDomain = ltrim(strstr($senderEmail, '@'), '@');
$headerMessageID = preg_match('/\sMSGID=(\S+)/', $line, $matches) ? $matches[1] : '';
$headerSubject = preg_match('/\sSUB="(.*)" MSGID/', $line, $matches) ? $matches[1] : '';
$recipient = preg_match('/\sRCPT=((\S+ ))+SUB/', $line, $matches) ? trim($matches[1]) : '';
$recipients = explode(':', $recipient);
if ($flag == 'FR') {
$fakeRejects[$id] = '';
}
} elseif (substr($id, 0, 2) == 'H=' && preg_match('/F=<(.*?)> (temporarily )?rejected RCPT/', $line, $matches)) {
$id = '';
$senderEmail = $matches[1] != '' ? $matches[1] : '<>';
$senderEmailDomain = ltrim(strstr($senderEmail, '@'), '@');
$temporary = isset($matches[2]);
preg_match('/\srejected RCPT <?([^>]+)>?:(.*)/', $line, $matches);
$recipients[] = $matches[1];
if (strpos($matches[2], 'Sender verify failed') !== false) {
$logType = LOG_TYPE_SENDER_VERIFY_FAIL;
} elseif (strpos($matches[2], 'Could not complete sender verify') !== false) {
$logType = LOG_TYPE_SENDER_VERIFY_TEMP_FAIL;
} elseif (strpos($matches[2], 'blacklisted') !== false) {
$logType = LOG_TYPE_BLACKLISTED;
} elseif ($temporary) {
$logType = LOG_TYPE_OTHER_TEMP_REJECT;
$additionalData = ltrim($matches[2]);
} else {
// user unknown, relay not permitted
// echo 'Perm reject RCPT: ' . $matches[1] . " : " . $matches[2] . "\n";
continue;
}
} else {
continue;
}
$logEntry =& new LogEntry($this->conn);
$logEntry->mta = $this->mtaId;
$logEntry->timestamp = $timestamp;
$logEntry->logType = $logType;
$logEntry->senderEmail = strtolower($senderEmail);
$logEntry->senderEmailDomain = strtolower($senderEmailDomain);
$logEntry->remoteHost = strtolower($remoteHost);
$logEntry->remoteHostDomain = strtolower($remoteHostDomain);
$logEntry->remoteIp = $ip;
$logEntry->eximMessageId = $id;
$logEntry->headerSubject = str_replace('\"', '"', $headerSubject);
$logEntry->headerMessageID = $headerMessageID;
$logEntry->additionalData = $additionalData;
foreach ($recipients as $recipientEmail) {
$recipientEmailDomain = '';
if (preg_match('/^' . EMAIL_REGEX . '$/', $recipientEmail)) {
$recipientEmailDomain = ltrim(strstr($recipientEmail, '@'), '@');
}
$logEntry->recipientEmail = strtolower($recipientEmail);
$logEntry->recipientEmailDomain = strtolower($recipientEmailDomain);
$logEntry->store();
}
unset($logEntry);
} while (true);
}
}
class LogEntry {
var $conn;
var $mta;
var $timestamp;
var $logType;
var $senderEmail;
var $senderEmailDomain;
var $remoteHost;
var $remoteHostDomain;
var $remoteIp;
var $eximMessageId;
var $headerSubject;
var $headerMessageID;
var $recipientEmail;
var $recipientEmailDomain;
var $additionalData;
function LogEntry(&$conn) {
$this->conn =& $conn;
}
function store() {
while (mysql_ping($this->conn) === false) {
syslog(LOG_WARNING, 'Lost connection to database. Will retry in 60 seconds.');
sleep(60);
}
$sql = sprintf("
INSERT INTO
log
(
mta,
timestamp,
log_type,
sender_email,
sender_email_domain,
remote_hostname,
remote_hostname_domain,
remote_ip,
exim_message_id,
recipient_email,
recipient_email_domain,
header_subject,
header_message_id,
additional_data
) VALUES (
%d,
FROM_UNIXTIME(%d),
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s'
)",
$this->mta,
$this->timestamp,
$this->logType,
mysql_real_escape_string($this->senderEmail, $this->conn),
mysql_real_escape_string($this->senderEmailDomain, $this->conn),
mysql_real_escape_string($this->remoteHost, $this->conn),
mysql_real_escape_string($this->remoteHostDomain, $this->conn),
mysql_real_escape_string($this->remoteIp, $this->conn),
mysql_real_escape_string($this->eximMessageId, $this->conn),
mysql_real_escape_string($this->recipientEmail, $this->conn),
mysql_real_escape_string($this->recipientEmailDomain, $this->conn),
mysql_real_escape_string($this->headerSubject, $this->conn),
mysql_real_escape_string($this->headerMessageID, $this->conn),
mysql_real_escape_string($this->additionalData, $this->conn));
if (! mysql_query($sql, $this->conn)) {
syslog(LOG_ERR, 'Failed to insert record: ' . mysql_error());
die();
}
}
}
?>