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