summaryrefslogtreecommitdiff
path: root/src/smtpdlog.c
blob: 6cb02662df4a809b74c068565192d6a97009bf0f (plain)
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#include "smtpdlog.h"

#include <unistd.h>

#include "buffer.h"
#include "byte.h"
#include "env.h"
#include "exit.h"
#include "fmt.h"
#include "str.h"

#define FDLOG 2

char *reply421pgl;
char *reply550hlo;
char *reply550mbx;
char *reply552siz;
char *reply553bmf;
char *reply553brt;
char *reply553ngw;
char *reply553env;
char *reply553inv;
char *reply554cnt;

static char strnum[FMT_ULONG];
static char logbuf[512];
buffer bo2 = BUFFER_INIT(write, FDLOG, logbuf, sizeof(logbuf));

void smtpdlog_init()
{
  reply421pgl = env_get("REPLY_GREYLISTED");
  reply550hlo = env_get("REPLY_HELO");
  reply550mbx = env_get("REPLY_MAILBOX");
  reply552siz = env_get("REPLY_MAXSIZE");
  reply553bmf = env_get("REPLY_BADMAILFROM");
  reply553brt = env_get("REPLY_BADRCPTTO");
  reply553env = env_get("REPLY_SENDEREXIST");
  reply553ngw = env_get("REPLY_NOGATEWAY");
  reply553inv = env_get("REPLY_SENDERINVALID");
  reply554cnt = env_get("REPLY_CONTENT");
}

static void logs(char *s)
{
  if (buffer_puts(&bo2, s) == -1) _exit(1);
} /* single string */

static void logp(char *s)
{
  logs(" P:");
  logs(s);
} /* protocol */

static void logh(char *s1, char *s2, char *s3)
{
  logs(" S:");
  logs(s1);
  logs(":");
  logs(s2);
  logs(" H:");
  logs(s3);
} /* host */

static void logm(char *s)
{
  logs(" F:");
  logs(s);
} /* mailfrom */

static void logt(char *s)
{
  logs(" T:");
  logs(s);
} /* rcptto */

static void logi(char *s)
{
  logs(" '");
  logs(s);
  logs("'");
} /* information */

static void logn(char *s)
{
  if (buffer_puts(&bo2, s) == -1) _exit(1);
  if (buffer_flush(&bo2) == -1) _exit(1);
} /* end */

static void logpid()
{
  strnum[fmt_ulong(strnum, getpid())] = 0;
  logs("qmail-smtpd: pid ");
  logs(strnum);
  logs(" ");
}

void smtp_loga(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7, char *s8, char *s9)
{
  logpid();
  logs(s1);
  logs(s9);
  logp(s2);
  logh(s3, s4, s5);
  logm(s6);
  logt(s7), logs(" ?~");
  logi(s8);
  logn("\n");
} /* Auth info */

void smtp_logb(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7)
{
  logpid();
  logs(s1);
  logs(s7);
  logp(s2);
  logh(s3, s4, s5);
  logs(" ?~");
  logi(s6);
  logn("\n");
} /* Auth info */

void smtp_logg(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7)
{
  logpid();
  logs(s1);
  logp(s2);
  logh(s3, s4, s5);
  logm(s6);
  logt(s7);
  logn("\n");
} /* Generic */

void smtp_logh(char *s1, char *s2, char *s3, char *s4, char *s5)
{
  logpid();
  logs(s1);
  logp(s2);
  logh(s3, s4, s5);
  logn("\n");
} /* Host */

void smtp_logi(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7, char *s8)
{
  logpid();
  logs(s1);
  logp(s2);
  logh(s3, s4, s5);
  logm(s6);
  logt(s7);
  logi(s8);
  logn("\n");
} /* Generic + Info */

void smtp_logr(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7, char *s8)
{
  logpid();
  logs(s1);
  logs(s2);
  logp(s3);
  logh(s4, s5, s6);
  logm(s7);
  logt(s8);
  logn("\n");
} /* Recipient */

void die_read()
{
  _exit(1);
}

