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