blob: fe03c145c19f11d61dc44af95053b882011cb455 [file] [log] [blame]
Ben Lindstromb6434ae2000-12-05 01:15:09 +00001/*
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 (for instance by leaving this copyright notice
7 * intact).
8 */
9
10#include "includes.h"
Ben Lindstromd20b8552001-03-05 07:01:18 +000011RCSID("$OpenBSD: ssh-keyscan.c,v 1.18 2001/03/03 06:53:12 deraadt Exp $");
Ben Lindstromb6434ae2000-12-05 01:15:09 +000012
Kevin Steves28a7f262001-02-05 15:43:59 +000013#if defined(HAVE_SYS_QUEUE_H) && !defined(HAVE_BOGUS_SYS_QUEUE_H)
Ben Lindstromb6434ae2000-12-05 01:15:09 +000014#include <sys/queue.h>
Kevin Steves2c65ada2000-12-06 22:25:40 +000015#else
Ben Lindstrom64136352001-02-02 19:03:13 +000016#include "fake-queue.h"
Kevin Steves2c65ada2000-12-06 22:25:40 +000017#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +000018#include <errno.h>
19
20#include <openssl/bn.h>
Ben Lindstromb6434ae2000-12-05 01:15:09 +000021
22#include "xmalloc.h"
23#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000024#include "ssh1.h"
Ben Lindstromb6434ae2000-12-05 01:15:09 +000025#include "key.h"
26#include "buffer.h"
27#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000028#include "log.h"
Ben Lindstromd20b8552001-03-05 07:01:18 +000029#include "atomicio.h"
Ben Lindstromb6434ae2000-12-05 01:15:09 +000030
31static int argno = 1; /* Number of argument currently being parsed */
32
33int family = AF_UNSPEC; /* IPv4, IPv6 or both */
34
Ben Lindstromb6434ae2000-12-05 01:15:09 +000035#define MAXMAXFD 256
36
37/* The number of seconds after which to give up on a TCP connection */
38int timeout = 5;
39
40int maxfd;
Ben Lindstromd20b8552001-03-05 07:01:18 +000041#define MAXCON (maxfd - 10)
Ben Lindstromb6434ae2000-12-05 01:15:09 +000042
Kevin Stevesec84dc12000-12-13 17:45:15 +000043#ifdef HAVE___PROGNAME
44extern char *__progname;
45#else
46char *__progname;
47#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +000048fd_set read_wait;
49int ncon;
50
51/*
52 * Keep a connection structure for each file descriptor. The state
53 * associated with file descriptor n is held in fdcon[n].
54 */
55typedef struct Connection {
Ben Lindstrom46c16222000-12-22 01:43:59 +000056 u_char c_status; /* State of connection on this file desc. */
Ben Lindstromb6434ae2000-12-05 01:15:09 +000057#define CS_UNUSED 0 /* File descriptor unused */
58#define CS_CON 1 /* Waiting to connect/read greeting */
59#define CS_SIZE 2 /* Waiting to read initial packet size */
60#define CS_KEYS 3 /* Waiting to read public key packet */
61 int c_fd; /* Quick lookup: c->c_fd == c - fdcon */
62 int c_plen; /* Packet length field for ssh packet */
63 int c_len; /* Total bytes which must be read. */
64 int c_off; /* Length of data read so far. */
65 char *c_namebase; /* Address to free for c_name and c_namelist */
66 char *c_name; /* Hostname of connection for errors */
67 char *c_namelist; /* Pointer to other possible addresses */
68 char *c_output_name; /* Hostname of connection for output */
69 char *c_data; /* Data read from this fd */
70 struct timeval c_tv; /* Time at which connection gets aborted */
71 TAILQ_ENTRY(Connection) c_link; /* List of connections in timeout order. */
72} con;
73
74TAILQ_HEAD(conlist, Connection) tq; /* Timeout Queue */
75con *fdcon;
76
77/*
78 * This is just a wrapper around fgets() to make it usable.
79 */
80
81/* Stress-test. Increase this later. */
82#define LINEBUF_SIZE 16
83
84typedef struct {
85 char *buf;
Ben Lindstrom46c16222000-12-22 01:43:59 +000086 u_int size;
Ben Lindstromb6434ae2000-12-05 01:15:09 +000087 int lineno;
88 const char *filename;
89 FILE *stream;
90 void (*errfun) (const char *,...);
91} Linebuf;
92
Ben Lindstromb0a4cd82001-03-05 04:54:49 +000093static __inline__ Linebuf *
Ben Lindstromb6434ae2000-12-05 01:15:09 +000094Linebuf_alloc(const char *filename, void (*errfun) (const char *,...))
95{
96 Linebuf *lb;
97
98 if (!(lb = malloc(sizeof(*lb)))) {
99 if (errfun)
100 (*errfun) ("linebuf (%s): malloc failed\n", lb->filename);
101 return (NULL);
102 }
103 if (filename) {
104 lb->filename = filename;
105 if (!(lb->stream = fopen(filename, "r"))) {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000106 xfree(lb);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000107 if (errfun)
108 (*errfun) ("%s: %s\n", filename, strerror(errno));
109 return (NULL);
110 }
111 } else {
112 lb->filename = "(stdin)";
113 lb->stream = stdin;
114 }
115
116 if (!(lb->buf = malloc(lb->size = LINEBUF_SIZE))) {
117 if (errfun)
118 (*errfun) ("linebuf (%s): malloc failed\n", lb->filename);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000119 xfree(lb);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000120 return (NULL);
121 }
122 lb->errfun = errfun;
123 lb->lineno = 0;
124 return (lb);
125}
126
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000127static __inline__ void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000128Linebuf_free(Linebuf * lb)
129{
130 fclose(lb->stream);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000131 xfree(lb->buf);
132 xfree(lb);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000133}
134
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000135static __inline__ void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000136Linebuf_restart(Linebuf * lb)
137{
138 clearerr(lb->stream);
139 rewind(lb->stream);
140 lb->lineno = 0;
141}
142
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000143static __inline__ int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000144Linebuf_lineno(Linebuf * lb)
145{
146 return (lb->lineno);
147}
148
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000149static __inline__ char *
Ben Lindstromc791beb2001-02-10 23:18:11 +0000150Linebuf_getline(Linebuf * lb)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000151{
152 int n = 0;
153
154 lb->lineno++;
155 for (;;) {
156 /* Read a line */
157 if (!fgets(&lb->buf[n], lb->size - n, lb->stream)) {
158 if (ferror(lb->stream) && lb->errfun)
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000159 (*lb->errfun) ("%s: %s\n", lb->filename,
160 strerror(errno));
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000161 return (NULL);
162 }
163 n = strlen(lb->buf);
164
165 /* Return it or an error if it fits */
166 if (n > 0 && lb->buf[n - 1] == '\n') {
167 lb->buf[n - 1] = '\0';
168 return (lb->buf);
169 }
170 if (n != lb->size - 1) {
171 if (lb->errfun)
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000172 (*lb->errfun) ("%s: skipping incomplete last line\n",
173 lb->filename);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000174 return (NULL);
175 }
176 /* Double the buffer if we need more space */
177 if (!(lb->buf = realloc(lb->buf, (lb->size *= 2)))) {
178 if (lb->errfun)
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000179 (*lb->errfun) ("linebuf (%s): realloc failed\n",
180 lb->filename);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000181 return (NULL);
182 }
183 }
184}
185
186static int
187fdlim_get(int hard)
188{
Ben Lindstrom5adbad22000-12-27 07:06:21 +0000189#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000190 struct rlimit rlfd;
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000191
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000192 if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
193 return (-1);
194 if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY)
195 return 10000;
196 else
197 return hard ? rlfd.rlim_max : rlfd.rlim_cur;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000198#elif defined (HAVE_SYSCONF)
199 return sysconf (_SC_OPEN_MAX);
200#else
201 return 10000;
202#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000203}
204
205static int
206fdlim_set(int lim)
207{
Ben Lindstrom5adbad22000-12-27 07:06:21 +0000208#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000209 struct rlimit rlfd;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000210#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000211 if (lim <= 0)
212 return (-1);
Ben Lindstrom5adbad22000-12-27 07:06:21 +0000213#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000214 if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
215 return (-1);
216 rlfd.rlim_cur = lim;
217 if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0)
218 return (-1);
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000219#elif defined (HAVE_SETDTABLESIZE)
Kevin Steves28a7f262001-02-05 15:43:59 +0000220 setdtablesize(lim);
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000221#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000222 return (0);
223}
224
225/*
226 * This is an strsep function that returns a null field for adjacent
227 * separators. This is the same as the 4.4BSD strsep, but different from the
228 * one in the GNU libc.
229 */
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000230static __inline__ char *
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000231xstrsep(char **str, const char *delim)
232{
233 char *s, *e;
234
235 if (!**str)
236 return (NULL);
237
238 s = *str;
239 e = s + strcspn(s, delim);
240
241 if (*e != '\0')
242 *e++ = '\0';
243 *str = e;
244
245 return (s);
246}
247
248/*
249 * Get the next non-null token (like GNU strsep). Strsep() will return a
250 * null token for two adjacent separators, so we may have to loop.
251 */
252char *
253strnnsep(char **stringp, char *delim)
254{
255 char *tok;
256
257 do {
258 tok = xstrsep(stringp, delim);
259 } while (tok && *tok == '\0');
260 return (tok);
261}
262
263void
264keyprint(char *host, char *output_name, char *kd, int len)
265{
266 static Key *rsa;
267 static Buffer msg;
268
269 if (rsa == NULL) {
270 buffer_init(&msg);
271 rsa = key_new(KEY_RSA1);
272 }
273 buffer_append(&msg, kd, len);
274 buffer_consume(&msg, 8 - (len & 7)); /* padding */
275 if (buffer_get_char(&msg) != (int) SSH_SMSG_PUBLIC_KEY) {
276 error("%s: invalid packet type", host);
277 buffer_clear(&msg);
278 return;
279 }
280 buffer_consume(&msg, 8); /* cookie */
281
282 /* server key */
283 (void) buffer_get_int(&msg);
284 buffer_get_bignum(&msg, rsa->rsa->e);
285 buffer_get_bignum(&msg, rsa->rsa->n);
286
287 /* host key */
288 (void) buffer_get_int(&msg);
289 buffer_get_bignum(&msg, rsa->rsa->e);
290 buffer_get_bignum(&msg, rsa->rsa->n);
291 buffer_clear(&msg);
292
293 fprintf(stdout, "%s ", output_name ? output_name : host);
294 key_write(rsa, stdout);
295 fputs("\n", stdout);
296}
297
298int
299tcpconnect(char *host)
300{
301 struct addrinfo hints, *ai, *aitop;
302 char strport[NI_MAXSERV];
303 int gaierr, s = -1;
304
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000305 snprintf(strport, sizeof strport, "%d", SSH_DEFAULT_PORT);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000306 memset(&hints, 0, sizeof(hints));
307 hints.ai_family = family;
308 hints.ai_socktype = SOCK_STREAM;
309 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
310 fatal("getaddrinfo %s: %s", host, gai_strerror(gaierr));
311 for (ai = aitop; ai; ai = ai->ai_next) {
312 s = socket(ai->ai_family, SOCK_STREAM, 0);
313 if (s < 0) {
314 error("socket: %s", strerror(errno));
315 continue;
316 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000317 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000318 fatal("F_SETFL: %s", strerror(errno));
319 if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 &&
320 errno != EINPROGRESS)
321 error("connect (`%s'): %s", host, strerror(errno));
322 else
323 break;
324 close(s);
325 s = -1;
326 }
327 freeaddrinfo(aitop);
328 return s;
329}
330
331int
332conalloc(char *iname, char *oname)
333{
334 int s;
335 char *namebase, *name, *namelist;
336
337 namebase = namelist = xstrdup(iname);
338
339 do {
340 name = xstrsep(&namelist, ",");
341 if (!name) {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000342 xfree(namebase);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000343 return (-1);
344 }
345 } while ((s = tcpconnect(name)) < 0);
346
347 if (s >= maxfd)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000348 fatal("conalloc: fdno %d too high", s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000349 if (fdcon[s].c_status)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000350 fatal("conalloc: attempt to reuse fdno %d", s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000351
352 fdcon[s].c_fd = s;
353 fdcon[s].c_status = CS_CON;
354 fdcon[s].c_namebase = namebase;
355 fdcon[s].c_name = name;
356 fdcon[s].c_namelist = namelist;
357 fdcon[s].c_output_name = xstrdup(oname);
358 fdcon[s].c_data = (char *) &fdcon[s].c_plen;
359 fdcon[s].c_len = 4;
360 fdcon[s].c_off = 0;
361 gettimeofday(&fdcon[s].c_tv, NULL);
362 fdcon[s].c_tv.tv_sec += timeout;
363 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
364 FD_SET(s, &read_wait);
365 ncon++;
366 return (s);
367}
368
369void
370confree(int s)
371{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000372 if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000373 fatal("confree: attempt to free bad fdno %d", s);
Ben Lindstromd20b8552001-03-05 07:01:18 +0000374 close(s);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000375 xfree(fdcon[s].c_namebase);
376 xfree(fdcon[s].c_output_name);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000377 if (fdcon[s].c_status == CS_KEYS)
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000378 xfree(fdcon[s].c_data);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000379 fdcon[s].c_status = CS_UNUSED;
380 TAILQ_REMOVE(&tq, &fdcon[s], c_link);
381 FD_CLR(s, &read_wait);
382 ncon--;
383}
384
385void
386contouch(int s)
387{
388 TAILQ_REMOVE(&tq, &fdcon[s], c_link);
389 gettimeofday(&fdcon[s].c_tv, NULL);
390 fdcon[s].c_tv.tv_sec += timeout;
391 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
392}
393
394int
395conrecycle(int s)
396{
397 int ret;
398 con *c = &fdcon[s];
399 char *iname, *oname;
400
401 iname = xstrdup(c->c_namelist);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000402 oname = xstrdup(c->c_output_name);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000403 confree(s);
404 ret = conalloc(iname, oname);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000405 xfree(iname);
406 xfree(oname);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000407 return (ret);
408}
409
410void
411congreet(int s)
412{
413 char buf[80];
414 int n;
415 con *c = &fdcon[s];
416
417 n = read(s, buf, sizeof(buf));
418 if (n < 0) {
419 if (errno != ECONNREFUSED)
420 error("read (%s): %s", c->c_name, strerror(errno));
421 conrecycle(s);
422 return;
423 }
424 if (buf[n - 1] != '\n') {
425 error("%s: bad greeting", c->c_name);
426 confree(s);
427 return;
428 }
429 buf[n - 1] = '\0';
430 fprintf(stderr, "# %s %s\n", c->c_name, buf);
431 n = snprintf(buf, sizeof buf, "SSH-1.5-OpenSSH-keyscan\r\n");
Ben Lindstromd20b8552001-03-05 07:01:18 +0000432 if (atomicio(write, s, buf, n) != n) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000433 error("write (%s): %s", c->c_name, strerror(errno));
434 confree(s);
435 return;
436 }
437 c->c_status = CS_SIZE;
438 contouch(s);
439}
440
441void
442conread(int s)
443{
444 int n;
445 con *c = &fdcon[s];
446
447 if (c->c_status == CS_CON) {
448 congreet(s);
449 return;
450 }
451 n = read(s, c->c_data + c->c_off, c->c_len - c->c_off);
452 if (n < 0) {
453 error("read (%s): %s", c->c_name, strerror(errno));
454 confree(s);
455 return;
456 }
457 c->c_off += n;
458
459 if (c->c_off == c->c_len)
460 switch (c->c_status) {
461 case CS_SIZE:
462 c->c_plen = htonl(c->c_plen);
463 c->c_len = c->c_plen + 8 - (c->c_plen & 7);
464 c->c_off = 0;
465 c->c_data = xmalloc(c->c_len);
466 c->c_status = CS_KEYS;
467 break;
468 case CS_KEYS:
469 keyprint(c->c_name, c->c_output_name, c->c_data, c->c_plen);
470 confree(s);
471 return;
472 break;
473 default:
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000474 fatal("conread: invalid status %d", c->c_status);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000475 break;
476 }
477
478 contouch(s);
479}
480
481void
482conloop(void)
483{
484 fd_set r, e;
485 struct timeval seltime, now;
486 int i;
487 con *c;
488
489 gettimeofday(&now, NULL);
490 c = tq.tqh_first;
491
Ben Lindstromd20b8552001-03-05 07:01:18 +0000492 if (c && (c->c_tv.tv_sec > now.tv_sec ||
493 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000494 seltime = c->c_tv;
495 seltime.tv_sec -= now.tv_sec;
496 seltime.tv_usec -= now.tv_usec;
Ben Lindstromc791beb2001-02-10 23:18:11 +0000497 if (seltime.tv_usec < 0) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000498 seltime.tv_usec += 1000000;
499 seltime.tv_sec--;
500 }
501 } else
502 seltime.tv_sec = seltime.tv_usec = 0;
503
504 r = e = read_wait;
Ben Lindstromf9452512001-02-15 03:12:08 +0000505 while (select(maxfd, &r, NULL, &e, &seltime) == -1 &&
506 (errno == EAGAIN || errno == EINTR))
507 ;
508
Ben Lindstromd20b8552001-03-05 07:01:18 +0000509 for (i = 0; i < maxfd; i++) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000510 if (FD_ISSET(i, &e)) {
511 error("%s: exception!", fdcon[i].c_name);
512 confree(i);
513 } else if (FD_ISSET(i, &r))
514 conread(i);
Ben Lindstromd20b8552001-03-05 07:01:18 +0000515 }
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000516
517 c = tq.tqh_first;
Ben Lindstromd20b8552001-03-05 07:01:18 +0000518 while (c && (c->c_tv.tv_sec < now.tv_sec ||
519 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000520 int s = c->c_fd;
Ben Lindstromd20b8552001-03-05 07:01:18 +0000521
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000522 c = c->c_link.tqe_next;
523 conrecycle(s);
524 }
525}
526
527char *
528nexthost(int argc, char **argv)
529{
530 static Linebuf *lb;
531
532 for (;;) {
533 if (!lb) {
534 if (argno >= argc)
535 return (NULL);
536 if (argv[argno][0] != '-')
537 return (argv[argno++]);
538 if (!strcmp(argv[argno], "--")) {
539 if (++argno >= argc)
540 return (NULL);
541 return (argv[argno++]);
542 } else if (!strncmp(argv[argno], "-f", 2)) {
543 char *fname;
Ben Lindstromd20b8552001-03-05 07:01:18 +0000544
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000545 if (argv[argno][2])
546 fname = &argv[argno++][2];
547 else if (++argno >= argc) {
548 error("missing filename for `-f'");
549 return (NULL);
550 } else
551 fname = argv[argno++];
552 if (!strcmp(fname, "-"))
553 fname = NULL;
Kevin Stevesfc74af42000-12-06 22:47:55 +0000554 lb = Linebuf_alloc(fname, error);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000555 } else
Ben Lindstromd20b8552001-03-05 07:01:18 +0000556 error("ignoring invalid/misplaced option `%s'",
557 argv[argno++]);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000558 } else {
559 char *line;
Ben Lindstromd20b8552001-03-05 07:01:18 +0000560
Ben Lindstromc791beb2001-02-10 23:18:11 +0000561 line = Linebuf_getline(lb);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000562 if (line)
563 return (line);
564 Linebuf_free(lb);
565 lb = NULL;
566 }
567 }
568}
569
570static void
571usage(void)
572{
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000573 fatal("usage: %s [-t timeout] { [--] host | -f file } ...", __progname);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000574 return;
575}
576
577int
578main(int argc, char **argv)
579{
580 char *host = NULL;
581
Kevin Stevesec84dc12000-12-13 17:45:15 +0000582 __progname = get_progname(argv[0]);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000583 TAILQ_INIT(&tq);
584
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000585 if (argc <= argno)
586 usage();
587
588 if (argv[1][0] == '-' && argv[1][1] == 't') {
589 argno++;
590 if (argv[1][2])
591 timeout = atoi(&argv[1][2]);
592 else {
593 if (argno >= argc)
594 usage();
595 timeout = atoi(argv[argno++]);
596 }
597 if (timeout <= 0)
598 usage();
599 }
600 if (argc <= argno)
601 usage();
602
603 maxfd = fdlim_get(1);
604 if (maxfd < 0)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000605 fatal("%s: fdlim_get: bad value", __progname);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000606 if (maxfd > MAXMAXFD)
607 maxfd = MAXMAXFD;
Ben Lindstromd20b8552001-03-05 07:01:18 +0000608 if (MAXCON <= 0)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000609 fatal("%s: not enough file descriptors", __progname);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000610 if (maxfd > fdlim_get(0))
611 fdlim_set(maxfd);
612 fdcon = xmalloc(maxfd * sizeof(con));
Ben Lindstromc791beb2001-02-10 23:18:11 +0000613 memset(fdcon, 0, maxfd * sizeof(con));
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000614
615 do {
Ben Lindstromd20b8552001-03-05 07:01:18 +0000616 while (ncon < MAXCON) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000617 char *name;
618
619 host = nexthost(argc, argv);
620 if (host == NULL)
621 break;
622 name = strnnsep(&host, " \t\n");
623 conalloc(name, *host ? host : name);
624 }
625 conloop();
626 } while (host);
627 while (ncon > 0)
628 conloop();
629
630 return (0);
631}