Tidying: Issues detected by gcc --fsanitize=undefined
[exim.git] / src / src / transports / autoreply.c
index 341b7b0c068f00cc861a487940b0f1fffdeebc23..e93267e4876d4d67862e3a77df852ab2ef33f325 100644 (file)
@@ -1,10 +1,8 @@
-/* $Cambridge: exim/src/src/transports/autoreply.c,v 1.1 2004/10/07 13:10:02 ph10 Exp $ */
-
 /*************************************************
 *     Exim - an Internet mail transport agent    *
 *************************************************/
 
-/* Copyright (c) University of Cambridge 1995 - 2004 */
+/* Copyright (c) University of Cambridge 1995 - 2015 */
 /* See the file NOTICE for conditions of use and distribution. */
 
 
@@ -159,12 +157,13 @@ if (ss == NULL)
 if (type != cke_text) for (t = ss; *t != 0; t++)
   {
   int c = *t;
+  const uschar * sp;
   if (mac_isprint(c)) continue;
   if (type == cke_hdr && c == '\n' && (t[1] == ' ' || t[1] == '\t')) continue;
-  s = string_printing(s);
+  sp = string_printing(s);
   addr->transport_return = FAIL;
   addr->message = string_sprintf("Expansion of \"%s\" in %s transport "
-    "contains non-printing character %d", s, name, c);
+    "contains non-printing character %d", sp, name, c);
   return NULL;
   }
 
@@ -189,7 +188,7 @@ Returns:      nothing
 */
 
 static void
