blob: 9f7aa94aaa938c9e22a442be0a31e6db452e676e [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
Ben Lindstroma238f6e2001-06-09 01:30:39 +00006 * OpenBSD project by leaving this copyright notice intact.
Ben Lindstromb6434ae2000-12-05 01:15:09 +00007 */
8
9#include "includes.h"
Ben Lindstrombba81212001-06-25 05:01:22 +000010RCSID("$OpenBSD: ssh-keyscan.c,v 1.24 2001/06/23 15:12:20 itojun Exp $");
Ben Lindstromb6434ae2000-12-05 01:15:09 +000011
Kevin Steves28a7f262001-02-05 15:43:59 +000012#if defined(HAVE_SYS_QUEUE_H) && !defined(HAVE_BOGUS_SYS_QUEUE_H)
Ben Lindstromb6434ae2000-12-05 01:15:09 +000013#include <sys/queue.h>
Kevin Steves2c65ada2000-12-06 22:25:40 +000014#else
Kevin Steves54f15b62001-03-14 18:37:13 +000015#include "openbsd-compat/fake-queue.h"
Kevin Steves2c65ada2000-12-06 22:25:40 +000016#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +000017#include <errno.h>
18
19#include <openssl/bn.h>
Ben Lindstromb6434ae2000-12-05 01:15:09 +000020
21#include "xmalloc.h"
22#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000023#include "ssh1.h"
Ben Lindstromb6434ae2000-12-05 01:15:09 +000024#include "key.h"
25#include "buffer.h"
26#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000027#include "log.h"
Ben Lindstromd20b8552001-03-05 07:01:18 +000028#include "atomicio.h"
Ben Lindstromb6434ae2000-12-05 01:15:09 +000029
30static int argno = 1; /* Number of argument currently being parsed */
31
32int family = AF_UNSPEC; /* IPv4, IPv6 or both */
33
Ben Lindstromb6434ae2000-12-05 01:15:09 +000034#define MAXMAXFD 256
35
36/* The number of seconds after which to give up on a TCP connection */
37int timeout = 5;
38
39int maxfd;
Ben Lindstromd20b8552001-03-05 07:01:18 +000040#define MAXCON (maxfd - 10)
Ben Lindstromb6434ae2000-12-05 01:15:09 +000041
Kevin Stevesec84dc12000-12-13 17:45:15 +000042#ifdef HAVE___PROGNAME
43extern char *__progname;
44#else
45char *__progname;
46#endif
Ben Lindstromc1e04212001-03-05 07:04:38 +000047fd_set *read_wait;
48size_t read_wait_size;
Ben Lindstromb6434ae2000-12-05 01:15:09 +000049int 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 Lindstrombba81212001-06-25 05:01:22 +000093static 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 Lindstrombba81212001-06-25 05:01:22 +0000127static 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 Lindstrombba81212001-06-25 05:01:22 +0000135#if 0
136static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000137Linebuf_restart(Linebuf * lb)
138{
139 clearerr(lb->stream);
140 rewind(lb->stream);
141 lb->lineno = 0;
142}
143
Ben Lindstrombba81212001-06-25 05:01:22 +0000144static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000145Linebuf_lineno(Linebuf * lb)
146{
147 return (lb->lineno);
148}
Ben Lindstrombba81212001-06-25 05:01:22 +0000149#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000150
Ben Lindstrombba81212001-06-25 05:01:22 +0000151static char *
Ben Lindstromc791beb2001-02-10 23:18:11 +0000152Linebuf_getline(Linebuf * lb)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000153{
154 int n = 0;
155
156 lb->lineno++;
157 for (;;) {
158 /* Read a line */
159 if (!fgets(&lb->buf[n], lb->size - n, lb->stream)) {
160 if (ferror(lb->stream) && lb->errfun)
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000161 (*lb->errfun) ("%s: %s\n", lb->filename,
162 strerror(errno));
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000163 return (NULL);
164 }
165 n = strlen(lb->buf);
166
167 /* Return it or an error if it fits */
168 if (n > 0 && lb->buf[n - 1] == '\n') {
169 lb->buf[n - 1] = '\0';
170 return (lb->buf);
171 }
172 if (n != lb->size - 1) {
173 if (lb->errfun)
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000174 (*lb->errfun) ("%s: skipping incomplete last line\n",
175 lb->filename);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000176 return (NULL);
177 }
178 /* Double the buffer if we need more space */
179 if (!(lb->buf = realloc(lb->buf, (lb->size *= 2)))) {
180 if (lb->errfun)
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000181 (*lb->errfun) ("linebuf (%s): realloc failed\n",
182 lb->filename);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000183 return (NULL);
184 }
185 }
186}
187
Ben Lindstrombba81212001-06-25 05:01:22 +0000188static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000189fdlim_get(int hard)
190{
Ben Lindstrom5adbad22000-12-27 07:06:21 +0000191#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000192 struct rlimit rlfd;
Ben Lindstromb0a4cd82001-03-05 04:54:49 +0000193
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000194 if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
195 return (-1);
196 if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY)
197 return 10000;
198 else
199 return hard ? rlfd.rlim_max : rlfd.rlim_cur;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000200#elif defined (HAVE_SYSCONF)
201 return sysconf (_SC_OPEN_MAX);
202#else
203 return 10000;
204#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000205}
206
Ben Lindstrombba81212001-06-25 05:01:22 +0000207static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000208fdlim_set(int lim)
209{
Ben Lindstrom5adbad22000-12-27 07:06:21 +0000210#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000211 struct rlimit rlfd;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000212#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000213 if (lim <= 0)
214 return (-1);
Ben Lindstrom5adbad22000-12-27 07:06:21 +0000215#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000216 if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
217 return (-1);
218 rlfd.rlim_cur = lim;
219 if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0)
220 return (-1);
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000221#elif defined (HAVE_SETDTABLESIZE)
Kevin Steves28a7f262001-02-05 15:43:59 +0000222 setdtablesize(lim);
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000223#endif
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000224 return (0);
225}
226
227/*
228 * This is an strsep function that returns a null field for adjacent
229 * separators. This is the same as the 4.4BSD strsep, but different from the
230 * one in the GNU libc.
231 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000232static char *
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000233xstrsep(char **str, const char *delim)
234{
235 char *s, *e;
236
237 if (!**str)
238 return (NULL);
239
240 s = *str;
241 e = s + strcspn(s, delim);
242
243 if (*e != '\0')
244 *e++ = '\0';
245 *str = e;
246
247 return (s);
248}
249
250/*
251 * Get the next non-null token (like GNU strsep). Strsep() will return a
252 * null token for two adjacent separators, so we may have to loop.
253 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000254static char *
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000255strnnsep(char **stringp, char *delim)
256{
257 char *tok;
258
259 do {
260 tok = xstrsep(stringp, delim);
261 } while (tok && *tok == '\0');
262 return (tok);
263}
264
Ben Lindstrombba81212001-06-25 05:01:22 +0000265static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000266keyprint(char *host, char *output_name, char *kd, int len)
267{
268 static Key *rsa;
269 static Buffer msg;
270
271 if (rsa == NULL) {
272 buffer_init(&msg);
273 rsa = key_new(KEY_RSA1);
274 }
275 buffer_append(&msg, kd, len);
276 buffer_consume(&msg, 8 - (len & 7)); /* padding */
277 if (buffer_get_char(&msg) != (int) SSH_SMSG_PUBLIC_KEY) {
278 error("%s: invalid packet type", host);
279 buffer_clear(&msg);
280 return;
281 }
282 buffer_consume(&msg, 8); /* cookie */
283
284 /* server key */
285 (void) buffer_get_int(&msg);
286 buffer_get_bignum(&msg, rsa->rsa->e);
287 buffer_get_bignum(&msg, rsa->rsa->n);
288
289 /* host key */
290 (void) buffer_get_int(&msg);
291 buffer_get_bignum(&msg, rsa->rsa->e);
292 buffer_get_bignum(&msg, rsa->rsa->n);
293 buffer_clear(&msg);
294
295 fprintf(stdout, "%s ", output_name ? output_name : host);
296 key_write(rsa, stdout);
297 fputs("\n", stdout);
298}
299
Ben Lindstrombba81212001-06-25 05:01:22 +0000300static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000301tcpconnect(char *host)
302{
303 struct addrinfo hints, *ai, *aitop;
304 char strport[NI_MAXSERV];
305 int gaierr, s = -1;
306
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000307 snprintf(strport, sizeof strport, "%d", SSH_DEFAULT_PORT);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000308 memset(&hints, 0, sizeof(hints));
309 hints.ai_family = family;
310 hints.ai_socktype = SOCK_STREAM;
311 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
312 fatal("getaddrinfo %s: %s", host, gai_strerror(gaierr));
313 for (ai = aitop; ai; ai = ai->ai_next) {
314 s = socket(ai->ai_family, SOCK_STREAM, 0);
315 if (s < 0) {
316 error("socket: %s", strerror(errno));
317 continue;
318 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +0000319 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000320 fatal("F_SETFL: %s", strerror(errno));
321 if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 &&
322 errno != EINPROGRESS)
323 error("connect (`%s'): %s", host, strerror(errno));
324 else
325 break;
326 close(s);
327 s = -1;
328 }
329 freeaddrinfo(aitop);
330 return s;
331}
332
Ben Lindstrombba81212001-06-25 05:01:22 +0000333static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000334conalloc(char *iname, char *oname)
335{
336 int s;
337 char *namebase, *name, *namelist;
338
339 namebase = namelist = xstrdup(iname);
340
341 do {
342 name = xstrsep(&namelist, ",");
343 if (!name) {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000344 xfree(namebase);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000345 return (-1);
346 }
347 } while ((s = tcpconnect(name)) < 0);
348
349 if (s >= maxfd)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000350 fatal("conalloc: fdno %d too high", s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000351 if (fdcon[s].c_status)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000352 fatal("conalloc: attempt to reuse fdno %d", s);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000353
354 fdcon[s].c_fd = s;
355 fdcon[s].c_status = CS_CON;
356 fdcon[s].c_namebase = namebase;
357 fdcon[s].c_name = name;
358 fdcon[s].c_namelist = namelist;
359 fdcon[s].c_output_name = xstrdup(oname);
360 fdcon[s].c_data = (char *) &fdcon[s].c_plen;
361 fdcon[s].c_len = 4;
362 fdcon[s].c_off = 0;
363 gettimeofday(&fdcon[s].c_tv, NULL);
364 fdcon[s].c_tv.tv_sec += timeout;
365 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
Ben Lindstromc1e04212001-03-05 07:04:38 +0000366 FD_SET(s, read_wait);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000367 ncon++;
368 return (s);
369}
370
Ben Lindstrombba81212001-06-25 05:01:22 +0000371static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000372confree(int s)
373{
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000374 if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000375 fatal("confree: attempt to free bad fdno %d", s);
Ben Lindstromd20b8552001-03-05 07:01:18 +0000376 close(s);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000377 xfree(fdcon[s].c_namebase);
378 xfree(fdcon[s].c_output_name);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000379 if (fdcon[s].c_status == CS_KEYS)
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000380 xfree(fdcon[s].c_data);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000381 fdcon[s].c_status = CS_UNUSED;
382 TAILQ_REMOVE(&tq, &fdcon[s], c_link);
Ben Lindstromc1e04212001-03-05 07:04:38 +0000383 FD_CLR(s, read_wait);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000384 ncon--;
385}
386
Ben Lindstrombba81212001-06-25 05:01:22 +0000387static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000388contouch(int s)
389{
390 TAILQ_REMOVE(&tq, &fdcon[s], c_link);
391 gettimeofday(&fdcon[s].c_tv, NULL);
392 fdcon[s].c_tv.tv_sec += timeout;
393 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
394}
395
Ben Lindstrombba81212001-06-25 05:01:22 +0000396static int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000397conrecycle(int s)
398{
399 int ret;
400 con *c = &fdcon[s];
401 char *iname, *oname;
402
403 iname = xstrdup(c->c_namelist);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000404 oname = xstrdup(c->c_output_name);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000405 confree(s);
406 ret = conalloc(iname, oname);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000407 xfree(iname);
408 xfree(oname);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000409 return (ret);
410}
411
Ben Lindstrombba81212001-06-25 05:01:22 +0000412static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000413congreet(int s)
414{
Ben Lindstrom884a4ac2001-03-06 03:33:04 +0000415 char buf[80], *cp;
416 size_t bufsiz;
Ben Lindstrome21c4ad2001-03-07 01:23:30 +0000417 int n = 0;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000418 con *c = &fdcon[s];
419
Ben Lindstrom884a4ac2001-03-06 03:33:04 +0000420 bufsiz = sizeof(buf);
421 cp = buf;
422 while (bufsiz-- && (n = read(s, cp, 1)) == 1 && *cp != '\n' && *cp != '\r')
423 cp++;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000424 if (n < 0) {
425 if (errno != ECONNREFUSED)
426 error("read (%s): %s", c->c_name, strerror(errno));
427 conrecycle(s);
428 return;
429 }
Ben Lindstrom884a4ac2001-03-06 03:33:04 +0000430 if (*cp != '\n' && *cp != '\r') {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000431 error("%s: bad greeting", c->c_name);
432 confree(s);
433 return;
434 }
Ben Lindstrom884a4ac2001-03-06 03:33:04 +0000435 *cp = '\0';
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000436 fprintf(stderr, "# %s %s\n", c->c_name, buf);
437 n = snprintf(buf, sizeof buf, "SSH-1.5-OpenSSH-keyscan\r\n");
Ben Lindstromd20b8552001-03-05 07:01:18 +0000438 if (atomicio(write, s, buf, n) != n) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000439 error("write (%s): %s", c->c_name, strerror(errno));
440 confree(s);
441 return;
442 }
443 c->c_status = CS_SIZE;
444 contouch(s);
445}
446
Ben Lindstrombba81212001-06-25 05:01:22 +0000447static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000448conread(int s)
449{
450 int n;
451 con *c = &fdcon[s];
452
453 if (c->c_status == CS_CON) {
454 congreet(s);
455 return;
456 }
457 n = read(s, c->c_data + c->c_off, c->c_len - c->c_off);
458 if (n < 0) {
459 error("read (%s): %s", c->c_name, strerror(errno));
460 confree(s);
461 return;
462 }
463 c->c_off += n;
464
465 if (c->c_off == c->c_len)
466 switch (c->c_status) {
467 case CS_SIZE:
468 c->c_plen = htonl(c->c_plen);
469 c->c_len = c->c_plen + 8 - (c->c_plen & 7);
470 c->c_off = 0;
471 c->c_data = xmalloc(c->c_len);
472 c->c_status = CS_KEYS;
473 break;
474 case CS_KEYS:
475 keyprint(c->c_name, c->c_output_name, c->c_data, c->c_plen);
476 confree(s);
477 return;
478 break;
479 default:
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000480 fatal("conread: invalid status %d", c->c_status);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000481 break;
482 }
483
484 contouch(s);
485}
486
Ben Lindstrombba81212001-06-25 05:01:22 +0000487static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000488conloop(void)
489{
Ben Lindstromc1e04212001-03-05 07:04:38 +0000490 fd_set *r, *e;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000491 struct timeval seltime, now;
492 int i;
493 con *c;
494
495 gettimeofday(&now, NULL);
496 c = tq.tqh_first;
497
Ben Lindstromd20b8552001-03-05 07:01:18 +0000498 if (c && (c->c_tv.tv_sec > now.tv_sec ||
499 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000500 seltime = c->c_tv;
501 seltime.tv_sec -= now.tv_sec;
502 seltime.tv_usec -= now.tv_usec;
Ben Lindstromc791beb2001-02-10 23:18:11 +0000503 if (seltime.tv_usec < 0) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000504 seltime.tv_usec += 1000000;
505 seltime.tv_sec--;
506 }
507 } else
508 seltime.tv_sec = seltime.tv_usec = 0;
509
Ben Lindstromc1e04212001-03-05 07:04:38 +0000510 r = xmalloc(read_wait_size);
511 memcpy(r, read_wait, read_wait_size);
512 e = xmalloc(read_wait_size);
513 memcpy(e, read_wait, read_wait_size);
514
515 while (select(maxfd, r, NULL, e, &seltime) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000516 (errno == EAGAIN || errno == EINTR))
517 ;
518
Ben Lindstromd20b8552001-03-05 07:01:18 +0000519 for (i = 0; i < maxfd; i++) {
Ben Lindstromc1e04212001-03-05 07:04:38 +0000520 if (FD_ISSET(i, e)) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000521 error("%s: exception!", fdcon[i].c_name);
522 confree(i);
Ben Lindstromc1e04212001-03-05 07:04:38 +0000523 } else if (FD_ISSET(i, r))
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000524 conread(i);
Ben Lindstromd20b8552001-03-05 07:01:18 +0000525 }
Ben Lindstromc1e04212001-03-05 07:04:38 +0000526 xfree(r);
527 xfree(e);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000528
529 c = tq.tqh_first;
Ben Lindstromd20b8552001-03-05 07:01:18 +0000530 while (c && (c->c_tv.tv_sec < now.tv_sec ||
531 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000532 int s = c->c_fd;
Ben Lindstromd20b8552001-03-05 07:01:18 +0000533
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000534 c = c->c_link.tqe_next;
535 conrecycle(s);
536 }
537}
538
Ben Lindstrombba81212001-06-25 05:01:22 +0000539static char *
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000540nexthost(int argc, char **argv)
541{
542 static Linebuf *lb;
543
544 for (;;) {
545 if (!lb) {
546 if (argno >= argc)
547 return (NULL);
548 if (argv[argno][0] != '-')
549 return (argv[argno++]);
550 if (!strcmp(argv[argno], "--")) {
551 if (++argno >= argc)
552 return (NULL);
553 return (argv[argno++]);
554 } else if (!strncmp(argv[argno], "-f", 2)) {
555 char *fname;
Ben Lindstromd20b8552001-03-05 07:01:18 +0000556
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000557 if (argv[argno][2])
558 fname = &argv[argno++][2];
559 else if (++argno >= argc) {
560 error("missing filename for `-f'");
561 return (NULL);
562 } else
563 fname = argv[argno++];
564 if (!strcmp(fname, "-"))
565 fname = NULL;
Kevin Stevesfc74af42000-12-06 22:47:55 +0000566 lb = Linebuf_alloc(fname, error);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000567 } else
Ben Lindstromd20b8552001-03-05 07:01:18 +0000568 error("ignoring invalid/misplaced option `%s'",
569 argv[argno++]);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000570 } else {
571 char *line;
Ben Lindstromd20b8552001-03-05 07:01:18 +0000572
Ben Lindstromc791beb2001-02-10 23:18:11 +0000573 line = Linebuf_getline(lb);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000574 if (line)
575 return (line);
576 Linebuf_free(lb);
577 lb = NULL;
578 }
579 }
580}
581
Ben Lindstrombba81212001-06-25 05:01:22 +0000582static void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000583usage(void)
584{
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000585 fatal("usage: %s [-t timeout] { [--] host | -f file } ...", __progname);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000586 return;
587}
588
589int
590main(int argc, char **argv)
591{
592 char *host = NULL;
593
Kevin Stevesec84dc12000-12-13 17:45:15 +0000594 __progname = get_progname(argv[0]);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000595 TAILQ_INIT(&tq);
596
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000597 if (argc <= argno)
598 usage();
599
600 if (argv[1][0] == '-' && argv[1][1] == 't') {
601 argno++;
602 if (argv[1][2])
603 timeout = atoi(&argv[1][2]);
604 else {
605 if (argno >= argc)
606 usage();
607 timeout = atoi(argv[argno++]);
608 }
609 if (timeout <= 0)
610 usage();
611 }
612 if (argc <= argno)
613 usage();
614
615 maxfd = fdlim_get(1);
616 if (maxfd < 0)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000617 fatal("%s: fdlim_get: bad value", __progname);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000618 if (maxfd > MAXMAXFD)
619 maxfd = MAXMAXFD;
Ben Lindstromd20b8552001-03-05 07:01:18 +0000620 if (MAXCON <= 0)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000621 fatal("%s: not enough file descriptors", __progname);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000622 if (maxfd > fdlim_get(0))
623 fdlim_set(maxfd);
624 fdcon = xmalloc(maxfd * sizeof(con));
Ben Lindstromc791beb2001-02-10 23:18:11 +0000625 memset(fdcon, 0, maxfd * sizeof(con));
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000626
Ben Lindstromc1e04212001-03-05 07:04:38 +0000627 read_wait_size = howmany(maxfd, NFDBITS) * sizeof(fd_mask);
628 read_wait = xmalloc(read_wait_size);
629 memset(read_wait, 0, read_wait_size);
630
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000631 do {
Ben Lindstromd20b8552001-03-05 07:01:18 +0000632 while (ncon < MAXCON) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000633 char *name;
634
635 host = nexthost(argc, argv);
636 if (host == NULL)
637 break;
638 name = strnnsep(&host, " \t\n");
639 conalloc(name, *host ? host : name);
640 }
641 conloop();
642 } while (host);
643 while (ncon > 0)
644 conloop();
645
646 return (0);
647}