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