annotate src/spamass.cpp.in @ 272:a99b6c1f5f67

Code cleanup, increase minimum hostname length for uribl checking
author Carl Byington <carl@five-ten-sg.com>
date Mon, 09 Sep 2013 19:30:21 -0700
parents 536d59c3a7b8
children 78eedbbce636
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
177
a4d313c2460b start embedded dcc filtering
carl
parents: 170
diff changeset
11 #include "includes.h"
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
12 #include <errno.h>
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
13 #include <fcntl.h>
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
14 #include <poll.h>
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
15 #include <signal.h>
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
16 #include <string>
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
17 #include <sys/types.h>
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
18 #include <sys/wait.h>
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
19 #include <unistd.h>
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
20
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
21
214
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
22 const char *spamc = "@SPAMC@";
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
23 const char *spamc_empty = "";
177
a4d313c2460b start embedded dcc filtering
carl
parents: 170
diff changeset
24 static bool warnedmacro = false; // have we logged that we couldn't fetch a macro?
a4d313c2460b start embedded dcc filtering
carl
parents: 170
diff changeset
25 const int maxlen = 1000; // used for snprintf buffers
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
26
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
27
214
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
28 SpamAssassin::SpamAssassin(mlfiPriv *priv_, int ip, const char *helo_, const char *from, const char *qid)
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
29 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
30 error = false;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
31 running = false;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
32 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
33 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
34 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
35 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
36 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
37 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
38 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
39 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
40 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
41 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
42 pipe_io[1][1] = -1;
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
43 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
44
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
45
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
46 SpamAssassin::~SpamAssassin()
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
47 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
48 // close all pipes that are still open
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
49 if (pipe_io[0][0] > -1) close(pipe_io[0][0]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
50 if (pipe_io[0][1] > -1) close(pipe_io[0][1]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
51 if (pipe_io[1][0] > -1) close(pipe_io[1][0]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
52 if (pipe_io[1][1] > -1) close(pipe_io[1][1]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
53 // child still running?
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
54 if (running) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
55 // make sure the pid is valid
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
56 if (pid > 0) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
57 // slaughter child
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
58 kill(pid, SIGKILL);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
59 // wait for child to terminate
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
60 int status;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
61 waitpid(pid, &status, 0);
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
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
66
214
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
67 void SpamAssassin::mlfi_envrcpt(SMFICTX *ctx, const char *envrcpt)
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
68 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
69 if (first_recipient) {
177
a4d313c2460b start embedded dcc filtering
carl
parents: 170
diff changeset
70 first_recipient = false;
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
71 /* Send the envelope headers as X-Envelope-From: and
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
72 X-Envelope-To: so that SpamAssassin can use them in its
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
73 whitelist checks. Also forge as complete a dummy
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
74 Received: header as possible because SA gets a lot of
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
75 info from it.
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
76
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
77 HReceived: $?sfrom $s $.$?_($?s$|from $.$_)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
78 $.$?{auth_type}(authenticated$?{auth_ssf} bits=${auth_ssf}$.)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
79 $.by $j ($v/$Z)$?r with $r$. id $i$?{tls_version}
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
80 (version=${tls_version} cipher=${cipher} bits=${cipher_bits} verify=${verify})$.$?u
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
81 for $u; $|;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
82 $.$b$?g
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
83 (envelope-from $g)$.
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
84
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
85 */
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
86 const char *macro_b, *macro_i, *macro_j, *macro_r,
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
87 *macro_s, *macro_v, *macro_Z, *macro__;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
88 char date[32];
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
89 time_t tval;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
90 time(&tval);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
91 strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S %z", localtime(&tval));
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
92 macro_b = date;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
93
168
6bac960af6b4 add generic reverse dns filtering regex
carl
parents: 167
diff changeset
94 // queue ID
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
95 macro_i = queueid;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
96
168
6bac960af6b4 add generic reverse dns filtering regex
carl
parents: 167
diff changeset
97 // FQDN of this site
6bac960af6b4 add generic reverse dns filtering regex
carl
parents: 167
diff changeset
98 macro_j = getorwarnmacro(ctx, "j", "localhost", "ENVRCPT");
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
99
170
bd33eaccfed8 fix pre/post scripts in rpm spec file
carl
parents: 168
diff changeset
100 // 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
101 macro_r = "SMTP";
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
102
170
bd33eaccfed8 fix pre/post scripts in rpm spec file
carl
parents: 168
diff changeset
103 // helo value we already have
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
104 macro_s = helo;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
105
170
bd33eaccfed8 fix pre/post scripts in rpm spec file
carl
parents: 168
diff changeset
106 // Sendmail binary version, not really needed by spam assassin
bd33eaccfed8 fix pre/post scripts in rpm spec file
carl
parents: 168
diff changeset
107 macro_v = "8.13.0";
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
108
170
bd33eaccfed8 fix pre/post scripts in rpm spec file
carl
parents: 168
diff changeset
109 // Sendmail .cf version, not really needed by spam assassin
bd33eaccfed8 fix pre/post scripts in rpm spec file
carl
parents: 168
diff changeset
110 macro_Z = "8.13.0";
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
111
168
6bac960af6b4 add generic reverse dns filtering regex
carl
parents: 167
diff changeset
112 // Validated sending site's address
170
bd33eaccfed8 fix pre/post scripts in rpm spec file
carl
parents: 168
diff changeset
113 macro__ = getorwarnmacro(ctx, "_", "unknown", "ENVRCPT");
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
114
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
115 output(string("Received: from ") + macro_s + " (" + macro__+ ")\r\n\t" +
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
116 "by " + macro_j + " (" + macro_v + "/" + macro_Z + ") with " + macro_r + " id " + macro_i + "\r\n\t" +
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
117 "for " + envfrom + ";\r\n\t" +
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
118 macro_b + "\r\n");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
119
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
120 output(string("X-Envelope-From: ") + envfrom + "\r\n");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
121 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
122 output(string("X-Envelope-To: ") + envrcpt + "\r\n");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
123 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
124
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
125
214
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
126 void SpamAssassin::mlfi_header(const char* headerf, const char* headerv)
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
127 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
128 if (!running) Connect();
177
a4d313c2460b start embedded dcc filtering
carl
parents: 170
diff changeset
129 if (running) {
a4d313c2460b start embedded dcc filtering
carl
parents: 170
diff changeset
130 output(spamc_input);
227
3fee608becbc Fixes to compile on old systems without memrchr or string::clear().
Carl Byington <carl@five-ten-sg.com>
parents: 214
diff changeset
131 spamc_input = "";
177
a4d313c2460b start embedded dcc filtering
carl
parents: 170
diff changeset
132 }
a4d313c2460b start embedded dcc filtering
carl
parents: 170
diff changeset
133
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
134 output(headerf);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
135 output(": ");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
136 output(headerv);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
137 output("\r\n");
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
214
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
147 void SpamAssassin::mlfi_body(const u_char *bodyp, size_t bodylen)
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
148 {
214
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
149 output((const char *)bodyp, bodylen);
163
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];
214
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
203 argv[0] = (char*)spamc;
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
204 argv[1] = (char*)"-c";
163
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
214
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
244 size_t total = 0;
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
245 size_t wsize = 0;
163
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
240
536d59c3a7b8 pollnval might be triggered on the last pollin data chunk
Carl Byington <carl@five-ten-sg.com>
parents: 227
diff changeset
265 if (fds[1].revents & (POLLERR|POLLNVAL)) {
163
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
214
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
397 const char *SpamAssassin::getorwarnmacro(SMFICTX *ctx, const char *macro, const char *def, const char *scope)
168
6bac960af6b4 add generic reverse dns filtering regex
carl
parents: 167
diff changeset
398 {
214
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
399 const char *rc = smfi_getsymval(ctx, (char*)macro);
168
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
214
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
408 void SpamAssassin::warnmacro(const char *macro, const char *scope)
163
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