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