]>
Commit | Line | Data |
---|---|---|
f6fdbddf | 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 | |
aa144206 | 6 | * OpenBSD project by leaving this copyright notice intact. |
f6fdbddf | 7 | */ |
8 | ||
9 | #include "includes.h" | |
aa144206 | 10 | RCSID("$OpenBSD: ssh-keyscan.c,v 1.23 2001/06/05 05:05:39 pvalchev Exp $"); |
f6fdbddf | 11 | |
b705bea0 | 12 | #if defined(HAVE_SYS_QUEUE_H) && !defined(HAVE_BOGUS_SYS_QUEUE_H) |
f6fdbddf | 13 | #include <sys/queue.h> |
bf5f69f7 | 14 | #else |
51e2fc8f | 15 | #include "openbsd-compat/fake-queue.h" |
bf5f69f7 | 16 | #endif |
f6fdbddf | 17 | #include <errno.h> |
18 | ||
19 | #include <openssl/bn.h> | |
f6fdbddf | 20 | |
21 | #include "xmalloc.h" | |
22 | #include "ssh.h" | |
42f11eb2 | 23 | #include "ssh1.h" |
f6fdbddf | 24 | #include "key.h" |
25 | #include "buffer.h" | |
26 | #include "bufaux.h" | |
42f11eb2 | 27 | #include "log.h" |
2671b47f | 28 | #include "atomicio.h" |
f6fdbddf | 29 | |
30 | static int argno = 1; /* Number of argument currently being parsed */ | |
31 | ||
32 | int family = AF_UNSPEC; /* IPv4, IPv6 or both */ | |
33 | ||
f6fdbddf | 34 | #define MAXMAXFD 256 |
35 | ||
36 | /* The number of seconds after which to give up on a TCP connection */ | |
37 | int timeout = 5; | |
38 | ||
39 | int maxfd; | |
2671b47f | 40 | #define MAXCON (maxfd - 10) |
f6fdbddf | 41 | |
1fe6a48f | 42 | #ifdef HAVE___PROGNAME |
43 | extern char *__progname; | |
44 | #else | |
45 | char *__progname; | |
46 | #endif | |
ff7fee59 | 47 | fd_set *read_wait; |
48 | size_t read_wait_size; | |
f6fdbddf | 49 | int 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 | */ | |
55 | typedef struct Connection { | |
1e3b8b07 | 56 | u_char c_status; /* State of connection on this file desc. */ |
f6fdbddf | 57 | #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 | ||
74 | TAILQ_HEAD(conlist, Connection) tq; /* Timeout Queue */ | |
75 | con *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 | ||
84 | typedef struct { | |
85 | char *buf; | |
1e3b8b07 | 86 | u_int size; |
f6fdbddf | 87 | int lineno; |
88 | const char *filename; | |
89 | FILE *stream; | |
90 | void (*errfun) (const char *,...); | |
91 | } Linebuf; | |
92 | ||
70a052c7 | 93 | Linebuf * |
f6fdbddf | 94 | Linebuf_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"))) { | |
b5c334cc | 106 | xfree(lb); |
f6fdbddf | 107 | 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); | |
b5c334cc | 119 | xfree(lb); |
f6fdbddf | 120 | return (NULL); |
121 | } | |
122 | lb->errfun = errfun; | |
123 | lb->lineno = 0; | |
124 | return (lb); | |
125 | } | |
126 | ||
70a052c7 | 127 | void |
f6fdbddf | 128 | Linebuf_free(Linebuf * lb) |
129 | { | |
130 | fclose(lb->stream); | |
b5c334cc | 131 | xfree(lb->buf); |
132 | xfree(lb); | |
f6fdbddf | 133 | } |
134 | ||
70a052c7 | 135 | void |
f6fdbddf | 136 | Linebuf_restart(Linebuf * lb) |
137 | { | |
138 | clearerr(lb->stream); | |
139 | rewind(lb->stream); | |
140 | lb->lineno = 0; | |
141 | } | |
142 | ||
70a052c7 | 143 | int |
f6fdbddf | 144 | Linebuf_lineno(Linebuf * lb) |
145 | { | |
146 | return (lb->lineno); | |
147 | } | |
148 | ||
70a052c7 | 149 | char * |
5be2ec5e | 150 | Linebuf_getline(Linebuf * lb) |
f6fdbddf | 151 | { |
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) | |
778f6940 | 159 | (*lb->errfun) ("%s: %s\n", lb->filename, |
160 | strerror(errno)); | |
f6fdbddf | 161 | 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) | |
778f6940 | 172 | (*lb->errfun) ("%s: skipping incomplete last line\n", |
173 | lb->filename); | |
f6fdbddf | 174 | 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) | |
778f6940 | 179 | (*lb->errfun) ("linebuf (%s): realloc failed\n", |
180 | lb->filename); | |
f6fdbddf | 181 | return (NULL); |
182 | } | |
183 | } | |
184 | } | |
185 | ||
70a052c7 | 186 | int |
f6fdbddf | 187 | fdlim_get(int hard) |
188 | { | |
f318b98b | 189 | #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE) |
f6fdbddf | 190 | struct rlimit rlfd; |
778f6940 | 191 | |
f6fdbddf | 192 | 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; | |
b03bd394 | 198 | #elif defined (HAVE_SYSCONF) |
199 | return sysconf (_SC_OPEN_MAX); | |
200 | #else | |
201 | return 10000; | |
202 | #endif | |
f6fdbddf | 203 | } |
204 | ||
70a052c7 | 205 | int |
f6fdbddf | 206 | fdlim_set(int lim) |
207 | { | |
f318b98b | 208 | #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE) |
f6fdbddf | 209 | struct rlimit rlfd; |
b03bd394 | 210 | #endif |
f6fdbddf | 211 | if (lim <= 0) |
212 | return (-1); | |
f318b98b | 213 | #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE) |
f6fdbddf | 214 | if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0) |
215 | return (-1); | |
216 | rlfd.rlim_cur = lim; | |
217 | if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0) | |
218 | return (-1); | |
b03bd394 | 219 | #elif defined (HAVE_SETDTABLESIZE) |
b705bea0 | 220 | setdtablesize(lim); |
b03bd394 | 221 | #endif |
f6fdbddf | 222 | 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 | */ | |
70a052c7 | 230 | char * |
f6fdbddf | 231 | xstrsep(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 | */ | |
252 | char * | |
253 | strnnsep(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 | ||
263 | void | |
264 | keyprint(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 | ||
298 | int | |
299 | tcpconnect(char *host) | |
300 | { | |
301 | struct addrinfo hints, *ai, *aitop; | |
302 | char strport[NI_MAXSERV]; | |
303 | int gaierr, s = -1; | |
304 | ||
b5c334cc | 305 | snprintf(strport, sizeof strport, "%d", SSH_DEFAULT_PORT); |
f6fdbddf | 306 | 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 | } | |
eea39c02 | 317 | if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) |
f6fdbddf | 318 | 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 | ||
331 | int | |
332 | conalloc(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) { | |
b5c334cc | 342 | xfree(namebase); |
f6fdbddf | 343 | return (-1); |
344 | } | |
345 | } while ((s = tcpconnect(name)) < 0); | |
346 | ||
347 | if (s >= maxfd) | |
227e8e86 | 348 | fatal("conalloc: fdno %d too high", s); |
f6fdbddf | 349 | if (fdcon[s].c_status) |
227e8e86 | 350 | fatal("conalloc: attempt to reuse fdno %d", s); |
f6fdbddf | 351 | |
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); | |
ff7fee59 | 364 | FD_SET(s, read_wait); |
f6fdbddf | 365 | ncon++; |
366 | return (s); | |
367 | } | |
368 | ||
369 | void | |
370 | confree(int s) | |
371 | { | |
f6fdbddf | 372 | if (s >= maxfd || fdcon[s].c_status == CS_UNUSED) |
227e8e86 | 373 | fatal("confree: attempt to free bad fdno %d", s); |
2671b47f | 374 | close(s); |
b5c334cc | 375 | xfree(fdcon[s].c_namebase); |
376 | xfree(fdcon[s].c_output_name); | |
f6fdbddf | 377 | if (fdcon[s].c_status == CS_KEYS) |
b5c334cc | 378 | xfree(fdcon[s].c_data); |
f6fdbddf | 379 | fdcon[s].c_status = CS_UNUSED; |
380 | TAILQ_REMOVE(&tq, &fdcon[s], c_link); | |
ff7fee59 | 381 | FD_CLR(s, read_wait); |
f6fdbddf | 382 | ncon--; |
383 | } | |
384 | ||
385 | void | |
386 | contouch(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 | ||
394 | int | |
395 | conrecycle(int s) | |
396 | { | |
397 | int ret; | |
398 | con *c = &fdcon[s]; | |
399 | char *iname, *oname; | |
400 | ||
401 | iname = xstrdup(c->c_namelist); | |
b5c334cc | 402 | oname = xstrdup(c->c_output_name); |
f6fdbddf | 403 | confree(s); |
404 | ret = conalloc(iname, oname); | |
b5c334cc | 405 | xfree(iname); |
406 | xfree(oname); | |
f6fdbddf | 407 | return (ret); |
408 | } | |
409 | ||
410 | void | |
411 | congreet(int s) | |
412 | { | |
c16c7f20 | 413 | char buf[80], *cp; |
414 | size_t bufsiz; | |
3251b439 | 415 | int n = 0; |
f6fdbddf | 416 | con *c = &fdcon[s]; |
417 | ||
c16c7f20 | 418 | bufsiz = sizeof(buf); |
419 | cp = buf; | |
420 | while (bufsiz-- && (n = read(s, cp, 1)) == 1 && *cp != '\n' && *cp != '\r') | |
421 | cp++; | |
f6fdbddf | 422 | if (n < 0) { |
423 | if (errno != ECONNREFUSED) | |
424 | error("read (%s): %s", c->c_name, strerror(errno)); | |
425 | conrecycle(s); | |
426 | return; | |
427 | } | |
c16c7f20 | 428 | if (*cp != '\n' && *cp != '\r') { |
f6fdbddf | 429 | error("%s: bad greeting", c->c_name); |
430 | confree(s); | |
431 | return; | |
432 | } | |
c16c7f20 | 433 | *cp = '\0'; |
f6fdbddf | 434 | fprintf(stderr, "# %s %s\n", c->c_name, buf); |
435 | n = snprintf(buf, sizeof buf, "SSH-1.5-OpenSSH-keyscan\r\n"); | |
2671b47f | 436 | if (atomicio(write, s, buf, n) != n) { |
f6fdbddf | 437 | 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 | ||
445 | void | |
446 | conread(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: | |
227e8e86 | 478 | fatal("conread: invalid status %d", c->c_status); |
f6fdbddf | 479 | break; |
480 | } | |
481 | ||
482 | contouch(s); | |
483 | } | |
484 | ||
485 | void | |
486 | conloop(void) | |
487 | { | |
ff7fee59 | 488 | fd_set *r, *e; |
f6fdbddf | 489 | struct timeval seltime, now; |
490 | int i; | |
491 | con *c; | |
492 | ||
493 | gettimeofday(&now, NULL); | |
494 | c = tq.tqh_first; | |
495 | ||
2671b47f | 496 | 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))) { | |
f6fdbddf | 498 | seltime = c->c_tv; |
499 | seltime.tv_sec -= now.tv_sec; | |
500 | seltime.tv_usec -= now.tv_usec; | |
5be2ec5e | 501 | if (seltime.tv_usec < 0) { |
f6fdbddf | 502 | seltime.tv_usec += 1000000; |
503 | seltime.tv_sec--; | |
504 | } | |
505 | } else | |
506 | seltime.tv_sec = seltime.tv_usec = 0; | |
507 | ||
ff7fee59 | 508 | 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 && | |
fd193ca4 | 514 | (errno == EAGAIN || errno == EINTR)) |
515 | ; | |
516 | ||
2671b47f | 517 | for (i = 0; i < maxfd; i++) { |
ff7fee59 | 518 | if (FD_ISSET(i, e)) { |
f6fdbddf | 519 | error("%s: exception!", fdcon[i].c_name); |
520 | confree(i); | |
ff7fee59 | 521 | } else if (FD_ISSET(i, r)) |
f6fdbddf | 522 | conread(i); |
2671b47f | 523 | } |
ff7fee59 | 524 | xfree(r); |
525 | xfree(e); | |
f6fdbddf | 526 | |
527 | c = tq.tqh_first; | |
2671b47f | 528 | 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))) { | |
f6fdbddf | 530 | int s = c->c_fd; |
2671b47f | 531 | |
f6fdbddf | 532 | c = c->c_link.tqe_next; |
533 | conrecycle(s); | |
534 | } | |
535 | } | |
536 | ||
537 | char * | |
538 | nexthost(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; | |
2671b47f | 554 | |
f6fdbddf | 555 | 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; | |
f20255cb | 564 | lb = Linebuf_alloc(fname, error); |
f6fdbddf | 565 | } else |
2671b47f | 566 | error("ignoring invalid/misplaced option `%s'", |
567 | argv[argno++]); | |
f6fdbddf | 568 | } else { |
569 | char *line; | |
2671b47f | 570 | |
5be2ec5e | 571 | line = Linebuf_getline(lb); |
f6fdbddf | 572 | if (line) |
573 | return (line); | |
574 | Linebuf_free(lb); | |
575 | lb = NULL; | |
576 | } | |
577 | } | |
578 | } | |
579 | ||
70a052c7 | 580 | void |
f6fdbddf | 581 | usage(void) |
582 | { | |
227e8e86 | 583 | fatal("usage: %s [-t timeout] { [--] host | -f file } ...", __progname); |
f6fdbddf | 584 | return; |
585 | } | |
586 | ||
587 | int | |
588 | main(int argc, char **argv) | |
589 | { | |
590 | char *host = NULL; | |
591 | ||
1fe6a48f | 592 | __progname = get_progname(argv[0]); |
f6fdbddf | 593 | TAILQ_INIT(&tq); |
594 | ||
f6fdbddf | 595 | 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) | |
227e8e86 | 615 | fatal("%s: fdlim_get: bad value", __progname); |
f6fdbddf | 616 | if (maxfd > MAXMAXFD) |
617 | maxfd = MAXMAXFD; | |
2671b47f | 618 | if (MAXCON <= 0) |
227e8e86 | 619 | fatal("%s: not enough file descriptors", __progname); |
f6fdbddf | 620 | if (maxfd > fdlim_get(0)) |
621 | fdlim_set(maxfd); | |
622 | fdcon = xmalloc(maxfd * sizeof(con)); | |
5be2ec5e | 623 | memset(fdcon, 0, maxfd * sizeof(con)); |
f6fdbddf | 624 | |
ff7fee59 | 625 | 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 | ||
f6fdbddf | 629 | do { |
2671b47f | 630 | while (ncon < MAXCON) { |
f6fdbddf | 631 | 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 | } |