6879d98
We can get stuck if a write is going to block because both ends are writing and
6879d98
neither end is reading.  This is a port of a patch which aims to solve that
6879d98
problem, but for now it's incomplete because we don't handle partial writes.  A
6879d98
proper non-blocking implementation would require a bit more work.
6879d98
6879d98
diff -up krb5-1.7/src/appl/bsd/defines.h krb5-1.7/src/appl/bsd/defines.h
6879d98
--- krb5-1.7/src/appl/bsd/defines.h	2008-12-15 15:29:01.000000000 -0500
6879d98
+++ krb5-1.7/src/appl/bsd/defines.h	2009-06-04 14:18:48.000000000 -0400
6879d98
@@ -34,6 +34,7 @@ extern int kcmd (int *sock, char **ahost
6879d98
 		 enum kcmd_proto *protonum /* input and output */
6879d98
 		 );
6879d98
 
6879d98
+extern int rcmd_stream_has_unsent_data (void);
6879d98
 extern int rcmd_stream_read (int fd, char *buf, size_t len, int secondary);
6879d98
 extern int rcmd_stream_write (int fd, char *buf, size_t len, int secondary);
6879d98
 extern int getport (int * /* portnum */, int * /* addrfamily */);
6879d98
diff -up krb5-1.7/src/appl/bsd/kcmd.c krb5-1.7/src/appl/bsd/kcmd.c
6879d98
--- krb5-1.7/src/appl/bsd/kcmd.c	2009-04-15 16:07:15.000000000 -0400
6879d98
+++ krb5-1.7/src/appl/bsd/kcmd.c	2009-06-04 14:18:48.000000000 -0400
6879d98
@@ -686,6 +686,11 @@ void rcmd_stream_init_normal()
6879d98
     output = twrite;
6879d98
 }
6879d98
 
6879d98
+int rcmd_stream_has_unsent_data (void)
6879d98
+{
6879d98
+    return (nstored > 0);
6879d98
+}
6879d98
+
6879d98
 void rcmd_stream_init_krb5(in_keyblock, encrypt_flag, lencheck, am_client,
6879d98
 			   protonum)
6879d98
      krb5_keyblock *in_keyblock;
