Put file-creating fopen() calls in content-scanning code in a wrapper
[exim.git] / src / src / malware.c
index c137d570f09a54ccc3c32bc7e614037d5a5355c6..7e3c587c971d1d4a7a329b6d4712072c4fb13845 100644 (file)
@@ -1,4 +1,4 @@
-/* $Cambridge: exim/src/src/malware.c,v 1.10 2005/06/27 14:29:43 ph10 Exp $ */
+/* $Cambridge: exim/src/src/malware.c,v 1.14 2006/02/22 14:46:44 ph10 Exp $ */
 
 /*************************************************
 *     Exim - an Internet mail transport agent    *
@@ -199,7 +199,7 @@ int malware(uschar **listptr) {
       /* prepare variables */
       drweb_cmd = htonl(DRWEBD_SCAN_CMD);
       drweb_flags = htonl(DRWEBD_RETURN_VIRUSES | DRWEBD_IS_MAIL);
-      snprintf(CS scanrequest, 1024,CS"%s/scan/%s/%s.eml",
+      (void)string_format(scanrequest, 1024,CS"%s/scan/%s/%s.eml",
             spool_directory, message_id, message_id);
 
       /* calc file size */
@@ -287,7 +287,7 @@ int malware(uschar **listptr) {
       /* prepare variables */
       drweb_cmd = htonl(DRWEBD_SCAN_CMD);
       drweb_flags = htonl(DRWEBD_RETURN_VIRUSES | DRWEBD_IS_MAIL);
-      snprintf(CS scanrequest, 1024,CS"%s/scan/%s/%s.eml", spool_directory, message_id, message_id);
+      (void)string_format(scanrequest, 1024,CS"%s/scan/%s/%s.eml", spool_directory, message_id, message_id);
       drweb_slen = htonl(Ustrlen(scanrequest));
 
       /* send scan request */
@@ -446,7 +446,7 @@ int malware(uschar **listptr) {
       };
 
       /* prepare our command */
-      snprintf(CS buf, 32768, "SCAN bPQRSTUW %s/scan/%s/%s.eml\r\n", spool_directory, message_id, message_id);
+      (void)string_format(buf, 32768, "SCAN bPQRSTUW %s/scan/%s/%s.eml\r\n", spool_directory, message_id, message_id);
 
       /* and send it */
       if (send(sock, buf, Ustrlen(buf), 0) < 0) {
@@ -479,7 +479,7 @@ int malware(uschar **listptr) {
       }
 
       /* prepare our command */
-      snprintf(CS buf, 32768, "quit\r\n");
+      (void)string_format(buf, 32768, "quit\r\n");
 
       /* and send it */
       if (send(sock, buf, Ustrlen(buf), 0) < 0) {
@@ -571,7 +571,7 @@ int malware(uschar **listptr) {
       };
 
       /* pass the mailfile to fsecure */
-      snprintf(CS file_name,1024,"SCAN\t%s/scan/%s/%s.eml\n", spool_directory, message_id, message_id);
+      (void)string_format(file_name,1024,"SCAN\t%s/scan/%s/%s.eml\n", spool_directory, message_id, message_id);
       /* debug_printf("send scan %s",file_name); */
       if (write(sock, file_name, Ustrlen(file_name)) < 0) {
         (void)close(sock);
@@ -661,7 +661,7 @@ int malware(uschar **listptr) {
       /* get current date and time, build scan request */
       time(&t);
       strftime(CS tmpbuf, sizeof(tmpbuf), "<0>%d %b %H:%M:%S:%%s/scan/%%s", localtime(&t));
-      snprintf(CS scanrequest, 1024,CS tmpbuf, spool_directory, message_id);
+      (void)string_format(scanrequest, 1024,CS tmpbuf, spool_directory, message_id);
 
       /* send scan request */
       if (send(sock, scanrequest, Ustrlen(scanrequest)+1, 0) < 0) {
@@ -854,8 +854,8 @@ int malware(uschar **listptr) {
       };
 
       /* prepare scanner call */
-      snprintf(CS file_name,1024,"%s/scan/%s", spool_directory, message_id);
-      snprintf(CS commandline,1024, CS cmdline_scanner,file_name);
+      (void)string_format(file_name,1024,"%s/scan/%s", spool_directory, message_id);
+      (void)string_format(commandline,1024, CS cmdline_scanner,file_name);
       /* redirect STDERR too */
       Ustrcat(commandline," 2>&1");
 
@@ -872,8 +872,8 @@ int malware(uschar **listptr) {
         return DEFER;
       };
 
-      snprintf(CS file_name,1024,"%s/scan/%s/%s_scanner_output", spool_directory, message_id, message_id);
-      scanner_record = fopen(CS file_name,"w");
+      (void)string_format(file_name,1024,"%s/scan/%s/%s_scanner_output", spool_directory, message_id, message_id);
+      scanner_record = modefopen(file_name,"wb",SPOOL_MODE);
 
       if (scanner_record == NULL) {
         log_write(0, LOG_MAIN|LOG_PANIC,
@@ -911,7 +911,7 @@ int malware(uschar **listptr) {
         malware_name = malware_name_buffer;
 
         /* re-open the scanner output file, look for name match */
-        scanner_record = fopen(CS file_name,"r");
+        scanner_record = fopen(CS file_name,"rb");
         while(fgets(CS linebuffer,32767,scanner_record) != NULL) {
           /* try match */
           result = pcre_exec(cmdline_regex_re, NULL, CS linebuffer, Ustrlen(linebuffer), 0, 0, ovector, 30);
@@ -964,7 +964,7 @@ int malware(uschar **listptr) {
       }
 
       /* pass the scan directory to sophie */
-      snprintf(CS file_name,1024,"%s/scan/%s", spool_directory, message_id);
+      (void)string_format(file_name,1024,"%s/scan/%s", spool_directory, message_id);
       if (write(sock, file_name, Ustrlen(file_name)) < 0) {
         (void)close(sock);
         log_write(0, LOG_MAIN|LOG_PANIC,
@@ -1079,7 +1079,7 @@ int malware(uschar **listptr) {
 
       /* Pass the string to ClamAV (7 = "SCAN \n" + \0) */
 
-          snprintf(CS file_name,1024,"SCAN %s/scan/%s\n", spool_directory, message_id);
+          (void)string_format(file_name,1024,"SCAN %s/scan/%s\n", spool_directory, message_id);
 
           if (send(sock, file_name, Ustrlen(file_name), 0) < 0) {
             (void)close(sock);
@@ -1141,7 +1141,7 @@ int malware(uschar **listptr) {
             return DEFER;
           }
 
-    snprintf(CS scanrequest, 1024,CS"%s/scan/%s/%s.eml",
+      (void)string_format(scanrequest, 1024,CS"%s/scan/%s/%s.eml",
       spool_directory, message_id, message_id);
 
     /* calc file size */
@@ -1218,7 +1218,7 @@ int malware(uschar **listptr) {
 
       /* Pass the string to ClamAV (7 = "SCAN \n" + \0) */
 
-      snprintf(CS file_name,1024,"SCAN %s/scan/%s\n", spool_directory, message_id);
+      (void)string_format(file_name,1024,"SCAN %s/scan/%s\n", spool_directory, message_id);
 
       if (send(sock, file_name, Ustrlen(file_name), 0) < 0) {
         (void)close(sock);
@@ -1265,6 +1265,10 @@ int malware(uschar **listptr) {
         return DEFER;
       }
 
+      /* strip newline at the end */
+      p = av_buffer + Ustrlen(av_buffer) - 1;
+      if( *p == '\n' ) *p = '\0';
+
       /* colon in returned output? */
       if((p = Ustrrchr(av_buffer,':')) == NULL) {
         log_write(0, LOG_MAIN|LOG_PANIC,
@@ -1273,13 +1277,10 @@ int malware(uschar **listptr) {
         return DEFER;
       }
 
-      /* strip filename strip CR at the end */
+      /* strip filename */
       ++p;
       while (*p == ' ') ++p;
       vname = p;
-      p = vname + Ustrlen(vname) - 1;
-      if( *p == '\n' ) *p = '\0';
-
       if ((p = Ustrstr(vname, "FOUND"))!=NULL) {
            *p=0;
            for (--p;p>vname && *p<=32;p--) *p=0;