Commit | Line | Data |
---|---|---|
8d769cc9 | 1 | /* -*-c-*- |
2 | * | |
9a4b2474 | 3 | * $Id: qmail-checkspam.c,v 1.2 2004/04/08 01:36:26 mdw Exp $ |
8d769cc9 | 4 | * |
5 | * Filter messages for spam | |
6 | * | |
7 | * (c) 2003 Mark Wooding | |
8 | */ | |
9 | ||
841e5aca | 10 | /*----- Licensing notice --------------------------------------------------* |
8d769cc9 | 11 | * |
12 | * This program is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU General Public License as published by | |
14 | * the Free Software Foundation; either version 2 of the License, or | |
15 | * (at your option) any later version. | |
841e5aca | 16 | * |
8d769cc9 | 17 | * This program is distributed in the hope that it will be useful, |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | * GNU General Public License for more details. | |
841e5aca | 21 | * |
8d769cc9 | 22 | * You should have received a copy of the GNU General Public License |
23 | * along with this program; if not, write to the Free Software Foundation, | |
24 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
25 | */ | |
26 | ||
8d769cc9 | 27 | /*----- Header files ------------------------------------------------------*/ |
28 | ||
29 | #include <ctype.h> | |
30 | #include <errno.h> | |
31 | #include <stdio.h> | |
32 | #include <stdlib.h> | |
33 | #include <string.h> | |
34 | ||
35 | #include <sys/types.h> | |
36 | #include <sys/unistd.h> | |
37 | ||
38 | #include <libspamc.h> | |
39 | ||
40 | /*----- Main code ---------------------------------------------------------*/ | |
41 | ||
42 | static const char *strenv(const char *e, const char *d) | |
43 | { | |
44 | const char *p = getenv(e); | |
45 | if (!p) return (d); | |
46 | return (p); | |
47 | } | |
48 | ||
49 | static double dblenv(const char *e, double d) | |
50 | { | |
51 | const char *p = getenv(e); | |
52 | char *q; | |
53 | int err = errno; | |
54 | double f; | |
55 | if (!p) return (d); | |
56 | errno = 0; | |
57 | f = strtod(p, &q); | |
58 | if (errno) return (d); | |
59 | errno = err; | |
60 | return (f); | |
61 | } | |
62 | ||
63 | static int intenv(const char *e, int d) | |
64 | { | |
65 | const char *p = getenv(e); | |
66 | char *q; | |
67 | int err = errno; | |
68 | long l; | |
69 | if (!p) return (d); | |
70 | errno = 0; | |
71 | l = strtol(p, &q, 0); | |
72 | if (errno) return (d); | |
73 | errno = err; | |
74 | if (l < 0 || l > INT_MAX) return (d); | |
75 | return ((int)l); | |
76 | } | |
77 | ||
78 | int shovel(int from, int to) | |
79 | { | |
80 | char buf[4096]; | |
81 | ssize_t n; | |
82 | char *p; | |
83 | size_t r; | |
84 | ||
85 | for (;;) { | |
86 | n = read(from, buf, sizeof(buf)); | |
87 | if (n < 0 && errno != EINTR && errno != EAGAIN) | |
88 | return (-1); | |
89 | else if (!n) | |
90 | return (0); | |
91 | p = buf; | |
92 | r = n; | |
93 | while (r) { | |
94 | n = write(to, p, n); | |
95 | if (n <= 0 && errno != EINTR && errno != EAGAIN) | |
96 | return (-1); | |
97 | r -= n; | |
98 | p += n; | |
99 | } | |
100 | } | |
101 | } | |
102 | ||
103 | int main(int argc, char *argv[]) | |
104 | { | |
105 | struct sockaddr sa; | |
106 | struct message m; | |
107 | int fd_m[2], fd_e[2]; | |
108 | pid_t kid; | |
02955332 | 109 | const char *qmq; |
8d769cc9 | 110 | int rc; |
111 | ||
112 | m.max_len = intenv("QMAIL_CHECKSPAM_MAXLEN", 2 * 1024 * 1024); | |
113 | m.timeout = intenv("QMAIL_CHECKSPAM_TIMEOUT", 300); | |
114 | rc = message_read(0, 0, &m); | |
115 | if (rc != 0 && rc != EX_TOOBIG) | |
116 | return (54); | |
117 | if (!rc) { | |
118 | if (lookup_host(strenv("QMAIL_CHECKSPAM_SPAMDHOST", "localhost"), | |
119 | intenv("QMAIL_CHECKSPAM_SPAMDPORT", 783), | |
120 | &sa)) | |
121 | return (56); | |
122 | if (message_filter(&sa, "spamd", 0, &m)) | |
123 | return (74); | |
124 | if (m.score >= dblenv("QMAIL_CHECKSPAM_THRESH", m.threshold)) | |
125 | return (31); | |
126 | } | |
127 | if (pipe(fd_m) || pipe(fd_e)) | |
128 | return (56); | |
129 | if ((kid = fork()) < 0) | |
130 | return (56); | |
131 | if (!kid) { | |
132 | close(fd_m[0]); | |
133 | close(fd_e[0]); | |
134 | if (message_write(fd_m[1], &m) < 0) | |
135 | _exit(127); | |
136 | if (rc == EX_TOOBIG && shovel(0, fd_m[1])) | |
137 | _exit(127); | |
138 | close(fd_m[1]); | |
139 | if (shovel(1, fd_e[1])) | |
140 | _exit(127); | |
141 | close(fd_e[1]); | |
142 | _exit(0); | |
143 | } | |
144 | ||
145 | dup2(fd_m[0], 0); | |
146 | dup2(fd_e[0], 1); | |
147 | close(fd_m[0]); | |
148 | close(fd_e[0]); | |
149 | close(fd_m[1]); | |
150 | close(fd_e[1]); | |
02955332 MW |
151 | qmq = strenv("QMAIL_CHECKSPAM_QUEUE", "/var/qmail/bin/qmail-queue"); |
152 | execlp(qmq, qmq, (char *)0); | |
8d769cc9 | 153 | fprintf(stderr, "failed to exec: %s\n", strerror(errno)); |
154 | return (56); | |
155 | } | |
156 | ||
157 | /*----- That's all, folks -------------------------------------------------*/ |