1 /* 2 * Author: Tatu Ylonen <ylo@cs.hut.fi> 3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4 * All rights reserved 5 * Code to connect to a remote host, and to perform the client side of the 6 * login (authentication) dialog. 7 * 8 * As far as I am concerned, the code I have written for this software 9 * can be used freely for any purpose. Any derived versions of this 10 * software must be clearly marked as such, and if the derived work is 11 * incompatible with the protocol description in the RFC file, it must be 12 * called by a name other than "ssh" or "Secure Shell". 13 */ 14 15 #include "includes.h" 16 RCSID("$OpenBSD: sshconnect.c,v 1.158 2004/06/21 17:36:31 avsm Exp $"); 17 18 #include <openssl/bn.h> 19 20 #include "ssh.h" 21 #include "xmalloc.h" 22 #include "rsa.h" 23 #include "buffer.h" 24 #include "packet.h" 25 #include "uidswap.h" 26 #include "compat.h" 27 #include "key.h" 28 #include "sshconnect.h" 29 #include "hostfile.h" 30 #include "log.h" 31 #include "readconf.h" 32 #include "atomicio.h" 33 #include "misc.h" 34 35 #include "dns.h" 36 37 char *client_version_string = NULL; 38 char *server_version_string = NULL; 39 40 int matching_host_key_dns = 0; 41 42 /* import */ 43 extern Options options; 44 extern char *__progname; 45 extern uid_t original_real_uid; 46 extern uid_t original_effective_uid; 47 extern pid_t proxy_command_pid; 48 49 static int show_other_keys(const char *, Key *); 50 static void warn_changed_key(Key *); 51 52 /* 53 * Connect to the given ssh server using a proxy command. 54 */ 55 static int 56 ssh_proxy_connect(const char *host, u_short port, const char *proxy_command) 57 { 58 Buffer command; 59 const char *cp; 60 char *command_string; 61 int pin[2], pout[2]; 62 pid_t pid; 63 char strport[NI_MAXSERV]; 64 65 /* Convert the port number into a string. */ 66 snprintf(strport, sizeof strport, "%hu", port); 67 68 /* 69 * Build the final command string in the buffer by making the 70 * appropriate substitutions to the given proxy command. 71 * 72 * Use "exec" to avoid "sh -c" processes on some platforms 73 * (e.g. Solaris) 74 */ 75 buffer_init(&command); 76 buffer_append(&command, "exec ", 5); 77 78 for (cp = proxy_command; *cp; cp++) { 79 if (cp[0] == '%' && cp[1] == '%') { 80 buffer_append(&command, "%", 1); 81 cp++; 82 continue; 83 } 84 if (cp[0] == '%' && cp[1] == 'h') { 85 buffer_append(&command, host, strlen(host)); 86 cp++; 87 continue; 88 } 89 if (cp[0] == '%' && cp[1] == 'p') { 90 buffer_append(&command, strport, strlen(strport)); 91 cp++; 92 continue; 93 } 94 buffer_append(&command, cp, 1); 95 } 96 buffer_append(&command, "\0", 1); 97 98 /* Get the final command string. */ 99 command_string = buffer_ptr(&command); 100 101 /* Create pipes for communicating with the proxy. */ 102 if (pipe(pin) < 0 || pipe(pout) < 0) 103 fatal("Could not create pipes to communicate with the proxy: %.100s", 104 strerror(errno)); 105 106 debug("Executing proxy command: %.500s", command_string); 107 108 /* Fork and execute the proxy command. */ 109 if ((pid = fork()) == 0) { 110 char *argv[10]; 111 112 /* Child. Permanently give up superuser privileges. */ 113 seteuid(original_real_uid); 114 setuid(original_real_uid); 115 116 /* Redirect stdin and stdout. */ 117 close(pin[1]); 118 if (pin[0] != 0) { 119 if (dup2(pin[0], 0) < 0) 120 perror("dup2 stdin"); 121 close(pin[0]); 122 } 123 close(pout[0]); 124 if (dup2(pout[1], 1) < 0) 125 perror("dup2 stdout"); 126 /* Cannot be 1 because pin allocated two descriptors. */ 127 close(pout[1]); 128 129 /* Stderr is left as it is so that error messages get 130 printed on the user's terminal. */ 131 argv[0] = _PATH_BSHELL; 132 argv[1] = "-c"; 133 argv[2] = command_string; 134 argv[3] = NULL; 135 136 /* Execute the proxy command. Note that we gave up any 137 extra privileges above. */ 138 execv(argv[0], argv); 139 perror(argv[0]); 140 exit(1); 141 } 142 /* Parent. */ 143 if (pid < 0) 144 fatal("fork failed: %.100s", strerror(errno)); 145 else 146 proxy_command_pid = pid; /* save pid to clean up later */ 147 148 /* Close child side of the descriptors. */ 149 close(pin[0]); 150 close(pout[1]); 151 152 /* Free the command name. */ 153 buffer_free(&command); 154 155 /* Set the connection file descriptors. */ 156 packet_set_connection(pout[0], pin[1]); 157 158 /* Indicate OK return */ 159 return 0; 160 } 161 162 /* 163 * Creates a (possibly privileged) socket for use as the ssh connection. 164 */ 165 static int 166 ssh_create_socket(int privileged, struct addrinfo *ai) 167 { 168 int sock, gaierr; 169 struct addrinfo hints, *res; 170 171 /* 172 * If we are running as root and want to connect to a privileged 173 * port, bind our own socket to a privileged port. 174 */ 175 if (privileged) { 176 int p = IPPORT_RESERVED - 1; 177 PRIV_START; 178 sock = rresvport_af(&p, ai->ai_family); 179 PRIV_END; 180 if (sock < 0) 181 error("rresvport: af=%d %.100s", ai->ai_family, 182 strerror(errno)); 183 else 184 debug("Allocated local port %d.", p); 185 return sock; 186 } 187 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); 188 if (sock < 0) 189 error("socket: %.100s", strerror(errno)); 190 191 /* Bind the socket to an alternative local IP address */ 192 if (options.bind_address == NULL) 193 return sock; 194 195 memset(&hints, 0, sizeof(hints)); 196 hints.ai_family = ai->ai_family; 197 hints.ai_socktype = ai->ai_socktype; 198 hints.ai_protocol = ai->ai_protocol; 199 hints.ai_flags = AI_PASSIVE; 200 gaierr = getaddrinfo(options.bind_address, "0", &hints, &res); 201 if (gaierr) { 202 error("getaddrinfo: %s: %s", options.bind_address, 203 gai_strerror(gaierr)); 204 close(sock); 205 return -1; 206 } 207 if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) { 208 error("bind: %s: %s", options.bind_address, strerror(errno)); 209 close(sock); 210 freeaddrinfo(res); 211 return -1; 212 } 213 freeaddrinfo(res); 214 return sock; 215 } 216 217 static int 218 timeout_connect(int sockfd, const struct sockaddr *serv_addr, 219 socklen_t addrlen, int timeout) 220 { 221 fd_set *fdset; 222 struct timeval tv; 223 socklen_t optlen; 224 int fdsetsz, optval, rc, result = -1; 225 226 if (timeout <= 0) 227 return (connect(sockfd, serv_addr, addrlen)); 228 229 set_nonblock(sockfd); 230 rc = connect(sockfd, serv_addr, addrlen); 231 if (rc == 0) { 232 unset_nonblock(sockfd); 233 return (0); 234 } 235 if (errno != EINPROGRESS) 236 return (-1); 237 238 fdsetsz = howmany(sockfd + 1, NFDBITS) * sizeof(fd_mask); 239 fdset = (fd_set *)xmalloc(fdsetsz); 240 241 memset(fdset, 0, fdsetsz); 242 FD_SET(sockfd, fdset); 243 tv.tv_sec = timeout; 244 tv.tv_usec = 0; 245 246 for(;;) { 247 rc = select(sockfd + 1, NULL, fdset, NULL, &tv); 248 if (rc != -1 || errno != EINTR) 249 break; 250 } 251 252 switch(rc) { 253 case 0: 254 /* Timed out */ 255 errno = ETIMEDOUT; 256 break; 257 case -1: 258 /* Select error */ 259 debug("select: %s", strerror(errno)); 260 break; 261 case 1: 262 /* Completed or failed */ 263 optval = 0; 264 optlen = sizeof(optval); 265 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval, 266 &optlen) == -1) { 267 debug("getsockopt: %s", strerror(errno)); 268 break; 269 } 270 if (optval != 0) { 271 errno = optval; 272 break; 273 } 274 result = 0; 275 unset_nonblock(sockfd); 276 break; 277 default: 278 /* Should not occur */ 279 fatal("Bogus return (%d) from select()", rc); 280 } 281 282 xfree(fdset); 283 return (result); 284 } 285 286 /* 287 * Opens a TCP/IP connection to the remote server on the given host. 288 * The address of the remote host will be returned in hostaddr. 289 * If port is 0, the default port will be used. If needpriv is true, 290 * a privileged port will be allocated to make the connection. 291 * This requires super-user privileges if needpriv is true. 292 * Connection_attempts specifies the maximum number of tries (one per 293 * second). If proxy_command is non-NULL, it specifies the command (with %h 294 * and %p substituted for host and port, respectively) to use to contact 295 * the daemon. 296 * Return values: 297 * 0 for OK 298 * ECONNREFUSED if we got a "Connection Refused" by the peer on any address 299 * ECONNABORTED if we failed without a "Connection refused" 300 * Suitable error messages for the connection failure will already have been 301 * printed. 302 */ 303 int 304 ssh_connect(const char *host, struct sockaddr_storage * hostaddr, 305 u_short port, int family, int connection_attempts, 306 int needpriv, const char *proxy_command) 307 { 308 int gaierr; 309 int on = 1; 310 int sock = -1, attempt; 311 char ntop[NI_MAXHOST], strport[NI_MAXSERV]; 312 struct addrinfo hints, *ai, *aitop; 313 struct servent *sp; 314 /* 315 * Did we get only other errors than "Connection refused" (which 316 * should block fallback to rsh and similar), or did we get at least 317 * one "Connection refused"? 318 */ 319 int full_failure = 1; 320 321 debug2("ssh_connect: needpriv %d", needpriv); 322 323 /* Get default port if port has not been set. */ 324 if (port == 0) { 325 sp = getservbyname(SSH_SERVICE_NAME, "tcp"); 326 if (sp) 327 port = ntohs(sp->s_port); 328 else 329 port = SSH_DEFAULT_PORT; 330 } 331 /* If a proxy command is given, connect using it. */ 332 if (proxy_command != NULL) 333 return ssh_proxy_connect(host, port, proxy_command); 334 335 /* No proxy command. */ 336 337 memset(&hints, 0, sizeof(hints)); 338 hints.ai_family = family; 339 hints.ai_socktype = SOCK_STREAM; 340 snprintf(strport, sizeof strport, "%u", port); 341 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) 342 fatal("%s: %.100s: %s", __progname, host, 343 gai_strerror(gaierr)); 344 345 /* 346 * Try to connect several times. On some machines, the first time 347 * will sometimes fail. In general socket code appears to behave 348 * quite magically on many machines. 349 */ 350 for (attempt = 0; ;) { 351 if (attempt > 0) 352 debug("Trying again..."); 353 354 /* Loop through addresses for this host, and try each one in 355 sequence until the connection succeeds. */ 356 for (ai = aitop; ai; ai = ai->ai_next) { 357 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) 358 continue; 359 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, 360 ntop, sizeof(ntop), strport, sizeof(strport), 361 NI_NUMERICHOST|NI_NUMERICSERV) != 0) { 362 error("ssh_connect: getnameinfo failed"); 363 continue; 364 } 365 debug("Connecting to %.200s [%.100s] port %s.", 366 host, ntop, strport); 367 368 /* Create a socket for connecting. */ 369 sock = ssh_create_socket(needpriv, ai); 370 if (sock < 0) 371 /* Any error is already output */ 372 continue; 373 374 if (timeout_connect(sock, ai->ai_addr, ai->ai_addrlen, 375 options.connection_timeout) >= 0) { 376 /* Successful connection. */ 377 memcpy(hostaddr, ai->ai_addr, ai->ai_addrlen); 378 break; 379 } else { 380 if (errno == ECONNREFUSED) 381 full_failure = 0; 382 debug("connect to address %s port %s: %s", 383 ntop, strport, strerror(errno)); 384 /* 385 * Close the failed socket; there appear to 386 * be some problems when reusing a socket for 387 * which connect() has already returned an 388 * error. 389 */ 390 close(sock); 391 } 392 } 393 if (ai) 394 break; /* Successful connection. */ 395 396 attempt++; 397 if (attempt >= connection_attempts) 398 break; 399 /* Sleep a moment before retrying. */ 400 sleep(1); 401 } 402 403 freeaddrinfo(aitop); 404 405 /* Return failure if we didn't get a successful connection. */ 406 if (attempt >= connection_attempts) { 407 logit("ssh: connect to host %s port %s: %s", 408 host, strport, strerror(errno)); 409 return full_failure ? ECONNABORTED : ECONNREFUSED; 410 } 411 412 debug("Connection established."); 413 414 /* Set SO_KEEPALIVE if requested. */ 415 if (options.tcp_keep_alive && 416 setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, 417 sizeof(on)) < 0) 418 error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno)); 419 420 /* Set the connection. */ 421 packet_set_connection(sock, sock); 422 423 return 0; 424 } 425 426 /* 427 * Waits for the server identification string, and sends our own 428 * identification string. 429 */ 430 static void 431 ssh_exchange_identification(void) 432 { 433 char buf[256], remote_version[256]; /* must be same size! */ 434 int remote_major, remote_minor, i, mismatch; 435 int connection_in = packet_get_connection_in(); 436 int connection_out = packet_get_connection_out(); 437 int minor1 = PROTOCOL_MINOR_1; 438 439 /* Read other side\'s version identification. */ 440 for (;;) { 441 for (i = 0; i < sizeof(buf) - 1; i++) { 442 int len = atomicio(read, connection_in, &buf[i], 1); 443 if (len < 0) 444 fatal("ssh_exchange_identification: read: %.100s", strerror(errno)); 445 if (len != 1) 446 fatal("ssh_exchange_identification: Connection closed by remote host"); 447 if (buf[i] == '\r') { 448 buf[i] = '\n'; 449 buf[i + 1] = 0; 450 continue; /**XXX wait for \n */ 451 } 452 if (buf[i] == '\n') { 453 buf[i + 1] = 0; 454 break; 455 } 456 } 457 buf[sizeof(buf) - 1] = 0; 458 if (strncmp(buf, "SSH-", 4) == 0) 459 break; 460 debug("ssh_exchange_identification: %s", buf); 461 } 462 server_version_string = xstrdup(buf); 463 464 /* 465 * Check that the versions match. In future this might accept 466 * several versions and set appropriate flags to handle them. 467 */ 468 if (sscanf(server_version_string, "SSH-%d.%d-%[^\n]\n", 469 &remote_major, &remote_minor, remote_version) != 3) 470 fatal("Bad remote protocol version identification: '%.100s'", buf); 471 debug("Remote protocol version %d.%d, remote software version %.100s", 472 remote_major, remote_minor, remote_version); 473 474 compat_datafellows(remote_version); 475 mismatch = 0; 476 477 switch (remote_major) { 478 case 1: 479 if (remote_minor == 99 && 480 (options.protocol & SSH_PROTO_2) && 481 !(options.protocol & SSH_PROTO_1_PREFERRED)) { 482 enable_compat20(); 483 break; 484 } 485 if (!(options.protocol & SSH_PROTO_1)) { 486 mismatch = 1; 487 break; 488 } 489 if (remote_minor < 3) { 490 fatal("Remote machine has too old SSH software version."); 491 } else if (remote_minor == 3 || remote_minor == 4) { 492 /* We speak 1.3, too. */ 493 enable_compat13(); 494 minor1 = 3; 495 if (options.forward_agent) { 496 logit("Agent forwarding disabled for protocol 1.3"); 497 options.forward_agent = 0; 498 } 499 } 500 break; 501 case 2: 502 if (options.protocol & SSH_PROTO_2) { 503 enable_compat20(); 504 break; 505 } 506 /* FALLTHROUGH */ 507 default: 508 mismatch = 1; 509 break; 510 } 511 if (mismatch) 512 fatal("Protocol major versions differ: %d vs. %d", 513 (options.protocol & SSH_PROTO_2) ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1, 514 remote_major); 515 /* Send our own protocol version identification. */ 516 snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n", 517 compat20 ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1, 518 compat20 ? PROTOCOL_MINOR_2 : minor1, 519 SSH_VERSION); 520 if (atomicio(vwrite, connection_out, buf, strlen(buf)) != strlen(buf)) 521 fatal("write: %.100s", strerror(errno)); 522 client_version_string = xstrdup(buf); 523 chop(client_version_string); 524 chop(server_version_string); 525 debug("Local version string %.100s", client_version_string); 526 } 527 528 /* defaults to 'no' */ 529 static int 530 confirm(const char *prompt) 531 { 532 const char *msg, *again = "Please type 'yes' or 'no': "; 533 char *p; 534 int ret = -1; 535 536 if (options.batch_mode) 537 return 0; 538 for (msg = prompt;;msg = again) { 539 p = read_passphrase(msg, RP_ECHO); 540 if (p == NULL || 541 (p[0] == '\0') || (p[0] == '\n') || 542 strncasecmp(p, "no", 2) == 0) 543 ret = 0; 544 if (p && strncasecmp(p, "yes", 3) == 0) 545 ret = 1; 546 if (p) 547 xfree(p); 548 if (ret != -1) 549 return ret; 550 } 551 } 552 553 /* 554 * check whether the supplied host key is valid, return -1 if the key 555 * is not valid. the user_hostfile will not be updated if 'readonly' is true. 556 */ 557 static int 558 check_host_key(char *host, struct sockaddr *hostaddr, Key *host_key, 559 int readonly, const char *user_hostfile, const char *system_hostfile) 560 { 561 Key *file_key; 562 const char *type = key_type(host_key); 563 char *ip = NULL; 564 char hostline[1000], *hostp, *fp; 565 HostStatus host_status; 566 HostStatus ip_status; 567 int local = 0, host_ip_differ = 0; 568 char ntop[NI_MAXHOST]; 569 char msg[1024]; 570 int len, host_line, ip_line; 571 const char *host_file = NULL, *ip_file = NULL; 572 573 /* 574 * Force accepting of the host key for loopback/localhost. The 575 * problem is that if the home directory is NFS-mounted to multiple 576 * machines, localhost will refer to a different machine in each of 577 * them, and the user will get bogus HOST_CHANGED warnings. This 578 * essentially disables host authentication for localhost; however, 579 * this is probably not a real problem. 580 */ 581 /** hostaddr == 0! */ 582 switch (hostaddr->sa_family) { 583 case AF_INET: 584 local = (ntohl(((struct sockaddr_in *)hostaddr)-> 585 sin_addr.s_addr) >> 24) == IN_LOOPBACKNET; 586 break; 587 case AF_INET6: 588 local = IN6_IS_ADDR_LOOPBACK( 589 &(((struct sockaddr_in6 *)hostaddr)->sin6_addr)); 590 break; 591 default: 592 local = 0; 593 break; 594 } 595 if (options.no_host_authentication_for_localhost == 1 && local && 596 options.host_key_alias == NULL) { 597 debug("Forcing accepting of host key for " 598 "loopback/localhost."); 599 return 0; 600 } 601 602 /* 603 * We don't have the remote ip-address for connections 604 * using a proxy command 605 */ 606 if (options.proxy_command == NULL) { 607 if (getnameinfo(hostaddr, hostaddr->sa_len, ntop, sizeof(ntop), 608 NULL, 0, NI_NUMERICHOST) != 0) 609 fatal("check_host_key: getnameinfo failed"); 610 ip = xstrdup(ntop); 611 } else { 612 ip = xstrdup("<no hostip for proxy command>"); 613 } 614 /* 615 * Turn off check_host_ip if the connection is to localhost, via proxy 616 * command or if we don't have a hostname to compare with 617 */ 618 if (options.check_host_ip && 619 (local || strcmp(host, ip) == 0 || options.proxy_command != NULL)) 620 options.check_host_ip = 0; 621 622 /* 623 * Allow the user to record the key under a different name. This is 624 * useful for ssh tunneling over forwarded connections or if you run 625 * multiple sshd's on different ports on the same machine. 626 */ 627 if (options.host_key_alias != NULL) { 628 host = options.host_key_alias; 629 debug("using hostkeyalias: %s", host); 630 } 631 632 /* 633 * Store the host key from the known host file in here so that we can 634 * compare it with the key for the IP address. 635 */ 636 file_key = key_new(host_key->type); 637 638 /* 639 * Check if the host key is present in the user\'s list of known 640 * hosts or in the systemwide list. 641 */ 642 host_file = user_hostfile; 643 host_status = check_host_in_hostfile(host_file, host, host_key, 644 file_key, &host_line); 645 if (host_status == HOST_NEW) { 646 host_file = system_hostfile; 647 host_status = check_host_in_hostfile(host_file, host, host_key, 648 file_key, &host_line); 649 } 650 /* 651 * Also perform check for the ip address, skip the check if we are 652 * localhost or the hostname was an ip address to begin with 653 */ 654 if (options.check_host_ip) { 655 Key *ip_key = key_new(host_key->type); 656 657 ip_file = user_hostfile; 658 ip_status = check_host_in_hostfile(ip_file, ip, host_key, 659 ip_key, &ip_line); 660 if (ip_status == HOST_NEW) { 661 ip_file = system_hostfile; 662 ip_status = check_host_in_hostfile(ip_file, ip, 663 host_key, ip_key, &ip_line); 664 } 665 if (host_status == HOST_CHANGED && 666 (ip_status != HOST_CHANGED || !key_equal(ip_key, file_key))) 667 host_ip_differ = 1; 668 669 key_free(ip_key); 670 } else 671 ip_status = host_status; 672 673 key_free(file_key); 674 675 switch (host_status) { 676 case HOST_OK: 677 /* The host is known and the key matches. */ 678 debug("Host '%.200s' is known and matches the %s host key.", 679 host, type); 680 debug("Found key in %s:%d", host_file, host_line); 681 if (options.check_host_ip && ip_status == HOST_NEW) { 682 if (readonly) 683 logit("%s host key for IP address " 684 "'%.128s' not in list of known hosts.", 685 type, ip); 686 else if (!add_host_to_hostfile(user_hostfile, ip, 687 host_key)) 688 logit("Failed to add the %s host key for IP " 689 "address '%.128s' to the list of known " 690 "hosts (%.30s).", type, ip, user_hostfile); 691 else 692 logit("Warning: Permanently added the %s host " 693 "key for IP address '%.128s' to the list " 694 "of known hosts.", type, ip); 695 } 696 break; 697 case HOST_NEW: 698 if (readonly) 699 goto fail; 700 /* The host is new. */ 701 if (options.strict_host_key_checking == 1) { 702 /* 703 * User has requested strict host key checking. We 704 * will not add the host key automatically. The only 705 * alternative left is to abort. 706 */ 707 error("No %s host key is known for %.200s and you " 708 "have requested strict checking.", type, host); 709 goto fail; 710 } else if (options.strict_host_key_checking == 2) { 711 char msg1[1024], msg2[1024]; 712 713 if (show_other_keys(host, host_key)) 714 snprintf(msg1, sizeof(msg1), 715 "\nbut keys of different type are already" 716 " known for this host."); 717 else 718 snprintf(msg1, sizeof(msg1), "."); 719 /* The default */ 720 fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX); 721 msg2[0] = '\0'; 722 if (options.verify_host_key_dns) { 723 if (matching_host_key_dns) 724 snprintf(msg2, sizeof(msg2), 725 "Matching host key fingerprint" 726 " found in DNS.\n"); 727 else 728 snprintf(msg2, sizeof(msg2), 729 "No matching host key fingerprint" 730 " found in DNS.\n"); 731 } 732 snprintf(msg, sizeof(msg), 733 "The authenticity of host '%.200s (%s)' can't be " 734 "established%s\n" 735 "%s key fingerprint is %s.\n%s" 736 "Are you sure you want to continue connecting " 737 "(yes/no)? ", 738 host, ip, msg1, type, fp, msg2); 739 xfree(fp); 740 if (!confirm(msg)) 741 goto fail; 742 } 743 if (options.check_host_ip && ip_status == HOST_NEW) { 744 snprintf(hostline, sizeof(hostline), "%s,%s", host, ip); 745 hostp = hostline; 746 } else 747 hostp = host; 748 749 /* 750 * If not in strict mode, add the key automatically to the 751 * local known_hosts file. 752 */ 753 if (!add_host_to_hostfile(user_hostfile, hostp, host_key)) 754 logit("Failed to add the host to the list of known " 755 "hosts (%.500s).", user_hostfile); 756 else 757 logit("Warning: Permanently added '%.200s' (%s) to the " 758 "list of known hosts.", hostp, type); 759 break; 760 case HOST_CHANGED: 761 if (options.check_host_ip && host_ip_differ) { 762 char *key_msg; 763 if (ip_status == HOST_NEW) 764 key_msg = "is unknown"; 765 else if (ip_status == HOST_OK) 766 key_msg = "is unchanged"; 767 else 768 key_msg = "has a different value"; 769 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); 770 error("@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @"); 771 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); 772 error("The %s host key for %s has changed,", type, host); 773 error("and the key for the according IP address %s", ip); 774 error("%s. This could either mean that", key_msg); 775 error("DNS SPOOFING is happening or the IP address for the host"); 776 error("and its host key have changed at the same time."); 777 if (ip_status != HOST_NEW) 778 error("Offending key for IP in %s:%d", ip_file, ip_line); 779 } 780 /* The host key has changed. */ 781 warn_changed_key(host_key); 782 error("Add correct host key in %.100s to get rid of this message.", 783 user_hostfile); 784 error("Offending key in %s:%d", host_file, host_line); 785 786 /* 787 * If strict host key checking is in use, the user will have 788 * to edit the key manually and we can only abort. 789 */ 790 if (options.strict_host_key_checking) { 791 error("%s host key for %.200s has changed and you have " 792 "requested strict checking.", type, host); 793 goto fail; 794 } 795 796 /* 797 * If strict host key checking has not been requested, allow 798 * the connection but without MITM-able authentication or 799 * agent forwarding. 800 */ 801 if (options.password_authentication) { 802 error("Password authentication is disabled to avoid " 803 "man-in-the-middle attacks."); 804 options.password_authentication = 0; 805 } 806 if (options.kbd_interactive_authentication) { 807 error("Keyboard-interactive authentication is disabled" 808 " to avoid man-in-the-middle attacks."); 809 options.kbd_interactive_authentication = 0; 810 options.challenge_response_authentication = 0; 811 } 812 if (options.challenge_response_authentication) { 813 error("Challenge/response authentication is disabled" 814 " to avoid man-in-the-middle attacks."); 815 options.challenge_response_authentication = 0; 816 } 817 if (options.forward_agent) { 818 error("Agent forwarding is disabled to avoid " 819 "man-in-the-middle attacks."); 820 options.forward_agent = 0; 821 } 822 if (options.forward_x11) { 823 error("X11 forwarding is disabled to avoid " 824 "man-in-the-middle attacks."); 825 options.forward_x11 = 0; 826 } 827 if (options.num_local_forwards > 0 || 828 options.num_remote_forwards > 0) { 829 error("Port forwarding is disabled to avoid " 830 "man-in-the-middle attacks."); 831 options.num_local_forwards = 832 options.num_remote_forwards = 0; 833 } 834 /* 835 * XXX Should permit the user to change to use the new id. 836 * This could be done by converting the host key to an 837 * identifying sentence, tell that the host identifies itself 838 * by that sentence, and ask the user if he/she whishes to 839 * accept the authentication. 840 */ 841 break; 842 case HOST_FOUND: 843 fatal("internal error"); 844 break; 845 } 846 847 if (options.check_host_ip && host_status != HOST_CHANGED && 848 ip_status == HOST_CHANGED) { 849 snprintf(msg, sizeof(msg), 850 "Warning: the %s host key for '%.200s' " 851 "differs from the key for the IP address '%.128s'" 852 "\nOffending key for IP in %s:%d", 853 type, host, ip, ip_file, ip_line); 854 if (host_status == HOST_OK) { 855 len = strlen(msg); 856 snprintf(msg + len, sizeof(msg) - len, 857 "\nMatching host key in %s:%d", 858 host_file, host_line); 859 } 860 if (options.strict_host_key_checking == 1) { 861 logit("%s", msg); 862 error("Exiting, you have requested strict checking."); 863 goto fail; 864 } else if (options.strict_host_key_checking == 2) { 865 strlcat(msg, "\nAre you sure you want " 866 "to continue connecting (yes/no)? ", sizeof(msg)); 867 if (!confirm(msg)) 868 goto fail; 869 } else { 870 logit("%s", msg); 871 } 872 } 873 874 xfree(ip); 875 return 0; 876 877 fail: 878 xfree(ip); 879 return -1; 880 } 881 882 /* returns 0 if key verifies or -1 if key does NOT verify */ 883 int 884 verify_host_key(char *host, struct sockaddr *hostaddr, Key *host_key) 885 { 886 struct stat st; 887 int flags = 0; 888 889 if (options.verify_host_key_dns && 890 verify_host_key_dns(host, hostaddr, host_key, &flags) == 0) { 891 892 if (flags & DNS_VERIFY_FOUND) { 893 894 if (options.verify_host_key_dns == 1 && 895 flags & DNS_VERIFY_MATCH && 896 flags & DNS_VERIFY_SECURE) 897 return 0; 898 899 if (flags & DNS_VERIFY_MATCH) { 900 matching_host_key_dns = 1; 901 } else { 902 warn_changed_key(host_key); 903 error("Update the SSHFP RR in DNS with the new " 904 "host key to get rid of this message."); 905 } 906 } 907 } 908 909 /* return ok if the key can be found in an old keyfile */ 910 if (stat(options.system_hostfile2, &st) == 0 || 911 stat(options.user_hostfile2, &st) == 0) { 912 if (check_host_key(host, hostaddr, host_key, /*readonly*/ 1, 913 options.user_hostfile2, options.system_hostfile2) == 0) 914 return 0; 915 } 916 return check_host_key(host, hostaddr, host_key, /*readonly*/ 0, 917 options.user_hostfile, options.system_hostfile); 918 } 919 920 /* 921 * Starts a dialog with the server, and authenticates the current user on the 922 * server. This does not need any extra privileges. The basic connection 923 * to the server must already have been established before this is called. 924 * If login fails, this function prints an error and never returns. 925 * This function does not require super-user privileges. 926 */ 927 void 928 ssh_login(Sensitive *sensitive, const char *orighost, 929 struct sockaddr *hostaddr, struct passwd *pw) 930 { 931 char *host, *cp; 932 char *server_user, *local_user; 933 934 local_user = xstrdup(pw->pw_name); 935 server_user = options.user ? options.user : local_user; 936 937 /* Convert the user-supplied hostname into all lowercase. */ 938 host = xstrdup(orighost); 939 for (cp = host; *cp; cp++) 940 if (isupper(*cp)) 941 *cp = tolower(*cp); 942 943 /* Exchange protocol version identification strings with the server. */ 944 ssh_exchange_identification(); 945 946 /* Put the connection into non-blocking mode. */ 947 packet_set_nonblocking(); 948 949 /* key exchange */ 950 /* authenticate user */ 951 if (compat20) { 952 ssh_kex2(host, hostaddr); 953 ssh_userauth2(local_user, server_user, host, sensitive); 954 } else { 955 ssh_kex(host, hostaddr); 956 ssh_userauth1(local_user, server_user, host, sensitive); 957 } 958 } 959 960 void 961 ssh_put_password(char *password) 962 { 963 int size; 964 char *padded; 965 966 if (datafellows & SSH_BUG_PASSWORDPAD) { 967 packet_put_cstring(password); 968 return; 969 } 970 size = roundup(strlen(password) + 1, 32); 971 padded = xmalloc(size); 972 memset(padded, 0, size); 973 strlcpy(padded, password, size); 974 packet_put_string(padded, size); 975 memset(padded, 0, size); 976 xfree(padded); 977 } 978 979 static int 980 show_key_from_file(const char *file, const char *host, int keytype) 981 { 982 Key *found; 983 char *fp; 984 int line, ret; 985 986 found = key_new(keytype); 987 if ((ret = lookup_key_in_hostfile_by_type(file, host, 988 keytype, found, &line))) { 989 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX); 990 logit("WARNING: %s key found for host %s\n" 991 "in %s:%d\n" 992 "%s key fingerprint %s.", 993 key_type(found), host, file, line, 994 key_type(found), fp); 995 xfree(fp); 996 } 997 key_free(found); 998 return (ret); 999 } 1000 1001 /* print all known host keys for a given host, but skip keys of given type */ 1002 static int 1003 show_other_keys(const char *host, Key *key) 1004 { 1005 int type[] = { KEY_RSA1, KEY_RSA, KEY_DSA, -1}; 1006 int i, found = 0; 1007 1008 for (i = 0; type[i] != -1; i++) { 1009 if (type[i] == key->type) 1010 continue; 1011 if (type[i] != KEY_RSA1 && 1012 show_key_from_file(options.user_hostfile2, host, type[i])) { 1013 found = 1; 1014 continue; 1015 } 1016 if (type[i] != KEY_RSA1 && 1017 show_key_from_file(options.system_hostfile2, host, type[i])) { 1018 found = 1; 1019 continue; 1020 } 1021 if (show_key_from_file(options.user_hostfile, host, type[i])) { 1022 found = 1; 1023 continue; 1024 } 1025 if (show_key_from_file(options.system_hostfile, host, type[i])) { 1026 found = 1; 1027 continue; 1028 } 1029 debug2("no key of type %d for host %s", type[i], host); 1030 } 1031 return (found); 1032 } 1033 1034 static void 1035 warn_changed_key(Key *host_key) 1036 { 1037 char *fp; 1038 const char *type = key_type(host_key); 1039 1040 fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX); 1041 1042 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); 1043 error("@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @"); 1044 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); 1045 error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!"); 1046 error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!"); 1047 error("It is also possible that the %s host key has just been changed.", type); 1048 error("The fingerprint for the %s key sent by the remote host is\n%s.", 1049 type, fp); 1050 error("Please contact your system administrator."); 1051 1052 xfree(fp); 1053 } 1054