Combined patch for simscan-1.4.1 (https://github.com/qmail/simscan/releases) by Roberto Puzzanghera More info at https://notes.sagredo.eu/en/qmail-notes-185/simscan-38.html v. 20230913 This patch puts together: * jms patch to improve the debugging of simscan on qmail-smtpd log https://notes.sagredo.eu/files/qmail/patches/simscan/jms/simscan-1.4.0-debug.patch more info here https://qmail.jms1.net/simscan/ * A bug fix by Bob Greco where a received message with multiple 'local' recipients executes spamc as null user and not as the user extracted from the first local recipient. https://notes.sagredo.eu/en/qmail-notes-185/simscan-38.html#comment844 * The "attachments-size-limit" patch by Roberto Puzzanghera https://notes.sagredo.eu/files/qmail/patches/simscan/simscan-1.4.1_attachments-size-limit_v2.patch This patch let the administrator set the attachments' size limit in bytes by setting the /var/qmail/control/simsizelimit file. Furthermore, events where simscan is not activated are now logged at smtpd level (it was logged only when debug is active). Note: this patch is alternative to this one https://notes.sagredo.eu/files/qmail/patches/simscan/simscan-1.4.1_attachments-size-limit.patch which sets the limit at compilation time. =================================================================================================== diff -ruN ../simscan-1.4.1_original/simscan.c simscan-1.4.1/simscan.c --- ../simscan-1.4.1_original/simscan.c 2015-01-23 09:57:43.000000000 +0100 +++ simscan-1.4.1/simscan.c 2023-09-13 21:34:53.791256656 +0200 @@ -102,6 +102,7 @@ int str_rstr(register char *h,register char *n); char *replace(char *string, char *oldpiece, char *newpiece); int DebugFlag = 0; +int DebugFiles = 0; /* --stdout is required for reading virus names */ char *viri_args[] = { "clamdscan", "--stdout", message_name, NULL }; @@ -171,6 +172,7 @@ int InClamHeaders; int check_clam(); int is_clam(char *clambuf); +void log_clam(char *clambuf); #endif /* Attachment scanning globals */ @@ -253,6 +255,11 @@ int i = 0; int gotrcpt = 0; int gotfrom = 0; + /* attachments-size-limit patch */ + char ch, simsizelimit_file[300], buf[32]; + FILE *simsizelimit; + int size_limit=250000, buflen=0, ibuf; + /* end attachments-size-limit patch */ /* print out version information if requested */ if ( argc > 1 && strcmp(argv[1],"-v" )==0 ) { @@ -274,6 +281,11 @@ if ( (tmpstr=getenv("SIMSCAN_DEBUG"))!=NULL ) { DebugFlag = atoi(tmpstr); } + + /* Set the "leave temp files alone" flag */ + if ( (tmpstr=getenv("SIMSCAN_DEBUG_FILES"))!=NULL ) { + DebugFiles = atoi(tmpstr); + } #ifdef ENABLE_ATTACH init_attach(); @@ -546,11 +558,33 @@ #endif #ifdef ENABLE_SPAM -if (msgsize >= 250000) { - if ( DebugFlag > 0 ) { - fprintf(stderr, "simscan: big file (%lu bytes); skipping SpamAssassin\n", - (unsigned long) msgsize); +/* attachments-size-limit patch */ +// read control/simsizelimit file +snprintf(simsizelimit_file, sizeof(simsizelimit_file), "%s/simsizelimit", CONTROLDIR); +simsizelimit = fopen(simsizelimit_file, "r"); +if( simsizelimit != NULL ) { + while(1) { + ch = fgetc(simsizelimit); + if(ch==EOF || ch==' ' || ch=='\n' || ch=='\t' || ch=='\r') { + ibuf = atoi(buf); + // returns 0 if it's not an integer + if (ibuf > 0) size_limit = ibuf; + break; + } + else { + buf[buflen] = ch; + buflen++; + } } + fclose(simsizelimit); +} +if ( DebugFlag > 0 ) fprintf(stderr, "simscan: size limit is %d bytes\n", size_limit); +if (msgsize >= size_limit) { +// if ( DebugFlag > 0 ) { // now logging also when debug is off + fprintf(stderr, "simscan: big file (%lu bytes); size limit is %d bytes; skipping SpamAssassin\n", + (unsigned long) msgsize, size_limit); +// } +/* end attachments-size-limit patch */ } else { /* re-open the file read only */ if ( (fd = open(message_name, O_RDONLY)) == -1 ) { @@ -828,10 +862,12 @@ } /* remove the working files */ - if ( remove_files(workdir) == -1 ) { - exit_clean(EXIT_400); + if ( DebugFiles < 2 ) { + if ( remove_files(workdir) == -1 ) { + exit_clean(EXIT_400); + } } - + /* pass qmail-queue's exit status on */ _exit(WEXITSTATUS(qstat)); @@ -984,6 +1020,7 @@ InClamHeaders = 1; memset(buffer,0,sizeof(buffer)); while((file_count=read(0,buffer,BUFFER_SIZE))>0) { + if ( DebugFlag > 2 ) log_clam(buffer) ; if ( InClamHeaders == 1 ) { is_clam(buffer); } @@ -1010,6 +1047,35 @@ return(-1); } +void log_clam(char *clambuf) +{ + char *p ; + int needh = 1 ; + + p = clambuf ; + while ( *p ) + { + if ( *p != '\r' ) + { + if ( needh ) + { + fputs ( "simscan: clamdscan: " , stderr ) ; + needh = 0 ; + } + + fputc ( *p , stderr ) ; + + if ( *p == '\n' ) + needh = 1 ; + } + + p++ ; + } + + if ( ! needh ) + fputc ( '\n' , stderr ) ; +} + int is_clam(char *clambuf) { int i,j,k; @@ -1315,7 +1381,7 @@ spamc_args[i++] = "-u"; spamc_args[i++] = spamuser; #ifdef ENABLE_SPAMC_USER - } else if ( MaxRcptTo==1 && i0 && i 0 ) { fprintf(stderr, "simscan:[%d]: exit error code: %d\n", getppid(), error_code); }