Bug 1339: DCC update (Wolfgang Breyha)
[exim.git] / src / src / dcc.c
index 6de5102d07d2cd7fac62974d2009d97629e965fa..44c0c009a92d4680d565bbbc2f11dbfe55433435 100644 (file)
@@ -1,10 +1,8 @@
-/* $Cambridge: exim/src/src/dcc.c,v 1.5 2009/11/12 08:34:23 nm4 Exp $ */
-
 /*************************************************
 *     Exim - an Internet mail transport agent    *
 *************************************************/
 
-/* Copyright (c) Wolfgang Breyha 2005-2009
+/* Copyright (c) Wolfgang Breyha 2005-2013
  * Vienna University Computer Center
  * wbreyha@gmx.net
  * See the file NOTICE for conditions of use and distribution.
@@ -21,7 +19,6 @@
 #include "unistd.h"
 
 uschar dcc_header_str[256];
-uschar dcc_result_str[256];
 int dcc_ok = 0;
 int dcc_rc = 0;
 
@@ -33,15 +30,16 @@ int flushbuffer (int socket, uschar *buffer)
   int retval, rsp;
   rsp = write(socket, buffer, Ustrlen(buffer));
   DEBUG(D_acl)
-    debug_printf("Result of the write() = %d\n", rsp);
+    debug_printf("DCC: Result of the write() = %d\n", rsp);
   if(rsp < 0)
   {
     DEBUG(D_acl)
-      debug_printf("Error writing buffer to socket: %s\n", strerror(errno));
+      debug_printf("DCC: Error writing buffer to socket: %s\n", strerror(errno));
     retval = errno;
-  } else {
+  }
+  else {
     DEBUG(D_acl)
-      debug_printf("Wrote buffer to socket:\n%s\n", buffer);
+      debug_printf("DCC: Wrote buffer to socket:\n%s\n", buffer);
     retval = 0;
   }
   return retval;
@@ -51,12 +49,11 @@ int dcc_process(uschar **listptr) {
   int sep = 0;
   uschar *list = *listptr;
   FILE *data_file;
-  uschar *dcc_daemon_ip = US"";
   uschar *dcc_default_ip_option = US"127.0.0.1";
-  uschar *dcc_ip_option = US"";
   uschar *dcc_helo_option = US"localhost";
   uschar *dcc_reject_message = US"Rejected by DCC";
   uschar *xtra_hdrs = NULL;
+  uschar *override_client_ip  = NULL;
 
   /* from local_scan */
   int i, j, k, c, retval, sockfd, resp, line;
