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