Debianization.
[misc] / qmail-checkspam.c
1 /* -*-c-*-
2 *
3 * $Id: qmail-checkspam.c,v 1.1 2003/10/01 00:08:57 mdw Exp $
4 *
5 * Filter messages for spam
6 *
7 * (c) 2003 Mark Wooding
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
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.
16 *
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.
21 *
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
27 /*----- Revision history --------------------------------------------------*
28 *
29 * $Log: qmail-checkspam.c,v $
30 * Revision 1.1 2003/10/01 00:08:57 mdw
31 * Collection of miscellaneous ill-documented tools.
32 *
33 */
34
35 /*----- Header files ------------------------------------------------------*/
36
37 #include <ctype.h>
38 #include <errno.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42
43 #include <sys/types.h>
44 #include <sys/unistd.h>
45
46 #include <libspamc.h>
47
48 /*----- Main code ---------------------------------------------------------*/
49
50 static const char *strenv(const char *e, const char *d)
51 {
52 const char *p = getenv(e);
53 if (!p) return (d);
54 return (p);
55 }
56
57 static double dblenv(const char *e, double d)
58 {
59 const char *p = getenv(e);
60 char *q;
61 int err = errno;
62 double f;
63 if (!p) return (d);
64 errno = 0;
65 f = strtod(p, &q);
66 if (errno) return (d);
67 errno = err;
68 return (f);
69 }
70
71 static int intenv(const char *e, int d)
72 {
73 const char *p = getenv(e);
74 char *q;
75 int err = errno;
76 long l;
77 if (!p) return (d);
78 errno = 0;
79 l = strtol(p, &q, 0);
80 if (errno) return (d);
81 errno = err;
82 if (l < 0 || l > INT_MAX) return (d);
83 return ((int)l);
84 }
85
86 int shovel(int from, int to)
87 {
88 char buf[4096];
89 ssize_t n;
90 char *p;
91 size_t r;
92
93 for (;;) {
94 n = read(from, buf, sizeof(buf));
95 if (n < 0 && errno != EINTR && errno != EAGAIN)
96 return (-1);
97 else if (!n)
98 return (0);
99 p = buf;
100 r = n;
101 while (r) {
102 n = write(to, p, n);
103 if (n <= 0 && errno != EINTR && errno != EAGAIN)
104 return (-1);
105 r -= n;
106 p += n;
107 }
108 }
109 }
110
111 int main(int argc, char *argv[])
112 {
113 struct sockaddr sa;
114 struct message m;
115 int fd_m[2], fd_e[2];
116 pid_t kid;
117 int rc;
118
119 m.max_len = intenv("QMAIL_CHECKSPAM_MAXLEN", 2 * 1024 * 1024);
120 m.timeout = intenv("QMAIL_CHECKSPAM_TIMEOUT", 300);
121 rc = message_read(0, 0, &m);
122 if (rc != 0 && rc != EX_TOOBIG)
123 return (54);
124 if (!rc) {
125 if (lookup_host(strenv("QMAIL_CHECKSPAM_SPAMDHOST", "localhost"),
126 intenv("QMAIL_CHECKSPAM_SPAMDPORT", 783),
127 &sa))
128 return (56);
129 if (message_filter(&sa, "spamd", 0, &m))
130 return (74);
131 if (m.score >= dblenv("QMAIL_CHECKSPAM_THRESH", m.threshold))
132 return (31);
133 }
134 if (pipe(fd_m) || pipe(fd_e))
135 return (56);
136 if ((kid = fork()) < 0)
137 return (56);
138 if (!kid) {
139 close(fd_m[0]);
140 close(fd_e[0]);
141 if (message_write(fd_m[1], &m) < 0)
142 _exit(127);
143 if (rc == EX_TOOBIG && shovel(0, fd_m[1]))
144 _exit(127);
145 close(fd_m[1]);
146 if (shovel(1, fd_e[1]))
147 _exit(127);
148 close(fd_e[1]);
149 _exit(0);
150 }
151
152 dup2(fd_m[0], 0);
153 dup2(fd_e[0], 1);
154 close(fd_m[0]);
155 close(fd_e[0]);
156 close(fd_m[1]);
157 close(fd_e[1]);
158 execlp(strenv("QMAIL_CHECKSPAM_QUEUE", "/var/qmail/bin/qmail-queue"),
159 (char *)0);
160 fprintf(stderr, "failed to exec: %s\n", strerror(errno));
161 return (56);
162 }
163
164 /*----- That's all, folks -------------------------------------------------*/