sshconnect.c revision 73400
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" 16RCSID("$OpenBSD: sshconnect.c,v 1.79 2000/09/17 15:52:51 markus Exp $"); 17RCSID("$FreeBSD: head/crypto/openssh/sshconnect.c 73400 2001-03-04 02:22:04Z assar $"); 18 19#include <openssl/bn.h> 20#include <openssl/dsa.h> 21#include <openssl/rsa.h> 22 23#include "xmalloc.h" 24#include "rsa.h" 25#include "ssh.h" 26#include "buffer.h" 27#include "packet.h" 28#include "uidswap.h" 29#include "compat.h" 30#include "readconf.h" 31#include "key.h" 32#include "sshconnect.h" 33#include "hostfile.h" 34 35char *client_version_string = NULL; 36char *server_version_string = NULL; 37 38extern Options options; 39extern char *__progname; 40 41/* 42 * Connect to the given ssh server using a proxy command. 43 */ 44int 45ssh_proxy_connect(const char *host, u_short port, uid_t original_real_uid, 46 const char *proxy_command) 47{ 48 Buffer command; 49 const char *cp; 50 char *command_string; 51 int pin[2], pout[2]; 52 pid_t pid; 53 char strport[NI_MAXSERV]; 54 55 /* Convert the port number into a string. */ 56 snprintf(strport, sizeof strport, "%hu", port); 57 58 /* Build the final command string in the buffer by making the 59 appropriate substitutions to the given proxy command. */ 60 buffer_init(&command); 61 for (cp = proxy_command; *cp; cp++) { 62 if (cp[0] == '%' && cp[1] == '%') { 63 buffer_append(&command, "%", 1); 64 cp++; 65 continue; 66 } 67 if (cp[0] == '%' && cp[1] == 'h') { 68 buffer_append(&command, host, strlen(host)); 69 cp++; 70 continue; 71 } 72 if (cp[0] == '%' && cp[1] == 'p') { 73 buffer_append(&command, strport, strlen(strport)); 74 cp++; 75 continue; 76 } 77 buffer_append(&command, cp, 1); 78 } 79 buffer_append(&command, "\0", 1); 80 81 /* Get the final command string. */ 82 command_string = buffer_ptr(&command); 83 84 /* Create pipes for communicating with the proxy. */ 85 if (pipe(pin) < 0 || pipe(pout) < 0) 86 fatal("Could not create pipes to communicate with the proxy: %.100s", 87 strerror(errno)); 88 89 debug("Executing proxy command: %.500s", command_string); 90 91 /* Fork and execute the proxy command. */ 92 if ((pid = fork()) == 0) { 93 char *argv[10]; 94 95 /* Child. Permanently give up superuser privileges. */ 96 permanently_set_uid(original_real_uid); 97 98 /* Redirect stdin and stdout. */ 99 close(pin[1]); 100 if (pin[0] != 0) { 101 if (dup2(pin[0], 0) < 0) 102 perror("dup2 stdin"); 103 close(pin[0]); 104 } 105 close(pout[0]); 106 if (dup2(pout[1], 1) < 0) 107 perror("dup2 stdout"); 108 /* Cannot be 1 because pin allocated two descriptors. */ 109 close(pout[1]); 110 111 /* Stderr is left as it is so that error messages get 112 printed on the user's terminal. */ 113 argv[0] = "/bin/sh"; 114 argv[1] = "-c"; 115 argv[2] = command_string; 116 argv[3] = NULL; 117 118 /* Execute the proxy command. Note that we gave up any 119 extra privileges above. */ 120 execv("/bin/sh", argv); 121 perror("/bin/sh"); 122 exit(1); 123 } 124 /* Parent. */ 125 if (pid < 0) 126 fatal("fork failed: %.100s", strerror(errno)); 127 128 /* Close child side of the descriptors. */ 129 close(pin[0]); 130 close(pout[1]); 131 132 /* Free the command name. */ 133 buffer_free(&command); 134 135 /* Set the connection file descriptors. */ 136 packet_set_connection(pout[0], pin[1]); 137 138 return 1; 139} 140 141/* 142 * Creates a (possibly privileged) socket for use as the ssh connection. 143 */ 144int 145ssh_create_socket(uid_t original_real_uid, int privileged, int family) 146{ 147 int sock; 148 149 /* 150 * If we are running as root and want to connect to a privileged 151 * port, bind our own socket to a privileged port. 152 */ 153 if (privileged) { 154 int p = IPPORT_RESERVED - 1; 155 sock = rresvport_af(&p, family); 156 if (sock < 0) 157 error("rresvport: af=%d %.100s", family, strerror(errno)); 158 else 159 debug("Allocated local port %d.", p); 160 } else { 161 /* 162 * Just create an ordinary socket on arbitrary port. We use 163 * the user's uid to create the socket. 164 */ 165 temporarily_use_uid(original_real_uid); 166 sock = socket(family, SOCK_STREAM, 0); 167 if (sock < 0) 168 error("socket: %.100s", strerror(errno)); 169 restore_uid(); 170 } 171 return sock; 172} 173 174/* 175 * Opens a TCP/IP connection to the remote server on the given host. 176 * The canonical host name used to connect will be returned in *host. 177 * The address of the remote host will be returned in hostaddr. 178 * If port is 0, the default port will be used. If anonymous is zero, 179 * a privileged port will be allocated to make the connection. 180 * This requires super-user privileges if anonymous is false. 181 * Connection_attempts specifies the maximum number of tries (one per 182 * second). If proxy_command is non-NULL, it specifies the command (with %h 183 * and %p substituted for host and port, respectively) to use to contact 184 * the daemon. 185 */ 186int 187ssh_connect(char **host, struct sockaddr_storage * hostaddr, 188 u_short port, int connection_attempts, 189 int anonymous, uid_t original_real_uid, 190 const char *proxy_command) 191{ 192 int sock = -1, attempt; 193 struct servent *sp; 194 struct addrinfo hints, *ai, *aitop; 195 char ntop[NI_MAXHOST], strport[NI_MAXSERV]; 196 int gaierr; 197 struct linger linger; 198 199 debug("ssh_connect: getuid %u geteuid %u anon %d", 200 (u_int) getuid(), (u_int) geteuid(), anonymous); 201 202 /* Get default port if port has not been set. */ 203 if (port == 0) { 204 sp = getservbyname(SSH_SERVICE_NAME, "tcp"); 205 if (sp) 206 port = ntohs(sp->s_port); 207 else 208 port = SSH_DEFAULT_PORT; 209 } 210 /* If a proxy command is given, connect using it. */ 211 if (proxy_command != NULL) 212 return ssh_proxy_connect(*host, port, original_real_uid, proxy_command); 213 214 /* No proxy command. */ 215 216 memset(&hints, 0, sizeof(hints)); 217 hints.ai_family = IPv4or6; 218 hints.ai_socktype = SOCK_STREAM; 219 hints.ai_flags = AI_CANONNAME; 220 snprintf(strport, sizeof strport, "%d", port); 221 if ((gaierr = getaddrinfo(*host, strport, &hints, &aitop)) != 0) 222 fatal("%s: %.100s: %s", __progname, *host, 223 gai_strerror(gaierr)); 224 225 /* 226 * Try to connect several times. On some machines, the first time 227 * will sometimes fail. In general socket code appears to behave 228 * quite magically on many machines. 229 */ 230 for (attempt = 0; attempt < connection_attempts; attempt++) { 231 if (attempt > 0) 232 debug("Trying again..."); 233 234 /* Loop through addresses for this host, and try each one in 235 sequence until the connection succeeds. */ 236 for (ai = aitop; ai; ai = ai->ai_next) { 237 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) 238 continue; 239 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, 240 ntop, sizeof(ntop), strport, sizeof(strport), 241 NI_NUMERICHOST|NI_NUMERICSERV) != 0) { 242 error("ssh_connect: getnameinfo failed"); 243 continue; 244 } 245 debug("Connecting to %.200s [%.100s] port %s.", 246 ai->ai_canonname, ntop, strport); 247 248 /* Create a socket for connecting. */ 249 sock = ssh_create_socket(original_real_uid, 250 !anonymous && geteuid() == 0 && port < IPPORT_RESERVED, 251 ai->ai_family); 252 if (sock < 0) 253 continue; 254 255 /* Connect to the host. We use the user's uid in the 256 * hope that it will help with tcp_wrappers showing 257 * the remote uid as root. 258 */ 259 temporarily_use_uid(original_real_uid); 260 if (connect(sock, ai->ai_addr, ai->ai_addrlen) >= 0) { 261 /* Successful connection. */ 262 memcpy(hostaddr, ai->ai_addr, ai->ai_addrlen); 263 restore_uid(); 264 break; 265 } else { 266 debug("connect: %.100s", strerror(errno)); 267 restore_uid(); 268 /* 269 * Close the failed socket; there appear to 270 * be some problems when reusing a socket for 271 * which connect() has already returned an 272 * error. 273 */ 274 shutdown(sock, SHUT_RDWR); 275 close(sock); 276 } 277 } 278 if (ai) { 279#if 0 280 if (ai->ai_canonname != NULL) 281 *host = xstrdup(ai->ai_canonname); 282#endif 283 break; /* Successful connection. */ 284 } 285 286 /* Sleep a moment before retrying. */ 287 sleep(1); 288 } 289 290 freeaddrinfo(aitop); 291 292 /* Return failure if we didn't get a successful connection. */ 293 if (attempt >= connection_attempts) 294 return 0; 295 296 debug("Connection established."); 297 298 /* 299 * Set socket options. We would like the socket to disappear as soon 300 * as it has been closed for whatever reason. 301 */ 302 /* setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)); */ 303 linger.l_onoff = 1; 304 linger.l_linger = 5; 305 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *) &linger, sizeof(linger)); 306 307 /* Set the connection. */ 308 packet_set_connection(sock, sock); 309 310 return 1; 311} 312 313/* 314 * Waits for the server identification string, and sends our own 315 * identification string. 316 */ 317void 318ssh_exchange_identification() 319{ 320 char buf[256], remote_version[256]; /* must be same size! */ 321 int remote_major, remote_minor, i, mismatch; 322 int connection_in = packet_get_connection_in(); 323 int connection_out = packet_get_connection_out(); 324 325 /* Read other side\'s version identification. */ 326 for (;;) { 327 for (i = 0; i < sizeof(buf) - 1; i++) { 328 int len = atomicio(read, connection_in, &buf[i], 1); 329 if (len < 0) 330 fatal("ssh_exchange_identification: read: %.100s", strerror(errno)); 331 if (len != 1) 332 fatal("ssh_exchange_identification: Connection closed by remote host"); 333 if (buf[i] == '\r') { 334 buf[i] = '\n'; 335 buf[i + 1] = 0; 336 continue; /**XXX wait for \n */ 337 } 338 if (buf[i] == '\n') { 339 buf[i + 1] = 0; 340 break; 341 } 342 } 343 buf[sizeof(buf) - 1] = 0; 344 if (strncmp(buf, "SSH-", 4) == 0) 345 break; 346 debug("ssh_exchange_identification: %s", buf); 347 } 348 server_version_string = xstrdup(buf); 349 350 /* 351 * Check that the versions match. In future this might accept 352 * several versions and set appropriate flags to handle them. 353 */ 354 if (sscanf(server_version_string, "SSH-%d.%d-%[^\n]\n", 355 &remote_major, &remote_minor, remote_version) != 3) 356 fatal("Bad remote protocol version identification: '%.100s'", buf); 357 debug("Remote protocol version %d.%d, remote software version %.100s", 358 remote_major, remote_minor, remote_version); 359 360 compat_datafellows(remote_version); 361 mismatch = 0; 362 363 switch(remote_major) { 364 case 1: 365 if (remote_minor == 99 && 366 (options.protocol & SSH_PROTO_2) && 367 !(options.protocol & SSH_PROTO_1_PREFERRED)) { 368 enable_compat20(); 369 break; 370 } 371 if (!(options.protocol & SSH_PROTO_1)) { 372 mismatch = 1; 373 break; 374 } 375 if (remote_minor < 3) { 376 fatal("Remote machine has too old SSH software version."); 377 } else if (remote_minor == 3) { 378 /* We speak 1.3, too. */ 379 enable_compat13(); 380 if (options.forward_agent) { 381 log("Agent forwarding disabled for protocol 1.3"); 382 options.forward_agent = 0; 383 } 384 } 385 break; 386 case 2: 387 if (options.protocol & SSH_PROTO_2) { 388 enable_compat20(); 389 break; 390 } 391 /* FALLTHROUGH */ 392 default: 393 mismatch = 1; 394 break; 395 } 396 if (mismatch) 397 fatal("Protocol major versions differ: %d vs. %d", 398 (options.protocol & SSH_PROTO_2) ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1, 399 remote_major); 400 if (compat20) 401 packet_set_ssh2_format(); 402 /* Send our own protocol version identification. */ 403 snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n", 404 compat20 ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1, 405 compat20 ? PROTOCOL_MINOR_2 : PROTOCOL_MINOR_1, 406 SSH_VERSION); 407 if (atomicio(write, connection_out, buf, strlen(buf)) != strlen(buf)) 408 fatal("write: %.100s", strerror(errno)); 409 client_version_string = xstrdup(buf); 410 chop(client_version_string); 411 chop(server_version_string); 412 debug("Local version string %.100s", client_version_string); 413} 414 415int 416read_yes_or_no(const char *prompt, int defval) 417{ 418 char buf[1024]; 419 FILE *f; 420 int retval = -1; 421 422 if (isatty(0)) 423 f = stdin; 424 else 425 f = fopen("/dev/tty", "rw"); 426 427 if (f == NULL) 428 return 0; 429 430 fflush(stdout); 431 432 while (1) { 433 fprintf(stderr, "%s", prompt); 434 if (fgets(buf, sizeof(buf), f) == NULL) { 435 /* Print a newline (the prompt probably didn\'t have one). */ 436 fprintf(stderr, "\n"); 437 strlcpy(buf, "no", sizeof buf); 438 } 439 /* Remove newline from response. */ 440 if (strchr(buf, '\n')) 441 *strchr(buf, '\n') = 0; 442 443 if (buf[0] == 0) 444 retval = defval; 445 if (strcmp(buf, "yes") == 0) 446 retval = 1; 447 else if (strcmp(buf, "no") == 0) 448 retval = 0; 449 else 450 fprintf(stderr, "Please type 'yes' or 'no'.\n"); 451 452 if (retval != -1) { 453 if (f != stdin) 454 fclose(f); 455 return retval; 456 } 457 } 458} 459 460/* 461 * check whether the supplied host key is valid, return only if ok. 462 */ 463 464void 465check_host_key(char *host, struct sockaddr *hostaddr, Key *host_key, 466 const char *user_hostfile, const char *system_hostfile) 467{ 468 Key *file_key; 469 char *type = key_type(host_key); 470 char *ip = NULL; 471 char hostline[1000], *hostp; 472 HostStatus host_status; 473 HostStatus ip_status; 474 int local = 0, host_ip_differ = 0; 475 char ntop[NI_MAXHOST]; 476 477 /* 478 * Force accepting of the host key for loopback/localhost. The 479 * problem is that if the home directory is NFS-mounted to multiple 480 * machines, localhost will refer to a different machine in each of 481 * them, and the user will get bogus HOST_CHANGED warnings. This 482 * essentially disables host authentication for localhost; however, 483 * this is probably not a real problem. 484 */ 485 /** hostaddr == 0! */ 486 switch (hostaddr->sa_family) { 487 case AF_INET: 488 local = (ntohl(((struct sockaddr_in *)hostaddr)->sin_addr.s_addr) >> 24) == IN_LOOPBACKNET; 489 break; 490 case AF_INET6: 491 local = IN6_IS_ADDR_LOOPBACK(&(((struct sockaddr_in6 *)hostaddr)->sin6_addr)); 492 break; 493 default: 494 local = 0; 495 break; 496 } 497 if (local) { 498 debug("Forcing accepting of host key for loopback/localhost."); 499 return; 500 } 501 502 /* 503 * Turn off check_host_ip for proxy connects, since 504 * we don't have the remote ip-address 505 */ 506 if (options.proxy_command != NULL && options.check_host_ip) 507 options.check_host_ip = 0; 508 509 if (options.check_host_ip) { 510 if (getnameinfo(hostaddr, hostaddr->sa_len, ntop, sizeof(ntop), 511 NULL, 0, NI_NUMERICHOST) != 0) 512 fatal("check_host_key: getnameinfo failed"); 513 ip = xstrdup(ntop); 514 } 515 516 /* 517 * Store the host key from the known host file in here so that we can 518 * compare it with the key for the IP address. 519 */ 520 file_key = key_new(host_key->type); 521 522 /* 523 * Check if the host key is present in the user\'s list of known 524 * hosts or in the systemwide list. 525 */ 526 host_status = check_host_in_hostfile(user_hostfile, host, host_key, file_key); 527 if (host_status == HOST_NEW) 528 host_status = check_host_in_hostfile(system_hostfile, host, host_key, file_key); 529 /* 530 * Also perform check for the ip address, skip the check if we are 531 * localhost or the hostname was an ip address to begin with 532 */ 533 if (options.check_host_ip && !local && strcmp(host, ip)) { 534 Key *ip_key = key_new(host_key->type); 535 ip_status = check_host_in_hostfile(user_hostfile, ip, host_key, ip_key); 536 537 if (ip_status == HOST_NEW) 538 ip_status = check_host_in_hostfile(system_hostfile, ip, host_key, ip_key); 539 if (host_status == HOST_CHANGED && 540 (ip_status != HOST_CHANGED || !key_equal(ip_key, file_key))) 541 host_ip_differ = 1; 542 543 key_free(ip_key); 544 } else 545 ip_status = host_status; 546 547 key_free(file_key); 548 549 switch (host_status) { 550 case HOST_OK: 551 /* The host is known and the key matches. */ 552 debug("Host '%.200s' is known and matches the %s host key.", 553 host, type); 554 if (options.check_host_ip) { 555 if (ip_status == HOST_NEW) { 556 if (!add_host_to_hostfile(user_hostfile, ip, host_key)) 557 log("Failed to add the %s host key for IP address '%.30s' to the list of known hosts (%.30s).", 558 type, ip, user_hostfile); 559 else 560 log("Warning: Permanently added the %s host key for IP address '%.30s' to the list of known hosts.", 561 type, ip); 562 } else if (ip_status != HOST_OK) 563 log("Warning: the %s host key for '%.200s' differs from the key for the IP address '%.30s'", 564 type, host, ip); 565 } 566 break; 567 case HOST_NEW: 568 /* The host is new. */ 569 if (options.strict_host_key_checking == 1) { 570 /* User has requested strict host key checking. We will not add the host key 571 automatically. The only alternative left is to abort. */ 572 fatal("No %s host key is known for %.200s and you have requested strict checking.", type, host); 573 } else if (options.strict_host_key_checking == 2) { 574 /* The default */ 575 char prompt[1024]; 576 char *fp = key_fingerprint(host_key); 577 snprintf(prompt, sizeof(prompt), 578 "The authenticity of host '%.200s' can't be established.\n" 579 "%s key fingerprint is %s.\n" 580 "Are you sure you want to continue connecting (yes/no)? ", 581 host, type, fp); 582 if (!read_yes_or_no(prompt, -1)) 583 fatal("Aborted by user!\n"); 584 } 585 if (options.check_host_ip && ip_status == HOST_NEW && strcmp(host, ip)) { 586 snprintf(hostline, sizeof(hostline), "%s,%s", host, ip); 587 hostp = hostline; 588 } else 589 hostp = host; 590 591 /* If not in strict mode, add the key automatically to the local known_hosts file. */ 592 if (!add_host_to_hostfile(user_hostfile, hostp, host_key)) 593 log("Failed to add the host to the list of known hosts (%.500s).", 594 user_hostfile); 595 else 596 log("Warning: Permanently added '%.200s' (%s) to the list of known hosts.", 597 hostp, type); 598 break; 599 case HOST_CHANGED: 600 if (options.check_host_ip && host_ip_differ) { 601 char *msg; 602 if (ip_status == HOST_NEW) 603 msg = "is unknown"; 604 else if (ip_status == HOST_OK) 605 msg = "is unchanged"; 606 else 607 msg = "has a different value"; 608 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); 609 error("@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @"); 610 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); 611 error("The %s host key for %s has changed,", type, host); 612 error("and the key for the according IP address %s", ip); 613 error("%s. This could either mean that", msg); 614 error("DNS SPOOFING is happening or the IP address for the host"); 615 error("and its host key have changed at the same time"); 616 } 617 /* The host key has changed. */ 618 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); 619 error("@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @"); 620 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); 621 error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!"); 622 error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!"); 623 error("It is also possible that the %s host key has just been changed.", type); 624 error("Please contact your system administrator."); 625 error("Add correct host key in %.100s to get rid of this message.", 626 user_hostfile); 627 628 /* 629 * If strict host key checking is in use, the user will have 630 * to edit the key manually and we can only abort. 631 */ 632 if (options.strict_host_key_checking) 633 fatal("%s host key for %.200s has changed and you have requested strict checking.", type, host); 634 635 /* 636 * If strict host key checking has not been requested, allow 637 * the connection but without password authentication or 638 * agent forwarding. 639 */ 640 if (options.password_authentication) { 641 error("Password authentication is disabled to avoid trojan horses."); 642 options.password_authentication = 0; 643 } 644 if (options.forward_agent) { 645 error("Agent forwarding is disabled to avoid trojan horses."); 646 options.forward_agent = 0; 647 } 648 /* 649 * XXX Should permit the user to change to use the new id. 650 * This could be done by converting the host key to an 651 * identifying sentence, tell that the host identifies itself 652 * by that sentence, and ask the user if he/she whishes to 653 * accept the authentication. 654 */ 655 break; 656 } 657 if (options.check_host_ip) 658 xfree(ip); 659} 660 661#ifdef KRB5 662int 663try_krb5_authentication(krb5_context *context, krb5_auth_context *auth_context) 664{ 665 krb5_error_code problem; 666 const char *tkfile; 667 struct stat buf; 668 krb5_ccache ccache = NULL; 669 krb5_creds req_creds; 670 krb5_creds *new_creds = NULL; 671 const char *remotehost; 672 krb5_data ap; 673 int type, payload_len; 674 krb5_ap_rep_enc_part *reply = NULL; 675 int ret; 676 677 memset(&ap, 0, sizeof(ap)); 678 679 problem = krb5_init_context(context); 680 if (problem) { 681 ret = 0; 682 goto out; 683 } 684 685 tkfile = krb5_cc_default_name(*context); 686 if (strncmp(tkfile, "FILE:", 5) == 0) 687 tkfile += 5; 688 689 if (stat(tkfile, &buf) == 0 && getuid() != buf.st_uid) { 690 debug("Kerberos V5: could not get default ccache (permission denied)."); 691 ret = 0; 692 goto out; 693 } 694 695 problem = krb5_cc_default(*context, &ccache); 696 if (problem) { 697 ret = 0; 698 goto out; 699 } 700 701 memset(&req_creds, 0, sizeof(req_creds)); 702 703 remotehost = get_canonical_hostname(); 704 705 problem = krb5_sname_to_principal(*context, remotehost, 706 "host", KRB5_NT_SRV_HST, 707 &req_creds.server); 708 if (problem) { 709 ret = 0; 710 goto out; 711 712 } 713 714 problem = krb5_cc_get_principal(*context, ccache, &req_creds.client); 715 if (problem) { 716 ret = 0; 717 goto out; 718 } 719 720 /* creds.session.keytype=ETYPE_DES_CBC_CRC; */ 721 722 problem = krb5_get_credentials(*context, 0, ccache, &req_creds, &new_creds); 723 if (problem) { 724 ret = 0; 725 goto out; 726 } 727 728 problem = krb5_auth_con_init(*context, auth_context); 729 if (problem) { 730 ret = 0; 731 goto out; 732 } 733 734 /* krb5_auth_con_setflags(ssh_context, auth_context, 735 KRB5_AUTH_CONTEXT_RET_TIME); 736 */ 737 problem = krb5_mk_req_extended(*context, auth_context, 738 AP_OPTS_MUTUAL_REQUIRED /*| AP_OPTS_USE_SUBKEY*/ , 739 NULL, new_creds, &ap); 740 if (problem) { 741 ret = 0; 742 goto out; 743 } 744 745 packet_start(SSH_CMSG_AUTH_KERBEROS); 746 packet_put_string((char *) ap.data, ap.length); 747 packet_send(); 748 packet_write_wait(); 749 750 xfree(ap.data); 751 ap.length = 0; 752 753 type = packet_read(&payload_len); 754 switch (type) { 755 case SSH_SMSG_FAILURE: 756 /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */ 757 debug("Kerberos V5 authentication failed."); 758 ret = 0; 759 break; 760 761 case SSH_SMSG_AUTH_KERBEROS_RESPONSE: 762 /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */ 763 debug("Kerberos V5 authentication accepted."); 764 765 /* Get server's response. */ 766 ap.data = packet_get_string((unsigned int *) &ap.length); 767 768 packet_integrity_check(payload_len, 4 + ap.length, type); 769 /* XXX je to dobre? */ 770 771 problem = krb5_rd_rep(*context, *auth_context, &ap, &reply); 772 if (problem) { 773 ret = 0; 774 } 775 ret = 1; 776 break; 777 778 default: 779 packet_disconnect("Protocol error on Kerberos V5 response: %d", type); 780 ret = 0; 781 break; 782 783 } 784 785out: 786 if (req_creds.server != NULL) 787 krb5_free_principal(*context, req_creds.server); 788 if (req_creds.client != NULL) 789 krb5_free_principal(*context, req_creds.client); 790 if (new_creds != NULL) 791 krb5_free_creds(*context, new_creds); 792 if (ccache != NULL) 793 krb5_cc_close(*context, ccache); 794 if (reply != NULL) 795 krb5_free_ap_rep_enc_part(*context, reply); 796 if (ap.length > 0) 797 krb5_data_free(&ap); 798 799 return ret; 800 801} 802 803void 804send_krb5_tgt(krb5_context context, krb5_auth_context auth_context) 805{ 806 int fd; 807 int type, payload_len; 808 krb5_error_code problem; 809 krb5_data outbuf; 810 krb5_ccache ccache = NULL; 811 krb5_creds creds; 812 krb5_kdc_flags flags; 813 const char* remotehost = get_canonical_hostname(); 814 815 memset(&creds, 0, sizeof(creds)); 816 memset(&outbuf, 0, sizeof(outbuf)); 817 818 fd = packet_get_connection_in(); 819 problem = krb5_auth_con_setaddrs_from_fd(context, auth_context, &fd); 820 if (problem) { 821 goto out; 822 } 823 824#if 0 825 tkfile = krb5_cc_default_name(context); 826 if (strncmp(tkfile, "FILE:", 5) == 0) 827 tkfile += 5; 828 829 if (stat(tkfile, &buf) == 0 && getuid() != buf.st_uid) { 830 debug("Kerberos V5: could not get default ccache (permission denied)."); 831 goto out; 832 } 833#endif 834 835 problem = krb5_cc_default(context, &ccache); 836 if (problem) { 837 goto out; 838 } 839 840 problem = krb5_cc_get_principal(context, ccache, &creds.client); 841 if (problem) { 842 goto out; 843 } 844 845 problem = krb5_build_principal(context, &creds.server, 846 strlen(creds.client->realm), 847 creds.client->realm, 848 "krbtgt", 849 creds.client->realm, 850 NULL); 851 if (problem) { 852 goto out; 853 } 854 855 creds.times.endtime = 0; 856 857 flags.i = 0; 858 flags.b.forwarded = 1; 859 flags.b.forwardable = krb5_config_get_bool(context, NULL, 860 "libdefaults", "forwardable", NULL); 861 862 problem = krb5_get_forwarded_creds (context, 863 auth_context, 864 ccache, 865 flags.i, 866 remotehost, 867 &creds, 868 &outbuf); 869 if (problem) { 870 goto out; 871 } 872 873 packet_start(SSH_CMSG_HAVE_KERBEROS_TGT); 874 packet_put_string((char *)outbuf.data, outbuf.length); 875 packet_send(); 876 packet_write_wait(); 877 878 type = packet_read(&payload_len); 879 switch (type) { 880 case SSH_SMSG_SUCCESS: 881 break; 882 case SSH_SMSG_FAILURE: 883 break; 884 default: 885 break; 886 } 887 888out: 889 if (creds.client) 890 krb5_free_principal(context, creds.client); 891 if (creds.server) 892 krb5_free_principal(context, creds.server); 893 if (ccache) 894 krb5_cc_close(context, ccache); 895 if (outbuf.data) 896 xfree(outbuf.data); 897 898 return; 899} 900#endif /* KRB5 */ 901 902/* 903 * Starts a dialog with the server, and authenticates the current user on the 904 * server. This does not need any extra privileges. The basic connection 905 * to the server must already have been established before this is called. 906 * If login fails, this function prints an error and never returns. 907 * This function does not require super-user privileges. 908 */ 909void 910ssh_login(int host_key_valid, RSA *own_host_key, const char *orighost, 911 struct sockaddr *hostaddr, uid_t original_real_uid) 912{ 913 struct passwd *pw; 914 char *host, *cp; 915 char *server_user, *local_user; 916 917 /* Get local user name. Use it as server user if no user name was given. */ 918 pw = getpwuid(original_real_uid); 919 if (!pw) 920 fatal("User id %u not found from user database.", original_real_uid); 921 local_user = xstrdup(pw->pw_name); 922 server_user = options.user ? options.user : local_user; 923 924 /* Convert the user-supplied hostname into all lowercase. */ 925 host = xstrdup(orighost); 926 for (cp = host; *cp; cp++) 927 if (isupper(*cp)) 928 *cp = tolower(*cp); 929 930 /* Exchange protocol version identification strings with the server. */ 931 ssh_exchange_identification(); 932 933 /* Put the connection into non-blocking mode. */ 934 packet_set_nonblocking(); 935 936 /* key exchange */ 937 /* authenticate user */ 938 if (compat20) { 939 ssh_kex2(host, hostaddr); 940 ssh_userauth2(server_user, host); 941 } else { 942 ssh_kex(host, hostaddr); 943 ssh_userauth(local_user, server_user, host, host_key_valid, own_host_key); 944 } 945} 946