RFC3461 support - MIME DSN messages. Bug 118
[exim.git] / src / src / spool_out.c
index bbfd785d255c7c85e2d058449021f8b95dab7b4f..01b70341d12794fa3072c2bba1868a2fdbc5a7f0 100644 (file)
@@ -1,10 +1,8 @@
-/* $Cambridge: exim/src/src/spool_out.c,v 1.10 2006/02/07 11:19:00 ph10 Exp $ */
-
 /*************************************************
 *     Exim - an Internet mail transport agent    *
 *************************************************/
 
-/* Copyright (c) University of Cambridge 1995 - 2006 */
+/* Copyright (c) University of Cambridge 1995 - 2012 */
 /* See the file NOTICE for conditions of use and distribution. */
 
 /* Functions for writing spool files, and moving them about. */
@@ -95,10 +93,12 @@ double-check the mode because the group setting doesn't always get set
 automatically. */
 
 if (fd >= 0)
-  {
-  (void)fchown(fd, exim_uid, exim_gid);
-  (void)fchmod(fd, SPOOL_MODE);
-  }
+  if (fchown(fd, exim_uid, exim_gid) || fchmod(fd, SPOOL_MODE))
+    {
+    DEBUG(D_any) debug_printf("failed setting perms on %s\n", temp_name);
+    (void) close(fd); fd = -1;
+    Uunlink(temp_name);
+    }
 
 return fd;
 }
@@ -190,25 +190,15 @@ if (sender_ident != NULL) fprintf(f, "-ident %s\n", sender_ident);
 if (received_protocol != NULL)
   fprintf(f, "-received_protocol %s\n", received_protocol);
 
-/* Preserve any ACL variables that are set. Because the values may contain
-newlines, we use an explicit length. */
+/* Preserve any ACL variables that are set. */
 
-for (i = 0; i < ACL_CVARS; i++)
-  {
-  if (acl_var[i] != NULL)
-    fprintf(f, "-aclc %d %d\n%s\n", i, Ustrlen(acl_var[i]), acl_var[i]);
-  }
-
-for (i = 0; i < ACL_MVARS; i++)
-  {
-  int j = i + ACL_CVARS;
-  if (acl_var[j] != NULL)
-    fprintf(f, "-aclm %d %d\n%s\n", i, Ustrlen(acl_var[j]), acl_var[j]);
-  }
+tree_walk(acl_var_c, &acl_var_write, f);
+tree_walk(acl_var_m, &acl_var_write, f);
 
 /* Now any other data that needs to be remembered. */
 
 fprintf(f, "-body_linecount %d\n", body_linecount);
+fprintf(f, "-max_received_linelength %d\n", max_received_linelength);
 
 if (body_zerocount > 0) fprintf(f, "-body_zerocount %d\n", body_zerocount);
 
@@ -238,9 +228,29 @@ if (bmi_verdicts != NULL) fprintf(f, "-bmi_verdicts %s\n", bmi_verdicts);
 #endif
 
 #ifdef SUPPORT_TLS
