annotate src/spamass.cpp.in @ 163:97d7da45fe2a

spamassassin changes
author carl
date Sun, 26 Aug 2007 19:03:17 -0700
parents
children 5809bcdc325b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
1 /*
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
2
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
3 Copyright (c) 2007 Carl Byington - 510 Software Group, released under
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
4 the GPL version 3 or any later version at your choice available at
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
5 http://www.gnu.org/licenses/gpl-3.0.txt
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
6
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
7 Based on spamass-milter by Georg C. F. Greve <greve@gnu.org>
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
8
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
9 */
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
10
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
11 #include "config.h"
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
12 #include "dnsbl.h"
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
13 #include <errno.h>
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
14 #include <fcntl.h>
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
15 #include <poll.h>
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
16 #include <signal.h>
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
17 #include <string>
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
18 #include <sys/types.h>
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
19 #include <sys/wait.h>
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
20 #include <unistd.h>
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
21
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
22
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
23 static const char Id[] = "$Id$";
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
24
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
25 bool warnedmacro = false; /* have we logged that we couldn't fetch a macro? */
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
26 const int maxlen = 1000; // used for snprintf buffers
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
27
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
28
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
29 SpamAssassin::SpamAssassin()
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
30 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
31 error = false;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
32 running = false;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
33 first_recipient = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
34 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
35
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
36
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
37 SpamAssassin::~SpamAssassin()
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
38 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
39 // close all pipes that are still open
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
40 if (pipe_io[0][0] > -1) close(pipe_io[0][0]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
41 if (pipe_io[0][1] > -1) close(pipe_io[0][1]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
42 if (pipe_io[1][0] > -1) close(pipe_io[1][0]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
43 if (pipe_io[1][1] > -1) close(pipe_io[1][1]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
44 // child still running?
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
45 if (running) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
46 // make sure the pid is valid
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
47 if (pid > 0) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
48 // slaughter child
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
49 kill(pid, SIGKILL);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
50 // wait for child to terminate
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
51 int status;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
52 waitpid(pid, &status, 0);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
53 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
54 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
55 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
56
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
57
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
58 void SpamAssassin::mlfi_connect(mlfiPriv *priv_, int ip)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
59 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
60 priv = priv_;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
61 ip4 = ip;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
62 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
63
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
64
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
65 void SpamAssassin::mlfi_helo(char *helohost)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
66 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
67 helo = helohost;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
68 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
69
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
70
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
71 void SpamAssassin::mlfi_envfrom(char *from, char *qid)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
72 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
73 envfrom = from;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
74 queueid = qid;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
75 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
76
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
77
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
78 void SpamAssassin::mlfi_envrcpt(SMFICTX *ctx, char *envrcpt)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
79 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
80 if (first_recipient) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
81 /* Send the envelope headers as X-Envelope-From: and
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
82 X-Envelope-To: so that SpamAssassin can use them in its
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
83 whitelist checks. Also forge as complete a dummy
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
84 Received: header as possible because SA gets a lot of
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
85 info from it.
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
86
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
87 HReceived: $?sfrom $s $.$?_($?s$|from $.$_)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
88 $.$?{auth_type}(authenticated$?{auth_ssf} bits=${auth_ssf}$.)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
89 $.by $j ($v/$Z)$?r with $r$. id $i$?{tls_version}
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
90 (version=${tls_version} cipher=${cipher} bits=${cipher_bits} verify=${verify})$.$?u
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
91 for $u; $|;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
92 $.$b$?g
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
93 (envelope-from $g)$.
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
94
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
95 */
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
96 const char *macro_b, *macro_i, *macro_j, *macro_r,
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
97 *macro_s, *macro_v, *macro_Z, *macro__;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
98 char date[32];
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
99 time_t tval;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
100 time(&tval);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
101 strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S %z", localtime(&tval));
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
102 macro_b = date;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
103
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
104 /* queue ID */
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
105 macro_i = queueid;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
106
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
107 /* FQDN of this site */
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
108 macro_j = smfi_getsymval(ctx, "j");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
109 if (!macro_j) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
110 macro_j = "localhost";
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
111 warnmacro("j", "ENVRCPT");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
112 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
113
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
114 /* Protocol used to receive the message */
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
115 macro_r = smfi_getsymval(ctx, "r");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
116 if (!macro_r) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
117 macro_r = "SMTP";
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
118 warnmacro("r", "ENVRCPT");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
119 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
120
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
121 macro_s = helo;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
122
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
123 /* Sendmail binary version */
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
124 macro_v = smfi_getsymval(ctx, "v");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
125 if (!macro_v) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
126 macro_v = "8.13.0";
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
127 warnmacro("v", "ENVRCPT");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
128 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
129
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
130 /* Sendmail .cf version */
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
131 macro_Z = smfi_getsymval(ctx, "Z");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
132 if (!macro_Z) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
133 macro_Z = "8.13.0";
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
134 warnmacro("Z", "ENVRCPT");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
135 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
136
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
137 /* Validated sending site's address */
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
138 macro__ = smfi_getsymval(ctx, "_");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
139 if (!macro__) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
140 macro__ = "unknown";
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
141 warnmacro("_", "ENVRCPT");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
142 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
143
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
144 output(string("Received: from ") + macro_s + " (" + macro__+ ")\r\n\t" +
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
145 "by " + macro_j + " (" + macro_v + "/" + macro_Z + ") with " + macro_r + " id " + macro_i + "\r\n\t" +
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
146 "for " + envfrom + ";\r\n\t" +
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
147 macro_b + "\r\n");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
148
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
149 output(string("X-Envelope-From: ") + envfrom + "\r\n");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
150 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
151 output(string("X-Envelope-To: ") + envrcpt + "\r\n");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
152 first_recipient = false;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
153 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
154
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
155
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
156 void SpamAssassin::mlfi_header(char* headerf, char* headerv)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
157 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
158 if (!running) Connect();
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
159 output(spamc_input);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
160 output(headerf);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
161 output(": ");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
162 output(headerv);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
163 output("\r\n");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
164 spamc_input.empty();
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
165 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
166
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
167
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
168 void SpamAssassin::mlfi_eoh()
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
169 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
170 output("\r\n");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
171 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
172
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
173
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
174 void SpamAssassin::mlfi_body(u_char *bodyp, size_t bodylen)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
175 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
176 output((char *)bodyp, bodylen);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
177 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
178
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
179
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
180 int SpamAssassin::mlfi_eom()
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
181 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
182 close_output(); // signal EOF to SpamAssassin
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
183 input(); // read what the Assassin is telling us
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
184 my_syslog(priv, "spamc returned " + spamc_output);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
185 return atoi(spamc_output.c_str());
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
186 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
187
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
188
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
189 void SpamAssassin::Connect()
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
190 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
191 if (error) return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
192 // set up pipes for in- and output
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
193 error |= (pipe(pipe_io[0]));
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
194 error |= (pipe(pipe_io[1]));
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
195 if (error) return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
196
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
197 // now execute SpamAssassin client for contact with SpamAssassin spamd
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
198 // start child process
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
199 pid = fork();
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
200 switch (pid) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
201 case -1:
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
202 // forking trouble.
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
203 my_syslog(priv, "unable to fork for spamc");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
204 error = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
205 close(pipe_io[0][0]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
206 close(pipe_io[0][1]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
207 close(pipe_io[1][0]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
208 close(pipe_io[1][1]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
209 pipe_io[0][0] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
210 pipe_io[0][1] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
211 pipe_io[1][0] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
212 pipe_io[1][1] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
213 return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
214 case 0:
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
215 // +++ CHILD +++
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
216
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
217 // close unused pipes
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
218 close(pipe_io[1][0]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
219 close(pipe_io[0][1]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
220
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
221 // redirect stdin(0), stdout(1) and stderr(2)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
222 dup2(pipe_io[0][0],0);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
223 dup2(pipe_io[1][1],1);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
224 dup2(pipe_io[1][1],2);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
225
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
226 closeall(3);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
227
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
228 // execute spamc
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
229 char* argv[3];
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
230 argv[0] = "@SPAMC@";
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
231 argv[0] = "/usr/bin/testspamc";
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
232 argv[1] = "-c";
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
233 argv[2] = NULL;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
234 execvp(argv[0] , argv); // does not return!
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
235 _exit(1); // exec failed
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
236 break;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
237 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
238
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
239 // +++ PARENT +++
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
240
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
241 // close unused pipes
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
242 close(pipe_io[0][0]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
243 close(pipe_io[1][1]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
244 pipe_io[0][0] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
245 pipe_io[1][1] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
246
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
247 // mark the pipes non-blocking
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
248 if (fcntl(pipe_io[0][1], F_SETFL, O_NONBLOCK) == -1)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
249 error = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
250 #if 0 /* don't really need to make the sink pipe nonblocking */
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
251 if (fcntl(pipe_io[1][0], F_SETFL, O_NONBLOCK) == -1)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
252 error = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
253 #endif
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
254
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
255 // we have to assume the client is running now.
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
256 running = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
257 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
258
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
259
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
260 void SpamAssassin::output(const char* buffer, size_t size)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
261 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
262 // if there are problems, fail.
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
263 if (error) return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
264
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
265 if (!running) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
266 // buffer it
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
267 spamc_input.append(buffer, size);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
268 return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
269 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
270
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
271 // send to SpamAssassin
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
272 long total = 0;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
273 long wsize = 0;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
274 string reason;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
275 int status;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
276 do {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
277 struct pollfd fds[2];
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
278 int nfds = 2, nready;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
279 fds[0].fd = pipe_io[0][1];
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
280 fds[0].events = POLLOUT;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
281 fds[0].revents = 0;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
282 fds[1].fd = pipe_io[1][0];
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
283 fds[1].events = POLLIN;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
284 fds[1].revents = 0;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
285
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
286 nready = poll(fds, nfds, 1000);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
287 if (nready == -1) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
288 my_syslog(priv, "poll failed");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
289 error = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
290 return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
291 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
292
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
293 if (fds[1].revents & (POLLERR|POLLNVAL|POLLHUP)) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
294 my_syslog(priv, "poll says my read pipe is busted");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
295 error = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
296 return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
297 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
298
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
299 if (fds[0].revents & (POLLERR|POLLNVAL|POLLHUP)) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
300 my_syslog(priv, "poll says my write pipe is busted");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
301 error = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
302 return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
303 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
304
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
305 if (fds[1].revents & POLLIN) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
306 read_pipe();
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
307 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
308
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
309 if (fds[0].revents & POLLOUT) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
310 switch(wsize = write(pipe_io[0][1], (char *)buffer + total, size - total)) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
311 case -1:
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
312 if (errno == EAGAIN) continue;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
313 reason = string(strerror(errno));
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
314 // close the pipes
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
315 close(pipe_io[0][1]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
316 close(pipe_io[1][0]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
317 pipe_io[0][1] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
318 pipe_io[1][0] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
319 // Slaughter child
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
320 kill(pid, SIGKILL);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
321 // wait for child to terminate
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
322 waitpid(pid, &status, 0);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
323 my_syslog(priv, "write error: " + reason);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
324 error = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
325 running = false;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
326 return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
327 default:
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
328 total += wsize;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
329 break;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
330 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
331 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
332 } while ( total < size );
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
333 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
334
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
335
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
336 void SpamAssassin::output(const char* buffer)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
337 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
338 output(buffer, strlen(buffer));
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
339 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
340
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
341
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
342 void SpamAssassin::output(string buffer)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
343 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
344 output(buffer.c_str(), buffer.size());
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
345 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
346
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
347
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
348 void SpamAssassin::close_output()
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
349 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
350 if (close(pipe_io[0][1]))
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
351 my_syslog(priv, "close error: " + string(strerror(errno)));
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
352 pipe_io[0][1] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
353 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
354
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
355
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
356 void SpamAssassin::input()
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
357 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
358 if (!running || error) return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
359 empty_and_close_pipe();
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
360 if (running) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
361 // wait until child is dead
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
362 int status;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
363 if (waitpid(pid, &status, 0) < 0) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
364 error = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
365 };
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
366 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
367 running = false;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
368 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
369
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
370
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
371 int SpamAssassin::read_pipe()
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
372 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
373 long size;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
374 int status;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
375 char iobuff[1024];
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
376 string reason;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
377
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
378 if (pipe_io[1][0] == -1) return 0;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
379
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
380 size = read(pipe_io[1][0], iobuff, 1024);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
381
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
382 if (size < 0) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
383 reason = string(strerror(errno));
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
384 // Close remaining pipe.
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
385 close(pipe_io[1][0]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
386 pipe_io[1][0] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
387 // Slaughter child
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
388 kill(pid, SIGKILL);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
389 // wait for child to terminate
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
390 waitpid(pid, &status, 0);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
391 my_syslog(priv, "read error: " + reason);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
392 size = 0;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
393 error = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
394 running = false;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
395 } else if (size == 0) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
396 // EOF. Close the pipe
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
397 if (close(pipe_io[1][0])) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
398 error = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
399 my_syslog(priv, "close error: " + string(strerror(errno)));
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
400 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
401 pipe_io[1][0] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
402 } else {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
403 // append to mail buffer
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
404 spamc_output.append(iobuff, size);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
405 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
406 return size;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
407 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
408
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
409
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
410 void SpamAssassin::empty_and_close_pipe()
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
411 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
412 while (read_pipe())
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
413 ;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
414 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
415
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
416
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
417 void SpamAssassin::closeall(int fd)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
418 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
419 int fdlimit = sysconf(_SC_OPEN_MAX);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
420 while (fd < fdlimit)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
421 close(fd++);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
422 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
423
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
424
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
425 void SpamAssassin::warnmacro(char *macro, char *scope)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
426 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
427 if (warnedmacro) return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
428 char buf[maxlen];
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
429 snprintf(buf, sizeof(buf), "Could not retrieve sendmail macro %s. Add it to confMILTER_MACROS_%s for better results.", macro, scope);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
430 my_syslog(priv, buf);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
431 warnedmacro = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
432 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
433