Unbreak build on Solaris.
[exim.git] / src / src / dcc.c
CommitLineData
6a8f9482
TK
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
1a9312b2 5/* Copyright (c) Wolfgang Breyha 2005-2009
6a8f9482
TK
6 * Vienna University Computer Center
7 * wbreyha@gmx.net
b9e94344 8 * See the file NOTICE for conditions of use and distribution.
6a8f9482
TK
9 */
10
11/* This patch is based on code from Tom Kistners exiscan (ACL integration) and
12 * the DCC local_scan patch from Christopher Bodenstein */
13
14/* Code for calling dccifd. Called from acl.c. */
15
16#include "exim.h"
17#ifdef EXPERIMENTAL_DCC
18#include "dcc.h"
19#include "unistd.h"
20
21uschar dcc_header_str[256];
22uschar dcc_result_str[256];
23int dcc_ok = 0;
24int dcc_rc = 0;
25
26/* This function takes a file descriptor and a buffer as input and
27 * returns either 0 for success or errno in case of error. */
28
29int flushbuffer (int socket, uschar *buffer)
30 {
31 int retval, rsp;
32 rsp = write(socket, buffer, Ustrlen(buffer));
33 DEBUG(D_acl)
34 debug_printf("Result of the write() = %d\n", rsp);
35 if(rsp < 0)
36 {
37 DEBUG(D_acl)
38 debug_printf("Error writing buffer to socket: %s\n", strerror(errno));
39 retval = errno;
40 } else {
41 DEBUG(D_acl)
42 debug_printf("Wrote buffer to socket:\n%s\n", buffer);
43 retval = 0;
44 }
45 return retval;
46}
47
48int dcc_process(uschar **listptr) {
49 int sep = 0;
50 uschar *list = *listptr;
6a8f9482 51 FILE *data_file;
6a8f9482 52 uschar *dcc_daemon_ip = US"";
6a8f9482
TK
53 uschar *dcc_default_ip_option = US"127.0.0.1";
54 uschar *dcc_ip_option = US"";
55 uschar *dcc_helo_option = US"localhost";
56 uschar *dcc_reject_message = US"Rejected by DCC";
1a9312b2 57 uschar *xtra_hdrs = NULL;
6a8f9482
TK
58
59 /* from local_scan */
e93a964c 60 int i, j, k, c, retval, sockfd, resp, line;
7156b1ef 61 unsigned int portnr;
6a8f9482
TK
62 struct sockaddr_un serv_addr;
63 struct sockaddr_in serv_addr_in;
64 struct hostent *ipaddress;
65 uschar sockpath[128];
66 uschar sockip[40], client_ip[40];
67 uschar opts[128];
68 uschar rcpt[128], from[128];
69 uschar sendbuf[4096];
70 uschar recvbuf[4096];
71 uschar xhdr[256];
72 uschar dcc_return_text[1024];
73 uschar mbox_path[1024];
74 uschar message_subdir[2];
75 struct header_line *dcchdr;
6a8f9482
TK
76 uschar *dcc_acl_options;
77 uschar dcc_acl_options_buffer[10];
1a9312b2 78 uschar dcc_xtra_hdrs[1024];
6a8f9482 79
6a8f9482
TK
80 /* grep 1st option */
81 if ((dcc_acl_options = string_nextinlist(&list, &sep,
82 dcc_acl_options_buffer,
83 sizeof(dcc_acl_options_buffer))) != NULL)
84 {
85 /* parse 1st option */
86 if ( (strcmpic(dcc_acl_options,US"false") == 0) ||
87 (Ustrcmp(dcc_acl_options,"0") == 0) ) {
88 /* explicitly no matching */
89 return FAIL;
90 };
91
92 /* special cases (match anything except empty) */
93 if ( (strcmpic(dcc_acl_options,US"true") == 0) ||
94 (Ustrcmp(dcc_acl_options,"*") == 0) ||
95 (Ustrcmp(dcc_acl_options,"1") == 0) ) {
96 dcc_acl_options = dcc_acl_options;
97 };
98 }
99 else {
100 /* empty means "don't match anything" */
101 return FAIL;
102 };
103
104 sep = 0;
105
106 /* if we scanned this message last time, just return */
107 if ( dcc_ok )
108 return dcc_rc;
109
110 /* open the spooled body */
111 message_subdir[1] = '\0';
112 for (i = 0; i < 2; i++) {
113 message_subdir[0] = (split_spool_directory == (i == 0))? message_id[5] : 0;
114 sprintf(CS mbox_path, "%s/input/%s/%s-D", spool_directory, message_subdir, message_id);
115 data_file = Ufopen(mbox_path,"rb");
116 if (data_file != NULL)
117 break;
118 };
119
120 if (data_file == NULL) {
121 /* error while spooling */
122 log_write(0, LOG_MAIN|LOG_PANIC,
123 "dcc acl condition: error while opening spool file");
124 return DEFER;
125 };
126
127 /* Initialize the variables */
128
129 bzero(sockip,sizeof(sockip));
130 if (dccifd_address) {
131 if (dccifd_address[0] == '/')
132 Ustrncpy(sockpath, dccifd_address, sizeof(sockpath));
133 else
7156b1ef 134 if( sscanf(CS dccifd_address, "%s %u", sockip, &portnr) != 2) {
6a8f9482
TK
135 log_write(0, LOG_MAIN,
136 "dcc acl condition: warning - invalid dccifd address: '%s'", dccifd_address);
137 (void)fclose(data_file);
138 return DEFER;
139 }
140 }
141
142 /* opts is what we send as dccifd options - see man dccifd */
143 /* We don't support any other option than 'header' so just copy that */
144 bzero(opts,sizeof(opts));
145 Ustrncpy(opts, "header", sizeof(opts)-1);
146 Ustrncpy(client_ip, dcc_ip_option, sizeof(client_ip)-1);
147 /* If the dcc_client_ip is not provided use the
148 * sender_host_address or 127.0.0.1 if it is NULL */
149 DEBUG(D_acl)
150 debug_printf("my_ip_option = %s - client_ip = %s - sender_host_address = %s\n", dcc_ip_option, client_ip, sender_host_address);
151 if(!(Ustrcmp(client_ip, ""))){
152 /* Do we have a sender_host_address or is it NULL? */
153 if(sender_host_address){
1a9312b2 154 Ustrncpy(client_ip, sender_host_address, sizeof(client_ip)-1);
6a8f9482
TK
155 } else {
156 /* sender_host_address is NULL which means it comes from localhost */
157 Ustrncpy(client_ip, dcc_default_ip_option, sizeof(client_ip)-1);
158 }
159 }
160 DEBUG(D_acl)
161 debug_printf("Client IP: %s\n", client_ip);
162 Ustrncpy(sockip, dcc_daemon_ip, sizeof(sockip)-1);
163 /* strncat(opts, my_request, strlen(my_request)); */
164 Ustrcat(opts, "\n");
165 Ustrncat(opts, client_ip, sizeof(opts)-Ustrlen(opts)-1);
166 Ustrncat(opts, "\nHELO ", sizeof(opts)-Ustrlen(opts)-1);
167 Ustrncat(opts, dcc_helo_option, sizeof(opts)-Ustrlen(opts)-2);
168 Ustrcat(opts, "\n");
169
170 /* initialize the other variables */
171 dcchdr = header_list;
6a8f9482
TK
172 /* we set the default return value to DEFER */
173 retval = DEFER;
174
175 bzero(sendbuf,sizeof(sendbuf));
176 bzero(xhdr,sizeof(xhdr));
177 bzero(rcpt,sizeof(rcpt));
178 bzero(from,sizeof(from));
179
1a9312b2
TK
180 /* send a null return path as "<>". */
181 if (Ustrlen(sender_address) > 0)
182 Ustrncpy(from, sender_address, sizeof(from));
183 else
184 Ustrncpy(from, "<>", sizeof(from));
6a8f9482
TK
185 Ustrncat(from, "\n", sizeof(from)-Ustrlen(from)-1);
186
187 /**************************************
188 * Now creating the socket connection *
189 **************************************/
190
191 /* If there is a dcc_daemon_ip, we use a tcp socket, otherwise a UNIX socket */
192 if(Ustrcmp(sockip, "")){
193 ipaddress = gethostbyname((char *)sockip);
194 bzero((char *) &serv_addr_in, sizeof(serv_addr_in));
195 serv_addr_in.sin_family = AF_INET;
196 bcopy((char *)ipaddress->h_addr, (char *)&serv_addr_in.sin_addr.s_addr, ipaddress->h_length);
197 serv_addr_in.sin_port = htons(portnr);
198 if ((sockfd = socket(AF_INET, SOCK_STREAM,0)) < 0){
199 DEBUG(D_acl)
200 debug_printf("Creating socket failed: %s\n", strerror(errno));
201 log_write(0,LOG_REJECT,"Creating socket failed: %s\n", strerror(errno));
202 /* if we cannot create the socket, defer the mail */
203 (void)fclose(data_file);
204 return retval;
205 }
206 /* Now connecting the socket (INET) */
207 if (connect(sockfd, (struct sockaddr *)&serv_addr_in, sizeof(serv_addr_in)) < 0){
208 DEBUG(D_acl)
209 debug_printf("Connecting socket failed: %s\n", strerror(errno));
210 log_write(0,LOG_REJECT,"Connecting socket failed: %s\n", strerror(errno));
211 /* if we cannot contact the socket, defer the mail */
212 (void)fclose(data_file);
213 return retval;
214 }
215 } else {
216 /* connecting to the dccifd UNIX socket */
217 bzero((char *)&serv_addr,sizeof(serv_addr));
218 serv_addr.sun_family = AF_UNIX;
219 Ustrcpy(serv_addr.sun_path, sockpath);
6a8f9482
TK
220 if ((sockfd = socket(AF_UNIX, SOCK_STREAM,0)) < 0){
221 DEBUG(D_acl)
222 debug_printf("Creating socket failed: %s\n", strerror(errno));
223 log_write(0,LOG_REJECT,"Creating socket failed: %s\n", strerror(errno));
224 /* if we cannot create the socket, defer the mail */
225 (void)fclose(data_file);
226 return retval;
227 }
228 /* Now connecting the socket (UNIX) */
e93a964c 229 if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){
6a8f9482
TK
230 DEBUG(D_acl)
231 debug_printf("Connecting socket failed: %s\n", strerror(errno));
232 log_write(0,LOG_REJECT,"Connecting socket failed: %s\n", strerror(errno));
233 /* if we cannot contact the socket, defer the mail */
234 (void)fclose(data_file);
235 return retval;
236 }
237 }
238 /* the socket is open, now send the options to dccifd*/
239 DEBUG(D_acl)
240 debug_printf("\n---------------------------\nSocket opened; now sending input\n-----------------\n");
241 /* First, fill in the input buffer */
242 Ustrncpy(sendbuf, opts, sizeof(sendbuf));
243 Ustrncat(sendbuf, from, sizeof(sendbuf)-Ustrlen(sendbuf)-1);
244
245 DEBUG(D_acl)
246 {
247 debug_printf("opts = %s\nsender = %s\nrcpt count = %d\n", opts, from, recipients_count);
248 debug_printf("Sending options:\n****************************\n");
249 }
250
251 /* let's send each of the recipients to dccifd */
252 for (i = 0; i < recipients_count; i++){
253 DEBUG(D_acl)
254 debug_printf("recipient = %s\n",recipients_list[i].address);
255 if(Ustrlen(sendbuf) + Ustrlen(recipients_list[i].address) > sizeof(sendbuf))
256 {
257 DEBUG(D_acl)
258 debug_printf("Writing buffer: %s\n", sendbuf);
259 flushbuffer(sockfd, sendbuf);
260 bzero(sendbuf, sizeof(sendbuf));
261 }
262 Ustrncat(sendbuf, recipients_list[i].address, sizeof(sendbuf)-Ustrlen(sendbuf)-1);
263 Ustrncat(sendbuf, "\r\n", sizeof(sendbuf)-Ustrlen(sendbuf)-1);
264 }
265 /* send a blank line between options and message */
266 Ustrncat(sendbuf, "\n", sizeof(sendbuf)-Ustrlen(sendbuf)-1);
267 /* Now we send the input buffer */
268 DEBUG(D_acl)
269 debug_printf("%s\n****************************\n", sendbuf);
270 flushbuffer(sockfd, sendbuf);
271
272 /* now send the message */
273 /* Clear the input buffer */
274 bzero(sendbuf, sizeof(sendbuf));
275 /* First send the headers */
276 /* Now send the headers */
277 DEBUG(D_acl)
278 debug_printf("Sending headers:\n****************************\n");
279 Ustrncpy(sendbuf, dcchdr->text, sizeof(sendbuf)-2);
280 while((dcchdr=dcchdr->next)) {
281 if(dcchdr->slen > sizeof(sendbuf)-2) {
282 /* The size of the header is bigger than the size of
283 * the input buffer, so split it up in smaller parts. */
284 flushbuffer(sockfd, sendbuf);
285 bzero(sendbuf, sizeof(sendbuf));
286 j = 0;
287 while(j < dcchdr->slen)
288 {
289 for(i = 0; i < sizeof(sendbuf)-2; i++) {
290 sendbuf[i] = dcchdr->text[j];
291 j++;
292 }
293 flushbuffer(sockfd, sendbuf);
294 bzero(sendbuf, sizeof(sendbuf));
295 }
296 } else if(Ustrlen(sendbuf) + dcchdr->slen > sizeof(sendbuf)-2) {
297 flushbuffer(sockfd, sendbuf);
298 bzero(sendbuf, sizeof(sendbuf));
299 Ustrncpy(sendbuf, dcchdr->text, sizeof(sendbuf)-2);
300 } else {
301 Ustrncat(sendbuf, dcchdr->text, sizeof(sendbuf)-Ustrlen(sendbuf)-2);
302 }
303 }
304
305 /* a blank line separates header from body */
306 Ustrncat(sendbuf, "\n", sizeof(sendbuf)-Ustrlen(sendbuf)-1);
307 flushbuffer(sockfd, sendbuf);
308 DEBUG(D_acl)
7156b1ef 309 debug_printf("\n****************************\n%s", sendbuf);
6a8f9482
TK
310
311 /* Clear the input buffer */
312 bzero(sendbuf, sizeof(sendbuf));
313
314 /* now send the body */
315 DEBUG(D_acl)
316 debug_printf("Writing body:\n****************************\n");
317 (void)fseek(data_file, SPOOL_DATA_START_OFFSET, SEEK_SET);
318 while((fread(sendbuf, 1, sizeof(sendbuf)-1, data_file)) > 0) {
319 flushbuffer(sockfd, sendbuf);
320 bzero(sendbuf, sizeof(sendbuf));
321 }
322 DEBUG(D_acl)
323 debug_printf("\n****************************\n");
324
325 /* shutdown() the socket */
326 if(shutdown(sockfd, 1) < 0){
327 DEBUG(D_acl)
328 debug_printf("Couldn't shutdown socket: %s\n", strerror(errno));
329 log_write(0,LOG_MAIN,"Couldn't shutdown socket: %s\n", strerror(errno));
330 /* If there is a problem with the shutdown()
331 * defer the mail. */
332 (void)fclose(data_file);
333 return retval;
334 }
335 DEBUG(D_acl)
336 debug_printf("\n-------------------------\nInput sent.\n-------------------------\n");
337
338 /********************************
339 * receiving output from dccifd *
340 ********************************/
341 DEBUG(D_acl)
342 debug_printf("\n-------------------------------------\nNow receiving output from server\n-----------------------------------\n");
343
344 /******************************************************************
345 * We should get 3 lines: *
346 * 1/ First line is overall result: either 'A' for Accept, *
347 * 'R' for Reject, 'S' for accept Some recipients or *
348 * 'T' for a Temporary error. *
349 * 2/ Second line contains the list of Accepted/Rejected *
350 * recipients in the form AARRA (A = accepted, R = rejected). *
351 * 3/ Third line contains the X-DCC header. *
352 ******************************************************************/
353
354 line = 1; /* we start at the first line of the output */
6a8f9482
TK
355 j = 0; /* will be used as index for the recipients list */
356 k = 0; /* initializing the index of the X-DCC header: xhdr[k] */
6a8f9482
TK
357
358 /* Let's read from the socket until there's nothing left to read */
359 bzero(recvbuf, sizeof(recvbuf));
7156b1ef 360 while((resp = read(sockfd, recvbuf, sizeof(recvbuf)-1)) > 0) {
6a8f9482
TK
361 /* How much did we get from the socket */
362 c = Ustrlen(recvbuf) + 1;
363 DEBUG(D_acl)
364 debug_printf("Length of the output buffer is: %d\nOutput buffer is:\n------------\n%s\n-----------\n", c, recvbuf);
365
366 /* Now let's read each character and see what we've got */
367 for(i = 0; i < c; i++) {
368 /* First check if we reached the end of the line and
369 * then increment the line counter */
370 if(recvbuf[i] == '\n') {
371 line++;
372 }
373 else {
374 /* The first character of the first line is the
375 * overall response. If there's another character
376 * on that line it is not correct. */
377 if(line == 1) {
378 if(i == 0) {
379 /* Now get the value and set the
380 * return value accordingly */
381 if(recvbuf[i] == 'A') {
382 DEBUG(D_acl)
383 debug_printf("Overall result = A\treturning OK\n");
384 Ustrcpy(dcc_return_text, "Mail accepted by DCC");
7156b1ef 385 dcc_result = US"A";
6a8f9482
TK
386 retval = OK;
387 }
388 else if(recvbuf[i] == 'R') {
389 DEBUG(D_acl)
390 debug_printf("Overall result = R\treturning FAIL\n");
7156b1ef 391 dcc_result = US"R";
6a8f9482
TK
392 retval = FAIL;
393 if(sender_host_name) {
394 log_write(0, LOG_MAIN, "H=%s [%s] F=<%s>: rejected by DCC", sender_host_name, sender_host_address, sender_address);
395 }
396 else {
397 log_write(0, LOG_MAIN, "H=[%s] F=<%s>: rejected by DCC", sender_host_address, sender_address);
398 }
399 Ustrncpy(dcc_return_text, dcc_reject_message, Ustrlen(dcc_reject_message) + 1);
400 }
401 else if(recvbuf[i] == 'S') {
402 DEBUG(D_acl)
403 debug_printf("Overall result = S\treturning OK\n");
404 Ustrcpy(dcc_return_text, "Not all recipients accepted by DCC");
1a9312b2
TK
405 /* Since we're in an ACL we want a global result
406 * so we accept for all */
7156b1ef 407 dcc_result = US"A";
6a8f9482
TK
408 retval = OK;
409 }
410 else if(recvbuf[i] == 'G') {
411 DEBUG(D_acl)
412 debug_printf("Overall result = G\treturning FAIL\n");
413 Ustrcpy(dcc_return_text, "Greylisted by DCC");
7156b1ef 414 dcc_result = US"G";
1a9312b2 415 retval = FAIL;
6a8f9482
TK
416 }
417 else if(recvbuf[i] == 'T') {
418 DEBUG(D_acl)
419 debug_printf("Overall result = T\treturning DEFER\n");
420 retval = DEFER;
421 log_write(0,LOG_MAIN,"Temporary error with DCC: %s\n", recvbuf);
422 Ustrcpy(dcc_return_text, "Temporary error with DCC");
7156b1ef 423 dcc_result = US"T";
6a8f9482
TK
424 }
425 else {
426 DEBUG(D_acl)
427 debug_printf("Overall result = something else\treturning DEFER\n");
428 retval = DEFER;
429 log_write(0,LOG_MAIN,"Unknown DCC response: %s\n", recvbuf);
430 Ustrcpy(dcc_return_text, "Unknown DCC response");
7156b1ef 431 dcc_result = US"T";
6a8f9482
TK
432 }
433 }
434 else {
435 /* We're on the first line but not on the first character,
436 * there must be something wrong. */
437 DEBUG(D_acl)
438 debug_printf("Line = %d but i = %d != 0 character is %c - This is wrong!\n", line, i, recvbuf[i]);
439 log_write(0,LOG_MAIN,"Wrong header from DCC, output is %s\n", recvbuf);
440 }
441 }
442 else if(line == 2) {
443 /* On the second line we get a list of
1a9312b2
TK
444 * answer for each recipient. We don't care about
445 * it because we're in an acl and so just take the
446 * global result. */
6a8f9482
TK
447 }
448 else if(line > 2) {
449 /* The third and following lines is the X-DCC header,
450 * so we store it in xhdr. */
451 /* check if we don't get more than what we can handle */
452 if(k < sizeof(xhdr)) { /* xhdr has a length of 120 */
6a8f9482
TK
453 xhdr[k] = recvbuf[i];
454 k++;
455 }
456 else {
457 DEBUG(D_acl)
458 debug_printf("We got more output than we can store in the X-DCC header. Truncating at 120 characters.\n");
459 }
460 }
461 else {
462 /* Wrong line number. There must be a problem with the output. */
463 DEBUG(D_acl)
464 debug_printf("Wrong line number in output. Line number is %d\n", line);
465 }
466 }
467 }
468 /* we reinitialize the output buffer before we read again */
469 bzero(recvbuf,sizeof(recvbuf));
470 }
471 /* We have read everything from the socket */
472
473 /* We need the terminate the X-DCC header with a '\n' character. This needs to be k-1
474 * for xhdr[k] contains '\0'. */
475 xhdr[k-1] = '\n';
476
477 /* Now let's sum up what we've got. */
478 DEBUG(D_acl)
1a9312b2 479 debug_printf("\n--------------------------\nOverall result = %d\nX-DCC header: %sReturn message: %s\ndcc_result: %s\n", retval, xhdr, dcc_return_text, dcc_result);
6a8f9482
TK
480
481 /* We only add the X-DCC header if it starts with X-DCC */
482 if(!(Ustrncmp(xhdr, "X-DCC", 5))){
483 dcc_header = xhdr;
484 if(dcc_direct_add_header) {
485 header_add(' ' , "%s", xhdr);
1a9312b2 486 /* since the MIME ACL already writes the .eml file to disk without DCC Header we've to erase it */
6a8f9482
TK
487 unspool_mbox();
488 }
489 }
490 else {
491 DEBUG(D_acl)
492 debug_printf("Wrong format of the X-DCC header: %s\n", xhdr);
493 }
494
1a9312b2
TK
495 /* check if we should add additional headers passed in acl_m_dcc_add_header */
496 if(dcc_direct_add_header) {
7156b1ef 497 if (((xtra_hdrs = expand_string(US"$acl_m_dcc_add_header")) != NULL) && (xtra_hdrs[0] != '\0')) {
1a9312b2
TK
498 Ustrncpy(dcc_xtra_hdrs, xtra_hdrs, sizeof(dcc_xtra_hdrs) - 2);
499 if (dcc_xtra_hdrs[Ustrlen(dcc_xtra_hdrs)-1] != '\n')
500 Ustrcat(dcc_xtra_hdrs, "\n");
501 header_add(' ', "%s", dcc_xtra_hdrs);
502 DEBUG(D_acl)
503 debug_printf("adding additional headers in $acl_m_dcc_add_header: %s", dcc_xtra_hdrs);
504 }
505 }
506
6a8f9482
TK
507 dcc_ok = 1;
508 /* Now return to exim main process */
509 DEBUG(D_acl)
1a9312b2 510 debug_printf("Before returning to exim main process:\nreturn_text = %s - retval = %d\ndcc_result = %s\n", dcc_return_text, retval, dcc_result);
6a8f9482
TK
511
512 (void)fclose(data_file);
6f0c431a
PP
513 dcc_rc = retval;
514 return dcc_rc;
6a8f9482
TK
515}
516
517#endif