blob: da34f7c63d96aaddcaf725e830be316fd50c93c0 [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 Lindstroma238f6e2001-06-09 01:30:39 +000010RCSID("$OpenBSD: ssh-keyscan.c,v 1.23 2001/06/05 05:05:39 pvalchev 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
Kevin Steves935aa242001-03-05 19:46:37 +000093Linebuf *
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
Kevin Steves935aa242001-03-05 19:46:37 +0000127void
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
Kevin Steves935aa242001-03-05 19:46:37 +0000135void
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
Kevin Steves935aa242001-03-05 19:46:37 +0000143int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000144Linebuf_lineno(Linebuf * lb)
145{
146 return (lb->lineno);
147}
148
Kevin Steves935aa242001-03-05 19:46:37 +0000149char *
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
Kevin Steves935aa242001-03-05 19:46:37 +0000186int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000187fdlim_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
Kevin Steves935aa242001-03-05 19:46:37 +0000205int
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000206fdlim_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 */
Kevin Steves935aa242001-03-05 19:46:37 +0000230char *
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);
Ben Lindstromc1e04212001-03-05 07:04:38 +0000364 FD_SET(s, read_wait);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000365 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);
Ben Lindstromc1e04212001-03-05 07:04:38 +0000381 FD_CLR(s, read_wait);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000382 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{
Ben Lindstrom884a4ac2001-03-06 03:33:04 +0000413 char buf[80], *cp;
414 size_t bufsiz;
Ben Lindstrome21c4ad2001-03-07 01:23:30 +0000415 int n = 0;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000416 con *c = &fdcon[s];
417
Ben Lindstrom884a4ac2001-03-06 03:33:04 +0000418 bufsiz = sizeof(buf);
419 cp = buf;
420 while (bufsiz-- && (n = read(s, cp, 1)) == 1 && *cp != '\n' && *cp != '\r')
421 cp++;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000422 if (n < 0) {
423 if (errno != ECONNREFUSED)
424 error("read (%s): %s", c->c_name, strerror(errno));
425 conrecycle(s);
426 return;
427 }
Ben Lindstrom884a4ac2001-03-06 03:33:04 +0000428 if (*cp != '\n' && *cp != '\r') {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000429 error("%s: bad greeting", c->c_name);
430 confree(s);
431 return;
432 }
Ben Lindstrom884a4ac2001-03-06 03:33:04 +0000433 *cp = '\0';
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000434 fprintf(stderr, "# %s %s\n", c->c_name, buf);
435 n = snprintf(buf, sizeof buf, "SSH-1.5-OpenSSH-keyscan\r\n");
Ben Lindstromd20b8552001-03-05 07:01:18 +0000436 if (atomicio(write, s, buf, n) != n) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000437 error("write (%s): %s", c->c_name, strerror(errno));
438 confree(s);
439 return;
440 }
441 c->c_status = CS_SIZE;
442 contouch(s);
443}
444
445void
446conread(int s)
447{
448 int n;
449 con *c = &fdcon[s];
450
451 if (c->c_status == CS_CON) {
452 congreet(s);
453 return;
454 }
455 n = read(s, c->c_data + c->c_off, c->c_len - c->c_off);
456 if (n < 0) {
457 error("read (%s): %s", c->c_name, strerror(errno));
458 confree(s);
459 return;
460 }
461 c->c_off += n;
462
463 if (c->c_off == c->c_len)
464 switch (c->c_status) {
465 case CS_SIZE:
466 c->c_plen = htonl(c->c_plen);
467 c->c_len = c->c_plen + 8 - (c->c_plen & 7);
468 c->c_off = 0;
469 c->c_data = xmalloc(c->c_len);
470 c->c_status = CS_KEYS;
471 break;
472 case CS_KEYS:
473 keyprint(c->c_name, c->c_output_name, c->c_data, c->c_plen);
474 confree(s);
475 return;
476 break;
477 default:
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000478 fatal("conread: invalid status %d", c->c_status);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000479 break;
480 }
481
482 contouch(s);
483}
484
485void
486conloop(void)
487{
Ben Lindstromc1e04212001-03-05 07:04:38 +0000488 fd_set *r, *e;
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000489 struct timeval seltime, now;
490 int i;
491 con *c;
492
493 gettimeofday(&now, NULL);
494 c = tq.tqh_first;
495
Ben Lindstromd20b8552001-03-05 07:01:18 +0000496 if (c && (c->c_tv.tv_sec > now.tv_sec ||
497 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000498 seltime = c->c_tv;
499 seltime.tv_sec -= now.tv_sec;
500 seltime.tv_usec -= now.tv_usec;
Ben Lindstromc791beb2001-02-10 23:18:11 +0000501 if (seltime.tv_usec < 0) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000502 seltime.tv_usec += 1000000;
503 seltime.tv_sec--;
504 }
505 } else
506 seltime.tv_sec = seltime.tv_usec = 0;
507
Ben Lindstromc1e04212001-03-05 07:04:38 +0000508 r = xmalloc(read_wait_size);
509 memcpy(r, read_wait, read_wait_size);
510 e = xmalloc(read_wait_size);
511 memcpy(e, read_wait, read_wait_size);
512
513 while (select(maxfd, r, NULL, e, &seltime) == -1 &&
Ben Lindstromf9452512001-02-15 03:12:08 +0000514 (errno == EAGAIN || errno == EINTR))
515 ;
516
Ben Lindstromd20b8552001-03-05 07:01:18 +0000517 for (i = 0; i < maxfd; i++) {
Ben Lindstromc1e04212001-03-05 07:04:38 +0000518 if (FD_ISSET(i, e)) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000519 error("%s: exception!", fdcon[i].c_name);
520 confree(i);
Ben Lindstromc1e04212001-03-05 07:04:38 +0000521 } else if (FD_ISSET(i, r))
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000522 conread(i);
Ben Lindstromd20b8552001-03-05 07:01:18 +0000523 }
Ben Lindstromc1e04212001-03-05 07:04:38 +0000524 xfree(r);
525 xfree(e);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000526
527 c = tq.tqh_first;
Ben Lindstromd20b8552001-03-05 07:01:18 +0000528 while (c && (c->c_tv.tv_sec < now.tv_sec ||
529 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000530 int s = c->c_fd;
Ben Lindstromd20b8552001-03-05 07:01:18 +0000531
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000532 c = c->c_link.tqe_next;
533 conrecycle(s);
534 }
535}
536
537char *
538nexthost(int argc, char **argv)
539{
540 static Linebuf *lb;
541
542 for (;;) {
543 if (!lb) {
544 if (argno >= argc)
545 return (NULL);
546 if (argv[argno][0] != '-')
547 return (argv[argno++]);
548 if (!strcmp(argv[argno], "--")) {
549 if (++argno >= argc)
550 return (NULL);
551 return (argv[argno++]);
552 } else if (!strncmp(argv[argno], "-f", 2)) {
553 char *fname;
Ben Lindstromd20b8552001-03-05 07:01:18 +0000554
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000555 if (argv[argno][2])
556 fname = &argv[argno++][2];
557 else if (++argno >= argc) {
558 error("missing filename for `-f'");
559 return (NULL);
560 } else
561 fname = argv[argno++];
562 if (!strcmp(fname, "-"))
563 fname = NULL;
Kevin Stevesfc74af42000-12-06 22:47:55 +0000564 lb = Linebuf_alloc(fname, error);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000565 } else
Ben Lindstromd20b8552001-03-05 07:01:18 +0000566 error("ignoring invalid/misplaced option `%s'",
567 argv[argno++]);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000568 } else {
569 char *line;
Ben Lindstromd20b8552001-03-05 07:01:18 +0000570
Ben Lindstromc791beb2001-02-10 23:18:11 +0000571 line = Linebuf_getline(lb);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000572 if (line)
573 return (line);
574 Linebuf_free(lb);
575 lb = NULL;
576 }
577 }
578}
579
Kevin Steves935aa242001-03-05 19:46:37 +0000580void
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000581usage(void)
582{
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000583 fatal("usage: %s [-t timeout] { [--] host | -f file } ...", __progname);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000584 return;
585}
586
587int
588main(int argc, char **argv)
589{
590 char *host = NULL;
591
Kevin Stevesec84dc12000-12-13 17:45:15 +0000592 __progname = get_progname(argv[0]);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000593 TAILQ_INIT(&tq);
594
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000595 if (argc <= argno)
596 usage();
597
598 if (argv[1][0] == '-' && argv[1][1] == 't') {
599 argno++;
600 if (argv[1][2])
601 timeout = atoi(&argv[1][2]);
602 else {
603 if (argno >= argc)
604 usage();
605 timeout = atoi(argv[argno++]);
606 }
607 if (timeout <= 0)
608 usage();
609 }
610 if (argc <= argno)
611 usage();
612
613 maxfd = fdlim_get(1);
614 if (maxfd < 0)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000615 fatal("%s: fdlim_get: bad value", __progname);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000616 if (maxfd > MAXMAXFD)
617 maxfd = MAXMAXFD;
Ben Lindstromd20b8552001-03-05 07:01:18 +0000618 if (MAXCON <= 0)
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000619 fatal("%s: not enough file descriptors", __progname);
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000620 if (maxfd > fdlim_get(0))
621 fdlim_set(maxfd);
622 fdcon = xmalloc(maxfd * sizeof(con));
Ben Lindstromc791beb2001-02-10 23:18:11 +0000623 memset(fdcon, 0, maxfd * sizeof(con));
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000624
Ben Lindstromc1e04212001-03-05 07:04:38 +0000625 read_wait_size = howmany(maxfd, NFDBITS) * sizeof(fd_mask);
626 read_wait = xmalloc(read_wait_size);
627 memset(read_wait, 0, read_wait_size);
628
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000629 do {
Ben Lindstromd20b8552001-03-05 07:01:18 +0000630 while (ncon < MAXCON) {
Ben Lindstromb6434ae2000-12-05 01:15:09 +0000631 char *name;
632
633 host = nexthost(argc, argv);
634 if (host == NULL)
635 break;
636 name = strnnsep(&host, " \t\n");
637 conalloc(name, *host ? host : name);
638 }
639 conloop();
640 } while (host);
641 while (ncon > 0)
642 conloop();
643
644 return (0);
645}