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