2015-11-09 00:24:03 +01:00
|
|
|
/* $OpenBSD: ssh-keyscan.c,v 1.104 2015/11/08 23:24:03 jmc Exp $ */
|
2000-12-05 02:15:09 +01: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
|
2001-06-09 03:30:39 +02:00
|
|
|
* OpenBSD project by leaving this copyright notice intact.
|
2000-12-05 02:15:09 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
2006-03-15 01:22:47 +01:00
|
|
|
|
2014-12-11 09:20:09 +01:00
|
|
|
#include <sys/types.h>
|
2002-09-12 02:43:29 +02:00
|
|
|
#include "openbsd-compat/sys-queue.h"
|
2006-03-15 01:22:47 +01:00
|
|
|
#include <sys/resource.h>
|
2006-08-05 02:57:45 +02:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
# include <sys/time.h>
|
|
|
|
#endif
|
2006-07-24 06:13:33 +02:00
|
|
|
|
2006-09-02 07:32:40 +02:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2006-07-24 06:13:33 +02:00
|
|
|
#include <openssl/bn.h>
|
|
|
|
|
2006-07-24 06:51:00 +02:00
|
|
|
#include <netdb.h>
|
2006-07-12 14:44:34 +02:00
|
|
|
#include <errno.h>
|
2006-08-05 03:34:19 +02:00
|
|
|
#include <stdarg.h>
|
2006-08-05 03:37:59 +02:00
|
|
|
#include <stdio.h>
|
2006-08-05 03:34:19 +02:00
|
|
|
#include <stdlib.h>
|
2006-08-05 04:39:39 +02:00
|
|
|
#include <signal.h>
|
2006-07-24 06:13:33 +02:00
|
|
|
#include <string.h>
|
2006-07-24 06:01:23 +02:00
|
|
|
#include <unistd.h>
|
2000-12-05 02:15:09 +01:00
|
|
|
|
|
|
|
#include "xmalloc.h"
|
|
|
|
#include "ssh.h"
|
2001-01-22 06:34:40 +01:00
|
|
|
#include "ssh1.h"
|
2015-01-19 21:32:39 +01:00
|
|
|
#include "sshbuf.h"
|
|
|
|
#include "sshkey.h"
|
2006-08-05 04:39:39 +02:00
|
|
|
#include "cipher.h"
|
2001-08-07 00:41:30 +02:00
|
|
|
#include "kex.h"
|
|
|
|
#include "compat.h"
|
|
|
|
#include "myproposal.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "dispatch.h"
|
2001-01-22 06:34:40 +01:00
|
|
|
#include "log.h"
|
2001-03-05 08:01:18 +01:00
|
|
|
#include "atomicio.h"
|
2001-08-07 00:41:30 +02:00
|
|
|
#include "misc.h"
|
2005-03-01 11:48:03 +01:00
|
|
|
#include "hostfile.h"
|
2015-01-19 21:32:39 +01:00
|
|
|
#include "ssherr.h"
|
|
|
|
#include "ssh_api.h"
|
2000-12-05 02:15:09 +01:00
|
|
|
|
2001-08-07 00:41:30 +02: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;
|
2000-12-05 02:15:09 +01:00
|
|
|
|
2001-08-07 00:41:30 +02:00
|
|
|
int ssh_port = SSH_DEFAULT_PORT;
|
2001-09-20 22:30:09 +02:00
|
|
|
|
2010-08-31 14:41:14 +02:00
|
|
|
#define KT_RSA1 1
|
|
|
|
#define KT_DSA 2
|
|
|
|
#define KT_RSA 4
|
|
|
|
#define KT_ECDSA 8
|
2013-12-07 01:24:01 +01:00
|
|
|
#define KT_ED25519 16
|
2001-09-20 22:30:09 +02:00
|
|
|
|
2015-11-08 23:30:20 +01:00
|
|
|
int get_cert = 0;
|
2014-04-20 04:59:46 +02:00
|
|
|
int get_keytypes = KT_RSA|KT_ECDSA|KT_ED25519;
|
2000-12-05 02:15:09 +01:00
|
|
|
|
2005-03-01 11:48:03 +01:00
|
|
|
int hash_hosts = 0; /* Hash hostname on output */
|
|
|
|
|
2000-12-05 02:15:09 +01:00
|
|
|
#define MAXMAXFD 256
|
|
|
|
|
|
|
|
/* The number of seconds after which to give up on a TCP connection */
|
|
|
|
int timeout = 5;
|
|
|
|
|
|
|
|
int maxfd;
|
2001-03-05 08:01:18 +01:00
|
|
|
#define MAXCON (maxfd - 10)
|
2000-12-05 02:15:09 +01:00
|
|
|
|
2000-12-13 18:45:15 +01:00
|
|
|
extern char *__progname;
|
2001-03-05 08:04:38 +01:00
|
|
|
fd_set *read_wait;
|
2006-03-26 05:19:21 +02:00
|
|
|
size_t read_wait_nfdset;
|
2000-12-05 02:15:09 +01:00
|
|
|
int ncon;
|
|
|
|
|
2015-01-27 13:06:23 +01:00
|
|
|
struct ssh *active_state = NULL; /* XXX needed for linking */
|
|
|
|
|
2000-12-05 02:15:09 +01:00
|
|
|
/*
|
|
|
|
* Keep a connection structure for each file descriptor. The state
|
|
|
|
* associated with file descriptor n is held in fdcon[n].
|
|
|
|
*/
|
|
|
|
typedef struct Connection {
|
2000-12-22 02:43:59 +01:00
|
|
|
u_char c_status; /* State of connection on this file desc. */
|
2000-12-05 02:15:09 +01:00
|
|
|
#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. */
|
2001-08-07 00:41:30 +02:00
|
|
|
int c_keytype; /* Only one of KT_RSA1, KT_DSA, or KT_RSA */
|
2015-04-05 17:43:43 +02:00
|
|
|
sig_atomic_t c_done; /* SSH2 done */
|
2000-12-05 02:15:09 +01: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 */
|
2015-01-19 21:32:39 +01:00
|
|
|
struct ssh *c_ssh; /* SSH-connection */
|
2000-12-05 02:15:09 +01: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;
|
|
|
|
|
2015-01-19 21:32:39 +01:00
|
|
|
static void keyprint(con *c, struct sshkey *key);
|
|
|
|
|
2001-06-25 07:01:22 +02:00
|
|
|
static int
|
2000-12-05 02:15:09 +01:00
|
|
|
fdlim_get(int hard)
|
|
|
|
{
|
2000-12-27 08:06:21 +01:00
|
|
|
#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
|
2000-12-05 02:15:09 +01:00
|
|
|
struct rlimit rlfd;
|
2001-03-05 05:54:49 +01:00
|
|
|
|
2000-12-05 02:15:09 +01:00
|
|
|
if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
|
|
|
|
return (-1);
|
|
|
|
if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY)
|
2003-11-24 03:07:45 +01:00
|
|
|
return SSH_SYSFDMAX;
|
2000-12-05 02:15:09 +01:00
|
|
|
else
|
|
|
|
return hard ? rlfd.rlim_max : rlfd.rlim_cur;
|
2000-12-27 05:57:41 +01:00
|
|
|
#else
|
2003-11-24 03:07:45 +01:00
|
|
|
return SSH_SYSFDMAX;
|
2000-12-27 05:57:41 +01:00
|
|
|
#endif
|
2000-12-05 02:15:09 +01:00
|
|
|
}
|
|
|
|
|
2001-06-25 07:01:22 +02:00
|
|
|
static int
|
2000-12-05 02:15:09 +01:00
|
|
|
fdlim_set(int lim)
|
|
|
|
{
|
2000-12-27 08:06:21 +01:00
|
|
|
#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
|
2000-12-05 02:15:09 +01:00
|
|
|
struct rlimit rlfd;
|
2000-12-27 05:57:41 +01:00
|
|
|
#endif
|
2002-07-08 00:25:29 +02:00
|
|
|
|
2000-12-05 02:15:09 +01:00
|
|
|
if (lim <= 0)
|
|
|
|
return (-1);
|
2000-12-27 08:06:21 +01:00
|
|
|
#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
|
2000-12-05 02:15:09 +01:00
|
|
|
if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
|
|
|
|
return (-1);
|
|
|
|
rlfd.rlim_cur = lim;
|
|
|
|
if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0)
|
|
|
|
return (-1);
|
2000-12-27 05:57:41 +01:00
|
|
|
#elif defined (HAVE_SETDTABLESIZE)
|
2001-02-05 16:43:59 +01:00
|
|
|
setdtablesize(lim);
|
2000-12-27 05:57:41 +01:00
|
|
|
#endif
|
2000-12-05 02:15:09 +01: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.
|
|
|
|
*/
|
2001-06-25 07:01:22 +02:00
|
|
|
static char *
|
2000-12-05 02:15:09 +01: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.
|
|
|
|
*/
|
2001-06-25 07:01:22 +02:00
|
|
|
static char *
|
2000-12-05 02:15:09 +01:00
|
|
|
strnnsep(char **stringp, char *delim)
|
|
|
|
{
|
|
|
|
char *tok;
|
|
|
|
|
|
|
|
do {
|
|
|
|
tok = xstrsep(stringp, delim);
|
|
|
|
} while (tok && *tok == '\0');
|
|
|
|
return (tok);
|
|
|
|
}
|
|
|
|
|
2014-05-15 06:24:09 +02:00
|
|
|
#ifdef WITH_SSH1
|
2015-01-19 21:32:39 +01:00
|
|
|
static struct sshkey *
|
2001-08-07 00:41:30 +02:00
|
|
|
keygrab_ssh1(con *c)
|
2000-12-05 02:15:09 +01:00
|
|
|
{
|
2015-01-19 21:32:39 +01:00
|
|
|
static struct sshkey *rsa;
|
|
|
|
static struct sshbuf *msg;
|
|
|
|
int r;
|
|
|
|
u_char type;
|
2000-12-05 02:15:09 +01:00
|
|
|
|
|
|
|
if (rsa == NULL) {
|
2015-01-19 21:32:39 +01:00
|
|
|
if ((rsa = sshkey_new(KEY_RSA1)) == NULL) {
|
|
|
|
error("%s: sshkey_new failed", __func__);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ((msg = sshbuf_new()) == NULL)
|
|
|
|
fatal("%s: sshbuf_new failed", __func__);
|
2000-12-05 02:15:09 +01:00
|
|
|
}
|
2015-01-19 21:32:39 +01:00
|
|
|
if ((r = sshbuf_put(msg, c->c_data, c->c_plen)) != 0 ||
|
|
|
|
(r = sshbuf_consume(msg, 8 - (c->c_plen & 7))) != 0 || /* padding */
|
|
|
|
(r = sshbuf_get_u8(msg, &type)) != 0)
|
|
|
|
goto buf_err;
|
|
|
|
if (type != (int) SSH_SMSG_PUBLIC_KEY) {
|
2001-08-07 00:41:30 +02:00
|
|
|
error("%s: invalid packet type", c->c_name);
|
2015-01-19 21:32:39 +01:00
|
|
|
sshbuf_reset(msg);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_consume(msg, 8)) != 0 || /* cookie */
|
|
|
|
/* server key */
|
|
|
|
(r = sshbuf_get_u32(msg, NULL)) != 0 ||
|
|
|
|
(r = sshbuf_get_bignum1(msg, NULL)) != 0 ||
|
|
|
|
(r = sshbuf_get_bignum1(msg, NULL)) != 0 ||
|
|
|
|
/* host key */
|
|
|
|
(r = sshbuf_get_u32(msg, NULL)) != 0 ||
|
|
|
|
(r = sshbuf_get_bignum1(msg, rsa->rsa->e)) != 0 ||
|
|
|
|
(r = sshbuf_get_bignum1(msg, rsa->rsa->n)) != 0) {
|
|
|
|
buf_err:
|
|
|
|
error("%s: buffer error: %s", __func__, ssh_err(r));
|
|
|
|
sshbuf_reset(msg);
|
2001-08-07 00:41:30 +02:00
|
|
|
return NULL;
|
2000-12-05 02:15:09 +01:00
|
|
|
}
|
2001-08-07 00:41:30 +02:00
|
|
|
|
2015-01-19 21:32:39 +01:00
|
|
|
sshbuf_reset(msg);
|
2000-12-05 02:15:09 +01:00
|
|
|
|
2001-08-07 00:41:30 +02:00
|
|
|
return (rsa);
|
|
|
|
}
|
2014-05-15 06:24:09 +02:00
|
|
|
#endif
|
2001-08-07 00:41:30 +02:00
|
|
|
|
|
|
|
static int
|
2015-01-19 21:32:39 +01:00
|
|
|
key_print_wrapper(struct sshkey *hostkey, struct ssh *ssh)
|
2001-08-07 00:41:30 +02:00
|
|
|
{
|
2015-01-19 21:32:39 +01:00
|
|
|
con *c;
|
|
|
|
|
|
|
|
if ((c = ssh_get_app_data(ssh)) != NULL)
|
|
|
|
keyprint(c, hostkey);
|
|
|
|
/* always abort key exchange */
|
|
|
|
return -1;
|
2001-08-07 00:41:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-01-19 21:32:39 +01:00
|
|
|
static void
|
2001-08-07 00:41:30 +02:00
|
|
|
keygrab_ssh2(con *c)
|
|
|
|
{
|
2014-04-20 05:17:20 +02:00
|
|
|
char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
|
2015-01-19 21:32:39 +01:00
|
|
|
int r;
|
2001-08-07 00:41:30 +02:00
|
|
|
|
|
|
|
enable_compat20();
|
2015-11-08 23:30:20 +01:00
|
|
|
switch (c->c_keytype) {
|
|
|
|
case KT_DSA:
|
|
|
|
myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ?
|
|
|
|
"ssh-dss-cert-v01@openssh.com" : "ssh-dss";
|
|
|
|
break;
|
|
|
|
case KT_RSA:
|
|
|
|
myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ?
|
|
|
|
"ssh-rsa-cert-v01@openssh.com" : "ssh-rsa";
|
|
|
|
break;
|
|
|
|
case KT_ED25519:
|
|
|
|
myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ?
|
|
|
|
"ssh-ed25519-cert-v01@openssh.com" : "ssh-ed25519";
|
|
|
|
break;
|
|
|
|
case KT_ECDSA:
|
|
|
|
myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ?
|
|
|
|
"ecdsa-sha2-nistp256-cert-v01@openssh.com,"
|
|
|
|
"ecdsa-sha2-nistp384-cert-v01@openssh.com,"
|
|
|
|
"ecdsa-sha2-nistp521-cert-v01@openssh.com" :
|
|
|
|
"ecdsa-sha2-nistp256,"
|
|
|
|
"ecdsa-sha2-nistp384,"
|
|
|
|
"ecdsa-sha2-nistp521";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fatal("unknown key type %d", c->c_keytype);
|
|
|
|
break;
|
|
|
|
}
|
2015-01-19 21:32:39 +01:00
|
|
|
if ((r = kex_setup(c->c_ssh, myproposal)) != 0) {
|
|
|
|
free(c->c_ssh);
|
|
|
|
fprintf(stderr, "kex_setup: %s\n", ssh_err(r));
|
2001-08-07 00:41:30 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
2015-01-19 21:32:39 +01:00
|
|
|
#ifdef WITH_OPENSSL
|
|
|
|
c->c_ssh->kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
|
|
|
|
c->c_ssh->kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
|
|
|
|
c->c_ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
|
|
|
|
c->c_ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
|
2015-02-22 19:04:21 +01:00
|
|
|
# ifdef OPENSSL_HAS_ECC
|
2015-01-19 21:32:39 +01:00
|
|
|
c->c_ssh->kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
|
2015-02-22 19:04:21 +01:00
|
|
|
# endif
|
2015-01-19 21:32:39 +01:00
|
|
|
#endif
|
|
|
|
c->c_ssh->kex->kex[KEX_C25519_SHA256] = kexc25519_client;
|
|
|
|
ssh_set_verify_host_key_callback(c->c_ssh, key_print_wrapper);
|
|
|
|
/*
|
|
|
|
* do the key-exchange until an error occurs or until
|
|
|
|
* the key_print_wrapper() callback sets c_done.
|
|
|
|
*/
|
|
|
|
ssh_dispatch_run(c->c_ssh, DISPATCH_BLOCK, &c->c_done, c->c_ssh);
|
2001-08-07 00:41:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-10-25 00:56:19 +02:00
|
|
|
keyprint_one(char *host, struct sshkey *key)
|
2001-08-07 00:41:30 +02:00
|
|
|
{
|
2015-10-25 00:56:19 +02:00
|
|
|
char *hostport;
|
2005-03-01 11:48:03 +01:00
|
|
|
|
|
|
|
if (hash_hosts && (host = host_hash(host, NULL, 0)) == NULL)
|
|
|
|
fatal("host_hash failed");
|
2001-08-07 00:41:30 +02:00
|
|
|
|
2015-04-10 02:08:55 +02:00
|
|
|
hostport = put_host_port(host, ssh_port);
|
2015-11-08 23:30:20 +01:00
|
|
|
if (!get_cert)
|
|
|
|
fprintf(stdout, "%s ", hostport);
|
2015-01-19 21:32:39 +01:00
|
|
|
sshkey_write(key, stdout);
|
2000-12-05 02:15:09 +01:00
|
|
|
fputs("\n", stdout);
|
2015-04-10 02:08:55 +02:00
|
|
|
free(hostport);
|
2000-12-05 02:15:09 +01:00
|
|
|
}
|
|
|
|
|
2015-10-25 00:56:19 +02:00
|
|
|
static void
|
|
|
|
keyprint(con *c, struct sshkey *key)
|
|
|
|
{
|
|
|
|
char *hosts = c->c_output_name ? c->c_output_name : c->c_name;
|
|
|
|
char *host, *ohosts;
|
|
|
|
|
|
|
|
if (key == NULL)
|
|
|
|
return;
|
2015-11-08 23:30:20 +01:00
|
|
|
if (get_cert || (!hash_hosts && ssh_port == SSH_DEFAULT_PORT)) {
|
2015-10-25 00:56:19 +02:00
|
|
|
keyprint_one(hosts, key);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ohosts = hosts = xstrdup(hosts);
|
|
|
|
while ((host = strsep(&hosts, ",")) != NULL)
|
|
|
|
keyprint_one(host, key);
|
|
|
|
free(ohosts);
|
|
|
|
}
|
|
|
|
|
2001-06-25 07:01:22 +02:00
|
|
|
static int
|
2000-12-05 02:15:09 +01:00
|
|
|
tcpconnect(char *host)
|
|
|
|
{
|
|
|
|
struct addrinfo hints, *ai, *aitop;
|
|
|
|
char strport[NI_MAXSERV];
|
|
|
|
int gaierr, s = -1;
|
|
|
|
|
2001-08-07 00:41:30 +02:00
|
|
|
snprintf(strport, sizeof strport, "%d", ssh_port);
|
2000-12-05 02:15:09 +01:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
2001-08-07 00:41:30 +02:00
|
|
|
hints.ai_family = IPv4or6;
|
2000-12-05 02:15:09 +01:00
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
2015-01-28 22:15:47 +01:00
|
|
|
if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
|
|
|
|
error("getaddrinfo %s: %s", host, ssh_gai_strerror(gaierr));
|
|
|
|
return -1;
|
|
|
|
}
|
2000-12-05 02:15:09 +01:00
|
|
|
for (ai = aitop; ai; ai = ai->ai_next) {
|
2010-01-10 00:31:12 +01:00
|
|
|
s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
|
2000-12-05 02:15:09 +01:00
|
|
|
if (s < 0) {
|
|
|
|
error("socket: %s", strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
2004-06-15 02:35:30 +02:00
|
|
|
if (set_nonblock(s) == -1)
|
|
|
|
fatal("%s: set_nonblock(%d)", __func__, s);
|
2000-12-05 02:15:09 +01: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;
|
|
|
|
}
|
|
|
|
|
2001-06-25 07:01:22 +02:00
|
|
|
static int
|
2001-08-07 00:41:30 +02:00
|
|
|
conalloc(char *iname, char *oname, int keytype)
|
2000-12-05 02:15:09 +01:00
|
|
|
{
|
|
|
|
char *namebase, *name, *namelist;
|
2002-07-08 00:17:22 +02:00
|
|
|
int s;
|
2000-12-05 02:15:09 +01:00
|
|
|
|
|
|
|
namebase = namelist = xstrdup(iname);
|
|
|
|
|
|
|
|
do {
|
|
|
|
name = xstrsep(&namelist, ",");
|
|
|
|
if (!name) {
|
2013-06-01 23:31:17 +02:00
|
|
|
free(namebase);
|
2000-12-05 02:15:09 +01:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
} while ((s = tcpconnect(name)) < 0);
|
|
|
|
|
|
|
|
if (s >= maxfd)
|
2000-12-15 19:39:12 +01:00
|
|
|
fatal("conalloc: fdno %d too high", s);
|
2000-12-05 02:15:09 +01:00
|
|
|
if (fdcon[s].c_status)
|
2000-12-15 19:39:12 +01:00
|
|
|
fatal("conalloc: attempt to reuse fdno %d", s);
|
2000-12-05 02:15:09 +01:00
|
|
|
|
2015-11-08 23:30:20 +01:00
|
|
|
debug3("%s: oname %s kt %d", __func__, oname, keytype);
|
2000-12-05 02:15:09 +01:00
|
|
|
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;
|
2001-08-07 00:41:30 +02:00
|
|
|
fdcon[s].c_keytype = keytype;
|
2000-12-05 02:15:09 +01:00
|
|
|
gettimeofday(&fdcon[s].c_tv, NULL);
|
|
|
|
fdcon[s].c_tv.tv_sec += timeout;
|
|
|
|
TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
|
2001-03-05 08:04:38 +01:00
|
|
|
FD_SET(s, read_wait);
|
2000-12-05 02:15:09 +01:00
|
|
|
ncon++;
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
2001-06-25 07:01:22 +02:00
|
|
|
static void
|
2000-12-05 02:15:09 +01:00
|
|
|
confree(int s)
|
|
|
|
{
|
|
|
|
if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
|
2000-12-15 19:39:12 +01:00
|
|
|
fatal("confree: attempt to free bad fdno %d", s);
|
2001-03-05 08:01:18 +01:00
|
|
|
close(s);
|
2013-06-01 23:31:17 +02:00
|
|
|
free(fdcon[s].c_namebase);
|
|
|
|
free(fdcon[s].c_output_name);
|
2000-12-05 02:15:09 +01:00
|
|
|
if (fdcon[s].c_status == CS_KEYS)
|
2013-06-01 23:31:17 +02:00
|
|
|
free(fdcon[s].c_data);
|
2000-12-05 02:15:09 +01:00
|
|
|
fdcon[s].c_status = CS_UNUSED;
|
2001-08-07 00:41:30 +02:00
|
|
|
fdcon[s].c_keytype = 0;
|
2015-01-19 21:32:39 +01:00
|
|
|
if (fdcon[s].c_ssh) {
|
|
|
|
ssh_packet_close(fdcon[s].c_ssh);
|
|
|
|
free(fdcon[s].c_ssh);
|
|
|
|
fdcon[s].c_ssh = NULL;
|
|
|
|
}
|
2000-12-05 02:15:09 +01:00
|
|
|
TAILQ_REMOVE(&tq, &fdcon[s], c_link);
|
2001-03-05 08:04:38 +01:00
|
|
|
FD_CLR(s, read_wait);
|
2000-12-05 02:15:09 +01:00
|
|
|
ncon--;
|
|
|
|
}
|
|
|
|
|
2001-06-25 07:01:22 +02:00
|
|
|
static void
|
2000-12-05 02:15:09 +01: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);
|
|
|
|
}
|
|
|
|
|
2001-06-25 07:01:22 +02:00
|
|
|
static int
|
2000-12-05 02:15:09 +01:00
|
|
|
conrecycle(int s)
|
|
|
|
{
|
|
|
|
con *c = &fdcon[s];
|
2002-07-08 00:17:22 +02:00
|
|
|
int ret;
|
2000-12-05 02:15:09 +01:00
|
|
|
|
2001-08-07 00:41:30 +02:00
|
|
|
ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype);
|
2000-12-05 02:15:09 +01:00
|
|
|
confree(s);
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2001-06-25 07:01:22 +02:00
|
|
|
static void
|
2000-12-05 02:15:09 +01:00
|
|
|
congreet(int s)
|
|
|
|
{
|
2005-06-17 04:59:34 +02:00
|
|
|
int n = 0, remote_major = 0, remote_minor = 0;
|
2001-08-07 00:41:30 +02:00
|
|
|
char buf[256], *cp;
|
2001-12-21 02:45:43 +01:00
|
|
|
char remote_version[sizeof buf];
|
2005-06-17 04:59:34 +02:00
|
|
|
size_t bufsiz;
|
2000-12-05 02:15:09 +01:00
|
|
|
con *c = &fdcon[s];
|
|
|
|
|
2005-11-05 05:12:28 +01: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;
|
2001-08-07 00:43:50 +02:00
|
|
|
}
|
2002-03-05 02:54:52 +01:00
|
|
|
if (n == 0) {
|
2005-05-26 04:23:44 +02: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-05 02:54:52 +01:00
|
|
|
conrecycle(s);
|
|
|
|
return;
|
|
|
|
}
|
2001-03-06 04:33:04 +01:00
|
|
|
if (*cp != '\n' && *cp != '\r') {
|
2000-12-05 02:15:09 +01:00
|
|
|
error("%s: bad greeting", c->c_name);
|
|
|
|
confree(s);
|
|
|
|
return;
|
|
|
|
}
|
2001-03-06 04:33:04 +01:00
|
|
|
*cp = '\0';
|
2015-01-30 02:13:33 +01:00
|
|
|
if ((c->c_ssh = ssh_packet_set_connection(NULL, s, s)) == NULL)
|
|
|
|
fatal("ssh_packet_set_connection failed");
|
2015-01-30 11:44:49 +01:00
|
|
|
ssh_packet_set_timeout(c->c_ssh, timeout, 1);
|
2015-01-19 21:32:39 +01:00
|
|
|
ssh_set_app_data(c->c_ssh, c); /* back link */
|
2001-12-21 02:45:43 +01:00
|
|
|
if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
|
|
|
|
&remote_major, &remote_minor, remote_version) == 3)
|
2015-01-19 21:32:39 +01:00
|
|
|
c->c_ssh->compat = compat_datafellows(remote_version);
|
2001-12-21 02:45:43 +01:00
|
|
|
else
|
2015-01-19 21:32:39 +01:00
|
|
|
c->c_ssh->compat = 0;
|
2001-08-07 00:41:30 +02:00
|
|
|
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;
|
|
|
|
}
|
2001-12-21 02:45:43 +01:00
|
|
|
} else if (remote_major != 1) {
|
|
|
|
debug("%s doesn't support ssh1", c->c_name);
|
|
|
|
confree(s);
|
|
|
|
return;
|
2001-08-07 00:41:30 +02:00
|
|
|
}
|
2015-04-10 02:08:55 +02:00
|
|
|
fprintf(stderr, "# %s:%d %s\n", c->c_name, ssh_port, chop(buf));
|
2001-08-07 00:41:30 +02:00
|
|
|
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-06-17 04:59:34 +02:00
|
|
|
if (n < 0 || (size_t)n >= sizeof(buf)) {
|
2005-05-26 04:07:32 +02:00
|
|
|
error("snprintf: buffer too small");
|
|
|
|
confree(s);
|
|
|
|
return;
|
|
|
|
}
|
2005-06-17 04:59:34 +02:00
|
|
|
if (atomicio(vwrite, s, buf, n) != (size_t)n) {
|
2000-12-05 02:15:09 +01:00
|
|
|
error("write (%s): %s", c->c_name, strerror(errno));
|
|
|
|
confree(s);
|
|
|
|
return;
|
|
|
|
}
|
2001-08-07 00:41:30 +02:00
|
|
|
if (c->c_keytype != KT_RSA1) {
|
2015-01-19 21:32:39 +01:00
|
|
|
keygrab_ssh2(c);
|
2001-08-07 00:41:30 +02:00
|
|
|
confree(s);
|
|
|
|
return;
|
|
|
|
}
|
2000-12-05 02:15:09 +01:00
|
|
|
c->c_status = CS_SIZE;
|
|
|
|
contouch(s);
|
|
|
|
}
|
|
|
|
|
2001-06-25 07:01:22 +02:00
|
|
|
static void
|
2000-12-05 02:15:09 +01:00
|
|
|
conread(int s)
|
|
|
|
{
|
|
|
|
con *c = &fdcon[s];
|
2005-05-26 04:23:44 +02:00
|
|
|
size_t n;
|
2000-12-05 02:15:09 +01:00
|
|
|
|
|
|
|
if (c->c_status == CS_CON) {
|
|
|
|
congreet(s);
|
|
|
|
return;
|
|
|
|
}
|
2004-08-13 13:19:37 +02:00
|
|
|
n = atomicio(read, s, c->c_data + c->c_off, c->c_len - c->c_off);
|
2005-05-26 04:23:44 +02:00
|
|
|
if (n == 0) {
|
2000-12-05 02:15:09 +01: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;
|
2014-05-15 06:24:09 +02:00
|
|
|
#ifdef WITH_SSH1
|
2000-12-05 02:15:09 +01:00
|
|
|
case CS_KEYS:
|
2001-08-07 00:41:30 +02:00
|
|
|
keyprint(c, keygrab_ssh1(c));
|
2000-12-05 02:15:09 +01:00
|
|
|
confree(s);
|
|
|
|
return;
|
2014-05-15 06:24:09 +02:00
|
|
|
#endif
|
2000-12-05 02:15:09 +01:00
|
|
|
default:
|
2000-12-15 19:39:12 +01:00
|
|
|
fatal("conread: invalid status %d", c->c_status);
|
2000-12-05 02:15:09 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
contouch(s);
|
|
|
|
}
|
|
|
|
|
2001-06-25 07:01:22 +02:00
|
|
|
static void
|
2000-12-05 02:15:09 +01:00
|
|
|
conloop(void)
|
|
|
|
{
|
|
|
|
struct timeval seltime, now;
|
2002-07-08 00:17:22 +02:00
|
|
|
fd_set *r, *e;
|
2000-12-05 02:15:09 +01:00
|
|
|
con *c;
|
2002-07-08 00:17:22 +02:00
|
|
|
int i;
|
2000-12-05 02:15:09 +01:00
|
|
|
|
|
|
|
gettimeofday(&now, NULL);
|
2002-06-21 02:09:54 +02:00
|
|
|
c = TAILQ_FIRST(&tq);
|
2000-12-05 02:15:09 +01:00
|
|
|
|
2001-03-05 08:01:18 +01: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))) {
|
2000-12-05 02:15:09 +01:00
|
|
|
seltime = c->c_tv;
|
|
|
|
seltime.tv_sec -= now.tv_sec;
|
|
|
|
seltime.tv_usec -= now.tv_usec;
|
2001-02-11 00:18:11 +01:00
|
|
|
if (seltime.tv_usec < 0) {
|
2000-12-05 02:15:09 +01:00
|
|
|
seltime.tv_usec += 1000000;
|
|
|
|
seltime.tv_sec--;
|
|
|
|
}
|
|
|
|
} else
|
2011-05-05 06:05:12 +02:00
|
|
|
timerclear(&seltime);
|
2000-12-05 02:15:09 +01:00
|
|
|
|
2006-03-26 05:19:21 +02: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-03-05 08:04:38 +01:00
|
|
|
|
|
|
|
while (select(maxfd, r, NULL, e, &seltime) == -1 &&
|
2008-07-04 15:10:49 +02:00
|
|
|
(errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
|
2001-02-15 04:12:08 +01:00
|
|
|
;
|
|
|
|
|
2001-03-05 08:01:18 +01:00
|
|
|
for (i = 0; i < maxfd; i++) {
|
2001-03-05 08:04:38 +01:00
|
|
|
if (FD_ISSET(i, e)) {
|
2000-12-05 02:15:09 +01:00
|
|
|
error("%s: exception!", fdcon[i].c_name);
|
|
|
|
confree(i);
|
2001-03-05 08:04:38 +01:00
|
|
|
} else if (FD_ISSET(i, r))
|
2000-12-05 02:15:09 +01:00
|
|
|
conread(i);
|
2001-03-05 08:01:18 +01:00
|
|
|
}
|
2013-06-01 23:31:17 +02:00
|
|
|
free(r);
|
|
|
|
free(e);
|
2000-12-05 02:15:09 +01:00
|
|
|
|
2002-06-21 02:09:54 +02:00
|
|
|
c = TAILQ_FIRST(&tq);
|
2001-03-05 08:01:18 +01: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))) {
|
2000-12-05 02:15:09 +01:00
|
|
|
int s = c->c_fd;
|
2001-03-05 08:01:18 +01:00
|
|
|
|
2002-06-21 02:09:54 +02:00
|
|
|
c = TAILQ_NEXT(c, c_link);
|
2000-12-05 02:15:09 +01:00
|
|
|
conrecycle(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-08-07 00:41:30 +02:00
|
|
|
static void
|
|
|
|
do_host(char *host)
|
2000-12-05 02:15:09 +01:00
|
|
|
{
|
2001-08-07 00:41:30 +02:00
|
|
|
char *name = strnnsep(&host, " \t\n");
|
|
|
|
int j;
|
|
|
|
|
2001-12-06 17:28:19 +01:00
|
|
|
if (name == NULL)
|
|
|
|
return;
|
2013-12-07 01:24:01 +01:00
|
|
|
for (j = KT_RSA1; j <= KT_ED25519; j *= 2) {
|
2001-08-07 00:41:30 +02:00
|
|
|
if (get_keytypes & j) {
|
|
|
|
while (ncon >= MAXCON)
|
|
|
|
conloop();
|
|
|
|
conalloc(name, *host ? host : name, j);
|
2000-12-05 02:15:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-02-26 18:52:14 +01:00
|
|
|
void
|
|
|
|
fatal(const char *fmt,...)
|
2001-08-07 00:41:30 +02:00
|
|
|
{
|
2002-02-26 18:52:14 +01:00
|
|
|
va_list args;
|
2002-07-08 00:17:22 +02:00
|
|
|
|
2002-02-26 18:52:14 +01:00
|
|
|
va_start(args, fmt);
|
|
|
|
do_log(SYSLOG_LEVEL_FATAL, fmt, args);
|
|
|
|
va_end(args);
|
2015-01-19 21:32:39 +01:00
|
|
|
exit(255);
|
2001-08-07 00:41:30 +02:00
|
|
|
}
|
|
|
|
|
2001-06-25 07:01:22 +02:00
|
|
|
static void
|
2000-12-05 02:15:09 +01:00
|
|
|
usage(void)
|
|
|
|
{
|
2008-11-03 09:27:07 +01:00
|
|
|
fprintf(stderr,
|
2015-11-09 00:24:03 +01:00
|
|
|
"usage: %s [-46cHv] [-f file] [-p port] [-T timeout] [-t type]\n"
|
2010-01-10 00:31:12 +01:00
|
|
|
"\t\t [host | addrlist namelist] ...\n",
|
2001-08-07 00:06:35 +02:00
|
|
|
__progname);
|
|
|
|
exit(1);
|
2000-12-05 02:15:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2001-08-07 00:41:30 +02:00
|
|
|
int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO;
|
2010-06-26 01:39:59 +02:00
|
|
|
int opt, fopt_count = 0, j;
|
|
|
|
char *tname, *cp, line[NI_MAXHOST];
|
|
|
|
FILE *fp;
|
|
|
|
u_long linenum;
|
2001-09-20 22:30:09 +02:00
|
|
|
|
2001-08-07 00:41:30 +02:00
|
|
|
extern int optind;
|
|
|
|
extern char *optarg;
|
2000-12-05 02:15:09 +01:00
|
|
|
|
2003-08-22 01:34:41 +02:00
|
|
|
__progname = ssh_get_progname(argv[0]);
|
2001-10-10 22:45:43 +02:00
|
|
|
seed_rng();
|
2000-12-05 02:15:09 +01:00
|
|
|
TAILQ_INIT(&tq);
|
|
|
|
|
2005-10-03 10:11:24 +02:00
|
|
|
/* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
|
|
|
|
sanitise_stdfd();
|
|
|
|
|
2001-08-07 00:41:30 +02:00
|
|
|
if (argc <= 1)
|
2000-12-05 02:15:09 +01:00
|
|
|
usage();
|
|
|
|
|
2015-11-08 23:30:20 +01:00
|
|
|
while ((opt = getopt(argc, argv, "cHv46p:T:t:f:")) != -1) {
|
2001-08-07 00:41:30 +02:00
|
|
|
switch (opt) {
|
2005-03-01 11:48:03 +01:00
|
|
|
case 'H':
|
|
|
|
hash_hosts = 1;
|
|
|
|
break;
|
2015-11-08 23:30:20 +01:00
|
|
|
case 'c':
|
|
|
|
get_cert = 1;
|
|
|
|
break;
|
2001-08-07 00:41:30 +02:00
|
|
|
case 'p':
|
|
|
|
ssh_port = a2port(optarg);
|
2009-01-28 06:31:22 +01:00
|
|
|
if (ssh_port <= 0) {
|
2001-08-07 00:41:30 +02:00
|
|
|
fprintf(stderr, "Bad port '%s'\n", optarg);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'T':
|
2002-07-04 02:07:13 +02:00
|
|
|
timeout = convtime(optarg);
|
|
|
|
if (timeout == -1 || timeout == 0) {
|
|
|
|
fprintf(stderr, "Bad timeout '%s'\n", optarg);
|
2000-12-05 02:15:09 +01:00
|
|
|
usage();
|
2002-07-04 02:07:13 +02:00
|
|
|
}
|
2001-08-07 00:41:30 +02: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;
|
2001-09-20 22:30:09 +02:00
|
|
|
case 'f':
|
2001-08-07 00:41:30 +02:00
|
|
|
if (strcmp(optarg, "-") == 0)
|
|
|
|
optarg = NULL;
|
|
|
|
argv[fopt_count++] = optarg;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
get_keytypes = 0;
|
|
|
|
tname = strtok(optarg, ",");
|
|
|
|
while (tname) {
|
2015-01-19 21:32:39 +01:00
|
|
|
int type = sshkey_type_from_name(tname);
|
2001-08-07 00:41:30 +02:00
|
|
|
switch (type) {
|
|
|
|
case KEY_RSA1:
|
|
|
|
get_keytypes |= KT_RSA1;
|
|
|
|
break;
|
|
|
|
case KEY_DSA:
|
|
|
|
get_keytypes |= KT_DSA;
|
|
|
|
break;
|
2010-08-31 14:41:14 +02:00
|
|
|
case KEY_ECDSA:
|
|
|
|
get_keytypes |= KT_ECDSA;
|
|
|
|
break;
|
2001-08-07 00:41:30 +02:00
|
|
|
case KEY_RSA:
|
|
|
|
get_keytypes |= KT_RSA;
|
|
|
|
break;
|
2013-12-07 01:24:01 +01:00
|
|
|
case KEY_ED25519:
|
|
|
|
get_keytypes |= KT_ED25519;
|
|
|
|
break;
|
2001-08-07 00:41:30 +02:00
|
|
|
case KEY_UNSPEC:
|
2001-12-06 17:45:10 +01:00
|
|
|
fatal("unknown key type %s", tname);
|
2001-08-07 00:41:30 +02:00
|
|
|
}
|
|
|
|
tname = strtok(NULL, ",");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '4':
|
|
|
|
IPv4or6 = AF_INET;
|
|
|
|
break;
|
|
|
|
case '6':
|
|
|
|
IPv4or6 = AF_INET6;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
2000-12-05 02:15:09 +01:00
|
|
|
usage();
|
2001-08-07 00:41:30 +02:00
|
|
|
}
|
2000-12-05 02:15:09 +01:00
|
|
|
}
|
2001-08-07 00:41:30 +02:00
|
|
|
if (optind == argc && !fopt_count)
|
2000-12-05 02:15:09 +01:00
|
|
|
usage();
|
|
|
|
|
2001-08-07 00:41:30 +02:00
|
|
|
log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1);
|
|
|
|
|
2000-12-05 02:15:09 +01:00
|
|
|
maxfd = fdlim_get(1);
|
|
|
|
if (maxfd < 0)
|
2000-12-15 19:39:12 +01:00
|
|
|
fatal("%s: fdlim_get: bad value", __progname);
|
2000-12-05 02:15:09 +01:00
|
|
|
if (maxfd > MAXMAXFD)
|
|
|
|
maxfd = MAXMAXFD;
|
2001-03-05 08:01:18 +01:00
|
|
|
if (MAXCON <= 0)
|
2000-12-15 19:39:12 +01:00
|
|
|
fatal("%s: not enough file descriptors", __progname);
|
2000-12-05 02:15:09 +01:00
|
|
|
if (maxfd > fdlim_get(0))
|
|
|
|
fdlim_set(maxfd);
|
2006-03-26 05:19:21 +02:00
|
|
|
fdcon = xcalloc(maxfd, sizeof(con));
|
2000-12-05 02:15:09 +01:00
|
|
|
|
2006-03-26 05:19:21 +02:00
|
|
|
read_wait_nfdset = howmany(maxfd, NFDBITS);
|
|
|
|
read_wait = xcalloc(read_wait_nfdset, sizeof(fd_mask));
|
2001-03-05 08:04:38 +01:00
|
|
|
|
2010-06-26 01:39:59 +02: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;
|
|
|
|
}
|
2001-08-07 00:41:30 +02:00
|
|
|
|
2010-06-26 01:39:59 +02:00
|
|
|
/* Skip empty lines */
|
|
|
|
if (*line == '\0')
|
2001-09-12 19:10:40 +02:00
|
|
|
continue;
|
2010-06-26 01:39:59 +02:00
|
|
|
|
|
|
|
do_host(line);
|
2000-12-05 02:15:09 +01:00
|
|
|
}
|
2010-06-26 01:39:59 +02:00
|
|
|
|
|
|
|
if (ferror(fp))
|
|
|
|
fatal("%s: %s: %s", __progname, argv[j],
|
|
|
|
strerror(errno));
|
|
|
|
|
|
|
|
fclose(fp);
|
2001-08-07 00:41:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while (optind < argc)
|
|
|
|
do_host(argv[optind++]);
|
|
|
|
|
2000-12-05 02:15:09 +01:00
|
|
|
while (ncon > 0)
|
|
|
|
conloop();
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|