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