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