summaryrefslogtreecommitdiffstats
path: root/krb5-1.5-io.patch
blob: 474c0cfa032a5ee95c31890644f5b80249711aea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
We can get stuck if a write is going to block because both ends are writing and
neither end is reading.  This is a port of a patch which aims to solve that
problem, but for now it's incomplete because we don't handle partial writes.  A
proper non-blocking implementation would require a bit more work.

diff -ur krb5-1.5/src/appl/bsd/defines.h krb5-1.5/src/appl/bsd/defines.h
--- krb5-1.5/src/appl/bsd/defines.h	2003-01-01 05:13:20.000000000 -0500
+++ krb5-1.5/src/appl/bsd/defines.h	2006-07-21 15:11:44.000000000 -0400
@@ -34,6 +34,7 @@
 		 enum kcmd_proto *protonum /* input and output */
 		 );
 
+extern int rcmd_stream_has_unsent_data (void);
 extern int rcmd_stream_read (int fd, char *buf, size_t len, int secondary);
 extern int rcmd_stream_write (int fd, char *buf, size_t len, int secondary);
 extern int getport (int * /* portnum */, int * /* addrfamily */);
diff -ur krb5-1.5/src/appl/bsd/kcmd.c krb5-1.5/src/appl/bsd/kcmd.c
--- krb5-1.5/src/appl/bsd/kcmd.c	2004-10-01 18:08:14.000000000 -0400
+++ krb5-1.5/src/appl/bsd/kcmd.c	2006-07-21 15:11:44.000000000 -0400
@@ -839,6 +839,11 @@
     output = twrite;
 }
 
+int rcmd_stream_has_unsent_data (void)
+{
+    return (nstored > 0);
+}
+
 void rcmd_stream_init_krb5(in_keyblock, encrypt_flag, lencheck, am_client,
 			   protonum)
      krb5_keyblock *in_keyblock;
@@ -1019,7 +1024,8 @@
 	cc = krb5_net_read(bsd_context, fd, &c, 1);
 	/* we should check for non-blocking here, but we'd have
 	   to make it save partial reads as well. */
-	if (cc <= 0) return cc; /* read error */
+	if (cc == 0) return nreturned; /* EOF */
+	if (cc < 0) return cc; /* read error */
 	if (cc == 1) {
 	    if (c == 0 || !do_lencheck) break;
 	}
