1 /* $OpenBSD: ssh.c,v 1.494 2018/10/03 06:38:35 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 <sys/types.h> 44 #include <sys/ioctl.h> 45 #include <sys/queue.h> 46 #include <sys/resource.h> 47 #include <sys/socket.h> 48 #include <sys/stat.h> 49 #include <sys/time.h> 50 #include <sys/wait.h> 51 52 #include <ctype.h> 53 #include <errno.h> 54 #include <fcntl.h> 55 #include <netdb.h> 56 #include <paths.h> 57 #include <pwd.h> 58 #include <signal.h> 59 #include <stddef.h> 60 #include <stdio.h> 61 #include <stdlib.h> 62 #include <string.h> 63 #include <unistd.h> 64 #include <limits.h> 65 #include <locale.h> 66 67 #ifdef WITH_OPENSSL 68 #include <openssl/evp.h> 69 #include <openssl/err.h> 70 #endif 71 72 #include "xmalloc.h" 73 #include "ssh.h" 74 #include "ssh2.h" 75 #include "canohost.h" 76 #include "compat.h" 77 #include "cipher.h" 78 #include "digest.h" 79 #include "packet.h" 80 #include "sshbuf.h" 81 #include "channels.h" 82 #include "sshkey.h" 83 #include "authfd.h" 84 #include "authfile.h" 85 #include "pathnames.h" 86 #include "dispatch.h" 87 #include "clientloop.h" 88 #include "log.h" 89 #include "misc.h" 90 #include "readconf.h" 91 #include "sshconnect.h" 92 #include "kex.h" 93 #include "mac.h" 94 #include "sshpty.h" 95 #include "match.h" 96 #include "msg.h" 97 #include "version.h" 98 #include "ssherr.h" 99 #include "myproposal.h" 100 101 #ifdef ENABLE_PKCS11 102 #include "ssh-pkcs11.h" 103 #endif 104 105 extern char *__progname; 106 107 /* Flag indicating whether debug mode is on. May be set on the command line. */ 108 int debug_flag = 0; 109 110 /* Flag indicating whether a tty should be requested */ 111 int tty_flag = 0; 112 113 /* don't exec a shell */ 114 int no_shell_flag = 0; 115 116 /* 117 * Flag indicating that nothing should be read from stdin. This can be set 118 * on the command line. 119 */ 120 int stdin_null_flag = 0; 121 122 /* 123 * Flag indicating that the current process should be backgrounded and 124 * a new slave launched in the foreground for ControlPersist. 125 */ 126 int need_controlpersist_detach = 0; 127 128 /* Copies of flags for ControlPersist foreground slave */ 129 int ostdin_null_flag, ono_shell_flag, otty_flag, orequest_tty; 130 131 /* 132 * Flag indicating that ssh should fork after authentication. This is useful 133 * so that the passphrase can be entered manually, and then ssh goes to the 134 * background. 135 */ 136 int fork_after_authentication_flag = 0; 137 138 /* 139 * General data structure for command line options and options configurable 140 * in configuration files. See readconf.h. 141 */ 142 Options options; 143 144 /* optional user configfile */ 145 char *config = NULL; 146 147 /* 148 * Name of the host we are connecting to. This is the name given on the 149 * command line, or the HostName specified for the user-supplied name in a 150 * configuration file. 151 */ 152 char *host; 153 154 /* Various strings used to to percent_expand() arguments */ 155 static char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV]; 156 static char uidstr[32], *host_arg, *conn_hash_hex; 157 158 /* socket address the host resolves to */ 159 struct sockaddr_storage hostaddr; 160 161 /* Private host keys. */ 162 Sensitive sensitive_data; 163 164 /* command to be executed */ 165 struct sshbuf *command; 166 167 /* Should we execute a command or invoke a subsystem? */ 168 int subsystem_flag = 0; 169 170 /* # of replies received for global requests */ 171 static int remote_forward_confirms_received = 0; 172 173 /* mux.c */ 174 extern int muxserver_sock; 175 extern u_int muxclient_command; 176 177 /* Prints a help message to the user. This function never returns. */ 178 179 static void 180 usage(void) 181 { 182 fprintf(stderr, 183 "usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-B bind_interface]\n" 184 " [-b bind_address] [-c cipher_spec] [-D [bind_address:]port]\n" 185 " [-E log_file] [-e escape_char] [-F configfile] [-I pkcs11]\n" 186 " [-i identity_file] [-J [user@]host[:port]] [-L address]\n" 187 " [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n" 188 " [-Q query_option] [-R address] [-S ctl_path] [-W host:port]\n" 189 " [-w local_tun[:remote_tun]] destination [command]\n" 190 ); 191 exit(255); 192 } 193 194 static int ssh_session2(struct ssh *, struct passwd *); 195 static void load_public_identity_files(struct passwd *); 196 static void main_sigchld_handler(int); 197 198 /* ~/ expand a list of paths. NB. assumes path[n] is heap-allocated. */ 199 static void 200 tilde_expand_paths(char **paths, u_int num_paths) 201 { 202 u_int i; 203 char *cp; 204 205 for (i = 0; i < num_paths; i++) { 206 cp = tilde_expand_filename(paths[i], getuid()); 207 free(paths[i]); 208 paths[i] = cp; 209 } 210 } 211 212 /* 213 * Attempt to resolve a host name / port to a set of addresses and 214 * optionally return any CNAMEs encountered along the way. 215 * Returns NULL on failure. 216 * NB. this function must operate with a options having undefined members. 217 */ 218 static struct addrinfo * 219 resolve_host(const char *name, int port, int logerr, char *cname, size_t clen) 220 { 221 char strport[NI_MAXSERV]; 222 struct addrinfo hints, *res; 223 int gaierr, loglevel = SYSLOG_LEVEL_DEBUG1; 224 225 if (port <= 0) 226 port = default_ssh_port(); 227 228 snprintf(strport, sizeof strport, "%d", port); 229 memset(&hints, 0, sizeof(hints)); 230 hints.ai_family = options.address_family == -1 ? 231 AF_UNSPEC : options.address_family; 232 hints.ai_socktype = SOCK_STREAM; 233 if (cname != NULL) 234 hints.ai_flags = AI_CANONNAME; 235 if ((gaierr = getaddrinfo(name, strport, &hints, &res)) != 0) { 236 if (logerr || (gaierr != EAI_NONAME && gaierr != EAI_NODATA)) 237 loglevel = SYSLOG_LEVEL_ERROR; 238 do_log2(loglevel, "%s: Could not resolve hostname %.100s: %s", 239 __progname, name, ssh_gai_strerror(gaierr)); 240 return NULL; 241 } 242 if (cname != NULL && res->ai_canonname != NULL) { 243 if (strlcpy(cname, res->ai_canonname, clen) >= clen) { 244 error("%s: host \"%s\" cname \"%s\" too long (max %lu)", 245 __func__, name, res->ai_canonname, (u_long)clen); 246 if (clen > 0) 247 *cname = '\0'; 248 } 249 } 250 return res; 251 } 252 253 /* Returns non-zero if name can only be an address and not a hostname */ 254 static int 255 is_addr_fast(const char *name) 256 { 257 return (strchr(name, '%') != NULL || strchr(name, ':') != NULL || 258 strspn(name, "0123456789.") == strlen(name)); 259 } 260 261 /* Returns non-zero if name represents a valid, single address */ 262 static int 263 is_addr(const char *name) 264 { 265 char strport[NI_MAXSERV]; 266 struct addrinfo hints, *res; 267 268 if (is_addr_fast(name)) 269 return 1; 270 271 snprintf(strport, sizeof strport, "%u", default_ssh_port()); 272 memset(&hints, 0, sizeof(hints)); 273 hints.ai_family = options.address_family == -1 ? 274 AF_UNSPEC : options.address_family; 275 hints.ai_socktype = SOCK_STREAM; 276 hints.ai_flags = AI_NUMERICHOST|AI_NUMERICSERV; 277 if (getaddrinfo(name, strport, &hints, &res) != 0) 278 return 0; 279 if (res == NULL || res->ai_next != NULL) { 280 freeaddrinfo(res); 281 return 0; 282 } 283 freeaddrinfo(res); 284 return 1; 285 } 286 287 /* 288 * Attempt to resolve a numeric host address / port to a single address. 289 * Returns a canonical address string. 290 * Returns NULL on failure. 291 * NB. this function must operate with a options having undefined members. 292 */ 293 static struct addrinfo * 294 resolve_addr(const char *name, int port, char *caddr, size_t clen) 295 { 296 char addr[NI_MAXHOST], strport[NI_MAXSERV]; 297 struct addrinfo hints, *res; 298 int gaierr; 299 300 if (port <= 0) 301 port = default_ssh_port(); 302 snprintf(strport, sizeof strport, "%u", port); 303 memset(&hints, 0, sizeof(hints)); 304 hints.ai_family = options.address_family == -1 ? 305 AF_UNSPEC : options.address_family; 306 hints.ai_socktype = SOCK_STREAM; 307 hints.ai_flags = AI_NUMERICHOST|AI_NUMERICSERV; 308 if ((gaierr = getaddrinfo(name, strport, &hints, &res)) != 0) { 309 debug2("%s: could not resolve name %.100s as address: %s", 310 __func__, name, ssh_gai_strerror(gaierr)); 311 return NULL; 312 } 313 if (res == NULL) { 314 debug("%s: getaddrinfo %.100s returned no addresses", 315 __func__, name); 316 return NULL; 317 } 318 if (res->ai_next != NULL) { 319 debug("%s: getaddrinfo %.100s returned multiple addresses", 320 __func__, name); 321 goto fail; 322 } 323 if ((gaierr = getnameinfo(res->ai_addr, res->ai_addrlen, 324 addr, sizeof(addr), NULL, 0, NI_NUMERICHOST)) != 0) { 325 debug("%s: Could not format address for name %.100s: %s", 326 __func__, name, ssh_gai_strerror(gaierr)); 327 goto fail; 328 } 329 if (strlcpy(caddr, addr, clen) >= clen) { 330 error("%s: host \"%s\" addr \"%s\" too long (max %lu)", 331 __func__, name, addr, (u_long)clen); 332 if (clen > 0) 333 *caddr = '\0'; 334 fail: 335 freeaddrinfo(res); 336 return NULL; 337 } 338 return res; 339 } 340 341 /* 342 * Check whether the cname is a permitted replacement for the hostname 343 * and perform the replacement if it is. 344 * NB. this function must operate with a options having undefined members. 345 */ 346 static int 347 check_follow_cname(int direct, char **namep, const char *cname) 348 { 349 int i; 350 struct allowed_cname *rule; 351 352 if (*cname == '\0' || options.num_permitted_cnames == 0 || 353 strcmp(*namep, cname) == 0) 354 return 0; 355 if (options.canonicalize_hostname == SSH_CANONICALISE_NO) 356 return 0; 357 /* 358 * Don't attempt to canonicalize names that will be interpreted by 359 * a proxy or jump host unless the user specifically requests so. 360 */ 361 if (!direct && 362 options.canonicalize_hostname != SSH_CANONICALISE_ALWAYS) 363 return 0; 364 debug3("%s: check \"%s\" CNAME \"%s\"", __func__, *namep, cname); 365 for (i = 0; i < options.num_permitted_cnames; i++) { 366 rule = options.permitted_cnames + i; 367 if (match_pattern_list(*namep, rule->source_list, 1) != 1 || 368 match_pattern_list(cname, rule->target_list, 1) != 1) 369 continue; 370 verbose("Canonicalized DNS aliased hostname " 371 "\"%s\" => \"%s\"", *namep, cname); 372 free(*namep); 373 *namep = xstrdup(cname); 374 return 1; 375 } 376 return 0; 377 } 378 379 /* 380 * Attempt to resolve the supplied hostname after applying the user's 381 * canonicalization rules. Returns the address list for the host or NULL 382 * if no name was found after canonicalization. 383 * NB. this function must operate with a options having undefined members. 384 */ 385 static struct addrinfo * 386 resolve_canonicalize(char **hostp, int port) 387 { 388 int i, direct, ndots; 389 char *cp, *fullhost, newname[NI_MAXHOST]; 390 struct addrinfo *addrs; 391 392 /* 393 * Attempt to canonicalise addresses, regardless of 394 * whether hostname canonicalisation was requested 395 */ 396 if ((addrs = resolve_addr(*hostp, port, 397 newname, sizeof(newname))) != NULL) { 398 debug2("%s: hostname %.100s is address", __func__, *hostp); 399 if (strcasecmp(*hostp, newname) != 0) { 400 debug2("%s: canonicalised address \"%s\" => \"%s\"", 401 __func__, *hostp, newname); 402 free(*hostp); 403 *hostp = xstrdup(newname); 404 } 405 return addrs; 406 } 407 408 /* 409 * If this looks like an address but didn't parse as one, it might 410 * be an address with an invalid interface scope. Skip further 411 * attempts at canonicalisation. 412 */ 413 if (is_addr_fast(*hostp)) { 414 debug("%s: hostname %.100s is an unrecognised address", 415 __func__, *hostp); 416 return NULL; 417 } 418 419 if (options.canonicalize_hostname == SSH_CANONICALISE_NO) 420 return NULL; 421 422 /* 423 * Don't attempt to canonicalize names that will be interpreted by 424 * a proxy unless the user specifically requests so. 425 */ 426 direct = option_clear_or_none(options.proxy_command) && 427 options.jump_host == NULL; 428 if (!direct && 429 options.canonicalize_hostname != SSH_CANONICALISE_ALWAYS) 430 return NULL; 431 432 /* If domain name is anchored, then resolve it now */ 433 if ((*hostp)[strlen(*hostp) - 1] == '.') { 434 debug3("%s: name is fully qualified", __func__); 435 fullhost = xstrdup(*hostp); 436 if ((addrs = resolve_host(fullhost, port, 0, 437 newname, sizeof(newname))) != NULL) 438 goto found; 439 free(fullhost); 440 goto notfound; 441 } 442 443 /* Don't apply canonicalization to sufficiently-qualified hostnames */ 444 ndots = 0; 445 for (cp = *hostp; *cp != '\0'; cp++) { 446 if (*cp == '.') 447 ndots++; 448 } 449 if (ndots > options.canonicalize_max_dots) { 450 debug3("%s: not canonicalizing hostname \"%s\" (max dots %d)", 451 __func__, *hostp, options.canonicalize_max_dots); 452 return NULL; 453 } 454 /* Attempt each supplied suffix */ 455 for (i = 0; i < options.num_canonical_domains; i++) { 456 *newname = '\0'; 457 xasprintf(&fullhost, "%s.%s.", *hostp, 458 options.canonical_domains[i]); 459 debug3("%s: attempting \"%s\" => \"%s\"", __func__, 460 *hostp, fullhost); 461 if ((addrs = resolve_host(fullhost, port, 0, 462 newname, sizeof(newname))) == NULL) { 463 free(fullhost); 464 continue; 465 } 466 found: 467 /* Remove trailing '.' */ 468 fullhost[strlen(fullhost) - 1] = '\0'; 469 /* Follow CNAME if requested */ 470 if (!check_follow_cname(direct, &fullhost, newname)) { 471 debug("Canonicalized hostname \"%s\" => \"%s\"", 472 *hostp, fullhost); 473 } 474 free(*hostp); 475 *hostp = fullhost; 476 return addrs; 477 } 478 notfound: 479 if (!options.canonicalize_fallback_local) 480 fatal("%s: Could not resolve host \"%s\"", __progname, *hostp); 481 debug2("%s: host %s not found in any suffix", __func__, *hostp); 482 return NULL; 483 } 484 485 /* 486 * Check the result of hostkey loading, ignoring some errors and 487 * fatal()ing for others. 488 */ 489 static void 490 check_load(int r, const char *path, const char *message) 491 { 492 switch (r) { 493 case 0: 494 break; 495 case SSH_ERR_INTERNAL_ERROR: 496 case SSH_ERR_ALLOC_FAIL: 497 fatal("load %s \"%s\": %s", message, path, ssh_err(r)); 498 case SSH_ERR_SYSTEM_ERROR: 499 /* Ignore missing files */ 500 if (errno == ENOENT) 501 break; 502 /* FALLTHROUGH */ 503 default: 504 error("load %s \"%s\": %s", message, path, ssh_err(r)); 505 break; 506 } 507 } 508 509 /* 510 * Read per-user configuration file. Ignore the system wide config 511 * file if the user specifies a config file on the command line. 512 */ 513 static void 514 process_config_files(const char *host_name, struct passwd *pw, int post_canon) 515 { 516 char buf[PATH_MAX]; 517 int r; 518 519 if (config != NULL) { 520 if (strcasecmp(config, "none") != 0 && 521 !read_config_file(config, pw, host, host_name, &options, 522 SSHCONF_USERCONF | (post_canon ? SSHCONF_POSTCANON : 0))) 523 fatal("Can't open user config file %.100s: " 524 "%.100s", config, strerror(errno)); 525 } else { 526 r = snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, 527 _PATH_SSH_USER_CONFFILE); 528 if (r > 0 && (size_t)r < sizeof(buf)) 529 (void)read_config_file(buf, pw, host, host_name, 530 &options, SSHCONF_CHECKPERM | SSHCONF_USERCONF | 531 (post_canon ? SSHCONF_POSTCANON : 0)); 532 533 /* Read systemwide configuration file after user config. */ 534 (void)read_config_file(_PATH_HOST_CONFIG_FILE, pw, 535 host, host_name, &options, 536 post_canon ? SSHCONF_POSTCANON : 0); 537 } 538 } 539 540 /* Rewrite the port number in an addrinfo list of addresses */ 541 static void 542 set_addrinfo_port(struct addrinfo *addrs, int port) 543 { 544 struct addrinfo *addr; 545 546 for (addr = addrs; addr != NULL; addr = addr->ai_next) { 547 switch (addr->ai_family) { 548 case AF_INET: 549 ((struct sockaddr_in *)addr->ai_addr)-> 550 sin_port = htons(port); 551 break; 552 case AF_INET6: 553 ((struct sockaddr_in6 *)addr->ai_addr)-> 554 sin6_port = htons(port); 555 break; 556 } 557 } 558 } 559 560 /* 561 * Main program for the ssh client. 562 */ 563 int 564 main(int ac, char **av) 565 { 566 struct ssh *ssh = NULL; 567 int i, r, opt, exit_status, use_syslog, direct, timeout_ms; 568 int was_addr, config_test = 0, opt_terminated = 0; 569 char *p, *cp, *line, *argv0, buf[PATH_MAX], *logfile; 570 char cname[NI_MAXHOST]; 571 struct stat st; 572 struct passwd *pw; 573 extern int optind, optreset; 574 extern char *optarg; 575 struct Forward fwd; 576 struct addrinfo *addrs = NULL; 577 struct ssh_digest_ctx *md; 578 u_char conn_hash[SSH_DIGEST_MAX_LENGTH]; 579 580 ssh_malloc_init(); /* must be called before any mallocs */ 581 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ 582 sanitise_stdfd(); 583 584 /* 585 * Discard other fds that are hanging around. These can cause problem 586 * with backgrounded ssh processes started by ControlPersist. 587 */ 588 closefrom(STDERR_FILENO + 1); 589 590 if (getuid() != geteuid()) 591 fatal("ssh setuid not supported."); 592 if (getgid() != getegid()) 593 fatal("ssh setgid not supported."); 594 595 /* Get user data. */ 596 pw = getpwuid(getuid()); 597 if (!pw) { 598 logit("No user exists for uid %lu", (u_long)getuid()); 599 exit(255); 600 } 601 /* Take a copy of the returned structure. */ 602 pw = pwcopy(pw); 603 604 /* 605 * Set our umask to something reasonable, as some files are created 606 * with the default umask. This will make them world-readable but 607 * writable only by the owner, which is ok for all files for which we 608 * don't set the modes explicitly. 609 */ 610 umask(022); 611 612 setlocale(LC_CTYPE, ""); 613 614 /* 615 * Initialize option structure to indicate that no values have been 616 * set. 617 */ 618 initialize_options(&options); 619 620 /* 621 * Prepare main ssh transport/connection structures 622 */ 623 if ((ssh = ssh_alloc_session_state()) == NULL) 624 fatal("Couldn't allocate session state"); 625 channel_init_channels(ssh); 626 active_state = ssh; /* XXX legacy API compat */ 627 628 /* Parse command-line arguments. */ 629 host = NULL; 630 use_syslog = 0; 631 logfile = NULL; 632 argv0 = av[0]; 633 634 again: 635 while ((opt = getopt(ac, av, "1246ab:c:e:fgi:kl:m:no:p:qstvx" 636 "AB:CD:E:F:GI:J:KL:MNO:PQ:R:S:TVw:W:XYy")) != -1) { 637 switch (opt) { 638 case '1': 639 fatal("SSH protocol v.1 is no longer supported"); 640 break; 641 case '2': 642 /* Ignored */ 643 break; 644 case '4': 645 options.address_family = AF_INET; 646 break; 647 case '6': 648 options.address_family = AF_INET6; 649 break; 650 case 'n': 651 stdin_null_flag = 1; 652 break; 653 case 'f': 654 fork_after_authentication_flag = 1; 655 stdin_null_flag = 1; 656 break; 657 case 'x': 658 options.forward_x11 = 0; 659 break; 660 case 'X': 661 options.forward_x11 = 1; 662 break; 663 case 'y': 664 use_syslog = 1; 665 break; 666 case 'E': 667 logfile = optarg; 668 break; 669 case 'G': 670 config_test = 1; 671 break; 672 case 'Y': 673 options.forward_x11 = 1; 674 options.forward_x11_trusted = 1; 675 break; 676 case 'g': 677 options.fwd_opts.gateway_ports = 1; 678 break; 679 case 'O': 680 if (options.stdio_forward_host != NULL) 681 fatal("Cannot specify multiplexing " 682 "command with -W"); 683 else if (muxclient_command != 0) 684 fatal("Multiplexing command already specified"); 685 if (strcmp(optarg, "check") == 0) 686 muxclient_command = SSHMUX_COMMAND_ALIVE_CHECK; 687 else if (strcmp(optarg, "forward") == 0) 688 muxclient_command = SSHMUX_COMMAND_FORWARD; 689 else if (strcmp(optarg, "exit") == 0) 690 muxclient_command = SSHMUX_COMMAND_TERMINATE; 691 else if (strcmp(optarg, "stop") == 0) 692 muxclient_command = SSHMUX_COMMAND_STOP; 693 else if (strcmp(optarg, "cancel") == 0) 694 muxclient_command = SSHMUX_COMMAND_CANCEL_FWD; 695 else if (strcmp(optarg, "proxy") == 0) 696 muxclient_command = SSHMUX_COMMAND_PROXY; 697 else 698 fatal("Invalid multiplex command."); 699 break; 700 case 'P': /* deprecated */ 701 break; 702 case 'Q': 703 cp = NULL; 704 if (strcmp(optarg, "cipher") == 0) 705 cp = cipher_alg_list('\n', 0); 706 else if (strcmp(optarg, "cipher-auth") == 0) 707 cp = cipher_alg_list('\n', 1); 708 else if (strcmp(optarg, "mac") == 0) 709 cp = mac_alg_list('\n'); 710 else if (strcmp(optarg, "kex") == 0) 711 cp = kex_alg_list('\n'); 712 else if (strcmp(optarg, "key") == 0) 713 cp = sshkey_alg_list(0, 0, 0, '\n'); 714 else if (strcmp(optarg, "key-cert") == 0) 715 cp = sshkey_alg_list(1, 0, 0, '\n'); 716 else if (strcmp(optarg, "key-plain") == 0) 717 cp = sshkey_alg_list(0, 1, 0, '\n'); 718 else if (strcmp(optarg, "sig") == 0) 719 cp = sshkey_alg_list(0, 1, 1, '\n'); 720 else if (strcmp(optarg, "protocol-version") == 0) 721 cp = xstrdup("2"); 722 else if (strcmp(optarg, "help") == 0) { 723 cp = xstrdup( 724 "cipher\ncipher-auth\nkex\nkey\n" 725 "key-cert\nkey-plain\nmac\n" 726 "protocol-version\nsig"); 727 } 728 if (cp == NULL) 729 fatal("Unsupported query \"%s\"", optarg); 730 printf("%s\n", cp); 731 free(cp); 732 exit(0); 733 break; 734 case 'a': 735 options.forward_agent = 0; 736 break; 737 case 'A': 738 options.forward_agent = 1; 739 break; 740 case 'k': 741 options.gss_deleg_creds = 0; 742 break; 743 case 'K': 744 options.gss_authentication = 1; 745 options.gss_deleg_creds = 1; 746 break; 747 case 'i': 748 p = tilde_expand_filename(optarg, getuid()); 749 if (stat(p, &st) < 0) 750 fprintf(stderr, "Warning: Identity file %s " 751 "not accessible: %s.\n", p, 752 strerror(errno)); 753 else 754 add_identity_file(&options, NULL, p, 1); 755 free(p); 756 break; 757 case 'I': 758 #ifdef ENABLE_PKCS11 759 free(options.pkcs11_provider); 760 options.pkcs11_provider = xstrdup(optarg); 761 #else 762 fprintf(stderr, "no support for PKCS#11.\n"); 763 #endif 764 break; 765 case 'J': 766 if (options.jump_host != NULL) 767 fatal("Only a single -J option permitted"); 768 if (options.proxy_command != NULL) 769 fatal("Cannot specify -J with ProxyCommand"); 770 if (parse_jump(optarg, &options, 1) == -1) 771 fatal("Invalid -J argument"); 772 options.proxy_command = xstrdup("none"); 773 break; 774 case 't': 775 if (options.request_tty == REQUEST_TTY_YES) 776 options.request_tty = REQUEST_TTY_FORCE; 777 else 778 options.request_tty = REQUEST_TTY_YES; 779 break; 780 case 'v': 781 if (debug_flag == 0) { 782 debug_flag = 1; 783 options.log_level = SYSLOG_LEVEL_DEBUG1; 784 } else { 785 if (options.log_level < SYSLOG_LEVEL_DEBUG3) { 786 debug_flag++; 787 options.log_level++; 788 } 789 } 790 break; 791 case 'V': 792 fprintf(stderr, "%s, %s\n", 793 SSH_VERSION, 794 #ifdef WITH_OPENSSL 795 SSLeay_version(SSLEAY_VERSION) 796 #else 797 "without OpenSSL" 798 #endif 799 ); 800 if (opt == 'V') 801 exit(0); 802 break; 803 case 'w': 804 if (options.tun_open == -1) 805 options.tun_open = SSH_TUNMODE_DEFAULT; 806 options.tun_local = a2tun(optarg, &options.tun_remote); 807 if (options.tun_local == SSH_TUNID_ERR) { 808 fprintf(stderr, 809 "Bad tun device '%s'\n", optarg); 810 exit(255); 811 } 812 break; 813 case 'W': 814 if (options.stdio_forward_host != NULL) 815 fatal("stdio forward already specified"); 816 if (muxclient_command != 0) 817 fatal("Cannot specify stdio forward with -O"); 818 if (parse_forward(&fwd, optarg, 1, 0)) { 819 options.stdio_forward_host = fwd.listen_host; 820 options.stdio_forward_port = fwd.listen_port; 821 free(fwd.connect_host); 822 } else { 823 fprintf(stderr, 824 "Bad stdio forwarding specification '%s'\n", 825 optarg); 826 exit(255); 827 } 828 options.request_tty = REQUEST_TTY_NO; 829 no_shell_flag = 1; 830 break; 831 case 'q': 832 options.log_level = SYSLOG_LEVEL_QUIET; 833 break; 834 case 'e': 835 if (optarg[0] == '^' && optarg[2] == 0 && 836 (u_char) optarg[1] >= 64 && 837 (u_char) optarg[1] < 128) 838 options.escape_char = (u_char) optarg[1] & 31; 839 else if (strlen(optarg) == 1) 840 options.escape_char = (u_char) optarg[0]; 841 else if (strcmp(optarg, "none") == 0) 842 options.escape_char = SSH_ESCAPECHAR_NONE; 843 else { 844 fprintf(stderr, "Bad escape character '%s'.\n", 845 optarg); 846 exit(255); 847 } 848 break; 849 case 'c': 850 if (!ciphers_valid(*optarg == '+' ? 851 optarg + 1 : optarg)) { 852 fprintf(stderr, "Unknown cipher type '%s'\n", 853 optarg); 854 exit(255); 855 } 856 free(options.ciphers); 857 options.ciphers = xstrdup(optarg); 858 break; 859 case 'm': 860 if (mac_valid(optarg)) { 861 free(options.macs); 862 options.macs = xstrdup(optarg); 863 } else { 864 fprintf(stderr, "Unknown mac type '%s'\n", 865 optarg); 866 exit(255); 867 } 868 break; 869 case 'M': 870 if (options.control_master == SSHCTL_MASTER_YES) 871 options.control_master = SSHCTL_MASTER_ASK; 872 else 873 options.control_master = SSHCTL_MASTER_YES; 874 break; 875 case 'p': 876 if (options.port == -1) { 877 options.port = a2port(optarg); 878 if (options.port <= 0) { 879 fprintf(stderr, "Bad port '%s'\n", 880 optarg); 881 exit(255); 882 } 883 } 884 break; 885 case 'l': 886 if (options.user == NULL) 887 options.user = optarg; 888 break; 889 890 case 'L': 891 if (parse_forward(&fwd, optarg, 0, 0)) 892 add_local_forward(&options, &fwd); 893 else { 894 fprintf(stderr, 895 "Bad local forwarding specification '%s'\n", 896 optarg); 897 exit(255); 898 } 899 break; 900 901 case 'R': 902 if (parse_forward(&fwd, optarg, 0, 1) || 903 parse_forward(&fwd, optarg, 1, 1)) { 904 add_remote_forward(&options, &fwd); 905 } else { 906 fprintf(stderr, 907 "Bad remote forwarding specification " 908 "'%s'\n", optarg); 909 exit(255); 910 } 911 break; 912 913 case 'D': 914 if (parse_forward(&fwd, optarg, 1, 0)) { 915 add_local_forward(&options, &fwd); 916 } else { 917 fprintf(stderr, 918 "Bad dynamic forwarding specification " 919 "'%s'\n", optarg); 920 exit(255); 921 } 922 break; 923 924 case 'C': 925 options.compression = 1; 926 break; 927 case 'N': 928 no_shell_flag = 1; 929 options.request_tty = REQUEST_TTY_NO; 930 break; 931 case 'T': 932 options.request_tty = REQUEST_TTY_NO; 933 break; 934 case 'o': 935 line = xstrdup(optarg); 936 if (process_config_line(&options, pw, 937 host ? host : "", host ? host : "", line, 938 "command-line", 0, NULL, SSHCONF_USERCONF) != 0) 939 exit(255); 940 free(line); 941 break; 942 case 's': 943 subsystem_flag = 1; 944 break; 945 case 'S': 946 free(options.control_path); 947 options.control_path = xstrdup(optarg); 948 break; 949 case 'b': 950 options.bind_address = optarg; 951 break; 952 case 'B': 953 options.bind_interface = optarg; 954 break; 955 case 'F': 956 config = optarg; 957 break; 958 default: 959 usage(); 960 } 961 } 962 963 if (optind > 1 && strcmp(av[optind - 1], "--") == 0) 964 opt_terminated = 1; 965 966 ac -= optind; 967 av += optind; 968 969 if (ac > 0 && !host) { 970 int tport; 971 char *tuser; 972 switch (parse_ssh_uri(*av, &tuser, &host, &tport)) { 973 case -1: 974 usage(); 975 break; 976 case 0: 977 if (options.user == NULL) { 978 options.user = tuser; 979 tuser = NULL; 980 } 981 free(tuser); 982 if (options.port == -1 && tport != -1) 983 options.port = tport; 984 break; 985 default: 986 p = xstrdup(*av); 987 cp = strrchr(p, '@'); 988 if (cp != NULL) { 989 if (cp == p) 990 usage(); 991 if (options.user == NULL) { 992 options.user = p; 993 p = NULL; 994 } 995 *cp++ = '\0'; 996 host = xstrdup(cp); 997 free(p); 998 } else 999 host = p; 1000 break; 1001 } 1002 if (ac > 1 && !opt_terminated) { 1003 optind = optreset = 1; 1004 goto again; 1005 } 1006 ac--, av++; 1007 } 1008 1009 /* Check that we got a host name. */ 1010 if (!host) 1011 usage(); 1012 1013 host_arg = xstrdup(host); 1014 1015 #ifdef WITH_OPENSSL 1016 OpenSSL_add_all_algorithms(); 1017 ERR_load_crypto_strings(); 1018 #endif 1019 1020 /* Initialize the command to execute on remote host. */ 1021 if ((command = sshbuf_new()) == NULL) 1022 fatal("sshbuf_new failed"); 1023 1024 /* 1025 * Save the command to execute on the remote host in a buffer. There 1026 * is no limit on the length of the command, except by the maximum 1027 * packet size. Also sets the tty flag if there is no command. 1028 */ 1029 if (!ac) { 1030 /* No command specified - execute shell on a tty. */ 1031 if (subsystem_flag) { 1032 fprintf(stderr, 1033 "You must specify a subsystem to invoke.\n"); 1034 usage(); 1035 } 1036 } else { 1037 /* A command has been specified. Store it into the buffer. */ 1038 for (i = 0; i < ac; i++) { 1039 if ((r = sshbuf_putf(command, "%s%s", 1040 i ? " " : "", av[i])) != 0) 1041 fatal("%s: buffer error: %s", 1042 __func__, ssh_err(r)); 1043 } 1044 } 1045 1046 /* 1047 * Initialize "log" output. Since we are the client all output 1048 * goes to stderr unless otherwise specified by -y or -E. 1049 */ 1050 if (use_syslog && logfile != NULL) 1051 fatal("Can't specify both -y and -E"); 1052 if (logfile != NULL) 1053 log_redirect_stderr_to(logfile); 1054 log_init(argv0, 1055 options.log_level == SYSLOG_LEVEL_NOT_SET ? 1056 SYSLOG_LEVEL_INFO : options.log_level, 1057 options.log_facility == SYSLOG_FACILITY_NOT_SET ? 1058 SYSLOG_FACILITY_USER : options.log_facility, 1059 !use_syslog); 1060 1061 if (debug_flag) 1062 logit("%s, %s", SSH_VERSION, 1063 #ifdef WITH_OPENSSL 1064 SSLeay_version(SSLEAY_VERSION) 1065 #else 1066 "without OpenSSL" 1067 #endif 1068 ); 1069 1070 /* Parse the configuration files */ 1071 process_config_files(host_arg, pw, 0); 1072 1073 /* Hostname canonicalisation needs a few options filled. */ 1074 fill_default_options_for_canonicalization(&options); 1075 1076 /* If the user has replaced the hostname then take it into use now */ 1077 if (options.hostname != NULL) { 1078 /* NB. Please keep in sync with readconf.c:match_cfg_line() */ 1079 cp = percent_expand(options.hostname, 1080 "h", host, (char *)NULL); 1081 free(host); 1082 host = cp; 1083 free(options.hostname); 1084 options.hostname = xstrdup(host); 1085 } 1086 1087 /* Don't lowercase addresses, they will be explicitly canonicalised */ 1088 if ((was_addr = is_addr(host)) == 0) 1089 lowercase(host); 1090 1091 /* 1092 * Try to canonicalize if requested by configuration or the 1093 * hostname is an address. 1094 */ 1095 if (options.canonicalize_hostname != SSH_CANONICALISE_NO || was_addr) 1096 addrs = resolve_canonicalize(&host, options.port); 1097 1098 /* 1099 * If CanonicalizePermittedCNAMEs have been specified but 1100 * other canonicalization did not happen (by not being requested 1101 * or by failing with fallback) then the hostname may still be changed 1102 * as a result of CNAME following. 1103 * 1104 * Try to resolve the bare hostname name using the system resolver's 1105 * usual search rules and then apply the CNAME follow rules. 1106 * 1107 * Skip the lookup if a ProxyCommand is being used unless the user 1108 * has specifically requested canonicalisation for this case via 1109 * CanonicalizeHostname=always 1110 */ 1111 direct = option_clear_or_none(options.proxy_command) && 1112 options.jump_host == NULL; 1113 if (addrs == NULL && options.num_permitted_cnames != 0 && (direct || 1114 options.canonicalize_hostname == SSH_CANONICALISE_ALWAYS)) { 1115 if ((addrs = resolve_host(host, options.port, 1116 direct, cname, sizeof(cname))) == NULL) { 1117 /* Don't fatal proxied host names not in the DNS */ 1118 if (direct) 1119 cleanup_exit(255); /* logged in resolve_host */ 1120 } else 1121 check_follow_cname(direct, &host, cname); 1122 } 1123 1124 /* 1125 * If canonicalisation is enabled then re-parse the configuration 1126 * files as new stanzas may match. 1127 */ 1128 if (options.canonicalize_hostname != 0) { 1129 debug("Re-reading configuration after hostname " 1130 "canonicalisation"); 1131 free(options.hostname); 1132 options.hostname = xstrdup(host); 1133 process_config_files(host_arg, pw, 1); 1134 /* 1135 * Address resolution happens early with canonicalisation 1136 * enabled and the port number may have changed since, so 1137 * reset it in address list 1138 */ 1139 if (addrs != NULL && options.port > 0) 1140 set_addrinfo_port(addrs, options.port); 1141 } 1142 1143 /* Fill configuration defaults. */ 1144 fill_default_options(&options); 1145 1146 /* 1147 * If ProxyJump option specified, then construct a ProxyCommand now. 1148 */ 1149 if (options.jump_host != NULL) { 1150 char port_s[8]; 1151 const char *sshbin = argv0; 1152 1153 /* 1154 * Try to use SSH indicated by argv[0], but fall back to 1155 * "ssh" if it appears unavailable. 1156 */ 1157 if (strchr(argv0, '/') != NULL && access(argv0, X_OK) != 0) 1158 sshbin = "ssh"; 1159 1160 /* Consistency check */ 1161 if (options.proxy_command != NULL) 1162 fatal("inconsistent options: ProxyCommand+ProxyJump"); 1163 /* Never use FD passing for ProxyJump */ 1164 options.proxy_use_fdpass = 0; 1165 snprintf(port_s, sizeof(port_s), "%d", options.jump_port); 1166 xasprintf(&options.proxy_command, 1167 "%s%s%s%s%s%s%s%s%s%s%.*s -W '[%%h]:%%p' %s", 1168 sshbin, 1169 /* Optional "-l user" argument if jump_user set */ 1170 options.jump_user == NULL ? "" : " -l ", 1171 options.jump_user == NULL ? "" : options.jump_user, 1172 /* Optional "-p port" argument if jump_port set */ 1173 options.jump_port <= 0 ? "" : " -p ", 1174 options.jump_port <= 0 ? "" : port_s, 1175 /* Optional additional jump hosts ",..." */ 1176 options.jump_extra == NULL ? "" : " -J ", 1177 options.jump_extra == NULL ? "" : options.jump_extra, 1178 /* Optional "-F" argumment if -F specified */ 1179 config == NULL ? "" : " -F ", 1180 config == NULL ? "" : config, 1181 /* Optional "-v" arguments if -v set */ 1182 debug_flag ? " -" : "", 1183 debug_flag, "vvv", 1184 /* Mandatory hostname */ 1185 options.jump_host); 1186 debug("Setting implicit ProxyCommand from ProxyJump: %s", 1187 options.proxy_command); 1188 } 1189 1190 if (options.port == 0) 1191 options.port = default_ssh_port(); 1192 channel_set_af(ssh, options.address_family); 1193 1194 /* Tidy and check options */ 1195 if (options.host_key_alias != NULL) 1196 lowercase(options.host_key_alias); 1197 if (options.proxy_command != NULL && 1198 strcmp(options.proxy_command, "-") == 0 && 1199 options.proxy_use_fdpass) 1200 fatal("ProxyCommand=- and ProxyUseFDPass are incompatible"); 1201 if (options.control_persist && 1202 options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK) { 1203 debug("UpdateHostKeys=ask is incompatible with ControlPersist; " 1204 "disabling"); 1205 options.update_hostkeys = 0; 1206 } 1207 if (options.connection_attempts <= 0) 1208 fatal("Invalid number of ConnectionAttempts"); 1209 1210 if (sshbuf_len(command) != 0 && options.remote_command != NULL) 1211 fatal("Cannot execute command-line and remote command."); 1212 1213 /* Cannot fork to background if no command. */ 1214 if (fork_after_authentication_flag && sshbuf_len(command) == 0 && 1215 options.remote_command == NULL && !no_shell_flag) 1216 fatal("Cannot fork into background without a command " 1217 "to execute."); 1218 1219 /* reinit */ 1220 log_init(argv0, options.log_level, options.log_facility, !use_syslog); 1221 1222 if (options.request_tty == REQUEST_TTY_YES || 1223 options.request_tty == REQUEST_TTY_FORCE) 1224 tty_flag = 1; 1225 1226 /* Allocate a tty by default if no command specified. */ 1227 if (sshbuf_len(command) == 0 && options.remote_command == NULL) 1228 tty_flag = options.request_tty != REQUEST_TTY_NO; 1229 1230 /* Force no tty */ 1231 if (options.request_tty == REQUEST_TTY_NO || 1232 (muxclient_command && muxclient_command != SSHMUX_COMMAND_PROXY)) 1233 tty_flag = 0; 1234 /* Do not allocate a tty if stdin is not a tty. */ 1235 if ((!isatty(fileno(stdin)) || stdin_null_flag) && 1236 options.request_tty != REQUEST_TTY_FORCE) { 1237 if (tty_flag) 1238 logit("Pseudo-terminal will not be allocated because " 1239 "stdin is not a terminal."); 1240 tty_flag = 0; 1241 } 1242 1243 if (options.user == NULL) 1244 options.user = xstrdup(pw->pw_name); 1245 1246 /* Set up strings used to percent_expand() arguments */ 1247 if (gethostname(thishost, sizeof(thishost)) == -1) 1248 fatal("gethostname: %s", strerror(errno)); 1249 strlcpy(shorthost, thishost, sizeof(shorthost)); 1250 shorthost[strcspn(thishost, ".")] = '\0'; 1251 snprintf(portstr, sizeof(portstr), "%d", options.port); 1252 snprintf(uidstr, sizeof(uidstr), "%llu", 1253 (unsigned long long)pw->pw_uid); 1254 1255 if ((md = ssh_digest_start(SSH_DIGEST_SHA1)) == NULL || 1256 ssh_digest_update(md, thishost, strlen(thishost)) < 0 || 1257 ssh_digest_update(md, host, strlen(host)) < 0 || 1258 ssh_digest_update(md, portstr, strlen(portstr)) < 0 || 1259 ssh_digest_update(md, options.user, strlen(options.user)) < 0 || 1260 ssh_digest_final(md, conn_hash, sizeof(conn_hash)) < 0) 1261 fatal("%s: mux digest failed", __func__); 1262 ssh_digest_free(md); 1263 conn_hash_hex = tohex(conn_hash, ssh_digest_bytes(SSH_DIGEST_SHA1)); 1264 1265 /* 1266 * Expand tokens in arguments. NB. LocalCommand is expanded later, 1267 * after port-forwarding is set up, so it may pick up any local 1268 * tunnel interface name allocated. 1269 */ 1270 if (options.remote_command != NULL) { 1271 debug3("expanding RemoteCommand: %s", options.remote_command); 1272 cp = options.remote_command; 1273 options.remote_command = percent_expand(cp, 1274 "C", conn_hash_hex, 1275 "L", shorthost, 1276 "d", pw->pw_dir, 1277 "h", host, 1278 "i", uidstr, 1279 "l", thishost, 1280 "n", host_arg, 1281 "p", portstr, 1282 "r", options.user, 1283 "u", pw->pw_name, 1284 (char *)NULL); 1285 debug3("expanded RemoteCommand: %s", options.remote_command); 1286 free(cp); 1287 if ((r = sshbuf_put(command, options.remote_command, 1288 strlen(options.remote_command))) != 0) 1289 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1290 } 1291 1292 if (options.control_path != NULL) { 1293 cp = tilde_expand_filename(options.control_path, getuid()); 1294 free(options.control_path); 1295 options.control_path = percent_expand(cp, 1296 "C", conn_hash_hex, 1297 "L", shorthost, 1298 "h", host, 1299 "i", uidstr, 1300 "l", thishost, 1301 "n", host_arg, 1302 "p", portstr, 1303 "r", options.user, 1304 "u", pw->pw_name, 1305 "i", uidstr, 1306 (char *)NULL); 1307 free(cp); 1308 } 1309 1310 if (config_test) { 1311 dump_client_config(&options, host); 1312 exit(0); 1313 } 1314 1315 if (muxclient_command != 0 && options.control_path == NULL) 1316 fatal("No ControlPath specified for \"-O\" command"); 1317 if (options.control_path != NULL) { 1318 int sock; 1319 if ((sock = muxclient(options.control_path)) >= 0) { 1320 ssh_packet_set_connection(ssh, sock, sock); 1321 packet_set_mux(); 1322 goto skip_connect; 1323 } 1324 } 1325 1326 /* 1327 * If hostname canonicalisation was not enabled, then we may not 1328 * have yet resolved the hostname. Do so now. 1329 */ 1330 if (addrs == NULL && options.proxy_command == NULL) { 1331 debug2("resolving \"%s\" port %d", host, options.port); 1332 if ((addrs = resolve_host(host, options.port, 1, 1333 cname, sizeof(cname))) == NULL) 1334 cleanup_exit(255); /* resolve_host logs the error */ 1335 } 1336 1337 timeout_ms = options.connection_timeout * 1000; 1338 1339 /* Open a connection to the remote host. */ 1340 if (ssh_connect(ssh, host, addrs, &hostaddr, options.port, 1341 options.address_family, options.connection_attempts, 1342 &timeout_ms, options.tcp_keep_alive) != 0) 1343 exit(255); 1344 1345 if (addrs != NULL) 1346 freeaddrinfo(addrs); 1347 1348 packet_set_timeout(options.server_alive_interval, 1349 options.server_alive_count_max); 1350 1351 ssh = active_state; /* XXX */ 1352 1353 if (timeout_ms > 0) 1354 debug3("timeout: %d ms remain after connect", timeout_ms); 1355 1356 /* 1357 * If we successfully made the connection and we have hostbased auth 1358 * enabled, load the public keys so we can later use the ssh-keysign 1359 * helper to sign challenges. 1360 */ 1361 sensitive_data.nkeys = 0; 1362 sensitive_data.keys = NULL; 1363 if (options.hostbased_authentication) { 1364 sensitive_data.nkeys = 10; 1365 sensitive_data.keys = xcalloc(sensitive_data.nkeys, 1366 sizeof(struct sshkey)); 1367 1368 /* XXX check errors? */ 1369 #define L_PUBKEY(p,o) do { \ 1370 if ((o) >= sensitive_data.nkeys) \ 1371 fatal("%s pubkey out of array bounds", __func__); \ 1372 check_load(sshkey_load_public(p, &(sensitive_data.keys[o]), NULL), \ 1373 p, "pubkey"); \ 1374 } while (0) 1375 #define L_CERT(p,o) do { \ 1376 if ((o) >= sensitive_data.nkeys) \ 1377 fatal("%s cert out of array bounds", __func__); \ 1378 check_load(sshkey_load_cert(p, &(sensitive_data.keys[o])), p, "cert"); \ 1379 } while (0) 1380 1381 if (options.hostbased_authentication == 1) { 1382 L_CERT(_PATH_HOST_ECDSA_KEY_FILE, 0); 1383 L_CERT(_PATH_HOST_ED25519_KEY_FILE, 1); 1384 L_CERT(_PATH_HOST_RSA_KEY_FILE, 2); 1385 L_CERT(_PATH_HOST_DSA_KEY_FILE, 3); 1386 L_PUBKEY(_PATH_HOST_ECDSA_KEY_FILE, 4); 1387 L_PUBKEY(_PATH_HOST_ED25519_KEY_FILE, 5); 1388 L_PUBKEY(_PATH_HOST_RSA_KEY_FILE, 6); 1389 L_PUBKEY(_PATH_HOST_DSA_KEY_FILE, 7); 1390 L_CERT(_PATH_HOST_XMSS_KEY_FILE, 8); 1391 L_PUBKEY(_PATH_HOST_XMSS_KEY_FILE, 9); 1392 } 1393 } 1394 1395 /* Create ~/.ssh * directory if it doesn't already exist. */ 1396 if (config == NULL) { 1397 r = snprintf(buf, sizeof buf, "%s%s%s", pw->pw_dir, 1398 strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR); 1399 if (r > 0 && (size_t)r < sizeof(buf) && stat(buf, &st) < 0) 1400 if (mkdir(buf, 0700) < 0) 1401 error("Could not create directory '%.200s'.", 1402 buf); 1403 } 1404 1405 /* load options.identity_files */ 1406 load_public_identity_files(pw); 1407 1408 /* optionally set the SSH_AUTHSOCKET_ENV_NAME variable */ 1409 if (options.identity_agent && 1410 strcmp(options.identity_agent, SSH_AUTHSOCKET_ENV_NAME) != 0) { 1411 if (strcmp(options.identity_agent, "none") == 0) { 1412 unsetenv(SSH_AUTHSOCKET_ENV_NAME); 1413 } else { 1414 p = tilde_expand_filename(options.identity_agent, 1415 getuid()); 1416 cp = percent_expand(p, 1417 "d", pw->pw_dir, 1418 "h", host, 1419 "i", uidstr, 1420 "l", thishost, 1421 "r", options.user, 1422 "u", pw->pw_name, 1423 (char *)NULL); 1424 free(p); 1425 /* 1426 * If identity_agent represents an environment variable 1427 * then recheck that it is valid (since processing with 1428 * percent_expand() may have changed it) and substitute 1429 * its value. 1430 */ 1431 if (cp[0] == '$') { 1432 if (!valid_env_name(cp + 1)) { 1433 fatal("Invalid IdentityAgent " 1434 "environment variable name %s", cp); 1435 } 1436 if ((p = getenv(cp + 1)) == NULL) 1437 unsetenv(SSH_AUTHSOCKET_ENV_NAME); 1438 else 1439 setenv(SSH_AUTHSOCKET_ENV_NAME, p, 1); 1440 } else { 1441 /* identity_agent specifies a path directly */ 1442 setenv(SSH_AUTHSOCKET_ENV_NAME, cp, 1); 1443 } 1444 free(cp); 1445 } 1446 } 1447 1448 /* Expand ~ in known host file names. */ 1449 tilde_expand_paths(options.system_hostfiles, 1450 options.num_system_hostfiles); 1451 tilde_expand_paths(options.user_hostfiles, options.num_user_hostfiles); 1452 1453 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */ 1454 signal(SIGCHLD, main_sigchld_handler); 1455 1456 /* Log into the remote system. Never returns if the login fails. */ 1457 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, 1458 options.port, pw, timeout_ms); 1459 1460 if (packet_connection_is_on_socket()) { 1461 verbose("Authenticated to %s ([%s]:%d).", host, 1462 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); 1463 } else { 1464 verbose("Authenticated to %s (via proxy).", host); 1465 } 1466 1467 /* We no longer need the private host keys. Clear them now. */ 1468 if (sensitive_data.nkeys != 0) { 1469 for (i = 0; i < sensitive_data.nkeys; i++) { 1470 if (sensitive_data.keys[i] != NULL) { 1471 /* Destroys contents safely */ 1472 debug3("clear hostkey %d", i); 1473 sshkey_free(sensitive_data.keys[i]); 1474 sensitive_data.keys[i] = NULL; 1475 } 1476 } 1477 free(sensitive_data.keys); 1478 } 1479 for (i = 0; i < options.num_identity_files; i++) { 1480 free(options.identity_files[i]); 1481 options.identity_files[i] = NULL; 1482 if (options.identity_keys[i]) { 1483 sshkey_free(options.identity_keys[i]); 1484 options.identity_keys[i] = NULL; 1485 } 1486 } 1487 for (i = 0; i < options.num_certificate_files; i++) { 1488 free(options.certificate_files[i]); 1489 options.certificate_files[i] = NULL; 1490 } 1491 1492 skip_connect: 1493 exit_status = ssh_session2(ssh, pw); 1494 packet_close(); 1495 1496 if (options.control_path != NULL && muxserver_sock != -1) 1497 unlink(options.control_path); 1498 1499 /* Kill ProxyCommand if it is running. */ 1500 ssh_kill_proxy_command(); 1501 1502 return exit_status; 1503 } 1504 1505 static void 1506 control_persist_detach(void) 1507 { 1508 pid_t pid; 1509 int devnull, keep_stderr; 1510 1511 debug("%s: backgrounding master process", __func__); 1512 1513 /* 1514 * master (current process) into the background, and make the 1515 * foreground process a client of the backgrounded master. 1516 */ 1517 switch ((pid = fork())) { 1518 case -1: 1519 fatal("%s: fork: %s", __func__, strerror(errno)); 1520 case 0: 1521 /* Child: master process continues mainloop */ 1522 break; 1523 default: 1524 /* Parent: set up mux slave to connect to backgrounded master */ 1525 debug2("%s: background process is %ld", __func__, (long)pid); 1526 stdin_null_flag = ostdin_null_flag; 1527 options.request_tty = orequest_tty; 1528 tty_flag = otty_flag; 1529 close(muxserver_sock); 1530 muxserver_sock = -1; 1531 options.control_master = SSHCTL_MASTER_NO; 1532 muxclient(options.control_path); 1533 /* muxclient() doesn't return on success. */ 1534 fatal("Failed to connect to new control master"); 1535 } 1536 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) { 1537 error("%s: open(\"/dev/null\"): %s", __func__, 1538 strerror(errno)); 1539 } else { 1540 keep_stderr = log_is_on_stderr() && debug_flag; 1541 if (dup2(devnull, STDIN_FILENO) == -1 || 1542 dup2(devnull, STDOUT_FILENO) == -1 || 1543 (!keep_stderr && dup2(devnull, STDERR_FILENO) == -1)) 1544 error("%s: dup2: %s", __func__, strerror(errno)); 1545 if (devnull > STDERR_FILENO) 1546 close(devnull); 1547 } 1548 daemon(1, 1); 1549 setproctitle("%s [mux]", options.control_path); 1550 } 1551 1552 /* Do fork() after authentication. Used by "ssh -f" */ 1553 static void 1554 fork_postauth(void) 1555 { 1556 if (need_controlpersist_detach) 1557 control_persist_detach(); 1558 debug("forking to background"); 1559 fork_after_authentication_flag = 0; 1560 if (daemon(1, 1) < 0) 1561 fatal("daemon() failed: %.200s", strerror(errno)); 1562 } 1563 1564 /* Callback for remote forward global requests */ 1565 static void 1566 ssh_confirm_remote_forward(struct ssh *ssh, int type, u_int32_t seq, void *ctxt) 1567 { 1568 struct Forward *rfwd = (struct Forward *)ctxt; 1569 1570 /* XXX verbose() on failure? */ 1571 debug("remote forward %s for: listen %s%s%d, connect %s:%d", 1572 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure", 1573 rfwd->listen_path ? rfwd->listen_path : 1574 rfwd->listen_host ? rfwd->listen_host : "", 1575 (rfwd->listen_path || rfwd->listen_host) ? ":" : "", 1576 rfwd->listen_port, rfwd->connect_path ? rfwd->connect_path : 1577 rfwd->connect_host, rfwd->connect_port); 1578 if (rfwd->listen_path == NULL && rfwd->listen_port == 0) { 1579 if (type == SSH2_MSG_REQUEST_SUCCESS) { 1580 rfwd->allocated_port = packet_get_int(); 1581 logit("Allocated port %u for remote forward to %s:%d", 1582 rfwd->allocated_port, 1583 rfwd->connect_host, rfwd->connect_port); 1584 channel_update_permission(ssh, 1585 rfwd->handle, rfwd->allocated_port); 1586 } else { 1587 channel_update_permission(ssh, rfwd->handle, -1); 1588 } 1589 } 1590 1591 if (type == SSH2_MSG_REQUEST_FAILURE) { 1592 if (options.exit_on_forward_failure) { 1593 if (rfwd->listen_path != NULL) 1594 fatal("Error: remote port forwarding failed " 1595 "for listen path %s", rfwd->listen_path); 1596 else 1597 fatal("Error: remote port forwarding failed " 1598 "for listen port %d", rfwd->listen_port); 1599 } else { 1600 if (rfwd->listen_path != NULL) 1601 logit("Warning: remote port forwarding failed " 1602 "for listen path %s", rfwd->listen_path); 1603 else 1604 logit("Warning: remote port forwarding failed " 1605 "for listen port %d", rfwd->listen_port); 1606 } 1607 } 1608 if (++remote_forward_confirms_received == options.num_remote_forwards) { 1609 debug("All remote forwarding requests processed"); 1610 if (fork_after_authentication_flag) 1611 fork_postauth(); 1612 } 1613 } 1614 1615 static void 1616 client_cleanup_stdio_fwd(struct ssh *ssh, int id, void *arg) 1617 { 1618 debug("stdio forwarding: done"); 1619 cleanup_exit(0); 1620 } 1621 1622 static void 1623 ssh_stdio_confirm(struct ssh *ssh, int id, int success, void *arg) 1624 { 1625 if (!success) 1626 fatal("stdio forwarding failed"); 1627 } 1628 1629 static void 1630 ssh_init_stdio_forwarding(struct ssh *ssh) 1631 { 1632 Channel *c; 1633 int in, out; 1634 1635 if (options.stdio_forward_host == NULL) 1636 return; 1637 1638 debug3("%s: %s:%d", __func__, options.stdio_forward_host, 1639 options.stdio_forward_port); 1640 1641 if ((in = dup(STDIN_FILENO)) < 0 || 1642 (out = dup(STDOUT_FILENO)) < 0) 1643 fatal("channel_connect_stdio_fwd: dup() in/out failed"); 1644 if ((c = channel_connect_stdio_fwd(ssh, options.stdio_forward_host, 1645 options.stdio_forward_port, in, out)) == NULL) 1646 fatal("%s: channel_connect_stdio_fwd failed", __func__); 1647 channel_register_cleanup(ssh, c->self, client_cleanup_stdio_fwd, 0); 1648 channel_register_open_confirm(ssh, c->self, ssh_stdio_confirm, NULL); 1649 } 1650 1651 static void 1652 ssh_init_forwarding(struct ssh *ssh, char **ifname) 1653 { 1654 int success = 0; 1655 int i; 1656 1657 /* Initiate local TCP/IP port forwardings. */ 1658 for (i = 0; i < options.num_local_forwards; i++) { 1659 debug("Local connections to %.200s:%d forwarded to remote " 1660 "address %.200s:%d", 1661 (options.local_forwards[i].listen_path != NULL) ? 1662 options.local_forwards[i].listen_path : 1663 (options.local_forwards[i].listen_host == NULL) ? 1664 (options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") : 1665 options.local_forwards[i].listen_host, 1666 options.local_forwards[i].listen_port, 1667 (options.local_forwards[i].connect_path != NULL) ? 1668 options.local_forwards[i].connect_path : 1669 options.local_forwards[i].connect_host, 1670 options.local_forwards[i].connect_port); 1671 success += channel_setup_local_fwd_listener(ssh, 1672 &options.local_forwards[i], &options.fwd_opts); 1673 } 1674 if (i > 0 && success != i && options.exit_on_forward_failure) 1675 fatal("Could not request local forwarding."); 1676 if (i > 0 && success == 0) 1677 error("Could not request local forwarding."); 1678 1679 /* Initiate remote TCP/IP port forwardings. */ 1680 for (i = 0; i < options.num_remote_forwards; i++) { 1681 debug("Remote connections from %.200s:%d forwarded to " 1682 "local address %.200s:%d", 1683 (options.remote_forwards[i].listen_path != NULL) ? 1684 options.remote_forwards[i].listen_path : 1685 (options.remote_forwards[i].listen_host == NULL) ? 1686 "LOCALHOST" : options.remote_forwards[i].listen_host, 1687 options.remote_forwards[i].listen_port, 1688 (options.remote_forwards[i].connect_path != NULL) ? 1689 options.remote_forwards[i].connect_path : 1690 options.remote_forwards[i].connect_host, 1691 options.remote_forwards[i].connect_port); 1692 options.remote_forwards[i].handle = 1693 channel_request_remote_forwarding(ssh, 1694 &options.remote_forwards[i]); 1695 if (options.remote_forwards[i].handle < 0) { 1696 if (options.exit_on_forward_failure) 1697 fatal("Could not request remote forwarding."); 1698 else 1699 logit("Warning: Could not request remote " 1700 "forwarding."); 1701 } else { 1702 client_register_global_confirm( 1703 ssh_confirm_remote_forward, 1704 &options.remote_forwards[i]); 1705 } 1706 } 1707 1708 /* Initiate tunnel forwarding. */ 1709 if (options.tun_open != SSH_TUNMODE_NO) { 1710 if ((*ifname = client_request_tun_fwd(ssh, 1711 options.tun_open, options.tun_local, 1712 options.tun_remote)) == NULL) { 1713 if (options.exit_on_forward_failure) 1714 fatal("Could not request tunnel forwarding."); 1715 else 1716 error("Could not request tunnel forwarding."); 1717 } 1718 } 1719 } 1720 1721 static void 1722 check_agent_present(void) 1723 { 1724 int r; 1725 1726 if (options.forward_agent) { 1727 /* Clear agent forwarding if we don't have an agent. */ 1728 if ((r = ssh_get_authentication_socket(NULL)) != 0) { 1729 options.forward_agent = 0; 1730 if (r != SSH_ERR_AGENT_NOT_PRESENT) 1731 debug("ssh_get_authentication_socket: %s", 1732 ssh_err(r)); 1733 } 1734 } 1735 } 1736 1737 static void 1738 ssh_session2_setup(struct ssh *ssh, int id, int success, void *arg) 1739 { 1740 extern char **environ; 1741 const char *display; 1742 int interactive = tty_flag; 1743 char *proto = NULL, *data = NULL; 1744 1745 if (!success) 1746 return; /* No need for error message, channels code sens one */ 1747 1748 display = getenv("DISPLAY"); 1749 if (display == NULL && options.forward_x11) 1750 debug("X11 forwarding requested but DISPLAY not set"); 1751 if (options.forward_x11 && client_x11_get_proto(ssh, display, 1752 options.xauth_location, options.forward_x11_trusted, 1753 options.forward_x11_timeout, &proto, &data) == 0) { 1754 /* Request forwarding with authentication spoofing. */ 1755 debug("Requesting X11 forwarding with authentication " 1756 "spoofing."); 1757 x11_request_forwarding_with_spoofing(ssh, id, display, proto, 1758 data, 1); 1759 client_expect_confirm(ssh, id, "X11 forwarding", CONFIRM_WARN); 1760 /* XXX exit_on_forward_failure */ 1761 interactive = 1; 1762 } 1763 1764 check_agent_present(); 1765 if (options.forward_agent) { 1766 debug("Requesting authentication agent forwarding."); 1767 channel_request_start(ssh, id, "auth-agent-req@openssh.com", 0); 1768 packet_send(); 1769 } 1770 1771 /* Tell the packet module whether this is an interactive session. */ 1772 packet_set_interactive(interactive, 1773 options.ip_qos_interactive, options.ip_qos_bulk); 1774 1775 client_session2_setup(ssh, id, tty_flag, subsystem_flag, getenv("TERM"), 1776 NULL, fileno(stdin), command, environ); 1777 } 1778 1779 /* open new channel for a session */ 1780 static int 1781 ssh_session2_open(struct ssh *ssh) 1782 { 1783 Channel *c; 1784 int window, packetmax, in, out, err; 1785 1786 if (stdin_null_flag) { 1787 in = open(_PATH_DEVNULL, O_RDONLY); 1788 } else { 1789 in = dup(STDIN_FILENO); 1790 } 1791 out = dup(STDOUT_FILENO); 1792 err = dup(STDERR_FILENO); 1793 1794 if (in < 0 || out < 0 || err < 0) 1795 fatal("dup() in/out/err failed"); 1796 1797 /* enable nonblocking unless tty */ 1798 if (!isatty(in)) 1799 set_nonblock(in); 1800 if (!isatty(out)) 1801 set_nonblock(out); 1802 if (!isatty(err)) 1803 set_nonblock(err); 1804 1805 window = CHAN_SES_WINDOW_DEFAULT; 1806 packetmax = CHAN_SES_PACKET_DEFAULT; 1807 if (tty_flag) { 1808 window >>= 1; 1809 packetmax >>= 1; 1810 } 1811 c = channel_new(ssh, 1812 "session", SSH_CHANNEL_OPENING, in, out, err, 1813 window, packetmax, CHAN_EXTENDED_WRITE, 1814 "client-session", /*nonblock*/0); 1815 1816 debug3("%s: channel_new: %d", __func__, c->self); 1817 1818 channel_send_open(ssh, c->self); 1819 if (!no_shell_flag) 1820 channel_register_open_confirm(ssh, c->self, 1821 ssh_session2_setup, NULL); 1822 1823 return c->self; 1824 } 1825 1826 static int 1827 ssh_session2(struct ssh *ssh, struct passwd *pw) 1828 { 1829 int devnull, id = -1; 1830 char *cp, *tun_fwd_ifname = NULL; 1831 1832 /* XXX should be pre-session */ 1833 if (!options.control_persist) 1834 ssh_init_stdio_forwarding(ssh); 1835 1836 ssh_init_forwarding(ssh, &tun_fwd_ifname); 1837 1838 if (options.local_command != NULL) { 1839 debug3("expanding LocalCommand: %s", options.local_command); 1840 cp = options.local_command; 1841 options.local_command = percent_expand(cp, 1842 "C", conn_hash_hex, 1843 "L", shorthost, 1844 "d", pw->pw_dir, 1845 "h", host, 1846 "i", uidstr, 1847 "l", thishost, 1848 "n", host_arg, 1849 "p", portstr, 1850 "r", options.user, 1851 "u", pw->pw_name, 1852 "T", tun_fwd_ifname == NULL ? "NONE" : tun_fwd_ifname, 1853 (char *)NULL); 1854 debug3("expanded LocalCommand: %s", options.local_command); 1855 free(cp); 1856 } 1857 1858 /* Start listening for multiplex clients */ 1859 if (!packet_get_mux()) 1860 muxserver_listen(ssh); 1861 1862 /* 1863 * If we are in control persist mode and have a working mux listen 1864 * socket, then prepare to background ourselves and have a foreground 1865 * client attach as a control slave. 1866 * NB. we must save copies of the flags that we override for 1867 * the backgrounding, since we defer attachment of the slave until 1868 * after the connection is fully established (in particular, 1869 * async rfwd replies have been received for ExitOnForwardFailure). 1870 */ 1871 if (options.control_persist && muxserver_sock != -1) { 1872 ostdin_null_flag = stdin_null_flag; 1873 ono_shell_flag = no_shell_flag; 1874 orequest_tty = options.request_tty; 1875 otty_flag = tty_flag; 1876 stdin_null_flag = 1; 1877 no_shell_flag = 1; 1878 tty_flag = 0; 1879 if (!fork_after_authentication_flag) 1880 need_controlpersist_detach = 1; 1881 fork_after_authentication_flag = 1; 1882 } 1883 /* 1884 * ControlPersist mux listen socket setup failed, attempt the 1885 * stdio forward setup that we skipped earlier. 1886 */ 1887 if (options.control_persist && muxserver_sock == -1) 1888 ssh_init_stdio_forwarding(ssh); 1889 1890 if (!no_shell_flag) 1891 id = ssh_session2_open(ssh); 1892 else { 1893 packet_set_interactive( 1894 options.control_master == SSHCTL_MASTER_NO, 1895 options.ip_qos_interactive, options.ip_qos_bulk); 1896 } 1897 1898 /* If we don't expect to open a new session, then disallow it */ 1899 if (options.control_master == SSHCTL_MASTER_NO && 1900 (datafellows & SSH_NEW_OPENSSH)) { 1901 debug("Requesting no-more-sessions@openssh.com"); 1902 packet_start(SSH2_MSG_GLOBAL_REQUEST); 1903 packet_put_cstring("no-more-sessions@openssh.com"); 1904 packet_put_char(0); 1905 packet_send(); 1906 } 1907 1908 /* Execute a local command */ 1909 if (options.local_command != NULL && 1910 options.permit_local_command) 1911 ssh_local_cmd(options.local_command); 1912 1913 /* 1914 * stdout is now owned by the session channel; clobber it here 1915 * so future channel closes are propagated to the local fd. 1916 * NB. this can only happen after LocalCommand has completed, 1917 * as it may want to write to stdout. 1918 */ 1919 if (!need_controlpersist_detach) { 1920 if ((devnull = open(_PATH_DEVNULL, O_WRONLY)) == -1) 1921 error("%s: open %s: %s", __func__, 1922 _PATH_DEVNULL, strerror(errno)); 1923 if (dup2(devnull, STDOUT_FILENO) < 0) 1924 fatal("%s: dup2() stdout failed", __func__); 1925 if (devnull > STDERR_FILENO) 1926 close(devnull); 1927 } 1928 1929 /* 1930 * If requested and we are not interested in replies to remote 1931 * forwarding requests, then let ssh continue in the background. 1932 */ 1933 if (fork_after_authentication_flag) { 1934 if (options.exit_on_forward_failure && 1935 options.num_remote_forwards > 0) { 1936 debug("deferring postauth fork until remote forward " 1937 "confirmation received"); 1938 } else 1939 fork_postauth(); 1940 } 1941 1942 return client_loop(ssh, tty_flag, tty_flag ? 1943 options.escape_char : SSH_ESCAPECHAR_NONE, id); 1944 } 1945 1946 /* Loads all IdentityFile and CertificateFile keys */ 1947 static void 1948 load_public_identity_files(struct passwd *pw) 1949 { 1950 char *filename, *cp; 1951 struct sshkey *public; 1952 int i; 1953 u_int n_ids, n_certs; 1954 char *identity_files[SSH_MAX_IDENTITY_FILES]; 1955 struct sshkey *identity_keys[SSH_MAX_IDENTITY_FILES]; 1956 int identity_file_userprovided[SSH_MAX_IDENTITY_FILES]; 1957 char *certificate_files[SSH_MAX_CERTIFICATE_FILES]; 1958 struct sshkey *certificates[SSH_MAX_CERTIFICATE_FILES]; 1959 int certificate_file_userprovided[SSH_MAX_CERTIFICATE_FILES]; 1960 #ifdef ENABLE_PKCS11 1961 struct sshkey **keys; 1962 int nkeys; 1963 #endif /* PKCS11 */ 1964 1965 n_ids = n_certs = 0; 1966 memset(identity_files, 0, sizeof(identity_files)); 1967 memset(identity_keys, 0, sizeof(identity_keys)); 1968 memset(identity_file_userprovided, 0, 1969 sizeof(identity_file_userprovided)); 1970 memset(certificate_files, 0, sizeof(certificate_files)); 1971 memset(certificates, 0, sizeof(certificates)); 1972 memset(certificate_file_userprovided, 0, 1973 sizeof(certificate_file_userprovided)); 1974 1975 #ifdef ENABLE_PKCS11 1976 if (options.pkcs11_provider != NULL && 1977 options.num_identity_files < SSH_MAX_IDENTITY_FILES && 1978 (pkcs11_init(!options.batch_mode) == 0) && 1979 (nkeys = pkcs11_add_provider(options.pkcs11_provider, NULL, 1980 &keys)) > 0) { 1981 for (i = 0; i < nkeys; i++) { 1982 if (n_ids >= SSH_MAX_IDENTITY_FILES) { 1983 sshkey_free(keys[i]); 1984 continue; 1985 } 1986 identity_keys[n_ids] = keys[i]; 1987 identity_files[n_ids] = 1988 xstrdup(options.pkcs11_provider); /* XXX */ 1989 n_ids++; 1990 } 1991 free(keys); 1992 } 1993 #endif /* ENABLE_PKCS11 */ 1994 for (i = 0; i < options.num_identity_files; i++) { 1995 if (n_ids >= SSH_MAX_IDENTITY_FILES || 1996 strcasecmp(options.identity_files[i], "none") == 0) { 1997 free(options.identity_files[i]); 1998 options.identity_files[i] = NULL; 1999 continue; 2000 } 2001 cp = tilde_expand_filename(options.identity_files[i], getuid()); 2002 filename = percent_expand(cp, "d", pw->pw_dir, 2003 "u", pw->pw_name, "l", thishost, "h", host, 2004 "r", options.user, (char *)NULL); 2005 free(cp); 2006 check_load(sshkey_load_public(filename, &public, NULL), 2007 filename, "pubkey"); 2008 debug("identity file %s type %d", filename, 2009 public ? public->type : -1); 2010 free(options.identity_files[i]); 2011 identity_files[n_ids] = filename; 2012 identity_keys[n_ids] = public; 2013 identity_file_userprovided[n_ids] = 2014 options.identity_file_userprovided[i]; 2015 if (++n_ids >= SSH_MAX_IDENTITY_FILES) 2016 continue; 2017 2018 /* 2019 * If no certificates have been explicitly listed then try 2020 * to add the default certificate variant too. 2021 */ 2022 if (options.num_certificate_files != 0) 2023 continue; 2024 xasprintf(&cp, "%s-cert", filename); 2025 check_load(sshkey_load_public(cp, &public, NULL), 2026 filename, "pubkey"); 2027 debug("identity file %s type %d", cp, 2028 public ? public->type : -1); 2029 if (public == NULL) { 2030 free(cp); 2031 continue; 2032 } 2033 if (!sshkey_is_cert(public)) { 2034 debug("%s: key %s type %s is not a certificate", 2035 __func__, cp, sshkey_type(public)); 2036 sshkey_free(public); 2037 free(cp); 2038 continue; 2039 } 2040 /* NB. leave filename pointing to private key */ 2041 identity_files[n_ids] = xstrdup(filename); 2042 identity_keys[n_ids] = public; 2043 identity_file_userprovided[n_ids] = 2044 options.identity_file_userprovided[i]; 2045 n_ids++; 2046 } 2047 2048 if (options.num_certificate_files > SSH_MAX_CERTIFICATE_FILES) 2049 fatal("%s: too many certificates", __func__); 2050 for (i = 0; i < options.num_certificate_files; i++) { 2051 cp = tilde_expand_filename(options.certificate_files[i], 2052 getuid()); 2053 filename = percent_expand(cp, 2054 "d", pw->pw_dir, 2055 "h", host, 2056 "i", uidstr, 2057 "l", thishost, 2058 "r", options.user, 2059 "u", pw->pw_name, 2060 (char *)NULL); 2061 free(cp); 2062 2063 check_load(sshkey_load_public(filename, &public, NULL), 2064 filename, "certificate"); 2065 debug("certificate file %s type %d", filename, 2066 public ? public->type : -1); 2067 free(options.certificate_files[i]); 2068 options.certificate_files[i] = NULL; 2069 if (public == NULL) { 2070 free(filename); 2071 continue; 2072 } 2073 if (!sshkey_is_cert(public)) { 2074 debug("%s: key %s type %s is not a certificate", 2075 __func__, filename, sshkey_type(public)); 2076 sshkey_free(public); 2077 free(filename); 2078 continue; 2079 } 2080 certificate_files[n_certs] = filename; 2081 certificates[n_certs] = public; 2082 certificate_file_userprovided[n_certs] = 2083 options.certificate_file_userprovided[i]; 2084 ++n_certs; 2085 } 2086 2087 options.num_identity_files = n_ids; 2088 memcpy(options.identity_files, identity_files, sizeof(identity_files)); 2089 memcpy(options.identity_keys, identity_keys, sizeof(identity_keys)); 2090 memcpy(options.identity_file_userprovided, 2091 identity_file_userprovided, sizeof(identity_file_userprovided)); 2092 2093 options.num_certificate_files = n_certs; 2094 memcpy(options.certificate_files, 2095 certificate_files, sizeof(certificate_files)); 2096 memcpy(options.certificates, certificates, sizeof(certificates)); 2097 memcpy(options.certificate_file_userprovided, 2098 certificate_file_userprovided, 2099 sizeof(certificate_file_userprovided)); 2100 } 2101 2102 static void 2103 main_sigchld_handler(int sig) 2104 { 2105 int save_errno = errno; 2106 pid_t pid; 2107 int status; 2108 2109 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 || 2110 (pid < 0 && errno == EINTR)) 2111 ; 2112 errno = save_errno; 2113 } 2114