--- clamav-devel-jm2/clamav-milter/clamav-milter.c	Wed May 12 20:30:25 2004
+++ clamav-devel-jm3/clamav-milter/clamav-milter.c	Wed May 12 20:45:21 2004
@@ -645,7 +645,7 @@
  */
 static	char	const	rcsid[] = "$Id: clamav-milter.c,v 1.87 2004/05/09 17:39:04 nigelhorne Exp $";
 
-#define	CM_VERSION	"0.70x"
+#define	CM_VERSION	"0.70x-jm-pl3"
 
 /*#define	CONFDIR	"/usr/local/etc"*/
 
@@ -757,7 +757,8 @@
 	u_char	*body;		/* body of the message if Sflag is set */
 	size_t	bodyLen;	/* number of bytes in body */
 	header_list_t headers;	/* Message headers */
-	long	numBytes;	/* Number of bytes sent so far */
+ 	size_t	numBytes;	/* Number of bytes sent so far */
+ 	char	*envid;		/* Sendmail envid ($i) */
 	char	*received;	/* keep track of received from */
 	const	char	*rejectCode;	/* 550 or 554? */
 	char	*messageID;	/* sendmailID */
@@ -780,7 +781,7 @@
 static	sfsistat	clamfi_close(SMFICTX *ctx);
 static	void		clamfi_cleanup(SMFICTX *ctx);
 static	void		clamfi_free(struct privdata *privdata);
-static	int		clamfi_send(struct privdata *privdata, size_t len, const char *format, ...);
+static	ssize_t		clamfi_send(struct privdata *privdata, size_t len, const char *format, ...);
 static	char		*strrcpy(char *dest, const char *source);
 static	int		clamd_recv(int sock, char *buf, size_t len);
 static	off_t		updateSigFile(void);
@@ -852,7 +853,7 @@
 				 * number of seconds to wait for clamd to
 				 * respond, see ReadTimeout in clamav.conf
 				 */
-static	long	streamMaxLength = -1;	/* StreamMaxLength from clamav.conf */
+static	size_t	streamMaxLength = -1;	/* StreamMaxLength from clamav.conf */
 static	int	logClean = 1;	/*
 				 * Add clean items to the log file
 				 */
@@ -1941,6 +1942,9 @@
 	privdata->rejectCode = "550";
 
 	privdata->from = strdup(argv[0]);
+	privdata->envid = smfi_getsymval(ctx,"i");
+	if(!privdata->envid)
+		privdata->envid = "Uknown";
 
 	if(hflag)
 		privdata->headers = header_list_new();
