xref: /onnv-gate/usr/src/cmd/ssh/ssh-keyscan/ssh-keyscan.c (revision 8658:9772d2c4d0b9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * Copyright 1995, 1996 by David Mazieres <dm@lcs.mit.edu>.
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * Modification and redistribution in source and binary forms is
50Sstevel@tonic-gate  * permitted provided that due credit is given to the author and the
60Sstevel@tonic-gate  * OpenBSD project by leaving this copyright notice intact.
70Sstevel@tonic-gate  */
80Sstevel@tonic-gate 
97574SJan.Pechanec@Sun.COM /*
10*8658SJan.Pechanec@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
117574SJan.Pechanec@Sun.COM  * Use is subject to license terms.
127574SJan.Pechanec@Sun.COM  */
137574SJan.Pechanec@Sun.COM 
140Sstevel@tonic-gate #include "includes.h"
150Sstevel@tonic-gate RCSID("$OpenBSD: ssh-keyscan.c,v 1.40 2002/07/06 17:47:58 stevesk Exp $");
160Sstevel@tonic-gate 
170Sstevel@tonic-gate #include "sys-queue.h"
180Sstevel@tonic-gate 
190Sstevel@tonic-gate #include <openssl/bn.h>
200Sstevel@tonic-gate 
210Sstevel@tonic-gate #include <setjmp.h>
220Sstevel@tonic-gate #include "xmalloc.h"
230Sstevel@tonic-gate #include "ssh.h"
240Sstevel@tonic-gate #include "ssh1.h"
250Sstevel@tonic-gate #include "key.h"
260Sstevel@tonic-gate #include "kex.h"
270Sstevel@tonic-gate #include "compat.h"
280Sstevel@tonic-gate #include "myproposal.h"
290Sstevel@tonic-gate #include "packet.h"
300Sstevel@tonic-gate #include "dispatch.h"
310Sstevel@tonic-gate #include "buffer.h"
320Sstevel@tonic-gate #include "bufaux.h"
330Sstevel@tonic-gate #include "log.h"
340Sstevel@tonic-gate #include "atomicio.h"
350Sstevel@tonic-gate #include "misc.h"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /* Flag indicating whether IPv4 or IPv6.  This can be set on the command line.
380Sstevel@tonic-gate    Default value is AF_UNSPEC means both IPv4 and IPv6. */
390Sstevel@tonic-gate #ifdef IPV4_DEFAULT
400Sstevel@tonic-gate int IPv4or6 = AF_INET;
410Sstevel@tonic-gate #else
420Sstevel@tonic-gate int IPv4or6 = AF_UNSPEC;
430Sstevel@tonic-gate #endif
440Sstevel@tonic-gate 
450Sstevel@tonic-gate int ssh_port = SSH_DEFAULT_PORT;
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #define KT_RSA1	1
480Sstevel@tonic-gate #define KT_DSA	2
490Sstevel@tonic-gate #define KT_RSA	4
500Sstevel@tonic-gate 
510Sstevel@tonic-gate int get_keytypes = KT_RSA1;	/* Get only RSA1 keys by default */
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #define MAXMAXFD 256
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /* The number of seconds after which to give up on a TCP connection */
560Sstevel@tonic-gate int timeout = 5;
570Sstevel@tonic-gate 
580Sstevel@tonic-gate int maxfd;
590Sstevel@tonic-gate #define MAXCON (maxfd - 10)
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #ifdef HAVE___PROGNAME
620Sstevel@tonic-gate extern char *__progname;
630Sstevel@tonic-gate #else
640Sstevel@tonic-gate char *__progname;
650Sstevel@tonic-gate #endif
660Sstevel@tonic-gate fd_set *read_wait;
670Sstevel@tonic-gate size_t read_wait_size;
680Sstevel@tonic-gate int ncon;
690Sstevel@tonic-gate int nonfatal_fatal = 0;
700Sstevel@tonic-gate jmp_buf kexjmp;
710Sstevel@tonic-gate Key *kexjmp_key;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  * Keep a connection structure for each file descriptor.  The state
750Sstevel@tonic-gate  * associated with file descriptor n is held in fdcon[n].
760Sstevel@tonic-gate  */
770Sstevel@tonic-gate typedef struct Connection {
780Sstevel@tonic-gate 	u_char c_status;	/* State of connection on this file desc. */
790Sstevel@tonic-gate #define CS_UNUSED 0		/* File descriptor unused */
800Sstevel@tonic-gate #define CS_CON 1		/* Waiting to connect/read greeting */
810Sstevel@tonic-gate #define CS_SIZE 2		/* Waiting to read initial packet size */
820Sstevel@tonic-gate #define CS_KEYS 3		/* Waiting to read public key packet */
830Sstevel@tonic-gate 	int c_fd;		/* Quick lookup: c->c_fd == c - fdcon */
840Sstevel@tonic-gate 	int c_plen;		/* Packet length field for ssh packet */
850Sstevel@tonic-gate 	int c_len;		/* Total bytes which must be read. */
860Sstevel@tonic-gate 	int c_off;		/* Length of data read so far. */
870Sstevel@tonic-gate 	int c_keytype;		/* Only one of KT_RSA1, KT_DSA, or KT_RSA */
880Sstevel@tonic-gate 	char *c_namebase;	/* Address to free for c_name and c_namelist */
890Sstevel@tonic-gate 	char *c_name;		/* Hostname of connection for errors */
900Sstevel@tonic-gate 	char *c_namelist;	/* Pointer to other possible addresses */
910Sstevel@tonic-gate 	char *c_output_name;	/* Hostname of connection for output */
920Sstevel@tonic-gate 	char *c_data;		/* Data read from this fd */
930Sstevel@tonic-gate 	Kex *c_kex;		/* The key-exchange struct for ssh2 */
940Sstevel@tonic-gate 	struct timeval c_tv;	/* Time at which connection gets aborted */
950Sstevel@tonic-gate 	TAILQ_ENTRY(Connection) c_link;	/* List of connections in timeout order. */
960Sstevel@tonic-gate } con;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate TAILQ_HEAD(conlist, Connection) tq;	/* Timeout Queue */
990Sstevel@tonic-gate con *fdcon;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate /*
1020Sstevel@tonic-gate  *  This is just a wrapper around fgets() to make it usable.
1030Sstevel@tonic-gate  */
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /* Stress-test.  Increase this later. */
1060Sstevel@tonic-gate #define LINEBUF_SIZE 16
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate typedef struct {
1090Sstevel@tonic-gate 	char *buf;
1100Sstevel@tonic-gate 	u_int size;
1110Sstevel@tonic-gate 	int lineno;
1120Sstevel@tonic-gate 	const char *filename;
1130Sstevel@tonic-gate 	FILE *stream;
1140Sstevel@tonic-gate 	void (*errfun) (const char *,...);
1150Sstevel@tonic-gate } Linebuf;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate static Linebuf *
Linebuf_alloc(const char * filename,void (* errfun)(const char *,...))1180Sstevel@tonic-gate Linebuf_alloc(const char *filename, void (*errfun) (const char *,...))
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate 	Linebuf *lb;
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	if (!(lb = malloc(sizeof(*lb)))) {
1230Sstevel@tonic-gate 		if (errfun)
1240Sstevel@tonic-gate 			(*errfun) ("linebuf (%s): malloc failed\n",
1250Sstevel@tonic-gate 			    filename ? filename : "(stdin)");
1260Sstevel@tonic-gate 		return (NULL);
1270Sstevel@tonic-gate 	}
1280Sstevel@tonic-gate 	if (filename) {
1290Sstevel@tonic-gate 		lb->filename = filename;
1300Sstevel@tonic-gate 		if (!(lb->stream = fopen(filename, "r"))) {
1310Sstevel@tonic-gate 			xfree(lb);
1320Sstevel@tonic-gate 			if (errfun)
1330Sstevel@tonic-gate 				(*errfun) ("%s: %s\n", filename, strerror(errno));
1340Sstevel@tonic-gate 			return (NULL);
1350Sstevel@tonic-gate 		}
1360Sstevel@tonic-gate 	} else {
1370Sstevel@tonic-gate 		lb->filename = "(stdin)";
1380Sstevel@tonic-gate 		lb->stream = stdin;
1390Sstevel@tonic-gate 	}
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	if (!(lb->buf = malloc(lb->size = LINEBUF_SIZE))) {
1420Sstevel@tonic-gate 		if (errfun)
1430Sstevel@tonic-gate 			(*errfun) ("linebuf (%s): malloc failed\n", lb->filename);
1440Sstevel@tonic-gate 		xfree(lb);
1450Sstevel@tonic-gate 		return (NULL);
1460Sstevel@tonic-gate 	}
1470Sstevel@tonic-gate 	lb->errfun = errfun;
1480Sstevel@tonic-gate 	lb->lineno = 0;
1490Sstevel@tonic-gate 	return (lb);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate static void
Linebuf_free(Linebuf * lb)1530Sstevel@tonic-gate Linebuf_free(Linebuf * lb)
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate 	fclose(lb->stream);
1560Sstevel@tonic-gate 	xfree(lb->buf);
1570Sstevel@tonic-gate 	xfree(lb);
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate #if 0
1610Sstevel@tonic-gate static void
1620Sstevel@tonic-gate Linebuf_restart(Linebuf * lb)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate 	clearerr(lb->stream);
1650Sstevel@tonic-gate 	rewind(lb->stream);
1660Sstevel@tonic-gate 	lb->lineno = 0;
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate static int
1700Sstevel@tonic-gate Linebuf_lineno(Linebuf * lb)
1710Sstevel@tonic-gate {
1720Sstevel@tonic-gate 	return (lb->lineno);
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate #endif
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate static char *
Linebuf_getline(Linebuf * lb)1770Sstevel@tonic-gate Linebuf_getline(Linebuf * lb)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate 	int n = 0;
1800Sstevel@tonic-gate 	void *p;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	lb->lineno++;
1830Sstevel@tonic-gate 	for (;;) {
1840Sstevel@tonic-gate 		/* Read a line */
1850Sstevel@tonic-gate 		if (!fgets(&lb->buf[n], lb->size - n, lb->stream)) {
1860Sstevel@tonic-gate 			if (ferror(lb->stream) && lb->errfun)
1870Sstevel@tonic-gate 				(*lb->errfun)("%s: %s\n", lb->filename,
1880Sstevel@tonic-gate 				    strerror(errno));
1890Sstevel@tonic-gate 			return (NULL);
1900Sstevel@tonic-gate 		}
1910Sstevel@tonic-gate 		n = strlen(lb->buf);
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 		/* Return it or an error if it fits */
1940Sstevel@tonic-gate 		if (n > 0 && lb->buf[n - 1] == '\n') {
1950Sstevel@tonic-gate 			lb->buf[n - 1] = '\0';
1960Sstevel@tonic-gate 			return (lb->buf);
1970Sstevel@tonic-gate 		}
1980Sstevel@tonic-gate 		if (n != lb->size - 1) {
1990Sstevel@tonic-gate 			if (lb->errfun)
2000Sstevel@tonic-gate 				(*lb->errfun)("%s: skipping incomplete last line\n",
2010Sstevel@tonic-gate 				    lb->filename);
2020Sstevel@tonic-gate 			return (NULL);
2030Sstevel@tonic-gate 		}
2040Sstevel@tonic-gate 		/* Double the buffer if we need more space */
2050Sstevel@tonic-gate 		lb->size *= 2;
2060Sstevel@tonic-gate 		if ((p = realloc(lb->buf, lb->size)) == NULL) {
2070Sstevel@tonic-gate 			lb->size /= 2;
2080Sstevel@tonic-gate 			if (lb->errfun)
2090Sstevel@tonic-gate 				(*lb->errfun)("linebuf (%s): realloc failed\n",
2100Sstevel@tonic-gate 				    lb->filename);
2110Sstevel@tonic-gate 			return (NULL);
2120Sstevel@tonic-gate 		}
2130Sstevel@tonic-gate 		lb->buf = p;
2140Sstevel@tonic-gate 	}
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate static int
fdlim_get(int hard)2180Sstevel@tonic-gate fdlim_get(int hard)
2190Sstevel@tonic-gate {
2200Sstevel@tonic-gate #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
2210Sstevel@tonic-gate 	struct rlimit rlfd;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
2240Sstevel@tonic-gate 		return (-1);
2250Sstevel@tonic-gate 	if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY)
2260Sstevel@tonic-gate 		return 10000;
2270Sstevel@tonic-gate 	else
2280Sstevel@tonic-gate 		return hard ? rlfd.rlim_max : rlfd.rlim_cur;
2290Sstevel@tonic-gate #elif defined (HAVE_SYSCONF)
2300Sstevel@tonic-gate 	return sysconf (_SC_OPEN_MAX);
2310Sstevel@tonic-gate #else
2320Sstevel@tonic-gate 	return 10000;
2330Sstevel@tonic-gate #endif
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate static int
fdlim_set(int lim)2370Sstevel@tonic-gate fdlim_set(int lim)
2380Sstevel@tonic-gate {
2390Sstevel@tonic-gate #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
2400Sstevel@tonic-gate 	struct rlimit rlfd;
2410Sstevel@tonic-gate #endif
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	if (lim <= 0)
2440Sstevel@tonic-gate 		return (-1);
2450Sstevel@tonic-gate #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
2460Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
2470Sstevel@tonic-gate 		return (-1);
2480Sstevel@tonic-gate 	rlfd.rlim_cur = lim;
2490Sstevel@tonic-gate 	if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0)
2500Sstevel@tonic-gate 		return (-1);
2510Sstevel@tonic-gate #elif defined (HAVE_SETDTABLESIZE)
2520Sstevel@tonic-gate 	setdtablesize(lim);
2530Sstevel@tonic-gate #endif
2540Sstevel@tonic-gate 	return (0);
2550Sstevel@tonic-gate }
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate /*
2580Sstevel@tonic-gate  * This is an strsep function that returns a null field for adjacent
2590Sstevel@tonic-gate  * separators.  This is the same as the 4.4BSD strsep, but different from the
2600Sstevel@tonic-gate  * one in the GNU libc.
2610Sstevel@tonic-gate  */
2620Sstevel@tonic-gate static char *
xstrsep(char ** str,const char * delim)2630Sstevel@tonic-gate xstrsep(char **str, const char *delim)
2640Sstevel@tonic-gate {
2650Sstevel@tonic-gate 	char *s, *e;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	if (!**str)
2680Sstevel@tonic-gate 		return (NULL);
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	s = *str;
2710Sstevel@tonic-gate 	e = s + strcspn(s, delim);
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	if (*e != '\0')
2740Sstevel@tonic-gate 		*e++ = '\0';
2750Sstevel@tonic-gate 	*str = e;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	return (s);
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate /*
2810Sstevel@tonic-gate  * Get the next non-null token (like GNU strsep).  Strsep() will return a
2820Sstevel@tonic-gate  * null token for two adjacent separators, so we may have to loop.
2830Sstevel@tonic-gate  */
2840Sstevel@tonic-gate static char *
strnnsep(char ** stringp,char * delim)2850Sstevel@tonic-gate strnnsep(char **stringp, char *delim)
2860Sstevel@tonic-gate {
2870Sstevel@tonic-gate 	char *tok;
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	do {
2900Sstevel@tonic-gate 		tok = xstrsep(stringp, delim);
2910Sstevel@tonic-gate 	} while (tok && *tok == '\0');
2920Sstevel@tonic-gate 	return (tok);
2930Sstevel@tonic-gate }
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate static Key *
keygrab_ssh1(con * c)2960Sstevel@tonic-gate keygrab_ssh1(con *c)
2970Sstevel@tonic-gate {
2980Sstevel@tonic-gate 	static Key *rsa;
2990Sstevel@tonic-gate 	static Buffer msg;
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	if (rsa == NULL) {
3020Sstevel@tonic-gate 		buffer_init(&msg);
3030Sstevel@tonic-gate 		rsa = key_new(KEY_RSA1);
3040Sstevel@tonic-gate 	}
3050Sstevel@tonic-gate 	buffer_append(&msg, c->c_data, c->c_plen);
3060Sstevel@tonic-gate 	buffer_consume(&msg, 8 - (c->c_plen & 7));	/* padding */
3070Sstevel@tonic-gate 	if (buffer_get_char(&msg) != (int) SSH_SMSG_PUBLIC_KEY) {
3080Sstevel@tonic-gate 		error("%s: invalid packet type", c->c_name);
3090Sstevel@tonic-gate 		buffer_clear(&msg);
3100Sstevel@tonic-gate 		return NULL;
3110Sstevel@tonic-gate 	}
3120Sstevel@tonic-gate 	buffer_consume(&msg, 8);		/* cookie */
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	/* server key */
3150Sstevel@tonic-gate 	(void) buffer_get_int(&msg);
3160Sstevel@tonic-gate 	buffer_get_bignum(&msg, rsa->rsa->e);
3170Sstevel@tonic-gate 	buffer_get_bignum(&msg, rsa->rsa->n);
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	/* host key */
3200Sstevel@tonic-gate 	(void) buffer_get_int(&msg);
3210Sstevel@tonic-gate 	buffer_get_bignum(&msg, rsa->rsa->e);
3220Sstevel@tonic-gate 	buffer_get_bignum(&msg, rsa->rsa->n);
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	buffer_clear(&msg);
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	return (rsa);
3270Sstevel@tonic-gate }
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate static int
hostjump(Key * hostkey)3300Sstevel@tonic-gate hostjump(Key *hostkey)
3310Sstevel@tonic-gate {
3320Sstevel@tonic-gate 	kexjmp_key = hostkey;
3330Sstevel@tonic-gate 	longjmp(kexjmp, 1);
3340Sstevel@tonic-gate 	/* NOTREACHED */
3350Sstevel@tonic-gate 	return (0);
3360Sstevel@tonic-gate }
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate static int
ssh2_capable(int remote_major,int remote_minor)3390Sstevel@tonic-gate ssh2_capable(int remote_major, int remote_minor)
3400Sstevel@tonic-gate {
3410Sstevel@tonic-gate 	switch (remote_major) {
3420Sstevel@tonic-gate 	case 1:
3430Sstevel@tonic-gate 		if (remote_minor == 99)
3440Sstevel@tonic-gate 			return 1;
3450Sstevel@tonic-gate 		break;
3460Sstevel@tonic-gate 	case 2:
3470Sstevel@tonic-gate 		return 1;
3480Sstevel@tonic-gate 	default:
3490Sstevel@tonic-gate 		break;
3500Sstevel@tonic-gate 	}
3510Sstevel@tonic-gate 	return 0;
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate static Key *
keygrab_ssh2(con * c)3550Sstevel@tonic-gate keygrab_ssh2(con *c)
3560Sstevel@tonic-gate {
3570Sstevel@tonic-gate 	int j;
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	packet_set_connection(c->c_fd, c->c_fd);
3600Sstevel@tonic-gate 	enable_compat20();
361*8658SJan.Pechanec@Sun.COM 	my_clnt_proposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
362*8658SJan.Pechanec@Sun.COM 	    c->c_keytype == KT_DSA? "ssh-dss": "ssh-rsa";
363*8658SJan.Pechanec@Sun.COM 	c->c_kex = kex_setup(c->c_name, my_clnt_proposal, NULL);
3647574SJan.Pechanec@Sun.COM 	kex_start(c->c_kex);
3650Sstevel@tonic-gate 	c->c_kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
3660Sstevel@tonic-gate 	c->c_kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
3670Sstevel@tonic-gate 	c->c_kex->verify_host_key = hostjump;
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	if (!(j = setjmp(kexjmp))) {
3700Sstevel@tonic-gate 		nonfatal_fatal = 1;
3710Sstevel@tonic-gate 		dispatch_run(DISPATCH_BLOCK, &c->c_kex->done, c->c_kex);
3720Sstevel@tonic-gate 		fprintf(stderr, "Impossible! dispatch_run() returned!\n");
3730Sstevel@tonic-gate 		exit(1);
3740Sstevel@tonic-gate 	}
3750Sstevel@tonic-gate 	nonfatal_fatal = 0;
3760Sstevel@tonic-gate 	xfree(c->c_kex);
3770Sstevel@tonic-gate 	c->c_kex = NULL;
3780Sstevel@tonic-gate 	packet_close();
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	return j < 0? NULL : kexjmp_key;
3810Sstevel@tonic-gate }
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate static void
keyprint(con * c,Key * key)3840Sstevel@tonic-gate keyprint(con *c, Key *key)
3850Sstevel@tonic-gate {
3860Sstevel@tonic-gate 	if (!key)
3870Sstevel@tonic-gate 		return;
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	fprintf(stdout, "%s ", c->c_output_name ? c->c_output_name : c->c_name);
3900Sstevel@tonic-gate 	key_write(key, stdout);
3910Sstevel@tonic-gate 	fputs("\n", stdout);
3920Sstevel@tonic-gate }
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate static int
tcpconnect(char * host)3950Sstevel@tonic-gate tcpconnect(char *host)
3960Sstevel@tonic-gate {
3970Sstevel@tonic-gate 	struct addrinfo hints, *ai, *aitop;
3980Sstevel@tonic-gate 	char strport[NI_MAXSERV];
3990Sstevel@tonic-gate 	int gaierr, s = -1;
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	snprintf(strport, sizeof strport, "%d", ssh_port);
4020Sstevel@tonic-gate 	memset(&hints, 0, sizeof(hints));
4030Sstevel@tonic-gate 	hints.ai_family = IPv4or6;
4040Sstevel@tonic-gate 	hints.ai_socktype = SOCK_STREAM;
4050Sstevel@tonic-gate 	if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
4060Sstevel@tonic-gate 		fatal("getaddrinfo %s: %s", host, gai_strerror(gaierr));
4070Sstevel@tonic-gate 	for (ai = aitop; ai; ai = ai->ai_next) {
4080Sstevel@tonic-gate 		s = socket(ai->ai_family, SOCK_STREAM, 0);
4090Sstevel@tonic-gate 		if (s < 0) {
4100Sstevel@tonic-gate 			error("socket: %s", strerror(errno));
4110Sstevel@tonic-gate 			continue;
4120Sstevel@tonic-gate 		}
4130Sstevel@tonic-gate 		if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
4140Sstevel@tonic-gate 			fatal("F_SETFL: %s", strerror(errno));
4150Sstevel@tonic-gate 		if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 &&
4160Sstevel@tonic-gate 		    errno != EINPROGRESS)
4170Sstevel@tonic-gate 			error("connect (`%s'): %s", host, strerror(errno));
4180Sstevel@tonic-gate 		else
4190Sstevel@tonic-gate 			break;
4200Sstevel@tonic-gate 		close(s);
4210Sstevel@tonic-gate 		s = -1;
4220Sstevel@tonic-gate 	}
4230Sstevel@tonic-gate 	freeaddrinfo(aitop);
4240Sstevel@tonic-gate 	return s;
4250Sstevel@tonic-gate }
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate static int
conalloc(char * iname,char * oname,int keytype)4280Sstevel@tonic-gate conalloc(char *iname, char *oname, int keytype)
4290Sstevel@tonic-gate {
4300Sstevel@tonic-gate 	char *namebase, *name, *namelist;
4310Sstevel@tonic-gate 	int s;
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	namebase = namelist = xstrdup(iname);
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	do {
4360Sstevel@tonic-gate 		name = xstrsep(&namelist, ",");
4370Sstevel@tonic-gate 		if (!name) {
4380Sstevel@tonic-gate 			xfree(namebase);
4390Sstevel@tonic-gate 			return (-1);
4400Sstevel@tonic-gate 		}
4410Sstevel@tonic-gate 	} while ((s = tcpconnect(name)) < 0);
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	if (s >= maxfd)
4440Sstevel@tonic-gate 		fatal("conalloc: fdno %d too high", s);
4450Sstevel@tonic-gate 	if (fdcon[s].c_status)
4460Sstevel@tonic-gate 		fatal("conalloc: attempt to reuse fdno %d", s);
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	fdcon[s].c_fd = s;
4490Sstevel@tonic-gate 	fdcon[s].c_status = CS_CON;
4500Sstevel@tonic-gate 	fdcon[s].c_namebase = namebase;
4510Sstevel@tonic-gate 	fdcon[s].c_name = name;
4520Sstevel@tonic-gate 	fdcon[s].c_namelist = namelist;
4530Sstevel@tonic-gate 	fdcon[s].c_output_name = xstrdup(oname);
4540Sstevel@tonic-gate 	fdcon[s].c_data = (char *) &fdcon[s].c_plen;
4550Sstevel@tonic-gate 	fdcon[s].c_len = 4;
4560Sstevel@tonic-gate 	fdcon[s].c_off = 0;
4570Sstevel@tonic-gate 	fdcon[s].c_keytype = keytype;
4580Sstevel@tonic-gate 	gettimeofday(&fdcon[s].c_tv, NULL);
4590Sstevel@tonic-gate 	fdcon[s].c_tv.tv_sec += timeout;
4600Sstevel@tonic-gate 	TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
4610Sstevel@tonic-gate 	FD_SET(s, read_wait);
4620Sstevel@tonic-gate 	ncon++;
4630Sstevel@tonic-gate 	return (s);
4640Sstevel@tonic-gate }
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate static void
confree(int s)4670Sstevel@tonic-gate confree(int s)
4680Sstevel@tonic-gate {
4690Sstevel@tonic-gate 	if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
4700Sstevel@tonic-gate 		fatal("confree: attempt to free bad fdno %d", s);
4710Sstevel@tonic-gate 	close(s);
4720Sstevel@tonic-gate 	xfree(fdcon[s].c_namebase);
4730Sstevel@tonic-gate 	xfree(fdcon[s].c_output_name);
4740Sstevel@tonic-gate 	if (fdcon[s].c_status == CS_KEYS)
4750Sstevel@tonic-gate 		xfree(fdcon[s].c_data);
4760Sstevel@tonic-gate 	fdcon[s].c_status = CS_UNUSED;
4770Sstevel@tonic-gate 	fdcon[s].c_keytype = 0;
4780Sstevel@tonic-gate 	TAILQ_REMOVE(&tq, &fdcon[s], c_link);
4790Sstevel@tonic-gate 	FD_CLR(s, read_wait);
4800Sstevel@tonic-gate 	ncon--;
4810Sstevel@tonic-gate }
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate static void
contouch(int s)4840Sstevel@tonic-gate contouch(int s)
4850Sstevel@tonic-gate {
4860Sstevel@tonic-gate 	TAILQ_REMOVE(&tq, &fdcon[s], c_link);
4870Sstevel@tonic-gate 	gettimeofday(&fdcon[s].c_tv, NULL);
4880Sstevel@tonic-gate 	fdcon[s].c_tv.tv_sec += timeout;
4890Sstevel@tonic-gate 	TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
4900Sstevel@tonic-gate }
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate static int
conrecycle(int s)4930Sstevel@tonic-gate conrecycle(int s)
4940Sstevel@tonic-gate {
4950Sstevel@tonic-gate 	con *c = &fdcon[s];
4960Sstevel@tonic-gate 	int ret;
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype);
4990Sstevel@tonic-gate 	confree(s);
5000Sstevel@tonic-gate 	return (ret);
5010Sstevel@tonic-gate }
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate static void
congreet(int s)5040Sstevel@tonic-gate congreet(int s)
5050Sstevel@tonic-gate {
5060Sstevel@tonic-gate 	int remote_major, remote_minor, n = 0;
5070Sstevel@tonic-gate 	char buf[256], *cp;
5080Sstevel@tonic-gate 	char remote_version[sizeof buf];
5090Sstevel@tonic-gate 	size_t bufsiz;
5100Sstevel@tonic-gate 	con *c = &fdcon[s];
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	bufsiz = sizeof(buf);
5130Sstevel@tonic-gate 	cp = buf;
5140Sstevel@tonic-gate 	while (bufsiz-- && (n = read(s, cp, 1)) == 1 && *cp != '\n') {
5150Sstevel@tonic-gate 		if (*cp == '\r')
5160Sstevel@tonic-gate 			*cp = '\n';
5170Sstevel@tonic-gate 		cp++;
5180Sstevel@tonic-gate 	}
5190Sstevel@tonic-gate 	if (n < 0) {
5200Sstevel@tonic-gate 		if (errno != ECONNREFUSED)
5210Sstevel@tonic-gate 			error("read (%s): %s", c->c_name, strerror(errno));
5220Sstevel@tonic-gate 		conrecycle(s);
5230Sstevel@tonic-gate 		return;
5240Sstevel@tonic-gate 	}
5250Sstevel@tonic-gate 	if (n == 0) {
5260Sstevel@tonic-gate 		error("%s: Connection closed by remote host", c->c_name);
5270Sstevel@tonic-gate 		conrecycle(s);
5280Sstevel@tonic-gate 		return;
5290Sstevel@tonic-gate 	}
5300Sstevel@tonic-gate 	if (*cp != '\n' && *cp != '\r') {
5310Sstevel@tonic-gate 		error("%s: bad greeting", c->c_name);
5320Sstevel@tonic-gate 		confree(s);
5330Sstevel@tonic-gate 		return;
5340Sstevel@tonic-gate 	}
5350Sstevel@tonic-gate 	*cp = '\0';
5360Sstevel@tonic-gate 	if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
5370Sstevel@tonic-gate 	    &remote_major, &remote_minor, remote_version) == 3)
5380Sstevel@tonic-gate 		compat_datafellows(remote_version);
5390Sstevel@tonic-gate 	else
5400Sstevel@tonic-gate 		datafellows = 0;
5410Sstevel@tonic-gate 	if (c->c_keytype != KT_RSA1) {
5420Sstevel@tonic-gate 		if (!ssh2_capable(remote_major, remote_minor)) {
5430Sstevel@tonic-gate 			debug("%s doesn't support ssh2", c->c_name);
5440Sstevel@tonic-gate 			confree(s);
5450Sstevel@tonic-gate 			return;
5460Sstevel@tonic-gate 		}
5470Sstevel@tonic-gate 	} else if (remote_major != 1) {
5480Sstevel@tonic-gate 		debug("%s doesn't support ssh1", c->c_name);
5490Sstevel@tonic-gate 		confree(s);
5500Sstevel@tonic-gate 		return;
5510Sstevel@tonic-gate 	}
5520Sstevel@tonic-gate 	fprintf(stderr, "# %s %s\n", c->c_name, chop(buf));
5530Sstevel@tonic-gate 	n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n",
5540Sstevel@tonic-gate 	    c->c_keytype == KT_RSA1? PROTOCOL_MAJOR_1 : PROTOCOL_MAJOR_2,
5550Sstevel@tonic-gate 	    c->c_keytype == KT_RSA1? PROTOCOL_MINOR_1 : PROTOCOL_MINOR_2);
5560Sstevel@tonic-gate 	if (atomicio(write, s, buf, n) != n) {
5570Sstevel@tonic-gate 		error("write (%s): %s", c->c_name, strerror(errno));
5580Sstevel@tonic-gate 		confree(s);
5590Sstevel@tonic-gate 		return;
5600Sstevel@tonic-gate 	}
5610Sstevel@tonic-gate 	if (c->c_keytype != KT_RSA1) {
5620Sstevel@tonic-gate 		keyprint(c, keygrab_ssh2(c));
5630Sstevel@tonic-gate 		confree(s);
5640Sstevel@tonic-gate 		return;
5650Sstevel@tonic-gate 	}
5660Sstevel@tonic-gate 	c->c_status = CS_SIZE;
5670Sstevel@tonic-gate 	contouch(s);
5680Sstevel@tonic-gate }
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate static void
conread(int s)5710Sstevel@tonic-gate conread(int s)
5720Sstevel@tonic-gate {
5730Sstevel@tonic-gate 	con *c = &fdcon[s];
5740Sstevel@tonic-gate 	int n;
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	if (c->c_status == CS_CON) {
5770Sstevel@tonic-gate 		congreet(s);
5780Sstevel@tonic-gate 		return;
5790Sstevel@tonic-gate 	}
5800Sstevel@tonic-gate 	n = read(s, c->c_data + c->c_off, c->c_len - c->c_off);
5810Sstevel@tonic-gate 	if (n < 0) {
5820Sstevel@tonic-gate 		error("read (%s): %s", c->c_name, strerror(errno));
5830Sstevel@tonic-gate 		confree(s);
5840Sstevel@tonic-gate 		return;
5850Sstevel@tonic-gate 	}
5860Sstevel@tonic-gate 	c->c_off += n;
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	if (c->c_off == c->c_len)
5890Sstevel@tonic-gate 		switch (c->c_status) {
5900Sstevel@tonic-gate 		case CS_SIZE:
5910Sstevel@tonic-gate 			c->c_plen = htonl(c->c_plen);
5920Sstevel@tonic-gate 			c->c_len = c->c_plen + 8 - (c->c_plen & 7);
5930Sstevel@tonic-gate 			c->c_off = 0;
5940Sstevel@tonic-gate 			c->c_data = xmalloc(c->c_len);
5950Sstevel@tonic-gate 			c->c_status = CS_KEYS;
5960Sstevel@tonic-gate 			break;
5970Sstevel@tonic-gate 		case CS_KEYS:
5980Sstevel@tonic-gate 			keyprint(c, keygrab_ssh1(c));
5990Sstevel@tonic-gate 			confree(s);
6000Sstevel@tonic-gate 			return;
6010Sstevel@tonic-gate 			break;
6020Sstevel@tonic-gate 		default:
6030Sstevel@tonic-gate 			fatal("conread: invalid status %d", c->c_status);
6040Sstevel@tonic-gate 			break;
6050Sstevel@tonic-gate 		}
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	contouch(s);
6080Sstevel@tonic-gate }
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate static void
conloop(void)6110Sstevel@tonic-gate conloop(void)
6120Sstevel@tonic-gate {
6130Sstevel@tonic-gate 	struct timeval seltime, now;
6140Sstevel@tonic-gate 	fd_set *r, *e;
6150Sstevel@tonic-gate 	con *c;
6160Sstevel@tonic-gate 	int i;
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 	gettimeofday(&now, NULL);
6190Sstevel@tonic-gate 	c = TAILQ_FIRST(&tq);
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 	if (c && (c->c_tv.tv_sec > now.tv_sec ||
6220Sstevel@tonic-gate 	    (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) {
6230Sstevel@tonic-gate 		seltime = c->c_tv;
6240Sstevel@tonic-gate 		seltime.tv_sec -= now.tv_sec;
6250Sstevel@tonic-gate 		seltime.tv_usec -= now.tv_usec;
6260Sstevel@tonic-gate 		if (seltime.tv_usec < 0) {
6270Sstevel@tonic-gate 			seltime.tv_usec += 1000000;
6280Sstevel@tonic-gate 			seltime.tv_sec--;
6290Sstevel@tonic-gate 		}
6300Sstevel@tonic-gate 	} else
6310Sstevel@tonic-gate 		seltime.tv_sec = seltime.tv_usec = 0;
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	r = xmalloc(read_wait_size);
6340Sstevel@tonic-gate 	memcpy(r, read_wait, read_wait_size);
6350Sstevel@tonic-gate 	e = xmalloc(read_wait_size);
6360Sstevel@tonic-gate 	memcpy(e, read_wait, read_wait_size);
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	while (select(maxfd, r, NULL, e, &seltime) == -1 &&
6390Sstevel@tonic-gate 	    (errno == EAGAIN || errno == EINTR))
6400Sstevel@tonic-gate 		;
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	for (i = 0; i < maxfd; i++) {
6430Sstevel@tonic-gate 		if (FD_ISSET(i, e)) {
6440Sstevel@tonic-gate 			error("%s: exception!", fdcon[i].c_name);
6450Sstevel@tonic-gate 			confree(i);
6460Sstevel@tonic-gate 		} else if (FD_ISSET(i, r))
6470Sstevel@tonic-gate 			conread(i);
6480Sstevel@tonic-gate 	}
6490Sstevel@tonic-gate 	xfree(r);
6500Sstevel@tonic-gate 	xfree(e);
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 	c = TAILQ_FIRST(&tq);
6530Sstevel@tonic-gate 	while (c && (c->c_tv.tv_sec < now.tv_sec ||
6540Sstevel@tonic-gate 	    (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) {
6550Sstevel@tonic-gate 		int s = c->c_fd;
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 		c = TAILQ_NEXT(c, c_link);
6580Sstevel@tonic-gate 		conrecycle(s);
6590Sstevel@tonic-gate 	}
6600Sstevel@tonic-gate }
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate static void
do_host(char * host)6630Sstevel@tonic-gate do_host(char *host)
6640Sstevel@tonic-gate {
6650Sstevel@tonic-gate 	char *name = strnnsep(&host, " \t\n");
6660Sstevel@tonic-gate 	int j;
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	if (name == NULL)
6690Sstevel@tonic-gate 		return;
6700Sstevel@tonic-gate 	for (j = KT_RSA1; j <= KT_RSA; j *= 2) {
6710Sstevel@tonic-gate 		if (get_keytypes & j) {
6720Sstevel@tonic-gate 			while (ncon >= MAXCON)
6730Sstevel@tonic-gate 				conloop();
6740Sstevel@tonic-gate 			conalloc(name, *host ? host : name, j);
6750Sstevel@tonic-gate 		}
6760Sstevel@tonic-gate 	}
6770Sstevel@tonic-gate }
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate void
fatal(const char * fmt,...)6800Sstevel@tonic-gate fatal(const char *fmt,...)
6810Sstevel@tonic-gate {
6820Sstevel@tonic-gate 	va_list args;
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	va_start(args, fmt);
6850Sstevel@tonic-gate 	do_log(SYSLOG_LEVEL_FATAL, fmt, args);
6860Sstevel@tonic-gate 	va_end(args);
6870Sstevel@tonic-gate 	if (nonfatal_fatal)
6880Sstevel@tonic-gate 		longjmp(kexjmp, -1);
6890Sstevel@tonic-gate 	else
6900Sstevel@tonic-gate 		fatal_cleanup();
6910Sstevel@tonic-gate }
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate static void
usage(void)6940Sstevel@tonic-gate usage(void)
6950Sstevel@tonic-gate {
6960Sstevel@tonic-gate 	fprintf(stderr,
6970Sstevel@tonic-gate 		gettext("Usage: %s [-v46] [-p port] [-T timeout] [-f file]\n"
6980Sstevel@tonic-gate 			"\t\t   [host | addrlist namelist] [...]\n"),
6990Sstevel@tonic-gate 	    __progname);
7000Sstevel@tonic-gate 	exit(1);
7010Sstevel@tonic-gate }
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate int
main(int argc,char ** argv)7040Sstevel@tonic-gate main(int argc, char **argv)
7050Sstevel@tonic-gate {
7060Sstevel@tonic-gate 	int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO;
7070Sstevel@tonic-gate 	int opt, fopt_count = 0;
7080Sstevel@tonic-gate 	char *tname;
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	extern int optind;
7110Sstevel@tonic-gate 	extern char *optarg;
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	__progname = get_progname(argv[0]);
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	(void) g11n_setlocale(LC_ALL, "");
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 	init_rng();
7180Sstevel@tonic-gate 	seed_rng();
7190Sstevel@tonic-gate 	TAILQ_INIT(&tq);
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	if (argc <= 1)
7220Sstevel@tonic-gate 		usage();
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "v46p:T:t:f:")) != -1) {
7250Sstevel@tonic-gate 		switch (opt) {
7260Sstevel@tonic-gate 		case 'p':
7270Sstevel@tonic-gate 			ssh_port = a2port(optarg);
7280Sstevel@tonic-gate 			if (ssh_port == 0) {
7290Sstevel@tonic-gate 				fprintf(stderr, gettext("Bad port '%s'\n"),
7300Sstevel@tonic-gate 					optarg);
7310Sstevel@tonic-gate 				exit(1);
7320Sstevel@tonic-gate 			}
7330Sstevel@tonic-gate 			break;
7340Sstevel@tonic-gate 		case 'T':
7350Sstevel@tonic-gate 			timeout = convtime(optarg);
7360Sstevel@tonic-gate 			if (timeout == -1 || timeout == 0) {
7370Sstevel@tonic-gate 				fprintf(stderr, gettext("Bad timeout '%s'\n"),
7380Sstevel@tonic-gate 					optarg);
7390Sstevel@tonic-gate 				usage();
7400Sstevel@tonic-gate 			}
7410Sstevel@tonic-gate 			break;
7420Sstevel@tonic-gate 		case 'v':
7430Sstevel@tonic-gate 			if (!debug_flag) {
7440Sstevel@tonic-gate 				debug_flag = 1;
7450Sstevel@tonic-gate 				log_level = SYSLOG_LEVEL_DEBUG1;
7460Sstevel@tonic-gate 			}
7470Sstevel@tonic-gate 			else if (log_level < SYSLOG_LEVEL_DEBUG3)
7480Sstevel@tonic-gate 				log_level++;
7490Sstevel@tonic-gate 			else
7500Sstevel@tonic-gate 				fatal("Too high debugging level.");
7510Sstevel@tonic-gate 			break;
7520Sstevel@tonic-gate 		case 'f':
7530Sstevel@tonic-gate 			if (strcmp(optarg, "-") == 0)
7540Sstevel@tonic-gate 				optarg = NULL;
7550Sstevel@tonic-gate 			argv[fopt_count++] = optarg;
7560Sstevel@tonic-gate 			break;
7570Sstevel@tonic-gate 		case 't':
7580Sstevel@tonic-gate 			get_keytypes = 0;
7590Sstevel@tonic-gate 			tname = strtok(optarg, ",");
7600Sstevel@tonic-gate 			while (tname) {
7610Sstevel@tonic-gate 				int type = key_type_from_name(tname);
7620Sstevel@tonic-gate 				switch (type) {
7630Sstevel@tonic-gate 				case KEY_RSA1:
7640Sstevel@tonic-gate 					get_keytypes |= KT_RSA1;
7650Sstevel@tonic-gate 					break;
7660Sstevel@tonic-gate 				case KEY_DSA:
7670Sstevel@tonic-gate 					get_keytypes |= KT_DSA;
7680Sstevel@tonic-gate 					break;
7690Sstevel@tonic-gate 				case KEY_RSA:
7700Sstevel@tonic-gate 					get_keytypes |= KT_RSA;
7710Sstevel@tonic-gate 					break;
7720Sstevel@tonic-gate 				case KEY_UNSPEC:
7730Sstevel@tonic-gate 					fatal("unknown key type %s", tname);
7740Sstevel@tonic-gate 				}
7750Sstevel@tonic-gate 				tname = strtok(NULL, ",");
7760Sstevel@tonic-gate 			}
7770Sstevel@tonic-gate 			break;
7780Sstevel@tonic-gate 		case '4':
7790Sstevel@tonic-gate 			IPv4or6 = AF_INET;
7800Sstevel@tonic-gate 			break;
7810Sstevel@tonic-gate 		case '6':
7820Sstevel@tonic-gate 			IPv4or6 = AF_INET6;
7830Sstevel@tonic-gate 			break;
7840Sstevel@tonic-gate 		case '?':
7850Sstevel@tonic-gate 		default:
7860Sstevel@tonic-gate 			usage();
7870Sstevel@tonic-gate 		}
7880Sstevel@tonic-gate 	}
7890Sstevel@tonic-gate 	if (optind == argc && !fopt_count)
7900Sstevel@tonic-gate 		usage();
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1);
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	maxfd = fdlim_get(1);
7950Sstevel@tonic-gate 	if (maxfd < 0)
7960Sstevel@tonic-gate 		fatal("%s: fdlim_get: bad value", __progname);
7970Sstevel@tonic-gate 	if (maxfd > MAXMAXFD)
7980Sstevel@tonic-gate 		maxfd = MAXMAXFD;
7990Sstevel@tonic-gate 	if (MAXCON <= 0)
8000Sstevel@tonic-gate 		fatal("%s: not enough file descriptors", __progname);
8010Sstevel@tonic-gate 	if (maxfd > fdlim_get(0))
8020Sstevel@tonic-gate 		fdlim_set(maxfd);
8030Sstevel@tonic-gate 	fdcon = xmalloc(maxfd * sizeof(con));
8040Sstevel@tonic-gate 	memset(fdcon, 0, maxfd * sizeof(con));
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	read_wait_size = howmany(maxfd, NFDBITS) * sizeof(fd_mask);
8070Sstevel@tonic-gate 	read_wait = xmalloc(read_wait_size);
8080Sstevel@tonic-gate 	memset(read_wait, 0, read_wait_size);
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	if (fopt_count) {
8110Sstevel@tonic-gate 		Linebuf *lb;
8120Sstevel@tonic-gate 		char *line;
8130Sstevel@tonic-gate 		int j;
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 		for (j = 0; j < fopt_count; j++) {
8160Sstevel@tonic-gate 			lb = Linebuf_alloc(argv[j], error);
8170Sstevel@tonic-gate 			if (!lb)
8180Sstevel@tonic-gate 				continue;
8190Sstevel@tonic-gate 			while ((line = Linebuf_getline(lb)) != NULL)
8200Sstevel@tonic-gate 				do_host(line);
8210Sstevel@tonic-gate 			Linebuf_free(lb);
8220Sstevel@tonic-gate 		}
8230Sstevel@tonic-gate 	}
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	while (optind < argc)
8260Sstevel@tonic-gate 		do_host(argv[optind++]);
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	while (ncon > 0)
8290Sstevel@tonic-gate 		conloop();
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	return (0);
8320Sstevel@tonic-gate }
833