Annotation of src/usr.bin/ssh/ssh-keyscan.c, Revision 1.110
1.110 ! djm 1: /* $OpenBSD: ssh-keyscan.c,v 1.109 2017/03/10 04:26:06 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.26 markus 172:
173: static int
1.95 markus 174: key_print_wrapper(struct sshkey *hostkey, struct ssh *ssh)
1.26 markus 175: {
1.95 markus 176: con *c;
177:
178: if ((c = ssh_get_app_data(ssh)) != NULL)
179: keyprint(c, hostkey);
180: /* always abort key exchange */
181: return -1;
1.26 markus 182: }
183:
184: static int
185: ssh2_capable(int remote_major, int remote_minor)
186: {
187: switch (remote_major) {
188: case 1:
189: if (remote_minor == 99)
190: return 1;
191: break;
192: case 2:
193: return 1;
194: default:
195: break;
196: }
197: return 0;
198: }
199:
1.95 markus 200: static void
1.26 markus 201: keygrab_ssh2(con *c)
202: {
1.91 markus 203: char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
1.95 markus 204: int r;
1.26 markus 205:
206: enable_compat20();
1.103 djm 207: switch (c->c_keytype) {
208: case KT_DSA:
209: myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ?
210: "ssh-dss-cert-v01@openssh.com" : "ssh-dss";
211: break;
212: case KT_RSA:
213: myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ?
214: "ssh-rsa-cert-v01@openssh.com" : "ssh-rsa";
215: break;
216: case KT_ED25519:
217: myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ?
218: "ssh-ed25519-cert-v01@openssh.com" : "ssh-ed25519";
219: break;
220: case KT_ECDSA:
221: myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = get_cert ?
222: "ecdsa-sha2-nistp256-cert-v01@openssh.com,"
223: "ecdsa-sha2-nistp384-cert-v01@openssh.com,"
224: "ecdsa-sha2-nistp521-cert-v01@openssh.com" :
225: "ecdsa-sha2-nistp256,"
226: "ecdsa-sha2-nistp384,"
227: "ecdsa-sha2-nistp521";
228: break;
229: default:
230: fatal("unknown key type %d", c->c_keytype);
231: break;
232: }
1.95 markus 233: if ((r = kex_setup(c->c_ssh, myproposal)) != 0) {
234: free(c->c_ssh);
235: fprintf(stderr, "kex_setup: %s\n", ssh_err(r));
236: exit(1);
237: }
1.92 markus 238: #ifdef WITH_OPENSSL
1.95 markus 239: c->c_ssh->kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
240: c->c_ssh->kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
1.106 djm 241: c->c_ssh->kex->kex[KEX_DH_GRP14_SHA256] = kexdh_client;
242: c->c_ssh->kex->kex[KEX_DH_GRP16_SHA512] = kexdh_client;
243: c->c_ssh->kex->kex[KEX_DH_GRP18_SHA512] = kexdh_client;
1.95 markus 244: c->c_ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
245: c->c_ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
246: c->c_ssh->kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
1.92 markus 247: #endif
1.95 markus 248: c->c_ssh->kex->kex[KEX_C25519_SHA256] = kexc25519_client;
249: ssh_set_verify_host_key_callback(c->c_ssh, key_print_wrapper);
250: /*
251: * do the key-exchange until an error occurs or until
252: * the key_print_wrapper() callback sets c_done.
253: */
254: ssh_dispatch_run(c->c_ssh, DISPATCH_BLOCK, &c->c_done, c->c_ssh);
1.26 markus 255: }
256:
257: static void
1.108 djm 258: keyprint_one(const char *host, struct sshkey *key)
1.26 markus 259: {
1.102 djm 260: char *hostport;
1.108 djm 261: const char *known_host, *hashed;
1.51 djm 262:
1.108 djm 263: hostport = put_host_port(host, ssh_port);
1.109 djm 264: lowercase(hostport);
1.108 djm 265: if (hash_hosts && (hashed = host_hash(host, NULL, 0)) == NULL)
1.51 djm 266: fatal("host_hash failed");
1.108 djm 267: known_host = hash_hosts ? hashed : hostport;
1.103 djm 268: if (!get_cert)
1.108 djm 269: fprintf(stdout, "%s ", known_host);
1.95 markus 270: sshkey_write(key, stdout);
1.1 markus 271: fputs("\n", stdout);
1.101 djm 272: free(hostport);
1.102 djm 273: }
274:
275: static void
276: keyprint(con *c, struct sshkey *key)
277: {
278: char *hosts = c->c_output_name ? c->c_output_name : c->c_name;
279: char *host, *ohosts;
280:
281: if (key == NULL)
282: return;
1.103 djm 283: if (get_cert || (!hash_hosts && ssh_port == SSH_DEFAULT_PORT)) {
1.102 djm 284: keyprint_one(hosts, key);
285: return;
286: }
287: ohosts = hosts = xstrdup(hosts);
288: while ((host = strsep(&hosts, ",")) != NULL)
289: keyprint_one(host, key);
290: free(ohosts);
1.1 markus 291: }
292:
1.24 itojun 293: static int
1.1 markus 294: tcpconnect(char *host)
295: {
296: struct addrinfo hints, *ai, *aitop;
297: char strport[NI_MAXSERV];
298: int gaierr, s = -1;
299:
1.26 markus 300: snprintf(strport, sizeof strport, "%d", ssh_port);
1.1 markus 301: memset(&hints, 0, sizeof(hints));
1.26 markus 302: hints.ai_family = IPv4or6;
1.1 markus 303: hints.ai_socktype = SOCK_STREAM;
1.97 djm 304: if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
305: error("getaddrinfo %s: %s", host, ssh_gai_strerror(gaierr));
306: return -1;
307: }
1.1 markus 308: for (ai = aitop; ai; ai = ai->ai_next) {
1.81 dtucker 309: s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.1 markus 310: if (s < 0) {
311: error("socket: %s", strerror(errno));
312: continue;
313: }
1.49 djm 314: if (set_nonblock(s) == -1)
315: fatal("%s: set_nonblock(%d)", __func__, s);
1.1 markus 316: if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 &&
317: errno != EINPROGRESS)
318: error("connect (`%s'): %s", host, strerror(errno));
319: else
320: break;
321: close(s);
322: s = -1;
323: }
324: freeaddrinfo(aitop);
325: return s;
326: }
327:
1.24 itojun 328: static int
1.26 markus 329: conalloc(char *iname, char *oname, int keytype)
1.1 markus 330: {
1.39 deraadt 331: char *namebase, *name, *namelist;
1.1 markus 332: int s;
333:
334: namebase = namelist = xstrdup(iname);
335:
336: do {
337: name = xstrsep(&namelist, ",");
338: if (!name) {
1.87 djm 339: free(namebase);
1.1 markus 340: return (-1);
341: }
342: } while ((s = tcpconnect(name)) < 0);
343:
344: if (s >= maxfd)
1.4 markus 345: fatal("conalloc: fdno %d too high", s);
1.1 markus 346: if (fdcon[s].c_status)
1.4 markus 347: fatal("conalloc: attempt to reuse fdno %d", s);
1.1 markus 348:
1.103 djm 349: debug3("%s: oname %s kt %d", __func__, oname, keytype);
1.1 markus 350: fdcon[s].c_fd = s;
351: fdcon[s].c_status = CS_CON;
352: fdcon[s].c_namebase = namebase;
353: fdcon[s].c_name = name;
354: fdcon[s].c_namelist = namelist;
355: fdcon[s].c_output_name = xstrdup(oname);
356: fdcon[s].c_data = (char *) &fdcon[s].c_plen;
357: fdcon[s].c_len = 4;
358: fdcon[s].c_off = 0;
1.26 markus 359: fdcon[s].c_keytype = keytype;
1.1 markus 360: gettimeofday(&fdcon[s].c_tv, NULL);
361: fdcon[s].c_tv.tv_sec += timeout;
362: TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
1.19 millert 363: FD_SET(s, read_wait);
1.1 markus 364: ncon++;
365: return (s);
366: }
367:
1.24 itojun 368: static void
1.1 markus 369: confree(int s)
370: {
371: if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
1.4 markus 372: fatal("confree: attempt to free bad fdno %d", s);
1.18 deraadt 373: close(s);
1.87 djm 374: free(fdcon[s].c_namebase);
375: free(fdcon[s].c_output_name);
1.1 markus 376: if (fdcon[s].c_status == CS_KEYS)
1.87 djm 377: free(fdcon[s].c_data);
1.1 markus 378: fdcon[s].c_status = CS_UNUSED;
1.26 markus 379: fdcon[s].c_keytype = 0;
1.95 markus 380: if (fdcon[s].c_ssh) {
381: ssh_packet_close(fdcon[s].c_ssh);
382: free(fdcon[s].c_ssh);
383: fdcon[s].c_ssh = NULL;
384: }
1.1 markus 385: TAILQ_REMOVE(&tq, &fdcon[s], c_link);
1.19 millert 386: FD_CLR(s, read_wait);
1.1 markus 387: ncon--;
388: }
389:
1.24 itojun 390: static void
1.1 markus 391: contouch(int s)
392: {
393: TAILQ_REMOVE(&tq, &fdcon[s], c_link);
394: gettimeofday(&fdcon[s].c_tv, NULL);
395: fdcon[s].c_tv.tv_sec += timeout;
396: TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
397: }
398:
1.24 itojun 399: static int
1.1 markus 400: conrecycle(int s)
401: {
1.39 deraadt 402: con *c = &fdcon[s];
1.1 markus 403: int ret;
404:
1.26 markus 405: ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype);
1.1 markus 406: confree(s);
407: return (ret);
408: }
409:
1.24 itojun 410: static void
1.1 markus 411: congreet(int s)
412: {
1.55 djm 413: int n = 0, remote_major = 0, remote_minor = 0;
1.26 markus 414: char buf[256], *cp;
1.33 markus 415: char remote_version[sizeof buf];
1.55 djm 416: size_t bufsiz;
1.1 markus 417: con *c = &fdcon[s];
418:
1.57 djm 419: for (;;) {
420: memset(buf, '\0', sizeof(buf));
421: bufsiz = sizeof(buf);
422: cp = buf;
423: while (bufsiz-- &&
424: (n = atomicio(read, s, cp, 1)) == 1 && *cp != '\n') {
425: if (*cp == '\r')
426: *cp = '\n';
427: cp++;
428: }
429: if (n != 1 || strncmp(buf, "SSH-", 4) == 0)
430: break;
1.27 markus 431: }
1.54 avsm 432: if (n == 0) {
433: switch (errno) {
434: case EPIPE:
435: error("%s: Connection closed by remote host", c->c_name);
436: break;
437: case ECONNREFUSED:
438: break;
439: default:
1.1 markus 440: error("read (%s): %s", c->c_name, strerror(errno));
1.54 avsm 441: break;
442: }
1.1 markus 443: conrecycle(s);
444: return;
445: }
1.21 millert 446: if (*cp != '\n' && *cp != '\r') {
1.1 markus 447: error("%s: bad greeting", c->c_name);
448: confree(s);
449: return;
450: }
1.21 millert 451: *cp = '\0';
1.98 djm 452: if ((c->c_ssh = ssh_packet_set_connection(NULL, s, s)) == NULL)
453: fatal("ssh_packet_set_connection failed");
1.99 djm 454: ssh_packet_set_timeout(c->c_ssh, timeout, 1);
1.95 markus 455: ssh_set_app_data(c->c_ssh, c); /* back link */
1.33 markus 456: if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
457: &remote_major, &remote_minor, remote_version) == 3)
1.95 markus 458: c->c_ssh->compat = compat_datafellows(remote_version);
1.33 markus 459: else
1.95 markus 460: c->c_ssh->compat = 0;
1.26 markus 461: if (c->c_keytype != KT_RSA1) {
462: if (!ssh2_capable(remote_major, remote_minor)) {
463: debug("%s doesn't support ssh2", c->c_name);
464: confree(s);
465: return;
466: }
1.33 markus 467: } else if (remote_major != 1) {
468: debug("%s doesn't support ssh1", c->c_name);
469: confree(s);
470: return;
1.26 markus 471: }
1.101 djm 472: fprintf(stderr, "# %s:%d %s\n", c->c_name, ssh_port, chop(buf));
1.26 markus 473: n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n",
474: c->c_keytype == KT_RSA1? PROTOCOL_MAJOR_1 : PROTOCOL_MAJOR_2,
475: c->c_keytype == KT_RSA1? PROTOCOL_MINOR_1 : PROTOCOL_MINOR_2);
1.55 djm 476: if (n < 0 || (size_t)n >= sizeof(buf)) {
1.53 moritz 477: error("snprintf: buffer too small");
478: confree(s);
479: return;
480: }
1.55 djm 481: if (atomicio(vwrite, s, buf, n) != (size_t)n) {
1.1 markus 482: error("write (%s): %s", c->c_name, strerror(errno));
483: confree(s);
484: return;
485: }
1.26 markus 486: if (c->c_keytype != KT_RSA1) {
1.95 markus 487: keygrab_ssh2(c);
1.26 markus 488: confree(s);
489: return;
490: }
1.1 markus 491: c->c_status = CS_SIZE;
492: contouch(s);
493: }
494:
1.24 itojun 495: static void
1.1 markus 496: conread(int s)
497: {
1.39 deraadt 498: con *c = &fdcon[s];
1.54 avsm 499: size_t n;
1.1 markus 500:
501: if (c->c_status == CS_CON) {
502: congreet(s);
503: return;
504: }
1.50 avsm 505: n = atomicio(read, s, c->c_data + c->c_off, c->c_len - c->c_off);
1.54 avsm 506: if (n == 0) {
1.1 markus 507: error("read (%s): %s", c->c_name, strerror(errno));
508: confree(s);
509: return;
510: }
511: c->c_off += n;
512:
513: if (c->c_off == c->c_len)
514: switch (c->c_status) {
515: case CS_SIZE:
516: c->c_plen = htonl(c->c_plen);
517: c->c_len = c->c_plen + 8 - (c->c_plen & 7);
518: c->c_off = 0;
519: c->c_data = xmalloc(c->c_len);
520: c->c_status = CS_KEYS;
521: break;
522: default:
1.4 markus 523: fatal("conread: invalid status %d", c->c_status);
1.1 markus 524: break;
525: }
526:
527: contouch(s);
528: }
529:
1.24 itojun 530: static void
1.1 markus 531: conloop(void)
532: {
1.39 deraadt 533: struct timeval seltime, now;
1.19 millert 534: fd_set *r, *e;
1.39 deraadt 535: con *c;
1.1 markus 536: int i;
537:
538: gettimeofday(&now, NULL);
1.36 itojun 539: c = TAILQ_FIRST(&tq);
1.1 markus 540:
1.18 deraadt 541: if (c && (c->c_tv.tv_sec > now.tv_sec ||
542: (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) {
1.1 markus 543: seltime = c->c_tv;
544: seltime.tv_sec -= now.tv_sec;
545: seltime.tv_usec -= now.tv_usec;
1.13 itojun 546: if (seltime.tv_usec < 0) {
1.1 markus 547: seltime.tv_usec += 1000000;
548: seltime.tv_sec--;
549: }
550: } else
1.85 okan 551: timerclear(&seltime);
1.1 markus 552:
1.63 djm 553: r = xcalloc(read_wait_nfdset, sizeof(fd_mask));
554: e = xcalloc(read_wait_nfdset, sizeof(fd_mask));
555: memcpy(r, read_wait, read_wait_nfdset * sizeof(fd_mask));
556: memcpy(e, read_wait, read_wait_nfdset * sizeof(fd_mask));
1.19 millert 557:
558: while (select(maxfd, r, NULL, e, &seltime) == -1 &&
1.16 deraadt 559: (errno == EAGAIN || errno == EINTR))
560: ;
561:
1.18 deraadt 562: for (i = 0; i < maxfd; i++) {
1.19 millert 563: if (FD_ISSET(i, e)) {
1.1 markus 564: error("%s: exception!", fdcon[i].c_name);
565: confree(i);
1.19 millert 566: } else if (FD_ISSET(i, r))
1.1 markus 567: conread(i);
1.18 deraadt 568: }
1.87 djm 569: free(r);
570: free(e);
1.1 markus 571:
1.36 itojun 572: c = TAILQ_FIRST(&tq);
1.18 deraadt 573: while (c && (c->c_tv.tv_sec < now.tv_sec ||
574: (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) {
1.1 markus 575: int s = c->c_fd;
1.18 deraadt 576:
1.36 itojun 577: c = TAILQ_NEXT(c, c_link);
1.1 markus 578: conrecycle(s);
579: }
580: }
581:
1.26 markus 582: static void
583: do_host(char *host)
1.1 markus 584: {
1.26 markus 585: char *name = strnnsep(&host, " \t\n");
586: int j;
1.1 markus 587:
1.31 markus 588: if (name == NULL)
589: return;
1.89 markus 590: for (j = KT_RSA1; j <= KT_ED25519; j *= 2) {
1.26 markus 591: if (get_keytypes & j) {
592: while (ncon >= MAXCON)
593: conloop();
594: conalloc(name, *host ? host : name, j);
1.1 markus 595: }
596: }
597: }
598:
1.34 markus 599: void
600: fatal(const char *fmt,...)
1.26 markus 601: {
1.34 markus 602: va_list args;
1.39 deraadt 603:
1.34 markus 604: va_start(args, fmt);
605: do_log(SYSLOG_LEVEL_FATAL, fmt, args);
606: va_end(args);
1.95 markus 607: exit(255);
1.26 markus 608: }
609:
610: static void
1.1 markus 611: usage(void)
612: {
1.77 sobrado 613: fprintf(stderr,
1.104 jmc 614: "usage: %s [-46cHv] [-f file] [-p port] [-T timeout] [-t type]\n"
1.81 dtucker 615: "\t\t [host | addrlist namelist] ...\n",
1.25 jakob 616: __progname);
617: exit(1);
1.1 markus 618: }
619:
620: int
621: main(int argc, char **argv)
622: {
1.26 markus 623: int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO;
1.82 djm 624: int opt, fopt_count = 0, j;
625: char *tname, *cp, line[NI_MAXHOST];
626: FILE *fp;
627: u_long linenum;
1.26 markus 628:
629: extern int optind;
630: extern char *optarg;
1.1 markus 631:
1.105 dtucker 632: ssh_malloc_init(); /* must be called before any mallocs */
1.1 markus 633: TAILQ_INIT(&tq);
1.56 djm 634:
635: /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
636: sanitise_stdfd();
1.1 markus 637:
1.26 markus 638: if (argc <= 1)
1.1 markus 639: usage();
640:
1.103 djm 641: while ((opt = getopt(argc, argv, "cHv46p:T:t:f:")) != -1) {
1.26 markus 642: switch (opt) {
1.51 djm 643: case 'H':
644: hash_hosts = 1;
1.103 djm 645: break;
646: case 'c':
647: get_cert = 1;
1.51 djm 648: break;
1.26 markus 649: case 'p':
650: ssh_port = a2port(optarg);
1.78 djm 651: if (ssh_port <= 0) {
1.26 markus 652: fprintf(stderr, "Bad port '%s'\n", optarg);
653: exit(1);
654: }
655: break;
656: case 'T':
1.38 stevesk 657: timeout = convtime(optarg);
658: if (timeout == -1 || timeout == 0) {
659: fprintf(stderr, "Bad timeout '%s'\n", optarg);
1.1 markus 660: usage();
1.38 stevesk 661: }
1.26 markus 662: break;
663: case 'v':
664: if (!debug_flag) {
665: debug_flag = 1;
666: log_level = SYSLOG_LEVEL_DEBUG1;
667: }
668: else if (log_level < SYSLOG_LEVEL_DEBUG3)
669: log_level++;
670: else
671: fatal("Too high debugging level.");
672: break;
673: case 'f':
674: if (strcmp(optarg, "-") == 0)
675: optarg = NULL;
676: argv[fopt_count++] = optarg;
677: break;
678: case 't':
679: get_keytypes = 0;
680: tname = strtok(optarg, ",");
681: while (tname) {
1.95 markus 682: int type = sshkey_type_from_name(tname);
1.107 djm 683:
1.26 markus 684: switch (type) {
685: case KEY_DSA:
686: get_keytypes |= KT_DSA;
1.83 djm 687: break;
688: case KEY_ECDSA:
689: get_keytypes |= KT_ECDSA;
1.26 markus 690: break;
691: case KEY_RSA:
692: get_keytypes |= KT_RSA;
1.89 markus 693: break;
694: case KEY_ED25519:
695: get_keytypes |= KT_ED25519;
1.26 markus 696: break;
697: case KEY_UNSPEC:
1.107 djm 698: default:
699: fatal("Unknown key type \"%s\"", tname);
1.26 markus 700: }
701: tname = strtok(NULL, ",");
702: }
703: break;
704: case '4':
705: IPv4or6 = AF_INET;
706: break;
707: case '6':
708: IPv4or6 = AF_INET6;
709: break;
710: case '?':
711: default:
712: usage();
1.1 markus 713: }
714: }
1.26 markus 715: if (optind == argc && !fopt_count)
1.1 markus 716: usage();
717:
1.26 markus 718: log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1);
719:
1.1 markus 720: maxfd = fdlim_get(1);
721: if (maxfd < 0)
1.4 markus 722: fatal("%s: fdlim_get: bad value", __progname);
1.1 markus 723: if (maxfd > MAXMAXFD)
724: maxfd = MAXMAXFD;
1.18 deraadt 725: if (MAXCON <= 0)
1.4 markus 726: fatal("%s: not enough file descriptors", __progname);
1.1 markus 727: if (maxfd > fdlim_get(0))
728: fdlim_set(maxfd);
1.63 djm 729: fdcon = xcalloc(maxfd, sizeof(con));
1.19 millert 730:
1.63 djm 731: read_wait_nfdset = howmany(maxfd, NFDBITS);
732: read_wait = xcalloc(read_wait_nfdset, sizeof(fd_mask));
1.1 markus 733:
1.82 djm 734: for (j = 0; j < fopt_count; j++) {
735: if (argv[j] == NULL)
736: fp = stdin;
737: else if ((fp = fopen(argv[j], "r")) == NULL)
738: fatal("%s: %s: %s", __progname, argv[j],
739: strerror(errno));
740: linenum = 0;
741:
742: while (read_keyfile_line(fp,
743: argv[j] == NULL ? "(stdin)" : argv[j], line, sizeof(line),
744: &linenum) != -1) {
745: /* Chomp off trailing whitespace and comments */
746: if ((cp = strchr(line, '#')) == NULL)
747: cp = line + strlen(line) - 1;
748: while (cp >= line) {
749: if (*cp == ' ' || *cp == '\t' ||
750: *cp == '\n' || *cp == '#')
751: *cp-- = '\0';
752: else
753: break;
754: }
755:
756: /* Skip empty lines */
757: if (*line == '\0')
1.28 danh 758: continue;
1.82 djm 759:
760: do_host(line);
1.26 markus 761: }
1.82 djm 762:
763: if (ferror(fp))
764: fatal("%s: %s: %s", __progname, argv[j],
765: strerror(errno));
766:
767: fclose(fp);
1.26 markus 768: }
769:
770: while (optind < argc)
771: do_host(argv[optind++]);
1.1 markus 772:
773: while (ncon > 0)
774: conloop();
775:
776: return (0);
777: }