annotate src/spamass.cpp.in @ 174:da0c41b9f672

don't whitelist addresses with embedded spaces
author carl
date Sun, 23 Sep 2007 11:20:12 -0700
parents bd33eaccfed8
children a4d313c2460b
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
168
6bac960af6b4 add generic reverse dns filtering regex
carl
parents: 167
diff changeset
96 // queue ID
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
97 macro_i = queueid;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
98
168
6bac960af6b4 add generic reverse dns filtering regex
carl
parents: 167
diff changeset
99 // FQDN of this site
6bac960af6b4 add generic reverse dns filtering regex
carl
parents: 167
diff changeset
100 macro_j = getorwarnmacro(ctx, "j", "localhost", "ENVRCPT");
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
101
170
bd33eaccfed8 fix pre/post scripts in rpm spec file
carl
parents: 168
diff changeset
102 // Protocol used to receive the message, not really needed by spam assassin
bd33eaccfed8 fix pre/post scripts in rpm spec file
carl
parents: 168
diff changeset
103 macro_r = "SMTP";
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
104
170
bd33eaccfed8 fix pre/post scripts in rpm spec file
carl
parents: 168
diff changeset
105 // helo value we already have
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
106 macro_s = helo;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
107
170
bd33eaccfed8 fix pre/post scripts in rpm spec file
carl
parents: 168
diff changeset
108 // Sendmail binary version, not really needed by spam assassin
bd33eaccfed8 fix pre/post scripts in rpm spec file
carl
parents: 168
diff changeset
109 macro_v = "8.13.0";
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
110
170
bd33eaccfed8 fix pre/post scripts in rpm spec file
carl
parents: 168
diff changeset
111 // Sendmail .cf version, not really needed by spam assassin
bd33eaccfed8 fix pre/post scripts in rpm spec file
carl
parents: 168
diff changeset
112 macro_Z = "8.13.0";
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
113
168
6bac960af6b4 add generic reverse dns filtering regex
carl
parents: 167
diff changeset
114 // Validated sending site's address
170
bd33eaccfed8 fix pre/post scripts in rpm spec file
carl
parents: 168
diff changeset
115 macro__ = getorwarnmacro(ctx, "_", "unknown", "ENVRCPT");
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
116
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
117 output(string("Received: from ") + macro_s + " (" + macro__+ ")\r\n\t" +
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
118 "by " + macro_j + " (" + macro_v + "/" + macro_Z + ") with " + macro_r + " id " + macro_i + "\r\n\t" +
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
119 "for " + envfrom + ";\r\n\t" +
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
120 macro_b + "\r\n");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
121
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
122 output(string("X-Envelope-From: ") + envfrom + "\r\n");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
123 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
124 output(string("X-Envelope-To: ") + envrcpt + "\r\n");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
125 first_recipient = false;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
126 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
127
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
128
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
129 void SpamAssassin::mlfi_header(char* headerf, char* headerv)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
130 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
131 if (!running) Connect();
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
132 output(spamc_input);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
133 output(headerf);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
134 output(": ");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
135 output(headerv);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
136 output("\r\n");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
137 spamc_input.empty();
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
138 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
139
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
140
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
141 void SpamAssassin::mlfi_eoh()
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
142 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
143 output("\r\n");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
144 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
145
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
146
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
147 void SpamAssassin::mlfi_body(u_char *bodyp, size_t bodylen)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
148 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
149 output((char *)bodyp, bodylen);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
150 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
151
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
152
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
153 int SpamAssassin::mlfi_eom()
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
154 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
155 close_output(); // signal EOF to SpamAssassin
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
156 input(); // read what the Assassin is telling us
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
157 my_syslog(priv, "spamc returned " + spamc_output);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
158 return atoi(spamc_output.c_str());
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
159 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
160
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
161
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
162 void SpamAssassin::Connect()
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
163 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
164 if (error) return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
165 // set up pipes for in- and output
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
166 error |= (pipe(pipe_io[0]));
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
167 error |= (pipe(pipe_io[1]));
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
168 if (error) return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
169
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
170 // now execute SpamAssassin client for contact with SpamAssassin spamd
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
171 // start child process
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
172 pid = fork();
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
173 switch (pid) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
174 case -1:
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
175 // forking trouble.
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
176 my_syslog(priv, "unable to fork for spamc");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
177 error = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
178 close(pipe_io[0][0]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
179 close(pipe_io[0][1]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
180 close(pipe_io[1][0]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
181 close(pipe_io[1][1]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
182 pipe_io[0][0] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
183 pipe_io[0][1] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
184 pipe_io[1][0] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
185 pipe_io[1][1] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
186 return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
187 case 0:
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
188 // +++ CHILD +++
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
189
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
190 // close unused pipes
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
191 close(pipe_io[1][0]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
192 close(pipe_io[0][1]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
193
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
194 // redirect stdin(0), stdout(1) and stderr(2)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
195 dup2(pipe_io[0][0],0);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
196 dup2(pipe_io[1][1],1);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
197 dup2(pipe_io[1][1],2);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
198
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
199 closeall(3);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
200
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
201 // execute spamc
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
202 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
203 argv[0] = spamc;
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
204 argv[1] = "-c";
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
205 argv[2] = NULL;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
206 execvp(argv[0] , argv); // does not return!
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
207 _exit(1); // exec failed
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
208 break;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
209 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
210
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
211 // +++ PARENT +++
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
212
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
213 // close unused pipes
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
214 close(pipe_io[0][0]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
215 close(pipe_io[1][1]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
216 pipe_io[0][0] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
217 pipe_io[1][1] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
218
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
219 // mark the pipes non-blocking
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
220 if (fcntl(pipe_io[0][1], F_SETFL, O_NONBLOCK) == -1)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
221 error = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
222 #if 0 /* don't really need to make the sink pipe nonblocking */
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
223 if (fcntl(pipe_io[1][0], F_SETFL, O_NONBLOCK) == -1)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
224 error = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
225 #endif
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
226
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
227 // we have to assume the client is running now.
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
228 running = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
229 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
230
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
231
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
232 void SpamAssassin::output(const char* buffer, size_t size)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
233 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
234 // if there are problems, fail.
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
235 if (error) return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
236
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
237 if (!running) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
238 // buffer it
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
239 spamc_input.append(buffer, size);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
240 return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
241 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
242
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
243 // send to SpamAssassin
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
244 long total = 0;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
245 long wsize = 0;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
246 string reason;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
247 int status;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
248 do {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
249 struct pollfd fds[2];
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
250 int nfds = 2, nready;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
251 fds[0].fd = pipe_io[0][1];
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
252 fds[0].events = POLLOUT;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
253 fds[0].revents = 0;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
254 fds[1].fd = pipe_io[1][0];
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
255 fds[1].events = POLLIN;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
256 fds[1].revents = 0;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
257
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
258 nready = poll(fds, nfds, 1000);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
259 if (nready == -1) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
260 my_syslog(priv, "poll failed");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
261 error = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
262 return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
263 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
264
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
265 if (fds[1].revents & (POLLERR|POLLNVAL|POLLHUP)) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
266 my_syslog(priv, "poll says my read pipe is busted");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
267 error = true;
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 if (fds[0].revents & (POLLERR|POLLNVAL|POLLHUP)) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
272 my_syslog(priv, "poll says my write pipe is busted");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
273 error = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
274 return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
275 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
276
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
277 if (fds[1].revents & POLLIN) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
278 read_pipe();
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
279 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
280
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
281 if (fds[0].revents & POLLOUT) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
282 switch(wsize = write(pipe_io[0][1], (char *)buffer + total, size - total)) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
283 case -1:
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
284 if (errno == EAGAIN) continue;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
285 reason = string(strerror(errno));
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
286 // close the pipes
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
287 close(pipe_io[0][1]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
288 close(pipe_io[1][0]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
289 pipe_io[0][1] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
290 pipe_io[1][0] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
291 // Slaughter child
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
292 kill(pid, SIGKILL);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
293 // wait for child to terminate
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
294 waitpid(pid, &status, 0);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
295 my_syslog(priv, "write error: " + reason);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
296 error = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
297 running = false;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
298 return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
299 default:
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
300 total += wsize;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
301 break;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
302 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
303 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
304 } while ( total < size );
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
305 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
306
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
307
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
308 void SpamAssassin::output(const char* buffer)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
309 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
310 output(buffer, strlen(buffer));
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
311 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
312
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
313
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
314 void SpamAssassin::output(string buffer)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
315 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
316 output(buffer.c_str(), buffer.size());
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
317 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
318
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
319
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
320 void SpamAssassin::close_output()
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
321 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
322 if (close(pipe_io[0][1]))
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
323 my_syslog(priv, "close error: " + string(strerror(errno)));
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
324 pipe_io[0][1] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
325 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
326
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
327
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
328 void SpamAssassin::input()
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
329 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
330 if (!running || error) return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
331 empty_and_close_pipe();
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
332 if (running) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
333 // wait until child is dead
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
334 int status;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
335 if (waitpid(pid, &status, 0) < 0) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
336 error = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
337 };
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
338 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
339 running = false;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
340 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
341
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
342
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
343 int SpamAssassin::read_pipe()
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
344 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
345 long size;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
346 int status;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
347 char iobuff[1024];
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
348 string reason;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
349
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
350 if (pipe_io[1][0] == -1) return 0;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
351
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
352 size = read(pipe_io[1][0], iobuff, 1024);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
353
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
354 if (size < 0) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
355 reason = string(strerror(errno));
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
356 // Close remaining pipe.
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
357 close(pipe_io[1][0]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
358 pipe_io[1][0] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
359 // Slaughter child
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
360 kill(pid, SIGKILL);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
361 // wait for child to terminate
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
362 waitpid(pid, &status, 0);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
363 my_syslog(priv, "read error: " + reason);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
364 size = 0;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
365 error = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
366 running = false;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
367 } else if (size == 0) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
368 // EOF. Close the pipe
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
369 if (close(pipe_io[1][0])) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
370 error = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
371 my_syslog(priv, "close error: " + string(strerror(errno)));
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
372 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
373 pipe_io[1][0] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
374 } else {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
375 // append to mail buffer
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
376 spamc_output.append(iobuff, size);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
377 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
378 return size;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
379 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
380
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
381
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
382 void SpamAssassin::empty_and_close_pipe()
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
383 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
384 while (read_pipe())
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
385 ;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
386 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
387
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
388
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
389 void SpamAssassin::closeall(int fd)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
390 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
391 int fdlimit = sysconf(_SC_OPEN_MAX);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
392 while (fd < fdlimit)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
393 close(fd++);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
394 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
395
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
396
168
6bac960af6b4 add generic reverse dns filtering regex
carl
parents: 167
diff changeset
397 char *SpamAssassin::getorwarnmacro(SMFICTX *ctx, char *macro, char *def, char *scope)
6bac960af6b4 add generic reverse dns filtering regex
carl
parents: 167
diff changeset
398 {
6bac960af6b4 add generic reverse dns filtering regex
carl
parents: 167
diff changeset
399 char *rc = smfi_getsymval(ctx, macro);
6bac960af6b4 add generic reverse dns filtering regex
carl
parents: 167
diff changeset
400 if (!rc) {
6bac960af6b4 add generic reverse dns filtering regex
carl
parents: 167
diff changeset
401 rc = def;
6bac960af6b4 add generic reverse dns filtering regex
carl
parents: 167
diff changeset
402 warnmacro(macro, scope);
6bac960af6b4 add generic reverse dns filtering regex
carl
parents: 167
diff changeset
403 }
6bac960af6b4 add generic reverse dns filtering regex
carl
parents: 167
diff changeset
404 return rc;
6bac960af6b4 add generic reverse dns filtering regex
carl
parents: 167
diff changeset
405 }
6bac960af6b4 add generic reverse dns filtering regex
carl
parents: 167
diff changeset
406
6bac960af6b4 add generic reverse dns filtering regex
carl
parents: 167
diff changeset
407
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
408 void SpamAssassin::warnmacro(char *macro, char *scope)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
409 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
410 if (warnedmacro) return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
411 char buf[maxlen];
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
412 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
413 my_syslog(priv, buf);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
414 warnedmacro = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
415 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
416