Annotation of src/usr.bin/ssh/ssh-keyscan.c, Revision 1.44.2.2
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.44.2.2! brad 10: RCSID("$OpenBSD: ssh-keyscan.c,v 1.50 2004/08/11 21:44:32 avsm 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;
1.39 deraadt 168: void *p;
1.1 markus 169:
170: lb->lineno++;
171: for (;;) {
172: /* Read a line */
173: if (!fgets(&lb->buf[n], lb->size - n, lb->stream)) {
174: if (ferror(lb->stream) && lb->errfun)
1.39 deraadt 175: (*lb->errfun)("%s: %s\n", lb->filename,
1.17 deraadt 176: strerror(errno));
1.1 markus 177: return (NULL);
178: }
179: n = strlen(lb->buf);
180:
181: /* Return it or an error if it fits */
182: if (n > 0 && lb->buf[n - 1] == '\n') {
183: lb->buf[n - 1] = '\0';
184: return (lb->buf);
185: }
186: if (n != lb->size - 1) {
187: if (lb->errfun)
1.39 deraadt 188: (*lb->errfun)("%s: skipping incomplete last line\n",
1.17 deraadt 189: lb->filename);
1.1 markus 190: return (NULL);
191: }
192: /* Double the buffer if we need more space */
1.39 deraadt 193: lb->size *= 2;
194: if ((p = realloc(lb->buf, lb->size)) == NULL) {
195: lb->size /= 2;
1.1 markus 196: if (lb->errfun)
1.39 deraadt 197: (*lb->errfun)("linebuf (%s): realloc failed\n",
1.17 deraadt 198: lb->filename);
1.1 markus 199: return (NULL);
200: }
1.39 deraadt 201: lb->buf = p;
1.1 markus 202: }
203: }
204:
1.24 itojun 205: static int
1.1 markus 206: fdlim_get(int hard)
207: {
208: struct rlimit rlfd;
1.17 deraadt 209:
1.1 markus 210: if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
211: return (-1);
212: if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY)
1.44.2.1 brad 213: return sysconf(_SC_OPEN_MAX);
1.1 markus 214: else
215: return hard ? rlfd.rlim_max : rlfd.rlim_cur;
216: }
217:
1.24 itojun 218: static int
1.1 markus 219: fdlim_set(int lim)
220: {
221: struct rlimit rlfd;
1.39 deraadt 222:
1.1 markus 223: if (lim <= 0)
224: return (-1);
225: if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
226: return (-1);
227: rlfd.rlim_cur = lim;
228: if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0)
229: return (-1);
230: return (0);
231: }
232:
233: /*
234: * This is an strsep function that returns a null field for adjacent
235: * separators. This is the same as the 4.4BSD strsep, but different from the
236: * one in the GNU libc.
237: */
1.24 itojun 238: static char *
1.1 markus 239: xstrsep(char **str, const char *delim)
240: {
241: char *s, *e;
242:
243: if (!**str)
244: return (NULL);
245:
246: s = *str;
247: e = s + strcspn(s, delim);
248:
249: if (*e != '\0')
250: *e++ = '\0';
251: *str = e;
252:
253: return (s);
254: }
255:
256: /*
257: * Get the next non-null token (like GNU strsep). Strsep() will return a
258: * null token for two adjacent separators, so we may have to loop.
259: */
1.24 itojun 260: static char *
1.1 markus 261: strnnsep(char **stringp, char *delim)
262: {
263: char *tok;
264:
265: do {
266: tok = xstrsep(stringp, delim);
267: } while (tok && *tok == '\0');
268: return (tok);
269: }
270:
1.26 markus 271: static Key *
272: keygrab_ssh1(con *c)
1.1 markus 273: {
274: static Key *rsa;
275: static Buffer msg;
276:
277: if (rsa == NULL) {
278: buffer_init(&msg);
279: rsa = key_new(KEY_RSA1);
280: }
1.26 markus 281: buffer_append(&msg, c->c_data, c->c_plen);
282: buffer_consume(&msg, 8 - (c->c_plen & 7)); /* padding */
1.1 markus 283: if (buffer_get_char(&msg) != (int) SSH_SMSG_PUBLIC_KEY) {
1.26 markus 284: error("%s: invalid packet type", c->c_name);
1.1 markus 285: buffer_clear(&msg);
1.26 markus 286: return NULL;
1.1 markus 287: }
288: buffer_consume(&msg, 8); /* cookie */
289:
290: /* server key */
291: (void) buffer_get_int(&msg);
292: buffer_get_bignum(&msg, rsa->rsa->e);
293: buffer_get_bignum(&msg, rsa->rsa->n);
294:
295: /* host key */
296: (void) buffer_get_int(&msg);
297: buffer_get_bignum(&msg, rsa->rsa->e);
298: buffer_get_bignum(&msg, rsa->rsa->n);
1.26 markus 299:
1.1 markus 300: buffer_clear(&msg);
301:
1.26 markus 302: return (rsa);
303: }
304:
305: static int
306: hostjump(Key *hostkey)
307: {
1.29 markus 308: kexjmp_key = hostkey;
309: longjmp(kexjmp, 1);
1.26 markus 310: }
311:
312: static int
313: ssh2_capable(int remote_major, int remote_minor)
314: {
315: switch (remote_major) {
316: case 1:
317: if (remote_minor == 99)
318: return 1;
319: break;
320: case 2:
321: return 1;
322: default:
323: break;
324: }
325: return 0;
326: }
327:
328: static Key *
329: keygrab_ssh2(con *c)
330: {
331: int j;
332:
333: packet_set_connection(c->c_fd, c->c_fd);
334: enable_compat20();
335: myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = c->c_keytype == KT_DSA?
336: "ssh-dss": "ssh-rsa";
337: c->c_kex = kex_setup(myproposal);
1.41 markus 338: c->c_kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
1.44.2.2! brad 339: c->c_kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
1.41 markus 340: c->c_kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
1.26 markus 341: c->c_kex->verify_host_key = hostjump;
342:
343: if (!(j = setjmp(kexjmp))) {
344: nonfatal_fatal = 1;
345: dispatch_run(DISPATCH_BLOCK, &c->c_kex->done, c->c_kex);
346: fprintf(stderr, "Impossible! dispatch_run() returned!\n");
347: exit(1);
348: }
349: nonfatal_fatal = 0;
350: xfree(c->c_kex);
351: c->c_kex = NULL;
352: packet_close();
353:
1.29 markus 354: return j < 0? NULL : kexjmp_key;
1.26 markus 355: }
356:
357: static void
358: keyprint(con *c, Key *key)
359: {
360: if (!key)
361: return;
362:
363: fprintf(stdout, "%s ", c->c_output_name ? c->c_output_name : c->c_name);
364: key_write(key, stdout);
1.1 markus 365: fputs("\n", stdout);
366: }
367:
1.24 itojun 368: static int
1.1 markus 369: tcpconnect(char *host)
370: {
371: struct addrinfo hints, *ai, *aitop;
372: char strport[NI_MAXSERV];
373: int gaierr, s = -1;
374:
1.26 markus 375: snprintf(strport, sizeof strport, "%d", ssh_port);
1.1 markus 376: memset(&hints, 0, sizeof(hints));
1.26 markus 377: hints.ai_family = IPv4or6;
1.1 markus 378: hints.ai_socktype = SOCK_STREAM;
379: if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
380: fatal("getaddrinfo %s: %s", host, gai_strerror(gaierr));
381: for (ai = aitop; ai; ai = ai->ai_next) {
1.42 markus 382: s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.1 markus 383: if (s < 0) {
384: error("socket: %s", strerror(errno));
385: continue;
386: }
1.44.2.2! brad 387: if (set_nonblock(s) == -1)
! 388: fatal("%s: set_nonblock(%d)", __func__, s);
1.1 markus 389: if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 &&
390: errno != EINPROGRESS)
391: error("connect (`%s'): %s", host, strerror(errno));
392: else
393: break;
394: close(s);
395: s = -1;
396: }
397: freeaddrinfo(aitop);
398: return s;
399: }
400:
1.24 itojun 401: static int
1.26 markus 402: conalloc(char *iname, char *oname, int keytype)
1.1 markus 403: {
1.39 deraadt 404: char *namebase, *name, *namelist;
1.1 markus 405: int s;
406:
407: namebase = namelist = xstrdup(iname);
408:
409: do {
410: name = xstrsep(&namelist, ",");
411: if (!name) {
1.9 markus 412: xfree(namebase);
1.1 markus 413: return (-1);
414: }
415: } while ((s = tcpconnect(name)) < 0);
416:
417: if (s >= maxfd)
1.4 markus 418: fatal("conalloc: fdno %d too high", s);
1.1 markus 419: if (fdcon[s].c_status)
1.4 markus 420: fatal("conalloc: attempt to reuse fdno %d", s);
1.1 markus 421:
422: fdcon[s].c_fd = s;
423: fdcon[s].c_status = CS_CON;
424: fdcon[s].c_namebase = namebase;
425: fdcon[s].c_name = name;
426: fdcon[s].c_namelist = namelist;
427: fdcon[s].c_output_name = xstrdup(oname);
428: fdcon[s].c_data = (char *) &fdcon[s].c_plen;
429: fdcon[s].c_len = 4;
430: fdcon[s].c_off = 0;
1.26 markus 431: fdcon[s].c_keytype = keytype;
1.1 markus 432: gettimeofday(&fdcon[s].c_tv, NULL);
433: fdcon[s].c_tv.tv_sec += timeout;
434: TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
1.19 millert 435: FD_SET(s, read_wait);
1.1 markus 436: ncon++;
437: return (s);
438: }
439:
1.24 itojun 440: static void
1.1 markus 441: confree(int s)
442: {
443: if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
1.4 markus 444: fatal("confree: attempt to free bad fdno %d", s);
1.18 deraadt 445: close(s);
1.9 markus 446: xfree(fdcon[s].c_namebase);
447: xfree(fdcon[s].c_output_name);
1.1 markus 448: if (fdcon[s].c_status == CS_KEYS)
1.9 markus 449: xfree(fdcon[s].c_data);
1.1 markus 450: fdcon[s].c_status = CS_UNUSED;
1.26 markus 451: fdcon[s].c_keytype = 0;
1.1 markus 452: TAILQ_REMOVE(&tq, &fdcon[s], c_link);
1.19 millert 453: FD_CLR(s, read_wait);
1.1 markus 454: ncon--;
455: }
456:
1.24 itojun 457: static void
1.1 markus 458: contouch(int s)
459: {
460: TAILQ_REMOVE(&tq, &fdcon[s], c_link);
461: gettimeofday(&fdcon[s].c_tv, NULL);
462: fdcon[s].c_tv.tv_sec += timeout;
463: TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
464: }
465:
1.24 itojun 466: static int
1.1 markus 467: conrecycle(int s)
468: {
1.39 deraadt 469: con *c = &fdcon[s];
1.1 markus 470: int ret;
471:
1.26 markus 472: ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype);
1.1 markus 473: confree(s);
474: return (ret);
475: }
476:
1.24 itojun 477: static void
1.1 markus 478: congreet(int s)
479: {
1.44.2.2! brad 480: int remote_major = 0, remote_minor = 0, n = 0;
1.26 markus 481: char buf[256], *cp;
1.33 markus 482: char remote_version[sizeof buf];
1.21 millert 483: size_t bufsiz;
1.1 markus 484: con *c = &fdcon[s];
485:
1.21 millert 486: bufsiz = sizeof(buf);
487: cp = buf;
1.44.2.2! brad 488: while (bufsiz-- && (n = atomicio(read, s, cp, 1)) == 1 && *cp != '\n') {
1.27 markus 489: if (*cp == '\r')
490: *cp = '\n';
1.21 millert 491: cp++;
1.27 markus 492: }
1.1 markus 493: if (n < 0) {
494: if (errno != ECONNREFUSED)
495: error("read (%s): %s", c->c_name, strerror(errno));
1.35 stevesk 496: conrecycle(s);
497: return;
498: }
499: if (n == 0) {
500: error("%s: Connection closed by remote host", c->c_name);
1.1 markus 501: conrecycle(s);
502: return;
503: }
1.21 millert 504: if (*cp != '\n' && *cp != '\r') {
1.1 markus 505: error("%s: bad greeting", c->c_name);
506: confree(s);
507: return;
508: }
1.21 millert 509: *cp = '\0';
1.33 markus 510: if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
511: &remote_major, &remote_minor, remote_version) == 3)
512: compat_datafellows(remote_version);
513: else
514: datafellows = 0;
1.26 markus 515: if (c->c_keytype != KT_RSA1) {
516: if (!ssh2_capable(remote_major, remote_minor)) {
517: debug("%s doesn't support ssh2", c->c_name);
518: confree(s);
519: return;
520: }
1.33 markus 521: } else if (remote_major != 1) {
522: debug("%s doesn't support ssh1", c->c_name);
523: confree(s);
524: return;
1.26 markus 525: }
1.27 markus 526: fprintf(stderr, "# %s %s\n", c->c_name, chop(buf));
1.26 markus 527: n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n",
528: c->c_keytype == KT_RSA1? PROTOCOL_MAJOR_1 : PROTOCOL_MAJOR_2,
529: c->c_keytype == KT_RSA1? PROTOCOL_MINOR_1 : PROTOCOL_MINOR_2);
1.44 deraadt 530: if (atomicio(vwrite, s, buf, n) != n) {
1.1 markus 531: error("write (%s): %s", c->c_name, strerror(errno));
532: confree(s);
533: return;
534: }
1.26 markus 535: if (c->c_keytype != KT_RSA1) {
536: keyprint(c, keygrab_ssh2(c));
537: confree(s);
538: return;
539: }
1.1 markus 540: c->c_status = CS_SIZE;
541: contouch(s);
542: }
543:
1.24 itojun 544: static void
1.1 markus 545: conread(int s)
546: {
1.39 deraadt 547: con *c = &fdcon[s];
1.1 markus 548: int n;
549:
550: if (c->c_status == CS_CON) {
551: congreet(s);
552: return;
553: }
1.44.2.2! brad 554: n = atomicio(read, s, c->c_data + c->c_off, c->c_len - c->c_off);
1.1 markus 555: if (n < 0) {
556: error("read (%s): %s", c->c_name, strerror(errno));
557: confree(s);
558: return;
559: }
560: c->c_off += n;
561:
562: if (c->c_off == c->c_len)
563: switch (c->c_status) {
564: case CS_SIZE:
565: c->c_plen = htonl(c->c_plen);
566: c->c_len = c->c_plen + 8 - (c->c_plen & 7);
567: c->c_off = 0;
568: c->c_data = xmalloc(c->c_len);
569: c->c_status = CS_KEYS;
570: break;
571: case CS_KEYS:
1.26 markus 572: keyprint(c, keygrab_ssh1(c));
1.1 markus 573: confree(s);
574: return;
575: break;
576: default:
1.4 markus 577: fatal("conread: invalid status %d", c->c_status);
1.1 markus 578: break;
579: }
580:
581: contouch(s);
582: }
583:
1.24 itojun 584: static void
1.1 markus 585: conloop(void)
586: {
1.39 deraadt 587: struct timeval seltime, now;
1.19 millert 588: fd_set *r, *e;
1.39 deraadt 589: con *c;
1.1 markus 590: int i;
591:
592: gettimeofday(&now, NULL);
1.36 itojun 593: c = TAILQ_FIRST(&tq);
1.1 markus 594:
1.18 deraadt 595: if (c && (c->c_tv.tv_sec > now.tv_sec ||
596: (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) {
1.1 markus 597: seltime = c->c_tv;
598: seltime.tv_sec -= now.tv_sec;
599: seltime.tv_usec -= now.tv_usec;
1.13 itojun 600: if (seltime.tv_usec < 0) {
1.1 markus 601: seltime.tv_usec += 1000000;
602: seltime.tv_sec--;
603: }
604: } else
605: seltime.tv_sec = seltime.tv_usec = 0;
606:
1.19 millert 607: r = xmalloc(read_wait_size);
608: memcpy(r, read_wait, read_wait_size);
609: e = xmalloc(read_wait_size);
610: memcpy(e, read_wait, read_wait_size);
611:
612: while (select(maxfd, r, NULL, e, &seltime) == -1 &&
1.16 deraadt 613: (errno == EAGAIN || errno == EINTR))
614: ;
615:
1.18 deraadt 616: for (i = 0; i < maxfd; i++) {
1.19 millert 617: if (FD_ISSET(i, e)) {
1.1 markus 618: error("%s: exception!", fdcon[i].c_name);
619: confree(i);
1.19 millert 620: } else if (FD_ISSET(i, r))
1.1 markus 621: conread(i);
1.18 deraadt 622: }
1.19 millert 623: xfree(r);
624: xfree(e);
1.1 markus 625:
1.36 itojun 626: c = TAILQ_FIRST(&tq);
1.18 deraadt 627: while (c && (c->c_tv.tv_sec < now.tv_sec ||
628: (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) {
1.1 markus 629: int s = c->c_fd;
1.18 deraadt 630:
1.36 itojun 631: c = TAILQ_NEXT(c, c_link);
1.1 markus 632: conrecycle(s);
633: }
634: }
635:
1.26 markus 636: static void
637: do_host(char *host)
1.1 markus 638: {
1.26 markus 639: char *name = strnnsep(&host, " \t\n");
640: int j;
1.1 markus 641:
1.31 markus 642: if (name == NULL)
643: return;
1.26 markus 644: for (j = KT_RSA1; j <= KT_RSA; j *= 2) {
645: if (get_keytypes & j) {
646: while (ncon >= MAXCON)
647: conloop();
648: conalloc(name, *host ? host : name, j);
1.1 markus 649: }
650: }
651: }
652:
1.34 markus 653: void
654: fatal(const char *fmt,...)
1.26 markus 655: {
1.34 markus 656: va_list args;
1.39 deraadt 657:
1.34 markus 658: va_start(args, fmt);
659: do_log(SYSLOG_LEVEL_FATAL, fmt, args);
660: va_end(args);
1.26 markus 661: if (nonfatal_fatal)
662: longjmp(kexjmp, -1);
1.34 markus 663: else
1.44.2.1 brad 664: exit(255);
1.26 markus 665: }
666:
667: static void
1.1 markus 668: usage(void)
669: {
1.43 deraadt 670: fprintf(stderr, "usage: %s [-v46] [-p port] [-T timeout] [-t type] [-f file]\n"
1.39 deraadt 671: "\t\t [host | addrlist namelist] [...]\n",
1.25 jakob 672: __progname);
673: exit(1);
1.1 markus 674: }
675:
676: int
677: main(int argc, char **argv)
678: {
1.26 markus 679: int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO;
680: int opt, fopt_count = 0;
681: char *tname;
682:
683: extern int optind;
684: extern char *optarg;
1.1 markus 685:
686: TAILQ_INIT(&tq);
687:
1.26 markus 688: if (argc <= 1)
1.1 markus 689: usage();
690:
1.26 markus 691: while ((opt = getopt(argc, argv, "v46p:T:t:f:")) != -1) {
692: switch (opt) {
693: case 'p':
694: ssh_port = a2port(optarg);
695: if (ssh_port == 0) {
696: fprintf(stderr, "Bad port '%s'\n", optarg);
697: exit(1);
698: }
699: break;
700: case 'T':
1.38 stevesk 701: timeout = convtime(optarg);
702: if (timeout == -1 || timeout == 0) {
703: fprintf(stderr, "Bad timeout '%s'\n", optarg);
1.1 markus 704: usage();
1.38 stevesk 705: }
1.26 markus 706: break;
707: case 'v':
708: if (!debug_flag) {
709: debug_flag = 1;
710: log_level = SYSLOG_LEVEL_DEBUG1;
711: }
712: else if (log_level < SYSLOG_LEVEL_DEBUG3)
713: log_level++;
714: else
715: fatal("Too high debugging level.");
716: break;
717: case 'f':
718: if (strcmp(optarg, "-") == 0)
719: optarg = NULL;
720: argv[fopt_count++] = optarg;
721: break;
722: case 't':
723: get_keytypes = 0;
724: tname = strtok(optarg, ",");
725: while (tname) {
726: int type = key_type_from_name(tname);
727: switch (type) {
728: case KEY_RSA1:
729: get_keytypes |= KT_RSA1;
730: break;
731: case KEY_DSA:
732: get_keytypes |= KT_DSA;
733: break;
734: case KEY_RSA:
735: get_keytypes |= KT_RSA;
736: break;
737: case KEY_UNSPEC:
1.32 stevesk 738: fatal("unknown key type %s", tname);
1.26 markus 739: }
740: tname = strtok(NULL, ",");
741: }
742: break;
743: case '4':
744: IPv4or6 = AF_INET;
745: break;
746: case '6':
747: IPv4or6 = AF_INET6;
748: break;
749: case '?':
750: default:
751: usage();
1.1 markus 752: }
753: }
1.26 markus 754: if (optind == argc && !fopt_count)
1.1 markus 755: usage();
756:
1.26 markus 757: log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1);
758:
1.1 markus 759: maxfd = fdlim_get(1);
760: if (maxfd < 0)
1.4 markus 761: fatal("%s: fdlim_get: bad value", __progname);
1.1 markus 762: if (maxfd > MAXMAXFD)
763: maxfd = MAXMAXFD;
1.18 deraadt 764: if (MAXCON <= 0)
1.4 markus 765: fatal("%s: not enough file descriptors", __progname);
1.1 markus 766: if (maxfd > fdlim_get(0))
767: fdlim_set(maxfd);
768: fdcon = xmalloc(maxfd * sizeof(con));
1.15 itojun 769: memset(fdcon, 0, maxfd * sizeof(con));
1.19 millert 770:
771: read_wait_size = howmany(maxfd, NFDBITS) * sizeof(fd_mask);
772: read_wait = xmalloc(read_wait_size);
773: memset(read_wait, 0, read_wait_size);
1.1 markus 774:
1.26 markus 775: if (fopt_count) {
776: Linebuf *lb;
777: char *line;
778: int j;
779:
780: for (j = 0; j < fopt_count; j++) {
781: lb = Linebuf_alloc(argv[j], error);
1.28 danh 782: if (!lb)
783: continue;
1.26 markus 784: while ((line = Linebuf_getline(lb)) != NULL)
785: do_host(line);
786: Linebuf_free(lb);
787: }
788: }
789:
790: while (optind < argc)
791: do_host(argv[optind++]);
1.1 markus 792:
793: while (ncon > 0)
794: conloop();
795:
796: return (0);
797: }