Browse code

Fix overflow To:

git-svn-id: file:///var/lib/svn/clamav-devel/trunk/clamav-devel@801 77e5149b-7576-45b1-b177-96237e5ba77b

Nigel Horne authored on 2004/08/26 19:24:29
Showing 3 changed files
... ...
@@ -1,3 +1,11 @@
1
+Thu Aug 26 11:23:22 BST 2004 (njh)
2
+----------------------------------
3
+  * clamav-milter/clamav-milter.c: Generate correct message if there is no
4
+			response from any clamd server
5
+		Handle %h (headers) in the template file
6
+		Fix bug in optimisation when more than one To line is
7
+			received
8
+
1 9
 Thu Aug 26 10:34:48 BST 2004 (njh)
2 10
 ----------------------------------
3 11
   * libclamav/mbox.c:	Scan CommuniGate files
... ...
@@ -458,7 +458,7 @@ Changes
458 458
 		Template files can now contain more than one variable
459 459
 		Template files sendmail variables handling changed to allow
460 460
 			access to variables not in braces. All sendmail
461
-			variables are now delimeted by dollars, e.g.  ${j}$
461
+			variables are now delimeted by dollars, e.g. ${j}$
462 462
 		Better local IP table by Damian Menscher <menscher@uiuc.edu> and
463 463
 			Andy Fiddaman <clam@fiddaman.net>
464 464
 0.75g	06/8/04	Handle privdata->from not set when --bounce is set
... ...
@@ -475,8 +475,13 @@ Changes
475 475
 		Optimise the sending of the To and From headers to clamd
476 476
 		Give better SMTP status message when asking for retransmit
477 477
 			when --dont-wait is set
478
-		Quarantine files, now handle operating system filename
478
+		Quarantine files now handle operating system filename
479 479
 			restrictions
480
+0.75m	26/8/04	Generate correct message if there is no response from any
481
+			clamd server
482
+		Handle %h (headers) in the template file
483
+		Fix bug in optimisation when more than one To line is
484
+			received
480 485
 
481 486
 BUG REPORTS
482 487
 
... ...
@@ -26,6 +26,9 @@
26 26
  *
27 27
  * Change History:
28 28
  * $Log: clamav-milter.c,v $
29
+ * Revision 1.121  2004/08/26 10:22:00  nigelhorne
30
+ * Fix overflow To:
31
+ *
29 32
  * Revision 1.120  2004/08/25 11:44:56  nigelhorne
30 33
  * Tidy
31 34
  *
... ...
@@ -371,9 +374,9 @@
371 371
  * Revision 1.6  2003/09/28 16:37:23  nigelhorne
372 372
  * Added -f flag use MaxThreads if --max-children not set
373 373
  */
374
-static	char	const	rcsid[] = "$Id: clamav-milter.c,v 1.120 2004/08/25 11:44:56 nigelhorne Exp $";
374
+static	char	const	rcsid[] = "$Id: clamav-milter.c,v 1.121 2004/08/26 10:22:00 nigelhorne Exp $";
375 375
 
376
-#define	CM_VERSION	"0.75l"
376
+#define	CM_VERSION	"0.75m"
377 377
 
378 378
 /*#define	CONFDIR	"/usr/local/etc"*/
379 379
 
... ...
@@ -1492,7 +1495,11 @@ findServer(void)
1492 1492
 	tv.tv_sec = readTimeout;
1493 1493
 	tv.tv_usec = 0;
1494 1494
 
1495
-	retval = select(maxsock + 1, &rfds, NULL, NULL, &tv);
1495
+	if(maxsock == 0)
1496
+		retval = 0;
1497
+	else
1498
+		retval = select(maxsock + 1, &rfds, NULL, NULL, &tv);
1499
+
1496 1500
 	if(retval < 0)
1497 1501
 		perror("select");
1498 1502
 
... ...
@@ -1501,10 +1508,56 @@ findServer(void)
1501 1501
 			close(socks[i]);
1502 1502
 
