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