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"
|
2002-06-21 02:09:54 +02:00
|
|
|
RCSID("$OpenBSD: ssh-keyscan.c,v 1.36 2002/06/16 21:30:58 itojun Exp $");
|
2000-12-05 02:15:09 +01:00
|
|
|
|
2001-03-14 19:37:13 +01:00
|
|
|
#include "openbsd-compat/fake-queue.h"
|
2000-12-05 02:15:09 +01:00
|
|
|
|
|
|
|
#include <openssl/bn.h>
|
|
|
|
|
2001-08-07 00:41:30 +02:00
|
|
|
#include <setjmp.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"
|
2000-12-05 02:15:09 +01:00
|
|
|
#include "key.h"
|
2001-08-07 00:41:30 +02:00
|
|
|
#include "kex.h"
|
|
|
|
#include "compat.h"
|
|
|
|
#include "myproposal.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "dispatch.h"
|
2000-12-05 02:15:09 +01:00
|
|
|
#include "buffer.h"
|
|
|
|
#include "bufaux.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"
|
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. */
|
2001-11-14 22:40:45 +01:00
|
|
|
#ifdef IPV4_DEFAULT
|
|
|
|
int IPv4or6 = AF_INET;
|
|
|
|
#else
|
2001-08-07 00:41:30 +02:00
|
|
|
int IPv4or6 = AF_UNSPEC;
|
2001-11-14 22:40:45 +01:00
|
|
|
#endif
|
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
|
|
|
|
|
|
|
#define KT_RSA1 1
|
|
|
|
#define KT_DSA 2
|
|
|
|
#define KT_RSA 4
|
|
|
|
|
|
|
|
int get_keytypes = KT_RSA1; /* Get only RSA1 keys by default */
|
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
|
|
|
#ifdef HAVE___PROGNAME
|
|
|
|
extern char *__progname;
|
|
|
|
#else
|
|
|
|
char *__progname;
|
|
|
|
#endif
|
2001-03-05 08:04:38 +01:00
|
|
|
fd_set *read_wait;
|
|
|
|
size_t read_wait_size;
|
2000-12-05 02:15:09 +01:00
|
|
|
int ncon;
|
2001-08-07 00:41:30 +02:00
|
|
|
int nonfatal_fatal = 0;
|
|
|
|
jmp_buf kexjmp;
|
2001-09-12 20:05:05 +02:00
|
|
|
Key *kexjmp_key;
|
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 */
|
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 */
|
2001-08-07 00:41:30 +02:00
|
|
|
Kex *c_kex; /* The key-exchange struct for ssh2 */
|
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;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is just a wrapper around fgets() to make it usable.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Stress-test. Increase this later. */
|
|
|
|
#define LINEBUF_SIZE 16
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
char *buf;
|
2000-12-22 02:43:59 +01:00
|
|
|
u_int size;
|
2000-12-05 02:15:09 +01:00
|
|
|
int lineno;
|
|
|
|
const char *filename;
|
|
|
|
FILE *stream;
|
|
|
|
void (*errfun) (const char *,...);
|
|
|
|
} Linebuf;
|
|
|
|
|
2001-06-25 07:01:22 +02:00
|
|
|
static Linebuf *
|
2000-12-05 02:15:09 +01:00
|
|
|
Linebuf_alloc(const char *filename, void (*errfun) (const char *,...))
|
|
|
|
{
|
|
|
|
Linebuf *lb;
|
|
|
|
|
|
|
|
if (!(lb = malloc(sizeof(*lb)))) {
|
|
|
|
if (errfun)
|
|
|
|
(*errfun) ("linebuf (%s): malloc failed\n", lb->filename);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
if (filename) {
|
|
|
|
lb->filename = filename;
|
|
|
|
if (!(lb->stream = fopen(filename, "r"))) {
|
2001-01-18 03:04:35 +01:00
|
|
|
xfree(lb);
|
2000-12-05 02:15:09 +01:00
|
|
|
if (errfun)
|
|
|
|
(*errfun) ("%s: %s\n", filename, strerror(errno));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lb->filename = "(stdin)";
|
|
|
|
lb->stream = stdin;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(lb->buf = malloc(lb->size = LINEBUF_SIZE))) {
|
|
|
|
if (errfun)
|
|
|
|
(*errfun) ("linebuf (%s): malloc failed\n", lb->filename);
|
2001-01-18 03:04:35 +01:00
|
|
|
xfree(lb);
|
2000-12-05 02:15:09 +01:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
lb->errfun = errfun;
|
|
|
|
lb->lineno = 0;
|
|
|
|
return (lb);
|
|
|
|
}
|
|
|
|
|
2001-06-25 07:01:22 +02:00
|
|
|
static void
|
2000-12-05 02:15:09 +01:00
|
|
|
Linebuf_free(Linebuf * lb)
|
|
|
|
{
|
|
|
|
fclose(lb->stream);
|
2001-01-18 03:04:35 +01:00
|
|
|
xfree(lb->buf);
|
|
|
|
xfree(lb);
|
2000-12-05 02:15:09 +01:00
|
|
|
}
|
|
|
|
|
2001-06-25 07:01:22 +02:00
|
|
|
#if 0
|
|
|
|
static void
|
2000-12-05 02:15:09 +01:00
|
|
|
Linebuf_restart(Linebuf * lb)
|
|
|
|
{
|
|
|
|
clearerr(lb->stream);
|
|
|
|
rewind(lb->stream);
|
|
|
|
lb->lineno = 0;
|
|
|
|
}
|
|
|
|
|
2001-06-25 07:01:22 +02:00
|
|
|
static int
|
2000-12-05 02:15:09 +01:00
|
|
|
Linebuf_lineno(Linebuf * lb)
|
|
|
|
{
|
|
|
|
return (lb->lineno);
|
|
|
|
}
|
2001-06-25 07:01:22 +02:00
|
|
|
#endif
|
2000-12-05 02:15:09 +01:00
|
|
|
|
2001-06-25 07:01:22 +02:00
|
|
|
static char *
|
2001-02-11 00:18:11 +01:00
|
|
|
Linebuf_getline(Linebuf * lb)
|
2000-12-05 02:15:09 +01:00
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
lb->lineno++;
|
|
|
|
for (;;) {
|
|
|
|
/* Read a line */
|
|
|
|
if (!fgets(&lb->buf[n], lb->size - n, lb->stream)) {
|
|
|
|
if (ferror(lb->stream) && lb->errfun)
|
2001-03-05 05:54:49 +01:00
|
|
|
(*lb->errfun) ("%s: %s\n", lb->filename,
|
|
|
|
strerror(errno));
|
2000-12-05 02:15:09 +01:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
n = strlen(lb->buf);
|
|
|
|
|
|
|
|
/* Return it or an error if it fits */
|
|
|
|
if (n > 0 && lb->buf[n - 1] == '\n') {
|
|
|
|
lb->buf[n - 1] = '\0';
|
|
|
|
return (lb->buf);
|
|
|
|
}
|
|
|
|
if (n != lb->size - 1) {
|
|
|
|
if (lb->errfun)
|
2001-03-05 05:54:49 +01:00
|
|
|
(*lb->errfun) ("%s: skipping incomplete last line\n",
|
|
|
|
lb->filename);
|
2000-12-05 02:15:09 +01:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
/* Double the buffer if we need more space */
|
|
|
|
if (!(lb->buf = realloc(lb->buf, (lb->size *= 2)))) {
|
|
|
|
if (lb->errfun)
|
2001-03-05 05:54:49 +01:00
|
|
|
(*lb->errfun) ("linebuf (%s): realloc failed\n",
|
|
|
|
lb->filename);
|
2000-12-05 02:15:09 +01:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
return 10000;
|
|
|
|
else
|
|
|
|
return hard ? rlfd.rlim_max : rlfd.rlim_cur;
|
2000-12-27 05:57:41 +01:00
|
|
|
#elif defined (HAVE_SYSCONF)
|
|
|
|
return sysconf (_SC_OPEN_MAX);
|
|
|
|
#else
|
|
|
|
return 10000;
|
|
|
|
#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
|
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);
|
|
|
|
}
|
|
|
|
|
2001-08-07 00:41:30 +02:00
|
|
|
static Key *
|
|
|
|
keygrab_ssh1(con *c)
|
2000-12-05 02:15:09 +01:00
|
|
|
{
|
|
|
|
static Key *rsa;
|
|
|
|
static Buffer msg;
|
|
|
|
|
|
|
|
if (rsa == NULL) {
|
|
|
|
buffer_init(&msg);
|
|
|
|
rsa = key_new(KEY_RSA1);
|
|
|
|
}
|
2001-08-07 00:41:30 +02:00
|
|
|
buffer_append(&msg, c->c_data, c->c_plen);
|
|
|
|
buffer_consume(&msg, 8 - (c->c_plen & 7)); /* padding */
|
2000-12-05 02:15:09 +01:00
|
|
|
if (buffer_get_char(&msg) != (int) SSH_SMSG_PUBLIC_KEY) {
|
2001-08-07 00:41:30 +02:00
|
|
|
error("%s: invalid packet type", c->c_name);
|
2000-12-05 02:15:09 +01:00
|
|
|
buffer_clear(&msg);
|
2001-08-07 00:41:30 +02:00
|
|
|
return NULL;
|
2000-12-05 02:15:09 +01: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);
|
2001-08-07 00:41:30 +02:00
|
|
|
|
2000-12-05 02:15:09 +01:00
|
|
|
buffer_clear(&msg);
|
|
|
|
|
2001-08-07 00:41:30 +02:00
|
|
|
return (rsa);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
hostjump(Key *hostkey)
|
|
|
|
{
|
2001-09-12 20:05:05 +02:00
|
|
|
kexjmp_key = hostkey;
|
|
|
|
longjmp(kexjmp, 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Key *
|
|
|
|
keygrab_ssh2(con *c)
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
|
|
|
|
packet_set_connection(c->c_fd, c->c_fd);
|
|
|
|
enable_compat20();
|
|
|
|
myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = c->c_keytype == KT_DSA?
|
|
|
|
"ssh-dss": "ssh-rsa";
|
|
|
|
c->c_kex = kex_setup(myproposal);
|
|
|
|
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;
|
|
|
|
xfree(c->c_kex);
|
|
|
|
c->c_kex = NULL;
|
|
|
|
packet_close();
|
|
|
|
|
2001-09-12 20:05:05 +02:00
|
|
|
return j < 0? NULL : kexjmp_key;
|
2001-08-07 00:41:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
keyprint(con *c, Key *key)
|
|
|
|
{
|
|
|
|
if (!key)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fprintf(stdout, "%s ", c->c_output_name ? c->c_output_name : c->c_name);
|
|
|
|
key_write(key, stdout);
|
2000-12-05 02:15:09 +01:00
|
|
|
fputs("\n", stdout);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
|
|
|
|
fatal("getaddrinfo %s: %s", host, gai_strerror(gaierr));
|
|
|
|
for (ai = aitop; ai; ai = ai->ai_next) {
|
|
|
|
s = socket(ai->ai_family, SOCK_STREAM, 0);
|
|
|
|
if (s < 0) {
|
|
|
|
error("socket: %s", strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
2001-01-09 01:35:42 +01:00
|
|
|
if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
|
2000-12-05 02:15:09 +01:00
|
|
|
fatal("F_SETFL: %s", strerror(errno));
|
|
|
|
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
|
|
|
{
|
|
|
|
int s;
|
|
|
|
char *namebase, *name, *namelist;
|
|
|
|
|
|
|
|
namebase = namelist = xstrdup(iname);
|
|
|
|
|
|
|
|
do {
|
|
|
|
name = xstrsep(&namelist, ",");
|
|
|
|
if (!name) {
|
2001-01-18 03:04:35 +01:00
|
|
|
xfree(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
|
|
|
|
|
|
|
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);
|
2001-01-18 03:04:35 +01:00
|
|
|
xfree(fdcon[s].c_namebase);
|
|
|
|
xfree(fdcon[s].c_output_name);
|
2000-12-05 02:15:09 +01:00
|
|
|
if (fdcon[s].c_status == CS_KEYS)
|
2001-01-18 03:04:35 +01:00
|
|
|
xfree(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;
|
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)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
con *c = &fdcon[s];
|
|
|
|
|
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)
|
|
|
|
{
|
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];
|
2001-03-06 04:33:04 +01:00
|
|
|
size_t bufsiz;
|
2001-12-21 02:45:43 +01:00
|
|
|
int remote_major, remote_minor, n = 0;
|
2000-12-05 02:15:09 +01:00
|
|
|
con *c = &fdcon[s];
|
|
|
|
|
2001-03-06 04:33:04 +01:00
|
|
|
bufsiz = sizeof(buf);
|
|
|
|
cp = buf;
|
2001-08-07 00:43:50 +02:00
|
|
|
while (bufsiz-- && (n = read(s, cp, 1)) == 1 && *cp != '\n') {
|
|
|
|
if (*cp == '\r')
|
|
|
|
*cp = '\n';
|
2001-03-06 04:33:04 +01:00
|
|
|
cp++;
|
2001-08-07 00:43:50 +02:00
|
|
|
}
|
2000-12-05 02:15:09 +01:00
|
|
|
if (n < 0) {
|
|
|
|
if (errno != ECONNREFUSED)
|
|
|
|
error("read (%s): %s", c->c_name, strerror(errno));
|
|
|
|
conrecycle(s);
|
|
|
|
return;
|
|
|
|
}
|
2002-03-05 02:54:52 +01:00
|
|
|
if (n == 0) {
|
|
|
|
error("%s: Connection closed by remote host", c->c_name);
|
|
|
|
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';
|
2001-12-21 02:45:43 +01:00
|
|
|
if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
|
|
|
|
&remote_major, &remote_minor, remote_version) == 3)
|
|
|
|
compat_datafellows(remote_version);
|
|
|
|
else
|
|
|
|
datafellows = 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
|
|
|
}
|
2001-08-07 00:43:50 +02:00
|
|
|
fprintf(stderr, "# %s %s\n", c->c_name, 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);
|
2001-03-05 08:01:18 +01:00
|
|
|
if (atomicio(write, s, buf, n) != 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) {
|
|
|
|
keyprint(c, keygrab_ssh2(c));
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
con *c = &fdcon[s];
|
|
|
|
|
|
|
|
if (c->c_status == CS_CON) {
|
|
|
|
congreet(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
n = read(s, c->c_data + c->c_off, c->c_len - c->c_off);
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
break;
|
|
|
|
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)
|
|
|
|
{
|
2001-03-05 08:04:38 +01:00
|
|
|
fd_set *r, *e;
|
2000-12-05 02:15:09 +01:00
|
|
|
struct timeval seltime, now;
|
|
|
|
int i;
|
|
|
|
con *c;
|
|
|
|
|
|
|
|
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
|
|
|
|
seltime.tv_sec = seltime.tv_usec = 0;
|
|
|
|
|
2001-03-05 08:04:38 +01:00
|
|
|
r = xmalloc(read_wait_size);
|
|
|
|
memcpy(r, read_wait, read_wait_size);
|
|
|
|
e = xmalloc(read_wait_size);
|
|
|
|
memcpy(e, read_wait, read_wait_size);
|
|
|
|
|
|
|
|
while (select(maxfd, r, NULL, e, &seltime) == -1 &&
|
2001-02-15 04:12:08 +01:00
|
|
|
(errno == EAGAIN || errno == EINTR))
|
|
|
|
;
|
|
|
|
|
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
|
|
|
}
|
2001-03-05 08:04:38 +01:00
|
|
|
xfree(r);
|
|
|
|
xfree(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;
|
2001-08-07 00:41:30 +02:00
|
|
|
for (j = KT_RSA1; j <= KT_RSA; j *= 2) {
|
|
|
|
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;
|
|
|
|
va_start(args, fmt);
|
|
|
|
do_log(SYSLOG_LEVEL_FATAL, fmt, args);
|
|
|
|
va_end(args);
|
2001-08-07 00:41:30 +02:00
|
|
|
if (nonfatal_fatal)
|
|
|
|
longjmp(kexjmp, -1);
|
2002-02-26 18:52:14 +01:00
|
|
|
else
|
|
|
|
fatal_cleanup();
|
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)
|
|
|
|
{
|
2001-08-07 00:41:30 +02:00
|
|
|
fprintf(stderr, "Usage: %s [options] host ...\n",
|
2001-08-07 00:06:35 +02:00
|
|
|
__progname);
|
|
|
|
fprintf(stderr, "Options:\n");
|
|
|
|
fprintf(stderr, " -f file Read hosts or addresses from file.\n");
|
2001-08-07 00:41:30 +02:00
|
|
|
fprintf(stderr, " -p port Connect to the specified port.\n");
|
|
|
|
fprintf(stderr, " -t keytype Specify the host key type.\n");
|
|
|
|
fprintf(stderr, " -T timeout Set connection timeout.\n");
|
2002-02-26 18:52:14 +01:00
|
|
|
fprintf(stderr, " -v Verbose; display verbose debugging messages.\n");
|
|
|
|
fprintf(stderr, " -4 Use IPv4 only.\n");
|
|
|
|
fprintf(stderr, " -6 Use IPv6 only.\n");
|
2001-08-07 00:06:35 +02:00
|
|
|
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;
|
|
|
|
int opt, fopt_count = 0;
|
|
|
|
char *tname;
|
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
|
|
|
|
2000-12-13 18:45:15 +01:00
|
|
|
__progname = get_progname(argv[0]);
|
2001-10-10 22:45:43 +02:00
|
|
|
init_rng();
|
|
|
|
seed_rng();
|
2000-12-05 02:15:09 +01:00
|
|
|
TAILQ_INIT(&tq);
|
|
|
|
|
2001-08-07 00:41:30 +02:00
|
|
|
if (argc <= 1)
|
2000-12-05 02:15:09 +01:00
|
|
|
usage();
|
|
|
|
|
2001-08-07 00:41:30 +02:00
|
|
|
while ((opt = getopt(argc, argv, "v46p:T:t:f:")) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 'p':
|
|
|
|
ssh_port = a2port(optarg);
|
|
|
|
if (ssh_port == 0) {
|
|
|
|
fprintf(stderr, "Bad port '%s'\n", optarg);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
timeout = atoi(optarg);
|
|
|
|
if (timeout <= 0)
|
2000-12-05 02:15:09 +01:00
|
|
|
usage();
|
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) {
|
|
|
|
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;
|
|
|
|
case KEY_RSA:
|
|
|
|
get_keytypes |= KT_RSA;
|
|
|
|
break;
|
|
|
|
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);
|
|
|
|
fdcon = xmalloc(maxfd * sizeof(con));
|
2001-02-11 00:18:11 +01:00
|
|
|
memset(fdcon, 0, maxfd * sizeof(con));
|
2000-12-05 02:15:09 +01:00
|
|
|
|
2001-03-05 08:04:38 +01:00
|
|
|
read_wait_size = howmany(maxfd, NFDBITS) * sizeof(fd_mask);
|
|
|
|
read_wait = xmalloc(read_wait_size);
|
|
|
|
memset(read_wait, 0, read_wait_size);
|
|
|
|
|
2001-08-07 00:41:30 +02:00
|
|
|
if (fopt_count) {
|
|
|
|
Linebuf *lb;
|
|
|
|
char *line;
|
|
|
|
int j;
|
|
|
|
|
|
|
|
for (j = 0; j < fopt_count; j++) {
|
|
|
|
lb = Linebuf_alloc(argv[j], error);
|
2001-09-12 19:10:40 +02:00
|
|
|
if (!lb)
|
|
|
|
continue;
|
2001-08-07 00:41:30 +02:00
|
|
|
while ((line = Linebuf_getline(lb)) != NULL)
|
|
|
|
do_host(line);
|
|
|
|
Linebuf_free(lb);
|
2000-12-05 02:15:09 +01:00
|
|
|
}
|
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);
|
|
|
|
}
|