- djm@cvs.openbsd.org 2003/05/15 14:55:25

[readconf.c readconf.h ssh_config ssh_config.5 sshconnect.c]
     add a ConnectTimeout option to ssh, based on patch from
     Jean-Charles Longuet (jclonguet at free.fr); portable #207 ok markus@
This commit is contained in:
Damien Miller 2003-05-16 11:39:04 +10:00
parent 99b4b88aba
commit b78d5eb6c5
6 changed files with 102 additions and 8 deletions

View File

@ -10,6 +10,10 @@
- markus@cvs.openbsd.org 2003/05/15 14:09:21 - markus@cvs.openbsd.org 2003/05/15 14:09:21
[auth2-krb5.c] [auth2-krb5.c]
fix 64bit issue; report itojun@ fix 64bit issue; report itojun@
- djm@cvs.openbsd.org 2003/05/15 14:55:25
[readconf.c readconf.h ssh_config ssh_config.5 sshconnect.c]
add a ConnectTimeout option to ssh, based on patch from
Jean-Charles Longuet (jclonguet at free.fr); portable #207 ok markus@
20030515 20030515
- (djm) OpenBSD CVS Sync - (djm) OpenBSD CVS Sync
@ -1541,4 +1545,4 @@
save auth method before monitor_reset_key_state(); bugzilla bug #284; save auth method before monitor_reset_key_state(); bugzilla bug #284;
ok provos@ ok provos@
$Id: ChangeLog,v 1.2726 2003/05/16 01:38:46 djm Exp $ $Id: ChangeLog,v 1.2727 2003/05/16 01:39:04 djm Exp $

View File

