xref: /titanic_50/usr/src/cmd/ssh/ssh-keyscan/ssh-keyscan.c (revision e63a6e294d707d97ff9384b78a34d4f0189e4574)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright 1995, 1996 by David Mazieres <dm@lcs.mit.edu>.
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * Modification and redistribution in source and binary forms is
57c478bd9Sstevel@tonic-gate  * permitted provided that due credit is given to the author and the
67c478bd9Sstevel@tonic-gate  * OpenBSD project by leaving this copyright notice intact.
77c478bd9Sstevel@tonic-gate  */
87c478bd9Sstevel@tonic-gate 
9cd7d5fafSJan Pechanec /*
10*e63a6e29SJan Pechanec  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
11cd7d5fafSJan Pechanec  * Use is subject to license terms.
12cd7d5fafSJan Pechanec  */
13cd7d5fafSJan Pechanec 
147c478bd9Sstevel@tonic-gate #include "includes.h"
157c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: ssh-keyscan.c,v 1.40 2002/07/06 17:47:58 stevesk Exp $");
167c478bd9Sstevel@tonic-gate 
177c478bd9Sstevel@tonic-gate #include "sys-queue.h"
187c478bd9Sstevel@tonic-gate 
197c478bd9Sstevel@tonic-gate #include <openssl/bn.h>
207c478bd9Sstevel@tonic-gate 
217c478bd9Sstevel@tonic-gate #include <setjmp.h>
227c478bd9Sstevel@tonic-gate #include "xmalloc.h"
237c478bd9Sstevel@tonic-gate #include "ssh.h"
247c478bd9Sstevel@tonic-gate #include "ssh1.h"
257c478bd9Sstevel@tonic-gate #include "key.h"
267c478bd9Sstevel@tonic-gate #include "kex.h"
277c478bd9Sstevel@tonic-gate #include "compat.h"
287c478bd9Sstevel@tonic-gate #include "myproposal.h"
297c478bd9Sstevel@tonic-gate #include "packet.h"
307c478bd9Sstevel@tonic-gate #include "dispatch.h"
317c478bd9Sstevel@tonic-gate #include "buffer.h"
327c478bd9Sstevel@tonic-gate #include "bufaux.h"
337c478bd9Sstevel@tonic-gate #include "log.h"
347c478bd9Sstevel@tonic-gate #include "atomicio.h"
357c478bd9Sstevel@tonic-gate #include "misc.h"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /* Flag indicating whether IPv4 or IPv6.  This can be set on the command line.
387c478bd9Sstevel@tonic-gate    Default value is AF_UNSPEC means both IPv4 and IPv6. */
397c478bd9Sstevel@tonic-gate #ifdef IPV4_DEFAULT
407c478bd9Sstevel@tonic-gate int IPv4or6 = AF_INET;
417c478bd9Sstevel@tonic-gate #else
427c478bd9Sstevel@tonic-gate int IPv4or6 = AF_UNSPEC;
437c478bd9Sstevel@tonic-gate #endif
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate int ssh_port = SSH_DEFAULT_PORT;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #define KT_RSA1	1
487c478bd9Sstevel@tonic-gate #define KT_DSA	2
497c478bd9Sstevel@tonic-gate #define KT_RSA	4
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate int get_keytypes = KT_RSA1;	/* Get only RSA1 keys by default */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #define MAXMAXFD 256
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /* The number of seconds after which to give up on a TCP connection */
567c478bd9Sstevel@tonic-gate int timeout = 5;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate int maxfd;
597c478bd9Sstevel@tonic-gate #define MAXCON (maxfd - 10)
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #ifdef HAVE___PROGNAME
627c478bd9Sstevel@tonic-gate extern char *__progname;
637c478bd9Sstevel@tonic-gate #else
647c478bd9Sstevel@tonic-gate char *__progname;
657c478bd9Sstevel@tonic-gate #endif
667c478bd9Sstevel@tonic-gate fd_set *read_wait;
677c478bd9Sstevel@tonic-gate size_t read_wait_size;
687c478bd9Sstevel@tonic-gate int ncon;
697c478bd9Sstevel@tonic-gate int nonfatal_fatal = 0;
707c478bd9Sstevel@tonic-gate jmp_buf kexjmp;
717c478bd9Sstevel@tonic-gate Key *kexjmp_key;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * Keep a connection structure for each file descriptor.  The state
757c478bd9Sstevel@tonic-gate  * associated with file descriptor n is held in fdcon[n].
767c478bd9Sstevel@tonic-gate  */
777c478bd9Sstevel@tonic-gate typedef struct Connection {
787c478bd9Sstevel@tonic-gate 	u_char c_status;	/* State of connection on this file desc. */
797c478bd9Sstevel@tonic-gate #define CS_UNUSED 0		/* File descriptor unused */
807c478bd9Sstevel@tonic-gate #define CS_CON 1		/* Waiting to connect/read greeting */
817c478bd9Sstevel@tonic-gate #define CS_SIZE 2		/* Waiting to read initial packet size */
827c478bd9Sstevel@tonic-gate #define CS_KEYS 3		/* Waiting to read public key packet */
837c478bd9Sstevel@tonic-gate 	int c_fd;		/* Quick lookup: c->c_fd == c - fdcon */
847c478bd9Sstevel@tonic-gate 	int c_plen;		/* Packet length field for ssh packet */
857c478bd9Sstevel@tonic-gate 	int c_len;		/* Total bytes which must be read. */
867c478bd9Sstevel@tonic-gate 	int c_off;		/* Length of data read so far. */
877c478bd9Sstevel@tonic-gate 	int c_keytype;		/* Only one of KT_RSA1, KT_DSA, or KT_RSA */
887c478bd9Sstevel@tonic-gate 	char *c_namebase;	/* Address to free for c_name and c_namelist */
897c478bd9Sstevel@tonic-gate 	char *c_name;		/* Hostname of connection for errors */
907c478bd9Sstevel@tonic-gate 	char *c_namelist;	/* Pointer to other possible addresses */
917c478bd9Sstevel@tonic-gate 	char *c_output_name;	/* Hostname of connection for output */
927c478bd9Sstevel@tonic-gate 	char *c_data;		/* Data read from this fd */
937c478bd9Sstevel@tonic-gate 	Kex *c_kex;		/* The key-exchange struct for ssh2 */
947c478bd9Sstevel@tonic-gate 	struct timeval c_tv;	/* Time at which connection gets aborted */
957c478bd9Sstevel@tonic-gate 	TAILQ_ENTRY(Connection) c_link;	/* List of connections in timeout order. */
967c478bd9Sstevel@tonic-gate } con;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate TAILQ_HEAD(conlist, Connection) tq;	/* Timeout Queue */
997c478bd9Sstevel@tonic-gate con *fdcon;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate  *  This is just a wrapper around fgets() to make it usable.
1037c478bd9Sstevel@tonic-gate  */
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /* Stress-test.  Increase this later. */
1067c478bd9Sstevel@tonic-gate #define LINEBUF_SIZE 16
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate typedef struct {
1097c478bd9Sstevel@tonic-gate 	char *buf;
1107c478bd9Sstevel@tonic-gate 	u_int size;
1117c478bd9Sstevel@tonic-gate 	int lineno;
1127c478bd9Sstevel@tonic-gate 	const char *filename;
1137c478bd9Sstevel@tonic-gate 	FILE *stream;
1147c478bd9Sstevel@tonic-gate 	void (*errfun) (const char *,...);
1157c478bd9Sstevel@tonic-gate } Linebuf;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate static Linebuf *
Linebuf_alloc(const char * filename,void (* errfun)(const char *,...))1187c478bd9Sstevel@tonic-gate Linebuf_alloc(const char *filename, void (*errfun) (const char *,...))
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate 	Linebuf *lb;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	if (!(lb = malloc(sizeof(*lb)))) {
1237c478bd9Sstevel@tonic-gate 		if (errfun)
1247c478bd9Sstevel@tonic-gate 			(*errfun) ("linebuf (%s): malloc failed\n",
1257c478bd9Sstevel@tonic-gate 			    filename ? filename : "(stdin)");
1267c478bd9Sstevel@tonic-gate 		return (NULL);
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate 	if (filename) {
1297c478bd9Sstevel@tonic-gate 		lb->filename = filename;
1307c478bd9Sstevel@tonic-gate 		if (!(lb->stream = fopen(filename, "r"))) {
1317c478bd9Sstevel@tonic-gate 			xfree(lb);
1327c478bd9Sstevel@tonic-gate 			if (errfun)
1337c478bd9Sstevel@tonic-gate 				(*errfun) ("%s: %s\n", filename, strerror(errno));
1347c478bd9Sstevel@tonic-gate 			return (NULL);
1357c478bd9Sstevel@tonic-gate 		}
1367c478bd9Sstevel@tonic-gate 	} else {
1377c478bd9Sstevel@tonic-gate 		lb->filename = "(stdin)";
1387c478bd9Sstevel@tonic-gate 		lb->stream = stdin;
1397c478bd9Sstevel@tonic-gate 	}
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	if (!(lb->buf = malloc(lb->size = LINEBUF_SIZE))) {
1427c478bd9Sstevel@tonic-gate 		if (errfun)
1437c478bd9Sstevel@tonic-gate 			(*errfun) ("linebuf (%s): malloc failed\n", lb->filename);
1447c478bd9Sstevel@tonic-gate 		xfree(lb);
1457c478bd9Sstevel@tonic-gate 		return (NULL);
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 	lb->errfun = errfun;
1487c478bd9Sstevel@tonic-gate 	lb->lineno = 0;
1497c478bd9Sstevel@tonic-gate 	return (lb);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate static void
Linebuf_free(Linebuf * lb)1537c478bd9Sstevel@tonic-gate Linebuf_free(Linebuf * lb)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	fclose(lb->stream);
1567c478bd9Sstevel@tonic-gate 	xfree(lb->buf);
1577c478bd9Sstevel@tonic-gate 	xfree(lb);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate #if 0
1617c478bd9Sstevel@tonic-gate static void
1627c478bd9Sstevel@tonic-gate Linebuf_restart(Linebuf * lb)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 	clearerr(lb->stream);
1657c478bd9Sstevel@tonic-gate 	rewind(lb->stream);
1667c478bd9Sstevel@tonic-gate 	lb->lineno = 0;
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate static int
1707c478bd9Sstevel@tonic-gate Linebuf_lineno(Linebuf * lb)
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate 	return (lb->lineno);
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate #endif
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate static char *
Linebuf_getline(Linebuf * lb)1777c478bd9Sstevel@tonic-gate Linebuf_getline(Linebuf * lb)
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate 	int n = 0;
1807c478bd9Sstevel@tonic-gate 	void *p;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	lb->lineno++;
1837c478bd9Sstevel@tonic-gate 	for (;;) {
1847c478bd9Sstevel@tonic-gate 		/* Read a line */
1857c478bd9Sstevel@tonic-gate 		if (!fgets(&lb->buf[n], lb->size - n, lb->stream)) {
1867c478bd9Sstevel@tonic-gate 			if (ferror(lb->stream) && lb->errfun)
1877c478bd9Sstevel@tonic-gate 				(*lb->errfun)("%s: %s\n", lb->filename,
1887c478bd9Sstevel@tonic-gate 				    strerror(errno));
1897c478bd9Sstevel@tonic-gate 			return (NULL);
1907c478bd9Sstevel@tonic-gate 		}
1917c478bd9Sstevel@tonic-gate 		n = strlen(lb->buf);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 		/* Return it or an error if it fits */
1947c478bd9Sstevel@tonic-gate 		if (n > 0 && lb->buf[n - 1] == '\n') {
1957c478bd9Sstevel@tonic-gate 			lb->buf[n - 1] = '\0';
1967c478bd9Sstevel@tonic-gate 			return (lb->buf);
1977c478bd9Sstevel@tonic-gate 		}
1987c478bd9Sstevel@tonic-gate 		if (n != lb->size - 1) {
1997c478bd9Sstevel@tonic-gate 			if (lb->errfun)
2007c478bd9Sstevel@tonic-gate 				(*lb->errfun)("%s: skipping incomplete last line\n",
2017c478bd9Sstevel@tonic-gate 				    lb->filename);
2027c478bd9Sstevel@tonic-gate 			return (NULL);
2037c478bd9Sstevel@tonic-gate 		}
2047c478bd9Sstevel@tonic-gate 		/* Double the buffer if we need more space */
2057c478bd9Sstevel@tonic-gate 		lb->size *= 2;
2067c478bd9Sstevel@tonic-gate 		if ((p = realloc(lb->buf, lb->size)) == NULL) {
2077c478bd9Sstevel@tonic-gate 			lb->size /= 2;
2087c478bd9Sstevel@tonic-gate 			if (lb->errfun)
2097c478bd9Sstevel@tonic-gate 				(*lb->errfun)("linebuf (%s): realloc failed\n",
2107c478bd9Sstevel@tonic-gate 				    lb->filename);
2117c478bd9Sstevel@tonic-gate 			return (NULL);
2127c478bd9Sstevel@tonic-gate 		}
2137c478bd9Sstevel@tonic-gate 		lb->buf = p;
2147c478bd9Sstevel@tonic-gate 	}
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate static int
fdlim_get(int hard)2187c478bd9Sstevel@tonic-gate fdlim_get(int hard)
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
2217c478bd9Sstevel@tonic-gate 	struct rlimit rlfd;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
2247c478bd9Sstevel@tonic-gate 		return (-1);
2257c478bd9Sstevel@tonic-gate 	if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY)
2267c478bd9Sstevel@tonic-gate 		return 10000;
2277c478bd9Sstevel@tonic-gate 	else
2287c478bd9Sstevel@tonic-gate 		return hard ? rlfd.rlim_max : rlfd.rlim_cur;
2297c478bd9Sstevel@tonic-gate #elif defined (HAVE_SYSCONF)
2307c478bd9Sstevel@tonic-gate 	return sysconf (_SC_OPEN_MAX);
2317c478bd9Sstevel@tonic-gate #else
2327c478bd9Sstevel@tonic-gate 	return 10000;
2337c478bd9Sstevel@tonic-gate #endif
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate static int
fdlim_set(int lim)2377c478bd9Sstevel@tonic-gate fdlim_set(int lim)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
2407c478bd9Sstevel@tonic-gate 	struct rlimit rlfd;
2417c478bd9Sstevel@tonic-gate #endif
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	if (lim <= 0)
2447c478bd9Sstevel@tonic-gate 		return (-1);
2457c478bd9Sstevel@tonic-gate #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
2467c478bd9Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
2477c478bd9Sstevel@tonic-gate 		return (-1);
2487c478bd9Sstevel@tonic-gate 	rlfd.rlim_cur = lim;
2497c478bd9Sstevel@tonic-gate 	if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0)
2507c478bd9Sstevel@tonic-gate 		return (-1);
2517c478bd9Sstevel@tonic-gate #elif defined (HAVE_SETDTABLESIZE)
2527c478bd9Sstevel@tonic-gate 	setdtablesize(lim);
2537c478bd9Sstevel@tonic-gate #endif
2547c478bd9Sstevel@tonic-gate 	return (0);
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate /*
2587c478bd9Sstevel@tonic-gate  * This is an strsep function that returns a null field for adjacent
2597c478bd9Sstevel@tonic-gate  * separators.  This is the same as the 4.4BSD strsep, but different from the
2607c478bd9Sstevel@tonic-gate  * one in the GNU libc.
2617c478bd9Sstevel@tonic-gate  */
2627c478bd9Sstevel@tonic-gate static char *
xstrsep(char ** str,const char * delim)2637c478bd9Sstevel@tonic-gate xstrsep(char **str, const char *delim)
2647c478bd9Sstevel@tonic-gate {
2657c478bd9Sstevel@tonic-gate 	char *s, *e;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	if (!**str)
2687c478bd9Sstevel@tonic-gate 		return (NULL);
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	s = *str;
2717c478bd9Sstevel@tonic-gate 	e = s + strcspn(s, delim);
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	if (*e != '\0')
2747c478bd9Sstevel@tonic-gate 		*e++ = '\0';
2757c478bd9Sstevel@tonic-gate 	*str = e;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	return (s);
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate /*
2817c478bd9Sstevel@tonic-gate  * Get the next non-null token (like GNU strsep).  Strsep() will return a
2827c478bd9Sstevel@tonic-gate  * null token for two adjacent separators, so we may have to loop.
2837c478bd9Sstevel@tonic-gate  */
2847c478bd9Sstevel@tonic-gate static char *
strnnsep(char ** stringp,char * delim)2857c478bd9Sstevel@tonic-gate strnnsep(char **stringp, char *delim)
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate 	char *tok;
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	do {
2907c478bd9Sstevel@tonic-gate 		tok = xstrsep(stringp, delim);
2917c478bd9Sstevel@tonic-gate 	} while (tok && *tok == '\0');
2927c478bd9Sstevel@tonic-gate 	return (tok);
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate static Key *
keygrab_ssh1(con * c)2967c478bd9Sstevel@tonic-gate keygrab_ssh1(con *c)
2977c478bd9Sstevel@tonic-gate {
2987c478bd9Sstevel@tonic-gate 	static Key *rsa;
2997c478bd9Sstevel@tonic-gate 	static Buffer msg;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	if (rsa == NULL) {
3027c478bd9Sstevel@tonic-gate 		buffer_init(&msg);
3037c478bd9Sstevel@tonic-gate 		rsa = key_new(KEY_RSA1);
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 	buffer_append(&msg, c->c_data, c->c_plen);
3067c478bd9Sstevel@tonic-gate 	buffer_consume(&msg, 8 - (c->c_plen & 7));	/* padding */
3077c478bd9Sstevel@tonic-gate 	if (buffer_get_char(&msg) != (int) SSH_SMSG_PUBLIC_KEY) {
3087c478bd9Sstevel@tonic-gate 		error("%s: invalid packet type", c->c_name);
3097c478bd9Sstevel@tonic-gate 		buffer_clear(&msg);
3107c478bd9Sstevel@tonic-gate 		return NULL;
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 	buffer_consume(&msg, 8);		/* cookie */
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	/* server key */
3157c478bd9Sstevel@tonic-gate 	(void) buffer_get_int(&msg);
3167c478bd9Sstevel@tonic-gate 	buffer_get_bignum(&msg, rsa->rsa->e);
3177c478bd9Sstevel@tonic-gate 	buffer_get_bignum(&msg, rsa->rsa->n);
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	/* host key */
3207c478bd9Sstevel@tonic-gate 	(void) buffer_get_int(&msg);
3217c478bd9Sstevel@tonic-gate 	buffer_get_bignum(&msg, rsa->rsa->e);
3227c478bd9Sstevel@tonic-gate 	buffer_get_bignum(&msg, rsa->rsa->n);
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	buffer_clear(&msg);
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	return (rsa);
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate static int
hostjump(Key * hostkey)3307c478bd9Sstevel@tonic-gate hostjump(Key *hostkey)
3317c478bd9Sstevel@tonic-gate {
3327c478bd9Sstevel@tonic-gate 	kexjmp_key = hostkey;
3337c478bd9Sstevel@tonic-gate 	longjmp(kexjmp, 1);
3347c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
3357c478bd9Sstevel@tonic-gate 	return (0);
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate static int
ssh2_capable(int remote_major,int remote_minor)3397c478bd9Sstevel@tonic-gate ssh2_capable(int remote_major, int remote_minor)
3407c478bd9Sstevel@tonic-gate {
3417c478bd9Sstevel@tonic-gate 	switch (remote_major) {
3427c478bd9Sstevel@tonic-gate 	case 1:
3437c478bd9Sstevel@tonic-gate 		if (remote_minor == 99)
3447c478bd9Sstevel@tonic-gate 			return 1;
3457c478bd9Sstevel@tonic-gate 		break;
3467c478bd9Sstevel@tonic-gate 	case 2:
3477c478bd9Sstevel@tonic-gate 		return 1;
3487c478bd9Sstevel@tonic-gate 	default:
3497c478bd9Sstevel@tonic-gate 		break;
3507c478bd9Sstevel@tonic-gate 	}
3517c478bd9Sstevel@tonic-gate 	return 0;
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate static Key *
keygrab_ssh2(con * c)3557c478bd9Sstevel@tonic-gate keygrab_ssh2(con *c)
3567c478bd9Sstevel@tonic-gate {
3577c478bd9Sstevel@tonic-gate 	int j;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	packet_set_connection(c->c_fd, c->c_fd);
3607c478bd9Sstevel@tonic-gate 	enable_compat20();
361*e63a6e29SJan Pechanec 	my_clnt_proposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
362*e63a6e29SJan Pechanec 	    c->c_keytype == KT_DSA? "ssh-dss": "ssh-rsa";
363*e63a6e29SJan Pechanec 	c->c_kex = kex_setup(c->c_name, my_clnt_proposal, NULL);
364cd7d5fafSJan Pechanec 	kex_start(c->c_kex);
3657c478bd9Sstevel@tonic-gate 	c->c_kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
3667c478bd9Sstevel@tonic-gate 	c->c_kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
3677c478bd9Sstevel@tonic-gate 	c->c_kex->verify_host_key = hostjump;
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	if (!(j = setjmp(kexjmp))) {
3707c478bd9Sstevel@tonic-gate 		nonfatal_fatal = 1;
3717c478bd9Sstevel@tonic-gate 		dispatch_run(DISPATCH_BLOCK, &c->c_kex->done, c->c_kex);
3727c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Impossible! dispatch_run() returned!\n");
3737c478bd9Sstevel@tonic-gate 		exit(1);
3747c478bd9Sstevel@tonic-gate 	}
3757c478bd9Sstevel@tonic-gate 	nonfatal_fatal = 0;
3767c478bd9Sstevel@tonic-gate 	xfree(c->c_kex);
3777c478bd9Sstevel@tonic-gate 	c->c_kex = NULL;
3787c478bd9Sstevel@tonic-gate 	packet_close();
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	return j < 0? NULL : kexjmp_key;
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate static void
keyprint(con * c,Key * key)3847c478bd9Sstevel@tonic-gate keyprint(con *c, Key *key)
3857c478bd9Sstevel@tonic-gate {
3867c478bd9Sstevel@tonic-gate 	if (!key)
3877c478bd9Sstevel@tonic-gate 		return;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	fprintf(stdout, "%s ", c->c_output_name ? c->c_output_name : c->c_name);
3907c478bd9Sstevel@tonic-gate 	key_write(key, stdout);
3917c478bd9Sstevel@tonic-gate 	fputs("\n", stdout);
3927c478bd9Sstevel@tonic-gate }
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate static int
tcpconnect(char * host)3957c478bd9Sstevel@tonic-gate tcpconnect(char *host)
3967c478bd9Sstevel@tonic-gate {
3977c478bd9Sstevel@tonic-gate 	struct addrinfo hints, *ai, *aitop;
3987c478bd9Sstevel@tonic-gate 	char strport[NI_MAXSERV];
3997c478bd9Sstevel@tonic-gate 	int gaierr, s = -1;
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	snprintf(strport, sizeof strport, "%d", ssh_port);
4027c478bd9Sstevel@tonic-gate 	memset(&hints, 0, sizeof(hints));
4037c478bd9Sstevel@tonic-gate 	hints.ai_family = IPv4or6;
4047c478bd9Sstevel@tonic-gate 	hints.ai_socktype = SOCK_STREAM;
4057c478bd9Sstevel@tonic-gate 	if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
4067c478bd9Sstevel@tonic-gate 		fatal("getaddrinfo %s: %s", host, gai_strerror(gaierr));
4077c478bd9Sstevel@tonic-gate 	for (ai = aitop; ai; ai = ai->ai_next) {
4087c478bd9Sstevel@tonic-gate 		s = socket(ai->ai_family, SOCK_STREAM, 0);
4097c478bd9Sstevel@tonic-gate 		if (s < 0) {
4107c478bd9Sstevel@tonic-gate 			error("socket: %s", strerror(errno));
4117c478bd9Sstevel@tonic-gate 			continue;
4127c478bd9Sstevel@tonic-gate 		}
4137c478bd9Sstevel@tonic-gate 		if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
4147c478bd9Sstevel@tonic-gate 			fatal("F_SETFL: %s", strerror(errno));
4157c478bd9Sstevel@tonic-gate 		if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 &&
4167c478bd9Sstevel@tonic-gate 		    errno != EINPROGRESS)
4177c478bd9Sstevel@tonic-gate 			error("connect (`%s'): %s", host, strerror(errno));
4187c478bd9Sstevel@tonic-gate 		else
4197c478bd9Sstevel@tonic-gate 			break;
4207c478bd9Sstevel@tonic-gate 		close(s);
4217c478bd9Sstevel@tonic-gate 		s = -1;
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate 	freeaddrinfo(aitop);
4247c478bd9Sstevel@tonic-gate 	return s;
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate static int
conalloc(char * iname,char * oname,int keytype)4287c478bd9Sstevel@tonic-gate conalloc(char *iname, char *oname, int keytype)
4297c478bd9Sstevel@tonic-gate {
4307c478bd9Sstevel@tonic-gate 	char *namebase, *name, *namelist;
4317c478bd9Sstevel@tonic-gate 	int s;
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	namebase = namelist = xstrdup(iname);
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	do {
4367c478bd9Sstevel@tonic-gate 		name = xstrsep(&namelist, ",");
4377c478bd9Sstevel@tonic-gate 		if (!name) {
4387c478bd9Sstevel@tonic-gate 			xfree(namebase);
4397c478bd9Sstevel@tonic-gate 			return (-1);
4407c478bd9Sstevel@tonic-gate 		}
4417c478bd9Sstevel@tonic-gate 	} while ((s = tcpconnect(name)) < 0);
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	if (s >= maxfd)
4447c478bd9Sstevel@tonic-gate 		fatal("conalloc: fdno %d too high", s);
4457c478bd9Sstevel@tonic-gate 	if (fdcon[s].c_status)
4467c478bd9Sstevel@tonic-gate 		fatal("conalloc: attempt to reuse fdno %d", s);
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	fdcon[s].c_fd = s;
4497c478bd9Sstevel@tonic-gate 	fdcon[s].c_status = CS_CON;
4507c478bd9Sstevel@tonic-gate 	fdcon[s].c_namebase = namebase;
4517c478bd9Sstevel@tonic-gate 	fdcon[s].c_name = name;
4527c478bd9Sstevel@tonic-gate 	fdcon[s].c_namelist = namelist;
4537c478bd9Sstevel@tonic-gate 	fdcon[s].c_output_name = xstrdup(oname);
4547c478bd9Sstevel@tonic-gate 	fdcon[s].c_data = (char *) &fdcon[s].c_plen;
4557c478bd9Sstevel@tonic-gate 	fdcon[s].c_len = 4;
4567c478bd9Sstevel@tonic-gate 	fdcon[s].c_off = 0;
4577c478bd9Sstevel@tonic-gate 	fdcon[s].c_keytype = keytype;
4587c478bd9Sstevel@tonic-gate 	gettimeofday(&fdcon[s].c_tv, NULL);
4597c478bd9Sstevel@tonic-gate 	fdcon[s].c_tv.tv_sec += timeout;
4607c478bd9Sstevel@tonic-gate 	TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
4617c478bd9Sstevel@tonic-gate 	FD_SET(s, read_wait);
4627c478bd9Sstevel@tonic-gate 	ncon++;
4637c478bd9Sstevel@tonic-gate 	return (s);
4647c478bd9Sstevel@tonic-gate }
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate static void
confree(int s)4677c478bd9Sstevel@tonic-gate confree(int s)
4687c478bd9Sstevel@tonic-gate {
4697c478bd9Sstevel@tonic-gate 	if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
4707c478bd9Sstevel@tonic-gate 		fatal("confree: attempt to free bad fdno %d", s);
4717c478bd9Sstevel@tonic-gate 	close(s);
4727c478bd9Sstevel@tonic-gate 	xfree(fdcon[s].c_namebase);
4737c478bd9Sstevel@tonic-gate 	xfree(fdcon[s].c_output_name);
4747c478bd9Sstevel@tonic-gate 	if (fdcon[s].c_status == CS_KEYS)
4757c478bd9Sstevel@tonic-gate 		xfree(fdcon[s].c_data);
4767c478bd9Sstevel@tonic-gate 	fdcon[s].c_status = CS_UNUSED;
4777c478bd9Sstevel@tonic-gate 	fdcon[s].c_keytype = 0;
4787c478bd9Sstevel@tonic-gate 	TAILQ_REMOVE(&tq, &fdcon[s], c_link);
4797c478bd9Sstevel@tonic-gate 	FD_CLR(s, read_wait);
4807c478bd9Sstevel@tonic-gate 	ncon--;
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate static void
contouch(int s)4847c478bd9Sstevel@tonic-gate contouch(int s)
4857c478bd9Sstevel@tonic-gate {
4867c478bd9Sstevel@tonic-gate 	TAILQ_REMOVE(&tq, &fdcon[s], c_link);
4877c478bd9Sstevel@tonic-gate 	gettimeofday(&fdcon[s].c_tv, NULL);
4887c478bd9Sstevel@tonic-gate 	fdcon[s].c_tv.tv_sec += timeout;
4897c478bd9Sstevel@tonic-gate 	TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
4907c478bd9Sstevel@tonic-gate }
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate static int
conrecycle(int s)4937c478bd9Sstevel@tonic-gate conrecycle(int s)
4947c478bd9Sstevel@tonic-gate {
4957c478bd9Sstevel@tonic-gate 	con *c = &fdcon[s];
4967c478bd9Sstevel@tonic-gate 	int ret;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype);
4997c478bd9Sstevel@tonic-gate 	confree(s);
5007c478bd9Sstevel@tonic-gate 	return (ret);
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate static void
congreet(int s)5047c478bd9Sstevel@tonic-gate congreet(int s)
5057c478bd9Sstevel@tonic-gate {
5067c478bd9Sstevel@tonic-gate 	int remote_major, remote_minor, n = 0;
5077c478bd9Sstevel@tonic-gate 	char buf[256], *cp;
5087c478bd9Sstevel@tonic-gate 	char remote_version[sizeof buf];
5097c478bd9Sstevel@tonic-gate 	size_t bufsiz;
5107c478bd9Sstevel@tonic-gate 	con *c = &fdcon[s];
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	bufsiz = sizeof(buf);
5137c478bd9Sstevel@tonic-gate 	cp = buf;
5147c478bd9Sstevel@tonic-gate 	while (bufsiz-- && (n = read(s, cp, 1)) == 1 && *cp != '\n') {
5157c478bd9Sstevel@tonic-gate 		if (*cp == '\r')
5167c478bd9Sstevel@tonic-gate 			*cp = '\n';
5177c478bd9Sstevel@tonic-gate 		cp++;
5187c478bd9Sstevel@tonic-gate 	}
5197c478bd9Sstevel@tonic-gate 	if (n < 0) {
5207c478bd9Sstevel@tonic-gate 		if (errno != ECONNREFUSED)
5217c478bd9Sstevel@tonic-gate 			error("read (%s): %s", c->c_name, strerror(errno));
5227c478bd9Sstevel@tonic-gate 		conrecycle(s);
5237c478bd9Sstevel@tonic-gate 		return;
5247c478bd9Sstevel@tonic-gate 	}
5257c478bd9Sstevel@tonic-gate 	if (n == 0) {
5267c478bd9Sstevel@tonic-gate 		error("%s: Connection closed by remote host", c->c_name);
5277c478bd9Sstevel@tonic-gate 		conrecycle(s);
5287c478bd9Sstevel@tonic-gate 		return;
5297c478bd9Sstevel@tonic-gate 	}
5307c478bd9Sstevel@tonic-gate 	if (*cp != '\n' && *cp != '\r') {
5317c478bd9Sstevel@tonic-gate 		error("%s: bad greeting", c->c_name);
5327c478bd9Sstevel@tonic-gate 		confree(s);
5337c478bd9Sstevel@tonic-gate 		return;
5347c478bd9Sstevel@tonic-gate 	}
5357c478bd9Sstevel@tonic-gate 	*cp = '\0';
5367c478bd9Sstevel@tonic-gate 	if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
5377c478bd9Sstevel@tonic-gate 	    &remote_major, &remote_minor, remote_version) == 3)
5387c478bd9Sstevel@tonic-gate 		compat_datafellows(remote_version);
5397c478bd9Sstevel@tonic-gate 	else
5407c478bd9Sstevel@tonic-gate 		datafellows = 0;
5417c478bd9Sstevel@tonic-gate 	if (c->c_keytype != KT_RSA1) {
5427c478bd9Sstevel@tonic-gate 		if (!ssh2_capable(remote_major, remote_minor)) {
5437c478bd9Sstevel@tonic-gate 			debug("%s doesn't support ssh2", c->c_name);
5447c478bd9Sstevel@tonic-gate 			confree(s);
5457c478bd9Sstevel@tonic-gate 			return;
5467c478bd9Sstevel@tonic-gate 		}
5477c478bd9Sstevel@tonic-gate 	} else if (remote_major != 1) {
5487c478bd9Sstevel@tonic-gate 		debug("%s doesn't support ssh1", c->c_name);
5497c478bd9Sstevel@tonic-gate 		confree(s);
5507c478bd9Sstevel@tonic-gate 		return;
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate 	fprintf(stderr, "# %s %s\n", c->c_name, chop(buf));
5537c478bd9Sstevel@tonic-gate 	n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n",
5547c478bd9Sstevel@tonic-gate 	    c->c_keytype == KT_RSA1? PROTOCOL_MAJOR_1 : PROTOCOL_MAJOR_2,
5557c478bd9Sstevel@tonic-gate 	    c->c_keytype == KT_RSA1? PROTOCOL_MINOR_1 : PROTOCOL_MINOR_2);
5567c478bd9Sstevel@tonic-gate 	if (atomicio(write, s, buf, n) != n) {
5577c478bd9Sstevel@tonic-gate 		error("write (%s): %s", c->c_name, strerror(errno));
5587c478bd9Sstevel@tonic-gate 		confree(s);
5597c478bd9Sstevel@tonic-gate 		return;
5607c478bd9Sstevel@tonic-gate 	}
5617c478bd9Sstevel@tonic-gate 	if (c->c_keytype != KT_RSA1) {
5627c478bd9Sstevel@tonic-gate 		keyprint(c, keygrab_ssh2(c));
5637c478bd9Sstevel@tonic-gate 		confree(s);
5647c478bd9Sstevel@tonic-gate 		return;
5657c478bd9Sstevel@tonic-gate 	}
5667c478bd9Sstevel@tonic-gate 	c->c_status = CS_SIZE;
5677c478bd9Sstevel@tonic-gate 	contouch(s);
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate static void
conread(int s)5717c478bd9Sstevel@tonic-gate conread(int s)
5727c478bd9Sstevel@tonic-gate {
5737c478bd9Sstevel@tonic-gate 	con *c = &fdcon[s];
5747c478bd9Sstevel@tonic-gate 	int n;
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	if (c->c_status == CS_CON) {
5777c478bd9Sstevel@tonic-gate 		congreet(s);
5787c478bd9Sstevel@tonic-gate 		return;
5797c478bd9Sstevel@tonic-gate 	}
5807c478bd9Sstevel@tonic-gate 	n = read(s, c->c_data + c->c_off, c->c_len - c->c_off);
5817c478bd9Sstevel@tonic-gate 	if (n < 0) {
5827c478bd9Sstevel@tonic-gate 		error("read (%s): %s", c->c_name, strerror(errno));
5837c478bd9Sstevel@tonic-gate 		confree(s);
5847c478bd9Sstevel@tonic-gate 		return;
5857c478bd9Sstevel@tonic-gate 	}
5867c478bd9Sstevel@tonic-gate 	c->c_off += n;
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	if (c->c_off == c->c_len)
5897c478bd9Sstevel@tonic-gate 		switch (c->c_status) {
5907c478bd9Sstevel@tonic-gate 		case CS_SIZE:
5917c478bd9Sstevel@tonic-gate 			c->c_plen = htonl(c->c_plen);
5927c478bd9Sstevel@tonic-gate 			c->c_len = c->c_plen + 8 - (c->c_plen & 7);
5937c478bd9Sstevel@tonic-gate 			c->c_off = 0;
5947c478bd9Sstevel@tonic-gate 			c->c_data = xmalloc(c->c_len);
5957c478bd9Sstevel@tonic-gate 			c->c_status = CS_KEYS;
5967c478bd9Sstevel@tonic-gate 			break;
5977c478bd9Sstevel@tonic-gate 		case CS_KEYS:
5987c478bd9Sstevel@tonic-gate 			keyprint(c, keygrab_ssh1(c));
5997c478bd9Sstevel@tonic-gate 			confree(s);
6007c478bd9Sstevel@tonic-gate 			return;
6017c478bd9Sstevel@tonic-gate 			break;
6027c478bd9Sstevel@tonic-gate 		default:
6037c478bd9Sstevel@tonic-gate 			fatal("conread: invalid status %d", c->c_status);
6047c478bd9Sstevel@tonic-gate 			break;
6057c478bd9Sstevel@tonic-gate 		}
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	contouch(s);
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate static void
conloop(void)6117c478bd9Sstevel@tonic-gate conloop(void)
6127c478bd9Sstevel@tonic-gate {
6137c478bd9Sstevel@tonic-gate 	struct timeval seltime, now;
6147c478bd9Sstevel@tonic-gate 	fd_set *r, *e;
6157c478bd9Sstevel@tonic-gate 	con *c;
6167c478bd9Sstevel@tonic-gate 	int i;
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	gettimeofday(&now, NULL);
6197c478bd9Sstevel@tonic-gate 	c = TAILQ_FIRST(&tq);
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	if (c && (c->c_tv.tv_sec > now.tv_sec ||
6227c478bd9Sstevel@tonic-gate 	    (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) {
6237c478bd9Sstevel@tonic-gate 		seltime = c->c_tv;
6247c478bd9Sstevel@tonic-gate 		seltime.tv_sec -= now.tv_sec;
6257c478bd9Sstevel@tonic-gate 		seltime.tv_usec -= now.tv_usec;
6267c478bd9Sstevel@tonic-gate 		if (seltime.tv_usec < 0) {
6277c478bd9Sstevel@tonic-gate 			seltime.tv_usec += 1000000;
6287c478bd9Sstevel@tonic-gate 			seltime.tv_sec--;
6297c478bd9Sstevel@tonic-gate 		}
6307c478bd9Sstevel@tonic-gate 	} else
6317c478bd9Sstevel@tonic-gate 		seltime.tv_sec = seltime.tv_usec = 0;
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	r = xmalloc(read_wait_size);
6347c478bd9Sstevel@tonic-gate 	memcpy(r, read_wait, read_wait_size);
6357c478bd9Sstevel@tonic-gate 	e = xmalloc(read_wait_size);
6367c478bd9Sstevel@tonic-gate 	memcpy(e, read_wait, read_wait_size);
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	while (select(maxfd, r, NULL, e, &seltime) == -1 &&
6397c478bd9Sstevel@tonic-gate 	    (errno == EAGAIN || errno == EINTR))
6407c478bd9Sstevel@tonic-gate 		;
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	for (i = 0; i < maxfd; i++) {
6437c478bd9Sstevel@tonic-gate 		if (FD_ISSET(i, e)) {
6447c478bd9Sstevel@tonic-gate 			error("%s: exception!", fdcon[i].c_name);
6457c478bd9Sstevel@tonic-gate 			confree(i);
6467c478bd9Sstevel@tonic-gate 		} else if (FD_ISSET(i, r))
6477c478bd9Sstevel@tonic-gate 			conread(i);
6487c478bd9Sstevel@tonic-gate 	}
6497c478bd9Sstevel@tonic-gate 	xfree(r);
6507c478bd9Sstevel@tonic-gate 	xfree(e);
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	c = TAILQ_FIRST(&tq);
6537c478bd9Sstevel@tonic-gate 	while (c && (c->c_tv.tv_sec < now.tv_sec ||
6547c478bd9Sstevel@tonic-gate 	    (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) {
6557c478bd9Sstevel@tonic-gate 		int s = c->c_fd;
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 		c = TAILQ_NEXT(c, c_link);
6587c478bd9Sstevel@tonic-gate 		conrecycle(s);
6597c478bd9Sstevel@tonic-gate 	}
6607c478bd9Sstevel@tonic-gate }
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate static void
do_host(char * host)6637c478bd9Sstevel@tonic-gate do_host(char *host)
6647c478bd9Sstevel@tonic-gate {
6657c478bd9Sstevel@tonic-gate 	char *name = strnnsep(&host, " \t\n");
6667c478bd9Sstevel@tonic-gate 	int j;
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	if (name == NULL)
6697c478bd9Sstevel@tonic-gate 		return;
6707c478bd9Sstevel@tonic-gate 	for (j = KT_RSA1; j <= KT_RSA; j *= 2) {
6717c478bd9Sstevel@tonic-gate 		if (get_keytypes & j) {
6727c478bd9Sstevel@tonic-gate 			while (ncon >= MAXCON)
6737c478bd9Sstevel@tonic-gate 				conloop();
6747c478bd9Sstevel@tonic-gate 			conalloc(name, *host ? host : name, j);
6757c478bd9Sstevel@tonic-gate 		}
6767c478bd9Sstevel@tonic-gate 	}
6777c478bd9Sstevel@tonic-gate }
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate void
fatal(const char * fmt,...)6807c478bd9Sstevel@tonic-gate fatal(const char *fmt,...)
6817c478bd9Sstevel@tonic-gate {
6827c478bd9Sstevel@tonic-gate 	va_list args;
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	va_start(args, fmt);
6857c478bd9Sstevel@tonic-gate 	do_log(SYSLOG_LEVEL_FATAL, fmt, args);
6867c478bd9Sstevel@tonic-gate 	va_end(args);
6877c478bd9Sstevel@tonic-gate 	if (nonfatal_fatal)
6887c478bd9Sstevel@tonic-gate 		longjmp(kexjmp, -1);
6897c478bd9Sstevel@tonic-gate 	else
6907c478bd9Sstevel@tonic-gate 		fatal_cleanup();
6917c478bd9Sstevel@tonic-gate }
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate static void
usage(void)6947c478bd9Sstevel@tonic-gate usage(void)
6957c478bd9Sstevel@tonic-gate {
6967c478bd9Sstevel@tonic-gate 	fprintf(stderr,
6977c478bd9Sstevel@tonic-gate 		gettext("Usage: %s [-v46] [-p port] [-T timeout] [-f file]\n"
6987c478bd9Sstevel@tonic-gate 			"\t\t   [host | addrlist namelist] [...]\n"),
6997c478bd9Sstevel@tonic-gate 	    __progname);
7007c478bd9Sstevel@tonic-gate 	exit(1);
7017c478bd9Sstevel@tonic-gate }
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)7047c478bd9Sstevel@tonic-gate main(int argc, char **argv)
7057c478bd9Sstevel@tonic-gate {
7067c478bd9Sstevel@tonic-gate 	int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO;
7077c478bd9Sstevel@tonic-gate 	int opt, fopt_count = 0;
7087c478bd9Sstevel@tonic-gate 	char *tname;
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	extern int optind;
7117c478bd9Sstevel@tonic-gate 	extern char *optarg;
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	__progname = get_progname(argv[0]);
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	(void) g11n_setlocale(LC_ALL, "");
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	init_rng();
7187c478bd9Sstevel@tonic-gate 	seed_rng();
7197c478bd9Sstevel@tonic-gate 	TAILQ_INIT(&tq);
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	if (argc <= 1)
7227c478bd9Sstevel@tonic-gate 		usage();
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "v46p:T:t:f:")) != -1) {
7257c478bd9Sstevel@tonic-gate 		switch (opt) {
7267c478bd9Sstevel@tonic-gate 		case 'p':
7277c478bd9Sstevel@tonic-gate 			ssh_port = a2port(optarg);
7287c478bd9Sstevel@tonic-gate 			if (ssh_port == 0) {
7297c478bd9Sstevel@tonic-gate 				fprintf(stderr, gettext("Bad port '%s'\n"),
7307c478bd9Sstevel@tonic-gate 					optarg);
7317c478bd9Sstevel@tonic-gate 				exit(1);
7327c478bd9Sstevel@tonic-gate 			}
7337c478bd9Sstevel@tonic-gate 			break;
7347c478bd9Sstevel@tonic-gate 		case 'T':
7357c478bd9Sstevel@tonic-gate 			timeout = convtime(optarg);
7367c478bd9Sstevel@tonic-gate 			if (timeout == -1 || timeout == 0) {
7377c478bd9Sstevel@tonic-gate 				fprintf(stderr, gettext("Bad timeout '%s'\n"),
7387c478bd9Sstevel@tonic-gate 					optarg);
7397c478bd9Sstevel@tonic-gate 				usage();
7407c478bd9Sstevel@tonic-gate 			}
7417c478bd9Sstevel@tonic-gate 			break;
7427c478bd9Sstevel@tonic-gate 		case 'v':
7437c478bd9Sstevel@tonic-gate 			if (!debug_flag) {
7447c478bd9Sstevel@tonic-gate 				debug_flag = 1;
7457c478bd9Sstevel@tonic-gate 				log_level = SYSLOG_LEVEL_DEBUG1;
7467c478bd9Sstevel@tonic-gate 			}
7477c478bd9Sstevel@tonic-gate 			else if (log_level < SYSLOG_LEVEL_DEBUG3)
7487c478bd9Sstevel@tonic-gate 				log_level++;
7497c478bd9Sstevel@tonic-gate 			else
7507c478bd9Sstevel@tonic-gate 				fatal("Too high debugging level.");
7517c478bd9Sstevel@tonic-gate 			break;
7527c478bd9Sstevel@tonic-gate 		case 'f':
7537c478bd9Sstevel@tonic-gate 			if (strcmp(optarg, "-") == 0)
7547c478bd9Sstevel@tonic-gate 				optarg = NULL;
7557c478bd9Sstevel@tonic-gate 			argv[fopt_count++] = optarg;
7567c478bd9Sstevel@tonic-gate 			break;
7577c478bd9Sstevel@tonic-gate 		case 't':
7587c478bd9Sstevel@tonic-gate 			get_keytypes = 0;
7597c478bd9Sstevel@tonic-gate 			tname = strtok(optarg, ",");
7607c478bd9Sstevel@tonic-gate 			while (tname) {
7617c478bd9Sstevel@tonic-gate 				int type = key_type_from_name(tname);
7627c478bd9Sstevel@tonic-gate 				switch (type) {
7637c478bd9Sstevel@tonic-gate 				case KEY_RSA1:
7647c478bd9Sstevel@tonic-gate 					get_keytypes |= KT_RSA1;
7657c478bd9Sstevel@tonic-gate 					break;
7667c478bd9Sstevel@tonic-gate 				case KEY_DSA:
7677c478bd9Sstevel@tonic-gate 					get_keytypes |= KT_DSA;
7687c478bd9Sstevel@tonic-gate 					break;
7697c478bd9Sstevel@tonic-gate 				case KEY_RSA:
7707c478bd9Sstevel@tonic-gate 					get_keytypes |= KT_RSA;
7717c478bd9Sstevel@tonic-gate 					break;
7727c478bd9Sstevel@tonic-gate 				case KEY_UNSPEC:
7737c478bd9Sstevel@tonic-gate 					fatal("unknown key type %s", tname);
7747c478bd9Sstevel@tonic-gate 				}
7757c478bd9Sstevel@tonic-gate 				tname = strtok(NULL, ",");
7767c478bd9Sstevel@tonic-gate 			}
7777c478bd9Sstevel@tonic-gate 			break;
7787c478bd9Sstevel@tonic-gate 		case '4':
7797c478bd9Sstevel@tonic-gate 			IPv4or6 = AF_INET;
7807c478bd9Sstevel@tonic-gate 			break;
7817c478bd9Sstevel@tonic-gate 		case '6':
7827c478bd9Sstevel@tonic-gate 			IPv4or6 = AF_INET6;
7837c478bd9Sstevel@tonic-gate 			break;
7847c478bd9Sstevel@tonic-gate 		case '?':
7857c478bd9Sstevel@tonic-gate 		default:
7867c478bd9Sstevel@tonic-gate 			usage();
7877c478bd9Sstevel@tonic-gate 		}
7887c478bd9Sstevel@tonic-gate 	}
7897c478bd9Sstevel@tonic-gate 	if (optind == argc && !fopt_count)
7907c478bd9Sstevel@tonic-gate 		usage();
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1);
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	maxfd = fdlim_get(1);
7957c478bd9Sstevel@tonic-gate 	if (maxfd < 0)
7967c478bd9Sstevel@tonic-gate 		fatal("%s: fdlim_get: bad value", __progname);
7977c478bd9Sstevel@tonic-gate 	if (maxfd > MAXMAXFD)
7987c478bd9Sstevel@tonic-gate 		maxfd = MAXMAXFD;
7997c478bd9Sstevel@tonic-gate 	if (MAXCON <= 0)
8007c478bd9Sstevel@tonic-gate 		fatal("%s: not enough file descriptors", __progname);
8017c478bd9Sstevel@tonic-gate 	if (maxfd > fdlim_get(0))
8027c478bd9Sstevel@tonic-gate 		fdlim_set(maxfd);
8037c478bd9Sstevel@tonic-gate 	fdcon = xmalloc(maxfd * sizeof(con));
8047c478bd9Sstevel@tonic-gate 	memset(fdcon, 0, maxfd * sizeof(con));
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	read_wait_size = howmany(maxfd, NFDBITS) * sizeof(fd_mask);
8077c478bd9Sstevel@tonic-gate 	read_wait = xmalloc(read_wait_size);
8087c478bd9Sstevel@tonic-gate 	memset(read_wait, 0, read_wait_size);
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 	if (fopt_count) {
8117c478bd9Sstevel@tonic-gate 		Linebuf *lb;
8127c478bd9Sstevel@tonic-gate 		char *line;
8137c478bd9Sstevel@tonic-gate 		int j;
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 		for (j = 0; j < fopt_count; j++) {
8167c478bd9Sstevel@tonic-gate 			lb = Linebuf_alloc(argv[j], error);
8177c478bd9Sstevel@tonic-gate 			if (!lb)
8187c478bd9Sstevel@tonic-gate 				continue;
8197c478bd9Sstevel@tonic-gate 			while ((line = Linebuf_getline(lb)) != NULL)
8207c478bd9Sstevel@tonic-gate 				do_host(line);
8217c478bd9Sstevel@tonic-gate 			Linebuf_free(lb);
8227c478bd9Sstevel@tonic-gate 		}
8237c478bd9Sstevel@tonic-gate 	}
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	while (optind < argc)
8267c478bd9Sstevel@tonic-gate 		do_host(argv[optind++]);
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	while (ncon > 0)
8297c478bd9Sstevel@tonic-gate 		conloop();
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 	return (0);
8327c478bd9Sstevel@tonic-gate }
833