annotate src/spamass.cpp.in @ 167:9b129ed78d7d stable-6-0-6

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