-if (tls_certificate_verified) fprintf(f, "-tls_certificate_verified\n");
-if (tls_cipher != NULL) fprintf(f, "-tls_cipher %s\n", tls_cipher);
-if (tls_peerdn != NULL) fprintf(f, "-tls_peerdn %s\n", tls_peerdn);
+if (tls_in.certificate_verified) fprintf(f, "-tls_certificate_verified\n");
+if (tls_in.cipher)       fprintf(f, "-tls_cipher %s\n", tls_in.cipher);
+if (tls_in.peercert)
+  {
+  (void) tls_export_cert(big_buffer, big_buffer_size, tls_in.peercert);
+  fprintf(f, "-tls_peercert %s\n", CS big_buffer);
+  }
+if (tls_in.peerdn)       fprintf(f, "-tls_peerdn %s\n", string_printing(tls_in.peerdn));
+if (tls_in.sni)                 fprintf(f, "-tls_sni %s\n",    string_printing(tls_in.sni));
+if (tls_in.ourcert)
+  {
+  (void) tls_export_cert(big_buffer, big_buffer_size, tls_in.ourcert);
+  fprintf(f, "-tls_ourcert %s\n", CS big_buffer);
+  }
+if (tls_in.ocsp)        fprintf(f, "-tls_ocsp %d\n",   tls_in.ocsp);
+#endif
+
+#ifdef EXPERIMENTAL_DSN
+/* Write the dsn flags to the spool header file */
+DEBUG(D_deliver) debug_printf("DSN: Write SPOOL :-dsn_envid %s\n", dsn_envid);
+if (dsn_envid != NULL) fprintf(f, "-dsn_envid %s\n", dsn_envid);
+DEBUG(D_deliver) debug_printf("DSN: Write SPOOL :-dsn_ret %d\n", dsn_ret);
+if (dsn_ret != 0) fprintf(f, "-dsn_ret %d\n", dsn_ret);
 #endif
 
 /* To complete the envelope, write out the tree of non-recipients, followed by
@@ -253,14 +263,34 @@ fprintf(f, "%d\n", recipients_count);
 for (i = 0; i < recipients_count; i++)
   {
   recipient_item *r = recipients_list + i;
-  if (r->pno < 0 && r->errors_to == NULL)
+#ifdef EXPERIMENTAL_DSN
+DEBUG(D_deliver) debug_printf("DSN: Flags :%d\n", r->dsn_flags);
+#endif
+  if (r->pno < 0 && r->errors_to == NULL
+    #ifdef EXPERIMENTAL_DSN
+     && r->dsn_flags == 0
+    #endif
+    )
     fprintf(f, "%s\n", r->address);
   else
     {
     uschar *errors_to = (r->errors_to == NULL)? US"" : r->errors_to;
+    #ifdef EXPERIMENTAL_DSN
+    /* for DSN SUPPORT extend exim 4 spool in a compatible way by
+       adding new values upfront and add flag 0x02 */
+    uschar *orcpt = (r->orcpt == NULL)? US"" : r->orcpt;
+    fprintf(f, "%s %s %d,%d %s %d,%d#3\n", r->address, orcpt, Ustrlen(orcpt), r->dsn_flags,
+      errors_to, Ustrlen(errors_to), r->pno);
+    #else
     fprintf(f, "%s %s %d,%d#1\n", r->address, errors_to,
       Ustrlen(errors_to), r->pno);
+    #endif
     }
+    
+    #ifdef EXPERIMENTAL_DSN
+      DEBUG(D_deliver) debug_printf("DSN: **** SPOOL_OUT - address: |%s| errorsto: |%s| orcpt: |%s| dsn_flags: %d\n",
+         r->address, r->errors_to, r->orcpt, r->dsn_flags);
+    #endif
   }
 
 /* Put a blank line before the headers */
@@ -298,7 +328,7 @@ if (fflush(f) != 0 || ferror(f))
 just pushes it out of C, and fclose() doesn't guarantee to do the write
 either. That's just the way Unix works... */
 
-if (fsync(fileno(f)) < 0)
+if (EXIMfsync(fileno(f)) < 0)
   return spool_write_error(where, errmsg, US"sync", temp_name, f);
 
 /* Get the size of the file, and close it. */
@@ -336,7 +366,7 @@ sprintf(CS temp_name, "%s/input/%s/.", spool_directory, message_subdir);
 if ((fd = Uopen(temp_name, O_RDONLY|O_DIRECTORY, 0)) < 0)
   return spool_write_error(where, errmsg, US"directory open", name, NULL);
 
-if (fsync(fd) < 0 && errno != EINVAL)
+if (EXIMfsync(fd) < 0 && errno != EINVAL)
   return spool_write_error(where, errmsg, US"directory sync", name, NULL);
 
 if (close(fd) < 0)