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