Annotation of src/usr.bin/ssh/ssh.c, Revision 1.389
1.389 ! djm 1: /* $OpenBSD: ssh.c,v 1.388 2013/10/17 00:46:49 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.377 djm 189: " [-L [bind_address:]port:host:hostport] [-Q protocol_feature]\n"
1.232 djm 190: " [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
1.233 jmc 191: " [-R [bind_address:]port:host:hostport] [-S ctl_path]\n"
1.331 dtucker 192: " [-W host:port] [-w local_tun[:remote_tun]]\n"
193: " [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;
492: case 'Q': /* deprecated */
493: cp = NULL;
494: if (strcasecmp(optarg, "cipher") == 0)
495: cp = cipher_alg_list();
496: else if (strcasecmp(optarg, "mac") == 0)
497: cp = mac_alg_list();
498: else if (strcasecmp(optarg, "kex") == 0)
499: cp = kex_alg_list();
500: else if (strcasecmp(optarg, "key") == 0)
501: cp = key_alg_list();
502: if (cp == NULL)
503: fatal("Unsupported query \"%s\"", optarg);
504: printf("%s\n", cp);
505: free(cp);
506: exit(0);
1.31 markus 507: break;
508: case 'a':
509: options.forward_agent = 0;
1.53 markus 510: break;
511: case 'A':
512: options.forward_agent = 1;
1.31 markus 513: break;
514: case 'k':
1.204 dtucker 515: options.gss_deleg_creds = 0;
1.297 djm 516: break;
517: case 'K':
518: options.gss_authentication = 1;
519: options.gss_deleg_creds = 1;
1.31 markus 520: break;
521: case 'i':
522: if (stat(optarg, &st) < 0) {
1.128 fgsch 523: fprintf(stderr, "Warning: Identity file %s "
1.231 otto 524: "not accessible: %s.\n", optarg,
525: strerror(errno));
1.31 markus 526: break;
527: }
1.371 dtucker 528: add_identity_file(&options, NULL, optarg, 1);
1.31 markus 529: break;
1.127 markus 530: case 'I':
1.333 markus 531: #ifdef ENABLE_PKCS11
532: options.pkcs11_provider = xstrdup(optarg);
1.137 jakob 533: #else
1.333 markus 534: fprintf(stderr, "no support for PKCS#11.\n");
1.137 jakob 535: #endif
1.127 markus 536: break;
1.31 markus 537: case 't':
1.359 djm 538: if (options.request_tty == REQUEST_TTY_YES)
539: options.request_tty = REQUEST_TTY_FORCE;
540: else
541: options.request_tty = REQUEST_TTY_YES;
1.31 markus 542: break;
543: case 'v':
1.197 markus 544: if (debug_flag == 0) {
1.66 markus 545: debug_flag = 1;
546: options.log_level = SYSLOG_LEVEL_DEBUG1;
1.197 markus 547: } else {
548: if (options.log_level < SYSLOG_LEVEL_DEBUG3)
549: options.log_level++;
550: }
1.375 dtucker 551: break;
1.31 markus 552: case 'V':
1.209 markus 553: fprintf(stderr, "%s, %s\n",
554: SSH_VERSION, SSLeay_version(SSLEAY_VERSION));
1.31 markus 555: if (opt == 'V')
556: exit(0);
557: break;
1.255 reyk 558: case 'w':
1.256 reyk 559: if (options.tun_open == -1)
560: options.tun_open = SSH_TUNMODE_DEFAULT;
1.255 reyk 561: options.tun_local = a2tun(optarg, &options.tun_remote);
1.256 reyk 562: if (options.tun_local == SSH_TUNID_ERR) {
1.316 djm 563: fprintf(stderr,
564: "Bad tun device '%s'\n", optarg);
1.257 dtucker 565: exit(255);
1.255 reyk 566: }
567: break;
1.331 dtucker 568: case 'W':
1.332 djm 569: if (stdio_forward_host != NULL)
570: fatal("stdio forward already specified");
571: if (muxclient_command != 0)
572: fatal("Cannot specify stdio forward with -O");
1.331 dtucker 573: if (parse_forward(&fwd, optarg, 1, 0)) {
574: stdio_forward_host = fwd.listen_host;
575: stdio_forward_port = fwd.listen_port;
1.378 djm 576: free(fwd.connect_host);
1.331 dtucker 577: } else {
578: fprintf(stderr,
579: "Bad stdio forwarding specification '%s'\n",
580: optarg);
581: exit(255);
582: }
1.359 djm 583: options.request_tty = REQUEST_TTY_NO;
1.331 dtucker 584: no_shell_flag = 1;
585: options.clear_forwardings = 1;
586: options.exit_on_forward_failure = 1;
587: break;
1.31 markus 588: case 'q':
589: options.log_level = SYSLOG_LEVEL_QUIET;
590: break;
591: case 'e':
592: if (optarg[0] == '^' && optarg[2] == 0 &&
1.128 fgsch 593: (u_char) optarg[1] >= 64 &&
594: (u_char) optarg[1] < 128)
1.78 markus 595: options.escape_char = (u_char) optarg[1] & 31;
1.31 markus 596: else if (strlen(optarg) == 1)
1.78 markus 597: options.escape_char = (u_char) optarg[0];
1.31 markus 598: else if (strcmp(optarg, "none") == 0)
1.119 stevesk 599: options.escape_char = SSH_ESCAPECHAR_NONE;
1.31 markus 600: else {
1.128 fgsch 601: fprintf(stderr, "Bad escape character '%s'.\n",
602: optarg);
1.257 dtucker 603: exit(255);
1.31 markus 604: }
605: break;
606: case 'c':
1.49 markus 607: if (ciphers_valid(optarg)) {
608: /* SSH2 only */
609: options.ciphers = xstrdup(optarg);
1.224 markus 610: options.cipher = SSH_CIPHER_INVALID;
1.49 markus 611: } else {
612: /* SSH1 only */
1.74 markus 613: options.cipher = cipher_number(optarg);
614: if (options.cipher == -1) {
1.128 fgsch 615: fprintf(stderr,
616: "Unknown cipher type '%s'\n",
617: optarg);
1.257 dtucker 618: exit(255);
1.49 markus 619: }
1.128 fgsch 620: if (options.cipher == SSH_CIPHER_3DES)
1.74 markus 621: options.ciphers = "3des-cbc";
1.128 fgsch 622: else if (options.cipher == SSH_CIPHER_BLOWFISH)
1.74 markus 623: options.ciphers = "blowfish-cbc";
1.128 fgsch 624: else
1.74 markus 625: options.ciphers = (char *)-1;
1.95 markus 626: }
627: break;
628: case 'm':
629: if (mac_valid(optarg))
630: options.macs = xstrdup(optarg);
631: else {
1.128 fgsch 632: fprintf(stderr, "Unknown mac type '%s'\n",
633: optarg);
1.257 dtucker 634: exit(255);
1.31 markus 635: }
636: break;
1.214 djm 637: case 'M':
1.242 djm 638: if (options.control_master == SSHCTL_MASTER_YES)
639: options.control_master = SSHCTL_MASTER_ASK;
640: else
641: options.control_master = SSHCTL_MASTER_YES;
1.214 djm 642: break;
1.31 markus 643: case 'p':
1.113 stevesk 644: options.port = a2port(optarg);
1.323 djm 645: if (options.port <= 0) {
1.109 markus 646: fprintf(stderr, "Bad port '%s'\n", optarg);
1.257 dtucker 647: exit(255);
1.109 markus 648: }
1.31 markus 649: break;
650: case 'l':
651: options.user = optarg;
652: break;
1.141 stevesk 653:
654: case 'L':
1.324 djm 655: if (parse_forward(&fwd, optarg, 0, 0))
1.232 djm 656: add_local_forward(&options, &fwd);
657: else {
1.128 fgsch 658: fprintf(stderr,
1.232 djm 659: "Bad local forwarding specification '%s'\n",
1.128 fgsch 660: optarg);
1.257 dtucker 661: exit(255);
1.31 markus 662: }
1.232 djm 663: break;
664:
665: case 'R':
1.324 djm 666: if (parse_forward(&fwd, optarg, 0, 1)) {
1.232 djm 667: add_remote_forward(&options, &fwd);
668: } else {
1.128 fgsch 669: fprintf(stderr,
1.232 djm 670: "Bad remote forwarding specification "
671: "'%s'\n", optarg);
1.257 dtucker 672: exit(255);
1.31 markus 673: }
674: break;
1.108 markus 675:
676: case 'D':
1.324 djm 677: if (parse_forward(&fwd, optarg, 1, 0)) {
1.322 stevesk 678: add_local_forward(&options, &fwd);
1.232 djm 679: } else {
1.322 stevesk 680: fprintf(stderr,
681: "Bad dynamic forwarding specification "
682: "'%s'\n", optarg);
1.257 dtucker 683: exit(255);
1.109 markus 684: }
1.108 markus 685: break;
686:
1.31 markus 687: case 'C':
688: options.compression = 1;
689: break;
1.45 markus 690: case 'N':
691: no_shell_flag = 1;
1.359 djm 692: options.request_tty = REQUEST_TTY_NO;
1.45 markus 693: break;
694: case 'T':
1.359 djm 695: options.request_tty = REQUEST_TTY_NO;
1.45 markus 696: break;
1.31 markus 697: case 'o':
1.205 markus 698: line = xstrdup(optarg);
1.382 djm 699: if (process_config_line(&options, pw, host ? host : "",
700: line, "command-line", 0, NULL, SSHCONF_USERCONF)
1.372 dtucker 701: != 0)
1.257 dtucker 702: exit(255);
1.378 djm 703: free(line);
1.31 markus 704: break;
1.85 djm 705: case 's':
706: subsystem_flag = 1;
1.117 markus 707: break;
1.214 djm 708: case 'S':
709: if (options.control_path != NULL)
710: free(options.control_path);
711: options.control_path = xstrdup(optarg);
712: break;
1.117 markus 713: case 'b':
714: options.bind_address = optarg;
1.85 djm 715: break;
1.139 markus 716: case 'F':
717: config = optarg;
718: break;
1.31 markus 719: default:
720: usage();
1.1 deraadt 721: }
1.31 markus 722: }
723:
1.128 fgsch 724: ac -= optind;
725: av += optind;
726:
1.329 guenther 727: if (ac > 0 && !host) {
1.188 markus 728: if (strrchr(*av, '@')) {
1.128 fgsch 729: p = xstrdup(*av);
1.188 markus 730: cp = strrchr(p, '@');
1.128 fgsch 731: if (cp == NULL || cp == p)
732: usage();
733: options.user = p;
734: *cp = '\0';
1.385 djm 735: host = xstrdup(++cp);
1.128 fgsch 736: } else
1.385 djm 737: host = xstrdup(*av);
1.189 millert 738: if (ac > 1) {
739: optind = optreset = 1;
1.128 fgsch 740: goto again;
741: }
1.189 millert 742: ac--, av++;
1.128 fgsch 743: }
744:
1.31 markus 745: /* Check that we got a host name. */
746: if (!host)
747: usage();
748:
1.385 djm 749: lowercase(host);
750: host_arg = xstrdup(host);
751:
1.350 djm 752: OpenSSL_add_all_algorithms();
1.72 markus 753: ERR_load_crypto_strings();
1.31 markus 754:
755: /* Initialize the command to execute on remote host. */
756: buffer_init(&command);
1.1 deraadt 757:
1.33 markus 758: /*
759: * Save the command to execute on the remote host in a buffer. There
760: * is no limit on the length of the command, except by the maximum
761: * packet size. Also sets the tty flag if there is no command.
762: */
1.128 fgsch 763: if (!ac) {
1.31 markus 764: /* No command specified - execute shell on a tty. */
1.85 djm 765: if (subsystem_flag) {
1.128 fgsch 766: fprintf(stderr,
767: "You must specify a subsystem to invoke.\n");
1.85 djm 768: usage();
769: }
1.31 markus 770: } else {
1.128 fgsch 771: /* A command has been specified. Store it into the buffer. */
772: for (i = 0; i < ac; i++) {
773: if (i)
1.31 markus 774: buffer_append(&command, " ", 1);
775: buffer_append(&command, av[i], strlen(av[i]));
776: }
777: }
778:
779: /* Cannot fork to background if no command. */
1.316 djm 780: if (fork_after_authentication_flag && buffer_len(&command) == 0 &&
781: !no_shell_flag)
782: fatal("Cannot fork into background without a command "
783: "to execute.");
1.31 markus 784:
1.101 markus 785: /*
786: * Initialize "log" output. Since we are the client all output
1.375 dtucker 787: * goes to stderr unless otherwise specified by -y or -E.
1.101 markus 788: */
1.375 dtucker 789: if (use_syslog && logfile != NULL)
790: fatal("Can't specify both -y and -E");
791: if (logfile != NULL) {
792: log_redirect_stderr_to(logfile);
1.378 djm 793: free(logfile);
1.375 dtucker 794: }
1.325 markus 795: log_init(argv0,
1.316 djm 796: options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
1.320 djm 797: SYSLOG_FACILITY_USER, !use_syslog);
1.375 dtucker 798:
799: if (debug_flag)
800: logit("%s, %s", SSH_VERSION, SSLeay_version(SSLEAY_VERSION));
1.31 markus 801:
1.139 markus 802: /*
803: * Read per-user configuration file. Ignore the system wide config
804: * file if the user specifies a config file on the command line.
805: */
806: if (config != NULL) {
1.374 djm 807: if (strcasecmp(config, "none") != 0 &&
1.382 djm 808: !read_config_file(config, pw, host, &options,
809: SSHCONF_USERCONF))
1.142 stevesk 810: fatal("Can't open user config file %.100s: "
811: "%.100s", config, strerror(errno));
1.295 stevesk 812: } else {
1.326 dtucker 813: r = snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir,
1.139 markus 814: _PATH_SSH_USER_CONFFILE);
1.326 dtucker 815: if (r > 0 && (size_t)r < sizeof(buf))
1.382 djm 816: (void)read_config_file(buf, pw, host, &options,
1.372 dtucker 817: SSHCONF_CHECKPERM|SSHCONF_USERCONF);
1.139 markus 818:
1.364 djm 819: /* Read systemwide configuration file after user config. */
1.382 djm 820: (void)read_config_file(_PATH_HOST_CONFIG_FILE, pw, host,
1.210 djm 821: &options, 0);
1.139 markus 822: }
1.31 markus 823:
824: /* Fill configuration defaults. */
825: fill_default_options(&options);
826:
1.196 djm 827: channel_set_af(options.address_family);
828:
1.383 djm 829: /* Tidy and check options */
830: if (options.host_key_alias != NULL)
831: lowercase(options.host_key_alias);
832: if (options.proxy_command != NULL &&
833: strcmp(options.proxy_command, "-") == 0 &&
834: options.proxy_use_fdpass)
835: fatal("ProxyCommand=- and ProxyUseFDPass are incompatible");
1.388 djm 836: if (original_effective_uid != 0)
837: options.use_privileged_port = 0;
1.383 djm 838:
1.31 markus 839: /* reinit */
1.325 markus 840: log_init(argv0, options.log_level, SYSLOG_FACILITY_USER, !use_syslog);
1.370 djm 841:
842: if (options.request_tty == REQUEST_TTY_YES ||
843: options.request_tty == REQUEST_TTY_FORCE)
844: tty_flag = 1;
845:
846: /* Allocate a tty by default if no command specified. */
847: if (buffer_len(&command) == 0)
848: tty_flag = options.request_tty != REQUEST_TTY_NO;
849:
850: /* Force no tty */
851: if (options.request_tty == REQUEST_TTY_NO || muxclient_command != 0)
852: tty_flag = 0;
853: /* Do not allocate a tty if stdin is not a tty. */
854: if ((!isatty(fileno(stdin)) || stdin_null_flag) &&
855: options.request_tty != REQUEST_TTY_FORCE) {
856: if (tty_flag)
857: logit("Pseudo-terminal will not be allocated because "
858: "stdin is not a terminal.");
859: tty_flag = 0;
860: }
1.31 markus 861:
862: if (options.user == NULL)
863: options.user = xstrdup(pw->pw_name);
864:
1.317 dtucker 865: /* Get default port if port has not been set. */
1.382 djm 866: if (options.port == 0)
867: options.port = default_ssh_port();
1.317 dtucker 868:
1.356 djm 869: /* preserve host name given on command line for %n expansion */
1.343 djm 870: if (options.hostname != NULL) {
1.389 ! djm 871: /* NB. Please keep in sync with readconf.c:match_cfg_line() */
1.385 djm 872: cp = percent_expand(options.hostname,
1.343 djm 873: "h", host, (char *)NULL);
1.385 djm 874: free(host);
875: host = cp;
876: }
877:
1.387 djm 878: /* If canonicalization requested then try to apply it */
1.386 djm 879: if (options.canonicalize_hostname != SSH_CANONICALISE_NO)
880: addrs = resolve_canonicalize(&host, options.port);
1.385 djm 881: /*
1.387 djm 882: * If canonicalization not requested, or if it failed then try to
1.385 djm 883: * resolve the bare hostname name using the system resolver's usual
884: * search rules.
885: */
886: if (addrs == NULL) {
887: if ((addrs = resolve_host(host, options.port, 1,
888: cname, sizeof(cname))) == NULL)
889: cleanup_exit(255); /* resolve_host logs the error */
890: check_follow_cname(&host, cname);
1.343 djm 891: }
892:
1.358 djm 893: if (gethostname(thishost, sizeof(thishost)) == -1)
894: fatal("gethostname: %s", strerror(errno));
895: strlcpy(shorthost, thishost, sizeof(shorthost));
896: shorthost[strcspn(thishost, ".")] = '\0';
897: snprintf(portstr, sizeof(portstr), "%d", options.port);
898:
1.317 dtucker 899: if (options.local_command != NULL) {
900: debug3("expanding LocalCommand: %s", options.local_command);
901: cp = options.local_command;
902: options.local_command = percent_expand(cp, "d", pw->pw_dir,
1.356 djm 903: "h", host, "l", thishost, "n", host_arg, "r", options.user,
1.358 djm 904: "p", portstr, "u", pw->pw_name, "L", shorthost,
905: (char *)NULL);
1.317 dtucker 906: debug3("expanded LocalCommand: %s", options.local_command);
1.378 djm 907: free(cp);
1.304 dtucker 908: }
1.31 markus 909:
1.214 djm 910: if (options.control_path != NULL) {
1.241 djm 911: cp = tilde_expand_filename(options.control_path,
912: original_real_uid);
1.378 djm 913: free(options.control_path);
1.358 djm 914: options.control_path = percent_expand(cp, "h", host,
915: "l", thishost, "n", host_arg, "r", options.user,
916: "p", portstr, "u", pw->pw_name, "L", shorthost,
917: (char *)NULL);
1.378 djm 918: free(cp);
1.214 djm 919: }
1.312 djm 920: if (muxclient_command != 0 && options.control_path == NULL)
1.240 djm 921: fatal("No ControlPath specified for \"-O\" command");
1.242 djm 922: if (options.control_path != NULL)
1.312 djm 923: muxclient(options.control_path);
1.214 djm 924:
1.303 djm 925: timeout_ms = options.connection_timeout * 1000;
926:
1.77 markus 927: /* Open a connection to the remote host. */
1.385 djm 928: if (ssh_connect(host, addrs, &hostaddr, options.port,
929: options.address_family, options.connection_attempts,
930: &timeout_ms, options.tcp_keep_alive,
1.388 djm 931: options.use_privileged_port) != 0)
1.257 dtucker 932: exit(255);
1.31 markus 933:
1.385 djm 934: freeaddrinfo(addrs);
935: packet_set_timeout(options.server_alive_interval,
936: options.server_alive_count_max);
937:
1.303 djm 938: if (timeout_ms > 0)
939: debug3("timeout: %d ms remain after connect", timeout_ms);
940:
1.33 markus 941: /*
942: * If we successfully made the connection, load the host private key
943: * in case we will need it later for combined rsa-rhosts
944: * authentication. This must be done before releasing extra
945: * privileges, because the file is only readable by root.
1.174 markus 946: * If we cannot access the private keys, load the public keys
947: * instead and try to execute the ssh-keysign helper instead.
1.33 markus 948: */
1.112 markus 949: sensitive_data.nkeys = 0;
950: sensitive_data.keys = NULL;
1.173 markus 951: sensitive_data.external_keysign = 0;
1.178 markus 952: if (options.rhosts_rsa_authentication ||
953: options.hostbased_authentication) {
1.349 djm 954: sensitive_data.nkeys = 7;
1.274 deraadt 955: sensitive_data.keys = xcalloc(sensitive_data.nkeys,
1.180 deraadt 956: sizeof(Key));
1.177 markus 957:
958: PRIV_START;
1.112 markus 959: sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
1.276 dtucker 960: _PATH_HOST_KEY_FILE, "", NULL, NULL);
1.345 djm 961: sensitive_data.keys[1] = key_load_private_cert(KEY_DSA,
962: _PATH_HOST_DSA_KEY_FILE, "", NULL);
1.349 djm 963: sensitive_data.keys[2] = key_load_private_cert(KEY_ECDSA,
964: _PATH_HOST_ECDSA_KEY_FILE, "", NULL);
965: sensitive_data.keys[3] = key_load_private_cert(KEY_RSA,
1.345 djm 966: _PATH_HOST_RSA_KEY_FILE, "", NULL);
1.349 djm 967: sensitive_data.keys[4] = key_load_private_type(KEY_DSA,
1.276 dtucker 968: _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL);
1.349 djm 969: sensitive_data.keys[5] = key_load_private_type(KEY_ECDSA,
970: _PATH_HOST_ECDSA_KEY_FILE, "", NULL, NULL);
971: sensitive_data.keys[6] = key_load_private_type(KEY_RSA,
1.276 dtucker 972: _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL);
1.177 markus 973: PRIV_END;
1.173 markus 974:
1.181 markus 975: if (options.hostbased_authentication == 1 &&
976: sensitive_data.keys[0] == NULL &&
1.349 djm 977: sensitive_data.keys[4] == NULL &&
978: sensitive_data.keys[5] == NULL &&
979: sensitive_data.keys[6] == NULL) {
1.345 djm 980: sensitive_data.keys[1] = key_load_cert(
981: _PATH_HOST_DSA_KEY_FILE);
982: sensitive_data.keys[2] = key_load_cert(
1.349 djm 983: _PATH_HOST_ECDSA_KEY_FILE);
984: sensitive_data.keys[3] = key_load_cert(
1.345 djm 985: _PATH_HOST_RSA_KEY_FILE);
1.349 djm 986: sensitive_data.keys[4] = key_load_public(
1.173 markus 987: _PATH_HOST_DSA_KEY_FILE, NULL);
1.349 djm 988: sensitive_data.keys[5] = key_load_public(
989: _PATH_HOST_ECDSA_KEY_FILE, NULL);
990: sensitive_data.keys[6] = key_load_public(
1.173 markus 991: _PATH_HOST_RSA_KEY_FILE, NULL);
992: sensitive_data.external_keysign = 1;
993: }
1.31 markus 994: }
1.33 markus 995: /*
996: * Get rid of any extra privileges that we may have. We will no
997: * longer need them. Also, extra privileges could make it very hard
998: * to read identity files and other non-world-readable files from the
999: * user's home directory if it happens to be on a NFS volume where
1000: * root is mapped to nobody.
1001: */
1.225 dtucker 1002: if (original_effective_uid == 0) {
1003: PRIV_START;
1004: permanently_set_uid(pw);
1005: }
1.31 markus 1006:
1.33 markus 1007: /*
1008: * Now that we are back to our own permissions, create ~/.ssh
1.254 djm 1009: * directory if it doesn't already exist.
1.33 markus 1010: */
1.367 djm 1011: if (config == NULL) {
1012: r = snprintf(buf, sizeof buf, "%s%s%s", pw->pw_dir,
1013: strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
1014: if (r > 0 && (size_t)r < sizeof(buf) && stat(buf, &st) < 0)
1015: if (mkdir(buf, 0700) < 0)
1016: error("Could not create directory '%.200s'.",
1017: buf);
1018: }
1.31 markus 1019:
1.104 markus 1020: /* load options.identity_files */
1021: load_public_identity_files();
1022:
1023: /* Expand ~ in known host file names. */
1.361 djm 1024: tilde_expand_paths(options.system_hostfiles,
1025: options.num_system_hostfiles);
1026: tilde_expand_paths(options.user_hostfiles, options.num_user_hostfiles);
1.149 markus 1027:
1028: signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
1.352 djm 1029: signal(SIGCHLD, main_sigchld_handler);
1.31 markus 1030:
1.316 djm 1031: /* Log into the remote system. Never returns if the login fails. */
1.303 djm 1032: ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr,
1.355 djm 1033: options.port, pw, timeout_ms);
1.339 djm 1034:
1035: if (packet_connection_is_on_socket()) {
1036: verbose("Authenticated to %s ([%s]:%d).", host,
1037: get_remote_ipaddr(), get_remote_port());
1038: } else {
1039: verbose("Authenticated to %s (via proxy).", host);
1040: }
1.31 markus 1041:
1.112 markus 1042: /* We no longer need the private host keys. Clear them now. */
1043: if (sensitive_data.nkeys != 0) {
1044: for (i = 0; i < sensitive_data.nkeys; i++) {
1045: if (sensitive_data.keys[i] != NULL) {
1046: /* Destroys contents safely */
1047: debug3("clear hostkey %d", i);
1048: key_free(sensitive_data.keys[i]);
1049: sensitive_data.keys[i] = NULL;
1050: }
1051: }
1.378 djm 1052: free(sensitive_data.keys);
1.134 markus 1053: }
1054: for (i = 0; i < options.num_identity_files; i++) {
1.378 djm 1055: free(options.identity_files[i]);
1056: options.identity_files[i] = NULL;
1.134 markus 1057: if (options.identity_keys[i]) {
1058: key_free(options.identity_keys[i]);
1059: options.identity_keys[i] = NULL;
1060: }
1.112 markus 1061: }
1.31 markus 1062:
1.45 markus 1063: exit_status = compat20 ? ssh_session2() : ssh_session();
1064: packet_close();
1.186 djm 1065:
1.312 djm 1066: if (options.control_path != NULL && muxserver_sock != -1)
1.214 djm 1067: unlink(options.control_path);
1068:
1.353 djm 1069: /* Kill ProxyCommand if it is running. */
1070: ssh_kill_proxy_command();
1.186 djm 1071:
1.45 markus 1072: return exit_status;
1073: }
1074:
1.344 djm 1075: static void
1076: control_persist_detach(void)
1077: {
1078: pid_t pid;
1.346 djm 1079: int devnull;
1.344 djm 1080:
1081: debug("%s: backgrounding master process", __func__);
1082:
1083: /*
1084: * master (current process) into the background, and make the
1085: * foreground process a client of the backgrounded master.
1086: */
1087: switch ((pid = fork())) {
1088: case -1:
1089: fatal("%s: fork: %s", __func__, strerror(errno));
1090: case 0:
1091: /* Child: master process continues mainloop */
1092: break;
1093: default:
1094: /* Parent: set up mux slave to connect to backgrounded master */
1095: debug2("%s: background process is %ld", __func__, (long)pid);
1096: stdin_null_flag = ostdin_null_flag;
1.359 djm 1097: options.request_tty = orequest_tty;
1.344 djm 1098: tty_flag = otty_flag;
1099: close(muxserver_sock);
1100: muxserver_sock = -1;
1.351 markus 1101: options.control_master = SSHCTL_MASTER_NO;
1.344 djm 1102: muxclient(options.control_path);
1103: /* muxclient() doesn't return on success. */
1104: fatal("Failed to connect to new control master");
1105: }
1.346 djm 1106: if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
1107: error("%s: open(\"/dev/null\"): %s", __func__,
1108: strerror(errno));
1109: } else {
1110: if (dup2(devnull, STDIN_FILENO) == -1 ||
1111: dup2(devnull, STDOUT_FILENO) == -1)
1112: error("%s: dup2: %s", __func__, strerror(errno));
1113: if (devnull > STDERR_FILENO)
1114: close(devnull);
1115: }
1.381 djm 1116: daemon(1, 1);
1.362 djm 1117: setproctitle("%s [mux]", options.control_path);
1.344 djm 1118: }
1119:
1120: /* Do fork() after authentication. Used by "ssh -f" */
1121: static void
1122: fork_postauth(void)
1123: {
1124: if (need_controlpersist_detach)
1125: control_persist_detach();
1126: debug("forking to background");
1127: fork_after_authentication_flag = 0;
1128: if (daemon(1, 1) < 0)
1129: fatal("daemon() failed: %.200s", strerror(errno));
1130: }
1131:
1.315 djm 1132: /* Callback for remote forward global requests */
1133: static void
1134: ssh_confirm_remote_forward(int type, u_int32_t seq, void *ctxt)
1135: {
1136: Forward *rfwd = (Forward *)ctxt;
1137:
1.324 djm 1138: /* XXX verbose() on failure? */
1.315 djm 1139: debug("remote forward %s for: listen %d, connect %s:%d",
1140: type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
1141: rfwd->listen_port, rfwd->connect_host, rfwd->connect_port);
1.366 markus 1142: if (rfwd->listen_port == 0) {
1143: if (type == SSH2_MSG_REQUEST_SUCCESS) {
1144: rfwd->allocated_port = packet_get_int();
1145: logit("Allocated port %u for remote forward to %s:%d",
1146: rfwd->allocated_port,
1147: rfwd->connect_host, rfwd->connect_port);
1148: channel_update_permitted_opens(rfwd->handle,
1149: rfwd->allocated_port);
1150: } else {
1151: channel_update_permitted_opens(rfwd->handle, -1);
1152: }
1.324 djm 1153: }
1154:
1.315 djm 1155: if (type == SSH2_MSG_REQUEST_FAILURE) {
1156: if (options.exit_on_forward_failure)
1157: fatal("Error: remote port forwarding failed for "
1158: "listen port %d", rfwd->listen_port);
1159: else
1160: logit("Warning: remote port forwarding failed for "
1161: "listen port %d", rfwd->listen_port);
1162: }
1.318 djm 1163: if (++remote_forward_confirms_received == options.num_remote_forwards) {
1.315 djm 1164: debug("All remote forwarding requests processed");
1.344 djm 1165: if (fork_after_authentication_flag)
1166: fork_postauth();
1.318 djm 1167: }
1.315 djm 1168: }
1169:
1.126 itojun 1170: static void
1.331 dtucker 1171: client_cleanup_stdio_fwd(int id, void *arg)
1172: {
1173: debug("stdio forwarding: done");
1174: cleanup_exit(0);
1175: }
1176:
1.368 djm 1177: static void
1178: ssh_init_stdio_forwarding(void)
1.331 dtucker 1179: {
1180: Channel *c;
1.332 djm 1181: int in, out;
1.331 dtucker 1182:
1.368 djm 1183: if (stdio_forward_host == NULL)
1184: return;
1.384 djm 1185: if (!compat20)
1.368 djm 1186: fatal("stdio forwarding require Protocol 2");
1187:
1188: debug3("%s: %s:%d", __func__, stdio_forward_host, stdio_forward_port);
1.332 djm 1189:
1.368 djm 1190: if ((in = dup(STDIN_FILENO)) < 0 ||
1191: (out = dup(STDOUT_FILENO)) < 0)
1.332 djm 1192: fatal("channel_connect_stdio_fwd: dup() in/out failed");
1.368 djm 1193: if ((c = channel_connect_stdio_fwd(stdio_forward_host,
1194: stdio_forward_port, in, out)) == NULL)
1195: fatal("%s: channel_connect_stdio_fwd failed", __func__);
1.331 dtucker 1196: channel_register_cleanup(c->self, client_cleanup_stdio_fwd, 0);
1197: }
1198:
1199: static void
1.70 markus 1200: ssh_init_forwarding(void)
1201: {
1.86 markus 1202: int success = 0;
1.70 markus 1203: int i;
1.331 dtucker 1204:
1.70 markus 1205: /* Initiate local TCP/IP port forwardings. */
1206: for (i = 0; i < options.num_local_forwards; i++) {
1.232 djm 1207: debug("Local connections to %.200s:%d forwarded to remote "
1208: "address %.200s:%d",
1.234 deraadt 1209: (options.local_forwards[i].listen_host == NULL) ?
1210: (options.gateway_ports ? "*" : "LOCALHOST") :
1.232 djm 1211: options.local_forwards[i].listen_host,
1212: options.local_forwards[i].listen_port,
1213: options.local_forwards[i].connect_host,
1214: options.local_forwards[i].connect_port);
1.158 markus 1215: success += channel_setup_local_fwd_listener(
1.232 djm 1216: options.local_forwards[i].listen_host,
1217: options.local_forwards[i].listen_port,
1218: options.local_forwards[i].connect_host,
1219: options.local_forwards[i].connect_port,
1.70 markus 1220: options.gateway_ports);
1221: }
1.283 markus 1222: if (i > 0 && success != i && options.exit_on_forward_failure)
1223: fatal("Could not request local forwarding.");
1.86 markus 1224: if (i > 0 && success == 0)
1225: error("Could not request local forwarding.");
1.70 markus 1226:
1227: /* Initiate remote TCP/IP port forwardings. */
1228: for (i = 0; i < options.num_remote_forwards; i++) {
1.232 djm 1229: debug("Remote connections from %.200s:%d forwarded to "
1230: "local address %.200s:%d",
1.248 djm 1231: (options.remote_forwards[i].listen_host == NULL) ?
1.253 djm 1232: "LOCALHOST" : options.remote_forwards[i].listen_host,
1.232 djm 1233: options.remote_forwards[i].listen_port,
1234: options.remote_forwards[i].connect_host,
1235: options.remote_forwards[i].connect_port);
1.366 markus 1236: options.remote_forwards[i].handle =
1237: channel_request_remote_forwarding(
1.232 djm 1238: options.remote_forwards[i].listen_host,
1239: options.remote_forwards[i].listen_port,
1240: options.remote_forwards[i].connect_host,
1.366 markus 1241: options.remote_forwards[i].connect_port);
1242: if (options.remote_forwards[i].handle < 0) {
1.283 markus 1243: if (options.exit_on_forward_failure)
1244: fatal("Could not request remote forwarding.");
1245: else
1246: logit("Warning: Could not request remote "
1247: "forwarding.");
1.366 markus 1248: } else {
1249: client_register_global_confirm(ssh_confirm_remote_forward,
1250: &options.remote_forwards[i]);
1.283 markus 1251: }
1.70 markus 1252: }
1.301 djm 1253:
1254: /* Initiate tunnel forwarding. */
1255: if (options.tun_open != SSH_TUNMODE_NO) {
1256: if (client_request_tun_fwd(options.tun_open,
1257: options.tun_local, options.tun_remote) == -1) {
1258: if (options.exit_on_forward_failure)
1259: fatal("Could not request tunnel forwarding.");
1260: else
1261: error("Could not request tunnel forwarding.");
1262: }
1263: }
1.70 markus 1264: }
1265:
1.126 itojun 1266: static void
1.70 markus 1267: check_agent_present(void)
1268: {
1269: if (options.forward_agent) {
1.254 djm 1270: /* Clear agent forwarding if we don't have an agent. */
1.185 stevesk 1271: if (!ssh_agent_present())
1.70 markus 1272: options.forward_agent = 0;
1273: }
1274: }
1275:
1.126 itojun 1276: static int
1.45 markus 1277: ssh_session(void)
1278: {
1279: int type;
1280: int interactive = 0;
1281: int have_tty = 0;
1282: struct winsize ws;
1283: char *cp;
1.243 djm 1284: const char *display;
1.45 markus 1285:
1.31 markus 1286: /* Enable compression if requested. */
1287: if (options.compression) {
1.316 djm 1288: debug("Requesting compression at level %d.",
1289: options.compression_level);
1.31 markus 1290:
1.316 djm 1291: if (options.compression_level < 1 ||
1292: options.compression_level > 9)
1293: fatal("Compression level must be from 1 (fast) to "
1294: "9 (slow, best).");
1.31 markus 1295:
1296: /* Send the request. */
1297: packet_start(SSH_CMSG_REQUEST_COMPRESSION);
1298: packet_put_int(options.compression_level);
1299: packet_send();
1300: packet_write_wait();
1.156 markus 1301: type = packet_read();
1.31 markus 1302: if (type == SSH_SMSG_SUCCESS)
1303: packet_start_compression(options.compression_level);
1304: else if (type == SSH_SMSG_FAILURE)
1.191 itojun 1305: logit("Warning: Remote host refused compression.");
1.31 markus 1306: else
1.316 djm 1307: packet_disconnect("Protocol error waiting for "
1308: "compression response.");
1.31 markus 1309: }
1310: /* Allocate a pseudo tty if appropriate. */
1311: if (tty_flag) {
1312: debug("Requesting pty.");
1313:
1314: /* Start the packet. */
1315: packet_start(SSH_CMSG_REQUEST_PTY);
1316:
1317: /* Store TERM in the packet. There is no limit on the
1318: length of the string. */
1319: cp = getenv("TERM");
1320: if (!cp)
1321: cp = "";
1.124 markus 1322: packet_put_cstring(cp);
1.31 markus 1323:
1324: /* Store window size in the packet. */
1325: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
1326: memset(&ws, 0, sizeof(ws));
1.269 deraadt 1327: packet_put_int((u_int)ws.ws_row);
1328: packet_put_int((u_int)ws.ws_col);
1329: packet_put_int((u_int)ws.ws_xpixel);
1330: packet_put_int((u_int)ws.ws_ypixel);
1.31 markus 1331:
1332: /* Store tty modes in the packet. */
1.115 stevesk 1333: tty_make_modes(fileno(stdin), NULL);
1.31 markus 1334:
1335: /* Send the packet, and wait for it to leave. */
1336: packet_send();
1337: packet_write_wait();
1338:
1339: /* Read response from the server. */
1.156 markus 1340: type = packet_read();
1.43 markus 1341: if (type == SSH_SMSG_SUCCESS) {
1.31 markus 1342: interactive = 1;
1.45 markus 1343: have_tty = 1;
1.43 markus 1344: } else if (type == SSH_SMSG_FAILURE)
1.316 djm 1345: logit("Warning: Remote host failed or refused to "
1346: "allocate a pseudo tty.");
1.31 markus 1347: else
1.316 djm 1348: packet_disconnect("Protocol error waiting for pty "
1349: "request response.");
1.31 markus 1350: }
1351: /* Request X11 forwarding if enabled and DISPLAY is set. */
1.243 djm 1352: display = getenv("DISPLAY");
1353: if (options.forward_x11 && display != NULL) {
1.150 stevesk 1354: char *proto, *data;
1.50 markus 1355: /* Get reasonable local authentication information. */
1.243 djm 1356: client_x11_get_proto(display, options.xauth_location,
1.384 djm 1357: options.forward_x11_trusted,
1.340 djm 1358: options.forward_x11_timeout,
1359: &proto, &data);
1.50 markus 1360: /* Request forwarding with authentication spoofing. */
1.316 djm 1361: debug("Requesting X11 forwarding with authentication "
1362: "spoofing.");
1.363 djm 1363: x11_request_forwarding_with_spoofing(0, display, proto,
1364: data, 0);
1.31 markus 1365: /* Read response from the server. */
1.156 markus 1366: type = packet_read();
1.31 markus 1367: if (type == SSH_SMSG_SUCCESS) {
1368: interactive = 1;
1.50 markus 1369: } else if (type == SSH_SMSG_FAILURE) {
1.191 itojun 1370: logit("Warning: Remote host denied X11 forwarding.");
1.50 markus 1371: } else {
1.316 djm 1372: packet_disconnect("Protocol error waiting for X11 "
1373: "forwarding");
1.50 markus 1374: }
1.31 markus 1375: }
1376: /* Tell the packet module whether this is an interactive session. */
1.354 djm 1377: packet_set_interactive(interactive,
1378: options.ip_qos_interactive, options.ip_qos_bulk);
1.31 markus 1379:
1380: /* Request authentication agent forwarding if appropriate. */
1.70 markus 1381: check_agent_present();
1382:
1.31 markus 1383: if (options.forward_agent) {
1384: debug("Requesting authentication agent forwarding.");
1385: auth_request_forwarding();
1386:
1387: /* Read response from the server. */
1.156 markus 1388: type = packet_read();
1.155 markus 1389: packet_check_eom();
1.31 markus 1390: if (type != SSH_SMSG_SUCCESS)
1.191 itojun 1391: logit("Warning: Remote host denied authentication agent forwarding.");
1.31 markus 1392: }
1393:
1.70 markus 1394: /* Initiate port forwardings. */
1.368 djm 1395: ssh_init_stdio_forwarding();
1.70 markus 1396: ssh_init_forwarding();
1.305 dtucker 1397:
1398: /* Execute a local command */
1399: if (options.local_command != NULL &&
1400: options.permit_local_command)
1401: ssh_local_cmd(options.local_command);
1.34 markus 1402:
1.318 djm 1403: /*
1404: * If requested and we are not interested in replies to remote
1405: * forwarding requests, then let ssh continue in the background.
1406: */
1.344 djm 1407: if (fork_after_authentication_flag) {
1408: if (options.exit_on_forward_failure &&
1409: options.num_remote_forwards > 0) {
1410: debug("deferring postauth fork until remote forward "
1411: "confirmation received");
1412: } else
1413: fork_postauth();
1.318 djm 1414: }
1.31 markus 1415:
1.33 markus 1416: /*
1417: * If a command was specified on the command line, execute the
1418: * command now. Otherwise request the server to start a shell.
1419: */
1.31 markus 1420: if (buffer_len(&command) > 0) {
1421: int len = buffer_len(&command);
1422: if (len > 900)
1423: len = 900;
1.316 djm 1424: debug("Sending command: %.*s", len,
1425: (u_char *)buffer_ptr(&command));
1.31 markus 1426: packet_start(SSH_CMSG_EXEC_CMD);
1427: packet_put_string(buffer_ptr(&command), buffer_len(&command));
1428: packet_send();
1429: packet_write_wait();
1430: } else {
1431: debug("Requesting shell.");
1432: packet_start(SSH_CMSG_EXEC_SHELL);
1433: packet_send();
1434: packet_write_wait();
1435: }
1436:
1437: /* Enter the interactive session. */
1.119 stevesk 1438: return client_loop(have_tty, tty_flag ?
1439: options.escape_char : SSH_ESCAPECHAR_NONE, 0);
1.89 markus 1440: }
1441:
1.214 djm 1442: /* request pty/x11/agent/tcpfwd/shell for channel */
1443: static void
1.337 djm 1444: ssh_session2_setup(int id, int success, void *arg)
1.214 djm 1445: {
1.215 djm 1446: extern char **environ;
1.243 djm 1447: const char *display;
1448: int interactive = tty_flag;
1.337 djm 1449:
1450: if (!success)
1451: return; /* No need for error message, channels code sens one */
1.215 djm 1452:
1.248 djm 1453: display = getenv("DISPLAY");
1.243 djm 1454: if (options.forward_x11 && display != NULL) {
1.150 stevesk 1455: char *proto, *data;
1.50 markus 1456: /* Get reasonable local authentication information. */
1.243 djm 1457: client_x11_get_proto(display, options.xauth_location,
1.340 djm 1458: options.forward_x11_trusted,
1459: options.forward_x11_timeout, &proto, &data);
1.50 markus 1460: /* Request forwarding with authentication spoofing. */
1.316 djm 1461: debug("Requesting X11 forwarding with authentication "
1462: "spoofing.");
1.363 djm 1463: x11_request_forwarding_with_spoofing(id, display, proto,
1464: data, 1);
1465: client_expect_confirm(id, "X11 forwarding", CONFIRM_WARN);
1466: /* XXX exit_on_forward_failure */
1.80 markus 1467: interactive = 1;
1.50 markus 1468: }
1469:
1.70 markus 1470: check_agent_present();
1471: if (options.forward_agent) {
1472: debug("Requesting authentication agent forwarding.");
1473: channel_request_start(id, "auth-agent-req@openssh.com", 0);
1474: packet_send();
1.212 djm 1475: }
1.369 dtucker 1476:
1477: /* Tell the packet module whether this is an interactive session. */
1478: packet_set_interactive(interactive,
1479: options.ip_qos_interactive, options.ip_qos_bulk);
1.212 djm 1480:
1.214 djm 1481: client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
1.311 djm 1482: NULL, fileno(stdin), &command, environ);
1.45 markus 1483: }
1484:
1.143 markus 1485: /* open new channel for a session */
1.126 itojun 1486: static int
1.143 markus 1487: ssh_session2_open(void)
1.45 markus 1488: {
1.118 markus 1489: Channel *c;
1490: int window, packetmax, in, out, err;
1.60 markus 1491:
1.62 markus 1492: if (stdin_null_flag) {
1.93 itojun 1493: in = open(_PATH_DEVNULL, O_RDONLY);
1.62 markus 1494: } else {
1495: in = dup(STDIN_FILENO);
1496: }
1.60 markus 1497: out = dup(STDOUT_FILENO);
1498: err = dup(STDERR_FILENO);
1.45 markus 1499:
1500: if (in < 0 || out < 0 || err < 0)
1.62 markus 1501: fatal("dup() in/out/err failed");
1.45 markus 1502:
1.69 markus 1503: /* enable nonblocking unless tty */
1504: if (!isatty(in))
1505: set_nonblock(in);
1506: if (!isatty(out))
1507: set_nonblock(out);
1508: if (!isatty(err))
1509: set_nonblock(err);
1510:
1.65 markus 1511: window = CHAN_SES_WINDOW_DEFAULT;
1512: packetmax = CHAN_SES_PACKET_DEFAULT;
1.164 markus 1513: if (tty_flag) {
1514: window >>= 1;
1515: packetmax >>= 1;
1.45 markus 1516: }
1.118 markus 1517: c = channel_new(
1.45 markus 1518: "session", SSH_CHANNEL_OPENING, in, out, err,
1.65 markus 1519: window, packetmax, CHAN_EXTENDED_WRITE,
1.192 markus 1520: "client-session", /*nonblock*/0);
1.45 markus 1521:
1.143 markus 1522: debug3("ssh_session2_open: channel_new: %d", c->self);
1.106 markus 1523:
1.122 markus 1524: channel_send_open(c->self);
1.143 markus 1525: if (!no_shell_flag)
1.310 djm 1526: channel_register_open_confirm(c->self,
1527: ssh_session2_setup, NULL);
1.106 markus 1528:
1.118 markus 1529: return c->self;
1.106 markus 1530: }
1531:
1.126 itojun 1532: static int
1.106 markus 1533: ssh_session2(void)
1534: {
1.143 markus 1535: int id = -1;
1.106 markus 1536:
1537: /* XXX should be pre-session */
1.368 djm 1538: if (!options.control_persist)
1539: ssh_init_stdio_forwarding();
1.106 markus 1540: ssh_init_forwarding();
1541:
1.344 djm 1542: /* Start listening for multiplex clients */
1543: muxserver_listen();
1544:
1545: /*
1.368 djm 1546: * If we are in control persist mode and have a working mux listen
1547: * socket, then prepare to background ourselves and have a foreground
1548: * client attach as a control slave.
1549: * NB. we must save copies of the flags that we override for
1.344 djm 1550: * the backgrounding, since we defer attachment of the slave until
1551: * after the connection is fully established (in particular,
1552: * async rfwd replies have been received for ExitOnForwardFailure).
1553: */
1554: if (options.control_persist && muxserver_sock != -1) {
1555: ostdin_null_flag = stdin_null_flag;
1556: ono_shell_flag = no_shell_flag;
1.359 djm 1557: orequest_tty = options.request_tty;
1.344 djm 1558: otty_flag = tty_flag;
1559: stdin_null_flag = 1;
1560: no_shell_flag = 1;
1561: tty_flag = 0;
1562: if (!fork_after_authentication_flag)
1563: need_controlpersist_detach = 1;
1564: fork_after_authentication_flag = 1;
1565: }
1.368 djm 1566: /*
1567: * ControlPersist mux listen socket setup failed, attempt the
1568: * stdio forward setup that we skipped earlier.
1569: */
1570: if (options.control_persist && muxserver_sock == -1)
1571: ssh_init_stdio_forwarding();
1.344 djm 1572:
1.143 markus 1573: if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1574: id = ssh_session2_open();
1.379 djm 1575: else {
1576: packet_set_interactive(
1577: options.control_master == SSHCTL_MASTER_NO,
1578: options.ip_qos_interactive, options.ip_qos_bulk);
1579: }
1.314 djm 1580:
1581: /* If we don't expect to open a new session, then disallow it */
1.319 markus 1582: if (options.control_master == SSHCTL_MASTER_NO &&
1583: (datafellows & SSH_NEW_OPENSSH)) {
1.314 djm 1584: debug("Requesting no-more-sessions@openssh.com");
1585: packet_start(SSH2_MSG_GLOBAL_REQUEST);
1586: packet_put_cstring("no-more-sessions@openssh.com");
1587: packet_put_char(0);
1588: packet_send();
1589: }
1.255 reyk 1590:
1591: /* Execute a local command */
1592: if (options.local_command != NULL &&
1593: options.permit_local_command)
1594: ssh_local_cmd(options.local_command);
1.301 djm 1595:
1.342 djm 1596: /*
1597: * If requested and we are not interested in replies to remote
1598: * forwarding requests, then let ssh continue in the background.
1599: */
1.344 djm 1600: if (fork_after_authentication_flag) {
1601: if (options.exit_on_forward_failure &&
1602: options.num_remote_forwards > 0) {
1603: debug("deferring postauth fork until remote forward "
1604: "confirmation received");
1605: } else
1606: fork_postauth();
1.318 djm 1607: }
1.327 andreas 1608:
1609: if (options.use_roaming)
1610: request_roaming();
1.31 markus 1611:
1.119 stevesk 1612: return client_loop(tty_flag, tty_flag ?
1613: options.escape_char : SSH_ESCAPECHAR_NONE, id);
1.72 markus 1614: }
1615:
1.126 itojun 1616: static void
1.104 markus 1617: load_public_identity_files(void)
1618: {
1.275 djm 1619: char *filename, *cp, thishost[NI_MAXHOST];
1.306 deraadt 1620: char *pwdir = NULL, *pwname = NULL;
1.167 markus 1621: int i = 0;
1.104 markus 1622: Key *public;
1.275 djm 1623: struct passwd *pw;
1.335 djm 1624: u_int n_ids;
1625: char *identity_files[SSH_MAX_IDENTITY_FILES];
1626: Key *identity_keys[SSH_MAX_IDENTITY_FILES];
1.333 markus 1627: #ifdef ENABLE_PKCS11
1.167 markus 1628: Key **keys;
1.333 markus 1629: int nkeys;
1.335 djm 1630: #endif /* PKCS11 */
1.104 markus 1631:
1.335 djm 1632: n_ids = 0;
1633: bzero(identity_files, sizeof(identity_files));
1634: bzero(identity_keys, sizeof(identity_keys));
1635:
1636: #ifdef ENABLE_PKCS11
1.333 markus 1637: if (options.pkcs11_provider != NULL &&
1.167 markus 1638: options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1.333 markus 1639: (pkcs11_init(!options.batch_mode) == 0) &&
1640: (nkeys = pkcs11_add_provider(options.pkcs11_provider, NULL,
1641: &keys)) > 0) {
1642: for (i = 0; i < nkeys; i++) {
1.335 djm 1643: if (n_ids >= SSH_MAX_IDENTITY_FILES) {
1644: key_free(keys[i]);
1645: continue;
1646: }
1647: identity_keys[n_ids] = keys[i];
1648: identity_files[n_ids] =
1.333 markus 1649: xstrdup(options.pkcs11_provider); /* XXX */
1.335 djm 1650: n_ids++;
1.167 markus 1651: }
1.378 djm 1652: free(keys);
1.127 markus 1653: }
1.333 markus 1654: #endif /* ENABLE_PKCS11 */
1.275 djm 1655: if ((pw = getpwuid(original_real_uid)) == NULL)
1656: fatal("load_public_identity_files: getpwuid failed");
1.307 dtucker 1657: pwname = xstrdup(pw->pw_name);
1658: pwdir = xstrdup(pw->pw_dir);
1.275 djm 1659: if (gethostname(thishost, sizeof(thishost)) == -1)
1660: fatal("load_public_identity_files: gethostname: %s",
1661: strerror(errno));
1.335 djm 1662: for (i = 0; i < options.num_identity_files; i++) {
1.373 djm 1663: if (n_ids >= SSH_MAX_IDENTITY_FILES ||
1664: strcasecmp(options.identity_files[i], "none") == 0) {
1.378 djm 1665: free(options.identity_files[i]);
1.335 djm 1666: continue;
1667: }
1.275 djm 1668: cp = tilde_expand_filename(options.identity_files[i],
1.131 millert 1669: original_real_uid);
1.306 deraadt 1670: filename = percent_expand(cp, "d", pwdir,
1671: "u", pwname, "l", thishost, "h", host,
1.275 djm 1672: "r", options.user, (char *)NULL);
1.378 djm 1673: free(cp);
1.131 millert 1674: public = key_load_public(filename, NULL);
1675: debug("identity file %s type %d", filename,
1676: public ? public->type : -1);
1.378 djm 1677: free(options.identity_files[i]);
1.335 djm 1678: identity_files[n_ids] = filename;
1679: identity_keys[n_ids] = public;
1680:
1681: if (++n_ids >= SSH_MAX_IDENTITY_FILES)
1682: continue;
1683:
1684: /* Try to add the certificate variant too */
1685: xasprintf(&cp, "%s-cert", filename);
1686: public = key_load_public(cp, NULL);
1687: debug("identity file %s type %d", cp,
1688: public ? public->type : -1);
1689: if (public == NULL) {
1.378 djm 1690: free(cp);
1.335 djm 1691: continue;
1692: }
1693: if (!key_is_cert(public)) {
1694: debug("%s: key %s type %s is not a certificate",
1695: __func__, cp, key_type(public));
1696: key_free(public);
1.378 djm 1697: free(cp);
1.335 djm 1698: continue;
1699: }
1700: identity_keys[n_ids] = public;
1701: /* point to the original path, most likely the private key */
1702: identity_files[n_ids] = xstrdup(filename);
1703: n_ids++;
1704: }
1705: options.num_identity_files = n_ids;
1706: memcpy(options.identity_files, identity_files, sizeof(identity_files));
1707: memcpy(options.identity_keys, identity_keys, sizeof(identity_keys));
1708:
1.306 deraadt 1709: bzero(pwname, strlen(pwname));
1.378 djm 1710: free(pwname);
1.306 deraadt 1711: bzero(pwdir, strlen(pwdir));
1.378 djm 1712: free(pwdir);
1.214 djm 1713: }
1.352 djm 1714:
1715: static void
1716: main_sigchld_handler(int sig)
1717: {
1718: int save_errno = errno;
1719: pid_t pid;
1720: int status;
1721:
1722: while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
1723: (pid < 0 && errno == EINTR))
1724: ;
1725:
1726: signal(sig, main_sigchld_handler);
1727: errno = save_errno;
1728: }