Annotation of src/usr.bin/ssh/ssh-keyscan.c, Revision 1.152
1.152 ! djm 1: /* $OpenBSD: ssh-keyscan.c,v 1.151 2023/02/10 06:41:53 jmc 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.152 ! djm 296: int r = 0;
1.51 djm 297:
1.126 djm 298: found_one = 1;
299:
1.117 djm 300: if (print_sshfp) {
1.150 djm 301: export_dns_rr(host, key, stdout, 0, hashalg);
1.117 djm 302: return;
303: }
304:
1.108 djm 305: hostport = put_host_port(host, ssh_port);
1.109 djm 306: lowercase(hostport);
1.144 djm 307: if (hash_hosts && (hashed = host_hash(hostport, NULL, 0)) == NULL)
1.51 djm 308: fatal("host_hash failed");
1.108 djm 309: known_host = hash_hosts ? hashed : hostport;
1.103 djm 310: if (!get_cert)
1.152 ! djm 311: r = fprintf(stdout, "%s ", known_host);
! 312: if (r >= 0 && sshkey_write(key, stdout) == 0)
! 313: (void)fputs("\n", stdout);
1.140 dtucker 314: free(hashed);
1.101 djm 315: free(hostport);
1.102 djm 316: }
317:
318: static void
319: keyprint(con *c, struct sshkey *key)
320: {
321: char *hosts = c->c_output_name ? c->c_output_name : c->c_name;
322: char *host, *ohosts;
323:
324: if (key == NULL)
325: return;
1.103 djm 326: if (get_cert || (!hash_hosts && ssh_port == SSH_DEFAULT_PORT)) {
1.102 djm 327: keyprint_one(hosts, key);
328: return;
329: }
330: ohosts = hosts = xstrdup(hosts);
331: while ((host = strsep(&hosts, ",")) != NULL)
332: keyprint_one(host, key);
333: free(ohosts);
1.1 markus 334: }
335:
1.24 itojun 336: static int
1.1 markus 337: tcpconnect(char *host)
338: {
339: struct addrinfo hints, *ai, *aitop;
340: char strport[NI_MAXSERV];
341: int gaierr, s = -1;
342:
1.26 markus 343: snprintf(strport, sizeof strport, "%d", ssh_port);
1.1 markus 344: memset(&hints, 0, sizeof(hints));
1.26 markus 345: hints.ai_family = IPv4or6;
1.1 markus 346: hints.ai_socktype = SOCK_STREAM;
1.97 djm 347: if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
348: error("getaddrinfo %s: %s", host, ssh_gai_strerror(gaierr));
349: return -1;
350: }
1.1 markus 351: for (ai = aitop; ai; ai = ai->ai_next) {
1.81 dtucker 352: s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.128 deraadt 353: if (s == -1) {
1.1 markus 354: error("socket: %s", strerror(errno));
355: continue;
356: }
1.49 djm 357: if (set_nonblock(s) == -1)
1.136 djm 358: fatal_f("set_nonblock(%d)", s);
1.128 deraadt 359: if (connect(s, ai->ai_addr, ai->ai_addrlen) == -1 &&
1.1 markus 360: errno != EINPROGRESS)
361: error("connect (`%s'): %s", host, strerror(errno));
362: else
363: break;
364: close(s);
365: s = -1;
366: }
367: freeaddrinfo(aitop);
368: return s;
369: }
370:
1.24 itojun 371: static int
1.147 djm 372: conalloc(const char *iname, const char *oname, int keytype)
1.1 markus 373: {
1.39 deraadt 374: char *namebase, *name, *namelist;
1.1 markus 375: int s;
376:
377: namebase = namelist = xstrdup(iname);
378:
379: do {
380: name = xstrsep(&namelist, ",");
381: if (!name) {
1.87 djm 382: free(namebase);
1.1 markus 383: return (-1);
384: }
385: } while ((s = tcpconnect(name)) < 0);
386:
387: if (s >= maxfd)
1.4 markus 388: fatal("conalloc: fdno %d too high", s);
1.1 markus 389: if (fdcon[s].c_status)
1.4 markus 390: fatal("conalloc: attempt to reuse fdno %d", s);
1.1 markus 391:
1.136 djm 392: debug3_f("oname %s kt %d", oname, keytype);
1.1 markus 393: fdcon[s].c_fd = s;
394: fdcon[s].c_status = CS_CON;
395: fdcon[s].c_namebase = namebase;
396: fdcon[s].c_name = name;
397: fdcon[s].c_namelist = namelist;
398: fdcon[s].c_output_name = xstrdup(oname);
399: fdcon[s].c_data = (char *) &fdcon[s].c_plen;
400: fdcon[s].c_len = 4;
401: fdcon[s].c_off = 0;
1.26 markus 402: fdcon[s].c_keytype = keytype;
1.141 deraadt 403: monotime_ts(&fdcon[s].c_ts);
404: fdcon[s].c_ts.tv_sec += timeout;
1.1 markus 405: TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
1.141 deraadt 406: read_wait[s].fd = s;
407: read_wait[s].events = POLLIN;
1.1 markus 408: ncon++;
409: return (s);
410: }
411:
1.24 itojun 412: static void
1.1 markus 413: confree(int s)
414: {
415: if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
1.4 markus 416: fatal("confree: attempt to free bad fdno %d", s);
1.87 djm 417: free(fdcon[s].c_namebase);
418: free(fdcon[s].c_output_name);
1.1 markus 419: if (fdcon[s].c_status == CS_KEYS)
1.87 djm 420: free(fdcon[s].c_data);
1.1 markus 421: fdcon[s].c_status = CS_UNUSED;
1.26 markus 422: fdcon[s].c_keytype = 0;
1.95 markus 423: if (fdcon[s].c_ssh) {
424: ssh_packet_close(fdcon[s].c_ssh);
425: free(fdcon[s].c_ssh);
426: fdcon[s].c_ssh = NULL;
1.115 dtucker 427: } else
428: close(s);
1.1 markus 429: TAILQ_REMOVE(&tq, &fdcon[s], c_link);
1.141 deraadt 430: read_wait[s].fd = -1;
431: read_wait[s].events = 0;
1.1 markus 432: ncon--;
433: }
434:
1.24 itojun 435: static void
1.1 markus 436: contouch(int s)
437: {
438: TAILQ_REMOVE(&tq, &fdcon[s], c_link);
1.141 deraadt 439: monotime_ts(&fdcon[s].c_ts);
440: fdcon[s].c_ts.tv_sec += timeout;
1.1 markus 441: TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
442: }
443:
1.24 itojun 444: static int
1.1 markus 445: conrecycle(int s)
446: {
1.39 deraadt 447: con *c = &fdcon[s];
1.1 markus 448: int ret;
449:
1.26 markus 450: ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype);
1.1 markus 451: confree(s);
452: return (ret);
453: }
454:
1.24 itojun 455: static void
1.1 markus 456: congreet(int s)
457: {
1.55 djm 458: int n = 0, remote_major = 0, remote_minor = 0;
1.26 markus 459: char buf[256], *cp;
1.33 markus 460: char remote_version[sizeof buf];
1.55 djm 461: size_t bufsiz;
1.1 markus 462: con *c = &fdcon[s];
463:
1.112 djm 464: /* send client banner */
465: n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n",
466: PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2);
467: if (n < 0 || (size_t)n >= sizeof(buf)) {
468: error("snprintf: buffer too small");
469: confree(s);
470: return;
471: }
472: if (atomicio(vwrite, s, buf, n) != (size_t)n) {
473: error("write (%s): %s", c->c_name, strerror(errno));
474: confree(s);
475: return;
476: }
477:
1.146 dtucker 478: /*
479: * Read the server banner as per RFC4253 section 4.2. The "SSH-"
1.149 jmc 480: * protocol identification string may be preceeded by an arbitrarily
1.146 dtucker 481: * large banner which we must read and ignore. Loop while reading
482: * newline-terminated lines until we have one starting with "SSH-".
483: * The ID string cannot be longer than 255 characters although the
484: * preceeding banner lines may (in which case they'll be discarded
485: * in multiple iterations of the outer loop).
486: */
1.57 djm 487: for (;;) {
488: memset(buf, '\0', sizeof(buf));
489: bufsiz = sizeof(buf);
490: cp = buf;
491: while (bufsiz-- &&
492: (n = atomicio(read, s, cp, 1)) == 1 && *cp != '\n') {
493: if (*cp == '\r')
494: *cp = '\n';
495: cp++;
496: }
497: if (n != 1 || strncmp(buf, "SSH-", 4) == 0)
498: break;
1.27 markus 499: }
1.54 avsm 500: if (n == 0) {
501: switch (errno) {
502: case EPIPE:
503: error("%s: Connection closed by remote host", c->c_name);
504: break;
505: case ECONNREFUSED:
506: break;
507: default:
1.1 markus 508: error("read (%s): %s", c->c_name, strerror(errno));
1.54 avsm 509: break;
510: }
1.1 markus 511: conrecycle(s);
1.146 dtucker 512: return;
513: }
514: if (cp >= buf + sizeof(buf)) {
515: error("%s: greeting exceeds allowable length", c->c_name);
516: confree(s);
1.1 markus 517: return;
518: }
1.21 millert 519: if (*cp != '\n' && *cp != '\r') {
1.1 markus 520: error("%s: bad greeting", c->c_name);
521: confree(s);
522: return;
523: }
1.21 millert 524: *cp = '\0';
1.98 djm 525: if ((c->c_ssh = ssh_packet_set_connection(NULL, s, s)) == NULL)
526: fatal("ssh_packet_set_connection failed");
1.99 djm 527: ssh_packet_set_timeout(c->c_ssh, timeout, 1);
1.95 markus 528: ssh_set_app_data(c->c_ssh, c); /* back link */
1.139 djm 529: c->c_ssh->compat = 0;
1.33 markus 530: if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
531: &remote_major, &remote_minor, remote_version) == 3)
1.139 djm 532: compat_banner(c->c_ssh, remote_version);
1.112 djm 533: if (!ssh2_capable(remote_major, remote_minor)) {
534: debug("%s doesn't support ssh2", c->c_name);
1.33 markus 535: confree(s);
536: return;
1.26 markus 537: }
1.117 djm 538: fprintf(stderr, "%c %s:%d %s\n", print_sshfp ? ';' : '#',
539: c->c_name, ssh_port, chop(buf));
1.112 djm 540: keygrab_ssh2(c);
541: confree(s);
1.1 markus 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.54 avsm 548: size_t n;
1.1 markus 549:
550: if (c->c_status == CS_CON) {
551: congreet(s);
552: return;
553: }
1.50 avsm 554: n = atomicio(read, s, c->c_data + c->c_off, c->c_len - c->c_off);
1.54 avsm 555: if (n == 0) {
1.1 markus 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: default:
1.4 markus 572: fatal("conread: invalid status %d", c->c_status);
1.1 markus 573: break;
574: }
575:
576: contouch(s);
577: }
578:
1.24 itojun 579: static void
1.1 markus 580: conloop(void)
581: {
1.141 deraadt 582: struct timespec seltime, now;
1.39 deraadt 583: con *c;
1.1 markus 584: int i;
585:
1.141 deraadt 586: monotime_ts(&now);
1.36 itojun 587: c = TAILQ_FIRST(&tq);
1.1 markus 588:
1.141 deraadt 589: if (c && timespeccmp(&c->c_ts, &now, >))
590: timespecsub(&c->c_ts, &now, &seltime);
1.132 cheloha 591: else
1.141 deraadt 592: timespecclear(&seltime);
1.1 markus 593:
1.145 deraadt 594: while (ppoll(read_wait, maxfd, &seltime, NULL) == -1) {
595: if (errno == EAGAIN || errno == EINTR)
596: continue;
597: error("poll error");
598: }
1.16 deraadt 599:
1.18 deraadt 600: for (i = 0; i < maxfd; i++) {
1.141 deraadt 601: if (read_wait[i].revents & (POLLHUP|POLLERR|POLLNVAL))
1.1 markus 602: confree(i);
1.143 djm 603: else if (read_wait[i].revents & (POLLIN|POLLHUP))
1.1 markus 604: conread(i);
1.18 deraadt 605: }
1.1 markus 606:
1.36 itojun 607: c = TAILQ_FIRST(&tq);
1.141 deraadt 608: while (c && timespeccmp(&c->c_ts, &now, <)) {
1.1 markus 609: int s = c->c_fd;
1.18 deraadt 610:
1.36 itojun 611: c = TAILQ_NEXT(c, c_link);
1.1 markus 612: conrecycle(s);
613: }
614: }
615:
1.26 markus 616: static void
1.147 djm 617: do_one_host(char *host)
1.1 markus 618: {
1.26 markus 619: char *name = strnnsep(&host, " \t\n");
620: int j;
1.1 markus 621:
1.31 markus 622: if (name == NULL)
623: return;
1.112 djm 624: for (j = KT_MIN; j <= KT_MAX; j *= 2) {
1.26 markus 625: if (get_keytypes & j) {
626: while (ncon >= MAXCON)
627: conloop();
628: conalloc(name, *host ? host : name, j);
1.1 markus 629: }
1.147 djm 630: }
631: }
632:
633: static void
634: do_host(char *host)
635: {
636: char daddr[128];
637: struct xaddr addr, end_addr;
638: u_int masklen;
639:
640: if (host == NULL)
641: return;
642: if (addr_pton_cidr(host, &addr, &masklen) != 0) {
643: /* Assume argument is a hostname */
644: do_one_host(host);
645: } else {
646: /* Argument is a CIDR range */
647: debug("CIDR range %s", host);
648: end_addr = addr;
649: if (addr_host_to_all1s(&end_addr, masklen) != 0)
650: goto badaddr;
651: /*
652: * Note: we deliberately include the all-zero/ones addresses.
653: */
654: for (;;) {
655: if (addr_ntop(&addr, daddr, sizeof(daddr)) != 0) {
656: badaddr:
657: error("Invalid address %s", host);
658: return;
659: }
660: debug("CIDR expand: address %s", daddr);
661: do_one_host(daddr);
662: if (addr_cmp(&addr, &end_addr) == 0)
663: break;
664: addr_increment(&addr);
665: };
1.1 markus 666: }
667: }
668:
1.34 markus 669: void
1.134 djm 670: sshfatal(const char *file, const char *func, int line, int showfunc,
1.135 djm 671: LogLevel level, const char *suffix, const char *fmt, ...)
1.26 markus 672: {
1.34 markus 673: va_list args;
1.39 deraadt 674:
1.34 markus 675: va_start(args, fmt);
1.137 djm 676: sshlogv(file, func, line, showfunc, level, suffix, fmt, args);
1.34 markus 677: va_end(args);
1.133 djm 678: cleanup_exit(255);
1.26 markus 679: }
680:
681: static void
1.1 markus 682: usage(void)
683: {
1.77 sobrado 684: fprintf(stderr,
1.151 jmc 685: "usage: ssh-keyscan [-46cDHv] [-f file] [-O option] [-p port] [-T timeout]\n"
686: " [-t type] [host | addrlist namelist]\n");
1.25 jakob 687: exit(1);
1.1 markus 688: }
689:
690: int
691: main(int argc, char **argv)
692: {
1.26 markus 693: int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO;
1.82 djm 694: int opt, fopt_count = 0, j;
1.120 markus 695: char *tname, *cp, *line = NULL;
696: size_t linesize = 0;
1.82 djm 697: FILE *fp;
1.26 markus 698:
699: extern int optind;
700: extern char *optarg;
1.1 markus 701:
702: TAILQ_INIT(&tq);
1.56 djm 703:
704: /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
705: sanitise_stdfd();
1.1 markus 706:
1.26 markus 707: if (argc <= 1)
1.1 markus 708: usage();
709:
1.150 djm 710: while ((opt = getopt(argc, argv, "cDHv46O:p:T:t:f:")) != -1) {
1.26 markus 711: switch (opt) {
1.51 djm 712: case 'H':
713: hash_hosts = 1;
1.103 djm 714: break;
715: case 'c':
716: get_cert = 1;
1.117 djm 717: break;
718: case 'D':
719: print_sshfp = 1;
1.51 djm 720: break;
1.26 markus 721: case 'p':
722: ssh_port = a2port(optarg);
1.78 djm 723: if (ssh_port <= 0) {
1.26 markus 724: fprintf(stderr, "Bad port '%s'\n", optarg);
725: exit(1);
726: }
727: break;
728: case 'T':
1.38 stevesk 729: timeout = convtime(optarg);
730: if (timeout == -1 || timeout == 0) {
731: fprintf(stderr, "Bad timeout '%s'\n", optarg);
1.1 markus 732: usage();
1.38 stevesk 733: }
1.26 markus 734: break;
735: case 'v':
736: if (!debug_flag) {
737: debug_flag = 1;
738: log_level = SYSLOG_LEVEL_DEBUG1;
739: }
740: else if (log_level < SYSLOG_LEVEL_DEBUG3)
741: log_level++;
742: else
743: fatal("Too high debugging level.");
744: break;
745: case 'f':
746: if (strcmp(optarg, "-") == 0)
747: optarg = NULL;
748: argv[fopt_count++] = optarg;
1.150 djm 749: break;
750: case 'O':
751: /* Maybe other misc options in the future too */
752: if (strncmp(optarg, "hashalg=", 8) != 0)
753: fatal("Unsupported -O option");
754: if ((hashalg = ssh_digest_alg_by_name(
755: optarg + 8)) == -1)
756: fatal("Unsupported hash algorithm");
1.26 markus 757: break;
758: case 't':
759: get_keytypes = 0;
760: tname = strtok(optarg, ",");
761: while (tname) {
1.95 markus 762: int type = sshkey_type_from_name(tname);
1.107 djm 763:
1.26 markus 764: switch (type) {
765: case KEY_DSA:
766: get_keytypes |= KT_DSA;
1.83 djm 767: break;
768: case KEY_ECDSA:
769: get_keytypes |= KT_ECDSA;
1.26 markus 770: break;
771: case KEY_RSA:
772: get_keytypes |= KT_RSA;
1.89 markus 773: break;
774: case KEY_ED25519:
775: get_keytypes |= KT_ED25519;
1.118 markus 776: break;
777: case KEY_XMSS:
778: get_keytypes |= KT_XMSS;
1.131 djm 779: break;
780: case KEY_ED25519_SK:
781: get_keytypes |= KT_ED25519_SK;
782: break;
783: case KEY_ECDSA_SK:
784: get_keytypes |= KT_ECDSA_SK;
1.26 markus 785: break;
786: case KEY_UNSPEC:
1.107 djm 787: default:
788: fatal("Unknown key type \"%s\"", tname);
1.26 markus 789: }
790: tname = strtok(NULL, ",");
791: }
792: break;
793: case '4':
794: IPv4or6 = AF_INET;
795: break;
796: case '6':
797: IPv4or6 = AF_INET6;
798: break;
799: default:
800: usage();
1.1 markus 801: }
802: }
1.26 markus 803: if (optind == argc && !fopt_count)
1.1 markus 804: usage();
805:
1.26 markus 806: log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1);
807:
1.1 markus 808: maxfd = fdlim_get(1);
809: if (maxfd < 0)
1.4 markus 810: fatal("%s: fdlim_get: bad value", __progname);
1.1 markus 811: if (maxfd > MAXMAXFD)
812: maxfd = MAXMAXFD;
1.18 deraadt 813: if (MAXCON <= 0)
1.4 markus 814: fatal("%s: not enough file descriptors", __progname);
1.1 markus 815: if (maxfd > fdlim_get(0))
816: fdlim_set(maxfd);
1.63 djm 817: fdcon = xcalloc(maxfd, sizeof(con));
1.141 deraadt 818: read_wait = xcalloc(maxfd, sizeof(struct pollfd));
1.142 deraadt 819: for (j = 0; j < maxfd; j++)
820: read_wait[j].fd = -1;
1.1 markus 821:
1.82 djm 822: for (j = 0; j < fopt_count; j++) {
823: if (argv[j] == NULL)
824: fp = stdin;
825: else if ((fp = fopen(argv[j], "r")) == NULL)
1.136 djm 826: fatal("%s: %s: %s", __progname, argv[j], strerror(errno));
1.82 djm 827:
1.120 markus 828: while (getline(&line, &linesize, fp) != -1) {
1.82 djm 829: /* Chomp off trailing whitespace and comments */
830: if ((cp = strchr(line, '#')) == NULL)
831: cp = line + strlen(line) - 1;
832: while (cp >= line) {
833: if (*cp == ' ' || *cp == '\t' ||
834: *cp == '\n' || *cp == '#')
835: *cp-- = '\0';
836: else
837: break;
838: }
839:
840: /* Skip empty lines */
841: if (*line == '\0')
1.28 danh 842: continue;
1.82 djm 843:
844: do_host(line);
1.26 markus 845: }
1.82 djm 846:
847: if (ferror(fp))
1.136 djm 848: fatal("%s: %s: %s", __progname, argv[j], strerror(errno));
1.82 djm 849:
850: fclose(fp);
1.26 markus 851: }
1.120 markus 852: free(line);
1.26 markus 853:
854: while (optind < argc)
855: do_host(argv[optind++]);
1.1 markus 856:
857: while (ncon > 0)
858: conloop();
859:
1.126 djm 860: return found_one ? 0 : 1;
1.1 markus 861: }