Annotation of src/usr.bin/ssh/ssh-keyscan.c, Revision 1.38
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.38 ! stevesk 10: RCSID("$OpenBSD: ssh-keyscan.c,v 1.37 2002/06/27 08:49:44 markus 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: {
167: int n = 0;
168:
169: lb->lineno++;
170: for (;;) {
171: /* Read a line */
172: if (!fgets(&lb->buf[n], lb->size - n, lb->stream)) {
173: if (ferror(lb->stream) && lb->errfun)
1.17 deraadt 174: (*lb->errfun) ("%s: %s\n", lb->filename,
175: strerror(errno));
1.1 markus 176: return (NULL);
177: }
178: n = strlen(lb->buf);
179:
180: /* Return it or an error if it fits */
181: if (n > 0 && lb->buf[n - 1] == '\n') {
182: lb->buf[n - 1] = '\0';
183: return (lb->buf);
184: }
185: if (n != lb->size - 1) {
186: if (lb->errfun)
1.17 deraadt 187: (*lb->errfun) ("%s: skipping incomplete last line\n",
188: lb->filename);
1.1 markus 189: return (NULL);
190: }
191: /* Double the buffer if we need more space */
192: if (!(lb->buf = realloc(lb->buf, (lb->size *= 2)))) {
193: if (lb->errfun)
1.17 deraadt 194: (*lb->errfun) ("linebuf (%s): realloc failed\n",
195: lb->filename);
1.1 markus 196: return (NULL);
197: }
198: }
199: }
200:
1.24 itojun 201: static int
1.1 markus 202: fdlim_get(int hard)
203: {
204: struct rlimit rlfd;
1.17 deraadt 205:
1.1 markus 206: if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
207: return (-1);
208: if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY)
209: return 10000;
210: else
211: return hard ? rlfd.rlim_max : rlfd.rlim_cur;
212: }
213:
1.24 itojun 214: static int
1.1 markus 215: fdlim_set(int lim)
216: {
217: struct rlimit rlfd;
218: if (lim <= 0)
219: return (-1);
220: if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
221: return (-1);
222: rlfd.rlim_cur = lim;
223: if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0)
224: return (-1);
225: return (0);
226: }
227:
228: /*
229: * This is an strsep function that returns a null field for adjacent
230: * separators. This is the same as the 4.4BSD strsep, but different from the
231: * one in the GNU libc.
232: */
1.24 itojun 233: static char *
1.1 markus 234: xstrsep(char **str, const char *delim)
235: {
236: char *s, *e;
237:
238: if (!**str)
239: return (NULL);
240:
241: s = *str;
242: e = s + strcspn(s, delim);
243:
244: if (*e != '\0')
245: *e++ = '\0';
246: *str = e;
247:
248: return (s);
249: }
250:
251: /*
252: * Get the next non-null token (like GNU strsep). Strsep() will return a
253: * null token for two adjacent separators, so we may have to loop.
254: */
1.24 itojun 255: static char *
1.1 markus 256: strnnsep(char **stringp, char *delim)
257: {
258: char *tok;
259:
260: do {
261: tok = xstrsep(stringp, delim);
262: } while (tok && *tok == '\0');
263: return (tok);
264: }
265:
1.26 markus 266: static Key *
267: keygrab_ssh1(con *c)
1.1 markus 268: {
269: static Key *rsa;
270: static Buffer msg;
271:
272: if (rsa == NULL) {
273: buffer_init(&msg);
274: rsa = key_new(KEY_RSA1);
275: }
1.26 markus 276: buffer_append(&msg, c->c_data, c->c_plen);
277: buffer_consume(&msg, 8 - (c->c_plen & 7)); /* padding */
1.1 markus 278: if (buffer_get_char(&msg) != (int) SSH_SMSG_PUBLIC_KEY) {
1.26 markus 279: error("%s: invalid packet type", c->c_name);
1.1 markus 280: buffer_clear(&msg);
1.26 markus 281: return NULL;
1.1 markus 282: }
283: buffer_consume(&msg, 8); /* cookie */
284:
285: /* server key */
286: (void) buffer_get_int(&msg);
287: buffer_get_bignum(&msg, rsa->rsa->e);
288: buffer_get_bignum(&msg, rsa->rsa->n);
289:
290: /* host key */
291: (void) buffer_get_int(&msg);
292: buffer_get_bignum(&msg, rsa->rsa->e);
293: buffer_get_bignum(&msg, rsa->rsa->n);
1.26 markus 294:
1.1 markus 295: buffer_clear(&msg);
296:
1.26 markus 297: return (rsa);
298: }
299:
300: static int
301: hostjump(Key *hostkey)
302: {
1.29 markus 303: kexjmp_key = hostkey;
304: longjmp(kexjmp, 1);
1.26 markus 305: }
306:
307: static int
308: ssh2_capable(int remote_major, int remote_minor)
309: {
310: switch (remote_major) {
311: case 1:
312: if (remote_minor == 99)
313: return 1;
314: break;
315: case 2:
316: return 1;
317: default:
318: break;
319: }
320: return 0;
321: }
322:
323: static Key *
324: keygrab_ssh2(con *c)
325: {
326: int j;
327:
328: packet_set_connection(c->c_fd, c->c_fd);
329: enable_compat20();
330: myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = c->c_keytype == KT_DSA?
331: "ssh-dss": "ssh-rsa";
332: c->c_kex = kex_setup(myproposal);
333: c->c_kex->verify_host_key = hostjump;
334:
335: if (!(j = setjmp(kexjmp))) {
336: nonfatal_fatal = 1;
337: dispatch_run(DISPATCH_BLOCK, &c->c_kex->done, c->c_kex);
338: fprintf(stderr, "Impossible! dispatch_run() returned!\n");
339: exit(1);
340: }
341: nonfatal_fatal = 0;
342: xfree(c->c_kex);
343: c->c_kex = NULL;
344: packet_close();
345:
1.29 markus 346: return j < 0? NULL : kexjmp_key;
1.26 markus 347: }
348:
349: static void
350: keyprint(con *c, Key *key)
351: {
352: if (!key)
353: return;
354:
355: fprintf(stdout, "%s ", c->c_output_name ? c->c_output_name : c->c_name);
356: key_write(key, stdout);
1.1 markus 357: fputs("\n", stdout);
358: }
359:
1.24 itojun 360: static int
1.1 markus 361: tcpconnect(char *host)
362: {
363: struct addrinfo hints, *ai, *aitop;
364: char strport[NI_MAXSERV];
365: int gaierr, s = -1;
366:
1.26 markus 367: snprintf(strport, sizeof strport, "%d", ssh_port);
1.1 markus 368: memset(&hints, 0, sizeof(hints));
1.26 markus 369: hints.ai_family = IPv4or6;
1.1 markus 370: hints.ai_socktype = SOCK_STREAM;
371: if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
372: fatal("getaddrinfo %s: %s", host, gai_strerror(gaierr));
373: for (ai = aitop; ai; ai = ai->ai_next) {
374: s = socket(ai->ai_family, SOCK_STREAM, 0);
375: if (s < 0) {
376: error("socket: %s", strerror(errno));
377: continue;
378: }
1.7 markus 379: if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
1.1 markus 380: fatal("F_SETFL: %s", strerror(errno));
381: if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 &&
382: errno != EINPROGRESS)
383: error("connect (`%s'): %s", host, strerror(errno));
384: else
385: break;
386: close(s);
387: s = -1;
388: }
389: freeaddrinfo(aitop);
390: return s;
391: }
392:
1.24 itojun 393: static int
1.26 markus 394: conalloc(char *iname, char *oname, int keytype)
1.1 markus 395: {
396: int s;
397: char *namebase, *name, *namelist;
398:
399: namebase = namelist = xstrdup(iname);
400:
401: do {
402: name = xstrsep(&namelist, ",");
403: if (!name) {
1.9 markus 404: xfree(namebase);
1.1 markus 405: return (-1);
406: }
407: } while ((s = tcpconnect(name)) < 0);
408:
409: if (s >= maxfd)
1.4 markus 410: fatal("conalloc: fdno %d too high", s);
1.1 markus 411: if (fdcon[s].c_status)
1.4 markus 412: fatal("conalloc: attempt to reuse fdno %d", s);
1.1 markus 413:
414: fdcon[s].c_fd = s;
415: fdcon[s].c_status = CS_CON;
416: fdcon[s].c_namebase = namebase;
417: fdcon[s].c_name = name;
418: fdcon[s].c_namelist = namelist;
419: fdcon[s].c_output_name = xstrdup(oname);
420: fdcon[s].c_data = (char *) &fdcon[s].c_plen;
421: fdcon[s].c_len = 4;
422: fdcon[s].c_off = 0;
1.26 markus 423: fdcon[s].c_keytype = keytype;
1.1 markus 424: gettimeofday(&fdcon[s].c_tv, NULL);
425: fdcon[s].c_tv.tv_sec += timeout;
426: TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
1.19 millert 427: FD_SET(s, read_wait);
1.1 markus 428: ncon++;
429: return (s);
430: }
431:
1.24 itojun 432: static void
1.1 markus 433: confree(int s)
434: {
435: if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
1.4 markus 436: fatal("confree: attempt to free bad fdno %d", s);
1.18 deraadt 437: close(s);
1.9 markus 438: xfree(fdcon[s].c_namebase);
439: xfree(fdcon[s].c_output_name);
1.1 markus 440: if (fdcon[s].c_status == CS_KEYS)
1.9 markus 441: xfree(fdcon[s].c_data);
1.1 markus 442: fdcon[s].c_status = CS_UNUSED;
1.26 markus 443: fdcon[s].c_keytype = 0;
1.1 markus 444: TAILQ_REMOVE(&tq, &fdcon[s], c_link);
1.19 millert 445: FD_CLR(s, read_wait);
1.1 markus 446: ncon--;
447: }
448:
1.24 itojun 449: static void
1.1 markus 450: contouch(int s)
451: {
452: TAILQ_REMOVE(&tq, &fdcon[s], c_link);
453: gettimeofday(&fdcon[s].c_tv, NULL);
454: fdcon[s].c_tv.tv_sec += timeout;
455: TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
456: }
457:
1.24 itojun 458: static int
1.1 markus 459: conrecycle(int s)
460: {
461: int ret;
462: con *c = &fdcon[s];
463:
1.26 markus 464: ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype);
1.1 markus 465: confree(s);
466: return (ret);
467: }
468:
1.24 itojun 469: static void
1.1 markus 470: congreet(int s)
471: {
1.26 markus 472: char buf[256], *cp;
1.33 markus 473: char remote_version[sizeof buf];
1.21 millert 474: size_t bufsiz;
1.33 markus 475: int remote_major, remote_minor, n = 0;
1.1 markus 476: con *c = &fdcon[s];
477:
1.21 millert 478: bufsiz = sizeof(buf);
479: cp = buf;
1.27 markus 480: while (bufsiz-- && (n = read(s, cp, 1)) == 1 && *cp != '\n') {
481: if (*cp == '\r')
482: *cp = '\n';
1.21 millert 483: cp++;
1.27 markus 484: }
1.1 markus 485: if (n < 0) {
486: if (errno != ECONNREFUSED)
487: error("read (%s): %s", c->c_name, strerror(errno));
1.35 stevesk 488: conrecycle(s);
489: return;
490: }
491: if (n == 0) {
492: error("%s: Connection closed by remote host", c->c_name);
1.1 markus 493: conrecycle(s);
494: return;
495: }
1.21 millert 496: if (*cp != '\n' && *cp != '\r') {
1.1 markus 497: error("%s: bad greeting", c->c_name);
498: confree(s);
499: return;
500: }
1.21 millert 501: *cp = '\0';
1.33 markus 502: if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
503: &remote_major, &remote_minor, remote_version) == 3)
504: compat_datafellows(remote_version);
505: else
506: datafellows = 0;
1.26 markus 507: if (c->c_keytype != KT_RSA1) {
508: if (!ssh2_capable(remote_major, remote_minor)) {
509: debug("%s doesn't support ssh2", c->c_name);
510: confree(s);
511: return;
512: }
1.33 markus 513: } else if (remote_major != 1) {
514: debug("%s doesn't support ssh1", c->c_name);
515: confree(s);
516: return;
1.26 markus 517: }
1.27 markus 518: fprintf(stderr, "# %s %s\n", c->c_name, chop(buf));
1.26 markus 519: n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n",
520: c->c_keytype == KT_RSA1? PROTOCOL_MAJOR_1 : PROTOCOL_MAJOR_2,
521: c->c_keytype == KT_RSA1? PROTOCOL_MINOR_1 : PROTOCOL_MINOR_2);
1.18 deraadt 522: if (atomicio(write, s, buf, n) != n) {
1.1 markus 523: error("write (%s): %s", c->c_name, strerror(errno));
524: confree(s);
525: return;
526: }
1.26 markus 527: if (c->c_keytype != KT_RSA1) {
528: keyprint(c, keygrab_ssh2(c));
529: confree(s);
530: return;
531: }
1.1 markus 532: c->c_status = CS_SIZE;
533: contouch(s);
534: }
535:
1.24 itojun 536: static void
1.1 markus 537: conread(int s)
538: {
539: int n;
540: con *c = &fdcon[s];
541:
542: if (c->c_status == CS_CON) {
543: congreet(s);
544: return;
545: }
546: n = read(s, c->c_data + c->c_off, c->c_len - c->c_off);
547: if (n < 0) {
548: error("read (%s): %s", c->c_name, strerror(errno));
549: confree(s);
550: return;
551: }
552: c->c_off += n;
553:
554: if (c->c_off == c->c_len)
555: switch (c->c_status) {
556: case CS_SIZE:
557: c->c_plen = htonl(c->c_plen);
558: c->c_len = c->c_plen + 8 - (c->c_plen & 7);
559: c->c_off = 0;
560: c->c_data = xmalloc(c->c_len);
561: c->c_status = CS_KEYS;
562: break;
563: case CS_KEYS:
1.26 markus 564: keyprint(c, keygrab_ssh1(c));
1.1 markus 565: confree(s);
566: return;
567: break;
568: default:
1.4 markus 569: fatal("conread: invalid status %d", c->c_status);
1.1 markus 570: break;
571: }
572:
573: contouch(s);
574: }
575:
1.24 itojun 576: static void
1.1 markus 577: conloop(void)
578: {
1.19 millert 579: fd_set *r, *e;
1.1 markus 580: struct timeval seltime, now;
581: int i;
582: con *c;
583:
584: gettimeofday(&now, NULL);
1.36 itojun 585: c = TAILQ_FIRST(&tq);
1.1 markus 586:
1.18 deraadt 587: if (c && (c->c_tv.tv_sec > now.tv_sec ||
588: (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) {
1.1 markus 589: seltime = c->c_tv;
590: seltime.tv_sec -= now.tv_sec;
591: seltime.tv_usec -= now.tv_usec;
1.13 itojun 592: if (seltime.tv_usec < 0) {
1.1 markus 593: seltime.tv_usec += 1000000;
594: seltime.tv_sec--;
595: }
596: } else
597: seltime.tv_sec = seltime.tv_usec = 0;
598:
1.19 millert 599: r = xmalloc(read_wait_size);
600: memcpy(r, read_wait, read_wait_size);
601: e = xmalloc(read_wait_size);
602: memcpy(e, read_wait, read_wait_size);
603:
604: while (select(maxfd, r, NULL, e, &seltime) == -1 &&
1.16 deraadt 605: (errno == EAGAIN || errno == EINTR))
606: ;
607:
1.18 deraadt 608: for (i = 0; i < maxfd; i++) {
1.19 millert 609: if (FD_ISSET(i, e)) {
1.1 markus 610: error("%s: exception!", fdcon[i].c_name);
611: confree(i);
1.19 millert 612: } else if (FD_ISSET(i, r))
1.1 markus 613: conread(i);
1.18 deraadt 614: }
1.19 millert 615: xfree(r);
616: xfree(e);
1.1 markus 617:
1.36 itojun 618: c = TAILQ_FIRST(&tq);
1.18 deraadt 619: while (c && (c->c_tv.tv_sec < now.tv_sec ||
620: (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) {
1.1 markus 621: int s = c->c_fd;
1.18 deraadt 622:
1.36 itojun 623: c = TAILQ_NEXT(c, c_link);
1.1 markus 624: conrecycle(s);
625: }
626: }
627:
1.26 markus 628: static void
629: do_host(char *host)
1.1 markus 630: {
1.26 markus 631: char *name = strnnsep(&host, " \t\n");
632: int j;
1.1 markus 633:
1.31 markus 634: if (name == NULL)
635: return;
1.26 markus 636: for (j = KT_RSA1; j <= KT_RSA; j *= 2) {
637: if (get_keytypes & j) {
638: while (ncon >= MAXCON)
639: conloop();
640: conalloc(name, *host ? host : name, j);
1.1 markus 641: }
642: }
643: }
644:
1.34 markus 645: void
646: fatal(const char *fmt,...)
1.26 markus 647: {
1.34 markus 648: va_list args;
649: va_start(args, fmt);
650: do_log(SYSLOG_LEVEL_FATAL, fmt, args);
651: va_end(args);
1.26 markus 652: if (nonfatal_fatal)
653: longjmp(kexjmp, -1);
1.34 markus 654: else
655: fatal_cleanup();
1.26 markus 656: }
657:
658: static void
1.1 markus 659: usage(void)
660: {
1.26 markus 661: fprintf(stderr, "Usage: %s [options] host ...\n",
1.25 jakob 662: __progname);
663: fprintf(stderr, "Options:\n");
664: fprintf(stderr, " -f file Read hosts or addresses from file.\n");
1.26 markus 665: fprintf(stderr, " -p port Connect to the specified port.\n");
666: fprintf(stderr, " -t keytype Specify the host key type.\n");
667: fprintf(stderr, " -T timeout Set connection timeout.\n");
1.34 markus 668: fprintf(stderr, " -v Verbose; display verbose debugging messages.\n");
669: fprintf(stderr, " -4 Use IPv4 only.\n");
670: fprintf(stderr, " -6 Use IPv6 only.\n");
1.25 jakob 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: }