comparison dccm/dccm.c @ 0:c7f6b056b673

First import of vendor version
author Peter Gervai <grin@grin.hu>
date Tue, 10 Mar 2009 13:49:58 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c7f6b056b673
1 /* Distributed Checksum Clearinghouse
2 *
3 * sendmail milter interface
4 *
5 * Copyright (c) 2008 by Rhyolite Software, LLC
6 *
7 * This agreement is not applicable to any entity which sells anti-spam
8 * solutions to others or provides an anti-spam solution as part of a
9 * security solution sold to other entities, or to a private network
10 * which employs the DCC or uses data provided by operation of the DCC
11 * but does not provide corresponding data to other users.
12 *
13 * Permission to use, copy, modify, and distribute this software without
14 * changes for any purpose with or without fee is hereby granted, provided
15 * that the above copyright notice and this permission notice appear in all
16 * copies and any distributed versions or copies are either unchanged
17 * or not called anything similar to "DCC" or "Distributed Checksum
18 * Clearinghouse".
19 *
20 * Parties not eligible to receive a license under this agreement can
21 * obtain a commercial license to use DCC by contacting Rhyolite Software
22 * at sales@rhyolite.com.
23 *
24 * A commercial license would be for Distributed Checksum and Reputation
25 * Clearinghouse software. That software includes additional features. This
26 * free license for Distributed ChecksumClearinghouse Software does not in any
27 * way grant permision to use Distributed Checksum and Reputation Clearinghouse
28 * software
29 *
30 * THE SOFTWARE IS PROVIDED "AS IS" AND RHYOLITE SOFTWARE, LLC DISCLAIMS ALL
31 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RHYOLITE SOFTWARE, LLC
33 * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
34 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
35 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
36 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
37 * SOFTWARE.
38 *
39 * Rhyolite Software DCC 1.3.103-1.238 $Revision$
40 */
41
42 #include "libmilter/mfapi.h"
43 #include "cmn_defs.h"
44
45 #undef NEW_MFAPI
46 #ifdef SM_LM_VRS_MAJOR
47 #if SM_LM_VRS_MAJOR(SMFI_VERSION) >= 1
48 #define NEW_MFAPI
49 #endif
50 #endif
51
52 u_char cannot_discard = 0; /* can trim targets after DATA */
53 u_char cannot_reject = 0;
54
55 static u_char background = 1;
56 static DCC_PATH pidpath;
57
58 static const char *progpath = DCC_LIBEXECDIR"/dccm";
59
60 static DCC_PATH conn_def;
61 static char *milter_conn = conn_def; /* MILTER socket specification */
62
63 static char sm_isspam_macro_def[] = "{dcc_isspam}";
64 static char *sm_isspam_macro = sm_isspam_macro_def;
65 static char sm_notspam_macro_def[] = "{dcc_notspam}";
66 static char *sm_notspam_macro = sm_notspam_macro_def;
67
68 /* DCC-milter state or context */
69 typedef struct work {
70 SMFICTX *milter_ctx;
71 # define WORK_MILTER_CTX_IDLE ((SMFICTX *)DCC_SRVR_PORT)
72 CMN_WORK cw;
73 # define NUM_XHDRS 5
74 struct { /* existing X-DCC headers */
75 u_char num;
76 u_char len;
77 char brand[DCC_BRAND_MAXLEN];
78 } xhdrs[NUM_XHDRS];
79 REPLY_TPLT sendmail_reply;
80 /* from here down is zeroed when the structure is allocated */
81 #define WORK_ZERO fwd
82 struct work *fwd;
83 /* from here down is zeroed when the structure is used for a 2nd msg */
84 #define WORK_REZERO num_x_dcc
85 u_char num_x_dcc;
86 } WORK;
87
88 #define WORK_EXCESS ((WORK *)1)
89
90
91 /* use a free list to avoid malloc() overhead */
92 static WORK *work_free;
93 static int work_too_many;
94 static time_t work_msg_time;
95
96 /* each dccm job involves
97 * a socket connected to sendmail,
98 * a log file,
99 * and a socket to talk to the DCC server.
100 * The file descriptors for the whitelists are accounted for in EXTRA_FILES */
101 #define FILES_PER_JOB 3
102 int max_max_work = MAX_SELECT_WORK;
103
104
105 static sfsistat dccm_conn(SMFICTX *, char *, _SOCK_ADDR *);
106 static sfsistat dccm_helo(SMFICTX *, char *);
107 static sfsistat dccm_envfrom(SMFICTX *, char **);
108 static sfsistat dccm_envrcpt(SMFICTX *, char **);
109 static sfsistat dccm_header(SMFICTX *, char *, char *);
110 static sfsistat dccm_eoh(SMFICTX *);
111 static sfsistat dccm_body(SMFICTX *, u_char *, size_t);
112 static sfsistat dccm_eom(SMFICTX *);
113 static sfsistat dccm_abort(SMFICTX *);
114 static sfsistat dccm_close(SMFICTX *);
115 #ifdef NEW_MFAPI
116 static sfsistat dccm_negotiate(SMFICTX *, unsigned long, unsigned long,
117 unsigned long, unsigned long,
118 unsigned long *, unsigned long *,
119 unsigned long *, unsigned long *);
120 #endif
121
122 static char dccm_name[] = {"DCC"};
123 static struct smfiDesc smfilter = {
124 dccm_name, /* filter name */
125 SMFI_VERSION, /* version code -- do not change */
126 SMFIF_CHGHDRS | SMFIF_ADDHDRS | SMFIF_DELRCPT, /* flags */
127 dccm_conn, /* connection info filter */
128 dccm_helo, /* SMTP HELO command filter */
129 dccm_envfrom, /* envelope sender filter */
130 dccm_envrcpt, /* envelope recipient filter */
131 dccm_header, /* header filter */
132 dccm_eoh, /* end of header */
133 dccm_body, /* body block filter */
134 dccm_eom, /* end of message */
135 dccm_abort, /* message aborted */
136 dccm_close, /* connection finished */
137 #ifdef NEW_MFAPI
138 0, /* unknown SMTP command */
139 0, /* xxfi_data */
140 dccm_negotiate, /* negotiate new milter options */
141 #endif
142 };
143
144
145 static REPLY_TPLT too_many_reply = {
146 DCC_XHDR_TOO_MANY_RCPTS, {REPLY_TPLT_NULL},
147 "452", "4.5.3", 0, DCC_XHDR_TOO_MANY_RCPTS};
148
149 static REPLY_TPLT incompat_white_reply = {
150 DCC_XHDR_INCOMPAT_WLIST, {REPLY_TPLT_NULL},
151 "452", "4.5.3", 0, DCC_XHDR_INCOMPAT_WLIST};
152
153
154 static char *add_braces(const char *);
155 static void del_sock(void);
156 static void add_work(int);
157
158
159 static void
160 usage(const char* barg, const char *bvar)
161 {
162 const char str[] = {
163 "usage: [-VdbxANQ] [-G on | off | noIP | IPmask/xx] [-h homedir]"
164 " [-I user]\n"
165 " [-p protocol:filename | protocol:port@host] [-m map]\n"
166 " [-w whiteclnt] [-U userdirs] [-a IGNORE | REJECT | DISCARD]\n"
167 " [-t type,[log-thold,][spam-thold]]"
168 " [-g [not-]type] [-S header]\n"
169 " [-l logdir] [-R rundir] [-r rejection-msg] [-j maxjobs]\n"
170 " [-B dnsbl-option] [-L ltype,facility.level]"
171 };
172 static u_char complained;
173
174 if (!complained) {
175 if (barg)
176 dcc_error_msg("unrecognized \"%s%s\"\n%s\n..."
177 " continuing",
178 barg, bvar, str);
179 else
180 dcc_error_msg("%s\n... continuing", str);
181 complained = 1;
182 }
183 }
184
185
186 int NRATTRIB
187 main(int argc, char **argv)
188 {
189 DCC_EMSG emsg;
190 #ifdef RLIMIT_NOFILE
191 struct rlimit nofile;
192 int old_rlim_cur;
193 #endif
194 long l;
195 u_char log_tgts_set = 0;
196 time_t smfi_main_start;
197 char *p;
198 const char *rundir = DCC_RUNDIR;
199 const char *homedir = 0;
200 const char *logdir = 0;
201 int result, i;
202
203 emsg[0] = '\0';
204 if (*argv[0] == '/')
205 progpath = argv[0];
206 dcc_syslog_init(1, argv[0], 0);
207 dcc_clear_tholds();
208
209 #ifdef RLIMIT_NOFILE
210 if (0 > getrlimit(RLIMIT_NOFILE, &nofile)) {
211 dcc_error_msg("getrlimit(RLIMIT_NOFILE): %s", ERROR_STR());
212 old_rlim_cur = 1000*1000;
213 } else {
214 old_rlim_cur = nofile.rlim_cur;
215 if (nofile.rlim_max < 1000*1000) {
216 i = nofile.rlim_max;
217 #ifndef USE_POLL
218 if (i > FD_SETSIZE)
219 i = FD_SETSIZE;
220 #endif
221 max_max_work = (i - EXTRA_FILES)/FILES_PER_JOB;
222 max_max_work_src = "RLIMIT_NOFILE limit";
223 }
224 }
225 #endif /* RLIMIT_NOFILE */
226 if (max_max_work <= 0) {
227 dcc_error_msg("too few open files allowed");
228 max_max_work = MIN_MAX_WORK;
229 }
230 max_work = max_max_work;
231
232 #define SLARGS "VdbxANQW" /* change start-dccm if these change */
233 while (EOF != (i = getopt(argc, argv, SLARGS"G:h:I:"
234 "p:m:w:U:a:t:g:S:l:R:r:s:o:j:B:L:"))) {
235 switch (i) {
236 case 'V':
237 fprintf(stderr, DCC_VERSION"\n");
238 exit(EX_OK);
239 break;
240
241 case 'd':
242 ++dcc_clnt_debug;
243 break;
244
245 case 'b':
246 background = 0;
247 break;
248
249 case 'x':
250 try_extra_hard = DCC_CLNT_FG_NO_FAIL;
251 break;
252
253 case 'A':
254 chghdr = ADDHDR;
255 smfilter.xxfi_flags &= ~SMFIF_CHGHDRS;
256 smfilter.xxfi_flags |= SMFIF_ADDHDRS;
257 break;
258
259 case 'N':
260 chghdr = NOHDR;
261 smfilter.xxfi_flags &= ~(SMFIF_ADDHDRS | SMFIF_CHGHDRS);
262 break;
263
264 case 'Q':
265 dcc_query_only = 1;
266 break;
267
268 case 'W': /* obsolete DCC off by default */
269 to_white_only = 1;
270 break;
271
272 case 'G':
273 if (!dcc_parse_client_grey(optarg))
274 usage("-G", optarg);
275 break;
276
277 case 'h':
278 homedir = optarg;
279 break;
280
281 case 'I':
282 dcc_daemon_su(optarg);
283 break;
284
285 case 'p':
286 milter_conn = optarg;
287 break;
288
289 case 'm':
290 mapfile_nm = optarg;
291 break;
292
293 case 'w':
294 main_white_nm = optarg;
295 break;
296
297 case 'U':
298 parse_userdirs(optarg);
299 break;
300
301 case 'a':
302 if (!strcasecmp(optarg, "IGNORE")) {
303 action = CMN_IGNORE;
304 } else if (!strcasecmp(optarg, "REJECT")) {
305 action = CMN_REJECT;
306 } else if (!strcasecmp(optarg, "DISCARD")) {
307 action = CMN_DISCARD;
308 } else {
309 dcc_error_msg("unrecognized -a action: %s",
310 optarg);
311 }
312 break;
313
314 case 't':
315 if (dcc_parse_tholds("-t ", optarg))
316 log_tgts_set = 1;
317 break;
318
319 case 'g': /* honor not-spam "counts" */
320 dcc_parse_honor(optarg);
321 break;
322
323 case 'S':
324 dcc_add_sub_hdr(0, optarg);
325 break;
326
327 case 'l': /* log rejected mail here */
328 logdir = optarg;
329 break;
330
331 case 'R':
332 rundir = optarg;
333 break;
334
335 case 'r':
336 parse_reply_arg(optarg);
337 break;
338
339 case 's': /* deprecated: set dcc_isspam */
340 sm_isspam_macro = add_braces(optarg);
341 break;
342
343 case 'o': /* deprecated: set dcc_notspam */
344 sm_notspam_macro = add_braces(optarg);
345 break;
346
347 case 'j': /* maximum simultaneous jobs */
348 l = strtoul(optarg, &p, 10);
349 if (*p != '\0' || l < MIN_MAX_WORK) {
350 dcc_error_msg("invalid queue length %s",
351 optarg);
352 } else if (l > max_max_work) {
353 dcc_error_msg("-j queue length %s"
354 " larger than %s; using %d",
355 optarg,
356 max_max_work_src, max_max_work);
357 max_work = max_max_work;
358 } else {
359 max_work = l;
360 }
361 break;
362
363 case 'B':
364 if (!dcc_parse_dnsbl(emsg, optarg, progpath, 0))
365 dcc_error_msg("%s", emsg);
366 break;
367
368 case 'L':
369 if (dcc_parse_log_opt(optarg))
370 helper_save_arg("-L", optarg);
371 break;
372
373 default:
374 usage(optopt2str(optopt), "");
375 }
376 }
377 if (argc != optind)
378 usage(argv[optind], "");
379
380 snprintf(conn_def, sizeof(conn_def), "%s/%s", rundir, dcc_progname);
381
382 dcc_cdhome(emsg, homedir, 0);
383 dcc_main_logdir_init(0, logdir);
384 if (dcc_main_logdir[0] == '\0') {
385 /* if not logging,
386 * tell sendmail to not bother with some stuff */
387 smfilter.xxfi_helo = 0;
388
389 if (log_tgts_set)
390 dcc_error_msg("log thresholds set with -t"
391 " but no -l directory");
392 if (userdirs != '\0')
393 dcc_error_msg("no -l directory prevents per-user"
394 " logging with -U");
395 }
396
397
398 #ifdef RLIMIT_NOFILE
399 i = max_work*FILES_PER_JOB+EXTRA_FILES;
400 if (old_rlim_cur < i) {
401 nofile.rlim_cur = i;
402 if (0 > setrlimit(RLIMIT_NOFILE, &nofile)) {
403 dcc_error_msg("setrlimit(RLIMIT_NOFILE,%d): %s",
404 i, ERROR_STR());
405 max_work = old_rlim_cur/FILES_PER_JOB - EXTRA_FILES;
406 if (max_work <= 0) {
407 dcc_error_msg("only %d open files allowed"
408 " by RLIMIT_NOFILE",
409 old_rlim_cur);
410 max_work = MIN_MAX_WORK;
411 }
412 }
413 }
414 #endif /* RLIMIT_NOFILE */
415
416 helper_init(max_work);
417
418 if (MI_SUCCESS != smfi_setconn(milter_conn))
419 dcc_logbad(EX_USAGE, "illegal sendmail connection"
420 " \"%s\"\n", optarg);
421
422 del_sock();
423
424 if (smfi_register(smfilter) == MI_FAILURE)
425 dcc_logbad(EX_UNAVAILABLE, "smfi_register failed\n");
426
427 if (background) {
428 if (daemon(1, 0) < 0)
429 dcc_logbad(EX_OSERR, "daemon(): %s", ERROR_STR());
430
431 dcc_daemon_restart(rundir, del_sock);
432 dcc_pidfile(pidpath, rundir);
433 }
434 /* Be careful to start all threads only after the fork() in daemon(),
435 * because some POSIX threads packages (e.g. FreeBSD) get confused
436 * about threads in the parent. */
437
438 cmn_init();
439 add_work(init_work);
440
441 dcc_trace_msg(DCC_VERSION" listening to %s with %s",
442 milter_conn, dcc_homedir);
443 if (dcc_clnt_debug)
444 dcc_trace_msg("init_work=%d max_work=%d max_max_work=%d (%s)",
445 total_work, max_work, max_max_work,
446 max_max_work_src);
447
448 /* It would be nice to remove the UNIX domain socket and PID file
449 * when smfi_main() returns, but we dare not because the library
450 * delays for several seconds after being signalled to stop.
451 * Our files might have been unlinked and the files now in
452 * the filesystem might belong to some other process. */
453 smfi_main_start = time(0);
454 result = smfi_main();
455
456 if (pidpath[0] != '\0')
457 unlink(pidpath);
458
459 totals_stop();
460
461 /* The sendmail libmilter machinery sometimes gets confused and
462 * gives up. Try to start over if we had been running for at least
463 * 10 minutes */
464 if (result != MI_SUCCESS
465 && time(0) > smfi_main_start+10*60) {
466 dcc_error_msg("try to restart after smfi_main() = %d", result);
467 exit(EX_DCC_RESTART);
468 }
469
470 if (result != MI_SUCCESS)
471 dcc_error_msg("smfi_main() = %d", result);
472 exit((result == MI_SUCCESS) ? EX_OK : EX_UNAVAILABLE);
473 }
474
475
476
477 static char *
478 add_braces(const char *s)
479 {
480 int i;
481 char *new;
482
483 i = strlen(s);
484 if (i >= 2 && s[0] == '{' && s[i-1] == '}')
485 return strdup(s);
486 new = dcc_malloc(i+3);
487 new[0] = '{';
488 memcpy(new+1, s, i);
489 new[i+1] = '}';
490 new[i+2] = '\0';
491 return new;
492 }
493
494
495
496 /* remove the Unix domain socket of a previous instance of this daemon */
497 static void
498 del_sock(void)
499 {
500 int s;
501 struct stat sb;
502 const char *conn;
503 struct sockaddr_un conn_sun;
504 int len, i;
505
506 /* Ignore the sendmail milter "local|whatever:" prefix.
507 * If it is a UNIX domain socket, fine. If not, no harm is done */
508 conn = strchr(milter_conn, ':');
509 if (conn)
510 ++conn;
511 else
512 conn = milter_conn;
513
514 len = strlen(conn);
515 if (len >= ISZ(conn_sun.sun_path))
516 return; /* perhaps not a UNIX domain socket */
517
518 memset(&conn_sun, 0, sizeof(conn_sun));
519 conn_sun.sun_family = AF_LOCAL;
520 strcpy(conn_sun.sun_path, conn);
521 #ifdef HAVE_SA_LEN
522 conn_sun.sun_len = SUN_LEN(&conn_sun);
523 #endif
524
525 if (0 > stat(conn_sun.sun_path, &sb))
526 return;
527 if (!(S_ISSOCK(sb.st_mode) || S_ISFIFO(sb.st_mode)))
528 dcc_logbad(EX_UNAVAILABLE, "non-socket present at %s",
529 conn_sun.sun_path);
530
531 /* The sendmail libmilter seems to delay as long as 5 seconds
532 * before stopping. It delays indefinitely if an SMTP client
533 * is stuck. */
534 i = 0;
535 for (;;) {
536 s = socket(AF_UNIX, SOCK_STREAM, 0);
537 if (s < 0) {
538 dcc_logbad(EX_OSERR, "socket(AF_UNIX): %s",
539 ERROR_STR());
540 return;
541 }
542 if (++i > 5*10)
543 dcc_logbad(EX_UNAVAILABLE,
544 "DCCM or something already or still running"
545 " with socket at %s",
546 conn_sun.sun_path);
547 if (0 > connect(s, (struct sockaddr *)&conn_sun,
548 sizeof(conn_sun))) {
549 /* unlink it only if it looks like a dead socket */
550 if (errno == ECONNREFUSED || errno == ECONNRESET
551 || errno == EACCES) {
552 if (0 > unlink(conn_sun.sun_path))
553 dcc_error_msg("unlink(old %s): %s",
554 conn_sun.sun_path,
555 ERROR_STR());
556 } else {
557 dcc_error_msg("connect(old %s): %s",
558 conn_sun.sun_path, ERROR_STR());
559 }
560 close(s);
561 break;
562 }
563 close(s);
564 usleep(100*1000);
565 }
566 }
567
568
569
570 /* create some contexts. */
571 static void
572 add_work(int i)
573 {
574 WORK *wp;
575
576 total_work += i;
577
578 wp = dcc_malloc(sizeof(*wp)*i);
579 memset(wp, 0, sizeof(*wp)*i);
580
581 while (i-- != 0) {
582 wp->milter_ctx = WORK_MILTER_CTX_IDLE;
583 cmn_create(&wp->cw);
584 wp->fwd = work_free;
585 work_free = wp;
586 ++wp;
587 }
588 }
589
590
591
592 static WORK *
593 work_alloc(void)
594 {
595 WORK *wp;
596
597 lock_work();
598 wp = work_free;
599 if (!wp) {
600 if (total_work > max_work) {
601 ++work_too_many;
602 unlock_work();
603 return 0;
604 }
605 if (dcc_clnt_debug > 1)
606 dcc_trace_msg("add %d work blocks to %d",
607 init_work, total_work);
608 add_work(init_work);
609 wp = work_free;
610 }
611 if (wp->milter_ctx != WORK_MILTER_CTX_IDLE)
612 dcc_logbad(EX_SOFTWARE, "corrupt WORK area");
613 work_free = wp->fwd;
614 unlock_work();
615
616 /* clear most of it */
617 cmn_clear(&wp->cw, wp, 1);
618 wp->cw.helo[0] = '\0';
619 memset(&wp->WORK_ZERO, 0,
620 sizeof(*wp) - ((char*)&wp->WORK_ZERO - (char*)wp));
621
622 return wp;
623 }
624
625
626
627 /* ocassionally close sockets to recover from dictionary attacks */
628 void
629 work_clean(void)
630 {
631 WORK *wp;
632 int keep, delete;
633
634 lock_work();
635 keep = 5;
636 delete = init_work;
637 for (wp = work_free; wp; wp = wp->fwd) {
638 if (!wp->cw.dcc_ctxt)
639 break;
640 if (--keep > 0)
641 continue;
642 dcc_clnt_soc_close(wp->cw.dcc_ctxt);
643 if (--delete <= 0)
644 break;
645 }
646 unlock_work();
647 }
648
649
650
651 typedef enum {GET_WP_START, /* not yet seen dccm_envfrom() */
652 GET_WP_GOING, /* have seen dccm_envfrom() */
653 GET_WP_ABORT, /* dccm_abort() */
654 GET_WP_CLOSE /* dccm_close() */
655 } GET_WP_MODE;
656 static WORK *
657 get_wp(SMFICTX *milter_ctx,
658 GET_WP_MODE mode)
659 {
660 WORK *wp;
661
662 wp = (WORK *)smfi_getpriv(milter_ctx);
663 if (!wp) {
664 /* milter context is not active */
665 if (mode == GET_WP_CLOSE || mode == GET_WP_ABORT)
666 return 0;
667 dcc_logbad(EX_SOFTWARE, "null SMFICTX pointer");
668 } else if (wp == WORK_EXCESS) {
669 if (mode == GET_WP_START || mode == GET_WP_GOING)
670 dcc_logbad(EX_SOFTWARE, "tardy WORK_EXCESS");
671 if (dcc_clnt_debug)
672 dcc_trace_msg("%s for excessive message",
673 mode == GET_WP_ABORT
674 ? "abort" : "close");
675 return 0;
676 }
677 if (wp->milter_ctx != milter_ctx)
678 dcc_logbad(EX_SOFTWARE,
679 "bogus SMFICTX pointer or corrupt WORK area");
680
681 if (!wp->cw.dcc_ctxt && (mode == GET_WP_START || mode == GET_WP_GOING))
682 dcc_logbad(EX_SOFTWARE, "tardy failure to find ctxt");
683
684 if (wp->cw.env_from[0] == '\0' && mode == GET_WP_GOING)
685 dcc_logbad(EX_SOFTWARE, "work cleared?");
686
687 return wp;
688 }
689
690
691
692 static void
693 set_sendmail_reply(WORK *wp,
694 const char *rcode, const char *xcode, const char *str)
695 {
696 int i;
697
698 /* kludge to fix lack of const declaration */
699 typedef int (*SR)(SMFICTX *, const char *, const char *, const char *);
700 static SR sr = (SR)smfi_setreply;
701 i = (*sr)(wp->milter_ctx, rcode, xcode, str);
702
703 if (i != MI_SUCCESS)
704 thr_error_msg(&wp->cw, "smfi_setreply(\"%s\",\"%s\",\"%s\")=%d",
705 rcode, xcode, str, i);
706 }
707
708
709
710 /* refuse one recipient */
711 static sfsistat
712 rcpt_tempfail(WORK *wp, RCPT_ST *rcpt_st, const REPLY_TPLT *tplt)
713 {
714 REPLY_STRS strs;
715
716 make_reply(&strs, tplt, &wp->cw, 0);
717 set_sendmail_reply(wp, strs.rcode, strs.xcode, strs.str);
718 wp->cw.ask_st |= ASK_ST_LOGIT;
719 if (rcpt_st) {
720 snprintf(rcpt_st->rej_msg, sizeof(rcpt_st->rej_msg),
721 "%s %s %s", strs.rcode, strs.xcode, strs.str);
722 rcpt_st->rej_result = strs.log_result;
723 rcpt_st->fgs |= RCPT_FG_REJ_FILTER;
724 }
725 return SMFIS_TEMPFAIL;
726 }
727
728
729
730 static void
731 msg_clear(WORK *wp)
732 {
733 cmn_clear(&wp->cw, wp, 0);
734 memset(&wp->WORK_REZERO, 0,
735 sizeof(*wp) - ((char*)&wp->WORK_REZERO - (char*)wp));
736 }
737
738
739
740 /* we are finished with one SMTP message.
741 * get ready for the next from the same connection to an SMTP client */
742 static void
743 msg_done(WORK *wp, const char *result)
744 {
745 LOG_CAPTION(wp, DCC_XHDR_RESULT);
746 log_write(&wp->cw, result ? result : DCC_XHDR_RESULT_ACCEPT, 0);
747 LOG_EOL(wp);
748
749 msg_clear(wp);
750 }
751
752
753
754 /* give up on entire message */
755 static sfsistat
756 msg_tempfail(WORK *wp, const REPLY_TPLT *tplt)
757 {
758 make_reply(&wp->cw.reply, tplt, &wp->cw, 0);
759 set_sendmail_reply(wp, wp->cw.reply.rcode, wp->cw.reply.xcode,
760 wp->cw.reply.str);
761 log_smtp_reply(&wp->cw);
762 wp->cw.ask_st |= ASK_ST_LOGIT;
763 msg_done(wp, wp->cw.reply.log_result);
764 return SMFIS_TEMPFAIL;
765 }
766
767
768
769 static sfsistat
770 msg_reject(WORK *wp)
771 {
772 sfsistat result;
773
774 /* temporize if we have not figured out what to say */
775 if (!wp->cw.reply.log_result) {
776 thr_error_msg(&wp->cw, "rejection reason undecided");
777 make_reply(&wp->cw.reply, &dcc_fail_reply, &wp->cw, 0);
778 }
779
780 set_sendmail_reply(wp, wp->cw.reply.rcode, wp->cw.reply.xcode,
781 wp->cw.reply.str);
782 log_smtp_reply(&wp->cw);
783
784 result = (wp->cw.reply.rcode[0] == '4') ? SMFIS_TEMPFAIL : SMFIS_REJECT;
785 msg_done(wp, wp->cw.reply.log_result);
786 return result;
787 }
788
789
790
791 /* see what sendmail had to say about the message */
792 static void
793 ask_sm(SMFICTX *milter_ctx, WORK *wp)
794 {
795 const char *m;
796
797 /* Do this only until we get an answer.
798 * The sendmail macro might not be set on the first rcpt_to command.
799 * If the is-spam macro is set before the not-spam macro, then this
800 * will get the wrong answer. However, undoing the effects of an
801 * is-spam setting would be a mess, because they included turning
802 * off DNSBL checks. */
803 if ((wp->cw.ask_st & (ASK_ST_MTA_NOTSPAM | ASK_ST_MTA_ISSPAM)) != 0)
804 return;
805
806 if (0 != (m = smfi_getsymval(milter_ctx, sm_notspam_macro))
807 && *m != '\0') {
808 /* We have a sendmail macro name that indicates a
809 * whitelisting from sendmail rules and databases,
810 * and the macro is set. */
811 wp->cw.ask_st |= ASK_ST_MTA_NOTSPAM;
812 wp->cw.ask_st &= ~ASK_ST_MTA_ISSPAM;
813 thr_log_print(&wp->cw, 1,
814 "sendmail.cf"DCC_XHDR_ISOK": \"%s\"\n", m);
815
816 } else if (!(wp->cw.ask_st & ASK_ST_MTA_ISSPAM)
817 && 0 != (m = smfi_getsymval(milter_ctx, sm_isspam_macro))
818 && *m != '\0') {
819 wp->cw.ask_st |= ASK_ST_MTA_ISSPAM;
820
821 make_tplt(&wp->sendmail_reply, 0, DCC_XCODE, DCC_RCODE, m,
822 DCC_XHDR_RESULT_REJECT);
823
824 thr_log_print(&wp->cw, 1, "sendmail.cf-->%s: \"%s\"\n",
825 sm_isspam_macro, wp->sendmail_reply.pat);
826
827 make_reply(&wp->cw.reply, &wp->sendmail_reply, &wp->cw, 0);
828
829 if (!CLITCMP(wp->cw.reply.str, "DISCARD")) {
830 wp->cw.reply.str += LITZ("DISCARD");
831 wp->cw.reply.str += strspn(wp->cw.reply.str,
832 DCC_WHITESPACE":");
833 wp->cw.action = CMN_DISCARD;
834 } else {
835 wp->cw.action = CMN_REJECT;
836 }
837 }
838 }
839
840
841
842 void
843 user_reject_discard(CMN_WORK *cwp, RCPT_ST *rcpt_st)
844 {
845 int i;
846
847 /* one of the other targets wants this message,
848 * try to remove this address from sendmail's list */
849 i = smfi_delrcpt(cwp->wp->milter_ctx, rcpt_st->env_to);
850 if (MI_SUCCESS != i)
851 thr_error_msg(cwp, "delrcpt(%s)=%d", rcpt_st->env_to, i);
852 }
853
854
855
856 #ifdef NEW_MFAPI
857 /* ask sendmail to tell us about rejected recipients */
858 static sfsistat
859 dccm_negotiate(SMFICTX *milter_ctx UATTRIB,
860 unsigned long f0, unsigned long f1,
861 unsigned long f2 UATTRIB, unsigned long f3 UATTRIB,
862 unsigned long *pf0, unsigned long *pf1 UATTRIB,
863 unsigned long *pf2 UATTRIB, unsigned long *pf3 UATTRIB)
864 {
865 *pf0 = f0;
866 *pf1 = SMFIP_RCPT_REJ & f1;
867
868 return SMFIS_CONTINUE;
869 }
870 #endif /* NEW_MFAPI */
871
872
873
874 /* start a new connection to an SMTP client */
875 static sfsistat
876 dccm_conn(SMFICTX *milter_ctx,
877 char *name, /* SMTP client hostname */
878 _SOCK_ADDR *sender)
879 {
880 WORK *wp;
881
882 wp = (WORK *)smfi_getpriv(milter_ctx);
883 if (wp) {
884 dcc_error_msg("bogus initial SMFICTX pointer");
885 smfi_setpriv(milter_ctx, 0);
886 return SMFIS_TEMPFAIL;
887 }
888 wp = work_alloc();
889 if (!wp) {
890 smfi_setpriv(milter_ctx, WORK_EXCESS);
891 return SMFIS_TEMPFAIL;
892 }
893 smfi_setpriv(milter_ctx, wp);
894 wp->milter_ctx = milter_ctx;
895
896 log_start(&wp->cw);
897
898 if (!name) {
899 if (dcc_clnt_debug)
900 thr_trace_msg(&wp->cw, "null sender name");
901 strcpy(wp->cw.clnt_name, "(null name)");
902 } else {
903 BUFCPY(wp->cw.clnt_name, name);
904 }
905
906 if (!sender) {
907 if (!strcasecmp(wp->cw.clnt_name, "localhost")) {
908 wp->cw.clnt_addr.s6_addr32[3] = htonl(0x7f000001);
909 wp->cw.clnt_addr.s6_addr32[0] = 0;
910 wp->cw.clnt_addr.s6_addr32[1] = 0;
911 wp->cw.clnt_addr.s6_addr32[2] = htonl(0xffff);
912 strcpy(wp->cw.clnt_str, "127.0.0.1");
913 } else {
914 if (dcc_clnt_debug)
915 thr_trace_msg(&wp->cw,
916 "null sender address for \"%s\"",
917 wp->cw.clnt_name);
918 wp->cw.clnt_str[0] = '\0';
919 }
920 } else if (sender->sa_family != AF_INET
921 && sender->sa_family != AF_INET6) {
922 dcc_error_msg("unexpected sender address family %d",
923 sender->sa_family);
924 wp->cw.clnt_str[0] = '\0';
925 } else {
926 if (sender->sa_family == AF_INET) {
927 dcc_ipv4toipv6(&wp->cw.clnt_addr,
928 ((struct sockaddr_in*)sender)->sin_addr);
929 dcc_ipv6tostr(wp->cw.clnt_str, sizeof(wp->cw.clnt_str),
930 &wp->cw.clnt_addr);
931 } else if (sender->sa_family == AF_INET6) {
932 memcpy(&wp->cw.clnt_addr,
933 &((struct sockaddr_in6 *)sender)->sin6_addr,
934 sizeof(wp->cw.clnt_addr));
935 dcc_ipv6tostr(wp->cw.clnt_str, sizeof(wp->cw.clnt_str),
936 &wp->cw.clnt_addr);
937 } else {
938 dcc_error_msg("unknown address family for \"%s\"",
939 wp->cw.clnt_name);
940 wp->cw.clnt_str[0] = '\0';
941 }
942 }
943
944 /* quit now if we cannot find a free client context */
945 if (!ck_dcc_ctxt(&wp->cw))
946 return msg_tempfail(wp, &dcc_fail_reply);
947
948 /* This much is common for all of the messages that might
949 * arrive through this connection to the SMTP client */
950
951 return SMFIS_CONTINUE;
952 }
953
954
955
956 /* log HELO */
957 static sfsistat
958 dccm_helo(SMFICTX *milter_ctx, char *helo)
959 {
960 WORK *wp;
961 int i;
962
963 wp = get_wp(milter_ctx, GET_WP_START);
964
965 i = strlen(helo);
966 if (i < ISZ(wp->cw.helo)) {
967 memcpy(wp->cw.helo, helo, i+1);
968 } else {
969 memcpy(wp->cw.helo, helo, ISZ(wp->cw.helo)-ISZ(DCC_HELO_CONT));
970 strcpy(&wp->cw.helo[ISZ(wp->cw.helo)-ISZ(DCC_HELO_CONT)],
971 DCC_HELO_CONT);
972 }
973
974 return SMFIS_CONTINUE;
975 }
976
977
978
979 /* deal with Mail From envelope value */
980 static sfsistat
981 dccm_envfrom(SMFICTX *milter_ctx, char **from)
982 {
983 static char dollar_i[] = "i";
984 static char mail_host_macro[] = "{mail_host}";
985 static char dcc_mail_host_macro[] = "{dcc_mail_host}";
986 const char *id, *mail_host;
987 WORK *wp;
988
989 wp = get_wp(milter_ctx, GET_WP_START);
990
991 log_start(&wp->cw);
992
993 dcc_cks_init(&wp->cw.cks);
994 dcc_dnsbl_init(&wp->cw.cks, wp->cw.dcc_ctxt, &wp->cw, wp->cw.id);
995
996 /* Assume for now (and again if this is not the first transaction
997 * for this SMTP session) that the sender is the current SMTP client
998 * whiteclnt. Received: headers might have the real sender */
999 strcpy(wp->cw.sender_name, wp->cw.clnt_name);
1000 strcpy(wp->cw.sender_str, wp->cw.clnt_str);
1001
1002 /* see if the SMTP client is one of our MX forwarders */
1003 if (wp->cw.sender_str[0] != '\0') {
1004 /* we need the IP checksum in the usual place to look in
1005 * the whitelist for it */
1006 dcc_get_ipv6_ck(&wp->cw.cks, &wp->cw.clnt_addr);
1007 check_mx_listing(&wp->cw);
1008 }
1009
1010 /* replace the message ID generated when the log file was started
1011 * with the sendmail message ID */
1012 id = smfi_getsymval(milter_ctx, dollar_i);
1013 if (id)
1014 BUFCPY(wp->cw.id, id);
1015
1016 BUFCPY(wp->cw.env_from, from[0]);
1017
1018 /* Even if sendmail.cf sets the ${dcc_mail_host} macro,
1019 * FEATURE(delay_checks) can delay its setting until after
1020 * the MAIL command has been processed and this milter function
1021 * has been called. */
1022 mail_host = smfi_getsymval(milter_ctx, dcc_mail_host_macro);
1023 if (!mail_host || !*mail_host)
1024 mail_host = smfi_getsymval(milter_ctx, mail_host_macro);
1025 if (mail_host)
1026 BUFCPY(wp->cw.mail_host, mail_host);
1027
1028 return SMFIS_CONTINUE;
1029 }
1030
1031
1032
1033 /* note another recipient */
1034 static sfsistat
1035 dccm_envrcpt(SMFICTX *milter_ctx, char **rcpt)
1036 {
1037 static char rcpt_mailer[] = "{rcpt_mailer}";
1038 static char rcpt_addr[] = "{rcpt_addr}";
1039 static char dcc_userdir[] = "{dcc_userdir}";
1040 const char *mailer, *addr, *dir;
1041 WORK *wp;
1042 RCPT_ST *rcpt_st;
1043
1044 wp = get_wp(milter_ctx, GET_WP_GOING);
1045
1046 rcpt_st = alloc_rcpt_st(&wp->cw, 1);
1047 if (!rcpt_st)
1048 return rcpt_tempfail(wp, 0, &too_many_reply);
1049
1050 BUFCPY(rcpt_st->env_to, rcpt[0]);
1051
1052 addr = smfi_getsymval(milter_ctx, rcpt_addr);
1053 mailer = smfi_getsymval(milter_ctx, rcpt_mailer);
1054
1055 #ifdef NEW_MFAPI
1056 /* count rejected recipient as if the message would have been
1057 * delivered to it */
1058 if (mailer && !strcmp(mailer, "error")) {
1059 rcpt_st->fgs |= RCPT_FG_BAD_USERNAME;
1060 if (!addr || addr[0] != '4')
1061 ++wp->cw.mta_rej_tgts;
1062 return SMFIS_CONTINUE;
1063 }
1064 #endif
1065
1066 if (addr)
1067 BUFCPY(rcpt_st->user, addr);
1068
1069 /* pick a per-user whitelist and log directory */
1070 dir = smfi_getsymval(milter_ctx, dcc_userdir);
1071 if (dir) {
1072 if (!get_user_dir(rcpt_st, dir, strlen(dir), 0, 0))
1073 thr_trace_msg(&wp->cw, "%s", wp->cw.emsg);
1074 } else if (mailer && addr) {
1075 if (!get_user_dir(rcpt_st, mailer, strlen(mailer),
1076 addr, strlen(addr)))
1077 thr_trace_msg(&wp->cw, "%s", wp->cw.emsg);
1078 }
1079
1080 /* sendmail might need to force discarding */
1081 ask_sm(milter_ctx, wp);
1082 if (!cmn_compat_whitelist(&wp->cw, rcpt_st))
1083 return rcpt_tempfail(wp, rcpt_st, &incompat_white_reply);
1084
1085 ++wp->cw.tgts;
1086
1087 return SMFIS_CONTINUE;
1088 }
1089
1090
1091
1092 static sfsistat
1093 dccm_header(SMFICTX *milter_ctx, char *headerf, char *headerv)
1094 {
1095 WORK *wp;
1096 int f_len, v_len;
1097 const char *cp;
1098 int i, j;
1099
1100 wp = get_wp(milter_ctx, GET_WP_GOING);
1101
1102 if (!(wp->cw.cmn_fgs & CMN_FG_ENV_LOGGED))
1103 thr_log_envelope(&wp->cw, 1);
1104
1105 f_len = strlen(headerf);
1106 v_len = strlen(headerv);
1107 if (wp->cw.log_fd >= 0) {
1108 log_body_write(&wp->cw, headerf, f_len);
1109 log_body_write(&wp->cw, ": ", LITZ(": "));
1110 log_body_write(&wp->cw, headerv, v_len);
1111 log_body_write(&wp->cw, "\n", 1);
1112 }
1113
1114 /* compute DCC checksums for favored headers */
1115 if (!strcasecmp(headerf, DCC_XHDR_TYPE_FROM)) {
1116 dcc_get_cks(&wp->cw.cks, DCC_CK_FROM, headerv, 1);
1117 return SMFIS_CONTINUE;
1118 }
1119 if (!strcasecmp(headerf, DCC_XHDR_TYPE_MESSAGE_ID)) {
1120 dcc_get_cks(&wp->cw.cks, DCC_CK_MESSAGE_ID, headerv, 1);
1121 return SMFIS_CONTINUE;
1122 }
1123 if (!strcasecmp(headerf, DCC_XHDR_TYPE_RECEIVED)) {
1124 dcc_get_cks(&wp->cw.cks, DCC_CK_RECEIVED, headerv, 1);
1125
1126 /* parse Received: headers if we do not have a
1127 * non-MX-whitelisted sender IP address
1128 * and sendmail gave us a valid address so that
1129 * there is a slot in the log file for an address.
1130 * Parsing a Received header offered by a spammer is
1131 * prevented by only parsing those added by MX-whitelisted
1132 * IP ddresses */
1133 if (wp->cw.cks.sums[DCC_CK_IP].type == DCC_CK_INVALID
1134 && wp->cw.log_ip_pos != 0) {
1135 const char *rh;
1136 int old_eof;
1137
1138 rh = parse_received(headerv, &wp->cw.cks,
1139 0, 0, /* already have HELO */
1140 wp->cw.sender_str,
1141 sizeof(wp->cw.sender_str),
1142 wp->cw.sender_name,
1143 sizeof(wp->cw.sender_name));
1144 if (rh == 0) {
1145 /* to avoid being fooled by forged Received:
1146 * fields, do not skip unrecognized forms */
1147 wp->cw.log_ip_pos = 0;
1148
1149 } else if (*rh != '\0') {
1150 thr_log_print(&wp->cw, 1,
1151 "skip %s Received: header\n", rh);
1152
1153 } else if (!check_mx_listing(&wp->cw)) {
1154 /* put the IP address in the log file
1155 * if now know it */
1156 i = strlen(wp->cw.sender_str);
1157 if (i > wp->cw.log_ip_len)
1158 i = wp->cw.log_ip_len;
1159 old_eof = log_lseek_get(&wp->cw);
1160 if (old_eof == 0) {
1161 ;
1162 } else if (-1 == lseek(wp->cw.log_fd,
1163 wp->cw.log_ip_pos,
1164 SEEK_SET)) {
1165 thr_error_msg(&wp->cw,
1166 "lseek(%s,%d,SEEK_SET):"
1167 " %s",
1168 wp->cw.log_nm,
1169 (int)wp->cw.log_ip_pos,
1170 ERROR_STR());
1171 } else {
1172 j = write(wp->cw.log_fd,
1173 wp->cw.sender_str, i);
1174 /* cannot log errors from that write()
1175 * because the file is at the wrong
1176 * position */
1177 lseek(wp->cw.log_fd,
1178 old_eof, SEEK_SET);
1179 }
1180 }
1181 }
1182 return SMFIS_CONTINUE;
1183 }
1184
1185 /* remember existing X-DCC headers so that we can delete them */
1186 if (chghdr == SETHDR
1187 && (j = f_len - LITZ(DCC_XHDR_START DCC_XHDR_END)) >= 0
1188 && !CLITCMP(headerf, DCC_XHDR_START)
1189 && !CLITCMP(headerf+f_len-LITZ(DCC_XHDR_END), DCC_XHDR_END)) {
1190 cp = headerf+LITZ(DCC_XHDR_START);
1191 for (i = 0; ; ++i) {
1192 if (i >= wp->num_x_dcc) {
1193 if (i < NUM_XHDRS) {
1194 ++wp->num_x_dcc;
1195 wp->xhdrs[i].num = 1;
1196 wp->xhdrs[i].len = j;
1197 memcpy(wp->xhdrs[i].brand, cp, j);
1198 }
1199 break;
1200 }
1201
1202 if (j == wp->xhdrs[i].len
1203 && !strncasecmp(cp, wp->xhdrs[i].brand, j)) {
1204 /* this is a familiar X-DCC header */
1205 if (wp->xhdrs[i].num < 255)
1206 ++wp->xhdrs[i].num;
1207 break;
1208 }
1209 }
1210 }
1211
1212 dcc_ck_get_sub(&wp->cw.cks, headerf, headerv);
1213
1214 /* Notice MIME multipart boundary definitions */
1215 dcc_ck_mime_hdr(&wp->cw.cks, headerf, headerv);
1216
1217 return SMFIS_CONTINUE;
1218 }
1219
1220
1221
1222 static sfsistat
1223 dccm_eoh(SMFICTX *milter_ctx)
1224 {
1225 WORK *wp;
1226
1227 wp = get_wp(milter_ctx, GET_WP_GOING);
1228
1229 /* finish logging the envelope on the first header,
1230 * but if there were no headers we must do it now */
1231 if (!(wp->cw.cmn_fgs & CMN_FG_ENV_LOGGED))
1232 thr_log_envelope(&wp->cw, 1);
1233
1234 /* Create a checksum for a null Message-ID header if there
1235 * was no Message-ID header. */
1236 if (wp->cw.cks.sums[DCC_CK_MESSAGE_ID].type != DCC_CK_MESSAGE_ID)
1237 dcc_get_cks(&wp->cw.cks, DCC_CK_MESSAGE_ID, "", 0);
1238
1239 /* log the blank line between the header and the body */
1240 log_body_write(&wp->cw, "\n", 1);
1241
1242 /* Check DNS blacklists for STMP client and envelope sender
1243 * unless DNSBL checks are turned off for all of the recipients */
1244 if (wp->cw.cks.dnsbl) {
1245 if (wp->cw.cks.sums[DCC_CK_IP].type == DCC_CK_IP)
1246 dcc_client_dnsbl(wp->cw.cks.dnsbl, &wp->cw.cks.ip_addr,
1247 wp->cw.sender_name);
1248 if (wp->cw.mail_host[0] != '\0')
1249 dcc_mail_host_dnsbl(wp->cw.cks.dnsbl, wp->cw.mail_host);
1250 }
1251
1252 return SMFIS_CONTINUE;
1253 }
1254
1255
1256
1257 static sfsistat
1258 dccm_body(SMFICTX *milter_ctx, u_char *bodyp, size_t bodylen)
1259 {
1260 WORK *wp;
1261
1262 wp = get_wp(milter_ctx, GET_WP_GOING);
1263
1264 /* Log the body block */
1265 log_body_write(&wp->cw, (const char *)bodyp, bodylen);
1266
1267 dcc_ck_body(&wp->cw.cks, bodyp, bodylen);
1268
1269 return SMFIS_CONTINUE;
1270 }
1271
1272
1273
1274 static void
1275 msg_fin(SMFICTX *milter_ctx, WORK *wp)
1276 {
1277 dcc_cks_fin(&wp->cw.cks);
1278
1279 LOG_CAPTION(wp, DCC_LOG_MSG_SEP);
1280 thr_log_late(&wp->cw);
1281
1282 /* get sendmail's final say */
1283 ask_sm(milter_ctx, wp);
1284
1285 /* check the grey and white lists */
1286 cmn_ask_white(&wp->cw);
1287 }
1288
1289
1290
1291 /* deal with the end of the SMTP message as announced by sendmail */
1292 static sfsistat
1293 dccm_eom(SMFICTX *milter_ctx)
1294 {
1295 static char null[] = ""; /* libmilter doesn't know about const */
1296 WORK *wp;
1297 char *hdr;
1298 char delbuf[LITZ(DCC_XHDR_START)+DCC_BRAND_MAXLEN+LITZ(DCC_XHDR_END)+1];
1299 int xhdr_fname_len;
1300 int i, j;
1301
1302 wp = get_wp(milter_ctx, GET_WP_GOING);
1303
1304 msg_fin(milter_ctx, wp);
1305
1306 /* delete pre-existing X-DCC headers to prevent tricks on MUAs that
1307 * pay attention to them */
1308 if (chghdr == SETHDR) {
1309 for (i = 0; i < wp->num_x_dcc; ++i) {
1310 snprintf(delbuf, sizeof(delbuf), DCC_XHDR_PAT,
1311 wp->xhdrs[i].len, wp->xhdrs[i].brand);
1312 do {
1313 j = smfi_chgheader(wp->milter_ctx, delbuf,
1314 wp->xhdrs[i].num, null);
1315 if (MI_SUCCESS != j) {
1316 thr_error_msg(&wp->cw,
1317 "smfi_delheader(\"%s\","
1318 "\"\")=%d",
1319 delbuf, j);
1320 }
1321 } while (--wp->xhdrs[i].num > 0);
1322 }
1323 }
1324
1325 wp->cw.header.buf[0] = '\0';
1326 wp->cw.header.used = 0;
1327 if (wp->cw.tgts <= wp->cw.white_tgts) {
1328 /* it is whitelist for all targets,
1329 * so add X-DCC header saying so */
1330 if (chghdr != NOHDR)
1331 xhdr_whitelist(&wp->cw.header);
1332 xhdr_fname_len = DCC_XHDR_WHITELIST_FNAME_LEN+2;
1333
1334 /* log it if the target count is high enough */
1335 dcc_honor_log_cnts(&wp->cw.ask_st, &wp->cw.cks, wp->cw.tgts);
1336
1337 } else {
1338 /* Report to the DCC
1339 * Request a temporary failure if the DCC failed and we
1340 * are trying hard */
1341 i = cmn_ask_dcc(&wp->cw);
1342 if (i <= 0) {
1343 if (!i && try_extra_hard)
1344 return msg_tempfail(wp, &dcc_fail_reply);
1345
1346 /* after unrecoverable errors without even a fake
1347 * header from local blacklisting, act as if the
1348 * DCC server said not-spam but without a header */
1349 }
1350 xhdr_fname_len = wp->cw.xhdr_fname_len+2;
1351 }
1352 /* install the X-DCC header */
1353 if (chghdr != NOHDR && wp->cw.header.buf[0] != '\0') {
1354 /* kludge the trailing '\n' that sendmail hates */
1355 wp->cw.header.buf[wp->cw.header.used-1] = '\0';
1356 hdr = &wp->cw.header.buf[xhdr_fname_len];
1357 i = smfi_addheader(wp->milter_ctx, wp->cw.xhdr_fname, hdr);
1358 if (MI_SUCCESS != i)
1359 thr_error_msg(&wp->cw,
1360 "smfi_addheader(\"%s\",\"%s\")=%d",
1361 wp->cw.xhdr_fname, hdr, i);
1362 wp->cw.header.buf[wp->cw.header.used-1] = '\n';
1363 }
1364
1365 ++totals.msgs;
1366 totals.tgts += wp->cw.tgts;
1367
1368 /* get consensus of targets' wishes */
1369 users_process(&wp->cw);
1370 /* log the consensus & generate SMTP rejection message if needed */
1371 users_log_result(&wp->cw, 0);
1372
1373 if (wp->cw.ask_st & ASK_ST_GREY_EMBARGO) {
1374 totals.tgts_embargoed += wp->cw.tgts;
1375 ++totals.msgs_embargoed;
1376 return msg_reject(wp);
1377 }
1378
1379 /* tell sendmail to deliver it if all (remaining) targets want it */
1380 if (wp->cw.reject_tgts == 0) {
1381 msg_done(wp, 0);
1382 return SMFIS_ACCEPT;
1383 }
1384
1385 /* it is rejectable spam unless we are ignoring results */
1386 switch (wp->cw.action) {
1387 case CMN_IGNORE:
1388 if (wp->cw.reject_tgts != 0) {
1389 totals.tgts_ignored += wp->cw.reject_tgts;
1390 ++totals.msgs_spam;
1391 }
1392 msg_done(wp, DCC_XHDR_RESULT_I_A);
1393 return SMFIS_ACCEPT;
1394
1395 case CMN_DISCARD:
1396 /* discard it if that is our choice
1397 * or if sendmail said to */
1398 if (wp->cw.reject_tgts != 0) {
1399 totals.tgts_discarded += wp->cw.reject_tgts;
1400 ++totals.msgs_spam;
1401 }
1402 msg_done(wp, DCC_XHDR_RESULT_DISCARD);
1403 return SMFIS_DISCARD;
1404
1405 case CMN_REJECT:
1406 if (wp->cw.reject_tgts != 0) {
1407 totals.tgts_rejected += wp->cw.reject_tgts;
1408 ++totals.msgs_spam;
1409 }
1410 }
1411
1412 /* tell sendmail what to do with it */
1413 return msg_reject(wp);
1414 }
1415
1416
1417
1418 /* deal with an aborted SMTP transaction */
1419 static void
1420 msg_abort(WORK *wp)
1421 {
1422 if (wp->cw.env_from[0] == '\0')
1423 return;
1424
1425 wp->cw.ask_st |= ASK_ST_INVALID_MSG;
1426 if (!(wp->cw.cmn_fgs & CMN_FG_ENV_LOGGED))
1427 thr_log_envelope(&wp->cw, 0);
1428 msg_fin(wp->milter_ctx, wp);
1429
1430 users_process(&wp->cw);
1431 users_log_result(&wp->cw, "STMP message aborted");
1432
1433 /* create log files for -d
1434 * and without any recipents but with "option log-all" */
1435 if (dcc_clnt_debug
1436 || (wp->cw.init_sws & FLTR_SW_LOG_ALL))
1437 wp->cw.ask_st |= ASK_ST_LOGIT;
1438
1439 if (wp->cw.ask_st & ASK_ST_LOGIT)
1440 LOG_CAPTION(wp, DCC_XHDR_RESULT"STMP message aborted\n");
1441 }
1442
1443
1444
1445 /* end of the SMTP session */
1446 static sfsistat
1447 dccm_close(SMFICTX *milter_ctx)
1448 {
1449 int msg_cnt;
1450 struct timeval tv;
1451 WORK *wp;
1452
1453 wp = get_wp(milter_ctx, GET_WP_CLOSE);
1454 if (!wp) {
1455 smfi_setpriv(milter_ctx, 0);
1456 return SMFIS_TEMPFAIL;
1457 }
1458
1459 msg_abort(wp);
1460
1461 /* finished with the context */
1462 log_stop(&wp->cw);
1463 lock_work();
1464 free_rcpt_sts(&wp->cw, 0);
1465
1466 wp->milter_ctx = WORK_MILTER_CTX_IDLE;
1467 wp->fwd = work_free;
1468 work_free = wp;
1469
1470 msg_cnt = work_too_many;
1471 if (msg_cnt != 0) {
1472 gettimeofday(&tv, 0);
1473 if (work_msg_time == tv.tv_sec) {
1474 msg_cnt = 0;
1475 } else {
1476 work_msg_time = tv.tv_sec;
1477 work_too_many = 0;
1478 }
1479 }
1480 unlock_work();
1481 if (msg_cnt != 0)
1482 dcc_error_msg("%d too many simultaneous mail messages",
1483 msg_cnt);
1484
1485 smfi_setpriv(milter_ctx, 0);
1486
1487 return SMFIS_CONTINUE;
1488 }
1489
1490
1491
1492 static sfsistat
1493 dccm_abort(SMFICTX *milter_ctx)
1494 {
1495 WORK *wp;
1496
1497 wp = get_wp(milter_ctx, GET_WP_ABORT);
1498 if (!wp)
1499 return SMFIS_TEMPFAIL;
1500
1501 msg_abort(wp);
1502
1503 /* get ready for possible new message */
1504 msg_clear(wp);
1505 return SMFIS_CONTINUE;
1506 }