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