annotate src/spamass.cpp.in @ 184:0e15a805d295 stable-6-0-11

embedded dcc filtering
author carl
date Sun, 07 Oct 2007 09:21:50 -0700
parents a4d313c2460b
children 92a5c866bdfa
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
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
22 static const char Id[] = "$Id$";
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
23
167
9b129ed78d7d actually use spamassassin result, allow build without spam assassin, only call it if some recipient needs it.
carl
parents: 164
diff changeset
24 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
25 char *spamc_empty = "";
177
a4d313c2460b start embedded dcc filtering
carl
parents: 170
diff changeset
26 static bool warnedmacro = false; // have we logged that we couldn't fetch a macro?
a4d313c2460b start embedded dcc filtering
carl
parents: 170
diff changeset
27 const int maxlen = 1000; // used for snprintf buffers
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
28
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
29
167
9b129ed78d7d actually use spamassassin result, allow build without spam assassin, only call it if some recipient needs it.
carl
parents: 164
diff changeset
30 SpamAssassin::SpamAssassin(mlfiPriv *priv_, int ip, char *helo_, char *from, char *qid)
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
31 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
32 error = false;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
33 running = false;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
34 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
35 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
36 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
37 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
38 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
39 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
40 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
41 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
42 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
43 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
44 pipe_io[1][1] = -1;
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
45 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
46
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
47
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
48 SpamAssassin::~SpamAssassin()
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
49 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
50 // close all pipes that are still open
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
51 if (pipe_io[0][0] > -1) close(pipe_io[0][0]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
52 if (pipe_io[0][1] > -1) close(pipe_io[0][1]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
53 if (pipe_io[1][0] > -1) close(pipe_io[1][0]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
54 if (pipe_io[1][1] > -1) close(pipe_io[1][1]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
55 // child still running?
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
56 if (running) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
57 // make sure the pid is valid
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
58 if (pid > 0) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
59 // slaughter child
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
60 kill(pid, SIGKILL);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
61 // wait for child to terminate
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
62 int status;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
63 waitpid(pid, &status, 0);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
64 }
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 void SpamAssassin::mlfi_envrcpt(SMFICTX *ctx, char *envrcpt)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
70 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
71 if (first_recipient) {
177
a4d313c2460b start embedded dcc filtering
carl
parents: 170
diff changeset
72 first_recipient = false;
163
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 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
126
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
127
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
128 void SpamAssassin::mlfi_header(char* headerf, char* headerv)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
129 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
130 if (!running) Connect();
177
a4d313c2460b start embedded dcc filtering
carl
parents: 170
diff changeset
131 if (running) {
a4d313c2460b start embedded dcc filtering
carl
parents: 170
diff changeset
132 output(spamc_input);
a4d313c2460b start embedded dcc filtering
carl
parents: 170
diff changeset
133 spamc_input.empty();
a4d313c2460b start embedded dcc filtering
carl
parents: 170
diff changeset
134 }
a4d313c2460b start embedded dcc filtering
carl
parents: 170
diff changeset
135
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
136 output(headerf);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
137 output(": ");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
138 output(headerv);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
139 output("\r\n");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
140 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
141
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
142
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
143 void SpamAssassin::mlfi_eoh()
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
144 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
145 output("\r\n");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
146 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
147
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
148
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
149 void SpamAssassin::mlfi_body(u_char *bodyp, size_t bodylen)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
150 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
151 output((char *)bodyp, bodylen);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
152 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
153
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
154
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
155 int SpamAssassin::mlfi_eom()
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
156 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
157 close_output(); // signal EOF to SpamAssassin
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
158 input(); // read what the Assassin is telling us
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
159 my_syslog(priv, "spamc returned " + spamc_output);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
160 return atoi(spamc_output.c_str());
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
161 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
162
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
164 void SpamAssassin::Connect()
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
165 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
166 if (error) return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
167 // set up pipes for in- and output
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
168 error |= (pipe(pipe_io[0]));
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
169 error |= (pipe(pipe_io[1]));
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
170 if (error) return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
171
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
172 // now execute SpamAssassin client for contact with SpamAssassin spamd
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
173 // start child process
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
174 pid = fork();
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
175 switch (pid) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
176 case -1:
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
177 // forking trouble.
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
178 my_syslog(priv, "unable to fork for spamc");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
179 error = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
180 close(pipe_io[0][0]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
181 close(pipe_io[0][1]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
182 close(pipe_io[1][0]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
183 close(pipe_io[1][1]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
184 pipe_io[0][0] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
185 pipe_io[0][1] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
186 pipe_io[1][0] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
187 pipe_io[1][1] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
188 return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
189 case 0:
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
190 // +++ CHILD +++
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
191
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
192 // close unused pipes
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
193 close(pipe_io[1][0]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
194 close(pipe_io[0][1]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
195
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
196 // redirect stdin(0), stdout(1) and stderr(2)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
197 dup2(pipe_io[0][0],0);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
198 dup2(pipe_io[1][1],1);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
199 dup2(pipe_io[1][1],2);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
200
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
201 closeall(3);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
202
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
203 // execute spamc
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
204 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
205 argv[0] = spamc;
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
206 argv[1] = "-c";
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
207 argv[2] = NULL;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
208 execvp(argv[0] , argv); // does not return!
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
209 _exit(1); // exec failed
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
210 break;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
211 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
212
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
213 // +++ PARENT +++
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
214
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
215 // close unused pipes
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
216 close(pipe_io[0][0]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
217 close(pipe_io[1][1]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
218 pipe_io[0][0] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
219 pipe_io[1][1] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
220
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
221 // mark the pipes non-blocking
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
222 if (fcntl(pipe_io[0][1], F_SETFL, O_NONBLOCK) == -1)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
223 error = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
224 #if 0 /* don't really need to make the sink pipe nonblocking */
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
225 if (fcntl(pipe_io[1][0], F_SETFL, O_NONBLOCK) == -1)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
226 error = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
227 #endif
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
228
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
229 // we have to assume the client is running now.
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
230 running = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
231 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
232
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
233
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
234 void SpamAssassin::output(const char* buffer, size_t size)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
235 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
236 // if there are problems, fail.
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
237 if (error) return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
238
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
239 if (!running) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
240 // buffer it
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
241 spamc_input.append(buffer, size);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
242 return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
243 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
244
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
245 // send to SpamAssassin
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
246 long total = 0;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
247 long wsize = 0;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
248 string reason;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
249 int status;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
250 do {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
251 struct pollfd fds[2];
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
252 int nfds = 2, nready;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
253 fds[0].fd = pipe_io[0][1];
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
254 fds[0].events = POLLOUT;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
255 fds[0].revents = 0;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
256 fds[1].fd = pipe_io[1][0];
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
257 fds[1].events = POLLIN;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
258 fds[1].revents = 0;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
259
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
260 nready = poll(fds, nfds, 1000);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
261 if (nready == -1) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
262 my_syslog(priv, "poll failed");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
263 error = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
264 return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
265 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
266
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
267 if (fds[1].revents & (POLLERR|POLLNVAL|POLLHUP)) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
268 my_syslog(priv, "poll says my read pipe is busted");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
269 error = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
270 return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
271 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
272
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
273 if (fds[0].revents & (POLLERR|POLLNVAL|POLLHUP)) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
274 my_syslog(priv, "poll says my write pipe is busted");
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
275 error = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
276 return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
277 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
278
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
279 if (fds[1].revents & POLLIN) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
280 read_pipe();
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
281 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
282
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
283 if (fds[0].revents & POLLOUT) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
284 switch(wsize = write(pipe_io[0][1], (char *)buffer + total, size - total)) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
285 case -1:
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
286 if (errno == EAGAIN) continue;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
287 reason = string(strerror(errno));
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
288 // close the pipes
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
289 close(pipe_io[0][1]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
290 close(pipe_io[1][0]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
291 pipe_io[0][1] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
292 pipe_io[1][0] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
293 // Slaughter child
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
294 kill(pid, SIGKILL);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
295 // wait for child to terminate
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
296 waitpid(pid, &status, 0);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
297 my_syslog(priv, "write error: " + reason);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
298 error = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
299 running = false;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
300 return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
301 default:
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
302 total += wsize;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
303 break;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
304 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
305 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
306 } while ( total < size );
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
307 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
308
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
309
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
310 void SpamAssassin::output(const char* buffer)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
311 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
312 output(buffer, strlen(buffer));
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
313 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
314
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
315
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
316 void SpamAssassin::output(string buffer)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
317 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
318 output(buffer.c_str(), buffer.size());
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
319 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
320
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
321
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
322 void SpamAssassin::close_output()
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
323 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
324 if (close(pipe_io[0][1]))
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
325 my_syslog(priv, "close error: " + string(strerror(errno)));
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
326 pipe_io[0][1] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
327 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
328
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
329
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
330 void SpamAssassin::input()
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
331 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
332 if (!running || error) return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
333 empty_and_close_pipe();
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
334 if (running) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
335 // wait until child is dead
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
336 int status;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
337 if (waitpid(pid, &status, 0) < 0) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
338 error = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
339 };
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
340 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
341 running = false;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
342 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
343
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
344
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
345 int SpamAssassin::read_pipe()
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
346 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
347 long size;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
348 int status;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
349 char iobuff[1024];
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
350 string reason;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
351
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
352 if (pipe_io[1][0] == -1) return 0;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
353
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
354 size = read(pipe_io[1][0], iobuff, 1024);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
355
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
356 if (size < 0) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
357 reason = string(strerror(errno));
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
358 // Close remaining pipe.
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
359 close(pipe_io[1][0]);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
360 pipe_io[1][0] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
361 // Slaughter child
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
362 kill(pid, SIGKILL);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
363 // wait for child to terminate
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
364 waitpid(pid, &status, 0);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
365 my_syslog(priv, "read error: " + reason);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
366 size = 0;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
367 error = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
368 running = false;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
369 } else if (size == 0) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
370 // EOF. Close the pipe
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
371 if (close(pipe_io[1][0])) {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
372 error = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
373 my_syslog(priv, "close error: " + string(strerror(errno)));
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
374 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
375 pipe_io[1][0] = -1;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
376 } else {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
377 // append to mail buffer
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
378 spamc_output.append(iobuff, size);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
379 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
380 return size;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
381 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
382
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
383
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
384 void SpamAssassin::empty_and_close_pipe()
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
385 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
386 while (read_pipe())
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
387 ;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
388 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
389
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
390
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
391 void SpamAssassin::closeall(int fd)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
392 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
393 int fdlimit = sysconf(_SC_OPEN_MAX);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
394 while (fd < fdlimit)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
395 close(fd++);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
396 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
397
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
398
168
6bac960af6b4 add generic reverse dns filtering regex
carl
parents: 167
diff changeset
399 char *SpamAssassin::getorwarnmacro(SMFICTX *ctx, char *macro, char *def, char *scope)
6bac960af6b4 add generic reverse dns filtering regex
carl
parents: 167
diff changeset
400 {
6bac960af6b4 add generic reverse dns filtering regex
carl
parents: 167
diff changeset
401 char *rc = smfi_getsymval(ctx, macro);
6bac960af6b4 add generic reverse dns filtering regex
carl
parents: 167
diff changeset
402 if (!rc) {
6bac960af6b4 add generic reverse dns filtering regex
carl
parents: 167
diff changeset
403 rc = def;
6bac960af6b4 add generic reverse dns filtering regex
carl
parents: 167
diff changeset
404 warnmacro(macro, scope);
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 return rc;
6bac960af6b4 add generic reverse dns filtering regex
carl
parents: 167
diff changeset
407 }
6bac960af6b4 add generic reverse dns filtering regex
carl
parents: 167
diff changeset
408
6bac960af6b4 add generic reverse dns filtering regex
carl
parents: 167
diff changeset
409
163
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
410 void SpamAssassin::warnmacro(char *macro, char *scope)
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
411 {
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
412 if (warnedmacro) return;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
413 char buf[maxlen];
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
414 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
415 my_syslog(priv, buf);
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
416 warnedmacro = true;
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
417 }
97d7da45fe2a spamassassin changes
carl
parents:
diff changeset
418