void die_alarm()
{
  out("451 timeout (#4.4.2)\r\n");
  flush();
  _exit(1);
}

void die_nomem()
{
  out("421 out of memory (#4.3.0)\r\n");
  flush();
  _exit(1);
}

void die_control()
{
  out("421 unable to read controls (#4.3.0)\r\n");
  flush();
  _exit(1);
}

void die_ipme()
{
  out("421 unable to figure out my IP addresses (#4.3.0)\r\n");
  flush();
  _exit(1);
}

void die_starttls()
{
  out("454 TLS not available due to temporary reason (#5.7.3)\r\n");
  flush();
  _exit(1);
}

void die_recipients()
{
  out("421 unable to check recipients (#4.3.0)\r\n");
  flush();
  _exit(1);
}

void err_unimpl()
{
  out("500 unimplemented (#5.5.1)\r\n");
}

void err_syntax()
{
  out("555 syntax error (#5.5.4)\r\n");
}

void err_noop()
{
  out("250 ok\r\n");
}

void err_vrfy()
{
  out("252 send some mail, i'll try my best\r\n");
}

void err_qqt()
{
  out("451 qqt failure (#4.3.0)\r\n");
}

int err_child()
{
  out("454 problem with child and I can't auth (#4.3.0)\r\n");
  return -1;
}

int err_fork()
{
  out("454 child won't start and I can't auth (#4.3.0)\r\n");
  return -1;
}

int err_pipe()
{
  out("454 unable to open pipe and I can't auth (#4.3.0)\r\n");
  return -1;
}

int err_write()
{
  out("454 unable to write pipe and I can't auth (#4.3.0)\r\n");
  return -1;
}

int err_postgl()
{
  out("454 problem with child and I can't greylist (#4.3.0)\r\n");
  return -1;
}

int err_forkgl()
{
  out("454 problem with child and I can't greylist (#4.3.0)\r\n");
  return -1;
}

/* TLS */

int err_starttls()
{
  out("454 TLS not available due to temporary reason (#5.7.3)\r\n");
  _exit(1);
}

void err_tlsreq(char *s1, char *s2, char *s3, char *s4, char *s5)
{
  out("535 STARTTLS required (#5.7.1)\r\n");
  smtp_logh(s1, s2, s3, s4, s5);
}

/* Helo */

void err_helo(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7, char *s8)
{
  out("550 sorry, invalid HELO/EHLO greeting ");
  if (reply550hlo) out(reply550hlo);
  out(" (#5.7.1)\r\n");
  smtp_logi(s1, s2, s3, s4, s5, s6, s7, s8);
}

/* Auth */

void err_authsetup(char *s1, char *s2, char *s3, char *s4, char *s5)
{
  out("530 Auth not available (#5.7.1)\r\n");
  smtp_logh(s1, s2, s3, s4, s5);
}

void err_authd()
{
  out("503 you're already authenticated (#5.5.0)\r\n");
}

void err_authmail()
{
  out("503 no auth during mail transaction (#5.5.0)\r\n");
}

void err_authfail(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7)
{
  out("535 authentication failed (#5.7.1)\r\n");
  smtp_logb(s1, s2, s3, s4, s5, s6, s7);
}

void err_authreq(char *s1, char *s2, char *s3, char *s4, char *s5)
{
  out("535 authentication required (#5.7.1)\r\n");
  smtp_logh(s1, s2, s3, s4, s5);
}

void err_submission(char *s1, char *s2, char *s3, char *s4, char *s5)
{
  out("530 Authorization required (#5.7.1) \r\n");
  smtp_logh(s1, s2, s3, s4, s5);
}

int err_authabort()
{
  out("501 auth exchange canceled (#5.0.0)\r\n");
  return -1;
}

int err_authinput()
{
  out("501 malformed auth input (#5.5.4)\r\n");
  return -1;
}

void err_authinvalid(char *s1, char *s2, char *s3, char *s4, char *s5)
{
  out("504 auth type unimplemented (#5.5.1)\r\n");
  smtp_logh(s1, s2, s3, s4, s5);
}

