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 * Created: Sat Mar 18 16:36:11 1995 ylo 6 * Ssh client program. This program can be used to log into a remote machine. 7 * The software supports strong authentication, encryption, and forwarding 8 * of X11, TCP/IP, and authentication connections. 9 * 10 * Modified to work with SSL by Niels Provos <provos@citi.umich.edu> in Canada. 11 * 12 * $FreeBSD$ 13 */ 14 15 #include "includes.h" 16 RCSID("$Id: ssh.c,v 1.43 2000/03/23 21:52:02 markus Exp $"); 17 18 #include "xmalloc.h" 19 #include "ssh.h" 20 #include "packet.h" 21 #include "buffer.h" 22 #include "authfd.h" 23 #include "readconf.h" 24 #include "uidswap.h" 25 26 /* Flag indicating whether IPv4 or IPv6. This can be set on the command line. 27 Default value is AF_UNSPEC means both IPv4 and IPv6. */ 28 int IPv4or6 = AF_UNSPEC; 29 30 /* Flag indicating whether debug mode is on. This can be set on the command line. */ 31 int debug_flag = 0; 32 33 int tty_flag = 0; 34 35 /* 36 * Flag indicating that nothing should be read from stdin. This can be set 37 * on the command line. 38 */ 39 int stdin_null_flag = 0; 40 41 /* 42 * Flag indicating that ssh should fork after authentication. This is useful 43 * so that the pasphrase can be entered manually, and then ssh goes to the 44 * background. 45 */ 46 int fork_after_authentication_flag = 0; 47 48 /* 49 * General data structure for command line options and options configurable 50 * in configuration files. See readconf.h. 51 */ 52 Options options; 53 54 /* 55 * Name of the host we are connecting to. This is the name given on the 56 * command line, or the HostName specified for the user-supplied name in a 57 * configuration file. 58 */ 59 char *host; 60 61 /* socket address the host resolves to */ 62 struct sockaddr_storage hostaddr; 63 64 /* 65 * Flag to indicate that we have received a window change signal which has 66 * not yet been processed. This will cause a message indicating the new 67 * window size to be sent to the server a little later. This is volatile 68 * because this is updated in a signal handler. 69 */ 70 volatile int received_window_change_signal = 0; 71 72 /* Value of argv[0] (set in the main program). */ 73 char *av0; 74 75 /* Flag indicating whether we have a valid host private key loaded. */ 76 int host_private_key_loaded = 0; 77 78 /* Host private key. */ 79 RSA *host_private_key = NULL; 80 81 /* Original real UID. */ 82 uid_t original_real_uid; 83 84 /* Prints a help message to the user. This function never returns. */ 85 86 void 87 usage() 88 { 89 fprintf(stderr, "Usage: %s [options] host [command]\n", av0); 90 fprintf(stderr, "Options:\n"); 91 fprintf(stderr, " -l user Log in using this user name.\n"); 92 fprintf(stderr, " -n Redirect input from /dev/null.\n"); 93 fprintf(stderr, " -a Disable authentication agent forwarding.\n"); 94 #ifdef AFS 95 fprintf(stderr, " -k Disable Kerberos ticket and AFS token forwarding.\n"); 96 #endif /* AFS */ 97 fprintf(stderr, " -x Disable X11 connection forwarding.\n"); 98 fprintf(stderr, " -X Enable X11 connection forwarding.\n"); 99 fprintf(stderr, " -i file Identity for RSA authentication (default: ~/.ssh/identity).\n"); 100 fprintf(stderr, " -t Tty; allocate a tty even if command is given.\n"); 101 fprintf(stderr, " -v Verbose; display verbose debugging messages.\n"); 102 fprintf(stderr, " -V Display version number only.\n"); 103 fprintf(stderr, " -P Don't allocate a privileged port.\n"); 104 fprintf(stderr, " -q Quiet; don't display any warning messages.\n"); 105 fprintf(stderr, " -f Fork into background after authentication.\n"); 106 fprintf(stderr, " -e char Set escape character; ``none'' = disable (default: ~).\n"); 107 108 fprintf(stderr, " -c cipher Select encryption algorithm: " 109 "``3des'', " 110 "``blowfish''\n"); 111 fprintf(stderr, " -p port Connect to this port. Server must be on the same port.\n"); 112 fprintf(stderr, " -L listen-port:host:port Forward local port to remote address\n"); 113 fprintf(stderr, " -R listen-port:host:port Forward remote port to local address\n"); 114 fprintf(stderr, " These cause %s to listen for connections on a port, and\n", av0); 115 fprintf(stderr, " forward them to the other side by connecting to host:port.\n"); 116 fprintf(stderr, " -C Enable compression.\n"); 117 fprintf(stderr, " -g Allow remote hosts to connect to forwarded ports.\n"); 118 fprintf(stderr, " -4 Use IPv4 only.\n"); 119 fprintf(stderr, " -6 Use IPv6 only.\n"); 120 fprintf(stderr, " -o 'option' Process the option as if it was read from a configuration file.\n"); 121 exit(1); 122 } 123 124 /* 125 * Connects to the given host using rsh (or prints an error message and exits 126 * if rsh is not available). This function never returns. 127 */ 128 void 129 rsh_connect(char *host, char *user, Buffer * command) 130 { 131 char *args[10]; 132 int i; 133 134 log("Using rsh. WARNING: Connection will not be encrypted."); 135 /* Build argument list for rsh. */ 136 i = 0; 137 #ifndef _PATH_RSH 138 #define _PATH_RSH "/usr/bin/rsh" 139 #endif 140 args[i++] = _PATH_RSH; 141 /* host may have to come after user on some systems */ 142 args[i++] = host; 143 if (user) { 144 args[i++] = "-l"; 145 args[i++] = user; 146 } 147 if (buffer_len(command) > 0) { 148 buffer_append(command, "\0", 1); 149 args[i++] = buffer_ptr(command); 150 } 151 args[i++] = NULL; 152 if (debug_flag) { 153 for (i = 0; args[i]; i++) { 154 if (i != 0) 155 fprintf(stderr, " "); 156 fprintf(stderr, "%s", args[i]); 157 } 158 fprintf(stderr, "\n"); 159 } 160 execv(_PATH_RSH, args); 161 perror(_PATH_RSH); 162 exit(1); 163 } 164 165 /* 166 * Main program for the ssh client. 167 */ 168 int 169 main(int ac, char **av) 170 { 171 int i, opt, optind, type, exit_status, ok, authfd; 172 u_short fwd_port, fwd_host_port; 173 char *optarg, *cp, buf[256]; 174 Buffer command; 175 struct winsize ws; 176 struct stat st; 177 struct passwd *pw, pwcopy; 178 int interactive = 0, dummy; 179 int have_pty = 0; 180 uid_t original_effective_uid; 181 int plen; 182 183 /* 184 * Save the original real uid. It will be needed later (uid-swapping 185 * may clobber the real uid). 186 */ 187 original_real_uid = getuid(); 188 original_effective_uid = geteuid(); 189 190 /* If we are installed setuid root be careful to not drop core. */ 191 if (original_real_uid != original_effective_uid) { 192 struct rlimit rlim; 193 rlim.rlim_cur = rlim.rlim_max = 0; 194 if (setrlimit(RLIMIT_CORE, &rlim) < 0) 195 fatal("setrlimit failed: %.100s", strerror(errno)); 196 } 197 /* 198 * Use uid-swapping to give up root privileges for the duration of 199 * option processing. We will re-instantiate the rights when we are 200 * ready to create the privileged port, and will permanently drop 201 * them when the port has been created (actually, when the connection 202 * has been made, as we may need to create the port several times). 203 */ 204 temporarily_use_uid(original_real_uid); 205 206 /* 207 * Set our umask to something reasonable, as some files are created 208 * with the default umask. This will make them world-readable but 209 * writable only by the owner, which is ok for all files for which we 210 * don't set the modes explicitly. 211 */ 212 umask(022); 213 214 /* Save our own name. */ 215 av0 = av[0]; 216 217 /* Initialize option structure to indicate that no values have been set. */ 218 initialize_options(&options); 219 220 /* Parse command-line arguments. */ 221 host = NULL; 222 223 /* If program name is not one of the standard names, use it as host name. */ 224 if (strchr(av0, '/')) 225 cp = strrchr(av0, '/') + 1; 226 else 227 cp = av0; 228 if (strcmp(cp, "rsh") != 0 && strcmp(cp, "ssh") != 0 && 229 strcmp(cp, "rlogin") != 0 && strcmp(cp, "slogin") != 0) 230 host = cp; 231 232 for (optind = 1; optind < ac; optind++) { 233 if (av[optind][0] != '-') { 234 if (host) 235 break; 236 if ((cp = strchr(av[optind], '@'))) { 237 if(cp == av[optind]) 238 usage(); 239 options.user = av[optind]; 240 *cp = '\0'; 241 host = ++cp; 242 } else 243 host = av[optind]; 244 continue; 245 } 246 opt = av[optind][1]; 247 if (!opt) 248 usage(); 249 if (strchr("eilcpLRo", opt)) { /* options with arguments */ 250 optarg = av[optind] + 2; 251 if (strcmp(optarg, "") == 0) { 252 if (optind >= ac - 1) 253 usage(); 254 optarg = av[++optind]; 255 } 256 } else { 257 if (av[optind][2]) 258 usage(); 259 optarg = NULL; 260 } 261 switch (opt) { 262 case '4': 263 IPv4or6 = AF_INET; 264 break; 265 266 case '6': 267 IPv4or6 = AF_INET6; 268 break; 269 270 case 'n': 271 stdin_null_flag = 1; 272 break; 273 274 case 'f': 275 fork_after_authentication_flag = 1; 276 stdin_null_flag = 1; 277 break; 278 279 case 'x': 280 options.forward_x11 = 0; 281 break; 282 283 case 'X': 284 options.forward_x11 = 1; 285 break; 286 287 case 'g': 288 options.gateway_ports = 1; 289 break; 290 291 case 'P': 292 options.use_privileged_port = 0; 293 break; 294 295 case 'a': 296 options.forward_agent = 0; 297 break; 298 #ifdef AFS 299 case 'k': 300 options.krb4_tgt_passing = 0; 301 options.krb5_tgt_passing = 0; 302 options.afs_token_passing = 0; 303 break; 304 #endif 305 case 'i': 306 if (stat(optarg, &st) < 0) { 307 fprintf(stderr, "Warning: Identity file %s does not exist.\n", 308 optarg); 309 break; 310 } 311 if (options.num_identity_files >= SSH_MAX_IDENTITY_FILES) 312 fatal("Too many identity files specified (max %d)", 313 SSH_MAX_IDENTITY_FILES); 314 options.identity_files[options.num_identity_files++] = 315 xstrdup(optarg); 316 break; 317 318 case 't': 319 tty_flag = 1; 320 break; 321 322 case 'v': 323 case 'V': 324 fprintf(stderr, "SSH Version %s, protocol version %d.%d.\n", 325 SSH_VERSION, PROTOCOL_MAJOR, PROTOCOL_MINOR); 326 fprintf(stderr, "Compiled with SSL.\n"); 327 if (opt == 'V') 328 exit(0); 329 debug_flag = 1; 330 options.log_level = SYSLOG_LEVEL_DEBUG; 331 break; 332 333 case 'q': 334 options.log_level = SYSLOG_LEVEL_QUIET; 335 break; 336 337 case 'e': 338 if (optarg[0] == '^' && optarg[2] == 0 && 339 (unsigned char) optarg[1] >= 64 && (unsigned char) optarg[1] < 128) 340 options.escape_char = (unsigned char) optarg[1] & 31; 341 else if (strlen(optarg) == 1) 342 options.escape_char = (unsigned char) optarg[0]; 343 else if (strcmp(optarg, "none") == 0) 344 options.escape_char = -2; 345 else { 346 fprintf(stderr, "Bad escape character '%s'.\n", optarg); 347 exit(1); 348 } 349 break; 350 351 case 'c': 352 options.cipher = cipher_number(optarg); 353 if (options.cipher == -1) { 354 fprintf(stderr, "Unknown cipher type '%s'\n", optarg); 355 exit(1); 356 } 357 break; 358 359 case 'p': 360 options.port = atoi(optarg); 361 break; 362 363 case 'l': 364 options.user = optarg; 365 break; 366 367 case 'R': 368 if (sscanf(optarg, "%hu/%255[^/]/%hu", &fwd_port, buf, 369 &fwd_host_port) != 3 && 370 sscanf(optarg, "%hu:%255[^:]:%hu", &fwd_port, buf, 371 &fwd_host_port) != 3) { 372 fprintf(stderr, "Bad forwarding specification '%s'.\n", optarg); 373 usage(); 374 /* NOTREACHED */ 375 } 376 add_remote_forward(&options, fwd_port, buf, fwd_host_port); 377 break; 378 379 case 'L': 380 if (sscanf(optarg, "%hu/%255[^/]/%hu", &fwd_port, buf, 381 &fwd_host_port) != 3 && 382 sscanf(optarg, "%hu:%255[^:]:%hu", &fwd_port, buf, 383 &fwd_host_port) != 3) { 384 fprintf(stderr, "Bad forwarding specification '%s'.\n", optarg); 385 usage(); 386 /* NOTREACHED */ 387 } 388 add_local_forward(&options, fwd_port, buf, fwd_host_port); 389 break; 390 391 case 'C': 392 options.compression = 1; 393 break; 394 395 case 'o': 396 dummy = 1; 397 if (process_config_line(&options, host ? host : "", optarg, 398 "command-line", 0, &dummy) != 0) 399 exit(1); 400 break; 401 402 default: 403 usage(); 404 } 405 } 406 407 /* Check that we got a host name. */ 408 if (!host) 409 usage(); 410 411 /* check if RSA support exists */ 412 if (rsa_alive() == 0) { 413 extern char *__progname; 414 415 fprintf(stderr, 416 "%s: no RSA support in libssl and libcrypto. See ssl(8).\n", 417 __progname); 418 exit(1); 419 } 420 /* Initialize the command to execute on remote host. */ 421 buffer_init(&command); 422 423 /* 424 * Save the command to execute on the remote host in a buffer. There 425 * is no limit on the length of the command, except by the maximum 426 * packet size. Also sets the tty flag if there is no command. 427 */ 428 if (optind == ac) { 429 /* No command specified - execute shell on a tty. */ 430 tty_flag = 1; 431 } else { 432 /* A command has been specified. Store it into the 433 buffer. */ 434 for (i = optind; i < ac; i++) { 435 if (i > optind) 436 buffer_append(&command, " ", 1); 437 buffer_append(&command, av[i], strlen(av[i])); 438 } 439 } 440 441 /* Cannot fork to background if no command. */ 442 if (fork_after_authentication_flag && buffer_len(&command) == 0) 443 fatal("Cannot fork into background without a command to execute."); 444 445 /* Allocate a tty by default if no command specified. */ 446 if (buffer_len(&command) == 0) 447 tty_flag = 1; 448 449 /* Do not allocate a tty if stdin is not a tty. */ 450 if (!isatty(fileno(stdin))) { 451 if (tty_flag) 452 fprintf(stderr, "Pseudo-terminal will not be allocated because stdin is not a terminal.\n"); 453 tty_flag = 0; 454 } 455 /* Get user data. */ 456 pw = getpwuid(original_real_uid); 457 if (!pw) { 458 fprintf(stderr, "You don't exist, go away!\n"); 459 exit(1); 460 } 461 /* Take a copy of the returned structure. */ 462 memset(&pwcopy, 0, sizeof(pwcopy)); 463 pwcopy.pw_name = xstrdup(pw->pw_name); 464 pwcopy.pw_passwd = xstrdup(pw->pw_passwd); 465 pwcopy.pw_uid = pw->pw_uid; 466 pwcopy.pw_gid = pw->pw_gid; 467 pwcopy.pw_dir = xstrdup(pw->pw_dir); 468 pwcopy.pw_shell = xstrdup(pw->pw_shell); 469 pw = &pwcopy; 470 471 /* Initialize "log" output. Since we are the client all output 472 actually goes to the terminal. */ 473 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 0); 474 475 /* Read per-user configuration file. */ 476 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, SSH_USER_CONFFILE); 477 read_config_file(buf, host, &options); 478 479 /* Read systemwide configuration file. */ 480 read_config_file(HOST_CONFIG_FILE, host, &options); 481 482 /* Fill configuration defaults. */ 483 fill_default_options(&options); 484 485 /* reinit */ 486 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 0); 487 488 if (options.user == NULL) 489 options.user = xstrdup(pw->pw_name); 490 491 if (options.hostname != NULL) 492 host = options.hostname; 493 494 /* Find canonic host name. */ 495 if (strchr(host, '.') == 0) { 496 struct addrinfo hints; 497 struct addrinfo *ai = NULL; 498 int errgai; 499 memset(&hints, 0, sizeof(hints)); 500 hints.ai_family = IPv4or6; 501 hints.ai_flags = AI_CANONNAME; 502 hints.ai_socktype = SOCK_STREAM; 503 errgai = getaddrinfo(host, NULL, &hints, &ai); 504 if (errgai == 0) { 505 if (ai->ai_canonname != NULL) 506 host = xstrdup(ai->ai_canonname); 507 freeaddrinfo(ai); 508 } 509 } 510 /* Disable rhosts authentication if not running as root. */ 511 if (original_effective_uid != 0 || !options.use_privileged_port) { 512 options.rhosts_authentication = 0; 513 options.rhosts_rsa_authentication = 0; 514 } 515 /* 516 * If using rsh has been selected, exec it now (without trying 517 * anything else). Note that we must release privileges first. 518 */ 519 if (options.use_rsh) { 520 /* 521 * Restore our superuser privileges. This must be done 522 * before permanently setting the uid. 523 */ 524 restore_uid(); 525 526 /* Switch to the original uid permanently. */ 527 permanently_set_uid(original_real_uid); 528 529 /* Execute rsh. */ 530 rsh_connect(host, options.user, &command); 531 fatal("rsh_connect returned"); 532 } 533 /* Restore our superuser privileges. */ 534 restore_uid(); 535 536 /* 537 * Open a connection to the remote host. This needs root privileges 538 * if rhosts_{rsa_}authentication is enabled. 539 */ 540 541 ok = ssh_connect(host, &hostaddr, options.port, 542 options.connection_attempts, 543 !options.rhosts_authentication && 544 !options.rhosts_rsa_authentication, 545 original_real_uid, 546 options.proxy_command); 547 548 /* 549 * If we successfully made the connection, load the host private key 550 * in case we will need it later for combined rsa-rhosts 551 * authentication. This must be done before releasing extra 552 * privileges, because the file is only readable by root. 553 */ 554 if (ok) { 555 host_private_key = RSA_new(); 556 if (load_private_key(HOST_KEY_FILE, "", host_private_key, NULL)) 557 host_private_key_loaded = 1; 558 } 559 /* 560 * Get rid of any extra privileges that we may have. We will no 561 * longer need them. Also, extra privileges could make it very hard 562 * to read identity files and other non-world-readable files from the 563 * user's home directory if it happens to be on a NFS volume where 564 * root is mapped to nobody. 565 */ 566 567 /* 568 * Note that some legacy systems need to postpone the following call 569 * to permanently_set_uid() until the private hostkey is destroyed 570 * with RSA_free(). Otherwise the calling user could ptrace() the 571 * process, read the private hostkey and impersonate the host. 572 * OpenBSD does not allow ptracing of setuid processes. 573 */ 574 permanently_set_uid(original_real_uid); 575 576 /* 577 * Now that we are back to our own permissions, create ~/.ssh 578 * directory if it doesn\'t already exist. 579 */ 580 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, SSH_USER_DIR); 581 if (stat(buf, &st) < 0) 582 if (mkdir(buf, 0755) < 0) 583 error("Could not create directory '%.200s'.", buf); 584 585 /* Check if the connection failed, and try "rsh" if appropriate. */ 586 if (!ok) { 587 if (options.port != 0) 588 log("Secure connection to %.100s on port %hu refused%.100s.", 589 host, options.port, 590 options.fallback_to_rsh ? "; reverting to insecure method" : ""); 591 else 592 log("Secure connection to %.100s refused%.100s.", host, 593 options.fallback_to_rsh ? "; reverting to insecure method" : ""); 594 595 if (options.fallback_to_rsh) { 596 rsh_connect(host, options.user, &command); 597 fatal("rsh_connect returned"); 598 } 599 exit(1); 600 } 601 /* Expand ~ in options.identity_files. */ 602 for (i = 0; i < options.num_identity_files; i++) 603 options.identity_files[i] = 604 tilde_expand_filename(options.identity_files[i], original_real_uid); 605 606 /* Expand ~ in known host file names. */ 607 options.system_hostfile = tilde_expand_filename(options.system_hostfile, 608 original_real_uid); 609 options.user_hostfile = tilde_expand_filename(options.user_hostfile, 610 original_real_uid); 611 612 /* Log into the remote system. This never returns if the login fails. */ 613 ssh_login(host_private_key_loaded, host_private_key, 614 host, (struct sockaddr *)&hostaddr, original_real_uid); 615 616 /* We no longer need the host private key. Clear it now. */ 617 if (host_private_key_loaded) 618 RSA_free(host_private_key); /* Destroys contents safely */ 619 620 /* Enable compression if requested. */ 621 if (options.compression) { 622 debug("Requesting compression at level %d.", options.compression_level); 623 624 if (options.compression_level < 1 || options.compression_level > 9) 625 fatal("Compression level must be from 1 (fast) to 9 (slow, best)."); 626 627 /* Send the request. */ 628 packet_start(SSH_CMSG_REQUEST_COMPRESSION); 629 packet_put_int(options.compression_level); 630 packet_send(); 631 packet_write_wait(); 632 type = packet_read(&plen); 633 if (type == SSH_SMSG_SUCCESS) 634 packet_start_compression(options.compression_level); 635 else if (type == SSH_SMSG_FAILURE) 636 log("Warning: Remote host refused compression."); 637 else 638 packet_disconnect("Protocol error waiting for compression response."); 639 } 640 /* Allocate a pseudo tty if appropriate. */ 641 if (tty_flag) { 642 debug("Requesting pty."); 643 644 /* Start the packet. */ 645 packet_start(SSH_CMSG_REQUEST_PTY); 646 647 /* Store TERM in the packet. There is no limit on the 648 length of the string. */ 649 cp = getenv("TERM"); 650 if (!cp) 651 cp = ""; 652 packet_put_string(cp, strlen(cp)); 653 654 /* Store window size in the packet. */ 655 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0) 656 memset(&ws, 0, sizeof(ws)); 657 packet_put_int(ws.ws_row); 658 packet_put_int(ws.ws_col); 659 packet_put_int(ws.ws_xpixel); 660 packet_put_int(ws.ws_ypixel); 661 662 /* Store tty modes in the packet. */ 663 tty_make_modes(fileno(stdin)); 664 665 /* Send the packet, and wait for it to leave. */ 666 packet_send(); 667 packet_write_wait(); 668 669 /* Read response from the server. */ 670 type = packet_read(&plen); 671 if (type == SSH_SMSG_SUCCESS) { 672 interactive = 1; 673 have_pty = 1; 674 } else if (type == SSH_SMSG_FAILURE) 675 log("Warning: Remote host failed or refused to allocate a pseudo tty."); 676 else 677 packet_disconnect("Protocol error waiting for pty request response."); 678 } 679 /* Request X11 forwarding if enabled and DISPLAY is set. */ 680 if (options.forward_x11 && getenv("DISPLAY") != NULL) { 681 char line[512], proto[512], data[512]; 682 FILE *f; 683 int forwarded = 0, got_data = 0, i; 684 685 #ifdef XAUTH_PATH 686 /* Try to get Xauthority information for the display. */ 687 snprintf(line, sizeof line, "%.100s list %.200s 2>/dev/null", 688 XAUTH_PATH, getenv("DISPLAY")); 689 f = popen(line, "r"); 690 if (f && fgets(line, sizeof(line), f) && 691 sscanf(line, "%*s %s %s", proto, data) == 2) 692 got_data = 1; 693 if (f) 694 pclose(f); 695 #endif /* XAUTH_PATH */ 696 /* 697 * If we didn't get authentication data, just make up some 698 * data. The forwarding code will check the validity of the 699 * response anyway, and substitute this data. The X11 700 * server, however, will ignore this fake data and use 701 * whatever authentication mechanisms it was using otherwise 702 * for the local connection. 703 */ 704 if (!got_data) { 705 u_int32_t rand = 0; 706 707 strlcpy(proto, "MIT-MAGIC-COOKIE-1", sizeof proto); 708 for (i = 0; i < 16; i++) { 709 if (i % 4 == 0) 710 rand = arc4random(); 711 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x", rand & 0xff); 712 rand >>= 8; 713 } 714 } 715 /* 716 * Got local authentication reasonable information. Request 717 * forwarding with authentication spoofing. 718 */ 719 debug("Requesting X11 forwarding with authentication spoofing."); 720 x11_request_forwarding_with_spoofing(proto, data); 721 722 /* Read response from the server. */ 723 type = packet_read(&plen); 724 if (type == SSH_SMSG_SUCCESS) { 725 forwarded = 1; 726 interactive = 1; 727 } else if (type == SSH_SMSG_FAILURE) 728 log("Warning: Remote host denied X11 forwarding."); 729 else 730 packet_disconnect("Protocol error waiting for X11 forwarding"); 731 } 732 /* Tell the packet module whether this is an interactive session. */ 733 packet_set_interactive(interactive, options.keepalives); 734 735 /* Clear agent forwarding if we don\'t have an agent. */ 736 authfd = ssh_get_authentication_socket(); 737 if (authfd < 0) 738 options.forward_agent = 0; 739 else 740 ssh_close_authentication_socket(authfd); 741 742 /* Request authentication agent forwarding if appropriate. */ 743 if (options.forward_agent) { 744 debug("Requesting authentication agent forwarding."); 745 auth_request_forwarding(); 746 747 /* Read response from the server. */ 748 type = packet_read(&plen); 749 packet_integrity_check(plen, 0, type); 750 if (type != SSH_SMSG_SUCCESS) 751 log("Warning: Remote host denied authentication agent forwarding."); 752 } 753 /* Initiate local TCP/IP port forwardings. */ 754 for (i = 0; i < options.num_local_forwards; i++) { 755 debug("Connections to local port %d forwarded to remote address %.200s:%d", 756 options.local_forwards[i].port, 757 options.local_forwards[i].host, 758 options.local_forwards[i].host_port); 759 channel_request_local_forwarding(options.local_forwards[i].port, 760 options.local_forwards[i].host, 761 options.local_forwards[i].host_port, 762 options.gateway_ports); 763 } 764 765 /* Initiate remote TCP/IP port forwardings. */ 766 for (i = 0; i < options.num_remote_forwards; i++) { 767 debug("Connections to remote port %d forwarded to local address %.200s:%d", 768 options.remote_forwards[i].port, 769 options.remote_forwards[i].host, 770 options.remote_forwards[i].host_port); 771 channel_request_remote_forwarding(options.remote_forwards[i].port, 772 options.remote_forwards[i].host, 773 options.remote_forwards[i].host_port); 774 } 775 776 /* If requested, let ssh continue in the background. */ 777 if (fork_after_authentication_flag) 778 if (daemon(1, 1) < 0) 779 fatal("daemon() failed: %.200s", strerror(errno)); 780 781 /* 782 * If a command was specified on the command line, execute the 783 * command now. Otherwise request the server to start a shell. 784 */ 785 if (buffer_len(&command) > 0) { 786 int len = buffer_len(&command); 787 if (len > 900) 788 len = 900; 789 debug("Sending command: %.*s", len, buffer_ptr(&command)); 790 packet_start(SSH_CMSG_EXEC_CMD); 791 packet_put_string(buffer_ptr(&command), buffer_len(&command)); 792 packet_send(); 793 packet_write_wait(); 794 } else { 795 debug("Requesting shell."); 796 packet_start(SSH_CMSG_EXEC_SHELL); 797 packet_send(); 798 packet_write_wait(); 799 } 800 801 /* Enter the interactive session. */ 802 exit_status = client_loop(have_pty, tty_flag ? options.escape_char : -1); 803 804 /* Close the connection to the remote host. */ 805 packet_close(); 806 807 /* Exit with the status returned by the program on the remote side. */ 808 exit(exit_status); 809 } 810