@ -12,7 +12,7 @@
*/ */
#include "includes.h" #include "includes.h"
RCSID("$OpenBSD: readconf.c,v 1.110 2003/05/15 14:02:47 jakob Exp $"); RCSID("$OpenBSD: readconf.c,v 1.111 2003/05/15 14:55:25 djm Exp $");
#include "ssh.h" #include "ssh.h"
#include "xmalloc.h" #include "xmalloc.h"
@ -106,7 +106,7 @@ typedef enum {
oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication, oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
oHostKeyAlgorithms, oBindAddress, oSmartcardDevice, oHostKeyAlgorithms, oBindAddress, oSmartcardDevice,
oClearAllForwardings, oNoHostAuthenticationForLocalhost, oClearAllForwardings, oNoHostAuthenticationForLocalhost,
oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
oDeprecated, oUnsupported oDeprecated, oUnsupported
} OpCodes; } OpCodes;
@ -193,6 +193,7 @@ static struct {
#endif #endif
{ "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost }, { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
{ "rekeylimit", oRekeyLimit }, { "rekeylimit", oRekeyLimit },
{ "connecttimeout", oConnectTimeout },
{ NULL, oBadOption } { NULL, oBadOption }
}; };
@ -309,6 +310,20 @@ process_config_line(Options *options, const char *host,
/* don't panic, but count bad options */ /* don't panic, but count bad options */
return -1; return -1;
/* NOTREACHED */ /* NOTREACHED */
case oConnectTimeout:
intptr = &options->connection_timeout;
/* parse_time: */
arg = strdelim(&s);
if (!arg || *arg == '\0')
fatal("%s line %d: missing time value.",
filename, linenum);
if ((value = convtime(arg)) == -1)
fatal("%s line %d: invalid time value.",
filename, linenum);
if (*intptr == -1)
*intptr = value;
break;
case oForwardAgent: case oForwardAgent:
intptr = &options->forward_agent; intptr = &options->forward_agent;
parse_flag: parse_flag:
@ -808,6 +823,7 @@ initialize_options(Options * options)
options->compression_level = -1; options->compression_level = -1;
options->port = -1; options->port = -1;
options->connection_attempts = -1; options->connection_attempts = -1;
options->connection_timeout = -1;
options->number_of_password_prompts = -1; options->number_of_password_prompts = -1;
options->cipher = -1; options->cipher = -1;
options->ciphers = NULL; options->ciphers = NULL;

View File

@ -1,4 +1,4 @@
/* $OpenBSD: readconf.h,v 1.49 2003/05/15 01:48:10 jakob Exp $ */ /* $OpenBSD: readconf.h,v 1.50 2003/05/15 14:55:25 djm Exp $ */
/* /*
* Author: Tatu Ylonen <ylo@cs.hut.fi> * Author: Tatu Ylonen <ylo@cs.hut.fi>
@ -60,6 +60,8 @@ typedef struct {
int port; /* Port to connect. */ int port; /* Port to connect. */
int connection_attempts; /* Max attempts (seconds) before int connection_attempts; /* Max attempts (seconds) before
* giving up */ * giving up */
int connection_timeout; /* Max time (seconds) before
* aborting connection attempt */
int number_of_password_prompts; /* Max number of password int number_of_password_prompts; /* Max number of password
* prompts. */ * prompts. */
int cipher; /* Cipher to use. */ int cipher; /* Cipher to use. */

View File

@ -1,4 +1,4 @@
# $OpenBSD: ssh_config,v 1.16 2002/07/03 14:21:05 markus Exp $ # $OpenBSD: ssh_config,v 1.17 2003/05/15 14:55:25 djm Exp $
# This is the ssh client system-wide configuration file. See # This is the ssh client system-wide configuration file. See
# ssh_config(5) for more information. This file provides defaults for # ssh_config(5) for more information. This file provides defaults for
@ -25,6 +25,7 @@
# HostbasedAuthentication no # HostbasedAuthentication no
# BatchMode no # BatchMode no
# CheckHostIP yes # CheckHostIP yes
# ConnectTimeout 0
# StrictHostKeyChecking ask # StrictHostKeyChecking ask
# IdentityFile ~/.ssh/identity # IdentityFile ~/.ssh/identity
# IdentityFile ~/.ssh/id_rsa # IdentityFile ~/.ssh/id_rsa

View File

@ -34,7 +34,7 @@
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\" .\"
.\" $OpenBSD: ssh_config.5,v 1.8 2003/05/14 18:16:20 jakob Exp $ .\" $OpenBSD: ssh_config.5,v 1.9 2003/05/15 14:55:25 djm Exp $
.Dd September 25, 1999 .Dd September 25, 1999
.Dt SSH_CONFIG 5 .Dt SSH_CONFIG 5
.Os .Os
@ -227,6 +227,11 @@ Specifies the number of tries (one per second) to make before exiting.
The argument must be an integer. The argument must be an integer.
This may be useful in scripts if the connection sometimes fails. This may be useful in scripts if the connection sometimes fails.
The default is 1. The default is 1.
.It Cm ConnectTimeout
Specifies the timeout (in seconds) used when connecting to the ssh
server, instead of using the default system TCP timeout. This value is
used only when the target is down or really unreachable, not when it
refuses the connection.
.It Cm DynamicForward .It Cm DynamicForward
Specifies that a TCP/IP port on the local machine be forwarded Specifies that a TCP/IP port on the local machine be forwarded
over the secure channel, and the application over the secure channel, and the application

View File

@ -13,7 +13,7 @@
*/ */
#include "includes.h" #include "includes.h"
RCSID("$OpenBSD: sshconnect.c,v 1.140 2003/05/14 18:16:21 jakob Exp $"); RCSID("$OpenBSD: sshconnect.c,v 1.141 2003/05/15 14:55:25 djm Exp $");
#include <openssl/bn.h> #include <openssl/bn.h>
@ -218,6 +218,71 @@ ssh_create_socket(int privileged, struct addrinfo *ai)
return sock; return sock;
} }
static int
timeout_connect(int sockfd, const struct sockaddr *serv_addr,
socklen_t addrlen, int timeout)
{
fd_set *fdset;
struct timeval tv;
socklen_t optlen;
int fdsetsz, optval, rc;
if (timeout <= 0)
return (connect(sockfd, serv_addr, addrlen));
if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
return (-1);
rc = connect(sockfd, serv_addr, addrlen);
if (rc == 0)
return (0);
if (errno != EINPROGRESS)
return (-1);
fdsetsz = howmany(sockfd + 1, NFDBITS) * sizeof(fd_mask);
fdset = (fd_set *)xmalloc(fdsetsz);
memset(fdset, '\0', fdsetsz);
FD_SET(sockfd, fdset);
tv.tv_sec = timeout;
tv.tv_usec = 0;
for(;;) {
rc = select(sockfd + 1, NULL, fdset, NULL, &tv);
if (rc != -1 || errno != EINTR)
break;
}
switch(rc) {
case 0:
/* Timed out */
errno = ETIMEDOUT;
return (-1);
case -1:
/* Select error */
debug("select: %s", strerror(errno));
return (-1);
case 1:
/* Completed or failed */
optval = 0;
optlen = sizeof(optval);
if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval,
&optlen) == -1)
debug("getsockopt: %s", strerror(errno));
return (-1);
if (optval != 0) {
errno = optval;
return (-1);
}
break;
default:
/* Should not occur */
fatal("Bogus return (%d) from select()", rc);
}
return (0);
}
/* /*
* Opens a TCP/IP connection to the remote server on the given host. * Opens a TCP/IP connection to the remote server on the given host.
* The address of the remote host will be returned in hostaddr. * The address of the remote host will be returned in hostaddr.
@ -306,7 +371,8 @@ ssh_connect(const char *host, struct sockaddr_storage * hostaddr,
/* Any error is already output */ /* Any error is already output */
continue; continue;
if (connect(sock, ai->ai_addr, ai->ai_addrlen) >= 0) { if (timeout_connect(sock, ai->ai_addr, ai->ai_addrlen,
options.connection_timeout) >= 0) {
/* Successful connection. */ /* Successful connection. */
memcpy(hostaddr, ai->ai_addr, ai->ai_addrlen); memcpy(hostaddr, ai->ai_addr, ai->ai_addrlen);
break; break;