summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>2005-05-26 12:23:44 +1000
committerDamien Miller <djm@mindrot.org>2005-05-26 12:23:44 +1000
commitb253cc42136649e3eac80e02667f8fbc1e43baaa (patch)
treee3824a905c7b12e4901e60e87ecdc968228e645e
parent02e754f1f01470c11a175a0d07381361afe37fff (diff)
- avsm@cvs.openbsd.org 2005/05/24 17:32:44
[atomicio.c atomicio.h authfd.c monitor_wrap.c msg.c scp.c sftp-client.c] [ssh-keyscan.c sshconnect.c] Switch atomicio to use a simpler interface; it now returns a size_t (containing number of bytes read/written), and indicates error by returning 0. EOF is signalled by errno==EPIPE. Typical use now becomes: if (atomicio(read, ..., len) != len) err(1,"read"); ok deraadt@, cloder@, djm@
-rw-r--r--ChangeLog14
-rw-r--r--atomicio.c14
-rw-r--r--atomicio.h4
-rw-r--r--authfd.c5
-rw-r--r--monitor_wrap.c19
-rw-r--r--msg.c15
-rw-r--r--scp.c26
-rw-r--r--sftp-client.c29
-rw-r--r--ssh-keyscan.c27
-rw-r--r--sshconnect.c13
10 files changed, 91 insertions, 75 deletions
diff --git a/ChangeLog b/ChangeLog
index 25bac49ab..b01e746bb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -83,6 +83,18 @@
83 - avsm@cvs.openbsd.org 2005/05/24 02:05:09 83 - avsm@cvs.openbsd.org 2005/05/24 02:05:09
84 [ssh-keygen.c] 84 [ssh-keygen.c]
85 some style nits from dmiller@, and use a fatal() instead of a printf()/exit 85 some style nits from dmiller@, and use a fatal() instead of a printf()/exit
86 - avsm@cvs.openbsd.org 2005/05/24 17:32:44
87 [atomicio.c atomicio.h authfd.c monitor_wrap.c msg.c scp.c sftp-client.c]
88 [ssh-keyscan.c sshconnect.c]
89 Switch atomicio to use a simpler interface; it now returns a size_t
90 (containing number of bytes read/written), and indicates error by
91 returning 0. EOF is signalled by errno==EPIPE.
92 Typical use now becomes:
93
94 if (atomicio(read, ..., len) != len)
95 err(1,"read");
96
97 ok deraadt@, cloder@, djm@
86 98
8720050524 9920050524
88 - (djm) [contrib/caldera/openssh.spec contrib/redhat/openssh.spec] 100 - (djm) [contrib/caldera/openssh.spec contrib/redhat/openssh.spec]
@@ -2582,4 +2594,4 @@
2582 - (djm) Trim deprecated options from INSTALL. Mention UsePAM 2594 - (djm) Trim deprecated options from INSTALL. Mention UsePAM
2583 - (djm) Fix quote handling in sftp; Patch from admorten AT umich.edu 2595 - (djm) Fix quote handling in sftp; Patch from admorten AT umich.edu
2584 2596
2585$Id: ChangeLog,v 1.3785 2005/05/26 02:19:39 djm Exp $ 2597$Id: ChangeLog,v 1.3786 2005/05/26 02:23:44 djm Exp $
diff --git a/atomicio.c b/atomicio.c
index 7637e1671..12abbda16 100644
--- a/atomicio.c
+++ b/atomicio.c
@@ -1,4 +1,5 @@
1/* 1/*
2 * Copyright (c) 2005 Anil Madhavapeddy. All rights reserved.
2 * Copyright (c) 1995,1999 Theo de Raadt. All rights reserved. 3 * Copyright (c) 1995,1999 Theo de Raadt. All rights reserved.
3 * All rights reserved. 4 * All rights reserved.
4 * 5 *
@@ -24,14 +25,14 @@
24 */ 25 */
25 26
26#include "includes.h" 27#include "includes.h"
27RCSID("$OpenBSD: atomicio.c,v 1.12 2003/07/31 15:50:16 avsm Exp $"); 28RCSID("$OpenBSD: atomicio.c,v 1.13 2005/05/24 17:32:43 avsm Exp $");
28 29
29#include "atomicio.h" 30#include "atomicio.h"
30 31
31/* 32/*
32 * ensure all of data on socket comes through. f==read || f==vwrite 33 * ensure all of data on socket comes through. f==read || f==vwrite
33 */ 34 */
34ssize_t 35size_t
35atomicio(f, fd, _s, n) 36atomicio(f, fd, _s, n)
36 ssize_t (*f) (int, void *, size_t); 37 ssize_t (*f) (int, void *, size_t);
37 int fd; 38 int fd;
@@ -39,7 +40,8 @@ atomicio(f, fd, _s, n)
39 size_t n; 40 size_t n;
40{ 41{
41 char *s = _s; 42 char *s = _s;
42 ssize_t res, pos = 0; 43 size_t pos = 0;
44 ssize_t res;
43 45
44 while (n > pos) { 46 while (n > pos) {
45 res = (f) (fd, s + pos, n - pos); 47 res = (f) (fd, s + pos, n - pos);
@@ -51,10 +53,12 @@ atomicio(f, fd, _s, n)
51 if (errno == EINTR || errno == EAGAIN) 53 if (errno == EINTR || errno == EAGAIN)
52#endif 54#endif
53 continue; 55 continue;
56 return 0;
54 case 0: 57 case 0:
55 return (res); 58 errno = EPIPE;
59 return pos;
56 default: 60 default:
57 pos += res; 61 pos += (u_int)res;
58 } 62 }
59 } 63 }
60 return (pos); 64 return (pos);
diff --git a/atomicio.h b/atomicio.h
index 5c0f392ef..7eccf206b 100644
--- a/atomicio.h
+++ b/atomicio.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: atomicio.h,v 1.5 2003/06/28 16:23:06 deraadt Exp $ */ 1/* $OpenBSD: atomicio.h,v 1.6 2005/05/24 17:32:43 avsm Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1995,1999 Theo de Raadt. All rights reserved. 4 * Copyright (c) 1995,1999 Theo de Raadt. All rights reserved.
@@ -28,6 +28,6 @@
28/* 28/*
29 * Ensure all of data on socket comes through. f==read || f==vwrite 29 * Ensure all of data on socket comes through. f==read || f==vwrite
30 */ 30 */
31ssize_t atomicio(ssize_t (*)(int, void *, size_t), int, void *, size_t); 31size_t atomicio(ssize_t (*)(int, void *, size_t), int, void *, size_t);
32 32
33#define vwrite (ssize_t (*)(int, void *, size_t))write 33#define vwrite (ssize_t (*)(int, void *, size_t))write
diff --git a/authfd.c b/authfd.c
index 662350cef..9ce5b5ea8 100644
--- a/authfd.c
+++ b/authfd.c
@@ -35,7 +35,7 @@
35 */ 35 */
36 36
37#include "includes.h" 37#include "includes.h"
38RCSID("$OpenBSD: authfd.c,v 1.64 2004/08/11 21:44:31 avsm Exp $"); 38RCSID("$OpenBSD: authfd.c,v 1.65 2005/05/24 17:32:43 avsm Exp $");
39 39
40#include <openssl/evp.h> 40#include <openssl/evp.h>
41 41
@@ -149,8 +149,7 @@ ssh_request_reply(AuthenticationConnection *auth, Buffer *request, Buffer *reply
149 l = len; 149 l = len;
150 if (l > sizeof(buf)) 150 if (l > sizeof(buf))
151 l = sizeof(buf); 151 l = sizeof(buf);
152 l = atomicio(read, auth->fd, buf, l); 152 if (atomicio(read, auth->fd, buf, l) != l) {
153 if (l <= 0) {
154 error("Error reading response from authentication socket."); 153 error("Error reading response from authentication socket.");
155 return 0; 154 return 0;
156 } 155 }
diff --git a/monitor_wrap.c b/monitor_wrap.c
index e1b6512b4..afa612f4e 100644
--- a/monitor_wrap.c
+++ b/monitor_wrap.c
@@ -25,7 +25,7 @@
25 */ 25 */
26 26
27#include "includes.h" 27#include "includes.h"
28RCSID("$OpenBSD: monitor_wrap.c,v 1.39 2004/07/17 05:31:41 dtucker Exp $"); 28RCSID("$OpenBSD: monitor_wrap.c,v 1.40 2005/05/24 17:32:43 avsm Exp $");
29 29
30#include <openssl/bn.h> 30#include <openssl/bn.h>
31#include <openssl/dh.h> 31#include <openssl/dh.h>
@@ -95,9 +95,9 @@ mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
95 PUT_32BIT(buf, mlen + 1); 95 PUT_32BIT(buf, mlen + 1);
96 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */ 96 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
97 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf)) 97 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
98 fatal("%s: write", __func__); 98 fatal("%s: write: %s", __func__, strerror(errno));
99 if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen) 99 if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
100 fatal("%s: write", __func__); 100 fatal("%s: write: %s", __func__, strerror(errno));
101} 101}
102 102
103void 103void
@@ -105,24 +105,21 @@ mm_request_receive(int sock, Buffer *m)
105{ 105{
106 u_char buf[4]; 106 u_char buf[4];
107 u_int msg_len; 107 u_int msg_len;
108 ssize_t res;
109 108
110 debug3("%s entering", __func__); 109 debug3("%s entering", __func__);
111 110
112 res = atomicio(read, sock, buf, sizeof(buf)); 111 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
113 if (res != sizeof(buf)) { 112 if (errno == EPIPE)
114 if (res == 0)
115 cleanup_exit(255); 113 cleanup_exit(255);
116 fatal("%s: read: %ld", __func__, (long)res); 114 fatal("%s: read: %s", __func__, strerror(errno));
117 } 115 }
118 msg_len = GET_32BIT(buf); 116 msg_len = GET_32BIT(buf);
119 if (msg_len > 256 * 1024) 117 if (msg_len > 256 * 1024)
120 fatal("%s: read: bad msg_len %d", __func__, msg_len); 118 fatal("%s: read: bad msg_len %d", __func__, msg_len);
121 buffer_clear(m); 119 buffer_clear(m);
122 buffer_append_space(m, msg_len); 120 buffer_append_space(m, msg_len);
123 res = atomicio(read, sock, buffer_ptr(m), msg_len); 121 if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
124 if (res != msg_len) 122 fatal("%s: read: %s", __func__, strerror(errno));
125 fatal("%s: read: %ld != msg_len", __func__, (long)res);
126} 123}
127 124
128void 125void
diff --git a/msg.c b/msg.c
index 30bc3f107..3e4c2882c 100644
--- a/msg.c
+++ b/msg.c
@@ -22,7 +22,7 @@
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */ 23 */
24#include "includes.h" 24#include "includes.h"
25RCSID("$OpenBSD: msg.c,v 1.7 2003/11/17 09:45:39 djm Exp $"); 25RCSID("$OpenBSD: msg.c,v 1.8 2005/05/24 17:32:43 avsm Exp $");
26 26
27#include "buffer.h" 27#include "buffer.h"
28#include "getput.h" 28#include "getput.h"
@@ -55,15 +55,13 @@ int
55ssh_msg_recv(int fd, Buffer *m) 55ssh_msg_recv(int fd, Buffer *m)
56{ 56{
57 u_char buf[4]; 57 u_char buf[4];
58 ssize_t res;
59 u_int msg_len; 58 u_int msg_len;
60 59
61 debug3("ssh_msg_recv entering"); 60 debug3("ssh_msg_recv entering");
62 61
63 res = atomicio(read, fd, buf, sizeof(buf)); 62 if (atomicio(read, fd, buf, sizeof(buf)) != sizeof(buf)) {
64 if (res != sizeof(buf)) { 63 if (errno != EPIPE)
65 if (res != 0) 64 error("ssh_msg_recv: read: header");
66 error("ssh_msg_recv: read: header %ld", (long)res);
67 return (-1); 65 return (-1);
68 } 66 }
69 msg_len = GET_32BIT(buf); 67 msg_len = GET_32BIT(buf);
@@ -73,9 +71,8 @@ ssh_msg_recv(int fd, Buffer *m)
73 } 71 }
74 buffer_clear(m); 72 buffer_clear(m);
75 buffer_append_space(m, msg_len); 73 buffer_append_space(m, msg_len);
76 res = atomicio(read, fd, buffer_ptr(m), msg_len); 74 if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) {
77 if (res != msg_len) { 75 error("ssh_msg_recv: read: %s", strerror(errno));
78 error("ssh_msg_recv: read: %ld != msg_len", (long)res);
79 return (-1); 76 return (-1);
80 } 77 }
81 return (0); 78 return (0);
diff --git a/scp.c b/scp.c
index 1d34cc639..1455c18fd 100644
--- a/scp.c
+++ b/scp.c
@@ -71,7 +71,7 @@
71 */ 71 */
72 72
73#include "includes.h" 73#include "includes.h"
74RCSID("$OpenBSD: scp.c,v 1.121 2005/04/02 12:41:16 djm Exp $"); 74RCSID("$OpenBSD: scp.c,v 1.122 2005/05/24 17:32:43 avsm Exp $");
75 75
76#include "xmalloc.h" 76#include "xmalloc.h"
77#include "atomicio.h" 77#include "atomicio.h"
@@ -502,7 +502,8 @@ source(int argc, char **argv)
502 struct stat stb; 502 struct stat stb;
503 static BUF buffer; 503 static BUF buffer;
504 BUF *bp; 504 BUF *bp;
505 off_t i, amt, result, statbytes; 505 off_t i, amt, statbytes;
506 size_t result;
506 int fd, haderr, indx; 507 int fd, haderr, indx;
507 char *last, *name, buf[2048]; 508 char *last, *name, buf[2048];
508 int len; 509 int len;
@@ -578,14 +579,14 @@ next: (void) close(fd);
578 if (!haderr) { 579 if (!haderr) {
579 result = atomicio(read, fd, bp->buf, amt); 580 result = atomicio(read, fd, bp->buf, amt);
580 if (result != amt) 581 if (result != amt)
581 haderr = result >= 0 ? EIO : errno; 582 haderr = errno;
582 } 583 }
583 if (haderr) 584 if (haderr)
584 (void) atomicio(vwrite, remout, bp->buf, amt); 585 (void) atomicio(vwrite, remout, bp->buf, amt);
585 else { 586 else {
586 result = atomicio(vwrite, remout, bp->buf, amt); 587 result = atomicio(vwrite, remout, bp->buf, amt);
587 if (result != amt) 588 if (result != amt)
588 haderr = result >= 0 ? EIO : errno; 589 haderr = errno;
589 statbytes += result; 590 statbytes += result;
590 } 591 }
591 if (limit_rate) 592 if (limit_rate)
@@ -720,7 +721,8 @@ sink(int argc, char **argv)
720 YES, NO, DISPLAYED 721 YES, NO, DISPLAYED
721 } wrerr; 722 } wrerr;
722 BUF *bp; 723 BUF *bp;
723 off_t i, j; 724 off_t i;
725 size_t j;
724 int amt, count, exists, first, mask, mode, ofd, omode; 726 int amt, count, exists, first, mask, mode, ofd, omode;
725 off_t size, statbytes; 727 off_t size, statbytes;
726 int setimes, targisdir, wrerrno = 0; 728 int setimes, targisdir, wrerrno = 0;
@@ -748,7 +750,7 @@ sink(int argc, char **argv)
748 targisdir = 1; 750 targisdir = 1;
749 for (first = 1;; first = 0) { 751 for (first = 1;; first = 0) {
750 cp = buf; 752 cp = buf;
751 if (atomicio(read, remin, cp, 1) <= 0) 753 if (atomicio(read, remin, cp, 1) != 1)
752 return; 754 return;
753 if (*cp++ == '\n') 755 if (*cp++ == '\n')
754 SCREWUP("unexpected <newline>"); 756 SCREWUP("unexpected <newline>");
@@ -902,7 +904,7 @@ bad: run_err("%s: %s", np, strerror(errno));
902 count += amt; 904 count += amt;
903 do { 905 do {
904 j = atomicio(read, remin, cp, amt); 906 j = atomicio(read, remin, cp, amt);
905 if (j <= 0) { 907 if (j == 0) {
906 run_err("%s", j ? strerror(errno) : 908 run_err("%s", j ? strerror(errno) :
907 "dropped connection"); 909 "dropped connection");
908 exit(1); 910 exit(1);
@@ -918,10 +920,10 @@ bad: run_err("%s: %s", np, strerror(errno));
918 if (count == bp->cnt) { 920 if (count == bp->cnt) {
919 /* Keep reading so we stay sync'd up. */ 921 /* Keep reading so we stay sync'd up. */
920 if (wrerr == NO) { 922 if (wrerr == NO) {
921 j = atomicio(vwrite, ofd, bp->buf, count); 923 if (atomicio(vwrite, ofd, bp->buf,
922 if (j != count) { 924 count) != count) {
923 wrerr = YES; 925 wrerr = YES;
924 wrerrno = j >= 0 ? EIO : errno; 926 wrerrno = errno;
925 } 927 }
926 } 928 }
927 count = 0; 929 count = 0;
@@ -931,9 +933,9 @@ bad: run_err("%s: %s", np, strerror(errno));
931 if (showprogress) 933 if (showprogress)
932 stop_progress_meter(); 934 stop_progress_meter();
933 if (count != 0 && wrerr == NO && 935 if (count != 0 && wrerr == NO &&
934 (j = atomicio(vwrite, ofd, bp->buf, count)) != count) { 936 atomicio(vwrite, ofd, bp->buf, count) != count) {
935 wrerr = YES; 937 wrerr = YES;
936 wrerrno = j >= 0 ? EIO : errno; 938 wrerrno = errno;
937 } 939 }
938 if (wrerr == NO && ftruncate(ofd, size) != 0) { 940 if (wrerr == NO && ftruncate(ofd, size) != 0) {
939 run_err("%s: truncate: %s", np, strerror(errno)); 941 run_err("%s: truncate: %s", np, strerror(errno));
diff --git a/sftp-client.c b/sftp-client.c
index 92df42751..47297898a 100644
--- a/sftp-client.c
+++ b/sftp-client.c
@@ -20,7 +20,7 @@
20/* XXX: copy between two remote sites */ 20/* XXX: copy between two remote sites */
21 21
22#include "includes.h" 22#include "includes.h"
23RCSID("$OpenBSD: sftp-client.c,v 1.53 2005/03/10 22:01:05 deraadt Exp $"); 23RCSID("$OpenBSD: sftp-client.c,v 1.54 2005/05/24 17:32:44 avsm Exp $");
24 24
25#include "openbsd-compat/sys-queue.h" 25#include "openbsd-compat/sys-queue.h"
26 26
@@ -64,10 +64,10 @@ send_msg(int fd, Buffer *m)
64 64
65 /* Send length first */ 65 /* Send length first */
66 PUT_32BIT(mlen, buffer_len(m)); 66 PUT_32BIT(mlen, buffer_len(m));
67 if (atomicio(vwrite, fd, mlen, sizeof(mlen)) <= 0) 67 if (atomicio(vwrite, fd, mlen, sizeof(mlen)) != sizeof(mlen))
68 fatal("Couldn't send packet: %s", strerror(errno)); 68 fatal("Couldn't send packet: %s", strerror(errno));
69 69
70 if (atomicio(vwrite, fd, buffer_ptr(m), buffer_len(m)) <= 0) 70 if (atomicio(vwrite, fd, buffer_ptr(m), buffer_len(m)) != buffer_len(m))
71 fatal("Couldn't send packet: %s", strerror(errno)); 71 fatal("Couldn't send packet: %s", strerror(errno));
72 72
73 buffer_clear(m); 73 buffer_clear(m);
@@ -76,26 +76,27 @@ send_msg(int fd, Buffer *m)
76static void 76static void
77get_msg(int fd, Buffer *m) 77get_msg(int fd, Buffer *m)
78{ 78{
79 ssize_t len;
80 u_int msg_len; 79 u_int msg_len;
81 80
82 buffer_append_space(m, 4); 81 buffer_append_space(m, 4);
83 len = atomicio(read, fd, buffer_ptr(m), 4); 82 if (atomicio(read, fd, buffer_ptr(m), 4) != 4) {
84 if (len == 0) 83 if (errno == EPIPE)
85 fatal("Connection closed"); 84 fatal("Connection closed");
86 else if (len == -1) 85 else
87 fatal("Couldn't read packet: %s", strerror(errno)); 86 fatal("Couldn't read packet: %s", strerror(errno));
87 }
88 88
89 msg_len = buffer_get_int(m); 89 msg_len = buffer_get_int(m);
90 if (msg_len > MAX_MSG_LENGTH) 90 if (msg_len > MAX_MSG_LENGTH)
91 fatal("Received message too long %u", msg_len); 91 fatal("Received message too long %u", msg_len);
92 92
93 buffer_append_space(m, msg_len); 93 buffer_append_space(m, msg_len);
94 len = atomicio(read, fd, buffer_ptr(m), msg_len); 94 if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) {
95 if (len == 0) 95 if (errno == EPIPE)
96 fatal("Connection closed"); 96 fatal("Connection closed");
97 else if (len == -1) 97 else
98 fatal("Read packet: %s", strerror(errno)); 98 fatal("Read packet: %s", strerror(errno));
99 }
99} 100}
100 101
101static void 102static void
diff --git a/ssh-keyscan.c b/ssh-keyscan.c
index fdcfc5b3c..7dffb8517 100644
--- a/ssh-keyscan.c
+++ b/ssh-keyscan.c
@@ -7,7 +7,7 @@
7 */ 7 */
8 8
9#include "includes.h" 9#include "includes.h"
10RCSID("$OpenBSD: ssh-keyscan.c,v 1.53 2005/04/28 10:17:56 moritz Exp $"); 10RCSID("$OpenBSD: ssh-keyscan.c,v 1.54 2005/05/24 17:32:44 avsm Exp $");
11 11
12#include "openbsd-compat/sys-queue.h" 12#include "openbsd-compat/sys-queue.h"
13 13
@@ -493,10 +493,10 @@ conrecycle(int s)
493static void 493static void
494congreet(int s) 494congreet(int s)
495{ 495{
496 int remote_major = 0, remote_minor = 0, n = 0; 496 int remote_major = 0, remote_minor = 0;
497 char buf[256], *cp; 497 char buf[256], *cp;
498 char remote_version[sizeof buf]; 498 char remote_version[sizeof buf];
499 size_t bufsiz; 499 size_t bufsiz, n = 0;
500 con *c = &fdcon[s]; 500 con *c = &fdcon[s];
501 501
502 bufsiz = sizeof(buf); 502 bufsiz = sizeof(buf);
@@ -506,14 +506,17 @@ congreet(int s)
506 *cp = '\n'; 506 *cp = '\n';
507 cp++; 507 cp++;
508 } 508 }
509 if (n < 0) {
510 if (errno != ECONNREFUSED)
511 error("read (%s): %s", c->c_name, strerror(errno));
512 conrecycle(s);
513 return;
514 }
515 if (n == 0) { 509 if (n == 0) {
516 error("%s: Connection closed by remote host", c->c_name); 510 switch (errno) {
511 case EPIPE:
512 error("%s: Connection closed by remote host", c->c_name);
513 break;
514 case ECONNREFUSED:
515 break;
516 default:
517 error("read (%s): %s", c->c_name, strerror(errno));
518 break;
519 }
517 conrecycle(s); 520 conrecycle(s);
518 return; 521 return;
519 } 522 }
@@ -566,14 +569,14 @@ static void
566conread(int s) 569conread(int s)
567{ 570{
568 con *c = &fdcon[s]; 571 con *c = &fdcon[s];
569 int n; 572 size_t n;
570 573
571 if (c->c_status == CS_CON) { 574 if (c->c_status == CS_CON) {
572 congreet(s); 575 congreet(s);
573 return; 576 return;
574 } 577 }
575 n = atomicio(read, s, c->c_data + c->c_off, c->c_len - c->c_off); 578 n = atomicio(read, s, c->c_data + c->c_off, c->c_len - c->c_off);
576 if (n < 0) { 579 if (n == 0) {
577 error("read (%s): %s", c->c_name, strerror(errno)); 580 error("read (%s): %s", c->c_name, strerror(errno));
578 confree(s); 581 confree(s);
579 return; 582 return;
diff --git a/sshconnect.c b/sshconnect.c
index 07703cf77..b79cead5d 100644
--- a/sshconnect.c
+++ b/sshconnect.c
@@ -13,7 +13,7 @@
13 */ 13 */
14 14
15#include "includes.h" 15#include "includes.h"
16RCSID("$OpenBSD: sshconnect.c,v 1.162 2005/03/10 22:01:06 deraadt Exp $"); 16RCSID("$OpenBSD: sshconnect.c,v 1.163 2005/05/24 17:32:44 avsm Exp $");
17 17
18#include <openssl/bn.h> 18#include <openssl/bn.h>
19 19
@@ -426,14 +426,15 @@ ssh_exchange_identification(void)
426 int connection_out = packet_get_connection_out(); 426 int connection_out = packet_get_connection_out();
427 int minor1 = PROTOCOL_MINOR_1; 427 int minor1 = PROTOCOL_MINOR_1;
428 428
429 /* Read other side\'s version identification. */ 429 /* Read other side's version identification. */
430 for (;;) { 430 for (;;) {
431 for (i = 0; i < sizeof(buf) - 1; i++) { 431 for (i = 0; i < sizeof(buf) - 1; i++) {
432 int len = atomicio(read, connection_in, &buf[i], 1); 432 size_t len = atomicio(read, connection_in, &buf[i], 1);
433 if (len < 0) 433
434 fatal("ssh_exchange_identification: read: %.100s", strerror(errno)); 434 if (len != 1 && errno == EPIPE)
435 if (len != 1)
436 fatal("ssh_exchange_identification: Connection closed by remote host"); 435 fatal("ssh_exchange_identification: Connection closed by remote host");
436 else if (len != 1)
437 fatal("ssh_exchange_identification: read: %.100s", strerror(errno));
437 if (buf[i] == '\r') { 438 if (buf[i] == '\r') {
438 buf[i] = '\n'; 439 buf[i] = '\n';
439 buf[i + 1] = 0; 440 buf[i + 1] = 0;