1503 1503
 	if(retval == 0) {
1504
+		static time_t lasttime;
1505
+		time_t thistime, diff;
1506
+		static pthread_mutex_t time_mutex = PTHREAD_MUTEX_INITIALIZER;
1507
+
1508
+		/*
1509
+		 * This is serious, we need to inform someone.
1510
+		 * In the absence of SNMP the best way is by e-mail. We
1511
+		 * don't want to flood so there's a need to restrict to
1512
+		 * no more than say one message every 15 minutes
1513
+		 */
1504 1514
 		free(socks);
1505 1515
 		cli_dbgmsg("findServer: No response from any server\n");
1506 1516
 		if(use_syslog)
1507 1517
 			syslog(LOG_WARNING, "findServer: No response from any server");
1518
+
1519
+		time(&thistime);
1520
+		pthread_mutex_lock(&time_mutex);
1521
+		diff = thistime - lasttime;
1522
+		pthread_mutex_unlock(&time_mutex);
1523
+
1524
+		if(diff >= (time_t)(15 * 60)) {
1525
+			char cmd[128];
1526
+			FILE *sendmail;
1527
+
1528
+			snprintf(cmd, sizeof(cmd) - 1, "%s -t", SENDMAIL_BIN);
1529
+
1530
+			sendmail = popen(cmd, "w");
1531
+
1532
+			if(sendmail) {
1533
+				fprintf(sendmail, "To: %s\n", postmaster);
1534
+				fprintf(sendmail, "From: %s\n", postmaster);
1535
+				fputs("Subject: ClamAV Down\n", sendmail);
1536
+				fputs("Priority: High\n\n", sendmail);
1537
+
1538
+				fputs("This is an automatic message\n\n", sendmail);
1539
+
1540
+				if(numServers == 1)
1541
+					fputs("The clamd program cannot be contacted.\n", sendmail);
1542
+				else
1543
+					fputs("No clamd server can be contacted.\n", sendmail);
1544
+
1545
+				fputs("Emails may not be being scanned, please check your servers.\n", sendmail);
1546
+
1547
+				if(pclose(sendmail) == 0) {
1548
+					pthread_mutex_lock(&time_mutex);
1549
+					time(&lasttime);
1550
+					pthread_mutex_unlock(&time_mutex);
1551
+				}
1552
+			}
1553
+		}
1508 1554
 		return 0;
1509 1555
 	} else if(retval < 0) {
1510 1556
 		free(socks);
... ...
@@ -1698,7 +1751,7 @@ clamfi_envfrom(SMFICTX *ctx, char **argv)
1698 1698
 
1699 1699
 			if(dont_wait) {
1700 1700
 				pthread_mutex_unlock(&n_children_mutex);
1701
-				smfi_setreply(ctx, "451", "4.7.1", "AV system temporarily overloaded - please try later");
1701
+				smfi_setreply(ctx, "451", "4.3.2", "AV system temporarily overloaded - please try later");
1702 1702
 				return SMFIS_TEMPFAIL;
1703 1703
 			}
1704 1704
 			/*
... ...
@@ -2282,7 +2335,7 @@ clamfi_eom(SMFICTX *ctx)
2282 2282
 
2283 2283
 				if((templatefile == NULL) ||
2284 2284
 				   (sendtemplate(ctx, templatefile, sendmail, virusname) < 0)) {
2285
-				   	/*
2285
+					/*
2286 2286
 					 * Use our own hardcoded template
2287 2287
 					 */
2288 2288
 					const char *sender;
... ...
@@ -3014,7 +3067,7 @@ connect2clamd(struct privdata *privdata)
3014 3014
 	 */
3015 3015
 	length = strlen(privdata->from) + 34;
3016 3016
 	for(to = privdata->to; *to; to++)
3017
-		length += strlen(*to) + 4;
3017
+		length += strlen(*to) + 5;
3018 3018
 
3019 3019
 	msg = cli_malloc(length + 1);
3020 3020
 
... ...
@@ -3100,6 +3153,7 @@ sendtemplate(SMFICTX *ctx, const char *filename, FILE *sendmail, const char *vir
3100 3100
 	FILE *fin = fopen(filename, "r");
3101 3101
 	struct stat statb;
3102 3102
 	char *buf, *ptr /* , *ptr2 */;
3103
+	struct privdata *privdata = (struct privdata *)smfi_getpriv(ctx);
3103 3104
 
3104 3105
 	if(fin == NULL) {
3105 3106
 		perror(filename);
... ...
@@ -3130,49 +3184,66 @@ sendtemplate(SMFICTX *ctx, const char *filename, FILE *sendmail, const char *vir
3130 3130
 	buf[statb.st_size] = '\0';
3131 3131
 
3132 3132
 	for(ptr = buf; *ptr; ptr++)
3133
-		if(*ptr == '\\') {
3134
-			if(*++ptr == '\0')
3135
-				break;
3136
-			putc(*ptr, sendmail);
3137
-		} else if(*ptr == '%') {
3138
-			/* clamAV variable */
3139
-			if(*++ptr == 'v')
3140
-				fputs(virusname, sendmail);
3141
-			else if(*ptr == '%')
3142
-				putc('%', sendmail);
3143
-			else if(*ptr == '\0')
3144
-				break;
3145
-			else if(use_syslog)
3146
-				syslog(LOG_ERR,
3147
-					"%s: Unknown clamAV variable \"%c\"\n",
3148
-					filename, *ptr);
3149
-		} else if(*ptr == '$') {
3150
-			const char *val;
3151
-			char *end = strchr(++ptr, '$');
3152
-
3153
-			if(end == NULL) {
3154
-				syslog(LOG_ERR,
3155
-					"%s: Unterminated sendmail variable \"%s\"\n",
3156
-						filename, ptr);
3157
-				continue;
3158
-			}
3159
-			*end = '\0';
3133
+		switch(*ptr) {
3134
+			case '%': /* clamAV variable */
3135
+				switch(*++ptr) {
3136
+					case 'v':	/* virus name */
3137
+						fputs(virusname, sendmail);
3138
+						break;
3139
+					case '%':
3140
+						putc('%', sendmail);
3141
+						break;
3142
+					case 'h':	/* headers */
3143
+						if(privdata)
3144
+							header_list_print(privdata->headers, sendmail);
3145
+						break;
3146
+					case '\0':
3147
+						putc('%', sendmail);
3148
+						--ptr;
3149
+						continue;
3150
+					default:
3151
+						syslog(LOG_ERR,
3152
+							"%s: Unknown clamAV variable \"%c\"\n",
3153
+							filename, *ptr);
3154
+						break;
3155
+				}
3156
+			case '$': /* sendmail string */ {
3157
+				const char *val;
3158
+				char *end = strchr(++ptr, '$');
3160 3159
 
3161
-			val = smfi_getsymval(ctx, ptr);
3162
-			if(val == NULL) {
3163
-				fputs(ptr, sendmail);
3164
-				if(use_syslog)
3160
+				if(end == NULL) {
3165 3161
 					syslog(LOG_ERR,
3166
-						"%s: Unknown sendmail variable \"%s\"\n",
3167
-						filename, ptr);
3168
-			} else
3169
-				fputs(val, sendmail);
3170
-			ptr = end;
3171
-		} else
3172
-			putc(*ptr, sendmail);
3162
+						"%s: Unterminated sendmail variable \"%s\"\n",
3163
+							filename, ptr);
3164
+					continue;
3165
+				}
3166
+				*end = '\0';
3167
+
3168
+				val = smfi_getsymval(ctx, ptr);
3169
+				if(val == NULL) {
3170
+					fputs(ptr, sendmail);
3171
+					if(use_syslog)
3172
+						syslog(LOG_ERR,
3173
+							"%s: Unknown sendmail variable \"%s\"\n",
3174
+							filename, ptr);
3175
+				} else
3176
+					fputs(val, sendmail);
3177
+				ptr = end;
3178
+			}
3179
+			case '\\':
3180
+				if(*++ptr == '\0') {
3181
+					--ptr;
3182
+					continue;
3183
+				}
3184
+				putc(*ptr, sendmail);
3185
+				break;
3186
+			default:
3187
+				putc(*ptr, sendmail);
3188
+		}
3173 3189
 
3174 3190
 	free(buf);
3175 3191
 
3192
+
3176 3193
 	return 0;
3177 3194
 }
3178 3195