1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Spam related facilities.
4 * Copyright (c) 2013 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #ifndef HAVE_AMALGAMATION
27 #if defined HAVE_SPAM_SPAMC || defined HAVE_SPAM_FILTER
28 # include <sys/wait.h>
31 #ifdef HAVE_SPAM_SPAMD
32 # include <sys/socket.h>
36 /* This is chosen rather arbitrarily.
37 * It must be able to swallow the first line of a rate response,
38 * and an entire CHECK/TELL spamd(1) response */
39 #if BUFFER_SIZE < 1024
40 # error *spam-interface* BUFFER_SIZE constraints are not matched
43 #ifdef HAVE_SPAM_SPAMD
44 # define SPAMD_IDENT "SPAMC/1.5"
46 # define SUN_LEN(SUP) \
47 (sizeof(*(SUP)) - sizeof((SUP)->sun_path) + strlen((SUP)->sun_path))
51 #ifdef HAVE_SPAM_FILTER
52 /* NELEM() of regmatch_t groups */
53 # define SPAM_FILTER_MATCHES 32u
63 #if defined HAVE_SPAM_SPAMC || defined HAVE_SPAM_FILTER
66 char *cf_result
; /* _SPAM_RATE: first response line */
70 /* .cf_cmd may be adjusted for each call (`spamforget')... */
73 char const *cf_env
[4];
74 sighandler_type cf_otstp
;
75 sighandler_type cf_ottin
;
76 sighandler_type cf_ottou
;
77 sighandler_type cf_ohup
;
78 sighandler_type cf_opipe
;
79 sighandler_type cf_oint
;
80 sighandler_type cf_oquit
;
84 #ifdef HAVE_SPAM_SPAMC
86 struct spam_cf c_super
;
87 char const *c_cmd_arr
[8];
91 #ifdef HAVE_SPAM_SPAMD
94 sighandler_type d_otstp
;
95 sighandler_type d_ottin
;
96 sighandler_type d_ottou
;
97 sighandler_type d_ohup
;
98 sighandler_type d_opipe
;
99 sighandler_type d_oint
;
100 sighandler_type d_oquit
;
101 struct sockaddr_un d_sun
;
105 #ifdef HAVE_SPAM_FILTER
107 struct spam_cf f_super
;
108 char const *f_cmd_nospam
; /* Working relative to current message.. */
109 char const *f_cmd_noham
;
112 ui32_t f_score_grpno
; /* 0 for not set */
113 regex_t f_score_regex
;
119 enum spam_action vc_action
;
120 bool_t vc_verbose
; /* Verbose output */
121 bool_t vc_progress
; /* "Progress meter" (mutual verbose) */
123 bool_t (*vc_act
)(struct spam_vc
*);
124 void (*vc_dtor
)(struct spam_vc
*);
125 char *vc_buffer
; /* I/O buffer, BUFFER_SIZE bytes */
126 size_t vc_mno
; /* Current message number */
127 struct message
*vc_mp
; /* Current message */
128 FILE *vc_ifp
; /* Input stream on .vc_mp */
130 #ifdef HAVE_SPAM_SPAMC
131 struct spam_spamc spamc
;
133 #ifdef HAVE_SPAM_SPAMD
134 struct spam_spamd spamd
;
136 #ifdef HAVE_SPAM_FILTER
137 struct spam_filter filter
;
139 #if defined HAVE_SPAM_SPAMC || defined HAVE_SPAM_FILTER
143 char const *vc_esep
; /* Error separator for progress mode */
146 /* Indices according to enum spam_action */
147 static char const _spam_cmds
[][16] = {
148 "spamrate", "spamham", "spamspam", "spamforget"
151 /* Shared action setup */
152 static bool_t
_spam_action(enum spam_action sa
, int *ip
);
154 /* *spam-interface*=spamc: initialize, communicate */
155 #ifdef HAVE_SPAM_SPAMC
156 static bool_t
_spamc_setup(struct spam_vc
*vcp
);
157 static bool_t
_spamc_interact(struct spam_vc
*vcp
);
158 static void _spamc_dtor(struct spam_vc
*vcp
);
161 /* *spam-interface*=spamd: initialize, communicate */
162 #ifdef HAVE_SPAM_SPAMD
163 static bool_t
_spamd_setup(struct spam_vc
*vcp
);
164 static bool_t
_spamd_interact(struct spam_vc
*vcp
);
167 /* *spam-interface*=filter: initialize, communicate */
168 #ifdef HAVE_SPAM_FILTER
169 static bool_t
_spamfilter_setup(struct spam_vc
*vcp
);
170 static bool_t
_spamfilter_interact(struct spam_vc
*vcp
);
171 static void _spamfilter_dtor(struct spam_vc
*vcp
);
174 /* *spam-interface*=(spamc|filter): create child + communication */
175 #if defined HAVE_SPAM_SPAMC || defined HAVE_SPAM_FILTER
176 static void _spam_cf_setup(struct spam_vc
*vcp
, bool_t useshell
);
177 static bool_t
_spam_cf_interact(struct spam_vc
*vcp
);
180 /* Convert a floating-point spam rate into message.m_spamscore */
181 #if defined HAVE_SPAM_SPAMC || defined HAVE_SPAM_SPAMD ||\
182 (defined HAVE_SPAM_FILTER && defined HAVE_REGEX)
183 static void _spam_rate2score(struct spam_vc
*vcp
, char *buf
);
187 _spam_action(enum spam_action sa
, int *ip
)
190 size_t maxsize
, skipped
, cnt
, curr
;
195 memset(&vc
, 0, sizeof vc
);
197 vc
.vc_verbose
= ((options
& OPT_VERB
) != 0);
198 vc
.vc_progress
= (!vc
.vc_verbose
&& ((options
& OPT_INTERACTIVE
) != 0));
199 vc
.vc_esep
= vc
.vc_progress
? "\n" : "";
201 /* Check and setup the desired spam interface */
202 if ((cp
= ok_vlook(spam_interface
)) == NULL
) {
203 n_err(_("`%s': no *spam-interface* set\n"), _spam_cmds
[sa
]);
205 #ifdef HAVE_SPAM_SPAMC
206 } else if (!asccasecmp(cp
, "spamc")) {
207 if (!_spamc_setup(&vc
))
210 #ifdef HAVE_SPAM_SPAMD
211 } else if (!asccasecmp(cp
, "spamd")) {
212 if (!_spamd_setup(&vc
))
215 #ifdef HAVE_SPAM_FILTER
216 } else if (!asccasecmp(cp
, "filter")) {
217 if (!_spamfilter_setup(&vc
))
221 n_err(_("`%s': unknown / unsupported *spam-interface*: \"%s\"\n"),
226 /* *spam-maxsize* we do handle ourselfs instead */
228 if ((cp
= ok_vlook(spam_maxsize
)) != NULL
)
229 maxsize
= (size_t)strtol(cp
, NULL
, 10); /* TODO strtol */
231 maxsize
= SPAM_MAXSIZE
;
233 /* Finally get an I/O buffer */
234 vc
.vc_buffer
= salloc(BUFFER_SIZE
);
237 if (vc
.vc_progress
) {
241 for (curr
= 0, ok
= TRU1
; *ip
!= 0; --cnt
, ++curr
, ++ip
) {
242 vc
.vc_mno
= (size_t)*ip
;
243 vc
.vc_mp
= message
+ vc
.vc_mno
- 1;
244 if (sa
== _SPAM_RATE
)
245 vc
.vc_mp
->m_spamscore
= 0;
247 if (vc
.vc_mp
->m_size
> maxsize
) {
249 n_err(_("`%s': message %lu exceeds maxsize (%lu > %lu), skip\n"),
250 _spam_cmds
[sa
], (ul_i
)vc
.vc_mno
, (ul_i
)(size_t)vc
.vc_mp
->m_size
,
252 else if (vc
.vc_progress
) {
253 fprintf(stdout
, "\r%s: !%-6" PRIuZ
" %6" PRIuZ
"/%-6" PRIuZ
,
254 _spam_cmds
[sa
], vc
.vc_mno
, cnt
, curr
);
260 n_err(_("`%s': message %lu\n"), _spam_cmds
[sa
], (ul_i
)vc
.vc_mno
);
261 else if (vc
.vc_progress
) {
262 fprintf(stdout
, "\r%s: .%-6" PRIuZ
" %6" PRIuZ
"/%-6" PRIuZ
,
263 _spam_cmds
[sa
], vc
.vc_mno
, cnt
, curr
);
268 if ((vc
.vc_ifp
= setinput(&mb
, vc
.vc_mp
, NEED_BODY
)) == NULL
) {
269 n_err(_("%s`%s': cannot load message %lu: %s\n"),
270 vc
.vc_esep
, _spam_cmds
[sa
], (ul_i
)vc
.vc_mno
, strerror(errno
));
275 if (!(ok
= (*vc
.vc_act
)(&vc
)))
279 if (vc
.vc_progress
) {
281 fprintf(stdout
, _(" %s (%" PRIuZ
"/%" PRIuZ
" all/skipped)\n"),
282 (ok
? "done" : "ERROR"), curr
, skipped
);
286 if (vc
.vc_dtor
!= NULL
)
293 #ifdef HAVE_SPAM_SPAMC
295 _spamc_setup(struct spam_vc
*vcp
)
297 struct spam_spamc
*sscp
;
299 char const **args
, *cp
;
303 sscp
= &vcp
->vc_t
.spamc
;
304 args
= sscp
->c_cmd_arr
;
306 if ((cp
= ok_vlook(spamc_command
)) == NULL
) {
307 # ifdef SPAM_SPAMC_PATH
308 cp
= SPAM_SPAMC_PATH
;
310 n_err(_("`%s': *spamc-command* is not set\n"),
311 _spam_cmds
[vcp
->vc_action
]);
317 switch (vcp
->vc_action
) {
336 *args
++ = "-l"; /* --log-to-stderr */
337 *args
++ = "-x"; /* No "safe callback", we need to react on errors! */
339 if ((cp
= ok_vlook(spamc_arguments
)) != NULL
)
342 if ((cp
= ok_vlook(spamc_user
)) != NULL
) {
348 assert(PTR2SIZE(args
- sscp
->c_cmd_arr
) <= NELEM(sscp
->c_cmd_arr
));
351 sscp
->c_super
.cf_cmd
= str_concat_cpa(&str
, sscp
->c_cmd_arr
, " ")->s
;
353 n_err(_("spamc(1) via \"%s\"\n"), sscp
->c_super
.cf_cmd
);
355 _spam_cf_setup(vcp
, FAL0
);
357 vcp
->vc_act
= &_spamc_interact
;
358 vcp
->vc_dtor
= &_spamc_dtor
;
360 # ifndef SPAM_SPAMC_PATH
368 _spamc_interact(struct spam_vc
*vcp
)
373 if (!(rv
= _spam_cf_interact(vcp
)))
376 vcp
->vc_mp
->m_flag
&= ~(MSPAM
| MSPAMUNSURE
);
377 if (vcp
->vc_action
!= _SPAM_RATE
) {
378 if (vcp
->vc_action
== _SPAM_SPAM
)
379 vcp
->vc_mp
->m_flag
|= MSPAM
;
383 switch (WEXITSTATUS(vcp
->vc_t
.spamc
.c_super
.cf_waitstat
)) {
385 vcp
->vc_mp
->m_flag
|= MSPAM
;
394 if ((cp
= strchr(buf
= vcp
->vc_t
.spamc
.c_super
.cf_result
, '/')) != NULL
)
395 buf
[PTR2SIZE(cp
- buf
)] = '\0';
396 _spam_rate2score(vcp
, buf
);
404 _spamc_dtor(struct spam_vc
*vcp
)
407 if (vcp
->vc_t
.spamc
.c_super
.cf_result
!= NULL
)
408 free(vcp
->vc_t
.spamc
.c_super
.cf_result
);
411 #endif /* HAVE_SPAM_SPAMC */
413 #ifdef HAVE_SPAM_SPAMD
415 _spamd_setup(struct spam_vc
*vcp
)
417 struct spam_spamd
*ssdp
;
423 ssdp
= &vcp
->vc_t
.spamd
;
425 if ((cp
= ok_vlook(spamd_user
)) != NULL
) {
428 ssdp
->d_user
.l
= strlen(ssdp
->d_user
.s
= UNCONST(cp
));
431 if ((cp
= ok_vlook(spamd_socket
)) == NULL
) {
432 n_err(_("`%s': required *spamd-socket* is not set\n"),
433 _spam_cmds
[vcp
->vc_action
]);
436 if ((l
= strlen(cp
) +1) >= sizeof(ssdp
->d_sun
.sun_path
)) {
437 n_err(_("`%s': *spamd-socket* too long: \"%s\"\n"),
438 _spam_cmds
[vcp
->vc_action
], cp
);
441 ssdp
->d_sun
.sun_family
= AF_UNIX
;
442 memcpy(ssdp
->d_sun
.sun_path
, cp
, l
);
444 vcp
->vc_act
= &_spamd_interact
;
451 static sigjmp_buf __spamd_actjmp
; /* TODO oneday, we won't need it no more */
452 static int volatile __spamd_sig
; /* TODO oneday, we won't need it no more */
454 __spamd_onsig(int sig
) /* TODO someday, we won't need it no more */
456 NYD_X
; /* Signal handler */
458 siglongjmp(__spamd_actjmp
, 1);
462 _spamd_interact(struct spam_vc
*vcp
)
464 struct spam_spamd
*ssdp
;
466 char *lp
, *cp
, * volatile headbuf
= NULL
;
467 int volatile dsfd
= -1;
468 bool_t
volatile rv
= FAL0
;
471 ssdp
= &vcp
->vc_t
.spamd
;
475 ssdp
->d_otstp
= safe_signal(SIGTSTP
, SIG_DFL
);
476 ssdp
->d_ottin
= safe_signal(SIGTTIN
, SIG_DFL
);
477 ssdp
->d_ottou
= safe_signal(SIGTTOU
, SIG_DFL
);
478 ssdp
->d_opipe
= safe_signal(SIGPIPE
, SIG_IGN
);
479 ssdp
->d_ohup
= safe_signal(SIGHUP
, &__spamd_onsig
);
480 ssdp
->d_oint
= safe_signal(SIGINT
, &__spamd_onsig
);
481 ssdp
->d_oquit
= safe_signal(SIGQUIT
, &__spamd_onsig
);
482 if (sigsetjmp(__spamd_actjmp
, 1)) {
483 if (*vcp
->vc_esep
!= '\0')
489 if ((dsfd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) == -1) {
490 n_err(_("%s`%s': can't create unix(4) socket: %s\n"),
491 vcp
->vc_esep
, _spam_cmds
[vcp
->vc_action
], strerror(errno
));
495 if (connect(dsfd
, (struct sockaddr
*)&ssdp
->d_sun
, SUN_LEN(&ssdp
->d_sun
)) ==
497 n_err(_("%s`%s': can't connect to *spam-socket*: %s\n"),
498 vcp
->vc_esep
, _spam_cmds
[vcp
->vc_action
], strerror(errno
));
503 /* The command header, finalized with an empty line.
504 * This needs to be written in a single write(2)! */
506 # define _X(X) do {memcpy(lp, X, sizeof(X) -1); lp += sizeof(X) -1;} while (0)
508 i
= ((cp
= ssdp
->d_user
.s
) != NULL
) ? ssdp
->d_user
.l
: 0;
509 lp
= headbuf
= ac_alloc(
510 sizeof(NETLINE("A_VERY_LONG_COMMAND " SPAMD_IDENT
)) +
511 sizeof(NETLINE("Content-length: 9223372036854775807")) +
512 ((cp
!= NULL
) ? sizeof("User: ") + i
+ sizeof(NETNL
) : 0) +
513 sizeof(NETLINE("Message-class: spam")) +
514 sizeof(NETLINE("Set: local")) +
515 sizeof(NETLINE("Remove: local")) +
516 sizeof(NETNL
) /*+1*/);
518 switch (vcp
->vc_action
) {
520 _X(NETLINE("CHECK " SPAMD_IDENT
));
525 _X(NETLINE("TELL " SPAMD_IDENT
));
529 lp
+= snprintf(lp
, 0x7FFF, NETLINE("Content-length: %" PRIuZ
),
530 (size_t)vcp
->vc_mp
->m_size
);
539 switch (vcp
->vc_action
) {
544 _X(NETLINE("Message-class: ham")
545 NETLINE("Set: local")
549 _X(NETLINE("Message-class: spam")
550 NETLINE("Set: local")
554 if (vcp
->vc_mp
->m_flag
& MSPAM
)
555 _X(NETLINE("Message-class: spam"));
557 _X(NETLINE("Message-class: ham"));
558 _X(NETLINE("Remove: local")
564 i
= PTR2SIZE(lp
- headbuf
);
565 if (options
& OPT_VERBVERB
)
566 n_err(">>> %.*s <<<\n", (int)i
, headbuf
);
567 if (i
!= (size_t)write(dsfd
, headbuf
, i
))
570 /* Then simply pass through the message "as-is" */
571 for (size
= vcp
->vc_mp
->m_size
; size
> 0;) {
572 i
= fread(vcp
->vc_buffer
, sizeof *vcp
->vc_buffer
,
573 MIN(size
, BUFFER_SIZE
), vcp
->vc_ifp
);
575 if (ferror(vcp
->vc_ifp
))
581 if (i
!= (size_t)write(dsfd
, vcp
->vc_buffer
, i
)) {
583 n_err(_("%s`%s': I/O on *spamd-socket* failed: %s\n"),
584 vcp
->vc_esep
, _spam_cmds
[vcp
->vc_action
], strerror(errno
));
589 /* We are finished, say so */
590 shutdown(dsfd
, SHUT_WR
);
592 /* Be aware on goto: i will be a line counter after this loop! */
593 for (size
= 0, i
= BUFFER_SIZE
-1;;) {
594 ssize_t j
= read(dsfd
, vcp
->vc_buffer
+ size
, i
);
601 /* For the current way of doing things a single read will suffice.
602 * Note we'll be "penaltized" when awaiting EOF on the socket, at least
603 * in blocking mode, so do avoid that and break off */
607 vcp
->vc_buffer
[size
] = '\0';
609 if (size
== 0 || size
== BUFFER_SIZE
) {
611 n_err(_("%s`%s': bogus spamd(1) I/O interaction (%lu)\n"),
612 vcp
->vc_esep
, _spam_cmds
[vcp
->vc_action
], (ul_i
)i
);
614 if (options
& OPT_VERBVERB
)
615 n_err(">>> BUFFER: %s <<<\n", vcp
->vc_buffer
);
620 /* From the response, read those lines that interest us */
621 for (lp
= vcp
->vc_buffer
; size
> 0; ++i
) {
623 lp
= strchr(lp
, NETNL
[0]);
627 if (lp
[1] != NETNL
[1])
630 size
-= PTR2SIZE(lp
- cp
);
633 if (!strncmp(cp
, "SPAMD/1.1 0 EX_OK", sizeof("SPAMD/1.1 0 EX_OK") -1))
635 if (vcp
->vc_action
!= _SPAM_RATE
||
636 strstr(cp
, "Service Unavailable") == NULL
)
639 /* Unfortunately a missing --allow-tell drops connection.. */
640 n_err(_("%s`%s': service not available in spamd(1) instance\n"),
641 vcp
->vc_esep
, _spam_cmds
[vcp
->vc_action
]);
645 switch (vcp
->vc_action
) {
647 if (strncmp(cp
, "Spam: ", sizeof("Spam: ") -1))
649 cp
+= sizeof("Spam: ") -1;
651 if (!strncmp(cp
, "False", sizeof("False") -1)) {
652 cp
+= sizeof("False") -1;
653 vcp
->vc_mp
->m_flag
&= ~(MSPAM
| MSPAMUNSURE
);
654 } else if (!strncmp(cp
, "True", sizeof("True") -1)) {
655 cp
+= sizeof("True") -1;
656 vcp
->vc_mp
->m_flag
&= ~(MSPAM
| MSPAMUNSURE
);
657 vcp
->vc_mp
->m_flag
|= MSPAM
;
661 while (blankspacechar(*cp
))
667 char *xcp
= strchr(cp
, '/');
669 size
= PTR2SIZE(xcp
- cp
);
672 _spam_rate2score(vcp
, cp
);
678 /* Empty response means ok but "did nothing" */
680 strncmp(cp
, "DidSet: local", sizeof("DidSet: local") -1))
682 if (*cp
== '\0' && vcp
->vc_verbose
)
683 n_err(_("\tBut spamd(1) \"did nothing\" for message\n"));
684 vcp
->vc_mp
->m_flag
&= ~(MSPAM
| MSPAMUNSURE
);
685 if (vcp
->vc_action
== _SPAM_SPAM
)
686 vcp
->vc_mp
->m_flag
|= MSPAM
;
691 strncmp(cp
, "DidRemove: local", sizeof("DidSet: local") -1))
693 if (*cp
== '\0' && vcp
->vc_verbose
)
694 n_err(_("\tBut spamd(1) \"did nothing\" for message\n"));
695 vcp
->vc_mp
->m_flag
&= ~(MSPAM
| MSPAMUNSURE
);
709 safe_signal(SIGQUIT
, ssdp
->d_oquit
);
710 safe_signal(SIGINT
, ssdp
->d_oint
);
711 safe_signal(SIGHUP
, ssdp
->d_ohup
);
712 safe_signal(SIGPIPE
, ssdp
->d_opipe
);
713 safe_signal(SIGTSTP
, ssdp
->d_otstp
);
714 safe_signal(SIGTTIN
, ssdp
->d_ottin
);
715 safe_signal(SIGTTOU
, ssdp
->d_ottou
);
718 if (__spamd_sig
!= 0) {
721 sigaddset(&cset
, __spamd_sig
);
722 sigprocmask(SIG_UNBLOCK
, &cset
, NULL
);
723 n_raise(__spamd_sig
);
728 #endif /* HAVE_SPAM_SPAMD */
730 #ifdef HAVE_SPAM_FILTER
732 _spamfilter_setup(struct spam_vc
*vcp
)
734 struct spam_filter
*sfp
;
735 char const *cp
, *var
;
739 sfp
= &vcp
->vc_t
.filter
;
741 switch (vcp
->vc_action
) {
743 cp
= ok_vlook(spamfilter_rate
);
744 var
= "spam-filter-rate";
747 cp
= ok_vlook(spamfilter_ham
);
748 var
= "spam-filter-ham";
751 cp
= ok_vlook(spamfilter_spam
);
752 var
= "spam-filter-spam";
756 n_err(_("`%s': *%s* is not set\n"), _spam_cmds
[vcp
->vc_action
], var
);
759 sfp
->f_super
.cf_cmd
= savestr(cp
);
762 var
= "spam-filter-nospam";
763 if ((cp
= ok_vlook(spamfilter_nospam
)) == NULL
)
765 sfp
->f_cmd_nospam
= savestr(cp
);
766 if ((cp
= ok_vlook(spamfilter_noham
)) == NULL
)
768 sfp
->f_cmd_noham
= savestr(cp
);
773 if (vcp
->vc_action
== _SPAM_RATE
&&
774 (cp
= ok_vlook(spamfilter_rate_scanscore
)) != NULL
) {
778 var
= strchr(cp
, ';');
780 n_err(_("`%s': *spamfilter-rate-scanscore*: no `;' in \"%s\"\n"),
781 _spam_cmds
[vcp
->vc_action
], cp
);
786 var
= savestrbuf(cp
, PTR2SIZE(var
- cp
));
787 sfp
->f_score_grpno
= (ui32_t
)strtoul(var
, &ep
, 0);
788 if (var
== ep
|| *ep
!= '\0') {
789 n_err(_("`%s': *spamfilter-rate-scanscore*: bad group in \"%s\"\n"),
790 _spam_cmds
[vcp
->vc_action
], cp
);
793 if (sfp
->f_score_grpno
>= SPAM_FILTER_MATCHES
) {
794 n_err(_("`%s': *spamfilter-rate-scanscore*: "
795 "group %u excesses limit %u\n"),
796 _spam_cmds
[vcp
->vc_action
], sfp
->f_score_grpno
,
797 SPAM_FILTER_MATCHES
);
801 if (regcomp(&sfp
->f_score_regex
, bp
, REG_EXTENDED
| REG_ICASE
)) {
802 n_err(_("`%s': invalid *spamfilter-rate-scanscore* regex: \"%s\"\n"),
803 _spam_cmds
[vcp
->vc_action
], cp
);
806 if (sfp
->f_score_grpno
> sfp
->f_score_regex
.re_nsub
) {
807 regfree(&sfp
->f_score_regex
);
808 n_err(_("`%s': *spamfilter-rate-scanscore*: "
809 "no group %u in \"%s\"\n"),
810 _spam_cmds
[vcp
->vc_action
], sfp
->f_score_grpno
, cp
);
814 # endif /* HAVE_REGEX */
816 _spam_cf_setup(vcp
, TRU1
);
818 vcp
->vc_act
= &_spamfilter_interact
;
819 vcp
->vc_dtor
= &_spamfilter_dtor
;
827 _spamfilter_interact(struct spam_vc
*vcp
)
830 regmatch_t rem
[SPAM_FILTER_MATCHES
], *remp
;
831 struct spam_filter
*sfp
;
837 if (vcp
->vc_action
== _SPAM_FORGET
)
838 vcp
->vc_t
.cf
.cf_cmd
= (vcp
->vc_mp
->m_flag
& MSPAM
)
839 ? vcp
->vc_t
.filter
.f_cmd_nospam
: vcp
->vc_t
.filter
.f_cmd_noham
;
841 if (!(rv
= _spam_cf_interact(vcp
)))
844 vcp
->vc_mp
->m_flag
&= ~(MSPAM
| MSPAMUNSURE
);
845 if (vcp
->vc_action
!= _SPAM_RATE
) {
846 if (vcp
->vc_action
== _SPAM_SPAM
)
847 vcp
->vc_mp
->m_flag
|= MSPAM
;
849 } else switch (WEXITSTATUS(vcp
->vc_t
.filter
.f_super
.cf_waitstat
)) {
851 vcp
->vc_mp
->m_flag
|= MSPAMUNSURE
;
856 vcp
->vc_mp
->m_flag
|= MSPAM
;
864 sfp
= &vcp
->vc_t
.filter
;
866 if (sfp
->f_score_grpno
== 0)
869 assert(sfp
->f_super
.cf_result
!= NULL
);
870 remp
= rem
+ sfp
->f_score_grpno
;
872 if (regexec(&sfp
->f_score_regex
, sfp
->f_super
.cf_result
, NELEM(rem
), rem
,
873 0) == REG_NOMATCH
|| (remp
->rm_so
| remp
->rm_eo
) < 0) {
874 n_err(_("`%s': *spamfilter-rate-scanscore* "
875 "doesn't match filter output!\n"),
876 _spam_cmds
[vcp
->vc_action
]);
877 sfp
->f_score_grpno
= 0;
881 cp
= sfp
->f_super
.cf_result
;
882 cp
[remp
->rm_eo
] = '\0';
884 _spam_rate2score(vcp
, cp
);
885 # endif /* HAVE_REGEX */
893 _spamfilter_dtor(struct spam_vc
*vcp
)
895 struct spam_filter
*sfp
;
898 sfp
= &vcp
->vc_t
.filter
;
900 if (sfp
->f_super
.cf_result
!= NULL
)
901 free(sfp
->f_super
.cf_result
);
903 if (sfp
->f_score_grpno
> 0)
904 regfree(&sfp
->f_score_regex
);
908 #endif /* HAVE_SPAM_FILTER */
910 #if defined HAVE_SPAM_SPAMC || defined HAVE_SPAM_FILTER
912 _spam_cf_setup(struct spam_vc
*vcp
, bool_t useshell
)
915 struct spam_cf
*scfp
;
918 LCTA(3 < NELEM(scfp
->cf_env
));
920 scfp
= &vcp
->vc_t
.cf
;
922 if ((scfp
->cf_useshell
= useshell
)) {
923 if ((cp
= ok_vlook(SHELL
)) == NULL
)
929 /* NAIL_FILENAME_GENERATED *//* TODO pathconf NAME_MAX; but user can create
930 * TODO a file wherever he wants! *Do* create a zero-size temporary file
931 * TODO and give *that* path as NAIL_FILENAME_TEMPORARY, clean it up once
932 * TODO the pipe returns? Like this we *can* verify path/name issues! */
933 scfp
->cf_env
[0] = str_concat_csvl(&s
, NAILENV_FILENAME_GENERATED
, "=",
934 getrandstring(MIN(NAME_MAX
/ 4, 16)), NULL
)->s
;
936 scfp
->cf_env
[1] = str_concat_csvl(&s
, NAILENV_TMPDIR
, "=", tempdir
, NULL
)->s
;
937 scfp
->cf_env
[2] = str_concat_csvl(&s
, "TMPDIR", "=", tempdir
, NULL
)->s
;
938 scfp
->cf_env
[3] = NULL
;
942 static sigjmp_buf __spam_cf_actjmp
; /* TODO someday, we won't need it */
943 static int volatile __spam_cf_sig
; /* TODO someday, we won't need it */
945 __spam_cf_onsig(int sig
) /* TODO someday, we won't need it no more */
947 NYD_X
; /* Signal handler */
949 siglongjmp(__spam_cf_actjmp
, 1);
953 _spam_cf_interact(struct spam_vc
*vcp
)
955 struct spam_cf
*scfp
;
966 _P2C
= _P2C_0
| _P2C_1
,
969 _C2P
= _C2P_0
| _C2P_1
,
974 } volatile state
= _NONE
;
977 scfp
= &vcp
->vc_t
.cf
;
978 if (scfp
->cf_result
!= NULL
) {
979 free(scfp
->cf_result
);
980 scfp
->cf_result
= NULL
;
983 /* TODO Avoid that we jump away; yet necessary signal mess */
984 /*__spam_cf_sig = 0;*/
987 scfp
->cf_otstp
= safe_signal(SIGTSTP
, SIG_DFL
);
988 scfp
->cf_ottin
= safe_signal(SIGTTIN
, SIG_DFL
);
989 scfp
->cf_ottou
= safe_signal(SIGTTOU
, SIG_DFL
);
990 scfp
->cf_opipe
= safe_signal(SIGPIPE
, SIG_IGN
);
991 scfp
->cf_ohup
= safe_signal(SIGHUP
, &__spam_cf_onsig
);
992 scfp
->cf_oint
= safe_signal(SIGINT
, &__spam_cf_onsig
);
993 scfp
->cf_oquit
= safe_signal(SIGQUIT
, &__spam_cf_onsig
);
994 /* Keep sigs blocked */
995 pid
= 0; /* cc uninit */
997 if (!pipe_cloexec(p2c
)) {
998 n_err(_("%s`%s': cannot create parent pipe: %s\n"),
999 vcp
->vc_esep
, _spam_cmds
[vcp
->vc_action
], strerror(errno
));
1004 if (!pipe_cloexec(c2p
)) {
1005 n_err(_("%s`%s': cannot create child pipe: %s\n"),
1006 vcp
->vc_esep
, _spam_cmds
[vcp
->vc_action
], strerror(errno
));
1011 if (sigsetjmp(__spam_cf_actjmp
, 1)) {
1012 if (*vcp
->vc_esep
!= '\0')
1013 n_err(vcp
->vc_esep
);
1020 /* Start our command as requested */
1022 if ((pid
= start_command(
1023 (scfp
->cf_acmd
!= NULL
? scfp
->cf_acmd
: scfp
->cf_cmd
),
1024 &cset
, p2c
[0], c2p
[1],
1025 scfp
->cf_a0
, (scfp
->cf_acmd
!= NULL
? scfp
->cf_cmd
: NULL
), NULL
,
1026 scfp
->cf_env
)) < 0) {
1034 /* Yes, we could sendmp(SEND_MBOX), but simply passing through the MBOX
1035 * content does the same in effect, however much more efficiently.
1036 * XXX NOTE: this may mean we pass a message without From_ line! */
1037 for (size
= vcp
->vc_mp
->m_size
; size
> 0;) {
1038 size_t i
= fread(vcp
->vc_buffer
, 1, MIN(size
, BUFFER_SIZE
), vcp
->vc_ifp
);
1040 if (ferror(vcp
->vc_ifp
))
1045 if (i
!= (size_t)write(p2c
[1], vcp
->vc_buffer
, i
)) {
1052 /* TODO Quite racy -- block anything for a while? */
1053 if (state
& _SIGHOLD
) {
1058 if (state
& _P2C_0
) {
1062 if (state
& _C2P_1
) {
1066 /* And cause EOF for the reader */
1067 if (state
& _P2C_1
) {
1072 if (state
& _RUNNING
) {
1073 if (!(state
& _ERRORS
) &&
1074 vcp
->vc_action
== _SPAM_RATE
&& !(state
& (_JUMPED
| _ERRORS
))) {
1075 ssize_t i
= read(c2p
[0], vcp
->vc_buffer
, BUFFER_SIZE
- 1);
1077 vcp
->vc_buffer
[i
] = '\0';
1078 if ((cp
= strchr(vcp
->vc_buffer
, NETNL
[0])) == NULL
&&
1079 (cp
= strchr(vcp
->vc_buffer
, NETNL
[1])) == NULL
) {
1080 n_err(_("%s`%s': program generates too much output: \"%s\"\n"),
1081 vcp
->vc_esep
, _spam_cmds
[vcp
->vc_action
], scfp
->cf_cmd
);
1084 scfp
->cf_result
= sbufdup(vcp
->vc_buffer
,
1085 PTR2SIZE(cp
- vcp
->vc_buffer
));
1086 /* FIXME consume child output until EOF??? */
1093 wait_child(pid
, &scfp
->cf_waitstat
);
1094 if (WIFEXITED(scfp
->cf_waitstat
))
1098 if (state
& _C2P_0
) {
1103 safe_signal(SIGQUIT
, scfp
->cf_oquit
);
1104 safe_signal(SIGINT
, scfp
->cf_oint
);
1105 safe_signal(SIGHUP
, scfp
->cf_ohup
);
1106 safe_signal(SIGPIPE
, scfp
->cf_opipe
);
1107 safe_signal(SIGTSTP
, scfp
->cf_otstp
);
1108 safe_signal(SIGTTIN
, scfp
->cf_ottin
);
1109 safe_signal(SIGTTOU
, scfp
->cf_ottou
);
1112 if (state
& _JUMPED
) {
1113 assert(vcp
->vc_dtor
!= NULL
);
1114 (*vcp
->vc_dtor
)(vcp
);
1117 sigaddset(&cset
, __spam_cf_sig
);
1118 sigprocmask(SIG_UNBLOCK
, &cset
, NULL
);
1119 n_raise(__spam_cf_sig
);
1121 return !(state
& (_JUMPED
| _ERRORS
));
1123 #endif /* HAVE_SPAM_SPAMC || HAVE_SPAM_FILTER */
1125 #if defined HAVE_SPAM_SPAMC || defined HAVE_SPAM_SPAMD ||\
1126 (defined HAVE_SPAM_FILTER && defined HAVE_REGEX)
1128 _spam_rate2score(struct spam_vc
*vcp
, char *buf
)
1134 m
= (ui32_t
)strtol(buf
, &cp
, 10);
1139 if (*cp
++ != '\0') {
1140 /* Floating-point rounding for non-mathematicians */
1142 if ((c1
= cp
[0]) != '\0' && (c2
= cp
[1]) != '\0' &&
1143 (c3
= cp
[2]) != '\0') {
1158 s
= (ui32_t
)strtol(cp
, NULL
, 10);
1162 vcp
->vc_mp
->m_spamscore
= (m
<< 8) | s
;
1166 #endif /* _SPAM_SPAMC || _SPAM_SPAMD || (_SPAM_FILTER && HAVE_REGEX) */
1169 c_spam_clear(void *v
)
1174 for (ip
= v
; *ip
!= 0; ++ip
)
1175 message
[(size_t)*ip
- 1].m_flag
&= ~(MSPAM
| MSPAMUNSURE
);
1186 for (ip
= v
; *ip
!= 0; ++ip
) {
1187 message
[(size_t)*ip
- 1].m_flag
&= ~(MSPAM
| MSPAMUNSURE
);
1188 message
[(size_t)*ip
- 1].m_flag
|= MSPAM
;
1195 c_spam_forget(void *v
)
1200 rv
= _spam_action(_SPAM_FORGET
, (int*)v
) ? OKAY
: STOP
;
1211 rv
= _spam_action(_SPAM_HAM
, (int*)v
) ? OKAY
: STOP
;
1217 c_spam_rate(void *v
)
1222 rv
= _spam_action(_SPAM_RATE
, (int*)v
) ? OKAY
: STOP
;
1228 c_spam_spam(void *v
)
1233 rv
= _spam_action(_SPAM_SPAM
, (int*)v
) ? OKAY
: STOP
;
1237 #endif /* HAVE_SPAM */