summaryrefslogtreecommitdiff
path: root/sshconnect.c
diff options
context:
space:
mode:
authordjm@openbsd.org <djm@openbsd.org>2018-01-23 05:17:04 +0000
committerDamien Miller <djm@mindrot.org>2018-01-23 16:40:28 +1100
commit7c77991f5de5d8475cbeb7cbb06d0c7d1611d7bb (patch)
treea91937cb7ce1bc2d7ae0e52160a5102199e16987 /sshconnect.c
parent9e9c4a7e57b96ab29fe6d7545ed09d2e5bddbdec (diff)
upstream commit
try harder to preserve errno during ssh_connect_direct() to make the final error message possibly accurate; bz#2814, ok dtucker@ OpenBSD-Commit-ID: 57de882cb47381c319b04499fef845dd0c2b46ca
Diffstat (limited to 'sshconnect.c')
-rw-r--r--sshconnect.c17
1 files changed, 12 insertions, 5 deletions
diff --git a/sshconnect.c b/sshconnect.c
index 44977707d..c25e192c8 100644
--- a/sshconnect.c
+++ b/sshconnect.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: sshconnect.c,v 1.289 2017/12/06 05:06:21 djm Exp $ */ 1/* $OpenBSD: sshconnect.c,v 1.290 2018/01/23 05:17:04 djm Exp $ */
2/* 2/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi> 3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@@ -416,7 +416,7 @@ ssh_connect_direct(struct ssh *ssh, const char *host, struct addrinfo *aitop,
416 int connection_attempts, int *timeout_ms, int want_keepalive, int needpriv) 416 int connection_attempts, int *timeout_ms, int want_keepalive, int needpriv)
417{ 417{
418 int on = 1; 418 int on = 1;
419 int sock = -1, attempt; 419 int oerrno, sock = -1, attempt;
420 char ntop[NI_MAXHOST], strport[NI_MAXSERV]; 420 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
421 struct addrinfo *ai; 421 struct addrinfo *ai;
422 422
@@ -436,12 +436,16 @@ ssh_connect_direct(struct ssh *ssh, const char *host, struct addrinfo *aitop,
436 */ 436 */
437 for (ai = aitop; ai; ai = ai->ai_next) { 437 for (ai = aitop; ai; ai = ai->ai_next) {
438 if (ai->ai_family != AF_INET && 438 if (ai->ai_family != AF_INET &&
439 ai->ai_family != AF_INET6) 439 ai->ai_family != AF_INET6) {
440 errno = EAFNOSUPPORT;
440 continue; 441 continue;
442 }
441 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, 443 if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
442 ntop, sizeof(ntop), strport, sizeof(strport), 444 ntop, sizeof(ntop), strport, sizeof(strport),
443 NI_NUMERICHOST|NI_NUMERICSERV) != 0) { 445 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
446 oerrno = errno;
444 error("%s: getnameinfo failed", __func__); 447 error("%s: getnameinfo failed", __func__);
448 errno = oerrno;
445 continue; 449 continue;
446 } 450 }
447 debug("Connecting to %.200s [%.100s] port %s.", 451 debug("Connecting to %.200s [%.100s] port %s.",
@@ -451,6 +455,7 @@ ssh_connect_direct(struct ssh *ssh, const char *host, struct addrinfo *aitop,
451 sock = ssh_create_socket(needpriv, ai); 455 sock = ssh_create_socket(needpriv, ai);
452 if (sock < 0) 456 if (sock < 0)
453 /* Any error is already output */ 457 /* Any error is already output */
458 errno = 0;
454 continue; 459 continue;
455 460
456 if (timeout_connect(sock, ai->ai_addr, ai->ai_addrlen, 461 if (timeout_connect(sock, ai->ai_addr, ai->ai_addrlen,
@@ -459,10 +464,12 @@ ssh_connect_direct(struct ssh *ssh, const char *host, struct addrinfo *aitop,
459 memcpy(hostaddr, ai->ai_addr, ai->ai_addrlen); 464 memcpy(hostaddr, ai->ai_addr, ai->ai_addrlen);
460 break; 465 break;
461 } else { 466 } else {
467 oerrno = errno;
462 debug("connect to address %s port %s: %s", 468 debug("connect to address %s port %s: %s",
463 ntop, strport, strerror(errno)); 469 ntop, strport, strerror(errno));
464 close(sock); 470 close(sock);
465 sock = -1; 471 sock = -1;
472 errno = oerrno;
466 } 473 }
467 } 474 }
468 if (sock != -1) 475 if (sock != -1)
@@ -472,8 +479,8 @@ ssh_connect_direct(struct ssh *ssh, const char *host, struct addrinfo *aitop,
472 /* Return failure if we didn't get a successful connection. */ 479 /* Return failure if we didn't get a successful connection. */
473 if (sock == -1) { 480 if (sock == -1) {
474 error("ssh: connect to host %s port %s: %s", 481 error("ssh: connect to host %s port %s: %s",
475 host, strport, strerror(errno)); 482 host, strport, errno == 0 ? "failure" : strerror(errno));
476 return (-1); 483 return -1;
477 } 484 }
478 485
479 debug("Connection established."); 486 debug("Connection established.");