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