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