--- clamav-devel/clamav-milter/clamav-milter.c	Wed Apr 28 15:00:28 2004
+++ clamav-devel.jm-pl2/clamav-milter/clamav-milter.c	Wed Apr 28 22:55:00 2004
@@ -616,7 +616,7 @@
  */
 static	char	const	rcsid[] = "$Id: clamav-milter.c,v 1.84 2004/04/28 14:28:29 nigelhorne Exp $";
 
-#define	CM_VERSION	"0.70t"
+#define	CM_VERSION	"0.70t-jm-pl2"
 
 /*#define	CONFDIR	"/usr/local/etc"*/
 
@@ -728,7 +728,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? */
 	int	discard;	/*
@@ -750,7 +751,7 @@
 static	sfsistat	clamfi_close(SMFICTX *ctx);
 static	void		clamfi_cleanup(SMFICTX *ctx);
 static	void		clamfi_free(struct privdata *privdata);
-static	int		clamfi_send(const struct privdata *privdata, size_t len, const char *format, ...);
+static	size_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);
@@ -822,7 +823,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
 				 */
@@ -1367,8 +1368,6 @@
 		if((open("/dev/console", O_WRONLY) == 1) ||
 		   (open("/dev/null", O_WRONLY) == 1))
 			dup(1);
-#endif
-
 #ifdef HAVE_SETPGRP
 #ifdef SETPGRP_VOID
 		setpgrp();
@@ -1889,9 +1888,9 @@
 	privdata->rejectCode = "550";
 
 	privdata->from = strdup(argv[0]);
-
-	if(streamMaxLength > 0L)
-		privdata->numBytes = (long)(strlen(argv[0]) + 6);
+	privdata->envid = smfi_getsymval(ctx,"i");
+	if(!privdata->envid)
+		privdata->envid = "Uknown";
 
 	if(hflag)
 		privdata->headers = header_list_new();
@@ -1927,9 +1926,6 @@
 	privdata->to[privdata->numTo] = strdup(argv[0]);
 	privdata->to[++privdata->numTo] = NULL;
 
-	if(streamMaxLength > 0L)
-		privdata->numBytes += (long)(strlen(argv[0]) + 4);
-
 	return SMFIS_CONTINUE;
 }
 
@@ -1966,9 +1962,6 @@
 		return cl_error;
 	}
 
-	if(streamMaxLength > 0L)
-		privdata->numBytes += (long)(strlen(headerf) + strlen(headerv) + 3);
-
 	if(hflag)
 		header_list_add(privdata->headers, headerf, headerv);
 	else if((strcasecmp(headerf, "Received") == 0) &&
@@ -2020,8 +2013,6 @@
 		clamfi_cleanup(ctx);
 		return cl_error;
 	}
-	if(streamMaxLength > 0L)
-		privdata->numBytes++;
 
 	/*
 	 * See if the e-mail is only going to members of the list
@@ -2078,20 +2069,6 @@
 	cli_dbgmsg("clamfi_envbody: %u bytes\n", len);
 #endif
 
-	if(streamMaxLength > 0L) {
-		privdata->numBytes += (long)len;
-		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);
-			}
-			clamfi_cleanup(ctx);	/* not needed, but just to be safe */
-			return SMFIS_ACCEPT;
-		}
-	}
 	if(clamfi_send(privdata, len, (char *)bodyp) < 0) {
 		clamfi_cleanup(ctx);	/* not needed, but just to be safe */
 		return cl_error;
@@ -2117,7 +2094,6 @@
 {
 	int rc = SMFIS_CONTINUE;
 	char *ptr;
-	const char *sendmailId;
 	struct privdata *privdata = (struct privdata *)smfi_getpriv(ctx);
 	char mess[128];
 
@@ -2193,10 +2169,6 @@
 	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);
 
@@ -2207,17 +2179,19 @@
 		if(strstr(mess, "Size exceeded") != NULL) {
 			/*
 			 * Clamd has stopped on StreamMaxLength before us
+			 *
+			 * With my clamd-stream-maxsizenoreturn patch this will never happen.
 			 */
 			if(use_syslog)
 				syslog(LOG_NOTICE, "%s: Message more than StreamMaxLength (%ld) bytes - not scanned",
-					sendmailId, streamMaxLength);
+ 					privdata->envid, streamMaxLength);
 			clamfi_cleanup(ctx);	/* not needed, but just to be safe */
 			return SMFIS_ACCEPT;
 		}
 
-		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;
 	}
@@ -2233,7 +2207,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) {
@@ -2309,7 +2283,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
@@ -2381,7 +2355,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);
@@ -2616,13 +2590,15 @@
 }
 
 /*
- * Returns < 0 for failure
+ * Return number of bytes written or < 0 for failure
+ * 
  */
-static int
-clamfi_send(const struct privdata *privdata, size_t len, const char *format, ...)
+static size_t
+clamfi_send(struct privdata *privdata, size_t len, const char *format, ...)
 {
 	char output[BUFSIZ];
 	const char *ptr;
+	size_t numBytes = 0;
 
 	assert(format != NULL);
 
@@ -2656,6 +2632,23 @@
 	}
 #endif
 
+	if(!quarantine_dir && (
+		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) :
@@ -2682,10 +2675,12 @@
 
 			return -1;
 		}
+		numBytes += nbytes;
+		privdata->numBytes += nbytes;
 		len -= nbytes;
 		ptr = &ptr[nbytes];
 	}
-	return 0;
+	return numBytes;
 }
 
 /*
