Annotation of src/usr.bin/ssh/ssh.c, Revision 1.527
1.527 ! dtucker 1: /* $OpenBSD: ssh.c,v 1.526 2020/04/03 06:07:57 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>
45: #include <sys/queue.h>
1.259 stevesk 46: #include <sys/resource.h>
1.280 stevesk 47: #include <sys/socket.h>
1.264 stevesk 48: #include <sys/stat.h>
1.312 djm 49: #include <sys/time.h>
1.352 djm 50: #include <sys/wait.h>
1.258 stevesk 51:
1.265 stevesk 52: #include <ctype.h>
1.285 stevesk 53: #include <errno.h>
1.281 stevesk 54: #include <fcntl.h>
1.286 stevesk 55: #include <netdb.h>
1.258 stevesk 56: #include <paths.h>
1.279 stevesk 57: #include <pwd.h>
1.263 stevesk 58: #include <signal.h>
1.287 stevesk 59: #include <stddef.h>
1.291 stevesk 60: #include <stdio.h>
1.290 stevesk 61: #include <stdlib.h>
1.289 stevesk 62: #include <string.h>
1.509 naddy 63: #include <stdarg.h>
1.288 stevesk 64: #include <unistd.h>
1.414 deraadt 65: #include <limits.h>
1.445 djm 66: #include <locale.h>
1.49 markus 67:
1.402 markus 68: #ifdef WITH_OPENSSL
1.49 markus 69: #include <openssl/evp.h>
1.72 markus 70: #include <openssl/err.h>
1.402 markus 71: #endif
1.1 deraadt 72:
1.293 deraadt 73: #include "xmalloc.h"
1.84 markus 74: #include "ssh.h"
75: #include "ssh2.h"
1.341 djm 76: #include "canohost.h"
1.84 markus 77: #include "compat.h"
78: #include "cipher.h"
1.1 deraadt 79: #include "packet.h"
1.482 markus 80: #include "sshbuf.h"
1.123 markus 81: #include "channels.h"
1.483 markus 82: #include "sshkey.h"
1.58 markus 83: #include "authfd.h"
1.49 markus 84: #include "authfile.h"
1.83 markus 85: #include "pathnames.h"
1.214 djm 86: #include "dispatch.h"
1.81 markus 87: #include "clientloop.h"
1.84 markus 88: #include "log.h"
1.406 millert 89: #include "misc.h"
1.84 markus 90: #include "readconf.h"
91: #include "sshconnect.h"
1.95 markus 92: #include "kex.h"
93: #include "mac.h"
1.213 deraadt 94: #include "sshpty.h"
1.212 djm 95: #include "match.h"
1.214 djm 96: #include "msg.h"
1.278 stevesk 97: #include "version.h"
1.412 djm 98: #include "ssherr.h"
1.420 djm 99: #include "myproposal.h"
1.49 markus 100:
1.333 markus 101: #ifdef ENABLE_PKCS11
102: #include "ssh-pkcs11.h"
1.137 jakob 103: #endif
1.498 djm 104:
1.49 markus 105: extern char *__progname;
1.1 deraadt 106:
1.316 djm 107: /* Flag indicating whether debug mode is on. May be set on the command line. */
1.1 deraadt 108: int debug_flag = 0;
109:
1.359 djm 110: /* Flag indicating whether a tty should be requested */
1.1 deraadt 111: int tty_flag = 0;
112:
1.45 markus 113: /* don't exec a shell */
114: int no_shell_flag = 0;
115:
1.33 markus 116: /*
117: * Flag indicating that nothing should be read from stdin. This can be set
118: * on the command line.
119: */
1.1 deraadt 120: int stdin_null_flag = 0;
121:
1.33 markus 122: /*
1.344 djm 123: * Flag indicating that the current process should be backgrounded and
124: * a new slave launched in the foreground for ControlPersist.
125: */
126: int need_controlpersist_detach = 0;
127:
128: /* Copies of flags for ControlPersist foreground slave */
1.359 djm 129: int ostdin_null_flag, ono_shell_flag, otty_flag, orequest_tty;
1.344 djm 130:
131: /*
1.33 markus 132: * Flag indicating that ssh should fork after authentication. This is useful
1.172 deraadt 133: * so that the passphrase can be entered manually, and then ssh goes to the
1.33 markus 134: * background.
135: */
1.1 deraadt 136: int fork_after_authentication_flag = 0;
137:
1.33 markus 138: /*
139: * General data structure for command line options and options configurable
140: * in configuration files. See readconf.h.
141: */
1.1 deraadt 142: Options options;
143:
1.139 markus 144: /* optional user configfile */
145: char *config = NULL;
146:
1.33 markus 147: /*
148: * Name of the host we are connecting to. This is the name given on the
1.503 jmc 149: * command line, or the Hostname specified for the user-supplied name in a
1.33 markus 150: * configuration file.
151: */
1.1 deraadt 152: char *host;
153:
1.510 djm 154: /*
155: * A config can specify a path to forward, overriding SSH_AUTH_SOCK. If this is
156: * not NULL, forward the socket at this path instead.
157: */
158: char *forward_agent_sock_path = NULL;
159:
1.466 djm 160: /* Various strings used to to percent_expand() arguments */
161: static char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
162: static char uidstr[32], *host_arg, *conn_hash_hex;
163:
1.22 provos 164: /* socket address the host resolves to */
1.37 markus 165: struct sockaddr_storage hostaddr;
1.1 deraadt 166:
1.112 markus 167: /* Private host keys. */
1.173 markus 168: Sensitive sensitive_data;
1.1 deraadt 169:
1.45 markus 170: /* command to be executed */
1.482 markus 171: struct sshbuf *command;
1.45 markus 172:
1.85 djm 173: /* Should we execute a command or invoke a subsystem? */
174: int subsystem_flag = 0;
175:
1.170 markus 176: /* # of replies received for global requests */
1.523 djm 177: static int forward_confirms_pending = -1;
1.170 markus 178:
1.313 djm 179: /* mux.c */
180: extern int muxserver_sock;
181: extern u_int muxclient_command;
182:
1.1 deraadt 183: /* Prints a help message to the user. This function never returns. */
184:
1.126 itojun 185: static void
1.93 itojun 186: usage(void)
1.1 deraadt 187: {
1.208 markus 188: fprintf(stderr,
1.474 djm 189: "usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-B bind_interface]\n"
190: " [-b bind_address] [-c cipher_spec] [-D [bind_address:]port]\n"
191: " [-E log_file] [-e escape_char] [-F configfile] [-I pkcs11]\n"
192: " [-i identity_file] [-J [user@]host[:port]] [-L address]\n"
193: " [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
194: " [-Q query_option] [-R address] [-S ctl_path] [-W host:port]\n"
195: " [-w local_tun[:remote_tun]] destination [command]\n"
1.208 markus 196: );
1.257 dtucker 197: exit(255);
1.1 deraadt 198: }
199:
1.466 djm 200: static int ssh_session2(struct ssh *, struct passwd *);
201: static void load_public_identity_files(struct passwd *);
1.352 djm 202: static void main_sigchld_handler(int);
1.312 djm 203:
1.361 djm 204: /* ~/ expand a list of paths. NB. assumes path[n] is heap-allocated. */
205: static void
206: tilde_expand_paths(char **paths, u_int num_paths)
207: {
208: u_int i;
209: char *cp;
210:
211: for (i = 0; i < num_paths; i++) {
1.490 dtucker 212: cp = tilde_expand_filename(paths[i], getuid());
1.378 djm 213: free(paths[i]);
1.361 djm 214: paths[i] = cp;
215: }
216: }
217:
1.527 ! dtucker 218: #define DEFAULT_CLIENT_PERCENT_EXPAND_ARGS \
! 219: "C", conn_hash_hex, \
! 220: "L", shorthost, \
! 221: "i", uidstr, \
! 222: "l", thishost, \
! 223: "n", host_arg, \
! 224: "p", portstr
! 225:
! 226: /*
! 227: * Expands the set of percent_expand options used by the majority of keywords
! 228: * in the client that support percent expansion.
! 229: * Caller must free returned string.
! 230: */
! 231: static char *
! 232: default_client_percent_expand(const char *str, const char *homedir,
! 233: const char *remhost, const char *remuser, const char *locuser)
! 234: {
! 235: return percent_expand(str,
! 236: /* values from statics above */
! 237: DEFAULT_CLIENT_PERCENT_EXPAND_ARGS,
! 238: /* values from arguments */
! 239: "d", homedir,
! 240: "h", remhost,
! 241: "r", remuser,
! 242: "u", locuser,
! 243: (char *)NULL);
! 244: }
! 245:
1.400 djm 246: /*
247: * Attempt to resolve a host name / port to a set of addresses and
248: * optionally return any CNAMEs encountered along the way.
249: * Returns NULL on failure.
250: * NB. this function must operate with a options having undefined members.
251: */
1.385 djm 252: static struct addrinfo *
1.400 djm 253: resolve_host(const char *name, int port, int logerr, char *cname, size_t clen)
1.385 djm 254: {
255: char strport[NI_MAXSERV];
256: struct addrinfo hints, *res;
1.501 dtucker 257: int gaierr;
258: LogLevel loglevel = SYSLOG_LEVEL_DEBUG1;
1.385 djm 259:
1.400 djm 260: if (port <= 0)
261: port = default_ssh_port();
1.521 markus 262: if (cname != NULL)
263: *cname = '\0';
1.400 djm 264:
1.427 djm 265: snprintf(strport, sizeof strport, "%d", port);
1.398 tedu 266: memset(&hints, 0, sizeof(hints));
1.400 djm 267: hints.ai_family = options.address_family == -1 ?
268: AF_UNSPEC : options.address_family;
1.385 djm 269: hints.ai_socktype = SOCK_STREAM;
270: if (cname != NULL)
271: hints.ai_flags = AI_CANONNAME;
272: if ((gaierr = getaddrinfo(name, strport, &hints, &res)) != 0) {
273: if (logerr || (gaierr != EAI_NONAME && gaierr != EAI_NODATA))
274: loglevel = SYSLOG_LEVEL_ERROR;
275: do_log2(loglevel, "%s: Could not resolve hostname %.100s: %s",
276: __progname, name, ssh_gai_strerror(gaierr));
277: return NULL;
278: }
279: if (cname != NULL && res->ai_canonname != NULL) {
280: if (strlcpy(cname, res->ai_canonname, clen) >= clen) {
281: error("%s: host \"%s\" cname \"%s\" too long (max %lu)",
282: __func__, name, res->ai_canonname, (u_long)clen);
283: if (clen > 0)
284: *cname = '\0';
285: }
286: }
287: return res;
288: }
289:
1.470 djm 290: /* Returns non-zero if name can only be an address and not a hostname */
291: static int
292: is_addr_fast(const char *name)
293: {
294: return (strchr(name, '%') != NULL || strchr(name, ':') != NULL ||
295: strspn(name, "0123456789.") == strlen(name));
296: }
297:
298: /* Returns non-zero if name represents a valid, single address */
299: static int
300: is_addr(const char *name)
301: {
302: char strport[NI_MAXSERV];
303: struct addrinfo hints, *res;
304:
305: if (is_addr_fast(name))
306: return 1;
307:
308: snprintf(strport, sizeof strport, "%u", default_ssh_port());
309: memset(&hints, 0, sizeof(hints));
310: hints.ai_family = options.address_family == -1 ?
311: AF_UNSPEC : options.address_family;
312: hints.ai_socktype = SOCK_STREAM;
313: hints.ai_flags = AI_NUMERICHOST|AI_NUMERICSERV;
314: if (getaddrinfo(name, strport, &hints, &res) != 0)
315: return 0;
316: if (res == NULL || res->ai_next != NULL) {
317: freeaddrinfo(res);
318: return 0;
319: }
320: freeaddrinfo(res);
321: return 1;
322: }
323:
1.385 djm 324: /*
1.413 djm 325: * Attempt to resolve a numeric host address / port to a single address.
326: * Returns a canonical address string.
327: * Returns NULL on failure.
328: * NB. this function must operate with a options having undefined members.
329: */
330: static struct addrinfo *
331: resolve_addr(const char *name, int port, char *caddr, size_t clen)
332: {
333: char addr[NI_MAXHOST], strport[NI_MAXSERV];
334: struct addrinfo hints, *res;
335: int gaierr;
336:
337: if (port <= 0)
338: port = default_ssh_port();
339: snprintf(strport, sizeof strport, "%u", port);
340: memset(&hints, 0, sizeof(hints));
341: hints.ai_family = options.address_family == -1 ?
342: AF_UNSPEC : options.address_family;
343: hints.ai_socktype = SOCK_STREAM;
344: hints.ai_flags = AI_NUMERICHOST|AI_NUMERICSERV;
345: if ((gaierr = getaddrinfo(name, strport, &hints, &res)) != 0) {
346: debug2("%s: could not resolve name %.100s as address: %s",
347: __func__, name, ssh_gai_strerror(gaierr));
348: return NULL;
349: }
350: if (res == NULL) {
351: debug("%s: getaddrinfo %.100s returned no addresses",
352: __func__, name);
353: return NULL;
354: }
355: if (res->ai_next != NULL) {
356: debug("%s: getaddrinfo %.100s returned multiple addresses",
357: __func__, name);
358: goto fail;
359: }
360: if ((gaierr = getnameinfo(res->ai_addr, res->ai_addrlen,
361: addr, sizeof(addr), NULL, 0, NI_NUMERICHOST)) != 0) {
362: debug("%s: Could not format address for name %.100s: %s",
363: __func__, name, ssh_gai_strerror(gaierr));
364: goto fail;
365: }
366: if (strlcpy(caddr, addr, clen) >= clen) {
367: error("%s: host \"%s\" addr \"%s\" too long (max %lu)",
368: __func__, name, addr, (u_long)clen);
369: if (clen > 0)
370: *caddr = '\0';
371: fail:
372: freeaddrinfo(res);
373: return NULL;
374: }
375: return res;
376: }
377:
378: /*
1.385 djm 379: * Check whether the cname is a permitted replacement for the hostname
380: * and perform the replacement if it is.
1.400 djm 381: * NB. this function must operate with a options having undefined members.
1.385 djm 382: */
383: static int
1.443 djm 384: check_follow_cname(int direct, char **namep, const char *cname)
1.385 djm 385: {
386: int i;
387: struct allowed_cname *rule;
388:
389: if (*cname == '\0' || options.num_permitted_cnames == 0 ||
390: strcmp(*namep, cname) == 0)
391: return 0;
1.386 djm 392: if (options.canonicalize_hostname == SSH_CANONICALISE_NO)
1.385 djm 393: return 0;
394: /*
1.386 djm 395: * Don't attempt to canonicalize names that will be interpreted by
1.443 djm 396: * a proxy or jump host unless the user specifically requests so.
1.385 djm 397: */
1.443 djm 398: if (!direct &&
1.386 djm 399: options.canonicalize_hostname != SSH_CANONICALISE_ALWAYS)
1.385 djm 400: return 0;
401: debug3("%s: check \"%s\" CNAME \"%s\"", __func__, *namep, cname);
402: for (i = 0; i < options.num_permitted_cnames; i++) {
403: rule = options.permitted_cnames + i;
1.418 djm 404: if (match_pattern_list(*namep, rule->source_list, 1) != 1 ||
405: match_pattern_list(cname, rule->target_list, 1) != 1)
1.385 djm 406: continue;
1.386 djm 407: verbose("Canonicalized DNS aliased hostname "
1.385 djm 408: "\"%s\" => \"%s\"", *namep, cname);
409: free(*namep);
410: *namep = xstrdup(cname);
411: return 1;
412: }
413: return 0;
414: }
415:
416: /*
417: * Attempt to resolve the supplied hostname after applying the user's
1.387 djm 418: * canonicalization rules. Returns the address list for the host or NULL
419: * if no name was found after canonicalization.
1.400 djm 420: * NB. this function must operate with a options having undefined members.
1.385 djm 421: */
422: static struct addrinfo *
1.400 djm 423: resolve_canonicalize(char **hostp, int port)
1.385 djm 424: {
1.443 djm 425: int i, direct, ndots;
1.413 djm 426: char *cp, *fullhost, newname[NI_MAXHOST];
1.385 djm 427: struct addrinfo *addrs;
428:
429: /*
1.470 djm 430: * Attempt to canonicalise addresses, regardless of
431: * whether hostname canonicalisation was requested
1.385 djm 432: */
1.413 djm 433: if ((addrs = resolve_addr(*hostp, port,
434: newname, sizeof(newname))) != NULL) {
435: debug2("%s: hostname %.100s is address", __func__, *hostp);
436: if (strcasecmp(*hostp, newname) != 0) {
437: debug2("%s: canonicalised address \"%s\" => \"%s\"",
438: __func__, *hostp, newname);
439: free(*hostp);
440: *hostp = xstrdup(newname);
441: }
442: return addrs;
443: }
444:
1.470 djm 445: /*
446: * If this looks like an address but didn't parse as one, it might
447: * be an address with an invalid interface scope. Skip further
448: * attempts at canonicalisation.
449: */
450: if (is_addr_fast(*hostp)) {
451: debug("%s: hostname %.100s is an unrecognised address",
452: __func__, *hostp);
453: return NULL;
454: }
455:
456: if (options.canonicalize_hostname == SSH_CANONICALISE_NO)
457: return NULL;
458:
459: /*
460: * Don't attempt to canonicalize names that will be interpreted by
461: * a proxy unless the user specifically requests so.
462: */
463: direct = option_clear_or_none(options.proxy_command) &&
464: options.jump_host == NULL;
465: if (!direct &&
466: options.canonicalize_hostname != SSH_CANONICALISE_ALWAYS)
467: return NULL;
468:
1.428 djm 469: /* If domain name is anchored, then resolve it now */
470: if ((*hostp)[strlen(*hostp) - 1] == '.') {
471: debug3("%s: name is fully qualified", __func__);
472: fullhost = xstrdup(*hostp);
473: if ((addrs = resolve_host(fullhost, port, 0,
474: newname, sizeof(newname))) != NULL)
475: goto found;
476: free(fullhost);
477: goto notfound;
478: }
479:
1.387 djm 480: /* Don't apply canonicalization to sufficiently-qualified hostnames */
1.385 djm 481: ndots = 0;
482: for (cp = *hostp; *cp != '\0'; cp++) {
483: if (*cp == '.')
484: ndots++;
485: }
1.386 djm 486: if (ndots > options.canonicalize_max_dots) {
487: debug3("%s: not canonicalizing hostname \"%s\" (max dots %d)",
488: __func__, *hostp, options.canonicalize_max_dots);
1.385 djm 489: return NULL;
490: }
491: /* Attempt each supplied suffix */
492: for (i = 0; i < options.num_canonical_domains; i++) {
493: xasprintf(&fullhost, "%s.%s.", *hostp,
494: options.canonical_domains[i]);
1.400 djm 495: debug3("%s: attempting \"%s\" => \"%s\"", __func__,
496: *hostp, fullhost);
497: if ((addrs = resolve_host(fullhost, port, 0,
1.413 djm 498: newname, sizeof(newname))) == NULL) {
1.385 djm 499: free(fullhost);
500: continue;
501: }
1.428 djm 502: found:
1.385 djm 503: /* Remove trailing '.' */
504: fullhost[strlen(fullhost) - 1] = '\0';
505: /* Follow CNAME if requested */
1.443 djm 506: if (!check_follow_cname(direct, &fullhost, newname)) {
1.386 djm 507: debug("Canonicalized hostname \"%s\" => \"%s\"",
1.385 djm 508: *hostp, fullhost);
509: }
510: free(*hostp);
511: *hostp = fullhost;
512: return addrs;
513: }
1.428 djm 514: notfound:
1.386 djm 515: if (!options.canonicalize_fallback_local)
1.400 djm 516: fatal("%s: Could not resolve host \"%s\"", __progname, *hostp);
517: debug2("%s: host %s not found in any suffix", __func__, *hostp);
1.385 djm 518: return NULL;
519: }
520:
1.32 deraadt 521: /*
1.483 markus 522: * Check the result of hostkey loading, ignoring some errors and
523: * fatal()ing for others.
524: */
525: static void
526: check_load(int r, const char *path, const char *message)
527: {
528: switch (r) {
529: case 0:
530: break;
531: case SSH_ERR_INTERNAL_ERROR:
532: case SSH_ERR_ALLOC_FAIL:
533: fatal("load %s \"%s\": %s", message, path, ssh_err(r));
534: case SSH_ERR_SYSTEM_ERROR:
535: /* Ignore missing files */
536: if (errno == ENOENT)
537: break;
538: /* FALLTHROUGH */
539: default:
540: error("load %s \"%s\": %s", message, path, ssh_err(r));
541: break;
542: }
543: }
544:
545: /*
1.400 djm 546: * Read per-user configuration file. Ignore the system wide config
547: * file if the user specifies a config file on the command line.
548: */
549: static void
1.496 djm 550: process_config_files(const char *host_name, struct passwd *pw, int final_pass,
551: int *want_final_pass)
1.400 djm 552: {
1.414 deraadt 553: char buf[PATH_MAX];
1.400 djm 554: int r;
555:
556: if (config != NULL) {
557: if (strcasecmp(config, "none") != 0 &&
1.466 djm 558: !read_config_file(config, pw, host, host_name, &options,
1.496 djm 559: SSHCONF_USERCONF | (final_pass ? SSHCONF_FINAL : 0),
560: want_final_pass))
1.400 djm 561: fatal("Can't open user config file %.100s: "
562: "%.100s", config, strerror(errno));
563: } else {
564: r = snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir,
565: _PATH_SSH_USER_CONFFILE);
566: if (r > 0 && (size_t)r < sizeof(buf))
1.466 djm 567: (void)read_config_file(buf, pw, host, host_name,
1.408 djm 568: &options, SSHCONF_CHECKPERM | SSHCONF_USERCONF |
1.496 djm 569: (final_pass ? SSHCONF_FINAL : 0), want_final_pass);
1.400 djm 570:
571: /* Read systemwide configuration file after user config. */
1.408 djm 572: (void)read_config_file(_PATH_HOST_CONFIG_FILE, pw,
1.466 djm 573: host, host_name, &options,
1.496 djm 574: final_pass ? SSHCONF_FINAL : 0, want_final_pass);
1.408 djm 575: }
576: }
577:
578: /* Rewrite the port number in an addrinfo list of addresses */
579: static void
580: set_addrinfo_port(struct addrinfo *addrs, int port)
581: {
582: struct addrinfo *addr;
583:
584: for (addr = addrs; addr != NULL; addr = addr->ai_next) {
585: switch (addr->ai_family) {
586: case AF_INET:
587: ((struct sockaddr_in *)addr->ai_addr)->
588: sin_port = htons(port);
589: break;
590: case AF_INET6:
591: ((struct sockaddr_in6 *)addr->ai_addr)->
592: sin6_port = htons(port);
593: break;
594: }
1.400 djm 595: }
596: }
597:
598: /*
1.32 deraadt 599: * Main program for the ssh client.
600: */
1.2 provos 601: int
602: main(int ac, char **av)
1.1 deraadt 603: {
1.437 djm 604: struct ssh *ssh = NULL;
1.462 djm 605: int i, r, opt, exit_status, use_syslog, direct, timeout_ms;
1.496 djm 606: int was_addr, config_test = 0, opt_terminated = 0, want_final_pass = 0;
1.466 djm 607: char *p, *cp, *line, *argv0, buf[PATH_MAX], *logfile;
608: char cname[NI_MAXHOST];
1.31 markus 609: struct stat st;
1.98 markus 610: struct passwd *pw;
1.144 stevesk 611: extern int optind, optreset;
612: extern char *optarg;
1.406 millert 613: struct Forward fwd;
1.385 djm 614: struct addrinfo *addrs = NULL;
1.513 dtucker 615: size_t n, len;
1.250 djm 616:
617: /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
618: sanitise_stdfd();
1.31 markus 619:
1.33 markus 620: /*
1.346 djm 621: * Discard other fds that are hanging around. These can cause problem
622: * with backgrounded ssh processes started by ControlPersist.
623: */
624: closefrom(STDERR_FILENO + 1);
625:
1.487 dtucker 626: if (getuid() != geteuid())
627: fatal("ssh setuid not supported.");
628: if (getgid() != getegid())
629: fatal("ssh setgid not supported.");
1.184 stevesk 630:
1.107 markus 631: /* Get user data. */
1.490 dtucker 632: pw = getpwuid(getuid());
1.107 markus 633: if (!pw) {
1.490 dtucker 634: logit("No user exists for uid %lu", (u_long)getuid());
1.257 dtucker 635: exit(255);
1.107 markus 636: }
637: /* Take a copy of the returned structure. */
638: pw = pwcopy(pw);
1.31 markus 639:
1.33 markus 640: /*
641: * Set our umask to something reasonable, as some files are created
642: * with the default umask. This will make them world-readable but
643: * writable only by the owner, which is ok for all files for which we
644: * don't set the modes explicitly.
645: */
1.31 markus 646: umask(022);
1.445 djm 647:
648: setlocale(LC_CTYPE, "");
1.31 markus 649:
1.316 djm 650: /*
651: * Initialize option structure to indicate that no values have been
652: * set.
653: */
1.31 markus 654: initialize_options(&options);
655:
1.463 djm 656: /*
657: * Prepare main ssh transport/connection structures
658: */
659: if ((ssh = ssh_alloc_session_state()) == NULL)
660: fatal("Couldn't allocate session state");
661: channel_init_channels(ssh);
662:
1.31 markus 663: /* Parse command-line arguments. */
664: host = NULL;
1.320 djm 665: use_syslog = 0;
1.375 dtucker 666: logfile = NULL;
1.325 markus 667: argv0 = av[0];
1.31 markus 668:
1.266 djm 669: again:
1.316 djm 670: while ((opt = getopt(ac, av, "1246ab:c:e:fgi:kl:m:no:p:qstvx"
1.474 djm 671: "AB:CD:E:F:GI:J:KL:MNO:PQ:R:S:TVw:W:XYy")) != -1) {
1.31 markus 672: switch (opt) {
1.91 jakob 673: case '1':
1.454 djm 674: fatal("SSH protocol v.1 is no longer supported");
1.91 jakob 675: break;
1.47 markus 676: case '2':
1.454 djm 677: /* Ignored */
1.47 markus 678: break;
1.37 markus 679: case '4':
1.196 djm 680: options.address_family = AF_INET;
1.37 markus 681: break;
682: case '6':
1.196 djm 683: options.address_family = AF_INET6;
1.37 markus 684: break;
1.31 markus 685: case 'n':
686: stdin_null_flag = 1;
687: break;
688: case 'f':
689: fork_after_authentication_flag = 1;
690: stdin_null_flag = 1;
691: break;
692: case 'x':
693: options.forward_x11 = 0;
694: break;
695: case 'X':
696: options.forward_x11 = 1;
697: break;
1.320 djm 698: case 'y':
699: use_syslog = 1;
700: break;
1.375 dtucker 701: case 'E':
1.422 dtucker 702: logfile = optarg;
1.375 dtucker 703: break;
1.408 djm 704: case 'G':
705: config_test = 1;
706: break;
1.202 markus 707: case 'Y':
708: options.forward_x11 = 1;
709: options.forward_x11_trusted = 1;
710: break;
1.31 markus 711: case 'g':
1.406 millert 712: options.fwd_opts.gateway_ports = 1;
1.31 markus 713: break;
1.229 djm 714: case 'O':
1.441 dtucker 715: if (options.stdio_forward_host != NULL)
1.332 djm 716: fatal("Cannot specify multiplexing "
717: "command with -W");
718: else if (muxclient_command != 0)
719: fatal("Multiplexing command already specified");
1.229 djm 720: if (strcmp(optarg, "check") == 0)
1.312 djm 721: muxclient_command = SSHMUX_COMMAND_ALIVE_CHECK;
1.338 markus 722: else if (strcmp(optarg, "forward") == 0)
723: muxclient_command = SSHMUX_COMMAND_FORWARD;
1.229 djm 724: else if (strcmp(optarg, "exit") == 0)
1.312 djm 725: muxclient_command = SSHMUX_COMMAND_TERMINATE;
1.357 djm 726: else if (strcmp(optarg, "stop") == 0)
727: muxclient_command = SSHMUX_COMMAND_STOP;
1.365 djm 728: else if (strcmp(optarg, "cancel") == 0)
729: muxclient_command = SSHMUX_COMMAND_CANCEL_FWD;
1.447 markus 730: else if (strcmp(optarg, "proxy") == 0)
731: muxclient_command = SSHMUX_COMMAND_PROXY;
1.229 djm 732: else
733: fatal("Invalid multiplex command.");
734: break;
1.183 stevesk 735: case 'P': /* deprecated */
1.376 djm 736: break;
1.394 deraadt 737: case 'Q':
1.376 djm 738: cp = NULL;
1.519 dtucker 739: if (strcmp(optarg, "cipher") == 0 ||
740: strcasecmp(optarg, "Ciphers") == 0)
1.393 djm 741: cp = cipher_alg_list('\n', 0);
1.394 deraadt 742: else if (strcmp(optarg, "cipher-auth") == 0)
1.393 djm 743: cp = cipher_alg_list('\n', 1);
1.519 dtucker 744: else if (strcmp(optarg, "mac") == 0 ||
745: strcasecmp(optarg, "MACs") == 0)
1.392 dtucker 746: cp = mac_alg_list('\n');
1.519 dtucker 747: else if (strcmp(optarg, "kex") == 0 ||
748: strcasecmp(optarg, "KexAlgorithms") == 0)
1.392 dtucker 749: cp = kex_alg_list('\n');
1.394 deraadt 750: else if (strcmp(optarg, "key") == 0)
1.451 djm 751: cp = sshkey_alg_list(0, 0, 0, '\n');
1.396 markus 752: else if (strcmp(optarg, "key-cert") == 0)
1.451 djm 753: cp = sshkey_alg_list(1, 0, 0, '\n');
1.396 markus 754: else if (strcmp(optarg, "key-plain") == 0)
1.451 djm 755: cp = sshkey_alg_list(0, 1, 0, '\n');
1.519 dtucker 756: else if (strcmp(optarg, "key-sig") == 0 ||
757: strcasecmp(optarg, "PubkeyAcceptedKeyTypes") == 0 ||
758: strcasecmp(optarg, "HostKeyAlgorithms") == 0 ||
759: strcasecmp(optarg, "HostbasedKeyTypes") == 0 ||
760: strcasecmp(optarg, "HostbasedAcceptedKeyTypes") == 0)
761: cp = sshkey_alg_list(0, 0, 1, '\n');
1.491 djm 762: else if (strcmp(optarg, "sig") == 0)
1.492 djm 763: cp = sshkey_alg_list(0, 1, 1, '\n');
1.491 djm 764: else if (strcmp(optarg, "protocol-version") == 0)
1.416 djm 765: cp = xstrdup("2");
1.513 dtucker 766: else if (strcmp(optarg, "compression") == 0) {
767: cp = xstrdup(compression_alg_list(0));
768: len = strlen(cp);
769: for (n = 0; n < len; n++)
770: if (cp[n] == ',')
771: cp[n] = '\n';
772: } else if (strcmp(optarg, "help") == 0) {
1.491 djm 773: cp = xstrdup(
1.513 dtucker 774: "cipher\ncipher-auth\ncompression\nkex\n"
1.519 dtucker 775: "key\nkey-cert\nkey-plain\nkey-sig\nmac\n"
1.491 djm 776: "protocol-version\nsig");
1.416 djm 777: }
1.376 djm 778: if (cp == NULL)
779: fatal("Unsupported query \"%s\"", optarg);
780: printf("%s\n", cp);
781: free(cp);
782: exit(0);
1.31 markus 783: break;
784: case 'a':
785: options.forward_agent = 0;
1.53 markus 786: break;
787: case 'A':
788: options.forward_agent = 1;
1.31 markus 789: break;
790: case 'k':
1.204 dtucker 791: options.gss_deleg_creds = 0;
1.297 djm 792: break;
793: case 'K':
794: options.gss_authentication = 1;
795: options.gss_deleg_creds = 1;
1.31 markus 796: break;
797: case 'i':
1.490 dtucker 798: p = tilde_expand_filename(optarg, getuid());
1.505 deraadt 799: if (stat(p, &st) == -1)
1.128 fgsch 800: fprintf(stderr, "Warning: Identity file %s "
1.429 dtucker 801: "not accessible: %s.\n", p,
1.231 otto 802: strerror(errno));
1.429 dtucker 803: else
804: add_identity_file(&options, NULL, p, 1);
805: free(p);
1.31 markus 806: break;
1.127 markus 807: case 'I':
1.333 markus 808: #ifdef ENABLE_PKCS11
1.422 dtucker 809: free(options.pkcs11_provider);
1.333 markus 810: options.pkcs11_provider = xstrdup(optarg);
1.137 jakob 811: #else
1.333 markus 812: fprintf(stderr, "no support for PKCS#11.\n");
1.137 jakob 813: #endif
1.127 markus 814: break;
1.443 djm 815: case 'J':
1.504 djm 816: if (options.jump_host != NULL) {
817: fatal("Only a single -J option is permitted "
818: "(use commas to separate multiple "
819: "jump hops)");
820: }
1.443 djm 821: if (options.proxy_command != NULL)
822: fatal("Cannot specify -J with ProxyCommand");
823: if (parse_jump(optarg, &options, 1) == -1)
824: fatal("Invalid -J argument");
825: options.proxy_command = xstrdup("none");
826: break;
1.31 markus 827: case 't':
1.359 djm 828: if (options.request_tty == REQUEST_TTY_YES)
829: options.request_tty = REQUEST_TTY_FORCE;
830: else
831: options.request_tty = REQUEST_TTY_YES;
1.31 markus 832: break;
833: case 'v':
1.197 markus 834: if (debug_flag == 0) {
1.66 markus 835: debug_flag = 1;
836: options.log_level = SYSLOG_LEVEL_DEBUG1;
1.197 markus 837: } else {
1.443 djm 838: if (options.log_level < SYSLOG_LEVEL_DEBUG3) {
839: debug_flag++;
1.197 markus 840: options.log_level++;
1.443 djm 841: }
1.197 markus 842: }
1.375 dtucker 843: break;
1.31 markus 844: case 'V':
1.209 markus 845: fprintf(stderr, "%s, %s\n",
1.402 markus 846: SSH_VERSION,
847: #ifdef WITH_OPENSSL
1.495 djm 848: OpenSSL_version(OPENSSL_VERSION)
1.402 markus 849: #else
850: "without OpenSSL"
851: #endif
852: );
1.31 markus 853: if (opt == 'V')
854: exit(0);
855: break;
1.255 reyk 856: case 'w':
1.256 reyk 857: if (options.tun_open == -1)
858: options.tun_open = SSH_TUNMODE_DEFAULT;
1.255 reyk 859: options.tun_local = a2tun(optarg, &options.tun_remote);
1.256 reyk 860: if (options.tun_local == SSH_TUNID_ERR) {
1.316 djm 861: fprintf(stderr,
862: "Bad tun device '%s'\n", optarg);
1.257 dtucker 863: exit(255);
1.255 reyk 864: }
865: break;
1.331 dtucker 866: case 'W':
1.441 dtucker 867: if (options.stdio_forward_host != NULL)
1.332 djm 868: fatal("stdio forward already specified");
869: if (muxclient_command != 0)
870: fatal("Cannot specify stdio forward with -O");
1.331 dtucker 871: if (parse_forward(&fwd, optarg, 1, 0)) {
1.441 dtucker 872: options.stdio_forward_host = fwd.listen_host;
873: options.stdio_forward_port = fwd.listen_port;
1.378 djm 874: free(fwd.connect_host);
1.331 dtucker 875: } else {
876: fprintf(stderr,
877: "Bad stdio forwarding specification '%s'\n",
878: optarg);
879: exit(255);
880: }
1.359 djm 881: options.request_tty = REQUEST_TTY_NO;
1.331 dtucker 882: no_shell_flag = 1;
883: break;
1.31 markus 884: case 'q':
885: options.log_level = SYSLOG_LEVEL_QUIET;
886: break;
887: case 'e':
888: if (optarg[0] == '^' && optarg[2] == 0 &&
1.128 fgsch 889: (u_char) optarg[1] >= 64 &&
890: (u_char) optarg[1] < 128)
1.78 markus 891: options.escape_char = (u_char) optarg[1] & 31;
1.31 markus 892: else if (strlen(optarg) == 1)
1.78 markus 893: options.escape_char = (u_char) optarg[0];
1.31 markus 894: else if (strcmp(optarg, "none") == 0)
1.119 stevesk 895: options.escape_char = SSH_ESCAPECHAR_NONE;
1.31 markus 896: else {
1.128 fgsch 897: fprintf(stderr, "Bad escape character '%s'.\n",
898: optarg);
1.257 dtucker 899: exit(255);
1.31 markus 900: }
901: break;
902: case 'c':
1.506 naddy 903: if (!ciphers_valid(*optarg == '+' || *optarg == '^' ?
1.420 djm 904: optarg + 1 : optarg)) {
905: fprintf(stderr, "Unknown cipher type '%s'\n",
906: optarg);
907: exit(255);
1.95 markus 908: }
1.456 djm 909: free(options.ciphers);
910: options.ciphers = xstrdup(optarg);
1.95 markus 911: break;
912: case 'm':
1.422 dtucker 913: if (mac_valid(optarg)) {
914: free(options.macs);
1.95 markus 915: options.macs = xstrdup(optarg);
1.422 dtucker 916: } else {
1.128 fgsch 917: fprintf(stderr, "Unknown mac type '%s'\n",
918: optarg);
1.257 dtucker 919: exit(255);
1.31 markus 920: }
921: break;
1.214 djm 922: case 'M':
1.242 djm 923: if (options.control_master == SSHCTL_MASTER_YES)
924: options.control_master = SSHCTL_MASTER_ASK;
925: else
926: options.control_master = SSHCTL_MASTER_YES;
1.214 djm 927: break;
1.31 markus 928: case 'p':
1.465 millert 929: if (options.port == -1) {
930: options.port = a2port(optarg);
931: if (options.port <= 0) {
932: fprintf(stderr, "Bad port '%s'\n",
933: optarg);
934: exit(255);
935: }
1.109 markus 936: }
1.31 markus 937: break;
938: case 'l':
1.465 millert 939: if (options.user == NULL)
940: options.user = optarg;
1.31 markus 941: break;
1.141 stevesk 942:
943: case 'L':
1.324 djm 944: if (parse_forward(&fwd, optarg, 0, 0))
1.232 djm 945: add_local_forward(&options, &fwd);
946: else {
1.128 fgsch 947: fprintf(stderr,
1.232 djm 948: "Bad local forwarding specification '%s'\n",
1.128 fgsch 949: optarg);
1.257 dtucker 950: exit(255);
1.31 markus 951: }
1.232 djm 952: break;
953:
954: case 'R':
1.464 markus 955: if (parse_forward(&fwd, optarg, 0, 1) ||
956: parse_forward(&fwd, optarg, 1, 1)) {
1.232 djm 957: add_remote_forward(&options, &fwd);
958: } else {
1.128 fgsch 959: fprintf(stderr,
1.232 djm 960: "Bad remote forwarding specification "
961: "'%s'\n", optarg);
1.257 dtucker 962: exit(255);
1.31 markus 963: }
964: break;
1.108 markus 965:
966: case 'D':
1.324 djm 967: if (parse_forward(&fwd, optarg, 1, 0)) {
1.322 stevesk 968: add_local_forward(&options, &fwd);
1.232 djm 969: } else {
1.322 stevesk 970: fprintf(stderr,
971: "Bad dynamic forwarding specification "
972: "'%s'\n", optarg);
1.257 dtucker 973: exit(255);
1.109 markus 974: }
1.108 markus 975: break;
976:
1.31 markus 977: case 'C':
1.513 dtucker 978: #ifdef WITH_ZLIB
1.31 markus 979: options.compression = 1;
1.513 dtucker 980: #else
981: error("Compression not supported, disabling.");
982: #endif
1.31 markus 983: break;
1.45 markus 984: case 'N':
985: no_shell_flag = 1;
1.359 djm 986: options.request_tty = REQUEST_TTY_NO;
1.45 markus 987: break;
988: case 'T':
1.359 djm 989: options.request_tty = REQUEST_TTY_NO;
1.45 markus 990: break;
1.31 markus 991: case 'o':
1.205 markus 992: line = xstrdup(optarg);
1.408 djm 993: if (process_config_line(&options, pw,
994: host ? host : "", host ? host : "", line,
995: "command-line", 0, NULL, SSHCONF_USERCONF) != 0)
1.257 dtucker 996: exit(255);
1.378 djm 997: free(line);
1.31 markus 998: break;
1.85 djm 999: case 's':
1000: subsystem_flag = 1;
1.117 markus 1001: break;
1.214 djm 1002: case 'S':
1.431 mmcc 1003: free(options.control_path);
1.214 djm 1004: options.control_path = xstrdup(optarg);
1005: break;
1.117 markus 1006: case 'b':
1007: options.bind_address = optarg;
1.474 djm 1008: break;
1009: case 'B':
1010: options.bind_interface = optarg;
1.85 djm 1011: break;
1.139 markus 1012: case 'F':
1013: config = optarg;
1014: break;
1.31 markus 1015: default:
1016: usage();
1.1 deraadt 1017: }
1.31 markus 1018: }
1019:
1.462 djm 1020: if (optind > 1 && strcmp(av[optind - 1], "--") == 0)
1021: opt_terminated = 1;
1022:
1.128 fgsch 1023: ac -= optind;
1024: av += optind;
1025:
1.329 guenther 1026: if (ac > 0 && !host) {
1.465 millert 1027: int tport;
1028: char *tuser;
1029: switch (parse_ssh_uri(*av, &tuser, &host, &tport)) {
1030: case -1:
1031: usage();
1032: break;
1033: case 0:
1034: if (options.user == NULL) {
1035: options.user = tuser;
1036: tuser = NULL;
1037: }
1038: free(tuser);
1039: if (options.port == -1 && tport != -1)
1040: options.port = tport;
1041: break;
1042: default:
1.128 fgsch 1043: p = xstrdup(*av);
1.188 markus 1044: cp = strrchr(p, '@');
1.465 millert 1045: if (cp != NULL) {
1046: if (cp == p)
1047: usage();
1048: if (options.user == NULL) {
1049: options.user = p;
1050: p = NULL;
1051: }
1052: *cp++ = '\0';
1053: host = xstrdup(cp);
1054: free(p);
1055: } else
1056: host = p;
1057: break;
1058: }
1.462 djm 1059: if (ac > 1 && !opt_terminated) {
1.189 millert 1060: optind = optreset = 1;
1.128 fgsch 1061: goto again;
1062: }
1.189 millert 1063: ac--, av++;
1.128 fgsch 1064: }
1065:
1.31 markus 1066: /* Check that we got a host name. */
1067: if (!host)
1068: usage();
1069:
1.385 djm 1070: host_arg = xstrdup(host);
1071:
1.402 markus 1072: #ifdef WITH_OPENSSL
1.350 djm 1073: OpenSSL_add_all_algorithms();
1.72 markus 1074: ERR_load_crypto_strings();
1.402 markus 1075: #endif
1.31 markus 1076:
1077: /* Initialize the command to execute on remote host. */
1.482 markus 1078: if ((command = sshbuf_new()) == NULL)
1079: fatal("sshbuf_new failed");
1.1 deraadt 1080:
1.33 markus 1081: /*
1082: * Save the command to execute on the remote host in a buffer. There
1083: * is no limit on the length of the command, except by the maximum
1084: * packet size. Also sets the tty flag if there is no command.
1085: */
1.128 fgsch 1086: if (!ac) {
1.31 markus 1087: /* No command specified - execute shell on a tty. */
1.85 djm 1088: if (subsystem_flag) {
1.128 fgsch 1089: fprintf(stderr,
1090: "You must specify a subsystem to invoke.\n");
1.85 djm 1091: usage();
1092: }
1.31 markus 1093: } else {
1.128 fgsch 1094: /* A command has been specified. Store it into the buffer. */
1095: for (i = 0; i < ac; i++) {
1.482 markus 1096: if ((r = sshbuf_putf(command, "%s%s",
1097: i ? " " : "", av[i])) != 0)
1098: fatal("%s: buffer error: %s",
1099: __func__, ssh_err(r));
1.31 markus 1100: }
1101: }
1102:
1.101 markus 1103: /*
1104: * Initialize "log" output. Since we are the client all output
1.375 dtucker 1105: * goes to stderr unless otherwise specified by -y or -E.
1.101 markus 1106: */
1.375 dtucker 1107: if (use_syslog && logfile != NULL)
1108: fatal("Can't specify both -y and -E");
1.422 dtucker 1109: if (logfile != NULL)
1.375 dtucker 1110: log_redirect_stderr_to(logfile);
1.325 markus 1111: log_init(argv0,
1.468 djm 1112: options.log_level == SYSLOG_LEVEL_NOT_SET ?
1.452 dtucker 1113: SYSLOG_LEVEL_INFO : options.log_level,
1.468 djm 1114: options.log_facility == SYSLOG_FACILITY_NOT_SET ?
1.452 dtucker 1115: SYSLOG_FACILITY_USER : options.log_facility,
1116: !use_syslog);
1.375 dtucker 1117:
1118: if (debug_flag)
1.402 markus 1119: logit("%s, %s", SSH_VERSION,
1120: #ifdef WITH_OPENSSL
1.495 djm 1121: OpenSSL_version(OPENSSL_VERSION)
1.402 markus 1122: #else
1123: "without OpenSSL"
1124: #endif
1125: );
1.31 markus 1126:
1.400 djm 1127: /* Parse the configuration files */
1.496 djm 1128: process_config_files(host_arg, pw, 0, &want_final_pass);
1129: if (want_final_pass)
1130: debug("configuration requests final Match pass");
1.400 djm 1131:
1132: /* Hostname canonicalisation needs a few options filled. */
1133: fill_default_options_for_canonicalization(&options);
1134:
1135: /* If the user has replaced the hostname then take it into use now */
1136: if (options.hostname != NULL) {
1137: /* NB. Please keep in sync with readconf.c:match_cfg_line() */
1138: cp = percent_expand(options.hostname,
1139: "h", host, (char *)NULL);
1140: free(host);
1141: host = cp;
1.408 djm 1142: free(options.hostname);
1143: options.hostname = xstrdup(host);
1.400 djm 1144: }
1145:
1.470 djm 1146: /* Don't lowercase addresses, they will be explicitly canonicalised */
1147: if ((was_addr = is_addr(host)) == 0)
1148: lowercase(host);
1149:
1150: /*
1151: * Try to canonicalize if requested by configuration or the
1152: * hostname is an address.
1153: */
1154: if (options.canonicalize_hostname != SSH_CANONICALISE_NO || was_addr)
1.400 djm 1155: addrs = resolve_canonicalize(&host, options.port);
1156:
1.139 markus 1157: /*
1.401 djm 1158: * If CanonicalizePermittedCNAMEs have been specified but
1159: * other canonicalization did not happen (by not being requested
1160: * or by failing with fallback) then the hostname may still be changed
1.468 djm 1161: * as a result of CNAME following.
1.401 djm 1162: *
1163: * Try to resolve the bare hostname name using the system resolver's
1164: * usual search rules and then apply the CNAME follow rules.
1165: *
1166: * Skip the lookup if a ProxyCommand is being used unless the user
1167: * has specifically requested canonicalisation for this case via
1168: * CanonicalizeHostname=always
1.139 markus 1169: */
1.443 djm 1170: direct = option_clear_or_none(options.proxy_command) &&
1171: options.jump_host == NULL;
1172: if (addrs == NULL && options.num_permitted_cnames != 0 && (direct ||
1173: options.canonicalize_hostname == SSH_CANONICALISE_ALWAYS)) {
1.403 djm 1174: if ((addrs = resolve_host(host, options.port,
1.493 djm 1175: direct, cname, sizeof(cname))) == NULL) {
1.403 djm 1176: /* Don't fatal proxied host names not in the DNS */
1.493 djm 1177: if (direct)
1.403 djm 1178: cleanup_exit(255); /* logged in resolve_host */
1179: } else
1.443 djm 1180: check_follow_cname(direct, &host, cname);
1.400 djm 1181: }
1.139 markus 1182:
1.400 djm 1183: /*
1.408 djm 1184: * If canonicalisation is enabled then re-parse the configuration
1185: * files as new stanzas may match.
1.400 djm 1186: */
1.496 djm 1187: if (options.canonicalize_hostname != 0 && !want_final_pass) {
1188: debug("hostname canonicalisation enabled, "
1189: "will re-parse configuration");
1190: want_final_pass = 1;
1191: }
1192:
1193: if (want_final_pass) {
1194: debug("re-parsing configuration");
1.408 djm 1195: free(options.hostname);
1196: options.hostname = xstrdup(host);
1.496 djm 1197: process_config_files(host_arg, pw, 1, NULL);
1.408 djm 1198: /*
1199: * Address resolution happens early with canonicalisation
1200: * enabled and the port number may have changed since, so
1201: * reset it in address list
1202: */
1203: if (addrs != NULL && options.port > 0)
1204: set_addrinfo_port(addrs, options.port);
1.139 markus 1205: }
1.31 markus 1206:
1207: /* Fill configuration defaults. */
1208: fill_default_options(&options);
1.443 djm 1209:
1210: /*
1211: * If ProxyJump option specified, then construct a ProxyCommand now.
1212: */
1213: if (options.jump_host != NULL) {
1214: char port_s[8];
1.478 djm 1215: const char *sshbin = argv0;
1.520 dtucker 1216: int port = options.port, jumpport = options.jump_port;
1217:
1218: if (port <= 0)
1219: port = default_ssh_port();
1220: if (jumpport <= 0)
1221: jumpport = default_ssh_port();
1222: if (strcmp(options.jump_host, host) == 0 && port == jumpport)
1223: fatal("jumphost loop via %s", options.jump_host);
1.478 djm 1224:
1225: /*
1226: * Try to use SSH indicated by argv[0], but fall back to
1227: * "ssh" if it appears unavailable.
1228: */
1229: if (strchr(argv0, '/') != NULL && access(argv0, X_OK) != 0)
1230: sshbin = "ssh";
1.443 djm 1231:
1232: /* Consistency check */
1233: if (options.proxy_command != NULL)
1234: fatal("inconsistent options: ProxyCommand+ProxyJump");
1235: /* Never use FD passing for ProxyJump */
1236: options.proxy_use_fdpass = 0;
1237: snprintf(port_s, sizeof(port_s), "%d", options.jump_port);
1238: xasprintf(&options.proxy_command,
1.478 djm 1239: "%s%s%s%s%s%s%s%s%s%s%.*s -W '[%%h]:%%p' %s",
1240: sshbin,
1.443 djm 1241: /* Optional "-l user" argument if jump_user set */
1242: options.jump_user == NULL ? "" : " -l ",
1243: options.jump_user == NULL ? "" : options.jump_user,
1244: /* Optional "-p port" argument if jump_port set */
1245: options.jump_port <= 0 ? "" : " -p ",
1246: options.jump_port <= 0 ? "" : port_s,
1247: /* Optional additional jump hosts ",..." */
1248: options.jump_extra == NULL ? "" : " -J ",
1249: options.jump_extra == NULL ? "" : options.jump_extra,
1250: /* Optional "-F" argumment if -F specified */
1251: config == NULL ? "" : " -F ",
1252: config == NULL ? "" : config,
1253: /* Optional "-v" arguments if -v set */
1254: debug_flag ? " -" : "",
1255: debug_flag, "vvv",
1256: /* Mandatory hostname */
1257: options.jump_host);
1258: debug("Setting implicit ProxyCommand from ProxyJump: %s",
1259: options.proxy_command);
1260: }
1.31 markus 1261:
1.400 djm 1262: if (options.port == 0)
1263: options.port = default_ssh_port();
1.463 djm 1264: channel_set_af(ssh, options.address_family);
1.196 djm 1265:
1.383 djm 1266: /* Tidy and check options */
1267: if (options.host_key_alias != NULL)
1268: lowercase(options.host_key_alias);
1269: if (options.proxy_command != NULL &&
1270: strcmp(options.proxy_command, "-") == 0 &&
1271: options.proxy_use_fdpass)
1272: fatal("ProxyCommand=- and ProxyUseFDPass are incompatible");
1.516 markus 1273: if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK) {
1274: if (options.control_persist && options.control_path != NULL) {
1275: debug("UpdateHostKeys=ask is incompatible with "
1276: "ControlPersist; disabling");
1277: options.update_hostkeys = 0;
1278: } else if (sshbuf_len(command) != 0 ||
1279: options.remote_command != NULL ||
1280: options.request_tty == REQUEST_TTY_NO) {
1281: debug("UpdateHostKeys=ask is incompatible with "
1282: "remote command execution; disabling");
1.517 djm 1283: options.update_hostkeys = 0;
1284: } else if (options.log_level < SYSLOG_LEVEL_INFO) {
1285: /* no point logging anything; user won't see it */
1.516 markus 1286: options.update_hostkeys = 0;
1287: }
1.415 djm 1288: }
1.430 djm 1289: if (options.connection_attempts <= 0)
1290: fatal("Invalid number of ConnectionAttempts");
1291:
1.482 markus 1292: if (sshbuf_len(command) != 0 && options.remote_command != NULL)
1.461 bluhm 1293: fatal("Cannot execute command-line and remote command.");
1294:
1295: /* Cannot fork to background if no command. */
1.482 markus 1296: if (fork_after_authentication_flag && sshbuf_len(command) == 0 &&
1.461 bluhm 1297: options.remote_command == NULL && !no_shell_flag)
1298: fatal("Cannot fork into background without a command "
1299: "to execute.");
1300:
1.31 markus 1301: /* reinit */
1.452 dtucker 1302: log_init(argv0, options.log_level, options.log_facility, !use_syslog);
1.370 djm 1303:
1304: if (options.request_tty == REQUEST_TTY_YES ||
1305: options.request_tty == REQUEST_TTY_FORCE)
1306: tty_flag = 1;
1307:
1308: /* Allocate a tty by default if no command specified. */
1.482 markus 1309: if (sshbuf_len(command) == 0 && options.remote_command == NULL)
1.370 djm 1310: tty_flag = options.request_tty != REQUEST_TTY_NO;
1311:
1312: /* Force no tty */
1.447 markus 1313: if (options.request_tty == REQUEST_TTY_NO ||
1314: (muxclient_command && muxclient_command != SSHMUX_COMMAND_PROXY))
1.370 djm 1315: tty_flag = 0;
1316: /* Do not allocate a tty if stdin is not a tty. */
1317: if ((!isatty(fileno(stdin)) || stdin_null_flag) &&
1318: options.request_tty != REQUEST_TTY_FORCE) {
1319: if (tty_flag)
1320: logit("Pseudo-terminal will not be allocated because "
1321: "stdin is not a terminal.");
1322: tty_flag = 0;
1323: }
1.31 markus 1324:
1325: if (options.user == NULL)
1326: options.user = xstrdup(pw->pw_name);
1.343 djm 1327:
1.466 djm 1328: /* Set up strings used to percent_expand() arguments */
1.358 djm 1329: if (gethostname(thishost, sizeof(thishost)) == -1)
1330: fatal("gethostname: %s", strerror(errno));
1331: strlcpy(shorthost, thishost, sizeof(shorthost));
1332: shorthost[strcspn(thishost, ".")] = '\0';
1333: snprintf(portstr, sizeof(portstr), "%d", options.port);
1.479 djm 1334: snprintf(uidstr, sizeof(uidstr), "%llu",
1335: (unsigned long long)pw->pw_uid);
1.358 djm 1336:
1.522 dtucker 1337: conn_hash_hex = ssh_connection_hash(thishost, host, portstr,
1338: options.user);
1.405 djm 1339:
1.466 djm 1340: /*
1341: * Expand tokens in arguments. NB. LocalCommand is expanded later,
1342: * after port-forwarding is set up, so it may pick up any local
1343: * tunnel interface name allocated.
1344: */
1.461 bluhm 1345: if (options.remote_command != NULL) {
1346: debug3("expanding RemoteCommand: %s", options.remote_command);
1347: cp = options.remote_command;
1.527 ! dtucker 1348: options.remote_command = default_client_percent_expand(cp,
! 1349: pw->pw_dir, host, options.user, pw->pw_name);
1.461 bluhm 1350: debug3("expanded RemoteCommand: %s", options.remote_command);
1351: free(cp);
1.482 markus 1352: if ((r = sshbuf_put(command, options.remote_command,
1353: strlen(options.remote_command))) != 0)
1354: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.304 dtucker 1355: }
1.31 markus 1356:
1.214 djm 1357: if (options.control_path != NULL) {
1.490 dtucker 1358: cp = tilde_expand_filename(options.control_path, getuid());
1.378 djm 1359: free(options.control_path);
1.527 ! dtucker 1360: options.control_path = default_client_percent_expand(cp,
! 1361: pw->pw_dir, host, options.user, pw->pw_name);
1.378 djm 1362: free(cp);
1.214 djm 1363: }
1.408 djm 1364:
1.522 dtucker 1365: if (options.identity_agent != NULL) {
1366: p = tilde_expand_filename(options.identity_agent, getuid());
1.527 ! dtucker 1367: cp = default_client_percent_expand(p,
! 1368: pw->pw_dir, host, options.user, pw->pw_name);
1.522 dtucker 1369: free(p);
1370: free(options.identity_agent);
1371: options.identity_agent = cp;
1372: }
1373:
1374: if (options.forward_agent_sock_path != NULL) {
1375: p = tilde_expand_filename(options.forward_agent_sock_path,
1376: getuid());
1.527 ! dtucker 1377: cp = default_client_percent_expand(p,
! 1378: pw->pw_dir, host, options.user, pw->pw_name);
1.522 dtucker 1379: free(p);
1380: free(options.forward_agent_sock_path);
1381: options.forward_agent_sock_path = cp;
1382: }
1383:
1.527 ! dtucker 1384: for (i = 0; i < options.num_local_forwards; i++) {
! 1385: if (options.local_forwards[i].listen_path != NULL) {
! 1386: cp = options.local_forwards[i].listen_path;
! 1387: p = options.local_forwards[i].listen_path =
! 1388: default_client_percent_expand(cp,
! 1389: pw->pw_dir, host, options.user, pw->pw_name);
! 1390: if (strcmp(cp, p) != 0)
! 1391: debug3("expanded LocalForward listen path "
! 1392: "'%s' -> '%s'", cp, p);
! 1393: free(cp);
! 1394: }
! 1395: if (options.local_forwards[i].connect_path != NULL) {
! 1396: cp = options.local_forwards[i].connect_path;
! 1397: p = options.local_forwards[i].connect_path =
! 1398: default_client_percent_expand(cp,
! 1399: pw->pw_dir, host, options.user, pw->pw_name);
! 1400: if (strcmp(cp, p) != 0)
! 1401: debug3("expanded LocalForward connect path "
! 1402: "'%s' -> '%s'", cp, p);
! 1403: free(cp);
! 1404: }
! 1405: }
! 1406:
! 1407: for (i = 0; i < options.num_remote_forwards; i++) {
! 1408: if (options.remote_forwards[i].listen_path != NULL) {
! 1409: cp = options.remote_forwards[i].listen_path;
! 1410: p = options.remote_forwards[i].listen_path =
! 1411: default_client_percent_expand(cp,
! 1412: pw->pw_dir, host, options.user, pw->pw_name);
! 1413: if (strcmp(cp, p) != 0)
! 1414: debug3("expanded RemoteForward listen path "
! 1415: "'%s' -> '%s'", cp, p);
! 1416: free(cp);
! 1417: }
! 1418: if (options.remote_forwards[i].connect_path != NULL) {
! 1419: cp = options.remote_forwards[i].connect_path;
! 1420: p = options.remote_forwards[i].connect_path =
! 1421: default_client_percent_expand(cp,
! 1422: pw->pw_dir, host, options.user, pw->pw_name);
! 1423: if (strcmp(cp, p) != 0)
! 1424: debug3("expanded RemoteForward connect path "
! 1425: "'%s' -> '%s'", cp, p);
! 1426: free(cp);
! 1427: }
! 1428: }
! 1429:
1.408 djm 1430: if (config_test) {
1431: dump_client_config(&options, host);
1432: exit(0);
1.508 djm 1433: }
1434:
1435: /* Expand SecurityKeyProvider if it refers to an environment variable */
1436: if (options.sk_provider != NULL && *options.sk_provider == '$' &&
1437: strlen(options.sk_provider) > 1) {
1438: if ((cp = getenv(options.sk_provider + 1)) == NULL) {
1.518 naddy 1439: debug("Authenticator provider %s did not resolve; "
1.508 djm 1440: "disabling", options.sk_provider);
1441: free(options.sk_provider);
1442: options.sk_provider = NULL;
1443: } else {
1444: debug2("resolved SecurityKeyProvider %s => %s",
1445: options.sk_provider, cp);
1446: free(options.sk_provider);
1447: options.sk_provider = xstrdup(cp);
1448: }
1.408 djm 1449: }
1.405 djm 1450:
1.312 djm 1451: if (muxclient_command != 0 && options.control_path == NULL)
1.240 djm 1452: fatal("No ControlPath specified for \"-O\" command");
1.447 markus 1453: if (options.control_path != NULL) {
1454: int sock;
1455: if ((sock = muxclient(options.control_path)) >= 0) {
1.463 djm 1456: ssh_packet_set_connection(ssh, sock, sock);
1.499 djm 1457: ssh_packet_set_mux(ssh);
1.447 markus 1458: goto skip_connect;
1459: }
1460: }
1.401 djm 1461:
1462: /*
1463: * If hostname canonicalisation was not enabled, then we may not
1464: * have yet resolved the hostname. Do so now.
1465: */
1466: if (addrs == NULL && options.proxy_command == NULL) {
1.421 djm 1467: debug2("resolving \"%s\" port %d", host, options.port);
1.401 djm 1468: if ((addrs = resolve_host(host, options.port, 1,
1469: cname, sizeof(cname))) == NULL)
1470: cleanup_exit(255); /* resolve_host logs the error */
1471: }
1.214 djm 1472:
1.303 djm 1473: timeout_ms = options.connection_timeout * 1000;
1474:
1.77 markus 1475: /* Open a connection to the remote host. */
1.511 beck 1476: if (ssh_connect(ssh, host, host_arg, addrs, &hostaddr, options.port,
1.385 djm 1477: options.address_family, options.connection_attempts,
1.488 dtucker 1478: &timeout_ms, options.tcp_keep_alive) != 0)
1.257 dtucker 1479: exit(255);
1.31 markus 1480:
1.391 djm 1481: if (addrs != NULL)
1482: freeaddrinfo(addrs);
1483:
1.499 djm 1484: ssh_packet_set_timeout(ssh, options.server_alive_interval,
1.385 djm 1485: options.server_alive_count_max);
1486:
1.303 djm 1487: if (timeout_ms > 0)
1488: debug3("timeout: %d ms remain after connect", timeout_ms);
1489:
1.33 markus 1490: /*
1.485 dtucker 1491: * If we successfully made the connection and we have hostbased auth
1492: * enabled, load the public keys so we can later use the ssh-keysign
1493: * helper to sign challenges.
1.33 markus 1494: */
1.112 markus 1495: sensitive_data.nkeys = 0;
1496: sensitive_data.keys = NULL;
1.457 djm 1497: if (options.hostbased_authentication) {
1.486 dtucker 1498: sensitive_data.nkeys = 10;
1.274 deraadt 1499: sensitive_data.keys = xcalloc(sensitive_data.nkeys,
1.483 markus 1500: sizeof(struct sshkey));
1501:
1502: /* XXX check errors? */
1.486 dtucker 1503: #define L_PUBKEY(p,o) do { \
1504: if ((o) >= sensitive_data.nkeys) \
1505: fatal("%s pubkey out of array bounds", __func__); \
1.483 markus 1506: check_load(sshkey_load_public(p, &(sensitive_data.keys[o]), NULL), \
1.486 dtucker 1507: p, "pubkey"); \
1508: } while (0)
1509: #define L_CERT(p,o) do { \
1510: if ((o) >= sensitive_data.nkeys) \
1511: fatal("%s cert out of array bounds", __func__); \
1512: check_load(sshkey_load_cert(p, &(sensitive_data.keys[o])), p, "cert"); \
1513: } while (0)
1.177 markus 1514:
1.485 dtucker 1515: if (options.hostbased_authentication == 1) {
1.486 dtucker 1516: L_CERT(_PATH_HOST_ECDSA_KEY_FILE, 0);
1517: L_CERT(_PATH_HOST_ED25519_KEY_FILE, 1);
1518: L_CERT(_PATH_HOST_RSA_KEY_FILE, 2);
1519: L_CERT(_PATH_HOST_DSA_KEY_FILE, 3);
1520: L_PUBKEY(_PATH_HOST_ECDSA_KEY_FILE, 4);
1521: L_PUBKEY(_PATH_HOST_ED25519_KEY_FILE, 5);
1522: L_PUBKEY(_PATH_HOST_RSA_KEY_FILE, 6);
1523: L_PUBKEY(_PATH_HOST_DSA_KEY_FILE, 7);
1524: L_CERT(_PATH_HOST_XMSS_KEY_FILE, 8);
1525: L_PUBKEY(_PATH_HOST_XMSS_KEY_FILE, 9);
1.173 markus 1526: }
1.31 markus 1527: }
1528:
1.487 dtucker 1529: /* Create ~/.ssh * directory if it doesn't already exist. */
1.367 djm 1530: if (config == NULL) {
1531: r = snprintf(buf, sizeof buf, "%s%s%s", pw->pw_dir,
1532: strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
1.505 deraadt 1533: if (r > 0 && (size_t)r < sizeof(buf) && stat(buf, &st) == -1)
1534: if (mkdir(buf, 0700) == -1)
1.367 djm 1535: error("Could not create directory '%.200s'.",
1536: buf);
1537: }
1.31 markus 1538:
1.104 markus 1539: /* load options.identity_files */
1.466 djm 1540: load_public_identity_files(pw);
1.439 markus 1541:
1.476 djm 1542: /* optionally set the SSH_AUTHSOCKET_ENV_NAME variable */
1.440 markus 1543: if (options.identity_agent &&
1544: strcmp(options.identity_agent, SSH_AUTHSOCKET_ENV_NAME) != 0) {
1.439 markus 1545: if (strcmp(options.identity_agent, "none") == 0) {
1546: unsetenv(SSH_AUTHSOCKET_ENV_NAME);
1547: } else {
1.522 dtucker 1548: cp = options.identity_agent;
1.494 djm 1549: if (cp[0] == '$') {
1550: if (!valid_env_name(cp + 1)) {
1551: fatal("Invalid IdentityAgent "
1552: "environment variable name %s", cp);
1553: }
1554: if ((p = getenv(cp + 1)) == NULL)
1555: unsetenv(SSH_AUTHSOCKET_ENV_NAME);
1556: else
1557: setenv(SSH_AUTHSOCKET_ENV_NAME, p, 1);
1558: } else {
1559: /* identity_agent specifies a path directly */
1560: setenv(SSH_AUTHSOCKET_ENV_NAME, cp, 1);
1561: }
1.510 djm 1562: }
1563: }
1564:
1.522 dtucker 1565: if (options.forward_agent && options.forward_agent_sock_path != NULL) {
1.526 djm 1566: cp = options.forward_agent_sock_path;
1.510 djm 1567: if (cp[0] == '$') {
1568: if (!valid_env_name(cp + 1)) {
1569: fatal("Invalid ForwardAgent environment variable name %s", cp);
1570: }
1571: if ((p = getenv(cp + 1)) != NULL)
1572: forward_agent_sock_path = p;
1573: else
1574: options.forward_agent = 0;
1575: free(cp);
1576: } else {
1577: forward_agent_sock_path = cp;
1.439 markus 1578: }
1579: }
1.104 markus 1580:
1581: /* Expand ~ in known host file names. */
1.361 djm 1582: tilde_expand_paths(options.system_hostfiles,
1583: options.num_system_hostfiles);
1584: tilde_expand_paths(options.user_hostfiles, options.num_user_hostfiles);
1.149 markus 1585:
1.512 dtucker 1586: ssh_signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
1587: ssh_signal(SIGCHLD, main_sigchld_handler);
1.31 markus 1588:
1.316 djm 1589: /* Log into the remote system. Never returns if the login fails. */
1.497 djm 1590: ssh_login(ssh, &sensitive_data, host, (struct sockaddr *)&hostaddr,
1.355 djm 1591: options.port, pw, timeout_ms);
1.339 djm 1592:
1.499 djm 1593: if (ssh_packet_connection_is_on_socket(ssh)) {
1.339 djm 1594: verbose("Authenticated to %s ([%s]:%d).", host,
1.437 djm 1595: ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
1.339 djm 1596: } else {
1597: verbose("Authenticated to %s (via proxy).", host);
1598: }
1.31 markus 1599:
1.112 markus 1600: /* We no longer need the private host keys. Clear them now. */
1601: if (sensitive_data.nkeys != 0) {
1602: for (i = 0; i < sensitive_data.nkeys; i++) {
1603: if (sensitive_data.keys[i] != NULL) {
1604: /* Destroys contents safely */
1605: debug3("clear hostkey %d", i);
1.483 markus 1606: sshkey_free(sensitive_data.keys[i]);
1.112 markus 1607: sensitive_data.keys[i] = NULL;
1608: }
1609: }
1.378 djm 1610: free(sensitive_data.keys);
1.134 markus 1611: }
1612: for (i = 0; i < options.num_identity_files; i++) {
1.378 djm 1613: free(options.identity_files[i]);
1614: options.identity_files[i] = NULL;
1.134 markus 1615: if (options.identity_keys[i]) {
1.483 markus 1616: sshkey_free(options.identity_keys[i]);
1.134 markus 1617: options.identity_keys[i] = NULL;
1618: }
1.112 markus 1619: }
1.426 djm 1620: for (i = 0; i < options.num_certificate_files; i++) {
1621: free(options.certificate_files[i]);
1622: options.certificate_files[i] = NULL;
1623: }
1.31 markus 1624:
1.447 markus 1625: skip_connect:
1.466 djm 1626: exit_status = ssh_session2(ssh, pw);
1.499 djm 1627: ssh_packet_close(ssh);
1.186 djm 1628:
1.312 djm 1629: if (options.control_path != NULL && muxserver_sock != -1)
1.214 djm 1630: unlink(options.control_path);
1631:
1.353 djm 1632: /* Kill ProxyCommand if it is running. */
1633: ssh_kill_proxy_command();
1.186 djm 1634:
1.45 markus 1635: return exit_status;
1636: }
1637:
1.344 djm 1638: static void
1639: control_persist_detach(void)
1640: {
1641: pid_t pid;
1.438 djm 1642: int devnull, keep_stderr;
1.344 djm 1643:
1644: debug("%s: backgrounding master process", __func__);
1645:
1.473 djm 1646: /*
1647: * master (current process) into the background, and make the
1648: * foreground process a client of the backgrounded master.
1649: */
1.344 djm 1650: switch ((pid = fork())) {
1651: case -1:
1652: fatal("%s: fork: %s", __func__, strerror(errno));
1653: case 0:
1654: /* Child: master process continues mainloop */
1.473 djm 1655: break;
1656: default:
1.344 djm 1657: /* Parent: set up mux slave to connect to backgrounded master */
1658: debug2("%s: background process is %ld", __func__, (long)pid);
1659: stdin_null_flag = ostdin_null_flag;
1.359 djm 1660: options.request_tty = orequest_tty;
1.344 djm 1661: tty_flag = otty_flag;
1.473 djm 1662: close(muxserver_sock);
1663: muxserver_sock = -1;
1.351 markus 1664: options.control_master = SSHCTL_MASTER_NO;
1.473 djm 1665: muxclient(options.control_path);
1.344 djm 1666: /* muxclient() doesn't return on success. */
1.473 djm 1667: fatal("Failed to connect to new control master");
1668: }
1.346 djm 1669: if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
1670: error("%s: open(\"/dev/null\"): %s", __func__,
1671: strerror(errno));
1672: } else {
1.438 djm 1673: keep_stderr = log_is_on_stderr() && debug_flag;
1.346 djm 1674: if (dup2(devnull, STDIN_FILENO) == -1 ||
1.438 djm 1675: dup2(devnull, STDOUT_FILENO) == -1 ||
1676: (!keep_stderr && dup2(devnull, STDERR_FILENO) == -1))
1.346 djm 1677: error("%s: dup2: %s", __func__, strerror(errno));
1678: if (devnull > STDERR_FILENO)
1679: close(devnull);
1680: }
1.381 djm 1681: daemon(1, 1);
1.362 djm 1682: setproctitle("%s [mux]", options.control_path);
1.344 djm 1683: }
1684:
1685: /* Do fork() after authentication. Used by "ssh -f" */
1686: static void
1687: fork_postauth(void)
1688: {
1689: if (need_controlpersist_detach)
1690: control_persist_detach();
1691: debug("forking to background");
1692: fork_after_authentication_flag = 0;
1.505 deraadt 1693: if (daemon(1, 1) == -1)
1.344 djm 1694: fatal("daemon() failed: %.200s", strerror(errno));
1695: }
1696:
1.523 djm 1697: static void
1698: forwarding_success(void)
1699: {
1.524 djm 1700: if (forward_confirms_pending == -1)
1701: return;
1702: if (--forward_confirms_pending == 0) {
1.525 djm 1703: debug("%s: all expected forwarding replies received", __func__);
1.523 djm 1704: if (fork_after_authentication_flag)
1705: fork_postauth();
1.524 djm 1706: } else {
1707: debug2("%s: %d expected forwarding replies remaining",
1708: __func__, forward_confirms_pending);
1.523 djm 1709: }
1710: }
1711:
1.315 djm 1712: /* Callback for remote forward global requests */
1713: static void
1.463 djm 1714: ssh_confirm_remote_forward(struct ssh *ssh, int type, u_int32_t seq, void *ctxt)
1.315 djm 1715: {
1.406 millert 1716: struct Forward *rfwd = (struct Forward *)ctxt;
1.499 djm 1717: u_int port;
1718: int r;
1.315 djm 1719:
1.324 djm 1720: /* XXX verbose() on failure? */
1.404 markus 1721: debug("remote forward %s for: listen %s%s%d, connect %s:%d",
1.315 djm 1722: type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
1.406 millert 1723: rfwd->listen_path ? rfwd->listen_path :
1724: rfwd->listen_host ? rfwd->listen_host : "",
1725: (rfwd->listen_path || rfwd->listen_host) ? ":" : "",
1726: rfwd->listen_port, rfwd->connect_path ? rfwd->connect_path :
1727: rfwd->connect_host, rfwd->connect_port);
1728: if (rfwd->listen_path == NULL && rfwd->listen_port == 0) {
1.366 markus 1729: if (type == SSH2_MSG_REQUEST_SUCCESS) {
1.499 djm 1730: if ((r = sshpkt_get_u32(ssh, &port)) != 0)
1731: fatal("%s: %s", __func__, ssh_err(r));
1732: if (port > 65535) {
1733: error("Invalid allocated port %u for remote "
1734: "forward to %s:%d", port,
1735: rfwd->connect_host, rfwd->connect_port);
1736: /* Ensure failure processing runs below */
1737: type = SSH2_MSG_REQUEST_FAILURE;
1738: channel_update_permission(ssh,
1739: rfwd->handle, -1);
1740: } else {
1741: rfwd->allocated_port = (int)port;
1742: logit("Allocated port %u for remote "
1743: "forward to %s:%d",
1744: rfwd->allocated_port, rfwd->connect_host,
1745: rfwd->connect_port);
1746: channel_update_permission(ssh,
1747: rfwd->handle, rfwd->allocated_port);
1748: }
1.366 markus 1749: } else {
1.480 djm 1750: channel_update_permission(ssh, rfwd->handle, -1);
1.366 markus 1751: }
1.324 djm 1752: }
1.468 djm 1753:
1.315 djm 1754: if (type == SSH2_MSG_REQUEST_FAILURE) {
1.406 millert 1755: if (options.exit_on_forward_failure) {
1756: if (rfwd->listen_path != NULL)
1757: fatal("Error: remote port forwarding failed "
1758: "for listen path %s", rfwd->listen_path);
1759: else
1760: fatal("Error: remote port forwarding failed "
1761: "for listen port %d", rfwd->listen_port);
1762: } else {
1763: if (rfwd->listen_path != NULL)
1764: logit("Warning: remote port forwarding failed "
1765: "for listen path %s", rfwd->listen_path);
1766: else
1767: logit("Warning: remote port forwarding failed "
1768: "for listen port %d", rfwd->listen_port);
1769: }
1.315 djm 1770: }
1.523 djm 1771: forwarding_success();
1.315 djm 1772: }
1773:
1.126 itojun 1774: static void
1.463 djm 1775: client_cleanup_stdio_fwd(struct ssh *ssh, int id, void *arg)
1.331 dtucker 1776: {
1777: debug("stdio forwarding: done");
1778: cleanup_exit(0);
1779: }
1780:
1.368 djm 1781: static void
1.463 djm 1782: ssh_stdio_confirm(struct ssh *ssh, int id, int success, void *arg)
1.407 djm 1783: {
1784: if (!success)
1785: fatal("stdio forwarding failed");
1786: }
1787:
1788: static void
1.523 djm 1789: ssh_tun_confirm(struct ssh *ssh, int id, int success, void *arg)
1790: {
1791: if (!success) {
1792: error("Tunnel forwarding failed");
1793: if (options.exit_on_forward_failure)
1794: cleanup_exit(255);
1795: }
1796:
1797: debug("%s: tunnel forward established, id=%d", __func__, id);
1798: forwarding_success();
1799: }
1800:
1801: static void
1.463 djm 1802: ssh_init_stdio_forwarding(struct ssh *ssh)
1.331 dtucker 1803: {
1804: Channel *c;
1.332 djm 1805: int in, out;
1.331 dtucker 1806:
1.441 dtucker 1807: if (options.stdio_forward_host == NULL)
1.368 djm 1808: return;
1809:
1.441 dtucker 1810: debug3("%s: %s:%d", __func__, options.stdio_forward_host,
1811: options.stdio_forward_port);
1.332 djm 1812:
1.505 deraadt 1813: if ((in = dup(STDIN_FILENO)) == -1 ||
1814: (out = dup(STDOUT_FILENO)) == -1)
1.332 djm 1815: fatal("channel_connect_stdio_fwd: dup() in/out failed");
1.463 djm 1816: if ((c = channel_connect_stdio_fwd(ssh, options.stdio_forward_host,
1.441 dtucker 1817: options.stdio_forward_port, in, out)) == NULL)
1.368 djm 1818: fatal("%s: channel_connect_stdio_fwd failed", __func__);
1.463 djm 1819: channel_register_cleanup(ssh, c->self, client_cleanup_stdio_fwd, 0);
1820: channel_register_open_confirm(ssh, c->self, ssh_stdio_confirm, NULL);
1.331 dtucker 1821: }
1822:
1823: static void
1.466 djm 1824: ssh_init_forwarding(struct ssh *ssh, char **ifname)
1.70 markus 1825: {
1.86 markus 1826: int success = 0;
1.70 markus 1827: int i;
1.331 dtucker 1828:
1.524 djm 1829: if (options.exit_on_forward_failure)
1830: forward_confirms_pending = 0; /* track pending requests */
1.70 markus 1831: /* Initiate local TCP/IP port forwardings. */
1832: for (i = 0; i < options.num_local_forwards; i++) {
1.232 djm 1833: debug("Local connections to %.200s:%d forwarded to remote "
1834: "address %.200s:%d",
1.406 millert 1835: (options.local_forwards[i].listen_path != NULL) ?
1836: options.local_forwards[i].listen_path :
1.234 deraadt 1837: (options.local_forwards[i].listen_host == NULL) ?
1.406 millert 1838: (options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") :
1.232 djm 1839: options.local_forwards[i].listen_host,
1840: options.local_forwards[i].listen_port,
1.406 millert 1841: (options.local_forwards[i].connect_path != NULL) ?
1842: options.local_forwards[i].connect_path :
1.232 djm 1843: options.local_forwards[i].connect_host,
1844: options.local_forwards[i].connect_port);
1.463 djm 1845: success += channel_setup_local_fwd_listener(ssh,
1.406 millert 1846: &options.local_forwards[i], &options.fwd_opts);
1.70 markus 1847: }
1.283 markus 1848: if (i > 0 && success != i && options.exit_on_forward_failure)
1849: fatal("Could not request local forwarding.");
1.86 markus 1850: if (i > 0 && success == 0)
1851: error("Could not request local forwarding.");
1.70 markus 1852:
1853: /* Initiate remote TCP/IP port forwardings. */
1854: for (i = 0; i < options.num_remote_forwards; i++) {
1.232 djm 1855: debug("Remote connections from %.200s:%d forwarded to "
1856: "local address %.200s:%d",
1.406 millert 1857: (options.remote_forwards[i].listen_path != NULL) ?
1858: options.remote_forwards[i].listen_path :
1.248 djm 1859: (options.remote_forwards[i].listen_host == NULL) ?
1.253 djm 1860: "LOCALHOST" : options.remote_forwards[i].listen_host,
1.232 djm 1861: options.remote_forwards[i].listen_port,
1.406 millert 1862: (options.remote_forwards[i].connect_path != NULL) ?
1863: options.remote_forwards[i].connect_path :
1.232 djm 1864: options.remote_forwards[i].connect_host,
1865: options.remote_forwards[i].connect_port);
1.523 djm 1866: if ((options.remote_forwards[i].handle =
1.463 djm 1867: channel_request_remote_forwarding(ssh,
1.523 djm 1868: &options.remote_forwards[i])) >= 0) {
1.463 djm 1869: client_register_global_confirm(
1870: ssh_confirm_remote_forward,
1.366 markus 1871: &options.remote_forwards[i]);
1.523 djm 1872: forward_confirms_pending++;
1873: } else if (options.exit_on_forward_failure)
1874: fatal("Could not request remote forwarding.");
1875: else
1876: logit("Warning: Could not request remote forwarding.");
1.70 markus 1877: }
1.301 djm 1878:
1879: /* Initiate tunnel forwarding. */
1880: if (options.tun_open != SSH_TUNMODE_NO) {
1.466 djm 1881: if ((*ifname = client_request_tun_fwd(ssh,
1882: options.tun_open, options.tun_local,
1.523 djm 1883: options.tun_remote, ssh_tun_confirm, NULL)) != NULL)
1884: forward_confirms_pending++;
1885: else if (options.exit_on_forward_failure)
1886: fatal("Could not request tunnel forwarding.");
1887: else
1888: error("Could not request tunnel forwarding.");
1.524 djm 1889: }
1890: if (forward_confirms_pending > 0) {
1891: debug("%s: expecting replies for %d forwards", __func__,
1892: forward_confirms_pending);
1.468 djm 1893: }
1.70 markus 1894: }
1895:
1.126 itojun 1896: static void
1.70 markus 1897: check_agent_present(void)
1898: {
1.412 djm 1899: int r;
1900:
1.70 markus 1901: if (options.forward_agent) {
1.254 djm 1902: /* Clear agent forwarding if we don't have an agent. */
1.412 djm 1903: if ((r = ssh_get_authentication_socket(NULL)) != 0) {
1.70 markus 1904: options.forward_agent = 0;
1.412 djm 1905: if (r != SSH_ERR_AGENT_NOT_PRESENT)
1906: debug("ssh_get_authentication_socket: %s",
1907: ssh_err(r));
1908: }
1.70 markus 1909: }
1910: }
1911:
1.214 djm 1912: static void
1.463 djm 1913: ssh_session2_setup(struct ssh *ssh, int id, int success, void *arg)
1.214 djm 1914: {
1.215 djm 1915: extern char **environ;
1.243 djm 1916: const char *display;
1.499 djm 1917: int r, interactive = tty_flag;
1.433 djm 1918: char *proto = NULL, *data = NULL;
1.337 djm 1919:
1920: if (!success)
1921: return; /* No need for error message, channels code sens one */
1.215 djm 1922:
1.248 djm 1923: display = getenv("DISPLAY");
1.417 djm 1924: if (display == NULL && options.forward_x11)
1925: debug("X11 forwarding requested but DISPLAY not set");
1.463 djm 1926: if (options.forward_x11 && client_x11_get_proto(ssh, display,
1.433 djm 1927: options.xauth_location, options.forward_x11_trusted,
1928: options.forward_x11_timeout, &proto, &data) == 0) {
1.50 markus 1929: /* Request forwarding with authentication spoofing. */
1.316 djm 1930: debug("Requesting X11 forwarding with authentication "
1931: "spoofing.");
1.463 djm 1932: x11_request_forwarding_with_spoofing(ssh, id, display, proto,
1.363 djm 1933: data, 1);
1.463 djm 1934: client_expect_confirm(ssh, id, "X11 forwarding", CONFIRM_WARN);
1.363 djm 1935: /* XXX exit_on_forward_failure */
1.80 markus 1936: interactive = 1;
1.50 markus 1937: }
1938:
1.70 markus 1939: check_agent_present();
1940: if (options.forward_agent) {
1941: debug("Requesting authentication agent forwarding.");
1.463 djm 1942: channel_request_start(ssh, id, "auth-agent-req@openssh.com", 0);
1.499 djm 1943: if ((r = sshpkt_send(ssh)) != 0)
1944: fatal("%s: %s", __func__, ssh_err(r));
1.212 djm 1945: }
1.369 dtucker 1946:
1947: /* Tell the packet module whether this is an interactive session. */
1.499 djm 1948: ssh_packet_set_interactive(ssh, interactive,
1.369 dtucker 1949: options.ip_qos_interactive, options.ip_qos_bulk);
1.212 djm 1950:
1.463 djm 1951: client_session2_setup(ssh, id, tty_flag, subsystem_flag, getenv("TERM"),
1.482 markus 1952: NULL, fileno(stdin), command, environ);
1.45 markus 1953: }
1954:
1.143 markus 1955: /* open new channel for a session */
1.126 itojun 1956: static int
1.463 djm 1957: ssh_session2_open(struct ssh *ssh)
1.45 markus 1958: {
1.118 markus 1959: Channel *c;
1960: int window, packetmax, in, out, err;
1.60 markus 1961:
1.62 markus 1962: if (stdin_null_flag) {
1.93 itojun 1963: in = open(_PATH_DEVNULL, O_RDONLY);
1.62 markus 1964: } else {
1965: in = dup(STDIN_FILENO);
1966: }
1.60 markus 1967: out = dup(STDOUT_FILENO);
1968: err = dup(STDERR_FILENO);
1.45 markus 1969:
1.505 deraadt 1970: if (in == -1 || out == -1 || err == -1)
1.62 markus 1971: fatal("dup() in/out/err failed");
1.45 markus 1972:
1.69 markus 1973: /* enable nonblocking unless tty */
1974: if (!isatty(in))
1975: set_nonblock(in);
1976: if (!isatty(out))
1977: set_nonblock(out);
1978: if (!isatty(err))
1979: set_nonblock(err);
1980:
1.65 markus 1981: window = CHAN_SES_WINDOW_DEFAULT;
1982: packetmax = CHAN_SES_PACKET_DEFAULT;
1.164 markus 1983: if (tty_flag) {
1984: window >>= 1;
1985: packetmax >>= 1;
1.45 markus 1986: }
1.463 djm 1987: c = channel_new(ssh,
1.45 markus 1988: "session", SSH_CHANNEL_OPENING, in, out, err,
1.65 markus 1989: window, packetmax, CHAN_EXTENDED_WRITE,
1.192 markus 1990: "client-session", /*nonblock*/0);
1.45 markus 1991:
1.463 djm 1992: debug3("%s: channel_new: %d", __func__, c->self);
1.106 markus 1993:
1.463 djm 1994: channel_send_open(ssh, c->self);
1.143 markus 1995: if (!no_shell_flag)
1.463 djm 1996: channel_register_open_confirm(ssh, c->self,
1.310 djm 1997: ssh_session2_setup, NULL);
1.106 markus 1998:
1.118 markus 1999: return c->self;
1.106 markus 2000: }
2001:
1.126 itojun 2002: static int
1.466 djm 2003: ssh_session2(struct ssh *ssh, struct passwd *pw)
1.106 markus 2004: {
1.499 djm 2005: int r, devnull, id = -1;
1.466 djm 2006: char *cp, *tun_fwd_ifname = NULL;
1.106 markus 2007:
2008: /* XXX should be pre-session */
1.368 djm 2009: if (!options.control_persist)
1.463 djm 2010: ssh_init_stdio_forwarding(ssh);
1.466 djm 2011:
2012: ssh_init_forwarding(ssh, &tun_fwd_ifname);
2013:
2014: if (options.local_command != NULL) {
2015: debug3("expanding LocalCommand: %s", options.local_command);
2016: cp = options.local_command;
2017: options.local_command = percent_expand(cp,
1.522 dtucker 2018: DEFAULT_CLIENT_PERCENT_EXPAND_ARGS,
1.466 djm 2019: "d", pw->pw_dir,
2020: "h", host,
2021: "r", options.user,
2022: "u", pw->pw_name,
2023: "T", tun_fwd_ifname == NULL ? "NONE" : tun_fwd_ifname,
2024: (char *)NULL);
2025: debug3("expanded LocalCommand: %s", options.local_command);
2026: free(cp);
2027: }
1.106 markus 2028:
1.344 djm 2029: /* Start listening for multiplex clients */
1.499 djm 2030: if (!ssh_packet_get_mux(ssh))
1.463 djm 2031: muxserver_listen(ssh);
1.344 djm 2032:
1.473 djm 2033: /*
1.368 djm 2034: * If we are in control persist mode and have a working mux listen
2035: * socket, then prepare to background ourselves and have a foreground
2036: * client attach as a control slave.
2037: * NB. we must save copies of the flags that we override for
1.344 djm 2038: * the backgrounding, since we defer attachment of the slave until
2039: * after the connection is fully established (in particular,
2040: * async rfwd replies have been received for ExitOnForwardFailure).
2041: */
1.473 djm 2042: if (options.control_persist && muxserver_sock != -1) {
1.344 djm 2043: ostdin_null_flag = stdin_null_flag;
2044: ono_shell_flag = no_shell_flag;
1.359 djm 2045: orequest_tty = options.request_tty;
1.344 djm 2046: otty_flag = tty_flag;
1.473 djm 2047: stdin_null_flag = 1;
2048: no_shell_flag = 1;
2049: tty_flag = 0;
1.344 djm 2050: if (!fork_after_authentication_flag)
2051: need_controlpersist_detach = 1;
2052: fork_after_authentication_flag = 1;
1.473 djm 2053: }
1.368 djm 2054: /*
2055: * ControlPersist mux listen socket setup failed, attempt the
2056: * stdio forward setup that we skipped earlier.
2057: */
2058: if (options.control_persist && muxserver_sock == -1)
1.463 djm 2059: ssh_init_stdio_forwarding(ssh);
1.344 djm 2060:
1.471 djm 2061: if (!no_shell_flag)
1.463 djm 2062: id = ssh_session2_open(ssh);
1.379 djm 2063: else {
1.499 djm 2064: ssh_packet_set_interactive(ssh,
1.379 djm 2065: options.control_master == SSHCTL_MASTER_NO,
2066: options.ip_qos_interactive, options.ip_qos_bulk);
2067: }
1.314 djm 2068:
2069: /* If we don't expect to open a new session, then disallow it */
1.319 markus 2070: if (options.control_master == SSHCTL_MASTER_NO &&
2071: (datafellows & SSH_NEW_OPENSSH)) {
1.314 djm 2072: debug("Requesting no-more-sessions@openssh.com");
1.499 djm 2073: if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
2074: (r = sshpkt_put_cstring(ssh,
2075: "no-more-sessions@openssh.com")) != 0 ||
2076: (r = sshpkt_put_u8(ssh, 0)) != 0 ||
2077: (r = sshpkt_send(ssh)) != 0)
2078: fatal("%s: %s", __func__, ssh_err(r));
1.314 djm 2079: }
1.255 reyk 2080:
2081: /* Execute a local command */
2082: if (options.local_command != NULL &&
2083: options.permit_local_command)
2084: ssh_local_cmd(options.local_command);
1.467 djm 2085:
2086: /*
2087: * stdout is now owned by the session channel; clobber it here
2088: * so future channel closes are propagated to the local fd.
2089: * NB. this can only happen after LocalCommand has completed,
2090: * as it may want to write to stdout.
2091: */
1.469 djm 2092: if (!need_controlpersist_detach) {
2093: if ((devnull = open(_PATH_DEVNULL, O_WRONLY)) == -1)
2094: error("%s: open %s: %s", __func__,
2095: _PATH_DEVNULL, strerror(errno));
1.505 deraadt 2096: if (dup2(devnull, STDOUT_FILENO) == -1)
1.469 djm 2097: fatal("%s: dup2() stdout failed", __func__);
2098: if (devnull > STDERR_FILENO)
2099: close(devnull);
2100: }
1.301 djm 2101:
1.342 djm 2102: /*
2103: * If requested and we are not interested in replies to remote
2104: * forwarding requests, then let ssh continue in the background.
2105: */
1.344 djm 2106: if (fork_after_authentication_flag) {
2107: if (options.exit_on_forward_failure &&
2108: options.num_remote_forwards > 0) {
2109: debug("deferring postauth fork until remote forward "
2110: "confirmation received");
2111: } else
2112: fork_postauth();
1.318 djm 2113: }
1.31 markus 2114:
1.463 djm 2115: return client_loop(ssh, tty_flag, tty_flag ?
1.119 stevesk 2116: options.escape_char : SSH_ESCAPECHAR_NONE, id);
1.72 markus 2117: }
2118:
1.426 djm 2119: /* Loads all IdentityFile and CertificateFile keys */
1.126 itojun 2120: static void
1.466 djm 2121: load_public_identity_files(struct passwd *pw)
1.104 markus 2122: {
1.466 djm 2123: char *filename, *cp;
1.460 markus 2124: struct sshkey *public;
1.426 djm 2125: int i;
2126: u_int n_ids, n_certs;
1.335 djm 2127: char *identity_files[SSH_MAX_IDENTITY_FILES];
1.460 markus 2128: struct sshkey *identity_keys[SSH_MAX_IDENTITY_FILES];
1.484 djm 2129: int identity_file_userprovided[SSH_MAX_IDENTITY_FILES];
1.426 djm 2130: char *certificate_files[SSH_MAX_CERTIFICATE_FILES];
2131: struct sshkey *certificates[SSH_MAX_CERTIFICATE_FILES];
1.484 djm 2132: int certificate_file_userprovided[SSH_MAX_CERTIFICATE_FILES];
1.333 markus 2133: #ifdef ENABLE_PKCS11
1.514 djm 2134: struct sshkey **keys = NULL;
2135: char **comments = NULL;
1.333 markus 2136: int nkeys;
1.335 djm 2137: #endif /* PKCS11 */
1.104 markus 2138:
1.426 djm 2139: n_ids = n_certs = 0;
1.398 tedu 2140: memset(identity_files, 0, sizeof(identity_files));
2141: memset(identity_keys, 0, sizeof(identity_keys));
1.484 djm 2142: memset(identity_file_userprovided, 0,
2143: sizeof(identity_file_userprovided));
1.426 djm 2144: memset(certificate_files, 0, sizeof(certificate_files));
2145: memset(certificates, 0, sizeof(certificates));
1.484 djm 2146: memset(certificate_file_userprovided, 0,
2147: sizeof(certificate_file_userprovided));
1.335 djm 2148:
2149: #ifdef ENABLE_PKCS11
1.333 markus 2150: if (options.pkcs11_provider != NULL &&
1.167 markus 2151: options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1.333 markus 2152: (pkcs11_init(!options.batch_mode) == 0) &&
2153: (nkeys = pkcs11_add_provider(options.pkcs11_provider, NULL,
1.514 djm 2154: &keys, &comments)) > 0) {
1.333 markus 2155: for (i = 0; i < nkeys; i++) {
1.335 djm 2156: if (n_ids >= SSH_MAX_IDENTITY_FILES) {
1.483 markus 2157: sshkey_free(keys[i]);
1.514 djm 2158: free(comments[i]);
1.335 djm 2159: continue;
2160: }
2161: identity_keys[n_ids] = keys[i];
1.514 djm 2162: identity_files[n_ids] = comments[i]; /* transferred */
1.335 djm 2163: n_ids++;
1.167 markus 2164: }
1.378 djm 2165: free(keys);
1.514 djm 2166: free(comments);
1.127 markus 2167: }
1.333 markus 2168: #endif /* ENABLE_PKCS11 */
1.335 djm 2169: for (i = 0; i < options.num_identity_files; i++) {
1.373 djm 2170: if (n_ids >= SSH_MAX_IDENTITY_FILES ||
2171: strcasecmp(options.identity_files[i], "none") == 0) {
1.378 djm 2172: free(options.identity_files[i]);
1.426 djm 2173: options.identity_files[i] = NULL;
1.335 djm 2174: continue;
2175: }
1.490 dtucker 2176: cp = tilde_expand_filename(options.identity_files[i], getuid());
1.527 ! dtucker 2177: filename = default_client_percent_expand(cp,
! 2178: pw->pw_dir, host, options.user, pw->pw_name);
1.378 djm 2179: free(cp);
1.483 markus 2180: check_load(sshkey_load_public(filename, &public, NULL),
2181: filename, "pubkey");
1.131 millert 2182: debug("identity file %s type %d", filename,
2183: public ? public->type : -1);
1.378 djm 2184: free(options.identity_files[i]);
1.335 djm 2185: identity_files[n_ids] = filename;
2186: identity_keys[n_ids] = public;
1.484 djm 2187: identity_file_userprovided[n_ids] =
2188: options.identity_file_userprovided[i];
1.335 djm 2189: if (++n_ids >= SSH_MAX_IDENTITY_FILES)
2190: continue;
2191:
1.426 djm 2192: /*
2193: * If no certificates have been explicitly listed then try
2194: * to add the default certificate variant too.
2195: */
2196: if (options.num_certificate_files != 0)
2197: continue;
1.335 djm 2198: xasprintf(&cp, "%s-cert", filename);
1.483 markus 2199: check_load(sshkey_load_public(cp, &public, NULL),
2200: filename, "pubkey");
1.335 djm 2201: debug("identity file %s type %d", cp,
2202: public ? public->type : -1);
2203: if (public == NULL) {
1.378 djm 2204: free(cp);
1.335 djm 2205: continue;
2206: }
1.483 markus 2207: if (!sshkey_is_cert(public)) {
1.335 djm 2208: debug("%s: key %s type %s is not a certificate",
1.483 markus 2209: __func__, cp, sshkey_type(public));
2210: sshkey_free(public);
1.378 djm 2211: free(cp);
1.335 djm 2212: continue;
2213: }
1.448 djm 2214: /* NB. leave filename pointing to private key */
2215: identity_files[n_ids] = xstrdup(filename);
1.335 djm 2216: identity_keys[n_ids] = public;
1.484 djm 2217: identity_file_userprovided[n_ids] =
2218: options.identity_file_userprovided[i];
1.335 djm 2219: n_ids++;
2220: }
1.426 djm 2221:
2222: if (options.num_certificate_files > SSH_MAX_CERTIFICATE_FILES)
2223: fatal("%s: too many certificates", __func__);
2224: for (i = 0; i < options.num_certificate_files; i++) {
2225: cp = tilde_expand_filename(options.certificate_files[i],
1.490 dtucker 2226: getuid());
1.527 ! dtucker 2227: filename = default_client_percent_expand(cp,
! 2228: pw->pw_dir, host, options.user, pw->pw_name);
1.426 djm 2229: free(cp);
2230:
1.483 markus 2231: check_load(sshkey_load_public(filename, &public, NULL),
2232: filename, "certificate");
1.426 djm 2233: debug("certificate file %s type %d", filename,
2234: public ? public->type : -1);
2235: free(options.certificate_files[i]);
2236: options.certificate_files[i] = NULL;
2237: if (public == NULL) {
2238: free(filename);
2239: continue;
2240: }
1.483 markus 2241: if (!sshkey_is_cert(public)) {
1.426 djm 2242: debug("%s: key %s type %s is not a certificate",
1.483 markus 2243: __func__, filename, sshkey_type(public));
2244: sshkey_free(public);
1.426 djm 2245: free(filename);
2246: continue;
2247: }
2248: certificate_files[n_certs] = filename;
2249: certificates[n_certs] = public;
1.484 djm 2250: certificate_file_userprovided[n_certs] =
2251: options.certificate_file_userprovided[i];
1.426 djm 2252: ++n_certs;
2253: }
2254:
1.335 djm 2255: options.num_identity_files = n_ids;
2256: memcpy(options.identity_files, identity_files, sizeof(identity_files));
2257: memcpy(options.identity_keys, identity_keys, sizeof(identity_keys));
1.484 djm 2258: memcpy(options.identity_file_userprovided,
2259: identity_file_userprovided, sizeof(identity_file_userprovided));
1.426 djm 2260:
2261: options.num_certificate_files = n_certs;
2262: memcpy(options.certificate_files,
2263: certificate_files, sizeof(certificate_files));
2264: memcpy(options.certificates, certificates, sizeof(certificates));
1.484 djm 2265: memcpy(options.certificate_file_userprovided,
2266: certificate_file_userprovided,
2267: sizeof(certificate_file_userprovided));
1.214 djm 2268: }
1.352 djm 2269:
2270: static void
2271: main_sigchld_handler(int sig)
2272: {
2273: int save_errno = errno;
2274: pid_t pid;
2275: int status;
2276:
2277: while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
1.505 deraadt 2278: (pid == -1 && errno == EINTR))
1.352 djm 2279: ;
2280: errno = save_errno;
2281: }