6879d98
@@ -846,7 +851,8 @@ static int v5_des_read(fd, buf, len, sec
6879d98
 	cc = krb5_net_read(bsd_context, fd, &c, 1);
6879d98
 	/* we should check for non-blocking here, but we'd have
6879d98
 	   to make it save partial reads as well. */
6879d98
-	if (cc <= 0) return cc; /* read error */
6879d98
+	if (cc == 0) return nreturned; /* EOF */
6879d98
+	if (cc < 0) return cc; /* read error */
6879d98
 	if (cc == 1) {
6879d98
 	    if (c == 0 || !do_lencheck) break;
6879d98
 	}
6879d98
diff -up krb5-1.7/src/appl/bsd/krsh.c krb5-1.7/src/appl/bsd/krsh.c
6879d98
--- krb5-1.7/src/appl/bsd/krsh.c	2009-06-04 14:18:47.000000000 -0400
6879d98
+++ krb5-1.7/src/appl/bsd/krsh.c	2009-06-04 14:18:48.000000000 -0400
6879d98
@@ -115,10 +115,11 @@ main(argc, argv0)
6879d98
      char **argv0;
6879d98
 {
6879d98
     int rem, pid = 0;
6879d98
-    char *host=0, **ap, buf[RCMD_BUFSIZ], *args, **argv = argv0, *user = 0;
6879d98
+    char *host=0, **ap, buf[PIPE_BUF], *args, **argv = argv0, *user = 0;
6879d98
     register int cc;
6879d98
     struct passwd *pwd;
6879d98
     fd_set readfrom, ready;
6879d98
+    fd_set writeto, ready_wr;
6879d98
     int one = 1;
6879d98
     struct servent *sp;
6879d98
     struct servent defaultservent;
6879d98
@@ -508,9 +509,14 @@ main(argc, argv0)
6879d98
     FD_ZERO(&readfrom);
6879d98
     FD_SET(rfd2, &readfrom);
6879d98
     FD_SET(rem, &readfrom);
6879d98
+    FD_ZERO(&writeto);
6879d98
     do {
6879d98
+	int max_fd;
6879d98
+	max_fd = (rfd2 > rem) ? rfd2 : rem;
6879d98
+	max_fd = (max_fd > 2) ? max_fd : 2;
6879d98
 	ready = readfrom;
6879d98
-	if (select(((rfd2 > rem) ? rfd2 : rem) + 1, &ready, 0, 0, 0) < 0) {
6879d98
+	ready_wr = writeto;
6879d98
+	if (select(max_fd + 1, &ready, &ready_wr, 0, 0) < 0) {
6879d98
 	    if (errno != EINTR) {
6879d98
 		perror("select");
6879d98
 		exit(1);
6879d98
@@ -518,22 +524,38 @@ main(argc, argv0)
6879d98
 	    continue;
6879d98
 	}
6879d98
 	if (FD_ISSET(rfd2, &ready)) {
6879d98
-	    errno = 0;
6879d98
-	    cc = rcmd_stream_read(rfd2, buf, sizeof buf, 1);
6879d98
-	    if (cc <= 0) {
6879d98
-		if ((errno != EWOULDBLOCK) && (errno != EAGAIN))
6879d98
-		    FD_CLR(rfd2, &readfrom);
6879d98
-	    } else
6879d98
-	      (void) write(2, buf, (unsigned) cc);
6879d98
+            FD_SET(2, &writeto);
6879d98
+	}
6879d98
+	if (FD_ISSET(2, &ready_wr)) {
6879d98
+	    do {
6879d98
+		errno = 0;
6879d98
+		cc = rcmd_stream_read(rfd2, buf, sizeof buf, 1);
6879d98
+		if (cc <= 0) {
6879d98
+		    if ((errno != EWOULDBLOCK) && (errno != EAGAIN)) {
6879d98
+			FD_CLR(rfd2, &readfrom);
6879d98
+			break;
6879d98
+		    }
6879d98
+		} else
6879d98
+		  (void) write(2, buf, (unsigned) cc);
6879d98
+	    } while (rcmd_stream_has_unsent_data());
6879d98
+	    FD_CLR(2, &writeto);
6879d98
 	}
6879d98
 	if (FD_ISSET(rem, &ready)) {
6879d98
-	    errno = 0;
6879d98
-	    cc = rcmd_stream_read(rem, buf, sizeof buf, 0);
6879d98
-	    if (cc <= 0) {
6879d98
-		if ((errno != EWOULDBLOCK) && (errno != EAGAIN))
6879d98
-		    FD_CLR(rem, &readfrom);
6879d98
-	    } else
6879d98
-	      (void) write(1, buf, (unsigned) cc);
6879d98
+	    FD_SET(1, &writeto);
6879d98
+	}
6879d98
+	if (FD_ISSET(1, &ready_wr)) {
6879d98
+	    do {
6879d98
+		errno = 0;
6879d98
+		cc = rcmd_stream_read(rem, buf, sizeof buf, 0);
6879d98
+		if (cc <= 0) {
6879d98
+		    if ((errno != EWOULDBLOCK) && (errno != EAGAIN)) {
6879d98
+			FD_CLR(rem, &readfrom);
6879d98
+			break;
6879d98
+		    }
6879d98
+		} else
6879d98
+		  (void) write(1, buf, (unsigned) cc);
6879d98
+	    } while (rcmd_stream_has_unsent_data());
6879d98
+	    FD_CLR(1, &writeto);
6879d98
 	}
6879d98
     } while (FD_ISSET(rem, &readfrom) || FD_ISSET(rfd2, &readfrom));
6879d98
     if (nflag == 0)
6879d98
diff -up krb5-1.7/src/appl/bsd/krshd.c krb5-1.7/src/appl/bsd/krshd.c
6879d98
--- krb5-1.7/src/appl/bsd/krshd.c	2009-06-04 14:18:47.000000000 -0400
6879d98
+++ krb5-1.7/src/appl/bsd/krshd.c	2009-06-04 14:18:48.000000000 -0400
6879d98
@@ -583,7 +583,8 @@ void doit(f, fromp)
6879d98
     short port;
6879d98
     int pv[2], pw[2], px[2], cc;
6879d98
     fd_set ready, readfrom;
6879d98
-    char buf[RCMD_BUFSIZ], sig;
6879d98
+    fd_set ready_wr, writeto;
6879d98
+    char buf[PIPE_BUF], sig;
6879d98
     struct sockaddr_storage localaddr;
6879d98
 #ifdef POSIX_SIGNALS
6879d98
     struct sigaction sa;
6879d98
@@ -1214,6 +1215,10 @@ void doit(f, fromp)
6879d98
 	    if (pw[0] > maxfd)
6879d98
 		maxfd = pw[0];
6879d98
 	    
6879d98
+	    if (px[1] > maxfd)
6879d98
+		maxfd = px[1];
6879d98
+	    FD_ZERO(&writeto);
6879d98
+
6879d98
 	    /* read from f, write to px[1] -- child stdin */
6879d98
 	    /* read from s, signal child */
6879d98
 	    /* read from pv[0], write to s -- child stderr */
6879d98
@@ -1221,36 +1226,47 @@ void doit(f, fromp)
6879d98
 
6879d98
 	    do {
6879d98
 		ready = readfrom;
6879d98
-		if (select(maxfd + 1, &ready, (fd_set *)0,
6879d98
+		ready_wr = writeto;
6879d98
+		if (select(maxfd + 1, &ready, &ready_wr,
6879d98
 			   (fd_set *)0, (struct timeval *)0) < 0) {
6879d98
 		    if (errno == EINTR) {
6879d98
 			continue;
6879d98
 		    } else {
6879d98
 			break;
6879d98
-		}
6879d98
+		    }
6879d98
 		}
6879d98
 
6879d98
 		if (port&&FD_ISSET(pv[0], &ready)) {
6879d98
+		    FD_SET(s, &writeto);
6879d98
+		    FD_CLR(pv[0], &readfrom);
6879d98
+		}
6879d98
+		if (port&&FD_ISSET(s, &ready_wr)) {
6879d98
 		    /* read from the child stderr, write to the net */
6879d98
 		    errno = 0;
6879d98
 		    cc = read(pv[0], buf, sizeof (buf));
6879d98
-		    if (cc <= 0) {
6879d98
+		    if ((cc <= 0) ||
6879d98
+			(rcmd_stream_write(s, buf, (unsigned) cc, 1) != cc)) {
6879d98
 			shutdown(s, 1+1);
6879d98
-			FD_CLR(pv[0], &readfrom);
6879d98
 		    } else {
6879d98
-			(void) rcmd_stream_write(s, buf, (unsigned) cc, 1);
6879d98
+			FD_SET(pv[0], &readfrom);
6879d98
 		    }
6879d98
+		    FD_CLR(s, &writeto);
6879d98
 		}
6879d98
 		if (FD_ISSET(pw[0], &ready)) {
6879d98
+		    FD_SET(f, &writeto);
6879d98
+		    FD_CLR(pw[0], &readfrom);
6879d98
+		}
6879d98
+		if (FD_ISSET(f, &ready_wr)) {
6879d98
 		    /* read from the child stdout, write to the net */
6879d98
 		    errno = 0;
6879d98
 		    cc = read(pw[0], buf, sizeof (buf));
6879d98
-		    if (cc <= 0) {
6879d98
+		    if ((cc <= 0) ||
6879d98
+			(rcmd_stream_write(f, buf, (unsigned) cc, 0) != cc)) {
6879d98
 			shutdown(f, 1+1);
6879d98
-			FD_CLR(pw[0], &readfrom);
6879d98
 		    } else {
6879d98
-			(void) rcmd_stream_write(f, buf, (unsigned) cc, 0);
6879d98
+			FD_SET(pw[0], &readfrom);
6879d98
 		    }
6879d98
+		    FD_CLR(f, &writeto);
6879d98
 		}
6879d98
 		if (port&&FD_ISSET(s, &ready)) {
6879d98
 		    /* read from the alternate channel, signal the child */
6879d98
@@ -1268,12 +1284,15 @@ void doit(f, fromp)
6879d98
 		    }
6879d98
 		}
6879d98
 		if (FD_ISSET(f, &ready)) {
6879d98
+		    FD_SET(px[1], &writeto);
6879d98
+		    FD_CLR(f, &readfrom);
6879d98
+		}
6879d98
+		if (FD_ISSET(px[1], &ready_wr)) {
6879d98
 		    /* read from the net, write to child stdin */
6879d98
 		    errno = 0;
6879d98
 		    cc = rcmd_stream_read(f, buf, sizeof(buf), 0);
6879d98
 		    if (cc <= 0) {
6879d98
 			(void) close(px[1]);
6879d98
-			FD_CLR(f, &readfrom);
6879d98
 		    } else {
6879d98
 		        int wcc;
6879d98
 		        wcc = write(px[1], buf, (unsigned) cc);
6879d98
@@ -1281,17 +1300,22 @@ void doit(f, fromp)
6879d98
 			  /* pipe closed, don't read any more */
6879d98
 			  /* might check for EPIPE */
6879d98
 			  (void) close(px[1]);
6879d98
-			  FD_CLR(f, &readfrom);
6879d98
-			} else if (wcc != cc) {
6879d98
-			  syslog(LOG_INFO, "only wrote %d/%d to child", 
6879d98
-				 wcc, cc);
6879d98
+			} else {
6879d98
+			    if (wcc != cc)
6879d98
+				syslog(LOG_INFO, "only wrote %d/%d to child", 
6879d98
+				       wcc, cc);
6879d98
+			    FD_SET(f, &readfrom);
6879d98
 			}
6879d98
 		    }
6879d98
+		    FD_CLR(px[1], &writeto);
6879d98
 		}
6879d98
 	    } while ((port&&FD_ISSET(s, &readfrom)) ||
6879d98
 		     FD_ISSET(f, &readfrom) ||
6879d98
 		     (port&&FD_ISSET(pv[0], &readfrom) )||
6879d98
-		     FD_ISSET(pw[0], &readfrom));
6879d98
+		     FD_ISSET(pw[0], &readfrom) ||
6879d98
+		     (port&&FD_ISSET(s, &writeto)) ||
6879d98
+		     FD_ISSET(f, &writeto) ||
6879d98
+		     FD_ISSET(px[1], &writeto));
6879d98
 	    ignore_signals();
6879d98
 #ifdef KERBEROS
6879d98
 	    syslog(LOG_INFO ,