Recast more internal string routines to use growable-strings
[exim.git] / src / src / dcc.c
index 680eea585ce807707dd8d001a4759eea138e0a41..5aa2b17aafacc08b9282f956679e3c7850bc535a 100644 (file)
@@ -2,10 +2,12 @@
 *     Exim - an Internet mail transport agent    *
 *************************************************/
 
-/* Copyright (c) Wolfgang Breyha 2005-2012
+/* Copyright (c) Wolfgang Breyha 2005 - 2015
  * Vienna University Computer Center
  * wbreyha@gmx.net
  * See the file NOTICE for conditions of use and distribution.
+ *
+ * Copyright (c) The Exim Maintainers 2015 - 2018
  */
 
 /* This patch is based on code from Tom Kistners exiscan (ACL integration) and
@@ -36,7 +38,8 @@ int flushbuffer (int socket, uschar *buffer)
     DEBUG(D_acl)
       debug_printf("DCC: Error writing buffer to socket: %s\n", strerror(errno));
     retval = errno;
-  } else {
+  }
+  else {
     DEBUG(D_acl)
       debug_printf("DCC: Wrote buffer to socket:\n%s\n", buffer);
     retval = 0;
@@ -44,16 +47,17 @@ int flushbuffer (int socket, uschar *buffer)
   return retval;
 }
 
-int dcc_process(uschar **listptr) {
+int
+dcc_process(uschar **listptr)
+{
   int sep = 0;
-  uschar *list = *listptr;
+  const 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;
@@ -68,7 +72,6 @@ int dcc_process(uschar **listptr) {
   uschar sendbuf[4096];
   uschar recvbuf[4096];
   uschar dcc_return_text[1024];
-  uschar mbox_path[1024];
   uschar message_subdir[2];
   struct header_line *dcchdr;
   uschar *dcc_acl_options;
@@ -77,50 +80,42 @@ int dcc_process(uschar **listptr) {
 
   /* grep 1st option */
   if ((dcc_acl_options = string_nextinlist(&list, &sep,
-                                           dcc_acl_options_buffer,
-                                           sizeof(dcc_acl_options_buffer))) != NULL)
-  {
+                  dcc_acl_options_buffer, sizeof(dcc_acl_options_buffer))))
+    {
     /* parse 1st option */
-    if ( (strcmpic(dcc_acl_options,US"false") == 0) ||
-         (Ustrcmp(dcc_acl_options,"0") == 0) ) {
-      /* explicitly no matching */
-      return FAIL;
-    };
-
-    /* special cases (match anything except empty) */
-    if ( (strcmpic(dcc_acl_options,US"true") == 0) ||
-         (Ustrcmp(dcc_acl_options,"*") == 0) ||
-         (Ustrcmp(dcc_acl_options,"1") == 0) ) {
-      dcc_acl_options = dcc_acl_options;
-    };
-  }
-  else {
-    /* empty means "don't match anything" */
-    return FAIL;
-  };
+    if (  strcmpic(dcc_acl_options, US"false") == 0
+       || Ustrcmp(dcc_acl_options, "0") == 0
+       )
+      return FAIL;     /* explicitly no matching */
+    }
+  else
+    return FAIL;       /* empty means "don't match anything" */
 
   sep = 0;
 
   /* if we scanned this message last time, just return */
-  if ( dcc_ok )
-      return dcc_rc;
+  if (dcc_ok)
+    return dcc_rc;
 
   /* open the spooled body */
   message_subdir[1] = '\0';
-  for (i = 0; i < 2; i++) {
-    message_subdir[0] = (split_spool_directory == (i == 0))? message_id[5] : 0;
-    sprintf(CS mbox_path, "%s/input/%s/%s-D", spool_directory, message_subdir, message_id);
-    data_file = Ufopen(mbox_path,"rb");
-    if (data_file != NULL)
+  for (i = 0; i < 2; i++)
+    {
+    message_subdir[0] = split_spool_directory == (i == 0) ? message_id[5] : 0;
+
+    if ((data_file = Ufopen(
+           spool_fname(US"input", message_subdir, message_id, US"-D"),
+           "rb")))
       break;
-  };
+    }
 
-  if (data_file == NULL) {
+  if (!data_file)
+    {
     /* error while spooling */
     log_write(0, LOG_MAIN|LOG_PANIC,
            "dcc acl condition: error while opening spool file");
     return DEFER;
-  };
+    }
 
   /* Initialize the variables */
 
@@ -140,24 +135,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("DCC: 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("DCC: 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);
@@ -186,12 +183,12 @@ 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));
+    ipaddress = gethostbyname(CS sockip);
+    bzero(CS  &serv_addr_in, sizeof(serv_addr_in));
     serv_addr_in.sin_family = AF_INET;
-    bcopy((char *)ipaddress->h_addr, (char *)&serv_addr_in.sin_addr.s_addr, ipaddress->h_length);
+    bcopy(CS ipaddress->h_addr, CS &serv_addr_in.sin_addr.s_addr, ipaddress->h_length);
     serv_addr_in.sin_port = htons(portnr);
     if ((sockfd = socket(AF_INET, SOCK_STREAM,0)) < 0){
       DEBUG(D_acl)
@@ -212,9 +209,9 @@ int dcc_process(uschar **listptr) {
     }
   } else {
     /* connecting to the dccifd UNIX socket */
-    bzero((char *)&serv_addr,sizeof(serv_addr));
+    bzero(&serv_addr, sizeof(serv_addr));
     serv_addr.sun_family = AF_UNIX;
-    Ustrcpy(serv_addr.sun_path, sockpath);
+    Ustrncpy(serv_addr.sun_path, sockpath, sizeof(serv_addr.sun_path));
     if ((sockfd = socket(AF_UNIX, SOCK_STREAM,0)) < 0){
       DEBUG(D_acl)
         debug_printf("DCC: Creating UNIX socket connection failed: %s\n", strerror(errno));
@@ -300,7 +297,7 @@ int dcc_process(uschar **listptr) {
     }
   }
 
-  /* a blank line seperates header from body */
+  /* a blank line separates header from body */
   Ustrncat(sendbuf, "\n", sizeof(sendbuf)-Ustrlen(sendbuf)-1);
   flushbuffer(sockfd, sendbuf);
   DEBUG(D_acl)
@@ -430,11 +427,11 @@ int dcc_process(uschar **listptr) {
             }
           }
           else {
-          /* We're on the first line but not on the first character,
-           * there must be something wrong. */
-            DEBUG(D_acl)
-              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);
+            /* We're on the first line but not on the first character,
+             * there must be something wrong. */
+            DEBUG(D_acl) 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) {
@@ -447,13 +444,13 @@ int dcc_process(uschar **listptr) {
           /* 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)) { 
+          if(k < sizeof(dcc_header_str)) {
             dcc_header_str[k] = recvbuf[i];
             k++;
           }
           else {
-            DEBUG(D_acl)
-              debug_printf("DCC: We got more output than we can store in the X-DCC header. Truncating at 120 characters.\n");
+            DEBUG(D_acl) debug_printf("DCC: We got more output than we can store"
+               " in the X-DCC header. Truncating at 120 characters.\n");
           }
         }
         else {