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