Add support for setclassresources() in the pipe transport on FreeBSD,
[exim.git] / src / src / mime.c
index 0a636720088b5ba1b96fb9e174fd19aa45bdfca6..e0ac66cbe9f666969aa00b44708501474eb0353b 100644 (file)
@@ -1,4 +1,4 @@
-/* $Cambridge: exim/src/src/mime.c,v 1.4 2005/02/17 11:58:26 ph10 Exp $ */
+/* $Cambridge: exim/src/src/mime.c,v 1.13 2005/11/15 10:08:25 ph10 Exp $ */
 
 /*************************************************
 *     Exim - an Internet mail transport agent    *
@@ -115,10 +115,7 @@ uschar *mime_decode_qp_char(uschar *qp_p,int *c) {
 }
 
 
-uschar *mime_parse_line(uschar *buffer, uschar *encoding, int *num_decoded) {
-  uschar *data = NULL;
-
-  data = (uschar *)malloc(Ustrlen(buffer)+2);
+uschar *mime_parse_line(uschar *buffer, uschar *data, uschar *encoding, int *num_decoded) {
 
   if (encoding == NULL) {
     /* no encoding type at all */
@@ -162,7 +159,6 @@ uschar *mime_parse_line(uschar *buffer, uschar *encoding, int *num_decoded) {
 
       /* byte 0 ---------------------- */
       if (*(p+1) == 255) {
-        mime_set_anomaly(MIME_ANOMALY_BROKEN_BASE64);
         break;
       }
       data[(*num_decoded)] = *p;
@@ -174,7 +170,6 @@ uschar *mime_parse_line(uschar *buffer, uschar *encoding, int *num_decoded) {
       p++;
       /* byte 1 ---------------------- */
       if (*(p+1) == 255) {
-        mime_set_anomaly(MIME_ANOMALY_BROKEN_BASE64);
         break;
       }
       data[(*num_decoded)] = *p;
@@ -186,7 +181,6 @@ uschar *mime_parse_line(uschar *buffer, uschar *encoding, int *num_decoded) {
       p++;
       /* byte 2 ---------------------- */
       if (*(p+1) == 255) {
-        mime_set_anomaly(MIME_ANOMALY_BROKEN_BASE64);
         break;
       }
       data[(*num_decoded)] = *p;
@@ -246,11 +240,11 @@ FILE *mime_get_decode_file(uschar *pname, uschar *fname) {
   filename = (uschar *)malloc(2048);
 
   if ((pname != NULL) && (fname != NULL)) {
-    snprintf(CS filename, 2048, "%s/%s", pname, fname);
-    f = fopen(CS filename,"w+");
+    (void)string_format(filename, 2048, "%s/%s", pname, fname);
+    f = fopen(CS filename,"wb+");
   }
   else if (pname == NULL) {
-    f = fopen(CS fname,"w+");
+    f = fopen(CS fname,"wb+");
   }
   else if (fname == NULL) {
     int file_nr = 0;
@@ -259,7 +253,7 @@ FILE *mime_get_decode_file(uschar *pname, uschar *fname) {
     /* must find first free sequential filename */
     do {
       struct stat mystat;
-      snprintf(CS filename,2048,"%s/%s-%05u", pname, message_id, file_nr);
+      (void)string_format(filename,2048,"%s/%s-%05u", pname, message_id, file_nr);
       file_nr++;
       /* security break */
       if (file_nr >= 1024)
@@ -267,7 +261,7 @@ FILE *mime_get_decode_file(uschar *pname, uschar *fname) {
       result = stat(CS filename,&mystat);
     }
     while(result != -1);
-    f = fopen(CS filename,"w+");
+    f = fopen(CS filename,"wb+");
   };
 
   /* set expansion variable */
@@ -285,6 +279,7 @@ int mime_decode(uschar **listptr) {
   uschar decode_path[1024];
   FILE *decode_file = NULL;
   uschar *buffer = NULL;
+  uschar *decode_buffer = NULL;
   long f_pos = 0;
   unsigned int size_counter = 0;
 
@@ -294,9 +289,9 @@ int mime_decode(uschar **listptr) {
   f_pos = ftell(mime_stream);
 
   /* build default decode path (will exist since MBOX must be spooled up) */
-  snprintf(CS decode_path,1024,"%s/scan/%s",spool_directory,message_id);
+  (void)string_format(decode_path,1024,"%s/scan/%s",spool_directory,message_id);
 
-  /* reserve a line buffer to work in */
+  /* reserve a line and decoder buffer to work in */
   buffer = (uschar *)malloc(MIME_MAX_LINE_LENGTH+1);
   if (buffer == NULL) {
     log_write(0, LOG_PANIC,
@@ -304,6 +299,13 @@ int mime_decode(uschar **listptr) {
     return DEFER;
   };
 
+  decode_buffer = (uschar *)malloc(MIME_MAX_LINE_LENGTH+1);
+  if (decode_buffer == NULL) {
+    log_write(0, LOG_PANIC,
+                 "decode ACL condition: can't allocate %d bytes of memory.", MIME_MAX_LINE_LENGTH+1);
+    return DEFER;
+  };
+
   /* try to find 1st option */
   if ((option = string_nextinlist(&list, &sep,
                                   option_buffer,
@@ -358,7 +360,8 @@ int mime_decode(uschar **listptr) {
       };
     };
 
-    decoded_line = mime_parse_line(buffer, mime_content_transfer_encoding, &decoded_line_length);
+    decoded_line = mime_parse_line(buffer, decode_buffer, mime_content_transfer_encoding, &decoded_line_length);
+
     /* write line to decode file */
     if (fwrite(decoded_line, 1, decoded_line_length, decode_file) < decoded_line_length) {
       /* error/short write */
@@ -376,10 +379,9 @@ int mime_decode(uschar **listptr) {
       size_counter = (size_counter % 1024);
     };
 
-    free(decoded_line);
   }
 
-  fclose(decode_file);
+  (void)fclose(decode_file);
 
   clearerr(mime_stream);
   fseek(mime_stream,f_pos,SEEK_SET);
@@ -413,7 +415,7 @@ int mime_get_header(FILE *f, uschar *header) {
         c = fgetc(f);
         if (c == EOF) break;
         if ( (c == '\t') || (c == ' ') ) continue;
-        ungetc(c,f);
+        (void)ungetc(c,f);
       };
       /* end of the header, terminate with ';' */
       c = ';';
@@ -482,7 +484,7 @@ int mime_get_header(FILE *f, uschar *header) {
     };
   };
 
-  if (header[num_copied-1] != ';') {
+  if ((num_copied > 0) && (header[num_copied-1] != ';')) {
     header[num_copied-1] = ';';
   };
 
@@ -497,8 +499,8 @@ int mime_get_header(FILE *f, uschar *header) {
 }
 
 
-int mime_acl_check(FILE *f, struct mime_boundary_context *context, uschar
-                   **user_msgptr, uschar **log_msgptr) {
+int mime_acl_check(uschar *acl, FILE *f, struct mime_boundary_context *context,
+                   uschar **user_msgptr, uschar **log_msgptr) {
   int rc = OK;
   uschar *header = NULL;
   struct mime_boundary_context nested_context;
@@ -507,7 +509,7 @@ int mime_acl_check(FILE *f, struct mime_boundary_context *context, uschar
   header = (uschar *)malloc(MIME_MAX_HEADER_SIZE+1);
   if (header == NULL) {
     log_write(0, LOG_PANIC,
-                 "acl_smtp_mime: can't allocate %d bytes of memory.", MIME_MAX_HEADER_SIZE+1);
+                 "MIME ACL: can't allocate %d bytes of memory.", MIME_MAX_HEADER_SIZE+1);
     return DEFER;
   };
 
@@ -617,7 +619,7 @@ int mime_acl_check(FILE *f, struct mime_boundary_context *context, uschar
                 memset(param_value,0,param_value_len+1);
                 q = p + mime_parameter_list[j].namelen;
                 Ustrncpy(param_value, q, param_value_len);
-                param_value = rfc2047_decode(param_value, TRUE, NULL, 32, &param_value_len, &q);
+                param_value = rfc2047_decode(param_value, check_rfc2047_length, NULL, 32, &param_value_len, &q);
                 debug_printf("Found %s MIME parameter in %s header, value is '%s'\n", mime_parameter_list[j].name, mime_header_list[i].name, param_value);
                 *((uschar **)(mime_parameter_list[j].value)) = param_value;
                 p += (mime_parameter_list[j].namelen + param_value_len + 1);
@@ -654,7 +656,7 @@ int mime_acl_check(FILE *f, struct mime_boundary_context *context, uschar
     mime_is_coverletter = !(context && context->context == MBC_ATTACHMENT);
 
     /* call ACL handling function */
-    rc = acl_check(ACL_WHERE_MIME, NULL, acl_smtp_mime, user_msgptr, log_msgptr);
+    rc = acl_check(ACL_WHERE_MIME, NULL, acl, user_msgptr, log_msgptr);
 
     mime_stream = NULL;
     mime_current_boundary = NULL;
@@ -675,7 +677,7 @@ int mime_acl_check(FILE *f, struct mime_boundary_context *context, uschar
       else
         nested_context.context = MBC_COVERLETTER_ONESHOT;
 
-      rc = mime_acl_check(f, &nested_context, user_msgptr, log_msgptr);
+      rc = mime_acl_check(acl, f, &nested_context, user_msgptr, log_msgptr);
       if (rc != OK) break;
     }
     else if ( (mime_content_type != NULL) &&
@@ -688,7 +690,7 @@ int mime_acl_check(FILE *f, struct mime_boundary_context *context, uschar
       /* must find first free sequential filename */
       do {
         struct stat mystat;
-        snprintf(CS filename,2048,"%s/scan/%s/__rfc822_%05u", spool_directory, message_id, file_nr);
+        (void)string_format(filename,2048,"%s/scan/%s/__rfc822_%05u", spool_directory, message_id, file_nr);
         file_nr++;
         /* security break */
         if (file_nr >= 128)