| 1 | /* $OpenBSD: ssh.c,v 1.364 2011/08/02 23:15:03 djm 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 | * 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 | * As far as I am concerned, the code I have written for this software |
| 11 | * can be used freely for any purpose. Any derived versions of this |
| 12 | * software must be clearly marked as such, and if the derived work is |
| 13 | * incompatible with the protocol description in the RFC file, it must be |
| 14 | * called by a name other than "ssh" or "Secure Shell". |
| 15 | * |
| 16 | * Copyright (c) 1999 Niels Provos. All rights reserved. |
| 17 | * Copyright (c) 2000, 2001, 2002, 2003 Markus Friedl. All rights reserved. |
| 18 | * |
| 19 | * Modified to work with SSL by Niels Provos <provos@citi.umich.edu> |
| 20 | * in Canada (German citizen). |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the above copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * |
| 31 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 32 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 33 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 34 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 35 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 36 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 37 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 38 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 39 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 40 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 41 | */ |
| 42 | |
| 43 | #include "includes.h" |
| 44 | |
| 45 | #include <sys/types.h> |
| 46 | #ifdef HAVE_SYS_STAT_H |
| 47 | # include <sys/stat.h> |
| 48 | #endif |
| 49 | #include <sys/resource.h> |
| 50 | #include <sys/ioctl.h> |
| 51 | #include <sys/param.h> |
| 52 | #include <sys/socket.h> |
| 53 | #include <sys/wait.h> |
| 54 | |
| 55 | #include <ctype.h> |
| 56 | #include <errno.h> |
| 57 | #include <fcntl.h> |
| 58 | #include <netdb.h> |
| 59 | #ifdef HAVE_PATHS_H |
| 60 | #include <paths.h> |
| 61 | #endif |
| 62 | #include <pwd.h> |
| 63 | #include <signal.h> |
| 64 | #include <stdarg.h> |
| 65 | #include <stddef.h> |
| 66 | #include <stdio.h> |
| 67 | #include <stdlib.h> |
| 68 | #include <string.h> |
| 69 | #include <unistd.h> |
| 70 | |
| 71 | #include <netinet/in.h> |
| 72 | #include <arpa/inet.h> |
| 73 | |
| 74 | #include <openssl/evp.h> |
| 75 | #include <openssl/err.h> |
| 76 | #include "openbsd-compat/openssl-compat.h" |
| 77 | #include "openbsd-compat/sys-queue.h" |
| 78 | |
| 79 | #include "xmalloc.h" |
| 80 | #include "ssh.h" |
| 81 | #include "ssh1.h" |
| 82 | #include "ssh2.h" |
| 83 | #include "canohost.h" |
| 84 | #include "compat.h" |
| 85 | #include "cipher.h" |
| 86 | #include "packet.h" |
| 87 | #include "buffer.h" |
| 88 | #include "channels.h" |
| 89 | #include "key.h" |
| 90 | #include "authfd.h" |
| 91 | #include "authfile.h" |
| 92 | #include "pathnames.h" |
| 93 | #include "dispatch.h" |
| 94 | #include "clientloop.h" |
| 95 | #include "log.h" |
| 96 | #include "readconf.h" |
| 97 | #include "sshconnect.h" |
| 98 | #include "misc.h" |
| 99 | #include "kex.h" |
| 100 | #include "mac.h" |
| 101 | #include "sshpty.h" |
| 102 | #include "match.h" |
| 103 | #include "msg.h" |
| 104 | #include "uidswap.h" |
| 105 | #include "roaming.h" |
| 106 | #include "version.h" |
| 107 | |
| 108 | #ifdef ENABLE_PKCS11 |
| 109 | #include "ssh-pkcs11.h" |
| 110 | #endif |
| 111 | |
| 112 | extern char *__progname; |
| 113 | |
| 114 | /* Saves a copy of argv for setproctitle emulation */ |
| 115 | #ifndef HAVE_SETPROCTITLE |
| 116 | static char **saved_av; |
| 117 | #endif |
| 118 | |
| 119 | /* Flag indicating whether debug mode is on. May be set on the command line. */ |
| 120 | int debug_flag = 0; |
| 121 | |
| 122 | /* Flag indicating whether a tty should be requested */ |
| 123 | int tty_flag = 0; |
| 124 | |
| 125 | /* don't exec a shell */ |
| 126 | int no_shell_flag = 0; |
| 127 | |
| 128 | /* |
| 129 | * Flag indicating that nothing should be read from stdin. This can be set |
| 130 | * on the command line. |
| 131 | */ |
| 132 | int stdin_null_flag = 0; |
| 133 | |
| 134 | /* |
| 135 | * Flag indicating that the current process should be backgrounded and |
| 136 | * a new slave launched in the foreground for ControlPersist. |
| 137 | */ |
| 138 | int need_controlpersist_detach = 0; |
| 139 | |
| 140 | /* Copies of flags for ControlPersist foreground slave */ |
| 141 | int ostdin_null_flag, ono_shell_flag, otty_flag, orequest_tty; |
| 142 | |
| 143 | /* |
| 144 | * Flag indicating that ssh should fork after authentication. This is useful |
| 145 | * so that the passphrase can be entered manually, and then ssh goes to the |
| 146 | * background. |
| 147 | */ |
| 148 | int fork_after_authentication_flag = 0; |
| 149 | |
| 150 | /* forward stdio to remote host and port */ |
| 151 | char *stdio_forward_host = NULL; |
| 152 | int stdio_forward_port = 0; |
| 153 | |
| 154 | /* |
| 155 | * General data structure for command line options and options configurable |
| 156 | * in configuration files. See readconf.h. |
| 157 | */ |
| 158 | Options options; |
| 159 | |
| 160 | /* optional user configfile */ |
| 161 | char *config = NULL; |
| 162 | |
| 163 | /* |
| 164 | * Name of the host we are connecting to. This is the name given on the |
| 165 | * command line, or the HostName specified for the user-supplied name in a |
| 166 | * configuration file. |
| 167 | */ |
| 168 | char *host; |
| 169 | |
| 170 | /* socket address the host resolves to */ |
| 171 | struct sockaddr_storage hostaddr; |
| 172 | |
| 173 | /* Private host keys. */ |
| 174 | Sensitive sensitive_data; |
| 175 | |
| 176 | /* Original real UID. */ |
| 177 | uid_t original_real_uid; |
| 178 | uid_t original_effective_uid; |
| 179 | |
| 180 | /* command to be executed */ |
| 181 | Buffer command; |
| 182 | |
| 183 | /* Should we execute a command or invoke a subsystem? */ |
| 184 | int subsystem_flag = 0; |
| 185 | |
| 186 | /* # of replies received for global requests */ |
| 187 | static int remote_forward_confirms_received = 0; |
| 188 | |
| 189 | /* mux.c */ |
| 190 | extern int muxserver_sock; |
| 191 | extern u_int muxclient_command; |
| 192 | |
| 193 | /* Prints a help message to the user. This function never returns. */ |
| 194 | |
| 195 | static void |
| 196 | usage(void) |
| 197 | { |
| 198 | fprintf(stderr, |
| 199 | "usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]\n" |
| 200 | " [-D [bind_address:]port] [-e escape_char] [-F configfile]\n" |
| 201 | " [-I pkcs11] [-i identity_file]\n" |
| 202 | " [-L [bind_address:]port:host:hostport]\n" |
| 203 | " [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n" |
| 204 | " [-R [bind_address:]port:host:hostport] [-S ctl_path]\n" |
| 205 | " [-W host:port] [-w local_tun[:remote_tun]]\n" |
| 206 | " [user@]hostname [command]\n" |
| 207 | ); |
| 208 | exit(255); |
| 209 | } |
| 210 | |
| 211 | static int ssh_session(void); |
| 212 | static int ssh_session2(void); |
| 213 | static void load_public_identity_files(void); |
| 214 | static void main_sigchld_handler(int); |
| 215 | |
| 216 | /* from muxclient.c */ |
| 217 | void muxclient(const char *); |
| 218 | void muxserver_listen(void); |
| 219 | |
| 220 | /* ~/ expand a list of paths. NB. assumes path[n] is heap-allocated. */ |
| 221 | static void |
| 222 | tilde_expand_paths(char **paths, u_int num_paths) |
| 223 | { |
| 224 | u_int i; |
| 225 | char *cp; |
| 226 | |
| 227 | for (i = 0; i < num_paths; i++) { |
| 228 | cp = tilde_expand_filename(paths[i], original_real_uid); |
| 229 | xfree(paths[i]); |
| 230 | paths[i] = cp; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /* |
| 235 | * Main program for the ssh client. |
| 236 | */ |
| 237 | int |
| 238 | main(int ac, char **av) |
| 239 | { |
| 240 | int i, r, opt, exit_status, use_syslog; |
| 241 | char *p, *cp, *line, *argv0, buf[MAXPATHLEN], *host_arg; |
| 242 | char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV]; |
| 243 | struct stat st; |
| 244 | struct passwd *pw; |
| 245 | int dummy, timeout_ms; |
| 246 | extern int optind, optreset; |
| 247 | extern char *optarg; |
| 248 | |
| 249 | struct servent *sp; |
| 250 | Forward fwd; |
| 251 | |
| 252 | /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ |
| 253 | sanitise_stdfd(); |
| 254 | |
| 255 | __progname = ssh_get_progname(av[0]); |
| 256 | |
| 257 | #ifndef HAVE_SETPROCTITLE |
| 258 | /* Prepare for later setproctitle emulation */ |
| 259 | /* Save argv so it isn't clobbered by setproctitle() emulation */ |
| 260 | saved_av = xcalloc(ac + 1, sizeof(*saved_av)); |
| 261 | for (i = 0; i < ac; i++) |
| 262 | saved_av[i] = xstrdup(av[i]); |
| 263 | saved_av[i] = NULL; |
| 264 | compat_init_setproctitle(ac, av); |
| 265 | av = saved_av; |
| 266 | #endif |
| 267 | |
| 268 | /* |
| 269 | * Discard other fds that are hanging around. These can cause problem |
| 270 | * with backgrounded ssh processes started by ControlPersist. |
| 271 | */ |
| 272 | closefrom(STDERR_FILENO + 1); |
| 273 | |
| 274 | /* |
| 275 | * Save the original real uid. It will be needed later (uid-swapping |
| 276 | * may clobber the real uid). |
| 277 | */ |
| 278 | original_real_uid = getuid(); |
| 279 | original_effective_uid = geteuid(); |
| 280 | |
| 281 | /* |
| 282 | * Use uid-swapping to give up root privileges for the duration of |
| 283 | * option processing. We will re-instantiate the rights when we are |
| 284 | * ready to create the privileged port, and will permanently drop |
| 285 | * them when the port has been created (actually, when the connection |
| 286 | * has been made, as we may need to create the port several times). |
| 287 | */ |
| 288 | PRIV_END; |
| 289 | |
| 290 | #ifdef HAVE_SETRLIMIT |
| 291 | /* If we are installed setuid root be careful to not drop core. */ |
| 292 | if (original_real_uid != original_effective_uid) { |
| 293 | struct rlimit rlim; |
| 294 | rlim.rlim_cur = rlim.rlim_max = 0; |
| 295 | if (setrlimit(RLIMIT_CORE, &rlim) < 0) |
| 296 | fatal("setrlimit failed: %.100s", strerror(errno)); |
| 297 | } |
| 298 | #endif |
| 299 | /* Get user data. */ |
| 300 | pw = getpwuid(original_real_uid); |
| 301 | if (!pw) { |
| 302 | logit("You don't exist, go away!"); |
| 303 | exit(255); |
| 304 | } |
| 305 | /* Take a copy of the returned structure. */ |
| 306 | pw = pwcopy(pw); |
| 307 | |
| 308 | /* |
| 309 | * Set our umask to something reasonable, as some files are created |
| 310 | * with the default umask. This will make them world-readable but |
| 311 | * writable only by the owner, which is ok for all files for which we |
| 312 | * don't set the modes explicitly. |
| 313 | */ |
| 314 | umask(022); |
| 315 | |
| 316 | /* |
| 317 | * Initialize option structure to indicate that no values have been |
| 318 | * set. |
| 319 | */ |
| 320 | initialize_options(&options); |
| 321 | |
| 322 | /* Parse command-line arguments. */ |
| 323 | host = NULL; |
| 324 | use_syslog = 0; |
| 325 | argv0 = av[0]; |
| 326 | |
| 327 | again: |
| 328 | while ((opt = getopt(ac, av, "1246ab:c:e:fgi:kl:m:no:p:qstvx" |
| 329 | "ACD:F:I:KL:MNO:PR:S:TVw:W:XYy")) != -1) { |
| 330 | switch (opt) { |
| 331 | case '1': |
| 332 | options.protocol = SSH_PROTO_1; |
| 333 | break; |
| 334 | case '2': |
| 335 | options.protocol = SSH_PROTO_2; |
| 336 | break; |
| 337 | case '4': |
| 338 | options.address_family = AF_INET; |
| 339 | break; |
| 340 | case '6': |
| 341 | options.address_family = AF_INET6; |
| 342 | break; |
| 343 | case 'n': |
| 344 | stdin_null_flag = 1; |
| 345 | break; |
| 346 | case 'f': |
| 347 | fork_after_authentication_flag = 1; |
| 348 | stdin_null_flag = 1; |
| 349 | break; |
| 350 | case 'x': |
| 351 | options.forward_x11 = 0; |
| 352 | break; |
| 353 | case 'X': |
| 354 | options.forward_x11 = 1; |
| 355 | break; |
| 356 | case 'y': |
| 357 | use_syslog = 1; |
| 358 | break; |
| 359 | case 'Y': |
| 360 | options.forward_x11 = 1; |
| 361 | options.forward_x11_trusted = 1; |
| 362 | break; |
| 363 | case 'g': |
| 364 | options.gateway_ports = 1; |
| 365 | break; |
| 366 | case 'O': |
| 367 | if (stdio_forward_host != NULL) |
| 368 | fatal("Cannot specify multiplexing " |
| 369 | "command with -W"); |
| 370 | else if (muxclient_command != 0) |
| 371 | fatal("Multiplexing command already specified"); |
| 372 | if (strcmp(optarg, "check") == 0) |
| 373 | muxclient_command = SSHMUX_COMMAND_ALIVE_CHECK; |
| 374 | else if (strcmp(optarg, "forward") == 0) |
| 375 | muxclient_command = SSHMUX_COMMAND_FORWARD; |
| 376 | else if (strcmp(optarg, "exit") == 0) |
| 377 | muxclient_command = SSHMUX_COMMAND_TERMINATE; |
| 378 | else if (strcmp(optarg, "stop") == 0) |
| 379 | muxclient_command = SSHMUX_COMMAND_STOP; |
| 380 | else |
| 381 | fatal("Invalid multiplex command."); |
| 382 | break; |
| 383 | case 'P': /* deprecated */ |
| 384 | options.use_privileged_port = 0; |
| 385 | break; |
| 386 | case 'a': |
| 387 | options.forward_agent = 0; |
| 388 | break; |
| 389 | case 'A': |
| 390 | options.forward_agent = 1; |
| 391 | break; |
| 392 | case 'k': |
| 393 | options.gss_deleg_creds = 0; |
| 394 | break; |
| 395 | case 'K': |
| 396 | options.gss_authentication = 1; |
| 397 | options.gss_deleg_creds = 1; |
| 398 | break; |
| 399 | case 'i': |
| 400 | if (stat(optarg, &st) < 0) { |
| 401 | fprintf(stderr, "Warning: Identity file %s " |
| 402 | "not accessible: %s.\n", optarg, |
| 403 | strerror(errno)); |
| 404 | break; |
| 405 | } |
| 406 | if (options.num_identity_files >= |
| 407 | SSH_MAX_IDENTITY_FILES) |
| 408 | fatal("Too many identity files specified " |
| 409 | "(max %d)", SSH_MAX_IDENTITY_FILES); |
| 410 | options.identity_files[options.num_identity_files++] = |
| 411 | xstrdup(optarg); |
| 412 | break; |
| 413 | case 'I': |
| 414 | #ifdef ENABLE_PKCS11 |
| 415 | options.pkcs11_provider = xstrdup(optarg); |
| 416 | #else |
| 417 | fprintf(stderr, "no support for PKCS#11.\n"); |
| 418 | #endif |
| 419 | break; |
| 420 | case 't': |
| 421 | if (options.request_tty == REQUEST_TTY_YES) |
| 422 | options.request_tty = REQUEST_TTY_FORCE; |
| 423 | else |
| 424 | options.request_tty = REQUEST_TTY_YES; |
| 425 | break; |
| 426 | case 'v': |
| 427 | if (debug_flag == 0) { |
| 428 | debug_flag = 1; |
| 429 | options.log_level = SYSLOG_LEVEL_DEBUG1; |
| 430 | } else { |
| 431 | if (options.log_level < SYSLOG_LEVEL_DEBUG3) |
| 432 | options.log_level++; |
| 433 | break; |
| 434 | } |
| 435 | /* FALLTHROUGH */ |
| 436 | case 'V': |
| 437 | fprintf(stderr, "%s, %s\n", |
| 438 | SSH_RELEASE, SSLeay_version(SSLEAY_VERSION)); |
| 439 | if (opt == 'V') |
| 440 | exit(0); |
| 441 | break; |
| 442 | case 'w': |
| 443 | if (options.tun_open == -1) |
| 444 | options.tun_open = SSH_TUNMODE_DEFAULT; |
| 445 | options.tun_local = a2tun(optarg, &options.tun_remote); |
| 446 | if (options.tun_local == SSH_TUNID_ERR) { |
| 447 | fprintf(stderr, |
| 448 | "Bad tun device '%s'\n", optarg); |
| 449 | exit(255); |
| 450 | } |
| 451 | break; |
| 452 | case 'W': |
| 453 | if (stdio_forward_host != NULL) |
| 454 | fatal("stdio forward already specified"); |
| 455 | if (muxclient_command != 0) |
| 456 | fatal("Cannot specify stdio forward with -O"); |
| 457 | if (parse_forward(&fwd, optarg, 1, 0)) { |
| 458 | stdio_forward_host = fwd.listen_host; |
| 459 | stdio_forward_port = fwd.listen_port; |
| 460 | xfree(fwd.connect_host); |
| 461 | } else { |
| 462 | fprintf(stderr, |
| 463 | "Bad stdio forwarding specification '%s'\n", |
| 464 | optarg); |
| 465 | exit(255); |
| 466 | } |
| 467 | options.request_tty = REQUEST_TTY_NO; |
| 468 | no_shell_flag = 1; |
| 469 | options.clear_forwardings = 1; |
| 470 | options.exit_on_forward_failure = 1; |
| 471 | break; |
| 472 | case 'q': |
| 473 | options.log_level = SYSLOG_LEVEL_QUIET; |
| 474 | break; |
| 475 | case 'e': |
| 476 | if (optarg[0] == '^' && optarg[2] == 0 && |
| 477 | (u_char) optarg[1] >= 64 && |
| 478 | (u_char) optarg[1] < 128) |
| 479 | options.escape_char = (u_char) optarg[1] & 31; |
| 480 | else if (strlen(optarg) == 1) |
| 481 | options.escape_char = (u_char) optarg[0]; |
| 482 | else if (strcmp(optarg, "none") == 0) |
| 483 | options.escape_char = SSH_ESCAPECHAR_NONE; |
| 484 | else { |
| 485 | fprintf(stderr, "Bad escape character '%s'.\n", |
| 486 | optarg); |
| 487 | exit(255); |
| 488 | } |
| 489 | break; |
| 490 | case 'c': |
| 491 | if (ciphers_valid(optarg)) { |
| 492 | /* SSH2 only */ |
| 493 | options.ciphers = xstrdup(optarg); |
| 494 | options.cipher = SSH_CIPHER_INVALID; |
| 495 | } else { |
| 496 | /* SSH1 only */ |
| 497 | options.cipher = cipher_number(optarg); |
| 498 | if (options.cipher == -1) { |
| 499 | fprintf(stderr, |
| 500 | "Unknown cipher type '%s'\n", |
| 501 | optarg); |
| 502 | exit(255); |
| 503 | } |
| 504 | if (options.cipher == SSH_CIPHER_3DES) |
| 505 | options.ciphers = "3des-cbc"; |
| 506 | else if (options.cipher == SSH_CIPHER_BLOWFISH) |
| 507 | options.ciphers = "blowfish-cbc"; |
| 508 | else |
| 509 | options.ciphers = (char *)-1; |
| 510 | } |
| 511 | break; |
| 512 | case 'm': |
| 513 | if (mac_valid(optarg)) |
| 514 | options.macs = xstrdup(optarg); |
| 515 | else { |
| 516 | fprintf(stderr, "Unknown mac type '%s'\n", |
| 517 | optarg); |
| 518 | exit(255); |
| 519 | } |
| 520 | break; |
| 521 | case 'M': |
| 522 | if (options.control_master == SSHCTL_MASTER_YES) |
| 523 | options.control_master = SSHCTL_MASTER_ASK; |
| 524 | else |
| 525 | options.control_master = SSHCTL_MASTER_YES; |
| 526 | break; |
| 527 | case 'p': |
| 528 | options.port = a2port(optarg); |
| 529 | if (options.port <= 0) { |
| 530 | fprintf(stderr, "Bad port '%s'\n", optarg); |
| 531 | exit(255); |
| 532 | } |
| 533 | break; |
| 534 | case 'l': |
| 535 | options.user = optarg; |
| 536 | break; |
| 537 | |
| 538 | case 'L': |
| 539 | if (parse_forward(&fwd, optarg, 0, 0)) |
| 540 | add_local_forward(&options, &fwd); |
| 541 | else { |
| 542 | fprintf(stderr, |
| 543 | "Bad local forwarding specification '%s'\n", |
| 544 | optarg); |
| 545 | exit(255); |
| 546 | } |
| 547 | break; |
| 548 | |
| 549 | case 'R': |
| 550 | if (parse_forward(&fwd, optarg, 0, 1)) { |
| 551 | add_remote_forward(&options, &fwd); |
| 552 | } else { |
| 553 | fprintf(stderr, |
| 554 | "Bad remote forwarding specification " |
| 555 | "'%s'\n", optarg); |
| 556 | exit(255); |
| 557 | } |
| 558 | break; |
| 559 | |
| 560 | case 'D': |
| 561 | if (parse_forward(&fwd, optarg, 1, 0)) { |
| 562 | add_local_forward(&options, &fwd); |
| 563 | } else { |
| 564 | fprintf(stderr, |
| 565 | "Bad dynamic forwarding specification " |
| 566 | "'%s'\n", optarg); |
| 567 | exit(255); |
| 568 | } |
| 569 | break; |
| 570 | |
| 571 | case 'C': |
| 572 | options.compression = 1; |
| 573 | break; |
| 574 | case 'N': |
| 575 | no_shell_flag = 1; |
| 576 | options.request_tty = REQUEST_TTY_NO; |
| 577 | break; |
| 578 | case 'T': |
| 579 | options.request_tty = REQUEST_TTY_NO; |
| 580 | /* ensure that the user doesn't try to backdoor a */ |
| 581 | /* null cipher switch on an interactive session */ |
| 582 | /* so explicitly disable it no matter what */ |
| 583 | options.none_switch=0; |
| 584 | break; |
| 585 | case 'o': |
| 586 | dummy = 1; |
| 587 | line = xstrdup(optarg); |
| 588 | if (process_config_line(&options, host ? host : "", |
| 589 | line, "command-line", 0, &dummy) != 0) |
| 590 | exit(255); |
| 591 | xfree(line); |
| 592 | break; |
| 593 | case 's': |
| 594 | subsystem_flag = 1; |
| 595 | break; |
| 596 | case 'S': |
| 597 | if (options.control_path != NULL) |
| 598 | free(options.control_path); |
| 599 | options.control_path = xstrdup(optarg); |
| 600 | break; |
| 601 | case 'b': |
| 602 | options.bind_address = optarg; |
| 603 | break; |
| 604 | case 'F': |
| 605 | config = optarg; |
| 606 | break; |
| 607 | default: |
| 608 | usage(); |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | ac -= optind; |
| 613 | av += optind; |
| 614 | |
| 615 | if (ac > 0 && !host) { |
| 616 | if (strrchr(*av, '@')) { |
| 617 | p = xstrdup(*av); |
| 618 | cp = strrchr(p, '@'); |
| 619 | if (cp == NULL || cp == p) |
| 620 | usage(); |
| 621 | options.user = p; |
| 622 | *cp = '\0'; |
| 623 | host = ++cp; |
| 624 | } else |
| 625 | host = *av; |
| 626 | if (ac > 1) { |
| 627 | optind = optreset = 1; |
| 628 | goto again; |
| 629 | } |
| 630 | ac--, av++; |
| 631 | } |
| 632 | |
| 633 | /* Check that we got a host name. */ |
| 634 | if (!host) |
| 635 | usage(); |
| 636 | |
| 637 | OpenSSL_add_all_algorithms(); |
| 638 | ERR_load_crypto_strings(); |
| 639 | |
| 640 | /* Initialize the command to execute on remote host. */ |
| 641 | buffer_init(&command); |
| 642 | |
| 643 | if (options.request_tty == REQUEST_TTY_YES || |
| 644 | options.request_tty == REQUEST_TTY_FORCE) |
| 645 | tty_flag = 1; |
| 646 | |
| 647 | /* |
| 648 | * Save the command to execute on the remote host in a buffer. There |
| 649 | * is no limit on the length of the command, except by the maximum |
| 650 | * packet size. Also sets the tty flag if there is no command. |
| 651 | */ |
| 652 | if (!ac) { |
| 653 | /* No command specified - execute shell on a tty. */ |
| 654 | tty_flag = options.request_tty != REQUEST_TTY_NO; |
| 655 | if (subsystem_flag) { |
| 656 | fprintf(stderr, |
| 657 | "You must specify a subsystem to invoke.\n"); |
| 658 | usage(); |
| 659 | } |
| 660 | } else { |
| 661 | /* A command has been specified. Store it into the buffer. */ |
| 662 | for (i = 0; i < ac; i++) { |
| 663 | if (i) |
| 664 | buffer_append(&command, " ", 1); |
| 665 | buffer_append(&command, av[i], strlen(av[i])); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | /* Cannot fork to background if no command. */ |
| 670 | if (fork_after_authentication_flag && buffer_len(&command) == 0 && |
| 671 | !no_shell_flag) |
| 672 | fatal("Cannot fork into background without a command " |
| 673 | "to execute."); |
| 674 | |
| 675 | /* Allocate a tty by default if no command specified. */ |
| 676 | if (buffer_len(&command) == 0) |
| 677 | tty_flag = options.request_tty != REQUEST_TTY_NO; |
| 678 | |
| 679 | /* Force no tty */ |
| 680 | if (options.request_tty == REQUEST_TTY_NO || muxclient_command != 0) |
| 681 | tty_flag = 0; |
| 682 | /* Do not allocate a tty if stdin is not a tty. */ |
| 683 | if ((!isatty(fileno(stdin)) || stdin_null_flag) && |
| 684 | options.request_tty != REQUEST_TTY_FORCE) { |
| 685 | if (tty_flag) |
| 686 | logit("Pseudo-terminal will not be allocated because " |
| 687 | "stdin is not a terminal."); |
| 688 | tty_flag = 0; |
| 689 | } |
| 690 | |
| 691 | /* |
| 692 | * Initialize "log" output. Since we are the client all output |
| 693 | * actually goes to stderr. |
| 694 | */ |
| 695 | log_init(argv0, |
| 696 | options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level, |
| 697 | SYSLOG_FACILITY_USER, !use_syslog); |
| 698 | |
| 699 | /* |
| 700 | * Read per-user configuration file. Ignore the system wide config |
| 701 | * file if the user specifies a config file on the command line. |
| 702 | */ |
| 703 | if (config != NULL) { |
| 704 | if (!read_config_file(config, host, &options, 0)) |
| 705 | fatal("Can't open user config file %.100s: " |
| 706 | "%.100s", config, strerror(errno)); |
| 707 | } else { |
| 708 | r = snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, |
| 709 | _PATH_SSH_USER_CONFFILE); |
| 710 | if (r > 0 && (size_t)r < sizeof(buf)) |
| 711 | (void)read_config_file(buf, host, &options, 1); |
| 712 | |
| 713 | /* Read systemwide configuration file after user config. */ |
| 714 | (void)read_config_file(_PATH_HOST_CONFIG_FILE, host, |
| 715 | &options, 0); |
| 716 | } |
| 717 | |
| 718 | /* Fill configuration defaults. */ |
| 719 | fill_default_options(&options); |
| 720 | |
| 721 | channel_set_af(options.address_family); |
| 722 | |
| 723 | /* reinit */ |
| 724 | log_init(argv0, options.log_level, SYSLOG_FACILITY_USER, !use_syslog); |
| 725 | |
| 726 | seed_rng(); |
| 727 | |
| 728 | if (options.user == NULL) |
| 729 | options.user = xstrdup(pw->pw_name); |
| 730 | |
| 731 | /* Get default port if port has not been set. */ |
| 732 | if (options.port == 0) { |
| 733 | sp = getservbyname(SSH_SERVICE_NAME, "tcp"); |
| 734 | options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT; |
| 735 | } |
| 736 | |
| 737 | /* preserve host name given on command line for %n expansion */ |
| 738 | host_arg = host; |
| 739 | if (options.hostname != NULL) { |
| 740 | host = percent_expand(options.hostname, |
| 741 | "h", host, (char *)NULL); |
| 742 | } |
| 743 | |
| 744 | if (gethostname(thishost, sizeof(thishost)) == -1) |
| 745 | fatal("gethostname: %s", strerror(errno)); |
| 746 | strlcpy(shorthost, thishost, sizeof(shorthost)); |
| 747 | shorthost[strcspn(thishost, ".")] = '\0'; |
| 748 | snprintf(portstr, sizeof(portstr), "%d", options.port); |
| 749 | |
| 750 | if (options.local_command != NULL) { |
| 751 | debug3("expanding LocalCommand: %s", options.local_command); |
| 752 | cp = options.local_command; |
| 753 | options.local_command = percent_expand(cp, "d", pw->pw_dir, |
| 754 | "h", host, "l", thishost, "n", host_arg, "r", options.user, |
| 755 | "p", portstr, "u", pw->pw_name, "L", shorthost, |
| 756 | (char *)NULL); |
| 757 | debug3("expanded LocalCommand: %s", options.local_command); |
| 758 | xfree(cp); |
| 759 | } |
| 760 | |
| 761 | /* force lowercase for hostkey matching */ |
| 762 | if (options.host_key_alias != NULL) { |
| 763 | for (p = options.host_key_alias; *p; p++) |
| 764 | if (isupper(*p)) |
| 765 | *p = (char)tolower(*p); |
| 766 | } |
| 767 | |
| 768 | if (options.proxy_command != NULL && |
| 769 | strcmp(options.proxy_command, "none") == 0) { |
| 770 | xfree(options.proxy_command); |
| 771 | options.proxy_command = NULL; |
| 772 | } |
| 773 | if (options.control_path != NULL && |
| 774 | strcmp(options.control_path, "none") == 0) { |
| 775 | xfree(options.control_path); |
| 776 | options.control_path = NULL; |
| 777 | } |
| 778 | |
| 779 | if (options.control_path != NULL) { |
| 780 | cp = tilde_expand_filename(options.control_path, |
| 781 | original_real_uid); |
| 782 | xfree(options.control_path); |
| 783 | options.control_path = percent_expand(cp, "h", host, |
| 784 | "l", thishost, "n", host_arg, "r", options.user, |
| 785 | "p", portstr, "u", pw->pw_name, "L", shorthost, |
| 786 | (char *)NULL); |
| 787 | xfree(cp); |
| 788 | } |
| 789 | if (muxclient_command != 0 && options.control_path == NULL) |
| 790 | fatal("No ControlPath specified for \"-O\" command"); |
| 791 | if (options.control_path != NULL) |
| 792 | muxclient(options.control_path); |
| 793 | |
| 794 | timeout_ms = options.connection_timeout * 1000; |
| 795 | |
| 796 | /* Open a connection to the remote host. */ |
| 797 | if (ssh_connect(host, &hostaddr, options.port, |
| 798 | options.address_family, options.connection_attempts, &timeout_ms, |
| 799 | options.tcp_keep_alive, |
| 800 | #ifdef HAVE_CYGWIN |
| 801 | options.use_privileged_port, |
| 802 | #else |
| 803 | original_effective_uid == 0 && options.use_privileged_port, |
| 804 | #endif |
| 805 | options.proxy_command) != 0) |
| 806 | exit(255); |
| 807 | |
| 808 | if (timeout_ms > 0) |
| 809 | debug3("timeout: %d ms remain after connect", timeout_ms); |
| 810 | |
| 811 | /* |
| 812 | * If we successfully made the connection, load the host private key |
| 813 | * in case we will need it later for combined rsa-rhosts |
| 814 | * authentication. This must be done before releasing extra |
| 815 | * privileges, because the file is only readable by root. |
| 816 | * If we cannot access the private keys, load the public keys |
| 817 | * instead and try to execute the ssh-keysign helper instead. |
| 818 | */ |
| 819 | sensitive_data.nkeys = 0; |
| 820 | sensitive_data.keys = NULL; |
| 821 | sensitive_data.external_keysign = 0; |
| 822 | if (options.rhosts_rsa_authentication || |
| 823 | options.hostbased_authentication) { |
| 824 | sensitive_data.nkeys = 7; |
| 825 | sensitive_data.keys = xcalloc(sensitive_data.nkeys, |
| 826 | sizeof(Key)); |
| 827 | for (i = 0; i < sensitive_data.nkeys; i++) |
| 828 | sensitive_data.keys[i] = NULL; |
| 829 | |
| 830 | PRIV_START; |
| 831 | sensitive_data.keys[0] = key_load_private_type(KEY_RSA1, |
| 832 | _PATH_HOST_KEY_FILE, "", NULL, NULL); |
| 833 | sensitive_data.keys[1] = key_load_private_cert(KEY_DSA, |
| 834 | _PATH_HOST_DSA_KEY_FILE, "", NULL); |
| 835 | #ifdef OPENSSL_HAS_ECC |
| 836 | sensitive_data.keys[2] = key_load_private_cert(KEY_ECDSA, |
| 837 | _PATH_HOST_ECDSA_KEY_FILE, "", NULL); |
| 838 | #endif |
| 839 | sensitive_data.keys[3] = key_load_private_cert(KEY_RSA, |
| 840 | _PATH_HOST_RSA_KEY_FILE, "", NULL); |
| 841 | sensitive_data.keys[4] = key_load_private_type(KEY_DSA, |
| 842 | _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL); |
| 843 | #ifdef OPENSSL_HAS_ECC |
| 844 | sensitive_data.keys[5] = key_load_private_type(KEY_ECDSA, |
| 845 | _PATH_HOST_ECDSA_KEY_FILE, "", NULL, NULL); |
| 846 | #endif |
| 847 | sensitive_data.keys[6] = key_load_private_type(KEY_RSA, |
| 848 | _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL); |
| 849 | PRIV_END; |
| 850 | |
| 851 | if (options.hostbased_authentication == 1 && |
| 852 | sensitive_data.keys[0] == NULL && |
| 853 | sensitive_data.keys[4] == NULL && |
| 854 | sensitive_data.keys[5] == NULL && |
| 855 | sensitive_data.keys[6] == NULL) { |
| 856 | sensitive_data.keys[1] = key_load_cert( |
| 857 | _PATH_HOST_DSA_KEY_FILE); |
| 858 | #ifdef OPENSSL_HAS_ECC |
| 859 | sensitive_data.keys[2] = key_load_cert( |
| 860 | _PATH_HOST_ECDSA_KEY_FILE); |
| 861 | #endif |
| 862 | sensitive_data.keys[3] = key_load_cert( |
| 863 | _PATH_HOST_RSA_KEY_FILE); |
| 864 | sensitive_data.keys[4] = key_load_public( |
| 865 | _PATH_HOST_DSA_KEY_FILE, NULL); |
| 866 | #ifdef OPENSSL_HAS_ECC |
| 867 | sensitive_data.keys[5] = key_load_public( |
| 868 | _PATH_HOST_ECDSA_KEY_FILE, NULL); |
| 869 | #endif |
| 870 | sensitive_data.keys[6] = key_load_public( |
| 871 | _PATH_HOST_RSA_KEY_FILE, NULL); |
| 872 | sensitive_data.external_keysign = 1; |
| 873 | } |
| 874 | } |
| 875 | /* |
| 876 | * Get rid of any extra privileges that we may have. We will no |
| 877 | * longer need them. Also, extra privileges could make it very hard |
| 878 | * to read identity files and other non-world-readable files from the |
| 879 | * user's home directory if it happens to be on a NFS volume where |
| 880 | * root is mapped to nobody. |
| 881 | */ |
| 882 | if (original_effective_uid == 0) { |
| 883 | PRIV_START; |
| 884 | permanently_set_uid(pw); |
| 885 | } |
| 886 | |
| 887 | /* |
| 888 | * Now that we are back to our own permissions, create ~/.ssh |
| 889 | * directory if it doesn't already exist. |
| 890 | */ |
| 891 | r = snprintf(buf, sizeof buf, "%s%s%s", pw->pw_dir, |
| 892 | strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR); |
| 893 | if (r > 0 && (size_t)r < sizeof(buf) && stat(buf, &st) < 0) { |
| 894 | #ifdef WITH_SELINUX |
| 895 | ssh_selinux_setfscreatecon(buf); |
| 896 | #endif |
| 897 | if (mkdir(buf, 0700) < 0) |
| 898 | error("Could not create directory '%.200s'.", buf); |
| 899 | #ifdef WITH_SELINUX |
| 900 | ssh_selinux_setfscreatecon(NULL); |
| 901 | #endif |
| 902 | } |
| 903 | /* load options.identity_files */ |
| 904 | load_public_identity_files(); |
| 905 | |
| 906 | /* Expand ~ in known host file names. */ |
| 907 | tilde_expand_paths(options.system_hostfiles, |
| 908 | options.num_system_hostfiles); |
| 909 | tilde_expand_paths(options.user_hostfiles, options.num_user_hostfiles); |
| 910 | |
| 911 | signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */ |
| 912 | signal(SIGCHLD, main_sigchld_handler); |
| 913 | |
| 914 | /* Log into the remote system. Never returns if the login fails. */ |
| 915 | ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, |
| 916 | options.port, pw, timeout_ms); |
| 917 | |
| 918 | if (packet_connection_is_on_socket()) { |
| 919 | verbose("Authenticated to %s ([%s]:%d).", host, |
| 920 | get_remote_ipaddr(), get_remote_port()); |
| 921 | } else { |
| 922 | verbose("Authenticated to %s (via proxy).", host); |
| 923 | } |
| 924 | |
| 925 | /* We no longer need the private host keys. Clear them now. */ |
| 926 | if (sensitive_data.nkeys != 0) { |
| 927 | for (i = 0; i < sensitive_data.nkeys; i++) { |
| 928 | if (sensitive_data.keys[i] != NULL) { |
| 929 | /* Destroys contents safely */ |
| 930 | debug3("clear hostkey %d", i); |
| 931 | key_free(sensitive_data.keys[i]); |
| 932 | sensitive_data.keys[i] = NULL; |
| 933 | } |
| 934 | } |
| 935 | xfree(sensitive_data.keys); |
| 936 | } |
| 937 | for (i = 0; i < options.num_identity_files; i++) { |
| 938 | if (options.identity_files[i]) { |
| 939 | xfree(options.identity_files[i]); |
| 940 | options.identity_files[i] = NULL; |
| 941 | } |
| 942 | if (options.identity_keys[i]) { |
| 943 | key_free(options.identity_keys[i]); |
| 944 | options.identity_keys[i] = NULL; |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | exit_status = compat20 ? ssh_session2() : ssh_session(); |
| 949 | packet_close(); |
| 950 | |
| 951 | if (options.control_path != NULL && muxserver_sock != -1) |
| 952 | unlink(options.control_path); |
| 953 | |
| 954 | /* Kill ProxyCommand if it is running. */ |
| 955 | ssh_kill_proxy_command(); |
| 956 | |
| 957 | return exit_status; |
| 958 | } |
| 959 | |
| 960 | static void |
| 961 | control_persist_detach(void) |
| 962 | { |
| 963 | pid_t pid; |
| 964 | int devnull; |
| 965 | |
| 966 | debug("%s: backgrounding master process", __func__); |
| 967 | |
| 968 | /* |
| 969 | * master (current process) into the background, and make the |
| 970 | * foreground process a client of the backgrounded master. |
| 971 | */ |
| 972 | switch ((pid = fork())) { |
| 973 | case -1: |
| 974 | fatal("%s: fork: %s", __func__, strerror(errno)); |
| 975 | case 0: |
| 976 | /* Child: master process continues mainloop */ |
| 977 | break; |
| 978 | default: |
| 979 | /* Parent: set up mux slave to connect to backgrounded master */ |
| 980 | debug2("%s: background process is %ld", __func__, (long)pid); |
| 981 | stdin_null_flag = ostdin_null_flag; |
| 982 | options.request_tty = orequest_tty; |
| 983 | tty_flag = otty_flag; |
| 984 | close(muxserver_sock); |
| 985 | muxserver_sock = -1; |
| 986 | options.control_master = SSHCTL_MASTER_NO; |
| 987 | muxclient(options.control_path); |
| 988 | /* muxclient() doesn't return on success. */ |
| 989 | fatal("Failed to connect to new control master"); |
| 990 | } |
| 991 | if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) { |
| 992 | error("%s: open(\"/dev/null\"): %s", __func__, |
| 993 | strerror(errno)); |
| 994 | } else { |
| 995 | if (dup2(devnull, STDIN_FILENO) == -1 || |
| 996 | dup2(devnull, STDOUT_FILENO) == -1) |
| 997 | error("%s: dup2: %s", __func__, strerror(errno)); |
| 998 | if (devnull > STDERR_FILENO) |
| 999 | close(devnull); |
| 1000 | } |
| 1001 | setproctitle("%s [mux]", options.control_path); |
| 1002 | } |
| 1003 | |
| 1004 | /* Do fork() after authentication. Used by "ssh -f" */ |
| 1005 | static void |
| 1006 | fork_postauth(void) |
| 1007 | { |
| 1008 | if (need_controlpersist_detach) |
| 1009 | control_persist_detach(); |
| 1010 | debug("forking to background"); |
| 1011 | fork_after_authentication_flag = 0; |
| 1012 | if (daemon(1, 1) < 0) |
| 1013 | fatal("daemon() failed: %.200s", strerror(errno)); |
| 1014 | } |
| 1015 | |
| 1016 | /* Callback for remote forward global requests */ |
| 1017 | static void |
| 1018 | ssh_confirm_remote_forward(int type, u_int32_t seq, void *ctxt) |
| 1019 | { |
| 1020 | Forward *rfwd = (Forward *)ctxt; |
| 1021 | |
| 1022 | /* XXX verbose() on failure? */ |
| 1023 | debug("remote forward %s for: listen %d, connect %s:%d", |
| 1024 | type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure", |
| 1025 | rfwd->listen_port, rfwd->connect_host, rfwd->connect_port); |
| 1026 | if (type == SSH2_MSG_REQUEST_SUCCESS && rfwd->listen_port == 0) { |
| 1027 | rfwd->allocated_port = packet_get_int(); |
| 1028 | logit("Allocated port %u for remote forward to %s:%d", |
| 1029 | rfwd->allocated_port, |
| 1030 | rfwd->connect_host, rfwd->connect_port); |
| 1031 | } |
| 1032 | |
| 1033 | if (type == SSH2_MSG_REQUEST_FAILURE) { |
| 1034 | if (options.exit_on_forward_failure) |
| 1035 | fatal("Error: remote port forwarding failed for " |
| 1036 | "listen port %d", rfwd->listen_port); |
| 1037 | else |
| 1038 | logit("Warning: remote port forwarding failed for " |
| 1039 | "listen port %d", rfwd->listen_port); |
| 1040 | } |
| 1041 | if (++remote_forward_confirms_received == options.num_remote_forwards) { |
| 1042 | debug("All remote forwarding requests processed"); |
| 1043 | if (fork_after_authentication_flag) |
| 1044 | fork_postauth(); |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | static void |
| 1049 | client_cleanup_stdio_fwd(int id, void *arg) |
| 1050 | { |
| 1051 | debug("stdio forwarding: done"); |
| 1052 | cleanup_exit(0); |
| 1053 | } |
| 1054 | |
| 1055 | static int |
| 1056 | client_setup_stdio_fwd(const char *host_to_connect, u_short port_to_connect) |
| 1057 | { |
| 1058 | Channel *c; |
| 1059 | int in, out; |
| 1060 | |
| 1061 | debug3("client_setup_stdio_fwd %s:%d", host_to_connect, |
| 1062 | port_to_connect); |
| 1063 | |
| 1064 | in = dup(STDIN_FILENO); |
| 1065 | out = dup(STDOUT_FILENO); |
| 1066 | if (in < 0 || out < 0) |
| 1067 | fatal("channel_connect_stdio_fwd: dup() in/out failed"); |
| 1068 | |
| 1069 | if ((c = channel_connect_stdio_fwd(host_to_connect, port_to_connect, |
| 1070 | in, out)) == NULL) |
| 1071 | return 0; |
| 1072 | channel_register_cleanup(c->self, client_cleanup_stdio_fwd, 0); |
| 1073 | return 1; |
| 1074 | } |
| 1075 | |
| 1076 | static void |
| 1077 | ssh_init_forwarding(void) |
| 1078 | { |
| 1079 | int success = 0; |
| 1080 | int i; |
| 1081 | |
| 1082 | if (stdio_forward_host != NULL) { |
| 1083 | if (!compat20) { |
| 1084 | fatal("stdio forwarding require Protocol 2"); |
| 1085 | } |
| 1086 | if (!client_setup_stdio_fwd(stdio_forward_host, |
| 1087 | stdio_forward_port)) |
| 1088 | fatal("Failed to connect in stdio forward mode."); |
| 1089 | } |
| 1090 | |
| 1091 | /* Initiate local TCP/IP port forwardings. */ |
| 1092 | for (i = 0; i < options.num_local_forwards; i++) { |
| 1093 | debug("Local connections to %.200s:%d forwarded to remote " |
| 1094 | "address %.200s:%d", |
| 1095 | (options.local_forwards[i].listen_host == NULL) ? |
| 1096 | (options.gateway_ports ? "*" : "LOCALHOST") : |
| 1097 | options.local_forwards[i].listen_host, |
| 1098 | options.local_forwards[i].listen_port, |
| 1099 | options.local_forwards[i].connect_host, |
| 1100 | options.local_forwards[i].connect_port); |
| 1101 | success += channel_setup_local_fwd_listener( |
| 1102 | options.local_forwards[i].listen_host, |
| 1103 | options.local_forwards[i].listen_port, |
| 1104 | options.local_forwards[i].connect_host, |
| 1105 | options.local_forwards[i].connect_port, |
| 1106 | options.gateway_ports); |
| 1107 | } |
| 1108 | if (i > 0 && success != i && options.exit_on_forward_failure) |
| 1109 | fatal("Could not request local forwarding."); |
| 1110 | if (i > 0 && success == 0) |
| 1111 | error("Could not request local forwarding."); |
| 1112 | |
| 1113 | /* Initiate remote TCP/IP port forwardings. */ |
| 1114 | for (i = 0; i < options.num_remote_forwards; i++) { |
| 1115 | debug("Remote connections from %.200s:%d forwarded to " |
| 1116 | "local address %.200s:%d", |
| 1117 | (options.remote_forwards[i].listen_host == NULL) ? |
| 1118 | "LOCALHOST" : options.remote_forwards[i].listen_host, |
| 1119 | options.remote_forwards[i].listen_port, |
| 1120 | options.remote_forwards[i].connect_host, |
| 1121 | options.remote_forwards[i].connect_port); |
| 1122 | if (channel_request_remote_forwarding( |
| 1123 | options.remote_forwards[i].listen_host, |
| 1124 | options.remote_forwards[i].listen_port, |
| 1125 | options.remote_forwards[i].connect_host, |
| 1126 | options.remote_forwards[i].connect_port) < 0) { |
| 1127 | if (options.exit_on_forward_failure) |
| 1128 | fatal("Could not request remote forwarding."); |
| 1129 | else |
| 1130 | logit("Warning: Could not request remote " |
| 1131 | "forwarding."); |
| 1132 | } |
| 1133 | client_register_global_confirm(ssh_confirm_remote_forward, |
| 1134 | &options.remote_forwards[i]); |
| 1135 | } |
| 1136 | |
| 1137 | /* Initiate tunnel forwarding. */ |
| 1138 | if (options.tun_open != SSH_TUNMODE_NO) { |
| 1139 | if (client_request_tun_fwd(options.tun_open, |
| 1140 | options.tun_local, options.tun_remote) == -1) { |
| 1141 | if (options.exit_on_forward_failure) |
| 1142 | fatal("Could not request tunnel forwarding."); |
| 1143 | else |
| 1144 | error("Could not request tunnel forwarding."); |
| 1145 | } |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | static void |
| 1150 | check_agent_present(void) |
| 1151 | { |
| 1152 | if (options.forward_agent) { |
| 1153 | /* Clear agent forwarding if we don't have an agent. */ |
| 1154 | if (!ssh_agent_present()) |
| 1155 | options.forward_agent = 0; |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | static int |
| 1160 | ssh_session(void) |
| 1161 | { |
| 1162 | int type; |
| 1163 | int interactive = 0; |
| 1164 | int have_tty = 0; |
| 1165 | struct winsize ws; |
| 1166 | char *cp; |
| 1167 | const char *display; |
| 1168 | |
| 1169 | /* Enable compression if requested. */ |
| 1170 | if (options.compression) { |
| 1171 | debug("Requesting compression at level %d.", |
| 1172 | options.compression_level); |
| 1173 | |
| 1174 | if (options.compression_level < 1 || |
| 1175 | options.compression_level > 9) |
| 1176 | fatal("Compression level must be from 1 (fast) to " |
| 1177 | "9 (slow, best)."); |
| 1178 | |
| 1179 | /* Send the request. */ |
| 1180 | packet_start(SSH_CMSG_REQUEST_COMPRESSION); |
| 1181 | packet_put_int(options.compression_level); |
| 1182 | packet_send(); |
| 1183 | packet_write_wait(); |
| 1184 | type = packet_read(); |
| 1185 | if (type == SSH_SMSG_SUCCESS) |
| 1186 | packet_start_compression(options.compression_level); |
| 1187 | else if (type == SSH_SMSG_FAILURE) |
| 1188 | logit("Warning: Remote host refused compression."); |
| 1189 | else |
| 1190 | packet_disconnect("Protocol error waiting for " |
| 1191 | "compression response."); |
| 1192 | } |
| 1193 | /* Allocate a pseudo tty if appropriate. */ |
| 1194 | if (tty_flag) { |
| 1195 | debug("Requesting pty."); |
| 1196 | |
| 1197 | /* Start the packet. */ |
| 1198 | packet_start(SSH_CMSG_REQUEST_PTY); |
| 1199 | |
| 1200 | /* Store TERM in the packet. There is no limit on the |
| 1201 | length of the string. */ |
| 1202 | cp = getenv("TERM"); |
| 1203 | if (!cp) |
| 1204 | cp = ""; |
| 1205 | packet_put_cstring(cp); |
| 1206 | |
| 1207 | /* Store window size in the packet. */ |
| 1208 | if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0) |
| 1209 | memset(&ws, 0, sizeof(ws)); |
| 1210 | packet_put_int((u_int)ws.ws_row); |
| 1211 | packet_put_int((u_int)ws.ws_col); |
| 1212 | packet_put_int((u_int)ws.ws_xpixel); |
| 1213 | packet_put_int((u_int)ws.ws_ypixel); |
| 1214 | |
| 1215 | /* Store tty modes in the packet. */ |
| 1216 | tty_make_modes(fileno(stdin), NULL); |
| 1217 | |
| 1218 | /* Send the packet, and wait for it to leave. */ |
| 1219 | packet_send(); |
| 1220 | packet_write_wait(); |
| 1221 | |
| 1222 | /* Read response from the server. */ |
| 1223 | type = packet_read(); |
| 1224 | if (type == SSH_SMSG_SUCCESS) { |
| 1225 | interactive = 1; |
| 1226 | have_tty = 1; |
| 1227 | } else if (type == SSH_SMSG_FAILURE) |
| 1228 | logit("Warning: Remote host failed or refused to " |
| 1229 | "allocate a pseudo tty."); |
| 1230 | else |
| 1231 | packet_disconnect("Protocol error waiting for pty " |
| 1232 | "request response."); |
| 1233 | } |
| 1234 | /* Request X11 forwarding if enabled and DISPLAY is set. */ |
| 1235 | display = getenv("DISPLAY"); |
| 1236 | if (options.forward_x11 && display != NULL) { |
| 1237 | char *proto, *data; |
| 1238 | /* Get reasonable local authentication information. */ |
| 1239 | client_x11_get_proto(display, options.xauth_location, |
| 1240 | options.forward_x11_trusted, |
| 1241 | options.forward_x11_timeout, |
| 1242 | &proto, &data); |
| 1243 | /* Request forwarding with authentication spoofing. */ |
| 1244 | debug("Requesting X11 forwarding with authentication " |
| 1245 | "spoofing."); |
| 1246 | x11_request_forwarding_with_spoofing(0, display, proto, |
| 1247 | data, 0); |
| 1248 | /* Read response from the server. */ |
| 1249 | type = packet_read(); |
| 1250 | if (type == SSH_SMSG_SUCCESS) { |
| 1251 | interactive = 1; |
| 1252 | } else if (type == SSH_SMSG_FAILURE) { |
| 1253 | logit("Warning: Remote host denied X11 forwarding."); |
| 1254 | } else { |
| 1255 | packet_disconnect("Protocol error waiting for X11 " |
| 1256 | "forwarding"); |
| 1257 | } |
| 1258 | } |
| 1259 | /* Tell the packet module whether this is an interactive session. */ |
| 1260 | packet_set_interactive(interactive, |
| 1261 | options.ip_qos_interactive, options.ip_qos_bulk); |
| 1262 | |
| 1263 | /* Request authentication agent forwarding if appropriate. */ |
| 1264 | check_agent_present(); |
| 1265 | |
| 1266 | if (options.forward_agent) { |
| 1267 | debug("Requesting authentication agent forwarding."); |
| 1268 | auth_request_forwarding(); |
| 1269 | |
| 1270 | /* Read response from the server. */ |
| 1271 | type = packet_read(); |
| 1272 | packet_check_eom(); |
| 1273 | if (type != SSH_SMSG_SUCCESS) |
| 1274 | logit("Warning: Remote host denied authentication agent forwarding."); |
| 1275 | } |
| 1276 | |
| 1277 | /* Initiate port forwardings. */ |
| 1278 | ssh_init_forwarding(); |
| 1279 | |
| 1280 | /* Execute a local command */ |
| 1281 | if (options.local_command != NULL && |
| 1282 | options.permit_local_command) |
| 1283 | ssh_local_cmd(options.local_command); |
| 1284 | |
| 1285 | /* |
| 1286 | * If requested and we are not interested in replies to remote |
| 1287 | * forwarding requests, then let ssh continue in the background. |
| 1288 | */ |
| 1289 | if (fork_after_authentication_flag) { |
| 1290 | if (options.exit_on_forward_failure && |
| 1291 | options.num_remote_forwards > 0) { |
| 1292 | debug("deferring postauth fork until remote forward " |
| 1293 | "confirmation received"); |
| 1294 | } else |
| 1295 | fork_postauth(); |
| 1296 | } |
| 1297 | |
| 1298 | /* |
| 1299 | * If a command was specified on the command line, execute the |
| 1300 | * command now. Otherwise request the server to start a shell. |
| 1301 | */ |
| 1302 | if (buffer_len(&command) > 0) { |
| 1303 | int len = buffer_len(&command); |
| 1304 | if (len > 900) |
| 1305 | len = 900; |
| 1306 | debug("Sending command: %.*s", len, |
| 1307 | (u_char *)buffer_ptr(&command)); |
| 1308 | packet_start(SSH_CMSG_EXEC_CMD); |
| 1309 | packet_put_string(buffer_ptr(&command), buffer_len(&command)); |
| 1310 | packet_send(); |
| 1311 | packet_write_wait(); |
| 1312 | } else { |
| 1313 | debug("Requesting shell."); |
| 1314 | packet_start(SSH_CMSG_EXEC_SHELL); |
| 1315 | packet_send(); |
| 1316 | packet_write_wait(); |
| 1317 | } |
| 1318 | |
| 1319 | /* Enter the interactive session. */ |
| 1320 | return client_loop(have_tty, tty_flag ? |
| 1321 | options.escape_char : SSH_ESCAPECHAR_NONE, 0); |
| 1322 | } |
| 1323 | |
| 1324 | /* request pty/x11/agent/tcpfwd/shell for channel */ |
| 1325 | static void |
| 1326 | ssh_session2_setup(int id, int success, void *arg) |
| 1327 | { |
| 1328 | extern char **environ; |
| 1329 | const char *display; |
| 1330 | int interactive = tty_flag; |
| 1331 | |
| 1332 | if (!success) |
| 1333 | return; /* No need for error message, channels code sens one */ |
| 1334 | |
| 1335 | display = getenv("DISPLAY"); |
| 1336 | if (options.forward_x11 && display != NULL) { |
| 1337 | char *proto, *data; |
| 1338 | /* Get reasonable local authentication information. */ |
| 1339 | client_x11_get_proto(display, options.xauth_location, |
| 1340 | options.forward_x11_trusted, |
| 1341 | options.forward_x11_timeout, &proto, &data); |
| 1342 | /* Request forwarding with authentication spoofing. */ |
| 1343 | debug("Requesting X11 forwarding with authentication " |
| 1344 | "spoofing."); |
| 1345 | x11_request_forwarding_with_spoofing(id, display, proto, |
| 1346 | data, 1); |
| 1347 | client_expect_confirm(id, "X11 forwarding", CONFIRM_WARN); |
| 1348 | /* XXX exit_on_forward_failure */ |
| 1349 | interactive = 1; |
| 1350 | } |
| 1351 | |
| 1352 | check_agent_present(); |
| 1353 | if (options.forward_agent) { |
| 1354 | debug("Requesting authentication agent forwarding."); |
| 1355 | channel_request_start(id, "auth-agent-req@openssh.com", 0); |
| 1356 | packet_send(); |
| 1357 | } |
| 1358 | |
| 1359 | client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"), |
| 1360 | NULL, fileno(stdin), &command, environ); |
| 1361 | } |
| 1362 | |
| 1363 | /* open new channel for a session */ |
| 1364 | static int |
| 1365 | ssh_session2_open(void) |
| 1366 | { |
| 1367 | Channel *c; |
| 1368 | int window, packetmax, in, out, err; |
| 1369 | int sock; |
| 1370 | int socksize; |
| 1371 | int socksizelen = sizeof(int); |
| 1372 | |
| 1373 | if (stdin_null_flag) { |
| 1374 | in = open(_PATH_DEVNULL, O_RDONLY); |
| 1375 | } else { |
| 1376 | in = dup(STDIN_FILENO); |
| 1377 | } |
| 1378 | out = dup(STDOUT_FILENO); |
| 1379 | err = dup(STDERR_FILENO); |
| 1380 | |
| 1381 | if (in < 0 || out < 0 || err < 0) |
| 1382 | fatal("dup() in/out/err failed"); |
| 1383 | |
| 1384 | /* enable nonblocking unless tty */ |
| 1385 | if (!isatty(in)) |
| 1386 | set_nonblock(in); |
| 1387 | if (!isatty(out)) |
| 1388 | set_nonblock(out); |
| 1389 | if (!isatty(err)) |
| 1390 | set_nonblock(err); |
| 1391 | |
| 1392 | /* we need to check to see if what they want to do about buffer */ |
| 1393 | /* sizes here. In a hpn to nonhpn connection we want to limit */ |
| 1394 | /* the window size to something reasonable in case the far side */ |
| 1395 | /* has the large window bug. In hpn to hpn connection we want to */ |
| 1396 | /* use the max window size but allow the user to override it */ |
| 1397 | /* lastly if they disabled hpn then use the ssh std window size */ |
| 1398 | |
| 1399 | /* so why don't we just do a getsockopt() here and set the */ |
| 1400 | /* ssh window to that? In the case of a autotuning receive */ |
| 1401 | /* window the window would get stuck at the initial buffer */ |
| 1402 | /* size generally less than 96k. Therefore we need to set the */ |
| 1403 | /* maximum ssh window size to the maximum hpn buffer size */ |
| 1404 | /* unless the user has specifically set the tcprcvbufpoll */ |
| 1405 | /* to no. In which case we *can* just set the window to the */ |
| 1406 | /* minimum of the hpn buffer size and tcp receive buffer size */ |
| 1407 | |
| 1408 | if (tty_flag) |
| 1409 | options.hpn_buffer_size = CHAN_SES_WINDOW_DEFAULT; |
| 1410 | else |
| 1411 | options.hpn_buffer_size = 2*1024*1024; |
| 1412 | |
| 1413 | if (datafellows & SSH_BUG_LARGEWINDOW) |
| 1414 | { |
| 1415 | debug("HPN to Non-HPN Connection"); |
| 1416 | } |
| 1417 | else |
| 1418 | { |
| 1419 | if (options.tcp_rcv_buf_poll <= 0) |
| 1420 | { |
| 1421 | sock = socket(AF_INET, SOCK_STREAM, 0); |
| 1422 | getsockopt(sock, SOL_SOCKET, SO_RCVBUF, |
| 1423 | &socksize, &socksizelen); |
| 1424 | close(sock); |
| 1425 | debug("socksize %d", socksize); |
| 1426 | options.hpn_buffer_size = socksize; |
| 1427 | debug ("HPNBufferSize set to TCP RWIN: %d", options.hpn_buffer_size); |
| 1428 | } |
| 1429 | else |
| 1430 | { |
| 1431 | if (options.tcp_rcv_buf > 0) |
| 1432 | { |
| 1433 | /*create a socket but don't connect it */ |
| 1434 | /* we use that the get the rcv socket size */ |
| 1435 | sock = socket(AF_INET, SOCK_STREAM, 0); |
| 1436 | /* if they are using the tcp_rcv_buf option */ |
| 1437 | /* attempt to set the buffer size to that */ |
| 1438 | if (options.tcp_rcv_buf) |
| 1439 | setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (void *)&options.tcp_rcv_buf, |
| 1440 | sizeof(options.tcp_rcv_buf)); |
| 1441 | getsockopt(sock, SOL_SOCKET, SO_RCVBUF, |
| 1442 | &socksize, &socksizelen); |
| 1443 | close(sock); |
| 1444 | debug("socksize %d", socksize); |
| 1445 | options.hpn_buffer_size = socksize; |
| 1446 | debug ("HPNBufferSize set to user TCPRcvBuf: %d", options.hpn_buffer_size); |
| 1447 | } |
| 1448 | } |
| 1449 | |
| 1450 | } |
| 1451 | |
| 1452 | debug("Final hpn_buffer_size = %d", options.hpn_buffer_size); |
| 1453 | |
| 1454 | window = options.hpn_buffer_size; |
| 1455 | |
| 1456 | channel_set_hpn(options.hpn_disabled, options.hpn_buffer_size); |
| 1457 | |
| 1458 | packetmax = CHAN_SES_PACKET_DEFAULT; |
| 1459 | if (tty_flag) { |
| 1460 | window = 4*CHAN_SES_PACKET_DEFAULT; |
| 1461 | window >>= 1; |
| 1462 | packetmax >>= 1; |
| 1463 | } |
| 1464 | c = channel_new( |
| 1465 | "session", SSH_CHANNEL_OPENING, in, out, err, |
| 1466 | window, packetmax, CHAN_EXTENDED_WRITE, |
| 1467 | "client-session", /*nonblock*/0); |
| 1468 | if ((options.tcp_rcv_buf_poll > 0) && (!options.hpn_disabled)) { |
| 1469 | c->dynamic_window = 1; |
| 1470 | debug ("Enabled Dynamic Window Scaling\n"); |
| 1471 | } |
| 1472 | debug3("ssh_session2_open: channel_new: %d", c->self); |
| 1473 | |
| 1474 | channel_send_open(c->self); |
| 1475 | if (!no_shell_flag) |
| 1476 | channel_register_open_confirm(c->self, |
| 1477 | ssh_session2_setup, NULL); |
| 1478 | |
| 1479 | return c->self; |
| 1480 | } |
| 1481 | |
| 1482 | static int |
| 1483 | ssh_session2(void) |
| 1484 | { |
| 1485 | int id = -1; |
| 1486 | |
| 1487 | /* XXX should be pre-session */ |
| 1488 | ssh_init_forwarding(); |
| 1489 | |
| 1490 | /* Start listening for multiplex clients */ |
| 1491 | muxserver_listen(); |
| 1492 | |
| 1493 | /* |
| 1494 | * If we are in control persist mode, then prepare to background |
| 1495 | * ourselves and have a foreground client attach as a control |
| 1496 | * slave. NB. we must save copies of the flags that we override for |
| 1497 | * the backgrounding, since we defer attachment of the slave until |
| 1498 | * after the connection is fully established (in particular, |
| 1499 | * async rfwd replies have been received for ExitOnForwardFailure). |
| 1500 | */ |
| 1501 | if (options.control_persist && muxserver_sock != -1) { |
| 1502 | ostdin_null_flag = stdin_null_flag; |
| 1503 | ono_shell_flag = no_shell_flag; |
| 1504 | orequest_tty = options.request_tty; |
| 1505 | otty_flag = tty_flag; |
| 1506 | stdin_null_flag = 1; |
| 1507 | no_shell_flag = 1; |
| 1508 | tty_flag = 0; |
| 1509 | if (!fork_after_authentication_flag) |
| 1510 | need_controlpersist_detach = 1; |
| 1511 | fork_after_authentication_flag = 1; |
| 1512 | } |
| 1513 | |
| 1514 | if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN)) |
| 1515 | id = ssh_session2_open(); |
| 1516 | |
| 1517 | /* If we don't expect to open a new session, then disallow it */ |
| 1518 | if (options.control_master == SSHCTL_MASTER_NO && |
| 1519 | (datafellows & SSH_NEW_OPENSSH)) { |
| 1520 | debug("Requesting no-more-sessions@openssh.com"); |
| 1521 | packet_start(SSH2_MSG_GLOBAL_REQUEST); |
| 1522 | packet_put_cstring("no-more-sessions@openssh.com"); |
| 1523 | packet_put_char(0); |
| 1524 | packet_send(); |
| 1525 | } |
| 1526 | |
| 1527 | /* Execute a local command */ |
| 1528 | if (options.local_command != NULL && |
| 1529 | options.permit_local_command) |
| 1530 | ssh_local_cmd(options.local_command); |
| 1531 | |
| 1532 | /* |
| 1533 | * If requested and we are not interested in replies to remote |
| 1534 | * forwarding requests, then let ssh continue in the background. |
| 1535 | */ |
| 1536 | if (fork_after_authentication_flag) { |
| 1537 | if (options.exit_on_forward_failure && |
| 1538 | options.num_remote_forwards > 0) { |
| 1539 | debug("deferring postauth fork until remote forward " |
| 1540 | "confirmation received"); |
| 1541 | } else |
| 1542 | fork_postauth(); |
| 1543 | } |
| 1544 | |
| 1545 | if (options.use_roaming) |
| 1546 | request_roaming(); |
| 1547 | |
| 1548 | return client_loop(tty_flag, tty_flag ? |
| 1549 | options.escape_char : SSH_ESCAPECHAR_NONE, id); |
| 1550 | } |
| 1551 | |
| 1552 | static void |
| 1553 | load_public_identity_files(void) |
| 1554 | { |
| 1555 | char *filename, *cp, thishost[NI_MAXHOST]; |
| 1556 | char *pwdir = NULL, *pwname = NULL; |
| 1557 | int i = 0; |
| 1558 | Key *public; |
| 1559 | struct passwd *pw; |
| 1560 | u_int n_ids; |
| 1561 | char *identity_files[SSH_MAX_IDENTITY_FILES]; |
| 1562 | Key *identity_keys[SSH_MAX_IDENTITY_FILES]; |
| 1563 | #ifdef ENABLE_PKCS11 |
| 1564 | Key **keys; |
| 1565 | int nkeys; |
| 1566 | #endif /* PKCS11 */ |
| 1567 | |
| 1568 | n_ids = 0; |
| 1569 | bzero(identity_files, sizeof(identity_files)); |
| 1570 | bzero(identity_keys, sizeof(identity_keys)); |
| 1571 | |
| 1572 | #ifdef ENABLE_PKCS11 |
| 1573 | if (options.pkcs11_provider != NULL && |
| 1574 | options.num_identity_files < SSH_MAX_IDENTITY_FILES && |
| 1575 | (pkcs11_init(!options.batch_mode) == 0) && |
| 1576 | (nkeys = pkcs11_add_provider(options.pkcs11_provider, NULL, |
| 1577 | &keys)) > 0) { |
| 1578 | for (i = 0; i < nkeys; i++) { |
| 1579 | if (n_ids >= SSH_MAX_IDENTITY_FILES) { |
| 1580 | key_free(keys[i]); |
| 1581 | continue; |
| 1582 | } |
| 1583 | identity_keys[n_ids] = keys[i]; |
| 1584 | identity_files[n_ids] = |
| 1585 | xstrdup(options.pkcs11_provider); /* XXX */ |
| 1586 | n_ids++; |
| 1587 | } |
| 1588 | xfree(keys); |
| 1589 | } |
| 1590 | #endif /* ENABLE_PKCS11 */ |
| 1591 | if ((pw = getpwuid(original_real_uid)) == NULL) |
| 1592 | fatal("load_public_identity_files: getpwuid failed"); |
| 1593 | pwname = xstrdup(pw->pw_name); |
| 1594 | pwdir = xstrdup(pw->pw_dir); |
| 1595 | if (gethostname(thishost, sizeof(thishost)) == -1) |
| 1596 | fatal("load_public_identity_files: gethostname: %s", |
| 1597 | strerror(errno)); |
| 1598 | for (i = 0; i < options.num_identity_files; i++) { |
| 1599 | if (n_ids >= SSH_MAX_IDENTITY_FILES) { |
| 1600 | xfree(options.identity_files[i]); |
| 1601 | continue; |
| 1602 | } |
| 1603 | cp = tilde_expand_filename(options.identity_files[i], |
| 1604 | original_real_uid); |
| 1605 | filename = percent_expand(cp, "d", pwdir, |
| 1606 | "u", pwname, "l", thishost, "h", host, |
| 1607 | "r", options.user, (char *)NULL); |
| 1608 | xfree(cp); |
| 1609 | public = key_load_public(filename, NULL); |
| 1610 | debug("identity file %s type %d", filename, |
| 1611 | public ? public->type : -1); |
| 1612 | xfree(options.identity_files[i]); |
| 1613 | identity_files[n_ids] = filename; |
| 1614 | identity_keys[n_ids] = public; |
| 1615 | |
| 1616 | if (++n_ids >= SSH_MAX_IDENTITY_FILES) |
| 1617 | continue; |
| 1618 | |
| 1619 | /* Try to add the certificate variant too */ |
| 1620 | xasprintf(&cp, "%s-cert", filename); |
| 1621 | public = key_load_public(cp, NULL); |
| 1622 | debug("identity file %s type %d", cp, |
| 1623 | public ? public->type : -1); |
| 1624 | if (public == NULL) { |
| 1625 | xfree(cp); |
| 1626 | continue; |
| 1627 | } |
| 1628 | if (!key_is_cert(public)) { |
| 1629 | debug("%s: key %s type %s is not a certificate", |
| 1630 | __func__, cp, key_type(public)); |
| 1631 | key_free(public); |
| 1632 | xfree(cp); |
| 1633 | continue; |
| 1634 | } |
| 1635 | identity_keys[n_ids] = public; |
| 1636 | /* point to the original path, most likely the private key */ |
| 1637 | identity_files[n_ids] = xstrdup(filename); |
| 1638 | n_ids++; |
| 1639 | } |
| 1640 | options.num_identity_files = n_ids; |
| 1641 | memcpy(options.identity_files, identity_files, sizeof(identity_files)); |
| 1642 | memcpy(options.identity_keys, identity_keys, sizeof(identity_keys)); |
| 1643 | |
| 1644 | bzero(pwname, strlen(pwname)); |
| 1645 | xfree(pwname); |
| 1646 | bzero(pwdir, strlen(pwdir)); |
| 1647 | xfree(pwdir); |
| 1648 | } |
| 1649 | |
| 1650 | static void |
| 1651 | main_sigchld_handler(int sig) |
| 1652 | { |
| 1653 | int save_errno = errno; |
| 1654 | pid_t pid; |
| 1655 | int status; |
| 1656 | |
| 1657 | while ((pid = waitpid(-1, &status, WNOHANG)) > 0 || |
| 1658 | (pid < 0 && errno == EINTR)) |
| 1659 | ; |
| 1660 | |
| 1661 | signal(sig, main_sigchld_handler); |
| 1662 | errno = save_errno; |
| 1663 | } |
| 1664 | |