Annotation of src/usr.bin/ssh/ssh.c, Revision 1.400
1.400 ! djm 1: /* $OpenBSD: ssh.c,v 1.399 2014/02/04 00:24:29 djm Exp $ */
1.1 deraadt 2: /*
1.32 deraadt 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: *
1.64 deraadt 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.
1.202 markus 17: * Copyright (c) 2000, 2001, 2002, 2003 Markus Friedl. All rights reserved.
1.64 deraadt 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.
1.32 deraadt 41: */
1.1 deraadt 42:
1.293 deraadt 43: #include <sys/types.h>
1.312 djm 44: #include <sys/ioctl.h>
1.326 dtucker 45: #include <sys/param.h>
1.312 djm 46: #include <sys/queue.h>
1.259 stevesk 47: #include <sys/resource.h>
1.280 stevesk 48: #include <sys/socket.h>
1.264 stevesk 49: #include <sys/stat.h>
1.312 djm 50: #include <sys/types.h>
51: #include <sys/time.h>
1.352 djm 52: #include <sys/wait.h>
1.258 stevesk 53:
1.265 stevesk 54: #include <ctype.h>
1.285 stevesk 55: #include <errno.h>
1.281 stevesk 56: #include <fcntl.h>
1.286 stevesk 57: #include <netdb.h>
1.258 stevesk 58: #include <paths.h>
1.279 stevesk 59: #include <pwd.h>
1.263 stevesk 60: #include <signal.h>
1.287 stevesk 61: #include <stddef.h>
1.291 stevesk 62: #include <stdio.h>
1.290 stevesk 63: #include <stdlib.h>
1.289 stevesk 64: #include <string.h>
1.288 stevesk 65: #include <unistd.h>
1.49 markus 66:
67: #include <openssl/evp.h>
1.72 markus 68: #include <openssl/err.h>
1.1 deraadt 69:
1.293 deraadt 70: #include "xmalloc.h"
1.84 markus 71: #include "ssh.h"
72: #include "ssh1.h"
73: #include "ssh2.h"
1.341 djm 74: #include "canohost.h"
1.84 markus 75: #include "compat.h"
76: #include "cipher.h"
1.1 deraadt 77: #include "packet.h"
78: #include "buffer.h"
1.123 markus 79: #include "channels.h"
1.49 markus 80: #include "key.h"
1.58 markus 81: #include "authfd.h"
1.49 markus 82: #include "authfile.h"
1.83 markus 83: #include "pathnames.h"
1.214 djm 84: #include "dispatch.h"
1.81 markus 85: #include "clientloop.h"
1.84 markus 86: #include "log.h"
87: #include "readconf.h"
88: #include "sshconnect.h"
89: #include "misc.h"
1.95 markus 90: #include "kex.h"
91: #include "mac.h"
1.213 deraadt 92: #include "sshpty.h"
1.212 djm 93: #include "match.h"
1.214 djm 94: #include "msg.h"
1.225 dtucker 95: #include "uidswap.h"
1.327 andreas 96: #include "roaming.h"
1.278 stevesk 97: #include "version.h"
1.49 markus 98:
1.333 markus 99: #ifdef ENABLE_PKCS11
100: #include "ssh-pkcs11.h"
1.137 jakob 101: #endif
1.127 markus 102:
1.49 markus 103: extern char *__progname;
1.1 deraadt 104:
1.316 djm 105: /* Flag indicating whether debug mode is on. May be set on the command line. */
1.1 deraadt 106: int debug_flag = 0;
107:
1.359 djm 108: /* Flag indicating whether a tty should be requested */
1.1 deraadt 109: int tty_flag = 0;
110:
1.45 markus 111: /* don't exec a shell */
112: int no_shell_flag = 0;
113:
1.33 markus 114: /*
115: * Flag indicating that nothing should be read from stdin. This can be set
116: * on the command line.
117: */
1.1 deraadt 118: int stdin_null_flag = 0;
119:
1.33 markus 120: /*
1.344 djm 121: * Flag indicating that the current process should be backgrounded and
122: * a new slave launched in the foreground for ControlPersist.
123: */
124: int need_controlpersist_detach = 0;
125:
126: /* Copies of flags for ControlPersist foreground slave */
1.359 djm 127: int ostdin_null_flag, ono_shell_flag, otty_flag, orequest_tty;
1.344 djm 128:
129: /*
1.33 markus 130: * Flag indicating that ssh should fork after authentication. This is useful
1.172 deraadt 131: * so that the passphrase can be entered manually, and then ssh goes to the
1.33 markus 132: * background.
133: */
1.1 deraadt 134: int fork_after_authentication_flag = 0;
135:
1.331 dtucker 136: /* forward stdio to remote host and port */
137: char *stdio_forward_host = NULL;
138: int stdio_forward_port = 0;
139:
1.33 markus 140: /*
141: * General data structure for command line options and options configurable
142: * in configuration files. See readconf.h.
143: */
1.1 deraadt 144: Options options;
145:
1.139 markus 146: /* optional user configfile */
147: char *config = NULL;
148:
1.33 markus 149: /*
150: * Name of the host we are connecting to. This is the name given on the
151: * command line, or the HostName specified for the user-supplied name in a
152: * configuration file.
153: */
1.1 deraadt 154: char *host;
155:
1.22 provos 156: /* socket address the host resolves to */
1.37 markus 157: struct sockaddr_storage hostaddr;
1.1 deraadt 158:
1.112 markus 159: /* Private host keys. */
1.173 markus 160: Sensitive sensitive_data;
1.1 deraadt 161:
1.10 dugsong 162: /* Original real UID. */
163: uid_t original_real_uid;
1.177 markus 164: uid_t original_effective_uid;
1.1 deraadt 165:
1.45 markus 166: /* command to be executed */
167: Buffer command;
168:
1.85 djm 169: /* Should we execute a command or invoke a subsystem? */
170: int subsystem_flag = 0;
171:
1.170 markus 172: /* # of replies received for global requests */
1.315 djm 173: static int remote_forward_confirms_received = 0;
1.170 markus 174:
1.313 djm 175: /* mux.c */
176: extern int muxserver_sock;
177: extern u_int muxclient_command;
178:
1.186 djm 179:
1.1 deraadt 180: /* Prints a help message to the user. This function never returns. */
181:
1.126 itojun 182: static void
1.93 itojun 183: usage(void)
1.1 deraadt 184: {
1.208 markus 185: fprintf(stderr,
1.321 jmc 186: "usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]\n"
1.375 dtucker 187: " [-D [bind_address:]port] [-E log_file] [-e escape_char]\n"
188: " [-F configfile] [-I pkcs11] [-i identity_file]\n"
1.394 deraadt 189: " [-L [bind_address:]port:host:hostport] [-l login_name] [-m mac_spec]\n"
1.395 jmc 190: " [-O ctl_cmd] [-o option] [-p port]\n"
191: " [-Q cipher | cipher-auth | mac | kex | key]\n"
192: " [-R [bind_address:]port:host:hostport] [-S ctl_path] [-W host:port]\n"
193: " [-w local_tun[:remote_tun]] [user@]hostname [command]\n"
1.208 markus 194: );
1.257 dtucker 195: exit(255);
1.1 deraadt 196: }
197:
1.126 itojun 198: static int ssh_session(void);
199: static int ssh_session2(void);
200: static void load_public_identity_files(void);
1.352 djm 201: static void main_sigchld_handler(int);
1.312 djm 202:
203: /* from muxclient.c */
204: void muxclient(const char *);
205: void muxserver_listen(void);
1.45 markus 206:
1.361 djm 207: /* ~/ expand a list of paths. NB. assumes path[n] is heap-allocated. */
208: static void
209: tilde_expand_paths(char **paths, u_int num_paths)
210: {
211: u_int i;
212: char *cp;
213:
214: for (i = 0; i < num_paths; i++) {
215: cp = tilde_expand_filename(paths[i], original_real_uid);
1.378 djm 216: free(paths[i]);
1.361 djm 217: paths[i] = cp;
218: }
219: }
220:
1.400 ! djm 221: /*
! 222: * Attempt to resolve a host name / port to a set of addresses and
! 223: * optionally return any CNAMEs encountered along the way.
! 224: * Returns NULL on failure.
! 225: * NB. this function must operate with a options having undefined members.
! 226: */
1.385 djm 227: static struct addrinfo *
1.400 ! djm 228: resolve_host(const char *name, int port, int logerr, char *cname, size_t clen)
1.385 djm 229: {
230: char strport[NI_MAXSERV];
231: struct addrinfo hints, *res;
232: int gaierr, loglevel = SYSLOG_LEVEL_DEBUG1;
233:
1.400 ! djm 234: if (port <= 0)
! 235: port = default_ssh_port();
! 236:
1.385 djm 237: snprintf(strport, sizeof strport, "%u", port);
1.398 tedu 238: memset(&hints, 0, sizeof(hints));
1.400 ! djm 239: hints.ai_family = options.address_family == -1 ?
! 240: AF_UNSPEC : options.address_family;
1.385 djm 241: hints.ai_socktype = SOCK_STREAM;
242: if (cname != NULL)
243: hints.ai_flags = AI_CANONNAME;
244: if ((gaierr = getaddrinfo(name, strport, &hints, &res)) != 0) {
245: if (logerr || (gaierr != EAI_NONAME && gaierr != EAI_NODATA))
246: loglevel = SYSLOG_LEVEL_ERROR;
247: do_log2(loglevel, "%s: Could not resolve hostname %.100s: %s",
248: __progname, name, ssh_gai_strerror(gaierr));
249: return NULL;
250: }
251: if (cname != NULL && res->ai_canonname != NULL) {
252: if (strlcpy(cname, res->ai_canonname, clen) >= clen) {
253: error("%s: host \"%s\" cname \"%s\" too long (max %lu)",
254: __func__, name, res->ai_canonname, (u_long)clen);
255: if (clen > 0)
256: *cname = '\0';
257: }
258: }
259: return res;
260: }
261:
262: /*
263: * Check whether the cname is a permitted replacement for the hostname
264: * and perform the replacement if it is.
1.400 ! djm 265: * NB. this function must operate with a options having undefined members.
1.385 djm 266: */
267: static int
268: check_follow_cname(char **namep, const char *cname)
269: {
270: int i;
271: struct allowed_cname *rule;
272:
273: if (*cname == '\0' || options.num_permitted_cnames == 0 ||
274: strcmp(*namep, cname) == 0)
275: return 0;
1.386 djm 276: if (options.canonicalize_hostname == SSH_CANONICALISE_NO)
1.385 djm 277: return 0;
278: /*
1.386 djm 279: * Don't attempt to canonicalize names that will be interpreted by
1.385 djm 280: * a proxy unless the user specifically requests so.
281: */
1.400 ! djm 282: if (!option_clear_or_none(options.proxy_command) &&
1.386 djm 283: options.canonicalize_hostname != SSH_CANONICALISE_ALWAYS)
1.385 djm 284: return 0;
285: debug3("%s: check \"%s\" CNAME \"%s\"", __func__, *namep, cname);
286: for (i = 0; i < options.num_permitted_cnames; i++) {
287: rule = options.permitted_cnames + i;
288: if (match_pattern_list(*namep, rule->source_list,
289: strlen(rule->source_list), 1) != 1 ||
290: match_pattern_list(cname, rule->target_list,
291: strlen(rule->target_list), 1) != 1)
292: continue;
1.386 djm 293: verbose("Canonicalized DNS aliased hostname "
1.385 djm 294: "\"%s\" => \"%s\"", *namep, cname);
295: free(*namep);
296: *namep = xstrdup(cname);
297: return 1;
298: }
299: return 0;
300: }
301:
302: /*
303: * Attempt to resolve the supplied hostname after applying the user's
1.387 djm 304: * canonicalization rules. Returns the address list for the host or NULL
305: * if no name was found after canonicalization.
1.400 ! djm 306: * NB. this function must operate with a options having undefined members.
1.385 djm 307: */
308: static struct addrinfo *
1.400 ! djm 309: resolve_canonicalize(char **hostp, int port)
1.385 djm 310: {
311: int i, ndots;
312: char *cp, *fullhost, cname_target[NI_MAXHOST];
313: struct addrinfo *addrs;
314:
1.386 djm 315: if (options.canonicalize_hostname == SSH_CANONICALISE_NO)
1.385 djm 316: return NULL;
1.400 ! djm 317:
1.385 djm 318: /*
1.386 djm 319: * Don't attempt to canonicalize names that will be interpreted by
1.385 djm 320: * a proxy unless the user specifically requests so.
321: */
1.400 ! djm 322: if (!option_clear_or_none(options.proxy_command) &&
1.386 djm 323: options.canonicalize_hostname != SSH_CANONICALISE_ALWAYS)
1.385 djm 324: return NULL;
1.400 ! djm 325:
1.387 djm 326: /* Don't apply canonicalization to sufficiently-qualified hostnames */
1.385 djm 327: ndots = 0;
328: for (cp = *hostp; *cp != '\0'; cp++) {
329: if (*cp == '.')
330: ndots++;
331: }
1.386 djm 332: if (ndots > options.canonicalize_max_dots) {
333: debug3("%s: not canonicalizing hostname \"%s\" (max dots %d)",
334: __func__, *hostp, options.canonicalize_max_dots);
1.385 djm 335: return NULL;
336: }
337: /* Attempt each supplied suffix */
338: for (i = 0; i < options.num_canonical_domains; i++) {
339: *cname_target = '\0';
340: xasprintf(&fullhost, "%s.%s.", *hostp,
341: options.canonical_domains[i]);
1.400 ! djm 342: debug3("%s: attempting \"%s\" => \"%s\"", __func__,
! 343: *hostp, fullhost);
! 344: if ((addrs = resolve_host(fullhost, port, 0,
1.385 djm 345: cname_target, sizeof(cname_target))) == NULL) {
346: free(fullhost);
347: continue;
348: }
349: /* Remove trailing '.' */
350: fullhost[strlen(fullhost) - 1] = '\0';
351: /* Follow CNAME if requested */
352: if (!check_follow_cname(&fullhost, cname_target)) {
1.386 djm 353: debug("Canonicalized hostname \"%s\" => \"%s\"",
1.385 djm 354: *hostp, fullhost);
355: }
356: free(*hostp);
357: *hostp = fullhost;
358: return addrs;
359: }
1.386 djm 360: if (!options.canonicalize_fallback_local)
1.400 ! djm 361: fatal("%s: Could not resolve host \"%s\"", __progname, *hostp);
! 362: debug2("%s: host %s not found in any suffix", __func__, *hostp);
1.385 djm 363: return NULL;
364: }
365:
1.32 deraadt 366: /*
1.400 ! djm 367: * Read per-user configuration file. Ignore the system wide config
! 368: * file if the user specifies a config file on the command line.
! 369: */
! 370: static void
! 371: process_config_files(struct passwd *pw)
! 372: {
! 373: char buf[MAXPATHLEN];
! 374: int r;
! 375:
! 376: if (config != NULL) {
! 377: if (strcasecmp(config, "none") != 0 &&
! 378: !read_config_file(config, pw, host, &options,
! 379: SSHCONF_USERCONF))
! 380: fatal("Can't open user config file %.100s: "
! 381: "%.100s", config, strerror(errno));
! 382: } else {
! 383: r = snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir,
! 384: _PATH_SSH_USER_CONFFILE);
! 385: if (r > 0 && (size_t)r < sizeof(buf))
! 386: (void)read_config_file(buf, pw, host, &options,
! 387: SSHCONF_CHECKPERM|SSHCONF_USERCONF);
! 388:
! 389: /* Read systemwide configuration file after user config. */
! 390: (void)read_config_file(_PATH_HOST_CONFIG_FILE, pw, host,
! 391: &options, 0);
! 392: }
! 393: }
! 394:
! 395: /*
1.32 deraadt 396: * Main program for the ssh client.
397: */
1.2 provos 398: int
399: main(int ac, char **av)
1.1 deraadt 400: {
1.326 dtucker 401: int i, r, opt, exit_status, use_syslog;
1.375 dtucker 402: char *p, *cp, *line, *argv0, buf[MAXPATHLEN], *host_arg, *logfile;
1.358 djm 403: char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
1.385 djm 404: char cname[NI_MAXHOST];
1.31 markus 405: struct stat st;
1.98 markus 406: struct passwd *pw;
1.382 djm 407: int timeout_ms;
1.144 stevesk 408: extern int optind, optreset;
409: extern char *optarg;
1.232 djm 410: Forward fwd;
1.385 djm 411: struct addrinfo *addrs = NULL;
1.250 djm 412:
413: /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
414: sanitise_stdfd();
1.31 markus 415:
1.33 markus 416: /*
1.346 djm 417: * Discard other fds that are hanging around. These can cause problem
418: * with backgrounded ssh processes started by ControlPersist.
419: */
420: closefrom(STDERR_FILENO + 1);
421:
422: /*
1.33 markus 423: * Save the original real uid. It will be needed later (uid-swapping
424: * may clobber the real uid).
425: */
1.31 markus 426: original_real_uid = getuid();
427: original_effective_uid = geteuid();
428:
1.184 stevesk 429: /*
430: * Use uid-swapping to give up root privileges for the duration of
431: * option processing. We will re-instantiate the rights when we are
432: * ready to create the privileged port, and will permanently drop
433: * them when the port has been created (actually, when the connection
434: * has been made, as we may need to create the port several times).
435: */
436: PRIV_END;
437:
1.31 markus 438: /* If we are installed setuid root be careful to not drop core. */
439: if (original_real_uid != original_effective_uid) {
440: struct rlimit rlim;
441: rlim.rlim_cur = rlim.rlim_max = 0;
442: if (setrlimit(RLIMIT_CORE, &rlim) < 0)
443: fatal("setrlimit failed: %.100s", strerror(errno));
1.1 deraadt 444: }
1.107 markus 445: /* Get user data. */
446: pw = getpwuid(original_real_uid);
447: if (!pw) {
1.380 djm 448: logit("No user exists for uid %lu", (u_long)original_real_uid);
1.257 dtucker 449: exit(255);
1.107 markus 450: }
451: /* Take a copy of the returned structure. */
452: pw = pwcopy(pw);
1.31 markus 453:
1.33 markus 454: /*
455: * Set our umask to something reasonable, as some files are created
456: * with the default umask. This will make them world-readable but
457: * writable only by the owner, which is ok for all files for which we
458: * don't set the modes explicitly.
459: */
1.31 markus 460: umask(022);
461:
1.316 djm 462: /*
463: * Initialize option structure to indicate that no values have been
464: * set.
465: */
1.31 markus 466: initialize_options(&options);
467:
468: /* Parse command-line arguments. */
469: host = NULL;
1.320 djm 470: use_syslog = 0;
1.375 dtucker 471: logfile = NULL;
1.325 markus 472: argv0 = av[0];
1.31 markus 473:
1.266 djm 474: again:
1.316 djm 475: while ((opt = getopt(ac, av, "1246ab:c:e:fgi:kl:m:no:p:qstvx"
1.376 djm 476: "ACD:E:F:I:KL:MNO:PQ:R:S:TVw:W:XYy")) != -1) {
1.31 markus 477: switch (opt) {
1.91 jakob 478: case '1':
479: options.protocol = SSH_PROTO_1;
480: break;
1.47 markus 481: case '2':
482: options.protocol = SSH_PROTO_2;
483: break;
1.37 markus 484: case '4':
1.196 djm 485: options.address_family = AF_INET;
1.37 markus 486: break;
487: case '6':
1.196 djm 488: options.address_family = AF_INET6;
1.37 markus 489: break;
1.31 markus 490: case 'n':
491: stdin_null_flag = 1;
492: break;
493: case 'f':
494: fork_after_authentication_flag = 1;
495: stdin_null_flag = 1;
496: break;
497: case 'x':
498: options.forward_x11 = 0;
499: break;
500: case 'X':
501: options.forward_x11 = 1;
502: break;
1.320 djm 503: case 'y':
504: use_syslog = 1;
505: break;
1.375 dtucker 506: case 'E':
507: logfile = xstrdup(optarg);
508: break;
1.202 markus 509: case 'Y':
510: options.forward_x11 = 1;
511: options.forward_x11_trusted = 1;
512: break;
1.31 markus 513: case 'g':
514: options.gateway_ports = 1;
515: break;
1.229 djm 516: case 'O':
1.332 djm 517: if (stdio_forward_host != NULL)
518: fatal("Cannot specify multiplexing "
519: "command with -W");
520: else if (muxclient_command != 0)
521: fatal("Multiplexing command already specified");
1.229 djm 522: if (strcmp(optarg, "check") == 0)
1.312 djm 523: muxclient_command = SSHMUX_COMMAND_ALIVE_CHECK;
1.338 markus 524: else if (strcmp(optarg, "forward") == 0)
525: muxclient_command = SSHMUX_COMMAND_FORWARD;
1.229 djm 526: else if (strcmp(optarg, "exit") == 0)
1.312 djm 527: muxclient_command = SSHMUX_COMMAND_TERMINATE;
1.357 djm 528: else if (strcmp(optarg, "stop") == 0)
529: muxclient_command = SSHMUX_COMMAND_STOP;
1.365 djm 530: else if (strcmp(optarg, "cancel") == 0)
531: muxclient_command = SSHMUX_COMMAND_CANCEL_FWD;
1.229 djm 532: else
533: fatal("Invalid multiplex command.");
534: break;
1.183 stevesk 535: case 'P': /* deprecated */
1.31 markus 536: options.use_privileged_port = 0;
1.376 djm 537: break;
1.394 deraadt 538: case 'Q':
1.376 djm 539: cp = NULL;
1.394 deraadt 540: if (strcmp(optarg, "cipher") == 0)
1.393 djm 541: cp = cipher_alg_list('\n', 0);
1.394 deraadt 542: else if (strcmp(optarg, "cipher-auth") == 0)
1.393 djm 543: cp = cipher_alg_list('\n', 1);
1.394 deraadt 544: else if (strcmp(optarg, "mac") == 0)
1.392 dtucker 545: cp = mac_alg_list('\n');
1.394 deraadt 546: else if (strcmp(optarg, "kex") == 0)
1.392 dtucker 547: cp = kex_alg_list('\n');
1.394 deraadt 548: else if (strcmp(optarg, "key") == 0)
1.396 markus 549: cp = key_alg_list(0, 0);
550: else if (strcmp(optarg, "key-cert") == 0)
551: cp = key_alg_list(1, 0);
552: else if (strcmp(optarg, "key-plain") == 0)
553: cp = key_alg_list(0, 1);
1.376 djm 554: if (cp == NULL)
555: fatal("Unsupported query \"%s\"", optarg);
556: printf("%s\n", cp);
557: free(cp);
558: exit(0);
1.31 markus 559: break;
560: case 'a':
561: options.forward_agent = 0;
1.53 markus 562: break;
563: case 'A':
564: options.forward_agent = 1;
1.31 markus 565: break;
566: case 'k':
1.204 dtucker 567: options.gss_deleg_creds = 0;
1.297 djm 568: break;
569: case 'K':
570: options.gss_authentication = 1;
571: options.gss_deleg_creds = 1;
1.31 markus 572: break;
573: case 'i':
574: if (stat(optarg, &st) < 0) {
1.128 fgsch 575: fprintf(stderr, "Warning: Identity file %s "
1.231 otto 576: "not accessible: %s.\n", optarg,
577: strerror(errno));
1.31 markus 578: break;
579: }
1.371 dtucker 580: add_identity_file(&options, NULL, optarg, 1);
1.31 markus 581: break;
1.127 markus 582: case 'I':
1.333 markus 583: #ifdef ENABLE_PKCS11
584: options.pkcs11_provider = xstrdup(optarg);
1.137 jakob 585: #else
1.333 markus 586: fprintf(stderr, "no support for PKCS#11.\n");
1.137 jakob 587: #endif
1.127 markus 588: break;
1.31 markus 589: case 't':
1.359 djm 590: if (options.request_tty == REQUEST_TTY_YES)
591: options.request_tty = REQUEST_TTY_FORCE;
592: else
593: options.request_tty = REQUEST_TTY_YES;
1.31 markus 594: break;
595: case 'v':
1.197 markus 596: if (debug_flag == 0) {
1.66 markus 597: debug_flag = 1;
598: options.log_level = SYSLOG_LEVEL_DEBUG1;
1.197 markus 599: } else {
600: if (options.log_level < SYSLOG_LEVEL_DEBUG3)
601: options.log_level++;
602: }
1.375 dtucker 603: break;
1.31 markus 604: case 'V':
1.209 markus 605: fprintf(stderr, "%s, %s\n",
606: SSH_VERSION, SSLeay_version(SSLEAY_VERSION));
1.31 markus 607: if (opt == 'V')
608: exit(0);
609: break;
1.255 reyk 610: case 'w':
1.256 reyk 611: if (options.tun_open == -1)
612: options.tun_open = SSH_TUNMODE_DEFAULT;
1.255 reyk 613: options.tun_local = a2tun(optarg, &options.tun_remote);
1.256 reyk 614: if (options.tun_local == SSH_TUNID_ERR) {
1.316 djm 615: fprintf(stderr,
616: "Bad tun device '%s'\n", optarg);
1.257 dtucker 617: exit(255);
1.255 reyk 618: }
619: break;
1.331 dtucker 620: case 'W':
1.332 djm 621: if (stdio_forward_host != NULL)
622: fatal("stdio forward already specified");
623: if (muxclient_command != 0)
624: fatal("Cannot specify stdio forward with -O");
1.331 dtucker 625: if (parse_forward(&fwd, optarg, 1, 0)) {
626: stdio_forward_host = fwd.listen_host;
627: stdio_forward_port = fwd.listen_port;
1.378 djm 628: free(fwd.connect_host);
1.331 dtucker 629: } else {
630: fprintf(stderr,
631: "Bad stdio forwarding specification '%s'\n",
632: optarg);
633: exit(255);
634: }
1.359 djm 635: options.request_tty = REQUEST_TTY_NO;
1.331 dtucker 636: no_shell_flag = 1;
637: options.clear_forwardings = 1;
638: options.exit_on_forward_failure = 1;
639: break;
1.31 markus 640: case 'q':
641: options.log_level = SYSLOG_LEVEL_QUIET;
642: break;
643: case 'e':
644: if (optarg[0] == '^' && optarg[2] == 0 &&
1.128 fgsch 645: (u_char) optarg[1] >= 64 &&
646: (u_char) optarg[1] < 128)
1.78 markus 647: options.escape_char = (u_char) optarg[1] & 31;
1.31 markus 648: else if (strlen(optarg) == 1)
1.78 markus 649: options.escape_char = (u_char) optarg[0];
1.31 markus 650: else if (strcmp(optarg, "none") == 0)
1.119 stevesk 651: options.escape_char = SSH_ESCAPECHAR_NONE;
1.31 markus 652: else {
1.128 fgsch 653: fprintf(stderr, "Bad escape character '%s'.\n",
654: optarg);
1.257 dtucker 655: exit(255);
1.31 markus 656: }
657: break;
658: case 'c':
1.49 markus 659: if (ciphers_valid(optarg)) {
660: /* SSH2 only */
661: options.ciphers = xstrdup(optarg);
1.224 markus 662: options.cipher = SSH_CIPHER_INVALID;
1.49 markus 663: } else {
664: /* SSH1 only */
1.74 markus 665: options.cipher = cipher_number(optarg);
666: if (options.cipher == -1) {
1.128 fgsch 667: fprintf(stderr,
668: "Unknown cipher type '%s'\n",
669: optarg);
1.257 dtucker 670: exit(255);
1.49 markus 671: }
1.128 fgsch 672: if (options.cipher == SSH_CIPHER_3DES)
1.74 markus 673: options.ciphers = "3des-cbc";
1.128 fgsch 674: else if (options.cipher == SSH_CIPHER_BLOWFISH)
1.74 markus 675: options.ciphers = "blowfish-cbc";
1.128 fgsch 676: else
1.74 markus 677: options.ciphers = (char *)-1;
1.95 markus 678: }
679: break;
680: case 'm':
681: if (mac_valid(optarg))
682: options.macs = xstrdup(optarg);
683: else {
1.128 fgsch 684: fprintf(stderr, "Unknown mac type '%s'\n",
685: optarg);
1.257 dtucker 686: exit(255);
1.31 markus 687: }
688: break;
1.214 djm 689: case 'M':
1.242 djm 690: if (options.control_master == SSHCTL_MASTER_YES)
691: options.control_master = SSHCTL_MASTER_ASK;
692: else
693: options.control_master = SSHCTL_MASTER_YES;
1.214 djm 694: break;
1.31 markus 695: case 'p':
1.113 stevesk 696: options.port = a2port(optarg);
1.323 djm 697: if (options.port <= 0) {
1.109 markus 698: fprintf(stderr, "Bad port '%s'\n", optarg);
1.257 dtucker 699: exit(255);
1.109 markus 700: }
1.31 markus 701: break;
702: case 'l':
703: options.user = optarg;
704: break;
1.141 stevesk 705:
706: case 'L':
1.324 djm 707: if (parse_forward(&fwd, optarg, 0, 0))
1.232 djm 708: add_local_forward(&options, &fwd);
709: else {
1.128 fgsch 710: fprintf(stderr,
1.232 djm 711: "Bad local forwarding specification '%s'\n",
1.128 fgsch 712: optarg);
1.257 dtucker 713: exit(255);
1.31 markus 714: }
1.232 djm 715: break;
716:
717: case 'R':
1.324 djm 718: if (parse_forward(&fwd, optarg, 0, 1)) {
1.232 djm 719: add_remote_forward(&options, &fwd);
720: } else {
1.128 fgsch 721: fprintf(stderr,
1.232 djm 722: "Bad remote forwarding specification "
723: "'%s'\n", optarg);
1.257 dtucker 724: exit(255);
1.31 markus 725: }
726: break;
1.108 markus 727:
728: case 'D':
1.324 djm 729: if (parse_forward(&fwd, optarg, 1, 0)) {
1.322 stevesk 730: add_local_forward(&options, &fwd);
1.232 djm 731: } else {
1.322 stevesk 732: fprintf(stderr,
733: "Bad dynamic forwarding specification "
734: "'%s'\n", optarg);
1.257 dtucker 735: exit(255);
1.109 markus 736: }
1.108 markus 737: break;
738:
1.31 markus 739: case 'C':
740: options.compression = 1;
741: break;
1.45 markus 742: case 'N':
743: no_shell_flag = 1;
1.359 djm 744: options.request_tty = REQUEST_TTY_NO;
1.45 markus 745: break;
746: case 'T':
1.359 djm 747: options.request_tty = REQUEST_TTY_NO;
1.45 markus 748: break;
1.31 markus 749: case 'o':
1.205 markus 750: line = xstrdup(optarg);
1.382 djm 751: if (process_config_line(&options, pw, host ? host : "",
752: line, "command-line", 0, NULL, SSHCONF_USERCONF)
1.372 dtucker 753: != 0)
1.257 dtucker 754: exit(255);
1.378 djm 755: free(line);
1.31 markus 756: break;
1.85 djm 757: case 's':
758: subsystem_flag = 1;
1.117 markus 759: break;
1.214 djm 760: case 'S':
761: if (options.control_path != NULL)
762: free(options.control_path);
763: options.control_path = xstrdup(optarg);
764: break;
1.117 markus 765: case 'b':
766: options.bind_address = optarg;
1.85 djm 767: break;
1.139 markus 768: case 'F':
769: config = optarg;
770: break;
1.31 markus 771: default:
772: usage();
1.1 deraadt 773: }
1.31 markus 774: }
775:
1.128 fgsch 776: ac -= optind;
777: av += optind;
778:
1.329 guenther 779: if (ac > 0 && !host) {
1.188 markus 780: if (strrchr(*av, '@')) {
1.128 fgsch 781: p = xstrdup(*av);
1.188 markus 782: cp = strrchr(p, '@');
1.128 fgsch 783: if (cp == NULL || cp == p)
784: usage();
785: options.user = p;
786: *cp = '\0';
1.385 djm 787: host = xstrdup(++cp);
1.128 fgsch 788: } else
1.385 djm 789: host = xstrdup(*av);
1.189 millert 790: if (ac > 1) {
791: optind = optreset = 1;
1.128 fgsch 792: goto again;
793: }
1.189 millert 794: ac--, av++;
1.128 fgsch 795: }
796:
1.31 markus 797: /* Check that we got a host name. */
798: if (!host)
799: usage();
800:
1.385 djm 801: host_arg = xstrdup(host);
802:
1.350 djm 803: OpenSSL_add_all_algorithms();
1.72 markus 804: ERR_load_crypto_strings();
1.31 markus 805:
806: /* Initialize the command to execute on remote host. */
807: buffer_init(&command);
1.1 deraadt 808:
1.33 markus 809: /*
810: * Save the command to execute on the remote host in a buffer. There
811: * is no limit on the length of the command, except by the maximum
812: * packet size. Also sets the tty flag if there is no command.
813: */
1.128 fgsch 814: if (!ac) {
1.31 markus 815: /* No command specified - execute shell on a tty. */
1.85 djm 816: if (subsystem_flag) {
1.128 fgsch 817: fprintf(stderr,
818: "You must specify a subsystem to invoke.\n");
1.85 djm 819: usage();
820: }
1.31 markus 821: } else {
1.128 fgsch 822: /* A command has been specified. Store it into the buffer. */
823: for (i = 0; i < ac; i++) {
824: if (i)
1.31 markus 825: buffer_append(&command, " ", 1);
826: buffer_append(&command, av[i], strlen(av[i]));
827: }
828: }
829:
830: /* Cannot fork to background if no command. */
1.316 djm 831: if (fork_after_authentication_flag && buffer_len(&command) == 0 &&
832: !no_shell_flag)
833: fatal("Cannot fork into background without a command "
834: "to execute.");
1.31 markus 835:
1.101 markus 836: /*
837: * Initialize "log" output. Since we are the client all output
1.375 dtucker 838: * goes to stderr unless otherwise specified by -y or -E.
1.101 markus 839: */
1.375 dtucker 840: if (use_syslog && logfile != NULL)
841: fatal("Can't specify both -y and -E");
842: if (logfile != NULL) {
843: log_redirect_stderr_to(logfile);
1.378 djm 844: free(logfile);
1.375 dtucker 845: }
1.325 markus 846: log_init(argv0,
1.316 djm 847: options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
1.320 djm 848: SYSLOG_FACILITY_USER, !use_syslog);
1.375 dtucker 849:
850: if (debug_flag)
851: logit("%s, %s", SSH_VERSION, SSLeay_version(SSLEAY_VERSION));
1.31 markus 852:
1.400 ! djm 853: /* Parse the configuration files */
! 854: process_config_files(pw);
! 855:
! 856: /* Hostname canonicalisation needs a few options filled. */
! 857: fill_default_options_for_canonicalization(&options);
! 858:
! 859: /* If the user has replaced the hostname then take it into use now */
! 860: if (options.hostname != NULL) {
! 861: /* NB. Please keep in sync with readconf.c:match_cfg_line() */
! 862: cp = percent_expand(options.hostname,
! 863: "h", host, (char *)NULL);
! 864: free(host);
! 865: host = cp;
! 866: }
! 867:
! 868: /* If canonicalization requested then try to apply it */
! 869: lowercase(host);
! 870: if (options.canonicalize_hostname != SSH_CANONICALISE_NO)
! 871: addrs = resolve_canonicalize(&host, options.port);
! 872:
1.139 markus 873: /*
1.400 ! djm 874: * If canonicalization not requested, or if it failed then try to
! 875: * resolve the bare hostname name using the system resolver's usual
! 876: * search rules. Skip the lookup if a ProxyCommand is being used
! 877: * unless the user has specifically requested canonicalisation.
1.139 markus 878: */
1.400 ! djm 879: if (addrs == NULL && (option_clear_or_none(options.proxy_command) ||
! 880: options.canonicalize_hostname == SSH_CANONICALISE_ALWAYS)) {
! 881: if ((addrs = resolve_host(host, options.port, 1,
! 882: cname, sizeof(cname))) == NULL)
! 883: cleanup_exit(255); /* resolve_host logs the error */
! 884: check_follow_cname(&host, cname);
! 885: }
1.139 markus 886:
1.400 ! djm 887: /*
! 888: * If the target hostname has changed as a result of canonicalisation
! 889: * then re-parse the configuration files as new stanzas may match.
! 890: */
! 891: if (strcasecmp(host_arg, host) != 0) {
! 892: debug("Hostname has changed; re-reading configuration");
! 893: process_config_files(pw);
1.139 markus 894: }
1.31 markus 895:
896: /* Fill configuration defaults. */
897: fill_default_options(&options);
898:
1.400 ! djm 899: if (options.port == 0)
! 900: options.port = default_ssh_port();
1.196 djm 901: channel_set_af(options.address_family);
902:
1.383 djm 903: /* Tidy and check options */
904: if (options.host_key_alias != NULL)
905: lowercase(options.host_key_alias);
906: if (options.proxy_command != NULL &&
907: strcmp(options.proxy_command, "-") == 0 &&
908: options.proxy_use_fdpass)
909: fatal("ProxyCommand=- and ProxyUseFDPass are incompatible");
1.388 djm 910: if (original_effective_uid != 0)
911: options.use_privileged_port = 0;
1.383 djm 912:
1.31 markus 913: /* reinit */
1.325 markus 914: log_init(argv0, options.log_level, SYSLOG_FACILITY_USER, !use_syslog);
1.370 djm 915:
916: if (options.request_tty == REQUEST_TTY_YES ||
917: options.request_tty == REQUEST_TTY_FORCE)
918: tty_flag = 1;
919:
920: /* Allocate a tty by default if no command specified. */
921: if (buffer_len(&command) == 0)
922: tty_flag = options.request_tty != REQUEST_TTY_NO;
923:
924: /* Force no tty */
925: if (options.request_tty == REQUEST_TTY_NO || muxclient_command != 0)
926: tty_flag = 0;
927: /* Do not allocate a tty if stdin is not a tty. */
928: if ((!isatty(fileno(stdin)) || stdin_null_flag) &&
929: options.request_tty != REQUEST_TTY_FORCE) {
930: if (tty_flag)
931: logit("Pseudo-terminal will not be allocated because "
932: "stdin is not a terminal.");
933: tty_flag = 0;
934: }
1.31 markus 935:
936: if (options.user == NULL)
937: options.user = xstrdup(pw->pw_name);
1.343 djm 938:
1.358 djm 939: if (gethostname(thishost, sizeof(thishost)) == -1)
940: fatal("gethostname: %s", strerror(errno));
941: strlcpy(shorthost, thishost, sizeof(shorthost));
942: shorthost[strcspn(thishost, ".")] = '\0';
943: snprintf(portstr, sizeof(portstr), "%d", options.port);
944:
1.317 dtucker 945: if (options.local_command != NULL) {
946: debug3("expanding LocalCommand: %s", options.local_command);
947: cp = options.local_command;
948: options.local_command = percent_expand(cp, "d", pw->pw_dir,
1.356 djm 949: "h", host, "l", thishost, "n", host_arg, "r", options.user,
1.358 djm 950: "p", portstr, "u", pw->pw_name, "L", shorthost,
951: (char *)NULL);
1.317 dtucker 952: debug3("expanded LocalCommand: %s", options.local_command);
1.378 djm 953: free(cp);
1.304 dtucker 954: }
1.31 markus 955:
1.214 djm 956: if (options.control_path != NULL) {
1.241 djm 957: cp = tilde_expand_filename(options.control_path,
958: original_real_uid);
1.378 djm 959: free(options.control_path);
1.358 djm 960: options.control_path = percent_expand(cp, "h", host,
961: "l", thishost, "n", host_arg, "r", options.user,
962: "p", portstr, "u", pw->pw_name, "L", shorthost,
963: (char *)NULL);
1.378 djm 964: free(cp);
1.214 djm 965: }
1.312 djm 966: if (muxclient_command != 0 && options.control_path == NULL)
1.240 djm 967: fatal("No ControlPath specified for \"-O\" command");
1.242 djm 968: if (options.control_path != NULL)
1.312 djm 969: muxclient(options.control_path);
1.214 djm 970:
1.303 djm 971: timeout_ms = options.connection_timeout * 1000;
972:
1.77 markus 973: /* Open a connection to the remote host. */
1.385 djm 974: if (ssh_connect(host, addrs, &hostaddr, options.port,
975: options.address_family, options.connection_attempts,
976: &timeout_ms, options.tcp_keep_alive,
1.388 djm 977: options.use_privileged_port) != 0)
1.257 dtucker 978: exit(255);
1.31 markus 979:
1.391 djm 980: if (addrs != NULL)
981: freeaddrinfo(addrs);
982:
1.385 djm 983: packet_set_timeout(options.server_alive_interval,
984: options.server_alive_count_max);
985:
1.303 djm 986: if (timeout_ms > 0)
987: debug3("timeout: %d ms remain after connect", timeout_ms);
988:
1.33 markus 989: /*
990: * If we successfully made the connection, load the host private key
991: * in case we will need it later for combined rsa-rhosts
992: * authentication. This must be done before releasing extra
993: * privileges, because the file is only readable by root.
1.174 markus 994: * If we cannot access the private keys, load the public keys
995: * instead and try to execute the ssh-keysign helper instead.
1.33 markus 996: */
1.112 markus 997: sensitive_data.nkeys = 0;
998: sensitive_data.keys = NULL;
1.173 markus 999: sensitive_data.external_keysign = 0;
1.178 markus 1000: if (options.rhosts_rsa_authentication ||
1001: options.hostbased_authentication) {
1.397 djm 1002: sensitive_data.nkeys = 9;
1.274 deraadt 1003: sensitive_data.keys = xcalloc(sensitive_data.nkeys,
1.180 deraadt 1004: sizeof(Key));
1.177 markus 1005:
1006: PRIV_START;
1.112 markus 1007: sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
1.276 dtucker 1008: _PATH_HOST_KEY_FILE, "", NULL, NULL);
1.345 djm 1009: sensitive_data.keys[1] = key_load_private_cert(KEY_DSA,
1010: _PATH_HOST_DSA_KEY_FILE, "", NULL);
1.349 djm 1011: sensitive_data.keys[2] = key_load_private_cert(KEY_ECDSA,
1012: _PATH_HOST_ECDSA_KEY_FILE, "", NULL);
1013: sensitive_data.keys[3] = key_load_private_cert(KEY_RSA,
1.345 djm 1014: _PATH_HOST_RSA_KEY_FILE, "", NULL);
1.397 djm 1015: sensitive_data.keys[4] = key_load_private_cert(KEY_ED25519,
1016: _PATH_HOST_ED25519_KEY_FILE, "", NULL);
1017: sensitive_data.keys[5] = key_load_private_type(KEY_DSA,
1.276 dtucker 1018: _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL);
1.397 djm 1019: sensitive_data.keys[6] = key_load_private_type(KEY_ECDSA,
1.349 djm 1020: _PATH_HOST_ECDSA_KEY_FILE, "", NULL, NULL);
1.397 djm 1021: sensitive_data.keys[7] = key_load_private_type(KEY_RSA,
1.276 dtucker 1022: _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL);
1.397 djm 1023: sensitive_data.keys[8] = key_load_private_type(KEY_ED25519,
1.396 markus 1024: _PATH_HOST_ED25519_KEY_FILE, "", NULL, NULL);
1.177 markus 1025: PRIV_END;
1.173 markus 1026:
1.181 markus 1027: if (options.hostbased_authentication == 1 &&
1028: sensitive_data.keys[0] == NULL &&
1.349 djm 1029: sensitive_data.keys[5] == NULL &&
1.396 markus 1030: sensitive_data.keys[6] == NULL &&
1.397 djm 1031: sensitive_data.keys[7] == NULL &&
1032: sensitive_data.keys[8] == NULL) {
1.345 djm 1033: sensitive_data.keys[1] = key_load_cert(
1034: _PATH_HOST_DSA_KEY_FILE);
1035: sensitive_data.keys[2] = key_load_cert(
1.349 djm 1036: _PATH_HOST_ECDSA_KEY_FILE);
1037: sensitive_data.keys[3] = key_load_cert(
1.345 djm 1038: _PATH_HOST_RSA_KEY_FILE);
1.397 djm 1039: sensitive_data.keys[4] = key_load_cert(
1040: _PATH_HOST_ED25519_KEY_FILE);
1041: sensitive_data.keys[5] = key_load_public(
1.173 markus 1042: _PATH_HOST_DSA_KEY_FILE, NULL);
1.397 djm 1043: sensitive_data.keys[6] = key_load_public(
1.349 djm 1044: _PATH_HOST_ECDSA_KEY_FILE, NULL);
1.397 djm 1045: sensitive_data.keys[7] = key_load_public(
1.173 markus 1046: _PATH_HOST_RSA_KEY_FILE, NULL);
1.397 djm 1047: sensitive_data.keys[8] = key_load_public(
1.396 markus 1048: _PATH_HOST_ED25519_KEY_FILE, NULL);
1.173 markus 1049: sensitive_data.external_keysign = 1;
1050: }
1.31 markus 1051: }
1.33 markus 1052: /*
1053: * Get rid of any extra privileges that we may have. We will no
1054: * longer need them. Also, extra privileges could make it very hard
1055: * to read identity files and other non-world-readable files from the
1056: * user's home directory if it happens to be on a NFS volume where
1057: * root is mapped to nobody.
1058: */
1.225 dtucker 1059: if (original_effective_uid == 0) {
1060: PRIV_START;
1061: permanently_set_uid(pw);
1062: }
1.31 markus 1063:
1.33 markus 1064: /*
1065: * Now that we are back to our own permissions, create ~/.ssh
1.254 djm 1066: * directory if it doesn't already exist.
1.33 markus 1067: */
1.367 djm 1068: if (config == NULL) {
1069: r = snprintf(buf, sizeof buf, "%s%s%s", pw->pw_dir,
1070: strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
1071: if (r > 0 && (size_t)r < sizeof(buf) && stat(buf, &st) < 0)
1072: if (mkdir(buf, 0700) < 0)
1073: error("Could not create directory '%.200s'.",
1074: buf);
1075: }
1.31 markus 1076:
1.104 markus 1077: /* load options.identity_files */
1078: load_public_identity_files();
1079:
1080: /* Expand ~ in known host file names. */
1.361 djm 1081: tilde_expand_paths(options.system_hostfiles,
1082: options.num_system_hostfiles);
1083: tilde_expand_paths(options.user_hostfiles, options.num_user_hostfiles);
1.149 markus 1084:
1085: signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
1.352 djm 1086: signal(SIGCHLD, main_sigchld_handler);
1.31 markus 1087:
1.316 djm 1088: /* Log into the remote system. Never returns if the login fails. */
1.303 djm 1089: ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr,
1.355 djm 1090: options.port, pw, timeout_ms);
1.339 djm 1091:
1092: if (packet_connection_is_on_socket()) {
1093: verbose("Authenticated to %s ([%s]:%d).", host,
1094: get_remote_ipaddr(), get_remote_port());
1095: } else {
1096: verbose("Authenticated to %s (via proxy).", host);
1097: }
1.31 markus 1098:
1.112 markus 1099: /* We no longer need the private host keys. Clear them now. */
1100: if (sensitive_data.nkeys != 0) {
1101: for (i = 0; i < sensitive_data.nkeys; i++) {
1102: if (sensitive_data.keys[i] != NULL) {
1103: /* Destroys contents safely */
1104: debug3("clear hostkey %d", i);
1105: key_free(sensitive_data.keys[i]);
1106: sensitive_data.keys[i] = NULL;
1107: }
1108: }
1.378 djm 1109: free(sensitive_data.keys);
1.134 markus 1110: }
1111: for (i = 0; i < options.num_identity_files; i++) {
1.378 djm 1112: free(options.identity_files[i]);
1113: options.identity_files[i] = NULL;
1.134 markus 1114: if (options.identity_keys[i]) {
1115: key_free(options.identity_keys[i]);
1116: options.identity_keys[i] = NULL;
1117: }
1.112 markus 1118: }
1.31 markus 1119:
1.45 markus 1120: exit_status = compat20 ? ssh_session2() : ssh_session();
1121: packet_close();
1.186 djm 1122:
1.312 djm 1123: if (options.control_path != NULL && muxserver_sock != -1)
1.214 djm 1124: unlink(options.control_path);
1125:
1.353 djm 1126: /* Kill ProxyCommand if it is running. */
1127: ssh_kill_proxy_command();
1.186 djm 1128:
1.45 markus 1129: return exit_status;
1130: }
1131:
1.344 djm 1132: static void
1133: control_persist_detach(void)
1134: {
1135: pid_t pid;
1.346 djm 1136: int devnull;
1.344 djm 1137:
1138: debug("%s: backgrounding master process", __func__);
1139:
1140: /*
1141: * master (current process) into the background, and make the
1142: * foreground process a client of the backgrounded master.
1143: */
1144: switch ((pid = fork())) {
1145: case -1:
1146: fatal("%s: fork: %s", __func__, strerror(errno));
1147: case 0:
1148: /* Child: master process continues mainloop */
1149: break;
1150: default:
1151: /* Parent: set up mux slave to connect to backgrounded master */
1152: debug2("%s: background process is %ld", __func__, (long)pid);
1153: stdin_null_flag = ostdin_null_flag;
1.359 djm 1154: options.request_tty = orequest_tty;
1.344 djm 1155: tty_flag = otty_flag;
1156: close(muxserver_sock);
1157: muxserver_sock = -1;
1.351 markus 1158: options.control_master = SSHCTL_MASTER_NO;
1.344 djm 1159: muxclient(options.control_path);
1160: /* muxclient() doesn't return on success. */
1161: fatal("Failed to connect to new control master");
1162: }
1.346 djm 1163: if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
1164: error("%s: open(\"/dev/null\"): %s", __func__,
1165: strerror(errno));
1166: } else {
1167: if (dup2(devnull, STDIN_FILENO) == -1 ||
1168: dup2(devnull, STDOUT_FILENO) == -1)
1169: error("%s: dup2: %s", __func__, strerror(errno));
1170: if (devnull > STDERR_FILENO)
1171: close(devnull);
1172: }
1.381 djm 1173: daemon(1, 1);
1.362 djm 1174: setproctitle("%s [mux]", options.control_path);
1.344 djm 1175: }
1176:
1177: /* Do fork() after authentication. Used by "ssh -f" */
1178: static void
1179: fork_postauth(void)
1180: {
1181: if (need_controlpersist_detach)
1182: control_persist_detach();
1183: debug("forking to background");
1184: fork_after_authentication_flag = 0;
1185: if (daemon(1, 1) < 0)
1186: fatal("daemon() failed: %.200s", strerror(errno));
1187: }
1188:
1.315 djm 1189: /* Callback for remote forward global requests */
1190: static void
1191: ssh_confirm_remote_forward(int type, u_int32_t seq, void *ctxt)
1192: {
1193: Forward *rfwd = (Forward *)ctxt;
1194:
1.324 djm 1195: /* XXX verbose() on failure? */
1.315 djm 1196: debug("remote forward %s for: listen %d, connect %s:%d",
1197: type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
1198: rfwd->listen_port, rfwd->connect_host, rfwd->connect_port);
1.366 markus 1199: if (rfwd->listen_port == 0) {
1200: if (type == SSH2_MSG_REQUEST_SUCCESS) {
1201: rfwd->allocated_port = packet_get_int();
1202: logit("Allocated port %u for remote forward to %s:%d",
1203: rfwd->allocated_port,
1204: rfwd->connect_host, rfwd->connect_port);
1205: channel_update_permitted_opens(rfwd->handle,
1206: rfwd->allocated_port);
1207: } else {
1208: channel_update_permitted_opens(rfwd->handle, -1);
1209: }
1.324 djm 1210: }
1211:
1.315 djm 1212: if (type == SSH2_MSG_REQUEST_FAILURE) {
1213: if (options.exit_on_forward_failure)
1214: fatal("Error: remote port forwarding failed for "
1215: "listen port %d", rfwd->listen_port);
1216: else
1217: logit("Warning: remote port forwarding failed for "
1218: "listen port %d", rfwd->listen_port);
1219: }
1.318 djm 1220: if (++remote_forward_confirms_received == options.num_remote_forwards) {
1.315 djm 1221: debug("All remote forwarding requests processed");
1.344 djm 1222: if (fork_after_authentication_flag)
1223: fork_postauth();
1.318 djm 1224: }
1.315 djm 1225: }
1226:
1.126 itojun 1227: static void
1.331 dtucker 1228: client_cleanup_stdio_fwd(int id, void *arg)
1229: {
1230: debug("stdio forwarding: done");
1231: cleanup_exit(0);
1232: }
1233:
1.368 djm 1234: static void
1235: ssh_init_stdio_forwarding(void)
1.331 dtucker 1236: {
1237: Channel *c;
1.332 djm 1238: int in, out;
1.331 dtucker 1239:
1.368 djm 1240: if (stdio_forward_host == NULL)
1241: return;
1.384 djm 1242: if (!compat20)
1.368 djm 1243: fatal("stdio forwarding require Protocol 2");
1244:
1245: debug3("%s: %s:%d", __func__, stdio_forward_host, stdio_forward_port);
1.332 djm 1246:
1.368 djm 1247: if ((in = dup(STDIN_FILENO)) < 0 ||
1248: (out = dup(STDOUT_FILENO)) < 0)
1.332 djm 1249: fatal("channel_connect_stdio_fwd: dup() in/out failed");
1.368 djm 1250: if ((c = channel_connect_stdio_fwd(stdio_forward_host,
1251: stdio_forward_port, in, out)) == NULL)
1252: fatal("%s: channel_connect_stdio_fwd failed", __func__);
1.331 dtucker 1253: channel_register_cleanup(c->self, client_cleanup_stdio_fwd, 0);
1254: }
1255:
1256: static void
1.70 markus 1257: ssh_init_forwarding(void)
1258: {
1.86 markus 1259: int success = 0;
1.70 markus 1260: int i;
1.331 dtucker 1261:
1.70 markus 1262: /* Initiate local TCP/IP port forwardings. */
1263: for (i = 0; i < options.num_local_forwards; i++) {
1.232 djm 1264: debug("Local connections to %.200s:%d forwarded to remote "
1265: "address %.200s:%d",
1.234 deraadt 1266: (options.local_forwards[i].listen_host == NULL) ?
1267: (options.gateway_ports ? "*" : "LOCALHOST") :
1.232 djm 1268: options.local_forwards[i].listen_host,
1269: options.local_forwards[i].listen_port,
1270: options.local_forwards[i].connect_host,
1271: options.local_forwards[i].connect_port);
1.158 markus 1272: success += channel_setup_local_fwd_listener(
1.232 djm 1273: options.local_forwards[i].listen_host,
1274: options.local_forwards[i].listen_port,
1275: options.local_forwards[i].connect_host,
1276: options.local_forwards[i].connect_port,
1.70 markus 1277: options.gateway_ports);
1278: }
1.283 markus 1279: if (i > 0 && success != i && options.exit_on_forward_failure)
1280: fatal("Could not request local forwarding.");
1.86 markus 1281: if (i > 0 && success == 0)
1282: error("Could not request local forwarding.");
1.70 markus 1283:
1284: /* Initiate remote TCP/IP port forwardings. */
1285: for (i = 0; i < options.num_remote_forwards; i++) {
1.232 djm 1286: debug("Remote connections from %.200s:%d forwarded to "
1287: "local address %.200s:%d",
1.248 djm 1288: (options.remote_forwards[i].listen_host == NULL) ?
1.253 djm 1289: "LOCALHOST" : options.remote_forwards[i].listen_host,
1.232 djm 1290: options.remote_forwards[i].listen_port,
1291: options.remote_forwards[i].connect_host,
1292: options.remote_forwards[i].connect_port);
1.366 markus 1293: options.remote_forwards[i].handle =
1294: channel_request_remote_forwarding(
1.232 djm 1295: options.remote_forwards[i].listen_host,
1296: options.remote_forwards[i].listen_port,
1297: options.remote_forwards[i].connect_host,
1.366 markus 1298: options.remote_forwards[i].connect_port);
1299: if (options.remote_forwards[i].handle < 0) {
1.283 markus 1300: if (options.exit_on_forward_failure)
1301: fatal("Could not request remote forwarding.");
1302: else
1303: logit("Warning: Could not request remote "
1304: "forwarding.");
1.366 markus 1305: } else {
1306: client_register_global_confirm(ssh_confirm_remote_forward,
1307: &options.remote_forwards[i]);
1.283 markus 1308: }
1.70 markus 1309: }
1.301 djm 1310:
1311: /* Initiate tunnel forwarding. */
1312: if (options.tun_open != SSH_TUNMODE_NO) {
1313: if (client_request_tun_fwd(options.tun_open,
1314: options.tun_local, options.tun_remote) == -1) {
1315: if (options.exit_on_forward_failure)
1316: fatal("Could not request tunnel forwarding.");
1317: else
1318: error("Could not request tunnel forwarding.");
1319: }
1320: }
1.70 markus 1321: }
1322:
1.126 itojun 1323: static void
1.70 markus 1324: check_agent_present(void)
1325: {
1326: if (options.forward_agent) {
1.254 djm 1327: /* Clear agent forwarding if we don't have an agent. */
1.185 stevesk 1328: if (!ssh_agent_present())
1.70 markus 1329: options.forward_agent = 0;
1330: }
1331: }
1332:
1.126 itojun 1333: static int
1.45 markus 1334: ssh_session(void)
1335: {
1336: int type;
1337: int interactive = 0;
1338: int have_tty = 0;
1339: struct winsize ws;
1340: char *cp;
1.243 djm 1341: const char *display;
1.45 markus 1342:
1.31 markus 1343: /* Enable compression if requested. */
1344: if (options.compression) {
1.316 djm 1345: debug("Requesting compression at level %d.",
1346: options.compression_level);
1.31 markus 1347:
1.316 djm 1348: if (options.compression_level < 1 ||
1349: options.compression_level > 9)
1350: fatal("Compression level must be from 1 (fast) to "
1351: "9 (slow, best).");
1.31 markus 1352:
1353: /* Send the request. */
1354: packet_start(SSH_CMSG_REQUEST_COMPRESSION);
1355: packet_put_int(options.compression_level);
1356: packet_send();
1357: packet_write_wait();
1.156 markus 1358: type = packet_read();
1.31 markus 1359: if (type == SSH_SMSG_SUCCESS)
1360: packet_start_compression(options.compression_level);
1361: else if (type == SSH_SMSG_FAILURE)
1.191 itojun 1362: logit("Warning: Remote host refused compression.");
1.31 markus 1363: else
1.316 djm 1364: packet_disconnect("Protocol error waiting for "
1365: "compression response.");
1.31 markus 1366: }
1367: /* Allocate a pseudo tty if appropriate. */
1368: if (tty_flag) {
1369: debug("Requesting pty.");
1370:
1371: /* Start the packet. */
1372: packet_start(SSH_CMSG_REQUEST_PTY);
1373:
1374: /* Store TERM in the packet. There is no limit on the
1375: length of the string. */
1376: cp = getenv("TERM");
1377: if (!cp)
1378: cp = "";
1.124 markus 1379: packet_put_cstring(cp);
1.31 markus 1380:
1381: /* Store window size in the packet. */
1382: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
1383: memset(&ws, 0, sizeof(ws));
1.269 deraadt 1384: packet_put_int((u_int)ws.ws_row);
1385: packet_put_int((u_int)ws.ws_col);
1386: packet_put_int((u_int)ws.ws_xpixel);
1387: packet_put_int((u_int)ws.ws_ypixel);
1.31 markus 1388:
1389: /* Store tty modes in the packet. */
1.115 stevesk 1390: tty_make_modes(fileno(stdin), NULL);
1.31 markus 1391:
1392: /* Send the packet, and wait for it to leave. */
1393: packet_send();
1394: packet_write_wait();
1395:
1396: /* Read response from the server. */
1.156 markus 1397: type = packet_read();
1.43 markus 1398: if (type == SSH_SMSG_SUCCESS) {
1.31 markus 1399: interactive = 1;
1.45 markus 1400: have_tty = 1;
1.43 markus 1401: } else if (type == SSH_SMSG_FAILURE)
1.316 djm 1402: logit("Warning: Remote host failed or refused to "
1403: "allocate a pseudo tty.");
1.31 markus 1404: else
1.316 djm 1405: packet_disconnect("Protocol error waiting for pty "
1406: "request response.");
1.31 markus 1407: }
1408: /* Request X11 forwarding if enabled and DISPLAY is set. */
1.243 djm 1409: display = getenv("DISPLAY");
1410: if (options.forward_x11 && display != NULL) {
1.150 stevesk 1411: char *proto, *data;
1.50 markus 1412: /* Get reasonable local authentication information. */
1.243 djm 1413: client_x11_get_proto(display, options.xauth_location,
1.384 djm 1414: options.forward_x11_trusted,
1.340 djm 1415: options.forward_x11_timeout,
1416: &proto, &data);
1.50 markus 1417: /* Request forwarding with authentication spoofing. */
1.316 djm 1418: debug("Requesting X11 forwarding with authentication "
1419: "spoofing.");
1.363 djm 1420: x11_request_forwarding_with_spoofing(0, display, proto,
1421: data, 0);
1.31 markus 1422: /* Read response from the server. */
1.156 markus 1423: type = packet_read();
1.31 markus 1424: if (type == SSH_SMSG_SUCCESS) {
1425: interactive = 1;
1.50 markus 1426: } else if (type == SSH_SMSG_FAILURE) {
1.191 itojun 1427: logit("Warning: Remote host denied X11 forwarding.");
1.50 markus 1428: } else {
1.316 djm 1429: packet_disconnect("Protocol error waiting for X11 "
1430: "forwarding");
1.50 markus 1431: }
1.31 markus 1432: }
1433: /* Tell the packet module whether this is an interactive session. */
1.354 djm 1434: packet_set_interactive(interactive,
1435: options.ip_qos_interactive, options.ip_qos_bulk);
1.31 markus 1436:
1437: /* Request authentication agent forwarding if appropriate. */
1.70 markus 1438: check_agent_present();
1439:
1.31 markus 1440: if (options.forward_agent) {
1441: debug("Requesting authentication agent forwarding.");
1442: auth_request_forwarding();
1443:
1444: /* Read response from the server. */
1.156 markus 1445: type = packet_read();
1.155 markus 1446: packet_check_eom();
1.31 markus 1447: if (type != SSH_SMSG_SUCCESS)
1.191 itojun 1448: logit("Warning: Remote host denied authentication agent forwarding.");
1.31 markus 1449: }
1450:
1.70 markus 1451: /* Initiate port forwardings. */
1.368 djm 1452: ssh_init_stdio_forwarding();
1.70 markus 1453: ssh_init_forwarding();
1.305 dtucker 1454:
1455: /* Execute a local command */
1456: if (options.local_command != NULL &&
1457: options.permit_local_command)
1458: ssh_local_cmd(options.local_command);
1.34 markus 1459:
1.318 djm 1460: /*
1461: * If requested and we are not interested in replies to remote
1462: * forwarding requests, then let ssh continue in the background.
1463: */
1.344 djm 1464: if (fork_after_authentication_flag) {
1465: if (options.exit_on_forward_failure &&
1466: options.num_remote_forwards > 0) {
1467: debug("deferring postauth fork until remote forward "
1468: "confirmation received");
1469: } else
1470: fork_postauth();
1.318 djm 1471: }
1.31 markus 1472:
1.33 markus 1473: /*
1474: * If a command was specified on the command line, execute the
1475: * command now. Otherwise request the server to start a shell.
1476: */
1.31 markus 1477: if (buffer_len(&command) > 0) {
1478: int len = buffer_len(&command);
1479: if (len > 900)
1480: len = 900;
1.316 djm 1481: debug("Sending command: %.*s", len,
1482: (u_char *)buffer_ptr(&command));
1.31 markus 1483: packet_start(SSH_CMSG_EXEC_CMD);
1484: packet_put_string(buffer_ptr(&command), buffer_len(&command));
1485: packet_send();
1486: packet_write_wait();
1487: } else {
1488: debug("Requesting shell.");
1489: packet_start(SSH_CMSG_EXEC_SHELL);
1490: packet_send();
1491: packet_write_wait();
1492: }
1493:
1494: /* Enter the interactive session. */
1.119 stevesk 1495: return client_loop(have_tty, tty_flag ?
1496: options.escape_char : SSH_ESCAPECHAR_NONE, 0);
1.89 markus 1497: }
1498:
1.214 djm 1499: /* request pty/x11/agent/tcpfwd/shell for channel */
1500: static void
1.337 djm 1501: ssh_session2_setup(int id, int success, void *arg)
1.214 djm 1502: {
1.215 djm 1503: extern char **environ;
1.243 djm 1504: const char *display;
1505: int interactive = tty_flag;
1.337 djm 1506:
1507: if (!success)
1508: return; /* No need for error message, channels code sens one */
1.215 djm 1509:
1.248 djm 1510: display = getenv("DISPLAY");
1.243 djm 1511: if (options.forward_x11 && display != NULL) {
1.150 stevesk 1512: char *proto, *data;
1.50 markus 1513: /* Get reasonable local authentication information. */
1.243 djm 1514: client_x11_get_proto(display, options.xauth_location,
1.340 djm 1515: options.forward_x11_trusted,
1516: options.forward_x11_timeout, &proto, &data);
1.50 markus 1517: /* Request forwarding with authentication spoofing. */
1.316 djm 1518: debug("Requesting X11 forwarding with authentication "
1519: "spoofing.");
1.363 djm 1520: x11_request_forwarding_with_spoofing(id, display, proto,
1521: data, 1);
1522: client_expect_confirm(id, "X11 forwarding", CONFIRM_WARN);
1523: /* XXX exit_on_forward_failure */
1.80 markus 1524: interactive = 1;
1.50 markus 1525: }
1526:
1.70 markus 1527: check_agent_present();
1528: if (options.forward_agent) {
1529: debug("Requesting authentication agent forwarding.");
1530: channel_request_start(id, "auth-agent-req@openssh.com", 0);
1531: packet_send();
1.212 djm 1532: }
1.369 dtucker 1533:
1534: /* Tell the packet module whether this is an interactive session. */
1535: packet_set_interactive(interactive,
1536: options.ip_qos_interactive, options.ip_qos_bulk);
1.212 djm 1537:
1.214 djm 1538: client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
1.311 djm 1539: NULL, fileno(stdin), &command, environ);
1.45 markus 1540: }
1541:
1.143 markus 1542: /* open new channel for a session */
1.126 itojun 1543: static int
1.143 markus 1544: ssh_session2_open(void)
1.45 markus 1545: {
1.118 markus 1546: Channel *c;
1547: int window, packetmax, in, out, err;
1.60 markus 1548:
1.62 markus 1549: if (stdin_null_flag) {
1.93 itojun 1550: in = open(_PATH_DEVNULL, O_RDONLY);
1.62 markus 1551: } else {
1552: in = dup(STDIN_FILENO);
1553: }
1.60 markus 1554: out = dup(STDOUT_FILENO);
1555: err = dup(STDERR_FILENO);
1.45 markus 1556:
1557: if (in < 0 || out < 0 || err < 0)
1.62 markus 1558: fatal("dup() in/out/err failed");
1.45 markus 1559:
1.69 markus 1560: /* enable nonblocking unless tty */
1561: if (!isatty(in))
1562: set_nonblock(in);
1563: if (!isatty(out))
1564: set_nonblock(out);
1565: if (!isatty(err))
1566: set_nonblock(err);
1567:
1.65 markus 1568: window = CHAN_SES_WINDOW_DEFAULT;
1569: packetmax = CHAN_SES_PACKET_DEFAULT;
1.164 markus 1570: if (tty_flag) {
1571: window >>= 1;
1572: packetmax >>= 1;
1.45 markus 1573: }
1.118 markus 1574: c = channel_new(
1.45 markus 1575: "session", SSH_CHANNEL_OPENING, in, out, err,
1.65 markus 1576: window, packetmax, CHAN_EXTENDED_WRITE,
1.192 markus 1577: "client-session", /*nonblock*/0);
1.45 markus 1578:
1.143 markus 1579: debug3("ssh_session2_open: channel_new: %d", c->self);
1.106 markus 1580:
1.122 markus 1581: channel_send_open(c->self);
1.143 markus 1582: if (!no_shell_flag)
1.310 djm 1583: channel_register_open_confirm(c->self,
1584: ssh_session2_setup, NULL);
1.106 markus 1585:
1.118 markus 1586: return c->self;
1.106 markus 1587: }
1588:
1.126 itojun 1589: static int
1.106 markus 1590: ssh_session2(void)
1591: {
1.143 markus 1592: int id = -1;
1.106 markus 1593:
1594: /* XXX should be pre-session */
1.368 djm 1595: if (!options.control_persist)
1596: ssh_init_stdio_forwarding();
1.106 markus 1597: ssh_init_forwarding();
1598:
1.344 djm 1599: /* Start listening for multiplex clients */
1600: muxserver_listen();
1601:
1602: /*
1.368 djm 1603: * If we are in control persist mode and have a working mux listen
1604: * socket, then prepare to background ourselves and have a foreground
1605: * client attach as a control slave.
1606: * NB. we must save copies of the flags that we override for
1.344 djm 1607: * the backgrounding, since we defer attachment of the slave until
1608: * after the connection is fully established (in particular,
1609: * async rfwd replies have been received for ExitOnForwardFailure).
1610: */
1611: if (options.control_persist && muxserver_sock != -1) {
1612: ostdin_null_flag = stdin_null_flag;
1613: ono_shell_flag = no_shell_flag;
1.359 djm 1614: orequest_tty = options.request_tty;
1.344 djm 1615: otty_flag = tty_flag;
1616: stdin_null_flag = 1;
1617: no_shell_flag = 1;
1618: tty_flag = 0;
1619: if (!fork_after_authentication_flag)
1620: need_controlpersist_detach = 1;
1621: fork_after_authentication_flag = 1;
1622: }
1.368 djm 1623: /*
1624: * ControlPersist mux listen socket setup failed, attempt the
1625: * stdio forward setup that we skipped earlier.
1626: */
1627: if (options.control_persist && muxserver_sock == -1)
1628: ssh_init_stdio_forwarding();
1.344 djm 1629:
1.143 markus 1630: if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1631: id = ssh_session2_open();
1.379 djm 1632: else {
1633: packet_set_interactive(
1634: options.control_master == SSHCTL_MASTER_NO,
1635: options.ip_qos_interactive, options.ip_qos_bulk);
1636: }
1.314 djm 1637:
1638: /* If we don't expect to open a new session, then disallow it */
1.319 markus 1639: if (options.control_master == SSHCTL_MASTER_NO &&
1640: (datafellows & SSH_NEW_OPENSSH)) {
1.314 djm 1641: debug("Requesting no-more-sessions@openssh.com");
1642: packet_start(SSH2_MSG_GLOBAL_REQUEST);
1643: packet_put_cstring("no-more-sessions@openssh.com");
1644: packet_put_char(0);
1645: packet_send();
1646: }
1.255 reyk 1647:
1648: /* Execute a local command */
1649: if (options.local_command != NULL &&
1650: options.permit_local_command)
1651: ssh_local_cmd(options.local_command);
1.301 djm 1652:
1.342 djm 1653: /*
1654: * If requested and we are not interested in replies to remote
1655: * forwarding requests, then let ssh continue in the background.
1656: */
1.344 djm 1657: if (fork_after_authentication_flag) {
1658: if (options.exit_on_forward_failure &&
1659: options.num_remote_forwards > 0) {
1660: debug("deferring postauth fork until remote forward "
1661: "confirmation received");
1662: } else
1663: fork_postauth();
1.318 djm 1664: }
1.327 andreas 1665:
1666: if (options.use_roaming)
1667: request_roaming();
1.31 markus 1668:
1.119 stevesk 1669: return client_loop(tty_flag, tty_flag ?
1670: options.escape_char : SSH_ESCAPECHAR_NONE, id);
1.72 markus 1671: }
1672:
1.126 itojun 1673: static void
1.104 markus 1674: load_public_identity_files(void)
1675: {
1.275 djm 1676: char *filename, *cp, thishost[NI_MAXHOST];
1.306 deraadt 1677: char *pwdir = NULL, *pwname = NULL;
1.167 markus 1678: int i = 0;
1.104 markus 1679: Key *public;
1.275 djm 1680: struct passwd *pw;
1.335 djm 1681: u_int n_ids;
1682: char *identity_files[SSH_MAX_IDENTITY_FILES];
1683: Key *identity_keys[SSH_MAX_IDENTITY_FILES];
1.333 markus 1684: #ifdef ENABLE_PKCS11
1.167 markus 1685: Key **keys;
1.333 markus 1686: int nkeys;
1.335 djm 1687: #endif /* PKCS11 */
1.104 markus 1688:
1.335 djm 1689: n_ids = 0;
1.398 tedu 1690: memset(identity_files, 0, sizeof(identity_files));
1691: memset(identity_keys, 0, sizeof(identity_keys));
1.335 djm 1692:
1693: #ifdef ENABLE_PKCS11
1.333 markus 1694: if (options.pkcs11_provider != NULL &&
1.167 markus 1695: options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1.333 markus 1696: (pkcs11_init(!options.batch_mode) == 0) &&
1697: (nkeys = pkcs11_add_provider(options.pkcs11_provider, NULL,
1698: &keys)) > 0) {
1699: for (i = 0; i < nkeys; i++) {
1.335 djm 1700: if (n_ids >= SSH_MAX_IDENTITY_FILES) {
1701: key_free(keys[i]);
1702: continue;
1703: }
1704: identity_keys[n_ids] = keys[i];
1705: identity_files[n_ids] =
1.333 markus 1706: xstrdup(options.pkcs11_provider); /* XXX */
1.335 djm 1707: n_ids++;
1.167 markus 1708: }
1.378 djm 1709: free(keys);
1.127 markus 1710: }
1.333 markus 1711: #endif /* ENABLE_PKCS11 */
1.275 djm 1712: if ((pw = getpwuid(original_real_uid)) == NULL)
1713: fatal("load_public_identity_files: getpwuid failed");
1.307 dtucker 1714: pwname = xstrdup(pw->pw_name);
1715: pwdir = xstrdup(pw->pw_dir);
1.275 djm 1716: if (gethostname(thishost, sizeof(thishost)) == -1)
1717: fatal("load_public_identity_files: gethostname: %s",
1718: strerror(errno));
1.335 djm 1719: for (i = 0; i < options.num_identity_files; i++) {
1.373 djm 1720: if (n_ids >= SSH_MAX_IDENTITY_FILES ||
1721: strcasecmp(options.identity_files[i], "none") == 0) {
1.378 djm 1722: free(options.identity_files[i]);
1.335 djm 1723: continue;
1724: }
1.275 djm 1725: cp = tilde_expand_filename(options.identity_files[i],
1.131 millert 1726: original_real_uid);
1.306 deraadt 1727: filename = percent_expand(cp, "d", pwdir,
1728: "u", pwname, "l", thishost, "h", host,
1.275 djm 1729: "r", options.user, (char *)NULL);
1.378 djm 1730: free(cp);
1.131 millert 1731: public = key_load_public(filename, NULL);
1732: debug("identity file %s type %d", filename,
1733: public ? public->type : -1);
1.378 djm 1734: free(options.identity_files[i]);
1.335 djm 1735: identity_files[n_ids] = filename;
1736: identity_keys[n_ids] = public;
1737:
1738: if (++n_ids >= SSH_MAX_IDENTITY_FILES)
1739: continue;
1740:
1741: /* Try to add the certificate variant too */
1742: xasprintf(&cp, "%s-cert", filename);
1743: public = key_load_public(cp, NULL);
1744: debug("identity file %s type %d", cp,
1745: public ? public->type : -1);
1746: if (public == NULL) {
1.378 djm 1747: free(cp);
1.335 djm 1748: continue;
1749: }
1750: if (!key_is_cert(public)) {
1751: debug("%s: key %s type %s is not a certificate",
1752: __func__, cp, key_type(public));
1753: key_free(public);
1.378 djm 1754: free(cp);
1.335 djm 1755: continue;
1756: }
1757: identity_keys[n_ids] = public;
1758: /* point to the original path, most likely the private key */
1759: identity_files[n_ids] = xstrdup(filename);
1760: n_ids++;
1761: }
1762: options.num_identity_files = n_ids;
1763: memcpy(options.identity_files, identity_files, sizeof(identity_files));
1764: memcpy(options.identity_keys, identity_keys, sizeof(identity_keys));
1765:
1.398 tedu 1766: explicit_bzero(pwname, strlen(pwname));
1.378 djm 1767: free(pwname);
1.398 tedu 1768: explicit_bzero(pwdir, strlen(pwdir));
1.378 djm 1769: free(pwdir);
1.214 djm 1770: }
1.352 djm 1771:
1772: static void
1773: main_sigchld_handler(int sig)
1774: {
1775: int save_errno = errno;
1776: pid_t pid;
1777: int status;
1778:
1779: while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
1780: (pid < 0 && errno == EINTR))
1781: ;
1782:
1783: signal(sig, main_sigchld_handler);
1784: errno = save_errno;
1785: }