freebsd-dev/ssh-keyscan.c

810 lines
18 KiB
C
Raw Normal View History

2018-05-06 12:27:04 +00:00
/* $OpenBSD: ssh-keyscan.c,v 1.119 2018/03/02 21:40:15 jmc Exp $ */
/*
* 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.
*/
#include "includes.h"
2006-09-30 13:29:51 +00:00
2015-07-02 13:15:34 +00:00
#include <sys/types.h>
#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>
#include <openssl/bn.h>
2006-09-30 13:29:51 +00:00
#include <netdb.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include "xmalloc.h"
#include "ssh.h"
2015-07-02 13:15:34 +00:00
#include "sshbuf.h"
#include "sshkey.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"
#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"
2015-07-02 13:15:34 +00:00
#include "ssherr.h"
#include "ssh_api.h"
2018-05-06 12:27:04 +00:00
#include "dns.h"
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;
2002-03-18 09:55:03 +00:00
int ssh_port = SSH_DEFAULT_PORT;
2018-05-06 12:24:45 +00:00
#define KT_DSA (1)
#define KT_RSA (1<<1)
#define KT_ECDSA (1<<2)
#define KT_ED25519 (1<<3)
2018-05-06 12:27:04 +00:00
#define KT_XMSS (1<<4)
2018-05-06 12:24:45 +00:00
#define KT_MIN KT_DSA
2018-05-06 12:27:04 +00:00
#define KT_MAX KT_XMSS
2002-03-18 09:55:03 +00:00
2016-03-10 20:10:25 +00:00
int get_cert = 0;
2015-01-05 16:09:55 +00:00
int get_keytypes = KT_RSA|KT_ECDSA|KT_ED25519;
2005-06-05 15:40:50 +00:00
int hash_hosts = 0; /* Hash hostname on output */
2018-05-06 12:27:04 +00:00
int print_sshfp = 0; /* Print SSHFP records instead of known_hosts */
#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;
int ncon;
2015-07-02 13:15:34 +00:00
struct ssh *active_state = NULL; /* XXX needed for linking */
/*
* 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. */
2018-05-06 12:24:45 +00:00
int c_keytype; /* Only one of KT_* */
2015-07-02 13:18:50 +00:00
sig_atomic_t c_done; /* SSH2 done */
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-07-02 13:15:34 +00:00
struct ssh *c_ssh; /* SSH-connection */
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-07-02 13:15:34 +00:00
static void keyprint(con *c, struct sshkey *key);
2002-03-18 09:55:03 +00:00
static int
fdlim_get(int hard)
{
2002-06-27 22:31:32 +00:00
#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
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;
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
}
2002-03-18 09:55:03 +00:00
static int
fdlim_set(int lim)
{
2002-06-27 22:31:32 +00:00
#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
struct rlimit rlfd;
2002-06-27 22:31:32 +00:00
#endif
if (lim <= 0)
return (-1);
2002-06-27 22:31:32 +00:00
#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
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
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 *
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 *
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 int
2015-07-02 13:15:34 +00:00
key_print_wrapper(struct sshkey *hostkey, struct ssh *ssh)
2002-03-18 09:55:03 +00:00
{
2015-07-02 13:15:34 +00:00
con *c;
if ((c = ssh_get_app_data(ssh)) != NULL)
keyprint(c, hostkey);
/* always abort key exchange */
return -1;
2002-03-18 09:55:03 +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-07-02 13:15:34 +00:00
static void
2002-03-18 09:55:03 +00:00
keygrab_ssh2(con *c)
{
2015-01-05 16:09:55 +00:00
char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
2015-07-02 13:15:34 +00:00
int r;
2002-03-18 09:55:03 +00:00
2016-03-10 20:10:25 +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-05-06 12:27:04 +00:00
case KT_XMSS:
myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ?
"ssh-xmss-cert-v01@openssh.com" : "ssh-xmss@openssh.com";
break;
2016-03-10 20:10:25 +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-07-02 13:15:34 +00:00
if ((r = kex_setup(c->c_ssh, myproposal)) != 0) {
free(c->c_ssh);
fprintf(stderr, "kex_setup: %s\n", ssh_err(r));
2002-03-18 09:55:03 +00:00
exit(1);
}
2015-07-02 13:15:34 +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;
2017-01-31 12:29:48 +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-07-02 13:15:34 +00:00
c->c_ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
c->c_ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
# ifdef OPENSSL_HAS_ECC
c->c_ssh->kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
# endif
#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.
*/
2018-05-06 12:24:45 +00:00
ssh_dispatch_run(c->c_ssh, DISPATCH_BLOCK, &c->c_done);
2002-03-18 09:55:03 +00:00
}
static void
2017-08-03 10:10:20 +00:00
keyprint_one(const char *host, struct sshkey *key)
2002-03-18 09:55:03 +00:00
{
2016-03-10 20:10:25 +00:00
char *hostport;
2017-08-03 10:10:20 +00:00
const char *known_host, *hashed;
2002-03-18 09:55:03 +00:00
2018-05-06 12:27:04 +00:00
if (print_sshfp) {
export_dns_rr(host, key, stdout, 0);
return;
}
2015-07-02 13:18:50 +00:00
hostport = put_host_port(host, ssh_port);
2017-08-03 10:10:20 +00:00
lowercase(hostport);
if (hash_hosts && (hashed = host_hash(host, NULL, 0)) == NULL)
fatal("host_hash failed");
known_host = hash_hosts ? hashed : hostport;
2016-03-10 20:10:25 +00:00
if (!get_cert)
2017-08-03 10:10:20 +00:00
fprintf(stdout, "%s ", known_host);
2015-07-02 13:15:34 +00:00
sshkey_write(key, stdout);
fputs("\n", stdout);
2015-07-02 13:18:50 +00:00
free(hostport);
}
2016-03-10 20:10:25 +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;
if (get_cert || (!hash_hosts && ssh_port == SSH_DEFAULT_PORT)) {
keyprint_one(hosts, key);
return;
}
ohosts = hosts = xstrdup(hosts);
while ((host = strsep(&hosts, ",")) != NULL)
keyprint_one(host, key);
free(ohosts);
}
2002-03-18 09:55:03 +00:00
static int
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);
memset(&hints, 0, sizeof(hints));
2002-03-18 09:55:03 +00:00
hints.ai_family = IPv4or6;
hints.ai_socktype = SOCK_STREAM;
2015-07-02 13:15:34 +00:00
if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
error("getaddrinfo %s: %s", host, ssh_gai_strerror(gaierr));
return -1;
}
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);
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);
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)
{
char *namebase, *name, *namelist;
int s;
namebase = namelist = xstrdup(iname);
do {
name = xstrsep(&namelist, ",");
if (!name) {
2013-09-18 17:27:38 +00:00
free(namebase);
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);
2016-03-10 20:10:25 +00:00
debug3("%s: oname %s kt %d", __func__, oname, keytype);
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;
2018-05-06 12:27:04 +00:00
monotime_tv(&fdcon[s].c_tv);
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
confree(int s)
{
if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
fatal("confree: attempt to free bad fdno %d", s);
2013-09-18 17:27:38 +00:00
free(fdcon[s].c_namebase);
free(fdcon[s].c_output_name);
if (fdcon[s].c_status == CS_KEYS)
2013-09-18 17:27:38 +00:00
free(fdcon[s].c_data);
fdcon[s].c_status = CS_UNUSED;
2002-03-18 09:55:03 +00:00
fdcon[s].c_keytype = 0;
2015-07-02 13:15:34 +00:00
if (fdcon[s].c_ssh) {
ssh_packet_close(fdcon[s].c_ssh);
free(fdcon[s].c_ssh);
fdcon[s].c_ssh = NULL;
2018-05-06 12:24:45 +00:00
} else
close(s);
TAILQ_REMOVE(&tq, &fdcon[s], c_link);
FD_CLR(s, read_wait);
ncon--;
}
2002-03-18 09:55:03 +00:00
static void
contouch(int s)
{
TAILQ_REMOVE(&tq, &fdcon[s], c_link);
2018-05-06 12:27:04 +00:00
monotime_tv(&fdcon[s].c_tv);
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
conrecycle(int s)
{
con *c = &fdcon[s];
int ret;
2002-03-18 09:55:03 +00:00
ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype);
confree(s);
return (ret);
}
2002-03-18 09:55:03 +00:00
static void
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];
size_t bufsiz;
con *c = &fdcon[s];
2018-05-06 12:24:45 +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;
}
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;
}
if (*cp != '\n' && *cp != '\r') {
error("%s: bad greeting", c->c_name);
confree(s);
return;
}
*cp = '\0';
2015-07-02 13:15:34 +00:00
if ((c->c_ssh = ssh_packet_set_connection(NULL, s, s)) == NULL)
fatal("ssh_packet_set_connection failed");
ssh_packet_set_timeout(c->c_ssh, timeout, 1);
ssh_set_app_data(c->c_ssh, c); /* back link */
2002-03-18 09:55:03 +00:00
if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
&remote_major, &remote_minor, remote_version) == 3)
2015-07-02 13:15:34 +00:00
c->c_ssh->compat = compat_datafellows(remote_version);
2002-03-18 09:55:03 +00:00
else
2015-07-02 13:15:34 +00:00
c->c_ssh->compat = 0;
2018-05-06 12:24:45 +00:00
if (!ssh2_capable(remote_major, remote_minor)) {
debug("%s doesn't support ssh2", c->c_name);
2002-03-18 09:55:03 +00:00
confree(s);
return;
}
2018-05-06 12:27:04 +00:00
fprintf(stderr, "%c %s:%d %s\n", print_sshfp ? ';' : '#',
c->c_name, ssh_port, chop(buf));
2018-05-06 12:24:45 +00:00
keygrab_ssh2(c);
confree(s);
}
2002-03-18 09:55:03 +00:00
static void
conread(int s)
{
con *c = &fdcon[s];
2005-09-03 06:59:33 +00:00
size_t n;
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) {
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:
fatal("conread: invalid status %d", c->c_status);
break;
}
contouch(s);
}
2002-03-18 09:55:03 +00:00
static void
conloop(void)
{
struct timeval seltime, now;
fd_set *r, *e;
con *c;
int i;
2018-05-06 12:27:04 +00:00
monotime_tv(&now);
2002-06-23 14:01:54 +00:00
c = TAILQ_FIRST(&tq);
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);
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));
while (select(maxfd, r, NULL, e, &seltime) == -1 &&
2008-07-23 09:33:08 +00:00
(errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
;
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);
2002-06-23 14:01:54 +00:00
c = TAILQ_FIRST(&tq);
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);
conrecycle(s);
}
}
2002-03-18 09:55:03 +00:00
static void
do_host(char *host)
{
2002-03-18 09:55:03 +00:00
char *name = strnnsep(&host, " \t\n");
int j;
2002-03-18 09:55:03 +00:00
if (name == NULL)
return;
2018-05-06 12:24:45 +00:00
for (j = KT_MIN; j <= KT_MAX; j *= 2) {
2002-03-18 09:55:03 +00:00
if (get_keytypes & j) {
while (ncon >= MAXCON)
conloop();
conalloc(name, *host ? host : name, j);
}
}
}
void
2002-03-18 09:55:03 +00:00
fatal(const char *fmt,...)
{
va_list args;
2002-03-18 09:55:03 +00:00
va_start(args, fmt);
do_log(SYSLOG_LEVEL_FATAL, fmt, args);
va_end(args);
2015-07-02 13:15:34 +00:00
exit(255);
2002-03-18 09:55:03 +00:00
}
static void
usage(void)
{
2009-02-24 18:49:27 +00:00
fprintf(stderr,
2018-05-06 12:27:04 +00:00
"usage: %s [-46cDHv] [-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);
}
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;
2016-03-10 20:10:25 +00:00
ssh_malloc_init(); /* must be called before any mallocs */
2004-01-07 11:10:17 +00:00
__progname = ssh_get_progname(argv[0]);
2002-06-27 22:31:32 +00:00
seed_rng();
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)
usage();
2018-05-06 12:27:04 +00:00
while ((opt = getopt(argc, argv, "cDHv46p: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;
2016-03-10 20:10:25 +00:00
case 'c':
get_cert = 1;
break;
2018-05-06 12:27:04 +00:00
case 'D':
print_sshfp = 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':
timeout = convtime(optarg);
if (timeout == -1 || timeout == 0) {
fprintf(stderr, "Bad timeout '%s'\n", optarg);
usage();
}
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) {
2015-07-02 13:15:34 +00:00
int type = sshkey_type_from_name(tname);
2017-08-03 10:10:20 +00:00
2002-03-18 09:55:03 +00:00
switch (type) {
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;
2018-05-06 12:27:04 +00:00
case KEY_XMSS:
get_keytypes |= KT_XMSS;
break;
2002-03-18 09:55:03 +00:00
case KEY_UNSPEC:
2017-08-03 10:10:20 +00:00
default:
fatal("Unknown key type \"%s\"", tname);
2002-03-18 09:55:03 +00:00
}
tname = strtok(NULL, ",");
}
break;
case '4':
IPv4or6 = AF_INET;
break;
case '6':
IPv4or6 = AF_INET6;
break;
case '?':
default:
usage();
2002-03-18 09:55:03 +00:00
}
}
2002-03-18 09:55:03 +00:00
if (optind == argc && !fopt_count)
usage();
2002-03-18 09:55:03 +00:00
log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1);
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));
2006-09-30 13:29:51 +00:00
read_wait_nfdset = howmany(maxfd, NFDBITS);
read_wait = xcalloc(read_wait_nfdset, sizeof(fd_mask));
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);
}
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++]);
while (ncon > 0)
conloop();
return (0);
}