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