Annotation of src/usr.bin/ssh/ssh-keyscan.c, Revision 1.115
1.115 ! dtucker 1: /* $OpenBSD: ssh-keyscan.c,v 1.114 2017/05/31 07:00:13 markus 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.1 markus 43:
1.26 markus 44: /* Flag indicating whether IPv4 or IPv6. This can be set on the command line.
45: Default value is AF_UNSPEC means both IPv4 and IPv6. */
46: int IPv4or6 = AF_UNSPEC;
47:
48: int ssh_port = SSH_DEFAULT_PORT;
1.1 markus 49:
1.112 djm 50: #define KT_DSA (1)
51: #define KT_RSA (1<<1)
52: #define KT_ECDSA (1<<2)
53: #define KT_ED25519 (1<<3)
54:
55: #define KT_MIN KT_DSA
56: #define KT_MAX KT_ED25519
1.26 markus 57:
1.103 djm 58: int get_cert = 0;
1.90 djm 59: int get_keytypes = KT_RSA|KT_ECDSA|KT_ED25519;
1.1 markus 60:
1.51 djm 61: int hash_hosts = 0; /* Hash hostname on output */
62:
1.1 markus 63: #define MAXMAXFD 256
64:
65: /* The number of seconds after which to give up on a TCP connection */
66: int timeout = 5;
67:
68: int maxfd;
1.18 deraadt 69: #define MAXCON (maxfd - 10)
1.1 markus 70:
1.3 markus 71: extern char *__progname;
1.19 millert 72: fd_set *read_wait;
1.63 djm 73: size_t read_wait_nfdset;
1.1 markus 74: int ncon;
75:
76: /*
77: * Keep a connection structure for each file descriptor. The state
78: * associated with file descriptor n is held in fdcon[n].
79: */
80: typedef struct Connection {
1.6 markus 81: u_char c_status; /* State of connection on this file desc. */
1.1 markus 82: #define CS_UNUSED 0 /* File descriptor unused */
83: #define CS_CON 1 /* Waiting to connect/read greeting */
84: #define CS_SIZE 2 /* Waiting to read initial packet size */
85: #define CS_KEYS 3 /* Waiting to read public key packet */
86: int c_fd; /* Quick lookup: c->c_fd == c - fdcon */
87: int c_plen; /* Packet length field for ssh packet */
88: int c_len; /* Total bytes which must be read. */
89: int c_off; /* Length of data read so far. */
1.112 djm 90: int c_keytype; /* Only one of KT_* */
1.100 miod 91: sig_atomic_t c_done; /* SSH2 done */
1.1 markus 92: char *c_namebase; /* Address to free for c_name and c_namelist */
93: char *c_name; /* Hostname of connection for errors */
94: char *c_namelist; /* Pointer to other possible addresses */
95: char *c_output_name; /* Hostname of connection for output */
96: char *c_data; /* Data read from this fd */
1.95 markus 97: struct ssh *c_ssh; /* SSH-connection */
1.1 markus 98: struct timeval c_tv; /* Time at which connection gets aborted */
99: TAILQ_ENTRY(Connection) c_link; /* List of connections in timeout order. */
100: } con;
101:
102: TAILQ_HEAD(conlist, Connection) tq; /* Timeout Queue */
103: con *fdcon;
104:
1.95 markus 105: static void keyprint(con *c, struct sshkey *key);
106:
1.24 itojun 107: static int
1.1 markus 108: fdlim_get(int hard)
109: {
110: struct rlimit rlfd;
1.17 deraadt 111:
1.1 markus 112: if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
113: return (-1);
114: if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY)
1.46 djm 115: return sysconf(_SC_OPEN_MAX);
1.1 markus 116: else
117: return hard ? rlfd.rlim_max : rlfd.rlim_cur;
118: }
119:
1.24 itojun 120: static int
1.1 markus 121: fdlim_set(int lim)
122: {
123: struct rlimit rlfd;
1.39 deraadt 124:
1.1 markus 125: if (lim <= 0)
126: return (-1);
127: if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
128: return (-1);
129: rlfd.rlim_cur = lim;
130: if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0)
131: return (-1);
132: return (0);
133: }
134:
135: /*
136: * This is an strsep function that returns a null field for adjacent
137: * separators. This is the same as the 4.4BSD strsep, but different from the
138: * one in the GNU libc.
139: */
1.24 itojun 140: static char *
1.1 markus 141: xstrsep(char **str, const char *delim)
142: {
143: char *s, *e;
144:
145: if (!**str)
146: return (NULL);
147:
148: s = *str;
149: e = s + strcspn(s, delim);
150:
151: if (*e != '\0')
152: *e++ = '\0';
153: *str = e;
154:
155: return (s);
156: }
157:
158: /*
159: * Get the next non-null token (like GNU strsep). Strsep() will return a
160: * null token for two adjacent separators, so we may have to loop.
161: */
1.24 itojun 162: static char *
1.1 markus 163: strnnsep(char **stringp, char *delim)
164: {
165: char *tok;
166:
167: do {
168: tok = xstrsep(stringp, delim);
169: } while (tok && *tok == '\0');
170: return (tok);
171: }
172:
1.26 markus 173:
174: static int
1.95 markus 175: key_print_wrapper(struct sshkey *hostkey, struct ssh *ssh)
1.26 markus 176: {
1.95 markus 177: con *c;
178:
179: if ((c = ssh_get_app_data(ssh)) != NULL)
180: keyprint(c, hostkey);
181: /* always abort key exchange */
182: return -1;
1.26 markus 183: }
184:
185: static int
186: ssh2_capable(int remote_major, int remote_minor)
187: {
188: switch (remote_major) {
189: case 1:
190: if (remote_minor == 99)
191: return 1;
192: break;
193: case 2:
194: return 1;
195: default:
196: break;
197: }
198: return 0;
199: }
200:
1.95 markus 201: static void
1.26 markus 202: keygrab_ssh2(con *c)
203: {
1.91 markus 204: char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
1.95 markus 205: int r;
1.26 markus 206:
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: */
1.114 markus 254: ssh_dispatch_run(c->c_ssh, DISPATCH_BLOCK, &c->c_done);
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.87 djm 373: free(fdcon[s].c_namebase);
374: free(fdcon[s].c_output_name);
1.1 markus 375: if (fdcon[s].c_status == CS_KEYS)
1.87 djm 376: free(fdcon[s].c_data);
1.1 markus 377: fdcon[s].c_status = CS_UNUSED;
1.26 markus 378: fdcon[s].c_keytype = 0;
1.95 markus 379: if (fdcon[s].c_ssh) {
380: ssh_packet_close(fdcon[s].c_ssh);
381: free(fdcon[s].c_ssh);
382: fdcon[s].c_ssh = NULL;
1.115 ! dtucker 383: } else
! 384: close(s);
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.112 djm 419: /* send client banner */
420: n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n",
421: PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2);
422: if (n < 0 || (size_t)n >= sizeof(buf)) {
423: error("snprintf: buffer too small");
424: confree(s);
425: return;
426: }
427: if (atomicio(vwrite, s, buf, n) != (size_t)n) {
428: error("write (%s): %s", c->c_name, strerror(errno));
429: confree(s);
430: return;
431: }
432:
1.57 djm 433: for (;;) {
434: memset(buf, '\0', sizeof(buf));
435: bufsiz = sizeof(buf);
436: cp = buf;
437: while (bufsiz-- &&
438: (n = atomicio(read, s, cp, 1)) == 1 && *cp != '\n') {
439: if (*cp == '\r')
440: *cp = '\n';
441: cp++;
442: }
443: if (n != 1 || strncmp(buf, "SSH-", 4) == 0)
444: break;
1.27 markus 445: }
1.54 avsm 446: if (n == 0) {
447: switch (errno) {
448: case EPIPE:
449: error("%s: Connection closed by remote host", c->c_name);
450: break;
451: case ECONNREFUSED:
452: break;
453: default:
1.1 markus 454: error("read (%s): %s", c->c_name, strerror(errno));
1.54 avsm 455: break;
456: }
1.1 markus 457: conrecycle(s);
458: return;
459: }
1.21 millert 460: if (*cp != '\n' && *cp != '\r') {
1.1 markus 461: error("%s: bad greeting", c->c_name);
462: confree(s);
463: return;
464: }
1.21 millert 465: *cp = '\0';
1.98 djm 466: if ((c->c_ssh = ssh_packet_set_connection(NULL, s, s)) == NULL)
467: fatal("ssh_packet_set_connection failed");
1.99 djm 468: ssh_packet_set_timeout(c->c_ssh, timeout, 1);
1.95 markus 469: ssh_set_app_data(c->c_ssh, c); /* back link */
1.33 markus 470: if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
471: &remote_major, &remote_minor, remote_version) == 3)
1.95 markus 472: c->c_ssh->compat = compat_datafellows(remote_version);
1.33 markus 473: else
1.95 markus 474: c->c_ssh->compat = 0;
1.112 djm 475: if (!ssh2_capable(remote_major, remote_minor)) {
476: debug("%s doesn't support ssh2", c->c_name);
1.33 markus 477: confree(s);
478: return;
1.26 markus 479: }
1.101 djm 480: fprintf(stderr, "# %s:%d %s\n", c->c_name, ssh_port, chop(buf));
1.112 djm 481: keygrab_ssh2(c);
482: confree(s);
1.1 markus 483: }
484:
1.24 itojun 485: static void
1.1 markus 486: conread(int s)
487: {
1.39 deraadt 488: con *c = &fdcon[s];
1.54 avsm 489: size_t n;
1.1 markus 490:
491: if (c->c_status == CS_CON) {
492: congreet(s);
493: return;
494: }
1.50 avsm 495: n = atomicio(read, s, c->c_data + c->c_off, c->c_len - c->c_off);
1.54 avsm 496: if (n == 0) {
1.1 markus 497: error("read (%s): %s", c->c_name, strerror(errno));
498: confree(s);
499: return;
500: }
501: c->c_off += n;
502:
503: if (c->c_off == c->c_len)
504: switch (c->c_status) {
505: case CS_SIZE:
506: c->c_plen = htonl(c->c_plen);
507: c->c_len = c->c_plen + 8 - (c->c_plen & 7);
508: c->c_off = 0;
509: c->c_data = xmalloc(c->c_len);
510: c->c_status = CS_KEYS;
511: break;
512: default:
1.4 markus 513: fatal("conread: invalid status %d", c->c_status);
1.1 markus 514: break;
515: }
516:
517: contouch(s);
518: }
519:
1.24 itojun 520: static void
1.1 markus 521: conloop(void)
522: {
1.39 deraadt 523: struct timeval seltime, now;
1.19 millert 524: fd_set *r, *e;
1.39 deraadt 525: con *c;
1.1 markus 526: int i;
527:
528: gettimeofday(&now, NULL);
1.36 itojun 529: c = TAILQ_FIRST(&tq);
1.1 markus 530:
1.18 deraadt 531: if (c && (c->c_tv.tv_sec > now.tv_sec ||
532: (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) {
1.1 markus 533: seltime = c->c_tv;
534: seltime.tv_sec -= now.tv_sec;
535: seltime.tv_usec -= now.tv_usec;
1.13 itojun 536: if (seltime.tv_usec < 0) {
1.1 markus 537: seltime.tv_usec += 1000000;
538: seltime.tv_sec--;
539: }
540: } else
1.85 okan 541: timerclear(&seltime);
1.1 markus 542:
1.63 djm 543: r = xcalloc(read_wait_nfdset, sizeof(fd_mask));
544: e = xcalloc(read_wait_nfdset, sizeof(fd_mask));
545: memcpy(r, read_wait, read_wait_nfdset * sizeof(fd_mask));
546: memcpy(e, read_wait, read_wait_nfdset * sizeof(fd_mask));
1.19 millert 547:
548: while (select(maxfd, r, NULL, e, &seltime) == -1 &&
1.16 deraadt 549: (errno == EAGAIN || errno == EINTR))
550: ;
551:
1.18 deraadt 552: for (i = 0; i < maxfd; i++) {
1.19 millert 553: if (FD_ISSET(i, e)) {
1.1 markus 554: error("%s: exception!", fdcon[i].c_name);
555: confree(i);
1.19 millert 556: } else if (FD_ISSET(i, r))
1.1 markus 557: conread(i);
1.18 deraadt 558: }
1.87 djm 559: free(r);
560: free(e);
1.1 markus 561:
1.36 itojun 562: c = TAILQ_FIRST(&tq);
1.18 deraadt 563: while (c && (c->c_tv.tv_sec < now.tv_sec ||
564: (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) {
1.1 markus 565: int s = c->c_fd;
1.18 deraadt 566:
1.36 itojun 567: c = TAILQ_NEXT(c, c_link);
1.1 markus 568: conrecycle(s);
569: }
570: }
571:
1.26 markus 572: static void
573: do_host(char *host)
1.1 markus 574: {
1.26 markus 575: char *name = strnnsep(&host, " \t\n");
576: int j;
1.1 markus 577:
1.31 markus 578: if (name == NULL)
579: return;
1.112 djm 580: for (j = KT_MIN; j <= KT_MAX; j *= 2) {
1.26 markus 581: if (get_keytypes & j) {
582: while (ncon >= MAXCON)
583: conloop();
584: conalloc(name, *host ? host : name, j);
1.1 markus 585: }
586: }
587: }
588:
1.34 markus 589: void
590: fatal(const char *fmt,...)
1.26 markus 591: {
1.34 markus 592: va_list args;
1.39 deraadt 593:
1.34 markus 594: va_start(args, fmt);
595: do_log(SYSLOG_LEVEL_FATAL, fmt, args);
596: va_end(args);
1.95 markus 597: exit(255);
1.26 markus 598: }
599:
600: static void
1.1 markus 601: usage(void)
602: {
1.77 sobrado 603: fprintf(stderr,
1.104 jmc 604: "usage: %s [-46cHv] [-f file] [-p port] [-T timeout] [-t type]\n"
1.81 dtucker 605: "\t\t [host | addrlist namelist] ...\n",
1.25 jakob 606: __progname);
607: exit(1);
1.1 markus 608: }
609:
610: int
611: main(int argc, char **argv)
612: {
1.26 markus 613: int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO;
1.82 djm 614: int opt, fopt_count = 0, j;
615: char *tname, *cp, line[NI_MAXHOST];
616: FILE *fp;
617: u_long linenum;
1.26 markus 618:
619: extern int optind;
620: extern char *optarg;
1.1 markus 621:
1.105 dtucker 622: ssh_malloc_init(); /* must be called before any mallocs */
1.1 markus 623: TAILQ_INIT(&tq);
1.56 djm 624:
625: /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
626: sanitise_stdfd();
1.1 markus 627:
1.26 markus 628: if (argc <= 1)
1.1 markus 629: usage();
630:
1.103 djm 631: while ((opt = getopt(argc, argv, "cHv46p:T:t:f:")) != -1) {
1.26 markus 632: switch (opt) {
1.51 djm 633: case 'H':
634: hash_hosts = 1;
1.103 djm 635: break;
636: case 'c':
637: get_cert = 1;
1.51 djm 638: break;
1.26 markus 639: case 'p':
640: ssh_port = a2port(optarg);
1.78 djm 641: if (ssh_port <= 0) {
1.26 markus 642: fprintf(stderr, "Bad port '%s'\n", optarg);
643: exit(1);
644: }
645: break;
646: case 'T':
1.38 stevesk 647: timeout = convtime(optarg);
648: if (timeout == -1 || timeout == 0) {
649: fprintf(stderr, "Bad timeout '%s'\n", optarg);
1.1 markus 650: usage();
1.38 stevesk 651: }
1.26 markus 652: break;
653: case 'v':
654: if (!debug_flag) {
655: debug_flag = 1;
656: log_level = SYSLOG_LEVEL_DEBUG1;
657: }
658: else if (log_level < SYSLOG_LEVEL_DEBUG3)
659: log_level++;
660: else
661: fatal("Too high debugging level.");
662: break;
663: case 'f':
664: if (strcmp(optarg, "-") == 0)
665: optarg = NULL;
666: argv[fopt_count++] = optarg;
667: break;
668: case 't':
669: get_keytypes = 0;
670: tname = strtok(optarg, ",");
671: while (tname) {
1.95 markus 672: int type = sshkey_type_from_name(tname);
1.107 djm 673:
1.26 markus 674: switch (type) {
675: case KEY_DSA:
676: get_keytypes |= KT_DSA;
1.83 djm 677: break;
678: case KEY_ECDSA:
679: get_keytypes |= KT_ECDSA;
1.26 markus 680: break;
681: case KEY_RSA:
682: get_keytypes |= KT_RSA;
1.89 markus 683: break;
684: case KEY_ED25519:
685: get_keytypes |= KT_ED25519;
1.26 markus 686: break;
687: case KEY_UNSPEC:
1.107 djm 688: default:
689: fatal("Unknown key type \"%s\"", tname);
1.26 markus 690: }
691: tname = strtok(NULL, ",");
692: }
693: break;
694: case '4':
695: IPv4or6 = AF_INET;
696: break;
697: case '6':
698: IPv4or6 = AF_INET6;
699: break;
700: case '?':
701: default:
702: usage();
1.1 markus 703: }
704: }
1.26 markus 705: if (optind == argc && !fopt_count)
1.1 markus 706: usage();
707:
1.26 markus 708: log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1);
709:
1.1 markus 710: maxfd = fdlim_get(1);
711: if (maxfd < 0)
1.4 markus 712: fatal("%s: fdlim_get: bad value", __progname);
1.1 markus 713: if (maxfd > MAXMAXFD)
714: maxfd = MAXMAXFD;
1.18 deraadt 715: if (MAXCON <= 0)
1.4 markus 716: fatal("%s: not enough file descriptors", __progname);
1.1 markus 717: if (maxfd > fdlim_get(0))
718: fdlim_set(maxfd);
1.63 djm 719: fdcon = xcalloc(maxfd, sizeof(con));
1.19 millert 720:
1.63 djm 721: read_wait_nfdset = howmany(maxfd, NFDBITS);
722: read_wait = xcalloc(read_wait_nfdset, sizeof(fd_mask));
1.1 markus 723:
1.82 djm 724: for (j = 0; j < fopt_count; j++) {
725: if (argv[j] == NULL)
726: fp = stdin;
727: else if ((fp = fopen(argv[j], "r")) == NULL)
728: fatal("%s: %s: %s", __progname, argv[j],
729: strerror(errno));
730: linenum = 0;
731:
732: while (read_keyfile_line(fp,
733: argv[j] == NULL ? "(stdin)" : argv[j], line, sizeof(line),
734: &linenum) != -1) {
735: /* Chomp off trailing whitespace and comments */
736: if ((cp = strchr(line, '#')) == NULL)
737: cp = line + strlen(line) - 1;
738: while (cp >= line) {
739: if (*cp == ' ' || *cp == '\t' ||
740: *cp == '\n' || *cp == '#')
741: *cp-- = '\0';
742: else
743: break;
744: }
745:
746: /* Skip empty lines */
747: if (*line == '\0')
1.28 danh 748: continue;
1.82 djm 749:
750: do_host(line);
1.26 markus 751: }
1.82 djm 752:
753: if (ferror(fp))
754: fatal("%s: %s: %s", __progname, argv[j],
755: strerror(errno));
756:
757: fclose(fp);
1.26 markus 758: }
759:
760: while (optind < argc)
761: do_host(argv[optind++]);
1.1 markus 762:
763: while (ncon > 0)
764: conloop();
765:
766: return (0);
767: }