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