5bf92ba
diff --git a/source/client/client.c b/source/client/client.c
5bf92ba
index e08fa89..d684ba8 100644
5bf92ba
--- a/source/client/client.c
5bf92ba
+++ b/source/client/client.c
5bf92ba
@@ -4382,7 +4382,7 @@ static void readline_callback(void)
5bf92ba
 
5bf92ba
 		set_smb_read_error(&cli->smb_rw_error, SMB_READ_OK);
5bf92ba
 
5bf92ba
-		status = receive_smb_raw(cli->fd, cli->inbuf, 0, 0, &len;;
5bf92ba
+		status = receive_smb_raw(cli->fd, cli->inbuf, cli->bufsize, 0, 0, &len;;
5bf92ba
 
5bf92ba
 		if (!NT_STATUS_IS_OK(status)) {
5bf92ba
 			DEBUG(0, ("Read from server failed, maybe it closed "
5bf92ba
diff --git a/source/lib/util_sock.c b/source/lib/util_sock.c
5bf92ba
index f252377..df89dc8 100644
5bf92ba
--- a/source/lib/util_sock.c
5bf92ba
+++ b/source/lib/util_sock.c
5bf92ba
@@ -1160,7 +1160,7 @@ NTSTATUS read_smb_length(int fd, char *inbuf, unsigned int timeout,
5bf92ba
  Doesn't check the MAC on signed packets.
5bf92ba
 ****************************************************************************/
5bf92ba
 
5bf92ba
-NTSTATUS receive_smb_raw(int fd, char *buffer, unsigned int timeout,
5bf92ba
+NTSTATUS receive_smb_raw(int fd, char *buffer, size_t buflen, unsigned int timeout,
5bf92ba
 			 size_t maxlen, size_t *p_len)
5bf92ba
 {
5bf92ba
 	size_t len;
5bf92ba
@@ -1173,17 +1173,10 @@ NTSTATUS receive_smb_raw(int fd, char *buffer, unsigned int timeout,
5bf92ba
 		return status;
5bf92ba
 	}
5bf92ba
 
5bf92ba
-	/*
5bf92ba
-	 * A WRITEX with CAP_LARGE_WRITEX can be 64k worth of data plus 65 bytes
5bf92ba
-	 * of header. Don't print the error if this fits.... JRA.
5bf92ba
-	 */
5bf92ba
-
5bf92ba
-	if (len > (BUFFER_SIZE + LARGE_WRITEX_HDR_SIZE)) {
5bf92ba
+	if (len > buflen) {
5bf92ba
 		DEBUG(0,("Invalid packet length! (%lu bytes).\n",
5bf92ba
 					(unsigned long)len));
5bf92ba
-		if (len > BUFFER_SIZE + (SAFETY_MARGIN/2)) {
5bf92ba
-			return NT_STATUS_INVALID_PARAMETER;
5bf92ba
-		}
5bf92ba
+		return NT_STATUS_INVALID_PARAMETER;
5bf92ba
 	}
5bf92ba
 
5bf92ba
 	if(len > 0) {
5bf92ba
diff --git a/source/libsmb/clientgen.c b/source/libsmb/clientgen.c
5bf92ba
index 6419123..2af4383 100644
5bf92ba
--- a/source/libsmb/clientgen.c
5bf92ba
+++ b/source/libsmb/clientgen.c
5bf92ba
@@ -57,8 +57,7 @@ int cli_set_port(struct cli_state *cli, int port)
5bf92ba
 }
5bf92ba
 
5bf92ba
 /****************************************************************************
5bf92ba
- Read an smb from a fd ignoring all keepalive packets. Note that the buffer 
5bf92ba
- *MUST* be of size BUFFER_SIZE+SAFETY_MARGIN.
5bf92ba
+ Read an smb from a fd ignoring all keepalive packets.
5bf92ba
  The timeout is in milliseconds
5bf92ba
 
5bf92ba
  This is exactly the same as receive_smb except that it never returns
5bf92ba
@@ -76,8 +75,8 @@ static ssize_t client_receive_smb(struct cli_state *cli, size_t maxlen)
5bf92ba
 
5bf92ba
 		set_smb_read_error(&cli->smb_rw_error, SMB_READ_OK);
5bf92ba
 
5bf92ba
-		status = receive_smb_raw(cli->fd, cli->inbuf, cli->timeout,
5bf92ba
-					 maxlen, &len;;
5bf92ba
+		status = receive_smb_raw(cli->fd, cli->inbuf, cli->bufsize,
5bf92ba
+					cli->timeout, maxlen, &len;;
5bf92ba
 		if (!NT_STATUS_IS_OK(status)) {
5bf92ba
 			DEBUG(10,("client_receive_smb failed\n"));
5bf92ba
 			show_msg(cli->inbuf);
5bf92ba
@@ -225,93 +224,6 @@ ssize_t cli_receive_smb_data(struct cli_state *cli, char *buffer, size_t len)
5bf92ba
 	return -1;
5bf92ba
 }
5bf92ba
 
5bf92ba
-/****************************************************************************
5bf92ba
- Read a smb readX header.
5bf92ba
- We can only use this if encryption and signing are off.
5bf92ba
-****************************************************************************/
5bf92ba
-
5bf92ba
-bool cli_receive_smb_readX_header(struct cli_state *cli)
5bf92ba
-{
5bf92ba
-	ssize_t len, offset;
5bf92ba
-
5bf92ba
-	if (cli->fd == -1)
5bf92ba
-		return false; 
5bf92ba
-
5bf92ba
- again:
5bf92ba
-
5bf92ba
-	/* Read up to the size of a readX header reply. */
5bf92ba
-	len = client_receive_smb(cli, (smb_size - 4) + 24);
5bf92ba
-	
5bf92ba
-	if (len > 0) {
5bf92ba
-		/* it might be an oplock break request */
5bf92ba
-		if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
5bf92ba
-		    CVAL(cli->inbuf,smb_com) == SMBlockingX &&
5bf92ba
-		    SVAL(cli->inbuf,smb_vwv6) == 0 &&
5bf92ba
-		    SVAL(cli->inbuf,smb_vwv7) == 0) {
5bf92ba
-			ssize_t total_len = smb_len(cli->inbuf);
5bf92ba
-
5bf92ba
-			if (total_len > CLI_SAMBA_MAX_LARGE_READX_SIZE+SAFETY_MARGIN) {
5bf92ba
-				goto read_err;
5bf92ba
-			}
5bf92ba
-
5bf92ba
-			/* Read the rest of the data. */
5bf92ba
-			if ((total_len - len > 0) &&
5bf92ba
-			    !cli_receive_smb_data(cli,cli->inbuf+len,total_len - len)) {
5bf92ba
-				goto read_err;
5bf92ba
-			}
5bf92ba
-
5bf92ba
-			if (cli->oplock_handler) {
5bf92ba
-				int fnum = SVAL(cli->inbuf,smb_vwv2);
5bf92ba
-				unsigned char level = CVAL(cli->inbuf,smb_vwv3+1);
5bf92ba
-				if (!cli->oplock_handler(cli, fnum, level)) return false;
5bf92ba
-			}
5bf92ba
-			/* try to prevent loops */
5bf92ba
-			SCVAL(cli->inbuf,smb_com,0xFF);
5bf92ba
-			goto again;
5bf92ba
-		}
5bf92ba
-	}
5bf92ba
-
5bf92ba
-	/* If it's not the above size it probably was an error packet. */
5bf92ba
-
5bf92ba
-	if ((len == (smb_size - 4) + 24) && !cli_is_error(cli)) {
5bf92ba
-		/* Check it's a non-chained readX reply. */
5bf92ba
-		if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) ||
5bf92ba
-			(CVAL(cli->inbuf,smb_vwv0) != 0xFF) ||
5bf92ba
-			(CVAL(cli->inbuf,smb_com) != SMBreadX)) {
5bf92ba
-			/* 
5bf92ba
-			 * We're not coping here with asnyc replies to
5bf92ba
-			 * other calls. Punt here - we need async client
5bf92ba
-			 * libs for this.
5bf92ba
-			 */
5bf92ba
-			goto read_err;
5bf92ba
-		}
5bf92ba
-
5bf92ba
-		/* 
5bf92ba
-		 * We know it's a readX reply - ensure we've read the
5bf92ba
-		 * padding bytes also.
5bf92ba
-		 */
5bf92ba
-
5bf92ba
-		offset = SVAL(cli->inbuf,smb_vwv6);
5bf92ba
-		if (offset > len) {
5bf92ba
-			ssize_t ret;
5bf92ba
-			size_t padbytes = offset - len;
5bf92ba
-			ret = cli_receive_smb_data(cli,smb_buf(cli->inbuf),padbytes);
5bf92ba
-			if (ret != padbytes) {
5bf92ba
-				goto read_err;
5bf92ba
-			}
5bf92ba
-		}
5bf92ba
-	}
5bf92ba
-
5bf92ba
-	return true;
5bf92ba
-
5bf92ba
-  read_err:
5bf92ba
-
5bf92ba
-	cli->smb_rw_error = SMB_READ_ERROR;
5bf92ba
-	close(cli->fd);
5bf92ba
-	cli->fd = -1;
5bf92ba
-	return false;
5bf92ba
-}
5bf92ba
-
5bf92ba
 static ssize_t write_socket(int fd, const char *buf, size_t len)
5bf92ba
 {
5bf92ba
         ssize_t ret=0;
5bf92ba
diff --git a/source/libsmb/clireadwrite.c b/source/libsmb/clireadwrite.c
5bf92ba
index 12ba4b7..487f446 100644
5bf92ba
--- a/source/libsmb/clireadwrite.c
5bf92ba
+++ b/source/libsmb/clireadwrite.c
5bf92ba
@@ -472,106 +472,6 @@ ssize_t cli_read(struct cli_state *cli, int fnum, char *buf,
5bf92ba
 	return ret;
5bf92ba
 }
5bf92ba
 
5bf92ba
-#if 0  /* relies on client_receive_smb(), now a static in libsmb/clientgen.c */
5bf92ba
-
5bf92ba
-/* This call is INCOMPATIBLE with SMB signing.  If you remove the #if 0
5bf92ba
-   you must fix ensure you don't attempt to sign the packets - data
5bf92ba
-   *will* be currupted */
5bf92ba
-
5bf92ba
-/****************************************************************************
5bf92ba
-Issue a single SMBreadraw and don't wait for a reply.
5bf92ba
-****************************************************************************/
5bf92ba
-
5bf92ba
-static bool cli_issue_readraw(struct cli_state *cli, int fnum, off_t offset, 
5bf92ba
-			   size_t size, int i)
5bf92ba
-{
5bf92ba
-
5bf92ba
-	if (!cli->sign_info.use_smb_signing) {
5bf92ba
-		DEBUG(0, ("Cannot use readraw and SMB Signing\n"));
5bf92ba
-		return False;
5bf92ba
-	}
5bf92ba
-	
5bf92ba
-	memset(cli->outbuf,'\0',smb_size);
5bf92ba
-	memset(cli->inbuf,'\0',smb_size);
5bf92ba
-
5bf92ba
-	cli_set_message(cli->outbuf,10,0,True);
5bf92ba
-		
5bf92ba
-	SCVAL(cli->outbuf,smb_com,SMBreadbraw);
5bf92ba
-	SSVAL(cli->outbuf,smb_tid,cli->cnum);
5bf92ba
-	cli_setup_packet(cli);
5bf92ba
-
5bf92ba
-	SSVAL(cli->outbuf,smb_vwv0,fnum);
5bf92ba
-	SIVAL(cli->outbuf,smb_vwv1,offset);
5bf92ba
-	SSVAL(cli->outbuf,smb_vwv2,size);
5bf92ba
-	SSVAL(cli->outbuf,smb_vwv3,size);
5bf92ba
-	SSVAL(cli->outbuf,smb_mid,cli->mid + i);
5bf92ba
-
5bf92ba
-	return cli_send_smb(cli);
5bf92ba
-}
5bf92ba
-
5bf92ba
-/****************************************************************************
5bf92ba
- Tester for the readraw call.
5bf92ba
-****************************************************************************/
5bf92ba
-
5bf92ba
-ssize_t cli_readraw(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
5bf92ba
-{
5bf92ba
-	char *p;
5bf92ba
-	int size2;
5bf92ba
-	size_t readsize;
5bf92ba
-	ssize_t total = 0;
5bf92ba
-
5bf92ba
-	if (size == 0) 
5bf92ba
-		return 0;
5bf92ba
-
5bf92ba
-	/*
5bf92ba
-	 * Set readsize to the maximum size we can handle in one readraw.
5bf92ba
-	 */
5bf92ba
-
5bf92ba
-	readsize = 0xFFFF;
5bf92ba
-
5bf92ba
-	while (total < size) {
5bf92ba
-		readsize = MIN(readsize, size-total);
5bf92ba
-
5bf92ba
-		/* Issue a read and receive a reply */
5bf92ba
-
5bf92ba
-		if (!cli_issue_readraw(cli, fnum, offset, readsize, 0))
5bf92ba
-			return -1;
5bf92ba
-
5bf92ba
-		if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout))
5bf92ba
-			return -1;
5bf92ba
-
5bf92ba
-		size2 = smb_len(cli->inbuf);
5bf92ba
-
5bf92ba
-		if (size2 > readsize) {
5bf92ba
-			DEBUG(5,("server returned more than we wanted!\n"));
5bf92ba
-			return -1;
5bf92ba
-		} else if (size2 < 0) {
5bf92ba
-			DEBUG(5,("read return < 0!\n"));
5bf92ba
-			return -1;
5bf92ba
-		}
5bf92ba
-
5bf92ba
-		/* Copy data into buffer */
5bf92ba
-
5bf92ba
-		if (size2) {
5bf92ba
-			p = cli->inbuf + 4;
5bf92ba
-			memcpy(buf + total, p, size2);
5bf92ba
-		}
5bf92ba
-
5bf92ba
-		total += size2;
5bf92ba
-		offset += size2;
5bf92ba
-
5bf92ba
-		/*
5bf92ba
-		 * If the server returned less than we asked for we're at EOF.
5bf92ba
-		 */
5bf92ba
-
5bf92ba
-		if (size2 < readsize)
5bf92ba
-			break;
5bf92ba
-	}
5bf92ba
-
5bf92ba
-	return total;
5bf92ba
-}
5bf92ba
-#endif
5bf92ba
-
5bf92ba
 /****************************************************************************
5bf92ba
  Issue a single SMBwrite and don't wait for a reply.
5bf92ba
 ****************************************************************************/
5bf92ba
diff --git a/source/smbd/process.c b/source/smbd/process.c
5bf92ba
index 5946989..ed34e9b 100644
5bf92ba
--- a/source/smbd/process.c
5bf92ba
+++ b/source/smbd/process.c
5bf92ba
@@ -120,9 +120,7 @@ static bool valid_packet_size(size_t len)
5bf92ba
 	if (len > (BUFFER_SIZE + LARGE_WRITEX_HDR_SIZE)) {
5bf92ba
 		DEBUG(0,("Invalid packet length! (%lu bytes).\n",
5bf92ba
 					(unsigned long)len));
5bf92ba
-		if (len > BUFFER_SIZE + (SAFETY_MARGIN/2)) {
5bf92ba
-			return false;
5bf92ba
-		}
5bf92ba
+		return false;
5bf92ba
 	}
5bf92ba
 	return true;
5bf92ba
 }
5bf92ba
diff --git a/source/utils/smbfilter.c b/source/utils/smbfilter.c
5bf92ba
index e128e1c..d274e09 100644
5bf92ba
--- a/source/utils/smbfilter.c
5bf92ba
+++ b/source/utils/smbfilter.c
5bf92ba
@@ -171,7 +171,8 @@ static void filter_child(int c, struct sockaddr_storage *dest_ss)
5bf92ba
 		if (c != -1 && FD_ISSET(c, &fds)) {
5bf92ba
 			size_t len;
5bf92ba
 			if (!NT_STATUS_IS_OK(receive_smb_raw(
5bf92ba
-						     c, packet, 0, 0, &len))) {
5bf92ba
+							c, packet, sizeof(packet),
5bf92ba
+							0, 0, &len))) {
5bf92ba
 				d_printf("client closed connection\n");
5bf92ba
 				exit(0);
5bf92ba
 			}
5bf92ba
@@ -184,7 +185,8 @@ static void filter_child(int c, struct sockaddr_storage *dest_ss)
5bf92ba
 		if (s != -1 && FD_ISSET(s, &fds)) {
5bf92ba
 			size_t len;
5bf92ba
 			if (!NT_STATUS_IS_OK(receive_smb_raw(
5bf92ba
-						     s, packet, 0, 0, &len))) {
5bf92ba
+							s, packet, sizeof(packet),
5bf92ba
+							0, 0, &len))) {
5bf92ba
 				d_printf("server closed connection\n");
5bf92ba
 				exit(0);
5bf92ba
 			}