Annotation of src/usr.bin/ssh/ssh.c, Revision 1.525
1.525 ! djm 1: /* $OpenBSD: ssh.c,v 1.524 2020/04/03 04:03:51 djm Exp $ */
1.1 deraadt 2: /*
1.32 deraadt 3: * Author: Tatu Ylonen <ylo@cs.hut.fi>
4: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5: * All rights reserved
6: * Ssh client program. This program can be used to log into a remote machine.
7: * The software supports strong authentication, encryption, and forwarding
8: * of X11, TCP/IP, and authentication connections.
9: *
1.64 deraadt 10: * As far as I am concerned, the code I have written for this software
11: * can be used freely for any purpose. Any derived versions of this
12: * software must be clearly marked as such, and if the derived work is
13: * incompatible with the protocol description in the RFC file, it must be
14: * called by a name other than "ssh" or "Secure Shell".
15: *
16: * Copyright (c) 1999 Niels Provos. All rights reserved.
1.202 markus 17: * Copyright (c) 2000, 2001, 2002, 2003 Markus Friedl. All rights reserved.
1.64 deraadt 18: *
19: * Modified to work with SSL by Niels Provos <provos@citi.umich.edu>
20: * in Canada (German citizen).
21: *
22: * Redistribution and use in source and binary forms, with or without
23: * modification, are permitted provided that the following conditions
24: * are met:
25: * 1. Redistributions of source code must retain the above copyright
26: * notice, this list of conditions and the following disclaimer.
27: * 2. Redistributions in binary form must reproduce the above copyright
28: * notice, this list of conditions and the following disclaimer in the
29: * documentation and/or other materials provided with the distribution.
30: *
31: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
35: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.32 deraadt 41: */
1.1 deraadt 42:
1.293 deraadt 43: #include <sys/types.h>
1.312 djm 44: #include <sys/ioctl.h>
45: #include <sys/queue.h>
1.259 stevesk 46: #include <sys/resource.h>
1.280 stevesk 47: #include <sys/socket.h>
1.264 stevesk 48: #include <sys/stat.h>
1.312 djm 49: #include <sys/time.h>
1.352 djm 50: #include <sys/wait.h>
1.258 stevesk 51:
1.265 stevesk 52: #include <ctype.h>
1.285 stevesk 53: #include <errno.h>
1.281 stevesk 54: #include <fcntl.h>
1.286 stevesk 55: #include <netdb.h>
1.258 stevesk 56: #include <paths.h>
1.279 stevesk 57: #include <pwd.h>
1.263 stevesk 58: #include <signal.h>
1.287 stevesk 59: #include <stddef.h>
1.291 stevesk 60: #include <stdio.h>
1.290 stevesk 61: #include <stdlib.h>
1.289 stevesk 62: #include <string.h>
1.509 naddy 63: #include <stdarg.h>
1.288 stevesk 64: #include <unistd.h>
1.414 deraadt 65: #include <limits.h>
1.445 djm 66: #include <locale.h>
1.49 markus 67:
1.402 markus 68: #ifdef WITH_OPENSSL
1.49 markus 69: #include <openssl/evp.h>
1.72 markus 70: #include <openssl/err.h>
1.402 markus 71: #endif
1.1 deraadt 72:
1.293 deraadt 73: #include "xmalloc.h"
1.84 markus 74: #include "ssh.h"
75: #include "ssh2.h"
1.341 djm 76: #include "canohost.h"
1.84 markus 77: #include "compat.h"
78: #include "cipher.h"
1.1 deraadt 79: #include "packet.h"
1.482 markus 80: #include "sshbuf.h"
1.123 markus 81: #include "channels.h"
1.483 markus 82: #include "sshkey.h"
1.58 markus 83: #include "authfd.h"
1.49 markus 84: #include "authfile.h"
1.83 markus 85: #include "pathnames.h"
1.214 djm 86: #include "dispatch.h"
1.81 markus 87: #include "clientloop.h"
1.84 markus 88: #include "log.h"
1.406 millert 89: #include "misc.h"
1.84 markus 90: #include "readconf.h"
91: #include "sshconnect.h"
1.95 markus 92: #include "kex.h"
93: #include "mac.h"
1.213 deraadt 94: #include "sshpty.h"
1.212 djm 95: #include "match.h"
1.214 djm 96: #include "msg.h"
1.278 stevesk 97: #include "version.h"
1.412 djm 98: #include "ssherr.h"
1.420 djm 99: #include "myproposal.h"
1.49 markus 100:
1.333 markus 101: #ifdef ENABLE_PKCS11
102: #include "ssh-pkcs11.h"
1.137 jakob 103: #endif
1.498 djm 104:
1.49 markus 105: extern char *__progname;
1.1 deraadt 106:
1.316 djm 107: /* Flag indicating whether debug mode is on. May be set on the command line. */
1.1 deraadt 108: int debug_flag = 0;
109:
1.359 djm 110: /* Flag indicating whether a tty should be requested */
1.1 deraadt 111: int tty_flag = 0;
112:
1.45 markus 113: /* don't exec a shell */
114: int no_shell_flag = 0;
115:
1.33 markus 116: /*
117: * Flag indicating that nothing should be read from stdin. This can be set
118: * on the command line.
119: */
1.1 deraadt 120: int stdin_null_flag = 0;
121:
1.33 markus 122: /*
1.344 djm 123: * Flag indicating that the current process should be backgrounded and
124: * a new slave launched in the foreground for ControlPersist.
125: */
126: int need_controlpersist_detach = 0;
127:
128: /* Copies of flags for ControlPersist foreground slave */
1.359 djm 129: int ostdin_null_flag, ono_shell_flag, otty_flag, orequest_tty;
1.344 djm 130:
131: /*
1.33 markus 132: * Flag indicating that ssh should fork after authentication. This is useful
1.172 deraadt 133: * so that the passphrase can be entered manually, and then ssh goes to the
1.33 markus 134: * background.
135: */
1.1 deraadt 136: int fork_after_authentication_flag = 0;
137:
1.33 markus 138: /*
139: * General data structure for command line options and options configurable
140: * in configuration files. See readconf.h.
141: */
1.1 deraadt 142: Options options;
143:
1.139 markus 144: /* optional user configfile */
145: char *config = NULL;
146:
1.33 markus 147: /*
148: * Name of the host we are connecting to. This is the name given on the
1.503 jmc 149: * command line, or the Hostname specified for the user-supplied name in a
1.33 markus 150: * configuration file.
151: */
1.1 deraadt 152: char *host;
153:
1.510 djm 154: /*
155: * A config can specify a path to forward, overriding SSH_AUTH_SOCK. If this is
156: * not NULL, forward the socket at this path instead.
157: */
158: char *forward_agent_sock_path = NULL;
159:
1.466 djm 160: /* Various strings used to to percent_expand() arguments */
161: static char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
162: static char uidstr[32], *host_arg, *conn_hash_hex;
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.523 djm 184: static int forward_confirms_pending = -1;
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.523 djm 1649: static void
1650: forwarding_success(void)
1651: {
1.524 djm 1652: if (forward_confirms_pending == -1)
1653: return;
1654: if (--forward_confirms_pending == 0) {
1.525 ! djm 1655: debug("%s: all expected forwarding replies received", __func__);
1.523 djm 1656: if (fork_after_authentication_flag)
1657: fork_postauth();
1.524 djm 1658: } else {
1659: debug2("%s: %d expected forwarding replies remaining",
1660: __func__, forward_confirms_pending);
1.523 djm 1661: }
1662: }
1663:
1.315 djm 1664: /* Callback for remote forward global requests */
1665: static void
1.463 djm 1666: ssh_confirm_remote_forward(struct ssh *ssh, int type, u_int32_t seq, void *ctxt)
1.315 djm 1667: {
1.406 millert 1668: struct Forward *rfwd = (struct Forward *)ctxt;
1.499 djm 1669: u_int port;
1670: int r;
1.315 djm 1671:
1.324 djm 1672: /* XXX verbose() on failure? */
1.404 markus 1673: debug("remote forward %s for: listen %s%s%d, connect %s:%d",
1.315 djm 1674: type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
1.406 millert 1675: rfwd->listen_path ? rfwd->listen_path :
1676: rfwd->listen_host ? rfwd->listen_host : "",
1677: (rfwd->listen_path || rfwd->listen_host) ? ":" : "",
1678: rfwd->listen_port, rfwd->connect_path ? rfwd->connect_path :
1679: rfwd->connect_host, rfwd->connect_port);
1680: if (rfwd->listen_path == NULL && rfwd->listen_port == 0) {
1.366 markus 1681: if (type == SSH2_MSG_REQUEST_SUCCESS) {
1.499 djm 1682: if ((r = sshpkt_get_u32(ssh, &port)) != 0)
1683: fatal("%s: %s", __func__, ssh_err(r));
1684: if (port > 65535) {
1685: error("Invalid allocated port %u for remote "
1686: "forward to %s:%d", port,
1687: rfwd->connect_host, rfwd->connect_port);
1688: /* Ensure failure processing runs below */
1689: type = SSH2_MSG_REQUEST_FAILURE;
1690: channel_update_permission(ssh,
1691: rfwd->handle, -1);
1692: } else {
1693: rfwd->allocated_port = (int)port;
1694: logit("Allocated port %u for remote "
1695: "forward to %s:%d",
1696: rfwd->allocated_port, rfwd->connect_host,
1697: rfwd->connect_port);
1698: channel_update_permission(ssh,
1699: rfwd->handle, rfwd->allocated_port);
1700: }
1.366 markus 1701: } else {
1.480 djm 1702: channel_update_permission(ssh, rfwd->handle, -1);
1.366 markus 1703: }
1.324 djm 1704: }
1.468 djm 1705:
1.315 djm 1706: if (type == SSH2_MSG_REQUEST_FAILURE) {
1.406 millert 1707: if (options.exit_on_forward_failure) {
1708: if (rfwd->listen_path != NULL)
1709: fatal("Error: remote port forwarding failed "
1710: "for listen path %s", rfwd->listen_path);
1711: else
1712: fatal("Error: remote port forwarding failed "
1713: "for listen port %d", rfwd->listen_port);
1714: } else {
1715: if (rfwd->listen_path != NULL)
1716: logit("Warning: remote port forwarding failed "
1717: "for listen path %s", rfwd->listen_path);
1718: else
1719: logit("Warning: remote port forwarding failed "
1720: "for listen port %d", rfwd->listen_port);
1721: }
1.315 djm 1722: }
1.523 djm 1723: forwarding_success();
1.315 djm 1724: }
1725:
1.126 itojun 1726: static void
1.463 djm 1727: client_cleanup_stdio_fwd(struct ssh *ssh, int id, void *arg)
1.331 dtucker 1728: {
1729: debug("stdio forwarding: done");
1730: cleanup_exit(0);
1731: }
1732:
1.368 djm 1733: static void
1.463 djm 1734: ssh_stdio_confirm(struct ssh *ssh, int id, int success, void *arg)
1.407 djm 1735: {
1736: if (!success)
1737: fatal("stdio forwarding failed");
1738: }
1739:
1740: static void
1.523 djm 1741: ssh_tun_confirm(struct ssh *ssh, int id, int success, void *arg)
1742: {
1743: if (!success) {
1744: error("Tunnel forwarding failed");
1745: if (options.exit_on_forward_failure)
1746: cleanup_exit(255);
1747: }
1748:
1749: debug("%s: tunnel forward established, id=%d", __func__, id);
1750: forwarding_success();
1751: }
1752:
1753: static void
1.463 djm 1754: ssh_init_stdio_forwarding(struct ssh *ssh)
1.331 dtucker 1755: {
1756: Channel *c;
1.332 djm 1757: int in, out;
1.331 dtucker 1758:
1.441 dtucker 1759: if (options.stdio_forward_host == NULL)
1.368 djm 1760: return;
1761:
1.441 dtucker 1762: debug3("%s: %s:%d", __func__, options.stdio_forward_host,
1763: options.stdio_forward_port);
1.332 djm 1764:
1.505 deraadt 1765: if ((in = dup(STDIN_FILENO)) == -1 ||
1766: (out = dup(STDOUT_FILENO)) == -1)
1.332 djm 1767: fatal("channel_connect_stdio_fwd: dup() in/out failed");
1.463 djm 1768: if ((c = channel_connect_stdio_fwd(ssh, options.stdio_forward_host,
1.441 dtucker 1769: options.stdio_forward_port, in, out)) == NULL)
1.368 djm 1770: fatal("%s: channel_connect_stdio_fwd failed", __func__);
1.463 djm 1771: channel_register_cleanup(ssh, c->self, client_cleanup_stdio_fwd, 0);
1772: channel_register_open_confirm(ssh, c->self, ssh_stdio_confirm, NULL);
1.331 dtucker 1773: }
1774:
1775: static void
1.466 djm 1776: ssh_init_forwarding(struct ssh *ssh, char **ifname)
1.70 markus 1777: {
1.86 markus 1778: int success = 0;
1.70 markus 1779: int i;
1.331 dtucker 1780:
1.524 djm 1781: if (options.exit_on_forward_failure)
1782: forward_confirms_pending = 0; /* track pending requests */
1.70 markus 1783: /* Initiate local TCP/IP port forwardings. */
1784: for (i = 0; i < options.num_local_forwards; i++) {
1.232 djm 1785: debug("Local connections to %.200s:%d forwarded to remote "
1786: "address %.200s:%d",
1.406 millert 1787: (options.local_forwards[i].listen_path != NULL) ?
1788: options.local_forwards[i].listen_path :
1.234 deraadt 1789: (options.local_forwards[i].listen_host == NULL) ?
1.406 millert 1790: (options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") :
1.232 djm 1791: options.local_forwards[i].listen_host,
1792: options.local_forwards[i].listen_port,
1.406 millert 1793: (options.local_forwards[i].connect_path != NULL) ?
1794: options.local_forwards[i].connect_path :
1.232 djm 1795: options.local_forwards[i].connect_host,
1796: options.local_forwards[i].connect_port);
1.463 djm 1797: success += channel_setup_local_fwd_listener(ssh,
1.406 millert 1798: &options.local_forwards[i], &options.fwd_opts);
1.70 markus 1799: }
1.283 markus 1800: if (i > 0 && success != i && options.exit_on_forward_failure)
1801: fatal("Could not request local forwarding.");
1.86 markus 1802: if (i > 0 && success == 0)
1803: error("Could not request local forwarding.");
1.70 markus 1804:
1805: /* Initiate remote TCP/IP port forwardings. */
1806: for (i = 0; i < options.num_remote_forwards; i++) {
1.232 djm 1807: debug("Remote connections from %.200s:%d forwarded to "
1808: "local address %.200s:%d",
1.406 millert 1809: (options.remote_forwards[i].listen_path != NULL) ?
1810: options.remote_forwards[i].listen_path :
1.248 djm 1811: (options.remote_forwards[i].listen_host == NULL) ?
1.253 djm 1812: "LOCALHOST" : options.remote_forwards[i].listen_host,
1.232 djm 1813: options.remote_forwards[i].listen_port,
1.406 millert 1814: (options.remote_forwards[i].connect_path != NULL) ?
1815: options.remote_forwards[i].connect_path :
1.232 djm 1816: options.remote_forwards[i].connect_host,
1817: options.remote_forwards[i].connect_port);
1.523 djm 1818: if ((options.remote_forwards[i].handle =
1.463 djm 1819: channel_request_remote_forwarding(ssh,
1.523 djm 1820: &options.remote_forwards[i])) >= 0) {
1.463 djm 1821: client_register_global_confirm(
1822: ssh_confirm_remote_forward,
1.366 markus 1823: &options.remote_forwards[i]);
1.523 djm 1824: forward_confirms_pending++;
1825: } else if (options.exit_on_forward_failure)
1826: fatal("Could not request remote forwarding.");
1827: else
1828: logit("Warning: Could not request remote forwarding.");
1.70 markus 1829: }
1.301 djm 1830:
1831: /* Initiate tunnel forwarding. */
1832: if (options.tun_open != SSH_TUNMODE_NO) {
1.466 djm 1833: if ((*ifname = client_request_tun_fwd(ssh,
1834: options.tun_open, options.tun_local,
1.523 djm 1835: options.tun_remote, ssh_tun_confirm, NULL)) != NULL)
1836: forward_confirms_pending++;
1837: else if (options.exit_on_forward_failure)
1838: fatal("Could not request tunnel forwarding.");
1839: else
1840: error("Could not request tunnel forwarding.");
1.524 djm 1841: }
1842: if (forward_confirms_pending > 0) {
1843: debug("%s: expecting replies for %d forwards", __func__,
1844: forward_confirms_pending);
1.468 djm 1845: }
1.70 markus 1846: }
1847:
1.126 itojun 1848: static void
1.70 markus 1849: check_agent_present(void)
1850: {
1.412 djm 1851: int r;
1852:
1.70 markus 1853: if (options.forward_agent) {
1.254 djm 1854: /* Clear agent forwarding if we don't have an agent. */
1.412 djm 1855: if ((r = ssh_get_authentication_socket(NULL)) != 0) {
1.70 markus 1856: options.forward_agent = 0;
1.412 djm 1857: if (r != SSH_ERR_AGENT_NOT_PRESENT)
1858: debug("ssh_get_authentication_socket: %s",
1859: ssh_err(r));
1860: }
1.70 markus 1861: }
1862: }
1863:
1.214 djm 1864: static void
1.463 djm 1865: ssh_session2_setup(struct ssh *ssh, int id, int success, void *arg)
1.214 djm 1866: {
1.215 djm 1867: extern char **environ;
1.243 djm 1868: const char *display;
1.499 djm 1869: int r, interactive = tty_flag;
1.433 djm 1870: char *proto = NULL, *data = NULL;
1.337 djm 1871:
1872: if (!success)
1873: return; /* No need for error message, channels code sens one */
1.215 djm 1874:
1.248 djm 1875: display = getenv("DISPLAY");
1.417 djm 1876: if (display == NULL && options.forward_x11)
1877: debug("X11 forwarding requested but DISPLAY not set");
1.463 djm 1878: if (options.forward_x11 && client_x11_get_proto(ssh, display,
1.433 djm 1879: options.xauth_location, options.forward_x11_trusted,
1880: options.forward_x11_timeout, &proto, &data) == 0) {
1.50 markus 1881: /* Request forwarding with authentication spoofing. */
1.316 djm 1882: debug("Requesting X11 forwarding with authentication "
1883: "spoofing.");
1.463 djm 1884: x11_request_forwarding_with_spoofing(ssh, id, display, proto,
1.363 djm 1885: data, 1);
1.463 djm 1886: client_expect_confirm(ssh, id, "X11 forwarding", CONFIRM_WARN);
1.363 djm 1887: /* XXX exit_on_forward_failure */
1.80 markus 1888: interactive = 1;
1.50 markus 1889: }
1890:
1.70 markus 1891: check_agent_present();
1892: if (options.forward_agent) {
1893: debug("Requesting authentication agent forwarding.");
1.463 djm 1894: channel_request_start(ssh, id, "auth-agent-req@openssh.com", 0);
1.499 djm 1895: if ((r = sshpkt_send(ssh)) != 0)
1896: fatal("%s: %s", __func__, ssh_err(r));
1.212 djm 1897: }
1.369 dtucker 1898:
1899: /* Tell the packet module whether this is an interactive session. */
1.499 djm 1900: ssh_packet_set_interactive(ssh, interactive,
1.369 dtucker 1901: options.ip_qos_interactive, options.ip_qos_bulk);
1.212 djm 1902:
1.463 djm 1903: client_session2_setup(ssh, id, tty_flag, subsystem_flag, getenv("TERM"),
1.482 markus 1904: NULL, fileno(stdin), command, environ);
1.45 markus 1905: }
1906:
1.143 markus 1907: /* open new channel for a session */
1.126 itojun 1908: static int
1.463 djm 1909: ssh_session2_open(struct ssh *ssh)
1.45 markus 1910: {
1.118 markus 1911: Channel *c;
1912: int window, packetmax, in, out, err;
1.60 markus 1913:
1.62 markus 1914: if (stdin_null_flag) {
1.93 itojun 1915: in = open(_PATH_DEVNULL, O_RDONLY);
1.62 markus 1916: } else {
1917: in = dup(STDIN_FILENO);
1918: }
1.60 markus 1919: out = dup(STDOUT_FILENO);
1920: err = dup(STDERR_FILENO);
1.45 markus 1921:
1.505 deraadt 1922: if (in == -1 || out == -1 || err == -1)
1.62 markus 1923: fatal("dup() in/out/err failed");
1.45 markus 1924:
1.69 markus 1925: /* enable nonblocking unless tty */
1926: if (!isatty(in))
1927: set_nonblock(in);
1928: if (!isatty(out))
1929: set_nonblock(out);
1930: if (!isatty(err))
1931: set_nonblock(err);
1932:
1.65 markus 1933: window = CHAN_SES_WINDOW_DEFAULT;
1934: packetmax = CHAN_SES_PACKET_DEFAULT;
1.164 markus 1935: if (tty_flag) {
1936: window >>= 1;
1937: packetmax >>= 1;
1.45 markus 1938: }
1.463 djm 1939: c = channel_new(ssh,
1.45 markus 1940: "session", SSH_CHANNEL_OPENING, in, out, err,
1.65 markus 1941: window, packetmax, CHAN_EXTENDED_WRITE,
1.192 markus 1942: "client-session", /*nonblock*/0);
1.45 markus 1943:
1.463 djm 1944: debug3("%s: channel_new: %d", __func__, c->self);
1.106 markus 1945:
1.463 djm 1946: channel_send_open(ssh, c->self);
1.143 markus 1947: if (!no_shell_flag)
1.463 djm 1948: channel_register_open_confirm(ssh, c->self,
1.310 djm 1949: ssh_session2_setup, NULL);
1.106 markus 1950:
1.118 markus 1951: return c->self;
1.106 markus 1952: }
1953:
1.126 itojun 1954: static int
1.466 djm 1955: ssh_session2(struct ssh *ssh, struct passwd *pw)
1.106 markus 1956: {
1.499 djm 1957: int r, devnull, id = -1;
1.466 djm 1958: char *cp, *tun_fwd_ifname = NULL;
1.106 markus 1959:
1960: /* XXX should be pre-session */
1.368 djm 1961: if (!options.control_persist)
1.463 djm 1962: ssh_init_stdio_forwarding(ssh);
1.466 djm 1963:
1964: ssh_init_forwarding(ssh, &tun_fwd_ifname);
1965:
1966: if (options.local_command != NULL) {
1967: debug3("expanding LocalCommand: %s", options.local_command);
1968: cp = options.local_command;
1969: options.local_command = percent_expand(cp,
1.522 dtucker 1970: DEFAULT_CLIENT_PERCENT_EXPAND_ARGS,
1.466 djm 1971: "d", pw->pw_dir,
1972: "h", host,
1973: "r", options.user,
1974: "u", pw->pw_name,
1975: "T", tun_fwd_ifname == NULL ? "NONE" : tun_fwd_ifname,
1976: (char *)NULL);
1977: debug3("expanded LocalCommand: %s", options.local_command);
1978: free(cp);
1979: }
1.106 markus 1980:
1.344 djm 1981: /* Start listening for multiplex clients */
1.499 djm 1982: if (!ssh_packet_get_mux(ssh))
1.463 djm 1983: muxserver_listen(ssh);
1.344 djm 1984:
1.473 djm 1985: /*
1.368 djm 1986: * If we are in control persist mode and have a working mux listen
1987: * socket, then prepare to background ourselves and have a foreground
1988: * client attach as a control slave.
1989: * NB. we must save copies of the flags that we override for
1.344 djm 1990: * the backgrounding, since we defer attachment of the slave until
1991: * after the connection is fully established (in particular,
1992: * async rfwd replies have been received for ExitOnForwardFailure).
1993: */
1.473 djm 1994: if (options.control_persist && muxserver_sock != -1) {
1.344 djm 1995: ostdin_null_flag = stdin_null_flag;
1996: ono_shell_flag = no_shell_flag;
1.359 djm 1997: orequest_tty = options.request_tty;
1.344 djm 1998: otty_flag = tty_flag;
1.473 djm 1999: stdin_null_flag = 1;
2000: no_shell_flag = 1;
2001: tty_flag = 0;
1.344 djm 2002: if (!fork_after_authentication_flag)
2003: need_controlpersist_detach = 1;
2004: fork_after_authentication_flag = 1;
1.473 djm 2005: }
1.368 djm 2006: /*
2007: * ControlPersist mux listen socket setup failed, attempt the
2008: * stdio forward setup that we skipped earlier.
2009: */
2010: if (options.control_persist && muxserver_sock == -1)
1.463 djm 2011: ssh_init_stdio_forwarding(ssh);
1.344 djm 2012:
1.471 djm 2013: if (!no_shell_flag)
1.463 djm 2014: id = ssh_session2_open(ssh);
1.379 djm 2015: else {
1.499 djm 2016: ssh_packet_set_interactive(ssh,
1.379 djm 2017: options.control_master == SSHCTL_MASTER_NO,
2018: options.ip_qos_interactive, options.ip_qos_bulk);
2019: }
1.314 djm 2020:
2021: /* If we don't expect to open a new session, then disallow it */
1.319 markus 2022: if (options.control_master == SSHCTL_MASTER_NO &&
2023: (datafellows & SSH_NEW_OPENSSH)) {
1.314 djm 2024: debug("Requesting no-more-sessions@openssh.com");
1.499 djm 2025: if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
2026: (r = sshpkt_put_cstring(ssh,
2027: "no-more-sessions@openssh.com")) != 0 ||
2028: (r = sshpkt_put_u8(ssh, 0)) != 0 ||
2029: (r = sshpkt_send(ssh)) != 0)
2030: fatal("%s: %s", __func__, ssh_err(r));
1.314 djm 2031: }
1.255 reyk 2032:
2033: /* Execute a local command */
2034: if (options.local_command != NULL &&
2035: options.permit_local_command)
2036: ssh_local_cmd(options.local_command);
1.467 djm 2037:
2038: /*
2039: * stdout is now owned by the session channel; clobber it here
2040: * so future channel closes are propagated to the local fd.
2041: * NB. this can only happen after LocalCommand has completed,
2042: * as it may want to write to stdout.
2043: */
1.469 djm 2044: if (!need_controlpersist_detach) {
2045: if ((devnull = open(_PATH_DEVNULL, O_WRONLY)) == -1)
2046: error("%s: open %s: %s", __func__,
2047: _PATH_DEVNULL, strerror(errno));
1.505 deraadt 2048: if (dup2(devnull, STDOUT_FILENO) == -1)
1.469 djm 2049: fatal("%s: dup2() stdout failed", __func__);
2050: if (devnull > STDERR_FILENO)
2051: close(devnull);
2052: }
1.301 djm 2053:
1.342 djm 2054: /*
2055: * If requested and we are not interested in replies to remote
2056: * forwarding requests, then let ssh continue in the background.
2057: */
1.344 djm 2058: if (fork_after_authentication_flag) {
2059: if (options.exit_on_forward_failure &&
2060: options.num_remote_forwards > 0) {
2061: debug("deferring postauth fork until remote forward "
2062: "confirmation received");
2063: } else
2064: fork_postauth();
1.318 djm 2065: }
1.31 markus 2066:
1.463 djm 2067: return client_loop(ssh, tty_flag, tty_flag ?
1.119 stevesk 2068: options.escape_char : SSH_ESCAPECHAR_NONE, id);
1.72 markus 2069: }
2070:
1.426 djm 2071: /* Loads all IdentityFile and CertificateFile keys */
1.126 itojun 2072: static void
1.466 djm 2073: load_public_identity_files(struct passwd *pw)
1.104 markus 2074: {
1.466 djm 2075: char *filename, *cp;
1.460 markus 2076: struct sshkey *public;
1.426 djm 2077: int i;
2078: u_int n_ids, n_certs;
1.335 djm 2079: char *identity_files[SSH_MAX_IDENTITY_FILES];
1.460 markus 2080: struct sshkey *identity_keys[SSH_MAX_IDENTITY_FILES];
1.484 djm 2081: int identity_file_userprovided[SSH_MAX_IDENTITY_FILES];
1.426 djm 2082: char *certificate_files[SSH_MAX_CERTIFICATE_FILES];
2083: struct sshkey *certificates[SSH_MAX_CERTIFICATE_FILES];
1.484 djm 2084: int certificate_file_userprovided[SSH_MAX_CERTIFICATE_FILES];
1.333 markus 2085: #ifdef ENABLE_PKCS11
1.514 djm 2086: struct sshkey **keys = NULL;
2087: char **comments = NULL;
1.333 markus 2088: int nkeys;
1.335 djm 2089: #endif /* PKCS11 */
1.104 markus 2090:
1.426 djm 2091: n_ids = n_certs = 0;
1.398 tedu 2092: memset(identity_files, 0, sizeof(identity_files));
2093: memset(identity_keys, 0, sizeof(identity_keys));
1.484 djm 2094: memset(identity_file_userprovided, 0,
2095: sizeof(identity_file_userprovided));
1.426 djm 2096: memset(certificate_files, 0, sizeof(certificate_files));
2097: memset(certificates, 0, sizeof(certificates));
1.484 djm 2098: memset(certificate_file_userprovided, 0,
2099: sizeof(certificate_file_userprovided));
1.335 djm 2100:
2101: #ifdef ENABLE_PKCS11
1.333 markus 2102: if (options.pkcs11_provider != NULL &&
1.167 markus 2103: options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1.333 markus 2104: (pkcs11_init(!options.batch_mode) == 0) &&
2105: (nkeys = pkcs11_add_provider(options.pkcs11_provider, NULL,
1.514 djm 2106: &keys, &comments)) > 0) {
1.333 markus 2107: for (i = 0; i < nkeys; i++) {
1.335 djm 2108: if (n_ids >= SSH_MAX_IDENTITY_FILES) {
1.483 markus 2109: sshkey_free(keys[i]);
1.514 djm 2110: free(comments[i]);
1.335 djm 2111: continue;
2112: }
2113: identity_keys[n_ids] = keys[i];
1.514 djm 2114: identity_files[n_ids] = comments[i]; /* transferred */
1.335 djm 2115: n_ids++;
1.167 markus 2116: }
1.378 djm 2117: free(keys);
1.514 djm 2118: free(comments);
1.127 markus 2119: }
1.333 markus 2120: #endif /* ENABLE_PKCS11 */
1.335 djm 2121: for (i = 0; i < options.num_identity_files; i++) {
1.373 djm 2122: if (n_ids >= SSH_MAX_IDENTITY_FILES ||
2123: strcasecmp(options.identity_files[i], "none") == 0) {
1.378 djm 2124: free(options.identity_files[i]);
1.426 djm 2125: options.identity_files[i] = NULL;
1.335 djm 2126: continue;
2127: }
1.490 dtucker 2128: cp = tilde_expand_filename(options.identity_files[i], getuid());
1.522 dtucker 2129: filename = percent_expand(cp,
2130: DEFAULT_CLIENT_PERCENT_EXPAND_ARGS,
2131: "d", pw->pw_dir,
2132: "h", host,
2133: "r", options.user,
2134: "u", pw->pw_name,
2135: (char *)NULL);
1.378 djm 2136: free(cp);
1.483 markus 2137: check_load(sshkey_load_public(filename, &public, NULL),
2138: filename, "pubkey");
1.131 millert 2139: debug("identity file %s type %d", filename,
2140: public ? public->type : -1);
1.378 djm 2141: free(options.identity_files[i]);
1.335 djm 2142: identity_files[n_ids] = filename;
2143: identity_keys[n_ids] = public;
1.484 djm 2144: identity_file_userprovided[n_ids] =
2145: options.identity_file_userprovided[i];
1.335 djm 2146: if (++n_ids >= SSH_MAX_IDENTITY_FILES)
2147: continue;
2148:
1.426 djm 2149: /*
2150: * If no certificates have been explicitly listed then try
2151: * to add the default certificate variant too.
2152: */
2153: if (options.num_certificate_files != 0)
2154: continue;
1.335 djm 2155: xasprintf(&cp, "%s-cert", filename);
1.483 markus 2156: check_load(sshkey_load_public(cp, &public, NULL),
2157: filename, "pubkey");
1.335 djm 2158: debug("identity file %s type %d", cp,
2159: public ? public->type : -1);
2160: if (public == NULL) {
1.378 djm 2161: free(cp);
1.335 djm 2162: continue;
2163: }
1.483 markus 2164: if (!sshkey_is_cert(public)) {
1.335 djm 2165: debug("%s: key %s type %s is not a certificate",
1.483 markus 2166: __func__, cp, sshkey_type(public));
2167: sshkey_free(public);
1.378 djm 2168: free(cp);
1.335 djm 2169: continue;
2170: }
1.448 djm 2171: /* NB. leave filename pointing to private key */
2172: identity_files[n_ids] = xstrdup(filename);
1.335 djm 2173: identity_keys[n_ids] = public;
1.484 djm 2174: identity_file_userprovided[n_ids] =
2175: options.identity_file_userprovided[i];
1.335 djm 2176: n_ids++;
2177: }
1.426 djm 2178:
2179: if (options.num_certificate_files > SSH_MAX_CERTIFICATE_FILES)
2180: fatal("%s: too many certificates", __func__);
2181: for (i = 0; i < options.num_certificate_files; i++) {
2182: cp = tilde_expand_filename(options.certificate_files[i],
1.490 dtucker 2183: getuid());
1.479 djm 2184: filename = percent_expand(cp,
1.522 dtucker 2185: DEFAULT_CLIENT_PERCENT_EXPAND_ARGS,
1.479 djm 2186: "d", pw->pw_dir,
2187: "h", host,
2188: "r", options.user,
2189: "u", pw->pw_name,
2190: (char *)NULL);
1.426 djm 2191: free(cp);
2192:
1.483 markus 2193: check_load(sshkey_load_public(filename, &public, NULL),
2194: filename, "certificate");
1.426 djm 2195: debug("certificate file %s type %d", filename,
2196: public ? public->type : -1);
2197: free(options.certificate_files[i]);
2198: options.certificate_files[i] = NULL;
2199: if (public == NULL) {
2200: free(filename);
2201: continue;
2202: }
1.483 markus 2203: if (!sshkey_is_cert(public)) {
1.426 djm 2204: debug("%s: key %s type %s is not a certificate",
1.483 markus 2205: __func__, filename, sshkey_type(public));
2206: sshkey_free(public);
1.426 djm 2207: free(filename);
2208: continue;
2209: }
2210: certificate_files[n_certs] = filename;
2211: certificates[n_certs] = public;
1.484 djm 2212: certificate_file_userprovided[n_certs] =
2213: options.certificate_file_userprovided[i];
1.426 djm 2214: ++n_certs;
2215: }
2216:
1.335 djm 2217: options.num_identity_files = n_ids;
2218: memcpy(options.identity_files, identity_files, sizeof(identity_files));
2219: memcpy(options.identity_keys, identity_keys, sizeof(identity_keys));
1.484 djm 2220: memcpy(options.identity_file_userprovided,
2221: identity_file_userprovided, sizeof(identity_file_userprovided));
1.426 djm 2222:
2223: options.num_certificate_files = n_certs;
2224: memcpy(options.certificate_files,
2225: certificate_files, sizeof(certificate_files));
2226: memcpy(options.certificates, certificates, sizeof(certificates));
1.484 djm 2227: memcpy(options.certificate_file_userprovided,
2228: certificate_file_userprovided,
2229: sizeof(certificate_file_userprovided));
1.214 djm 2230: }
1.352 djm 2231:
2232: static void
2233: main_sigchld_handler(int sig)
2234: {
2235: int save_errno = errno;
2236: pid_t pid;
2237: int status;
2238:
2239: while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
1.505 deraadt 2240: (pid == -1 && errno == EINTR))
1.352 djm 2241: ;
2242: errno = save_errno;
2243: }