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