@@ -70,7 +67,6 @@ int dcc_process(uschar **listptr) {
   uschar rcpt[128], from[128];
   uschar sendbuf[4096];
   uschar recvbuf[4096];
-  uschar xhdr[256];
   uschar dcc_return_text[1024];
   uschar mbox_path[1024];
   uschar message_subdir[2];
@@ -144,24 +140,26 @@ int dcc_process(uschar **listptr) {
   /* opts is what we send as dccifd options - see man dccifd */
   /* We don't support any other option than 'header' so just copy that */
   bzero(opts,sizeof(opts));
-  Ustrncpy(opts, "header", sizeof(opts)-1);
-  Ustrncpy(client_ip, dcc_ip_option, sizeof(client_ip)-1);
-  /* If the dcc_client_ip is not provided use the
-   * sender_host_address or 127.0.0.1 if it is NULL */
-  DEBUG(D_acl)
-    debug_printf("my_ip_option = %s - client_ip = %s - sender_host_address = %s\n", dcc_ip_option, client_ip, sender_host_address);
-  if(!(Ustrcmp(client_ip, ""))){
-    /* Do we have a sender_host_address or is it NULL? */
-    if(sender_host_address){
-      Ustrncpy(client_ip, sender_host_address, sizeof(client_ip)-1);
-    } else {
-      /* sender_host_address is NULL which means it comes from localhost */
-      Ustrncpy(client_ip, dcc_default_ip_option, sizeof(client_ip)-1);
-    }
+  Ustrncpy(opts, dccifd_options, sizeof(opts)-1);
+  /* if $acl_m_dcc_override_client_ip is set use it */
+  if (((override_client_ip = expand_string(US"$acl_m_dcc_override_client_ip")) != NULL) && 
+       (override_client_ip[0] != '\0')) {
+    Ustrncpy(client_ip, override_client_ip, sizeof(client_ip)-1);
+    DEBUG(D_acl)
+      debug_printf("DCC: Client IP (overridden): %s\n", client_ip);
+  } 
+  else if(sender_host_address) {
+  /* else if $sender_host_address is available use that? */
+    Ustrncpy(client_ip, sender_host_address, sizeof(client_ip)-1);
+    DEBUG(D_acl)
+      debug_printf("DCC: Client IP (sender_host_address): %s\n", client_ip);
+  } 
+  else {
+    /* sender_host_address is NULL which means it comes from localhost */
+    Ustrncpy(client_ip, dcc_default_ip_option, sizeof(client_ip)-1);
+    DEBUG(D_acl)
+      debug_printf("DCC: Client IP (default): %s\n", client_ip);
   }
-  DEBUG(D_acl)
-    debug_printf("Client IP: %s\n", client_ip);
-  Ustrncpy(sockip, dcc_daemon_ip, sizeof(sockip)-1);
   /* strncat(opts, my_request, strlen(my_request)); */
   Ustrcat(opts, "\n");
   Ustrncat(opts, client_ip, sizeof(opts)-Ustrlen(opts)-1);
@@ -175,7 +173,7 @@ int dcc_process(uschar **listptr) {
   retval = DEFER;
 
   bzero(sendbuf,sizeof(sendbuf));
-  bzero(xhdr,sizeof(xhdr));
+  bzero(dcc_header_str,sizeof(dcc_header_str));
   bzero(rcpt,sizeof(rcpt));
   bzero(from,sizeof(from));
 
@@ -190,7 +188,7 @@ int dcc_process(uschar **listptr) {
    * Now creating the socket connection *
    **************************************/
 
-  /* If there is a dcc_daemon_ip, we use a tcp socket, otherwise a UNIX socket */
+  /* If sockip contains an ip, we use a tcp socket, otherwise a UNIX socket */
   if(Ustrcmp(sockip, "")){
     ipaddress = gethostbyname((char *)sockip);
     bzero((char *) &serv_addr_in, sizeof(serv_addr_in));
@@ -199,8 +197,8 @@ int dcc_process(uschar **listptr) {
     serv_addr_in.sin_port = htons(portnr);
     if ((sockfd = socket(AF_INET, SOCK_STREAM,0)) < 0){
       DEBUG(D_acl)
-        debug_printf("Creating socket failed: %s\n", strerror(errno));
-      log_write(0,LOG_REJECT,"Creating socket failed: %s\n", strerror(errno));
+        debug_printf("DCC: Creating TCP socket connection failed: %s\n", strerror(errno));
+      log_write(0,LOG_PANIC,"DCC: Creating TCP socket connection failed: %s\n", strerror(errno));
       /* if we cannot create the socket, defer the mail */
       (void)fclose(data_file);
       return retval;
@@ -208,8 +206,8 @@ int dcc_process(uschar **listptr) {
     /* Now connecting the socket (INET) */
     if (connect(sockfd, (struct sockaddr *)&serv_addr_in, sizeof(serv_addr_in)) < 0){
       DEBUG(D_acl)
-        debug_printf("Connecting socket failed: %s\n", strerror(errno));
-      log_write(0,LOG_REJECT,"Connecting socket failed: %s\n", strerror(errno));
+        debug_printf("DCC: Connecting to TCP socket failed: %s\n", strerror(errno));
+      log_write(0,LOG_PANIC,"DCC: Connecting to TCP socket failed: %s\n", strerror(errno));
       /* if we cannot contact the socket, defer the mail */
       (void)fclose(data_file);
       return retval;
@@ -221,8 +219,8 @@ int dcc_process(uschar **listptr) {
     Ustrcpy(serv_addr.sun_path, sockpath);
     if ((sockfd = socket(AF_UNIX, SOCK_STREAM,0)) < 0){
       DEBUG(D_acl)
-        debug_printf("Creating socket failed: %s\n", strerror(errno));
-      log_write(0,LOG_REJECT,"Creating socket failed: %s\n", strerror(errno));
+        debug_printf("DCC: Creating UNIX socket connection failed: %s\n", strerror(errno));
+      log_write(0,LOG_PANIC,"DCC: Creating UNIX socket connection failed: %s\n", strerror(errno));
       /* if we cannot create the socket, defer the mail */
       (void)fclose(data_file);
       return retval;
@@ -230,8 +228,8 @@ int dcc_process(uschar **listptr) {
     /* Now connecting the socket (UNIX) */
     if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){
       DEBUG(D_acl)
-                            debug_printf("Connecting socket failed: %s\n", strerror(errno));
-      log_write(0,LOG_REJECT,"Connecting socket failed: %s\n", strerror(errno));
+                            debug_printf("DCC: Connecting to UNIX socket failed: %s\n", strerror(errno));
+      log_write(0,LOG_PANIC,"DCC: Connecting to UNIX socket failed: %s\n", strerror(errno));
       /* if we cannot contact the socket, defer the mail */
       (void)fclose(data_file);
       return retval;
@@ -239,25 +237,25 @@ int dcc_process(uschar **listptr) {
   }
   /* the socket is open, now send the options to dccifd*/
   DEBUG(D_acl)
-    debug_printf("\n---------------------------\nSocket opened; now sending input\n-----------------\n");
+    debug_printf("\nDCC: ---------------------------\nDCC: Socket opened; now sending input\nDCC: -----------------\n");
   /* First, fill in the input buffer */
   Ustrncpy(sendbuf, opts, sizeof(sendbuf));
   Ustrncat(sendbuf, from, sizeof(sendbuf)-Ustrlen(sendbuf)-1);
 
   DEBUG(D_acl)
   {
-    debug_printf("opts = %s\nsender = %s\nrcpt count = %d\n", opts, from, recipients_count);
-    debug_printf("Sending options:\n****************************\n");
+    debug_printf("DCC: opts = %s\nDCC: sender = %s\nDCC: rcpt count = %d\n", opts, from, recipients_count);
+    debug_printf("DCC: Sending options:\nDCC: ****************************\n");
   }
 
   /* let's send each of the recipients to dccifd */
   for (i = 0; i < recipients_count; i++){
     DEBUG(D_acl)
-      debug_printf("recipient = %s\n",recipients_list[i].address);
+      debug_printf("DCC: recipient = %s\n",recipients_list[i].address);
     if(Ustrlen(sendbuf) + Ustrlen(recipients_list[i].address) > sizeof(sendbuf))
     {
       DEBUG(D_acl)
-        debug_printf("Writing buffer: %s\n", sendbuf);
+        debug_printf("DCC: Writing buffer: %s\n", sendbuf);
       flushbuffer(sockfd, sendbuf);
       bzero(sendbuf, sizeof(sendbuf));
     }
@@ -268,7 +266,7 @@ int dcc_process(uschar **listptr) {
   Ustrncat(sendbuf, "\n", sizeof(sendbuf)-Ustrlen(sendbuf)-1);
   /* Now we send the input buffer */
   DEBUG(D_acl)
-    debug_printf("%s\n****************************\n", sendbuf);
+    debug_printf("DCC: %s\nDCC: ****************************\n", sendbuf);
   flushbuffer(sockfd, sendbuf);
 
   /* now send the message */
@@ -277,7 +275,7 @@ int dcc_process(uschar **listptr) {
   /* First send the headers */
   /* Now send the headers */
   DEBUG(D_acl)
-    debug_printf("Sending headers:\n****************************\n");
+    debug_printf("DCC: Sending headers:\nDCC: ****************************\n");
   Ustrncpy(sendbuf, dcchdr->text, sizeof(sendbuf)-2);
   while((dcchdr=dcchdr->next)) {
     if(dcchdr->slen > sizeof(sendbuf)-2) {
@@ -304,44 +302,44 @@ int dcc_process(uschar **listptr) {
     }
   }
 
-  /* a blank line separates header from body */
+  /* a blank line seperates header from body */
   Ustrncat(sendbuf, "\n", sizeof(sendbuf)-Ustrlen(sendbuf)-1);
   flushbuffer(sockfd, sendbuf);
   DEBUG(D_acl)
-    debug_printf("\n****************************\n%s", sendbuf);
+    debug_printf("\nDCC: ****************************\n%s", sendbuf);
 
   /* Clear the input buffer */
   bzero(sendbuf, sizeof(sendbuf));
 
   /* now send the body */
   DEBUG(D_acl)
-    debug_printf("Writing body:\n****************************\n");
+    debug_printf("DCC: Writing body:\nDCC: ****************************\n");
   (void)fseek(data_file, SPOOL_DATA_START_OFFSET, SEEK_SET);
   while((fread(sendbuf, 1, sizeof(sendbuf)-1, data_file)) > 0) {
     flushbuffer(sockfd, sendbuf);
     bzero(sendbuf, sizeof(sendbuf));
   }
   DEBUG(D_acl)
-    debug_printf("\n****************************\n");
+    debug_printf("\nDCC: ****************************\n");
 
   /* shutdown() the socket */
   if(shutdown(sockfd, 1) < 0){
     DEBUG(D_acl)
-      debug_printf("Couldn't shutdown socket: %s\n", strerror(errno));
-    log_write(0,LOG_MAIN,"Couldn't shutdown socket: %s\n", strerror(errno));
+      debug_printf("DCC: Couldn't shutdown socket: %s\n", strerror(errno));
+    log_write(0,LOG_MAIN,"DCC: Couldn't shutdown socket: %s\n", strerror(errno));
     /* If there is a problem with the shutdown()
      * defer the mail. */
     (void)fclose(data_file);
     return retval;
   }
   DEBUG(D_acl)
-    debug_printf("\n-------------------------\nInput sent.\n-------------------------\n");
+    debug_printf("\nDCC: -------------------------\nDCC: Input sent.\nDCC: -------------------------\n");
 
     /********************************
    * receiving output from dccifd *
    ********************************/
   DEBUG(D_acl)
-    debug_printf("\n-------------------------------------\nNow receiving output from server\n-----------------------------------\n");
+    debug_printf("\nDCC: -------------------------------------\nDCC: Now receiving output from server\nDCC: -----------------------------------\n");
 
   /******************************************************************
    * We should get 3 lines:                                         *
@@ -355,7 +353,7 @@ int dcc_process(uschar **listptr) {
 
   line = 1;    /* we start at the first line of the output */
   j = 0;       /* will be used as index for the recipients list */
-  k = 0;       /* initializing the index of the X-DCC header: xhdr[k] */
+  k = 0;       /* initializing the index of the X-DCC header: dcc_header_str[k] */
 
   /* Let's read from the socket until there's nothing left to read */
   bzero(recvbuf, sizeof(recvbuf));
@@ -363,7 +361,7 @@ int dcc_process(uschar **listptr) {
     /* How much did we get from the socket */
     c = Ustrlen(recvbuf) + 1;
     DEBUG(D_acl)
-      debug_printf("Length of the output buffer is: %d\nOutput buffer is:\n------------\n%s\n-----------\n", c, recvbuf);
+      debug_printf("DCC: Length of the output buffer is: %d\nDCC: Output buffer is:\nDCC: ------------\nDCC: %s\nDCC: -----------\n", c, recvbuf);
 
     /* Now let's read each character and see what we've got */
     for(i = 0; i < c; i++) {
@@ -382,14 +380,14 @@ int dcc_process(uschar **listptr) {
              * return value accordingly */
             if(recvbuf[i] == 'A') {
               DEBUG(D_acl)
-                debug_printf("Overall result = A\treturning OK\n");
+                debug_printf("DCC: Overall result = A\treturning OK\n");
               Ustrcpy(dcc_return_text, "Mail accepted by DCC");
               dcc_result = US"A";
               retval = OK;
             }
             else if(recvbuf[i] == 'R') {
               DEBUG(D_acl)
-                debug_printf("Overall result = R\treturning FAIL\n");
+                debug_printf("DCC: Overall result = R\treturning FAIL\n");
               dcc_result = US"R";
               retval = FAIL;
               if(sender_host_name) {
@@ -402,7 +400,7 @@ int dcc_process(uschar **listptr) {
             }
             else if(recvbuf[i] == 'S') {
               DEBUG(D_acl)
-                debug_printf("Overall result  = S\treturning OK\n");
+                debug_printf("DCC: Overall result  = S\treturning OK\n");
               Ustrcpy(dcc_return_text, "Not all recipients accepted by DCC");
               /* Since we're in an ACL we want a global result
                * so we accept for all */
@@ -411,14 +409,14 @@ int dcc_process(uschar **listptr) {
             }
             else if(recvbuf[i] == 'G') {
               DEBUG(D_acl)
-                debug_printf("Overall result  = G\treturning FAIL\n");
+                debug_printf("DCC: Overall result  = G\treturning FAIL\n");
               Ustrcpy(dcc_return_text, "Greylisted by DCC");
               dcc_result = US"G";
               retval = FAIL;
             }
             else if(recvbuf[i] == 'T') {
               DEBUG(D_acl)
-                debug_printf("Overall result = T\treturning DEFER\n");
+                debug_printf("DCC: Overall result = T\treturning DEFER\n");
               retval = DEFER;
               log_write(0,LOG_MAIN,"Temporary error with DCC: %s\n", recvbuf);
               Ustrcpy(dcc_return_text, "Temporary error with DCC");
@@ -426,7 +424,7 @@ int dcc_process(uschar **listptr) {
             }
             else {
               DEBUG(D_acl)
-                debug_printf("Overall result = something else\treturning DEFER\n");
+                debug_printf("DCC: Overall result = something else\treturning DEFER\n");
               retval = DEFER;
               log_write(0,LOG_MAIN,"Unknown DCC response: %s\n", recvbuf);
               Ustrcpy(dcc_return_text, "Unknown DCC response");
@@ -437,33 +435,33 @@ int dcc_process(uschar **listptr) {
           /* We're on the first line but not on the first character,
            * there must be something wrong. */
             DEBUG(D_acl)
-              debug_printf("Line = %d but i = %d != 0  character is %c - This is wrong!\n", line, i, recvbuf[i]);
+              debug_printf("DCC: Line = %d but i = %d != 0  character is %c - This is wrong!\n", line, i, recvbuf[i]);
               log_write(0,LOG_MAIN,"Wrong header from DCC, output is %s\n", recvbuf);
           }
         }
         else if(line == 2) {
           /* On the second line we get a list of
-           * answer for each recipient. We don't care about
-           * it because we're in an acl and so just take the
+           * answers for each recipient. We don't care about
+           * it because we're in an acl and take the
            * global result. */
         }
         else if(line > 2) {
-          /* The third and following lines is the X-DCC header,
-           * so we store it in xhdr. */
-          /* check if we don't get more than what we can handle */
-          if(k < sizeof(xhdr)) { /* xhdr has a length of 120 */
-            xhdr[k] = recvbuf[i];
+          /* The third and following lines are the X-DCC header,
+           * so we store it in dcc_header_str. */
+          /* check if we don't get more than we can handle */
+          if(k < sizeof(dcc_header_str)) { 
+            dcc_header_str[k] = recvbuf[i];
             k++;
           }
           else {
             DEBUG(D_acl)
-              debug_printf("We got more output than we can store in the X-DCC header. Truncating at 120 characters.\n");
+              debug_printf("DCC: We got more output than we can store in the X-DCC header. Truncating at 120 characters.\n");
           }
         }
         else {
           /* Wrong line number. There must be a problem with the output. */
           DEBUG(D_acl)
-            debug_printf("Wrong line number in output. Line number is %d\n", line);
+            debug_printf("DCC: Wrong line number in output. Line number is %d\n", line);
         }
       }
     }
@@ -472,26 +470,26 @@ int dcc_process(uschar **listptr) {
   }
   /* We have read everything from the socket */
 
-  /* We need the terminate the X-DCC header with a '\n' character. This needs to be k-1
-   * for xhdr[k] contains '\0'. */
-  xhdr[k-1] = '\n';
+  /* We need to terminate the X-DCC header with a '\n' character. This needs to be k-1
+   * since dcc_header_str[k] contains '\0'. */
+  dcc_header_str[k-1] = '\n';
 
   /* Now let's sum up what we've got. */
   DEBUG(D_acl)
-    debug_printf("\n--------------------------\nOverall result = %d\nX-DCC header: %sReturn message: %s\ndcc_result: %s\n", retval, xhdr, dcc_return_text, dcc_result);
+    debug_printf("\nDCC: --------------------------\nDCC: Overall result = %d\nDCC: X-DCC header: %sReturn message: %s\nDCC: dcc_result: %s\n", retval, dcc_header_str, dcc_return_text, dcc_result);
 
   /* We only add the X-DCC header if it starts with X-DCC */
-  if(!(Ustrncmp(xhdr, "X-DCC", 5))){
-    dcc_header = xhdr;
+  if(!(Ustrncmp(dcc_header_str, "X-DCC", 5))){
+    dcc_header = dcc_header_str;
     if(dcc_direct_add_header) {
-      header_add(' ' , "%s", xhdr);
+      header_add(' ' , "%s", dcc_header_str);
   /* since the MIME ACL already writes the .eml file to disk without DCC Header we've to erase it */
       unspool_mbox();
     }
   }
   else {
     DEBUG(D_acl)
-      debug_printf("Wrong format of the X-DCC header: %s\n", xhdr);
+      debug_printf("DCC: Wrong format of the X-DCC header: %s\n", dcc_header_str);
   }
 
   /* check if we should add additional headers passed in acl_m_dcc_add_header */
@@ -502,14 +500,14 @@ int dcc_process(uschar **listptr) {
         Ustrcat(dcc_xtra_hdrs, "\n");
       header_add(' ', "%s", dcc_xtra_hdrs);
       DEBUG(D_acl)
-        debug_printf("adding additional headers in $acl_m_dcc_add_header: %s", dcc_xtra_hdrs);
+        debug_printf("DCC: adding additional headers in $acl_m_dcc_add_header: %s", dcc_xtra_hdrs);
     }
   }
 
   dcc_ok = 1;
   /* Now return to exim main process */
   DEBUG(D_acl)
-    debug_printf("Before returning to exim main process:\nreturn_text = %s - retval = %d\ndcc_result = %s\n", dcc_return_text, retval, dcc_result);
+    debug_printf("DCC: Before returning to exim main process:\nDCC: return_text = %s - retval = %d\nDCC: dcc_result = %s\n", dcc_return_text, retval, dcc_result);
 
   (void)fclose(data_file);
   dcc_rc = retval;