2018-06-06 18:29:18 +00:00
|
|
|
/* $OpenBSD: ssh-keyscan.c,v 1.120 2018/06/06 18:29:18 markus Exp $ */
|
2000-12-05 01:15:09 +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
|
2001-06-09 01:30:39 +00:00
|
|
|
* OpenBSD project by leaving this copyright notice intact.
|
2000-12-05 01:15:09 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
2006-03-15 00:22:47 +00:00
|
|
|
|
2014-12-11 08:20:09 +00:00
|
|
|
#include <sys/types.h>
|
2002-09-12 00:43:29 +00:00
|
|
|
#include "openbsd-compat/sys-queue.h"
|
2006-03-15 00:22:47 +00:00
|
|
|
#include <sys/resource.h>
|
2006-08-05 00:57:45 +00:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
# include <sys/time.h>
|
|
|
|
#endif
|
2006-07-24 04:13:33 +00:00
|
|
|
|
2006-09-02 05:32:40 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2006-07-24 04:13:33 +00:00
|
|
|
#include <openssl/bn.h>
|
|
|
|
|
2006-07-24 04:51:00 +00:00
|
|
|
#include <netdb.h>
|
2006-07-12 12:44:34 +00:00
|
|
|
#include <errno.h>
|
2006-08-05 01:34:19 +00:00
|
|
|
#include <stdarg.h>
|
2006-08-05 01:37:59 +00:00
|
|
|
#include <stdio.h>
|
2006-08-05 01:34:19 +00:00
|
|
|
#include <stdlib.h>
|
2006-08-05 02:39:39 +00:00
|
|
|
#include <signal.h>
|
2006-07-24 04:13:33 +00:00
|
|
|
#include <string.h>
|
2006-07-24 04:01:23 +00:00
|
|
|
#include <unistd.h>
|
2000-12-05 01:15:09 +00:00
|
|
|
|
|
|
|
#include "xmalloc.h"
|
|
|
|
#include "ssh.h"
|
2015-01-19 20:32:39 +00:00
|
|
|
#include "sshbuf.h"
|
|
|
|
#include "sshkey.h"
|
2006-08-05 02:39:39 +00:00
|
|
|
#include "cipher.h"
|
2001-08-06 22:41:30 +00:00
|
|
|
#include "kex.h"
|
|
|
|
#include "compat.h"
|
|
|
|
#include "myproposal.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "dispatch.h"
|
2001-01-22 05:34:40 +00:00
|
|
|
#include "log.h"
|
2001-03-05 07:01:18 +00:00
|
|
|
#include "atomicio.h"
|
2001-08-06 22:41:30 +00:00
|
|
|
#include "misc.h"
|
2005-03-01 10:48:03 +00:00
|
|
|
#include "hostfile.h"
|
2015-01-19 20:32:39 +00:00
|
|
|
#include "ssherr.h"
|
|
|
|
#include "ssh_api.h"
|
2018-02-23 05:14:05 +00:00
|
|
|
#include "dns.h"
|
2000-12-05 01:15:09 +00:00
|
|
|
|
2001-08-06 22:41:30 +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;
|
2000-12-05 01:15:09 +00:00
|
|
|
|
2001-08-06 22:41:30 +00:00
|
|
|
int ssh_port = SSH_DEFAULT_PORT;
|
2001-09-20 20:30:09 +00:00
|
|
|
|
2017-04-30 23:18:44 +00:00
|
|
|
#define KT_DSA (1)
|
|
|
|
#define KT_RSA (1<<1)
|
|
|
|
#define KT_ECDSA (1<<2)
|
|
|
|
#define KT_ED25519 (1<<3)
|
2018-02-23 15:58:37 +00:00
|
|
|
#define KT_XMSS (1<<4)
|
2017-04-30 23:18:44 +00:00
|
|
|
|
|
|
|
#define KT_MIN KT_DSA
|
2018-02-23 15:58:37 +00:00
|
|
|
#define KT_MAX KT_XMSS
|
2001-09-20 20:30:09 +00:00
|
|
|
|
2015-11-08 22:30:20 +00:00
|
|
|
int get_cert = 0;
|
2014-04-20 02:59:46 +00:00
|
|
|
int get_keytypes = KT_RSA|KT_ECDSA|KT_ED25519;
|
2000-12-05 01:15:09 +00:00
|
|
|
|
2005-03-01 10:48:03 +00:00
|
|
|
int hash_hosts = 0; /* Hash hostname on output */
|
|
|
|
|
2018-02-23 05:14:05 +00:00
|
|
|
int print_sshfp = 0; /* Print SSHFP records instead of known_hosts */
|
|
|
|
|
2000-12-05 01:15:09 +00: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 07:01:18 +00:00
|
|
|
#define MAXCON (maxfd - 10)
|
2000-12-05 01:15:09 +00:00
|
|
|
|
2000-12-13 17:45:15 +00:00
|
|
|
extern char *__progname;
|
2001-03-05 07:04:38 +00:00
|
|
|
fd_set *read_wait;
|
2006-03-26 03:19:21 +00:00
|
|
|
size_t read_wait_nfdset;
|
2000-12-05 01:15:09 +00:00
|
|
|
int ncon;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 01:43:59 +00:00
|
|
|
u_char c_status; /* State of connection on this file desc. */
|
2000-12-05 01:15:09 +00: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. */
|
2017-04-30 23:18:44 +00:00
|
|
|
int c_keytype; /* Only one of KT_* */
|
2015-04-05 15:43:43 +00:00
|
|
|
sig_atomic_t c_done; /* SSH2 done */
|
2000-12-05 01:15:09 +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 */
|
2015-01-19 20:32:39 +00:00
|
|
|
struct ssh *c_ssh; /* SSH-connection */
|
2000-12-05 01:15:09 +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;
|
|
|
|
|
2015-01-19 20:32:39 +00:00
|
|
|
static void keyprint(con *c, struct sshkey *key);
|
|
|
|
|
2001-06-25 05:01:22 +00:00
|
|
|
static int
|
2000-12-05 01:15:09 +00:00
|
|
|
fdlim_get(int hard)
|
|
|
|
{
|
2000-12-27 07:06:21 +00:00
|
|
|
#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
|
2000-12-05 01:15:09 +00:00
|
|
|
struct rlimit rlfd;
|
2001-03-05 04:54:49 +00:00
|
|
|
|
2000-12-05 01:15:09 +00:00
|
|
|
if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
|
|
|
|
return (-1);
|
|
|
|
if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY)
|
2003-11-24 02:07:45 +00:00
|
|
|
return SSH_SYSFDMAX;
|
2000-12-05 01:15:09 +00:00
|
|
|
else
|
|
|
|
return hard ? rlfd.rlim_max : rlfd.rlim_cur;
|
2000-12-27 04:57:41 +00:00
|
|
|
#else
|
2003-11-24 02:07:45 +00:00
|
|
|
return SSH_SYSFDMAX;
|
2000-12-27 04:57:41 +00:00
|
|
|
#endif
|
2000-12-05 01:15:09 +00:00
|
|
|
}
|
|
|
|
|
2001-06-25 05:01:22 +00:00
|
|
|
static int
|
2000-12-05 01:15:09 +00:00
|
|
|
fdlim_set(int lim)
|
|
|
|
{
|
2000-12-27 07:06:21 +00:00
|
|
|
#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
|
2000-12-05 01:15:09 +00:00
|
|
|
struct rlimit rlfd;
|
2000-12-27 04:57:41 +00:00
|
|
|
#endif
|
2002-07-07 22:25:29 +00:00
|
|
|
|
2000-12-05 01:15:09 +00:00
|
|
|
if (lim <= 0)
|
|
|
|
return (-1);
|
2000-12-27 07:06:21 +00:00
|
|
|
#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
|
2000-12-05 01:15:09 +00:00
|
|
|
if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
|
|
|
|
return (-1);
|
|
|
|
rlfd.rlim_cur = lim;
|
|
|
|
if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0)
|
|
|
|
return (-1);
|
2000-12-27 04:57:41 +00:00
|
|
|
#elif defined (HAVE_SETDTABLESIZE)
|
2001-02-05 15:43:59 +00:00
|
|
|
setdtablesize(lim);
|
2000-12-27 04:57:41 +00:00
|
|
|
#endif
|
2000-12-05 01:15:09 +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.
|
|
|
|
*/
|
2001-06-25 05:01:22 +00:00
|
|
|
static char *
|
2000-12-05 01:15:09 +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.
|
|
|
|
*/
|
2001-06-25 05:01:22 +00:00
|
|
|
static char *
|
2000-12-05 01:15:09 +00:00
|
|
|
strnnsep(char **stringp, char *delim)
|
|
|
|
{
|
|
|
|
char *tok;
|
|
|
|
|
|
|
|
do {
|
|
|
|
tok = xstrsep(stringp, delim);
|
|
|
|
} while (tok && *tok == '\0');
|
|
|
|
return (tok);
|
|
|
|
}
|
|
|
|
|
2001-08-06 22:41:30 +00:00
|
|
|
|
|
|
|
static int
|
2015-01-19 20:32:39 +00:00
|
|
|
key_print_wrapper(struct sshkey *hostkey, struct ssh *ssh)
|
2001-08-06 22:41:30 +00:00
|
|
|
{
|
2015-01-19 20:32:39 +00:00
|
|
|
con *c;
|
|
|
|
|
|
|
|
if ((c = ssh_get_app_data(ssh)) != NULL)
|
|
|
|
keyprint(c, hostkey);
|
|
|
|
/* always abort key exchange */
|
|
|
|
return -1;
|
2001-08-06 22:41:30 +00: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 20:32:39 +00:00
|
|
|
static void
|
2001-08-06 22:41:30 +00:00
|
|
|
keygrab_ssh2(con *c)
|
|
|
|
{
|
2014-04-20 03:17:20 +00:00
|
|
|
char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
|
2015-01-19 20:32:39 +00:00
|
|
|
int r;
|
2001-08-06 22:41:30 +00:00
|
|
|
|
2015-11-08 22:30:20 +00: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;
|
2018-02-23 15:58:37 +00:00
|
|
|
case KT_XMSS:
|
|
|
|
myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ?
|
|
|
|
"ssh-xmss-cert-v01@openssh.com" : "ssh-xmss@openssh.com";
|
|
|
|
break;
|
2015-11-08 22:30:20 +00:00
|
|
|
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 20:32:39 +00: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-06 22:41:30 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2015-01-19 20:32:39 +00: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;
|
2016-05-02 10:26:04 +00:00
|
|
|
c->c_ssh->kex->kex[KEX_DH_GRP14_SHA256] = kexdh_client;
|
|
|
|
c->c_ssh->kex->kex[KEX_DH_GRP16_SHA512] = kexdh_client;
|
|
|
|
c->c_ssh->kex->kex[KEX_DH_GRP18_SHA512] = kexdh_client;
|
2015-01-19 20:32:39 +00:00
|
|
|
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 18:04:21 +00:00
|
|
|
# ifdef OPENSSL_HAS_ECC
|
2015-01-19 20:32:39 +00:00
|
|
|
c->c_ssh->kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
|
2015-02-22 18:04:21 +00:00
|
|
|
# endif
|
2015-01-19 20:32:39 +00: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.
|
|
|
|
*/
|
2017-05-31 07:00:13 +00:00
|
|
|
ssh_dispatch_run(c->c_ssh, DISPATCH_BLOCK, &c->c_done);
|
2001-08-06 22:41:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-03-10 03:18:24 +00:00
|
|
|
keyprint_one(const char *host, struct sshkey *key)
|
2001-08-06 22:41:30 +00:00
|
|
|
{
|
2015-10-24 22:56:19 +00:00
|
|
|
char *hostport;
|
2017-03-10 03:18:24 +00:00
|
|
|
const char *known_host, *hashed;
|
2001-08-06 22:41:30 +00:00
|
|
|
|
2018-02-23 05:14:05 +00:00
|
|
|
if (print_sshfp) {
|
|
|
|
export_dns_rr(host, key, stdout, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-10 00:08:55 +00:00
|
|
|
hostport = put_host_port(host, ssh_port);
|
2017-03-10 04:26:06 +00:00
|
|
|
lowercase(hostport);
|
2017-03-10 03:18:24 +00:00
|
|
|
if (hash_hosts && (hashed = host_hash(host, NULL, 0)) == NULL)
|
|
|
|
fatal("host_hash failed");
|
|
|
|
known_host = hash_hosts ? hashed : hostport;
|
2015-11-08 22:30:20 +00:00
|
|
|
if (!get_cert)
|
2017-03-10 03:18:24 +00:00
|
|
|
fprintf(stdout, "%s ", known_host);
|
2015-01-19 20:32:39 +00:00
|
|
|
sshkey_write(key, stdout);
|
2000-12-05 01:15:09 +00:00
|
|
|
fputs("\n", stdout);
|
2015-04-10 00:08:55 +00:00
|
|
|
free(hostport);
|
2000-12-05 01:15:09 +00:00
|
|
|
}
|
|
|
|
|
2015-10-24 22:56:19 +00: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 22:30:20 +00:00
|
|
|
if (get_cert || (!hash_hosts && ssh_port == SSH_DEFAULT_PORT)) {
|
2015-10-24 22:56:19 +00:00
|
|
|
keyprint_one(hosts, key);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ohosts = hosts = xstrdup(hosts);
|
|
|
|
while ((host = strsep(&hosts, ",")) != NULL)
|
|
|
|
keyprint_one(host, key);
|
|
|
|
free(ohosts);
|
|
|
|
}
|
|
|
|
|
2001-06-25 05:01:22 +00:00
|
|
|
static int
|
2000-12-05 01:15:09 +00:00
|
|
|
tcpconnect(char *host)
|
|
|
|
{
|
|
|
|
struct addrinfo hints, *ai, *aitop;
|
|
|
|
char strport[NI_MAXSERV];
|
|
|
|
int gaierr, s = -1;
|
|
|
|
|
2001-08-06 22:41:30 +00:00
|
|
|
snprintf(strport, sizeof strport, "%d", ssh_port);
|
2000-12-05 01:15:09 +00:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
2001-08-06 22:41:30 +00:00
|
|
|
hints.ai_family = IPv4or6;
|
2000-12-05 01:15:09 +00:00
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
2015-01-28 21:15:47 +00:00
|
|
|
if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
|
|
|
|
error("getaddrinfo %s: %s", host, ssh_gai_strerror(gaierr));
|
|
|
|
return -1;
|
|
|
|
}
|
2000-12-05 01:15:09 +00:00
|
|
|
for (ai = aitop; ai; ai = ai->ai_next) {
|
2010-01-09 23:31:12 +00:00
|
|
|
s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
|
2000-12-05 01:15:09 +00:00
|
|
|
if (s < 0) {
|
|
|
|
error("socket: %s", strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
2004-06-15 00:35:30 +00:00
|
|
|
if (set_nonblock(s) == -1)
|
|
|
|
fatal("%s: set_nonblock(%d)", __func__, s);
|
2000-12-05 01:15:09 +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;
|
|
|
|
}
|
|
|
|
|
2001-06-25 05:01:22 +00:00
|
|
|
static int
|
2001-08-06 22:41:30 +00:00
|
|
|
conalloc(char *iname, char *oname, int keytype)
|
2000-12-05 01:15:09 +00:00
|
|
|
{
|
|
|
|
char *namebase, *name, *namelist;
|
2002-07-07 22:17:22 +00:00
|
|
|
int s;
|
2000-12-05 01:15:09 +00:00
|
|
|
|
|
|
|
namebase = namelist = xstrdup(iname);
|
|
|
|
|
|
|
|
do {
|
|
|
|
name = xstrsep(&namelist, ",");
|
|
|
|
if (!name) {
|
2013-06-01 21:31:17 +00:00
|
|
|
free(namebase);
|
2000-12-05 01:15:09 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
} while ((s = tcpconnect(name)) < 0);
|
|
|
|
|
|
|
|
if (s >= maxfd)
|
2000-12-15 18:39:12 +00:00
|
|
|
fatal("conalloc: fdno %d too high", s);
|
2000-12-05 01:15:09 +00:00
|
|
|
if (fdcon[s].c_status)
|
2000-12-15 18:39:12 +00:00
|
|
|
fatal("conalloc: attempt to reuse fdno %d", s);
|
2000-12-05 01:15:09 +00:00
|
|
|
|
2015-11-08 22:30:20 +00:00
|
|
|
debug3("%s: oname %s kt %d", __func__, oname, keytype);
|
2000-12-05 01:15:09 +00: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-06 22:41:30 +00:00
|
|
|
fdcon[s].c_keytype = keytype;
|
2017-11-25 06:46:22 +00:00
|
|
|
monotime_tv(&fdcon[s].c_tv);
|
2000-12-05 01:15:09 +00:00
|
|
|
fdcon[s].c_tv.tv_sec += timeout;
|
|
|
|
TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
|
2001-03-05 07:04:38 +00:00
|
|
|
FD_SET(s, read_wait);
|
2000-12-05 01:15:09 +00:00
|
|
|
ncon++;
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
2001-06-25 05:01:22 +00:00
|
|
|
static void
|
2000-12-05 01:15:09 +00:00
|
|
|
confree(int s)
|
|
|
|
{
|
|
|
|
if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
|
2000-12-15 18:39:12 +00:00
|
|
|
fatal("confree: attempt to free bad fdno %d", s);
|
2013-06-01 21:31:17 +00:00
|
|
|
free(fdcon[s].c_namebase);
|
|
|
|
free(fdcon[s].c_output_name);
|
2000-12-05 01:15:09 +00:00
|
|
|
if (fdcon[s].c_status == CS_KEYS)
|
2013-06-01 21:31:17 +00:00
|
|
|
free(fdcon[s].c_data);
|
2000-12-05 01:15:09 +00:00
|
|
|
fdcon[s].c_status = CS_UNUSED;
|
2001-08-06 22:41:30 +00:00
|
|
|
fdcon[s].c_keytype = 0;
|
2015-01-19 20:32:39 +00:00
|
|
|
if (fdcon[s].c_ssh) {
|
|
|
|
ssh_packet_close(fdcon[s].c_ssh);
|
|
|
|
free(fdcon[s].c_ssh);
|
|
|
|
fdcon[s].c_ssh = NULL;
|
2017-06-30 04:17:23 +00:00
|
|
|
} else
|
|
|
|
close(s);
|
2000-12-05 01:15:09 +00:00
|
|
|
TAILQ_REMOVE(&tq, &fdcon[s], c_link);
|
2001-03-05 07:04:38 +00:00
|
|
|
FD_CLR(s, read_wait);
|
2000-12-05 01:15:09 +00:00
|
|
|
ncon--;
|
|
|
|
}
|
|
|
|
|
2001-06-25 05:01:22 +00:00
|
|
|
static void
|
2000-12-05 01:15:09 +00:00
|
|
|
contouch(int s)
|
|
|
|
{
|
|
|
|
TAILQ_REMOVE(&tq, &fdcon[s], c_link);
|
2017-11-25 06:46:22 +00:00
|
|
|
monotime_tv(&fdcon[s].c_tv);
|
2000-12-05 01:15:09 +00:00
|
|
|
fdcon[s].c_tv.tv_sec += timeout;
|
|
|
|
TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
|
|
|
|
}
|
|
|
|
|
2001-06-25 05:01:22 +00:00
|
|
|
static int
|
2000-12-05 01:15:09 +00:00
|
|
|
conrecycle(int s)
|
|
|
|
{
|
|
|
|
con *c = &fdcon[s];
|
2002-07-07 22:17:22 +00:00
|
|
|
int ret;
|
2000-12-05 01:15:09 +00:00
|
|
|
|
2001-08-06 22:41:30 +00:00
|
|
|
ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype);
|
2000-12-05 01:15:09 +00:00
|
|
|
confree(s);
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2001-06-25 05:01:22 +00:00
|
|
|
static void
|
2000-12-05 01:15:09 +00:00
|
|
|
congreet(int s)
|
|
|
|
{
|
2005-06-17 02:59:34 +00:00
|
|
|
int n = 0, remote_major = 0, remote_minor = 0;
|
2001-08-06 22:41:30 +00:00
|
|
|
char buf[256], *cp;
|
2001-12-21 01:45:43 +00:00
|
|
|
char remote_version[sizeof buf];
|
2005-06-17 02:59:34 +00:00
|
|
|
size_t bufsiz;
|
2000-12-05 01:15:09 +00:00
|
|
|
con *c = &fdcon[s];
|
|
|
|
|
2017-04-30 23:18:44 +00:00
|
|
|
/* send client banner */
|
|
|
|
n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n",
|
|
|
|
PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2);
|
|
|
|
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) {
|
|
|
|
error("write (%s): %s", c->c_name, strerror(errno));
|
|
|
|
confree(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-11-05 04:12:28 +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;
|
2001-08-06 22:43:50 +00:00
|
|
|
}
|
2002-03-05 01:54:52 +00:00
|
|
|
if (n == 0) {
|
2005-05-26 02:23:44 +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-05 01:54:52 +00:00
|
|
|
conrecycle(s);
|
|
|
|
return;
|
|
|
|
}
|
2001-03-06 03:33:04 +00:00
|
|
|
if (*cp != '\n' && *cp != '\r') {
|
2000-12-05 01:15:09 +00:00
|
|
|
error("%s: bad greeting", c->c_name);
|
|
|
|
confree(s);
|
|
|
|
return;
|
|
|
|
}
|
2001-03-06 03:33:04 +00:00
|
|
|
*cp = '\0';
|
2015-01-30 01:13:33 +00:00
|
|
|
if ((c->c_ssh = ssh_packet_set_connection(NULL, s, s)) == NULL)
|
|
|
|
fatal("ssh_packet_set_connection failed");
|
2015-01-30 10:44:49 +00:00
|
|
|
ssh_packet_set_timeout(c->c_ssh, timeout, 1);
|
2015-01-19 20:32:39 +00:00
|
|
|
ssh_set_app_data(c->c_ssh, c); /* back link */
|
2001-12-21 01:45:43 +00:00
|
|
|
if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
|
|
|
|
&remote_major, &remote_minor, remote_version) == 3)
|
2015-01-19 20:32:39 +00:00
|
|
|
c->c_ssh->compat = compat_datafellows(remote_version);
|
2001-12-21 01:45:43 +00:00
|
|
|
else
|
2015-01-19 20:32:39 +00:00
|
|
|
c->c_ssh->compat = 0;
|
2017-04-30 23:18:44 +00:00
|
|
|
if (!ssh2_capable(remote_major, remote_minor)) {
|
|
|
|
debug("%s doesn't support ssh2", c->c_name);
|
2001-12-21 01:45:43 +00:00
|
|
|
confree(s);
|
|
|
|
return;
|
2001-08-06 22:41:30 +00:00
|
|
|
}
|
2018-02-23 05:14:05 +00:00
|
|
|
fprintf(stderr, "%c %s:%d %s\n", print_sshfp ? ';' : '#',
|
|
|
|
c->c_name, ssh_port, chop(buf));
|
2017-04-30 23:18:44 +00:00
|
|
|
keygrab_ssh2(c);
|
|
|
|
confree(s);
|
2000-12-05 01:15:09 +00:00
|
|
|
}
|
|
|
|
|
2001-06-25 05:01:22 +00:00
|
|
|
static void
|
2000-12-05 01:15:09 +00:00
|
|
|
conread(int s)
|
|
|
|
{
|
|
|
|
con *c = &fdcon[s];
|
2005-05-26 02:23:44 +00:00
|
|
|
size_t n;
|
2000-12-05 01:15:09 +00:00
|
|
|
|
|
|
|
if (c->c_status == CS_CON) {
|
|
|
|
congreet(s);
|
|
|
|
return;
|
|
|
|
}
|
2004-08-13 11:19:37 +00:00
|
|
|
n = atomicio(read, s, c->c_data + c->c_off, c->c_len - c->c_off);
|
2005-05-26 02:23:44 +00:00
|
|
|
if (n == 0) {
|
2000-12-05 01:15:09 +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;
|
|
|
|
default:
|
2000-12-15 18:39:12 +00:00
|
|
|
fatal("conread: invalid status %d", c->c_status);
|
2000-12-05 01:15:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
contouch(s);
|
|
|
|
}
|
|
|
|
|
2001-06-25 05:01:22 +00:00
|
|
|
static void
|
2000-12-05 01:15:09 +00:00
|
|
|
conloop(void)
|
|
|
|
{
|
|
|
|
struct timeval seltime, now;
|
2002-07-07 22:17:22 +00:00
|
|
|
fd_set *r, *e;
|
2000-12-05 01:15:09 +00:00
|
|
|
con *c;
|
2002-07-07 22:17:22 +00:00
|
|
|
int i;
|
2000-12-05 01:15:09 +00:00
|
|
|
|
2017-11-25 06:46:22 +00:00
|
|
|
monotime_tv(&now);
|
2002-06-21 00:09:54 +00:00
|
|
|
c = TAILQ_FIRST(&tq);
|
2000-12-05 01:15:09 +00:00
|
|
|
|
2001-03-05 07:01:18 +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))) {
|
2000-12-05 01:15:09 +00:00
|
|
|
seltime = c->c_tv;
|
|
|
|
seltime.tv_sec -= now.tv_sec;
|
|
|
|
seltime.tv_usec -= now.tv_usec;
|
2001-02-10 23:18:11 +00:00
|
|
|
if (seltime.tv_usec < 0) {
|
2000-12-05 01:15:09 +00:00
|
|
|
seltime.tv_usec += 1000000;
|
|
|
|
seltime.tv_sec--;
|
|
|
|
}
|
|
|
|
} else
|
2011-05-05 04:05:12 +00:00
|
|
|
timerclear(&seltime);
|
2000-12-05 01:15:09 +00:00
|
|
|
|
2006-03-26 03:19:21 +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-03-05 07:04:38 +00:00
|
|
|
|
|
|
|
while (select(maxfd, r, NULL, e, &seltime) == -1 &&
|
2008-07-04 13:10:49 +00:00
|
|
|
(errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
|
2001-02-15 03:12:08 +00:00
|
|
|
;
|
|
|
|
|
2001-03-05 07:01:18 +00:00
|
|
|
for (i = 0; i < maxfd; i++) {
|
2001-03-05 07:04:38 +00:00
|
|
|
if (FD_ISSET(i, e)) {
|
2000-12-05 01:15:09 +00:00
|
|
|
error("%s: exception!", fdcon[i].c_name);
|
|
|
|
confree(i);
|
2001-03-05 07:04:38 +00:00
|
|
|
} else if (FD_ISSET(i, r))
|
2000-12-05 01:15:09 +00:00
|
|
|
conread(i);
|
2001-03-05 07:01:18 +00:00
|
|
|
}
|
2013-06-01 21:31:17 +00:00
|
|
|
free(r);
|
|
|
|
free(e);
|
2000-12-05 01:15:09 +00:00
|
|
|
|
2002-06-21 00:09:54 +00:00
|
|
|
c = TAILQ_FIRST(&tq);
|
2001-03-05 07:01:18 +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))) {
|
2000-12-05 01:15:09 +00:00
|
|
|
int s = c->c_fd;
|
2001-03-05 07:01:18 +00:00
|
|
|
|
2002-06-21 00:09:54 +00:00
|
|
|
c = TAILQ_NEXT(c, c_link);
|
2000-12-05 01:15:09 +00:00
|
|
|
conrecycle(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-08-06 22:41:30 +00:00
|
|
|
static void
|
|
|
|
do_host(char *host)
|
2000-12-05 01:15:09 +00:00
|
|
|
{
|
2001-08-06 22:41:30 +00:00
|
|
|
char *name = strnnsep(&host, " \t\n");
|
|
|
|
int j;
|
|
|
|
|
2001-12-06 16:28:19 +00:00
|
|
|
if (name == NULL)
|
|
|
|
return;
|
2017-04-30 23:18:44 +00:00
|
|
|
for (j = KT_MIN; j <= KT_MAX; j *= 2) {
|
2001-08-06 22:41:30 +00:00
|
|
|
if (get_keytypes & j) {
|
|
|
|
while (ncon >= MAXCON)
|
|
|
|
conloop();
|
|
|
|
conalloc(name, *host ? host : name, j);
|
2000-12-05 01:15:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-02-26 17:52:14 +00:00
|
|
|
void
|
|
|
|
fatal(const char *fmt,...)
|
2001-08-06 22:41:30 +00:00
|
|
|
{
|
2002-02-26 17:52:14 +00:00
|
|
|
va_list args;
|
2002-07-07 22:17:22 +00:00
|
|
|
|
2002-02-26 17:52:14 +00:00
|
|
|
va_start(args, fmt);
|
|
|
|
do_log(SYSLOG_LEVEL_FATAL, fmt, args);
|
|
|
|
va_end(args);
|
2015-01-19 20:32:39 +00:00
|
|
|
exit(255);
|
2001-08-06 22:41:30 +00:00
|
|
|
}
|
|
|
|
|
2001-06-25 05:01:22 +00:00
|
|
|
static void
|
2000-12-05 01:15:09 +00:00
|
|
|
usage(void)
|
|
|
|
{
|
2008-11-03 08:27:07 +00:00
|
|
|
fprintf(stderr,
|
2018-02-23 05:14:05 +00:00
|
|
|
"usage: %s [-46cDHv] [-f file] [-p port] [-T timeout] [-t type]\n"
|
2018-03-02 21:40:15 +00:00
|
|
|
"\t\t [host | addrlist namelist]\n",
|
2001-08-06 22:06:35 +00:00
|
|
|
__progname);
|
|
|
|
exit(1);
|
2000-12-05 01:15:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2001-08-06 22:41:30 +00:00
|
|
|
int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO;
|
2010-06-25 23:39:59 +00:00
|
|
|
int opt, fopt_count = 0, j;
|
2018-06-06 18:29:18 +00:00
|
|
|
char *tname, *cp, *line = NULL;
|
|
|
|
size_t linesize = 0;
|
2010-06-25 23:39:59 +00:00
|
|
|
FILE *fp;
|
2001-09-20 20:30:09 +00:00
|
|
|
|
2001-08-06 22:41:30 +00:00
|
|
|
extern int optind;
|
|
|
|
extern char *optarg;
|
2000-12-05 01:15:09 +00:00
|
|
|
|
2016-02-15 09:47:49 +00:00
|
|
|
ssh_malloc_init(); /* must be called before any mallocs */
|
2003-08-21 23:34:41 +00:00
|
|
|
__progname = ssh_get_progname(argv[0]);
|
2001-10-10 20:45:43 +00:00
|
|
|
seed_rng();
|
2000-12-05 01:15:09 +00:00
|
|
|
TAILQ_INIT(&tq);
|
|
|
|
|
2005-10-03 08:11:24 +00:00
|
|
|
/* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
|
|
|
|
sanitise_stdfd();
|
|
|
|
|
2001-08-06 22:41:30 +00:00
|
|
|
if (argc <= 1)
|
2000-12-05 01:15:09 +00:00
|
|
|
usage();
|
|
|
|
|
2018-02-23 05:14:05 +00:00
|
|
|
while ((opt = getopt(argc, argv, "cDHv46p:T:t:f:")) != -1) {
|
2001-08-06 22:41:30 +00:00
|
|
|
switch (opt) {
|
2005-03-01 10:48:03 +00:00
|
|
|
case 'H':
|
|
|
|
hash_hosts = 1;
|
|
|
|
break;
|
2015-11-08 22:30:20 +00:00
|
|
|
case 'c':
|
|
|
|
get_cert = 1;
|
|
|
|
break;
|
2018-02-23 05:14:05 +00:00
|
|
|
case 'D':
|
|
|
|
print_sshfp = 1;
|
|
|
|
break;
|
2001-08-06 22:41:30 +00:00
|
|
|
case 'p':
|
|
|
|
ssh_port = a2port(optarg);
|
2009-01-28 05:31:22 +00:00
|
|
|
if (ssh_port <= 0) {
|
2001-08-06 22:41:30 +00:00
|
|
|
fprintf(stderr, "Bad port '%s'\n", optarg);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'T':
|
2002-07-04 00:07:13 +00:00
|
|
|
timeout = convtime(optarg);
|
|
|
|
if (timeout == -1 || timeout == 0) {
|
|
|
|
fprintf(stderr, "Bad timeout '%s'\n", optarg);
|
2000-12-05 01:15:09 +00:00
|
|
|
usage();
|
2002-07-04 00:07:13 +00:00
|
|
|
}
|
2001-08-06 22:41:30 +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;
|
2001-09-20 20:30:09 +00:00
|
|
|
case 'f':
|
2001-08-06 22:41:30 +00: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 20:32:39 +00:00
|
|
|
int type = sshkey_type_from_name(tname);
|
2017-01-06 03:41:58 +00:00
|
|
|
|
2001-08-06 22:41:30 +00:00
|
|
|
switch (type) {
|
|
|
|
case KEY_DSA:
|
|
|
|
get_keytypes |= KT_DSA;
|
|
|
|
break;
|
2010-08-31 12:41:14 +00:00
|
|
|
case KEY_ECDSA:
|
|
|
|
get_keytypes |= KT_ECDSA;
|
|
|
|
break;
|
2001-08-06 22:41:30 +00:00
|
|
|
case KEY_RSA:
|
|
|
|
get_keytypes |= KT_RSA;
|
|
|
|
break;
|
2013-12-07 00:24:01 +00:00
|
|
|
case KEY_ED25519:
|
|
|
|
get_keytypes |= KT_ED25519;
|
|
|
|
break;
|
2018-02-23 15:58:37 +00:00
|
|
|
case KEY_XMSS:
|
|
|
|
get_keytypes |= KT_XMSS;
|
|
|
|
break;
|
2001-08-06 22:41:30 +00:00
|
|
|
case KEY_UNSPEC:
|
2017-01-06 03:41:58 +00:00
|
|
|
default:
|
|
|
|
fatal("Unknown key type \"%s\"", tname);
|
2001-08-06 22:41:30 +00:00
|
|
|
}
|
|
|
|
tname = strtok(NULL, ",");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '4':
|
|
|
|
IPv4or6 = AF_INET;
|
|
|
|
break;
|
|
|
|
case '6':
|
|
|
|
IPv4or6 = AF_INET6;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
2000-12-05 01:15:09 +00:00
|
|
|
usage();
|
2001-08-06 22:41:30 +00:00
|
|
|
}
|
2000-12-05 01:15:09 +00:00
|
|
|
}
|
2001-08-06 22:41:30 +00:00
|
|
|
if (optind == argc && !fopt_count)
|
2000-12-05 01:15:09 +00:00
|
|
|
usage();
|
|
|
|
|
2001-08-06 22:41:30 +00:00
|
|
|
log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1);
|
|
|
|
|
2000-12-05 01:15:09 +00:00
|
|
|
maxfd = fdlim_get(1);
|
|
|
|
if (maxfd < 0)
|
2000-12-15 18:39:12 +00:00
|
|
|
fatal("%s: fdlim_get: bad value", __progname);
|
2000-12-05 01:15:09 +00:00
|
|
|
if (maxfd > MAXMAXFD)
|
|
|
|
maxfd = MAXMAXFD;
|
2001-03-05 07:01:18 +00:00
|
|
|
if (MAXCON <= 0)
|
2000-12-15 18:39:12 +00:00
|
|
|
fatal("%s: not enough file descriptors", __progname);
|
2000-12-05 01:15:09 +00:00
|
|
|
if (maxfd > fdlim_get(0))
|
|
|
|
fdlim_set(maxfd);
|
2006-03-26 03:19:21 +00:00
|
|
|
fdcon = xcalloc(maxfd, sizeof(con));
|
2000-12-05 01:15:09 +00:00
|
|
|
|
2006-03-26 03:19:21 +00:00
|
|
|
read_wait_nfdset = howmany(maxfd, NFDBITS);
|
|
|
|
read_wait = xcalloc(read_wait_nfdset, sizeof(fd_mask));
|
2001-03-05 07:04:38 +00:00
|
|
|
|
2010-06-25 23:39:59 +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));
|
|
|
|
|
2018-06-06 18:29:18 +00:00
|
|
|
while (getline(&line, &linesize, fp) != -1) {
|
2010-06-25 23:39:59 +00:00
|
|
|
/* 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-06 22:41:30 +00:00
|
|
|
|
2010-06-25 23:39:59 +00:00
|
|
|
/* Skip empty lines */
|
|
|
|
if (*line == '\0')
|
2001-09-12 17:10:40 +00:00
|
|
|
continue;
|
2010-06-25 23:39:59 +00:00
|
|
|
|
|
|
|
do_host(line);
|
2000-12-05 01:15:09 +00:00
|
|
|
}
|
2010-06-25 23:39:59 +00:00
|
|
|
|
|
|
|
if (ferror(fp))
|
|
|
|
fatal("%s: %s: %s", __progname, argv[j],
|
|
|
|
strerror(errno));
|
|
|
|
|
|
|
|
fclose(fp);
|
2001-08-06 22:41:30 +00:00
|
|
|
}
|
2018-06-06 18:29:18 +00:00
|
|
|
free(line);
|
2001-08-06 22:41:30 +00:00
|
|
|
|
|
|
|
while (optind < argc)
|
|
|
|
do_host(argv[optind++]);
|
|
|
|
|
2000-12-05 01:15:09 +00:00
|
|
|
while (ncon > 0)
|
|
|
|
conloop();
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|