blob: 55a496cb1fe615bc324f6db9e781de15492a0780 [file] [log] [blame]
djm@openbsd.org674b3b62015-09-11 03:47:28 +00001/* $OpenBSD: ssh.c,v 1.423 2015/09/11 03:47:28 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];
djm@openbsd.org674b3b62015-09-11 03:47:28 +0000509 char cname[NI_MAXHOST], uidstr[32], *conn_hash_hex;
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];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000519
Darren Tuckerce321d82005-10-03 18:11:24 +1000520 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
521 sanitise_stdfd();
522
Damien Miller59d3d5b2003-08-22 09:34:41 +1000523 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000524
Damien Millerea2c1a42011-06-03 12:10:22 +1000525#ifndef HAVE_SETPROCTITLE
526 /* Prepare for later setproctitle emulation */
527 /* Save argv so it isn't clobbered by setproctitle() emulation */
528 saved_av = xcalloc(ac + 1, sizeof(*saved_av));
529 for (i = 0; i < ac; i++)
530 saved_av[i] = xstrdup(av[i]);
531 saved_av[i] = NULL;
532 compat_init_setproctitle(ac, av);
533 av = saved_av;
534#endif
535
Damien Miller5428f641999-11-25 11:54:57 +1100536 /*
Damien Miller00d9ae22010-08-17 01:59:31 +1000537 * Discard other fds that are hanging around. These can cause problem
538 * with backgrounded ssh processes started by ControlPersist.
539 */
540 closefrom(STDERR_FILENO + 1);
541
542 /*
Damien Miller5428f641999-11-25 11:54:57 +1100543 * Save the original real uid. It will be needed later (uid-swapping
544 * may clobber the real uid).
545 */
Damien Miller95def091999-11-25 00:26:21 +1100546 original_real_uid = getuid();
547 original_effective_uid = geteuid();
Damien Millera8e06ce2003-11-21 23:48:55 +1100548
Damien Miller50b9a602002-09-04 16:50:06 +1000549 /*
550 * Use uid-swapping to give up root privileges for the duration of
551 * option processing. We will re-instantiate the rights when we are
552 * ready to create the privileged port, and will permanently drop
553 * them when the port has been created (actually, when the connection
554 * has been made, as we may need to create the port several times).
555 */
556 PRIV_END;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000557
Damien Miller05dd7952000-10-01 00:42:48 +1100558#ifdef HAVE_SETRLIMIT
Damien Miller95def091999-11-25 00:26:21 +1100559 /* If we are installed setuid root be careful to not drop core. */
560 if (original_real_uid != original_effective_uid) {
561 struct rlimit rlim;
562 rlim.rlim_cur = rlim.rlim_max = 0;
563 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
564 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000565 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100566#endif
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000567 /* Get user data. */
568 pw = getpwuid(original_real_uid);
569 if (!pw) {
Damien Miller3009d3c2013-07-20 13:22:31 +1000570 logit("No user exists for uid %lu", (u_long)original_real_uid);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100571 exit(255);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000572 }
573 /* Take a copy of the returned structure. */
574 pw = pwcopy(pw);
575
Damien Miller5428f641999-11-25 11:54:57 +1100576 /*
Damien Miller5428f641999-11-25 11:54:57 +1100577 * Set our umask to something reasonable, as some files are created
578 * with the default umask. This will make them world-readable but
579 * writable only by the owner, which is ok for all files for which we
580 * don't set the modes explicitly.
581 */
Damien Miller95def091999-11-25 00:26:21 +1100582 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000583
Darren Tuckerd6173c02008-06-13 04:52:53 +1000584 /*
585 * Initialize option structure to indicate that no values have been
586 * set.
587 */
Damien Miller95def091999-11-25 00:26:21 +1100588 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000589
Damien Miller95def091999-11-25 00:26:21 +1100590 /* Parse command-line arguments. */
591 host = NULL;
Damien Millere272a5b2008-11-03 19:22:37 +1100592 use_syslog = 0;
Damien Miller03d4d7e2013-04-23 15:21:06 +1000593 logfile = NULL;
Darren Tucker72efd742009-06-21 17:48:00 +1000594 argv0 = av[0];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000595
Damien Miller2ecb6bd2006-03-15 12:03:53 +1100596 again:
Darren Tuckerd6173c02008-06-13 04:52:53 +1000597 while ((opt = getopt(ac, av, "1246ab:c:e:fgi:kl:m:no:p:qstvx"
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000598 "ACD:E:F:GI:KL:MNO:PQ:R:S:TVw:W:XYy")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100599 switch (opt) {
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000600 case '1':
601 options.protocol = SSH_PROTO_1;
602 break;
Damien Miller4af51302000-04-16 11:18:38 +1000603 case '2':
604 options.protocol = SSH_PROTO_2;
605 break;
Damien Miller34132e52000-01-14 15:45:46 +1100606 case '4':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000607 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100608 break;
Damien Miller34132e52000-01-14 15:45:46 +1100609 case '6':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000610 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100611 break;
Damien Miller95def091999-11-25 00:26:21 +1100612 case 'n':
613 stdin_null_flag = 1;
614 break;
Damien Miller95def091999-11-25 00:26:21 +1100615 case 'f':
616 fork_after_authentication_flag = 1;
617 stdin_null_flag = 1;
618 break;
Damien Miller95def091999-11-25 00:26:21 +1100619 case 'x':
620 options.forward_x11 = 0;
621 break;
Damien Miller95def091999-11-25 00:26:21 +1100622 case 'X':
623 options.forward_x11 = 1;
624 break;
Damien Millere272a5b2008-11-03 19:22:37 +1100625 case 'y':
626 use_syslog = 1;
627 break;
Damien Miller03d4d7e2013-04-23 15:21:06 +1000628 case 'E':
dtucker@openbsd.org4f7cc2f2015-09-04 08:21:47 +0000629 logfile = optarg;
Damien Miller03d4d7e2013-04-23 15:21:06 +1000630 break;
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000631 case 'G':
632 config_test = 1;
633 break;
Darren Tucker0a118da2003-10-15 15:54:32 +1000634 case 'Y':
635 options.forward_x11 = 1;
636 options.forward_x11_trusted = 1;
637 break;
Damien Miller95def091999-11-25 00:26:21 +1100638 case 'g':
Damien Miller7acefbb2014-07-18 14:11:24 +1000639 options.fwd_opts.gateway_ports = 1;
Damien Miller95def091999-11-25 00:26:21 +1100640 break;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100641 case 'O':
Damien Millere1537f92010-01-26 13:26:22 +1100642 if (stdio_forward_host != NULL)
643 fatal("Cannot specify multiplexing "
644 "command with -W");
645 else if (muxclient_command != 0)
646 fatal("Multiplexing command already specified");
Darren Tucker7ebfc102004-11-07 20:06:19 +1100647 if (strcmp(optarg, "check") == 0)
Damien Millerb1cbfa22008-05-19 16:00:08 +1000648 muxclient_command = SSHMUX_COMMAND_ALIVE_CHECK;
Damien Miller388f6fc2010-05-21 14:57:35 +1000649 else if (strcmp(optarg, "forward") == 0)
650 muxclient_command = SSHMUX_COMMAND_FORWARD;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100651 else if (strcmp(optarg, "exit") == 0)
Damien Millerb1cbfa22008-05-19 16:00:08 +1000652 muxclient_command = SSHMUX_COMMAND_TERMINATE;
Damien Miller6c3eec72011-05-05 14:16:22 +1000653 else if (strcmp(optarg, "stop") == 0)
654 muxclient_command = SSHMUX_COMMAND_STOP;
Damien Millerf6dff7c2011-09-22 21:38:52 +1000655 else if (strcmp(optarg, "cancel") == 0)
656 muxclient_command = SSHMUX_COMMAND_CANCEL_FWD;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100657 else
658 fatal("Invalid multiplex command.");
659 break;
Damien Miller147bba32002-09-04 16:46:06 +1000660 case 'P': /* deprecated */
Damien Miller95def091999-11-25 00:26:21 +1100661 options.use_privileged_port = 0;
662 break;
Damien Millerd937dc02013-12-05 10:19:54 +1100663 case 'Q':
Damien Millerea111192013-04-23 19:24:32 +1000664 cp = NULL;
Damien Millerd937dc02013-12-05 10:19:54 +1100665 if (strcmp(optarg, "cipher") == 0)
Damien Miller0fde8ac2013-11-21 14:12:23 +1100666 cp = cipher_alg_list('\n', 0);
Damien Millerd937dc02013-12-05 10:19:54 +1100667 else if (strcmp(optarg, "cipher-auth") == 0)
Damien Miller0fde8ac2013-11-21 14:12:23 +1100668 cp = cipher_alg_list('\n', 1);
Damien Millerd937dc02013-12-05 10:19:54 +1100669 else if (strcmp(optarg, "mac") == 0)
Damien Miller690d9892013-11-08 12:16:49 +1100670 cp = mac_alg_list('\n');
Damien Millerd937dc02013-12-05 10:19:54 +1100671 else if (strcmp(optarg, "kex") == 0)
Damien Miller690d9892013-11-08 12:16:49 +1100672 cp = kex_alg_list('\n');
Damien Millerd937dc02013-12-05 10:19:54 +1100673 else if (strcmp(optarg, "key") == 0)
Damien Miller5be9d9e2013-12-07 11:24:01 +1100674 cp = key_alg_list(0, 0);
675 else if (strcmp(optarg, "key-cert") == 0)
676 cp = key_alg_list(1, 0);
677 else if (strcmp(optarg, "key-plain") == 0)
678 cp = key_alg_list(0, 1);
djm@openbsd.org68d2dfc2015-03-03 06:48:58 +0000679 else if (strcmp(optarg, "protocol-version") == 0) {
680#ifdef WITH_SSH1
681 cp = xstrdup("1\n2");
682#else
683 cp = xstrdup("2");
684#endif
685 }
Damien Millerea111192013-04-23 19:24:32 +1000686 if (cp == NULL)
687 fatal("Unsupported query \"%s\"", optarg);
688 printf("%s\n", cp);
689 free(cp);
690 exit(0);
691 break;
Damien Miller95def091999-11-25 00:26:21 +1100692 case 'a':
693 options.forward_agent = 0;
694 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000695 case 'A':
696 options.forward_agent = 1;
697 break;
Damien Miller95def091999-11-25 00:26:21 +1100698 case 'k':
Damien Millere0113cc2003-11-24 13:10:09 +1100699 options.gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100700 break;
Darren Tucker415bddc2007-06-12 23:43:16 +1000701 case 'K':
702 options.gss_authentication = 1;
703 options.gss_deleg_creds = 1;
704 break;
Damien Miller95def091999-11-25 00:26:21 +1100705 case 'i':
706 if (stat(optarg, &st) < 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000707 fprintf(stderr, "Warning: Identity file %s "
Damien Miller3eb48b62005-03-01 21:15:46 +1100708 "not accessible: %s.\n", optarg,
709 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100710 break;
711 }
Darren Tucker19104782013-04-05 11:13:08 +1100712 add_identity_file(&options, NULL, optarg, 1);
Damien Miller95def091999-11-25 00:26:21 +1100713 break;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000714 case 'I':
Damien Miller7ea845e2010-02-12 09:21:02 +1100715#ifdef ENABLE_PKCS11
dtucker@openbsd.org4f7cc2f2015-09-04 08:21:47 +0000716 free(options.pkcs11_provider);
Damien Miller7ea845e2010-02-12 09:21:02 +1100717 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 */
dtucker@openbsd.org4f7cc2f2015-09-04 08:21:47 +0000801 free(options.ciphers);
Damien Millereba71ba2000-04-29 23:57:08 +1000802 options.ciphers = xstrdup(optarg);
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000803 options.cipher = SSH_CIPHER_INVALID;
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000804 break;
Damien Miller95def091999-11-25 00:26:21 +1100805 }
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000806 /* SSH1 only */
807 options.cipher = cipher_number(optarg);
808 if (options.cipher == -1) {
809 fprintf(stderr, "Unknown cipher type '%s'\n",
810 optarg);
811 exit(255);
812 }
813 if (options.cipher == SSH_CIPHER_3DES)
814 options.ciphers = xstrdup("3des-cbc");
815 else if (options.cipher == SSH_CIPHER_BLOWFISH)
816 options.ciphers = xstrdup("blowfish-cbc");
817 else
818 options.ciphers = xstrdup(KEX_CLIENT_ENCRYPT);
Damien Miller95def091999-11-25 00:26:21 +1100819 break;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000820 case 'm':
dtucker@openbsd.org4f7cc2f2015-09-04 08:21:47 +0000821 if (mac_valid(optarg)) {
822 free(options.macs);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000823 options.macs = xstrdup(optarg);
dtucker@openbsd.org4f7cc2f2015-09-04 08:21:47 +0000824 } else {
Damien Millerf4614452001-07-14 12:18:10 +1000825 fprintf(stderr, "Unknown mac type '%s'\n",
826 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100827 exit(255);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000828 }
829 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000830 case 'M':
Damien Millerd14b1e72005-06-16 13:19:41 +1000831 if (options.control_master == SSHCTL_MASTER_YES)
832 options.control_master = SSHCTL_MASTER_ASK;
833 else
834 options.control_master = SSHCTL_MASTER_YES;
Damien Miller0e220db2004-06-15 10:34:08 +1000835 break;
Damien Miller95def091999-11-25 00:26:21 +1100836 case 'p':
Ben Lindstrom19066a12001-04-12 23:39:26 +0000837 options.port = a2port(optarg);
Damien Miller3dc71ad2009-01-28 16:31:22 +1100838 if (options.port <= 0) {
Ben Lindstrom146edb92001-04-11 23:06:28 +0000839 fprintf(stderr, "Bad port '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100840 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000841 }
Damien Miller95def091999-11-25 00:26:21 +1100842 break;
Damien Miller95def091999-11-25 00:26:21 +1100843 case 'l':
844 options.user = optarg;
845 break;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000846
Damien Miller95def091999-11-25 00:26:21 +1100847 case 'L':
Damien Miller4bf648f2009-02-14 16:28:21 +1100848 if (parse_forward(&fwd, optarg, 0, 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +1100849 add_local_forward(&options, &fwd);
850 else {
Damien Millerf4614452001-07-14 12:18:10 +1000851 fprintf(stderr,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100852 "Bad local forwarding specification '%s'\n",
Damien Millerf4614452001-07-14 12:18:10 +1000853 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100854 exit(255);
Ben Lindstrom1a174712001-09-12 17:56:15 +0000855 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100856 break;
857
858 case 'R':
Damien Miller4bf648f2009-02-14 16:28:21 +1100859 if (parse_forward(&fwd, optarg, 0, 1)) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100860 add_remote_forward(&options, &fwd);
861 } else {
862 fprintf(stderr,
863 "Bad remote forwarding specification "
864 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100865 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100866 }
Damien Miller95def091999-11-25 00:26:21 +1100867 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000868
869 case 'D':
Damien Miller4bf648f2009-02-14 16:28:21 +1100870 if (parse_forward(&fwd, optarg, 1, 0)) {
Damien Millera699d952008-11-03 19:27:34 +1100871 add_local_forward(&options, &fwd);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100872 } else {
Damien Millera699d952008-11-03 19:27:34 +1100873 fprintf(stderr,
874 "Bad dynamic forwarding specification "
875 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100876 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000877 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000878 break;
879
Damien Miller95def091999-11-25 00:26:21 +1100880 case 'C':
881 options.compression = 1;
882 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000883 case 'N':
884 no_shell_flag = 1;
Damien Miller21771e22011-05-15 08:45:50 +1000885 options.request_tty = REQUEST_TTY_NO;
Damien Miller1383bd82000-04-06 12:32:37 +1000886 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000887 case 'T':
Damien Miller21771e22011-05-15 08:45:50 +1000888 options.request_tty = REQUEST_TTY_NO;
Damien Miller1383bd82000-04-06 12:32:37 +1000889 break;
Damien Miller95def091999-11-25 00:26:21 +1100890 case 'o':
Damien Miller9836cf82003-12-17 16:30:06 +1100891 line = xstrdup(optarg);
djm@openbsd.org957fbce2014-10-08 22:20:25 +0000892 if (process_config_line(&options, pw,
893 host ? host : "", host ? host : "", line,
894 "command-line", 0, NULL, SSHCONF_USERCONF) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100895 exit(255);
Darren Tuckera627d422013-06-02 07:31:17 +1000896 free(line);
Damien Miller95def091999-11-25 00:26:21 +1100897 break;
Damien Miller832562e2001-01-30 09:30:01 +1100898 case 's':
899 subsystem_flag = 1;
900 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000901 case 'S':
902 if (options.control_path != NULL)
903 free(options.control_path);
904 options.control_path = xstrdup(optarg);
Damien Miller0e220db2004-06-15 10:34:08 +1000905 break;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000906 case 'b':
907 options.bind_address = optarg;
908 break;
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000909 case 'F':
910 config = optarg;
911 break;
Damien Miller95def091999-11-25 00:26:21 +1100912 default:
913 usage();
914 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000915 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000916
Damien Millerf4614452001-07-14 12:18:10 +1000917 ac -= optind;
918 av += optind;
919
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100920 if (ac > 0 && !host) {
Ben Lindstromc276c122002-12-23 02:14:51 +0000921 if (strrchr(*av, '@')) {
Damien Millerf4614452001-07-14 12:18:10 +1000922 p = xstrdup(*av);
Ben Lindstromc276c122002-12-23 02:14:51 +0000923 cp = strrchr(p, '@');
Damien Millerf4614452001-07-14 12:18:10 +1000924 if (cp == NULL || cp == p)
925 usage();
926 options.user = p;
927 *cp = '\0';
Damien Miller0faf7472013-10-17 11:47:23 +1100928 host = xstrdup(++cp);
Damien Millerf4614452001-07-14 12:18:10 +1000929 } else
Damien Miller0faf7472013-10-17 11:47:23 +1100930 host = xstrdup(*av);
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000931 if (ac > 1) {
932 optind = optreset = 1;
Damien Millerf4614452001-07-14 12:18:10 +1000933 goto again;
934 }
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000935 ac--, av++;
Damien Millerf4614452001-07-14 12:18:10 +1000936 }
937
Damien Miller95def091999-11-25 00:26:21 +1100938 /* Check that we got a host name. */
939 if (!host)
940 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000941
Damien Miller0faf7472013-10-17 11:47:23 +1100942 host_arg = xstrdup(host);
943
Damien Miller1f0311c2014-05-15 14:24:09 +1000944#ifdef WITH_OPENSSL
Damien Miller4314c2b2010-09-10 11:12:09 +1000945 OpenSSL_add_all_algorithms();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100946 ERR_load_crypto_strings();
Damien Miller1f0311c2014-05-15 14:24:09 +1000947#endif
Damien Millercb5e44a2000-09-29 12:12:36 +1100948
Damien Miller95def091999-11-25 00:26:21 +1100949 /* Initialize the command to execute on remote host. */
950 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000951
Damien Miller5428f641999-11-25 11:54:57 +1100952 /*
953 * Save the command to execute on the remote host in a buffer. There
954 * is no limit on the length of the command, except by the maximum
955 * packet size. Also sets the tty flag if there is no command.
956 */
Damien Millerf4614452001-07-14 12:18:10 +1000957 if (!ac) {
Damien Miller95def091999-11-25 00:26:21 +1100958 /* No command specified - execute shell on a tty. */
Damien Miller832562e2001-01-30 09:30:01 +1100959 if (subsystem_flag) {
Damien Millerf4614452001-07-14 12:18:10 +1000960 fprintf(stderr,
961 "You must specify a subsystem to invoke.\n");
Damien Miller832562e2001-01-30 09:30:01 +1100962 usage();
963 }
Damien Miller95def091999-11-25 00:26:21 +1100964 } else {
Damien Millerf4614452001-07-14 12:18:10 +1000965 /* A command has been specified. Store it into the buffer. */
966 for (i = 0; i < ac; i++) {
967 if (i)
Damien Miller95def091999-11-25 00:26:21 +1100968 buffer_append(&command, " ", 1);
969 buffer_append(&command, av[i], strlen(av[i]));
970 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000971 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000972
Damien Miller95def091999-11-25 00:26:21 +1100973 /* Cannot fork to background if no command. */
Darren Tuckerd6173c02008-06-13 04:52:53 +1000974 if (fork_after_authentication_flag && buffer_len(&command) == 0 &&
975 !no_shell_flag)
976 fatal("Cannot fork into background without a command "
977 "to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000978
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000979 /*
980 * Initialize "log" output. Since we are the client all output
Damien Miller03d4d7e2013-04-23 15:21:06 +1000981 * goes to stderr unless otherwise specified by -y or -E.
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000982 */
Damien Miller03d4d7e2013-04-23 15:21:06 +1000983 if (use_syslog && logfile != NULL)
984 fatal("Can't specify both -y and -E");
dtucker@openbsd.org4f7cc2f2015-09-04 08:21:47 +0000985 if (logfile != NULL)
Damien Miller03d4d7e2013-04-23 15:21:06 +1000986 log_redirect_stderr_to(logfile);
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);
djm@openbsd.org674b3b62015-09-11 03:47:28 +00001125 snprintf(uidstr, sizeof(uidstr), "%d", pw->pw_uid);
Darren Tuckerf6b01b72008-06-13 04:56:37 +10001126
Damien Miller9c386432014-07-03 21:27:46 +10001127 if ((md = ssh_digest_start(SSH_DIGEST_SHA1)) == NULL ||
1128 ssh_digest_update(md, thishost, strlen(thishost)) < 0 ||
1129 ssh_digest_update(md, host, strlen(host)) < 0 ||
1130 ssh_digest_update(md, portstr, strlen(portstr)) < 0 ||
1131 ssh_digest_update(md, options.user, strlen(options.user)) < 0 ||
1132 ssh_digest_final(md, conn_hash, sizeof(conn_hash)) < 0)
1133 fatal("%s: mux digest failed", __func__);
1134 ssh_digest_free(md);
1135 conn_hash_hex = tohex(conn_hash, ssh_digest_bytes(SSH_DIGEST_SHA1));
1136
Damien Millerdfc85fa2011-05-15 08:44:02 +10001137 if (options.local_command != NULL) {
Darren Tuckerf6b01b72008-06-13 04:56:37 +10001138 debug3("expanding LocalCommand: %s", options.local_command);
1139 cp = options.local_command;
Damien Miller9c386432014-07-03 21:27:46 +10001140 options.local_command = percent_expand(cp,
1141 "C", conn_hash_hex,
1142 "L", shorthost,
1143 "d", pw->pw_dir,
1144 "h", host,
1145 "l", thishost,
1146 "n", host_arg,
1147 "p", portstr,
1148 "r", options.user,
1149 "u", pw->pw_name,
Damien Millerdfc85fa2011-05-15 08:44:02 +10001150 (char *)NULL);
Darren Tuckerf6b01b72008-06-13 04:56:37 +10001151 debug3("expanded LocalCommand: %s", options.local_command);
Darren Tuckera627d422013-06-02 07:31:17 +10001152 free(cp);
Darren Tuckerf6b01b72008-06-13 04:56:37 +10001153 }
1154
Damien Miller0e220db2004-06-15 10:34:08 +10001155 if (options.control_path != NULL) {
Damien Miller6476cad2005-06-16 13:18:34 +10001156 cp = tilde_expand_filename(options.control_path,
1157 original_real_uid);
Darren Tuckera627d422013-06-02 07:31:17 +10001158 free(options.control_path);
Damien Miller9c386432014-07-03 21:27:46 +10001159 options.control_path = percent_expand(cp,
1160 "C", conn_hash_hex,
1161 "L", shorthost,
1162 "h", host,
1163 "l", thishost,
1164 "n", host_arg,
1165 "p", portstr,
1166 "r", options.user,
1167 "u", pw->pw_name,
djm@openbsd.org674b3b62015-09-11 03:47:28 +00001168 "i", uidstr,
Damien Millerdfc85fa2011-05-15 08:44:02 +10001169 (char *)NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001170 free(cp);
Damien Miller0e220db2004-06-15 10:34:08 +10001171 }
Damien Miller9c386432014-07-03 21:27:46 +10001172 free(conn_hash_hex);
1173
djm@openbsd.org957fbce2014-10-08 22:20:25 +00001174 if (config_test) {
1175 dump_client_config(&options, host);
1176 exit(0);
1177 }
1178
Damien Millerb1cbfa22008-05-19 16:00:08 +10001179 if (muxclient_command != 0 && options.control_path == NULL)
Darren Tucker0814d312005-06-01 23:08:51 +10001180 fatal("No ControlPath specified for \"-O\" command");
Damien Millerd14b1e72005-06-16 13:19:41 +10001181 if (options.control_path != NULL)
Damien Millerb1cbfa22008-05-19 16:00:08 +10001182 muxclient(options.control_path);
Damien Miller0e220db2004-06-15 10:34:08 +10001183
Damien Miller08b57c62014-02-27 10:17:13 +11001184 /*
1185 * If hostname canonicalisation was not enabled, then we may not
1186 * have yet resolved the hostname. Do so now.
1187 */
1188 if (addrs == NULL && options.proxy_command == NULL) {
djm@openbsd.orga85768a2015-09-04 04:56:09 +00001189 debug2("resolving \"%s\" port %d", host, options.port);
Damien Miller08b57c62014-02-27 10:17:13 +11001190 if ((addrs = resolve_host(host, options.port, 1,
1191 cname, sizeof(cname))) == NULL)
1192 cleanup_exit(255); /* resolve_host logs the error */
1193 }
1194
Damien Miller67bd0622007-09-17 12:06:57 +10001195 timeout_ms = options.connection_timeout * 1000;
1196
Kevin Stevesfcec7f82000-12-15 19:55:48 +00001197 /* Open a connection to the remote host. */
Damien Miller0faf7472013-10-17 11:47:23 +11001198 if (ssh_connect(host, addrs, &hostaddr, options.port,
1199 options.address_family, options.connection_attempts,
1200 &timeout_ms, options.tcp_keep_alive,
1201 options.use_privileged_port) != 0)
1202 exit(255);
1203
Damien Miller28631ce2013-10-26 10:07:56 +11001204 if (addrs != NULL)
1205 freeaddrinfo(addrs);
1206
Damien Miller0faf7472013-10-17 11:47:23 +11001207 packet_set_timeout(options.server_alive_interval,
1208 options.server_alive_count_max);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001209
Damien Miller67bd0622007-09-17 12:06:57 +10001210 if (timeout_ms > 0)
1211 debug3("timeout: %d ms remain after connect", timeout_ms);
1212
Damien Miller5428f641999-11-25 11:54:57 +11001213 /*
1214 * If we successfully made the connection, load the host private key
1215 * in case we will need it later for combined rsa-rhosts
1216 * authentication. This must be done before releasing extra
1217 * privileges, because the file is only readable by root.
Ben Lindstrom9e5bb572002-06-06 19:58:27 +00001218 * If we cannot access the private keys, load the public keys
1219 * instead and try to execute the ssh-keysign helper instead.
Damien Miller5428f641999-11-25 11:54:57 +11001220 */
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001221 sensitive_data.nkeys = 0;
1222 sensitive_data.keys = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001223 sensitive_data.external_keysign = 0;
Ben Lindstrom24157572002-06-12 16:09:39 +00001224 if (options.rhosts_rsa_authentication ||
1225 options.hostbased_authentication) {
Damien Miller0fa47cf2013-12-29 17:53:39 +11001226 sensitive_data.nkeys = 9;
Damien Millerddd63ab2006-03-31 23:10:51 +11001227 sensitive_data.keys = xcalloc(sensitive_data.nkeys,
Ben Lindstroma962c2f2002-07-04 00:14:17 +00001228 sizeof(Key));
Damien Miller6af914a2010-09-10 11:39:26 +10001229 for (i = 0; i < sensitive_data.nkeys; i++)
1230 sensitive_data.keys[i] = NULL;
Ben Lindstromf9c48842002-06-11 16:37:51 +00001231
1232 PRIV_START;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001233 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
Darren Tucker232b76f2006-05-06 17:41:51 +10001234 _PATH_HOST_KEY_FILE, "", NULL, NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001235#ifdef OPENSSL_HAS_ECC
djm@openbsd.orgab24ab82015-01-08 10:15:45 +00001236 sensitive_data.keys[1] = key_load_private_cert(KEY_ECDSA,
Damien Millereb8b60e2010-08-31 22:41:14 +10001237 _PATH_HOST_ECDSA_KEY_FILE, "", NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001238#endif
djm@openbsd.orgab24ab82015-01-08 10:15:45 +00001239 sensitive_data.keys[2] = key_load_private_cert(KEY_ED25519,
1240 _PATH_HOST_ED25519_KEY_FILE, "", NULL);
Damien Millereb8b60e2010-08-31 22:41:14 +10001241 sensitive_data.keys[3] = key_load_private_cert(KEY_RSA,
Damien Millerc1583312010-08-05 13:04:50 +10001242 _PATH_HOST_RSA_KEY_FILE, "", NULL);
djm@openbsd.orgab24ab82015-01-08 10:15:45 +00001243 sensitive_data.keys[4] = key_load_private_cert(KEY_DSA,
1244 _PATH_HOST_DSA_KEY_FILE, "", NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001245#ifdef OPENSSL_HAS_ECC
djm@openbsd.orgab24ab82015-01-08 10:15:45 +00001246 sensitive_data.keys[5] = key_load_private_type(KEY_ECDSA,
Damien Millereb8b60e2010-08-31 22:41:14 +10001247 _PATH_HOST_ECDSA_KEY_FILE, "", NULL, NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001248#endif
djm@openbsd.orgab24ab82015-01-08 10:15:45 +00001249 sensitive_data.keys[6] = key_load_private_type(KEY_ED25519,
1250 _PATH_HOST_ED25519_KEY_FILE, "", NULL, NULL);
Damien Miller0fa47cf2013-12-29 17:53:39 +11001251 sensitive_data.keys[7] = key_load_private_type(KEY_RSA,
Darren Tucker232b76f2006-05-06 17:41:51 +10001252 _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL);
djm@openbsd.orgab24ab82015-01-08 10:15:45 +00001253 sensitive_data.keys[8] = key_load_private_type(KEY_DSA,
1254 _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL);
Ben Lindstromf9c48842002-06-11 16:37:51 +00001255 PRIV_END;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001256
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +00001257 if (options.hostbased_authentication == 1 &&
1258 sensitive_data.keys[0] == NULL &&
Damien Millereb8b60e2010-08-31 22:41:14 +10001259 sensitive_data.keys[5] == NULL &&
Damien Miller5be9d9e2013-12-07 11:24:01 +11001260 sensitive_data.keys[6] == NULL &&
Damien Miller0fa47cf2013-12-29 17:53:39 +11001261 sensitive_data.keys[7] == NULL &&
1262 sensitive_data.keys[8] == NULL) {
Damien Miller6af914a2010-09-10 11:39:26 +10001263#ifdef OPENSSL_HAS_ECC
djm@openbsd.orgab24ab82015-01-08 10:15:45 +00001264 sensitive_data.keys[1] = key_load_cert(
Damien Millereb8b60e2010-08-31 22:41:14 +10001265 _PATH_HOST_ECDSA_KEY_FILE);
Damien Miller6af914a2010-09-10 11:39:26 +10001266#endif
djm@openbsd.orgab24ab82015-01-08 10:15:45 +00001267 sensitive_data.keys[2] = key_load_cert(
1268 _PATH_HOST_ED25519_KEY_FILE);
Damien Millereb8b60e2010-08-31 22:41:14 +10001269 sensitive_data.keys[3] = key_load_cert(
Damien Millerc1583312010-08-05 13:04:50 +10001270 _PATH_HOST_RSA_KEY_FILE);
Damien Miller0fa47cf2013-12-29 17:53:39 +11001271 sensitive_data.keys[4] = key_load_cert(
djm@openbsd.orgab24ab82015-01-08 10:15:45 +00001272 _PATH_HOST_DSA_KEY_FILE);
Damien Miller6af914a2010-09-10 11:39:26 +10001273#ifdef OPENSSL_HAS_ECC
djm@openbsd.orgab24ab82015-01-08 10:15:45 +00001274 sensitive_data.keys[5] = key_load_public(
Damien Millereb8b60e2010-08-31 22:41:14 +10001275 _PATH_HOST_ECDSA_KEY_FILE, NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001276#endif
djm@openbsd.orgab24ab82015-01-08 10:15:45 +00001277 sensitive_data.keys[6] = key_load_public(
1278 _PATH_HOST_ED25519_KEY_FILE, NULL);
Damien Miller5be9d9e2013-12-07 11:24:01 +11001279 sensitive_data.keys[7] = key_load_public(
Damien Miller0fa47cf2013-12-29 17:53:39 +11001280 _PATH_HOST_RSA_KEY_FILE, NULL);
1281 sensitive_data.keys[8] = key_load_public(
djm@openbsd.orgab24ab82015-01-08 10:15:45 +00001282 _PATH_HOST_DSA_KEY_FILE, NULL);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001283 sensitive_data.external_keysign = 1;
1284 }
Damien Miller95def091999-11-25 00:26:21 +11001285 }
Damien Miller5428f641999-11-25 11:54:57 +11001286 /*
1287 * Get rid of any extra privileges that we may have. We will no
1288 * longer need them. Also, extra privileges could make it very hard
1289 * to read identity files and other non-world-readable files from the
1290 * user's home directory if it happens to be on a NFS volume where
1291 * root is mapped to nobody.
1292 */
Darren Tucker25f60a72004-08-15 17:23:34 +10001293 if (original_effective_uid == 0) {
1294 PRIV_START;
1295 permanently_set_uid(pw);
1296 }
Damien Miller95def091999-11-25 00:26:21 +11001297
Damien Miller5428f641999-11-25 11:54:57 +11001298 /*
1299 * Now that we are back to our own permissions, create ~/.ssh
Damien Miller788f2122005-11-05 15:14:59 +11001300 * directory if it doesn't already exist.
Damien Miller5428f641999-11-25 11:54:57 +11001301 */
Darren Tucker45c66d72011-11-04 10:50:40 +11001302 if (config == NULL) {
1303 r = snprintf(buf, sizeof buf, "%s%s%s", pw->pw_dir,
1304 strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
1305 if (r > 0 && (size_t)r < sizeof(buf) && stat(buf, &st) < 0) {
Darren Tucker8ccb7392010-09-10 12:28:24 +10001306#ifdef WITH_SELINUX
Darren Tucker45c66d72011-11-04 10:50:40 +11001307 ssh_selinux_setfscreatecon(buf);
Darren Tucker8ccb7392010-09-10 12:28:24 +10001308#endif
Darren Tucker45c66d72011-11-04 10:50:40 +11001309 if (mkdir(buf, 0700) < 0)
1310 error("Could not create directory '%.200s'.",
1311 buf);
Darren Tucker8ccb7392010-09-10 12:28:24 +10001312#ifdef WITH_SELINUX
Darren Tucker45c66d72011-11-04 10:50:40 +11001313 ssh_selinux_setfscreatecon(NULL);
Darren Tucker8ccb7392010-09-10 12:28:24 +10001314#endif
Darren Tucker45c66d72011-11-04 10:50:40 +11001315 }
Darren Tucker8ccb7392010-09-10 12:28:24 +10001316 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001317 /* load options.identity_files */
1318 load_public_identity_files();
1319
1320 /* Expand ~ in known host file names. */
Damien Miller295ee632011-05-29 21:42:31 +10001321 tilde_expand_paths(options.system_hostfiles,
1322 options.num_system_hostfiles);
1323 tilde_expand_paths(options.user_hostfiles, options.num_user_hostfiles);
Damien Miller95def091999-11-25 00:26:21 +11001324
Damien Miller07cd5892001-11-12 10:52:03 +11001325 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
Damien Miller857b02e2010-09-24 22:02:56 +10001326 signal(SIGCHLD, main_sigchld_handler);
Damien Miller07cd5892001-11-12 10:52:03 +11001327
Darren Tuckerd6173c02008-06-13 04:52:53 +10001328 /* Log into the remote system. Never returns if the login fails. */
Damien Miller67bd0622007-09-17 12:06:57 +10001329 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr,
Damien Millerd925dcd2010-12-01 12:21:51 +11001330 options.port, pw, timeout_ms);
Damien Miller95def091999-11-25 00:26:21 +11001331
Damien Miller383ffe62010-06-26 10:02:03 +10001332 if (packet_connection_is_on_socket()) {
1333 verbose("Authenticated to %s ([%s]:%d).", host,
1334 get_remote_ipaddr(), get_remote_port());
1335 } else {
1336 verbose("Authenticated to %s (via proxy).", host);
1337 }
1338
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001339 /* We no longer need the private host keys. Clear them now. */
1340 if (sensitive_data.nkeys != 0) {
1341 for (i = 0; i < sensitive_data.nkeys; i++) {
1342 if (sensitive_data.keys[i] != NULL) {
1343 /* Destroys contents safely */
1344 debug3("clear hostkey %d", i);
1345 key_free(sensitive_data.keys[i]);
1346 sensitive_data.keys[i] = NULL;
1347 }
1348 }
Darren Tuckera627d422013-06-02 07:31:17 +10001349 free(sensitive_data.keys);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001350 }
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +00001351 for (i = 0; i < options.num_identity_files; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10001352 free(options.identity_files[i]);
1353 options.identity_files[i] = NULL;
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +00001354 if (options.identity_keys[i]) {
1355 key_free(options.identity_keys[i]);
1356 options.identity_keys[i] = NULL;
1357 }
1358 }
Damien Miller95def091999-11-25 00:26:21 +11001359
Damien Miller1383bd82000-04-06 12:32:37 +10001360 exit_status = compat20 ? ssh_session2() : ssh_session();
1361 packet_close();
Damien Miller8c4e18a2002-09-19 12:05:02 +10001362
Damien Millerb1cbfa22008-05-19 16:00:08 +10001363 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10001364 unlink(options.control_path);
1365
Damien Millera41ccca2010-10-07 22:07:32 +11001366 /* Kill ProxyCommand if it is running. */
1367 ssh_kill_proxy_command();
Damien Miller8c4e18a2002-09-19 12:05:02 +10001368
Damien Miller1383bd82000-04-06 12:32:37 +10001369 return exit_status;
1370}
1371
Damien Millere11e1ea2010-08-03 16:04:46 +10001372static void
1373control_persist_detach(void)
1374{
1375 pid_t pid;
Damien Miller00d9ae22010-08-17 01:59:31 +10001376 int devnull;
Damien Millere11e1ea2010-08-03 16:04:46 +10001377
1378 debug("%s: backgrounding master process", __func__);
1379
1380 /*
1381 * master (current process) into the background, and make the
1382 * foreground process a client of the backgrounded master.
1383 */
1384 switch ((pid = fork())) {
1385 case -1:
1386 fatal("%s: fork: %s", __func__, strerror(errno));
1387 case 0:
1388 /* Child: master process continues mainloop */
1389 break;
1390 default:
1391 /* Parent: set up mux slave to connect to backgrounded master */
1392 debug2("%s: background process is %ld", __func__, (long)pid);
1393 stdin_null_flag = ostdin_null_flag;
Damien Miller21771e22011-05-15 08:45:50 +10001394 options.request_tty = orequest_tty;
Damien Millere11e1ea2010-08-03 16:04:46 +10001395 tty_flag = otty_flag;
1396 close(muxserver_sock);
1397 muxserver_sock = -1;
Damien Miller5929c522010-09-10 11:17:02 +10001398 options.control_master = SSHCTL_MASTER_NO;
Damien Millere11e1ea2010-08-03 16:04:46 +10001399 muxclient(options.control_path);
1400 /* muxclient() doesn't return on success. */
1401 fatal("Failed to connect to new control master");
1402 }
Damien Miller00d9ae22010-08-17 01:59:31 +10001403 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
1404 error("%s: open(\"/dev/null\"): %s", __func__,
1405 strerror(errno));
1406 } else {
1407 if (dup2(devnull, STDIN_FILENO) == -1 ||
1408 dup2(devnull, STDOUT_FILENO) == -1)
1409 error("%s: dup2: %s", __func__, strerror(errno));
1410 if (devnull > STDERR_FILENO)
1411 close(devnull);
1412 }
Damien Miller98e27dc2013-07-25 11:55:52 +10001413 daemon(1, 1);
Damien Millerea2c1a42011-06-03 12:10:22 +10001414 setproctitle("%s [mux]", options.control_path);
Damien Millere11e1ea2010-08-03 16:04:46 +10001415}
1416
1417/* Do fork() after authentication. Used by "ssh -f" */
1418static void
1419fork_postauth(void)
1420{
1421 if (need_controlpersist_detach)
1422 control_persist_detach();
1423 debug("forking to background");
1424 fork_after_authentication_flag = 0;
1425 if (daemon(1, 1) < 0)
1426 fatal("daemon() failed: %.200s", strerror(errno));
1427}
1428
Darren Tucker9f407c42008-06-13 04:50:27 +10001429/* Callback for remote forward global requests */
1430static void
1431ssh_confirm_remote_forward(int type, u_int32_t seq, void *ctxt)
1432{
Damien Miller7acefbb2014-07-18 14:11:24 +10001433 struct Forward *rfwd = (struct Forward *)ctxt;
Darren Tucker9f407c42008-06-13 04:50:27 +10001434
Damien Miller4bf648f2009-02-14 16:28:21 +11001435 /* XXX verbose() on failure? */
Damien Miller4b3ed642014-07-02 15:29:40 +10001436 debug("remote forward %s for: listen %s%s%d, connect %s:%d",
Darren Tucker9f407c42008-06-13 04:50:27 +10001437 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
Damien Miller7acefbb2014-07-18 14:11:24 +10001438 rfwd->listen_path ? rfwd->listen_path :
1439 rfwd->listen_host ? rfwd->listen_host : "",
1440 (rfwd->listen_path || rfwd->listen_host) ? ":" : "",
1441 rfwd->listen_port, rfwd->connect_path ? rfwd->connect_path :
1442 rfwd->connect_host, rfwd->connect_port);
1443 if (rfwd->listen_path == NULL && rfwd->listen_port == 0) {
Darren Tucker68afb8c2011-10-02 18:59:03 +11001444 if (type == SSH2_MSG_REQUEST_SUCCESS) {
1445 rfwd->allocated_port = packet_get_int();
1446 logit("Allocated port %u for remote forward to %s:%d",
1447 rfwd->allocated_port,
1448 rfwd->connect_host, rfwd->connect_port);
1449 channel_update_permitted_opens(rfwd->handle,
1450 rfwd->allocated_port);
1451 } else {
1452 channel_update_permitted_opens(rfwd->handle, -1);
1453 }
Damien Miller4bf648f2009-02-14 16:28:21 +11001454 }
1455
Darren Tucker9f407c42008-06-13 04:50:27 +10001456 if (type == SSH2_MSG_REQUEST_FAILURE) {
Damien Miller7acefbb2014-07-18 14:11:24 +10001457 if (options.exit_on_forward_failure) {
1458 if (rfwd->listen_path != NULL)
1459 fatal("Error: remote port forwarding failed "
1460 "for listen path %s", rfwd->listen_path);
1461 else
1462 fatal("Error: remote port forwarding failed "
1463 "for listen port %d", rfwd->listen_port);
1464 } else {
1465 if (rfwd->listen_path != NULL)
1466 logit("Warning: remote port forwarding failed "
1467 "for listen path %s", rfwd->listen_path);
1468 else
1469 logit("Warning: remote port forwarding failed "
1470 "for listen port %d", rfwd->listen_port);
1471 }
Darren Tucker9f407c42008-06-13 04:50:27 +10001472 }
Darren Tucker9a2a6092008-07-04 12:53:50 +10001473 if (++remote_forward_confirms_received == options.num_remote_forwards) {
Darren Tucker9f407c42008-06-13 04:50:27 +10001474 debug("All remote forwarding requests processed");
Damien Millere11e1ea2010-08-03 16:04:46 +10001475 if (fork_after_authentication_flag)
1476 fork_postauth();
Darren Tucker9a2a6092008-07-04 12:53:50 +10001477 }
Darren Tucker9f407c42008-06-13 04:50:27 +10001478}
1479
Ben Lindstrombba81212001-06-25 05:01:22 +00001480static void
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001481client_cleanup_stdio_fwd(int id, void *arg)
1482{
1483 debug("stdio forwarding: done");
1484 cleanup_exit(0);
1485}
1486
Darren Tucker2d6665d2011-11-04 10:54:22 +11001487static void
Damien Miller357610d2014-07-18 15:04:10 +10001488ssh_stdio_confirm(int id, int success, void *arg)
1489{
1490 if (!success)
1491 fatal("stdio forwarding failed");
1492}
1493
1494static void
Darren Tucker2d6665d2011-11-04 10:54:22 +11001495ssh_init_stdio_forwarding(void)
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001496{
1497 Channel *c;
Damien Millere1537f92010-01-26 13:26:22 +11001498 int in, out;
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001499
Darren Tucker2d6665d2011-11-04 10:54:22 +11001500 if (stdio_forward_host == NULL)
1501 return;
Damien Miller386feab2013-10-15 12:14:49 +11001502 if (!compat20)
Darren Tucker2d6665d2011-11-04 10:54:22 +11001503 fatal("stdio forwarding require Protocol 2");
Damien Millere1537f92010-01-26 13:26:22 +11001504
Darren Tucker2d6665d2011-11-04 10:54:22 +11001505 debug3("%s: %s:%d", __func__, stdio_forward_host, stdio_forward_port);
1506
1507 if ((in = dup(STDIN_FILENO)) < 0 ||
1508 (out = dup(STDOUT_FILENO)) < 0)
Damien Millere1537f92010-01-26 13:26:22 +11001509 fatal("channel_connect_stdio_fwd: dup() in/out failed");
Darren Tucker2d6665d2011-11-04 10:54:22 +11001510 if ((c = channel_connect_stdio_fwd(stdio_forward_host,
1511 stdio_forward_port, in, out)) == NULL)
1512 fatal("%s: channel_connect_stdio_fwd failed", __func__);
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001513 channel_register_cleanup(c->self, client_cleanup_stdio_fwd, 0);
Damien Miller357610d2014-07-18 15:04:10 +10001514 channel_register_open_confirm(c->self, ssh_stdio_confirm, NULL);
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001515}
1516
1517static void
Damien Miller0bc1bd82000-11-13 22:57:25 +11001518ssh_init_forwarding(void)
1519{
Kevin Steves12057502001-02-05 14:54:34 +00001520 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001521 int i;
Kevin Steves12057502001-02-05 14:54:34 +00001522
Damien Miller0bc1bd82000-11-13 22:57:25 +11001523 /* Initiate local TCP/IP port forwardings. */
1524 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001525 debug("Local connections to %.200s:%d forwarded to remote "
1526 "address %.200s:%d",
Damien Miller7acefbb2014-07-18 14:11:24 +10001527 (options.local_forwards[i].listen_path != NULL) ?
1528 options.local_forwards[i].listen_path :
Darren Tucker47eede72005-03-14 23:08:12 +11001529 (options.local_forwards[i].listen_host == NULL) ?
Damien Miller7acefbb2014-07-18 14:11:24 +10001530 (options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") :
Damien Millerf91ee4c2005-03-01 21:24:33 +11001531 options.local_forwards[i].listen_host,
1532 options.local_forwards[i].listen_port,
Damien Miller7acefbb2014-07-18 14:11:24 +10001533 (options.local_forwards[i].connect_path != NULL) ?
1534 options.local_forwards[i].connect_path :
Damien Millerf91ee4c2005-03-01 21:24:33 +11001535 options.local_forwards[i].connect_host,
1536 options.local_forwards[i].connect_port);
Damien Millerb16461c2002-01-22 23:29:22 +11001537 success += channel_setup_local_fwd_listener(
Damien Miller7acefbb2014-07-18 14:11:24 +10001538 &options.local_forwards[i], &options.fwd_opts);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001539 }
Darren Tuckere7d4b192006-07-12 22:17:10 +10001540 if (i > 0 && success != i && options.exit_on_forward_failure)
1541 fatal("Could not request local forwarding.");
Kevin Steves12057502001-02-05 14:54:34 +00001542 if (i > 0 && success == 0)
1543 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001544
1545 /* Initiate remote TCP/IP port forwardings. */
1546 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001547 debug("Remote connections from %.200s:%d forwarded to "
1548 "local address %.200s:%d",
Damien Miller7acefbb2014-07-18 14:11:24 +10001549 (options.remote_forwards[i].listen_path != NULL) ?
1550 options.remote_forwards[i].listen_path :
Damien Miller46d38de2005-07-17 17:02:09 +10001551 (options.remote_forwards[i].listen_host == NULL) ?
Damien Milleraa3bb102005-11-05 15:12:59 +11001552 "LOCALHOST" : options.remote_forwards[i].listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +11001553 options.remote_forwards[i].listen_port,
Damien Miller7acefbb2014-07-18 14:11:24 +10001554 (options.remote_forwards[i].connect_path != NULL) ?
1555 options.remote_forwards[i].connect_path :
Damien Millerf91ee4c2005-03-01 21:24:33 +11001556 options.remote_forwards[i].connect_host,
1557 options.remote_forwards[i].connect_port);
Darren Tucker68afb8c2011-10-02 18:59:03 +11001558 options.remote_forwards[i].handle =
1559 channel_request_remote_forwarding(
Damien Miller7acefbb2014-07-18 14:11:24 +10001560 &options.remote_forwards[i]);
Darren Tucker68afb8c2011-10-02 18:59:03 +11001561 if (options.remote_forwards[i].handle < 0) {
Darren Tuckere7d4b192006-07-12 22:17:10 +10001562 if (options.exit_on_forward_failure)
1563 fatal("Could not request remote forwarding.");
1564 else
1565 logit("Warning: Could not request remote "
1566 "forwarding.");
Darren Tucker68afb8c2011-10-02 18:59:03 +11001567 } else {
1568 client_register_global_confirm(ssh_confirm_remote_forward,
1569 &options.remote_forwards[i]);
Darren Tuckere7d4b192006-07-12 22:17:10 +10001570 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001571 }
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001572
1573 /* Initiate tunnel forwarding. */
1574 if (options.tun_open != SSH_TUNMODE_NO) {
1575 if (client_request_tun_fwd(options.tun_open,
1576 options.tun_local, options.tun_remote) == -1) {
1577 if (options.exit_on_forward_failure)
1578 fatal("Could not request tunnel forwarding.");
1579 else
1580 error("Could not request tunnel forwarding.");
1581 }
1582 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001583}
1584
Ben Lindstrombba81212001-06-25 05:01:22 +00001585static void
Damien Miller0bc1bd82000-11-13 22:57:25 +11001586check_agent_present(void)
1587{
djm@openbsd.org141efe42015-01-14 20:05:27 +00001588 int r;
1589
Damien Miller0bc1bd82000-11-13 22:57:25 +11001590 if (options.forward_agent) {
Damien Miller788f2122005-11-05 15:14:59 +11001591 /* Clear agent forwarding if we don't have an agent. */
djm@openbsd.org141efe42015-01-14 20:05:27 +00001592 if ((r = ssh_get_authentication_socket(NULL)) != 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001593 options.forward_agent = 0;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001594 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1595 debug("ssh_get_authentication_socket: %s",
1596 ssh_err(r));
1597 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001598 }
1599}
1600
Ben Lindstrombba81212001-06-25 05:01:22 +00001601static int
Damien Miller1383bd82000-04-06 12:32:37 +10001602ssh_session(void)
1603{
1604 int type;
Damien Miller1383bd82000-04-06 12:32:37 +10001605 int interactive = 0;
1606 int have_tty = 0;
1607 struct winsize ws;
Damien Miller1383bd82000-04-06 12:32:37 +10001608 char *cp;
Damien Miller17e7ed02005-06-17 12:54:33 +10001609 const char *display;
Damien Miller1383bd82000-04-06 12:32:37 +10001610
Damien Miller95def091999-11-25 00:26:21 +11001611 /* Enable compression if requested. */
1612 if (options.compression) {
Darren Tuckerd6173c02008-06-13 04:52:53 +10001613 debug("Requesting compression at level %d.",
1614 options.compression_level);
Damien Miller95def091999-11-25 00:26:21 +11001615
Darren Tuckerd6173c02008-06-13 04:52:53 +10001616 if (options.compression_level < 1 ||
1617 options.compression_level > 9)
1618 fatal("Compression level must be from 1 (fast) to "
1619 "9 (slow, best).");
Damien Miller95def091999-11-25 00:26:21 +11001620
1621 /* Send the request. */
1622 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
1623 packet_put_int(options.compression_level);
1624 packet_send();
1625 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +11001626 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +11001627 if (type == SSH_SMSG_SUCCESS)
1628 packet_start_compression(options.compression_level);
1629 else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +10001630 logit("Warning: Remote host refused compression.");
Damien Miller95def091999-11-25 00:26:21 +11001631 else
Darren Tuckerd6173c02008-06-13 04:52:53 +10001632 packet_disconnect("Protocol error waiting for "
1633 "compression response.");
Damien Miller95def091999-11-25 00:26:21 +11001634 }
1635 /* Allocate a pseudo tty if appropriate. */
1636 if (tty_flag) {
1637 debug("Requesting pty.");
1638
1639 /* Start the packet. */
1640 packet_start(SSH_CMSG_REQUEST_PTY);
1641
1642 /* Store TERM in the packet. There is no limit on the
1643 length of the string. */
1644 cp = getenv("TERM");
1645 if (!cp)
1646 cp = "";
Ben Lindstrom664408d2001-06-09 01:42:01 +00001647 packet_put_cstring(cp);
Damien Miller95def091999-11-25 00:26:21 +11001648
1649 /* Store window size in the packet. */
1650 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
1651 memset(&ws, 0, sizeof(ws));
Damien Miller71a73672006-03-26 14:04:36 +11001652 packet_put_int((u_int)ws.ws_row);
1653 packet_put_int((u_int)ws.ws_col);
1654 packet_put_int((u_int)ws.ws_xpixel);
1655 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller95def091999-11-25 00:26:21 +11001656
1657 /* Store tty modes in the packet. */
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001658 tty_make_modes(fileno(stdin), NULL);
Damien Miller95def091999-11-25 00:26:21 +11001659
1660 /* Send the packet, and wait for it to leave. */
1661 packet_send();
1662 packet_write_wait();
1663
1664 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +11001665 type = packet_read();
Damien Miller450a7a12000-03-26 13:04:51 +10001666 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +11001667 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001668 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +10001669 } else if (type == SSH_SMSG_FAILURE)
Darren Tuckerd6173c02008-06-13 04:52:53 +10001670 logit("Warning: Remote host failed or refused to "
1671 "allocate a pseudo tty.");
Damien Miller95def091999-11-25 00:26:21 +11001672 else
Darren Tuckerd6173c02008-06-13 04:52:53 +10001673 packet_disconnect("Protocol error waiting for pty "
1674 "request response.");
Damien Miller95def091999-11-25 00:26:21 +11001675 }
1676 /* Request X11 forwarding if enabled and DISPLAY is set. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001677 display = getenv("DISPLAY");
djm@openbsd.orga58be332015-04-17 13:16:48 +00001678 if (display == NULL && options.forward_x11)
1679 debug("X11 forwarding requested but DISPLAY not set");
Damien Miller17e7ed02005-06-17 12:54:33 +10001680 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001681 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001682 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001683 client_x11_get_proto(display, options.xauth_location,
Damien Miller386feab2013-10-15 12:14:49 +11001684 options.forward_x11_trusted,
Damien Miller1ab6a512010-06-26 10:02:24 +10001685 options.forward_x11_timeout,
1686 &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001687 /* Request forwarding with authentication spoofing. */
Darren Tuckerd6173c02008-06-13 04:52:53 +10001688 debug("Requesting X11 forwarding with authentication "
1689 "spoofing.");
Damien Miller6d7b4372011-06-23 08:31:57 +10001690 x11_request_forwarding_with_spoofing(0, display, proto,
1691 data, 0);
Damien Miller95def091999-11-25 00:26:21 +11001692 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +11001693 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +11001694 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +11001695 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001696 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller996acd22003-04-09 20:59:48 +10001697 logit("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +10001698 } else {
Darren Tuckerd6173c02008-06-13 04:52:53 +10001699 packet_disconnect("Protocol error waiting for X11 "
1700 "forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +10001701 }
Damien Miller95def091999-11-25 00:26:21 +11001702 }
1703 /* Tell the packet module whether this is an interactive session. */
Damien Miller0dac6fb2010-11-20 15:19:38 +11001704 packet_set_interactive(interactive,
1705 options.ip_qos_interactive, options.ip_qos_bulk);
Damien Miller95def091999-11-25 00:26:21 +11001706
1707 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001708 check_agent_present();
1709
Damien Miller95def091999-11-25 00:26:21 +11001710 if (options.forward_agent) {
1711 debug("Requesting authentication agent forwarding.");
1712 auth_request_forwarding();
1713
1714 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +11001715 type = packet_read();
Damien Miller48b03fc2002-01-22 23:11:40 +11001716 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +11001717 if (type != SSH_SMSG_SUCCESS)
Damien Miller996acd22003-04-09 20:59:48 +10001718 logit("Warning: Remote host denied authentication agent forwarding.");
Damien Miller95def091999-11-25 00:26:21 +11001719 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001720
Damien Miller0bc1bd82000-11-13 22:57:25 +11001721 /* Initiate port forwardings. */
Darren Tucker2d6665d2011-11-04 10:54:22 +11001722 ssh_init_stdio_forwarding();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001723 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001724
Darren Tuckerb776c852007-12-02 23:06:35 +11001725 /* Execute a local command */
1726 if (options.local_command != NULL &&
1727 options.permit_local_command)
1728 ssh_local_cmd(options.local_command);
1729
Darren Tucker9a2a6092008-07-04 12:53:50 +10001730 /*
1731 * If requested and we are not interested in replies to remote
1732 * forwarding requests, then let ssh continue in the background.
1733 */
Damien Millere11e1ea2010-08-03 16:04:46 +10001734 if (fork_after_authentication_flag) {
1735 if (options.exit_on_forward_failure &&
1736 options.num_remote_forwards > 0) {
1737 debug("deferring postauth fork until remote forward "
1738 "confirmation received");
1739 } else
1740 fork_postauth();
Darren Tucker9a2a6092008-07-04 12:53:50 +10001741 }
Damien Miller5428f641999-11-25 11:54:57 +11001742
1743 /*
1744 * If a command was specified on the command line, execute the
1745 * command now. Otherwise request the server to start a shell.
1746 */
Damien Miller95def091999-11-25 00:26:21 +11001747 if (buffer_len(&command) > 0) {
1748 int len = buffer_len(&command);
1749 if (len > 900)
1750 len = 900;
Darren Tuckerd6173c02008-06-13 04:52:53 +10001751 debug("Sending command: %.*s", len,
1752 (u_char *)buffer_ptr(&command));
Damien Miller95def091999-11-25 00:26:21 +11001753 packet_start(SSH_CMSG_EXEC_CMD);
1754 packet_put_string(buffer_ptr(&command), buffer_len(&command));
1755 packet_send();
1756 packet_write_wait();
1757 } else {
1758 debug("Requesting shell.");
1759 packet_start(SSH_CMSG_EXEC_SHELL);
1760 packet_send();
1761 packet_write_wait();
1762 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001763
Damien Miller95def091999-11-25 00:26:21 +11001764 /* Enter the interactive session. */
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001765 return client_loop(have_tty, tty_flag ?
1766 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
Damien Miller1383bd82000-04-06 12:32:37 +10001767}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001768
Ben Lindstromf558cf62001-09-20 23:13:49 +00001769/* request pty/x11/agent/tcpfwd/shell for channel */
Ben Lindstrombba81212001-06-25 05:01:22 +00001770static void
Damien Millerd530f5f2010-05-21 14:57:10 +10001771ssh_session2_setup(int id, int success, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +10001772{
Damien Miller3756dce2004-06-18 01:17:29 +10001773 extern char **environ;
Damien Miller17e7ed02005-06-17 12:54:33 +10001774 const char *display;
Damien Miller0e220db2004-06-15 10:34:08 +10001775 int interactive = tty_flag;
Damien Miller17e7ed02005-06-17 12:54:33 +10001776
Damien Millerd530f5f2010-05-21 14:57:10 +10001777 if (!success)
1778 return; /* No need for error message, channels code sens one */
1779
Damien Miller46d38de2005-07-17 17:02:09 +10001780 display = getenv("DISPLAY");
djm@openbsd.orga58be332015-04-17 13:16:48 +00001781 if (display == NULL && options.forward_x11)
1782 debug("X11 forwarding requested but DISPLAY not set");
Damien Miller17e7ed02005-06-17 12:54:33 +10001783 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001784 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001785 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001786 client_x11_get_proto(display, options.xauth_location,
Damien Miller1ab6a512010-06-26 10:02:24 +10001787 options.forward_x11_trusted,
1788 options.forward_x11_timeout, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001789 /* Request forwarding with authentication spoofing. */
Darren Tuckerd6173c02008-06-13 04:52:53 +10001790 debug("Requesting X11 forwarding with authentication "
1791 "spoofing.");
Damien Miller6d7b4372011-06-23 08:31:57 +10001792 x11_request_forwarding_with_spoofing(id, display, proto,
1793 data, 1);
1794 client_expect_confirm(id, "X11 forwarding", CONFIRM_WARN);
1795 /* XXX exit_on_forward_failure */
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001796 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001797 }
1798
Damien Miller0bc1bd82000-11-13 22:57:25 +11001799 check_agent_present();
1800 if (options.forward_agent) {
1801 debug("Requesting authentication agent forwarding.");
1802 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1803 packet_send();
1804 }
1805
Darren Tucker7b305012012-07-02 18:55:09 +10001806 /* Tell the packet module whether this is an interactive session. */
1807 packet_set_interactive(interactive,
1808 options.ip_qos_interactive, options.ip_qos_bulk);
1809
Damien Miller0e220db2004-06-15 10:34:08 +10001810 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
Damien Miller5771ed72008-05-19 15:35:33 +10001811 NULL, fileno(stdin), &command, environ);
Damien Miller1383bd82000-04-06 12:32:37 +10001812}
1813
Ben Lindstromf558cf62001-09-20 23:13:49 +00001814/* open new channel for a session */
Ben Lindstrombba81212001-06-25 05:01:22 +00001815static int
Ben Lindstromf558cf62001-09-20 23:13:49 +00001816ssh_session2_open(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001817{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001818 Channel *c;
1819 int window, packetmax, in, out, err;
Damien Millerad833b32000-08-23 10:46:23 +10001820
Damien Millercaf6dd62000-08-29 11:33:50 +11001821 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001822 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001823 } else {
1824 in = dup(STDIN_FILENO);
1825 }
1826 out = dup(STDOUT_FILENO);
1827 err = dup(STDERR_FILENO);
1828
1829 if (in < 0 || out < 0 || err < 0)
1830 fatal("dup() in/out/err failed");
1831
Damien Miller69b69aa2000-10-28 14:19:58 +11001832 /* enable nonblocking unless tty */
1833 if (!isatty(in))
1834 set_nonblock(in);
1835 if (!isatty(out))
1836 set_nonblock(out);
1837 if (!isatty(err))
1838 set_nonblock(err);
1839
Damien Millere4340be2000-09-16 13:29:08 +11001840 window = CHAN_SES_WINDOW_DEFAULT;
1841 packetmax = CHAN_SES_PACKET_DEFAULT;
Damien Miller19a59452002-02-19 15:20:57 +11001842 if (tty_flag) {
1843 window >>= 1;
1844 packetmax >>= 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001845 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001846 c = channel_new(
Damien Miller1383bd82000-04-06 12:32:37 +10001847 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001848 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001849 "client-session", /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001850
Ben Lindstromf558cf62001-09-20 23:13:49 +00001851 debug3("ssh_session2_open: channel_new: %d", c->self);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001852
Ben Lindstrom5ec26452001-06-09 00:18:51 +00001853 channel_send_open(c->self);
Ben Lindstromf558cf62001-09-20 23:13:49 +00001854 if (!no_shell_flag)
Damien Millerb84886b2008-05-19 15:05:07 +10001855 channel_register_open_confirm(c->self,
1856 ssh_session2_setup, NULL);
Damien Miller1383bd82000-04-06 12:32:37 +10001857
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001858 return c->self;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001859}
1860
Ben Lindstrombba81212001-06-25 05:01:22 +00001861static int
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001862ssh_session2(void)
1863{
Ben Lindstromf558cf62001-09-20 23:13:49 +00001864 int id = -1;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001865
1866 /* XXX should be pre-session */
Darren Tucker2d6665d2011-11-04 10:54:22 +11001867 if (!options.control_persist)
1868 ssh_init_stdio_forwarding();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001869 ssh_init_forwarding();
1870
Damien Millere11e1ea2010-08-03 16:04:46 +10001871 /* Start listening for multiplex clients */
1872 muxserver_listen();
1873
1874 /*
Darren Tucker2d6665d2011-11-04 10:54:22 +11001875 * If we are in control persist mode and have a working mux listen
1876 * socket, then prepare to background ourselves and have a foreground
1877 * client attach as a control slave.
1878 * NB. we must save copies of the flags that we override for
Damien Millere11e1ea2010-08-03 16:04:46 +10001879 * the backgrounding, since we defer attachment of the slave until
1880 * after the connection is fully established (in particular,
1881 * async rfwd replies have been received for ExitOnForwardFailure).
1882 */
1883 if (options.control_persist && muxserver_sock != -1) {
1884 ostdin_null_flag = stdin_null_flag;
1885 ono_shell_flag = no_shell_flag;
Damien Miller21771e22011-05-15 08:45:50 +10001886 orequest_tty = options.request_tty;
Damien Millere11e1ea2010-08-03 16:04:46 +10001887 otty_flag = tty_flag;
1888 stdin_null_flag = 1;
1889 no_shell_flag = 1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001890 tty_flag = 0;
1891 if (!fork_after_authentication_flag)
1892 need_controlpersist_detach = 1;
1893 fork_after_authentication_flag = 1;
1894 }
Darren Tucker2d6665d2011-11-04 10:54:22 +11001895 /*
1896 * ControlPersist mux listen socket setup failed, attempt the
1897 * stdio forward setup that we skipped earlier.
1898 */
1899 if (options.control_persist && muxserver_sock == -1)
1900 ssh_init_stdio_forwarding();
Damien Millere11e1ea2010-08-03 16:04:46 +10001901
Ben Lindstromf558cf62001-09-20 23:13:49 +00001902 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1903 id = ssh_session2_open();
Damien Miller649fe022013-07-18 16:13:55 +10001904 else {
1905 packet_set_interactive(
1906 options.control_master == SSHCTL_MASTER_NO,
1907 options.ip_qos_interactive, options.ip_qos_bulk);
1908 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001909
Darren Tucker8901fa92008-06-11 09:34:01 +10001910 /* If we don't expect to open a new session, then disallow it */
Damien Miller456e6f02008-11-03 19:20:10 +11001911 if (options.control_master == SSHCTL_MASTER_NO &&
1912 (datafellows & SSH_NEW_OPENSSH)) {
Darren Tucker8901fa92008-06-11 09:34:01 +10001913 debug("Requesting no-more-sessions@openssh.com");
1914 packet_start(SSH2_MSG_GLOBAL_REQUEST);
1915 packet_put_cstring("no-more-sessions@openssh.com");
1916 packet_put_char(0);
1917 packet_send();
1918 }
1919
Damien Millerd27b9472005-12-13 19:29:02 +11001920 /* Execute a local command */
1921 if (options.local_command != NULL &&
1922 options.permit_local_command)
1923 ssh_local_cmd(options.local_command);
1924
Damien Miller1f25ab42010-07-16 13:56:23 +10001925 /*
1926 * If requested and we are not interested in replies to remote
1927 * forwarding requests, then let ssh continue in the background.
1928 */
Damien Millere11e1ea2010-08-03 16:04:46 +10001929 if (fork_after_authentication_flag) {
1930 if (options.exit_on_forward_failure &&
1931 options.num_remote_forwards > 0) {
1932 debug("deferring postauth fork until remote forward "
1933 "confirmation received");
1934 } else
1935 fork_postauth();
Darren Tucker9a2a6092008-07-04 12:53:50 +10001936 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001937
Darren Tuckerf1de4e52010-01-08 16:54:59 +11001938 if (options.use_roaming)
1939 request_roaming();
1940
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001941 return client_loop(tty_flag, tty_flag ?
1942 options.escape_char : SSH_ESCAPECHAR_NONE, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001943}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001944
Ben Lindstrombba81212001-06-25 05:01:22 +00001945static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001946load_public_identity_files(void)
1947{
Damien Miller6b1d53c2006-03-31 23:13:21 +11001948 char *filename, *cp, thishost[NI_MAXHOST];
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001949 char *pwdir = NULL, *pwname = NULL;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001950 int i = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001951 Key *public;
Damien Miller6b1d53c2006-03-31 23:13:21 +11001952 struct passwd *pw;
Damien Miller0a80ca12010-02-27 07:55:05 +11001953 u_int n_ids;
1954 char *identity_files[SSH_MAX_IDENTITY_FILES];
1955 Key *identity_keys[SSH_MAX_IDENTITY_FILES];
Damien Miller7ea845e2010-02-12 09:21:02 +11001956#ifdef ENABLE_PKCS11
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001957 Key **keys;
Damien Miller7ea845e2010-02-12 09:21:02 +11001958 int nkeys;
Damien Miller0a80ca12010-02-27 07:55:05 +11001959#endif /* PKCS11 */
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001960
Damien Miller0a80ca12010-02-27 07:55:05 +11001961 n_ids = 0;
Damien Miller1d2c4562014-02-04 11:18:20 +11001962 memset(identity_files, 0, sizeof(identity_files));
1963 memset(identity_keys, 0, sizeof(identity_keys));
Damien Miller0a80ca12010-02-27 07:55:05 +11001964
1965#ifdef ENABLE_PKCS11
Damien Miller7ea845e2010-02-12 09:21:02 +11001966 if (options.pkcs11_provider != NULL &&
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001967 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
Damien Miller7ea845e2010-02-12 09:21:02 +11001968 (pkcs11_init(!options.batch_mode) == 0) &&
1969 (nkeys = pkcs11_add_provider(options.pkcs11_provider, NULL,
1970 &keys)) > 0) {
Damien Miller7ea845e2010-02-12 09:21:02 +11001971 for (i = 0; i < nkeys; i++) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001972 if (n_ids >= SSH_MAX_IDENTITY_FILES) {
1973 key_free(keys[i]);
1974 continue;
1975 }
1976 identity_keys[n_ids] = keys[i];
1977 identity_files[n_ids] =
Damien Miller7ea845e2010-02-12 09:21:02 +11001978 xstrdup(options.pkcs11_provider); /* XXX */
Damien Miller0a80ca12010-02-27 07:55:05 +11001979 n_ids++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001980 }
Darren Tuckera627d422013-06-02 07:31:17 +10001981 free(keys);
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001982 }
Damien Miller7ea845e2010-02-12 09:21:02 +11001983#endif /* ENABLE_PKCS11 */
Damien Miller6b1d53c2006-03-31 23:13:21 +11001984 if ((pw = getpwuid(original_real_uid)) == NULL)
1985 fatal("load_public_identity_files: getpwuid failed");
Darren Tuckere143f062007-12-02 23:21:16 +11001986 pwname = xstrdup(pw->pw_name);
1987 pwdir = xstrdup(pw->pw_dir);
Damien Miller6b1d53c2006-03-31 23:13:21 +11001988 if (gethostname(thishost, sizeof(thishost)) == -1)
1989 fatal("load_public_identity_files: gethostname: %s",
1990 strerror(errno));
Damien Miller0a80ca12010-02-27 07:55:05 +11001991 for (i = 0; i < options.num_identity_files; i++) {
Darren Tucker15fd19c2013-04-05 11:22:26 +11001992 if (n_ids >= SSH_MAX_IDENTITY_FILES ||
1993 strcasecmp(options.identity_files[i], "none") == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +10001994 free(options.identity_files[i]);
Damien Miller0a80ca12010-02-27 07:55:05 +11001995 continue;
1996 }
Damien Miller6b1d53c2006-03-31 23:13:21 +11001997 cp = tilde_expand_filename(options.identity_files[i],
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001998 original_real_uid);
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001999 filename = percent_expand(cp, "d", pwdir,
2000 "u", pwname, "l", thishost, "h", host,
Damien Miller6b1d53c2006-03-31 23:13:21 +11002001 "r", options.user, (char *)NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10002002 free(cp);
Ben Lindstromd0fca422001-03-26 13:44:06 +00002003 public = key_load_public(filename, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00002004 debug("identity file %s type %d", filename,
2005 public ? public->type : -1);
Darren Tuckera627d422013-06-02 07:31:17 +10002006 free(options.identity_files[i]);
Damien Miller0a80ca12010-02-27 07:55:05 +11002007 identity_files[n_ids] = filename;
2008 identity_keys[n_ids] = public;
2009
2010 if (++n_ids >= SSH_MAX_IDENTITY_FILES)
2011 continue;
2012
2013 /* Try to add the certificate variant too */
2014 xasprintf(&cp, "%s-cert", filename);
2015 public = key_load_public(cp, NULL);
2016 debug("identity file %s type %d", cp,
2017 public ? public->type : -1);
2018 if (public == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +10002019 free(cp);
Damien Miller0a80ca12010-02-27 07:55:05 +11002020 continue;
2021 }
2022 if (!key_is_cert(public)) {
2023 debug("%s: key %s type %s is not a certificate",
2024 __func__, cp, key_type(public));
2025 key_free(public);
Darren Tuckera627d422013-06-02 07:31:17 +10002026 free(cp);
Damien Miller0a80ca12010-02-27 07:55:05 +11002027 continue;
2028 }
2029 identity_keys[n_ids] = public;
2030 /* point to the original path, most likely the private key */
2031 identity_files[n_ids] = xstrdup(filename);
2032 n_ids++;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00002033 }
Damien Miller0a80ca12010-02-27 07:55:05 +11002034 options.num_identity_files = n_ids;
2035 memcpy(options.identity_files, identity_files, sizeof(identity_files));
2036 memcpy(options.identity_keys, identity_keys, sizeof(identity_keys));
2037
Damien Miller1d2c4562014-02-04 11:18:20 +11002038 explicit_bzero(pwname, strlen(pwname));
Darren Tuckera627d422013-06-02 07:31:17 +10002039 free(pwname);
Damien Miller1d2c4562014-02-04 11:18:20 +11002040 explicit_bzero(pwdir, strlen(pwdir));
Darren Tuckera627d422013-06-02 07:31:17 +10002041 free(pwdir);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00002042}
Damien Miller857b02e2010-09-24 22:02:56 +10002043
2044static void
2045main_sigchld_handler(int sig)
2046{
2047 int save_errno = errno;
2048 pid_t pid;
2049 int status;
2050
2051 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
2052 (pid < 0 && errno == EINTR))
2053 ;
2054
2055 signal(sig, main_sigchld_handler);
2056 errno = save_errno;
2057}