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
491
492
493
494
495
496
497
498
499
500
501
502
|
/* $OpenBSD: mda.c,v 1.80 2012/10/14 20:18:22 eric Exp $ */
/*
* Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org>
* Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>
* Copyright (c) 2009 Jacek Masiulaniec <jacekm@dobremiasto.net>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/tree.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <ctype.h>
#include <err.h>
#include <event.h>
#include <imsg.h>
#include <inttypes.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <vis.h>
#include "smtpd.h"
#include "log.h"
#define MDA_MAXPERUSER 7
struct mda_session {
struct envelope msg;
struct msgbuf w;
struct event ev;
FILE *datafp;
};
struct mda_user {
TAILQ_ENTRY(mda_user) entry;
char name[MAXLOGNAME];
size_t running;
};
static void mda_imsg(struct imsgev *, struct imsg *);
static void mda_shutdown(void);
static void mda_sig_handler(int, short, void *);
static void mda_store(struct mda_session *);
static void mda_store_event(int, short, void *);
static int mda_check_loop(FILE *, struct envelope *);
static int mda_user_increment(const char *);
static void mda_user_decrement(const char *);
static struct tree sessions;
static uint32_t mda_id = 0;
static TAILQ_HEAD(, mda_user) users;
static void
mda_imsg(struct imsgev *iev, struct imsg *imsg)
{
char output[128], *error, *parent_error;
char stat[MAX_LINE_SIZE];
struct deliver deliver;
struct mda_session *s;
struct delivery_mda *d_mda;
struct envelope *ep;
FILE *fp;
uint16_t msg;
uint32_t id;
if (iev->proc == PROC_QUEUE) {
switch (imsg->hdr.type) {
case IMSG_MDA_SESS_NEW:
ep = (struct envelope *)imsg->data;
fp = fdopen(imsg->fd, "r");
if (fp == NULL)
fatalx("mda: fdopen");
if (mda_check_loop(fp, ep)) {
log_debug("mda: loop detected");
envelope_set_errormsg(ep, "646 loop detected");
imsg_compose_event(env->sc_ievs[PROC_QUEUE],
IMSG_QUEUE_DELIVERY_LOOP, 0, 0, -1, ep,
sizeof *ep);
fclose(fp);
return;
}
if (mda_user_increment(ep->agent.mda.user) == -1) {
envelope_set_errormsg(ep, "mda limit reached");
imsg_compose_event(env->sc_ievs[PROC_QUEUE],
IMSG_QUEUE_DELIVERY_TEMPFAIL, 0, 0, -1, ep,
sizeof *ep);
fclose(fp);
return;
}
/* make new session based on provided args */
s = xcalloc(1, sizeof *s, "mda_imsg");
msgbuf_init(&s->w);
s->msg = *ep;
s->datafp = fp;
id = mda_id++;
tree_xset(&sessions, id, s);
/* request parent to fork a helper process */
ep = &s->msg;
d_mda = &s->msg.agent.mda;
switch (d_mda->method) {
case A_MDA:
deliver.mode = A_MDA;
strlcpy(deliver.user, d_mda->user,
sizeof (deliver.user));
strlcpy(deliver.to, d_mda->buffer,
sizeof deliver.to);
break;
case A_MBOX:
deliver.mode = A_MBOX;
strlcpy(deliver.user, "root",
sizeof (deliver.user));
strlcpy(deliver.to, d_mda->user,
sizeof (deliver.to));
snprintf(deliver.from, sizeof(deliver.from),
"%s@%s", ep->sender.user,
ep->sender.domain);
break;
case A_MAILDIR:
deliver.mode = A_MAILDIR;
strlcpy(deliver.user, d_mda->user,
sizeof deliver.user);
strlcpy(deliver.to, d_mda->buffer,
sizeof deliver.to);
break;
case A_FILENAME:
deliver.mode = A_FILENAME;
strlcpy(deliver.user, d_mda->user,
sizeof deliver.user);
strlcpy(deliver.to, d_mda->buffer,
sizeof deliver.to);
break;
default:
errx(1, "mda: unknown delivery method: %d",
d_mda->method);
}
imsg_compose_event(env->sc_ievs[PROC_PARENT],
IMSG_PARENT_FORK_MDA, id, 0, -1, &deliver,
sizeof deliver);
return;
}
}
if (iev->proc == PROC_PARENT) {
switch (imsg->hdr.type) {
case IMSG_PARENT_FORK_MDA:
s = tree_xget(&sessions, imsg->hdr.peerid);
if (imsg->fd < 0)
fatalx("mda: fd pass fail");
s->w.fd = imsg->fd;
mda_store(s);
return;
case IMSG_MDA_DONE:
s = tree_xpop(&sessions, imsg->hdr.peerid);
/*
* Grab last line of mda stdout/stderr if available.
*/
output[0] = '\0';
if (imsg->fd != -1) {
char *ln, *buf;
size_t len;
buf = NULL;
if (lseek(imsg->fd, 0, SEEK_SET) < 0)
fatalx("lseek");
fp = fdopen(imsg->fd, "r");
if (fp == NULL)
fatal("mda: fdopen");
while ((ln = fgetln(fp, &len))) {
if (ln[len - 1] == '\n')
ln[len - 1] = '\0';
else {
buf = xmalloc(len + 1,
"mda_imsg");
memcpy(buf, ln, len);
buf[len] = '\0';
ln = buf;
}
strlcpy(output, "\"", sizeof output);
strnvis(output + 1, ln,
sizeof(output) - 2,
VIS_SAFE | VIS_CSTYLE);
strlcat(output, "\"", sizeof output);
log_debug("mda_out: %s", output);
}
free(buf);
fclose(fp);
}
/*
* Choose between parent's description of error and
* child's output, the latter having preference over
* the former.
*/
error = NULL;
parent_error = imsg->data;
if (strcmp(parent_error, "exited okay") == 0) {
if (!feof(s->datafp) || s->w.queued)
error = "mda exited prematurely";
} else {
if (output[0])
error = output;
else
error = parent_error;
}
/* update queue entry */
msg = IMSG_QUEUE_DELIVERY_OK;
if (error) {
msg = IMSG_QUEUE_DELIVERY_TEMPFAIL;
envelope_set_errormsg(&s->msg, "%s", error);
snprintf(stat, sizeof stat, "Error (%s)", error);
}
imsg_compose_event(env->sc_ievs[PROC_QUEUE], msg,
0, 0, -1, &s->msg, sizeof s->msg);
log_envelope(&s->msg, NULL, error ? stat : "Delivered");
mda_user_decrement(s->msg.agent.mda.user);
/* destroy session */
if (s->w.fd != -1)
close(s->w.fd);
if (s->datafp)
fclose(s->datafp);
msgbuf_clear(&s->w);
event_del(&s->ev);
free(s);
return;
case IMSG_CTL_VERBOSE:
log_verbose(*(int *)imsg->data);
return;
}
}
errx(1, "mda_imsg: unexpected %s imsg", imsg_to_str(imsg->hdr.type));
}
static void
mda_sig_handler(int sig, short event, void *p)
{
switch (sig) {
case SIGINT:
case SIGTERM:
mda_shutdown();
break;
default:
fatalx("mda_sig_handler: unexpected signal");
}
}
static void
mda_shutdown(void)
{
log_info("mail delivery agent exiting");
_exit(0);
}
pid_t
mda(void)
{
pid_t pid;
struct passwd *pw;
struct event ev_sigint;
struct event ev_sigterm;
struct peer peers[] = {
{ PROC_PARENT, imsg_dispatch },
{ PROC_QUEUE, imsg_dispatch },
{ PROC_CONTROL, imsg_dispatch }
};
switch (pid = fork()) {
case -1:
fatal("mda: cannot fork");
case 0:
break;
default:
return (pid);
}
purge_config(PURGE_EVERYTHING);
pw = env->sc_pw;
if (chroot(pw->pw_dir) == -1)
fatal("mda: chroot");
if (chdir("/") == -1)
fatal("mda: chdir(\"/\")");
smtpd_process = PROC_MDA;
setproctitle("%s", env->sc_title[smtpd_process]);
if (setgroups(1, &pw->pw_gid) ||
setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
fatal("mda: cannot drop privileges");
tree_init(&sessions);
TAILQ_INIT(&users);
imsg_callback = mda_imsg;
event_init();
signal_set(&ev_sigint, SIGINT, mda_sig_handler, NULL);
signal_set(&ev_sigterm, SIGTERM, mda_sig_handler, NULL);
signal_add(&ev_sigint, NULL);
signal_add(&ev_sigterm, NULL);
signal(SIGPIPE, SIG_IGN);
signal(SIGHUP, SIG_IGN);
config_pipes(peers, nitems(peers));
config_peers(peers, nitems(peers));
if (event_dispatch() < 0)
fatal("event_dispatch");
mda_shutdown();
return (0);
}
static void
mda_store(struct mda_session *s)
{
char *p;
struct ibuf *buf;
int len;
if (s->msg.sender.user[0] && s->msg.sender.domain[0])
/* XXX: remove user provided Return-Path, if any */
len = asprintf(&p, "Return-Path: %s@%s\nDelivered-To: %s@%s\n",
s->msg.sender.user, s->msg.sender.domain,
s->msg.rcpt.user,
s->msg.rcpt.domain);
else
len = asprintf(&p, "Delivered-To: %s@%s\n",
s->msg.rcpt.user,
s->msg.rcpt.domain);
if (len == -1)
fatal("mda_store: asprintf");
session_socket_blockmode(s->w.fd, BM_NONBLOCK);
if ((buf = ibuf_open(len)) == NULL)
fatal(NULL);
if (ibuf_add(buf, p, len) < 0)
fatal(NULL);
ibuf_close(&s->w, buf);
event_set(&s->ev, s->w.fd, EV_WRITE, mda_store_event, s);
event_add(&s->ev, NULL);
free(p);
}
static void
mda_store_event(int fd, short event, void *p)
{
char tmp[16384];
struct mda_session *s = p;
struct ibuf *buf;
size_t len;
if (s->w.queued == 0) {
if ((buf = ibuf_dynamic(0, sizeof tmp)) == NULL)
fatal(NULL);
len = fread(tmp, 1, sizeof tmp, s->datafp);
if (ferror(s->datafp))
fatal("mda_store_event: fread failed");
if (feof(s->datafp) && len == 0) {
close(s->w.fd);
s->w.fd = -1;
return;
}
if (ibuf_add(buf, tmp, len) < 0)
fatal(NULL);
ibuf_close(&s->w, buf);
}
if (ibuf_write(&s->w) < 0) {
close(s->w.fd);
s->w.fd = -1;
return;
}
event_set(&s->ev, fd, EV_WRITE, mda_store_event, s);
event_add(&s->ev, NULL);
}
static int
mda_check_loop(FILE *fp, struct envelope *ep)
{
char *buf, *lbuf;
size_t len;
struct mailaddr maddr;
int ret = 0;
lbuf = NULL;
while ((buf = fgetln(fp, &len))) {
if (buf[len - 1] == '\n')
buf[len - 1] = '\0';
else {
/* EOF without EOL, copy and add the NUL */
lbuf = xmalloc(len + 1, "mda_check_loop");
memcpy(lbuf, buf, len);
lbuf[len] = '\0';
buf = lbuf;
}
if (strchr(buf, ':') == NULL && !isspace((int)*buf))
break;
if (strncasecmp("Delivered-To: ", buf, 14) == 0) {
bzero(&maddr, sizeof maddr);
if (! email_to_mailaddr(&maddr, buf + 14))
continue;
if (strcasecmp(maddr.user, ep->dest.user) == 0 &&
strcasecmp(maddr.domain, ep->dest.domain) == 0) {
ret = 1;
break;
}
}
if (lbuf) {
free(lbuf);
lbuf = NULL;
}
}
if (lbuf)
free(lbuf);
fseek(fp, SEEK_SET, 0);
return (ret);
}
static int
mda_user_increment(const char *name)
{
struct mda_user *user;
TAILQ_FOREACH(user, &users, entry)
if (!strcmp(name, user->name)) {
if (user->running >= MDA_MAXPERUSER) {
log_debug("mda: too many mda proc for user %s",
name);
return (-1);
}
user->running += 1;
return (0);
}
user = xmalloc(sizeof *user, "mda_user");
strlcpy(user->name, name, sizeof user->name);
user->running = 1;
TAILQ_INSERT_TAIL(&users, user, entry);
return (0);
}
static void
mda_user_decrement(const char *name)
{
struct mda_user *user;
TAILQ_FOREACH(user, &users, entry)
if (!strcmp(name, user->name))
if (--user->running == 0) {
TAILQ_REMOVE(&users, user, entry);
free(user);
break;
}
}
|