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