blob: 13299f7d1bbeea4322e2c275178facc3968d87da [file] [log] [blame]
naddy@openbsd.org189550f2019-11-18 16:10:05 +00001/* $OpenBSD: ssh.c,v 1.509 2019/11/18 16:10:05 naddy 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"
Damien Miller9c386432014-07-03 21:27:46 +100089#include "digest.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000090#include "packet.h"
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +000091#include "sshbuf.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000092#include "channels.h"
markus@openbsd.org5467fbc2018-07-11 18:53:29 +000093#include "sshkey.h"
Damien Miller994cf142000-07-21 10:19:44 +100094#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100095#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000096#include "pathnames.h"
Damien Miller0e220db2004-06-15 10:34:08 +100097#include "dispatch.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +000098#include "clientloop.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000099#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +1000100#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000101#include "readconf.h"
102#include "sshconnect.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000103#include "kex.h"
104#include "mac.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +1000105#include "sshpty.h"
Darren Tucker46bc0752004-05-02 22:11:30 +1000106#include "match.h"
Damien Miller0e220db2004-06-15 10:34:08 +1000107#include "msg.h"
Damien Millerb7576772006-07-10 20:23:39 +1000108#include "version.h"
djm@openbsd.org141efe42015-01-14 20:05:27 +0000109#include "ssherr.h"
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000110#include "myproposal.h"
Damien Millerdda78a02016-12-12 13:57:10 +1100111#include "utf8.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112
Damien Miller7ea845e2010-02-12 09:21:02 +1100113#ifdef ENABLE_PKCS11
114#include "ssh-pkcs11.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +0000115#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000116
Damien Miller3f905871999-11-15 17:10:57 +1100117extern char *__progname;
Damien Miller3f905871999-11-15 17:10:57 +1100118
Damien Millerea2c1a42011-06-03 12:10:22 +1000119/* Saves a copy of argv for setproctitle emulation */
120#ifndef HAVE_SETPROCTITLE
121static char **saved_av;
122#endif
123
Darren Tuckerd6173c02008-06-13 04:52:53 +1000124/* Flag indicating whether debug mode is on. May be set on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125int debug_flag = 0;
126
Damien Miller21771e22011-05-15 08:45:50 +1000127/* Flag indicating whether a tty should be requested */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128int tty_flag = 0;
129
Damien Miller1383bd82000-04-06 12:32:37 +1000130/* don't exec a shell */
131int no_shell_flag = 0;
Damien Miller1383bd82000-04-06 12:32:37 +1000132
Damien Miller5428f641999-11-25 11:54:57 +1100133/*
134 * Flag indicating that nothing should be read from stdin. This can be set
135 * on the command line.
136 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137int stdin_null_flag = 0;
138
Damien Miller5428f641999-11-25 11:54:57 +1100139/*
Damien Millere11e1ea2010-08-03 16:04:46 +1000140 * Flag indicating that the current process should be backgrounded and
141 * a new slave launched in the foreground for ControlPersist.
142 */
143int need_controlpersist_detach = 0;
144
145/* Copies of flags for ControlPersist foreground slave */
Damien Miller21771e22011-05-15 08:45:50 +1000146int ostdin_null_flag, ono_shell_flag, otty_flag, orequest_tty;
Damien Millere11e1ea2010-08-03 16:04:46 +1000147
148/*
Damien Miller5428f641999-11-25 11:54:57 +1100149 * Flag indicating that ssh should fork after authentication. This is useful
Ben Lindstromf666fec2002-06-06 19:51:58 +0000150 * so that the passphrase can be entered manually, and then ssh goes to the
Damien Miller5428f641999-11-25 11:54:57 +1100151 * background.
152 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000153int fork_after_authentication_flag = 0;
154
Damien Miller5428f641999-11-25 11:54:57 +1100155/*
156 * General data structure for command line options and options configurable
157 * in configuration files. See readconf.h.
158 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000159Options options;
160
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000161/* optional user configfile */
162char *config = NULL;
163
Damien Miller5428f641999-11-25 11:54:57 +1100164/*
165 * Name of the host we are connecting to. This is the name given on the
jmc@openbsd.org73491492019-06-12 11:31:50 +0000166 * command line, or the Hostname specified for the user-supplied name in a
Damien Miller5428f641999-11-25 11:54:57 +1100167 * configuration file.
168 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000169char *host;
170
djm@openbsd.orgb7548b12017-10-23 05:08:00 +0000171/* Various strings used to to percent_expand() arguments */
172static char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
173static char uidstr[32], *host_arg, *conn_hash_hex;
174
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000175/* socket address the host resolves to */
Damien Miller34132e52000-01-14 15:45:46 +1100176struct sockaddr_storage hostaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000177
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000178/* Private host keys. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000179Sensitive sensitive_data;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000180
Damien Miller1383bd82000-04-06 12:32:37 +1000181/* command to be executed */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000182struct sshbuf *command;
Damien Miller1383bd82000-04-06 12:32:37 +1000183
Damien Miller832562e2001-01-30 09:30:01 +1100184/* Should we execute a command or invoke a subsystem? */
185int subsystem_flag = 0;
186
Damien Miller2797f7f2002-04-23 21:09:44 +1000187/* # of replies received for global requests */
Darren Tucker9f407c42008-06-13 04:50:27 +1000188static int remote_forward_confirms_received = 0;
Damien Miller2797f7f2002-04-23 21:09:44 +1000189
Damien Millerb1cbfa22008-05-19 16:00:08 +1000190/* mux.c */
191extern int muxserver_sock;
192extern u_int muxclient_command;
Damien Miller0e220db2004-06-15 10:34:08 +1000193
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000194/* Prints a help message to the user. This function never returns. */
195
Ben Lindstrombba81212001-06-25 05:01:22 +0000196static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000197usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000198{
Damien Miller50955102004-03-22 09:34:58 +1100199 fprintf(stderr,
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000200"usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-B bind_interface]\n"
201" [-b bind_address] [-c cipher_spec] [-D [bind_address:]port]\n"
202" [-E log_file] [-e escape_char] [-F configfile] [-I pkcs11]\n"
203" [-i identity_file] [-J [user@]host[:port]] [-L address]\n"
204" [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
205" [-Q query_option] [-R address] [-S ctl_path] [-W host:port]\n"
206" [-w local_tun[:remote_tun]] destination [command]\n"
Damien Miller50955102004-03-22 09:34:58 +1100207 );
Darren Tuckere9a9b712005-12-20 16:15:51 +1100208 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209}
210
djm@openbsd.orgb7548b12017-10-23 05:08:00 +0000211static int ssh_session2(struct ssh *, struct passwd *);
212static void load_public_identity_files(struct passwd *);
Damien Miller857b02e2010-09-24 22:02:56 +1000213static void main_sigchld_handler(int);
Damien Millerb1cbfa22008-05-19 16:00:08 +1000214
Damien Miller295ee632011-05-29 21:42:31 +1000215/* ~/ expand a list of paths. NB. assumes path[n] is heap-allocated. */
216static void
217tilde_expand_paths(char **paths, u_int num_paths)
218{
219 u_int i;
220 char *cp;
221
222 for (i = 0; i < num_paths; i++) {
dtucker@openbsd.orge655ee02018-07-27 05:34:42 +0000223 cp = tilde_expand_filename(paths[i], getuid());
Darren Tuckera627d422013-06-02 07:31:17 +1000224 free(paths[i]);
Damien Miller295ee632011-05-29 21:42:31 +1000225 paths[i] = cp;
226 }
227}
228
Damien Miller13f97b22014-02-24 15:57:55 +1100229/*
230 * Attempt to resolve a host name / port to a set of addresses and
231 * optionally return any CNAMEs encountered along the way.
232 * Returns NULL on failure.
233 * NB. this function must operate with a options having undefined members.
234 */
Damien Miller0faf7472013-10-17 11:47:23 +1100235static struct addrinfo *
Damien Miller13f97b22014-02-24 15:57:55 +1100236resolve_host(const char *name, int port, int logerr, char *cname, size_t clen)
Damien Miller0faf7472013-10-17 11:47:23 +1100237{
238 char strport[NI_MAXSERV];
239 struct addrinfo hints, *res;
dtucker@openbsd.org3a7db912019-04-23 11:56:41 +0000240 int gaierr;
241 LogLevel loglevel = SYSLOG_LEVEL_DEBUG1;
Damien Miller0faf7472013-10-17 11:47:23 +1100242
Damien Miller13f97b22014-02-24 15:57:55 +1100243 if (port <= 0)
244 port = default_ssh_port();
245
djm@openbsd.orgb1d38a32015-10-15 23:51:40 +0000246 snprintf(strport, sizeof strport, "%d", port);
Damien Miller1d2c4562014-02-04 11:18:20 +1100247 memset(&hints, 0, sizeof(hints));
Damien Miller13f97b22014-02-24 15:57:55 +1100248 hints.ai_family = options.address_family == -1 ?
249 AF_UNSPEC : options.address_family;
Damien Miller0faf7472013-10-17 11:47:23 +1100250 hints.ai_socktype = SOCK_STREAM;
251 if (cname != NULL)
252 hints.ai_flags = AI_CANONNAME;
253 if ((gaierr = getaddrinfo(name, strport, &hints, &res)) != 0) {
254 if (logerr || (gaierr != EAI_NONAME && gaierr != EAI_NODATA))
255 loglevel = SYSLOG_LEVEL_ERROR;
256 do_log2(loglevel, "%s: Could not resolve hostname %.100s: %s",
257 __progname, name, ssh_gai_strerror(gaierr));
258 return NULL;
259 }
260 if (cname != NULL && res->ai_canonname != NULL) {
261 if (strlcpy(cname, res->ai_canonname, clen) >= clen) {
262 error("%s: host \"%s\" cname \"%s\" too long (max %lu)",
263 __func__, name, res->ai_canonname, (u_long)clen);
264 if (clen > 0)
265 *cname = '\0';
266 }
267 }
268 return res;
269}
270
djm@openbsd.orgfc21ea92018-01-23 05:06:25 +0000271/* Returns non-zero if name can only be an address and not a hostname */
272static int
273is_addr_fast(const char *name)
274{
275 return (strchr(name, '%') != NULL || strchr(name, ':') != NULL ||
276 strspn(name, "0123456789.") == strlen(name));
277}
278
279/* Returns non-zero if name represents a valid, single address */
280static int
281is_addr(const char *name)
282{
283 char strport[NI_MAXSERV];
284 struct addrinfo hints, *res;
285
286 if (is_addr_fast(name))
287 return 1;
288
289 snprintf(strport, sizeof strport, "%u", default_ssh_port());
290 memset(&hints, 0, sizeof(hints));
291 hints.ai_family = options.address_family == -1 ?
292 AF_UNSPEC : options.address_family;
293 hints.ai_socktype = SOCK_STREAM;
294 hints.ai_flags = AI_NUMERICHOST|AI_NUMERICSERV;
295 if (getaddrinfo(name, strport, &hints, &res) != 0)
296 return 0;
297 if (res == NULL || res->ai_next != NULL) {
298 freeaddrinfo(res);
299 return 0;
300 }
301 freeaddrinfo(res);
302 return 1;
303}
304
Damien Miller0faf7472013-10-17 11:47:23 +1100305/*
djm@openbsd.org90109022015-01-16 07:19:48 +0000306 * Attempt to resolve a numeric host address / port to a single address.
307 * Returns a canonical address string.
308 * Returns NULL on failure.
309 * NB. this function must operate with a options having undefined members.
310 */
311static struct addrinfo *
312resolve_addr(const char *name, int port, char *caddr, size_t clen)
313{
314 char addr[NI_MAXHOST], strport[NI_MAXSERV];
315 struct addrinfo hints, *res;
316 int gaierr;
317
318 if (port <= 0)
319 port = default_ssh_port();
320 snprintf(strport, sizeof strport, "%u", port);
321 memset(&hints, 0, sizeof(hints));
322 hints.ai_family = options.address_family == -1 ?
323 AF_UNSPEC : options.address_family;
324 hints.ai_socktype = SOCK_STREAM;
325 hints.ai_flags = AI_NUMERICHOST|AI_NUMERICSERV;
326 if ((gaierr = getaddrinfo(name, strport, &hints, &res)) != 0) {
327 debug2("%s: could not resolve name %.100s as address: %s",
328 __func__, name, ssh_gai_strerror(gaierr));
329 return NULL;
330 }
331 if (res == NULL) {
332 debug("%s: getaddrinfo %.100s returned no addresses",
333 __func__, name);
334 return NULL;
335 }
336 if (res->ai_next != NULL) {
337 debug("%s: getaddrinfo %.100s returned multiple addresses",
338 __func__, name);
339 goto fail;
340 }
341 if ((gaierr = getnameinfo(res->ai_addr, res->ai_addrlen,
342 addr, sizeof(addr), NULL, 0, NI_NUMERICHOST)) != 0) {
343 debug("%s: Could not format address for name %.100s: %s",
344 __func__, name, ssh_gai_strerror(gaierr));
345 goto fail;
346 }
347 if (strlcpy(caddr, addr, clen) >= clen) {
348 error("%s: host \"%s\" addr \"%s\" too long (max %lu)",
349 __func__, name, addr, (u_long)clen);
350 if (clen > 0)
351 *caddr = '\0';
352 fail:
353 freeaddrinfo(res);
354 return NULL;
355 }
356 return res;
357}
358
359/*
Damien Miller0faf7472013-10-17 11:47:23 +1100360 * Check whether the cname is a permitted replacement for the hostname
361 * and perform the replacement if it is.
Damien Miller13f97b22014-02-24 15:57:55 +1100362 * NB. this function must operate with a options having undefined members.
Damien Miller0faf7472013-10-17 11:47:23 +1100363 */
364static int
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000365check_follow_cname(int direct, char **namep, const char *cname)
Damien Miller0faf7472013-10-17 11:47:23 +1100366{
367 int i;
368 struct allowed_cname *rule;
369
370 if (*cname == '\0' || options.num_permitted_cnames == 0 ||
371 strcmp(*namep, cname) == 0)
372 return 0;
Damien Miller38505592013-10-17 11:48:13 +1100373 if (options.canonicalize_hostname == SSH_CANONICALISE_NO)
Damien Miller0faf7472013-10-17 11:47:23 +1100374 return 0;
375 /*
Damien Miller38505592013-10-17 11:48:13 +1100376 * Don't attempt to canonicalize names that will be interpreted by
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000377 * a proxy or jump host unless the user specifically requests so.
Damien Miller0faf7472013-10-17 11:47:23 +1100378 */
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000379 if (!direct &&
Damien Miller38505592013-10-17 11:48:13 +1100380 options.canonicalize_hostname != SSH_CANONICALISE_ALWAYS)
Damien Miller0faf7472013-10-17 11:47:23 +1100381 return 0;
382 debug3("%s: check \"%s\" CNAME \"%s\"", __func__, *namep, cname);
383 for (i = 0; i < options.num_permitted_cnames; i++) {
384 rule = options.permitted_cnames + i;
djm@openbsd.orge661a862015-05-04 06:10:48 +0000385 if (match_pattern_list(*namep, rule->source_list, 1) != 1 ||
386 match_pattern_list(cname, rule->target_list, 1) != 1)
Damien Miller0faf7472013-10-17 11:47:23 +1100387 continue;
Damien Miller38505592013-10-17 11:48:13 +1100388 verbose("Canonicalized DNS aliased hostname "
Damien Miller0faf7472013-10-17 11:47:23 +1100389 "\"%s\" => \"%s\"", *namep, cname);
390 free(*namep);
391 *namep = xstrdup(cname);
392 return 1;
393 }
394 return 0;
395}
396
397/*
398 * Attempt to resolve the supplied hostname after applying the user's
Damien Miller51682fa2013-10-17 11:48:31 +1100399 * canonicalization rules. Returns the address list for the host or NULL
400 * if no name was found after canonicalization.
Damien Miller13f97b22014-02-24 15:57:55 +1100401 * NB. this function must operate with a options having undefined members.
Damien Miller0faf7472013-10-17 11:47:23 +1100402 */
403static struct addrinfo *
Damien Miller13f97b22014-02-24 15:57:55 +1100404resolve_canonicalize(char **hostp, int port)
Damien Miller0faf7472013-10-17 11:47:23 +1100405{
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000406 int i, direct, ndots;
djm@openbsd.org90109022015-01-16 07:19:48 +0000407 char *cp, *fullhost, newname[NI_MAXHOST];
Damien Miller0faf7472013-10-17 11:47:23 +1100408 struct addrinfo *addrs;
409
djm@openbsd.orgfc21ea92018-01-23 05:06:25 +0000410 /*
411 * Attempt to canonicalise addresses, regardless of
412 * whether hostname canonicalisation was requested
413 */
414 if ((addrs = resolve_addr(*hostp, port,
415 newname, sizeof(newname))) != NULL) {
416 debug2("%s: hostname %.100s is address", __func__, *hostp);
417 if (strcasecmp(*hostp, newname) != 0) {
418 debug2("%s: canonicalised address \"%s\" => \"%s\"",
419 __func__, *hostp, newname);
420 free(*hostp);
421 *hostp = xstrdup(newname);
422 }
423 return addrs;
424 }
425
426 /*
427 * If this looks like an address but didn't parse as one, it might
428 * be an address with an invalid interface scope. Skip further
429 * attempts at canonicalisation.
430 */
431 if (is_addr_fast(*hostp)) {
432 debug("%s: hostname %.100s is an unrecognised address",
433 __func__, *hostp);
434 return NULL;
435 }
436
Damien Miller38505592013-10-17 11:48:13 +1100437 if (options.canonicalize_hostname == SSH_CANONICALISE_NO)
Damien Miller0faf7472013-10-17 11:47:23 +1100438 return NULL;
Damien Miller13f97b22014-02-24 15:57:55 +1100439
Damien Miller0faf7472013-10-17 11:47:23 +1100440 /*
Damien Miller38505592013-10-17 11:48:13 +1100441 * Don't attempt to canonicalize names that will be interpreted by
Damien Miller0faf7472013-10-17 11:47:23 +1100442 * a proxy unless the user specifically requests so.
443 */
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000444 direct = option_clear_or_none(options.proxy_command) &&
445 options.jump_host == NULL;
446 if (!direct &&
Damien Miller38505592013-10-17 11:48:13 +1100447 options.canonicalize_hostname != SSH_CANONICALISE_ALWAYS)
Damien Miller0faf7472013-10-17 11:47:23 +1100448 return NULL;
Damien Miller13f97b22014-02-24 15:57:55 +1100449
djm@openbsd.org5ee00632015-10-16 18:40:49 +0000450 /* If domain name is anchored, then resolve it now */
451 if ((*hostp)[strlen(*hostp) - 1] == '.') {
452 debug3("%s: name is fully qualified", __func__);
453 fullhost = xstrdup(*hostp);
454 if ((addrs = resolve_host(fullhost, port, 0,
455 newname, sizeof(newname))) != NULL)
456 goto found;
457 free(fullhost);
458 goto notfound;
459 }
460
Damien Miller51682fa2013-10-17 11:48:31 +1100461 /* Don't apply canonicalization to sufficiently-qualified hostnames */
Damien Miller0faf7472013-10-17 11:47:23 +1100462 ndots = 0;
463 for (cp = *hostp; *cp != '\0'; cp++) {
464 if (*cp == '.')
465 ndots++;
466 }
Damien Miller38505592013-10-17 11:48:13 +1100467 if (ndots > options.canonicalize_max_dots) {
468 debug3("%s: not canonicalizing hostname \"%s\" (max dots %d)",
469 __func__, *hostp, options.canonicalize_max_dots);
Damien Miller0faf7472013-10-17 11:47:23 +1100470 return NULL;
471 }
472 /* Attempt each supplied suffix */
473 for (i = 0; i < options.num_canonical_domains; i++) {
djm@openbsd.org90109022015-01-16 07:19:48 +0000474 *newname = '\0';
Damien Miller0faf7472013-10-17 11:47:23 +1100475 xasprintf(&fullhost, "%s.%s.", *hostp,
476 options.canonical_domains[i]);
Damien Miller13f97b22014-02-24 15:57:55 +1100477 debug3("%s: attempting \"%s\" => \"%s\"", __func__,
478 *hostp, fullhost);
479 if ((addrs = resolve_host(fullhost, port, 0,
djm@openbsd.org90109022015-01-16 07:19:48 +0000480 newname, sizeof(newname))) == NULL) {
Damien Miller0faf7472013-10-17 11:47:23 +1100481 free(fullhost);
482 continue;
483 }
djm@openbsd.org5ee00632015-10-16 18:40:49 +0000484 found:
Damien Miller0faf7472013-10-17 11:47:23 +1100485 /* Remove trailing '.' */
486 fullhost[strlen(fullhost) - 1] = '\0';
487 /* Follow CNAME if requested */
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000488 if (!check_follow_cname(direct, &fullhost, newname)) {
Damien Miller38505592013-10-17 11:48:13 +1100489 debug("Canonicalized hostname \"%s\" => \"%s\"",
Damien Miller0faf7472013-10-17 11:47:23 +1100490 *hostp, fullhost);
491 }
492 free(*hostp);
493 *hostp = fullhost;
494 return addrs;
495 }
djm@openbsd.org5ee00632015-10-16 18:40:49 +0000496 notfound:
Damien Miller38505592013-10-17 11:48:13 +1100497 if (!options.canonicalize_fallback_local)
Damien Miller13f97b22014-02-24 15:57:55 +1100498 fatal("%s: Could not resolve host \"%s\"", __progname, *hostp);
499 debug2("%s: host %s not found in any suffix", __func__, *hostp);
Damien Miller0faf7472013-10-17 11:47:23 +1100500 return NULL;
501}
502
Damien Miller95def091999-11-25 00:26:21 +1100503/*
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000504 * Check the result of hostkey loading, ignoring some errors and
505 * fatal()ing for others.
506 */
507static void
508check_load(int r, const char *path, const char *message)
509{
510 switch (r) {
511 case 0:
512 break;
513 case SSH_ERR_INTERNAL_ERROR:
514 case SSH_ERR_ALLOC_FAIL:
515 fatal("load %s \"%s\": %s", message, path, ssh_err(r));
516 case SSH_ERR_SYSTEM_ERROR:
517 /* Ignore missing files */
518 if (errno == ENOENT)
519 break;
520 /* FALLTHROUGH */
521 default:
522 error("load %s \"%s\": %s", message, path, ssh_err(r));
523 break;
524 }
525}
526
527/*
Damien Miller13f97b22014-02-24 15:57:55 +1100528 * Read per-user configuration file. Ignore the system wide config
529 * file if the user specifies a config file on the command line.
530 */
531static void
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000532process_config_files(const char *host_name, struct passwd *pw, int final_pass,
533 int *want_final_pass)
Damien Miller13f97b22014-02-24 15:57:55 +1100534{
deraadt@openbsd.org087266e2015-01-20 23:14:00 +0000535 char buf[PATH_MAX];
Damien Miller13f97b22014-02-24 15:57:55 +1100536 int r;
537
538 if (config != NULL) {
539 if (strcasecmp(config, "none") != 0 &&
djm@openbsd.orgb7548b12017-10-23 05:08:00 +0000540 !read_config_file(config, pw, host, host_name, &options,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000541 SSHCONF_USERCONF | (final_pass ? SSHCONF_FINAL : 0),
542 want_final_pass))
Damien Miller13f97b22014-02-24 15:57:55 +1100543 fatal("Can't open user config file %.100s: "
544 "%.100s", config, strerror(errno));
545 } else {
546 r = snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir,
547 _PATH_SSH_USER_CONFFILE);
548 if (r > 0 && (size_t)r < sizeof(buf))
djm@openbsd.orgb7548b12017-10-23 05:08:00 +0000549 (void)read_config_file(buf, pw, host, host_name,
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000550 &options, SSHCONF_CHECKPERM | SSHCONF_USERCONF |
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000551 (final_pass ? SSHCONF_FINAL : 0), want_final_pass);
Damien Miller13f97b22014-02-24 15:57:55 +1100552
553 /* Read systemwide configuration file after user config. */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000554 (void)read_config_file(_PATH_HOST_CONFIG_FILE, pw,
djm@openbsd.orgb7548b12017-10-23 05:08:00 +0000555 host, host_name, &options,
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000556 final_pass ? SSHCONF_FINAL : 0, want_final_pass);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000557 }
558}
559
560/* Rewrite the port number in an addrinfo list of addresses */
561static void
562set_addrinfo_port(struct addrinfo *addrs, int port)
563{
564 struct addrinfo *addr;
565
566 for (addr = addrs; addr != NULL; addr = addr->ai_next) {
567 switch (addr->ai_family) {
568 case AF_INET:
569 ((struct sockaddr_in *)addr->ai_addr)->
570 sin_port = htons(port);
571 break;
572 case AF_INET6:
573 ((struct sockaddr_in6 *)addr->ai_addr)->
574 sin6_port = htons(port);
575 break;
576 }
Damien Miller13f97b22014-02-24 15:57:55 +1100577 }
578}
579
580/*
Damien Miller95def091999-11-25 00:26:21 +1100581 * Main program for the ssh client.
582 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000583int
584main(int ac, char **av)
585{
djm@openbsd.org95767262016-03-07 19:02:43 +0000586 struct ssh *ssh = NULL;
djm@openbsd.org643c2ad2017-08-12 06:46:01 +0000587 int i, r, opt, exit_status, use_syslog, direct, timeout_ms;
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +0000588 int was_addr, config_test = 0, opt_terminated = 0, want_final_pass = 0;
djm@openbsd.orgb7548b12017-10-23 05:08:00 +0000589 char *p, *cp, *line, *argv0, buf[PATH_MAX], *logfile;
590 char cname[NI_MAXHOST];
Damien Miller95def091999-11-25 00:26:21 +1100591 struct stat st;
Ben Lindstrom086cf212001-03-05 05:56:40 +0000592 struct passwd *pw;
Ben Lindstrom5ccf63a2001-09-24 20:00:10 +0000593 extern int optind, optreset;
Damien Miller4f8e6692001-07-14 13:22:53 +1000594 extern char *optarg;
Damien Miller7acefbb2014-07-18 14:11:24 +1000595 struct Forward fwd;
Damien Miller0faf7472013-10-17 11:47:23 +1100596 struct addrinfo *addrs = NULL;
Damien Miller9c386432014-07-03 21:27:46 +1000597 struct ssh_digest_ctx *md;
598 u_char conn_hash[SSH_DIGEST_MAX_LENGTH];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000599
Darren Tuckerce321d82005-10-03 18:11:24 +1000600 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
601 sanitise_stdfd();
602
Damien Miller59d3d5b2003-08-22 09:34:41 +1000603 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000604
Damien Millerea2c1a42011-06-03 12:10:22 +1000605#ifndef HAVE_SETPROCTITLE
606 /* Prepare for later setproctitle emulation */
607 /* Save argv so it isn't clobbered by setproctitle() emulation */
608 saved_av = xcalloc(ac + 1, sizeof(*saved_av));
609 for (i = 0; i < ac; i++)
610 saved_av[i] = xstrdup(av[i]);
611 saved_av[i] = NULL;
612 compat_init_setproctitle(ac, av);
613 av = saved_av;
614#endif
615
Damien Miller42c5ec42018-11-23 10:40:06 +1100616 seed_rng();
617
Damien Miller5428f641999-11-25 11:54:57 +1100618 /*
Damien Miller00d9ae22010-08-17 01:59:31 +1000619 * Discard other fds that are hanging around. These can cause problem
620 * with backgrounded ssh processes started by ControlPersist.
621 */
622 closefrom(STDERR_FILENO + 1);
623
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000624 /* Get user data. */
dtucker@openbsd.orge655ee02018-07-27 05:34:42 +0000625 pw = getpwuid(getuid());
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000626 if (!pw) {
dtucker@openbsd.orge655ee02018-07-27 05:34:42 +0000627 logit("No user exists for uid %lu", (u_long)getuid());
Darren Tuckere9a9b712005-12-20 16:15:51 +1100628 exit(255);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000629 }
630 /* Take a copy of the returned structure. */
631 pw = pwcopy(pw);
632
Damien Miller5428f641999-11-25 11:54:57 +1100633 /*
Damien Miller5428f641999-11-25 11:54:57 +1100634 * Set our umask to something reasonable, as some files are created
635 * with the default umask. This will make them world-readable but
636 * writable only by the owner, which is ok for all files for which we
637 * don't set the modes explicitly.
638 */
Damien Miller95def091999-11-25 00:26:21 +1100639 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000640
Damien Millerdda78a02016-12-12 13:57:10 +1100641 msetlocale();
djm@openbsd.org65c6c6b2016-07-17 04:20:16 +0000642
Darren Tuckerd6173c02008-06-13 04:52:53 +1000643 /*
644 * Initialize option structure to indicate that no values have been
645 * set.
646 */
Damien Miller95def091999-11-25 00:26:21 +1100647 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000648
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000649 /*
650 * Prepare main ssh transport/connection structures
651 */
652 if ((ssh = ssh_alloc_session_state()) == NULL)
653 fatal("Couldn't allocate session state");
654 channel_init_channels(ssh);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000655
Damien Miller95def091999-11-25 00:26:21 +1100656 /* Parse command-line arguments. */
657 host = NULL;
Damien Millere272a5b2008-11-03 19:22:37 +1100658 use_syslog = 0;
Damien Miller03d4d7e2013-04-23 15:21:06 +1000659 logfile = NULL;
Darren Tucker72efd742009-06-21 17:48:00 +1000660 argv0 = av[0];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000661
Damien Miller2ecb6bd2006-03-15 12:03:53 +1100662 again:
Darren Tuckerd6173c02008-06-13 04:52:53 +1000663 while ((opt = getopt(ac, av, "1246ab:c:e:fgi:kl:m:no:p:qstvx"
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000664 "AB:CD:E:F:GI:J:KL:MNO:PQ:R:S:TVw:W:XYy")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100665 switch (opt) {
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000666 case '1':
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000667 fatal("SSH protocol v.1 is no longer supported");
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000668 break;
Damien Miller4af51302000-04-16 11:18:38 +1000669 case '2':
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000670 /* Ignored */
Damien Miller4af51302000-04-16 11:18:38 +1000671 break;
Damien Miller34132e52000-01-14 15:45:46 +1100672 case '4':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000673 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100674 break;
Damien Miller34132e52000-01-14 15:45:46 +1100675 case '6':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000676 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100677 break;
Damien Miller95def091999-11-25 00:26:21 +1100678 case 'n':
679 stdin_null_flag = 1;
680 break;
Damien Miller95def091999-11-25 00:26:21 +1100681 case 'f':
682 fork_after_authentication_flag = 1;
683 stdin_null_flag = 1;
684 break;
Damien Miller95def091999-11-25 00:26:21 +1100685 case 'x':
686 options.forward_x11 = 0;
687 break;
Damien Miller95def091999-11-25 00:26:21 +1100688 case 'X':
689 options.forward_x11 = 1;
690 break;
Damien Millere272a5b2008-11-03 19:22:37 +1100691 case 'y':
692 use_syslog = 1;
693 break;
Damien Miller03d4d7e2013-04-23 15:21:06 +1000694 case 'E':
dtucker@openbsd.org4f7cc2f2015-09-04 08:21:47 +0000695 logfile = optarg;
Damien Miller03d4d7e2013-04-23 15:21:06 +1000696 break;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000697 case 'G':
698 config_test = 1;
699 break;
Darren Tucker0a118da2003-10-15 15:54:32 +1000700 case 'Y':
701 options.forward_x11 = 1;
702 options.forward_x11_trusted = 1;
703 break;
Damien Miller95def091999-11-25 00:26:21 +1100704 case 'g':
Damien Miller7acefbb2014-07-18 14:11:24 +1000705 options.fwd_opts.gateway_ports = 1;
Damien Miller95def091999-11-25 00:26:21 +1100706 break;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100707 case 'O':
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +0000708 if (options.stdio_forward_host != NULL)
Damien Millere1537f92010-01-26 13:26:22 +1100709 fatal("Cannot specify multiplexing "
710 "command with -W");
711 else if (muxclient_command != 0)
712 fatal("Multiplexing command already specified");
Darren Tucker7ebfc102004-11-07 20:06:19 +1100713 if (strcmp(optarg, "check") == 0)
Damien Millerb1cbfa22008-05-19 16:00:08 +1000714 muxclient_command = SSHMUX_COMMAND_ALIVE_CHECK;
Damien Miller388f6fc2010-05-21 14:57:35 +1000715 else if (strcmp(optarg, "forward") == 0)
716 muxclient_command = SSHMUX_COMMAND_FORWARD;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100717 else if (strcmp(optarg, "exit") == 0)
Damien Millerb1cbfa22008-05-19 16:00:08 +1000718 muxclient_command = SSHMUX_COMMAND_TERMINATE;
Damien Miller6c3eec72011-05-05 14:16:22 +1000719 else if (strcmp(optarg, "stop") == 0)
720 muxclient_command = SSHMUX_COMMAND_STOP;
Damien Millerf6dff7c2011-09-22 21:38:52 +1000721 else if (strcmp(optarg, "cancel") == 0)
722 muxclient_command = SSHMUX_COMMAND_CANCEL_FWD;
markus@openbsd.org8d057842016-09-30 09:19:13 +0000723 else if (strcmp(optarg, "proxy") == 0)
724 muxclient_command = SSHMUX_COMMAND_PROXY;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100725 else
726 fatal("Invalid multiplex command.");
727 break;
Damien Miller147bba32002-09-04 16:46:06 +1000728 case 'P': /* deprecated */
Damien Miller95def091999-11-25 00:26:21 +1100729 break;
Damien Millerd937dc02013-12-05 10:19:54 +1100730 case 'Q':
Damien Millerea111192013-04-23 19:24:32 +1000731 cp = NULL;
Damien Millerd937dc02013-12-05 10:19:54 +1100732 if (strcmp(optarg, "cipher") == 0)
Damien Miller0fde8ac2013-11-21 14:12:23 +1100733 cp = cipher_alg_list('\n', 0);
Damien Millerd937dc02013-12-05 10:19:54 +1100734 else if (strcmp(optarg, "cipher-auth") == 0)
Damien Miller0fde8ac2013-11-21 14:12:23 +1100735 cp = cipher_alg_list('\n', 1);
Damien Millerd937dc02013-12-05 10:19:54 +1100736 else if (strcmp(optarg, "mac") == 0)
Damien Miller690d9892013-11-08 12:16:49 +1100737 cp = mac_alg_list('\n');
Damien Millerd937dc02013-12-05 10:19:54 +1100738 else if (strcmp(optarg, "kex") == 0)
Damien Miller690d9892013-11-08 12:16:49 +1100739 cp = kex_alg_list('\n');
Damien Millerd937dc02013-12-05 10:19:54 +1100740 else if (strcmp(optarg, "key") == 0)
djm@openbsd.org183ba552017-03-10 04:07:20 +0000741 cp = sshkey_alg_list(0, 0, 0, '\n');
Damien Miller5be9d9e2013-12-07 11:24:01 +1100742 else if (strcmp(optarg, "key-cert") == 0)
djm@openbsd.org183ba552017-03-10 04:07:20 +0000743 cp = sshkey_alg_list(1, 0, 0, '\n');
Damien Miller5be9d9e2013-12-07 11:24:01 +1100744 else if (strcmp(optarg, "key-plain") == 0)
djm@openbsd.org183ba552017-03-10 04:07:20 +0000745 cp = sshkey_alg_list(0, 1, 0, '\n');
djm@openbsd.org357128a2018-09-12 01:30:10 +0000746 else if (strcmp(optarg, "sig") == 0)
djm@openbsd.orgaa083aa2018-09-20 03:31:49 +0000747 cp = sshkey_alg_list(0, 1, 1, '\n');
djm@openbsd.org357128a2018-09-12 01:30:10 +0000748 else if (strcmp(optarg, "protocol-version") == 0)
djm@openbsd.org68d2dfc2015-03-03 06:48:58 +0000749 cp = xstrdup("2");
djm@openbsd.org357128a2018-09-12 01:30:10 +0000750 else if (strcmp(optarg, "help") == 0) {
751 cp = xstrdup(
752 "cipher\ncipher-auth\nkex\nkey\n"
753 "key-cert\nkey-plain\nmac\n"
754 "protocol-version\nsig");
djm@openbsd.org68d2dfc2015-03-03 06:48:58 +0000755 }
Damien Millerea111192013-04-23 19:24:32 +1000756 if (cp == NULL)
757 fatal("Unsupported query \"%s\"", optarg);
758 printf("%s\n", cp);
759 free(cp);
760 exit(0);
761 break;
Damien Miller95def091999-11-25 00:26:21 +1100762 case 'a':
763 options.forward_agent = 0;
764 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000765 case 'A':
766 options.forward_agent = 1;
767 break;
Damien Miller95def091999-11-25 00:26:21 +1100768 case 'k':
Damien Millere0113cc2003-11-24 13:10:09 +1100769 options.gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100770 break;
Darren Tucker415bddc2007-06-12 23:43:16 +1000771 case 'K':
772 options.gss_authentication = 1;
773 options.gss_deleg_creds = 1;
774 break;
Damien Miller95def091999-11-25 00:26:21 +1100775 case 'i':
dtucker@openbsd.orge655ee02018-07-27 05:34:42 +0000776 p = tilde_expand_filename(optarg, getuid());
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000777 if (stat(p, &st) == -1)
Damien Millerf4614452001-07-14 12:18:10 +1000778 fprintf(stderr, "Warning: Identity file %s "
dtucker@openbsd.org03239c12015-10-25 23:42:00 +0000779 "not accessible: %s.\n", p,
Damien Miller3eb48b62005-03-01 21:15:46 +1100780 strerror(errno));
dtucker@openbsd.org03239c12015-10-25 23:42:00 +0000781 else
782 add_identity_file(&options, NULL, p, 1);
783 free(p);
Damien Miller95def091999-11-25 00:26:21 +1100784 break;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000785 case 'I':
Damien Miller7ea845e2010-02-12 09:21:02 +1100786#ifdef ENABLE_PKCS11
dtucker@openbsd.org4f7cc2f2015-09-04 08:21:47 +0000787 free(options.pkcs11_provider);
Damien Miller7ea845e2010-02-12 09:21:02 +1100788 options.pkcs11_provider = xstrdup(optarg);
Ben Lindstrombcc18082001-08-06 21:59:25 +0000789#else
Damien Miller7ea845e2010-02-12 09:21:02 +1100790 fprintf(stderr, "no support for PKCS#11.\n");
Ben Lindstrombcc18082001-08-06 21:59:25 +0000791#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000792 break;
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000793 case 'J':
djm@openbsd.orgcb8f5652019-06-14 04:13:58 +0000794 if (options.jump_host != NULL) {
795 fatal("Only a single -J option is permitted "
796 "(use commas to separate multiple "
797 "jump hops)");
798 }
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000799 if (options.proxy_command != NULL)
800 fatal("Cannot specify -J with ProxyCommand");
801 if (parse_jump(optarg, &options, 1) == -1)
802 fatal("Invalid -J argument");
803 options.proxy_command = xstrdup("none");
804 break;
Damien Miller95def091999-11-25 00:26:21 +1100805 case 't':
Damien Miller21771e22011-05-15 08:45:50 +1000806 if (options.request_tty == REQUEST_TTY_YES)
807 options.request_tty = REQUEST_TTY_FORCE;
808 else
809 options.request_tty = REQUEST_TTY_YES;
Damien Miller95def091999-11-25 00:26:21 +1100810 break;
Damien Miller95def091999-11-25 00:26:21 +1100811 case 'v':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000812 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100813 debug_flag = 1;
814 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000815 } else {
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000816 if (options.log_level < SYSLOG_LEVEL_DEBUG3) {
817 debug_flag++;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000818 options.log_level++;
djm@openbsd.orged877ef2016-07-15 00:24:30 +0000819 }
Darren Tuckere98dfa32003-07-19 19:54:31 +1000820 }
Damien Miller03d4d7e2013-04-23 15:21:06 +1000821 break;
Damien Miller95def091999-11-25 00:26:21 +1100822 case 'V':
Damien Miller0c889cd2004-03-22 09:36:00 +1100823 fprintf(stderr, "%s, %s\n",
Damien Miller1f0311c2014-05-15 14:24:09 +1000824 SSH_RELEASE,
825#ifdef WITH_OPENSSL
djm@openbsd.orga65784c2018-10-23 05:56:35 +0000826 OpenSSL_version(OPENSSL_VERSION)
Damien Miller1f0311c2014-05-15 14:24:09 +1000827#else
828 "without OpenSSL"
829#endif
830 );
Damien Miller95def091999-11-25 00:26:21 +1100831 if (opt == 'V')
832 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100833 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100834 case 'w':
Damien Miller7b58e802005-12-13 19:33:19 +1100835 if (options.tun_open == -1)
836 options.tun_open = SSH_TUNMODE_DEFAULT;
Damien Millerd27b9472005-12-13 19:29:02 +1100837 options.tun_local = a2tun(optarg, &options.tun_remote);
Damien Miller7b58e802005-12-13 19:33:19 +1100838 if (options.tun_local == SSH_TUNID_ERR) {
Darren Tuckerd6173c02008-06-13 04:52:53 +1000839 fprintf(stderr,
840 "Bad tun device '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100841 exit(255);
Damien Millerd27b9472005-12-13 19:29:02 +1100842 }
843 break;
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100844 case 'W':
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +0000845 if (options.stdio_forward_host != NULL)
Damien Millere1537f92010-01-26 13:26:22 +1100846 fatal("stdio forward already specified");
847 if (muxclient_command != 0)
848 fatal("Cannot specify stdio forward with -O");
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100849 if (parse_forward(&fwd, optarg, 1, 0)) {
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +0000850 options.stdio_forward_host = fwd.listen_host;
851 options.stdio_forward_port = fwd.listen_port;
Darren Tuckera627d422013-06-02 07:31:17 +1000852 free(fwd.connect_host);
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100853 } else {
854 fprintf(stderr,
855 "Bad stdio forwarding specification '%s'\n",
856 optarg);
857 exit(255);
858 }
Damien Miller21771e22011-05-15 08:45:50 +1000859 options.request_tty = REQUEST_TTY_NO;
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100860 no_shell_flag = 1;
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100861 break;
Damien Miller95def091999-11-25 00:26:21 +1100862 case 'q':
863 options.log_level = SYSLOG_LEVEL_QUIET;
864 break;
Damien Miller95def091999-11-25 00:26:21 +1100865 case 'e':
866 if (optarg[0] == '^' && optarg[2] == 0 &&
Damien Millerf4614452001-07-14 12:18:10 +1000867 (u_char) optarg[1] >= 64 &&
868 (u_char) optarg[1] < 128)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000869 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100870 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000871 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100872 else if (strcmp(optarg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000873 options.escape_char = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100874 else {
Damien Millerf4614452001-07-14 12:18:10 +1000875 fprintf(stderr, "Bad escape character '%s'.\n",
876 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100877 exit(255);
Damien Miller95def091999-11-25 00:26:21 +1100878 }
879 break;
Damien Miller95def091999-11-25 00:26:21 +1100880 case 'c':
naddy@openbsd.org91a21352019-09-06 14:45:34 +0000881 if (!ciphers_valid(*optarg == '+' || *optarg == '^' ?
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000882 optarg + 1 : optarg)) {
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000883 fprintf(stderr, "Unknown cipher type '%s'\n",
884 optarg);
885 exit(255);
886 }
djm@openbsd.orgcdccebd2017-04-30 23:15:04 +0000887 free(options.ciphers);
888 options.ciphers = xstrdup(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100889 break;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000890 case 'm':
dtucker@openbsd.org4f7cc2f2015-09-04 08:21:47 +0000891 if (mac_valid(optarg)) {
892 free(options.macs);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000893 options.macs = xstrdup(optarg);
dtucker@openbsd.org4f7cc2f2015-09-04 08:21:47 +0000894 } else {
Damien Millerf4614452001-07-14 12:18:10 +1000895 fprintf(stderr, "Unknown mac type '%s'\n",
896 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100897 exit(255);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000898 }
899 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000900 case 'M':
Damien Millerd14b1e72005-06-16 13:19:41 +1000901 if (options.control_master == SSHCTL_MASTER_YES)
902 options.control_master = SSHCTL_MASTER_ASK;
903 else
904 options.control_master = SSHCTL_MASTER_YES;
Damien Miller0e220db2004-06-15 10:34:08 +1000905 break;
Damien Miller95def091999-11-25 00:26:21 +1100906 case 'p':
millert@openbsd.org887669e2017-10-21 23:06:24 +0000907 if (options.port == -1) {
908 options.port = a2port(optarg);
909 if (options.port <= 0) {
910 fprintf(stderr, "Bad port '%s'\n",
911 optarg);
912 exit(255);
913 }
Ben Lindstrom146edb92001-04-11 23:06:28 +0000914 }
Damien Miller95def091999-11-25 00:26:21 +1100915 break;
Damien Miller95def091999-11-25 00:26:21 +1100916 case 'l':
millert@openbsd.org887669e2017-10-21 23:06:24 +0000917 if (options.user == NULL)
918 options.user = optarg;
Damien Miller95def091999-11-25 00:26:21 +1100919 break;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000920
Damien Miller95def091999-11-25 00:26:21 +1100921 case 'L':
Damien Miller4bf648f2009-02-14 16:28:21 +1100922 if (parse_forward(&fwd, optarg, 0, 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +1100923 add_local_forward(&options, &fwd);
924 else {
Damien Millerf4614452001-07-14 12:18:10 +1000925 fprintf(stderr,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100926 "Bad local forwarding specification '%s'\n",
Damien Millerf4614452001-07-14 12:18:10 +1000927 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100928 exit(255);
Ben Lindstrom1a174712001-09-12 17:56:15 +0000929 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100930 break;
931
932 case 'R':
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000933 if (parse_forward(&fwd, optarg, 0, 1) ||
934 parse_forward(&fwd, optarg, 1, 1)) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100935 add_remote_forward(&options, &fwd);
936 } else {
937 fprintf(stderr,
938 "Bad remote forwarding specification "
939 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100940 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100941 }
Damien Miller95def091999-11-25 00:26:21 +1100942 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000943
944 case 'D':
Damien Miller4bf648f2009-02-14 16:28:21 +1100945 if (parse_forward(&fwd, optarg, 1, 0)) {
Damien Millera699d952008-11-03 19:27:34 +1100946 add_local_forward(&options, &fwd);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100947 } else {
Damien Millera699d952008-11-03 19:27:34 +1100948 fprintf(stderr,
949 "Bad dynamic forwarding specification "
950 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100951 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000952 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000953 break;
954
Damien Miller95def091999-11-25 00:26:21 +1100955 case 'C':
956 options.compression = 1;
957 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000958 case 'N':
959 no_shell_flag = 1;
Damien Miller21771e22011-05-15 08:45:50 +1000960 options.request_tty = REQUEST_TTY_NO;
Damien Miller1383bd82000-04-06 12:32:37 +1000961 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000962 case 'T':
Damien Miller21771e22011-05-15 08:45:50 +1000963 options.request_tty = REQUEST_TTY_NO;
Damien Miller1383bd82000-04-06 12:32:37 +1000964 break;
Damien Miller95def091999-11-25 00:26:21 +1100965 case 'o':
Damien Miller9836cf82003-12-17 16:30:06 +1100966 line = xstrdup(optarg);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000967 if (process_config_line(&options, pw,
968 host ? host : "", host ? host : "", line,
969 "command-line", 0, NULL, SSHCONF_USERCONF) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100970 exit(255);
Darren Tuckera627d422013-06-02 07:31:17 +1000971 free(line);
Damien Miller95def091999-11-25 00:26:21 +1100972 break;
Damien Miller832562e2001-01-30 09:30:01 +1100973 case 's':
974 subsystem_flag = 1;
975 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000976 case 'S':
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +0000977 free(options.control_path);
Damien Miller0e220db2004-06-15 10:34:08 +1000978 options.control_path = xstrdup(optarg);
Damien Miller0e220db2004-06-15 10:34:08 +1000979 break;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000980 case 'b':
981 options.bind_address = optarg;
982 break;
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000983 case 'B':
984 options.bind_interface = optarg;
985 break;
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000986 case 'F':
987 config = optarg;
988 break;
Damien Miller95def091999-11-25 00:26:21 +1100989 default:
990 usage();
991 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000992 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000993
djm@openbsd.org643c2ad2017-08-12 06:46:01 +0000994 if (optind > 1 && strcmp(av[optind - 1], "--") == 0)
995 opt_terminated = 1;
996
Damien Millerf4614452001-07-14 12:18:10 +1000997 ac -= optind;
998 av += optind;
999
Darren Tuckerb8c884a2010-01-08 18:53:43 +11001000 if (ac > 0 && !host) {
millert@openbsd.org887669e2017-10-21 23:06:24 +00001001 int tport;
1002 char *tuser;
1003 switch (parse_ssh_uri(*av, &tuser, &host, &tport)) {
1004 case -1:
1005 usage();
1006 break;
1007 case 0:
1008 if (options.user == NULL) {
1009 options.user = tuser;
1010 tuser = NULL;
1011 }
1012 free(tuser);
1013 if (options.port == -1 && tport != -1)
1014 options.port = tport;
1015 break;
1016 default:
Damien Millerf4614452001-07-14 12:18:10 +10001017 p = xstrdup(*av);
Ben Lindstromc276c122002-12-23 02:14:51 +00001018 cp = strrchr(p, '@');
millert@openbsd.org887669e2017-10-21 23:06:24 +00001019 if (cp != NULL) {
1020 if (cp == p)
1021 usage();
1022 if (options.user == NULL) {
1023 options.user = p;
1024 p = NULL;
1025 }
1026 *cp++ = '\0';
1027 host = xstrdup(cp);
1028 free(p);
1029 } else
1030 host = p;
1031 break;
1032 }
djm@openbsd.org643c2ad2017-08-12 06:46:01 +00001033 if (ac > 1 && !opt_terminated) {
Ben Lindstromb9fa6912002-12-23 02:24:54 +00001034 optind = optreset = 1;
Damien Millerf4614452001-07-14 12:18:10 +10001035 goto again;
1036 }
Ben Lindstromb9fa6912002-12-23 02:24:54 +00001037 ac--, av++;
Damien Millerf4614452001-07-14 12:18:10 +10001038 }
1039
Damien Miller95def091999-11-25 00:26:21 +11001040 /* Check that we got a host name. */
1041 if (!host)
1042 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001043
Damien Miller0faf7472013-10-17 11:47:23 +11001044 host_arg = xstrdup(host);
1045
Damien Miller95def091999-11-25 00:26:21 +11001046 /* Initialize the command to execute on remote host. */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001047 if ((command = sshbuf_new()) == NULL)
1048 fatal("sshbuf_new failed");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001049
Damien Miller5428f641999-11-25 11:54:57 +11001050 /*
1051 * Save the command to execute on the remote host in a buffer. There
1052 * is no limit on the length of the command, except by the maximum
1053 * packet size. Also sets the tty flag if there is no command.
1054 */
Damien Millerf4614452001-07-14 12:18:10 +10001055 if (!ac) {
Damien Miller95def091999-11-25 00:26:21 +11001056 /* No command specified - execute shell on a tty. */
Damien Miller832562e2001-01-30 09:30:01 +11001057 if (subsystem_flag) {
Damien Millerf4614452001-07-14 12:18:10 +10001058 fprintf(stderr,
1059 "You must specify a subsystem to invoke.\n");
Damien Miller832562e2001-01-30 09:30:01 +11001060 usage();
1061 }
Damien Miller95def091999-11-25 00:26:21 +11001062 } else {
Damien Millerf4614452001-07-14 12:18:10 +10001063 /* A command has been specified. Store it into the buffer. */
1064 for (i = 0; i < ac; i++) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001065 if ((r = sshbuf_putf(command, "%s%s",
1066 i ? " " : "", av[i])) != 0)
1067 fatal("%s: buffer error: %s",
1068 __func__, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +11001069 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001070 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001071
Ben Lindstrom8a432f52001-03-05 07:24:46 +00001072 /*
1073 * Initialize "log" output. Since we are the client all output
Damien Miller03d4d7e2013-04-23 15:21:06 +10001074 * goes to stderr unless otherwise specified by -y or -E.
Ben Lindstrom8a432f52001-03-05 07:24:46 +00001075 */
Damien Miller03d4d7e2013-04-23 15:21:06 +10001076 if (use_syslog && logfile != NULL)
1077 fatal("Can't specify both -y and -E");
dtucker@openbsd.org4f7cc2f2015-09-04 08:21:47 +00001078 if (logfile != NULL)
Damien Miller03d4d7e2013-04-23 15:21:06 +10001079 log_redirect_stderr_to(logfile);
Darren Tucker72efd742009-06-21 17:48:00 +10001080 log_init(argv0,
djm@openbsd.org@openbsd.orgdbe06622017-10-27 01:57:06 +00001081 options.log_level == SYSLOG_LEVEL_NOT_SET ?
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001082 SYSLOG_LEVEL_INFO : options.log_level,
djm@openbsd.org@openbsd.orgdbe06622017-10-27 01:57:06 +00001083 options.log_facility == SYSLOG_FACILITY_NOT_SET ?
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001084 SYSLOG_FACILITY_USER : options.log_facility,
1085 !use_syslog);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001086
Damien Miller03d4d7e2013-04-23 15:21:06 +10001087 if (debug_flag)
Damien Miller1f0311c2014-05-15 14:24:09 +10001088 logit("%s, %s", SSH_RELEASE,
1089#ifdef WITH_OPENSSL
djm@openbsd.orga65784c2018-10-23 05:56:35 +00001090 OpenSSL_version(OPENSSL_VERSION)
Damien Miller1f0311c2014-05-15 14:24:09 +10001091#else
1092 "without OpenSSL"
1093#endif
1094 );
Damien Miller03d4d7e2013-04-23 15:21:06 +10001095
Damien Miller13f97b22014-02-24 15:57:55 +11001096 /* Parse the configuration files */
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001097 process_config_files(host_arg, pw, 0, &want_final_pass);
1098 if (want_final_pass)
1099 debug("configuration requests final Match pass");
Ben Lindstrom83f07d12001-10-03 17:22:29 +00001100
Damien Miller13f97b22014-02-24 15:57:55 +11001101 /* Hostname canonicalisation needs a few options filled. */
1102 fill_default_options_for_canonicalization(&options);
1103
1104 /* If the user has replaced the hostname then take it into use now */
1105 if (options.hostname != NULL) {
1106 /* NB. Please keep in sync with readconf.c:match_cfg_line() */
1107 cp = percent_expand(options.hostname,
1108 "h", host, (char *)NULL);
1109 free(host);
1110 host = cp;
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001111 free(options.hostname);
1112 options.hostname = xstrdup(host);
Damien Miller13f97b22014-02-24 15:57:55 +11001113 }
1114
djm@openbsd.orgfc21ea92018-01-23 05:06:25 +00001115 /* Don't lowercase addresses, they will be explicitly canonicalised */
1116 if ((was_addr = is_addr(host)) == 0)
1117 lowercase(host);
1118
1119 /*
1120 * Try to canonicalize if requested by configuration or the
1121 * hostname is an address.
1122 */
1123 if (options.canonicalize_hostname != SSH_CANONICALISE_NO || was_addr)
Damien Miller13f97b22014-02-24 15:57:55 +11001124 addrs = resolve_canonicalize(&host, options.port);
1125
1126 /*
Damien Miller08b57c62014-02-27 10:17:13 +11001127 * If CanonicalizePermittedCNAMEs have been specified but
1128 * other canonicalization did not happen (by not being requested
1129 * or by failing with fallback) then the hostname may still be changed
djm@openbsd.org@openbsd.orgdbe06622017-10-27 01:57:06 +00001130 * as a result of CNAME following.
Damien Miller08b57c62014-02-27 10:17:13 +11001131 *
1132 * Try to resolve the bare hostname name using the system resolver's
1133 * usual search rules and then apply the CNAME follow rules.
1134 *
1135 * Skip the lookup if a ProxyCommand is being used unless the user
1136 * has specifically requested canonicalisation for this case via
1137 * CanonicalizeHostname=always
Damien Miller13f97b22014-02-24 15:57:55 +11001138 */
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001139 direct = option_clear_or_none(options.proxy_command) &&
1140 options.jump_host == NULL;
1141 if (addrs == NULL && options.num_permitted_cnames != 0 && (direct ||
1142 options.canonicalize_hostname == SSH_CANONICALISE_ALWAYS)) {
Damien Miller19439e92014-07-02 15:28:40 +10001143 if ((addrs = resolve_host(host, options.port,
djm@openbsd.org383a33d2018-09-21 03:11:36 +00001144 direct, cname, sizeof(cname))) == NULL) {
Damien Miller19439e92014-07-02 15:28:40 +10001145 /* Don't fatal proxied host names not in the DNS */
djm@openbsd.org383a33d2018-09-21 03:11:36 +00001146 if (direct)
Damien Miller19439e92014-07-02 15:28:40 +10001147 cleanup_exit(255); /* logged in resolve_host */
1148 } else
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001149 check_follow_cname(direct, &host, cname);
Damien Miller13f97b22014-02-24 15:57:55 +11001150 }
1151
1152 /*
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001153 * If canonicalisation is enabled then re-parse the configuration
1154 * files as new stanzas may match.
Damien Miller13f97b22014-02-24 15:57:55 +11001155 */
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001156 if (options.canonicalize_hostname != 0 && !want_final_pass) {
1157 debug("hostname canonicalisation enabled, "
1158 "will re-parse configuration");
1159 want_final_pass = 1;
1160 }
1161
1162 if (want_final_pass) {
1163 debug("re-parsing configuration");
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001164 free(options.hostname);
1165 options.hostname = xstrdup(host);
djm@openbsd.org9e34e0c2018-11-23 05:08:07 +00001166 process_config_files(host_arg, pw, 1, NULL);
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001167 /*
1168 * Address resolution happens early with canonicalisation
1169 * enabled and the port number may have changed since, so
1170 * reset it in address list
1171 */
1172 if (addrs != NULL && options.port > 0)
1173 set_addrinfo_port(addrs, options.port);
Ben Lindstrom14f31ab2001-09-12 17:48:04 +00001174 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001175
Damien Miller95def091999-11-25 00:26:21 +11001176 /* Fill configuration defaults. */
1177 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001178
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001179 /*
1180 * If ProxyJump option specified, then construct a ProxyCommand now.
1181 */
1182 if (options.jump_host != NULL) {
1183 char port_s[8];
djm@openbsd.orgd8748b92018-06-01 03:11:49 +00001184 const char *sshbin = argv0;
1185
1186 /*
1187 * Try to use SSH indicated by argv[0], but fall back to
1188 * "ssh" if it appears unavailable.
1189 */
1190 if (strchr(argv0, '/') != NULL && access(argv0, X_OK) != 0)
1191 sshbin = "ssh";
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001192
1193 /* Consistency check */
1194 if (options.proxy_command != NULL)
1195 fatal("inconsistent options: ProxyCommand+ProxyJump");
1196 /* Never use FD passing for ProxyJump */
1197 options.proxy_use_fdpass = 0;
1198 snprintf(port_s, sizeof(port_s), "%d", options.jump_port);
1199 xasprintf(&options.proxy_command,
djm@openbsd.orgd8748b92018-06-01 03:11:49 +00001200 "%s%s%s%s%s%s%s%s%s%s%.*s -W '[%%h]:%%p' %s",
1201 sshbin,
djm@openbsd.orged877ef2016-07-15 00:24:30 +00001202 /* Optional "-l user" argument if jump_user set */
1203 options.jump_user == NULL ? "" : " -l ",
1204 options.jump_user == NULL ? "" : options.jump_user,
1205 /* Optional "-p port" argument if jump_port set */
1206 options.jump_port <= 0 ? "" : " -p ",
1207 options.jump_port <= 0 ? "" : port_s,
1208 /* Optional additional jump hosts ",..." */
1209 options.jump_extra == NULL ? "" : " -J ",
1210 options.jump_extra == NULL ? "" : options.jump_extra,
1211 /* Optional "-F" argumment if -F specified */
1212 config == NULL ? "" : " -F ",
1213 config == NULL ? "" : config,
1214 /* Optional "-v" arguments if -v set */
1215 debug_flag ? " -" : "",
1216 debug_flag, "vvv",
1217 /* Mandatory hostname */
1218 options.jump_host);
1219 debug("Setting implicit ProxyCommand from ProxyJump: %s",
1220 options.proxy_command);
1221 }
1222
Damien Miller13f97b22014-02-24 15:57:55 +11001223 if (options.port == 0)
1224 options.port = default_ssh_port();
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001225 channel_set_af(ssh, options.address_family);
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001226
Damien Millere9fc72e2013-10-15 12:14:12 +11001227 /* Tidy and check options */
1228 if (options.host_key_alias != NULL)
1229 lowercase(options.host_key_alias);
1230 if (options.proxy_command != NULL &&
1231 strcmp(options.proxy_command, "-") == 0 &&
1232 options.proxy_use_fdpass)
1233 fatal("ProxyCommand=- and ProxyUseFDPass are incompatible");
djm@openbsd.org44732de2015-02-20 22:17:21 +00001234 if (options.control_persist &&
1235 options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK) {
1236 debug("UpdateHostKeys=ask is incompatible with ControlPersist; "
1237 "disabling");
1238 options.update_hostkeys = 0;
1239 }
djm@openbsd.org88b6fcd2015-11-19 08:23:27 +00001240 if (options.connection_attempts <= 0)
1241 fatal("Invalid number of ConnectionAttempts");
Damien Millere9fc72e2013-10-15 12:14:12 +11001242
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001243 if (sshbuf_len(command) != 0 && options.remote_command != NULL)
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001244 fatal("Cannot execute command-line and remote command.");
1245
1246 /* Cannot fork to background if no command. */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001247 if (fork_after_authentication_flag && sshbuf_len(command) == 0 &&
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001248 options.remote_command == NULL && !no_shell_flag)
1249 fatal("Cannot fork into background without a command "
1250 "to execute.");
1251
Damien Miller95def091999-11-25 00:26:21 +11001252 /* reinit */
dtucker@openbsd.org68d3a2a2017-04-28 03:20:27 +00001253 log_init(argv0, options.log_level, options.log_facility, !use_syslog);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001254
Damien Millerfff9f092012-07-06 13:45:01 +10001255 if (options.request_tty == REQUEST_TTY_YES ||
1256 options.request_tty == REQUEST_TTY_FORCE)
1257 tty_flag = 1;
1258
1259 /* Allocate a tty by default if no command specified. */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001260 if (sshbuf_len(command) == 0 && options.remote_command == NULL)
Damien Millerfff9f092012-07-06 13:45:01 +10001261 tty_flag = options.request_tty != REQUEST_TTY_NO;
1262
1263 /* Force no tty */
markus@openbsd.org8d057842016-09-30 09:19:13 +00001264 if (options.request_tty == REQUEST_TTY_NO ||
1265 (muxclient_command && muxclient_command != SSHMUX_COMMAND_PROXY))
Damien Millerfff9f092012-07-06 13:45:01 +10001266 tty_flag = 0;
1267 /* Do not allocate a tty if stdin is not a tty. */
1268 if ((!isatty(fileno(stdin)) || stdin_null_flag) &&
1269 options.request_tty != REQUEST_TTY_FORCE) {
1270 if (tty_flag)
1271 logit("Pseudo-terminal will not be allocated because "
1272 "stdin is not a terminal.");
1273 tty_flag = 0;
1274 }
1275
Damien Miller95def091999-11-25 00:26:21 +11001276 if (options.user == NULL)
1277 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001278
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001279 /* Set up strings used to percent_expand() arguments */
Damien Millerdfc85fa2011-05-15 08:44:02 +10001280 if (gethostname(thishost, sizeof(thishost)) == -1)
1281 fatal("gethostname: %s", strerror(errno));
1282 strlcpy(shorthost, thishost, sizeof(shorthost));
1283 shorthost[strcspn(thishost, ".")] = '\0';
1284 snprintf(portstr, sizeof(portstr), "%d", options.port);
djm@openbsd.org9c935dd2018-06-01 03:33:53 +00001285 snprintf(uidstr, sizeof(uidstr), "%llu",
1286 (unsigned long long)pw->pw_uid);
Darren Tuckerf6b01b72008-06-13 04:56:37 +10001287
Damien Miller9c386432014-07-03 21:27:46 +10001288 if ((md = ssh_digest_start(SSH_DIGEST_SHA1)) == NULL ||
1289 ssh_digest_update(md, thishost, strlen(thishost)) < 0 ||
1290 ssh_digest_update(md, host, strlen(host)) < 0 ||
1291 ssh_digest_update(md, portstr, strlen(portstr)) < 0 ||
1292 ssh_digest_update(md, options.user, strlen(options.user)) < 0 ||
1293 ssh_digest_final(md, conn_hash, sizeof(conn_hash)) < 0)
1294 fatal("%s: mux digest failed", __func__);
1295 ssh_digest_free(md);
1296 conn_hash_hex = tohex(conn_hash, ssh_digest_bytes(SSH_DIGEST_SHA1));
1297
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001298 /*
1299 * Expand tokens in arguments. NB. LocalCommand is expanded later,
1300 * after port-forwarding is set up, so it may pick up any local
1301 * tunnel interface name allocated.
1302 */
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001303 if (options.remote_command != NULL) {
1304 debug3("expanding RemoteCommand: %s", options.remote_command);
1305 cp = options.remote_command;
1306 options.remote_command = percent_expand(cp,
1307 "C", conn_hash_hex,
1308 "L", shorthost,
1309 "d", pw->pw_dir,
1310 "h", host,
djm@openbsd.org9c935dd2018-06-01 03:33:53 +00001311 "i", uidstr,
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001312 "l", thishost,
1313 "n", host_arg,
1314 "p", portstr,
1315 "r", options.user,
1316 "u", pw->pw_name,
1317 (char *)NULL);
1318 debug3("expanded RemoteCommand: %s", options.remote_command);
1319 free(cp);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001320 if ((r = sshbuf_put(command, options.remote_command,
1321 strlen(options.remote_command))) != 0)
1322 fatal("%s: buffer error: %s", __func__, ssh_err(r));
bluhm@openbsd.org1112b532017-05-30 18:58:37 +00001323 }
1324
Damien Miller0e220db2004-06-15 10:34:08 +10001325 if (options.control_path != NULL) {
dtucker@openbsd.orge655ee02018-07-27 05:34:42 +00001326 cp = tilde_expand_filename(options.control_path, getuid());
Darren Tuckera627d422013-06-02 07:31:17 +10001327 free(options.control_path);
Damien Miller9c386432014-07-03 21:27:46 +10001328 options.control_path = percent_expand(cp,
1329 "C", conn_hash_hex,
1330 "L", shorthost,
1331 "h", host,
djm@openbsd.org9c935dd2018-06-01 03:33:53 +00001332 "i", uidstr,
Damien Miller9c386432014-07-03 21:27:46 +10001333 "l", thishost,
1334 "n", host_arg,
1335 "p", portstr,
1336 "r", options.user,
1337 "u", pw->pw_name,
djm@openbsd.org674b3b62015-09-11 03:47:28 +00001338 "i", uidstr,
Damien Millerdfc85fa2011-05-15 08:44:02 +10001339 (char *)NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001340 free(cp);
Damien Miller0e220db2004-06-15 10:34:08 +10001341 }
Damien Miller9c386432014-07-03 21:27:46 +10001342
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001343 if (config_test) {
1344 dump_client_config(&options, host);
1345 exit(0);
1346 }
1347
djm@openbsd.org884416b2019-10-31 21:18:28 +00001348 /* Expand SecurityKeyProvider if it refers to an environment variable */
1349 if (options.sk_provider != NULL && *options.sk_provider == '$' &&
1350 strlen(options.sk_provider) > 1) {
1351 if ((cp = getenv(options.sk_provider + 1)) == NULL) {
1352 debug("Security key provider %s did not resolve; "
1353 "disabling", options.sk_provider);
1354 free(options.sk_provider);
1355 options.sk_provider = NULL;
1356 } else {
1357 debug2("resolved SecurityKeyProvider %s => %s",
1358 options.sk_provider, cp);
1359 free(options.sk_provider);
1360 options.sk_provider = xstrdup(cp);
1361 }
1362 }
1363
Damien Millerb1cbfa22008-05-19 16:00:08 +10001364 if (muxclient_command != 0 && options.control_path == NULL)
Darren Tucker0814d312005-06-01 23:08:51 +10001365 fatal("No ControlPath specified for \"-O\" command");
markus@openbsd.org8d057842016-09-30 09:19:13 +00001366 if (options.control_path != NULL) {
1367 int sock;
1368 if ((sock = muxclient(options.control_path)) >= 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001369 ssh_packet_set_connection(ssh, sock, sock);
djm@openbsd.org25b2ed62019-01-19 21:36:06 +00001370 ssh_packet_set_mux(ssh);
markus@openbsd.org8d057842016-09-30 09:19:13 +00001371 goto skip_connect;
1372 }
1373 }
Damien Miller0e220db2004-06-15 10:34:08 +10001374
Damien Miller08b57c62014-02-27 10:17:13 +11001375 /*
1376 * If hostname canonicalisation was not enabled, then we may not
1377 * have yet resolved the hostname. Do so now.
1378 */
1379 if (addrs == NULL && options.proxy_command == NULL) {
djm@openbsd.orga85768a2015-09-04 04:56:09 +00001380 debug2("resolving \"%s\" port %d", host, options.port);
Damien Miller08b57c62014-02-27 10:17:13 +11001381 if ((addrs = resolve_host(host, options.port, 1,
1382 cname, sizeof(cname))) == NULL)
1383 cleanup_exit(255); /* resolve_host logs the error */
1384 }
1385
Damien Miller67bd0622007-09-17 12:06:57 +10001386 timeout_ms = options.connection_timeout * 1000;
1387
Kevin Stevesfcec7f82000-12-15 19:55:48 +00001388 /* Open a connection to the remote host. */
djm@openbsd.orgfbe24b12019-09-13 04:27:35 +00001389 if (ssh_connect(ssh, host_arg, host, addrs, &hostaddr, options.port,
Damien Miller0faf7472013-10-17 11:47:23 +11001390 options.address_family, options.connection_attempts,
dtucker@openbsd.org95d41e92018-07-19 10:28:47 +00001391 &timeout_ms, options.tcp_keep_alive) != 0)
Damien Miller0faf7472013-10-17 11:47:23 +11001392 exit(255);
1393
Damien Miller28631ce2013-10-26 10:07:56 +11001394 if (addrs != NULL)
1395 freeaddrinfo(addrs);
1396
djm@openbsd.org25b2ed62019-01-19 21:36:06 +00001397 ssh_packet_set_timeout(ssh, options.server_alive_interval,
Damien Miller0faf7472013-10-17 11:47:23 +11001398 options.server_alive_count_max);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001399
Damien Miller67bd0622007-09-17 12:06:57 +10001400 if (timeout_ms > 0)
1401 debug3("timeout: %d ms remain after connect", timeout_ms);
1402
Damien Miller5428f641999-11-25 11:54:57 +11001403 /*
dtucker@openbsd.org26efc2f2018-07-16 11:05:41 +00001404 * If we successfully made the connection and we have hostbased auth
1405 * enabled, load the public keys so we can later use the ssh-keysign
1406 * helper to sign challenges.
Damien Miller5428f641999-11-25 11:54:57 +11001407 */
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001408 sensitive_data.nkeys = 0;
1409 sensitive_data.keys = NULL;
djm@openbsd.org873d3e72017-04-30 23:18:44 +00001410 if (options.hostbased_authentication) {
dtucker@openbsd.orgac590762018-07-16 22:25:01 +00001411 sensitive_data.nkeys = 10;
Damien Millerddd63ab2006-03-31 23:10:51 +11001412 sensitive_data.keys = xcalloc(sensitive_data.nkeys,
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001413 sizeof(struct sshkey));
1414
1415 /* XXX check errors? */
dtucker@openbsd.orgac590762018-07-16 22:25:01 +00001416#define L_PUBKEY(p,o) do { \
1417 if ((o) >= sensitive_data.nkeys) \
1418 fatal("%s pubkey out of array bounds", __func__); \
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001419 check_load(sshkey_load_public(p, &(sensitive_data.keys[o]), NULL), \
dtucker@openbsd.orgac590762018-07-16 22:25:01 +00001420 p, "pubkey"); \
1421} while (0)
1422#define L_CERT(p,o) do { \
1423 if ((o) >= sensitive_data.nkeys) \
1424 fatal("%s cert out of array bounds", __func__); \
1425 check_load(sshkey_load_cert(p, &(sensitive_data.keys[o])), p, "cert"); \
1426} while (0)
Ben Lindstromf9c48842002-06-11 16:37:51 +00001427
dtucker@openbsd.org26efc2f2018-07-16 11:05:41 +00001428 if (options.hostbased_authentication == 1) {
dtucker@openbsd.orgac590762018-07-16 22:25:01 +00001429 L_CERT(_PATH_HOST_ECDSA_KEY_FILE, 0);
1430 L_CERT(_PATH_HOST_ED25519_KEY_FILE, 1);
1431 L_CERT(_PATH_HOST_RSA_KEY_FILE, 2);
1432 L_CERT(_PATH_HOST_DSA_KEY_FILE, 3);
1433 L_PUBKEY(_PATH_HOST_ECDSA_KEY_FILE, 4);
1434 L_PUBKEY(_PATH_HOST_ED25519_KEY_FILE, 5);
1435 L_PUBKEY(_PATH_HOST_RSA_KEY_FILE, 6);
1436 L_PUBKEY(_PATH_HOST_DSA_KEY_FILE, 7);
1437 L_CERT(_PATH_HOST_XMSS_KEY_FILE, 8);
1438 L_PUBKEY(_PATH_HOST_XMSS_KEY_FILE, 9);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001439 }
Damien Miller95def091999-11-25 00:26:21 +11001440 }
Damien Miller95def091999-11-25 00:26:21 +11001441
dtucker@openbsd.org258dc8b2018-07-18 11:34:04 +00001442 /* Create ~/.ssh * directory if it doesn't already exist. */
Darren Tucker45c66d72011-11-04 10:50:40 +11001443 if (config == NULL) {
1444 r = snprintf(buf, sizeof buf, "%s%s%s", pw->pw_dir,
1445 strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001446 if (r > 0 && (size_t)r < sizeof(buf) && stat(buf, &st) == -1) {
Darren Tucker8ccb7392010-09-10 12:28:24 +10001447#ifdef WITH_SELINUX
Darren Tucker45c66d72011-11-04 10:50:40 +11001448 ssh_selinux_setfscreatecon(buf);
Darren Tucker8ccb7392010-09-10 12:28:24 +10001449#endif
Darren Tucker45c66d72011-11-04 10:50:40 +11001450 if (mkdir(buf, 0700) < 0)
1451 error("Could not create directory '%.200s'.",
1452 buf);
Darren Tucker8ccb7392010-09-10 12:28:24 +10001453#ifdef WITH_SELINUX
Darren Tucker45c66d72011-11-04 10:50:40 +11001454 ssh_selinux_setfscreatecon(NULL);
Darren Tucker8ccb7392010-09-10 12:28:24 +10001455#endif
Darren Tucker45c66d72011-11-04 10:50:40 +11001456 }
Darren Tucker8ccb7392010-09-10 12:28:24 +10001457 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001458 /* load options.identity_files */
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001459 load_public_identity_files(pw);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001460
djm@openbsd.org001aa552018-04-10 00:10:49 +00001461 /* optionally set the SSH_AUTHSOCKET_ENV_NAME variable */
markus@openbsd.org1a75d142016-05-04 14:29:58 +00001462 if (options.identity_agent &&
1463 strcmp(options.identity_agent, SSH_AUTHSOCKET_ENV_NAME) != 0) {
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001464 if (strcmp(options.identity_agent, "none") == 0) {
1465 unsetenv(SSH_AUTHSOCKET_ENV_NAME);
1466 } else {
1467 p = tilde_expand_filename(options.identity_agent,
dtucker@openbsd.orge655ee02018-07-27 05:34:42 +00001468 getuid());
djm@openbsd.org9c935dd2018-06-01 03:33:53 +00001469 cp = percent_expand(p,
1470 "d", pw->pw_dir,
1471 "h", host,
1472 "i", uidstr,
1473 "l", thishost,
1474 "r", options.user,
1475 "u", pw->pw_name,
1476 (char *)NULL);
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001477 free(p);
djm@openbsd.org5eff5b82018-10-03 06:38:35 +00001478 /*
1479 * If identity_agent represents an environment variable
1480 * then recheck that it is valid (since processing with
1481 * percent_expand() may have changed it) and substitute
1482 * its value.
1483 */
1484 if (cp[0] == '$') {
1485 if (!valid_env_name(cp + 1)) {
1486 fatal("Invalid IdentityAgent "
1487 "environment variable name %s", cp);
1488 }
1489 if ((p = getenv(cp + 1)) == NULL)
1490 unsetenv(SSH_AUTHSOCKET_ENV_NAME);
1491 else
1492 setenv(SSH_AUTHSOCKET_ENV_NAME, p, 1);
1493 } else {
1494 /* identity_agent specifies a path directly */
1495 setenv(SSH_AUTHSOCKET_ENV_NAME, cp, 1);
1496 }
1497 free(cp);
markus@openbsd.orgb02ad1c2016-05-04 12:21:53 +00001498 }
1499 }
1500
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001501 /* Expand ~ in known host file names. */
Damien Miller295ee632011-05-29 21:42:31 +10001502 tilde_expand_paths(options.system_hostfiles,
1503 options.num_system_hostfiles);
1504 tilde_expand_paths(options.user_hostfiles, options.num_user_hostfiles);
Damien Miller95def091999-11-25 00:26:21 +11001505
Damien Miller07cd5892001-11-12 10:52:03 +11001506 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
Damien Miller857b02e2010-09-24 22:02:56 +10001507 signal(SIGCHLD, main_sigchld_handler);
Damien Miller07cd5892001-11-12 10:52:03 +11001508
Darren Tuckerd6173c02008-06-13 04:52:53 +10001509 /* Log into the remote system. Never returns if the login fails. */
djm@openbsd.org0a843d92018-12-27 03:25:24 +00001510 ssh_login(ssh, &sensitive_data, host, (struct sockaddr *)&hostaddr,
Damien Millerd925dcd2010-12-01 12:21:51 +11001511 options.port, pw, timeout_ms);
Damien Miller95def091999-11-25 00:26:21 +11001512
djm@openbsd.org25b2ed62019-01-19 21:36:06 +00001513 if (ssh_packet_connection_is_on_socket(ssh)) {
Damien Miller383ffe62010-06-26 10:02:03 +10001514 verbose("Authenticated to %s ([%s]:%d).", host,
djm@openbsd.org95767262016-03-07 19:02:43 +00001515 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
Damien Miller383ffe62010-06-26 10:02:03 +10001516 } else {
1517 verbose("Authenticated to %s (via proxy).", host);
1518 }
1519
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001520 /* We no longer need the private host keys. Clear them now. */
1521 if (sensitive_data.nkeys != 0) {
1522 for (i = 0; i < sensitive_data.nkeys; i++) {
1523 if (sensitive_data.keys[i] != NULL) {
1524 /* Destroys contents safely */
1525 debug3("clear hostkey %d", i);
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001526 sshkey_free(sensitive_data.keys[i]);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001527 sensitive_data.keys[i] = NULL;
1528 }
1529 }
Darren Tuckera627d422013-06-02 07:31:17 +10001530 free(sensitive_data.keys);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001531 }
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +00001532 for (i = 0; i < options.num_identity_files; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10001533 free(options.identity_files[i]);
1534 options.identity_files[i] = NULL;
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +00001535 if (options.identity_keys[i]) {
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001536 sshkey_free(options.identity_keys[i]);
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +00001537 options.identity_keys[i] = NULL;
1538 }
1539 }
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001540 for (i = 0; i < options.num_certificate_files; i++) {
1541 free(options.certificate_files[i]);
1542 options.certificate_files[i] = NULL;
1543 }
Damien Miller95def091999-11-25 00:26:21 +11001544
markus@openbsd.org8d057842016-09-30 09:19:13 +00001545 skip_connect:
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001546 exit_status = ssh_session2(ssh, pw);
djm@openbsd.org25b2ed62019-01-19 21:36:06 +00001547 ssh_packet_close(ssh);
Damien Miller8c4e18a2002-09-19 12:05:02 +10001548
Damien Millerb1cbfa22008-05-19 16:00:08 +10001549 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10001550 unlink(options.control_path);
1551
Damien Millera41ccca2010-10-07 22:07:32 +11001552 /* Kill ProxyCommand if it is running. */
1553 ssh_kill_proxy_command();
Damien Miller8c4e18a2002-09-19 12:05:02 +10001554
Damien Miller1383bd82000-04-06 12:32:37 +10001555 return exit_status;
1556}
1557
Damien Millere11e1ea2010-08-03 16:04:46 +10001558static void
1559control_persist_detach(void)
1560{
1561 pid_t pid;
djm@openbsd.orgd2d6bf82016-04-29 08:07:53 +00001562 int devnull, keep_stderr;
Damien Millere11e1ea2010-08-03 16:04:46 +10001563
1564 debug("%s: backgrounding master process", __func__);
1565
djm@openbsd.orgb8bbff32018-02-13 03:36:56 +00001566 /*
1567 * master (current process) into the background, and make the
1568 * foreground process a client of the backgrounded master.
1569 */
Damien Millere11e1ea2010-08-03 16:04:46 +10001570 switch ((pid = fork())) {
1571 case -1:
1572 fatal("%s: fork: %s", __func__, strerror(errno));
1573 case 0:
1574 /* Child: master process continues mainloop */
djm@openbsd.orgb8bbff32018-02-13 03:36:56 +00001575 break;
1576 default:
Damien Millere11e1ea2010-08-03 16:04:46 +10001577 /* Parent: set up mux slave to connect to backgrounded master */
1578 debug2("%s: background process is %ld", __func__, (long)pid);
1579 stdin_null_flag = ostdin_null_flag;
Damien Miller21771e22011-05-15 08:45:50 +10001580 options.request_tty = orequest_tty;
Damien Millere11e1ea2010-08-03 16:04:46 +10001581 tty_flag = otty_flag;
djm@openbsd.orgb8bbff32018-02-13 03:36:56 +00001582 close(muxserver_sock);
1583 muxserver_sock = -1;
Damien Miller5929c522010-09-10 11:17:02 +10001584 options.control_master = SSHCTL_MASTER_NO;
djm@openbsd.orgb8bbff32018-02-13 03:36:56 +00001585 muxclient(options.control_path);
Damien Millere11e1ea2010-08-03 16:04:46 +10001586 /* muxclient() doesn't return on success. */
djm@openbsd.orgb8bbff32018-02-13 03:36:56 +00001587 fatal("Failed to connect to new control master");
1588 }
Damien Miller00d9ae22010-08-17 01:59:31 +10001589 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
1590 error("%s: open(\"/dev/null\"): %s", __func__,
1591 strerror(errno));
1592 } else {
djm@openbsd.orgd2d6bf82016-04-29 08:07:53 +00001593 keep_stderr = log_is_on_stderr() && debug_flag;
Damien Miller00d9ae22010-08-17 01:59:31 +10001594 if (dup2(devnull, STDIN_FILENO) == -1 ||
djm@openbsd.orgd2d6bf82016-04-29 08:07:53 +00001595 dup2(devnull, STDOUT_FILENO) == -1 ||
1596 (!keep_stderr && dup2(devnull, STDERR_FILENO) == -1))
Damien Miller00d9ae22010-08-17 01:59:31 +10001597 error("%s: dup2: %s", __func__, strerror(errno));
1598 if (devnull > STDERR_FILENO)
1599 close(devnull);
1600 }
Damien Miller98e27dc2013-07-25 11:55:52 +10001601 daemon(1, 1);
Damien Millerea2c1a42011-06-03 12:10:22 +10001602 setproctitle("%s [mux]", options.control_path);
Damien Millere11e1ea2010-08-03 16:04:46 +10001603}
1604
1605/* Do fork() after authentication. Used by "ssh -f" */
1606static void
1607fork_postauth(void)
1608{
1609 if (need_controlpersist_detach)
1610 control_persist_detach();
1611 debug("forking to background");
1612 fork_after_authentication_flag = 0;
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001613 if (daemon(1, 1) == -1)
Damien Millere11e1ea2010-08-03 16:04:46 +10001614 fatal("daemon() failed: %.200s", strerror(errno));
1615}
1616
Darren Tucker9f407c42008-06-13 04:50:27 +10001617/* Callback for remote forward global requests */
1618static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001619ssh_confirm_remote_forward(struct ssh *ssh, int type, u_int32_t seq, void *ctxt)
Darren Tucker9f407c42008-06-13 04:50:27 +10001620{
Damien Miller7acefbb2014-07-18 14:11:24 +10001621 struct Forward *rfwd = (struct Forward *)ctxt;
djm@openbsd.org25b2ed62019-01-19 21:36:06 +00001622 u_int port;
1623 int r;
Darren Tucker9f407c42008-06-13 04:50:27 +10001624
Damien Miller4bf648f2009-02-14 16:28:21 +11001625 /* XXX verbose() on failure? */
Damien Miller4b3ed642014-07-02 15:29:40 +10001626 debug("remote forward %s for: listen %s%s%d, connect %s:%d",
Darren Tucker9f407c42008-06-13 04:50:27 +10001627 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
Damien Miller7acefbb2014-07-18 14:11:24 +10001628 rfwd->listen_path ? rfwd->listen_path :
1629 rfwd->listen_host ? rfwd->listen_host : "",
1630 (rfwd->listen_path || rfwd->listen_host) ? ":" : "",
1631 rfwd->listen_port, rfwd->connect_path ? rfwd->connect_path :
1632 rfwd->connect_host, rfwd->connect_port);
1633 if (rfwd->listen_path == NULL && rfwd->listen_port == 0) {
Darren Tucker68afb8c2011-10-02 18:59:03 +11001634 if (type == SSH2_MSG_REQUEST_SUCCESS) {
djm@openbsd.org25b2ed62019-01-19 21:36:06 +00001635 if ((r = sshpkt_get_u32(ssh, &port)) != 0)
1636 fatal("%s: %s", __func__, ssh_err(r));
1637 if (port > 65535) {
1638 error("Invalid allocated port %u for remote "
1639 "forward to %s:%d", port,
1640 rfwd->connect_host, rfwd->connect_port);
1641 /* Ensure failure processing runs below */
1642 type = SSH2_MSG_REQUEST_FAILURE;
1643 channel_update_permission(ssh,
1644 rfwd->handle, -1);
1645 } else {
1646 rfwd->allocated_port = (int)port;
1647 logit("Allocated port %u for remote "
1648 "forward to %s:%d",
1649 rfwd->allocated_port, rfwd->connect_host,
1650 rfwd->connect_port);
1651 channel_update_permission(ssh,
1652 rfwd->handle, rfwd->allocated_port);
1653 }
Darren Tucker68afb8c2011-10-02 18:59:03 +11001654 } else {
djm@openbsd.org115063a2018-06-06 18:22:41 +00001655 channel_update_permission(ssh, rfwd->handle, -1);
Darren Tucker68afb8c2011-10-02 18:59:03 +11001656 }
Damien Miller4bf648f2009-02-14 16:28:21 +11001657 }
djm@openbsd.org@openbsd.orgdbe06622017-10-27 01:57:06 +00001658
Darren Tucker9f407c42008-06-13 04:50:27 +10001659 if (type == SSH2_MSG_REQUEST_FAILURE) {
Damien Miller7acefbb2014-07-18 14:11:24 +10001660 if (options.exit_on_forward_failure) {
1661 if (rfwd->listen_path != NULL)
1662 fatal("Error: remote port forwarding failed "
1663 "for listen path %s", rfwd->listen_path);
1664 else
1665 fatal("Error: remote port forwarding failed "
1666 "for listen port %d", rfwd->listen_port);
1667 } else {
1668 if (rfwd->listen_path != NULL)
1669 logit("Warning: remote port forwarding failed "
1670 "for listen path %s", rfwd->listen_path);
1671 else
1672 logit("Warning: remote port forwarding failed "
1673 "for listen port %d", rfwd->listen_port);
1674 }
Darren Tucker9f407c42008-06-13 04:50:27 +10001675 }
Darren Tucker9a2a6092008-07-04 12:53:50 +10001676 if (++remote_forward_confirms_received == options.num_remote_forwards) {
Darren Tucker9f407c42008-06-13 04:50:27 +10001677 debug("All remote forwarding requests processed");
Damien Millere11e1ea2010-08-03 16:04:46 +10001678 if (fork_after_authentication_flag)
1679 fork_postauth();
Darren Tucker9a2a6092008-07-04 12:53:50 +10001680 }
Darren Tucker9f407c42008-06-13 04:50:27 +10001681}
1682
Ben Lindstrombba81212001-06-25 05:01:22 +00001683static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001684client_cleanup_stdio_fwd(struct ssh *ssh, int id, void *arg)
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001685{
1686 debug("stdio forwarding: done");
1687 cleanup_exit(0);
1688}
1689
Darren Tucker2d6665d2011-11-04 10:54:22 +11001690static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001691ssh_stdio_confirm(struct ssh *ssh, int id, int success, void *arg)
Damien Miller357610d2014-07-18 15:04:10 +10001692{
1693 if (!success)
1694 fatal("stdio forwarding failed");
1695}
1696
1697static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001698ssh_init_stdio_forwarding(struct ssh *ssh)
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001699{
1700 Channel *c;
Damien Millere1537f92010-01-26 13:26:22 +11001701 int in, out;
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001702
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +00001703 if (options.stdio_forward_host == NULL)
Darren Tucker2d6665d2011-11-04 10:54:22 +11001704 return;
Damien Millere1537f92010-01-26 13:26:22 +11001705
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +00001706 debug3("%s: %s:%d", __func__, options.stdio_forward_host,
1707 options.stdio_forward_port);
Darren Tucker2d6665d2011-11-04 10:54:22 +11001708
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001709 if ((in = dup(STDIN_FILENO)) == -1 ||
1710 (out = dup(STDOUT_FILENO)) == -1)
Damien Millere1537f92010-01-26 13:26:22 +11001711 fatal("channel_connect_stdio_fwd: dup() in/out failed");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001712 if ((c = channel_connect_stdio_fwd(ssh, options.stdio_forward_host,
dtucker@openbsd.org8543ff32016-06-03 03:14:41 +00001713 options.stdio_forward_port, in, out)) == NULL)
Darren Tucker2d6665d2011-11-04 10:54:22 +11001714 fatal("%s: channel_connect_stdio_fwd failed", __func__);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001715 channel_register_cleanup(ssh, c->self, client_cleanup_stdio_fwd, 0);
1716 channel_register_open_confirm(ssh, c->self, ssh_stdio_confirm, NULL);
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001717}
1718
1719static void
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001720ssh_init_forwarding(struct ssh *ssh, char **ifname)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001721{
Kevin Steves12057502001-02-05 14:54:34 +00001722 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001723 int i;
Kevin Steves12057502001-02-05 14:54:34 +00001724
Damien Miller0bc1bd82000-11-13 22:57:25 +11001725 /* Initiate local TCP/IP port forwardings. */
1726 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001727 debug("Local connections to %.200s:%d forwarded to remote "
1728 "address %.200s:%d",
Damien Miller7acefbb2014-07-18 14:11:24 +10001729 (options.local_forwards[i].listen_path != NULL) ?
1730 options.local_forwards[i].listen_path :
Darren Tucker47eede72005-03-14 23:08:12 +11001731 (options.local_forwards[i].listen_host == NULL) ?
Damien Miller7acefbb2014-07-18 14:11:24 +10001732 (options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") :
Damien Millerf91ee4c2005-03-01 21:24:33 +11001733 options.local_forwards[i].listen_host,
1734 options.local_forwards[i].listen_port,
Damien Miller7acefbb2014-07-18 14:11:24 +10001735 (options.local_forwards[i].connect_path != NULL) ?
1736 options.local_forwards[i].connect_path :
Damien Millerf91ee4c2005-03-01 21:24:33 +11001737 options.local_forwards[i].connect_host,
1738 options.local_forwards[i].connect_port);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001739 success += channel_setup_local_fwd_listener(ssh,
Damien Miller7acefbb2014-07-18 14:11:24 +10001740 &options.local_forwards[i], &options.fwd_opts);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001741 }
Darren Tuckere7d4b192006-07-12 22:17:10 +10001742 if (i > 0 && success != i && options.exit_on_forward_failure)
1743 fatal("Could not request local forwarding.");
Kevin Steves12057502001-02-05 14:54:34 +00001744 if (i > 0 && success == 0)
1745 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001746
1747 /* Initiate remote TCP/IP port forwardings. */
1748 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001749 debug("Remote connections from %.200s:%d forwarded to "
1750 "local address %.200s:%d",
Damien Miller7acefbb2014-07-18 14:11:24 +10001751 (options.remote_forwards[i].listen_path != NULL) ?
1752 options.remote_forwards[i].listen_path :
Damien Miller46d38de2005-07-17 17:02:09 +10001753 (options.remote_forwards[i].listen_host == NULL) ?
Damien Milleraa3bb102005-11-05 15:12:59 +11001754 "LOCALHOST" : options.remote_forwards[i].listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +11001755 options.remote_forwards[i].listen_port,
Damien Miller7acefbb2014-07-18 14:11:24 +10001756 (options.remote_forwards[i].connect_path != NULL) ?
1757 options.remote_forwards[i].connect_path :
Damien Millerf91ee4c2005-03-01 21:24:33 +11001758 options.remote_forwards[i].connect_host,
1759 options.remote_forwards[i].connect_port);
Darren Tucker68afb8c2011-10-02 18:59:03 +11001760 options.remote_forwards[i].handle =
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001761 channel_request_remote_forwarding(ssh,
Damien Miller7acefbb2014-07-18 14:11:24 +10001762 &options.remote_forwards[i]);
Darren Tucker68afb8c2011-10-02 18:59:03 +11001763 if (options.remote_forwards[i].handle < 0) {
Darren Tuckere7d4b192006-07-12 22:17:10 +10001764 if (options.exit_on_forward_failure)
1765 fatal("Could not request remote forwarding.");
1766 else
1767 logit("Warning: Could not request remote "
1768 "forwarding.");
Darren Tucker68afb8c2011-10-02 18:59:03 +11001769 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001770 client_register_global_confirm(
1771 ssh_confirm_remote_forward,
Darren Tucker68afb8c2011-10-02 18:59:03 +11001772 &options.remote_forwards[i]);
Darren Tuckere7d4b192006-07-12 22:17:10 +10001773 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001774 }
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001775
1776 /* Initiate tunnel forwarding. */
1777 if (options.tun_open != SSH_TUNMODE_NO) {
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001778 if ((*ifname = client_request_tun_fwd(ssh,
1779 options.tun_open, options.tun_local,
1780 options.tun_remote)) == NULL) {
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001781 if (options.exit_on_forward_failure)
1782 fatal("Could not request tunnel forwarding.");
1783 else
1784 error("Could not request tunnel forwarding.");
1785 }
djm@openbsd.org@openbsd.orgdbe06622017-10-27 01:57:06 +00001786 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001787}
1788
Ben Lindstrombba81212001-06-25 05:01:22 +00001789static void
Damien Miller0bc1bd82000-11-13 22:57:25 +11001790check_agent_present(void)
1791{
djm@openbsd.org141efe42015-01-14 20:05:27 +00001792 int r;
1793
Damien Miller0bc1bd82000-11-13 22:57:25 +11001794 if (options.forward_agent) {
Damien Miller788f2122005-11-05 15:14:59 +11001795 /* Clear agent forwarding if we don't have an agent. */
djm@openbsd.org141efe42015-01-14 20:05:27 +00001796 if ((r = ssh_get_authentication_socket(NULL)) != 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001797 options.forward_agent = 0;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001798 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1799 debug("ssh_get_authentication_socket: %s",
1800 ssh_err(r));
1801 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001802 }
1803}
1804
Ben Lindstrombba81212001-06-25 05:01:22 +00001805static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001806ssh_session2_setup(struct ssh *ssh, int id, int success, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +10001807{
Damien Miller3756dce2004-06-18 01:17:29 +10001808 extern char **environ;
Damien Miller17e7ed02005-06-17 12:54:33 +10001809 const char *display;
djm@openbsd.org25b2ed62019-01-19 21:36:06 +00001810 int r, interactive = tty_flag;
djm@openbsd.orged4ce822016-01-13 23:04:47 +00001811 char *proto = NULL, *data = NULL;
Damien Miller17e7ed02005-06-17 12:54:33 +10001812
Damien Millerd530f5f2010-05-21 14:57:10 +10001813 if (!success)
1814 return; /* No need for error message, channels code sens one */
1815
Damien Miller46d38de2005-07-17 17:02:09 +10001816 display = getenv("DISPLAY");
djm@openbsd.orga58be332015-04-17 13:16:48 +00001817 if (display == NULL && options.forward_x11)
1818 debug("X11 forwarding requested but DISPLAY not set");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001819 if (options.forward_x11 && client_x11_get_proto(ssh, display,
djm@openbsd.orged4ce822016-01-13 23:04:47 +00001820 options.xauth_location, options.forward_x11_trusted,
1821 options.forward_x11_timeout, &proto, &data) == 0) {
Damien Millerbd483e72000-04-30 10:00:53 +10001822 /* Request forwarding with authentication spoofing. */
Darren Tuckerd6173c02008-06-13 04:52:53 +10001823 debug("Requesting X11 forwarding with authentication "
1824 "spoofing.");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001825 x11_request_forwarding_with_spoofing(ssh, id, display, proto,
Damien Miller6d7b4372011-06-23 08:31:57 +10001826 data, 1);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001827 client_expect_confirm(ssh, id, "X11 forwarding", CONFIRM_WARN);
Damien Miller6d7b4372011-06-23 08:31:57 +10001828 /* XXX exit_on_forward_failure */
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001829 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001830 }
1831
Damien Miller0bc1bd82000-11-13 22:57:25 +11001832 check_agent_present();
1833 if (options.forward_agent) {
1834 debug("Requesting authentication agent forwarding.");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001835 channel_request_start(ssh, id, "auth-agent-req@openssh.com", 0);
djm@openbsd.org25b2ed62019-01-19 21:36:06 +00001836 if ((r = sshpkt_send(ssh)) != 0)
1837 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller0bc1bd82000-11-13 22:57:25 +11001838 }
1839
Darren Tucker7b305012012-07-02 18:55:09 +10001840 /* Tell the packet module whether this is an interactive session. */
djm@openbsd.org25b2ed62019-01-19 21:36:06 +00001841 ssh_packet_set_interactive(ssh, interactive,
Darren Tucker7b305012012-07-02 18:55:09 +10001842 options.ip_qos_interactive, options.ip_qos_bulk);
1843
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001844 client_session2_setup(ssh, id, tty_flag, subsystem_flag, getenv("TERM"),
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001845 NULL, fileno(stdin), command, environ);
Damien Miller1383bd82000-04-06 12:32:37 +10001846}
1847
Ben Lindstromf558cf62001-09-20 23:13:49 +00001848/* open new channel for a session */
Ben Lindstrombba81212001-06-25 05:01:22 +00001849static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001850ssh_session2_open(struct ssh *ssh)
Damien Miller1383bd82000-04-06 12:32:37 +10001851{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001852 Channel *c;
1853 int window, packetmax, in, out, err;
Damien Millerad833b32000-08-23 10:46:23 +10001854
Damien Millercaf6dd62000-08-29 11:33:50 +11001855 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001856 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001857 } else {
1858 in = dup(STDIN_FILENO);
1859 }
1860 out = dup(STDOUT_FILENO);
1861 err = dup(STDERR_FILENO);
1862
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001863 if (in == -1 || out == -1 || err == -1)
Damien Millercaf6dd62000-08-29 11:33:50 +11001864 fatal("dup() in/out/err failed");
1865
Damien Miller69b69aa2000-10-28 14:19:58 +11001866 /* enable nonblocking unless tty */
1867 if (!isatty(in))
1868 set_nonblock(in);
1869 if (!isatty(out))
1870 set_nonblock(out);
1871 if (!isatty(err))
1872 set_nonblock(err);
1873
Damien Millere4340be2000-09-16 13:29:08 +11001874 window = CHAN_SES_WINDOW_DEFAULT;
1875 packetmax = CHAN_SES_PACKET_DEFAULT;
Damien Miller19a59452002-02-19 15:20:57 +11001876 if (tty_flag) {
1877 window >>= 1;
1878 packetmax >>= 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001879 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001880 c = channel_new(ssh,
Damien Miller1383bd82000-04-06 12:32:37 +10001881 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001882 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001883 "client-session", /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001884
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001885 debug3("%s: channel_new: %d", __func__, c->self);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001886
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001887 channel_send_open(ssh, c->self);
Ben Lindstromf558cf62001-09-20 23:13:49 +00001888 if (!no_shell_flag)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001889 channel_register_open_confirm(ssh, c->self,
Damien Millerb84886b2008-05-19 15:05:07 +10001890 ssh_session2_setup, NULL);
Damien Miller1383bd82000-04-06 12:32:37 +10001891
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001892 return c->self;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001893}
1894
Ben Lindstrombba81212001-06-25 05:01:22 +00001895static int
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001896ssh_session2(struct ssh *ssh, struct passwd *pw)
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001897{
djm@openbsd.org25b2ed62019-01-19 21:36:06 +00001898 int r, devnull, id = -1;
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001899 char *cp, *tun_fwd_ifname = NULL;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001900
1901 /* XXX should be pre-session */
Darren Tucker2d6665d2011-11-04 10:54:22 +11001902 if (!options.control_persist)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001903 ssh_init_stdio_forwarding(ssh);
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001904
1905 ssh_init_forwarding(ssh, &tun_fwd_ifname);
1906
1907 if (options.local_command != NULL) {
1908 debug3("expanding LocalCommand: %s", options.local_command);
1909 cp = options.local_command;
1910 options.local_command = percent_expand(cp,
1911 "C", conn_hash_hex,
1912 "L", shorthost,
1913 "d", pw->pw_dir,
1914 "h", host,
djm@openbsd.org9c935dd2018-06-01 03:33:53 +00001915 "i", uidstr,
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00001916 "l", thishost,
1917 "n", host_arg,
1918 "p", portstr,
1919 "r", options.user,
1920 "u", pw->pw_name,
1921 "T", tun_fwd_ifname == NULL ? "NONE" : tun_fwd_ifname,
1922 (char *)NULL);
1923 debug3("expanded LocalCommand: %s", options.local_command);
1924 free(cp);
1925 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001926
Damien Millere11e1ea2010-08-03 16:04:46 +10001927 /* Start listening for multiplex clients */
djm@openbsd.org25b2ed62019-01-19 21:36:06 +00001928 if (!ssh_packet_get_mux(ssh))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001929 muxserver_listen(ssh);
Damien Millere11e1ea2010-08-03 16:04:46 +10001930
djm@openbsd.orgb8bbff32018-02-13 03:36:56 +00001931 /*
Darren Tucker2d6665d2011-11-04 10:54:22 +11001932 * If we are in control persist mode and have a working mux listen
1933 * socket, then prepare to background ourselves and have a foreground
1934 * client attach as a control slave.
1935 * NB. we must save copies of the flags that we override for
Damien Millere11e1ea2010-08-03 16:04:46 +10001936 * the backgrounding, since we defer attachment of the slave until
1937 * after the connection is fully established (in particular,
1938 * async rfwd replies have been received for ExitOnForwardFailure).
1939 */
djm@openbsd.orgb8bbff32018-02-13 03:36:56 +00001940 if (options.control_persist && muxserver_sock != -1) {
Damien Millere11e1ea2010-08-03 16:04:46 +10001941 ostdin_null_flag = stdin_null_flag;
1942 ono_shell_flag = no_shell_flag;
Damien Miller21771e22011-05-15 08:45:50 +10001943 orequest_tty = options.request_tty;
Damien Millere11e1ea2010-08-03 16:04:46 +10001944 otty_flag = tty_flag;
djm@openbsd.orgb8bbff32018-02-13 03:36:56 +00001945 stdin_null_flag = 1;
1946 no_shell_flag = 1;
1947 tty_flag = 0;
Damien Millere11e1ea2010-08-03 16:04:46 +10001948 if (!fork_after_authentication_flag)
1949 need_controlpersist_detach = 1;
1950 fork_after_authentication_flag = 1;
djm@openbsd.orgb8bbff32018-02-13 03:36:56 +00001951 }
Darren Tucker2d6665d2011-11-04 10:54:22 +11001952 /*
1953 * ControlPersist mux listen socket setup failed, attempt the
1954 * stdio forward setup that we skipped earlier.
1955 */
1956 if (options.control_persist && muxserver_sock == -1)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001957 ssh_init_stdio_forwarding(ssh);
Damien Millere11e1ea2010-08-03 16:04:46 +10001958
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001959 if (!no_shell_flag)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001960 id = ssh_session2_open(ssh);
Damien Miller649fe022013-07-18 16:13:55 +10001961 else {
djm@openbsd.org25b2ed62019-01-19 21:36:06 +00001962 ssh_packet_set_interactive(ssh,
Damien Miller649fe022013-07-18 16:13:55 +10001963 options.control_master == SSHCTL_MASTER_NO,
1964 options.ip_qos_interactive, options.ip_qos_bulk);
1965 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001966
Darren Tucker8901fa92008-06-11 09:34:01 +10001967 /* If we don't expect to open a new session, then disallow it */
Damien Miller456e6f02008-11-03 19:20:10 +11001968 if (options.control_master == SSHCTL_MASTER_NO &&
1969 (datafellows & SSH_NEW_OPENSSH)) {
Darren Tucker8901fa92008-06-11 09:34:01 +10001970 debug("Requesting no-more-sessions@openssh.com");
djm@openbsd.org25b2ed62019-01-19 21:36:06 +00001971 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
1972 (r = sshpkt_put_cstring(ssh,
1973 "no-more-sessions@openssh.com")) != 0 ||
1974 (r = sshpkt_put_u8(ssh, 0)) != 0 ||
1975 (r = sshpkt_send(ssh)) != 0)
1976 fatal("%s: %s", __func__, ssh_err(r));
Darren Tucker8901fa92008-06-11 09:34:01 +10001977 }
1978
Damien Millerd27b9472005-12-13 19:29:02 +11001979 /* Execute a local command */
1980 if (options.local_command != NULL &&
1981 options.permit_local_command)
1982 ssh_local_cmd(options.local_command);
1983
Damien Miller1f25ab42010-07-16 13:56:23 +10001984 /*
djm@openbsd.org4d5456c2017-10-25 00:21:37 +00001985 * stdout is now owned by the session channel; clobber it here
1986 * so future channel closes are propagated to the local fd.
1987 * NB. this can only happen after LocalCommand has completed,
1988 * as it may want to write to stdout.
1989 */
djm@openbsd.org@openbsd.org939b30b2017-11-01 00:04:15 +00001990 if (!need_controlpersist_detach) {
1991 if ((devnull = open(_PATH_DEVNULL, O_WRONLY)) == -1)
1992 error("%s: open %s: %s", __func__,
1993 _PATH_DEVNULL, strerror(errno));
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001994 if (dup2(devnull, STDOUT_FILENO) == -1)
djm@openbsd.org@openbsd.org939b30b2017-11-01 00:04:15 +00001995 fatal("%s: dup2() stdout failed", __func__);
1996 if (devnull > STDERR_FILENO)
1997 close(devnull);
1998 }
djm@openbsd.org4d5456c2017-10-25 00:21:37 +00001999
2000 /*
Damien Miller1f25ab42010-07-16 13:56:23 +10002001 * If requested and we are not interested in replies to remote
2002 * forwarding requests, then let ssh continue in the background.
2003 */
Damien Millere11e1ea2010-08-03 16:04:46 +10002004 if (fork_after_authentication_flag) {
2005 if (options.exit_on_forward_failure &&
2006 options.num_remote_forwards > 0) {
2007 debug("deferring postauth fork until remote forward "
2008 "confirmation received");
2009 } else
2010 fork_postauth();
Darren Tucker9a2a6092008-07-04 12:53:50 +10002011 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00002012
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002013 return client_loop(ssh, tty_flag, tty_flag ?
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00002014 options.escape_char : SSH_ESCAPECHAR_NONE, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002015}
Damien Miller0bc1bd82000-11-13 22:57:25 +11002016
djm@openbsd.org4e44a792015-09-24 06:15:11 +00002017/* Loads all IdentityFile and CertificateFile keys */
Ben Lindstrombba81212001-06-25 05:01:22 +00002018static void
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00002019load_public_identity_files(struct passwd *pw)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00002020{
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00002021 char *filename, *cp;
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00002022 struct sshkey *public;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00002023 int i;
2024 u_int n_ids, n_certs;
Damien Miller0a80ca12010-02-27 07:55:05 +11002025 char *identity_files[SSH_MAX_IDENTITY_FILES];
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00002026 struct sshkey *identity_keys[SSH_MAX_IDENTITY_FILES];
djm@openbsd.org3eb7f102018-07-16 07:06:50 +00002027 int identity_file_userprovided[SSH_MAX_IDENTITY_FILES];
djm@openbsd.org4e44a792015-09-24 06:15:11 +00002028 char *certificate_files[SSH_MAX_CERTIFICATE_FILES];
2029 struct sshkey *certificates[SSH_MAX_CERTIFICATE_FILES];
djm@openbsd.org3eb7f102018-07-16 07:06:50 +00002030 int certificate_file_userprovided[SSH_MAX_CERTIFICATE_FILES];
Damien Miller7ea845e2010-02-12 09:21:02 +11002031#ifdef ENABLE_PKCS11
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00002032 struct sshkey **keys;
Damien Miller7ea845e2010-02-12 09:21:02 +11002033 int nkeys;
Damien Miller0a80ca12010-02-27 07:55:05 +11002034#endif /* PKCS11 */
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00002035
djm@openbsd.org4e44a792015-09-24 06:15:11 +00002036 n_ids = n_certs = 0;
Damien Miller1d2c4562014-02-04 11:18:20 +11002037 memset(identity_files, 0, sizeof(identity_files));
2038 memset(identity_keys, 0, sizeof(identity_keys));
djm@openbsd.org3eb7f102018-07-16 07:06:50 +00002039 memset(identity_file_userprovided, 0,
2040 sizeof(identity_file_userprovided));
djm@openbsd.org4e44a792015-09-24 06:15:11 +00002041 memset(certificate_files, 0, sizeof(certificate_files));
2042 memset(certificates, 0, sizeof(certificates));
djm@openbsd.org3eb7f102018-07-16 07:06:50 +00002043 memset(certificate_file_userprovided, 0,
2044 sizeof(certificate_file_userprovided));
Damien Miller0a80ca12010-02-27 07:55:05 +11002045
2046#ifdef ENABLE_PKCS11
Damien Miller7ea845e2010-02-12 09:21:02 +11002047 if (options.pkcs11_provider != NULL &&
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00002048 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
Damien Miller7ea845e2010-02-12 09:21:02 +11002049 (pkcs11_init(!options.batch_mode) == 0) &&
2050 (nkeys = pkcs11_add_provider(options.pkcs11_provider, NULL,
2051 &keys)) > 0) {
Damien Miller7ea845e2010-02-12 09:21:02 +11002052 for (i = 0; i < nkeys; i++) {
Damien Miller0a80ca12010-02-27 07:55:05 +11002053 if (n_ids >= SSH_MAX_IDENTITY_FILES) {
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00002054 sshkey_free(keys[i]);
Damien Miller0a80ca12010-02-27 07:55:05 +11002055 continue;
2056 }
2057 identity_keys[n_ids] = keys[i];
2058 identity_files[n_ids] =
Damien Miller7ea845e2010-02-12 09:21:02 +11002059 xstrdup(options.pkcs11_provider); /* XXX */
Damien Miller0a80ca12010-02-27 07:55:05 +11002060 n_ids++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00002061 }
Darren Tuckera627d422013-06-02 07:31:17 +10002062 free(keys);
Ben Lindstrom711b04a2001-08-06 21:12:42 +00002063 }
Damien Miller7ea845e2010-02-12 09:21:02 +11002064#endif /* ENABLE_PKCS11 */
Damien Miller0a80ca12010-02-27 07:55:05 +11002065 for (i = 0; i < options.num_identity_files; i++) {
Darren Tucker15fd19c2013-04-05 11:22:26 +11002066 if (n_ids >= SSH_MAX_IDENTITY_FILES ||
2067 strcasecmp(options.identity_files[i], "none") == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +10002068 free(options.identity_files[i]);
djm@openbsd.org4e44a792015-09-24 06:15:11 +00002069 options.identity_files[i] = NULL;
Damien Miller0a80ca12010-02-27 07:55:05 +11002070 continue;
2071 }
dtucker@openbsd.orge655ee02018-07-27 05:34:42 +00002072 cp = tilde_expand_filename(options.identity_files[i], getuid());
djm@openbsd.orgb7548b12017-10-23 05:08:00 +00002073 filename = percent_expand(cp, "d", pw->pw_dir,
2074 "u", pw->pw_name, "l", thishost, "h", host,
Damien Miller6b1d53c2006-03-31 23:13:21 +11002075 "r", options.user, (char *)NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10002076 free(cp);
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00002077 check_load(sshkey_load_public(filename, &public, NULL),
2078 filename, "pubkey");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00002079 debug("identity file %s type %d", filename,
2080 public ? public->type : -1);
Darren Tuckera627d422013-06-02 07:31:17 +10002081 free(options.identity_files[i]);
Damien Miller0a80ca12010-02-27 07:55:05 +11002082 identity_files[n_ids] = filename;
2083 identity_keys[n_ids] = public;
djm@openbsd.org3eb7f102018-07-16 07:06:50 +00002084 identity_file_userprovided[n_ids] =
2085 options.identity_file_userprovided[i];
Damien Miller0a80ca12010-02-27 07:55:05 +11002086 if (++n_ids >= SSH_MAX_IDENTITY_FILES)
2087 continue;
2088
djm@openbsd.org4e44a792015-09-24 06:15:11 +00002089 /*
2090 * If no certificates have been explicitly listed then try
2091 * to add the default certificate variant too.
2092 */
2093 if (options.num_certificate_files != 0)
2094 continue;
Damien Miller0a80ca12010-02-27 07:55:05 +11002095 xasprintf(&cp, "%s-cert", filename);
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00002096 check_load(sshkey_load_public(cp, &public, NULL),
2097 filename, "pubkey");
Damien Miller0a80ca12010-02-27 07:55:05 +11002098 debug("identity file %s type %d", cp,
2099 public ? public->type : -1);
2100 if (public == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +10002101 free(cp);
Damien Miller0a80ca12010-02-27 07:55:05 +11002102 continue;
2103 }
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00002104 if (!sshkey_is_cert(public)) {
Damien Miller0a80ca12010-02-27 07:55:05 +11002105 debug("%s: key %s type %s is not a certificate",
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00002106 __func__, cp, sshkey_type(public));
2107 sshkey_free(public);
Darren Tuckera627d422013-06-02 07:31:17 +10002108 free(cp);
Damien Miller0a80ca12010-02-27 07:55:05 +11002109 continue;
2110 }
djm@openbsd.orgb4867e02016-12-06 07:48:01 +00002111 /* NB. leave filename pointing to private key */
2112 identity_files[n_ids] = xstrdup(filename);
Damien Miller0a80ca12010-02-27 07:55:05 +11002113 identity_keys[n_ids] = public;
djm@openbsd.org3eb7f102018-07-16 07:06:50 +00002114 identity_file_userprovided[n_ids] =
2115 options.identity_file_userprovided[i];
Damien Miller0a80ca12010-02-27 07:55:05 +11002116 n_ids++;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00002117 }
djm@openbsd.org4e44a792015-09-24 06:15:11 +00002118
2119 if (options.num_certificate_files > SSH_MAX_CERTIFICATE_FILES)
2120 fatal("%s: too many certificates", __func__);
2121 for (i = 0; i < options.num_certificate_files; i++) {
2122 cp = tilde_expand_filename(options.certificate_files[i],
dtucker@openbsd.orge655ee02018-07-27 05:34:42 +00002123 getuid());
djm@openbsd.org9c935dd2018-06-01 03:33:53 +00002124 filename = percent_expand(cp,
2125 "d", pw->pw_dir,
2126 "h", host,
djm@openbsd.org03688892018-06-08 03:35:36 +00002127 "i", uidstr,
djm@openbsd.org9c935dd2018-06-01 03:33:53 +00002128 "l", thishost,
2129 "r", options.user,
2130 "u", pw->pw_name,
2131 (char *)NULL);
djm@openbsd.org4e44a792015-09-24 06:15:11 +00002132 free(cp);
2133
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00002134 check_load(sshkey_load_public(filename, &public, NULL),
2135 filename, "certificate");
djm@openbsd.org4e44a792015-09-24 06:15:11 +00002136 debug("certificate file %s type %d", filename,
2137 public ? public->type : -1);
2138 free(options.certificate_files[i]);
2139 options.certificate_files[i] = NULL;
2140 if (public == NULL) {
2141 free(filename);
2142 continue;
2143 }
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00002144 if (!sshkey_is_cert(public)) {
djm@openbsd.org4e44a792015-09-24 06:15:11 +00002145 debug("%s: key %s type %s is not a certificate",
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00002146 __func__, filename, sshkey_type(public));
2147 sshkey_free(public);
djm@openbsd.org4e44a792015-09-24 06:15:11 +00002148 free(filename);
2149 continue;
2150 }
2151 certificate_files[n_certs] = filename;
2152 certificates[n_certs] = public;
djm@openbsd.org3eb7f102018-07-16 07:06:50 +00002153 certificate_file_userprovided[n_certs] =
2154 options.certificate_file_userprovided[i];
djm@openbsd.org4e44a792015-09-24 06:15:11 +00002155 ++n_certs;
2156 }
2157
Damien Miller0a80ca12010-02-27 07:55:05 +11002158 options.num_identity_files = n_ids;
2159 memcpy(options.identity_files, identity_files, sizeof(identity_files));
2160 memcpy(options.identity_keys, identity_keys, sizeof(identity_keys));
djm@openbsd.org3eb7f102018-07-16 07:06:50 +00002161 memcpy(options.identity_file_userprovided,
2162 identity_file_userprovided, sizeof(identity_file_userprovided));
Damien Miller0a80ca12010-02-27 07:55:05 +11002163
djm@openbsd.org4e44a792015-09-24 06:15:11 +00002164 options.num_certificate_files = n_certs;
2165 memcpy(options.certificate_files,
2166 certificate_files, sizeof(certificate_files));
2167 memcpy(options.certificates, certificates, sizeof(certificates));
djm@openbsd.org3eb7f102018-07-16 07:06:50 +00002168 memcpy(options.certificate_file_userprovided,
2169 certificate_file_userprovided,
2170 sizeof(certificate_file_userprovided));
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00002171}
Damien Miller857b02e2010-09-24 22:02:56 +10002172
2173static void
2174main_sigchld_handler(int sig)
2175{
2176 int save_errno = errno;
2177 pid_t pid;
2178 int status;
2179
2180 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00002181 (pid == -1 && errno == EINTR))
Damien Miller857b02e2010-09-24 22:02:56 +10002182 ;
Damien Miller857b02e2010-09-24 22:02:56 +10002183 errno = save_errno;
2184}