-
Notifications
You must be signed in to change notification settings - Fork 50
/
mail-state.c
560 lines (472 loc) · 13.1 KB
/
mail-state.c
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/* $Id$ */
/*
* Copyright (c) 2006 Nicholas Marriott <[email protected]>
*
* 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 MIND, 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 <fnmatch.h>
#include <string.h>
#include <unistd.h>
#include "fdm.h"
#include "fetch.h"
#include "match.h"
void apply_result(struct expritem *, int *, int);
struct replstrs *find_delivery_users(struct mail_ctx *, struct action *, int *);
int fill_from_strings(struct mail_ctx *, struct rule *,
struct replstrs *);
int fill_from_string(struct mail_ctx *, struct rule *,
struct replstr *);
int fill_from_action(struct mail_ctx *, struct rule *,
struct action *, struct replstrs *);
int start_action(struct mail_ctx *, struct deliver_ctx *);
int finish_action(struct deliver_ctx *, struct msg *,
struct msgbuf *);
#define ACTION_DONE 0
#define ACTION_ERROR 1
#define ACTION_PARENT 2
/*
* Number of chained actions. Limit on recursion with things like:
*
* action "name" { action "name" }
*/
u_int chained;
/* Check mail against next rule or part of expression. */
int
mail_match(struct mail_ctx *mctx, struct msg *msg, struct msgbuf *msgbuf)
{
struct account *a = mctx->account;
struct mail *m = mctx->mail;
struct expritem *ei;
struct replstrs *users;
int should_free, this = -1, error = MAIL_CONTINUE;
char desc[DESCBUFSIZE];
set_wrapped(m, ' ');
/* If blocked, check for msgs from parent. */
if (mctx->msgid != 0) {
if (msg == NULL || msg->id != mctx->msgid)
return (MAIL_BLOCKED);
mctx->msgid = 0;
if (msg->type != MSG_DONE)
fatalx("unexpected message");
if (msgbuf->buf != NULL && msgbuf->len != 0) {
strb_destroy(&m->tags);
m->tags = msgbuf->buf;
reset_tags(&m->tags);
}
ei = mctx->expritem;
switch (msg->data.error) {
case MATCH_ERROR:
return (MAIL_ERROR);
case MATCH_TRUE:
this = 1;
break;
case MATCH_FALSE:
this = 0;
break;
default:
fatalx("unexpected response");
}
apply_result(ei, &mctx->result, this);
goto next_expritem;
}
/* Check for completion and end of ruleset. */
if (mctx->done)
return (MAIL_DONE);
if (mctx->rule == NULL) {
switch (conf.impl_act) {
case DECISION_NONE:
log_warnx("%s: reached end of ruleset. no "
"unmatched-mail option; keeping mail", a->name);
m->decision = DECISION_KEEP;
break;
case DECISION_KEEP:
log_debug2("%s: reached end of ruleset. keeping mail",
a->name);
m->decision = DECISION_KEEP;
break;
case DECISION_DROP:
log_debug2("%s: reached end of ruleset. dropping mail",
a->name);
m->decision = DECISION_DROP;
break;
}
return (MAIL_DONE);
}
/* Expression not started. Start it. */
if (mctx->expritem == NULL) {
/* Start the expression. */
mctx->result = 0;
mctx->expritem = TAILQ_FIRST(mctx->rule->expr);
}
/* Check this expression item and adjust the result. */
ei = mctx->expritem;
/* Handle short-circuit evaluation. */
switch (ei->op) {
case OP_NONE:
break;
case OP_AND:
/* And and the result is already false. */
if (!mctx->result)
goto next_expritem;
break;
case OP_OR:
/* Or and the result is already true. */
if (mctx->result)
goto next_expritem;
break;
}
switch (ei->match->match(mctx, ei)) {
case MATCH_ERROR:
return (MAIL_ERROR);
case MATCH_PARENT:
return (MAIL_BLOCKED);
case MATCH_TRUE:
this = 1;
break;
case MATCH_FALSE:
this = 0;
break;
default:
fatalx("unexpected op");
}
apply_result(ei, &mctx->result, this);
ei->match->desc(ei, desc, sizeof desc);
log_debug3("%s: tried %s, result now %d", a->name, desc, mctx->result);
next_expritem:
/* Move to the next item. If there is one, then return. */
mctx->expritem = TAILQ_NEXT(mctx->expritem, entry);
if (mctx->expritem != NULL)
return (MAIL_CONTINUE);
log_debug3("%s: finished rule %u, result %d", a->name, mctx->rule->idx,
mctx->result);
/* If the result was false, skip to find the next rule. */
if (!mctx->result)
goto next_rule;
log_debug2("%s: matched to rule %u", a->name, mctx->rule->idx);
/*
* If this rule is stop, mark the context so when we get back after
* delivery we know to stop.
*/
if (mctx->rule->stop)
mctx->done = 1;
/* Handle nested rules. */
if (!TAILQ_EMPTY(&mctx->rule->rules)) {
log_debug2("%s: entering nested rules", a->name);
/*
* Stack the current rule (we are at the end of it so the
* the expritem must be NULL already).
*/
ARRAY_ADD(&mctx->stack, mctx->rule);
/* Continue with the first rule of the nested list. */
mctx->rule = TAILQ_FIRST(&mctx->rule->rules);
return (MAIL_CONTINUE);
}
mctx->matched = 1;
/* Handle lambda actions. */
if (mctx->rule->lambda != NULL) {
users = find_delivery_users(mctx, NULL, &should_free);
chained = MAXACTIONCHAIN;
if (fill_from_action(mctx,
mctx->rule, mctx->rule->lambda, users) != 0) {
if (should_free)
ARRAY_FREEALL(users);
return (MAIL_ERROR);
}
if (should_free)
ARRAY_FREEALL(users);
error = MAIL_DELIVER;
}
/* Fill the delivery action queue. */
if (!ARRAY_EMPTY(mctx->rule->actions)) {
chained = MAXACTIONCHAIN;
if (fill_from_strings(mctx,
mctx->rule, mctx->rule->actions) != 0)
return (MAIL_ERROR);
error = MAIL_DELIVER;
}
next_rule:
/* Move to the next rule. */
mctx->ruleidx = mctx->rule->idx; /* save last index */
mctx->rule = TAILQ_NEXT(mctx->rule, entry);
/* If no more rules, try to move up the stack. */
while (mctx->rule == NULL) {
if (ARRAY_EMPTY(&mctx->stack))
break;
log_debug2("%s: exiting nested rules", a->name);
mctx->rule = ARRAY_LAST(&mctx->stack);
mctx->rule = TAILQ_NEXT(mctx->rule, entry);
ARRAY_TRUNC(&mctx->stack, 1);
}
return (error);
}
void
apply_result(struct expritem *ei, int *result, int this)
{
if (ei->inverted)
this = !this;
switch (ei->op) {
case OP_NONE:
*result = this;
break;
case OP_OR:
*result = *result || this;
break;
case OP_AND:
*result = *result && this;
break;
}
}
/* Run next delivery action. */
int
mail_deliver(struct mail_ctx *mctx, struct msg *msg, struct msgbuf *msgbuf)
{
struct account *a = mctx->account;
struct mail *m = mctx->mail;
struct deliver_ctx *dctx;
set_wrapped(m, '\n');
/* If blocked, check for msgs from parent. */
if (mctx->msgid != 0) {
if (msg == NULL || msg->id != mctx->msgid)
return (MAIL_BLOCKED);
mctx->msgid = 0;
/* Got message. Finish delivery. */
dctx = TAILQ_FIRST(&mctx->dqueue);
if (finish_action(dctx, msg, msgbuf) == ACTION_ERROR)
return (MAIL_ERROR);
/* Move on to dequeue this delivery action. */
goto done;
}
/* Check if delivery is complete. */
if (TAILQ_EMPTY(&mctx->dqueue))
return (MAIL_MATCH);
/* Get the first delivery action and start it. */
dctx = TAILQ_FIRST(&mctx->dqueue);
switch (start_action(mctx, dctx)) {
case ACTION_ERROR:
return (MAIL_ERROR);
case ACTION_PARENT:
return (MAIL_BLOCKED);
}
done:
/* Remove completed action from queue. */
TAILQ_REMOVE(&mctx->dqueue, dctx, entry);
log_debug("%s: message %u delivered (rule %u, %s) in %.3f seconds",
a->name, m->idx, dctx->rule->idx,
dctx->actitem->deliver->name, get_time() - dctx->tim);
user_free(dctx->udata);
xfree(dctx);
return (MAIL_CONTINUE);
}
struct replstrs *
find_delivery_users(struct mail_ctx *mctx, struct action *t, int *should_free)
{
struct account *a = mctx->account;
struct rule *r = mctx->rule;
struct replstrs *users;
*should_free = 0;
users = NULL;
if (r->users != NULL) /* rule comes first */
users = r->users;
else if (t != NULL && t->users != NULL) /* then action */
users = t->users;
else if (a->users != NULL) /* then account */
users = a->users;
if (users == NULL) {
*should_free = 1;
users = xmalloc(sizeof *users);
ARRAY_INIT(users);
ARRAY_EXPAND(users, 1);
ARRAY_LAST(users).str = conf.def_user;
}
return (users);
}
int
fill_from_strings(struct mail_ctx *mctx, struct rule *r, struct replstrs *rsa)
{
struct account *a = mctx->account;
u_int i;
struct replstr *rs;
chained--;
if (chained == 0) {
log_warnx("%s: too many chained actions", a->name);
return (-1);
}
for (i = 0; i < ARRAY_LENGTH(rsa); i++) {
rs = &ARRAY_ITEM(rsa, i);
if (fill_from_string(mctx, r, rs) != 0)
return (-1);
}
return (0);
}
int
fill_from_string(struct mail_ctx *mctx, struct rule *r, struct replstr *rs)
{
struct account *a = mctx->account;
struct mail *m = mctx->mail;
struct action *t;
struct actions *ta;
u_int i;
char *s;
struct replstrs *users;
int should_free;
s = replacestr(rs, m->tags, m, &m->rml);
log_debug2("%s: looking for actions matching: %s", a->name, s);
ta = match_actions(s);
if (ARRAY_EMPTY(ta))
goto empty;
xfree(s);
log_debug2("%s: found %u actions", a->name, ARRAY_LENGTH(ta));
for (i = 0; i < ARRAY_LENGTH(ta); i++) {
t = ARRAY_ITEM(ta, i);
users = find_delivery_users(mctx, t, &should_free);
if (fill_from_action(mctx, r, t, users) != 0) {
if (should_free)
ARRAY_FREEALL(users);
ARRAY_FREEALL(ta);
return (-1);
}
if (should_free)
ARRAY_FREEALL(users);
}
ARRAY_FREEALL(ta);
return (0);
empty:
log_warnx("%s: no actions matching: %s (%s)", a->name, s, rs->str);
xfree(s);
ARRAY_FREEALL(ta);
return (-1);
}
int
fill_from_action(struct mail_ctx *mctx, struct rule *r, struct action *t,
struct replstrs *users)
{
struct account *a = mctx->account;
struct mail *m = mctx->mail;
struct deliver_action_data *data;
struct actitem *ti;
struct deliver_ctx *dctx;
u_int i;
char *user;
struct userdata *udata;
for (i = 0; i < ARRAY_LENGTH(users); i++) {
user = replacestr(&ARRAY_ITEM(users, i), m->tags, m, &m->rml);
if (user == NULL || *user == '\0') {
if (user != NULL)
xfree(user);
log_warnx("%s: empty user", a->name);
return (-1);
}
if ((udata = user_lookup(user, conf.user_order)) == NULL) {
log_warnx("%s: bad user: %s", a->name, user);
xfree(user);
return (-1);
}
xfree(user);
TAILQ_FOREACH(ti, t->list, entry) {
if (ti->deliver == NULL) {
data = ti->data;
if (fill_from_strings(
mctx, r, data->actions) != 0) {
user_free(udata);
return (-1);
}
continue;
}
dctx = xcalloc(1, sizeof *dctx);
dctx->action = t;
dctx->actitem = ti;
dctx->account = a;
dctx->rule = r;
dctx->mail = m;
dctx->udata = user_copy(udata);
log_debug3("%s: action %s:%u (%s), user %s", a->name,
t->name, ti->idx, ti->deliver->name,
ARRAY_ITEM(users, i).str);
TAILQ_INSERT_TAIL(&mctx->dqueue, dctx, entry);
}
user_free(udata);
}
return (0);
}
int
start_action(struct mail_ctx *mctx, struct deliver_ctx *dctx)
{
struct account *a = dctx->account;
struct action *t = dctx->action;
struct actitem *ti = dctx->actitem;
struct mail *m = dctx->mail;
struct msg msg;
struct msgbuf msgbuf;
dctx->tim = get_time();
log_debug2("%s: message %u, running action %s:%u (%s) as user %s",
a->name, m->idx, t->name, ti->idx, ti->deliver->name,
dctx->udata->name);
add_tag(&m->tags, "action", "%s", t->name);
add_tag(&m->tags, "rule", "%u", mctx->ruleidx);
update_tags(&m->tags, dctx->udata);
/* Just deliver now for in-child delivery. */
if (ti->deliver->type == DELIVER_INCHILD) {
if (ti->deliver->deliver(dctx, ti) != DELIVER_SUCCESS) {
reset_tags(&m->tags);
return (ACTION_ERROR);
}
reset_tags(&m->tags);
return (ACTION_DONE);
}
memset(&msg, 0, sizeof msg);
msg.type = MSG_ACTION;
msg.id = m->idx;
msg.data.account = a;
msg.data.actitem = ti;
msg.data.uid = dctx->udata->uid;
msg.data.gid = dctx->udata->gid;
msgbuf.buf = m->tags;
msgbuf.len = STRB_SIZE(m->tags);
mail_send(m, &msg);
log_debug3("%s: sending action to parent", a->name);
if (privsep_send(mctx->io, &msg, &msgbuf) != 0)
fatalx("privsep_send error");
mctx->msgid = msg.id;
reset_tags(&m->tags);
return (ACTION_PARENT);
}
int
finish_action(struct deliver_ctx *dctx, struct msg *msg, struct msgbuf *msgbuf)
{
struct account *a = dctx->account;
struct actitem *ti = dctx->actitem;
struct mail *m = dctx->mail;
u_int lines;
if (msgbuf->buf != NULL && msgbuf->len != 0) {
strb_destroy(&m->tags);
m->tags = msgbuf->buf;
reset_tags(&m->tags);
}
if (msg->data.error != 0)
return (ACTION_ERROR);
if (ti->deliver->type != DELIVER_WRBACK)
return (ACTION_DONE);
if (mail_receive(m, msg, 1) != 0) {
log_warn("%s: can't receive mail", a->name);
return (ACTION_ERROR);
}
log_debug2("%s: message %u, received modified mail: size %zu, body %zd",
a->name, m->idx, m->size, m->body);
/* Trim from line. */
trim_from(m);
/* Recreate the wrapped array. */
lines = fill_wrapped(m);
log_debug2("%s: found %u wrapped lines", a->name, lines);
return (ACTION_DONE);
}