xref: /titanic_50/usr/src/cmd/ssh/ssh-keyscan/ssh-keyscan.c (revision 275c9da86e89f8abf71135cf63d9fc23671b2e60)
1 /*
2  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 /*
6  * Copyright 1995, 1996 by David Mazieres <dm@lcs.mit.edu>.
7  *
8  * Modification and redistribution in source and binary forms is
9  * permitted provided that due credit is given to the author and the
10  * OpenBSD project by leaving this copyright notice intact.
11  */
12 
13 #include "includes.h"
14 RCSID("$OpenBSD: ssh-keyscan.c,v 1.40 2002/07/06 17:47:58 stevesk Exp $");
15 
16 #pragma ident	"%Z%%M%	%I%	%E% SMI"
17 
18 #include "sys-queue.h"
19 
20 #include <openssl/bn.h>
21 
22 #include <setjmp.h>
23 #include "xmalloc.h"
24 #include "ssh.h"
25 #include "ssh1.h"
26 #include "key.h"
27 #include "kex.h"
28 #include "compat.h"
29 #include "myproposal.h"
30 #include "packet.h"
31 #include "dispatch.h"
32 #include "buffer.h"
33 #include "bufaux.h"
34 #include "log.h"
35 #include "atomicio.h"
36 #include "misc.h"
37 
38 /* Flag indicating whether IPv4 or IPv6.  This can be set on the command line.
39    Default value is AF_UNSPEC means both IPv4 and IPv6. */
40 #ifdef IPV4_DEFAULT
41 int IPv4or6 = AF_INET;
42 #else
43 int IPv4or6 = AF_UNSPEC;
44 #endif
45 
46 int ssh_port = SSH_DEFAULT_PORT;
47 
48 #define KT_RSA1	1
49 #define KT_DSA	2
50 #define KT_RSA	4
51 
52 int get_keytypes = KT_RSA1;	/* Get only RSA1 keys by default */
53 
54 #define MAXMAXFD 256
55 
56 /* The number of seconds after which to give up on a TCP connection */
57 int timeout = 5;
58 
59 int maxfd;
60 #define MAXCON (maxfd - 10)
61 
62 #ifdef HAVE___PROGNAME
63 extern char *__progname;
64 #else
65 char *__progname;
66 #endif
67 fd_set *read_wait;
68 size_t read_wait_size;
69 int ncon;
70 int nonfatal_fatal = 0;
71 jmp_buf kexjmp;
72 Key *kexjmp_key;
73 
74 /*
75  * Keep a connection structure for each file descriptor.  The state
76  * associated with file descriptor n is held in fdcon[n].
77  */
78 typedef struct Connection {
79 	u_char c_status;	/* State of connection on this file desc. */
80 #define CS_UNUSED 0		/* File descriptor unused */
81 #define CS_CON 1		/* Waiting to connect/read greeting */
82 #define CS_SIZE 2		/* Waiting to read initial packet size */
83 #define CS_KEYS 3		/* Waiting to read public key packet */
84 	int c_fd;		/* Quick lookup: c->c_fd == c - fdcon */
85 	int c_plen;		/* Packet length field for ssh packet */
86 	int c_len;		/* Total bytes which must be read. */
87 	int c_off;		/* Length of data read so far. */
88 	int c_keytype;		/* Only one of KT_RSA1, KT_DSA, or KT_RSA */
89 	char *c_namebase;	/* Address to free for c_name and c_namelist */
90 	char *c_name;		/* Hostname of connection for errors */
91 	char *c_namelist;	/* Pointer to other possible addresses */
92 	char *c_output_name;	/* Hostname of connection for output */
93 	char *c_data;		/* Data read from this fd */
94 	Kex *c_kex;		/* The key-exchange struct for ssh2 */
95 	struct timeval c_tv;	/* Time at which connection gets aborted */
96 	TAILQ_ENTRY(Connection) c_link;	/* List of connections in timeout order. */
97 } con;
98 
99 TAILQ_HEAD(conlist, Connection) tq;	/* Timeout Queue */
100 con *fdcon;
101 
102 /*
103  *  This is just a wrapper around fgets() to make it usable.
104  */
105 
106 /* Stress-test.  Increase this later. */
107 #define LINEBUF_SIZE 16
108 
109 typedef struct {
110 	char *buf;
111 	u_int size;
112 	int lineno;
113 	const char *filename;
114 	FILE *stream;
115 	void (*errfun) (const char *,...);
116 } Linebuf;
117 
118 static Linebuf *
119 Linebuf_alloc(const char *filename, void (*errfun) (const char *,...))
120 {
121 	Linebuf *lb;
122 
123 	if (!(lb = malloc(sizeof(*lb)))) {
124 		if (errfun)
125 			(*errfun) ("linebuf (%s): malloc failed\n",
126 			    filename ? filename : "(stdin)");
127 		return (NULL);
128 	}
129 	if (filename) {
130 		lb->filename = filename;
131 		if (!(lb->stream = fopen(filename, "r"))) {
132 			xfree(lb);
133 			if (errfun)
134 				(*errfun) ("%s: %s\n", filename, strerror(errno));
135 			return (NULL);
136 		}
137 	} else {
138 		lb->filename = "(stdin)";
139 		lb->stream = stdin;
140 	}
141 
142 	if (!(lb->buf = malloc(lb->size = LINEBUF_SIZE))) {
143 		if (errfun)
144 			(*errfun) ("linebuf (%s): malloc failed\n", lb->filename);
145 		xfree(lb);
146 		return (NULL);
147 	}
148 	lb->errfun = errfun;
149 	lb->lineno = 0;
150 	return (lb);
151 }
152 
153 static void
154 Linebuf_free(Linebuf * lb)
155 {
156 	fclose(lb->stream);
157 	xfree(lb->buf);
158 	xfree(lb);
159 }
160 
161 #if 0
162 static void
163 Linebuf_restart(Linebuf * lb)
164 {
165 	clearerr(lb->stream);
166 	rewind(lb->stream);
167 	lb->lineno = 0;
168 }
169 
170 static int
171 Linebuf_lineno(Linebuf * lb)
172 {
173 	return (lb->lineno);
174 }
175 #endif
176 
177 static char *
178 Linebuf_getline(Linebuf * lb)
179 {
180 	int n = 0;
181 	void *p;
182 
183 	lb->lineno++;
184 	for (;;) {
185 		/* Read a line */
186 		if (!fgets(&lb->buf[n], lb->size - n, lb->stream)) {
187 			if (ferror(lb->stream) && lb->errfun)
188 				(*lb->errfun)("%s: %s\n", lb->filename,
189 				    strerror(errno));
190 			return (NULL);
191 		}
192 		n = strlen(lb->buf);
193 
194 		/* Return it or an error if it fits */
195 		if (n > 0 && lb->buf[n - 1] == '\n') {
196 			lb->buf[n - 1] = '\0';
197 			return (lb->buf);
198 		}
199 		if (n != lb->size - 1) {
200 			if (lb->errfun)
201 				(*lb->errfun)("%s: skipping incomplete last line\n",
202 				    lb->filename);
203 			return (NULL);
204 		}
205 		/* Double the buffer if we need more space */
206 		lb->size *= 2;
207 		if ((p = realloc(lb->buf, lb->size)) == NULL) {
208 			lb->size /= 2;
209 			if (lb->errfun)
210 				(*lb->errfun)("linebuf (%s): realloc failed\n",
211 				    lb->filename);
212 			return (NULL);
213 		}
214 		lb->buf = p;
215 	}
216 }
217 
218 static int
219 fdlim_get(int hard)
220 {
221 #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
222 	struct rlimit rlfd;
223 
224 	if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
225 		return (-1);
226 	if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY)
227 		return 10000;
228 	else
229 		return hard ? rlfd.rlim_max : rlfd.rlim_cur;
230 #elif defined (HAVE_SYSCONF)
231 	return sysconf (_SC_OPEN_MAX);
232 #else
233 	return 10000;
234 #endif
235 }
236 
237 static int
238 fdlim_set(int lim)
239 {
240 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
241 	struct rlimit rlfd;
242 #endif
243 
244 	if (lim <= 0)
245 		return (-1);
246 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
247 	if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
248 		return (-1);
249 	rlfd.rlim_cur = lim;
250 	if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0)
251 		return (-1);
252 #elif defined (HAVE_SETDTABLESIZE)
253 	setdtablesize(lim);
254 #endif
255 	return (0);
256 }
257 
258 /*
259  * This is an strsep function that returns a null field for adjacent
260  * separators.  This is the same as the 4.4BSD strsep, but different from the
261  * one in the GNU libc.
262  */
263 static char *
264 xstrsep(char **str, const char *delim)
265 {
266 	char *s, *e;
267 
268 	if (!**str)
269 		return (NULL);
270 
271 	s = *str;
272 	e = s + strcspn(s, delim);
273 
274 	if (*e != '\0')
275 		*e++ = '\0';
276 	*str = e;
277 
278 	return (s);
279 }
280 
281 /*
282  * Get the next non-null token (like GNU strsep).  Strsep() will return a
283  * null token for two adjacent separators, so we may have to loop.
284  */
285 static char *
286 strnnsep(char **stringp, char *delim)
287 {
288 	char *tok;
289 
290 	do {
291 		tok = xstrsep(stringp, delim);
292 	} while (tok && *tok == '\0');
293 	return (tok);
294 }
295 
296 static Key *
297 keygrab_ssh1(con *c)
298 {
299 	static Key *rsa;
300 	static Buffer msg;
301 
302 	if (rsa == NULL) {
303 		buffer_init(&msg);
304 		rsa = key_new(KEY_RSA1);
305 	}
306 	buffer_append(&msg, c->c_data, c->c_plen);
307 	buffer_consume(&msg, 8 - (c->c_plen & 7));	/* padding */
308 	if (buffer_get_char(&msg) != (int) SSH_SMSG_PUBLIC_KEY) {
309 		error("%s: invalid packet type", c->c_name);
310 		buffer_clear(&msg);
311 		return NULL;
312 	}
313 	buffer_consume(&msg, 8);		/* cookie */
314 
315 	/* server key */
316 	(void) buffer_get_int(&msg);
317 	buffer_get_bignum(&msg, rsa->rsa->e);
318 	buffer_get_bignum(&msg, rsa->rsa->n);
319 
320 	/* host key */
321 	(void) buffer_get_int(&msg);
322 	buffer_get_bignum(&msg, rsa->rsa->e);
323 	buffer_get_bignum(&msg, rsa->rsa->n);
324 
325 	buffer_clear(&msg);
326 
327 	return (rsa);
328 }
329 
330 static int
331 hostjump(Key *hostkey)
332 {
333 	kexjmp_key = hostkey;
334 	longjmp(kexjmp, 1);
335 	/* NOTREACHED */
336 	return (0);
337 }
338 
339 static int
340 ssh2_capable(int remote_major, int remote_minor)
341 {
342 	switch (remote_major) {
343 	case 1:
344 		if (remote_minor == 99)
345 			return 1;
346 		break;
347 	case 2:
348 		return 1;
349 	default:
350 		break;
351 	}
352 	return 0;
353 }
354 
355 static Key *
356 keygrab_ssh2(con *c)
357 {
358 	int j;
359 
360 	packet_set_connection(c->c_fd, c->c_fd);
361 	enable_compat20();
362 	myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = c->c_keytype == KT_DSA?
363 	    "ssh-dss": "ssh-rsa";
364 	c->c_kex = kex_setup(c->c_name, myproposal, NULL);
365 	c->c_kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
366 	c->c_kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
367 	c->c_kex->verify_host_key = hostjump;
368 
369 	if (!(j = setjmp(kexjmp))) {
370 		nonfatal_fatal = 1;
371 		dispatch_run(DISPATCH_BLOCK, &c->c_kex->done, c->c_kex);
372 		fprintf(stderr, "Impossible! dispatch_run() returned!\n");
373 		exit(1);
374 	}
375 	nonfatal_fatal = 0;
376 	xfree(c->c_kex);
377 	c->c_kex = NULL;
378 	packet_close();
379 
380 	return j < 0? NULL : kexjmp_key;
381 }
382 
383 static void
384 keyprint(con *c, Key *key)
385 {
386 	if (!key)
387 		return;
388 
389 	fprintf(stdout, "%s ", c->c_output_name ? c->c_output_name : c->c_name);
390 	key_write(key, stdout);
391 	fputs("\n", stdout);
392 }
393 
394 static int
395 tcpconnect(char *host)
396 {
397 	struct addrinfo hints, *ai, *aitop;
398 	char strport[NI_MAXSERV];
399 	int gaierr, s = -1;
400 
401 	snprintf(strport, sizeof strport, "%d", ssh_port);
402 	memset(&hints, 0, sizeof(hints));
403 	hints.ai_family = IPv4or6;
404 	hints.ai_socktype = SOCK_STREAM;
405 	if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
406 		fatal("getaddrinfo %s: %s", host, gai_strerror(gaierr));
407 	for (ai = aitop; ai; ai = ai->ai_next) {
408 		s = socket(ai->ai_family, SOCK_STREAM, 0);
409 		if (s < 0) {
410 			error("socket: %s", strerror(errno));
411 			continue;
412 		}
413 		if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
414 			fatal("F_SETFL: %s", strerror(errno));
415 		if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 &&
416 		    errno != EINPROGRESS)
417 			error("connect (`%s'): %s", host, strerror(errno));
418 		else
419 			break;
420 		close(s);
421 		s = -1;
422 	}
423 	freeaddrinfo(aitop);
424 	return s;
425 }
426 
427 static int
428 conalloc(char *iname, char *oname, int keytype)
429 {
430 	char *namebase, *name, *namelist;
431 	int s;
432 
433 	namebase = namelist = xstrdup(iname);
434 
435 	do {
436 		name = xstrsep(&namelist, ",");
437 		if (!name) {
438 			xfree(namebase);
439 			return (-1);
440 		}
441 	} while ((s = tcpconnect(name)) < 0);
442 
443 	if (s >= maxfd)
444 		fatal("conalloc: fdno %d too high", s);
445 	if (fdcon[s].c_status)
446 		fatal("conalloc: attempt to reuse fdno %d", s);
447 
448 	fdcon[s].c_fd = s;
449 	fdcon[s].c_status = CS_CON;
450 	fdcon[s].c_namebase = namebase;
451 	fdcon[s].c_name = name;
452 	fdcon[s].c_namelist = namelist;
453 	fdcon[s].c_output_name = xstrdup(oname);
454 	fdcon[s].c_data = (char *) &fdcon[s].c_plen;
455 	fdcon[s].c_len = 4;
456 	fdcon[s].c_off = 0;
457 	fdcon[s].c_keytype = keytype;
458 	gettimeofday(&fdcon[s].c_tv, NULL);
459 	fdcon[s].c_tv.tv_sec += timeout;
460 	TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
461 	FD_SET(s, read_wait);
462 	ncon++;
463 	return (s);
464 }
465 
466 static void
467 confree(int s)
468 {
469 	if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
470 		fatal("confree: attempt to free bad fdno %d", s);
471 	close(s);
472 	xfree(fdcon[s].c_namebase);
473 	xfree(fdcon[s].c_output_name);
474 	if (fdcon[s].c_status == CS_KEYS)
475 		xfree(fdcon[s].c_data);
476 	fdcon[s].c_status = CS_UNUSED;
477 	fdcon[s].c_keytype = 0;
478 	TAILQ_REMOVE(&tq, &fdcon[s], c_link);
479 	FD_CLR(s, read_wait);
480 	ncon--;
481 }
482 
483 static void
484 contouch(int s)
485 {
486 	TAILQ_REMOVE(&tq, &fdcon[s], c_link);
487 	gettimeofday(&fdcon[s].c_tv, NULL);
488 	fdcon[s].c_tv.tv_sec += timeout;
489 	TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
490 }
491 
492 static int
493 conrecycle(int s)
494 {
495 	con *c = &fdcon[s];
496 	int ret;
497 
498 	ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype);
499 	confree(s);
500 	return (ret);
501 }
502 
503 static void
504 congreet(int s)
505 {
506 	int remote_major, remote_minor, n = 0;
507 	char buf[256], *cp;
508 	char remote_version[sizeof buf];
509 	size_t bufsiz;
510 	con *c = &fdcon[s];
511 
512 	bufsiz = sizeof(buf);
513 	cp = buf;
514 	while (bufsiz-- && (n = read(s, cp, 1)) == 1 && *cp != '\n') {
515 		if (*cp == '\r')
516 			*cp = '\n';
517 		cp++;
518 	}
519 	if (n < 0) {
520 		if (errno != ECONNREFUSED)
521 			error("read (%s): %s", c->c_name, strerror(errno));
522 		conrecycle(s);
523 		return;
524 	}
525 	if (n == 0) {
526 		error("%s: Connection closed by remote host", c->c_name);
527 		conrecycle(s);
528 		return;
529 	}
530 	if (*cp != '\n' && *cp != '\r') {
531 		error("%s: bad greeting", c->c_name);
532 		confree(s);
533 		return;
534 	}
535 	*cp = '\0';
536 	if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
537 	    &remote_major, &remote_minor, remote_version) == 3)
538 		compat_datafellows(remote_version);
539 	else
540 		datafellows = 0;
541 	if (c->c_keytype != KT_RSA1) {
542 		if (!ssh2_capable(remote_major, remote_minor)) {
543 			debug("%s doesn't support ssh2", c->c_name);
544 			confree(s);
545 			return;
546 		}
547 	} else if (remote_major != 1) {
548 		debug("%s doesn't support ssh1", c->c_name);
549 		confree(s);
550 		return;
551 	}
552 	fprintf(stderr, "# %s %s\n", c->c_name, chop(buf));
553 	n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n",
554 	    c->c_keytype == KT_RSA1? PROTOCOL_MAJOR_1 : PROTOCOL_MAJOR_2,
555 	    c->c_keytype == KT_RSA1? PROTOCOL_MINOR_1 : PROTOCOL_MINOR_2);
556 	if (atomicio(write, s, buf, n) != n) {
557 		error("write (%s): %s", c->c_name, strerror(errno));
558 		confree(s);
559 		return;
560 	}
561 	if (c->c_keytype != KT_RSA1) {
562 		keyprint(c, keygrab_ssh2(c));
563 		confree(s);
564 		return;
565 	}
566 	c->c_status = CS_SIZE;
567 	contouch(s);
568 }
569 
570 static void
571 conread(int s)
572 {
573 	con *c = &fdcon[s];
574 	int n;
575 
576 	if (c->c_status == CS_CON) {
577 		congreet(s);
578 		return;
579 	}
580 	n = read(s, c->c_data + c->c_off, c->c_len - c->c_off);
581 	if (n < 0) {
582 		error("read (%s): %s", c->c_name, strerror(errno));
583 		confree(s);
584 		return;
585 	}
586 	c->c_off += n;
587 
588 	if (c->c_off == c->c_len)
589 		switch (c->c_status) {
590 		case CS_SIZE:
591 			c->c_plen = htonl(c->c_plen);
592 			c->c_len = c->c_plen + 8 - (c->c_plen & 7);
593 			c->c_off = 0;
594 			c->c_data = xmalloc(c->c_len);
595 			c->c_status = CS_KEYS;
596 			break;
597 		case CS_KEYS:
598 			keyprint(c, keygrab_ssh1(c));
599 			confree(s);
600 			return;
601 			break;
602 		default:
603 			fatal("conread: invalid status %d", c->c_status);
604 			break;
605 		}
606 
607 	contouch(s);
608 }
609 
610 static void
611 conloop(void)
612 {
613 	struct timeval seltime, now;
614 	fd_set *r, *e;
615 	con *c;
616 	int i;
617 
618 	gettimeofday(&now, NULL);
619 	c = TAILQ_FIRST(&tq);
620 
621 	if (c && (c->c_tv.tv_sec > now.tv_sec ||
622 	    (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) {
623 		seltime = c->c_tv;
624 		seltime.tv_sec -= now.tv_sec;
625 		seltime.tv_usec -= now.tv_usec;
626 		if (seltime.tv_usec < 0) {
627 			seltime.tv_usec += 1000000;
628 			seltime.tv_sec--;
629 		}
630 	} else
631 		seltime.tv_sec = seltime.tv_usec = 0;
632 
633 	r = xmalloc(read_wait_size);
634 	memcpy(r, read_wait, read_wait_size);
635 	e = xmalloc(read_wait_size);
636 	memcpy(e, read_wait, read_wait_size);
637 
638 	while (select(maxfd, r, NULL, e, &seltime) == -1 &&
639 	    (errno == EAGAIN || errno == EINTR))
640 		;
641 
642 	for (i = 0; i < maxfd; i++) {
643 		if (FD_ISSET(i, e)) {
644 			error("%s: exception!", fdcon[i].c_name);
645 			confree(i);
646 		} else if (FD_ISSET(i, r))
647 			conread(i);
648 	}
649 	xfree(r);
650 	xfree(e);
651 
652 	c = TAILQ_FIRST(&tq);
653 	while (c && (c->c_tv.tv_sec < now.tv_sec ||
654 	    (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) {
655 		int s = c->c_fd;
656 
657 		c = TAILQ_NEXT(c, c_link);
658 		conrecycle(s);
659 	}
660 }
661 
662 static void
663 do_host(char *host)
664 {
665 	char *name = strnnsep(&host, " \t\n");
666 	int j;
667 
668 	if (name == NULL)
669 		return;
670 	for (j = KT_RSA1; j <= KT_RSA; j *= 2) {
671 		if (get_keytypes & j) {
672 			while (ncon >= MAXCON)
673 				conloop();
674 			conalloc(name, *host ? host : name, j);
675 		}
676 	}
677 }
678 
679 void
680 fatal(const char *fmt,...)
681 {
682 	va_list args;
683 
684 	va_start(args, fmt);
685 	do_log(SYSLOG_LEVEL_FATAL, fmt, args);
686 	va_end(args);
687 	if (nonfatal_fatal)
688 		longjmp(kexjmp, -1);
689 	else
690 		fatal_cleanup();
691 }
692 
693 static void
694 usage(void)
695 {
696 	fprintf(stderr,
697 		gettext("Usage: %s [-v46] [-p port] [-T timeout] [-f file]\n"
698 			"\t\t   [host | addrlist namelist] [...]\n"),
699 	    __progname);
700 	exit(1);
701 }
702 
703 int
704 main(int argc, char **argv)
705 {
706 	int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO;
707 	int opt, fopt_count = 0;
708 	char *tname;
709 
710 	extern int optind;
711 	extern char *optarg;
712 
713 	__progname = get_progname(argv[0]);
714 
715 	(void) g11n_setlocale(LC_ALL, "");
716 
717 	init_rng();
718 	seed_rng();
719 	TAILQ_INIT(&tq);
720 
721 	if (argc <= 1)
722 		usage();
723 
724 	while ((opt = getopt(argc, argv, "v46p:T:t:f:")) != -1) {
725 		switch (opt) {
726 		case 'p':
727 			ssh_port = a2port(optarg);
728 			if (ssh_port == 0) {
729 				fprintf(stderr, gettext("Bad port '%s'\n"),
730 					optarg);
731 				exit(1);
732 			}
733 			break;
734 		case 'T':
735 			timeout = convtime(optarg);
736 			if (timeout == -1 || timeout == 0) {
737 				fprintf(stderr, gettext("Bad timeout '%s'\n"),
738 					optarg);
739 				usage();
740 			}
741 			break;
742 		case 'v':
743 			if (!debug_flag) {
744 				debug_flag = 1;
745 				log_level = SYSLOG_LEVEL_DEBUG1;
746 			}
747 			else if (log_level < SYSLOG_LEVEL_DEBUG3)
748 				log_level++;
749 			else
750 				fatal("Too high debugging level.");
751 			break;
752 		case 'f':
753 			if (strcmp(optarg, "-") == 0)
754 				optarg = NULL;
755 			argv[fopt_count++] = optarg;
756 			break;
757 		case 't':
758 			get_keytypes = 0;
759 			tname = strtok(optarg, ",");
760 			while (tname) {
761 				int type = key_type_from_name(tname);
762 				switch (type) {
763 				case KEY_RSA1:
764 					get_keytypes |= KT_RSA1;
765 					break;
766 				case KEY_DSA:
767 					get_keytypes |= KT_DSA;
768 					break;
769 				case KEY_RSA:
770 					get_keytypes |= KT_RSA;
771 					break;
772 				case KEY_UNSPEC:
773 					fatal("unknown key type %s", tname);
774 				}
775 				tname = strtok(NULL, ",");
776 			}
777 			break;
778 		case '4':
779 			IPv4or6 = AF_INET;
780 			break;
781 		case '6':
782 			IPv4or6 = AF_INET6;
783 			break;
784 		case '?':
785 		default:
786 			usage();
787 		}
788 	}
789 	if (optind == argc && !fopt_count)
790 		usage();
791 
792 	log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1);
793 
794 	maxfd = fdlim_get(1);
795 	if (maxfd < 0)
796 		fatal("%s: fdlim_get: bad value", __progname);
797 	if (maxfd > MAXMAXFD)
798 		maxfd = MAXMAXFD;
799 	if (MAXCON <= 0)
800 		fatal("%s: not enough file descriptors", __progname);
801 	if (maxfd > fdlim_get(0))
802 		fdlim_set(maxfd);
803 	fdcon = xmalloc(maxfd * sizeof(con));
804 	memset(fdcon, 0, maxfd * sizeof(con));
805 
806 	read_wait_size = howmany(maxfd, NFDBITS) * sizeof(fd_mask);
807 	read_wait = xmalloc(read_wait_size);
808 	memset(read_wait, 0, read_wait_size);
809 
810 	if (fopt_count) {
811 		Linebuf *lb;
812 		char *line;
813 		int j;
814 
815 		for (j = 0; j < fopt_count; j++) {
816 			lb = Linebuf_alloc(argv[j], error);
817 			if (!lb)
818 				continue;
819 			while ((line = Linebuf_getline(lb)) != NULL)
820 				do_host(line);
821 			Linebuf_free(lb);
822 		}
823 	}
824 
825 	while (optind < argc)
826 		do_host(argv[optind++]);
827 
828 	while (ncon > 0)
829 		conloop();
830 
831 	return (0);
832 }
833