196
|
1 /*
|
|
2
|
|
3 Copyright (c) 2007 Carl Byington - 510 Software Group, released under
|
|
4 the GPL version 3 or any later version at your choice available at
|
|
5 http://www.gnu.org/licenses/gpl-3.0.txt
|
|
6
|
|
7 */
|
|
8
|
|
9 #include "includes.h"
|
|
10 #include <errno.h>
|
|
11 #include <fcntl.h>
|
|
12 #include <poll.h>
|
|
13 #include <signal.h>
|
|
14 #include <string>
|
|
15 #include <sys/types.h>
|
|
16 #include <sys/wait.h>
|
|
17 #include <unistd.h>
|
|
18
|
|
19 // needed for socket io
|
|
20 #include <sys/ioctl.h>
|
|
21 #include <net/if.h>
|
|
22 #include <arpa/inet.h>
|
|
23 #include <netinet/in.h>
|
|
24 #include <netinet/tcp.h>
|
|
25 #include <netdb.h>
|
|
26 #include <sys/socket.h>
|
|
27 #include <sys/un.h>
|
|
28
|
|
29
|
|
30 const int maxlen = 1000; // used for snprintf buffers
|
|
31 extern int NULL_SOCKET;
|
|
32 const char *options = "header\n";
|
|
33
|
|
34
|
|
35 ////////////////////////////////////////////////
|
|
36 // helper to convert syslog control chars
|
|
37 //
|
|
38 string escaper(string v);
|
|
39 string escaper(string v)
|
|
40 {
|
|
41 size_t n = v.length();
|
|
42 char buf[n+1];
|
|
43 strncpy(buf, v.c_str(), n);
|
|
44 for (size_t i=0; i<n; i++) {
|
|
45 if (buf[i] == '\r') buf[i] = 'r';
|
|
46 if (buf[i] == '\n') buf[i] = 'n';
|
|
47 if ((unsigned char)(buf[i]) < ' '){
|
|
48 buf[i] = '.';
|
|
49 }
|
|
50 }
|
|
51 return string(buf, n);
|
|
52 }
|
|
53
|
|
54
|
|
55 DccInterface::DccInterface(char *port_, mlfiPriv *priv_, int ip, char *helo_, char *from)
|
|
56 {
|
|
57 err = false;
|
|
58 first_recipient = true;
|
|
59 first_header = true;
|
|
60 priv = priv_;
|
|
61 ip4 = ip;
|
|
62 helo = helo_;
|
|
63 envfrom = from;
|
|
64 dccifd_port = port_;
|
|
65 dccifd_socket = NULL_SOCKET;
|
|
66 }
|
|
67
|
|
68
|
|
69 DccInterface::~DccInterface()
|
|
70 {
|
|
71 my_disconnect();
|
|
72 }
|
|
73
|
|
74
|
|
75 void DccInterface::mlfi_envrcpt(SMFICTX *ctx, char *envrcpt, bool grey)
|
|
76 {
|
|
77 if (first_recipient) {
|
|
78 first_recipient = false;
|
|
79 char adr[sizeof "255.255.255.255 "];
|
|
80 adr[0] = '\0';
|
|
81 inet_ntop(AF_INET, (const u_char *)&ip4, adr, sizeof(adr));
|
|
82 // Validated sending site's address
|
|
83 char *rdns = getmacro(ctx, "_", "");
|
|
84 char buf[maxlen+1];
|
|
85 if (*rdns == '[') rdns = "";
|
|
86 else {
|
|
87 int n = 0;
|
|
88 while ((n < maxlen) && rdns[n] && (rdns[n] != ' ')) n++;
|
|
89 strncpy(buf, rdns, n);
|
|
90 buf[n] = '\0';
|
|
91 rdns = buf;
|
|
92 }
|
|
93 output(options);
|
|
94 output(adr); output("\r");
|
|
95 output(rdns); output("\n");
|
|
96 //output("4.3.2.1\r\n"); // !! not local whitelisting
|
|
97 output(helo); output("\n");
|
|
98 output(envfrom); output("\n");
|
|
99 }
|
|
100 output(envrcpt);
|
|
101 if (grey) output("\r\n");
|
|
102 else output("\rdnsblnogrey\n");
|
|
103 }
|
|
104
|
|
105
|
|
106 void DccInterface::mlfi_header(SMFICTX *ctx, char* headerf, char* headerv)
|
|
107 {
|
|
108 if (dccifd_socket == NULL_SOCKET) Connect();
|
|
109 if ((dccifd_socket != NULL_SOCKET) && (!dccifd_input.empty())) {
|
|
110 output(dccifd_input);
|
|
111 dccifd_input.clear();
|
|
112 }
|
|
113
|
|
114 if (first_header) {
|
|
115 output("\n");
|
|
116 first_header = false;
|
|
117 }
|
|
118
|
|
119 output(headerf);
|
|
120 output(": ");
|
|
121 output(headerv);
|
|
122 output("\r\n");
|
|
123 }
|
|
124
|
|
125
|
|
126 void DccInterface::mlfi_eoh()
|
|
127 {
|
|
128 output("\r\n");
|
|
129 }
|
|
130
|
|
131
|
|
132 void DccInterface::mlfi_body(u_char *bodyp, size_t bodylen)
|
|
133 {
|
|
134 output((char *)bodyp, bodylen);
|
|
135 }
|
|
136
|
|
137
|
|
138 void DccInterface::mlfi_eom(bool &grey, int &bulk)
|
|
139 {
|
|
140 // AnAnX-DCC-Rhyolite-Metrics: ns.five-ten-sg.com 104; Body=2 Fuz1=2nn
|
|
141
|
|
142 close_output(); // signal EOF to DccInterface
|
|
143 input(); // read what the dcc has to say about this message
|
|
144 my_syslog(priv, "dcc returned " + escaper(dccifd_output));
|
|
145 grey = false;
|
|
146 bulk = 0;
|
|
147 const int n = dccifd_output.length();
|
|
148 char buf[n+1];
|
|
149 strncpy(buf, dccifd_output.c_str(), n);
|
|
150 buf[n] = '\0';
|
|
151
|
|
152 int newlines = 0;
|
|
153 int j, i = 0;
|
|
154 while ((i<n) && (newlines < 2)) {
|
|
155 switch (buf[i++]) {
|
|
156 case 'G' :
|
|
157 grey = true;
|
|
158 break;
|
|
159 case '\n' :
|
|
160 newlines++;
|
|
161 default : ;
|
|
162 }
|
|
163 }
|
|
164
|
|
165 // skip up to and including ;
|
|
166 while ((i<n) && (buf[i++] != ';'));
|
|
167
|
|
168 // convert to lower, = to space, ctrl-chars to space
|
|
169 for (j=i; j<n; j++) {
|
|
170 buf[j] = tolower(buf[j]);
|
|
171 if (buf[j] == '=') buf[j] = ' ';
|
|
172 if (buf[j] < ' ') buf[j] = ' ';
|
|
173 }
|
|
174
|
|
175 while (i<n) {
|
|
176 // skip leading blanks
|
|
177 while ((i<n) && (buf[i] == ' ')) i++;
|
|
178
|
|
179 // find blank terminator
|
|
180 for (j=i; (j<n) && (buf[j] != ' '); j++);
|
|
181
|
|
182 // find anything?
|
|
183 if (j > i) {
|
|
184 // process this token
|
|
185 buf[j] = '\0';
|
|
186 //my_syslog(priv, string("dccifd token") + (buf+i));
|
|
187 if (strcmp(buf+i, "bulk") == 0) bulk = dccbulk;
|
|
188 else if (strcmp(buf+i, "many") == 0) bulk = dccbulk;
|
|
189 else if (strcmp(buf+i, "whitelist") == 0) bulk = 0;
|
|
190 else if (isdigit(buf[i])) {
|
|
191 int b = atoi(buf+i);
|
|
192 if (b > bulk) bulk = b;
|
|
193 }
|
|
194 // skip this token
|
|
195 i = j+1;
|
|
196 }
|
|
197 }
|
|
198 char buff[maxlen];
|
|
199 snprintf(buff, sizeof(buff), "dccifd found grey %s bulk %d", ((grey) ? "yes" : "no"), bulk);
|
|
200 //my_syslog(priv, buff);
|
|
201 }
|
|
202
|
|
203
|
|
204 void DccInterface::my_disconnect()
|
|
205 {
|
|
206 if (dccifd_socket != NULL_SOCKET) {
|
|
207 shutdown(dccifd_socket, SHUT_RDWR);
|
|
208 close(dccifd_socket);
|
|
209 dccifd_socket = NULL_SOCKET;
|
|
210 }
|
|
211 }
|
|
212
|
|
213
|
|
214 void DccInterface::Connect()
|
|
215 {
|
|
216 if (err) return;
|
|
217
|
|
218 sockaddr_un server;
|
|
219 memset(&server, '\0', sizeof(server));
|
|
220 server.sun_family = AF_UNIX;
|
|
221 strncpy(server.sun_path, dccifd_port, sizeof(server.sun_path)-1);
|
|
222 dccifd_socket = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
223 if (dccifd_socket != NULL_SOCKET) {
|
|
224 bool rc = (connect(dccifd_socket, (sockaddr *)&server, sizeof(server)) == 0);
|
|
225 if (!rc) {
|
|
226 my_disconnect();
|
|
227 err = true;
|
|
228 }
|
|
229 }
|
|
230 }
|
|
231
|
|
232
|
|
233 size_t DccInterface::my_write(const char *buf, size_t len) {
|
|
234 if (err) return 0;
|
|
235 size_t rs = 0;
|
|
236 while (len) {
|
|
237 ssize_t ws = write(dccifd_socket, buf, len);
|
|
238 if (ws > 0) {
|
|
239 rs += ws;
|
|
240 len -= ws;
|
|
241 buf += ws;
|
|
242 }
|
|
243 else {
|
|
244 // error or peer closed the socket!
|
|
245 rs = 0;
|
|
246 err = true;
|
|
247 break;
|
|
248 }
|
|
249 }
|
|
250 return rs;
|
|
251 }
|
|
252
|
|
253 size_t DccInterface::my_read(char *buf, size_t len) {
|
|
254 if (err) return 0;
|
|
255 size_t rs = 0;
|
|
256 while (len) {
|
|
257 ssize_t ws = read(dccifd_socket, buf, len);
|
|
258 if (ws > 0) {
|
|
259 rs += ws;
|
|
260 len -= ws;
|
|
261 buf += ws;
|
|
262 }
|
|
263 else if (ws < 0) {
|
|
264 // read error
|
|
265 rs = 0;
|
|
266 err = true;
|
|
267 break;
|
|
268 }
|
|
269 else {
|
|
270 // peer closed the socket, end of file
|
|
271 break;
|
|
272 }
|
|
273 }
|
|
274 return rs;
|
|
275 }
|
|
276
|
|
277 void DccInterface::output(const char* buffer, size_t size)
|
|
278 {
|
|
279 // if there are problems, fail.
|
|
280 if (err) return;
|
|
281
|
|
282 // buffer it if not connected yet
|
|
283 if (dccifd_socket == NULL_SOCKET) {
|
|
284 //my_syslog(priv, string("dcc buffered ") + escaper(string(buffer, size)));
|
|
285 dccifd_input.append(buffer, size);
|
|
286 return;
|
|
287 }
|
|
288
|
|
289 // write it if we are connected
|
|
290 //my_syslog(priv, string("dcc write ") + escaper(string(buffer, size)));
|
|
291 my_write(buffer, size);
|
|
292 }
|
|
293
|
|
294
|
|
295 void DccInterface::output(const char* buffer)
|
|
296 {
|
|
297 output(buffer, strlen(buffer));
|
|
298 }
|
|
299
|
|
300
|
|
301 void DccInterface::output(string buffer)
|
|
302 {
|
|
303 output(buffer.c_str(), buffer.size());
|
|
304 }
|
|
305
|
|
306
|
|
307 void DccInterface::close_output()
|
|
308 {
|
|
309 if (dccifd_socket != NULL_SOCKET) {
|
|
310 shutdown(dccifd_socket, SHUT_WR);
|
|
311 }
|
|
312 }
|
|
313
|
|
314
|
|
315 void DccInterface::input()
|
|
316 {
|
|
317 if ((dccifd_socket == NULL_SOCKET) || err) return;
|
|
318 char buf[maxlen];
|
|
319 int rs;
|
|
320 while (rs = my_read(buf, maxlen)) {
|
|
321 //my_syslog(priv, string("dcc read ") + escaper(string(buf, rs)));
|
|
322 dccifd_output.append(buf, rs);
|
|
323 }
|
|
324 my_disconnect();
|
|
325 }
|
|
326
|
|
327
|
|
328 char *DccInterface::getmacro(SMFICTX *ctx, char *macro, char *def)
|
|
329 {
|
|
330 char *rc = smfi_getsymval(ctx, macro);
|
|
331 if (!rc) rc = def;
|
|
332 return rc;
|
|
333 }
|
|
334
|
|
335
|