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