blob: 59c1f931cb0a2f779f0e1478c680bba7cb14e952 [file] [log] [blame]
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001/* $OpenBSD: ssh.c,v 1.420 2015/07/30 00:01:34 djm 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>
Damien Millere6b3b612006-07-24 14:01:23 +100068#include <unistd.h>
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000069#include <limits.h>
Damien Millereba71ba2000-04-29 23:57:08 +100070
Darren Tucker46aa3e02006-09-02 15:32:40 +100071#include <netinet/in.h>
72#include <arpa/inet.h>
73
Damien Miller1f0311c2014-05-15 14:24:09 +100074#ifdef WITH_OPENSSL
Damien Millereba71ba2000-04-29 23:57:08 +100075#include <openssl/evp.h>
Damien Miller0bc1bd82000-11-13 22:57:25 +110076#include <openssl/err.h>
Damien Miller1f0311c2014-05-15 14:24:09 +100077#endif
Darren Tuckerbfaaf962008-02-28 19:13:52 +110078#include "openbsd-compat/openssl-compat.h"
Damien Millerb84886b2008-05-19 15:05:07 +100079#include "openbsd-compat/sys-queue.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080
Damien Millerd7834352006-08-05 12:39:39 +100081#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000083#include "ssh1.h"
Damien Miller1383bd82000-04-06 12:32:37 +100084#include "ssh2.h"
Damien Millerb96c4412010-07-02 13:34:24 +100085#include "canohost.h"
Damien Miller1383bd82000-04-06 12:32:37 +100086#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000087#include "cipher.h"
Damien Miller9c386432014-07-03 21:27:46 +100088#include "digest.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000089#include "packet.h"
90#include "buffer.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000091#include "channels.h"
Damien Millereba71ba2000-04-29 23:57:08 +100092#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100093#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100094#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000095#include "pathnames.h"
Damien Miller0e220db2004-06-15 10:34:08 +100096#include "dispatch.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +000097#include "clientloop.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000098#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100099#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000100#include "readconf.h"
101#include "sshconnect.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000102#include "kex.h"
103#include "mac.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +1000104#include "sshpty.h"
Darren Tucker46bc0752004-05-02 22:11:30 +1000105#include "match.h"
Damien Miller0e220db2004-06-15 10:34:08 +1000106#include "msg.h"
Darren Tucker25f60a72004-08-15 17:23:34 +1000107#include "uidswap.h"
Darren Tuckerf1de4e52010-01-08 16:54:59 +1100108#include "roaming.h"
Damien Millerb7576772006-07-10 20:23:39 +1000109#include "version.h"
djm@openbsd.org141efe42015-01-14 20:05:27 +0000110#include "ssherr.h"
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000111#include "myproposal.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
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100155/* forward stdio to remote host and port */
156char *stdio_forward_host = NULL;
157int stdio_forward_port = 0;
158
Damien Miller5428f641999-11-25 11:54:57 +1100159/*
160 * General data structure for command line options and options configurable
161 * in configuration files. See readconf.h.
162 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000163Options options;
164
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000165/* optional user configfile */
166char *config = NULL;
167
Damien Miller5428f641999-11-25 11:54:57 +1100168/*
169 * Name of the host we are connecting to. This is the name given on the
170 * command line, or the HostName specified for the user-supplied name in a
171 * configuration file.
172 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173char *host;
174
175/* 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
181/* Original real UID. */
182uid_t original_real_uid;
Ben Lindstromf9c48842002-06-11 16:37:51 +0000183uid_t original_effective_uid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000184
Damien Miller1383bd82000-04-06 12:32:37 +1000185/* command to be executed */
186Buffer command;
187
Damien Miller832562e2001-01-30 09:30:01 +1100188/* Should we execute a command or invoke a subsystem? */
189int subsystem_flag = 0;
190
Damien Miller2797f7f2002-04-23 21:09:44 +1000191/* # of replies received for global requests */
Darren Tucker9f407c42008-06-13 04:50:27 +1000192static int remote_forward_confirms_received = 0;
Damien Miller2797f7f2002-04-23 21:09:44 +1000193
Damien Millerb1cbfa22008-05-19 16:00:08 +1000194/* mux.c */
195extern int muxserver_sock;
196extern u_int muxclient_command;
Damien Miller0e220db2004-06-15 10:34:08 +1000197
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000198/* Prints a help message to the user. This function never returns. */
199
Ben Lindstrombba81212001-06-25 05:01:22 +0000200static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000201usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202{
Damien Miller50955102004-03-22 09:34:58 +1100203 fprintf(stderr,
jmc@openbsd.orgb1ba15f2014-10-09 06:21:31 +0000204"usage: ssh [-1246AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]\n"
Damien Miller03d4d7e2013-04-23 15:21:06 +1000205" [-D [bind_address:]port] [-E log_file] [-e escape_char]\n"
206" [-F configfile] [-I pkcs11] [-i identity_file]\n"
millert@openbsd.orgd5d91d02015-07-20 18:42:35 +0000207" [-L address] [-l login_name] [-m mac_spec]\n"
Damien Millerbdb352a2013-12-05 10:20:52 +1100208" [-O ctl_cmd] [-o option] [-p port]\n"
209" [-Q cipher | cipher-auth | mac | kex | key]\n"
millert@openbsd.orgd5d91d02015-07-20 18:42:35 +0000210" [-R address] [-S ctl_path] [-W host:port]\n"
Damien Millerbdb352a2013-12-05 10:20:52 +1100211" [-w local_tun[:remote_tun]] [user@]hostname [command]\n"
Damien Miller50955102004-03-22 09:34:58 +1100212 );
Darren Tuckere9a9b712005-12-20 16:15:51 +1100213 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000214}
215
Ben Lindstrombba81212001-06-25 05:01:22 +0000216static int ssh_session(void);
217static int ssh_session2(void);
218static void load_public_identity_files(void);
Damien Miller857b02e2010-09-24 22:02:56 +1000219static void main_sigchld_handler(int);
Damien Millerb1cbfa22008-05-19 16:00:08 +1000220
221/* from muxclient.c */
222void muxclient(const char *);
223void muxserver_listen(void);
Damien Miller1383bd82000-04-06 12:32:37 +1000224
Damien Miller295ee632011-05-29 21:42:31 +1000225/* ~/ expand a list of paths. NB. assumes path[n] is heap-allocated. */
226static void
227tilde_expand_paths(char **paths, u_int num_paths)
228{
229 u_int i;
230 char *cp;
231
232 for (i = 0; i < num_paths; i++) {
233 cp = tilde_expand_filename(paths[i], original_real_uid);
Darren Tuckera627d422013-06-02 07:31:17 +1000234 free(paths[i]);
Damien Miller295ee632011-05-29 21:42:31 +1000235 paths[i] = cp;
236 }
237}
238
Damien Miller13f97b22014-02-24 15:57:55 +1100239/*
240 * Attempt to resolve a host name / port to a set of addresses and
241 * optionally return any CNAMEs encountered along the way.
242 * Returns NULL on failure.
243 * NB. this function must operate with a options having undefined members.
244 */
Damien Miller0faf7472013-10-17 11:47:23 +1100245static struct addrinfo *
Damien Miller13f97b22014-02-24 15:57:55 +1100246resolve_host(const char *name, int port, int logerr, char *cname, size_t clen)
Damien Miller0faf7472013-10-17 11:47:23 +1100247{
248 char strport[NI_MAXSERV];
249 struct addrinfo hints, *res;
250 int gaierr, loglevel = SYSLOG_LEVEL_DEBUG1;
251
Damien Miller13f97b22014-02-24 15:57:55 +1100252 if (port <= 0)
253 port = default_ssh_port();
254
Damien Miller0faf7472013-10-17 11:47:23 +1100255 snprintf(strport, sizeof strport, "%u", port);
Damien Miller1d2c4562014-02-04 11:18:20 +1100256 memset(&hints, 0, sizeof(hints));
Damien Miller13f97b22014-02-24 15:57:55 +1100257 hints.ai_family = options.address_family == -1 ?
258 AF_UNSPEC : options.address_family;
Damien Miller0faf7472013-10-17 11:47:23 +1100259 hints.ai_socktype = SOCK_STREAM;
260 if (cname != NULL)
261 hints.ai_flags = AI_CANONNAME;
262 if ((gaierr = getaddrinfo(name, strport, &hints, &res)) != 0) {
263 if (logerr || (gaierr != EAI_NONAME && gaierr != EAI_NODATA))
264 loglevel = SYSLOG_LEVEL_ERROR;
265 do_log2(loglevel, "%s: Could not resolve hostname %.100s: %s",
266 __progname, name, ssh_gai_strerror(gaierr));
267 return NULL;
268 }
269 if (cname != NULL && res->ai_canonname != NULL) {
270 if (strlcpy(cname, res->ai_canonname, clen) >= clen) {
271 error("%s: host \"%s\" cname \"%s\" too long (max %lu)",
272 __func__, name, res->ai_canonname, (u_long)clen);
273 if (clen > 0)
274 *cname = '\0';
275 }
276 }
277 return res;
278}
279
280/*
djm@openbsd.org90109022015-01-16 07:19:48 +0000281 * Attempt to resolve a numeric host address / port to a single address.
282 * Returns a canonical address string.
283 * Returns NULL on failure.
284 * NB. this function must operate with a options having undefined members.
285 */
286static struct addrinfo *
287resolve_addr(const char *name, int port, char *caddr, size_t clen)
288{
289 char addr[NI_MAXHOST], strport[NI_MAXSERV];
290 struct addrinfo hints, *res;
291 int gaierr;
292
293 if (port <= 0)
294 port = default_ssh_port();
295 snprintf(strport, sizeof strport, "%u", port);
296 memset(&hints, 0, sizeof(hints));
297 hints.ai_family = options.address_family == -1 ?
298 AF_UNSPEC : options.address_family;
299 hints.ai_socktype = SOCK_STREAM;
300 hints.ai_flags = AI_NUMERICHOST|AI_NUMERICSERV;
301 if ((gaierr = getaddrinfo(name, strport, &hints, &res)) != 0) {
302 debug2("%s: could not resolve name %.100s as address: %s",
303 __func__, name, ssh_gai_strerror(gaierr));
304 return NULL;
305 }
306 if (res == NULL) {
307 debug("%s: getaddrinfo %.100s returned no addresses",
308 __func__, name);
309 return NULL;
310 }
311 if (res->ai_next != NULL) {
312 debug("%s: getaddrinfo %.100s returned multiple addresses",
313 __func__, name);
314 goto fail;
315 }
316 if ((gaierr = getnameinfo(res->ai_addr, res->ai_addrlen,
317 addr, sizeof(addr), NULL, 0, NI_NUMERICHOST)) != 0) {
318 debug("%s: Could not format address for name %.100s: %s",
319 __func__, name, ssh_gai_strerror(gaierr));
320 goto fail;
321 }
322 if (strlcpy(caddr, addr, clen) >= clen) {
323 error("%s: host \"%s\" addr \"%s\" too long (max %lu)",
324 __func__, name, addr, (u_long)clen);
325 if (clen > 0)
326 *caddr = '\0';
327 fail:
328 freeaddrinfo(res);
329 return NULL;
330 }
331 return res;
332}
333
334/*
Damien Miller0faf7472013-10-17 11:47:23 +1100335 * Check whether the cname is a permitted replacement for the hostname
336 * and perform the replacement if it is.
Damien Miller13f97b22014-02-24 15:57:55 +1100337 * NB. this function must operate with a options having undefined members.
Damien Miller0faf7472013-10-17 11:47:23 +1100338 */
339static int
340check_follow_cname(char **namep, const char *cname)
341{
342 int i;
343 struct allowed_cname *rule;
344
345 if (*cname == '\0' || options.num_permitted_cnames == 0 ||
346 strcmp(*namep, cname) == 0)
347 return 0;
Damien Miller38505592013-10-17 11:48:13 +1100348 if (options.canonicalize_hostname == SSH_CANONICALISE_NO)
Damien Miller0faf7472013-10-17 11:47:23 +1100349 return 0;
350 /*
Damien Miller38505592013-10-17 11:48:13 +1100351 * Don't attempt to canonicalize names that will be interpreted by
Damien Miller0faf7472013-10-17 11:47:23 +1100352 * a proxy unless the user specifically requests so.
353 */
Damien Miller13f97b22014-02-24 15:57:55 +1100354 if (!option_clear_or_none(options.proxy_command) &&
Damien Miller38505592013-10-17 11:48:13 +1100355 options.canonicalize_hostname != SSH_CANONICALISE_ALWAYS)
Damien Miller0faf7472013-10-17 11:47:23 +1100356 return 0;
357 debug3("%s: check \"%s\" CNAME \"%s\"", __func__, *namep, cname);
358 for (i = 0; i < options.num_permitted_cnames; i++) {
359 rule = options.permitted_cnames + i;
djm@openbsd.orge661a862015-05-04 06:10:48 +0000360 if (match_pattern_list(*namep, rule->source_list, 1) != 1 ||
361 match_pattern_list(cname, rule->target_list, 1) != 1)
Damien Miller0faf7472013-10-17 11:47:23 +1100362 continue;
Damien Miller38505592013-10-17 11:48:13 +1100363 verbose("Canonicalized DNS aliased hostname "
Damien Miller0faf7472013-10-17 11:47:23 +1100364 "\"%s\" => \"%s\"", *namep, cname);
365 free(*namep);
366 *namep = xstrdup(cname);
367 return 1;
368 }
369 return 0;
370}
371
372/*
373 * Attempt to resolve the supplied hostname after applying the user's
Damien Miller51682fa2013-10-17 11:48:31 +1100374 * canonicalization rules. Returns the address list for the host or NULL
375 * if no name was found after canonicalization.
Damien Miller13f97b22014-02-24 15:57:55 +1100376 * NB. this function must operate with a options having undefined members.
Damien Miller0faf7472013-10-17 11:47:23 +1100377 */
378static struct addrinfo *
Damien Miller13f97b22014-02-24 15:57:55 +1100379resolve_canonicalize(char **hostp, int port)
Damien Miller0faf7472013-10-17 11:47:23 +1100380{
381 int i, ndots;
djm@openbsd.org90109022015-01-16 07:19:48 +0000382 char *cp, *fullhost, newname[NI_MAXHOST];
Damien Miller0faf7472013-10-17 11:47:23 +1100383 struct addrinfo *addrs;
384
Damien Miller38505592013-10-17 11:48:13 +1100385 if (options.canonicalize_hostname == SSH_CANONICALISE_NO)
Damien Miller0faf7472013-10-17 11:47:23 +1100386 return NULL;
Damien Miller13f97b22014-02-24 15:57:55 +1100387
Damien Miller0faf7472013-10-17 11:47:23 +1100388 /*
Damien Miller38505592013-10-17 11:48:13 +1100389 * Don't attempt to canonicalize names that will be interpreted by
Damien Miller0faf7472013-10-17 11:47:23 +1100390 * a proxy unless the user specifically requests so.
391 */
Damien Miller13f97b22014-02-24 15:57:55 +1100392 if (!option_clear_or_none(options.proxy_command) &&
Damien Miller38505592013-10-17 11:48:13 +1100393 options.canonicalize_hostname != SSH_CANONICALISE_ALWAYS)
Damien Miller0faf7472013-10-17 11:47:23 +1100394 return NULL;
Damien Miller13f97b22014-02-24 15:57:55 +1100395
djm@openbsd.org90109022015-01-16 07:19:48 +0000396 /* Try numeric hostnames first */
397 if ((addrs = resolve_addr(*hostp, port,
398 newname, sizeof(newname))) != NULL) {
399 debug2("%s: hostname %.100s is address", __func__, *hostp);
400 if (strcasecmp(*hostp, newname) != 0) {
401 debug2("%s: canonicalised address \"%s\" => \"%s\"",
402 __func__, *hostp, newname);
403 free(*hostp);
404 *hostp = xstrdup(newname);
405 }
406 return addrs;
407 }
408
Damien Miller51682fa2013-10-17 11:48:31 +1100409 /* Don't apply canonicalization to sufficiently-qualified hostnames */
Damien Miller0faf7472013-10-17 11:47:23 +1100410 ndots = 0;
411 for (cp = *hostp; *cp != '\0'; cp++) {
412 if (*cp == '.')
413 ndots++;
414 }
Damien Miller38505592013-10-17 11:48:13 +1100415 if (ndots > options.canonicalize_max_dots) {
416 debug3("%s: not canonicalizing hostname \"%s\" (max dots %d)",
417 __func__, *hostp, options.canonicalize_max_dots);
Damien Miller0faf7472013-10-17 11:47:23 +1100418 return NULL;
419 }
420 /* Attempt each supplied suffix */
421 for (i = 0; i < options.num_canonical_domains; i++) {
djm@openbsd.org90109022015-01-16 07:19:48 +0000422 *newname = '\0';
Damien Miller0faf7472013-10-17 11:47:23 +1100423 xasprintf(&fullhost, "%s.%s.", *hostp,
424 options.canonical_domains[i]);
Damien Miller13f97b22014-02-24 15:57:55 +1100425 debug3("%s: attempting \"%s\" => \"%s\"", __func__,
426 *hostp, fullhost);
427 if ((addrs = resolve_host(fullhost, port, 0,
djm@openbsd.org90109022015-01-16 07:19:48 +0000428 newname, sizeof(newname))) == NULL) {
Damien Miller0faf7472013-10-17 11:47:23 +1100429 free(fullhost);
430 continue;
431 }
432 /* Remove trailing '.' */
433 fullhost[strlen(fullhost) - 1] = '\0';
434 /* Follow CNAME if requested */
djm@openbsd.org90109022015-01-16 07:19:48 +0000435 if (!check_follow_cname(&fullhost, newname)) {
Damien Miller38505592013-10-17 11:48:13 +1100436 debug("Canonicalized hostname \"%s\" => \"%s\"",
Damien Miller0faf7472013-10-17 11:47:23 +1100437 *hostp, fullhost);
438 }
439 free(*hostp);
440 *hostp = fullhost;
441 return addrs;
442 }
Damien Miller38505592013-10-17 11:48:13 +1100443 if (!options.canonicalize_fallback_local)
Damien Miller13f97b22014-02-24 15:57:55 +1100444 fatal("%s: Could not resolve host \"%s\"", __progname, *hostp);
445 debug2("%s: host %s not found in any suffix", __func__, *hostp);
Damien Miller0faf7472013-10-17 11:47:23 +1100446 return NULL;
447}
448
Damien Miller95def091999-11-25 00:26:21 +1100449/*
Damien Miller13f97b22014-02-24 15:57:55 +1100450 * Read per-user configuration file. Ignore the system wide config
451 * file if the user specifies a config file on the command line.
452 */
453static void
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000454process_config_files(const char *host_arg, struct passwd *pw, int post_canon)
Damien Miller13f97b22014-02-24 15:57:55 +1100455{
deraadt@openbsd.org087266e2015-01-20 23:14:00 +0000456 char buf[PATH_MAX];
Damien Miller13f97b22014-02-24 15:57:55 +1100457 int r;
458
459 if (config != NULL) {
460 if (strcasecmp(config, "none") != 0 &&
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000461 !read_config_file(config, pw, host, host_arg, &options,
462 SSHCONF_USERCONF | (post_canon ? SSHCONF_POSTCANON : 0)))
Damien Miller13f97b22014-02-24 15:57:55 +1100463 fatal("Can't open user config file %.100s: "
464 "%.100s", config, strerror(errno));
465 } else {
466 r = snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir,
467 _PATH_SSH_USER_CONFFILE);
468 if (r > 0 && (size_t)r < sizeof(buf))
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000469 (void)read_config_file(buf, pw, host, host_arg,
470 &options, SSHCONF_CHECKPERM | SSHCONF_USERCONF |
471 (post_canon ? SSHCONF_POSTCANON : 0));
Damien Miller13f97b22014-02-24 15:57:55 +1100472
473 /* Read systemwide configuration file after user config. */
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000474 (void)read_config_file(_PATH_HOST_CONFIG_FILE, pw,
475 host, host_arg, &options,
476 post_canon ? SSHCONF_POSTCANON : 0);
477 }
478}
479
480/* Rewrite the port number in an addrinfo list of addresses */
481static void
482set_addrinfo_port(struct addrinfo *addrs, int port)
483{
484 struct addrinfo *addr;
485
486 for (addr = addrs; addr != NULL; addr = addr->ai_next) {
487 switch (addr->ai_family) {
488 case AF_INET:
489 ((struct sockaddr_in *)addr->ai_addr)->
490 sin_port = htons(port);
491 break;
492 case AF_INET6:
493 ((struct sockaddr_in6 *)addr->ai_addr)->
494 sin6_port = htons(port);
495 break;
496 }
Damien Miller13f97b22014-02-24 15:57:55 +1100497 }
498}
499
500/*
Damien Miller95def091999-11-25 00:26:21 +1100501 * Main program for the ssh client.
502 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000503int
504main(int ac, char **av)
505{
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000506 int i, r, opt, exit_status, use_syslog, config_test = 0;
deraadt@openbsd.org087266e2015-01-20 23:14:00 +0000507 char *p, *cp, *line, *argv0, buf[PATH_MAX], *host_arg, *logfile;
Damien Millerdfc85fa2011-05-15 08:44:02 +1000508 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
Damien Miller0faf7472013-10-17 11:47:23 +1100509 char cname[NI_MAXHOST];
Damien Miller95def091999-11-25 00:26:21 +1100510 struct stat st;
Ben Lindstrom086cf212001-03-05 05:56:40 +0000511 struct passwd *pw;
Damien Miller194fd902013-10-15 12:13:05 +1100512 int timeout_ms;
Ben Lindstrom5ccf63a2001-09-24 20:00:10 +0000513 extern int optind, optreset;
Damien Miller4f8e6692001-07-14 13:22:53 +1000514 extern char *optarg;
Damien Miller7acefbb2014-07-18 14:11:24 +1000515 struct Forward fwd;
Damien Miller0faf7472013-10-17 11:47:23 +1100516 struct addrinfo *addrs = NULL;
Damien Miller9c386432014-07-03 21:27:46 +1000517 struct ssh_digest_ctx *md;
518 u_char conn_hash[SSH_DIGEST_MAX_LENGTH];
519 char *conn_hash_hex;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000520
Darren Tuckerce321d82005-10-03 18:11:24 +1000521 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
522 sanitise_stdfd();
523
Damien Miller59d3d5b2003-08-22 09:34:41 +1000524 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000525
Damien Millerea2c1a42011-06-03 12:10:22 +1000526#ifndef HAVE_SETPROCTITLE
527 /* Prepare for later setproctitle emulation */
528 /* Save argv so it isn't clobbered by setproctitle() emulation */
529 saved_av = xcalloc(ac + 1, sizeof(*saved_av));
530 for (i = 0; i < ac; i++)
531 saved_av[i] = xstrdup(av[i]);
532 saved_av[i] = NULL;
533 compat_init_setproctitle(ac, av);
534 av = saved_av;
535#endif
536
Damien Miller5428f641999-11-25 11:54:57 +1100537 /*
Damien Miller00d9ae22010-08-17 01:59:31 +1000538 * Discard other fds that are hanging around. These can cause problem
539 * with backgrounded ssh processes started by ControlPersist.
540 */
541 closefrom(STDERR_FILENO + 1);
542
543 /*
Damien Miller5428f641999-11-25 11:54:57 +1100544 * Save the original real uid. It will be needed later (uid-swapping
545 * may clobber the real uid).
546 */
Damien Miller95def091999-11-25 00:26:21 +1100547 original_real_uid = getuid();
548 original_effective_uid = geteuid();
Damien Millera8e06ce2003-11-21 23:48:55 +1100549
Damien Miller50b9a602002-09-04 16:50:06 +1000550 /*
551 * Use uid-swapping to give up root privileges for the duration of
552 * option processing. We will re-instantiate the rights when we are
553 * ready to create the privileged port, and will permanently drop
554 * them when the port has been created (actually, when the connection
555 * has been made, as we may need to create the port several times).
556 */
557 PRIV_END;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000558
Damien Miller05dd7952000-10-01 00:42:48 +1100559#ifdef HAVE_SETRLIMIT
Damien Miller95def091999-11-25 00:26:21 +1100560 /* If we are installed setuid root be careful to not drop core. */
561 if (original_real_uid != original_effective_uid) {
562 struct rlimit rlim;
563 rlim.rlim_cur = rlim.rlim_max = 0;
564 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
565 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000566 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100567#endif
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000568 /* Get user data. */
569 pw = getpwuid(original_real_uid);
570 if (!pw) {
Damien Miller3009d3c2013-07-20 13:22:31 +1000571 logit("No user exists for uid %lu", (u_long)original_real_uid);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100572 exit(255);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000573 }
574 /* Take a copy of the returned structure. */
575 pw = pwcopy(pw);
576
Damien Miller5428f641999-11-25 11:54:57 +1100577 /*
Damien Miller5428f641999-11-25 11:54:57 +1100578 * Set our umask to something reasonable, as some files are created
579 * with the default umask. This will make them world-readable but
580 * writable only by the owner, which is ok for all files for which we
581 * don't set the modes explicitly.
582 */
Damien Miller95def091999-11-25 00:26:21 +1100583 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000584
Darren Tuckerd6173c02008-06-13 04:52:53 +1000585 /*
586 * Initialize option structure to indicate that no values have been
587 * set.
588 */
Damien Miller95def091999-11-25 00:26:21 +1100589 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000590
Damien Miller95def091999-11-25 00:26:21 +1100591 /* Parse command-line arguments. */
592 host = NULL;
Damien Millere272a5b2008-11-03 19:22:37 +1100593 use_syslog = 0;
Damien Miller03d4d7e2013-04-23 15:21:06 +1000594 logfile = NULL;
Darren Tucker72efd742009-06-21 17:48:00 +1000595 argv0 = av[0];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000596
Damien Miller2ecb6bd2006-03-15 12:03:53 +1100597 again:
Darren Tuckerd6173c02008-06-13 04:52:53 +1000598 while ((opt = getopt(ac, av, "1246ab:c:e:fgi:kl:m:no:p:qstvx"
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000599 "ACD:E:F:GI:KL:MNO:PQ:R:S:TVw:W:XYy")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100600 switch (opt) {
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000601 case '1':
602 options.protocol = SSH_PROTO_1;
603 break;
Damien Miller4af51302000-04-16 11:18:38 +1000604 case '2':
605 options.protocol = SSH_PROTO_2;
606 break;
Damien Miller34132e52000-01-14 15:45:46 +1100607 case '4':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000608 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100609 break;
Damien Miller34132e52000-01-14 15:45:46 +1100610 case '6':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000611 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100612 break;
Damien Miller95def091999-11-25 00:26:21 +1100613 case 'n':
614 stdin_null_flag = 1;
615 break;
Damien Miller95def091999-11-25 00:26:21 +1100616 case 'f':
617 fork_after_authentication_flag = 1;
618 stdin_null_flag = 1;
619 break;
Damien Miller95def091999-11-25 00:26:21 +1100620 case 'x':
621 options.forward_x11 = 0;
622 break;
Damien Miller95def091999-11-25 00:26:21 +1100623 case 'X':
624 options.forward_x11 = 1;
625 break;
Damien Millere272a5b2008-11-03 19:22:37 +1100626 case 'y':
627 use_syslog = 1;
628 break;
Damien Miller03d4d7e2013-04-23 15:21:06 +1000629 case 'E':
630 logfile = xstrdup(optarg);
631 break;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000632 case 'G':
633 config_test = 1;
634 break;
Darren Tucker0a118da2003-10-15 15:54:32 +1000635 case 'Y':
636 options.forward_x11 = 1;
637 options.forward_x11_trusted = 1;
638 break;
Damien Miller95def091999-11-25 00:26:21 +1100639 case 'g':
Damien Miller7acefbb2014-07-18 14:11:24 +1000640 options.fwd_opts.gateway_ports = 1;
Damien Miller95def091999-11-25 00:26:21 +1100641 break;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100642 case 'O':
Damien Millere1537f92010-01-26 13:26:22 +1100643 if (stdio_forward_host != NULL)
644 fatal("Cannot specify multiplexing "
645 "command with -W");
646 else if (muxclient_command != 0)
647 fatal("Multiplexing command already specified");
Darren Tucker7ebfc102004-11-07 20:06:19 +1100648 if (strcmp(optarg, "check") == 0)
Damien Millerb1cbfa22008-05-19 16:00:08 +1000649 muxclient_command = SSHMUX_COMMAND_ALIVE_CHECK;
Damien Miller388f6fc2010-05-21 14:57:35 +1000650 else if (strcmp(optarg, "forward") == 0)
651 muxclient_command = SSHMUX_COMMAND_FORWARD;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100652 else if (strcmp(optarg, "exit") == 0)
Damien Millerb1cbfa22008-05-19 16:00:08 +1000653 muxclient_command = SSHMUX_COMMAND_TERMINATE;
Damien Miller6c3eec72011-05-05 14:16:22 +1000654 else if (strcmp(optarg, "stop") == 0)
655 muxclient_command = SSHMUX_COMMAND_STOP;
Damien Millerf6dff7c2011-09-22 21:38:52 +1000656 else if (strcmp(optarg, "cancel") == 0)
657 muxclient_command = SSHMUX_COMMAND_CANCEL_FWD;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100658 else
659 fatal("Invalid multiplex command.");
660 break;
Damien Miller147bba32002-09-04 16:46:06 +1000661 case 'P': /* deprecated */
Damien Miller95def091999-11-25 00:26:21 +1100662 options.use_privileged_port = 0;
663 break;
Damien Millerd937dc02013-12-05 10:19:54 +1100664 case 'Q':
Damien Millerea111192013-04-23 19:24:32 +1000665 cp = NULL;
Damien Millerd937dc02013-12-05 10:19:54 +1100666 if (strcmp(optarg, "cipher") == 0)
Damien Miller0fde8ac2013-11-21 14:12:23 +1100667 cp = cipher_alg_list('\n', 0);
Damien Millerd937dc02013-12-05 10:19:54 +1100668 else if (strcmp(optarg, "cipher-auth") == 0)
Damien Miller0fde8ac2013-11-21 14:12:23 +1100669 cp = cipher_alg_list('\n', 1);
Damien Millerd937dc02013-12-05 10:19:54 +1100670 else if (strcmp(optarg, "mac") == 0)
Damien Miller690d9892013-11-08 12:16:49 +1100671 cp = mac_alg_list('\n');
Damien Millerd937dc02013-12-05 10:19:54 +1100672 else if (strcmp(optarg, "kex") == 0)
Damien Miller690d9892013-11-08 12:16:49 +1100673 cp = kex_alg_list('\n');
Damien Millerd937dc02013-12-05 10:19:54 +1100674 else if (strcmp(optarg, "key") == 0)
Damien Miller5be9d9e2013-12-07 11:24:01 +1100675 cp = key_alg_list(0, 0);
676 else if (strcmp(optarg, "key-cert") == 0)
677 cp = key_alg_list(1, 0);
678 else if (strcmp(optarg, "key-plain") == 0)
679 cp = key_alg_list(0, 1);
djm@openbsd.org68d2dfc2015-03-03 06:48:58 +0000680 else if (strcmp(optarg, "protocol-version") == 0) {
681#ifdef WITH_SSH1
682 cp = xstrdup("1\n2");
683#else
684 cp = xstrdup("2");
685#endif
686 }
Damien Millerea111192013-04-23 19:24:32 +1000687 if (cp == NULL)
688 fatal("Unsupported query \"%s\"", optarg);
689 printf("%s\n", cp);
690 free(cp);
691 exit(0);
692 break;
Damien Miller95def091999-11-25 00:26:21 +1100693 case 'a':
694 options.forward_agent = 0;
695 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000696 case 'A':
697 options.forward_agent = 1;
698 break;
Damien Miller95def091999-11-25 00:26:21 +1100699 case 'k':
Damien Millere0113cc2003-11-24 13:10:09 +1100700 options.gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100701 break;
Darren Tucker415bddc2007-06-12 23:43:16 +1000702 case 'K':
703 options.gss_authentication = 1;
704 options.gss_deleg_creds = 1;
705 break;
Damien Miller95def091999-11-25 00:26:21 +1100706 case 'i':
707 if (stat(optarg, &st) < 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000708 fprintf(stderr, "Warning: Identity file %s "
Damien Miller3eb48b62005-03-01 21:15:46 +1100709 "not accessible: %s.\n", optarg,
710 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100711 break;
712 }
Darren Tucker19104782013-04-05 11:13:08 +1100713 add_identity_file(&options, NULL, optarg, 1);
Damien Miller95def091999-11-25 00:26:21 +1100714 break;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000715 case 'I':
Damien Miller7ea845e2010-02-12 09:21:02 +1100716#ifdef ENABLE_PKCS11
717 options.pkcs11_provider = xstrdup(optarg);
Ben Lindstrombcc18082001-08-06 21:59:25 +0000718#else
Damien Miller7ea845e2010-02-12 09:21:02 +1100719 fprintf(stderr, "no support for PKCS#11.\n");
Ben Lindstrombcc18082001-08-06 21:59:25 +0000720#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000721 break;
Damien Miller95def091999-11-25 00:26:21 +1100722 case 't':
Damien Miller21771e22011-05-15 08:45:50 +1000723 if (options.request_tty == REQUEST_TTY_YES)
724 options.request_tty = REQUEST_TTY_FORCE;
725 else
726 options.request_tty = REQUEST_TTY_YES;
Damien Miller95def091999-11-25 00:26:21 +1100727 break;
Damien Miller95def091999-11-25 00:26:21 +1100728 case 'v':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000729 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100730 debug_flag = 1;
731 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000732 } else {
733 if (options.log_level < SYSLOG_LEVEL_DEBUG3)
734 options.log_level++;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000735 }
Damien Miller03d4d7e2013-04-23 15:21:06 +1000736 break;
Damien Miller95def091999-11-25 00:26:21 +1100737 case 'V':
Damien Miller0c889cd2004-03-22 09:36:00 +1100738 fprintf(stderr, "%s, %s\n",
Damien Miller1f0311c2014-05-15 14:24:09 +1000739 SSH_RELEASE,
740#ifdef WITH_OPENSSL
741 SSLeay_version(SSLEAY_VERSION)
742#else
743 "without OpenSSL"
744#endif
745 );
Damien Miller95def091999-11-25 00:26:21 +1100746 if (opt == 'V')
747 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100748 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100749 case 'w':
Damien Miller7b58e802005-12-13 19:33:19 +1100750 if (options.tun_open == -1)
751 options.tun_open = SSH_TUNMODE_DEFAULT;
Damien Millerd27b9472005-12-13 19:29:02 +1100752 options.tun_local = a2tun(optarg, &options.tun_remote);
Damien Miller7b58e802005-12-13 19:33:19 +1100753 if (options.tun_local == SSH_TUNID_ERR) {
Darren Tuckerd6173c02008-06-13 04:52:53 +1000754 fprintf(stderr,
755 "Bad tun device '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100756 exit(255);
Damien Millerd27b9472005-12-13 19:29:02 +1100757 }
758 break;
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100759 case 'W':
Damien Millere1537f92010-01-26 13:26:22 +1100760 if (stdio_forward_host != NULL)
761 fatal("stdio forward already specified");
762 if (muxclient_command != 0)
763 fatal("Cannot specify stdio forward with -O");
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100764 if (parse_forward(&fwd, optarg, 1, 0)) {
765 stdio_forward_host = fwd.listen_host;
766 stdio_forward_port = fwd.listen_port;
Darren Tuckera627d422013-06-02 07:31:17 +1000767 free(fwd.connect_host);
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100768 } else {
769 fprintf(stderr,
770 "Bad stdio forwarding specification '%s'\n",
771 optarg);
772 exit(255);
773 }
Damien Miller21771e22011-05-15 08:45:50 +1000774 options.request_tty = REQUEST_TTY_NO;
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100775 no_shell_flag = 1;
776 options.clear_forwardings = 1;
777 options.exit_on_forward_failure = 1;
778 break;
Damien Miller95def091999-11-25 00:26:21 +1100779 case 'q':
780 options.log_level = SYSLOG_LEVEL_QUIET;
781 break;
Damien Miller95def091999-11-25 00:26:21 +1100782 case 'e':
783 if (optarg[0] == '^' && optarg[2] == 0 &&
Damien Millerf4614452001-07-14 12:18:10 +1000784 (u_char) optarg[1] >= 64 &&
785 (u_char) optarg[1] < 128)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000786 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100787 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000788 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100789 else if (strcmp(optarg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000790 options.escape_char = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100791 else {
Damien Millerf4614452001-07-14 12:18:10 +1000792 fprintf(stderr, "Bad escape character '%s'.\n",
793 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100794 exit(255);
Damien Miller95def091999-11-25 00:26:21 +1100795 }
796 break;
Damien Miller95def091999-11-25 00:26:21 +1100797 case 'c':
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000798 if (ciphers_valid(*optarg == '+' ?
799 optarg + 1 : optarg)) {
Damien Millereba71ba2000-04-29 23:57:08 +1000800 /* SSH2 only */
801 options.ciphers = xstrdup(optarg);
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000802 options.cipher = SSH_CIPHER_INVALID;
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000803 break;
Damien Miller95def091999-11-25 00:26:21 +1100804 }
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000805 /* SSH1 only */
806 options.cipher = cipher_number(optarg);
807 if (options.cipher == -1) {
808 fprintf(stderr, "Unknown cipher type '%s'\n",
809 optarg);
810 exit(255);
811 }
812 if (options.cipher == SSH_CIPHER_3DES)
813 options.ciphers = xstrdup("3des-cbc");
814 else if (options.cipher == SSH_CIPHER_BLOWFISH)
815 options.ciphers = xstrdup("blowfish-cbc");
816 else
817 options.ciphers = xstrdup(KEX_CLIENT_ENCRYPT);
Damien Miller95def091999-11-25 00:26:21 +1100818 break;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000819 case 'm':
820 if (mac_valid(optarg))
821 options.macs = xstrdup(optarg);
822 else {
Damien Millerf4614452001-07-14 12:18:10 +1000823 fprintf(stderr, "Unknown mac type '%s'\n",
824 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100825 exit(255);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000826 }
827 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000828 case 'M':
Damien Millerd14b1e72005-06-16 13:19:41 +1000829 if (options.control_master == SSHCTL_MASTER_YES)
830 options.control_master = SSHCTL_MASTER_ASK;
831 else
832 options.control_master = SSHCTL_MASTER_YES;
Damien Miller0e220db2004-06-15 10:34:08 +1000833 break;
Damien Miller95def091999-11-25 00:26:21 +1100834 case 'p':
Ben Lindstrom19066a12001-04-12 23:39:26 +0000835 options.port = a2port(optarg);
Damien Miller3dc71ad2009-01-28 16:31:22 +1100836 if (options.port <= 0) {
Ben Lindstrom146edb92001-04-11 23:06:28 +0000837 fprintf(stderr, "Bad port '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100838 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000839 }
Damien Miller95def091999-11-25 00:26:21 +1100840 break;
Damien Miller95def091999-11-25 00:26:21 +1100841 case 'l':
842 options.user = optarg;
843 break;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000844
Damien Miller95def091999-11-25 00:26:21 +1100845 case 'L':
Damien Miller4bf648f2009-02-14 16:28:21 +1100846 if (parse_forward(&fwd, optarg, 0, 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +1100847 add_local_forward(&options, &fwd);
848 else {
Damien Millerf4614452001-07-14 12:18:10 +1000849 fprintf(stderr,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100850 "Bad local forwarding specification '%s'\n",
Damien Millerf4614452001-07-14 12:18:10 +1000851 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100852 exit(255);
Ben Lindstrom1a174712001-09-12 17:56:15 +0000853 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100854 break;
855
856 case 'R':
Damien Miller4bf648f2009-02-14 16:28:21 +1100857 if (parse_forward(&fwd, optarg, 0, 1)) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100858 add_remote_forward(&options, &fwd);
859 } else {
860 fprintf(stderr,
861 "Bad remote forwarding specification "
862 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100863 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100864 }
Damien Miller95def091999-11-25 00:26:21 +1100865 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000866
867 case 'D':
Damien Miller4bf648f2009-02-14 16:28:21 +1100868 if (parse_forward(&fwd, optarg, 1, 0)) {
Damien Millera699d952008-11-03 19:27:34 +1100869 add_local_forward(&options, &fwd);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100870 } else {
Damien Millera699d952008-11-03 19:27:34 +1100871 fprintf(stderr,
872 "Bad dynamic forwarding specification "
873 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100874 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000875 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000876 break;
877
Damien Miller95def091999-11-25 00:26:21 +1100878 case 'C':
879 options.compression = 1;
880 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000881 case 'N':
882 no_shell_flag = 1;
Damien Miller21771e22011-05-15 08:45:50 +1000883 options.request_tty = REQUEST_TTY_NO;
Damien Miller1383bd82000-04-06 12:32:37 +1000884 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000885 case 'T':
Damien Miller21771e22011-05-15 08:45:50 +1000886 options.request_tty = REQUEST_TTY_NO;
Damien Miller1383bd82000-04-06 12:32:37 +1000887 break;
Damien Miller95def091999-11-25 00:26:21 +1100888 case 'o':
Damien Miller9836cf82003-12-17 16:30:06 +1100889 line = xstrdup(optarg);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000890 if (process_config_line(&options, pw,
891 host ? host : "", host ? host : "", line,
892 "command-line", 0, NULL, SSHCONF_USERCONF) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100893 exit(255);
Darren Tuckera627d422013-06-02 07:31:17 +1000894 free(line);
Damien Miller95def091999-11-25 00:26:21 +1100895 break;
Damien Miller832562e2001-01-30 09:30:01 +1100896 case 's':
897 subsystem_flag = 1;
898 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000899 case 'S':
900 if (options.control_path != NULL)
901 free(options.control_path);
902 options.control_path = xstrdup(optarg);
Damien Miller0e220db2004-06-15 10:34:08 +1000903 break;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000904 case 'b':
905 options.bind_address = optarg;
906 break;
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000907 case 'F':
908 config = optarg;
909 break;
Damien Miller95def091999-11-25 00:26:21 +1100910 default:
911 usage();
912 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000913 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000914
Damien Millerf4614452001-07-14 12:18:10 +1000915 ac -= optind;
916 av += optind;
917
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100918 if (ac > 0 && !host) {
Ben Lindstromc276c122002-12-23 02:14:51 +0000919 if (strrchr(*av, '@')) {
Damien Millerf4614452001-07-14 12:18:10 +1000920 p = xstrdup(*av);
Ben Lindstromc276c122002-12-23 02:14:51 +0000921 cp = strrchr(p, '@');
Damien Millerf4614452001-07-14 12:18:10 +1000922 if (cp == NULL || cp == p)
923 usage();
924 options.user = p;
925 *cp = '\0';
Damien Miller0faf7472013-10-17 11:47:23 +1100926 host = xstrdup(++cp);
Damien Millerf4614452001-07-14 12:18:10 +1000927 } else
Damien Miller0faf7472013-10-17 11:47:23 +1100928 host = xstrdup(*av);
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000929 if (ac > 1) {
930 optind = optreset = 1;
Damien Millerf4614452001-07-14 12:18:10 +1000931 goto again;
932 }
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000933 ac--, av++;
Damien Millerf4614452001-07-14 12:18:10 +1000934 }
935
Damien Miller95def091999-11-25 00:26:21 +1100936 /* Check that we got a host name. */
937 if (!host)
938 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000939
Damien Miller0faf7472013-10-17 11:47:23 +1100940 host_arg = xstrdup(host);
941
Damien Miller1f0311c2014-05-15 14:24:09 +1000942#ifdef WITH_OPENSSL
Damien Miller4314c2b2010-09-10 11:12:09 +1000943 OpenSSL_add_all_algorithms();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100944 ERR_load_crypto_strings();
Damien Miller1f0311c2014-05-15 14:24:09 +1000945#endif
Damien Millercb5e44a2000-09-29 12:12:36 +1100946
Damien Miller95def091999-11-25 00:26:21 +1100947 /* Initialize the command to execute on remote host. */
948 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000949
Damien Miller5428f641999-11-25 11:54:57 +1100950 /*
951 * Save the command to execute on the remote host in a buffer. There
952 * is no limit on the length of the command, except by the maximum
953 * packet size. Also sets the tty flag if there is no command.
954 */
Damien Millerf4614452001-07-14 12:18:10 +1000955 if (!ac) {
Damien Miller95def091999-11-25 00:26:21 +1100956 /* No command specified - execute shell on a tty. */
Damien Miller832562e2001-01-30 09:30:01 +1100957 if (subsystem_flag) {
Damien Millerf4614452001-07-14 12:18:10 +1000958 fprintf(stderr,
959 "You must specify a subsystem to invoke.\n");
Damien Miller832562e2001-01-30 09:30:01 +1100960 usage();
961 }
Damien Miller95def091999-11-25 00:26:21 +1100962 } else {
Damien Millerf4614452001-07-14 12:18:10 +1000963 /* A command has been specified. Store it into the buffer. */
964 for (i = 0; i < ac; i++) {
965 if (i)
Damien Miller95def091999-11-25 00:26:21 +1100966 buffer_append(&command, " ", 1);
967 buffer_append(&command, av[i], strlen(av[i]));
968 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000969 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000970
Damien Miller95def091999-11-25 00:26:21 +1100971 /* Cannot fork to background if no command. */
Darren Tuckerd6173c02008-06-13 04:52:53 +1000972 if (fork_after_authentication_flag && buffer_len(&command) == 0 &&
973 !no_shell_flag)
974 fatal("Cannot fork into background without a command "
975 "to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000976
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000977 /*
978 * Initialize "log" output. Since we are the client all output
Damien Miller03d4d7e2013-04-23 15:21:06 +1000979 * goes to stderr unless otherwise specified by -y or -E.
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000980 */
Damien Miller03d4d7e2013-04-23 15:21:06 +1000981 if (use_syslog && logfile != NULL)
982 fatal("Can't specify both -y and -E");
983 if (logfile != NULL) {
984 log_redirect_stderr_to(logfile);
Darren Tuckera627d422013-06-02 07:31:17 +1000985 free(logfile);
Damien Miller03d4d7e2013-04-23 15:21:06 +1000986 }
Darren Tucker72efd742009-06-21 17:48:00 +1000987 log_init(argv0,
Darren Tuckerd6173c02008-06-13 04:52:53 +1000988 options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
Damien Millere272a5b2008-11-03 19:22:37 +1100989 SYSLOG_FACILITY_USER, !use_syslog);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000990
Damien Miller03d4d7e2013-04-23 15:21:06 +1000991 if (debug_flag)
Damien Miller1f0311c2014-05-15 14:24:09 +1000992 logit("%s, %s", SSH_RELEASE,
993#ifdef WITH_OPENSSL
994 SSLeay_version(SSLEAY_VERSION)
995#else
996 "without OpenSSL"
997#endif
998 );
Damien Miller03d4d7e2013-04-23 15:21:06 +1000999
Damien Miller13f97b22014-02-24 15:57:55 +11001000 /* Parse the configuration files */
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001001 process_config_files(host_arg, pw, 0);
Ben Lindstrom83f07d12001-10-03 17:22:29 +00001002
Damien Miller13f97b22014-02-24 15:57:55 +11001003 /* Hostname canonicalisation needs a few options filled. */
1004 fill_default_options_for_canonicalization(&options);
1005
1006 /* If the user has replaced the hostname then take it into use now */
1007 if (options.hostname != NULL) {
1008 /* NB. Please keep in sync with readconf.c:match_cfg_line() */
1009 cp = percent_expand(options.hostname,
1010 "h", host, (char *)NULL);
1011 free(host);
1012 host = cp;
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001013 free(options.hostname);
1014 options.hostname = xstrdup(host);
Damien Miller13f97b22014-02-24 15:57:55 +11001015 }
1016
1017 /* If canonicalization requested then try to apply it */
1018 lowercase(host);
1019 if (options.canonicalize_hostname != SSH_CANONICALISE_NO)
1020 addrs = resolve_canonicalize(&host, options.port);
1021
1022 /*
Damien Miller08b57c62014-02-27 10:17:13 +11001023 * If CanonicalizePermittedCNAMEs have been specified but
1024 * other canonicalization did not happen (by not being requested
1025 * or by failing with fallback) then the hostname may still be changed
1026 * as a result of CNAME following.
1027 *
1028 * Try to resolve the bare hostname name using the system resolver's
1029 * usual search rules and then apply the CNAME follow rules.
1030 *
1031 * Skip the lookup if a ProxyCommand is being used unless the user
1032 * has specifically requested canonicalisation for this case via
1033 * CanonicalizeHostname=always
Damien Miller13f97b22014-02-24 15:57:55 +11001034 */
Damien Miller08b57c62014-02-27 10:17:13 +11001035 if (addrs == NULL && options.num_permitted_cnames != 0 &&
1036 (option_clear_or_none(options.proxy_command) ||
Damien Miller13f97b22014-02-24 15:57:55 +11001037 options.canonicalize_hostname == SSH_CANONICALISE_ALWAYS)) {
Damien Miller19439e92014-07-02 15:28:40 +10001038 if ((addrs = resolve_host(host, options.port,
1039 option_clear_or_none(options.proxy_command),
1040 cname, sizeof(cname))) == NULL) {
1041 /* Don't fatal proxied host names not in the DNS */
1042 if (option_clear_or_none(options.proxy_command))
1043 cleanup_exit(255); /* logged in resolve_host */
1044 } else
1045 check_follow_cname(&host, cname);
Damien Miller13f97b22014-02-24 15:57:55 +11001046 }
1047
1048 /*
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001049 * If canonicalisation is enabled then re-parse the configuration
1050 * files as new stanzas may match.
Damien Miller13f97b22014-02-24 15:57:55 +11001051 */
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001052 if (options.canonicalize_hostname != 0) {
1053 debug("Re-reading configuration after hostname "
1054 "canonicalisation");
1055 free(options.hostname);
1056 options.hostname = xstrdup(host);
1057 process_config_files(host_arg, pw, 1);
1058 /*
1059 * Address resolution happens early with canonicalisation
1060 * enabled and the port number may have changed since, so
1061 * reset it in address list
1062 */
1063 if (addrs != NULL && options.port > 0)
1064 set_addrinfo_port(addrs, options.port);
Ben Lindstrom14f31ab2001-09-12 17:48:04 +00001065 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001066
Damien Miller95def091999-11-25 00:26:21 +11001067 /* Fill configuration defaults. */
1068 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001069
Damien Miller13f97b22014-02-24 15:57:55 +11001070 if (options.port == 0)
1071 options.port = default_ssh_port();
Darren Tucker0a4f04b2003-07-03 20:37:47 +10001072 channel_set_af(options.address_family);
1073
Damien Millere9fc72e2013-10-15 12:14:12 +11001074 /* Tidy and check options */
1075 if (options.host_key_alias != NULL)
1076 lowercase(options.host_key_alias);
1077 if (options.proxy_command != NULL &&
1078 strcmp(options.proxy_command, "-") == 0 &&
1079 options.proxy_use_fdpass)
1080 fatal("ProxyCommand=- and ProxyUseFDPass are incompatible");
djm@openbsd.org44732de2015-02-20 22:17:21 +00001081 if (options.control_persist &&
1082 options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK) {
1083 debug("UpdateHostKeys=ask is incompatible with ControlPersist; "
1084 "disabling");
1085 options.update_hostkeys = 0;
1086 }
Damien Miller0faf7472013-10-17 11:47:23 +11001087#ifndef HAVE_CYGWIN
1088 if (original_effective_uid != 0)
1089 options.use_privileged_port = 0;
1090#endif
Damien Millere9fc72e2013-10-15 12:14:12 +11001091
Damien Miller95def091999-11-25 00:26:21 +11001092 /* reinit */
Darren Tucker72efd742009-06-21 17:48:00 +10001093 log_init(argv0, options.log_level, SYSLOG_FACILITY_USER, !use_syslog);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001094
Damien Millerfff9f092012-07-06 13:45:01 +10001095 if (options.request_tty == REQUEST_TTY_YES ||
1096 options.request_tty == REQUEST_TTY_FORCE)
1097 tty_flag = 1;
1098
1099 /* Allocate a tty by default if no command specified. */
1100 if (buffer_len(&command) == 0)
1101 tty_flag = options.request_tty != REQUEST_TTY_NO;
1102
1103 /* Force no tty */
1104 if (options.request_tty == REQUEST_TTY_NO || muxclient_command != 0)
1105 tty_flag = 0;
1106 /* Do not allocate a tty if stdin is not a tty. */
1107 if ((!isatty(fileno(stdin)) || stdin_null_flag) &&
1108 options.request_tty != REQUEST_TTY_FORCE) {
1109 if (tty_flag)
1110 logit("Pseudo-terminal will not be allocated because "
1111 "stdin is not a terminal.");
1112 tty_flag = 0;
1113 }
1114
Damien Miller60bc5172001-03-19 09:38:15 +11001115 seed_rng();
1116
Damien Miller95def091999-11-25 00:26:21 +11001117 if (options.user == NULL)
1118 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001119
Damien Millerdfc85fa2011-05-15 08:44:02 +10001120 if (gethostname(thishost, sizeof(thishost)) == -1)
1121 fatal("gethostname: %s", strerror(errno));
1122 strlcpy(shorthost, thishost, sizeof(shorthost));
1123 shorthost[strcspn(thishost, ".")] = '\0';
1124 snprintf(portstr, sizeof(portstr), "%d", options.port);
Darren Tuckerf6b01b72008-06-13 04:56:37 +10001125
Damien Miller9c386432014-07-03 21:27:46 +10001126 if ((md = ssh_digest_start(SSH_DIGEST_SHA1)) == NULL ||
1127 ssh_digest_update(md, thishost, strlen(thishost)) < 0 ||
1128 ssh_digest_update(md, host, strlen(host)) < 0 ||
1129 ssh_digest_update(md, portstr, strlen(portstr)) < 0 ||
1130 ssh_digest_update(md, options.user, strlen(options.user)) < 0 ||
1131 ssh_digest_final(md, conn_hash, sizeof(conn_hash)) < 0)
1132 fatal("%s: mux digest failed", __func__);
1133 ssh_digest_free(md);
1134 conn_hash_hex = tohex(conn_hash, ssh_digest_bytes(SSH_DIGEST_SHA1));
1135
Damien Millerdfc85fa2011-05-15 08:44:02 +10001136 if (options.local_command != NULL) {
Darren Tuckerf6b01b72008-06-13 04:56:37 +10001137 debug3("expanding LocalCommand: %s", options.local_command);
1138 cp = options.local_command;
Damien Miller9c386432014-07-03 21:27:46 +10001139 options.local_command = percent_expand(cp,
1140 "C", conn_hash_hex,
1141 "L", shorthost,
1142 "d", pw->pw_dir,
1143 "h", host,
1144 "l", thishost,
1145 "n", host_arg,
1146 "p", portstr,
1147 "r", options.user,
1148 "u", pw->pw_name,
Damien Millerdfc85fa2011-05-15 08:44:02 +10001149 (char *)NULL);
Darren Tuckerf6b01b72008-06-13 04:56:37 +10001150 debug3("expanded LocalCommand: %s", options.local_command);
Darren Tuckera627d422013-06-02 07:31:17 +10001151 free(cp);
Darren Tuckerf6b01b72008-06-13 04:56:37 +10001152 }
1153
Damien Miller0e220db2004-06-15 10:34:08 +10001154 if (options.control_path != NULL) {
Damien Miller6476cad2005-06-16 13:18:34 +10001155 cp = tilde_expand_filename(options.control_path,
1156 original_real_uid);
Darren Tuckera627d422013-06-02 07:31:17 +10001157 free(options.control_path);
Damien Miller9c386432014-07-03 21:27:46 +10001158 options.control_path = percent_expand(cp,
1159 "C", conn_hash_hex,
1160 "L", shorthost,
1161 "h", host,
1162 "l", thishost,
1163 "n", host_arg,
1164 "p", portstr,
1165 "r", options.user,
1166 "u", pw->pw_name,
Damien Millerdfc85fa2011-05-15 08:44:02 +10001167 (char *)NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001168 free(cp);
Damien Miller0e220db2004-06-15 10:34:08 +10001169 }
Damien Miller9c386432014-07-03 21:27:46 +10001170 free(conn_hash_hex);
1171
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001172 if (config_test) {
1173 dump_client_config(&options, host);
1174 exit(0);
1175 }
1176
Damien Millerb1cbfa22008-05-19 16:00:08 +10001177 if (muxclient_command != 0 && options.control_path == NULL)
Darren Tucker0814d312005-06-01 23:08:51 +10001178 fatal("No ControlPath specified for \"-O\" command");
Damien Millerd14b1e72005-06-16 13:19:41 +10001179 if (options.control_path != NULL)
Damien Millerb1cbfa22008-05-19 16:00:08 +10001180 muxclient(options.control_path);
Damien Miller0e220db2004-06-15 10:34:08 +10001181
Damien Miller08b57c62014-02-27 10:17:13 +11001182 /*
1183 * If hostname canonicalisation was not enabled, then we may not
1184 * have yet resolved the hostname. Do so now.
1185 */
1186 if (addrs == NULL && options.proxy_command == NULL) {
1187 if ((addrs = resolve_host(host, options.port, 1,
1188 cname, sizeof(cname))) == NULL)
1189 cleanup_exit(255); /* resolve_host logs the error */
1190 }
1191
Damien Miller67bd0622007-09-17 12:06:57 +10001192 timeout_ms = options.connection_timeout * 1000;
1193
Kevin Stevesfcec7f82000-12-15 19:55:48 +00001194 /* Open a connection to the remote host. */
Damien Miller0faf7472013-10-17 11:47:23 +11001195 if (ssh_connect(host, addrs, &hostaddr, options.port,
1196 options.address_family, options.connection_attempts,
1197 &timeout_ms, options.tcp_keep_alive,
1198 options.use_privileged_port) != 0)
1199 exit(255);
1200
Damien Miller28631ce2013-10-26 10:07:56 +11001201 if (addrs != NULL)
1202 freeaddrinfo(addrs);
1203
Damien Miller0faf7472013-10-17 11:47:23 +11001204 packet_set_timeout(options.server_alive_interval,
1205 options.server_alive_count_max);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001206
Damien Miller67bd0622007-09-17 12:06:57 +10001207 if (timeout_ms > 0)
1208 debug3("timeout: %d ms remain after connect", timeout_ms);
1209
Damien Miller5428f641999-11-25 11:54:57 +11001210 /*
1211 * If we successfully made the connection, load the host private key
1212 * in case we will need it later for combined rsa-rhosts
1213 * authentication. This must be done before releasing extra
1214 * privileges, because the file is only readable by root.
Ben Lindstrom9e5bb572002-06-06 19:58:27 +00001215 * If we cannot access the private keys, load the public keys
1216 * instead and try to execute the ssh-keysign helper instead.
Damien Miller5428f641999-11-25 11:54:57 +11001217 */
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001218 sensitive_data.nkeys = 0;
1219 sensitive_data.keys = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001220 sensitive_data.external_keysign = 0;
Ben Lindstrom24157572002-06-12 16:09:39 +00001221 if (options.rhosts_rsa_authentication ||
1222 options.hostbased_authentication) {
Damien Miller0fa47cf2013-12-29 17:53:39 +11001223 sensitive_data.nkeys = 9;
Damien Millerddd63ab2006-03-31 23:10:51 +11001224 sensitive_data.keys = xcalloc(sensitive_data.nkeys,
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001225 sizeof(Key));
Damien Miller6af914a2010-09-10 11:39:26 +10001226 for (i = 0; i < sensitive_data.nkeys; i++)
1227 sensitive_data.keys[i] = NULL;
Ben Lindstromf9c48842002-06-11 16:37:51 +00001228
1229 PRIV_START;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001230 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
Darren Tucker232b76f2006-05-06 17:41:51 +10001231 _PATH_HOST_KEY_FILE, "", NULL, NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001232#ifdef OPENSSL_HAS_ECC
djm@openbsd.orgab24ab82015-01-08 10:15:45 +00001233 sensitive_data.keys[1] = key_load_private_cert(KEY_ECDSA,
Damien Millereb8b60e2010-08-31 22:41:14 +10001234 _PATH_HOST_ECDSA_KEY_FILE, "", NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001235#endif
djm@openbsd.orgab24ab82015-01-08 10:15:45 +00001236 sensitive_data.keys[2] = key_load_private_cert(KEY_ED25519,
1237 _PATH_HOST_ED25519_KEY_FILE, "", NULL);
Damien Millereb8b60e2010-08-31 22:41:14 +10001238 sensitive_data.keys[3] = key_load_private_cert(KEY_RSA,
Damien Millerc1583312010-08-05 13:04:50 +10001239 _PATH_HOST_RSA_KEY_FILE, "", NULL);
djm@openbsd.orgab24ab82015-01-08 10:15:45 +00001240 sensitive_data.keys[4] = key_load_private_cert(KEY_DSA,
1241 _PATH_HOST_DSA_KEY_FILE, "", NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001242#ifdef OPENSSL_HAS_ECC
djm@openbsd.orgab24ab82015-01-08 10:15:45 +00001243 sensitive_data.keys[5] = key_load_private_type(KEY_ECDSA,
Damien Millereb8b60e2010-08-31 22:41:14 +10001244 _PATH_HOST_ECDSA_KEY_FILE, "", NULL, NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001245#endif
djm@openbsd.orgab24ab82015-01-08 10:15:45 +00001246 sensitive_data.keys[6] = key_load_private_type(KEY_ED25519,
1247 _PATH_HOST_ED25519_KEY_FILE, "", NULL, NULL);
Damien Miller0fa47cf2013-12-29 17:53:39 +11001248 sensitive_data.keys[7] = key_load_private_type(KEY_RSA,
Darren Tucker232b76f2006-05-06 17:41:51 +10001249 _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL);
djm@openbsd.orgab24ab82015-01-08 10:15:45 +00001250 sensitive_data.keys[8] = key_load_private_type(KEY_DSA,
1251 _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL);
Ben Lindstromf9c48842002-06-11 16:37:51 +00001252 PRIV_END;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001253
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +00001254 if (options.hostbased_authentication == 1 &&
1255 sensitive_data.keys[0] == NULL &&
Damien Millereb8b60e2010-08-31 22:41:14 +10001256 sensitive_data.keys[5] == NULL &&
Damien Miller5be9d9e2013-12-07 11:24:01 +11001257 sensitive_data.keys[6] == NULL &&
Damien Miller0fa47cf2013-12-29 17:53:39 +11001258 sensitive_data.keys[7] == NULL &&
1259 sensitive_data.keys[8] == NULL) {
Damien Miller6af914a2010-09-10 11:39:26 +10001260#ifdef OPENSSL_HAS_ECC
djm@openbsd.orgab24ab82015-01-08 10:15:45 +00001261 sensitive_data.keys[1] = key_load_cert(
Damien Millereb8b60e2010-08-31 22:41:14 +10001262 _PATH_HOST_ECDSA_KEY_FILE);
Damien Miller6af914a2010-09-10 11:39:26 +10001263#endif
djm@openbsd.orgab24ab82015-01-08 10:15:45 +00001264 sensitive_data.keys[2] = key_load_cert(
1265 _PATH_HOST_ED25519_KEY_FILE);
Damien Millereb8b60e2010-08-31 22:41:14 +10001266 sensitive_data.keys[3] = key_load_cert(
Damien Millerc1583312010-08-05 13:04:50 +10001267 _PATH_HOST_RSA_KEY_FILE);
Damien Miller0fa47cf2013-12-29 17:53:39 +11001268 sensitive_data.keys[4] = key_load_cert(
djm@openbsd.orgab24ab82015-01-08 10:15:45 +00001269 _PATH_HOST_DSA_KEY_FILE);
Damien Miller6af914a2010-09-10 11:39:26 +10001270#ifdef OPENSSL_HAS_ECC
djm@openbsd.orgab24ab82015-01-08 10:15:45 +00001271 sensitive_data.keys[5] = key_load_public(
Damien Millereb8b60e2010-08-31 22:41:14 +10001272 _PATH_HOST_ECDSA_KEY_FILE, NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001273#endif
djm@openbsd.orgab24ab82015-01-08 10:15:45 +00001274 sensitive_data.keys[6] = key_load_public(
1275 _PATH_HOST_ED25519_KEY_FILE, NULL);
Damien Miller5be9d9e2013-12-07 11:24:01 +11001276 sensitive_data.keys[7] = key_load_public(
Damien Miller0fa47cf2013-12-29 17:53:39 +11001277 _PATH_HOST_RSA_KEY_FILE, NULL);
1278 sensitive_data.keys[8] = key_load_public(
djm@openbsd.orgab24ab82015-01-08 10:15:45 +00001279 _PATH_HOST_DSA_KEY_FILE, NULL);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001280 sensitive_data.external_keysign = 1;
1281 }
Damien Miller95def091999-11-25 00:26:21 +11001282 }
Damien Miller5428f641999-11-25 11:54:57 +11001283 /*
1284 * Get rid of any extra privileges that we may have. We will no
1285 * longer need them. Also, extra privileges could make it very hard
1286 * to read identity files and other non-world-readable files from the
1287 * user's home directory if it happens to be on a NFS volume where
1288 * root is mapped to nobody.
1289 */
Darren Tucker25f60a72004-08-15 17:23:34 +10001290 if (original_effective_uid == 0) {
1291 PRIV_START;
1292 permanently_set_uid(pw);
1293 }
Damien Miller95def091999-11-25 00:26:21 +11001294
Damien Miller5428f641999-11-25 11:54:57 +11001295 /*
1296 * Now that we are back to our own permissions, create ~/.ssh
Damien Miller788f2122005-11-05 15:14:59 +11001297 * directory if it doesn't already exist.
Damien Miller5428f641999-11-25 11:54:57 +11001298 */
Darren Tucker45c66d72011-11-04 10:50:40 +11001299 if (config == NULL) {
1300 r = snprintf(buf, sizeof buf, "%s%s%s", pw->pw_dir,
1301 strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
1302 if (r > 0 && (size_t)r < sizeof(buf) && stat(buf, &st) < 0) {
Darren Tucker8ccb7392010-09-10 12:28:24 +10001303#ifdef WITH_SELINUX
Darren Tucker45c66d72011-11-04 10:50:40 +11001304 ssh_selinux_setfscreatecon(buf);
Darren Tucker8ccb7392010-09-10 12:28:24 +10001305#endif
Darren Tucker45c66d72011-11-04 10:50:40 +11001306 if (mkdir(buf, 0700) < 0)
1307 error("Could not create directory '%.200s'.",
1308 buf);
Darren Tucker8ccb7392010-09-10 12:28:24 +10001309#ifdef WITH_SELINUX
Darren Tucker45c66d72011-11-04 10:50:40 +11001310 ssh_selinux_setfscreatecon(NULL);
Darren Tucker8ccb7392010-09-10 12:28:24 +10001311#endif
Darren Tucker45c66d72011-11-04 10:50:40 +11001312 }
Darren Tucker8ccb7392010-09-10 12:28:24 +10001313 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001314 /* load options.identity_files */
1315 load_public_identity_files();
1316
1317 /* Expand ~ in known host file names. */
Damien Miller295ee632011-05-29 21:42:31 +10001318 tilde_expand_paths(options.system_hostfiles,
1319 options.num_system_hostfiles);
1320 tilde_expand_paths(options.user_hostfiles, options.num_user_hostfiles);
Damien Miller95def091999-11-25 00:26:21 +11001321
Damien Miller07cd5892001-11-12 10:52:03 +11001322 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
Damien Miller857b02e2010-09-24 22:02:56 +10001323 signal(SIGCHLD, main_sigchld_handler);
Damien Miller07cd5892001-11-12 10:52:03 +11001324
Darren Tuckerd6173c02008-06-13 04:52:53 +10001325 /* Log into the remote system. Never returns if the login fails. */
Damien Miller67bd0622007-09-17 12:06:57 +10001326 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr,
Damien Millerd925dcd2010-12-01 12:21:51 +11001327 options.port, pw, timeout_ms);
Damien Miller95def091999-11-25 00:26:21 +11001328
Damien Miller383ffe62010-06-26 10:02:03 +10001329 if (packet_connection_is_on_socket()) {
1330 verbose("Authenticated to %s ([%s]:%d).", host,
1331 get_remote_ipaddr(), get_remote_port());
1332 } else {
1333 verbose("Authenticated to %s (via proxy).", host);
1334 }
1335
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001336 /* We no longer need the private host keys. Clear them now. */
1337 if (sensitive_data.nkeys != 0) {
1338 for (i = 0; i < sensitive_data.nkeys; i++) {
1339 if (sensitive_data.keys[i] != NULL) {
1340 /* Destroys contents safely */
1341 debug3("clear hostkey %d", i);
1342 key_free(sensitive_data.keys[i]);
1343 sensitive_data.keys[i] = NULL;
1344 }
1345 }
Darren Tuckera627d422013-06-02 07:31:17 +10001346 free(sensitive_data.keys);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001347 }
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +00001348 for (i = 0; i < options.num_identity_files; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10001349 free(options.identity_files[i]);
1350 options.identity_files[i] = NULL;
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +00001351 if (options.identity_keys[i]) {
1352 key_free(options.identity_keys[i]);
1353 options.identity_keys[i] = NULL;
1354 }
1355 }
Damien Miller95def091999-11-25 00:26:21 +11001356
Damien Miller1383bd82000-04-06 12:32:37 +10001357 exit_status = compat20 ? ssh_session2() : ssh_session();
1358 packet_close();
Damien Miller8c4e18a2002-09-19 12:05:02 +10001359
Damien Millerb1cbfa22008-05-19 16:00:08 +10001360 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10001361 unlink(options.control_path);
1362
Damien Millera41ccca2010-10-07 22:07:32 +11001363 /* Kill ProxyCommand if it is running. */
1364 ssh_kill_proxy_command();
Damien Miller8c4e18a2002-09-19 12:05:02 +10001365
Damien Miller1383bd82000-04-06 12:32:37 +10001366 return exit_status;
1367}
1368
Damien Millere11e1ea2010-08-03 16:04:46 +10001369static void
1370control_persist_detach(void)
1371{
1372 pid_t pid;
Damien Miller00d9ae22010-08-17 01:59:31 +10001373 int devnull;
Damien Millere11e1ea2010-08-03 16:04:46 +10001374
1375 debug("%s: backgrounding master process", __func__);
1376
1377 /*
1378 * master (current process) into the background, and make the
1379 * foreground process a client of the backgrounded master.
1380 */
1381 switch ((pid = fork())) {
1382 case -1:
1383 fatal("%s: fork: %s", __func__, strerror(errno));
1384 case 0:
1385 /* Child: master process continues mainloop */
1386 break;
1387 default:
1388 /* Parent: set up mux slave to connect to backgrounded master */
1389 debug2("%s: background process is %ld", __func__, (long)pid);
1390 stdin_null_flag = ostdin_null_flag;
Damien Miller21771e22011-05-15 08:45:50 +10001391 options.request_tty = orequest_tty;
Damien Millere11e1ea2010-08-03 16:04:46 +10001392 tty_flag = otty_flag;
1393 close(muxserver_sock);
1394 muxserver_sock = -1;
Damien Miller5929c522010-09-10 11:17:02 +10001395 options.control_master = SSHCTL_MASTER_NO;
Damien Millere11e1ea2010-08-03 16:04:46 +10001396 muxclient(options.control_path);
1397 /* muxclient() doesn't return on success. */
1398 fatal("Failed to connect to new control master");
1399 }
Damien Miller00d9ae22010-08-17 01:59:31 +10001400 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
1401 error("%s: open(\"/dev/null\"): %s", __func__,
1402 strerror(errno));
1403 } else {
1404 if (dup2(devnull, STDIN_FILENO) == -1 ||
1405 dup2(devnull, STDOUT_FILENO) == -1)
1406 error("%s: dup2: %s", __func__, strerror(errno));
1407 if (devnull > STDERR_FILENO)
1408 close(devnull);
1409 }
Damien Miller98e27dc2013-07-25 11:55:52 +10001410 daemon(1, 1);
Damien Millerea2c1a42011-06-03 12:10:22 +10001411 setproctitle("%s [mux]", options.control_path);
Damien Millere11e1ea2010-08-03 16:04:46 +10001412}
1413
1414/* Do fork() after authentication. Used by "ssh -f" */
1415static void
1416fork_postauth(void)
1417{
1418 if (need_controlpersist_detach)
1419 control_persist_detach();
1420 debug("forking to background");
1421 fork_after_authentication_flag = 0;
1422 if (daemon(1, 1) < 0)
1423 fatal("daemon() failed: %.200s", strerror(errno));
1424}
1425
Darren Tucker9f407c42008-06-13 04:50:27 +10001426/* Callback for remote forward global requests */
1427static void
1428ssh_confirm_remote_forward(int type, u_int32_t seq, void *ctxt)
1429{
Damien Miller7acefbb2014-07-18 14:11:24 +10001430 struct Forward *rfwd = (struct Forward *)ctxt;
Darren Tucker9f407c42008-06-13 04:50:27 +10001431
Damien Miller4bf648f2009-02-14 16:28:21 +11001432 /* XXX verbose() on failure? */
Damien Miller4b3ed642014-07-02 15:29:40 +10001433 debug("remote forward %s for: listen %s%s%d, connect %s:%d",
Darren Tucker9f407c42008-06-13 04:50:27 +10001434 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
Damien Miller7acefbb2014-07-18 14:11:24 +10001435 rfwd->listen_path ? rfwd->listen_path :
1436 rfwd->listen_host ? rfwd->listen_host : "",
1437 (rfwd->listen_path || rfwd->listen_host) ? ":" : "",
1438 rfwd->listen_port, rfwd->connect_path ? rfwd->connect_path :
1439 rfwd->connect_host, rfwd->connect_port);
1440 if (rfwd->listen_path == NULL && rfwd->listen_port == 0) {
Darren Tucker68afb8c2011-10-02 18:59:03 +11001441 if (type == SSH2_MSG_REQUEST_SUCCESS) {
1442 rfwd->allocated_port = packet_get_int();
1443 logit("Allocated port %u for remote forward to %s:%d",
1444 rfwd->allocated_port,
1445 rfwd->connect_host, rfwd->connect_port);
1446 channel_update_permitted_opens(rfwd->handle,
1447 rfwd->allocated_port);
1448 } else {
1449 channel_update_permitted_opens(rfwd->handle, -1);
1450 }
Damien Miller4bf648f2009-02-14 16:28:21 +11001451 }
1452
Darren Tucker9f407c42008-06-13 04:50:27 +10001453 if (type == SSH2_MSG_REQUEST_FAILURE) {
Damien Miller7acefbb2014-07-18 14:11:24 +10001454 if (options.exit_on_forward_failure) {
1455 if (rfwd->listen_path != NULL)
1456 fatal("Error: remote port forwarding failed "
1457 "for listen path %s", rfwd->listen_path);
1458 else
1459 fatal("Error: remote port forwarding failed "
1460 "for listen port %d", rfwd->listen_port);
1461 } else {
1462 if (rfwd->listen_path != NULL)
1463 logit("Warning: remote port forwarding failed "
1464 "for listen path %s", rfwd->listen_path);
1465 else
1466 logit("Warning: remote port forwarding failed "
1467 "for listen port %d", rfwd->listen_port);
1468 }
Darren Tucker9f407c42008-06-13 04:50:27 +10001469 }
Darren Tucker9a2a6092008-07-04 12:53:50 +10001470 if (++remote_forward_confirms_received == options.num_remote_forwards) {
Darren Tucker9f407c42008-06-13 04:50:27 +10001471 debug("All remote forwarding requests processed");
Damien Millere11e1ea2010-08-03 16:04:46 +10001472 if (fork_after_authentication_flag)
1473 fork_postauth();
Darren Tucker9a2a6092008-07-04 12:53:50 +10001474 }
Darren Tucker9f407c42008-06-13 04:50:27 +10001475}
1476
Ben Lindstrombba81212001-06-25 05:01:22 +00001477static void
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001478client_cleanup_stdio_fwd(int id, void *arg)
1479{
1480 debug("stdio forwarding: done");
1481 cleanup_exit(0);
1482}
1483
Darren Tucker2d6665d2011-11-04 10:54:22 +11001484static void
Damien Miller357610d2014-07-18 15:04:10 +10001485ssh_stdio_confirm(int id, int success, void *arg)
1486{
1487 if (!success)
1488 fatal("stdio forwarding failed");
1489}
1490
1491static void
Darren Tucker2d6665d2011-11-04 10:54:22 +11001492ssh_init_stdio_forwarding(void)
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001493{
1494 Channel *c;
Damien Millere1537f92010-01-26 13:26:22 +11001495 int in, out;
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001496
Darren Tucker2d6665d2011-11-04 10:54:22 +11001497 if (stdio_forward_host == NULL)
1498 return;
Damien Miller386feab2013-10-15 12:14:49 +11001499 if (!compat20)
Darren Tucker2d6665d2011-11-04 10:54:22 +11001500 fatal("stdio forwarding require Protocol 2");
Damien Millere1537f92010-01-26 13:26:22 +11001501
Darren Tucker2d6665d2011-11-04 10:54:22 +11001502 debug3("%s: %s:%d", __func__, stdio_forward_host, stdio_forward_port);
1503
1504 if ((in = dup(STDIN_FILENO)) < 0 ||
1505 (out = dup(STDOUT_FILENO)) < 0)
Damien Millere1537f92010-01-26 13:26:22 +11001506 fatal("channel_connect_stdio_fwd: dup() in/out failed");
Darren Tucker2d6665d2011-11-04 10:54:22 +11001507 if ((c = channel_connect_stdio_fwd(stdio_forward_host,
1508 stdio_forward_port, in, out)) == NULL)
1509 fatal("%s: channel_connect_stdio_fwd failed", __func__);
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001510 channel_register_cleanup(c->self, client_cleanup_stdio_fwd, 0);
Damien Miller357610d2014-07-18 15:04:10 +10001511 channel_register_open_confirm(c->self, ssh_stdio_confirm, NULL);
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001512}
1513
1514static void
Damien Miller0bc1bd82000-11-13 22:57:25 +11001515ssh_init_forwarding(void)
1516{
Kevin Steves12057502001-02-05 14:54:34 +00001517 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001518 int i;
Kevin Steves12057502001-02-05 14:54:34 +00001519
Damien Miller0bc1bd82000-11-13 22:57:25 +11001520 /* Initiate local TCP/IP port forwardings. */
1521 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001522 debug("Local connections to %.200s:%d forwarded to remote "
1523 "address %.200s:%d",
Damien Miller7acefbb2014-07-18 14:11:24 +10001524 (options.local_forwards[i].listen_path != NULL) ?
1525 options.local_forwards[i].listen_path :
Darren Tucker47eede72005-03-14 23:08:12 +11001526 (options.local_forwards[i].listen_host == NULL) ?
Damien Miller7acefbb2014-07-18 14:11:24 +10001527 (options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") :
Damien Millerf91ee4c2005-03-01 21:24:33 +11001528 options.local_forwards[i].listen_host,
1529 options.local_forwards[i].listen_port,
Damien Miller7acefbb2014-07-18 14:11:24 +10001530 (options.local_forwards[i].connect_path != NULL) ?
1531 options.local_forwards[i].connect_path :
Damien Millerf91ee4c2005-03-01 21:24:33 +11001532 options.local_forwards[i].connect_host,
1533 options.local_forwards[i].connect_port);
Damien Millerb16461c2002-01-22 23:29:22 +11001534 success += channel_setup_local_fwd_listener(
Damien Miller7acefbb2014-07-18 14:11:24 +10001535 &options.local_forwards[i], &options.fwd_opts);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001536 }
Darren Tuckere7d4b192006-07-12 22:17:10 +10001537 if (i > 0 && success != i && options.exit_on_forward_failure)
1538 fatal("Could not request local forwarding.");
Kevin Steves12057502001-02-05 14:54:34 +00001539 if (i > 0 && success == 0)
1540 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001541
1542 /* Initiate remote TCP/IP port forwardings. */
1543 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001544 debug("Remote connections from %.200s:%d forwarded to "
1545 "local address %.200s:%d",
Damien Miller7acefbb2014-07-18 14:11:24 +10001546 (options.remote_forwards[i].listen_path != NULL) ?
1547 options.remote_forwards[i].listen_path :
Damien Miller46d38de2005-07-17 17:02:09 +10001548 (options.remote_forwards[i].listen_host == NULL) ?
Damien Milleraa3bb102005-11-05 15:12:59 +11001549 "LOCALHOST" : options.remote_forwards[i].listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +11001550 options.remote_forwards[i].listen_port,
Damien Miller7acefbb2014-07-18 14:11:24 +10001551 (options.remote_forwards[i].connect_path != NULL) ?
1552 options.remote_forwards[i].connect_path :
Damien Millerf91ee4c2005-03-01 21:24:33 +11001553 options.remote_forwards[i].connect_host,
1554 options.remote_forwards[i].connect_port);
Darren Tucker68afb8c2011-10-02 18:59:03 +11001555 options.remote_forwards[i].handle =
1556 channel_request_remote_forwarding(
Damien Miller7acefbb2014-07-18 14:11:24 +10001557 &options.remote_forwards[i]);
Darren Tucker68afb8c2011-10-02 18:59:03 +11001558 if (options.remote_forwards[i].handle < 0) {
Darren Tuckere7d4b192006-07-12 22:17:10 +10001559 if (options.exit_on_forward_failure)
1560 fatal("Could not request remote forwarding.");
1561 else
1562 logit("Warning: Could not request remote "
1563 "forwarding.");
Darren Tucker68afb8c2011-10-02 18:59:03 +11001564 } else {
1565 client_register_global_confirm(ssh_confirm_remote_forward,
1566 &options.remote_forwards[i]);
Darren Tuckere7d4b192006-07-12 22:17:10 +10001567 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001568 }
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001569
1570 /* Initiate tunnel forwarding. */
1571 if (options.tun_open != SSH_TUNMODE_NO) {
1572 if (client_request_tun_fwd(options.tun_open,
1573 options.tun_local, options.tun_remote) == -1) {
1574 if (options.exit_on_forward_failure)
1575 fatal("Could not request tunnel forwarding.");
1576 else
1577 error("Could not request tunnel forwarding.");
1578 }
1579 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001580}
1581
Ben Lindstrombba81212001-06-25 05:01:22 +00001582static void
Damien Miller0bc1bd82000-11-13 22:57:25 +11001583check_agent_present(void)
1584{
djm@openbsd.org141efe42015-01-14 20:05:27 +00001585 int r;
1586
Damien Miller0bc1bd82000-11-13 22:57:25 +11001587 if (options.forward_agent) {
Damien Miller788f2122005-11-05 15:14:59 +11001588 /* Clear agent forwarding if we don't have an agent. */
djm@openbsd.org141efe42015-01-14 20:05:27 +00001589 if ((r = ssh_get_authentication_socket(NULL)) != 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001590 options.forward_agent = 0;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001591 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1592 debug("ssh_get_authentication_socket: %s",
1593 ssh_err(r));
1594 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001595 }
1596}
1597
Ben Lindstrombba81212001-06-25 05:01:22 +00001598static int
Damien Miller1383bd82000-04-06 12:32:37 +10001599ssh_session(void)
1600{
1601 int type;
Damien Miller1383bd82000-04-06 12:32:37 +10001602 int interactive = 0;
1603 int have_tty = 0;
1604 struct winsize ws;
Damien Miller1383bd82000-04-06 12:32:37 +10001605 char *cp;
Damien Miller17e7ed02005-06-17 12:54:33 +10001606 const char *display;
Damien Miller1383bd82000-04-06 12:32:37 +10001607
Damien Miller95def091999-11-25 00:26:21 +11001608 /* Enable compression if requested. */
1609 if (options.compression) {
Darren Tuckerd6173c02008-06-13 04:52:53 +10001610 debug("Requesting compression at level %d.",
1611 options.compression_level);
Damien Miller95def091999-11-25 00:26:21 +11001612
Darren Tuckerd6173c02008-06-13 04:52:53 +10001613 if (options.compression_level < 1 ||
1614 options.compression_level > 9)
1615 fatal("Compression level must be from 1 (fast) to "
1616 "9 (slow, best).");
Damien Miller95def091999-11-25 00:26:21 +11001617
1618 /* Send the request. */
1619 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
1620 packet_put_int(options.compression_level);
1621 packet_send();
1622 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +11001623 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +11001624 if (type == SSH_SMSG_SUCCESS)
1625 packet_start_compression(options.compression_level);
1626 else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +10001627 logit("Warning: Remote host refused compression.");
Damien Miller95def091999-11-25 00:26:21 +11001628 else
Darren Tuckerd6173c02008-06-13 04:52:53 +10001629 packet_disconnect("Protocol error waiting for "
1630 "compression response.");
Damien Miller95def091999-11-25 00:26:21 +11001631 }
1632 /* Allocate a pseudo tty if appropriate. */
1633 if (tty_flag) {
1634 debug("Requesting pty.");
1635
1636 /* Start the packet. */
1637 packet_start(SSH_CMSG_REQUEST_PTY);
1638
1639 /* Store TERM in the packet. There is no limit on the
1640 length of the string. */
1641 cp = getenv("TERM");
1642 if (!cp)
1643 cp = "";
Ben Lindstrom664408d2001-06-09 01:42:01 +00001644 packet_put_cstring(cp);
Damien Miller95def091999-11-25 00:26:21 +11001645
1646 /* Store window size in the packet. */
1647 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
1648 memset(&ws, 0, sizeof(ws));
Damien Miller71a73672006-03-26 14:04:36 +11001649 packet_put_int((u_int)ws.ws_row);
1650 packet_put_int((u_int)ws.ws_col);
1651 packet_put_int((u_int)ws.ws_xpixel);
1652 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller95def091999-11-25 00:26:21 +11001653
1654 /* Store tty modes in the packet. */
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001655 tty_make_modes(fileno(stdin), NULL);
Damien Miller95def091999-11-25 00:26:21 +11001656
1657 /* Send the packet, and wait for it to leave. */
1658 packet_send();
1659 packet_write_wait();
1660
1661 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +11001662 type = packet_read();
Damien Miller450a7a12000-03-26 13:04:51 +10001663 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +11001664 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001665 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +10001666 } else if (type == SSH_SMSG_FAILURE)
Darren Tuckerd6173c02008-06-13 04:52:53 +10001667 logit("Warning: Remote host failed or refused to "
1668 "allocate a pseudo tty.");
Damien Miller95def091999-11-25 00:26:21 +11001669 else
Darren Tuckerd6173c02008-06-13 04:52:53 +10001670 packet_disconnect("Protocol error waiting for pty "
1671 "request response.");
Damien Miller95def091999-11-25 00:26:21 +11001672 }
1673 /* Request X11 forwarding if enabled and DISPLAY is set. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001674 display = getenv("DISPLAY");
djm@openbsd.orga58be332015-04-17 13:16:48 +00001675 if (display == NULL && options.forward_x11)
1676 debug("X11 forwarding requested but DISPLAY not set");
Damien Miller17e7ed02005-06-17 12:54:33 +10001677 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001678 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001679 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001680 client_x11_get_proto(display, options.xauth_location,
Damien Miller386feab2013-10-15 12:14:49 +11001681 options.forward_x11_trusted,
Damien Miller1ab6a512010-06-26 10:02:24 +10001682 options.forward_x11_timeout,
1683 &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001684 /* Request forwarding with authentication spoofing. */
Darren Tuckerd6173c02008-06-13 04:52:53 +10001685 debug("Requesting X11 forwarding with authentication "
1686 "spoofing.");
Damien Miller6d7b4372011-06-23 08:31:57 +10001687 x11_request_forwarding_with_spoofing(0, display, proto,
1688 data, 0);
Damien Miller95def091999-11-25 00:26:21 +11001689 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +11001690 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +11001691 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +11001692 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001693 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller996acd22003-04-09 20:59:48 +10001694 logit("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +10001695 } else {
Darren Tuckerd6173c02008-06-13 04:52:53 +10001696 packet_disconnect("Protocol error waiting for X11 "
1697 "forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +10001698 }
Damien Miller95def091999-11-25 00:26:21 +11001699 }
1700 /* Tell the packet module whether this is an interactive session. */
Damien Miller0dac6fb2010-11-20 15:19:38 +11001701 packet_set_interactive(interactive,
1702 options.ip_qos_interactive, options.ip_qos_bulk);
Damien Miller95def091999-11-25 00:26:21 +11001703
1704 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001705 check_agent_present();
1706
Damien Miller95def091999-11-25 00:26:21 +11001707 if (options.forward_agent) {
1708 debug("Requesting authentication agent forwarding.");
1709 auth_request_forwarding();
1710
1711 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +11001712 type = packet_read();
Damien Miller48b03fc2002-01-22 23:11:40 +11001713 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +11001714 if (type != SSH_SMSG_SUCCESS)
Damien Miller996acd22003-04-09 20:59:48 +10001715 logit("Warning: Remote host denied authentication agent forwarding.");
Damien Miller95def091999-11-25 00:26:21 +11001716 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001717
Damien Miller0bc1bd82000-11-13 22:57:25 +11001718 /* Initiate port forwardings. */
Darren Tucker2d6665d2011-11-04 10:54:22 +11001719 ssh_init_stdio_forwarding();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001720 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001721
Darren Tuckerb776c852007-12-02 23:06:35 +11001722 /* Execute a local command */
1723 if (options.local_command != NULL &&
1724 options.permit_local_command)
1725 ssh_local_cmd(options.local_command);
1726
Darren Tucker9a2a6092008-07-04 12:53:50 +10001727 /*
1728 * If requested and we are not interested in replies to remote
1729 * forwarding requests, then let ssh continue in the background.
1730 */
Damien Millere11e1ea2010-08-03 16:04:46 +10001731 if (fork_after_authentication_flag) {
1732 if (options.exit_on_forward_failure &&
1733 options.num_remote_forwards > 0) {
1734 debug("deferring postauth fork until remote forward "
1735 "confirmation received");
1736 } else
1737 fork_postauth();
Darren Tucker9a2a6092008-07-04 12:53:50 +10001738 }
Damien Miller5428f641999-11-25 11:54:57 +11001739
1740 /*
1741 * If a command was specified on the command line, execute the
1742 * command now. Otherwise request the server to start a shell.
1743 */
Damien Miller95def091999-11-25 00:26:21 +11001744 if (buffer_len(&command) > 0) {
1745 int len = buffer_len(&command);
1746 if (len > 900)
1747 len = 900;
Darren Tuckerd6173c02008-06-13 04:52:53 +10001748 debug("Sending command: %.*s", len,
1749 (u_char *)buffer_ptr(&command));
Damien Miller95def091999-11-25 00:26:21 +11001750 packet_start(SSH_CMSG_EXEC_CMD);
1751 packet_put_string(buffer_ptr(&command), buffer_len(&command));
1752 packet_send();
1753 packet_write_wait();
1754 } else {
1755 debug("Requesting shell.");
1756 packet_start(SSH_CMSG_EXEC_SHELL);
1757 packet_send();
1758 packet_write_wait();
1759 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001760
Damien Miller95def091999-11-25 00:26:21 +11001761 /* Enter the interactive session. */
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001762 return client_loop(have_tty, tty_flag ?
1763 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
Damien Miller1383bd82000-04-06 12:32:37 +10001764}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001765
Ben Lindstromf558cf62001-09-20 23:13:49 +00001766/* request pty/x11/agent/tcpfwd/shell for channel */
Ben Lindstrombba81212001-06-25 05:01:22 +00001767static void
Damien Millerd530f5f2010-05-21 14:57:10 +10001768ssh_session2_setup(int id, int success, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +10001769{
Damien Miller3756dce2004-06-18 01:17:29 +10001770 extern char **environ;
Damien Miller17e7ed02005-06-17 12:54:33 +10001771 const char *display;
Damien Miller0e220db2004-06-15 10:34:08 +10001772 int interactive = tty_flag;
Damien Miller17e7ed02005-06-17 12:54:33 +10001773
Damien Millerd530f5f2010-05-21 14:57:10 +10001774 if (!success)
1775 return; /* No need for error message, channels code sens one */
1776
Damien Miller46d38de2005-07-17 17:02:09 +10001777 display = getenv("DISPLAY");
djm@openbsd.orga58be332015-04-17 13:16:48 +00001778 if (display == NULL && options.forward_x11)
1779 debug("X11 forwarding requested but DISPLAY not set");
Damien Miller17e7ed02005-06-17 12:54:33 +10001780 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001781 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001782 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001783 client_x11_get_proto(display, options.xauth_location,
Damien Miller1ab6a512010-06-26 10:02:24 +10001784 options.forward_x11_trusted,
1785 options.forward_x11_timeout, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001786 /* Request forwarding with authentication spoofing. */
Darren Tuckerd6173c02008-06-13 04:52:53 +10001787 debug("Requesting X11 forwarding with authentication "
1788 "spoofing.");
Damien Miller6d7b4372011-06-23 08:31:57 +10001789 x11_request_forwarding_with_spoofing(id, display, proto,
1790 data, 1);
1791 client_expect_confirm(id, "X11 forwarding", CONFIRM_WARN);
1792 /* XXX exit_on_forward_failure */
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001793 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001794 }
1795
Damien Miller0bc1bd82000-11-13 22:57:25 +11001796 check_agent_present();
1797 if (options.forward_agent) {
1798 debug("Requesting authentication agent forwarding.");
1799 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1800 packet_send();
1801 }
1802
Darren Tucker7b305012012-07-02 18:55:09 +10001803 /* Tell the packet module whether this is an interactive session. */
1804 packet_set_interactive(interactive,
1805 options.ip_qos_interactive, options.ip_qos_bulk);
1806
Damien Miller0e220db2004-06-15 10:34:08 +10001807 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
Damien Miller5771ed72008-05-19 15:35:33 +10001808 NULL, fileno(stdin), &command, environ);
Damien Miller1383bd82000-04-06 12:32:37 +10001809}
1810
Ben Lindstromf558cf62001-09-20 23:13:49 +00001811/* open new channel for a session */
Ben Lindstrombba81212001-06-25 05:01:22 +00001812static int
Ben Lindstromf558cf62001-09-20 23:13:49 +00001813ssh_session2_open(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001814{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001815 Channel *c;
1816 int window, packetmax, in, out, err;
Damien Millerad833b32000-08-23 10:46:23 +10001817
Damien Millercaf6dd62000-08-29 11:33:50 +11001818 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001819 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001820 } else {
1821 in = dup(STDIN_FILENO);
1822 }
1823 out = dup(STDOUT_FILENO);
1824 err = dup(STDERR_FILENO);
1825
1826 if (in < 0 || out < 0 || err < 0)
1827 fatal("dup() in/out/err failed");
1828
Damien Miller69b69aa2000-10-28 14:19:58 +11001829 /* enable nonblocking unless tty */
1830 if (!isatty(in))
1831 set_nonblock(in);
1832 if (!isatty(out))
1833 set_nonblock(out);
1834 if (!isatty(err))
1835 set_nonblock(err);
1836
Damien Millere4340be2000-09-16 13:29:08 +11001837 window = CHAN_SES_WINDOW_DEFAULT;
1838 packetmax = CHAN_SES_PACKET_DEFAULT;
Damien Miller19a59452002-02-19 15:20:57 +11001839 if (tty_flag) {
1840 window >>= 1;
1841 packetmax >>= 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001842 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001843 c = channel_new(
Damien Miller1383bd82000-04-06 12:32:37 +10001844 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001845 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001846 "client-session", /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001847
Ben Lindstromf558cf62001-09-20 23:13:49 +00001848 debug3("ssh_session2_open: channel_new: %d", c->self);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001849
Ben Lindstrom5ec26452001-06-09 00:18:51 +00001850 channel_send_open(c->self);
Ben Lindstromf558cf62001-09-20 23:13:49 +00001851 if (!no_shell_flag)
Damien Millerb84886b2008-05-19 15:05:07 +10001852 channel_register_open_confirm(c->self,
1853 ssh_session2_setup, NULL);
Damien Miller1383bd82000-04-06 12:32:37 +10001854
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001855 return c->self;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001856}
1857
Ben Lindstrombba81212001-06-25 05:01:22 +00001858static int
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001859ssh_session2(void)
1860{
Ben Lindstromf558cf62001-09-20 23:13:49 +00001861 int id = -1;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001862
1863 /* XXX should be pre-session */
Darren Tucker2d6665d2011-11-04 10:54:22 +11001864 if (!options.control_persist)
1865 ssh_init_stdio_forwarding();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001866 ssh_init_forwarding();
1867
Damien Millere11e1ea2010-08-03 16:04:46 +10001868 /* Start listening for multiplex clients */
1869 muxserver_listen();
1870
1871 /*
Darren Tucker2d6665d2011-11-04 10:54:22 +11001872 * If we are in control persist mode and have a working mux listen
1873 * socket, then prepare to background ourselves and have a foreground
1874 * client attach as a control slave.
1875 * NB. we must save copies of the flags that we override for
Damien Millere11e1ea2010-08-03 16:04:46 +10001876 * the backgrounding, since we defer attachment of the slave until
1877 * after the connection is fully established (in particular,
1878 * async rfwd replies have been received for ExitOnForwardFailure).
1879 */
1880 if (options.control_persist && muxserver_sock != -1) {
1881 ostdin_null_flag = stdin_null_flag;
1882 ono_shell_flag = no_shell_flag;
Damien Miller21771e22011-05-15 08:45:50 +10001883 orequest_tty = options.request_tty;
Damien Millere11e1ea2010-08-03 16:04:46 +10001884 otty_flag = tty_flag;
1885 stdin_null_flag = 1;
1886 no_shell_flag = 1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001887 tty_flag = 0;
1888 if (!fork_after_authentication_flag)
1889 need_controlpersist_detach = 1;
1890 fork_after_authentication_flag = 1;
1891 }
Darren Tucker2d6665d2011-11-04 10:54:22 +11001892 /*
1893 * ControlPersist mux listen socket setup failed, attempt the
1894 * stdio forward setup that we skipped earlier.
1895 */
1896 if (options.control_persist && muxserver_sock == -1)
1897 ssh_init_stdio_forwarding();
Damien Millere11e1ea2010-08-03 16:04:46 +10001898
Ben Lindstromf558cf62001-09-20 23:13:49 +00001899 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1900 id = ssh_session2_open();
Damien Miller649fe022013-07-18 16:13:55 +10001901 else {
1902 packet_set_interactive(
1903 options.control_master == SSHCTL_MASTER_NO,
1904 options.ip_qos_interactive, options.ip_qos_bulk);
1905 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001906
Darren Tucker8901fa92008-06-11 09:34:01 +10001907 /* If we don't expect to open a new session, then disallow it */
Damien Miller456e6f02008-11-03 19:20:10 +11001908 if (options.control_master == SSHCTL_MASTER_NO &&
1909 (datafellows & SSH_NEW_OPENSSH)) {
Darren Tucker8901fa92008-06-11 09:34:01 +10001910 debug("Requesting no-more-sessions@openssh.com");
1911 packet_start(SSH2_MSG_GLOBAL_REQUEST);
1912 packet_put_cstring("no-more-sessions@openssh.com");
1913 packet_put_char(0);
1914 packet_send();
1915 }
1916
Damien Millerd27b9472005-12-13 19:29:02 +11001917 /* Execute a local command */
1918 if (options.local_command != NULL &&
1919 options.permit_local_command)
1920 ssh_local_cmd(options.local_command);
1921
Damien Miller1f25ab42010-07-16 13:56:23 +10001922 /*
1923 * If requested and we are not interested in replies to remote
1924 * forwarding requests, then let ssh continue in the background.
1925 */
Damien Millere11e1ea2010-08-03 16:04:46 +10001926 if (fork_after_authentication_flag) {
1927 if (options.exit_on_forward_failure &&
1928 options.num_remote_forwards > 0) {
1929 debug("deferring postauth fork until remote forward "
1930 "confirmation received");
1931 } else
1932 fork_postauth();
Darren Tucker9a2a6092008-07-04 12:53:50 +10001933 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001934
Darren Tuckerf1de4e52010-01-08 16:54:59 +11001935 if (options.use_roaming)
1936 request_roaming();
1937
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001938 return client_loop(tty_flag, tty_flag ?
1939 options.escape_char : SSH_ESCAPECHAR_NONE, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001940}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001941
Ben Lindstrombba81212001-06-25 05:01:22 +00001942static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001943load_public_identity_files(void)
1944{
Damien Miller6b1d53c2006-03-31 23:13:21 +11001945 char *filename, *cp, thishost[NI_MAXHOST];
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001946 char *pwdir = NULL, *pwname = NULL;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001947 int i = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001948 Key *public;
Damien Miller6b1d53c2006-03-31 23:13:21 +11001949 struct passwd *pw;
Damien Miller0a80ca12010-02-27 07:55:05 +11001950 u_int n_ids;
1951 char *identity_files[SSH_MAX_IDENTITY_FILES];
1952 Key *identity_keys[SSH_MAX_IDENTITY_FILES];
Damien Miller7ea845e2010-02-12 09:21:02 +11001953#ifdef ENABLE_PKCS11
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001954 Key **keys;
Damien Miller7ea845e2010-02-12 09:21:02 +11001955 int nkeys;
Damien Miller0a80ca12010-02-27 07:55:05 +11001956#endif /* PKCS11 */
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001957
Damien Miller0a80ca12010-02-27 07:55:05 +11001958 n_ids = 0;
Damien Miller1d2c4562014-02-04 11:18:20 +11001959 memset(identity_files, 0, sizeof(identity_files));
1960 memset(identity_keys, 0, sizeof(identity_keys));
Damien Miller0a80ca12010-02-27 07:55:05 +11001961
1962#ifdef ENABLE_PKCS11
Damien Miller7ea845e2010-02-12 09:21:02 +11001963 if (options.pkcs11_provider != NULL &&
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001964 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
Damien Miller7ea845e2010-02-12 09:21:02 +11001965 (pkcs11_init(!options.batch_mode) == 0) &&
1966 (nkeys = pkcs11_add_provider(options.pkcs11_provider, NULL,
1967 &keys)) > 0) {
Damien Miller7ea845e2010-02-12 09:21:02 +11001968 for (i = 0; i < nkeys; i++) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001969 if (n_ids >= SSH_MAX_IDENTITY_FILES) {
1970 key_free(keys[i]);
1971 continue;
1972 }
1973 identity_keys[n_ids] = keys[i];
1974 identity_files[n_ids] =
Damien Miller7ea845e2010-02-12 09:21:02 +11001975 xstrdup(options.pkcs11_provider); /* XXX */
Damien Miller0a80ca12010-02-27 07:55:05 +11001976 n_ids++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001977 }
Darren Tuckera627d422013-06-02 07:31:17 +10001978 free(keys);
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001979 }
Damien Miller7ea845e2010-02-12 09:21:02 +11001980#endif /* ENABLE_PKCS11 */
Damien Miller6b1d53c2006-03-31 23:13:21 +11001981 if ((pw = getpwuid(original_real_uid)) == NULL)
1982 fatal("load_public_identity_files: getpwuid failed");
Darren Tuckere143f062007-12-02 23:21:16 +11001983 pwname = xstrdup(pw->pw_name);
1984 pwdir = xstrdup(pw->pw_dir);
Damien Miller6b1d53c2006-03-31 23:13:21 +11001985 if (gethostname(thishost, sizeof(thishost)) == -1)
1986 fatal("load_public_identity_files: gethostname: %s",
1987 strerror(errno));
Damien Miller0a80ca12010-02-27 07:55:05 +11001988 for (i = 0; i < options.num_identity_files; i++) {
Darren Tucker15fd19c2013-04-05 11:22:26 +11001989 if (n_ids >= SSH_MAX_IDENTITY_FILES ||
1990 strcasecmp(options.identity_files[i], "none") == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +10001991 free(options.identity_files[i]);
Damien Miller0a80ca12010-02-27 07:55:05 +11001992 continue;
1993 }
Damien Miller6b1d53c2006-03-31 23:13:21 +11001994 cp = tilde_expand_filename(options.identity_files[i],
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001995 original_real_uid);
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001996 filename = percent_expand(cp, "d", pwdir,
1997 "u", pwname, "l", thishost, "h", host,
Damien Miller6b1d53c2006-03-31 23:13:21 +11001998 "r", options.user, (char *)NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001999 free(cp);
Ben Lindstromd0fca422001-03-26 13:44:06 +00002000 public = key_load_public(filename, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00002001 debug("identity file %s type %d", filename,
2002 public ? public->type : -1);
Darren Tuckera627d422013-06-02 07:31:17 +10002003 free(options.identity_files[i]);
Damien Miller0a80ca12010-02-27 07:55:05 +11002004 identity_files[n_ids] = filename;
2005 identity_keys[n_ids] = public;
2006
2007 if (++n_ids >= SSH_MAX_IDENTITY_FILES)
2008 continue;
2009
2010 /* Try to add the certificate variant too */
2011 xasprintf(&cp, "%s-cert", filename);
2012 public = key_load_public(cp, NULL);
2013 debug("identity file %s type %d", cp,
2014 public ? public->type : -1);
2015 if (public == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +10002016 free(cp);
Damien Miller0a80ca12010-02-27 07:55:05 +11002017 continue;
2018 }
2019 if (!key_is_cert(public)) {
2020 debug("%s: key %s type %s is not a certificate",
2021 __func__, cp, key_type(public));
2022 key_free(public);
Darren Tuckera627d422013-06-02 07:31:17 +10002023 free(cp);
Damien Miller0a80ca12010-02-27 07:55:05 +11002024 continue;
2025 }
2026 identity_keys[n_ids] = public;
2027 /* point to the original path, most likely the private key */
2028 identity_files[n_ids] = xstrdup(filename);
2029 n_ids++;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00002030 }
Damien Miller0a80ca12010-02-27 07:55:05 +11002031 options.num_identity_files = n_ids;
2032 memcpy(options.identity_files, identity_files, sizeof(identity_files));
2033 memcpy(options.identity_keys, identity_keys, sizeof(identity_keys));
2034
Damien Miller1d2c4562014-02-04 11:18:20 +11002035 explicit_bzero(pwname, strlen(pwname));
Darren Tuckera627d422013-06-02 07:31:17 +10002036 free(pwname);
Damien Miller1d2c4562014-02-04 11:18:20 +11002037 explicit_bzero(pwdir, strlen(pwdir));
Darren Tuckera627d422013-06-02 07:31:17 +10002038 free(pwdir);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00002039}
Damien Miller857b02e2010-09-24 22:02:56 +10002040
2041static void
2042main_sigchld_handler(int sig)
2043{
2044 int save_errno = errno;
2045 pid_t pid;
2046 int status;
2047
2048 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
2049 (pid < 0 && errno == EINTR))
2050 ;
2051
2052 signal(sig, main_sigchld_handler);
2053 errno = save_errno;
2054}