1 /* $OpenBSD: ssh-keyscan.c,v 1.79 2009/10/28 16:38:18 reyk Exp $ */ 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 7 * OpenBSD project by leaving this copyright notice intact. 8 */ 9 10 #include <sys/types.h> 11 #include <sys/socket.h> 12 #include <sys/queue.h> 13 #include <sys/time.h> 14 #include <sys/resource.h> 15 16 #include <openssl/bn.h> 17 18 #include <errno.h> 19 #include <netdb.h> 20 #include <setjmp.h> 21 #include <stdarg.h> 22 #include <stdio.h> 23 #include <stdlib.h> 24 #include <signal.h> 25 #include <string.h> 26 #include <unistd.h> 27 28 #include "xmalloc.h" 29 #include "ssh.h" 30 #include "ssh1.h" 31 #include "buffer.h" 32 #include "key.h" 33 #include "cipher.h" 34 #include "kex.h" 35 #include "compat.h" 36 #include "myproposal.h" 37 #include "packet.h" 38 #include "dispatch.h" 39 #include "log.h" 40 #include "atomicio.h" 41 #include "misc.h" 42 #include "hostfile.h" 43 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; 49 50 #define KT_RSA1 1 51 #define KT_DSA 2 52 #define KT_RSA 4 53 54 int get_keytypes = KT_RSA; /* Get only RSA keys by default */ 55 56 int hash_hosts = 0; /* Hash hostname on output */ 57 58 #define MAXMAXFD 256 59 60 /* The number of seconds after which to give up on a TCP connection */ 61 int timeout = 5; 62 63 int maxfd; 64 #define MAXCON (maxfd - 10) 65 66 /* The default routing domain */ 67 int scan_rdomain = -1; 68 69 extern char *__progname; 70 fd_set *read_wait; 71 size_t read_wait_nfdset; 72 int ncon; 73 int nonfatal_fatal = 0; 74 jmp_buf kexjmp; 75 Key *kexjmp_key; 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 { 82 u_char c_status; /* State of connection on this file desc. */ 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. */ 91 int c_keytype; /* Only one of KT_RSA1, KT_DSA, or KT_RSA */ 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 */ 97 Kex *c_kex; /* The key-exchange struct for ssh2 */ 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 105 /* 106 * This is just a wrapper around fgets() to make it usable. 107 */ 108 109 /* Stress-test. Increase this later. */ 110 #define LINEBUF_SIZE 16 111 112 typedef struct { 113 char *buf; 114 u_int size; 115 int lineno; 116 const char *filename; 117 FILE *stream; 118 void (*errfun) (const char *,...); 119 } Linebuf; 120 121 static Linebuf * 122 Linebuf_alloc(const char *filename, void (*errfun) (const char *,...)) 123 { 124 Linebuf *lb; 125 126 if (!(lb = malloc(sizeof(*lb)))) { 127 if (errfun) 128 (*errfun) ("linebuf (%s): malloc failed\n", 129 filename ? filename : "(stdin)"); 130 return (NULL); 131 } 132 if (filename) { 133 lb->filename = filename; 134 if (!(lb->stream = fopen(filename, "r"))) { 135 xfree(lb); 136 if (errfun) 137 (*errfun) ("%s: %s\n", filename, strerror(errno)); 138 return (NULL); 139 } 140 } else { 141 lb->filename = "(stdin)"; 142 lb->stream = stdin; 143 } 144 145 if (!(lb->buf = malloc((lb->size = LINEBUF_SIZE)))) { 146 if (errfun) 147 (*errfun) ("linebuf (%s): malloc failed\n", lb->filename); 148 xfree(lb); 149 return (NULL); 150 } 151 lb->errfun = errfun; 152 lb->lineno = 0; 153 return (lb); 154 } 155 156 static void 157 Linebuf_free(Linebuf * lb) 158 { 159 fclose(lb->stream); 160 xfree(lb->buf); 161 xfree(lb); 162 } 163 164 #if 0 165 static void 166 Linebuf_restart(Linebuf * lb) 167 { 168 clearerr(lb->stream); 169 rewind(lb->stream); 170 lb->lineno = 0; 171 } 172 173 static int 174 Linebuf_lineno(Linebuf * lb) 175 { 176 return (lb->lineno); 177 } 178 #endif 179 180 static char * 181 Linebuf_getline(Linebuf * lb) 182 { 183 size_t n = 0; 184 void *p; 185 186 lb->lineno++; 187 for (;;) { 188 /* Read a line */ 189 if (!fgets(&lb->buf[n], lb->size - n, lb->stream)) { 190 if (ferror(lb->stream) && lb->errfun) 191 (*lb->errfun)("%s: %s\n", lb->filename, 192 strerror(errno)); 193 return (NULL); 194 } 195 n = strlen(lb->buf); 196 197 /* Return it or an error if it fits */ 198 if (n > 0 && lb->buf[n - 1] == '\n') { 199 lb->buf[n - 1] = '\0'; 200 return (lb->buf); 201 } 202 if (n != lb->size - 1) { 203 if (lb->errfun) 204 (*lb->errfun)("%s: skipping incomplete last line\n", 205 lb->filename); 206 return (NULL); 207 } 208 /* Double the buffer if we need more space */ 209 lb->size *= 2; 210 if ((p = realloc(lb->buf, lb->size)) == NULL) { 211 lb->size /= 2; 212 if (lb->errfun) 213 (*lb->errfun)("linebuf (%s): realloc failed\n", 214 lb->filename); 215 return (NULL); 216 } 217 lb->buf = p; 218 } 219 } 220 221 static int 222 fdlim_get(int hard) 223 { 224 struct rlimit rlfd; 225 226 if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0) 227 return (-1); 228 if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY) 229 return sysconf(_SC_OPEN_MAX); 230 else 231 return hard ? rlfd.rlim_max : rlfd.rlim_cur; 232 } 233 234 static int 235 fdlim_set(int lim) 236 { 237 struct rlimit rlfd; 238 239 if (lim <= 0) 240 return (-1); 241 if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0) 242 return (-1); 243 rlfd.rlim_cur = lim; 244 if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0) 245 return (-1); 246 return (0); 247 } 248 249 /* 250 * This is an strsep function that returns a null field for adjacent 251 * separators. This is the same as the 4.4BSD strsep, but different from the 252 * one in the GNU libc. 253 */ 254 static char * 255 xstrsep(char **str, const char *delim) 256 { 257 char *s, *e; 258 259 if (!**str) 260 return (NULL); 261 262 s = *str; 263 e = s + strcspn(s, delim); 264 265 if (*e != '\0') 266 *e++ = '\0'; 267 *str = e; 268 269 return (s); 270 } 271 272 /* 273 * Get the next non-null token (like GNU strsep). Strsep() will return a 274 * null token for two adjacent separators, so we may have to loop. 275 */ 276 static char * 277 strnnsep(char **stringp, char *delim) 278 { 279 char *tok; 280 281 do { 282 tok = xstrsep(stringp, delim); 283 } while (tok && *tok == '\0'); 284 return (tok); 285 } 286 287 static Key * 288 keygrab_ssh1(con *c) 289 { 290 static Key *rsa; 291 static Buffer msg; 292 293 if (rsa == NULL) { 294 buffer_init(&msg); 295 rsa = key_new(KEY_RSA1); 296 } 297 buffer_append(&msg, c->c_data, c->c_plen); 298 buffer_consume(&msg, 8 - (c->c_plen & 7)); /* padding */ 299 if (buffer_get_char(&msg) != (int) SSH_SMSG_PUBLIC_KEY) { 300 error("%s: invalid packet type", c->c_name); 301 buffer_clear(&msg); 302 return NULL; 303 } 304 buffer_consume(&msg, 8); /* cookie */ 305 306 /* server key */ 307 (void) buffer_get_int(&msg); 308 buffer_get_bignum(&msg, rsa->rsa->e); 309 buffer_get_bignum(&msg, rsa->rsa->n); 310 311 /* host key */ 312 (void) buffer_get_int(&msg); 313 buffer_get_bignum(&msg, rsa->rsa->e); 314 buffer_get_bignum(&msg, rsa->rsa->n); 315 316 buffer_clear(&msg); 317 318 return (rsa); 319 } 320 321 static int 322 hostjump(Key *hostkey) 323 { 324 kexjmp_key = hostkey; 325 longjmp(kexjmp, 1); 326 } 327 328 static int 329 ssh2_capable(int remote_major, int remote_minor) 330 { 331 switch (remote_major) { 332 case 1: 333 if (remote_minor == 99) 334 return 1; 335 break; 336 case 2: 337 return 1; 338 default: 339 break; 340 } 341 return 0; 342 } 343 344 static Key * 345 keygrab_ssh2(con *c) 346 { 347 int j; 348 349 packet_set_connection(c->c_fd, c->c_fd); 350 enable_compat20(); 351 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = c->c_keytype == KT_DSA? 352 "ssh-dss": "ssh-rsa"; 353 c->c_kex = kex_setup(myproposal); 354 c->c_kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client; 355 c->c_kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client; 356 c->c_kex->kex[KEX_DH_GEX_SHA1] = kexgex_client; 357 c->c_kex->kex[KEX_DH_GEX_SHA256] = kexgex_client; 358 c->c_kex->verify_host_key = hostjump; 359 360 if (!(j = setjmp(kexjmp))) { 361 nonfatal_fatal = 1; 362 dispatch_run(DISPATCH_BLOCK, &c->c_kex->done, c->c_kex); 363 fprintf(stderr, "Impossible! dispatch_run() returned!\n"); 364 exit(1); 365 } 366 nonfatal_fatal = 0; 367 xfree(c->c_kex); 368 c->c_kex = NULL; 369 packet_close(); 370 371 return j < 0? NULL : kexjmp_key; 372 } 373 374 static void 375 keyprint(con *c, Key *key) 376 { 377 char *host = c->c_output_name ? c->c_output_name : c->c_name; 378 379 if (!key) 380 return; 381 if (hash_hosts && (host = host_hash(host, NULL, 0)) == NULL) 382 fatal("host_hash failed"); 383 384 fprintf(stdout, "%s ", host); 385 key_write(key, stdout); 386 fputs("\n", stdout); 387 } 388 389 static int 390 tcpconnect(char *host) 391 { 392 struct addrinfo hints, *ai, *aitop; 393 char strport[NI_MAXSERV]; 394 int gaierr, s = -1; 395 396 snprintf(strport, sizeof strport, "%d", ssh_port); 397 memset(&hints, 0, sizeof(hints)); 398 hints.ai_family = IPv4or6; 399 hints.ai_socktype = SOCK_STREAM; 400 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) 401 fatal("getaddrinfo %s: %s", host, ssh_gai_strerror(gaierr)); 402 for (ai = aitop; ai; ai = ai->ai_next) { 403 s = socket_rdomain(ai->ai_family, ai->ai_socktype, 404 ai->ai_protocol, scan_rdomain); 405 if (s < 0) { 406 error("socket: %s", strerror(errno)); 407 continue; 408 } 409 if (set_nonblock(s) == -1) 410 fatal("%s: set_nonblock(%d)", __func__, s); 411 if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 && 412 errno != EINPROGRESS) 413 error("connect (`%s'): %s", host, strerror(errno)); 414 else 415 break; 416 close(s); 417 s = -1; 418 } 419 freeaddrinfo(aitop); 420 return s; 421 } 422 423 static int 424 conalloc(char *iname, char *oname, int keytype) 425 { 426 char *namebase, *name, *namelist; 427 int s; 428 429 namebase = namelist = xstrdup(iname); 430 431 do { 432 name = xstrsep(&namelist, ","); 433 if (!name) { 434 xfree(namebase); 435 return (-1); 436 } 437 } while ((s = tcpconnect(name)) < 0); 438 439 if (s >= maxfd) 440 fatal("conalloc: fdno %d too high", s); 441 if (fdcon[s].c_status) 442 fatal("conalloc: attempt to reuse fdno %d", s); 443 444 fdcon[s].c_fd = s; 445 fdcon[s].c_status = CS_CON; 446 fdcon[s].c_namebase = namebase; 447 fdcon[s].c_name = name; 448 fdcon[s].c_namelist = namelist; 449 fdcon[s].c_output_name = xstrdup(oname); 450 fdcon[s].c_data = (char *) &fdcon[s].c_plen; 451 fdcon[s].c_len = 4; 452 fdcon[s].c_off = 0; 453 fdcon[s].c_keytype = keytype; 454 gettimeofday(&fdcon[s].c_tv, NULL); 455 fdcon[s].c_tv.tv_sec += timeout; 456 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link); 457 FD_SET(s, read_wait); 458 ncon++; 459 return (s); 460 } 461 462 static void 463 confree(int s) 464 { 465 if (s >= maxfd || fdcon[s].c_status == CS_UNUSED) 466 fatal("confree: attempt to free bad fdno %d", s); 467 close(s); 468 xfree(fdcon[s].c_namebase); 469 xfree(fdcon[s].c_output_name); 470 if (fdcon[s].c_status == CS_KEYS) 471 xfree(fdcon[s].c_data); 472 fdcon[s].c_status = CS_UNUSED; 473 fdcon[s].c_keytype = 0; 474 TAILQ_REMOVE(&tq, &fdcon[s], c_link); 475 FD_CLR(s, read_wait); 476 ncon--; 477 } 478 479 static void 480 contouch(int s) 481 { 482 TAILQ_REMOVE(&tq, &fdcon[s], c_link); 483 gettimeofday(&fdcon[s].c_tv, NULL); 484 fdcon[s].c_tv.tv_sec += timeout; 485 TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link); 486 } 487 488 static int 489 conrecycle(int s) 490 { 491 con *c = &fdcon[s]; 492 int ret; 493 494 ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype); 495 confree(s); 496 return (ret); 497 } 498 499 static void 500 congreet(int s) 501 { 502 int n = 0, remote_major = 0, remote_minor = 0; 503 char buf[256], *cp; 504 char remote_version[sizeof buf]; 505 size_t bufsiz; 506 con *c = &fdcon[s]; 507 508 for (;;) { 509 memset(buf, '\0', sizeof(buf)); 510 bufsiz = sizeof(buf); 511 cp = buf; 512 while (bufsiz-- && 513 (n = atomicio(read, s, cp, 1)) == 1 && *cp != '\n') { 514 if (*cp == '\r') 515 *cp = '\n'; 516 cp++; 517 } 518 if (n != 1 || strncmp(buf, "SSH-", 4) == 0) 519 break; 520 } 521 if (n == 0) { 522 switch (errno) { 523 case EPIPE: 524 error("%s: Connection closed by remote host", c->c_name); 525 break; 526 case ECONNREFUSED: 527 break; 528 default: 529 error("read (%s): %s", c->c_name, strerror(errno)); 530 break; 531 } 532 conrecycle(s); 533 return; 534 } 535 if (*cp != '\n' && *cp != '\r') { 536 error("%s: bad greeting", c->c_name); 537 confree(s); 538 return; 539 } 540 *cp = '\0'; 541 if (sscanf(buf, "SSH-%d.%d-%[^\n]\n", 542 &remote_major, &remote_minor, remote_version) == 3) 543 compat_datafellows(remote_version); 544 else 545 datafellows = 0; 546 if (c->c_keytype != KT_RSA1) { 547 if (!ssh2_capable(remote_major, remote_minor)) { 548 debug("%s doesn't support ssh2", c->c_name); 549 confree(s); 550 return; 551 } 552 } else if (remote_major != 1) { 553 debug("%s doesn't support ssh1", c->c_name); 554 confree(s); 555 return; 556 } 557 fprintf(stderr, "# %s %s\n", c->c_name, chop(buf)); 558 n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n", 559 c->c_keytype == KT_RSA1? PROTOCOL_MAJOR_1 : PROTOCOL_MAJOR_2, 560 c->c_keytype == KT_RSA1? PROTOCOL_MINOR_1 : PROTOCOL_MINOR_2); 561 if (n < 0 || (size_t)n >= sizeof(buf)) { 562 error("snprintf: buffer too small"); 563 confree(s); 564 return; 565 } 566 if (atomicio(vwrite, s, buf, n) != (size_t)n) { 567 error("write (%s): %s", c->c_name, strerror(errno)); 568 confree(s); 569 return; 570 } 571 if (c->c_keytype != KT_RSA1) { 572 keyprint(c, keygrab_ssh2(c)); 573 confree(s); 574 return; 575 } 576 c->c_status = CS_SIZE; 577 contouch(s); 578 } 579 580 static void 581 conread(int s) 582 { 583 con *c = &fdcon[s]; 584 size_t n; 585 586 if (c->c_status == CS_CON) { 587 congreet(s); 588 return; 589 } 590 n = atomicio(read, s, c->c_data + c->c_off, c->c_len - c->c_off); 591 if (n == 0) { 592 error("read (%s): %s", c->c_name, strerror(errno)); 593 confree(s); 594 return; 595 } 596 c->c_off += n; 597 598 if (c->c_off == c->c_len) 599 switch (c->c_status) { 600 case CS_SIZE: 601 c->c_plen = htonl(c->c_plen); 602 c->c_len = c->c_plen + 8 - (c->c_plen & 7); 603 c->c_off = 0; 604 c->c_data = xmalloc(c->c_len); 605 c->c_status = CS_KEYS; 606 break; 607 case CS_KEYS: 608 keyprint(c, keygrab_ssh1(c)); 609 confree(s); 610 return; 611 default: 612 fatal("conread: invalid status %d", c->c_status); 613 break; 614 } 615 616 contouch(s); 617 } 618 619 static void 620 conloop(void) 621 { 622 struct timeval seltime, now; 623 fd_set *r, *e; 624 con *c; 625 int i; 626 627 gettimeofday(&now, NULL); 628 c = TAILQ_FIRST(&tq); 629 630 if (c && (c->c_tv.tv_sec > now.tv_sec || 631 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) { 632 seltime = c->c_tv; 633 seltime.tv_sec -= now.tv_sec; 634 seltime.tv_usec -= now.tv_usec; 635 if (seltime.tv_usec < 0) { 636 seltime.tv_usec += 1000000; 637 seltime.tv_sec--; 638 } 639 } else 640 seltime.tv_sec = seltime.tv_usec = 0; 641 642 r = xcalloc(read_wait_nfdset, sizeof(fd_mask)); 643 e = xcalloc(read_wait_nfdset, sizeof(fd_mask)); 644 memcpy(r, read_wait, read_wait_nfdset * sizeof(fd_mask)); 645 memcpy(e, read_wait, read_wait_nfdset * sizeof(fd_mask)); 646 647 while (select(maxfd, r, NULL, e, &seltime) == -1 && 648 (errno == EAGAIN || errno == EINTR)) 649 ; 650 651 for (i = 0; i < maxfd; i++) { 652 if (FD_ISSET(i, e)) { 653 error("%s: exception!", fdcon[i].c_name); 654 confree(i); 655 } else if (FD_ISSET(i, r)) 656 conread(i); 657 } 658 xfree(r); 659 xfree(e); 660 661 c = TAILQ_FIRST(&tq); 662 while (c && (c->c_tv.tv_sec < now.tv_sec || 663 (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) { 664 int s = c->c_fd; 665 666 c = TAILQ_NEXT(c, c_link); 667 conrecycle(s); 668 } 669 } 670 671 static void 672 do_host(char *host) 673 { 674 char *name = strnnsep(&host, " \t\n"); 675 int j; 676 677 if (name == NULL) 678 return; 679 for (j = KT_RSA1; j <= KT_RSA; j *= 2) { 680 if (get_keytypes & j) { 681 while (ncon >= MAXCON) 682 conloop(); 683 conalloc(name, *host ? host : name, j); 684 } 685 } 686 } 687 688 void 689 fatal(const char *fmt,...) 690 { 691 va_list args; 692 693 va_start(args, fmt); 694 do_log(SYSLOG_LEVEL_FATAL, fmt, args); 695 va_end(args); 696 if (nonfatal_fatal) 697 longjmp(kexjmp, -1); 698 else 699 exit(255); 700 } 701 702 static void 703 usage(void) 704 { 705 fprintf(stderr, 706 "usage: %s [-46Hv] [-f file] [-p port] [-T timeout] [-t type]\n" 707 "\t\t [-V rdomain] [host | addrlist namelist] ...\n", 708 __progname); 709 exit(1); 710 } 711 712 int 713 main(int argc, char **argv) 714 { 715 int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO; 716 int opt, fopt_count = 0; 717 char *tname; 718 719 extern int optind; 720 extern char *optarg; 721 722 TAILQ_INIT(&tq); 723 724 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ 725 sanitise_stdfd(); 726 727 if (argc <= 1) 728 usage(); 729 730 while ((opt = getopt(argc, argv, "Hv46p:T:t:f:V:")) != -1) { 731 switch (opt) { 732 case 'H': 733 hash_hosts = 1; 734 break; 735 case 'p': 736 ssh_port = a2port(optarg); 737 if (ssh_port <= 0) { 738 fprintf(stderr, "Bad port '%s'\n", optarg); 739 exit(1); 740 } 741 break; 742 case 'T': 743 timeout = convtime(optarg); 744 if (timeout == -1 || timeout == 0) { 745 fprintf(stderr, "Bad timeout '%s'\n", optarg); 746 usage(); 747 } 748 break; 749 case 'v': 750 if (!debug_flag) { 751 debug_flag = 1; 752 log_level = SYSLOG_LEVEL_DEBUG1; 753 } 754 else if (log_level < SYSLOG_LEVEL_DEBUG3) 755 log_level++; 756 else 757 fatal("Too high debugging level."); 758 break; 759 case 'f': 760 if (strcmp(optarg, "-") == 0) 761 optarg = NULL; 762 argv[fopt_count++] = optarg; 763 break; 764 case 't': 765 get_keytypes = 0; 766 tname = strtok(optarg, ","); 767 while (tname) { 768 int type = key_type_from_name(tname); 769 switch (type) { 770 case KEY_RSA1: 771 get_keytypes |= KT_RSA1; 772 break; 773 case KEY_DSA: 774 get_keytypes |= KT_DSA; 775 break; 776 case KEY_RSA: 777 get_keytypes |= KT_RSA; 778 break; 779 case KEY_UNSPEC: 780 fatal("unknown key type %s", tname); 781 } 782 tname = strtok(NULL, ","); 783 } 784 break; 785 case '4': 786 IPv4or6 = AF_INET; 787 break; 788 case '6': 789 IPv4or6 = AF_INET6; 790 break; 791 case 'V': 792 scan_rdomain = a2port(optarg); 793 if (scan_rdomain < 0) 794 scan_rdomain = -1; 795 break; 796 case '?': 797 default: 798 usage(); 799 } 800 } 801 if (optind == argc && !fopt_count) 802 usage(); 803 804 log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1); 805 806 maxfd = fdlim_get(1); 807 if (maxfd < 0) 808 fatal("%s: fdlim_get: bad value", __progname); 809 if (maxfd > MAXMAXFD) 810 maxfd = MAXMAXFD; 811 if (MAXCON <= 0) 812 fatal("%s: not enough file descriptors", __progname); 813 if (maxfd > fdlim_get(0)) 814 fdlim_set(maxfd); 815 fdcon = xcalloc(maxfd, sizeof(con)); 816 817 read_wait_nfdset = howmany(maxfd, NFDBITS); 818 read_wait = xcalloc(read_wait_nfdset, sizeof(fd_mask)); 819 820 if (fopt_count) { 821 Linebuf *lb; 822 char *line; 823 int j; 824 825 for (j = 0; j < fopt_count; j++) { 826 lb = Linebuf_alloc(argv[j], error); 827 if (!lb) 828 continue; 829 while ((line = Linebuf_getline(lb)) != NULL) 830 do_host(line); 831 Linebuf_free(lb); 832 } 833 } 834 835 while (optind < argc) 836 do_host(argv[optind++]); 837 838 while (ncon > 0) 839 conloop(); 840 841 return (0); 842 } 843