-check_never_mail(uschar **listptr, uschar *never_mail)
+check_never_mail(uschar **listptr, const uschar *never_mail)
 {
 uschar *s = *listptr;
 
@@ -268,7 +267,6 @@ autoreply_transport_entry(
 {
 int fd, pid, rc;
 int cache_fd = -1;
-int log_fd = -1;
 int cache_size = 0;
 int add_size = 0;
 EXIM_DB *dbm_file = NULL;
@@ -277,6 +275,7 @@ uschar *from, *reply_to, *to, *cc, *bcc, *subject, *headers, *text, *file;
 uschar *logfile, *oncelog;
 uschar *cache_buff = NULL;
 uschar *cache_time = NULL;
+uschar *message_id = NULL;
 header_line *h;
 time_t now = time(NULL);
 time_t once_repeat_sec = 0;
@@ -380,7 +379,7 @@ remove those that match. */
 
 if (ob->never_mail != NULL)
   {
-  uschar *never_mail = expand_string(ob->never_mail);
+  const uschar *never_mail = expand_string(ob->never_mail);
 
   if (never_mail == NULL)
     {
@@ -420,7 +419,7 @@ recipient, the effect might not be quite as envisaged. If once_file_size is
 set, instead of a dbm file, we use a regular file containing a circular buffer
 recipient cache. */
 
-if (oncelog != NULL && to != NULL)
+if (oncelog != NULL && *oncelog != 0 && to != NULL)
   {
   time_t then = 0;
 
@@ -522,16 +521,19 @@ if (oncelog != NULL && to != NULL)
 
   if (then != 0 && (once_repeat_sec <= 0 || now - then < once_repeat_sec))
     {
+    int log_fd;
     DEBUG(D_transport) debug_printf("message previously sent to %s%s\n", to,
       (once_repeat_sec > 0)? " and repeat time not reached" : "");
-    log_fd = Uopen(logfile, O_WRONLY|O_APPEND|O_CREAT, ob->mode);
+    log_fd = logfile ? Uopen(logfile, O_WRONLY|O_APPEND|O_CREAT, ob->mode) : -1;
     if (log_fd >= 0)
       {
       uschar *ptr = log_buffer;
       sprintf(CS ptr, "%s\n  previously sent to %.200s\n", tod_stamp(tod_log), to);
       while(*ptr) ptr++;
-      write(log_fd, log_buffer, ptr - log_buffer);
-      close(log_fd);
+      if(write(log_fd, log_buffer, ptr - log_buffer) != ptr-log_buffer
+        || close(log_fd))
+        DEBUG(D_transport) debug_printf("Problem writing log file %s for %s "
+          "transport\n", logfile, tblock->name);
       }
     goto END_OFF;
     }
@@ -590,9 +592,57 @@ for (h = header_list; h != NULL; h = h->next)
 
 if (h != NULL)
   {
-  uschar *s = Ustrchr(h->text, ':') + 1;
-  while (isspace(*s)) s++;
-  fprintf(f, "In-Reply-To: %s", s);
+  message_id = Ustrchr(h->text, ':') + 1;
+  while (isspace(*message_id)) message_id++;
+  fprintf(f, "In-Reply-To: %s", message_id);
+  }
+
+/* Generate a References header if there is at least one of Message-ID:,
+References:, or In-Reply-To: (see RFC 2822). */
+
+for (h = header_list; h != NULL; h = h->next)
+  if (h->type != htype_old && strncmpic(US"References:", h->text, 11) == 0)
+    break;
+
+if (h == NULL)
+  for (h = header_list; h != NULL; h = h->next)
+    if (h->type != htype_old && strncmpic(US"In-Reply-To:", h->text, 12) == 0)
+      break;
+
+/* We limit the total length of references.  Although there is no fixed
+limit, some systems do not like headers growing beyond recognition.
+Keep the first message ID for the thread root and the last few for
+the position inside the thread, up to a maximum of 12 altogether. */
+
+if (h != NULL || message_id != NULL)
+  {
+  fprintf(f, "References:");
+  if (h != NULL)
+    {
+    uschar *s, *id, *error;
+    uschar *referenced_ids[12];
+    int reference_count = 0;
+    int i;
+
+    s = Ustrchr(h->text, ':') + 1;
+    parse_allow_group = FALSE;
+    while (*s != 0 && (s = parse_message_id(s, &id, &error)) != NULL)
+      {
+      if (reference_count == sizeof(referenced_ids)/sizeof(uschar *))
+        {
+        memmove(referenced_ids + 1, referenced_ids + 2,
+           sizeof(referenced_ids) - 2*sizeof(uschar *));
+        referenced_ids[reference_count - 1] = id;
+        }
+      else referenced_ids[reference_count++] = id;
+      }
+    for (i = 0; i < reference_count; ++i) fprintf(f, " %s", referenced_ids[i]);
+    }
+
+  /* The message id will have a newline on the end of it. */
+
+  if (message_id != NULL) fprintf(f, " %s", message_id);
+    else fprintf(f, "\n");
   }
 
 /* Add an Auto-Submitted: header */
@@ -627,31 +677,36 @@ if (ff != NULL)
       }
     else fprintf(f, "%s", CS big_buffer);
     }
+  (void) fclose(ff);
   }
 
 /* Copy the original message if required, observing the return size
-limit. */
+limit if we are returning the body. */
 
 if (return_message)
   {
-  if (bounce_return_size_limit > 0)
+  uschar *rubric = (tblock->headers_only)?
+    US"------ This is a copy of the message's header lines.\n"
+    : (tblock->body_only)?
+    US"------ This is a copy of the body of the message, without the headers.\n"
+    :
+    US"------ This is a copy of the message, including all the headers.\n";
+
+  if (bounce_return_size_limit > 0 && !tblock->headers_only)
     {
     struct stat statbuf;
     int max = (bounce_return_size_limit/DELIVER_IN_BUFFER_SIZE + 1) *
       DELIVER_IN_BUFFER_SIZE;
     if (fstat(deliver_datafile, &statbuf) == 0 && statbuf.st_size > max)
       {
-      int size = statbuf.st_size;  /* Because might be a long */
-      fprintf(f, "\n"
-"------ This is a copy of the message, including all the headers.\n"
-"------ The body of the message is %d characters long; only the first\n"
-"------ %d or so are included here.\n\n", size, (max/1000)*1000);
+      fprintf(f, "\n%s"
+"------ The body of the message is " OFF_T_FMT " characters long; only the first\n"
+"------ %d or so are included here.\n\n", rubric, statbuf.st_size,
+        (max/1000)*1000);
       }
-    else fprintf(f, "\n"
-"------ This is a copy of the message, including all the headers. ------\n\n");
+    else fprintf(f, "\n%s\n", rubric);
     }
-  else fprintf(f, "\n"
-"------ This is a copy of the message, including all the headers. ------\n\n");
+  else fprintf(f, "\n%s\n", rubric);
 
   fflush(f);
   transport_count = 0;
@@ -667,7 +722,7 @@ if (return_message)
 
 /* End the message and wait for the child process to end; no timeout. */
 
-fclose(f);
+(void)fclose(f);
 rc = child_close(pid, 0);
 
 /* Update the "sent to" log whatever the yield. This errs on the side of
@@ -702,7 +757,9 @@ if (cache_fd >= 0)
     }
 
   memcpy(cache_time, &now, sizeof(time_t));
-  write(cache_fd, from, size);
+  if(write(cache_fd, from, size) != size)
+    DEBUG(D_transport) debug_printf("Problem writing cache file %s for %s "
+      "transport\n", oncelog, tblock->name);
   }
 
 /* Update DBM file */
@@ -798,8 +855,10 @@ if (logfile != NULL)
         "  %s\n", headers);
       while(*ptr) ptr++;
       }
-    write(log_fd, log_buffer, ptr - log_buffer);
-    close(log_fd);
+    if(write(log_fd, log_buffer, ptr - log_buffer) != ptr-log_buffer
+      || close(log_fd))
+      DEBUG(D_transport) debug_printf("Problem writing log file %s for %s "
+        "transport\n", logfile, tblock->name);
     }
   else DEBUG(D_transport) debug_printf("Failed to open log file %s for %s "
     "transport: %s\n", logfile, tblock->name, strerror(errno));
@@ -807,7 +866,7 @@ if (logfile != NULL)
 
 END_OFF:
 if (dbm_file != NULL) EXIM_DBCLOSE(dbm_file);
-if (cache_fd > 0) close(cache_fd);
+if (cache_fd > 0) (void)close(cache_fd);
 
 DEBUG(D_transport) debug_printf("%s transport succeeded\n", tblock->name);