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