2014-01-30 10:56:49 +00:00
|
|
|
/* $OpenBSD: ssh-keyscan.c,v 1.89 2013/12/06 13:39:49 markus Exp $ */
|
2001-05-04 03:57:05 +00:00
|
|
|
/*
|
|
|
|
* Copyright 1995, 1996 by David Mazieres <dm@lcs.mit.edu>.
|
|
|
|
*
|
|
|
|
* Modification and redistribution in source and binary forms is
|
|
|
|
* permitted provided that due credit is given to the author and the
|
2002-03-18 09:55:03 +00:00
|
|
|
* OpenBSD project by leaving this copyright notice intact.
|
2001-05-04 03:57:05 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
2006-09-30 13:29:51 +00:00
|
|
|
|
2002-10-29 09:43:00 +00:00
|
|
|
#include "openbsd-compat/sys-queue.h"
|
2006-09-30 13:29:51 +00:00
|
|
|
#include <sys/resource.h>
|
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
# include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2001-05-04 03:57:05 +00:00
|
|
|
|
|
|
|
#include <openssl/bn.h>
|
|
|
|
|
2006-09-30 13:29:51 +00:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <errno.h>
|
2002-03-18 09:55:03 +00:00
|
|
|
#include <setjmp.h>
|
2006-09-30 13:29:51 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2001-05-04 03:57:05 +00:00
|
|
|
#include "xmalloc.h"
|
|
|
|
#include "ssh.h"
|
|
|
|
#include "ssh1.h"
|
2006-09-30 13:29:51 +00:00
|
|
|
#include "buffer.h"
|
2001-05-04 03:57:05 +00:00
|
|
|
#include "key.h"
|
2006-09-30 13:29:51 +00:00
|
|
|
#include "cipher.h"
|
2002-03-18 09:55:03 +00:00
|
|
|
#include "kex.h"
|
|
|
|
#include "compat.h"
|
|
|
|
#include "myproposal.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "dispatch.h"
|
2001-05-04 03:57:05 +00:00
|
|
|
#include "log.h"
|
|
|
|
#include "atomicio.h"
|
2002-03-18 09:55:03 +00:00
|
|
|
#include "misc.h"
|
2005-06-05 15:40:50 +00:00
|
|
|
#include "hostfile.h"
|
2001-05-04 03:57:05 +00:00
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
/* Flag indicating whether IPv4 or IPv6. This can be set on the command line.
|
|
|
|
Default value is AF_UNSPEC means both IPv4 and IPv6. */
|
|
|
|
int IPv4or6 = AF_UNSPEC;
|
2001-05-04 03:57:05 +00:00
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
int ssh_port = SSH_DEFAULT_PORT;
|
|
|
|
|
2011-02-17 11:47:40 +00:00
|
|
|
#define KT_RSA1 1
|
|
|
|
#define KT_DSA 2
|
|
|
|
#define KT_RSA 4
|
|
|
|
#define KT_ECDSA 8
|
2014-01-30 10:56:49 +00:00
|
|
|
#define KT_ED25519 16
|
2002-03-18 09:55:03 +00:00
|
|
|
|
2012-08-29 15:55:54 +00:00
|
|
|
int get_keytypes = KT_RSA|KT_ECDSA;/* Get RSA and ECDSA keys by default */
|
2001-05-04 03:57:05 +00:00
|
|
|
|
2005-06-05 15:40:50 +00:00
|
|
|
int hash_hosts = 0; /* Hash hostname on output */
|
|
|
|
|
2001-05-04 03:57:05 +00:00
|
|
|
#define MAXMAXFD 256
|
|
|
|
|
|
|
|
/* The number of seconds after which to give up on a TCP connection */
|
|
|
|
int timeout = 5;
|
|
|
|
|
|
|
|
int maxfd;
|
|
|
|
#define MAXCON (maxfd - 10)
|
|
|
|
|
|
|
|
extern char *__progname;
|
|
|
|
fd_set *read_wait;
|
2006-09-30 13:29:51 +00:00
|
|
|
size_t read_wait_nfdset;
|
2001-05-04 03:57:05 +00:00
|
|
|
int ncon;
|
2002-03-18 09:55:03 +00:00
|
|
|
int nonfatal_fatal = 0;
|
|
|
|
jmp_buf kexjmp;
|
|
|
|
Key *kexjmp_key;
|
2001-05-04 03:57:05 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Keep a connection structure for each file descriptor. The state
|
|
|
|
* associated with file descriptor n is held in fdcon[n].
|
|
|
|
*/
|
|
|
|
typedef struct Connection {
|
|
|
|
u_char c_status; /* State of connection on this file desc. */
|
|
|
|
#define CS_UNUSED 0 /* File descriptor unused */
|
|
|
|
#define CS_CON 1 /* Waiting to connect/read greeting */
|
|
|
|
#define CS_SIZE 2 /* Waiting to read initial packet size */
|
|
|
|
#define CS_KEYS 3 /* Waiting to read public key packet */
|
|
|
|
int c_fd; /* Quick lookup: c->c_fd == c - fdcon */
|
|
|
|
int c_plen; /* Packet length field for ssh packet */
|
|
|
|
int c_len; /* Total bytes which must be read. */
|
|
|
|
int c_off; /* Length of data read so far. */
|
2002-03-18 09:55:03 +00:00
|
|
|
int c_keytype; /* Only one of KT_RSA1, KT_DSA, or KT_RSA */
|
2001-05-04 03:57:05 +00:00
|
|
|
char *c_namebase; /* Address to free for c_name and c_namelist */
|
|
|
|
char *c_name; /* Hostname of connection for errors */
|
|
|
|
char *c_namelist; /* Pointer to other possible addresses */
|
|
|
|
char *c_output_name; /* Hostname of connection for output */
|
|
|
|
char *c_data; /* Data read from this fd */
|
2002-03-18 09:55:03 +00:00
|
|
|
Kex *c_kex; /* The key-exchange struct for ssh2 */
|
2001-05-04 03:57:05 +00:00
|
|
|
struct timeval c_tv; /* Time at which connection gets aborted */
|
|
|
|
TAILQ_ENTRY(Connection) c_link; /* List of connections in timeout order. */
|
|
|
|
} con;
|
|
|
|
|
|
|
|
TAILQ_HEAD(conlist, Connection) tq; /* Timeout Queue */
|
|
|
|
con *fdcon;
|
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
static int
|
2001-05-04 03:57:05 +00:00
|
|
|
fdlim_get(int hard)
|
|
|
|
{
|
2002-06-27 22:31:32 +00:00
|
|
|
#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
|
2001-05-04 03:57:05 +00:00
|
|
|
struct rlimit rlfd;
|
|
|
|
|
|
|
|
if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
|
|
|
|
return (-1);
|
|
|
|
if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY)
|
2004-02-26 10:38:49 +00:00
|
|
|
return SSH_SYSFDMAX;
|
2001-05-04 03:57:05 +00:00
|
|
|
else
|
|
|
|
return hard ? rlfd.rlim_max : rlfd.rlim_cur;
|
2002-06-27 22:31:32 +00:00
|
|
|
#else
|
2004-02-26 10:38:49 +00:00
|
|
|
return SSH_SYSFDMAX;
|
2002-06-27 22:31:32 +00:00
|
|
|
#endif
|
2001-05-04 03:57:05 +00:00
|
|
|
}
|
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
static int
|
2001-05-04 03:57:05 +00:00
|
|
|
fdlim_set(int lim)
|
|
|
|
{
|
2002-06-27 22:31:32 +00:00
|
|
|
#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
|
2001-05-04 03:57:05 +00:00
|
|
|
struct rlimit rlfd;
|
2002-06-27 22:31:32 +00:00
|
|
|
#endif
|
2002-10-29 09:43:00 +00:00
|
|
|
|
2001-05-04 03:57:05 +00:00
|
|
|
if (lim <= 0)
|
|
|
|
return (-1);
|
2002-06-27 22:31:32 +00:00
|
|
|
#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
|
2001-05-04 03:57:05 +00:00
|
|
|
if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
|
|
|
|
return (-1);
|
|
|
|
rlfd.rlim_cur = lim;
|
|
|
|
if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0)
|
|
|
|
return (-1);
|
2002-06-27 22:31:32 +00:00
|
|
|
#elif defined (HAVE_SETDTABLESIZE)
|
|
|
|
setdtablesize(lim);
|
|
|
|
#endif
|
2001-05-04 03:57:05 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is an strsep function that returns a null field for adjacent
|
|
|
|
* separators. This is the same as the 4.4BSD strsep, but different from the
|
|
|
|
* one in the GNU libc.
|
|
|
|
*/
|
2002-03-18 09:55:03 +00:00
|
|
|
static char *
|
2001-05-04 03:57:05 +00:00
|
|
|
xstrsep(char **str, const char *delim)
|
|
|
|
{
|
|
|
|
char *s, *e;
|
|
|
|
|
|
|
|
if (!**str)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
s = *str;
|
|
|
|
e = s + strcspn(s, delim);
|
|
|
|
|
|
|
|
if (*e != '\0')
|
|
|
|
*e++ = '\0';
|
|
|
|
*str = e;
|
|
|
|
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the next non-null token (like GNU strsep). Strsep() will return a
|
|
|
|
* null token for two adjacent separators, so we may have to loop.
|
|
|
|
*/
|
2002-03-18 09:55:03 +00:00
|
|
|
static char *
|
2001-05-04 03:57:05 +00:00
|
|
|
strnnsep(char **stringp, char *delim)
|
|
|
|
{
|
|
|
|
char *tok;
|
|
|
|
|
|
|
|
do {
|
|
|
|
tok = xstrsep(stringp, delim);
|
|
|
|
} while (tok && *tok == '\0');
|
|
|
|
return (tok);
|
|
|
|
}
|
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
static Key *
|
|
|
|
keygrab_ssh1(con *c)
|
2001-05-04 03:57:05 +00:00
|
|
|
{
|
|
|
|
static Key *rsa;
|
|
|
|
static Buffer msg;
|
|
|
|
|
|
|
|
if (rsa == NULL) {
|
|
|
|
buffer_init(&msg);
|
|
|
|
rsa = key_new(KEY_RSA1);
|
|
|
|
}
|
2002-03-18 09:55:03 +00:00
|
|
|
buffer_append(&msg, c->c_data, c->c_plen);
|
|
|
|
buffer_consume(&msg, 8 - (c->c_plen & 7)); /* padding */
|
2001-05-04 03:57:05 +00:00
|
|
|
if (buffer_get_char(&msg) != (int) SSH_SMSG_PUBLIC_KEY) {
|
2002-03-18 09:55:03 +00:00
|
|
|
error("%s: invalid packet type", c->c_name);
|
2001-05-04 03:57:05 +00:00
|
|
|
buffer_clear(&msg);
|
2002-03-18 09:55:03 +00:00
|
|
|
return NULL;
|
2001-05-04 03:57:05 +00:00
|
|
|
}
|
|
|
|
buffer_consume(&msg, 8); /* cookie */
|
|
|
|
|
|
|
|
/* server key */
|
|
|
|
(void) buffer_get_int(&msg);
|
|
|
|
buffer_get_bignum(&msg, rsa->rsa->e);
|
|
|
|
buffer_get_bignum(&msg, rsa->rsa->n);
|
|
|
|
|
|
|
|
/* host key */
|
|
|
|
(void) buffer_get_int(&msg);
|
|
|
|
buffer_get_bignum(&msg, rsa->rsa->e);
|
|
|
|
buffer_get_bignum(&msg, rsa->rsa->n);
|
2002-03-18 09:55:03 +00:00
|
|
|
|
2001-05-04 03:57:05 +00:00
|
|
|
buffer_clear(&msg);
|
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
return (rsa);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
hostjump(Key *hostkey)
|
|
|
|
{
|
|
|
|
kexjmp_key = hostkey;
|
|
|
|
longjmp(kexjmp, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ssh2_capable(int remote_major, int remote_minor)
|
|
|
|
{
|
|
|
|
switch (remote_major) {
|
|
|
|
case 1:
|
|
|
|
if (remote_minor == 99)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Key *
|
|
|
|
keygrab_ssh2(con *c)
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
|
|
|
|
packet_set_connection(c->c_fd, c->c_fd);
|
|
|
|
enable_compat20();
|
2014-01-30 10:56:49 +00:00
|
|
|
myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
|
|
|
|
c->c_keytype == KT_DSA ? "ssh-dss" :
|
|
|
|
(c->c_keytype == KT_RSA ? "ssh-rsa" :
|
|
|
|
(c->c_keytype == KT_ED25519 ? "ssh-ed25519" :
|
|
|
|
"ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521"));
|
2002-03-18 09:55:03 +00:00
|
|
|
c->c_kex = kex_setup(myproposal);
|
2003-04-23 16:53:02 +00:00
|
|
|
c->c_kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
|
2004-10-28 16:03:53 +00:00
|
|
|
c->c_kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
|
2003-04-23 16:53:02 +00:00
|
|
|
c->c_kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
|
2006-09-30 13:29:51 +00:00
|
|
|
c->c_kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
|
2011-02-17 11:47:40 +00:00
|
|
|
c->c_kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
|
2014-01-30 10:56:49 +00:00
|
|
|
c->c_kex->kex[KEX_C25519_SHA256] = kexc25519_client;
|
2002-03-18 09:55:03 +00:00
|
|
|
c->c_kex->verify_host_key = hostjump;
|
|
|
|
|
|
|
|
if (!(j = setjmp(kexjmp))) {
|
|
|
|
nonfatal_fatal = 1;
|
|
|
|
dispatch_run(DISPATCH_BLOCK, &c->c_kex->done, c->c_kex);
|
|
|
|
fprintf(stderr, "Impossible! dispatch_run() returned!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
nonfatal_fatal = 0;
|
2013-09-18 17:27:38 +00:00
|
|
|
free(c->c_kex);
|
2002-03-18 09:55:03 +00:00
|
|
|
c->c_kex = NULL;
|
|
|
|
packet_close();
|
|
|
|
|
|
|
|
return j < 0? NULL : kexjmp_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
keyprint(con *c, Key *key)
|
|
|
|
{
|
2005-06-05 15:40:50 +00:00
|
|
|
char *host = c->c_output_name ? c->c_output_name : c->c_name;
|
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
if (!key)
|
|
|
|
return;
|
2005-06-05 15:40:50 +00:00
|
|
|
if (hash_hosts && (host = host_hash(host, NULL, 0)) == NULL)
|
|
|
|
fatal("host_hash failed");
|
2002-03-18 09:55:03 +00:00
|
|
|
|
2005-06-05 15:40:50 +00:00
|
|
|
fprintf(stdout, "%s ", host);
|
2002-03-18 09:55:03 +00:00
|
|
|
key_write(key, stdout);
|
2001-05-04 03:57:05 +00:00
|
|
|
fputs("\n", stdout);
|
|
|
|
}
|
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
static int
|
2001-05-04 03:57:05 +00:00
|
|
|
tcpconnect(char *host)
|
|
|
|
{
|
|
|
|
struct addrinfo hints, *ai, *aitop;
|
|
|
|
char strport[NI_MAXSERV];
|
|
|
|
int gaierr, s = -1;
|
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
snprintf(strport, sizeof strport, "%d", ssh_port);
|
2001-05-04 03:57:05 +00:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
2002-03-18 09:55:03 +00:00
|
|
|
hints.ai_family = IPv4or6;
|
2001-05-04 03:57:05 +00:00
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
|
2008-07-23 09:28:49 +00:00
|
|
|
fatal("getaddrinfo %s: %s", host, ssh_gai_strerror(gaierr));
|
2001-05-04 03:57:05 +00:00
|
|
|
for (ai = aitop; ai; ai = ai->ai_next) {
|
2004-01-07 11:10:17 +00:00
|
|
|
s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
|
2001-05-04 03:57:05 +00:00
|
|
|
if (s < 0) {
|
|
|
|
error("socket: %s", strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
2004-10-28 16:03:53 +00:00
|
|
|
if (set_nonblock(s) == -1)
|
|
|
|
fatal("%s: set_nonblock(%d)", __func__, s);
|
2001-05-04 03:57:05 +00:00
|
|
|
if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 &&
|
|
|
|
errno != EINPROGRESS)
|
|
|
|
error("connect (`%s'): %s", host, strerror(errno));
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
close(s);
|
|
|
|
s = -1;
|
|
|
|
}
|
|
|
|
freeaddrinfo(aitop);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
static int
|
|
|
|
conalloc(char *iname, char *oname, int keytype)
|
2001-05-04 03:57:05 +00:00
|
|
|
{
|
|
|
|
char *namebase, *name, *namelist;
|
2002-10-29 09:43:00 +00:00
|
|
|
int s;
|
2001-05-04 03:57:05 +00:00
|
|
|
|
|
|
|
namebase = namelist = xstrdup(iname);
|
|
|
|
|
|
|
|
do {
|
|
|
|
name = xstrsep(&namelist, ",");
|
|
|
|
if (!name) {
|
2013-09-18 17:27:38 +00:00
|
|
|
free(namebase);
|
2001-05-04 03:57:05 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
} while ((s = tcpconnect(name)) < 0);
|
|
|
|
|
|
|
|
if (s >= maxfd)
|
|
|
|
fatal("conalloc: fdno %d too high", s);
|
|
|
|
if (fdcon[s].c_status)
|
|
|
|
fatal("conalloc: attempt to reuse fdno %d", s);
|
|
|
|
|
|
|
|
fdcon[s].c_fd = s;
|
|
|
|
fdcon[s].c_status = CS_CON;
|
|
|
|
fdcon[s].c_namebase = namebase;
|
|
|
|
fdcon[s].c_name = name;
|
|
|
|
fdcon[s].c_namelist = namelist;
|
|
|
|
fdcon[s].c_output_name = xstrdup(oname);
|
|
|
|
fdcon[s].c_data = (char *) &fdcon[s].c_plen;
|
|
|
|
fdcon[s].c_len = 4;
|
|
|
|
fdcon[s].c_off = 0;
|
2002-03-18 09:55:03 +00:00
|
|
|
fdcon[s].c_keytype = keytype;
|
2001-05-04 03:57:05 +00:00
|
|
|
gettimeofday(&fdcon[s].c_tv, NULL);
|
|
|
|
fdcon[s].c_tv.tv_sec += timeout;
|
|
|
|
TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
|
|
|
|
FD_SET(s, read_wait);
|
|
|
|
ncon++;
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
static void
|
2001-05-04 03:57:05 +00:00
|
|
|
confree(int s)
|
|
|
|
{
|
|
|
|
if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
|
|
|
|
fatal("confree: attempt to free bad fdno %d", s);
|
|
|
|
close(s);
|
2013-09-18 17:27:38 +00:00
|
|
|
free(fdcon[s].c_namebase);
|
|
|
|
free(fdcon[s].c_output_name);
|
2001-05-04 03:57:05 +00:00
|
|
|
if (fdcon[s].c_status == CS_KEYS)
|
2013-09-18 17:27:38 +00:00
|
|
|
free(fdcon[s].c_data);
|
2001-05-04 03:57:05 +00:00
|
|
|
fdcon[s].c_status = CS_UNUSED;
|
2002-03-18 09:55:03 +00:00
|
|
|
fdcon[s].c_keytype = 0;
|
2001-05-04 03:57:05 +00:00
|
|
|
TAILQ_REMOVE(&tq, &fdcon[s], c_link);
|
|
|
|
FD_CLR(s, read_wait);
|
|
|
|
ncon--;
|
|
|
|
}
|
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
static void
|
2001-05-04 03:57:05 +00:00
|
|
|
contouch(int s)
|
|
|
|
{
|
|
|
|
TAILQ_REMOVE(&tq, &fdcon[s], c_link);
|
|
|
|
gettimeofday(&fdcon[s].c_tv, NULL);
|
|
|
|
fdcon[s].c_tv.tv_sec += timeout;
|
|
|
|
TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
|
|
|
|
}
|
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
static int
|
2001-05-04 03:57:05 +00:00
|
|
|
conrecycle(int s)
|
|
|
|
{
|
|
|
|
con *c = &fdcon[s];
|
2002-10-29 09:43:00 +00:00
|
|
|
int ret;
|
2001-05-04 03:57:05 +00:00
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype);
|
2001-05-04 03:57:05 +00:00
|
|
|
confree(s);
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
static void
|
2001-05-04 03:57:05 +00:00
|
|
|
congreet(int s)
|
|
|
|
{
|
2005-09-03 06:59:33 +00:00
|
|
|
int n = 0, remote_major = 0, remote_minor = 0;
|
2002-03-18 09:55:03 +00:00
|
|
|
char buf[256], *cp;
|
|
|
|
char remote_version[sizeof buf];
|
2001-05-04 03:57:05 +00:00
|
|
|
size_t bufsiz;
|
|
|
|
con *c = &fdcon[s];
|
|
|
|
|
2006-03-22 19:46:12 +00:00
|
|
|
for (;;) {
|
|
|
|
memset(buf, '\0', sizeof(buf));
|
|
|
|
bufsiz = sizeof(buf);
|
|
|
|
cp = buf;
|
|
|
|
while (bufsiz-- &&
|
|
|
|
(n = atomicio(read, s, cp, 1)) == 1 && *cp != '\n') {
|
|
|
|
if (*cp == '\r')
|
|
|
|
*cp = '\n';
|
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
if (n != 1 || strncmp(buf, "SSH-", 4) == 0)
|
|
|
|
break;
|
2002-03-18 09:55:03 +00:00
|
|
|
}
|
|
|
|
if (n == 0) {
|
2005-09-03 06:59:33 +00:00
|
|
|
switch (errno) {
|
|
|
|
case EPIPE:
|
|
|
|
error("%s: Connection closed by remote host", c->c_name);
|
|
|
|
break;
|
|
|
|
case ECONNREFUSED:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("read (%s): %s", c->c_name, strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
2002-03-18 09:55:03 +00:00
|
|
|
conrecycle(s);
|
|
|
|
return;
|
|
|
|
}
|
2001-05-04 03:57:05 +00:00
|
|
|
if (*cp != '\n' && *cp != '\r') {
|
|
|
|
error("%s: bad greeting", c->c_name);
|
|
|
|
confree(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*cp = '\0';
|
2002-03-18 09:55:03 +00:00
|
|
|
if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
|
|
|
|
&remote_major, &remote_minor, remote_version) == 3)
|
|
|
|
compat_datafellows(remote_version);
|
|
|
|
else
|
|
|
|
datafellows = 0;
|
|
|
|
if (c->c_keytype != KT_RSA1) {
|
|
|
|
if (!ssh2_capable(remote_major, remote_minor)) {
|
|
|
|
debug("%s doesn't support ssh2", c->c_name);
|
|
|
|
confree(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (remote_major != 1) {
|
|
|
|
debug("%s doesn't support ssh1", c->c_name);
|
|
|
|
confree(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "# %s %s\n", c->c_name, chop(buf));
|
|
|
|
n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n",
|
|
|
|
c->c_keytype == KT_RSA1? PROTOCOL_MAJOR_1 : PROTOCOL_MAJOR_2,
|
|
|
|
c->c_keytype == KT_RSA1? PROTOCOL_MINOR_1 : PROTOCOL_MINOR_2);
|
2005-09-03 06:59:33 +00:00
|
|
|
if (n < 0 || (size_t)n >= sizeof(buf)) {
|
|
|
|
error("snprintf: buffer too small");
|
|
|
|
confree(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (atomicio(vwrite, s, buf, n) != (size_t)n) {
|
2001-05-04 03:57:05 +00:00
|
|
|
error("write (%s): %s", c->c_name, strerror(errno));
|
|
|
|
confree(s);
|
|
|
|
return;
|
|
|
|
}
|
2002-03-18 09:55:03 +00:00
|
|
|
if (c->c_keytype != KT_RSA1) {
|
|
|
|
keyprint(c, keygrab_ssh2(c));
|
|
|
|
confree(s);
|
|
|
|
return;
|
|
|
|
}
|
2001-05-04 03:57:05 +00:00
|
|
|
c->c_status = CS_SIZE;
|
|
|
|
contouch(s);
|
|
|
|
}
|
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
static void
|
2001-05-04 03:57:05 +00:00
|
|
|
conread(int s)
|
|
|
|
{
|
|
|
|
con *c = &fdcon[s];
|
2005-09-03 06:59:33 +00:00
|
|
|
size_t n;
|
2001-05-04 03:57:05 +00:00
|
|
|
|
|
|
|
if (c->c_status == CS_CON) {
|
|
|
|
congreet(s);
|
|
|
|
return;
|
|
|
|
}
|
2004-10-28 16:03:53 +00:00
|
|
|
n = atomicio(read, s, c->c_data + c->c_off, c->c_len - c->c_off);
|
2005-09-03 06:59:33 +00:00
|
|
|
if (n == 0) {
|
2001-05-04 03:57:05 +00:00
|
|
|
error("read (%s): %s", c->c_name, strerror(errno));
|
|
|
|
confree(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
c->c_off += n;
|
|
|
|
|
|
|
|
if (c->c_off == c->c_len)
|
|
|
|
switch (c->c_status) {
|
|
|
|
case CS_SIZE:
|
|
|
|
c->c_plen = htonl(c->c_plen);
|
|
|
|
c->c_len = c->c_plen + 8 - (c->c_plen & 7);
|
|
|
|
c->c_off = 0;
|
|
|
|
c->c_data = xmalloc(c->c_len);
|
|
|
|
c->c_status = CS_KEYS;
|
|
|
|
break;
|
|
|
|
case CS_KEYS:
|
2002-03-18 09:55:03 +00:00
|
|
|
keyprint(c, keygrab_ssh1(c));
|
2001-05-04 03:57:05 +00:00
|
|
|
confree(s);
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
fatal("conread: invalid status %d", c->c_status);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
contouch(s);
|
|
|
|
}
|
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
static void
|
2001-05-04 03:57:05 +00:00
|
|
|
conloop(void)
|
|
|
|
{
|
|
|
|
struct timeval seltime, now;
|
2002-10-29 09:43:00 +00:00
|
|
|
fd_set *r, *e;
|
2001-05-04 03:57:05 +00:00
|
|
|
con *c;
|
2002-10-29 09:43:00 +00:00
|
|
|
int i;
|
2001-05-04 03:57:05 +00:00
|
|
|
|
|
|
|
gettimeofday(&now, NULL);
|
2002-06-23 14:01:54 +00:00
|
|
|
c = TAILQ_FIRST(&tq);
|
2001-05-04 03:57:05 +00:00
|
|
|
|
|
|
|
if (c && (c->c_tv.tv_sec > now.tv_sec ||
|
|
|
|
(c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) {
|
|
|
|
seltime = c->c_tv;
|
|
|
|
seltime.tv_sec -= now.tv_sec;
|
|
|
|
seltime.tv_usec -= now.tv_usec;
|
|
|
|
if (seltime.tv_usec < 0) {
|
|
|
|
seltime.tv_usec += 1000000;
|
|
|
|
seltime.tv_sec--;
|
|
|
|
}
|
|
|
|
} else
|
2011-09-28 08:14:41 +00:00
|
|
|
timerclear(&seltime);
|
2001-05-04 03:57:05 +00:00
|
|
|
|
2006-09-30 13:29:51 +00:00
|
|
|
r = xcalloc(read_wait_nfdset, sizeof(fd_mask));
|
|
|
|
e = xcalloc(read_wait_nfdset, sizeof(fd_mask));
|
|
|
|
memcpy(r, read_wait, read_wait_nfdset * sizeof(fd_mask));
|
|
|
|
memcpy(e, read_wait, read_wait_nfdset * sizeof(fd_mask));
|
2001-05-04 03:57:05 +00:00
|
|
|
|
|
|
|
while (select(maxfd, r, NULL, e, &seltime) == -1 &&
|
2008-07-23 09:33:08 +00:00
|
|
|
(errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
|
2001-05-04 03:57:05 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
for (i = 0; i < maxfd; i++) {
|
|
|
|
if (FD_ISSET(i, e)) {
|
|
|
|
error("%s: exception!", fdcon[i].c_name);
|
|
|
|
confree(i);
|
|
|
|
} else if (FD_ISSET(i, r))
|
|
|
|
conread(i);
|
|
|
|
}
|
2013-09-18 17:27:38 +00:00
|
|
|
free(r);
|
|
|
|
free(e);
|
2001-05-04 03:57:05 +00:00
|
|
|
|
2002-06-23 14:01:54 +00:00
|
|
|
c = TAILQ_FIRST(&tq);
|
2001-05-04 03:57:05 +00:00
|
|
|
while (c && (c->c_tv.tv_sec < now.tv_sec ||
|
|
|
|
(c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) {
|
|
|
|
int s = c->c_fd;
|
|
|
|
|
2002-06-23 14:01:54 +00:00
|
|
|
c = TAILQ_NEXT(c, c_link);
|
2001-05-04 03:57:05 +00:00
|
|
|
conrecycle(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
static void
|
|
|
|
do_host(char *host)
|
2001-05-04 03:57:05 +00:00
|
|
|
{
|
2002-03-18 09:55:03 +00:00
|
|
|
char *name = strnnsep(&host, " \t\n");
|
|
|
|
int j;
|
2001-05-04 03:57:05 +00:00
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
if (name == NULL)
|
|
|
|
return;
|
2014-01-30 10:56:49 +00:00
|
|
|
for (j = KT_RSA1; j <= KT_ED25519; j *= 2) {
|
2002-03-18 09:55:03 +00:00
|
|
|
if (get_keytypes & j) {
|
|
|
|
while (ncon >= MAXCON)
|
|
|
|
conloop();
|
|
|
|
conalloc(name, *host ? host : name, j);
|
2001-05-04 03:57:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-03-18 09:55:03 +00:00
|
|
|
fatal(const char *fmt,...)
|
|
|
|
{
|
|
|
|
va_list args;
|
2002-10-29 09:43:00 +00:00
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
va_start(args, fmt);
|
|
|
|
do_log(SYSLOG_LEVEL_FATAL, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
if (nonfatal_fatal)
|
|
|
|
longjmp(kexjmp, -1);
|
|
|
|
else
|
2004-02-26 10:38:49 +00:00
|
|
|
exit(255);
|
2002-03-18 09:55:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-05-04 03:57:05 +00:00
|
|
|
usage(void)
|
|
|
|
{
|
2009-02-24 18:49:27 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"usage: %s [-46Hv] [-f file] [-p port] [-T timeout] [-t type]\n"
|
|
|
|
"\t\t [host | addrlist namelist] ...\n",
|
2002-03-18 09:55:03 +00:00
|
|
|
__progname);
|
|
|
|
exit(1);
|
2001-05-04 03:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2002-03-18 09:55:03 +00:00
|
|
|
int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO;
|
2010-11-08 10:45:44 +00:00
|
|
|
int opt, fopt_count = 0, j;
|
|
|
|
char *tname, *cp, line[NI_MAXHOST];
|
|
|
|
FILE *fp;
|
|
|
|
u_long linenum;
|
2002-03-18 09:55:03 +00:00
|
|
|
|
|
|
|
extern int optind;
|
|
|
|
extern char *optarg;
|
2001-05-04 03:57:05 +00:00
|
|
|
|
2004-01-07 11:10:17 +00:00
|
|
|
__progname = ssh_get_progname(argv[0]);
|
2002-06-27 22:31:32 +00:00
|
|
|
seed_rng();
|
2001-05-04 03:57:05 +00:00
|
|
|
TAILQ_INIT(&tq);
|
|
|
|
|
2006-03-22 19:46:12 +00:00
|
|
|
/* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
|
|
|
|
sanitise_stdfd();
|
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
if (argc <= 1)
|
2001-05-04 03:57:05 +00:00
|
|
|
usage();
|
|
|
|
|
2005-06-05 15:40:50 +00:00
|
|
|
while ((opt = getopt(argc, argv, "Hv46p:T:t:f:")) != -1) {
|
2002-03-18 09:55:03 +00:00
|
|
|
switch (opt) {
|
2005-06-05 15:40:50 +00:00
|
|
|
case 'H':
|
|
|
|
hash_hosts = 1;
|
|
|
|
break;
|
2002-03-18 09:55:03 +00:00
|
|
|
case 'p':
|
|
|
|
ssh_port = a2port(optarg);
|
2009-02-24 18:49:27 +00:00
|
|
|
if (ssh_port <= 0) {
|
2002-03-18 09:55:03 +00:00
|
|
|
fprintf(stderr, "Bad port '%s'\n", optarg);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'T':
|
2002-10-29 09:43:00 +00:00
|
|
|
timeout = convtime(optarg);
|
|
|
|
if (timeout == -1 || timeout == 0) {
|
|
|
|
fprintf(stderr, "Bad timeout '%s'\n", optarg);
|
2001-05-04 03:57:05 +00:00
|
|
|
usage();
|
2002-10-29 09:43:00 +00:00
|
|
|
}
|
2002-03-18 09:55:03 +00:00
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
if (!debug_flag) {
|
|
|
|
debug_flag = 1;
|
|
|
|
log_level = SYSLOG_LEVEL_DEBUG1;
|
|
|
|
}
|
|
|
|
else if (log_level < SYSLOG_LEVEL_DEBUG3)
|
|
|
|
log_level++;
|
|
|
|
else
|
|
|
|
fatal("Too high debugging level.");
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
if (strcmp(optarg, "-") == 0)
|
|
|
|
optarg = NULL;
|
|
|
|
argv[fopt_count++] = optarg;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
get_keytypes = 0;
|
|
|
|
tname = strtok(optarg, ",");
|
|
|
|
while (tname) {
|
|
|
|
int type = key_type_from_name(tname);
|
|
|
|
switch (type) {
|
|
|
|
case KEY_RSA1:
|
|
|
|
get_keytypes |= KT_RSA1;
|
|
|
|
break;
|
|
|
|
case KEY_DSA:
|
|
|
|
get_keytypes |= KT_DSA;
|
|
|
|
break;
|
2011-02-17 11:47:40 +00:00
|
|
|
case KEY_ECDSA:
|
|
|
|
get_keytypes |= KT_ECDSA;
|
|
|
|
break;
|
2002-03-18 09:55:03 +00:00
|
|
|
case KEY_RSA:
|
|
|
|
get_keytypes |= KT_RSA;
|
|
|
|
break;
|
2014-01-30 10:56:49 +00:00
|
|
|
case KEY_ED25519:
|
|
|
|
get_keytypes |= KT_ED25519;
|
|
|
|
break;
|
2002-03-18 09:55:03 +00:00
|
|
|
case KEY_UNSPEC:
|
|
|
|
fatal("unknown key type %s", tname);
|
|
|
|
}
|
|
|
|
tname = strtok(NULL, ",");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '4':
|
|
|
|
IPv4or6 = AF_INET;
|
|
|
|
break;
|
|
|
|
case '6':
|
|
|
|
IPv4or6 = AF_INET6;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
2001-05-04 03:57:05 +00:00
|
|
|
usage();
|
2002-03-18 09:55:03 +00:00
|
|
|
}
|
2001-05-04 03:57:05 +00:00
|
|
|
}
|
2002-03-18 09:55:03 +00:00
|
|
|
if (optind == argc && !fopt_count)
|
2001-05-04 03:57:05 +00:00
|
|
|
usage();
|
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1);
|
|
|
|
|
2001-05-04 03:57:05 +00:00
|
|
|
maxfd = fdlim_get(1);
|
|
|
|
if (maxfd < 0)
|
|
|
|
fatal("%s: fdlim_get: bad value", __progname);
|
|
|
|
if (maxfd > MAXMAXFD)
|
|
|
|
maxfd = MAXMAXFD;
|
|
|
|
if (MAXCON <= 0)
|
|
|
|
fatal("%s: not enough file descriptors", __progname);
|
|
|
|
if (maxfd > fdlim_get(0))
|
|
|
|
fdlim_set(maxfd);
|
2006-09-30 13:29:51 +00:00
|
|
|
fdcon = xcalloc(maxfd, sizeof(con));
|
2001-05-04 03:57:05 +00:00
|
|
|
|
2006-09-30 13:29:51 +00:00
|
|
|
read_wait_nfdset = howmany(maxfd, NFDBITS);
|
|
|
|
read_wait = xcalloc(read_wait_nfdset, sizeof(fd_mask));
|
2001-05-04 03:57:05 +00:00
|
|
|
|
2010-11-08 10:45:44 +00:00
|
|
|
for (j = 0; j < fopt_count; j++) {
|
|
|
|
if (argv[j] == NULL)
|
|
|
|
fp = stdin;
|
|
|
|
else if ((fp = fopen(argv[j], "r")) == NULL)
|
|
|
|
fatal("%s: %s: %s", __progname, argv[j],
|
|
|
|
strerror(errno));
|
|
|
|
linenum = 0;
|
|
|
|
|
|
|
|
while (read_keyfile_line(fp,
|
|
|
|
argv[j] == NULL ? "(stdin)" : argv[j], line, sizeof(line),
|
|
|
|
&linenum) != -1) {
|
|
|
|
/* Chomp off trailing whitespace and comments */
|
|
|
|
if ((cp = strchr(line, '#')) == NULL)
|
|
|
|
cp = line + strlen(line) - 1;
|
|
|
|
while (cp >= line) {
|
|
|
|
if (*cp == ' ' || *cp == '\t' ||
|
|
|
|
*cp == '\n' || *cp == '#')
|
|
|
|
*cp-- = '\0';
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2002-03-18 09:55:03 +00:00
|
|
|
|
2010-11-08 10:45:44 +00:00
|
|
|
/* Skip empty lines */
|
|
|
|
if (*line == '\0')
|
2002-03-18 09:55:03 +00:00
|
|
|
continue;
|
2010-11-08 10:45:44 +00:00
|
|
|
|
|
|
|
do_host(line);
|
2001-05-04 03:57:05 +00:00
|
|
|
}
|
2010-11-08 10:45:44 +00:00
|
|
|
|
|
|
|
if (ferror(fp))
|
|
|
|
fatal("%s: %s: %s", __progname, argv[j],
|
|
|
|
strerror(errno));
|
|
|
|
|
|
|
|
fclose(fp);
|
2002-03-18 09:55:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (optind < argc)
|
|
|
|
do_host(argv[optind++]);
|
|
|
|
|
2001-05-04 03:57:05 +00:00
|
|
|
while (ncon > 0)
|
|
|
|
conloop();
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|