int err_noauth()
{
  out("504 auth type unimplemented (#5.5.1)\r\n");
  return -1;
}

/* Mail From: */

void err_wantmail()
{
  out("503 MAIL first (#5.5.1)\r\n");
}

void err_mav(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7)
{
  out("553 sorry, invalid sender address specified ");
  if (reply553inv) out(reply553inv);
  out(" (#5.7.1)\r\n");
  smtp_logg(s1, s2, s3, s4, s5, s6, s7);
}

void err_bmf(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7, char *s8)
{
  out("553 sorry, your envelope sender is in my badmailfrom list ");
  if (reply553bmf) out(reply553bmf);
  out(" (#5.7.1)\r\n");
  smtp_logi(s1, s2, s3, s4, s5, s6, s7, s8);
}

void err_mfdns(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7)
{
  out("553 sorry, your envelope sender must exist ");
  if (reply553env) out(reply553env);
  out(" (#5.7.1)\r\n");
  smtp_logg(s1, s2, s3, s4, s5, s6, s7);
}

/* SPF */

void err_spf(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7, char *msg)
{
  int i, j;
  int len = str_len(msg);

  for (i = 0; i < len; i = j + 1) {
    j = byte_chr(msg + i, len - i, '\n') + i;
    if (j < len) {
      out("550-");
      msg[j] = 0;
      out(msg);
      msg[j] = '\n';
    } else {
      out("550 ");
      out(msg);
    }
  }
  out(" (#5.7.1)\r\n");

  smtp_logg(s1, s2, s3, s4, s5, s6, s7);
}

/* Rcpt To: */

void err_wantrcpt()
{
  out("503 RCPT first (#5.5.1)\r\n");
}

void postgrey(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7)
{
  out("421 greylisted");
  if (reply421pgl) out(reply421pgl);
  out(" (#4.3.0)\r\n");
  smtp_logg(s1, s2, s3, s4, s5, s6, s7);
}

void err_nogateway(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7)
{
  out("553 sorry, that domain isn't in my list of allowed rcpthosts ");
  if (reply553ngw) out(reply553ngw);
  out(" (#5.7.1)\r\n");
  smtp_logg(s1, s2, s3, s4, s5, s6, s7);
}

void err_brt(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7)
{
  out("553 sorry, your envelope recipient is in my badrcptto list ");
  if (reply553brt) out(reply553brt);
  out(" (#5.7.1)\r\n");
  smtp_logg(s1, s2, s3, s4, s5, s6, s7);
}

void err_rcpts(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7)
{
  out("452 sorry, too many recipients (#4.5.3)\r\n"); /* RFC 5321 */
  smtp_logg(s1, s2, s3, s4, s5, s6, s7);
}

void err_recipient(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7)
{
  if (env_get("RECIPIENTS450"))
    out("450 sorry, mailbox currently unavailable (#4.2.1)\r\n");
  else {
    out("550 sorry, no mailbox by that name ");
    if (reply550mbx) out(reply550mbx);
    out(" (#5.7.1)\r\n");
  }
  smtp_logg(s1, s2, s3, s4, s5, s6, s7);
}

/* Data */

void straynewline()
{
  out("451 Bare Line Feeds (LF) are not accepted in SMTP; CRLF is required according to RFC 2822.\r\n");
  flush();
  _exit(1);
}

void err_notorious()
{
  out("503 DATA command not accepted at this time (#5.5.1)\r\n");
  flush();
  _exit(1);
}

void err_size(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7)
{
  out("552 sorry, that message size exceeds my databytes limit (#5.3.4)\r\n");
  smtp_logg(s1, s2, s3, s4, s5, s6, s7);
}

void err_data(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7, char *s8)
{
  out("554 sorry, invalid message content ");
  if (reply554cnt) out(reply554cnt);
  out(" (#5.3.2)\r\n");
  smtp_logi(s1, s2, s3, s4, s5, s6, s7, s8);
}