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