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