blob: 98b6ce788a8182e7564609f5b255f64814f7ffc3 [file] [log] [blame]
dtucker@openbsd.org990687a2020-04-10 00:52:07 +00001/* $OpenBSD: ssh.c,v 1.527 2020/04/10 00:52:07 dtucker Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * 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 *
Damien Millere4340be2000-09-16 13:29:08 +110010 * 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.
Darren Tucker0a118da2003-10-15 15:54:32 +100017 * Copyright (c) 2000, 2001, 2002, 2003 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110018 *
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.
Damien Miller95def091999-11-25 00:26:21 +110041 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100042
43#include "includes.h"
Damien Millercd4223c2006-03-15 11:22:47 +110044
Damien Millerf17883e2006-03-15 11:45:54 +110045#include <sys/types.h>
46#ifdef HAVE_SYS_STAT_H
47# include <sys/stat.h>
48#endif
Damien Millercd4223c2006-03-15 11:22:47 +110049#include <sys/resource.h>
Damien Miller17e91c02006-03-15 11:28:34 +110050#include <sys/ioctl.h>
Damien Millere3b60b52006-07-10 21:08:03 +100051#include <sys/socket.h>
Damien Miller857b02e2010-09-24 22:02:56 +100052#include <sys/wait.h>
Damien Miller03e20032006-03-15 11:16:59 +110053
Damien Millerc7b06362006-03-15 11:53:45 +110054#include <ctype.h>
Darren Tuckerba724052006-07-12 22:24:22 +100055#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100056#include <fcntl.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100057#include <netdb.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110058#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110059#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110060#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100061#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110062#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100063#include <stdarg.h>
Damien Miller2d00e632006-07-24 13:53:19 +100064#include <stddef.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100065#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100066#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100067#include <string.h>
naddy@openbsd.org189550f2019-11-18 16:10:05 +000068#include <stdarg.h>
Damien Millere6b3b612006-07-24 14:01:23 +100069#include <unistd.h>
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000070#include <limits.h>
djm@openbsd.org65c6c6b2016-07-17 04:20:16 +000071#include <locale.h>
Damien Millereba71ba2000-04-29 23:57:08 +100072
Darren Tucker46aa3e02006-09-02 15:32:40 +100073#include <netinet/in.h>
74#include <arpa/inet.h>
75
Damien Miller1f0311c2014-05-15 14:24:09 +100076#ifdef WITH_OPENSSL
Damien Millereba71ba2000-04-29 23:57:08 +100077#include <openssl/evp.h>
Damien Miller0bc1bd82000-11-13 22:57:25 +110078#include <openssl/err.h>
Damien Miller1f0311c2014-05-15 14:24:09 +100079#endif
Darren Tuckerbfaaf962008-02-28 19:13:52 +110080#include "openbsd-compat/openssl-compat.h"
Damien Millerb84886b2008-05-19 15:05:07 +100081#include "openbsd-compat/sys-queue.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082
Damien Millerd7834352006-08-05 12:39:39 +100083#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084#include "ssh.h"
Damien Miller1383bd82000-04-06 12:32:37 +100085#include "ssh2.h"
Damien Millerb96c4412010-07-02 13:34:24 +100086#include "canohost.h"
Damien Miller1383bd82000-04-06 12:32:37 +100087#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000088#include "cipher.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000089#include "packet.h"
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +000090#include "sshbuf.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000091#include "channels.h"
markus@openbsd.org5467fbc2018-07-11 18:53:29 +000092#include "sshkey.h"
Damien Miller994cf142000-07-21 10:19:44 +100093#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100094#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000095#include "pathnames.h"
Damien Miller0e220db2004-06-15 10:34:08 +100096#include "dispatch.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +000097#include "clientloop.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000098#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100099#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000100#include "readconf.h"
101#include "sshconnect.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000102#include "kex.h"
103#include "mac.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +1000104#include "sshpty.h"
Darren Tucker46bc0752004-05-02 22:11:30 +1000105#include "match.h"
Damien Miller0e220db2004-06-15 10:34:08 +1000106#include "msg.h"
Damien Millerb7576772006-07-10 20:23:39 +1000107#include "version.h"
djm@openbsd.org141efe42015-01-14 20:05:27 +0000108#include "ssherr.h"
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000109#include "myproposal.h"
Damien Millerdda78a02016-12-12 13:57:10 +1100110#include "utf8.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111
Damien Miller7ea845e2010-02-12 09:21:02 +1100112#ifdef ENABLE_PKCS11
113#include "ssh-pkcs11.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +0000114#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000115
Damien Miller3f905871999-11-15 17:10:57 +1100116extern char *__progname;
Damien Miller3f905871999-11-15 17:10:57 +1100117
Damien Millerea2c1a42011-06-03 12:10:22 +1000118/* Saves a copy of argv for setproctitle emulation */
119#ifndef HAVE_SETPROCTITLE
120static char **saved_av;
121#endif
122
Darren Tuckerd6173c02008-06-13 04:52:53 +1000123/* Flag indicating whether debug mode is on. May be set on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124int debug_flag = 0;
125
Damien Miller21771e22011-05-15 08:45:50 +1000126/* Flag indicating whether a tty should be requested */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127int tty_flag = 0;
128
Damien Miller1383bd82000-04-06 12:32:37 +1000129/* don't exec a shell */
130int no_shell_flag = 0;
Damien Miller1383bd82000-04-06 12:32:37 +1000131
Damien Miller5428f641999-11-25 11:54:57 +1100132/*
133 * Flag indicating that nothing should be read from stdin. This can be set
134 * on the command line.
135 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136int stdin_null_flag = 0;
137
Damien Miller5428f641999-11-25 11:54:57 +1100138/*
Damien Millere11e1ea2010-08-03 16:04:46 +1000139 * Flag indicating that the current process should be backgrounded and
140 * a new slave launched in the foreground for ControlPersist.
141 */
142int need_controlpersist_detach = 0;
143
144/* Copies of flags for ControlPersist foreground slave */
Damien Miller21771e22011-05-15 08:45:50 +1000145int ostdin_null_flag, ono_shell_flag, otty_flag, orequest_tty;
Damien Millere11e1ea2010-08-03 16:04:46 +1000146
147/*
Damien Miller5428f641999-11-25 11:54:57 +1100148 * Flag indicating that ssh should fork after authentication. This is useful
Ben Lindstromf666fec2002-06-06 19:51:58 +0000149 * so that the passphrase can be entered manually, and then ssh goes to the
Damien Miller5428f641999-11-25 11:54:57 +1100150 * background.
151 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152int fork_after_authentication_flag = 0;
153
Damien Miller5428f641999-11-25 11:54:57 +1100154/*
155 * General data structure for command line options and options configurable
156 * in configuration files. See readconf.h.
157 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158Options options;
159
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000160/* optional user configfile */
161char *config = NULL;
162
Damien Miller5428f641999-11-25 11:54:57 +1100163/*
164 * Name of the host we are connecting to. This is the name given on the
jmc@openbsd.org73491492019-06-12 11:31:50 +0000165 * command line, or the Hostname specified for the user-supplied name in a
Damien Miller5428f641999-11-25 11:54:57 +1100166 * configuration file.
167 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168char *host;
169
djm@openbsd.org40be78f2019-12-21 02:19:13 +0000170/*
171 * A config can specify a path to forward, overriding SSH_AUTH_SOCK. If this is
172 * not NULL, forward the socket at this path instead.
173 */
174char *forward_agent_sock_path = NULL;
175
djm@openbsd.orgb7548b12017-10-23 05:08:00 +0000176/* Various strings used to to percent_expand() arguments */
177static char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
178static char uidstr[32], *host_arg, *conn_hash_hex;
179
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000180/* socket address the host resolves to */
Damien Miller34132e52000-01-14 15:45:46 +1100181struct sockaddr_storage hostaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000182
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000183/* Private host keys. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000184Sensitive sensitive_data;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000185
Damien Miller1383bd82000-04-06 12:32:37 +1000186/* command to be executed */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000187struct sshbuf *command;
Damien Miller1383bd82000-04-06 12:32:37 +1000188
Damien Miller832562e2001-01-30 09:30:01 +1100189/* Should we execute a command or invoke a subsystem? */
190int subsystem_flag = 0;
191
Damien Miller2797f7f2002-04-23 21:09:44 +1000192/* # of replies received for global requests */
djm@openbsd.org663e84b2020-04-03 02:40:32 +0000193static int forward_confirms_pending = -1;
Damien Miller2797f7f2002-04-23 21:09:44 +1000194
Damien Millerb1cbfa22008-05-19 16:00:08 +1000195/* mux.c */
196extern int muxserver_sock;
197extern u_int muxclient_command;
Damien Miller0e220db2004-06-15 10:34:08 +1000198
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199/* Prints a help message to the user. This function never returns. */
200
Ben Lindstrombba81212001-06-25 05:01:22 +0000201static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000202usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203{
Damien Miller50955102004-03-22 09:34:58 +1100204 fprintf(stderr,
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000205"usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-B bind_interface]\n"
206" [-b bind_address] [-c cipher_spec] [-D [bind_address:]port]\n"
207" [-E log_file] [-e escape_char] [-F configfile] [-I pkcs11]\n"
208" [-i identity_file] [-J [user@]host[:port]] [-L address]\n"
209" [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
210" [-Q query_option] [-R address] [-S ctl_path] [-W host:port]\n"
211" [-w local_tun[:remote_tun]] destination [command]\n"
Damien Miller50955102004-03-22 09:34:58 +1100212 );
Darren Tuckere9a9b712005-12-20 16:15:51 +1100213 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000214}
215
djm@openbsd.orgb7548b12017-10-23 05:08:00 +0000216static int ssh_session2(struct ssh *, struct passwd *);
217static void load_public_identity_files(struct passwd *);
Damien Miller857b02e2010-09-24 22:02:56 +1000218static void main_sigchld_handler(int);
Damien Millerb1cbfa22008-05-19 16:00:08 +1000219
Damien Miller295ee632011-05-29 21:42:31 +1000220/* ~/ expand a list of paths. NB. assumes path[n] is heap-allocated. */
221static void
222tilde_expand_paths(char **paths, u_int num_paths)
223{
224 u_int i;
225 char *cp;
226
227 for (i = 0; i < num_paths; i++) {
dtucker@openbsd.orge655ee02018-07-27 05:34:42 +0000228 cp = tilde_expand_filename(paths[i], getuid());
Darren Tuckera627d422013-06-02 07:31:17 +1000229 free(paths[i]);
Damien Miller295ee632011-05-29 21:42:31 +1000230 paths[i] = cp;
231 }
232}
233
dtucker@openbsd.org990687a2020-04-10 00:52:07 +0000234#define DEFAULT_CLIENT_PERCENT_EXPAND_ARGS \
235 "C", conn_hash_hex, \
236 "L", shorthost, \
237 "i", uidstr, \
238 "l", thishost, \
239 "n", host_arg, \
240 "p", portstr
241
242/*
243 * Expands the set of percent_expand options used by the majority of keywords
244 * in the client that support percent expansion.
245 * Caller must free returned string.
246 */
247static char *
248default_client_percent_expand(const char *str, const char *homedir,
249 const char *remhost, const char *remuser, const char *locuser)
250{
251 return percent_expand(str,
252 /* values from statics above */
253 DEFAULT_CLIENT_PERCENT_EXPAND_ARGS,
254 /* values from arguments */
255 "d", homedir,
256 "h", remhost,
257 "r", remuser,
258 "u", locuser,
259 (char *)NULL);
260}
261
Damien Miller13f97b22014-02-24 15:57:55 +1100262/*
263 * Attempt to resolve a host name / port to a set of addresses and
264 * optionally return any CNAMEs encountered along the way.
265 * Returns NULL on failure.
266 * NB. this function must operate with a options having undefined members.
267 */
Damien Miller0faf7472013-10-17 11:47:23 +1100268static struct addrinfo *
Damien Miller13f97b22014-02-24 15:57:55 +1100269resolve_host(const char *name, int port, int logerr, char *cname, size_t clen)
Damien Miller0faf7472013-10-17 11:47:23 +1100270{
271 char strport[NI_MAXSERV];
272 struct addrinfo hints, *res;
dtucker@openbsd.org3a7db912019-04-23 11:56:41 +0000273 int gaierr;
274 LogLevel loglevel = SYSLOG_LEVEL_DEBUG1;
Damien Miller0faf7472013-10-17 11:47:23 +1100275
Damien Miller13f97b22014-02-24 15:57:55 +1100276 if (port <= 0)
277 port = default_ssh_port();
markus@openbsd.org31f1ee52020-03-06 18:20:02 +0000278 if (cname != NULL)
279 *cname = '\0';
Damien Miller13f97b22014-02-24 15:57:55 +1100280
djm@openbsd.orgb1d38a32015-10-15 23:51:40 +0000281 snprintf(strport, sizeof strport, "%d", port);
Damien Miller1d2c4562014-02-04 11:18:20 +1100282 memset(&hints, 0, sizeof(hints));
Damien Miller13f97b22014-02-24 15:57:55 +1100283 hints.ai_family = options.address_family == -1 ?
284 AF_UNSPEC : options.address_family;
Damien Miller0faf7472013-10-17 11:47:23 +1100285 hints.ai_socktype = SOCK_STREAM;
286 if (cname != NULL)
287 hints.ai_flags = AI_CANONNAME;
288 if ((gaierr = getaddrinfo(name, strport, &hints, &res)) != 0) {
289 if (logerr || (gaierr != EAI_NONAME && gaierr != EAI_NODATA))
290 loglevel = SYSLOG_LEVEL_ERROR;
291 do_log2(loglevel, "%s: Could not resolve hostname %.100s: %s",
292 __progname, name, ssh_gai_strerror(gaierr));
293 return NULL;
294 }
295 if (cname != NULL && res->ai_canonname != NULL) {
296 if (strlcpy(cname, res->ai_canonname, clen) >= clen) {
297 error("%s: host \"%s\" cname \"%s\" too long (max %lu)",
298 __func__, name, res->ai_canonname, (u_long)clen);
299 if (clen > 0)
300 *cname = '\0';
301 }
302 }
303 return res;
304}
305
djm@openbsd.orgfc21ea92018-01-23 05:06:25 +0000306/* Returns non-zero if name can only be an address and not a hostname */
307static int
308is_addr_fast(const char *name)
309{
310 return (strchr(name, '%') != NULL || strchr(name, ':') != NULL ||
311 strspn(name, "0123456789.") == strlen(name));
312}
313
314/* Returns non-zero if name represents a valid, single address */
315static int
316is_addr(const char *name)
317{
318 char strport[NI_MAXSERV];
319 struct addrinfo hints, *res;
320
321 if (is_addr_fast(name))
322 return 1;
323
324 snprintf(strport, sizeof strport, "%u", default_ssh_port());
325 memset(&hints, 0, sizeof(hints));
326 hints.ai_family = options.address_family == -1 ?
327 AF_UNSPEC : options.address_family;
328 hints.ai_socktype = SOCK_STREAM;
329 hints.ai_flags = AI_NUMERICHOST|AI_NUMERICSERV;
330 if (getaddrinfo(name, strport, &hints, &res) != 0)
331 return 0;
332 if (res == NULL || res->ai_next != NULL) {
333 freeaddrinfo(res);
334 return 0;
335 }
336 freeaddrinfo(res);
337 return 1;
338}
339
Damien Miller0faf7472013-10-17 11:47:23 +1100340/*
djm@openbsd.org90109022015-01-16 07:19:48 +0000341 * Attempt to resolve a numeric host address / port to a single address.
342 * Returns a canonical address string.
343 * Returns NULL on failure.
344 * NB. this function must operate with a options having undefined members.
345 */
346static struct addrinfo *
347resolve_addr(const char *name, int port, char *caddr, size_t clen)
348{
349 char addr[NI_MAXHOST], strport[NI_MAXSERV];
350 struct addrinfo hints, *res;
351 int gaierr;
352
353 if (port <= 0)
354 port = default_ssh_port();
355 snprintf(strport, sizeof strport, "%u", port);
356 memset(&hints, 0, sizeof(hints));
357 hints.ai_family = options.address_family == -1 ?
358 AF_UNSPEC : options.address_family;
359 hints.ai_socktype = SOCK_STREAM;
360 hints.ai_flags = AI_NUMERICHOST|AI_NUMERICSERV;
361 if ((gaierr = getaddrinfo(name, strport, &hints, &res)) != 0) {
362 debug2("%s: could not resolve name %.100s as address: %s",
363 __func__, name, ssh_gai_strerror(gaierr));
364 return NULL;
365 }
366 if (res == NULL) {
367 debug("%s: getaddrinfo %.100s returned no addresses",
368 __func__, name);
369 return NULL;
370 }
371 if (res->ai_next != NULL) {
372 debug("%s: getaddrinfo %.100s returned multiple addresses",
373 __func__, name);
374 goto fail;
375 }
376 if ((gaierr = getnameinfo(res->ai_addr, res->ai_addrlen,
377 addr, sizeof(addr), NULL, 0, NI_NUMERICHOST)) != 0) {
378 debug("%s: Could not format address for name %.100s: %s",
379 __func__, name, ssh_gai_strerror(gaierr));
380 goto fail;
381 }
382 if (strlcpy(caddr, addr, clen) >= clen) {
383 error("%s: host \"%s\" addr \"%s\" too long (max %lu)",
384 __func__, name, addr, (u_long)clen);
385 if (clen > 0)
386 *caddr = '\0';
387 fail:
388 freeaddrinfo(res);
389 return NULL;
390 }
391 return res;
392}
393
394/*
Damien Miller0faf7472013-10-17 11:47:23 +1100395 * Check whether the cname is a permitted replacement for the hostname
396 * and perform the replacement if it is.
Damien Miller13f97b22014-02-24 15:57:55 +1100397 * NB. this function must operate with a options having undefined members.
Damien Miller0faf7472013-10-17 11:47:23 +1100398 */
399static int
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000400check_follow_cname(int direct, char **namep, const char *cname)
Damien Miller0faf7472013-10-17 11:47:23 +1100401{
402 int i;
403 struct allowed_cname *rule;
404
405 if (*cname == '\0' || options.num_permitted_cnames == 0 ||
406 strcmp(*namep, cname) == 0)
407 return 0;
Damien Miller38505592013-10-17 11:48:13 +1100408 if (options.canonicalize_hostname == SSH_CANONICALISE_NO)
Damien Miller0faf7472013-10-17 11:47:23 +1100409 return 0;
410 /*
Damien Miller38505592013-10-17 11:48:13 +1100411 * Don't attempt to canonicalize names that will be interpreted by
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000412 * a proxy or jump host unless the user specifically requests so.
Damien Miller0faf7472013-10-17 11:47:23 +1100413 */
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000414 if (!direct &&
Damien Miller38505592013-10-17 11:48:13 +1100415 options.canonicalize_hostname != SSH_CANONICALISE_ALWAYS)
Damien Miller0faf7472013-10-17 11:47:23 +1100416 return 0;
417 debug3("%s: check \"%s\" CNAME \"%s\"", __func__, *namep, cname);
418 for (i = 0; i < options.num_permitted_cnames; i++) {
419 rule = options.permitted_cnames + i;
djm@openbsd.orge661a862015-05-04 06:10:48 +0000420 if (match_pattern_list(*namep, rule->source_list, 1) != 1 ||
421 match_pattern_list(cname, rule->target_list, 1) != 1)
Damien Miller0faf7472013-10-17 11:47:23 +1100422 continue;
Damien Miller38505592013-10-17 11:48:13 +1100423 verbose("Canonicalized DNS aliased hostname "
Damien Miller0faf7472013-10-17 11:47:23 +1100424 "\"%s\" => \"%s\"", *namep, cname);
425 free(*namep);
426 *namep = xstrdup(cname);
427 return 1;
428 }
429 return 0;
430}
431
432/*
433 * Attempt to resolve the supplied hostname after applying the user's
Damien Miller51682fa2013-10-17 11:48:31 +1100434 * canonicalization rules. Returns the address list for the host or NULL
435 * if no name was found after canonicalization.
Damien Miller13f97b22014-02-24 15:57:55 +1100436 * NB. this function must operate with a options having undefined members.
Damien Miller0faf7472013-10-17 11:47:23 +1100437 */
438static struct addrinfo *
Damien Miller13f97b22014-02-24 15:57:55 +1100439resolve_canonicalize(char **hostp, int port)
Damien Miller0faf7472013-10-17 11:47:23 +1100440{
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000441 int i, direct, ndots;
djm@openbsd.org90109022015-01-16 07:19:48 +0000442 char *cp, *fullhost, newname[NI_MAXHOST];
Damien Miller0faf7472013-10-17 11:47:23 +1100443 struct addrinfo *addrs;
444
djm@openbsd.orgfc21ea92018-01-23 05:06:25 +0000445 /*
446 * Attempt to canonicalise addresses, regardless of
447 * whether hostname canonicalisation was requested
448 */
449 if ((addrs = resolve_addr(*hostp, port,
450 newname, sizeof(newname))) != NULL) {
451 debug2("%s: hostname %.100s is address", __func__, *hostp);
452 if (strcasecmp(*hostp, newname) != 0) {
453 debug2("%s: canonicalised address \"%s\" => \"%s\"",
454 __func__, *hostp, newname);
455 free(*hostp);
456 *hostp = xstrdup(newname);
457 }
458 return addrs;
459 }
460
461 /*
462 * If this looks like an address but didn't parse as one, it might
463 * be an address with an invalid interface scope. Skip further
464 * attempts at canonicalisation.
465 */
466 if (is_addr_fast(*hostp)) {
467 debug("%s: hostname %.100s is an unrecognised address",
468 __func__, *hostp);
469 return NULL;
470 }
471
Damien Miller38505592013-10-17 11:48:13 +1100472 if (options.canonicalize_hostname == SSH_CANONICALISE_NO)
Damien Miller0faf7472013-10-17 11:47:23 +1100473 return NULL;
Damien Miller13f97b22014-02-24 15:57:55 +1100474
Damien Miller0faf7472013-10-17 11:47:23 +1100475 /*
Damien Miller38505592013-10-17 11:48:13 +1100476 * Don't attempt to canonicalize names that will be interpreted by
Damien Miller0faf7472013-10-17 11:47:23 +1100477 * a proxy unless the user specifically requests so.
478 */
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000479 direct = option_clear_or_none(options.proxy_command) &&
480 options.jump_host == NULL;
481 if (!direct &&
Damien Miller38505592013-10-17 11:48:13 +1100482 options.canonicalize_hostname != SSH_CANONICALISE_ALWAYS)
Damien Miller0faf7472013-10-17 11:47:23 +1100483 return NULL;
Damien Miller13f97b22014-02-24 15:57:55 +1100484
djm@openbsd.org5ee00632015-10-16 18:40:49 +0000485 /* If domain name is anchored, then resolve it now */
486 if ((*hostp)[strlen(*hostp) - 1] == '.') {
487 debug3("%s: name is fully qualified", __func__);
488 fullhost = xstrdup(*hostp);
489 if ((addrs = resolve_host(fullhost, port, 0,
490 newname, sizeof(newname))) != NULL)
491 goto found;
492 free(fullhost);
493 goto notfound;
494 }
495
Damien Miller51682fa2013-10-17 11:48:31 +1100496 /* Don't apply canonicalization to sufficiently-qualified hostnames */
Damien Miller0faf7472013-10-17 11:47:23 +1100497 ndots = 0;
498 for (cp = *hostp; *cp != '\0'; cp++) {
499 if (*cp == '.')
500 ndots++;
501 }
Damien Miller38505592013-10-17 11:48:13 +1100502 if (ndots > options.canonicalize_max_dots) {
503 debug3("%s: not canonicalizing hostname \"%s\" (max dots %d)",
504 __func__, *hostp, options.canonicalize_max_dots);
Damien Miller0faf7472013-10-17 11:47:23 +1100505 return NULL;
506 }
507 /* Attempt each supplied suffix */
508 for (i = 0; i < options.num_canonical_domains; i++) {
Damien Miller0faf7472013-10-17 11:47:23 +1100509 xasprintf(&fullhost, "%s.%s.", *hostp,
510 options.canonical_domains[i]);
Damien Miller13f97b22014-02-24 15:57:55 +1100511 debug3("%s: attempting \"%s\" => \"%s\"", __func__,
512 *hostp, fullhost);
513 if ((addrs = resolve_host(fullhost, port, 0,
djm@openbsd.org90109022015-01-16 07:19:48 +0000514 newname, sizeof(newname))) == NULL) {
Damien Miller0faf7472013-10-17 11:47:23 +1100515 free(fullhost);
516 continue;
517 }
djm@openbsd.org5ee00632015-10-16 18:40:49 +0000518 found:
Damien Miller0faf7472013-10-17 11:47:23 +1100519 /* Remove trailing '.' */
520 fullhost[strlen(fullhost) - 1] = '\0';
521 /* Follow CNAME if requested */
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000522 if (!check_follow_cname(direct, &fullhost, newname)) {
Damien Miller38505592013-10-17 11:48:13 +1100523 debug("Canonicalized hostname \"%s\" => \"%s\"",
Damien Miller0faf7472013-10-17 11:47:23 +1100524 *hostp, fullhost);
525 }
526 free(*hostp);
527 *hostp = fullhost;
528 return addrs;
529 }
djm@openbsd.org5ee00632015-10-16 18:40:49 +0000530 notfound:
Damien Miller38505592013-10-17 11:48:13 +1100531 if (!options.canonicalize_fallback_local)
Damien Miller13f97b22014-02-24 15:57:55 +1100532 fatal("%s: Could not resolve host \"%s\"", __progname, *hostp);
533 debug2("%s: host %s not found in any suffix", __func__, *hostp);
Damien Miller0faf7472013-10-17 11:47:23 +1100534 return NULL;
535}
536
Damien Miller95def091999-11-25 00:26:21 +1100537/*
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000538 * Check the result of hostkey loading, ignoring some errors and
539 * fatal()ing for others.
540 */
541static void
542check_load(int r, const char *path, const char *message)
543{
544 switch (r) {
545 case 0:
546 break;
547 case SSH_ERR_INTERNAL_ERROR:
548 case SSH_ERR_ALLOC_FAIL:
549 fatal("load %s \"%s\": %s", message, path, ssh_err(r));
550 case SSH_ERR_SYSTEM_ERROR:
551 /* Ignore missing files */
552 if (errno == ENOENT)
553 break;
554 /* FALLTHROUGH */
555 default:
556 error("load %s \"%s\": %s", message, path, ssh_err(r));
557 break;
558 }
559}
560
561/*
Damien Miller13f97b22014-02-24 15:57:55 +1100562 * Read per-user configuration file. Ignore the system wide config
563 * file if the user specifies a config file on the command line.
564 */
565static void
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000566process_config_files(const char *host_name, struct passwd *pw, int final_pass,
567 int *want_final_pass)
Damien Miller13f97b22014-02-24 15:57:55 +1100568{
deraadt@openbsd.org087266e2015-01-20 23:14:00 +0000569 char buf[PATH_MAX];
Damien Miller13f97b22014-02-24 15:57:55 +1100570 int r;
571
572 if (config != NULL) {
573 if (strcasecmp(config, "none") != 0 &&
djm@openbsd.orgb7548b12017-10-23 05:08:00 +0000574 !read_config_file(config, pw, host, host_name, &options,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000575 SSHCONF_USERCONF | (final_pass ? SSHCONF_FINAL : 0),
576 want_final_pass))
Damien Miller13f97b22014-02-24 15:57:55 +1100577 fatal("Can't open user config file %.100s: "
578 "%.100s", config, strerror(errno));
579 } else {
580 r = snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir,
581 _PATH_SSH_USER_CONFFILE);
582 if (r > 0 && (size_t)r < sizeof(buf))
djm@openbsd.orgb7548b12017-10-23 05:08:00 +0000583 (void)read_config_file(buf, pw, host, host_name,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000584 &options, SSHCONF_CHECKPERM | SSHCONF_USERCONF |
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000585 (final_pass ? SSHCONF_FINAL : 0), want_final_pass);
Damien Miller13f97b22014-02-24 15:57:55 +1100586
587 /* Read systemwide configuration file after user config. */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000588 (void)read_config_file(_PATH_HOST_CONFIG_FILE, pw,
djm@openbsd.orgb7548b12017-10-23 05:08:00 +0000589 host, host_name, &options,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000590 final_pass ? SSHCONF_FINAL : 0, want_final_pass);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000591 }
592}
593
594/* Rewrite the port number in an addrinfo list of addresses */
595static void
596set_addrinfo_port(struct addrinfo *addrs, int port)
597{
598 struct addrinfo *addr;
599
600 for (addr = addrs; addr != NULL; addr = addr->ai_next) {
601 switch (addr->ai_family) {
602 case AF_INET:
603 ((struct sockaddr_in *)addr->ai_addr)->
604 sin_port = htons(port);
605 break;
606 case AF_INET6:
607 ((struct sockaddr_in6 *)addr->ai_addr)->
608 sin6_port = htons(port);
609 break;
610 }
Damien Miller13f97b22014-02-24 15:57:55 +1100611 }
612}
613
614/*
Damien Miller95def091999-11-25 00:26:21 +1100615 * Main program for the ssh client.
616 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000617int
618main(int ac, char **av)
619{
djm@openbsd.org95767262016-03-07 19:02:43 +0000620 struct ssh *ssh = NULL;
djm@openbsd.org643c2ad2017-08-12 06:46:01 +0000621 int i, r, opt, exit_status, use_syslog, direct, timeout_ms;
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000622 int was_addr, config_test = 0, opt_terminated = 0, want_final_pass = 0;
djm@openbsd.orgb7548b12017-10-23 05:08:00 +0000623 char *p, *cp, *line, *argv0, buf[PATH_MAX], *logfile;
624 char cname[NI_MAXHOST];
Damien Miller95def091999-11-25 00:26:21 +1100625 struct stat st;
Ben Lindstrom086cf212001-03-05 05:56:40 +0000626 struct passwd *pw;
Ben Lindstrom5ccf63a2001-09-24 20:00:10 +0000627 extern int optind, optreset;
Damien Miller4f8e6692001-07-14 13:22:53 +1000628 extern char *optarg;
Damien Miller7acefbb2014-07-18 14:11:24 +1000629 struct Forward fwd;
Damien Miller0faf7472013-10-17 11:47:23 +1100630 struct addrinfo *addrs = NULL;
dtucker@openbsd.org7f8e66f2020-01-23 10:24:29 +0000631 size_t n, len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000632
Darren Tuckerce321d82005-10-03 18:11:24 +1000633 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
634 sanitise_stdfd();
635
Damien Miller59d3d5b2003-08-22 09:34:41 +1000636 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000637
Damien Millerea2c1a42011-06-03 12:10:22 +1000638#ifndef HAVE_SETPROCTITLE
639 /* Prepare for later setproctitle emulation */
640 /* Save argv so it isn't clobbered by setproctitle() emulation */
641 saved_av = xcalloc(ac + 1, sizeof(*saved_av));
642 for (i = 0; i < ac; i++)
643 saved_av[i] = xstrdup(av[i]);
644 saved_av[i] = NULL;
645 compat_init_setproctitle(ac, av);
646 av = saved_av;
647#endif
648
Damien Miller42c5ec42018-11-23 10:40:06 +1100649 seed_rng();
650
Damien Miller5428f641999-11-25 11:54:57 +1100651 /*
Damien Miller00d9ae22010-08-17 01:59:31 +1000652 * Discard other fds that are hanging around. These can cause problem
653 * with backgrounded ssh processes started by ControlPersist.
654 */
655 closefrom(STDERR_FILENO + 1);
656
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000657 /* Get user data. */
dtucker@openbsd.orge655ee02018-07-27 05:34:42 +0000658 pw = getpwuid(getuid());
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000659 if (!pw) {
dtucker@openbsd.orge655ee02018-07-27 05:34:42 +0000660 logit("No user exists for uid %lu", (u_long)getuid());
Darren Tuckere9a9b712005-12-20 16:15:51 +1100661 exit(255);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000662 }
663 /* Take a copy of the returned structure. */
664 pw = pwcopy(pw);
665
Damien Miller5428f641999-11-25 11:54:57 +1100666 /*
Damien Miller5428f641999-11-25 11:54:57 +1100667 * Set our umask to something reasonable, as some files are created
668 * with the default umask. This will make them world-readable but
669 * writable only by the owner, which is ok for all files for which we
670 * don't set the modes explicitly.
671 */
Damien Miller95def091999-11-25 00:26:21 +1100672 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000673
Damien Millerdda78a02016-12-12 13:57:10 +1100674 msetlocale();
djm@openbsd.org65c6c6b2016-07-17 04:20:16 +0000675
Darren Tuckerd6173c02008-06-13 04:52:53 +1000676 /*
677 * Initialize option structure to indicate that no values have been
678 * set.
679 */
Damien Miller95def091999-11-25 00:26:21 +1100680 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000681
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000682 /*
683 * Prepare main ssh transport/connection structures
684 */
685 if ((ssh = ssh_alloc_session_state()) == NULL)
686 fatal("Couldn't allocate session state");
687 channel_init_channels(ssh);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000688
Damien Miller95def091999-11-25 00:26:21 +1100689 /* Parse command-line arguments. */
690 host = NULL;
Damien Millere272a5b2008-11-03 19:22:37 +1100691 use_syslog = 0;
Damien Miller03d4d7e2013-04-23 15:21:06 +1000692 logfile = NULL;
Darren Tucker72efd742009-06-21 17:48:00 +1000693 argv0 = av[0];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000694
Damien Miller2ecb6bd2006-03-15 12:03:53 +1100695 again:
Darren Tuckerd6173c02008-06-13 04:52:53 +1000696 while ((opt = getopt(ac, av, "1246ab:c:e:fgi:kl:m:no:p:qstvx"
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000697 "AB:CD:E:F:GI:J:KL:MNO:PQ:R:S:TVw:W:XYy")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100698 switch (opt) {
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000699 case '1':
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000700 fatal("SSH protocol v.1 is no longer supported");
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000701 break;
Damien Miller4af51302000-04-16 11:18:38 +1000702 case '2':
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000703 /* Ignored */
Damien Miller4af51302000-04-16 11:18:38 +1000704 break;
Damien Miller34132e52000-01-14 15:45:46 +1100705 case '4':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000706 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100707 break;
Damien Miller34132e52000-01-14 15:45:46 +1100708 case '6':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000709 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100710 break;
Damien Miller95def091999-11-25 00:26:21 +1100711 case 'n':
712 stdin_null_flag = 1;
713 break;
Damien Miller95def091999-11-25 00:26:21 +1100714 case 'f':
715 fork_after_authentication_flag = 1;
716 stdin_null_flag = 1;
717 break;
Damien Miller95def091999-11-25 00:26:21 +1100718 case 'x':
719 options.forward_x11 = 0;
720 break;
Damien Miller95def091999-11-25 00:26:21 +1100721 case 'X':
722 options.forward_x11 = 1;
723 break;
Damien Millere272a5b2008-11-03 19:22:37 +1100724 case 'y':
725 use_syslog = 1;
726 break;
Damien Miller03d4d7e2013-04-23 15:21:06 +1000727 case 'E':
dtucker@openbsd.org4f7cc2f2015-09-04 08:21:47 +0000728 logfile = optarg;
Damien Miller03d4d7e2013-04-23 15:21:06 +1000729 break;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000730 case 'G':
731 config_test = 1;
732 break;
Darren Tucker0a118da2003-10-15 15:54:32 +1000733 case 'Y':
734 options.forward_x11 = 1;
735 options.forward_x11_trusted = 1;
736 break;
Damien Miller95def091999-11-25 00:26:21 +1100737 case 'g':
Damien Miller7acefbb2014-07-18 14:11:24 +1000738 options.fwd_opts.gateway_ports = 1;
Damien Miller95def091999-11-25 00:26:21 +1100739 break;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100740 case 'O':
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +0000741 if (options.stdio_forward_host != NULL)
Damien Millere1537f92010-01-26 13:26:22 +1100742 fatal("Cannot specify multiplexing "
743 "command with -W");
744 else if (muxclient_command != 0)
745 fatal("Multiplexing command already specified");
Darren Tucker7ebfc102004-11-07 20:06:19 +1100746 if (strcmp(optarg, "check") == 0)
Damien Millerb1cbfa22008-05-19 16:00:08 +1000747 muxclient_command = SSHMUX_COMMAND_ALIVE_CHECK;
Damien Miller388f6fc2010-05-21 14:57:35 +1000748 else if (strcmp(optarg, "forward") == 0)
749 muxclient_command = SSHMUX_COMMAND_FORWARD;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100750 else if (strcmp(optarg, "exit") == 0)
Damien Millerb1cbfa22008-05-19 16:00:08 +1000751 muxclient_command = SSHMUX_COMMAND_TERMINATE;
Damien Miller6c3eec72011-05-05 14:16:22 +1000752 else if (strcmp(optarg, "stop") == 0)
753 muxclient_command = SSHMUX_COMMAND_STOP;
Damien Millerf6dff7c2011-09-22 21:38:52 +1000754 else if (strcmp(optarg, "cancel") == 0)
755 muxclient_command = SSHMUX_COMMAND_CANCEL_FWD;
markus@openbsd.org8d057842016-09-30 09:19:13 +0000756 else if (strcmp(optarg, "proxy") == 0)
757 muxclient_command = SSHMUX_COMMAND_PROXY;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100758 else
759 fatal("Invalid multiplex command.");
760 break;
Damien Miller147bba32002-09-04 16:46:06 +1000761 case 'P': /* deprecated */
Damien Miller95def091999-11-25 00:26:21 +1100762 break;
Damien Millerd937dc02013-12-05 10:19:54 +1100763 case 'Q':
Damien Millerea111192013-04-23 19:24:32 +1000764 cp = NULL;
dtucker@openbsd.orgd4d9e1d2020-02-07 03:54:44 +0000765 if (strcmp(optarg, "cipher") == 0 ||
766 strcasecmp(optarg, "Ciphers") == 0)
Damien Miller0fde8ac2013-11-21 14:12:23 +1100767 cp = cipher_alg_list('\n', 0);
Damien Millerd937dc02013-12-05 10:19:54 +1100768 else if (strcmp(optarg, "cipher-auth") == 0)
Damien Miller0fde8ac2013-11-21 14:12:23 +1100769 cp = cipher_alg_list('\n', 1);
dtucker@openbsd.orgd4d9e1d2020-02-07 03:54:44 +0000770 else if (strcmp(optarg, "mac") == 0 ||
771 strcasecmp(optarg, "MACs") == 0)
Damien Miller690d9892013-11-08 12:16:49 +1100772 cp = mac_alg_list('\n');
dtucker@openbsd.orgd4d9e1d2020-02-07 03:54:44 +0000773 else if (strcmp(optarg, "kex") == 0 ||
774 strcasecmp(optarg, "KexAlgorithms") == 0)
Damien Miller690d9892013-11-08 12:16:49 +1100775 cp = kex_alg_list('\n');
Damien Millerd937dc02013-12-05 10:19:54 +1100776 else if (strcmp(optarg, "key") == 0)
djm@openbsd.org183ba552017-03-10 04:07:20 +0000777 cp = sshkey_alg_list(0, 0, 0, '\n');
Damien Miller5be9d9e2013-12-07 11:24:01 +1100778 else if (strcmp(optarg, "key-cert") == 0)
djm@openbsd.org183ba552017-03-10 04:07:20 +0000779 cp = sshkey_alg_list(1, 0, 0, '\n');
Damien Miller5be9d9e2013-12-07 11:24:01 +1100780 else if (strcmp(optarg, "key-plain") == 0)
djm@openbsd.org183ba552017-03-10 04:07:20 +0000781 cp = sshkey_alg_list(0, 1, 0, '\n');
dtucker@openbsd.orgd4d9e1d2020-02-07 03:54:44 +0000782 else if (strcmp(optarg, "key-sig") == 0 ||
783 strcasecmp(optarg, "PubkeyAcceptedKeyTypes") == 0 ||
784 strcasecmp(optarg, "HostKeyAlgorithms") == 0 ||
785 strcasecmp(optarg, "HostbasedKeyTypes") == 0 ||
786 strcasecmp(optarg, "HostbasedAcceptedKeyTypes") == 0)
787 cp = sshkey_alg_list(0, 0, 1, '\n');
djm@openbsd.org357128a2018-09-12 01:30:10 +0000788 else if (strcmp(optarg, "sig") == 0)
djm@openbsd.orgaa083aa2018-09-20 03:31:49 +0000789 cp = sshkey_alg_list(0, 1, 1, '\n');
djm@openbsd.org357128a2018-09-12 01:30:10 +0000790 else if (strcmp(optarg, "protocol-version") == 0)
djm@openbsd.org68d2dfc2015-03-03 06:48:58 +0000791 cp = xstrdup("2");
dtucker@openbsd.org7f8e66f2020-01-23 10:24:29 +0000792 else if (strcmp(optarg, "compression") == 0) {
793 cp = xstrdup(compression_alg_list(0));
794 len = strlen(cp);
795 for (n = 0; n < len; n++)
796 if (cp[n] == ',')
797 cp[n] = '\n';
798 } else if (strcmp(optarg, "help") == 0) {
djm@openbsd.org357128a2018-09-12 01:30:10 +0000799 cp = xstrdup(
dtucker@openbsd.org7f8e66f2020-01-23 10:24:29 +0000800 "cipher\ncipher-auth\ncompression\nkex\n"
dtucker@openbsd.orgd4d9e1d2020-02-07 03:54:44 +0000801 "key\nkey-cert\nkey-plain\nkey-sig\nmac\n"
djm@openbsd.org357128a2018-09-12 01:30:10 +0000802 "protocol-version\nsig");
djm@openbsd.org68d2dfc2015-03-03 06:48:58 +0000803 }
Damien Millerea111192013-04-23 19:24:32 +1000804 if (cp == NULL)
805 fatal("Unsupported query \"%s\"", optarg);
806 printf("%s\n", cp);
807 free(cp);
808 exit(0);
809 break;
Damien Miller95def091999-11-25 00:26:21 +1100810 case 'a':
811 options.forward_agent = 0;
812 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000813 case 'A':
814 options.forward_agent = 1;
815 break;
Damien Miller95def091999-11-25 00:26:21 +1100816 case 'k':
Damien Millere0113cc2003-11-24 13:10:09 +1100817 options.gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100818 break;
Darren Tucker415bddc2007-06-12 23:43:16 +1000819 case 'K':
820 options.gss_authentication = 1;
821 options.gss_deleg_creds = 1;
822 break;
Damien Miller95def091999-11-25 00:26:21 +1100823 case 'i':
dtucker@openbsd.orge655ee02018-07-27 05:34:42 +0000824 p = tilde_expand_filename(optarg, getuid());
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000825 if (stat(p, &st) == -1)
Damien Millerf4614452001-07-14 12:18:10 +1000826 fprintf(stderr, "Warning: Identity file %s "
dtucker@openbsd.org03239c12015-10-25 23:42:00 +0000827 "not accessible: %s.\n", p,
Damien Miller3eb48b62005-03-01 21:15:46 +1100828 strerror(errno));
dtucker@openbsd.org03239c12015-10-25 23:42:00 +0000829 else
830 add_identity_file(&options, NULL, p, 1);
831 free(p);
Damien Miller95def091999-11-25 00:26:21 +1100832 break;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000833 case 'I':
Damien Miller7ea845e2010-02-12 09:21:02 +1100834#ifdef ENABLE_PKCS11
dtucker@openbsd.org4f7cc2f2015-09-04 08:21:47 +0000835 free(options.pkcs11_provider);
Damien Miller7ea845e2010-02-12 09:21:02 +1100836 options.pkcs11_provider = xstrdup(optarg);
Ben Lindstrombcc18082001-08-06 21:59:25 +0000837#else
Damien Miller7ea845e2010-02-12 09:21:02 +1100838 fprintf(stderr, "no support for PKCS#11.\n");
Ben Lindstrombcc18082001-08-06 21:59:25 +0000839#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000840 break;
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000841 case 'J':
djm@openbsd.orgcb8f5652019-06-14 04:13:58 +0000842 if (options.jump_host != NULL) {
843 fatal("Only a single -J option is permitted "
844 "(use commas to separate multiple "
845 "jump hops)");
846 }
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000847 if (options.proxy_command != NULL)
848 fatal("Cannot specify -J with ProxyCommand");
849 if (parse_jump(optarg, &options, 1) == -1)
850 fatal("Invalid -J argument");
851 options.proxy_command = xstrdup("none");
852 break;
Damien Miller95def091999-11-25 00:26:21 +1100853 case 't':
Damien Miller21771e22011-05-15 08:45:50 +1000854 if (options.request_tty == REQUEST_TTY_YES)
855 options.request_tty = REQUEST_TTY_FORCE;
856 else
857 options.request_tty = REQUEST_TTY_YES;
Damien Miller95def091999-11-25 00:26:21 +1100858 break;
Damien Miller95def091999-11-25 00:26:21 +1100859 case 'v':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000860 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100861 debug_flag = 1;
862 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000863 } else {
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000864 if (options.log_level < SYSLOG_LEVEL_DEBUG3) {
865 debug_flag++;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000866 options.log_level++;
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000867 }
Darren Tuckere98dfa32003-07-19 19:54:31 +1000868 }
Damien Miller03d4d7e2013-04-23 15:21:06 +1000869 break;
Damien Miller95def091999-11-25 00:26:21 +1100870 case 'V':
Damien Miller0c889cd2004-03-22 09:36:00 +1100871 fprintf(stderr, "%s, %s\n",
Damien Miller1f0311c2014-05-15 14:24:09 +1000872 SSH_RELEASE,
873#ifdef WITH_OPENSSL
djm@openbsd.orga65784c2018-10-23 05:56:35 +0000874 OpenSSL_version(OPENSSL_VERSION)
Damien Miller1f0311c2014-05-15 14:24:09 +1000875#else
876 "without OpenSSL"
877#endif
878 );
Damien Miller95def091999-11-25 00:26:21 +1100879 if (opt == 'V')
880 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100881 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100882 case 'w':
Damien Miller7b58e802005-12-13 19:33:19 +1100883 if (options.tun_open == -1)
884 options.tun_open = SSH_TUNMODE_DEFAULT;
Damien Millerd27b9472005-12-13 19:29:02 +1100885 options.tun_local = a2tun(optarg, &options.tun_remote);
Damien Miller7b58e802005-12-13 19:33:19 +1100886 if (options.tun_local == SSH_TUNID_ERR) {
Darren Tuckerd6173c02008-06-13 04:52:53 +1000887 fprintf(stderr,
888 "Bad tun device '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100889 exit(255);
Damien Millerd27b9472005-12-13 19:29:02 +1100890 }
891 break;
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100892 case 'W':
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +0000893 if (options.stdio_forward_host != NULL)
Damien Millere1537f92010-01-26 13:26:22 +1100894 fatal("stdio forward already specified");
895 if (muxclient_command != 0)
896 fatal("Cannot specify stdio forward with -O");
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100897 if (parse_forward(&fwd, optarg, 1, 0)) {
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +0000898 options.stdio_forward_host = fwd.listen_host;
899 options.stdio_forward_port = fwd.listen_port;
Darren Tuckera627d422013-06-02 07:31:17 +1000900 free(fwd.connect_host);
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100901 } else {
902 fprintf(stderr,
903 "Bad stdio forwarding specification '%s'\n",
904 optarg);
905 exit(255);
906 }
Damien Miller21771e22011-05-15 08:45:50 +1000907 options.request_tty = REQUEST_TTY_NO;
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100908 no_shell_flag = 1;
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100909 break;
Damien Miller95def091999-11-25 00:26:21 +1100910 case 'q':
911 options.log_level = SYSLOG_LEVEL_QUIET;
912 break;
Damien Miller95def091999-11-25 00:26:21 +1100913 case 'e':
914 if (optarg[0] == '^' && optarg[2] == 0 &&
Damien Millerf4614452001-07-14 12:18:10 +1000915 (u_char) optarg[1] >= 64 &&
916 (u_char) optarg[1] < 128)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000917 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100918 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000919 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100920 else if (strcmp(optarg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000921 options.escape_char = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100922 else {
Damien Millerf4614452001-07-14 12:18:10 +1000923 fprintf(stderr, "Bad escape character '%s'.\n",
924 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100925 exit(255);
Damien Miller95def091999-11-25 00:26:21 +1100926 }
927 break;
Damien Miller95def091999-11-25 00:26:21 +1100928 case 'c':
naddy@openbsd.org91a21352019-09-06 14:45:34 +0000929 if (!ciphers_valid(*optarg == '+' || *optarg == '^' ?
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000930 optarg + 1 : optarg)) {
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000931 fprintf(stderr, "Unknown cipher type '%s'\n",
932 optarg);
933 exit(255);
934 }
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000935 free(options.ciphers);
936 options.ciphers = xstrdup(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100937 break;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000938 case 'm':
dtucker@openbsd.org4f7cc2f2015-09-04 08:21:47 +0000939 if (mac_valid(optarg)) {
940 free(options.macs);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000941 options.macs = xstrdup(optarg);
dtucker@openbsd.org4f7cc2f2015-09-04 08:21:47 +0000942 } else {
Damien Millerf4614452001-07-14 12:18:10 +1000943 fprintf(stderr, "Unknown mac type '%s'\n",
944 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100945 exit(255);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000946 }
947 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000948 case 'M':
Damien Millerd14b1e72005-06-16 13:19:41 +1000949 if (options.control_master == SSHCTL_MASTER_YES)
950 options.control_master = SSHCTL_MASTER_ASK;
951 else
952 options.control_master = SSHCTL_MASTER_YES;
Damien Miller0e220db2004-06-15 10:34:08 +1000953 break;
Damien Miller95def091999-11-25 00:26:21 +1100954 case 'p':
millert@openbsd.org887669e2017-10-21 23:06:24 +0000955 if (options.port == -1) {
956 options.port = a2port(optarg);
957 if (options.port <= 0) {
958 fprintf(stderr, "Bad port '%s'\n",
959 optarg);
960 exit(255);
961 }
Ben Lindstrom146edb92001-04-11 23:06:28 +0000962 }
Damien Miller95def091999-11-25 00:26:21 +1100963 break;
Damien Miller95def091999-11-25 00:26:21 +1100964 case 'l':
millert@openbsd.org887669e2017-10-21 23:06:24 +0000965 if (options.user == NULL)
966 options.user = optarg;
Damien Miller95def091999-11-25 00:26:21 +1100967 break;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000968
Damien Miller95def091999-11-25 00:26:21 +1100969 case 'L':
Damien Miller4bf648f2009-02-14 16:28:21 +1100970 if (parse_forward(&fwd, optarg, 0, 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +1100971 add_local_forward(&options, &fwd);
972 else {
Damien Millerf4614452001-07-14 12:18:10 +1000973 fprintf(stderr,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100974 "Bad local forwarding specification '%s'\n",
Damien Millerf4614452001-07-14 12:18:10 +1000975 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100976 exit(255);
Ben Lindstrom1a174712001-09-12 17:56:15 +0000977 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100978 break;
979
980 case 'R':
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000981 if (parse_forward(&fwd, optarg, 0, 1) ||
982 parse_forward(&fwd, optarg, 1, 1)) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100983 add_remote_forward(&options, &fwd);
984 } else {
985 fprintf(stderr,
986 "Bad remote forwarding specification "
987 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100988 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100989 }
Damien Miller95def091999-11-25 00:26:21 +1100990 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000991
992 case 'D':
Damien Miller4bf648f2009-02-14 16:28:21 +1100993 if (parse_forward(&fwd, optarg, 1, 0)) {
Damien Millera699d952008-11-03 19:27:34 +1100994 add_local_forward(&options, &fwd);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100995 } else {
Damien Millera699d952008-11-03 19:27:34 +1100996 fprintf(stderr,
997 "Bad dynamic forwarding specification "
998 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100999 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +00001000 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001001 break;
1002
Damien Miller95def091999-11-25 00:26:21 +11001003 case 'C':
dtucker@openbsd.org7f8e66f2020-01-23 10:24:29 +00001004#ifdef WITH_ZLIB
Damien Miller95def091999-11-25 00:26:21 +11001005 options.compression = 1;
dtucker@openbsd.org7f8e66f2020-01-23 10:24:29 +00001006#else
1007 error("Compression not supported, disabling.");
1008#endif
Damien Miller95def091999-11-25 00:26:21 +11001009 break;
Damien Miller1383bd82000-04-06 12:32:37 +10001010 case 'N':
1011 no_shell_flag = 1;
Damien Miller21771e22011-05-15 08:45:50 +10001012 options.request_tty = REQUEST_TTY_NO;
Damien Miller1383bd82000-04-06 12:32:37 +10001013 break;
Damien Miller1383bd82000-04-06 12:32:37 +10001014 case 'T':
Damien Miller21771e22011-05-15 08:45:50 +10001015 options.request_tty = REQUEST_TTY_NO;
Damien Miller1383bd82000-04-06 12:32:37 +10001016 break;
Damien Miller95def091999-11-25 00:26:21 +11001017 case 'o':
Damien Miller9836cf82003-12-17 16:30:06 +11001018 line = xstrdup(optarg);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001019 if (process_config_line(&options, pw,
1020 host ? host : "", host ? host : "", line,
1021 "command-line", 0, NULL, SSHCONF_USERCONF) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +11001022 exit(255);
Darren Tuckera627d422013-06-02 07:31:17 +10001023 free(line);
Damien Miller95def091999-11-25 00:26:21 +11001024 break;
Damien Miller832562e2001-01-30 09:30:01 +11001025 case 's':
1026 subsystem_flag = 1;
1027 break;
Damien Miller0e220db2004-06-15 10:34:08 +10001028 case 'S':
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00001029 free(options.control_path);
Damien Miller0e220db2004-06-15 10:34:08 +10001030 options.control_path = xstrdup(optarg);
Damien Miller0e220db2004-06-15 10:34:08 +10001031 break;
Ben Lindstrome0f88042001-04-30 13:06:24 +00001032 case 'b':
1033 options.bind_address = optarg;
1034 break;
djm@openbsd.orgac2e3022018-02-23 02:34:33 +00001035 case 'B':
1036 options.bind_interface = optarg;
1037 break;
Ben Lindstrom14f31ab2001-09-12 17:48:04 +00001038 case 'F':
1039 config = optarg;
1040 break;
Damien Miller95def091999-11-25 00:26:21 +11001041 default:
1042 usage();
1043 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001044 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001045
djm@openbsd.org643c2ad2017-08-12 06:46:01 +00001046 if (optind > 1 && strcmp(av[optind - 1], "--") == 0)
1047 opt_terminated = 1;
1048
Damien Millerf4614452001-07-14 12:18:10 +10001049 ac -= optind;
1050 av += optind;
1051
Darren Tuckerb8c884a2010-01-08 18:53:43 +11001052 if (ac > 0 && !host) {
millert@openbsd.org887669e2017-10-21 23:06:24 +00001053 int tport;
1054 char *tuser;
1055 switch (parse_ssh_uri(*av, &tuser, &host, &tport)) {
1056 case -1:
1057 usage();
1058 break;
1059 case 0:
1060 if (options.user == NULL) {
1061 options.user = tuser;
1062 tuser = NULL;
1063 }
1064 free(tuser);
1065 if (options.port == -1 && tport != -1)
1066 options.port = tport;
1067 break;
1068 default:
Damien Millerf4614452001-07-14 12:18:10 +10001069 p = xstrdup(*av);
Ben Lindstromc276c122002-12-23 02:14:51 +00001070 cp = strrchr(p, '@');
millert@openbsd.org887669e2017-10-21 23:06:24 +00001071 if (cp != NULL) {
1072 if (cp == p)
1073 usage();
1074 if (options.user == NULL) {
1075 options.user = p;
1076 p = NULL;
1077 }
1078 *cp++ = '\0';
1079 host = xstrdup(cp);
1080 free(p);
1081 } else
1082 host = p;
1083 break;
1084 }
djm@openbsd.org643c2ad2017-08-12 06:46:01 +00001085 if (ac > 1 && !opt_terminated) {
Ben Lindstromb9fa6912002-12-23 02:24:54 +00001086 optind = optreset = 1;
Damien Millerf4614452001-07-14 12:18:10 +10001087 goto again;
1088 }
Ben Lindstromb9fa6912002-12-23 02:24:54 +00001089 ac--, av++;
Damien Millerf4614452001-07-14 12:18:10 +10001090 }
1091
Damien Miller95def091999-11-25 00:26:21 +11001092 /* Check that we got a host name. */
1093 if (!host)
1094 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001095
Damien Miller0faf7472013-10-17 11:47:23 +11001096 host_arg = xstrdup(host);
1097
Damien Miller95def091999-11-25 00:26:21 +11001098 /* Initialize the command to execute on remote host. */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001099 if ((command = sshbuf_new()) == NULL)
1100 fatal("sshbuf_new failed");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001101
Damien Miller5428f641999-11-25 11:54:57 +11001102 /*
1103 * Save the command to execute on the remote host in a buffer. There
1104 * is no limit on the length of the command, except by the maximum
1105 * packet size. Also sets the tty flag if there is no command.
1106 */
Damien Millerf4614452001-07-14 12:18:10 +10001107 if (!ac) {
Damien Miller95def091999-11-25 00:26:21 +11001108 /* No command specified - execute shell on a tty. */
Damien Miller832562e2001-01-30 09:30:01 +11001109 if (subsystem_flag) {
Damien Millerf4614452001-07-14 12:18:10 +10001110 fprintf(stderr,
1111 "You must specify a subsystem to invoke.\n");
Damien Miller832562e2001-01-30 09:30:01 +11001112 usage();
1113 }
Damien Miller95def091999-11-25 00:26:21 +11001114 } else {
Damien Millerf4614452001-07-14 12:18:10 +10001115 /* A command has been specified. Store it into the buffer. */
1116 for (i = 0; i < ac; i++) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001117 if ((r = sshbuf_putf(command, "%s%s",
1118 i ? " " : "", av[i])) != 0)
1119 fatal("%s: buffer error: %s",
1120 __func__, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +11001121 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001122 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001123
Ben Lindstrom8a432f52001-03-05 07:24:46 +00001124 /*
1125 * Initialize "log" output. Since we are the client all output
Damien Miller03d4d7e2013-04-23 15:21:06 +10001126 * goes to stderr unless otherwise specified by -y or -E.
Ben Lindstrom8a432f52001-03-05 07:24:46 +00001127 */
Damien Miller03d4d7e2013-04-23 15:21:06 +10001128 if (use_syslog && logfile != NULL)
1129 fatal("Can't specify both -y and -E");
dtucker@openbsd.org4f7cc2f2015-09-04 08:21:47 +00001130 if (logfile != NULL)
Damien Miller03d4d7e2013-04-23 15:21:06 +10001131 log_redirect_stderr_to(logfile);
Darren Tucker72efd742009-06-21 17:48:00 +10001132 log_init(argv0,
djm@openbsd.org@openbsd.orgdbe06622017-10-27 01:57:06 +00001133 options.log_level == SYSLOG_LEVEL_NOT_SET ?
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001134 SYSLOG_LEVEL_INFO : options.log_level,
djm@openbsd.org@openbsd.orgdbe06622017-10-27 01:57:06 +00001135 options.log_facility == SYSLOG_FACILITY_NOT_SET ?
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001136 SYSLOG_FACILITY_USER : options.log_facility,
1137 !use_syslog);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001138
Damien Miller03d4d7e2013-04-23 15:21:06 +10001139 if (debug_flag)
Damien Miller1f0311c2014-05-15 14:24:09 +10001140 logit("%s, %s", SSH_RELEASE,
1141#ifdef WITH_OPENSSL
djm@openbsd.orga65784c2018-10-23 05:56:35 +00001142 OpenSSL_version(OPENSSL_VERSION)
Damien Miller1f0311c2014-05-15 14:24:09 +10001143#else
1144 "without OpenSSL"
1145#endif
1146 );
Damien Miller03d4d7e2013-04-23 15:21:06 +10001147
Damien Miller13f97b22014-02-24 15:57:55 +11001148 /* Parse the configuration files */
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001149 process_config_files(host_arg, pw, 0, &want_final_pass);
1150 if (want_final_pass)
1151 debug("configuration requests final Match pass");
Ben Lindstrom83f07d12001-10-03 17:22:29 +00001152
Damien Miller13f97b22014-02-24 15:57:55 +11001153 /* Hostname canonicalisation needs a few options filled. */
1154 fill_default_options_for_canonicalization(&options);
1155
1156 /* If the user has replaced the hostname then take it into use now */
1157 if (options.hostname != NULL) {
1158 /* NB. Please keep in sync with readconf.c:match_cfg_line() */
1159 cp = percent_expand(options.hostname,
1160 "h", host, (char *)NULL);
1161 free(host);
1162 host = cp;
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001163 free(options.hostname);
1164 options.hostname = xstrdup(host);
Damien Miller13f97b22014-02-24 15:57:55 +11001165 }
1166
djm@openbsd.orgfc21ea92018-01-23 05:06:25 +00001167 /* Don't lowercase addresses, they will be explicitly canonicalised */
1168 if ((was_addr = is_addr(host)) == 0)
1169 lowercase(host);
1170
1171 /*
1172 * Try to canonicalize if requested by configuration or the
1173 * hostname is an address.
1174 */
1175 if (options.canonicalize_hostname != SSH_CANONICALISE_NO || was_addr)
Damien Miller13f97b22014-02-24 15:57:55 +11001176 addrs = resolve_canonicalize(&host, options.port);
1177
1178 /*
Damien Miller08b57c62014-02-27 10:17:13 +11001179 * If CanonicalizePermittedCNAMEs have been specified but
1180 * other canonicalization did not happen (by not being requested
1181 * or by failing with fallback) then the hostname may still be changed
djm@openbsd.org@openbsd.orgdbe06622017-10-27 01:57:06 +00001182 * as a result of CNAME following.
Damien Miller08b57c62014-02-27 10:17:13 +11001183 *
1184 * Try to resolve the bare hostname name using the system resolver's
1185 * usual search rules and then apply the CNAME follow rules.
1186 *
1187 * Skip the lookup if a ProxyCommand is being used unless the user
1188 * has specifically requested canonicalisation for this case via
1189 * CanonicalizeHostname=always
Damien Miller13f97b22014-02-24 15:57:55 +11001190 */
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001191 direct = option_clear_or_none(options.proxy_command) &&
1192 options.jump_host == NULL;
1193 if (addrs == NULL && options.num_permitted_cnames != 0 && (direct ||
1194 options.canonicalize_hostname == SSH_CANONICALISE_ALWAYS)) {
Damien Miller19439e92014-07-02 15:28:40 +10001195 if ((addrs = resolve_host(host, options.port,
djm@openbsd.org383a33d2018-09-21 03:11:36 +00001196 direct, cname, sizeof(cname))) == NULL) {
Damien Miller19439e92014-07-02 15:28:40 +10001197 /* Don't fatal proxied host names not in the DNS */
djm@openbsd.org383a33d2018-09-21 03:11:36 +00001198 if (direct)
Damien Miller19439e92014-07-02 15:28:40 +10001199 cleanup_exit(255); /* logged in resolve_host */
1200 } else
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001201 check_follow_cname(direct, &host, cname);
Damien Miller13f97b22014-02-24 15:57:55 +11001202 }
1203
1204 /*
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001205 * If canonicalisation is enabled then re-parse the configuration
1206 * files as new stanzas may match.
Damien Miller13f97b22014-02-24 15:57:55 +11001207 */
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001208 if (options.canonicalize_hostname != 0 && !want_final_pass) {
1209 debug("hostname canonicalisation enabled, "
1210 "will re-parse configuration");
1211 want_final_pass = 1;
1212 }
1213
1214 if (want_final_pass) {
1215 debug("re-parsing configuration");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001216 free(options.hostname);
1217 options.hostname = xstrdup(host);
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001218 process_config_files(host_arg, pw, 1, NULL);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001219 /*
1220 * Address resolution happens early with canonicalisation
1221 * enabled and the port number may have changed since, so
1222 * reset it in address list
1223 */
1224 if (addrs != NULL && options.port > 0)
1225 set_addrinfo_port(addrs, options.port);
Ben Lindstrom14f31ab2001-09-12 17:48:04 +00001226 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001227
Damien Miller95def091999-11-25 00:26:21 +11001228 /* Fill configuration defaults. */
1229 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001230
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001231 /*
1232 * If ProxyJump option specified, then construct a ProxyCommand now.
1233 */
1234 if (options.jump_host != NULL) {
1235 char port_s[8];
djm@openbsd.orgd8748b92018-06-01 03:11:49 +00001236 const char *sshbin = argv0;
dtucker@openbsd.orgde1f3562020-02-18 08:49:49 +00001237 int port = options.port, jumpport = options.jump_port;
1238
1239 if (port <= 0)
1240 port = default_ssh_port();
1241 if (jumpport <= 0)
1242 jumpport = default_ssh_port();
1243 if (strcmp(options.jump_host, host) == 0 && port == jumpport)
1244 fatal("jumphost loop via %s", options.jump_host);
djm@openbsd.orgd8748b92018-06-01 03:11:49 +00001245
1246 /*
1247 * Try to use SSH indicated by argv[0], but fall back to
1248 * "ssh" if it appears unavailable.
1249 */
1250 if (strchr(argv0, '/') != NULL && access(argv0, X_OK) != 0)
1251 sshbin = "ssh";
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001252
1253 /* Consistency check */
1254 if (options.proxy_command != NULL)
1255 fatal("inconsistent options: ProxyCommand+ProxyJump");
1256 /* Never use FD passing for ProxyJump */
1257 options.proxy_use_fdpass = 0;
1258 snprintf(port_s, sizeof(port_s), "%d", options.jump_port);
1259 xasprintf(&options.proxy_command,
djm@openbsd.orgd8748b92018-06-01 03:11:49 +00001260 "%s%s%s%s%s%s%s%s%s%s%.*s -W '[%%h]:%%p' %s",
1261 sshbin,
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001262 /* Optional "-l user" argument if jump_user set */
1263 options.jump_user == NULL ? "" : " -l ",
1264 options.jump_user == NULL ? "" : options.jump_user,
1265 /* Optional "-p port" argument if jump_port set */
1266 options.jump_port <= 0 ? "" : " -p ",
1267 options.jump_port <= 0 ? "" : port_s,
1268 /* Optional additional jump hosts ",..." */
1269 options.jump_extra == NULL ? "" : " -J ",
1270 options.jump_extra == NULL ? "" : options.jump_extra,
1271 /* Optional "-F" argumment if -F specified */
1272 config == NULL ? "" : " -F ",
1273 config == NULL ? "" : config,
1274 /* Optional "-v" arguments if -v set */
1275 debug_flag ? " -" : "",
1276 debug_flag, "vvv",
1277 /* Mandatory hostname */
1278 options.jump_host);
1279 debug("Setting implicit ProxyCommand from ProxyJump: %s",
1280 options.proxy_command);
1281 }
1282
Damien Miller13f97b22014-02-24 15:57:55 +11001283 if (options.port == 0)
1284 options.port = default_ssh_port();
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001285 channel_set_af(ssh, options.address_family);
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001286
Damien Millere9fc72e2013-10-15 12:14:12 +11001287 /* Tidy and check options */
1288 if (options.host_key_alias != NULL)
1289 lowercase(options.host_key_alias);
1290 if (options.proxy_command != NULL &&
1291 strcmp(options.proxy_command, "-") == 0 &&
1292 options.proxy_use_fdpass)
1293 fatal("ProxyCommand=- and ProxyUseFDPass are incompatible");
markus@openbsd.orgda222162020-01-27 20:51:32 +00001294 if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK) {
1295 if (options.control_persist && options.control_path != NULL) {
1296 debug("UpdateHostKeys=ask is incompatible with "
1297 "ControlPersist; disabling");
1298 options.update_hostkeys = 0;
1299 } else if (sshbuf_len(command) != 0 ||
1300 options.remote_command != NULL ||
1301 options.request_tty == REQUEST_TTY_NO) {
1302 debug("UpdateHostKeys=ask is incompatible with "
1303 "remote command execution; disabling");
1304 options.update_hostkeys = 0;
djm@openbsd.org156bef32020-01-28 07:24:15 +00001305 } else if (options.log_level < SYSLOG_LEVEL_INFO) {
1306 /* no point logging anything; user won't see it */
1307 options.update_hostkeys = 0;
markus@openbsd.orgda222162020-01-27 20:51:32 +00001308 }
djm@openbsd.org44732de2015-02-20 22:17:21 +00001309 }
djm@openbsd.org88b6fcd2015-11-19 08:23:27 +00001310 if (options.connection_attempts <= 0)
1311 fatal("Invalid number of ConnectionAttempts");
Damien Millere9fc72e2013-10-15 12:14:12 +11001312
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001313 if (sshbuf_len(command) != 0 && options.remote_command != NULL)
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001314 fatal("Cannot execute command-line and remote command.");
1315
1316 /* Cannot fork to background if no command. */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001317 if (fork_after_authentication_flag && sshbuf_len(command) == 0 &&
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001318 options.remote_command == NULL && !no_shell_flag)
1319 fatal("Cannot fork into background without a command "
1320 "to execute.");
1321
Damien Miller95def091999-11-25 00:26:21 +11001322 /* reinit */
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001323 log_init(argv0, options.log_level, options.log_facility, !use_syslog);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001324
Damien Millerfff9f092012-07-06 13:45:01 +10001325 if (options.request_tty == REQUEST_TTY_YES ||
1326 options.request_tty == REQUEST_TTY_FORCE)
1327 tty_flag = 1;
1328
1329 /* Allocate a tty by default if no command specified. */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001330 if (sshbuf_len(command) == 0 && options.remote_command == NULL)
Damien Millerfff9f092012-07-06 13:45:01 +10001331 tty_flag = options.request_tty != REQUEST_TTY_NO;
1332
1333 /* Force no tty */
markus@openbsd.org8d057842016-09-30 09:19:13 +00001334 if (options.request_tty == REQUEST_TTY_NO ||
1335 (muxclient_command && muxclient_command != SSHMUX_COMMAND_PROXY))
Damien Millerfff9f092012-07-06 13:45:01 +10001336 tty_flag = 0;
1337 /* Do not allocate a tty if stdin is not a tty. */
1338 if ((!isatty(fileno(stdin)) || stdin_null_flag) &&
1339 options.request_tty != REQUEST_TTY_FORCE) {
1340 if (tty_flag)
1341 logit("Pseudo-terminal will not be allocated because "
1342 "stdin is not a terminal.");
1343 tty_flag = 0;
1344 }
1345
Damien Miller95def091999-11-25 00:26:21 +11001346 if (options.user == NULL)
1347 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001348
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001349 /* Set up strings used to percent_expand() arguments */
Damien Millerdfc85fa2011-05-15 08:44:02 +10001350 if (gethostname(thishost, sizeof(thishost)) == -1)
1351 fatal("gethostname: %s", strerror(errno));
1352 strlcpy(shorthost, thishost, sizeof(shorthost));
1353 shorthost[strcspn(thishost, ".")] = '\0';
1354 snprintf(portstr, sizeof(portstr), "%d", options.port);
djm@openbsd.org9c935dd2018-06-01 03:33:53 +00001355 snprintf(uidstr, sizeof(uidstr), "%llu",
1356 (unsigned long long)pw->pw_uid);
Darren Tuckerf6b01b72008-06-13 04:56:37 +10001357
dtucker@openbsd.orged833da2020-04-03 02:27:12 +00001358 conn_hash_hex = ssh_connection_hash(thishost, host, portstr,
1359 options.user);
Damien Miller9c386432014-07-03 21:27:46 +10001360
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001361 /*
1362 * Expand tokens in arguments. NB. LocalCommand is expanded later,
1363 * after port-forwarding is set up, so it may pick up any local
1364 * tunnel interface name allocated.
1365 */
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001366 if (options.remote_command != NULL) {
1367 debug3("expanding RemoteCommand: %s", options.remote_command);
1368 cp = options.remote_command;
dtucker@openbsd.org990687a2020-04-10 00:52:07 +00001369 options.remote_command = default_client_percent_expand(cp,
1370 pw->pw_dir, host, options.user, pw->pw_name);
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001371 debug3("expanded RemoteCommand: %s", options.remote_command);
1372 free(cp);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001373 if ((r = sshbuf_put(command, options.remote_command,
1374 strlen(options.remote_command))) != 0)
1375 fatal("%s: buffer error: %s", __func__, ssh_err(r));
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001376 }
1377
Damien Miller0e220db2004-06-15 10:34:08 +10001378 if (options.control_path != NULL) {
dtucker@openbsd.orge655ee02018-07-27 05:34:42 +00001379 cp = tilde_expand_filename(options.control_path, getuid());
Darren Tuckera627d422013-06-02 07:31:17 +10001380 free(options.control_path);
dtucker@openbsd.org990687a2020-04-10 00:52:07 +00001381 options.control_path = default_client_percent_expand(cp,
1382 pw->pw_dir, host, options.user, pw->pw_name);
Darren Tuckera627d422013-06-02 07:31:17 +10001383 free(cp);
Damien Miller0e220db2004-06-15 10:34:08 +10001384 }
Damien Miller9c386432014-07-03 21:27:46 +10001385
dtucker@openbsd.orged833da2020-04-03 02:27:12 +00001386 if (options.identity_agent != NULL) {
1387 p = tilde_expand_filename(options.identity_agent, getuid());
dtucker@openbsd.org990687a2020-04-10 00:52:07 +00001388 cp = default_client_percent_expand(p,
1389 pw->pw_dir, host, options.user, pw->pw_name);
dtucker@openbsd.orged833da2020-04-03 02:27:12 +00001390 free(p);
1391 free(options.identity_agent);
1392 options.identity_agent = cp;
1393 }
1394
1395 if (options.forward_agent_sock_path != NULL) {
1396 p = tilde_expand_filename(options.forward_agent_sock_path,
1397 getuid());
dtucker@openbsd.org990687a2020-04-10 00:52:07 +00001398 cp = default_client_percent_expand(p,
1399 pw->pw_dir, host, options.user, pw->pw_name);
dtucker@openbsd.orged833da2020-04-03 02:27:12 +00001400 free(p);
1401 free(options.forward_agent_sock_path);
1402 options.forward_agent_sock_path = cp;
1403 }
1404
dtucker@openbsd.org990687a2020-04-10 00:52:07 +00001405 for (i = 0; i < options.num_local_forwards; i++) {
1406 if (options.local_forwards[i].listen_path != NULL) {
1407 cp = options.local_forwards[i].listen_path;
1408 p = options.local_forwards[i].listen_path =
1409 default_client_percent_expand(cp,
1410 pw->pw_dir, host, options.user, pw->pw_name);
1411 if (strcmp(cp, p) != 0)
1412 debug3("expanded LocalForward listen path "
1413 "'%s' -> '%s'", cp, p);
1414 free(cp);
1415 }
1416 if (options.local_forwards[i].connect_path != NULL) {
1417 cp = options.local_forwards[i].connect_path;
1418 p = options.local_forwards[i].connect_path =
1419 default_client_percent_expand(cp,
1420 pw->pw_dir, host, options.user, pw->pw_name);
1421 if (strcmp(cp, p) != 0)
1422 debug3("expanded LocalForward connect path "
1423 "'%s' -> '%s'", cp, p);
1424 free(cp);
1425 }
1426 }
1427
1428 for (i = 0; i < options.num_remote_forwards; i++) {
1429 if (options.remote_forwards[i].listen_path != NULL) {
1430 cp = options.remote_forwards[i].listen_path;
1431 p = options.remote_forwards[i].listen_path =
1432 default_client_percent_expand(cp,
1433 pw->pw_dir, host, options.user, pw->pw_name);
1434 if (strcmp(cp, p) != 0)
1435 debug3("expanded RemoteForward listen path "
1436 "'%s' -> '%s'", cp, p);
1437 free(cp);
1438 }
1439 if (options.remote_forwards[i].connect_path != NULL) {
1440 cp = options.remote_forwards[i].connect_path;
1441 p = options.remote_forwards[i].connect_path =
1442 default_client_percent_expand(cp,
1443 pw->pw_dir, host, options.user, pw->pw_name);
1444 if (strcmp(cp, p) != 0)
1445 debug3("expanded RemoteForward connect path "
1446 "'%s' -> '%s'", cp, p);
1447 free(cp);
1448 }
1449 }
1450
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001451 if (config_test) {
1452 dump_client_config(&options, host);
1453 exit(0);
1454 }
1455
djm@openbsd.org884416b2019-10-31 21:18:28 +00001456 /* Expand SecurityKeyProvider if it refers to an environment variable */
1457 if (options.sk_provider != NULL && *options.sk_provider == '$' &&
1458 strlen(options.sk_provider) > 1) {
1459 if ((cp = getenv(options.sk_provider + 1)) == NULL) {
naddy@openbsd.orga47f6a62020-02-06 22:30:54 +00001460 debug("Authenticator provider %s did not resolve; "
djm@openbsd.org884416b2019-10-31 21:18:28 +00001461 "disabling", options.sk_provider);
1462 free(options.sk_provider);
1463 options.sk_provider = NULL;
1464 } else {
1465 debug2("resolved SecurityKeyProvider %s => %s",
1466 options.sk_provider, cp);
1467 free(options.sk_provider);
1468 options.sk_provider = xstrdup(cp);
1469 }
1470 }
1471
Damien Millerb1cbfa22008-05-19 16:00:08 +10001472 if (muxclient_command != 0 && options.control_path == NULL)
Darren Tucker0814d312005-06-01 23:08:51 +10001473 fatal("No ControlPath specified for \"-O\" command");
markus@openbsd.org8d057842016-09-30 09:19:13 +00001474 if (options.control_path != NULL) {
1475 int sock;
1476 if ((sock = muxclient(options.control_path)) >= 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001477 ssh_packet_set_connection(ssh, sock, sock);
djm@openbsd.org25b2ed62019-01-19 21:36:06 +00001478 ssh_packet_set_mux(ssh);
markus@openbsd.org8d057842016-09-30 09:19:13 +00001479 goto skip_connect;
1480 }
1481 }
Damien Miller0e220db2004-06-15 10:34:08 +10001482
Damien Miller08b57c62014-02-27 10:17:13 +11001483 /*
1484 * If hostname canonicalisation was not enabled, then we may not
1485 * have yet resolved the hostname. Do so now.
1486 */
1487 if (addrs == NULL && options.proxy_command == NULL) {
djm@openbsd.orga85768a2015-09-04 04:56:09 +00001488 debug2("resolving \"%s\" port %d", host, options.port);
Damien Miller08b57c62014-02-27 10:17:13 +11001489 if ((addrs = resolve_host(host, options.port, 1,
1490 cname, sizeof(cname))) == NULL)
1491 cleanup_exit(255); /* resolve_host logs the error */
1492 }
1493
Damien Miller67bd0622007-09-17 12:06:57 +10001494 timeout_ms = options.connection_timeout * 1000;
1495
Kevin Stevesfcec7f82000-12-15 19:55:48 +00001496 /* Open a connection to the remote host. */
beck@openbsd.org2ab33572020-01-05 16:28:22 +00001497 if (ssh_connect(ssh, host, host_arg, addrs, &hostaddr, options.port,
Damien Miller0faf7472013-10-17 11:47:23 +11001498 options.address_family, options.connection_attempts,
dtucker@openbsd.org95d41e92018-07-19 10:28:47 +00001499 &timeout_ms, options.tcp_keep_alive) != 0)
Damien Miller0faf7472013-10-17 11:47:23 +11001500 exit(255);
1501
Damien Miller28631ce2013-10-26 10:07:56 +11001502 if (addrs != NULL)
1503 freeaddrinfo(addrs);
1504
djm@openbsd.org25b2ed62019-01-19 21:36:06 +00001505 ssh_packet_set_timeout(ssh, options.server_alive_interval,
Damien Miller0faf7472013-10-17 11:47:23 +11001506 options.server_alive_count_max);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001507
Damien Miller67bd0622007-09-17 12:06:57 +10001508 if (timeout_ms > 0)
1509 debug3("timeout: %d ms remain after connect", timeout_ms);
1510
Damien Miller5428f641999-11-25 11:54:57 +11001511 /*
dtucker@openbsd.org26efc2f2018-07-16 11:05:41 +00001512 * If we successfully made the connection and we have hostbased auth
1513 * enabled, load the public keys so we can later use the ssh-keysign
1514 * helper to sign challenges.
Damien Miller5428f641999-11-25 11:54:57 +11001515 */
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001516 sensitive_data.nkeys = 0;
1517 sensitive_data.keys = NULL;
djm@openbsd.org873d3e72017-04-30 23:18:44 +00001518 if (options.hostbased_authentication) {
dtucker@openbsd.orgac590762018-07-16 22:25:01 +00001519 sensitive_data.nkeys = 10;
Damien Millerddd63ab2006-03-31 23:10:51 +11001520 sensitive_data.keys = xcalloc(sensitive_data.nkeys,
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001521 sizeof(struct sshkey));
1522
1523 /* XXX check errors? */
dtucker@openbsd.orgac590762018-07-16 22:25:01 +00001524#define L_PUBKEY(p,o) do { \
1525 if ((o) >= sensitive_data.nkeys) \
1526 fatal("%s pubkey out of array bounds", __func__); \
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001527 check_load(sshkey_load_public(p, &(sensitive_data.keys[o]), NULL), \
dtucker@openbsd.orgac590762018-07-16 22:25:01 +00001528 p, "pubkey"); \
1529} while (0)
1530#define L_CERT(p,o) do { \
1531 if ((o) >= sensitive_data.nkeys) \
1532 fatal("%s cert out of array bounds", __func__); \
1533 check_load(sshkey_load_cert(p, &(sensitive_data.keys[o])), p, "cert"); \
1534} while (0)
Ben Lindstromf9c48842002-06-11 16:37:51 +00001535
dtucker@openbsd.org26efc2f2018-07-16 11:05:41 +00001536 if (options.hostbased_authentication == 1) {
dtucker@openbsd.orgac590762018-07-16 22:25:01 +00001537 L_CERT(_PATH_HOST_ECDSA_KEY_FILE, 0);
1538 L_CERT(_PATH_HOST_ED25519_KEY_FILE, 1);
1539 L_CERT(_PATH_HOST_RSA_KEY_FILE, 2);
1540 L_CERT(_PATH_HOST_DSA_KEY_FILE, 3);
1541 L_PUBKEY(_PATH_HOST_ECDSA_KEY_FILE, 4);
1542 L_PUBKEY(_PATH_HOST_ED25519_KEY_FILE, 5);
1543 L_PUBKEY(_PATH_HOST_RSA_KEY_FILE, 6);
1544 L_PUBKEY(_PATH_HOST_DSA_KEY_FILE, 7);
1545 L_CERT(_PATH_HOST_XMSS_KEY_FILE, 8);
1546 L_PUBKEY(_PATH_HOST_XMSS_KEY_FILE, 9);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001547 }
Damien Miller95def091999-11-25 00:26:21 +11001548 }
Damien Miller95def091999-11-25 00:26:21 +11001549
dtucker@openbsd.org258dc8b2018-07-18 11:34:04 +00001550 /* Create ~/.ssh * directory if it doesn't already exist. */
Darren Tucker45c66d72011-11-04 10:50:40 +11001551 if (config == NULL) {
1552 r = snprintf(buf, sizeof buf, "%s%s%s", pw->pw_dir,
1553 strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001554 if (r > 0 && (size_t)r < sizeof(buf) && stat(buf, &st) == -1) {
Darren Tucker8ccb7392010-09-10 12:28:24 +10001555#ifdef WITH_SELINUX
Darren Tucker45c66d72011-11-04 10:50:40 +11001556 ssh_selinux_setfscreatecon(buf);
Darren Tucker8ccb7392010-09-10 12:28:24 +10001557#endif
Darren Tucker45c66d72011-11-04 10:50:40 +11001558 if (mkdir(buf, 0700) < 0)
1559 error("Could not create directory '%.200s'.",
1560 buf);
Darren Tucker8ccb7392010-09-10 12:28:24 +10001561#ifdef WITH_SELINUX
Darren Tucker45c66d72011-11-04 10:50:40 +11001562 ssh_selinux_setfscreatecon(NULL);
Darren Tucker8ccb7392010-09-10 12:28:24 +10001563#endif
Darren Tucker45c66d72011-11-04 10:50:40 +11001564 }
Darren Tucker8ccb7392010-09-10 12:28:24 +10001565 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001566 /* load options.identity_files */
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001567 load_public_identity_files(pw);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001568
djm@openbsd.org001aa552018-04-10 00:10:49 +00001569 /* optionally set the SSH_AUTHSOCKET_ENV_NAME variable */
markus@openbsd.org1a75d142016-05-04 14:29:58 +00001570 if (options.identity_agent &&
1571 strcmp(options.identity_agent, SSH_AUTHSOCKET_ENV_NAME) != 0) {
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001572 if (strcmp(options.identity_agent, "none") == 0) {
1573 unsetenv(SSH_AUTHSOCKET_ENV_NAME);
1574 } else {
dtucker@openbsd.orged833da2020-04-03 02:27:12 +00001575 cp = options.identity_agent;
djm@openbsd.org5eff5b82018-10-03 06:38:35 +00001576 if (cp[0] == '$') {
1577 if (!valid_env_name(cp + 1)) {
1578 fatal("Invalid IdentityAgent "
1579 "environment variable name %s", cp);
1580 }
1581 if ((p = getenv(cp + 1)) == NULL)
1582 unsetenv(SSH_AUTHSOCKET_ENV_NAME);
1583 else
1584 setenv(SSH_AUTHSOCKET_ENV_NAME, p, 1);
1585 } else {
1586 /* identity_agent specifies a path directly */
1587 setenv(SSH_AUTHSOCKET_ENV_NAME, cp, 1);
1588 }
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001589 }
1590 }
1591
dtucker@openbsd.orged833da2020-04-03 02:27:12 +00001592 if (options.forward_agent && options.forward_agent_sock_path != NULL) {
djm@openbsd.org81624022020-04-03 06:07:57 +00001593 cp = options.forward_agent_sock_path;
djm@openbsd.org40be78f2019-12-21 02:19:13 +00001594 if (cp[0] == '$') {
1595 if (!valid_env_name(cp + 1)) {
1596 fatal("Invalid ForwardAgent environment variable name %s", cp);
1597 }
1598 if ((p = getenv(cp + 1)) != NULL)
1599 forward_agent_sock_path = p;
1600 else
1601 options.forward_agent = 0;
1602 free(cp);
1603 } else {
1604 forward_agent_sock_path = cp;
1605 }
1606 }
1607
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001608 /* Expand ~ in known host file names. */
Damien Miller295ee632011-05-29 21:42:31 +10001609 tilde_expand_paths(options.system_hostfiles,
1610 options.num_system_hostfiles);
1611 tilde_expand_paths(options.user_hostfiles, options.num_user_hostfiles);
Damien Miller95def091999-11-25 00:26:21 +11001612
dtucker@openbsd.org3bf2a6a2020-01-23 07:10:22 +00001613 ssh_signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
1614 ssh_signal(SIGCHLD, main_sigchld_handler);
Damien Miller07cd5892001-11-12 10:52:03 +11001615
Darren Tuckerd6173c02008-06-13 04:52:53 +10001616 /* Log into the remote system. Never returns if the login fails. */
djm@openbsd.org0a843d92018-12-27 03:25:24 +00001617 ssh_login(ssh, &sensitive_data, host, (struct sockaddr *)&hostaddr,
Damien Millerd925dcd2010-12-01 12:21:51 +11001618 options.port, pw, timeout_ms);
Damien Miller95def091999-11-25 00:26:21 +11001619
djm@openbsd.org25b2ed62019-01-19 21:36:06 +00001620 if (ssh_packet_connection_is_on_socket(ssh)) {
Damien Miller383ffe62010-06-26 10:02:03 +10001621 verbose("Authenticated to %s ([%s]:%d).", host,
djm@openbsd.org95767262016-03-07 19:02:43 +00001622 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
Damien Miller383ffe62010-06-26 10:02:03 +10001623 } else {
1624 verbose("Authenticated to %s (via proxy).", host);
1625 }
1626
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001627 /* We no longer need the private host keys. Clear them now. */
1628 if (sensitive_data.nkeys != 0) {
1629 for (i = 0; i < sensitive_data.nkeys; i++) {
1630 if (sensitive_data.keys[i] != NULL) {
1631 /* Destroys contents safely */
1632 debug3("clear hostkey %d", i);
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001633 sshkey_free(sensitive_data.keys[i]);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001634 sensitive_data.keys[i] = NULL;
1635 }
1636 }
Darren Tuckera627d422013-06-02 07:31:17 +10001637 free(sensitive_data.keys);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001638 }
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +00001639 for (i = 0; i < options.num_identity_files; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10001640 free(options.identity_files[i]);
1641 options.identity_files[i] = NULL;
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +00001642 if (options.identity_keys[i]) {
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001643 sshkey_free(options.identity_keys[i]);
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +00001644 options.identity_keys[i] = NULL;
1645 }
1646 }
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001647 for (i = 0; i < options.num_certificate_files; i++) {
1648 free(options.certificate_files[i]);
1649 options.certificate_files[i] = NULL;
1650 }
Damien Miller95def091999-11-25 00:26:21 +11001651
markus@openbsd.org8d057842016-09-30 09:19:13 +00001652 skip_connect:
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001653 exit_status = ssh_session2(ssh, pw);
djm@openbsd.org25b2ed62019-01-19 21:36:06 +00001654 ssh_packet_close(ssh);
Damien Miller8c4e18a2002-09-19 12:05:02 +10001655
Damien Millerb1cbfa22008-05-19 16:00:08 +10001656 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10001657 unlink(options.control_path);
1658
Damien Millera41ccca2010-10-07 22:07:32 +11001659 /* Kill ProxyCommand if it is running. */
1660 ssh_kill_proxy_command();
Damien Miller8c4e18a2002-09-19 12:05:02 +10001661
Damien Miller1383bd82000-04-06 12:32:37 +10001662 return exit_status;
1663}
1664
Damien Millere11e1ea2010-08-03 16:04:46 +10001665static void
1666control_persist_detach(void)
1667{
1668 pid_t pid;
djm@openbsd.orgd2d6bf82016-04-29 08:07:53 +00001669 int devnull, keep_stderr;
Damien Millere11e1ea2010-08-03 16:04:46 +10001670
1671 debug("%s: backgrounding master process", __func__);
1672
djm@openbsd.orgb8bbff32018-02-13 03:36:56 +00001673 /*
1674 * master (current process) into the background, and make the
1675 * foreground process a client of the backgrounded master.
1676 */
Damien Millere11e1ea2010-08-03 16:04:46 +10001677 switch ((pid = fork())) {
1678 case -1:
1679 fatal("%s: fork: %s", __func__, strerror(errno));
1680 case 0:
1681 /* Child: master process continues mainloop */
djm@openbsd.orgb8bbff32018-02-13 03:36:56 +00001682 break;
1683 default:
Damien Millere11e1ea2010-08-03 16:04:46 +10001684 /* Parent: set up mux slave to connect to backgrounded master */
1685 debug2("%s: background process is %ld", __func__, (long)pid);
1686 stdin_null_flag = ostdin_null_flag;
Damien Miller21771e22011-05-15 08:45:50 +10001687 options.request_tty = orequest_tty;
Damien Millere11e1ea2010-08-03 16:04:46 +10001688 tty_flag = otty_flag;
djm@openbsd.orgb8bbff32018-02-13 03:36:56 +00001689 close(muxserver_sock);
1690 muxserver_sock = -1;
Damien Miller5929c522010-09-10 11:17:02 +10001691 options.control_master = SSHCTL_MASTER_NO;
djm@openbsd.orgb8bbff32018-02-13 03:36:56 +00001692 muxclient(options.control_path);
Damien Millere11e1ea2010-08-03 16:04:46 +10001693 /* muxclient() doesn't return on success. */
djm@openbsd.orgb8bbff32018-02-13 03:36:56 +00001694 fatal("Failed to connect to new control master");
1695 }
Damien Miller00d9ae22010-08-17 01:59:31 +10001696 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
1697 error("%s: open(\"/dev/null\"): %s", __func__,
1698 strerror(errno));
1699 } else {
djm@openbsd.orgd2d6bf82016-04-29 08:07:53 +00001700 keep_stderr = log_is_on_stderr() && debug_flag;
Damien Miller00d9ae22010-08-17 01:59:31 +10001701 if (dup2(devnull, STDIN_FILENO) == -1 ||
djm@openbsd.orgd2d6bf82016-04-29 08:07:53 +00001702 dup2(devnull, STDOUT_FILENO) == -1 ||
1703 (!keep_stderr && dup2(devnull, STDERR_FILENO) == -1))
Damien Miller00d9ae22010-08-17 01:59:31 +10001704 error("%s: dup2: %s", __func__, strerror(errno));
1705 if (devnull > STDERR_FILENO)
1706 close(devnull);
1707 }
Damien Miller98e27dc2013-07-25 11:55:52 +10001708 daemon(1, 1);
Damien Millerea2c1a42011-06-03 12:10:22 +10001709 setproctitle("%s [mux]", options.control_path);
Damien Millere11e1ea2010-08-03 16:04:46 +10001710}
1711
1712/* Do fork() after authentication. Used by "ssh -f" */
1713static void
1714fork_postauth(void)
1715{
1716 if (need_controlpersist_detach)
1717 control_persist_detach();
1718 debug("forking to background");
1719 fork_after_authentication_flag = 0;
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001720 if (daemon(1, 1) == -1)
Damien Millere11e1ea2010-08-03 16:04:46 +10001721 fatal("daemon() failed: %.200s", strerror(errno));
1722}
1723
djm@openbsd.org663e84b2020-04-03 02:40:32 +00001724static void
1725forwarding_success(void)
1726{
djm@openbsd.org7b4d8992020-04-03 04:03:51 +00001727 if (forward_confirms_pending == -1)
1728 return;
1729 if (--forward_confirms_pending == 0) {
djm@openbsd.orgebd29e92020-04-03 04:06:26 +00001730 debug("%s: all expected forwarding replies received", __func__);
djm@openbsd.org663e84b2020-04-03 02:40:32 +00001731 if (fork_after_authentication_flag)
1732 fork_postauth();
djm@openbsd.org7b4d8992020-04-03 04:03:51 +00001733 } else {
1734 debug2("%s: %d expected forwarding replies remaining",
1735 __func__, forward_confirms_pending);
djm@openbsd.org663e84b2020-04-03 02:40:32 +00001736 }
1737}
1738
Darren Tucker9f407c42008-06-13 04:50:27 +10001739/* Callback for remote forward global requests */
1740static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001741ssh_confirm_remote_forward(struct ssh *ssh, int type, u_int32_t seq, void *ctxt)
Darren Tucker9f407c42008-06-13 04:50:27 +10001742{
Damien Miller7acefbb2014-07-18 14:11:24 +10001743 struct Forward *rfwd = (struct Forward *)ctxt;
djm@openbsd.org25b2ed62019-01-19 21:36:06 +00001744 u_int port;
1745 int r;
Darren Tucker9f407c42008-06-13 04:50:27 +10001746
Damien Miller4bf648f2009-02-14 16:28:21 +11001747 /* XXX verbose() on failure? */
Damien Miller4b3ed642014-07-02 15:29:40 +10001748 debug("remote forward %s for: listen %s%s%d, connect %s:%d",
Darren Tucker9f407c42008-06-13 04:50:27 +10001749 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
Damien Miller7acefbb2014-07-18 14:11:24 +10001750 rfwd->listen_path ? rfwd->listen_path :
1751 rfwd->listen_host ? rfwd->listen_host : "",
1752 (rfwd->listen_path || rfwd->listen_host) ? ":" : "",
1753 rfwd->listen_port, rfwd->connect_path ? rfwd->connect_path :
1754 rfwd->connect_host, rfwd->connect_port);
1755 if (rfwd->listen_path == NULL && rfwd->listen_port == 0) {
Darren Tucker68afb8c2011-10-02 18:59:03 +11001756 if (type == SSH2_MSG_REQUEST_SUCCESS) {
djm@openbsd.org25b2ed62019-01-19 21:36:06 +00001757 if ((r = sshpkt_get_u32(ssh, &port)) != 0)
1758 fatal("%s: %s", __func__, ssh_err(r));
1759 if (port > 65535) {
1760 error("Invalid allocated port %u for remote "
1761 "forward to %s:%d", port,
1762 rfwd->connect_host, rfwd->connect_port);
1763 /* Ensure failure processing runs below */
1764 type = SSH2_MSG_REQUEST_FAILURE;
1765 channel_update_permission(ssh,
1766 rfwd->handle, -1);
1767 } else {
1768 rfwd->allocated_port = (int)port;
1769 logit("Allocated port %u for remote "
1770 "forward to %s:%d",
1771 rfwd->allocated_port, rfwd->connect_host,
1772 rfwd->connect_port);
1773 channel_update_permission(ssh,
1774 rfwd->handle, rfwd->allocated_port);
1775 }
Darren Tucker68afb8c2011-10-02 18:59:03 +11001776 } else {
djm@openbsd.org115063a2018-06-06 18:22:41 +00001777 channel_update_permission(ssh, rfwd->handle, -1);
Darren Tucker68afb8c2011-10-02 18:59:03 +11001778 }
Damien Miller4bf648f2009-02-14 16:28:21 +11001779 }
djm@openbsd.org@openbsd.orgdbe06622017-10-27 01:57:06 +00001780
Darren Tucker9f407c42008-06-13 04:50:27 +10001781 if (type == SSH2_MSG_REQUEST_FAILURE) {
Damien Miller7acefbb2014-07-18 14:11:24 +10001782 if (options.exit_on_forward_failure) {
1783 if (rfwd->listen_path != NULL)
1784 fatal("Error: remote port forwarding failed "
1785 "for listen path %s", rfwd->listen_path);
1786 else
1787 fatal("Error: remote port forwarding failed "
1788 "for listen port %d", rfwd->listen_port);
1789 } else {
1790 if (rfwd->listen_path != NULL)
1791 logit("Warning: remote port forwarding failed "
1792 "for listen path %s", rfwd->listen_path);
1793 else
1794 logit("Warning: remote port forwarding failed "
1795 "for listen port %d", rfwd->listen_port);
1796 }
Darren Tucker9f407c42008-06-13 04:50:27 +10001797 }
djm@openbsd.org663e84b2020-04-03 02:40:32 +00001798 forwarding_success();
Darren Tucker9f407c42008-06-13 04:50:27 +10001799}
1800
Ben Lindstrombba81212001-06-25 05:01:22 +00001801static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001802client_cleanup_stdio_fwd(struct ssh *ssh, int id, void *arg)
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001803{
1804 debug("stdio forwarding: done");
1805 cleanup_exit(0);
1806}
1807
Darren Tucker2d6665d2011-11-04 10:54:22 +11001808static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001809ssh_stdio_confirm(struct ssh *ssh, int id, int success, void *arg)
Damien Miller357610d2014-07-18 15:04:10 +10001810{
1811 if (!success)
1812 fatal("stdio forwarding failed");
1813}
1814
1815static void
djm@openbsd.org663e84b2020-04-03 02:40:32 +00001816ssh_tun_confirm(struct ssh *ssh, int id, int success, void *arg)
1817{
1818 if (!success) {
1819 error("Tunnel forwarding failed");
1820 if (options.exit_on_forward_failure)
1821 cleanup_exit(255);
1822 }
1823
1824 debug("%s: tunnel forward established, id=%d", __func__, id);
1825 forwarding_success();
1826}
1827
1828static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001829ssh_init_stdio_forwarding(struct ssh *ssh)
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001830{
1831 Channel *c;
Damien Millere1537f92010-01-26 13:26:22 +11001832 int in, out;
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001833
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +00001834 if (options.stdio_forward_host == NULL)
Darren Tucker2d6665d2011-11-04 10:54:22 +11001835 return;
Damien Millere1537f92010-01-26 13:26:22 +11001836
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +00001837 debug3("%s: %s:%d", __func__, options.stdio_forward_host,
1838 options.stdio_forward_port);
Darren Tucker2d6665d2011-11-04 10:54:22 +11001839
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001840 if ((in = dup(STDIN_FILENO)) == -1 ||
1841 (out = dup(STDOUT_FILENO)) == -1)
Damien Millere1537f92010-01-26 13:26:22 +11001842 fatal("channel_connect_stdio_fwd: dup() in/out failed");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001843 if ((c = channel_connect_stdio_fwd(ssh, options.stdio_forward_host,
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +00001844 options.stdio_forward_port, in, out)) == NULL)
Darren Tucker2d6665d2011-11-04 10:54:22 +11001845 fatal("%s: channel_connect_stdio_fwd failed", __func__);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001846 channel_register_cleanup(ssh, c->self, client_cleanup_stdio_fwd, 0);
1847 channel_register_open_confirm(ssh, c->self, ssh_stdio_confirm, NULL);
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001848}
1849
1850static void
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001851ssh_init_forwarding(struct ssh *ssh, char **ifname)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001852{
Kevin Steves12057502001-02-05 14:54:34 +00001853 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001854 int i;
Kevin Steves12057502001-02-05 14:54:34 +00001855
djm@openbsd.org7b4d8992020-04-03 04:03:51 +00001856 if (options.exit_on_forward_failure)
1857 forward_confirms_pending = 0; /* track pending requests */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001858 /* Initiate local TCP/IP port forwardings. */
1859 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001860 debug("Local connections to %.200s:%d forwarded to remote "
1861 "address %.200s:%d",
Damien Miller7acefbb2014-07-18 14:11:24 +10001862 (options.local_forwards[i].listen_path != NULL) ?
1863 options.local_forwards[i].listen_path :
Darren Tucker47eede72005-03-14 23:08:12 +11001864 (options.local_forwards[i].listen_host == NULL) ?
Damien Miller7acefbb2014-07-18 14:11:24 +10001865 (options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") :
Damien Millerf91ee4c2005-03-01 21:24:33 +11001866 options.local_forwards[i].listen_host,
1867 options.local_forwards[i].listen_port,
Damien Miller7acefbb2014-07-18 14:11:24 +10001868 (options.local_forwards[i].connect_path != NULL) ?
1869 options.local_forwards[i].connect_path :
Damien Millerf91ee4c2005-03-01 21:24:33 +11001870 options.local_forwards[i].connect_host,
1871 options.local_forwards[i].connect_port);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001872 success += channel_setup_local_fwd_listener(ssh,
Damien Miller7acefbb2014-07-18 14:11:24 +10001873 &options.local_forwards[i], &options.fwd_opts);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001874 }
Darren Tuckere7d4b192006-07-12 22:17:10 +10001875 if (i > 0 && success != i && options.exit_on_forward_failure)
1876 fatal("Could not request local forwarding.");
Kevin Steves12057502001-02-05 14:54:34 +00001877 if (i > 0 && success == 0)
1878 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001879
1880 /* Initiate remote TCP/IP port forwardings. */
1881 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001882 debug("Remote connections from %.200s:%d forwarded to "
1883 "local address %.200s:%d",
Damien Miller7acefbb2014-07-18 14:11:24 +10001884 (options.remote_forwards[i].listen_path != NULL) ?
1885 options.remote_forwards[i].listen_path :
Damien Miller46d38de2005-07-17 17:02:09 +10001886 (options.remote_forwards[i].listen_host == NULL) ?
Damien Milleraa3bb102005-11-05 15:12:59 +11001887 "LOCALHOST" : options.remote_forwards[i].listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +11001888 options.remote_forwards[i].listen_port,
Damien Miller7acefbb2014-07-18 14:11:24 +10001889 (options.remote_forwards[i].connect_path != NULL) ?
1890 options.remote_forwards[i].connect_path :
Damien Millerf91ee4c2005-03-01 21:24:33 +11001891 options.remote_forwards[i].connect_host,
1892 options.remote_forwards[i].connect_port);
djm@openbsd.org663e84b2020-04-03 02:40:32 +00001893 if ((options.remote_forwards[i].handle =
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001894 channel_request_remote_forwarding(ssh,
djm@openbsd.org663e84b2020-04-03 02:40:32 +00001895 &options.remote_forwards[i])) >= 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001896 client_register_global_confirm(
1897 ssh_confirm_remote_forward,
Darren Tucker68afb8c2011-10-02 18:59:03 +11001898 &options.remote_forwards[i]);
djm@openbsd.org663e84b2020-04-03 02:40:32 +00001899 forward_confirms_pending++;
1900 } else if (options.exit_on_forward_failure)
1901 fatal("Could not request remote forwarding.");
1902 else
1903 logit("Warning: Could not request remote forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001904 }
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001905
1906 /* Initiate tunnel forwarding. */
1907 if (options.tun_open != SSH_TUNMODE_NO) {
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001908 if ((*ifname = client_request_tun_fwd(ssh,
1909 options.tun_open, options.tun_local,
djm@openbsd.org663e84b2020-04-03 02:40:32 +00001910 options.tun_remote, ssh_tun_confirm, NULL)) != NULL)
1911 forward_confirms_pending++;
1912 else if (options.exit_on_forward_failure)
1913 fatal("Could not request tunnel forwarding.");
1914 else
1915 error("Could not request tunnel forwarding.");
djm@openbsd.org@openbsd.orgdbe06622017-10-27 01:57:06 +00001916 }
djm@openbsd.org7b4d8992020-04-03 04:03:51 +00001917 if (forward_confirms_pending > 0) {
1918 debug("%s: expecting replies for %d forwards", __func__,
1919 forward_confirms_pending);
1920 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001921}
1922
Ben Lindstrombba81212001-06-25 05:01:22 +00001923static void
Damien Miller0bc1bd82000-11-13 22:57:25 +11001924check_agent_present(void)
1925{
djm@openbsd.org141efe42015-01-14 20:05:27 +00001926 int r;
1927
Damien Miller0bc1bd82000-11-13 22:57:25 +11001928 if (options.forward_agent) {
Damien Miller788f2122005-11-05 15:14:59 +11001929 /* Clear agent forwarding if we don't have an agent. */
djm@openbsd.org141efe42015-01-14 20:05:27 +00001930 if ((r = ssh_get_authentication_socket(NULL)) != 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001931 options.forward_agent = 0;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001932 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1933 debug("ssh_get_authentication_socket: %s",
1934 ssh_err(r));
1935 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001936 }
1937}
1938
Ben Lindstrombba81212001-06-25 05:01:22 +00001939static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001940ssh_session2_setup(struct ssh *ssh, int id, int success, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +10001941{
Damien Miller3756dce2004-06-18 01:17:29 +10001942 extern char **environ;
Damien Miller17e7ed02005-06-17 12:54:33 +10001943 const char *display;
djm@openbsd.org25b2ed62019-01-19 21:36:06 +00001944 int r, interactive = tty_flag;
djm@openbsd.orged4ce822016-01-13 23:04:47 +00001945 char *proto = NULL, *data = NULL;
Damien Miller17e7ed02005-06-17 12:54:33 +10001946
Damien Millerd530f5f2010-05-21 14:57:10 +10001947 if (!success)
1948 return; /* No need for error message, channels code sens one */
1949
Damien Miller46d38de2005-07-17 17:02:09 +10001950 display = getenv("DISPLAY");
djm@openbsd.orga58be332015-04-17 13:16:48 +00001951 if (display == NULL && options.forward_x11)
1952 debug("X11 forwarding requested but DISPLAY not set");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001953 if (options.forward_x11 && client_x11_get_proto(ssh, display,
djm@openbsd.orged4ce822016-01-13 23:04:47 +00001954 options.xauth_location, options.forward_x11_trusted,
1955 options.forward_x11_timeout, &proto, &data) == 0) {
Damien Millerbd483e72000-04-30 10:00:53 +10001956 /* Request forwarding with authentication spoofing. */
Darren Tuckerd6173c02008-06-13 04:52:53 +10001957 debug("Requesting X11 forwarding with authentication "
1958 "spoofing.");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001959 x11_request_forwarding_with_spoofing(ssh, id, display, proto,
Damien Miller6d7b4372011-06-23 08:31:57 +10001960 data, 1);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001961 client_expect_confirm(ssh, id, "X11 forwarding", CONFIRM_WARN);
Damien Miller6d7b4372011-06-23 08:31:57 +10001962 /* XXX exit_on_forward_failure */
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001963 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001964 }
1965
Damien Miller0bc1bd82000-11-13 22:57:25 +11001966 check_agent_present();
1967 if (options.forward_agent) {
1968 debug("Requesting authentication agent forwarding.");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001969 channel_request_start(ssh, id, "auth-agent-req@openssh.com", 0);
djm@openbsd.org25b2ed62019-01-19 21:36:06 +00001970 if ((r = sshpkt_send(ssh)) != 0)
1971 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller0bc1bd82000-11-13 22:57:25 +11001972 }
1973
Darren Tucker7b305012012-07-02 18:55:09 +10001974 /* Tell the packet module whether this is an interactive session. */
djm@openbsd.org25b2ed62019-01-19 21:36:06 +00001975 ssh_packet_set_interactive(ssh, interactive,
Darren Tucker7b305012012-07-02 18:55:09 +10001976 options.ip_qos_interactive, options.ip_qos_bulk);
1977
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001978 client_session2_setup(ssh, id, tty_flag, subsystem_flag, getenv("TERM"),
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001979 NULL, fileno(stdin), command, environ);
Damien Miller1383bd82000-04-06 12:32:37 +10001980}
1981
Ben Lindstromf558cf62001-09-20 23:13:49 +00001982/* open new channel for a session */
Ben Lindstrombba81212001-06-25 05:01:22 +00001983static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001984ssh_session2_open(struct ssh *ssh)
Damien Miller1383bd82000-04-06 12:32:37 +10001985{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001986 Channel *c;
1987 int window, packetmax, in, out, err;
Damien Millerad833b32000-08-23 10:46:23 +10001988
Damien Millercaf6dd62000-08-29 11:33:50 +11001989 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001990 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001991 } else {
1992 in = dup(STDIN_FILENO);
1993 }
1994 out = dup(STDOUT_FILENO);
1995 err = dup(STDERR_FILENO);
1996
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001997 if (in == -1 || out == -1 || err == -1)
Damien Millercaf6dd62000-08-29 11:33:50 +11001998 fatal("dup() in/out/err failed");
1999
Damien Miller69b69aa2000-10-28 14:19:58 +11002000 /* enable nonblocking unless tty */
2001 if (!isatty(in))
2002 set_nonblock(in);
2003 if (!isatty(out))
2004 set_nonblock(out);
2005 if (!isatty(err))
2006 set_nonblock(err);
2007
Damien Millere4340be2000-09-16 13:29:08 +11002008 window = CHAN_SES_WINDOW_DEFAULT;
2009 packetmax = CHAN_SES_PACKET_DEFAULT;
Damien Miller19a59452002-02-19 15:20:57 +11002010 if (tty_flag) {
2011 window >>= 1;
2012 packetmax >>= 1;
Damien Miller1383bd82000-04-06 12:32:37 +10002013 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002014 c = channel_new(ssh,
Damien Miller1383bd82000-04-06 12:32:37 +10002015 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11002016 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002017 "client-session", /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10002018
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002019 debug3("%s: channel_new: %d", __func__, c->self);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00002020
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002021 channel_send_open(ssh, c->self);
Ben Lindstromf558cf62001-09-20 23:13:49 +00002022 if (!no_shell_flag)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002023 channel_register_open_confirm(ssh, c->self,
Damien Millerb84886b2008-05-19 15:05:07 +10002024 ssh_session2_setup, NULL);
Damien Miller1383bd82000-04-06 12:32:37 +10002025
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002026 return c->self;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00002027}
2028
Ben Lindstrombba81212001-06-25 05:01:22 +00002029static int
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00002030ssh_session2(struct ssh *ssh, struct passwd *pw)
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00002031{
djm@openbsd.org25b2ed62019-01-19 21:36:06 +00002032 int r, devnull, id = -1;
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00002033 char *cp, *tun_fwd_ifname = NULL;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00002034
2035 /* XXX should be pre-session */
Darren Tucker2d6665d2011-11-04 10:54:22 +11002036 if (!options.control_persist)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002037 ssh_init_stdio_forwarding(ssh);
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00002038
2039 ssh_init_forwarding(ssh, &tun_fwd_ifname);
2040
2041 if (options.local_command != NULL) {
2042 debug3("expanding LocalCommand: %s", options.local_command);
2043 cp = options.local_command;
2044 options.local_command = percent_expand(cp,
dtucker@openbsd.orged833da2020-04-03 02:27:12 +00002045 DEFAULT_CLIENT_PERCENT_EXPAND_ARGS,
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00002046 "d", pw->pw_dir,
2047 "h", host,
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00002048 "r", options.user,
2049 "u", pw->pw_name,
2050 "T", tun_fwd_ifname == NULL ? "NONE" : tun_fwd_ifname,
2051 (char *)NULL);
2052 debug3("expanded LocalCommand: %s", options.local_command);
2053 free(cp);
2054 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00002055
Damien Millere11e1ea2010-08-03 16:04:46 +10002056 /* Start listening for multiplex clients */
djm@openbsd.org25b2ed62019-01-19 21:36:06 +00002057 if (!ssh_packet_get_mux(ssh))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002058 muxserver_listen(ssh);
Damien Millere11e1ea2010-08-03 16:04:46 +10002059
djm@openbsd.orgb8bbff32018-02-13 03:36:56 +00002060 /*
Darren Tucker2d6665d2011-11-04 10:54:22 +11002061 * If we are in control persist mode and have a working mux listen
2062 * socket, then prepare to background ourselves and have a foreground
2063 * client attach as a control slave.
2064 * NB. we must save copies of the flags that we override for
Damien Millere11e1ea2010-08-03 16:04:46 +10002065 * the backgrounding, since we defer attachment of the slave until
2066 * after the connection is fully established (in particular,
2067 * async rfwd replies have been received for ExitOnForwardFailure).
2068 */
djm@openbsd.orgb8bbff32018-02-13 03:36:56 +00002069 if (options.control_persist && muxserver_sock != -1) {
Damien Millere11e1ea2010-08-03 16:04:46 +10002070 ostdin_null_flag = stdin_null_flag;
2071 ono_shell_flag = no_shell_flag;
Damien Miller21771e22011-05-15 08:45:50 +10002072 orequest_tty = options.request_tty;
Damien Millere11e1ea2010-08-03 16:04:46 +10002073 otty_flag = tty_flag;
djm@openbsd.orgb8bbff32018-02-13 03:36:56 +00002074 stdin_null_flag = 1;
2075 no_shell_flag = 1;
2076 tty_flag = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10002077 if (!fork_after_authentication_flag)
2078 need_controlpersist_detach = 1;
2079 fork_after_authentication_flag = 1;
djm@openbsd.orgb8bbff32018-02-13 03:36:56 +00002080 }
Darren Tucker2d6665d2011-11-04 10:54:22 +11002081 /*
2082 * ControlPersist mux listen socket setup failed, attempt the
2083 * stdio forward setup that we skipped earlier.
2084 */
2085 if (options.control_persist && muxserver_sock == -1)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002086 ssh_init_stdio_forwarding(ssh);
Damien Millere11e1ea2010-08-03 16:04:46 +10002087
djm@openbsd.org14b5c632018-01-23 05:27:21 +00002088 if (!no_shell_flag)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002089 id = ssh_session2_open(ssh);
Damien Miller649fe022013-07-18 16:13:55 +10002090 else {
djm@openbsd.org25b2ed62019-01-19 21:36:06 +00002091 ssh_packet_set_interactive(ssh,
Damien Miller649fe022013-07-18 16:13:55 +10002092 options.control_master == SSHCTL_MASTER_NO,
2093 options.ip_qos_interactive, options.ip_qos_bulk);
2094 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00002095
Darren Tucker8901fa92008-06-11 09:34:01 +10002096 /* If we don't expect to open a new session, then disallow it */
Damien Miller456e6f02008-11-03 19:20:10 +11002097 if (options.control_master == SSHCTL_MASTER_NO &&
2098 (datafellows & SSH_NEW_OPENSSH)) {
Darren Tucker8901fa92008-06-11 09:34:01 +10002099 debug("Requesting no-more-sessions@openssh.com");
djm@openbsd.org25b2ed62019-01-19 21:36:06 +00002100 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
2101 (r = sshpkt_put_cstring(ssh,
2102 "no-more-sessions@openssh.com")) != 0 ||
2103 (r = sshpkt_put_u8(ssh, 0)) != 0 ||
2104 (r = sshpkt_send(ssh)) != 0)
2105 fatal("%s: %s", __func__, ssh_err(r));
Darren Tucker8901fa92008-06-11 09:34:01 +10002106 }
2107
Damien Millerd27b9472005-12-13 19:29:02 +11002108 /* Execute a local command */
2109 if (options.local_command != NULL &&
2110 options.permit_local_command)
2111 ssh_local_cmd(options.local_command);
2112
Damien Miller1f25ab42010-07-16 13:56:23 +10002113 /*
djm@openbsd.org4d5456c2017-10-25 00:21:37 +00002114 * stdout is now owned by the session channel; clobber it here
2115 * so future channel closes are propagated to the local fd.
2116 * NB. this can only happen after LocalCommand has completed,
2117 * as it may want to write to stdout.
2118 */
djm@openbsd.org@openbsd.org939b30b2017-11-01 00:04:15 +00002119 if (!need_controlpersist_detach) {
2120 if ((devnull = open(_PATH_DEVNULL, O_WRONLY)) == -1)
2121 error("%s: open %s: %s", __func__,
2122 _PATH_DEVNULL, strerror(errno));
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00002123 if (dup2(devnull, STDOUT_FILENO) == -1)
djm@openbsd.org@openbsd.org939b30b2017-11-01 00:04:15 +00002124 fatal("%s: dup2() stdout failed", __func__);
2125 if (devnull > STDERR_FILENO)
2126 close(devnull);
2127 }
djm@openbsd.org4d5456c2017-10-25 00:21:37 +00002128
2129 /*
Damien Miller1f25ab42010-07-16 13:56:23 +10002130 * If requested and we are not interested in replies to remote
2131 * forwarding requests, then let ssh continue in the background.
2132 */
Damien Millere11e1ea2010-08-03 16:04:46 +10002133 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();
Darren Tucker9a2a6092008-07-04 12:53:50 +10002140 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00002141
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002142 return client_loop(ssh, tty_flag, tty_flag ?
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00002143 options.escape_char : SSH_ESCAPECHAR_NONE, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002144}
Damien Miller0bc1bd82000-11-13 22:57:25 +11002145
djm@openbsd.org4e44a792015-09-24 06:15:11 +00002146/* Loads all IdentityFile and CertificateFile keys */
Ben Lindstrombba81212001-06-25 05:01:22 +00002147static void
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00002148load_public_identity_files(struct passwd *pw)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00002149{
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00002150 char *filename, *cp;
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00002151 struct sshkey *public;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00002152 int i;
2153 u_int n_ids, n_certs;
Damien Miller0a80ca12010-02-27 07:55:05 +11002154 char *identity_files[SSH_MAX_IDENTITY_FILES];
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00002155 struct sshkey *identity_keys[SSH_MAX_IDENTITY_FILES];
djm@openbsd.org3eb7f102018-07-16 07:06:50 +00002156 int identity_file_userprovided[SSH_MAX_IDENTITY_FILES];
djm@openbsd.org4e44a792015-09-24 06:15:11 +00002157 char *certificate_files[SSH_MAX_CERTIFICATE_FILES];
2158 struct sshkey *certificates[SSH_MAX_CERTIFICATE_FILES];
djm@openbsd.org3eb7f102018-07-16 07:06:50 +00002159 int certificate_file_userprovided[SSH_MAX_CERTIFICATE_FILES];
Damien Miller7ea845e2010-02-12 09:21:02 +11002160#ifdef ENABLE_PKCS11
djm@openbsd.org89a8d452020-01-25 00:03:36 +00002161 struct sshkey **keys = NULL;
2162 char **comments = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +11002163 int nkeys;
Damien Miller0a80ca12010-02-27 07:55:05 +11002164#endif /* PKCS11 */
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00002165
djm@openbsd.org4e44a792015-09-24 06:15:11 +00002166 n_ids = n_certs = 0;
Damien Miller1d2c4562014-02-04 11:18:20 +11002167 memset(identity_files, 0, sizeof(identity_files));
2168 memset(identity_keys, 0, sizeof(identity_keys));
djm@openbsd.org3eb7f102018-07-16 07:06:50 +00002169 memset(identity_file_userprovided, 0,
2170 sizeof(identity_file_userprovided));
djm@openbsd.org4e44a792015-09-24 06:15:11 +00002171 memset(certificate_files, 0, sizeof(certificate_files));
2172 memset(certificates, 0, sizeof(certificates));
djm@openbsd.org3eb7f102018-07-16 07:06:50 +00002173 memset(certificate_file_userprovided, 0,
2174 sizeof(certificate_file_userprovided));
Damien Miller0a80ca12010-02-27 07:55:05 +11002175
2176#ifdef ENABLE_PKCS11
Damien Miller7ea845e2010-02-12 09:21:02 +11002177 if (options.pkcs11_provider != NULL &&
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00002178 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
Damien Miller7ea845e2010-02-12 09:21:02 +11002179 (pkcs11_init(!options.batch_mode) == 0) &&
2180 (nkeys = pkcs11_add_provider(options.pkcs11_provider, NULL,
djm@openbsd.org89a8d452020-01-25 00:03:36 +00002181 &keys, &comments)) > 0) {
Damien Miller7ea845e2010-02-12 09:21:02 +11002182 for (i = 0; i < nkeys; i++) {
Damien Miller0a80ca12010-02-27 07:55:05 +11002183 if (n_ids >= SSH_MAX_IDENTITY_FILES) {
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00002184 sshkey_free(keys[i]);
djm@openbsd.org89a8d452020-01-25 00:03:36 +00002185 free(comments[i]);
Damien Miller0a80ca12010-02-27 07:55:05 +11002186 continue;
2187 }
2188 identity_keys[n_ids] = keys[i];
djm@openbsd.org89a8d452020-01-25 00:03:36 +00002189 identity_files[n_ids] = comments[i]; /* transferred */
Damien Miller0a80ca12010-02-27 07:55:05 +11002190 n_ids++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00002191 }
Darren Tuckera627d422013-06-02 07:31:17 +10002192 free(keys);
djm@openbsd.org89a8d452020-01-25 00:03:36 +00002193 free(comments);
Ben Lindstrom711b04a2001-08-06 21:12:42 +00002194 }
Damien Miller7ea845e2010-02-12 09:21:02 +11002195#endif /* ENABLE_PKCS11 */
Damien Miller0a80ca12010-02-27 07:55:05 +11002196 for (i = 0; i < options.num_identity_files; i++) {
Darren Tucker15fd19c2013-04-05 11:22:26 +11002197 if (n_ids >= SSH_MAX_IDENTITY_FILES ||
2198 strcasecmp(options.identity_files[i], "none") == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +10002199 free(options.identity_files[i]);
djm@openbsd.org4e44a792015-09-24 06:15:11 +00002200 options.identity_files[i] = NULL;
Damien Miller0a80ca12010-02-27 07:55:05 +11002201 continue;
2202 }
dtucker@openbsd.orge655ee02018-07-27 05:34:42 +00002203 cp = tilde_expand_filename(options.identity_files[i], getuid());
dtucker@openbsd.org990687a2020-04-10 00:52:07 +00002204 filename = default_client_percent_expand(cp,
2205 pw->pw_dir, host, options.user, pw->pw_name);
Darren Tuckera627d422013-06-02 07:31:17 +10002206 free(cp);
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00002207 check_load(sshkey_load_public(filename, &public, NULL),
2208 filename, "pubkey");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00002209 debug("identity file %s type %d", filename,
2210 public ? public->type : -1);
Darren Tuckera627d422013-06-02 07:31:17 +10002211 free(options.identity_files[i]);
Damien Miller0a80ca12010-02-27 07:55:05 +11002212 identity_files[n_ids] = filename;
2213 identity_keys[n_ids] = public;
djm@openbsd.org3eb7f102018-07-16 07:06:50 +00002214 identity_file_userprovided[n_ids] =
2215 options.identity_file_userprovided[i];
Damien Miller0a80ca12010-02-27 07:55:05 +11002216 if (++n_ids >= SSH_MAX_IDENTITY_FILES)
2217 continue;
2218
djm@openbsd.org4e44a792015-09-24 06:15:11 +00002219 /*
2220 * If no certificates have been explicitly listed then try
2221 * to add the default certificate variant too.
2222 */
2223 if (options.num_certificate_files != 0)
2224 continue;
Damien Miller0a80ca12010-02-27 07:55:05 +11002225 xasprintf(&cp, "%s-cert", filename);
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00002226 check_load(sshkey_load_public(cp, &public, NULL),
2227 filename, "pubkey");
Damien Miller0a80ca12010-02-27 07:55:05 +11002228 debug("identity file %s type %d", cp,
2229 public ? public->type : -1);
2230 if (public == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +10002231 free(cp);
Damien Miller0a80ca12010-02-27 07:55:05 +11002232 continue;
2233 }
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00002234 if (!sshkey_is_cert(public)) {
Damien Miller0a80ca12010-02-27 07:55:05 +11002235 debug("%s: key %s type %s is not a certificate",
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00002236 __func__, cp, sshkey_type(public));
2237 sshkey_free(public);
Darren Tuckera627d422013-06-02 07:31:17 +10002238 free(cp);
Damien Miller0a80ca12010-02-27 07:55:05 +11002239 continue;
2240 }
djm@openbsd.orgb4867e02016-12-06 07:48:01 +00002241 /* NB. leave filename pointing to private key */
2242 identity_files[n_ids] = xstrdup(filename);
Damien Miller0a80ca12010-02-27 07:55:05 +11002243 identity_keys[n_ids] = public;
djm@openbsd.org3eb7f102018-07-16 07:06:50 +00002244 identity_file_userprovided[n_ids] =
2245 options.identity_file_userprovided[i];
Damien Miller0a80ca12010-02-27 07:55:05 +11002246 n_ids++;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00002247 }
djm@openbsd.org4e44a792015-09-24 06:15:11 +00002248
2249 if (options.num_certificate_files > SSH_MAX_CERTIFICATE_FILES)
2250 fatal("%s: too many certificates", __func__);
2251 for (i = 0; i < options.num_certificate_files; i++) {
2252 cp = tilde_expand_filename(options.certificate_files[i],
dtucker@openbsd.orge655ee02018-07-27 05:34:42 +00002253 getuid());
dtucker@openbsd.org990687a2020-04-10 00:52:07 +00002254 filename = default_client_percent_expand(cp,
2255 pw->pw_dir, host, options.user, pw->pw_name);
djm@openbsd.org4e44a792015-09-24 06:15:11 +00002256 free(cp);
2257
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00002258 check_load(sshkey_load_public(filename, &public, NULL),
2259 filename, "certificate");
djm@openbsd.org4e44a792015-09-24 06:15:11 +00002260 debug("certificate file %s type %d", filename,
2261 public ? public->type : -1);
2262 free(options.certificate_files[i]);
2263 options.certificate_files[i] = NULL;
2264 if (public == NULL) {
2265 free(filename);
2266 continue;
2267 }
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00002268 if (!sshkey_is_cert(public)) {
djm@openbsd.org4e44a792015-09-24 06:15:11 +00002269 debug("%s: key %s type %s is not a certificate",
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00002270 __func__, filename, sshkey_type(public));
2271 sshkey_free(public);
djm@openbsd.org4e44a792015-09-24 06:15:11 +00002272 free(filename);
2273 continue;
2274 }
2275 certificate_files[n_certs] = filename;
2276 certificates[n_certs] = public;
djm@openbsd.org3eb7f102018-07-16 07:06:50 +00002277 certificate_file_userprovided[n_certs] =
2278 options.certificate_file_userprovided[i];
djm@openbsd.org4e44a792015-09-24 06:15:11 +00002279 ++n_certs;
2280 }
2281
Damien Miller0a80ca12010-02-27 07:55:05 +11002282 options.num_identity_files = n_ids;
2283 memcpy(options.identity_files, identity_files, sizeof(identity_files));
2284 memcpy(options.identity_keys, identity_keys, sizeof(identity_keys));
djm@openbsd.org3eb7f102018-07-16 07:06:50 +00002285 memcpy(options.identity_file_userprovided,
2286 identity_file_userprovided, sizeof(identity_file_userprovided));
Damien Miller0a80ca12010-02-27 07:55:05 +11002287
djm@openbsd.org4e44a792015-09-24 06:15:11 +00002288 options.num_certificate_files = n_certs;
2289 memcpy(options.certificate_files,
2290 certificate_files, sizeof(certificate_files));
2291 memcpy(options.certificates, certificates, sizeof(certificates));
djm@openbsd.org3eb7f102018-07-16 07:06:50 +00002292 memcpy(options.certificate_file_userprovided,
2293 certificate_file_userprovided,
2294 sizeof(certificate_file_userprovided));
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00002295}
Damien Miller857b02e2010-09-24 22:02:56 +10002296
2297static void
2298main_sigchld_handler(int sig)
2299{
2300 int save_errno = errno;
2301 pid_t pid;
2302 int status;
2303
2304 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00002305 (pid == -1 && errno == EINTR))
Damien Miller857b02e2010-09-24 22:02:56 +10002306 ;
Damien Miller857b02e2010-09-24 22:02:56 +10002307 errno = save_errno;
2308}