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