@@ -2112,7 +2116,6 @@
 clamfi_body(SMFICTX *ctx, u_char *bodyp, size_t len)
 {
 	struct privdata *privdata = (struct privdata *)smfi_getpriv(ctx);
-	int nbytes;
 
 	if(logVerbose)
 		syslog(LOG_DEBUG, "clamfi_envbody: %u bytes", len);
@@ -2120,26 +2123,11 @@
 	cli_dbgmsg("clamfi_envbody: %u bytes\n", len);
 #endif
 
-	nbytes = clamfi_send(privdata, len, (char *)bodyp);
-	if(streamMaxLength > 0L) {
-		if(privdata->numBytes > streamMaxLength) {
-			if(use_syslog) {
-				const char *sendmailId = smfi_getsymval(ctx, "i");
-				if(sendmailId == NULL)
-					sendmailId = "Unknown";
-				syslog(LOG_NOTICE, "%s: Message more than StreamMaxLength (%ld) bytes - not scanned",
-					sendmailId, streamMaxLength);
-			}
-			if(!nflag)
-				smfi_addheader(ctx, "X-Virus-Status", "Not Scanned - StreamMaxLength exceeded");
-
-			return SMFIS_ACCEPT;	/* clamfi_close will be called */
-		}
-	}
-	if(nbytes < len) {
-		clamfi_cleanup(ctx);	/* not needed, but just to be safe */
+	if(clamfi_send(privdata, len, (char *)bodyp) < 0) {
+		clamfi_cleanup(ctx);
 		return cl_error;
 	}
+
 	if(Sflag) {
 		if(privdata->body) {
 			assert(privdata->bodyLen > 0);
@@ -2161,7 +2149,6 @@
 {
 	int rc = SMFIS_CONTINUE;
 	char *ptr;
-	const char *sendmailId;
 	struct privdata *privdata = (struct privdata *)smfi_getpriv(ctx);
 	char mess[128];
 
@@ -2237,32 +2224,16 @@
 	close(privdata->cmdSocket);
 	privdata->cmdSocket = -1;
 
-	sendmailId = smfi_getsymval(ctx, "i");
-	if(sendmailId == NULL)
-		sendmailId = "Unknown";
-
 	if(!nflag)
 		smfi_addheader(ctx, "X-Virus-Scanned", clamav_version);
 
 	if(strstr(mess, "ERROR") != NULL) {
-		if(strstr(mess, "Size exceeded") != NULL) {
-			/*
-			 * Clamd has stopped on StreamMaxLength before us
-			 */
-			if(use_syslog)
-				syslog(LOG_NOTICE, "%s: Message more than StreamMaxLength (%ld) bytes - not scanned",
-					sendmailId, streamMaxLength);
-			clamfi_cleanup(ctx);	/* not needed, but just to be safe */
-			if(!nflag)
-				smfi_addheader(ctx, "X-Virus-Status", "Not Scanned - StreamMaxLength exceeded");
-			return SMFIS_ACCEPT;
-		}
 		if(!nflag)
 			smfi_addheader(ctx, "X-Virus-Status", "Not Scanned");
 
-		cli_warnmsg("%s: %s\n", sendmailId, mess);
+		cli_warnmsg("%s: %s\n", privdata->envid, mess);
 		if(use_syslog)
-			syslog(LOG_ERR, "%s: %s\n", sendmailId, mess);
+			syslog(LOG_ERR, "%s: %s\n", privdata->envid, mess);
 		clamfi_cleanup(ctx);
 		return cl_error;
 	}
@@ -2278,7 +2249,7 @@
 		if(use_syslog && logClean)
 			/* Include the sendmail queue ID in the log */
 			syslog(LOG_NOTICE, "%s: clean message from %s",
-				sendmailId,
+				privdata->envid,
 				(privdata->from) ? privdata->from : "an unknown sender");
 
 		if(privdata->body) {
@@ -2354,7 +2325,7 @@
 			(void)strcpy(ptr, "\n");
 
 			/* Include the sendmail queue ID in the log */
-			syslog(LOG_NOTICE, "%s: %s%s", sendmailId, mess, err);
+			syslog(LOG_NOTICE, "%s: %s %s", privdata->envid, mess, err);
 #ifdef	CL_DEBUG
 			cli_dbgmsg("%s\n", err);
 #endif
@@ -2431,7 +2402,7 @@
 						 * information
 						 */
 						fprintf(sendmail, "The message %s sent from %s to\n",
-							sendmailId, from);
+							privdata->envid, from);
 					else
 						fprintf(sendmail, "A message sent from %s to\n",
 							from);
@@ -2669,12 +2640,12 @@
 /*
  * Returns < 0 for failure, otherwise the number of bytes sent
  */
-static int
+static ssize_t
 clamfi_send(struct privdata *privdata, size_t len, const char *format, ...)
 {
 	char output[BUFSIZ];
 	const char *ptr;
-	int ret = 0;
+	ssize_t ret = 0;
 
 	assert(format != NULL);
 
@@ -2708,6 +2679,24 @@
 	}
 #endif
 
+	if(!quarantine_dir && (
+ 		streamMaxLength &&
+		len + privdata->numBytes > streamMaxLength) 
+	   ){
+		if(use_syslog && privdata->numBytes != streamMaxLength){
+                        char buf[1024];
+                        syslog(LOG_INFO,"%s: size exceeded max of %u , "
+					"already wrote %u, will write %u more instead "
+					"of len %u",
+                                        privdata->envid,
+                                        streamMaxLength,
+                                        privdata->numBytes,
+                                        streamMaxLength - privdata->numBytes,
+					len);
+                }
+		len = (streamMaxLength - privdata->numBytes);
+	}
+
 	while(len > 0) {
 		const int nbytes = (quarantine_dir) ?
 			write(privdata->dataSocket, ptr, len) :
@@ -2738,11 +2726,7 @@
 		len -= nbytes;
 		ptr = &ptr[nbytes];
 
-		if(streamMaxLength > 0L) {
-			privdata->numBytes += nbytes;
-			if(privdata->numBytes >= streamMaxLength)
-				break;
-		}
+		privdata->numBytes += nbytes;
 	}
 	return ret;
 }