diff -ur krb5-1.5/src/appl/bsd/krsh.c krb5-1.5/src/appl/bsd/krsh.c
--- krb5-1.5/src/appl/bsd/krsh.c	2006-07-21 16:05:57.000000000 -0400
+++ krb5-1.5/src/appl/bsd/krsh.c	2006-07-21 15:19:05.000000000 -0400
@@ -128,10 +128,11 @@
      char **argv0;
 {
     int rem, pid = 0;
-    char *host=0, *cp, **ap, buf[RCMD_BUFSIZ], *args, **argv = argv0, *user = 0;
+    char *host=0, *cp, **ap, buf[PIPE_BUF], *args, **argv = argv0, *user = 0;
     register int cc;
     struct passwd *pwd;
     fd_set readfrom, ready;
+    fd_set writeto, ready_wr;
     int one = 1;
     struct servent *sp;
     struct servent defaultservent;
@@ -548,9 +549,14 @@
     FD_ZERO(&readfrom);
     FD_SET(rfd2, &readfrom);
     FD_SET(rem, &readfrom);
+    FD_ZERO(&writeto);
     do {
+	int max_fd;
+	max_fd = (rfd2 > rem) ? rfd2 : rem;
+	max_fd = (max_fd > 2) ? max_fd : 2;
 	ready = readfrom;
-	if (select(((rfd2 > rem) ? rfd2 : rem) + 1, &ready, 0, 0, 0) < 0) {
+	ready_wr = writeto;
+	if (select(max_fd + 1, &ready, &ready_wr, 0, 0) < 0) {
 	    if (errno != EINTR) {
 		perror("select");
 		exit(1);
@@ -558,22 +564,38 @@
 	    continue;
 	}
 	if (FD_ISSET(rfd2, &ready)) {
-	    errno = 0;
-	    cc = rcmd_stream_read(rfd2, buf, sizeof buf, 1);
-	    if (cc <= 0) {
-		if ((errno != EWOULDBLOCK) && (errno != EAGAIN))
-		    FD_CLR(rfd2, &readfrom);
-	    } else
-	      (void) write(2, buf, (unsigned) cc);
+            FD_SET(2, &writeto);
+	}
+	if (FD_ISSET(2, &ready_wr)) {
+	    do {
+		errno = 0;
+		cc = rcmd_stream_read(rfd2, buf, sizeof buf, 1);
+		if (cc <= 0) {
+		    if ((errno != EWOULDBLOCK) && (errno != EAGAIN)) {
+			FD_CLR(rfd2, &readfrom);
+			break;
+		    }
+		} else
+		  (void) write(2, buf, (unsigned) cc);
+	    } while (rcmd_stream_has_unsent_data());
+	    FD_CLR(2, &writeto);
 	}
 	if (FD_ISSET(rem, &ready)) {
-	    errno = 0;
-	    cc = rcmd_stream_read(rem, buf, sizeof buf, 0);
-	    if (cc <= 0) {
-		if ((errno != EWOULDBLOCK) && (errno != EAGAIN))
-		    FD_CLR(rem, &readfrom);
-	    } else
-	      (void) write(1, buf, (unsigned) cc);
+	    FD_SET(1, &writeto);
+	}
+	if (FD_ISSET(1, &ready_wr)) {
+	    do {
+		errno = 0;
+		cc = rcmd_stream_read(rem, buf, sizeof buf, 0);
+		if (cc <= 0) {
+		    if ((errno != EWOULDBLOCK) && (errno != EAGAIN)) {
+			FD_CLR(rem, &readfrom);
+			break;
+		    }
+		} else
+		  (void) write(1, buf, (unsigned) cc);
+	    } while (rcmd_stream_has_unsent_data());
+	    FD_CLR(1, &writeto);
 	}
     } while (FD_ISSET(rem, &readfrom) || FD_ISSET(rfd2, &readfrom));
     if (nflag == 0)
diff -ur krb5-1.5/src/appl/bsd/krshd.c krb5-1.5/src/appl/bsd/krshd.c
--- krb5-1.5/src/appl/bsd/krshd.c	2006-06-20 00:06:52.000000000 -0400
+++ krb5-1.5/src/appl/bsd/krshd.c	2006-07-21 16:02:12.000000000 -0400
@@ -633,7 +633,8 @@
     short port;
     int pv[2], pw[2], px[2], cc;
     fd_set ready, readfrom;
-    char buf[RCMD_BUFSIZ], sig;
+    fd_set ready_wr, writeto;
+    char buf[PIPE_BUF], sig;
     struct sockaddr_storage localaddr;
 #ifdef POSIX_SIGNALS
     struct sigaction sa;
@@ -1261,6 +1262,10 @@
 	    if (pw[0] > maxfd)
 		maxfd = pw[0];
 	    
+	    if (px[1] > maxfd)
+		maxfd = px[1];
+	    FD_ZERO(&writeto);
+
 	    /* read from f, write to px[1] -- child stdin */
 	    /* read from s, signal child */
 	    /* read from pv[0], write to s -- child stderr */
@@ -1268,36 +1273,47 @@
 
 	    do {
 		ready = readfrom;
-		if (select(maxfd + 1, &ready, (fd_set *)0,
+		ready_wr = writeto;
+		if (select(maxfd + 1, &ready, &ready_wr,
 			   (fd_set *)0, (struct timeval *)0) < 0) {
 		    if (errno == EINTR) {
 			continue;
 		    } else {
 			break;
-		}
+		    }
 		}
 
 		if (port&&FD_ISSET(pv[0], &ready)) {
+		    FD_SET(s, &writeto);
+		    FD_CLR(pv[0], &readfrom);
+		}
+		if (port&&FD_ISSET(s, &ready_wr)) {
 		    /* read from the child stderr, write to the net */
 		    errno = 0;
 		    cc = read(pv[0], buf, sizeof (buf));
-		    if (cc <= 0) {
+		    if ((cc <= 0) ||
+			(rcmd_stream_write(s, buf, (unsigned) cc, 1) != cc)) {
 			shutdown(s, 1+1);
-			FD_CLR(pv[0], &readfrom);
 		    } else {
-			(void) rcmd_stream_write(s, buf, (unsigned) cc, 1);
+			FD_SET(pv[0], &readfrom);
 		    }
+		    FD_CLR(s, &writeto);
 		}
 		if (FD_ISSET(pw[0], &ready)) {
+		    FD_SET(f, &writeto);
+		    FD_CLR(pw[0], &readfrom);
+		}
+		if (FD_ISSET(f, &ready_wr)) {
 		    /* read from the child stdout, write to the net */
 		    errno = 0;
 		    cc = read(pw[0], buf, sizeof (buf));
-		    if (cc <= 0) {
+		    if ((cc <= 0) ||
+			(rcmd_stream_write(f, buf, (unsigned) cc, 0) != cc)) {
 			shutdown(f, 1+1);
-			FD_CLR(pw[0], &readfrom);
 		    } else {
-			(void) rcmd_stream_write(f, buf, (unsigned) cc, 0);
+			FD_SET(pw[0], &readfrom);
 		    }
+		    FD_CLR(f, &writeto);
 		}
 		if (port&&FD_ISSET(s, &ready)) {
 		    /* read from the alternate channel, signal the child */
@@ -1315,12 +1331,15 @@
 		    }
 		}
 		if (FD_ISSET(f, &ready)) {
+		    FD_SET(px[1], &writeto);
+		    FD_CLR(f, &readfrom);
+		}
+		if (FD_ISSET(px[1], &ready_wr)) {
 		    /* read from the net, write to child stdin */
 		    errno = 0;
 		    cc = rcmd_stream_read(f, buf, sizeof(buf), 0);
 		    if (cc <= 0) {
 			(void) close(px[1]);
-			FD_CLR(f, &readfrom);
 		    } else {
 		        int wcc;
 		        wcc = write(px[1], buf, (unsigned) cc);
@@ -1328,17 +1347,22 @@
 			  /* pipe closed, don't read any more */
 			  /* might check for EPIPE */
 			  (void) close(px[1]);
-			  FD_CLR(f, &readfrom);
-			} else if (wcc != cc) {
-			  syslog(LOG_INFO, "only wrote %d/%d to child", 
-				 wcc, cc);
+			} else {
+			    if (wcc != cc)
+				syslog(LOG_INFO, "only wrote %d/%d to child", 
+				       wcc, cc);
+			    FD_SET(f, &readfrom);
 			}
 		    }
+		    FD_CLR(px[1], &writeto);
 		}
 	    } while ((port&&FD_ISSET(s, &readfrom)) ||
 		     FD_ISSET(f, &readfrom) ||
 		     (port&&FD_ISSET(pv[0], &readfrom) )||
-		     FD_ISSET(pw[0], &readfrom));
+		     FD_ISSET(pw[0], &readfrom) ||
+		     (port&&FD_ISSET(s, &writeto)) ||
+		     FD_ISSET(f, &writeto) ||
+		     FD_ISSET(px[1], &writeto));
 	    ignore_signals();
 #ifdef KERBEROS
 	    syslog(LOG_INFO ,