blob: 230591b3a1ad4d05401dfcc7c1d812c45c95efaa [file] [log] [blame]
Damien Miller0faf7472013-10-17 11:47:23 +11001/* $OpenBSD: ssh.c,v 1.385 2013/10/16 02:31:46 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>
Darren Tucker199b1342009-07-06 07:16:56 +100051#include <sys/param.h>
Damien Millere3b60b52006-07-10 21:08:03 +100052#include <sys/socket.h>
Damien Miller857b02e2010-09-24 22:02:56 +100053#include <sys/wait.h>
Damien Miller03e20032006-03-15 11:16:59 +110054
Damien Millerc7b06362006-03-15 11:53:45 +110055#include <ctype.h>
Darren Tuckerba724052006-07-12 22:24:22 +100056#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100057#include <fcntl.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100058#include <netdb.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110059#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110060#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110061#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100062#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110063#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100064#include <stdarg.h>
Damien Miller2d00e632006-07-24 13:53:19 +100065#include <stddef.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100066#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100067#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100068#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100069#include <unistd.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 Millereba71ba2000-04-29 23:57:08 +100074#include <openssl/evp.h>
Damien Miller0bc1bd82000-11-13 22:57:25 +110075#include <openssl/err.h>
Darren Tuckerbfaaf962008-02-28 19:13:52 +110076#include "openbsd-compat/openssl-compat.h"
Damien Millerb84886b2008-05-19 15:05:07 +100077#include "openbsd-compat/sys-queue.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078
Damien Millerd7834352006-08-05 12:39:39 +100079#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000081#include "ssh1.h"
Damien Miller1383bd82000-04-06 12:32:37 +100082#include "ssh2.h"
Damien Millerb96c4412010-07-02 13:34:24 +100083#include "canohost.h"
Damien Miller1383bd82000-04-06 12:32:37 +100084#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000085#include "cipher.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000086#include "packet.h"
87#include "buffer.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000088#include "channels.h"
Damien Millereba71ba2000-04-29 23:57:08 +100089#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100090#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100091#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000092#include "pathnames.h"
Damien Miller0e220db2004-06-15 10:34:08 +100093#include "dispatch.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +000094#include "clientloop.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000095#include "log.h"
96#include "readconf.h"
97#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000098#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000099#include "kex.h"
100#include "mac.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +1000101#include "sshpty.h"
Darren Tucker46bc0752004-05-02 22:11:30 +1000102#include "match.h"
Damien Miller0e220db2004-06-15 10:34:08 +1000103#include "msg.h"
Darren Tucker25f60a72004-08-15 17:23:34 +1000104#include "uidswap.h"
Darren Tuckerf1de4e52010-01-08 16:54:59 +1100105#include "roaming.h"
Damien Millerb7576772006-07-10 20:23:39 +1000106#include "version.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107
Damien Miller7ea845e2010-02-12 09:21:02 +1100108#ifdef ENABLE_PKCS11
109#include "ssh-pkcs11.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +0000110#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000111
Damien Miller3f905871999-11-15 17:10:57 +1100112extern char *__progname;
Damien Miller3f905871999-11-15 17:10:57 +1100113
Damien Millerea2c1a42011-06-03 12:10:22 +1000114/* Saves a copy of argv for setproctitle emulation */
115#ifndef HAVE_SETPROCTITLE
116static char **saved_av;
117#endif
118
Darren Tuckerd6173c02008-06-13 04:52:53 +1000119/* Flag indicating whether debug mode is on. May be set on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120int debug_flag = 0;
121
Damien Miller21771e22011-05-15 08:45:50 +1000122/* Flag indicating whether a tty should be requested */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123int tty_flag = 0;
124
Damien Miller1383bd82000-04-06 12:32:37 +1000125/* don't exec a shell */
126int no_shell_flag = 0;
Damien Miller1383bd82000-04-06 12:32:37 +1000127
Damien Miller5428f641999-11-25 11:54:57 +1100128/*
129 * Flag indicating that nothing should be read from stdin. This can be set
130 * on the command line.
131 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132int stdin_null_flag = 0;
133
Damien Miller5428f641999-11-25 11:54:57 +1100134/*
Damien Millere11e1ea2010-08-03 16:04:46 +1000135 * Flag indicating that the current process should be backgrounded and
136 * a new slave launched in the foreground for ControlPersist.
137 */
138int need_controlpersist_detach = 0;
139
140/* Copies of flags for ControlPersist foreground slave */
Damien Miller21771e22011-05-15 08:45:50 +1000141int ostdin_null_flag, ono_shell_flag, otty_flag, orequest_tty;
Damien Millere11e1ea2010-08-03 16:04:46 +1000142
143/*
Damien Miller5428f641999-11-25 11:54:57 +1100144 * Flag indicating that ssh should fork after authentication. This is useful
Ben Lindstromf666fec2002-06-06 19:51:58 +0000145 * so that the passphrase can be entered manually, and then ssh goes to the
Damien Miller5428f641999-11-25 11:54:57 +1100146 * background.
147 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148int fork_after_authentication_flag = 0;
149
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100150/* forward stdio to remote host and port */
151char *stdio_forward_host = NULL;
152int stdio_forward_port = 0;
153
Damien Miller5428f641999-11-25 11:54:57 +1100154/*
155 * General data structure for command line options and options configurable
156 * in configuration files. See readconf.h.
157 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158Options options;
159
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000160/* optional user configfile */
161char *config = NULL;
162
Damien Miller5428f641999-11-25 11:54:57 +1100163/*
164 * Name of the host we are connecting to. This is the name given on the
165 * command line, or the HostName specified for the user-supplied name in a
166 * configuration file.
167 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168char *host;
169
170/* socket address the host resolves to */
Damien Miller34132e52000-01-14 15:45:46 +1100171struct sockaddr_storage hostaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000172
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000173/* Private host keys. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000174Sensitive sensitive_data;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000175
176/* Original real UID. */
177uid_t original_real_uid;
Ben Lindstromf9c48842002-06-11 16:37:51 +0000178uid_t original_effective_uid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000179
Damien Miller1383bd82000-04-06 12:32:37 +1000180/* command to be executed */
181Buffer command;
182
Damien Miller832562e2001-01-30 09:30:01 +1100183/* Should we execute a command or invoke a subsystem? */
184int subsystem_flag = 0;
185
Damien Miller2797f7f2002-04-23 21:09:44 +1000186/* # of replies received for global requests */
Darren Tucker9f407c42008-06-13 04:50:27 +1000187static int remote_forward_confirms_received = 0;
Damien Miller2797f7f2002-04-23 21:09:44 +1000188
Damien Millerb1cbfa22008-05-19 16:00:08 +1000189/* mux.c */
190extern int muxserver_sock;
191extern u_int muxclient_command;
Damien Miller0e220db2004-06-15 10:34:08 +1000192
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000193/* Prints a help message to the user. This function never returns. */
194
Ben Lindstrombba81212001-06-25 05:01:22 +0000195static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000196usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000197{
Damien Miller50955102004-03-22 09:34:58 +1100198 fprintf(stderr,
Damien Millerc13c3ee2008-11-03 19:23:28 +1100199"usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]\n"
Damien Miller03d4d7e2013-04-23 15:21:06 +1000200" [-D [bind_address:]port] [-E log_file] [-e escape_char]\n"
201" [-F configfile] [-I pkcs11] [-i identity_file]\n"
Damien Miller34bd20a2013-04-23 19:25:00 +1000202" [-L [bind_address:]port:host:hostport] [-Q protocol_feature]\n"
Damien Millerf91ee4c2005-03-01 21:24:33 +1100203" [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100204" [-R [bind_address:]port:host:hostport] [-S ctl_path]\n"
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100205" [-W host:port] [-w local_tun[:remote_tun]]\n"
206" [user@]hostname [command]\n"
Damien Miller50955102004-03-22 09:34:58 +1100207 );
Darren Tuckere9a9b712005-12-20 16:15:51 +1100208 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209}
210
Ben Lindstrombba81212001-06-25 05:01:22 +0000211static int ssh_session(void);
212static int ssh_session2(void);
213static void load_public_identity_files(void);
Damien Miller857b02e2010-09-24 22:02:56 +1000214static void main_sigchld_handler(int);
Damien Millerb1cbfa22008-05-19 16:00:08 +1000215
216/* from muxclient.c */
217void muxclient(const char *);
218void muxserver_listen(void);
Damien Miller1383bd82000-04-06 12:32:37 +1000219
Damien Miller295ee632011-05-29 21:42:31 +1000220/* ~/ expand a list of paths. NB. assumes path[n] is heap-allocated. */
221static void
222tilde_expand_paths(char **paths, u_int num_paths)
223{
224 u_int i;
225 char *cp;
226
227 for (i = 0; i < num_paths; i++) {
228 cp = tilde_expand_filename(paths[i], original_real_uid);
Darren Tuckera627d422013-06-02 07:31:17 +1000229 free(paths[i]);
Damien Miller295ee632011-05-29 21:42:31 +1000230 paths[i] = cp;
231 }
232}
233
Damien Miller0faf7472013-10-17 11:47:23 +1100234static struct addrinfo *
235resolve_host(const char *name, u_int port, int logerr, char *cname, size_t clen)
236{
237 char strport[NI_MAXSERV];
238 struct addrinfo hints, *res;
239 int gaierr, loglevel = SYSLOG_LEVEL_DEBUG1;
240
241 snprintf(strport, sizeof strport, "%u", port);
242 bzero(&hints, sizeof(hints));
243 hints.ai_family = options.address_family;
244 hints.ai_socktype = SOCK_STREAM;
245 if (cname != NULL)
246 hints.ai_flags = AI_CANONNAME;
247 if ((gaierr = getaddrinfo(name, strport, &hints, &res)) != 0) {
248 if (logerr || (gaierr != EAI_NONAME && gaierr != EAI_NODATA))
249 loglevel = SYSLOG_LEVEL_ERROR;
250 do_log2(loglevel, "%s: Could not resolve hostname %.100s: %s",
251 __progname, name, ssh_gai_strerror(gaierr));
252 return NULL;
253 }
254 if (cname != NULL && res->ai_canonname != NULL) {
255 if (strlcpy(cname, res->ai_canonname, clen) >= clen) {
256 error("%s: host \"%s\" cname \"%s\" too long (max %lu)",
257 __func__, name, res->ai_canonname, (u_long)clen);
258 if (clen > 0)
259 *cname = '\0';
260 }
261 }
262 return res;
263}
264
265/*
266 * Check whether the cname is a permitted replacement for the hostname
267 * and perform the replacement if it is.
268 */
269static int
270check_follow_cname(char **namep, const char *cname)
271{
272 int i;
273 struct allowed_cname *rule;
274
275 if (*cname == '\0' || options.num_permitted_cnames == 0 ||
276 strcmp(*namep, cname) == 0)
277 return 0;
278 if (options.canonicalise_hostname == SSH_CANONICALISE_NO)
279 return 0;
280 /*
281 * Don't attempt to canonicalise names that will be interpreted by
282 * a proxy unless the user specifically requests so.
283 */
284 if (options.proxy_command != NULL &&
285 options.canonicalise_hostname != SSH_CANONICALISE_ALWAYS)
286 return 0;
287 debug3("%s: check \"%s\" CNAME \"%s\"", __func__, *namep, cname);
288 for (i = 0; i < options.num_permitted_cnames; i++) {
289 rule = options.permitted_cnames + i;
290 if (match_pattern_list(*namep, rule->source_list,
291 strlen(rule->source_list), 1) != 1 ||
292 match_pattern_list(cname, rule->target_list,
293 strlen(rule->target_list), 1) != 1)
294 continue;
295 verbose("Canonicalised DNS aliased hostname "
296 "\"%s\" => \"%s\"", *namep, cname);
297 free(*namep);
298 *namep = xstrdup(cname);
299 return 1;
300 }
301 return 0;
302}
303
304/*
305 * Attempt to resolve the supplied hostname after applying the user's
306 * canonicalisation rules. Returns the address list for the host or NULL
307 * if no name was found after canonicalisation.
308 */
309static struct addrinfo *
310resolve_canonicalise(char **hostp, u_int port)
311{
312 int i, ndots;
313 char *cp, *fullhost, cname_target[NI_MAXHOST];
314 struct addrinfo *addrs;
315
316 if (options.canonicalise_hostname == SSH_CANONICALISE_NO)
317 return NULL;
318 /*
319 * Don't attempt to canonicalise names that will be interpreted by
320 * a proxy unless the user specifically requests so.
321 */
322 if (options.proxy_command != NULL &&
323 options.canonicalise_hostname != SSH_CANONICALISE_ALWAYS)
324 return NULL;
325 /* Don't apply canonicalisation to sufficiently-qualified hostnames */
326 ndots = 0;
327 for (cp = *hostp; *cp != '\0'; cp++) {
328 if (*cp == '.')
329 ndots++;
330 }
331 if (ndots > options.canonicalise_max_dots) {
332 debug3("%s: not canonicalising hostname \"%s\" (max dots %d)",
333 __func__, *hostp, options.canonicalise_max_dots);
334 return NULL;
335 }
336 /* Attempt each supplied suffix */
337 for (i = 0; i < options.num_canonical_domains; i++) {
338 *cname_target = '\0';
339 xasprintf(&fullhost, "%s.%s.", *hostp,
340 options.canonical_domains[i]);
341 if ((addrs = resolve_host(fullhost, options.port, 0,
342 cname_target, sizeof(cname_target))) == NULL) {
343 free(fullhost);
344 continue;
345 }
346 /* Remove trailing '.' */
347 fullhost[strlen(fullhost) - 1] = '\0';
348 /* Follow CNAME if requested */
349 if (!check_follow_cname(&fullhost, cname_target)) {
350 debug("Canonicalised hostname \"%s\" => \"%s\"",
351 *hostp, fullhost);
352 }
353 free(*hostp);
354 *hostp = fullhost;
355 return addrs;
356 }
357 if (!options.canonicalise_fallback_local)
358 fatal("%s: Could not resolve host \"%s\"", __progname, host);
359 return NULL;
360}
361
Damien Miller95def091999-11-25 00:26:21 +1100362/*
363 * Main program for the ssh client.
364 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000365int
366main(int ac, char **av)
367{
Darren Tucker199b1342009-07-06 07:16:56 +1000368 int i, r, opt, exit_status, use_syslog;
Damien Miller03d4d7e2013-04-23 15:21:06 +1000369 char *p, *cp, *line, *argv0, buf[MAXPATHLEN], *host_arg, *logfile;
Damien Millerdfc85fa2011-05-15 08:44:02 +1000370 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
Damien Miller0faf7472013-10-17 11:47:23 +1100371 char cname[NI_MAXHOST];
Damien Miller95def091999-11-25 00:26:21 +1100372 struct stat st;
Ben Lindstrom086cf212001-03-05 05:56:40 +0000373 struct passwd *pw;
Damien Miller194fd902013-10-15 12:13:05 +1100374 int timeout_ms;
Ben Lindstrom5ccf63a2001-09-24 20:00:10 +0000375 extern int optind, optreset;
Damien Miller4f8e6692001-07-14 13:22:53 +1000376 extern char *optarg;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100377 Forward fwd;
Damien Miller0faf7472013-10-17 11:47:23 +1100378 struct addrinfo *addrs = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000379
Darren Tuckerce321d82005-10-03 18:11:24 +1000380 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
381 sanitise_stdfd();
382
Damien Miller59d3d5b2003-08-22 09:34:41 +1000383 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000384
Damien Millerea2c1a42011-06-03 12:10:22 +1000385#ifndef HAVE_SETPROCTITLE
386 /* Prepare for later setproctitle emulation */
387 /* Save argv so it isn't clobbered by setproctitle() emulation */
388 saved_av = xcalloc(ac + 1, sizeof(*saved_av));
389 for (i = 0; i < ac; i++)
390 saved_av[i] = xstrdup(av[i]);
391 saved_av[i] = NULL;
392 compat_init_setproctitle(ac, av);
393 av = saved_av;
394#endif
395
Damien Miller5428f641999-11-25 11:54:57 +1100396 /*
Damien Miller00d9ae22010-08-17 01:59:31 +1000397 * Discard other fds that are hanging around. These can cause problem
398 * with backgrounded ssh processes started by ControlPersist.
399 */
400 closefrom(STDERR_FILENO + 1);
401
402 /*
Damien Miller5428f641999-11-25 11:54:57 +1100403 * Save the original real uid. It will be needed later (uid-swapping
404 * may clobber the real uid).
405 */
Damien Miller95def091999-11-25 00:26:21 +1100406 original_real_uid = getuid();
407 original_effective_uid = geteuid();
Damien Millera8e06ce2003-11-21 23:48:55 +1100408
Damien Miller50b9a602002-09-04 16:50:06 +1000409 /*
410 * Use uid-swapping to give up root privileges for the duration of
411 * option processing. We will re-instantiate the rights when we are
412 * ready to create the privileged port, and will permanently drop
413 * them when the port has been created (actually, when the connection
414 * has been made, as we may need to create the port several times).
415 */
416 PRIV_END;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000417
Damien Miller05dd7952000-10-01 00:42:48 +1100418#ifdef HAVE_SETRLIMIT
Damien Miller95def091999-11-25 00:26:21 +1100419 /* If we are installed setuid root be careful to not drop core. */
420 if (original_real_uid != original_effective_uid) {
421 struct rlimit rlim;
422 rlim.rlim_cur = rlim.rlim_max = 0;
423 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
424 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000425 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100426#endif
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000427 /* Get user data. */
428 pw = getpwuid(original_real_uid);
429 if (!pw) {
Damien Miller3009d3c2013-07-20 13:22:31 +1000430 logit("No user exists for uid %lu", (u_long)original_real_uid);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100431 exit(255);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000432 }
433 /* Take a copy of the returned structure. */
434 pw = pwcopy(pw);
435
Damien Miller5428f641999-11-25 11:54:57 +1100436 /*
Damien Miller5428f641999-11-25 11:54:57 +1100437 * Set our umask to something reasonable, as some files are created
438 * with the default umask. This will make them world-readable but
439 * writable only by the owner, which is ok for all files for which we
440 * don't set the modes explicitly.
441 */
Damien Miller95def091999-11-25 00:26:21 +1100442 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000443
Darren Tuckerd6173c02008-06-13 04:52:53 +1000444 /*
445 * Initialize option structure to indicate that no values have been
446 * set.
447 */
Damien Miller95def091999-11-25 00:26:21 +1100448 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000449
Damien Miller95def091999-11-25 00:26:21 +1100450 /* Parse command-line arguments. */
451 host = NULL;
Damien Millere272a5b2008-11-03 19:22:37 +1100452 use_syslog = 0;
Damien Miller03d4d7e2013-04-23 15:21:06 +1000453 logfile = NULL;
Darren Tucker72efd742009-06-21 17:48:00 +1000454 argv0 = av[0];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000455
Damien Miller2ecb6bd2006-03-15 12:03:53 +1100456 again:
Darren Tuckerd6173c02008-06-13 04:52:53 +1000457 while ((opt = getopt(ac, av, "1246ab:c:e:fgi:kl:m:no:p:qstvx"
Damien Millerea111192013-04-23 19:24:32 +1000458 "ACD:E:F:I:KL:MNO:PQ:R:S:TVw:W:XYy")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100459 switch (opt) {
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000460 case '1':
461 options.protocol = SSH_PROTO_1;
462 break;
Damien Miller4af51302000-04-16 11:18:38 +1000463 case '2':
464 options.protocol = SSH_PROTO_2;
465 break;
Damien Miller34132e52000-01-14 15:45:46 +1100466 case '4':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000467 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100468 break;
Damien Miller34132e52000-01-14 15:45:46 +1100469 case '6':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000470 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100471 break;
Damien Miller95def091999-11-25 00:26:21 +1100472 case 'n':
473 stdin_null_flag = 1;
474 break;
Damien Miller95def091999-11-25 00:26:21 +1100475 case 'f':
476 fork_after_authentication_flag = 1;
477 stdin_null_flag = 1;
478 break;
Damien Miller95def091999-11-25 00:26:21 +1100479 case 'x':
480 options.forward_x11 = 0;
481 break;
Damien Miller95def091999-11-25 00:26:21 +1100482 case 'X':
483 options.forward_x11 = 1;
484 break;
Damien Millere272a5b2008-11-03 19:22:37 +1100485 case 'y':
486 use_syslog = 1;
487 break;
Damien Miller03d4d7e2013-04-23 15:21:06 +1000488 case 'E':
489 logfile = xstrdup(optarg);
490 break;
Darren Tucker0a118da2003-10-15 15:54:32 +1000491 case 'Y':
492 options.forward_x11 = 1;
493 options.forward_x11_trusted = 1;
494 break;
Damien Miller95def091999-11-25 00:26:21 +1100495 case 'g':
496 options.gateway_ports = 1;
497 break;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100498 case 'O':
Damien Millere1537f92010-01-26 13:26:22 +1100499 if (stdio_forward_host != NULL)
500 fatal("Cannot specify multiplexing "
501 "command with -W");
502 else if (muxclient_command != 0)
503 fatal("Multiplexing command already specified");
Darren Tucker7ebfc102004-11-07 20:06:19 +1100504 if (strcmp(optarg, "check") == 0)
Damien Millerb1cbfa22008-05-19 16:00:08 +1000505 muxclient_command = SSHMUX_COMMAND_ALIVE_CHECK;
Damien Miller388f6fc2010-05-21 14:57:35 +1000506 else if (strcmp(optarg, "forward") == 0)
507 muxclient_command = SSHMUX_COMMAND_FORWARD;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100508 else if (strcmp(optarg, "exit") == 0)
Damien Millerb1cbfa22008-05-19 16:00:08 +1000509 muxclient_command = SSHMUX_COMMAND_TERMINATE;
Damien Miller6c3eec72011-05-05 14:16:22 +1000510 else if (strcmp(optarg, "stop") == 0)
511 muxclient_command = SSHMUX_COMMAND_STOP;
Damien Millerf6dff7c2011-09-22 21:38:52 +1000512 else if (strcmp(optarg, "cancel") == 0)
513 muxclient_command = SSHMUX_COMMAND_CANCEL_FWD;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100514 else
515 fatal("Invalid multiplex command.");
516 break;
Damien Miller147bba32002-09-04 16:46:06 +1000517 case 'P': /* deprecated */
Damien Miller95def091999-11-25 00:26:21 +1100518 options.use_privileged_port = 0;
519 break;
Damien Millerea111192013-04-23 19:24:32 +1000520 case 'Q': /* deprecated */
521 cp = NULL;
522 if (strcasecmp(optarg, "cipher") == 0)
523 cp = cipher_alg_list();
524 else if (strcasecmp(optarg, "mac") == 0)
525 cp = mac_alg_list();
526 else if (strcasecmp(optarg, "kex") == 0)
527 cp = kex_alg_list();
528 else if (strcasecmp(optarg, "key") == 0)
529 cp = key_alg_list();
530 if (cp == NULL)
531 fatal("Unsupported query \"%s\"", optarg);
532 printf("%s\n", cp);
533 free(cp);
534 exit(0);
535 break;
Damien Miller95def091999-11-25 00:26:21 +1100536 case 'a':
537 options.forward_agent = 0;
538 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000539 case 'A':
540 options.forward_agent = 1;
541 break;
Damien Miller95def091999-11-25 00:26:21 +1100542 case 'k':
Damien Millere0113cc2003-11-24 13:10:09 +1100543 options.gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100544 break;
Darren Tucker415bddc2007-06-12 23:43:16 +1000545 case 'K':
546 options.gss_authentication = 1;
547 options.gss_deleg_creds = 1;
548 break;
Damien Miller95def091999-11-25 00:26:21 +1100549 case 'i':
550 if (stat(optarg, &st) < 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000551 fprintf(stderr, "Warning: Identity file %s "
Damien Miller3eb48b62005-03-01 21:15:46 +1100552 "not accessible: %s.\n", optarg,
553 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100554 break;
555 }
Darren Tucker19104782013-04-05 11:13:08 +1100556 add_identity_file(&options, NULL, optarg, 1);
Damien Miller95def091999-11-25 00:26:21 +1100557 break;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000558 case 'I':
Damien Miller7ea845e2010-02-12 09:21:02 +1100559#ifdef ENABLE_PKCS11
560 options.pkcs11_provider = xstrdup(optarg);
Ben Lindstrombcc18082001-08-06 21:59:25 +0000561#else
Damien Miller7ea845e2010-02-12 09:21:02 +1100562 fprintf(stderr, "no support for PKCS#11.\n");
Ben Lindstrombcc18082001-08-06 21:59:25 +0000563#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000564 break;
Damien Miller95def091999-11-25 00:26:21 +1100565 case 't':
Damien Miller21771e22011-05-15 08:45:50 +1000566 if (options.request_tty == REQUEST_TTY_YES)
567 options.request_tty = REQUEST_TTY_FORCE;
568 else
569 options.request_tty = REQUEST_TTY_YES;
Damien Miller95def091999-11-25 00:26:21 +1100570 break;
Damien Miller95def091999-11-25 00:26:21 +1100571 case 'v':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000572 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100573 debug_flag = 1;
574 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000575 } else {
576 if (options.log_level < SYSLOG_LEVEL_DEBUG3)
577 options.log_level++;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000578 }
Damien Miller03d4d7e2013-04-23 15:21:06 +1000579 break;
Damien Miller95def091999-11-25 00:26:21 +1100580 case 'V':
Damien Miller0c889cd2004-03-22 09:36:00 +1100581 fprintf(stderr, "%s, %s\n",
Damien Miller2aa6d3c2004-09-12 16:53:04 +1000582 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
Damien Miller95def091999-11-25 00:26:21 +1100583 if (opt == 'V')
584 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100585 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100586 case 'w':
Damien Miller7b58e802005-12-13 19:33:19 +1100587 if (options.tun_open == -1)
588 options.tun_open = SSH_TUNMODE_DEFAULT;
Damien Millerd27b9472005-12-13 19:29:02 +1100589 options.tun_local = a2tun(optarg, &options.tun_remote);
Damien Miller7b58e802005-12-13 19:33:19 +1100590 if (options.tun_local == SSH_TUNID_ERR) {
Darren Tuckerd6173c02008-06-13 04:52:53 +1000591 fprintf(stderr,
592 "Bad tun device '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100593 exit(255);
Damien Millerd27b9472005-12-13 19:29:02 +1100594 }
595 break;
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100596 case 'W':
Damien Millere1537f92010-01-26 13:26:22 +1100597 if (stdio_forward_host != NULL)
598 fatal("stdio forward already specified");
599 if (muxclient_command != 0)
600 fatal("Cannot specify stdio forward with -O");
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100601 if (parse_forward(&fwd, optarg, 1, 0)) {
602 stdio_forward_host = fwd.listen_host;
603 stdio_forward_port = fwd.listen_port;
Darren Tuckera627d422013-06-02 07:31:17 +1000604 free(fwd.connect_host);
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100605 } else {
606 fprintf(stderr,
607 "Bad stdio forwarding specification '%s'\n",
608 optarg);
609 exit(255);
610 }
Damien Miller21771e22011-05-15 08:45:50 +1000611 options.request_tty = REQUEST_TTY_NO;
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100612 no_shell_flag = 1;
613 options.clear_forwardings = 1;
614 options.exit_on_forward_failure = 1;
615 break;
Damien Miller95def091999-11-25 00:26:21 +1100616 case 'q':
617 options.log_level = SYSLOG_LEVEL_QUIET;
618 break;
Damien Miller95def091999-11-25 00:26:21 +1100619 case 'e':
620 if (optarg[0] == '^' && optarg[2] == 0 &&
Damien Millerf4614452001-07-14 12:18:10 +1000621 (u_char) optarg[1] >= 64 &&
622 (u_char) optarg[1] < 128)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000623 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100624 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000625 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100626 else if (strcmp(optarg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000627 options.escape_char = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100628 else {
Damien Millerf4614452001-07-14 12:18:10 +1000629 fprintf(stderr, "Bad escape character '%s'.\n",
630 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100631 exit(255);
Damien Miller95def091999-11-25 00:26:21 +1100632 }
633 break;
Damien Miller95def091999-11-25 00:26:21 +1100634 case 'c':
Damien Millereba71ba2000-04-29 23:57:08 +1000635 if (ciphers_valid(optarg)) {
636 /* SSH2 only */
637 options.ciphers = xstrdup(optarg);
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000638 options.cipher = SSH_CIPHER_INVALID;
Damien Millereba71ba2000-04-29 23:57:08 +1000639 } else {
640 /* SSH1 only */
Damien Millere39cacc2000-11-29 12:18:44 +1100641 options.cipher = cipher_number(optarg);
642 if (options.cipher == -1) {
Damien Millerf4614452001-07-14 12:18:10 +1000643 fprintf(stderr,
644 "Unknown cipher type '%s'\n",
645 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100646 exit(255);
Damien Millereba71ba2000-04-29 23:57:08 +1000647 }
Damien Millerf4614452001-07-14 12:18:10 +1000648 if (options.cipher == SSH_CIPHER_3DES)
Damien Millere39cacc2000-11-29 12:18:44 +1100649 options.ciphers = "3des-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000650 else if (options.cipher == SSH_CIPHER_BLOWFISH)
Damien Millere39cacc2000-11-29 12:18:44 +1100651 options.ciphers = "blowfish-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000652 else
Damien Millere39cacc2000-11-29 12:18:44 +1100653 options.ciphers = (char *)-1;
Damien Miller95def091999-11-25 00:26:21 +1100654 }
655 break;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000656 case 'm':
657 if (mac_valid(optarg))
658 options.macs = xstrdup(optarg);
659 else {
Damien Millerf4614452001-07-14 12:18:10 +1000660 fprintf(stderr, "Unknown mac type '%s'\n",
661 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100662 exit(255);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000663 }
664 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000665 case 'M':
Damien Millerd14b1e72005-06-16 13:19:41 +1000666 if (options.control_master == SSHCTL_MASTER_YES)
667 options.control_master = SSHCTL_MASTER_ASK;
668 else
669 options.control_master = SSHCTL_MASTER_YES;
Damien Miller0e220db2004-06-15 10:34:08 +1000670 break;
Damien Miller95def091999-11-25 00:26:21 +1100671 case 'p':
Ben Lindstrom19066a12001-04-12 23:39:26 +0000672 options.port = a2port(optarg);
Damien Miller3dc71ad2009-01-28 16:31:22 +1100673 if (options.port <= 0) {
Ben Lindstrom146edb92001-04-11 23:06:28 +0000674 fprintf(stderr, "Bad port '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100675 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000676 }
Damien Miller95def091999-11-25 00:26:21 +1100677 break;
Damien Miller95def091999-11-25 00:26:21 +1100678 case 'l':
679 options.user = optarg;
680 break;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000681
Damien Miller95def091999-11-25 00:26:21 +1100682 case 'L':
Damien Miller4bf648f2009-02-14 16:28:21 +1100683 if (parse_forward(&fwd, optarg, 0, 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +1100684 add_local_forward(&options, &fwd);
685 else {
Damien Millerf4614452001-07-14 12:18:10 +1000686 fprintf(stderr,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100687 "Bad local forwarding specification '%s'\n",
Damien Millerf4614452001-07-14 12:18:10 +1000688 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100689 exit(255);
Ben Lindstrom1a174712001-09-12 17:56:15 +0000690 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100691 break;
692
693 case 'R':
Damien Miller4bf648f2009-02-14 16:28:21 +1100694 if (parse_forward(&fwd, optarg, 0, 1)) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100695 add_remote_forward(&options, &fwd);
696 } else {
697 fprintf(stderr,
698 "Bad remote forwarding specification "
699 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100700 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100701 }
Damien Miller95def091999-11-25 00:26:21 +1100702 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000703
704 case 'D':
Damien Miller4bf648f2009-02-14 16:28:21 +1100705 if (parse_forward(&fwd, optarg, 1, 0)) {
Damien Millera699d952008-11-03 19:27:34 +1100706 add_local_forward(&options, &fwd);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100707 } else {
Damien Millera699d952008-11-03 19:27:34 +1100708 fprintf(stderr,
709 "Bad dynamic forwarding specification "
710 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100711 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000712 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000713 break;
714
Damien Miller95def091999-11-25 00:26:21 +1100715 case 'C':
716 options.compression = 1;
717 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000718 case 'N':
719 no_shell_flag = 1;
Damien Miller21771e22011-05-15 08:45:50 +1000720 options.request_tty = REQUEST_TTY_NO;
Damien Miller1383bd82000-04-06 12:32:37 +1000721 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000722 case 'T':
Damien Miller21771e22011-05-15 08:45:50 +1000723 options.request_tty = REQUEST_TTY_NO;
Damien Miller1383bd82000-04-06 12:32:37 +1000724 break;
Damien Miller95def091999-11-25 00:26:21 +1100725 case 'o':
Damien Miller9836cf82003-12-17 16:30:06 +1100726 line = xstrdup(optarg);
Damien Miller194fd902013-10-15 12:13:05 +1100727 if (process_config_line(&options, pw, host ? host : "",
728 line, "command-line", 0, NULL, SSHCONF_USERCONF)
Darren Tuckeraefa3682013-04-05 11:18:35 +1100729 != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100730 exit(255);
Darren Tuckera627d422013-06-02 07:31:17 +1000731 free(line);
Damien Miller95def091999-11-25 00:26:21 +1100732 break;
Damien Miller832562e2001-01-30 09:30:01 +1100733 case 's':
734 subsystem_flag = 1;
735 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000736 case 'S':
737 if (options.control_path != NULL)
738 free(options.control_path);
739 options.control_path = xstrdup(optarg);
Damien Miller0e220db2004-06-15 10:34:08 +1000740 break;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000741 case 'b':
742 options.bind_address = optarg;
743 break;
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000744 case 'F':
745 config = optarg;
746 break;
Damien Miller95def091999-11-25 00:26:21 +1100747 default:
748 usage();
749 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000750 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000751
Damien Millerf4614452001-07-14 12:18:10 +1000752 ac -= optind;
753 av += optind;
754
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100755 if (ac > 0 && !host) {
Ben Lindstromc276c122002-12-23 02:14:51 +0000756 if (strrchr(*av, '@')) {
Damien Millerf4614452001-07-14 12:18:10 +1000757 p = xstrdup(*av);
Ben Lindstromc276c122002-12-23 02:14:51 +0000758 cp = strrchr(p, '@');
Damien Millerf4614452001-07-14 12:18:10 +1000759 if (cp == NULL || cp == p)
760 usage();
761 options.user = p;
762 *cp = '\0';
Damien Miller0faf7472013-10-17 11:47:23 +1100763 host = xstrdup(++cp);
Damien Millerf4614452001-07-14 12:18:10 +1000764 } else
Damien Miller0faf7472013-10-17 11:47:23 +1100765 host = xstrdup(*av);
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000766 if (ac > 1) {
767 optind = optreset = 1;
Damien Millerf4614452001-07-14 12:18:10 +1000768 goto again;
769 }
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000770 ac--, av++;
Damien Millerf4614452001-07-14 12:18:10 +1000771 }
772
Damien Miller95def091999-11-25 00:26:21 +1100773 /* Check that we got a host name. */
774 if (!host)
775 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000776
Damien Miller0faf7472013-10-17 11:47:23 +1100777 lowercase(host);
778 host_arg = xstrdup(host);
779
Damien Miller4314c2b2010-09-10 11:12:09 +1000780 OpenSSL_add_all_algorithms();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100781 ERR_load_crypto_strings();
Damien Millercb5e44a2000-09-29 12:12:36 +1100782
Damien Miller95def091999-11-25 00:26:21 +1100783 /* Initialize the command to execute on remote host. */
784 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000785
Damien Miller5428f641999-11-25 11:54:57 +1100786 /*
787 * Save the command to execute on the remote host in a buffer. There
788 * is no limit on the length of the command, except by the maximum
789 * packet size. Also sets the tty flag if there is no command.
790 */
Damien Millerf4614452001-07-14 12:18:10 +1000791 if (!ac) {
Damien Miller95def091999-11-25 00:26:21 +1100792 /* No command specified - execute shell on a tty. */
Damien Miller832562e2001-01-30 09:30:01 +1100793 if (subsystem_flag) {
Damien Millerf4614452001-07-14 12:18:10 +1000794 fprintf(stderr,
795 "You must specify a subsystem to invoke.\n");
Damien Miller832562e2001-01-30 09:30:01 +1100796 usage();
797 }
Damien Miller95def091999-11-25 00:26:21 +1100798 } else {
Damien Millerf4614452001-07-14 12:18:10 +1000799 /* A command has been specified. Store it into the buffer. */
800 for (i = 0; i < ac; i++) {
801 if (i)
Damien Miller95def091999-11-25 00:26:21 +1100802 buffer_append(&command, " ", 1);
803 buffer_append(&command, av[i], strlen(av[i]));
804 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000805 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000806
Damien Miller95def091999-11-25 00:26:21 +1100807 /* Cannot fork to background if no command. */
Darren Tuckerd6173c02008-06-13 04:52:53 +1000808 if (fork_after_authentication_flag && buffer_len(&command) == 0 &&
809 !no_shell_flag)
810 fatal("Cannot fork into background without a command "
811 "to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000812
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000813 /*
814 * Initialize "log" output. Since we are the client all output
Damien Miller03d4d7e2013-04-23 15:21:06 +1000815 * goes to stderr unless otherwise specified by -y or -E.
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000816 */
Damien Miller03d4d7e2013-04-23 15:21:06 +1000817 if (use_syslog && logfile != NULL)
818 fatal("Can't specify both -y and -E");
819 if (logfile != NULL) {
820 log_redirect_stderr_to(logfile);
Darren Tuckera627d422013-06-02 07:31:17 +1000821 free(logfile);
Damien Miller03d4d7e2013-04-23 15:21:06 +1000822 }
Darren Tucker72efd742009-06-21 17:48:00 +1000823 log_init(argv0,
Darren Tuckerd6173c02008-06-13 04:52:53 +1000824 options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
Damien Millere272a5b2008-11-03 19:22:37 +1100825 SYSLOG_FACILITY_USER, !use_syslog);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000826
Damien Miller03d4d7e2013-04-23 15:21:06 +1000827 if (debug_flag)
828 logit("%s, %s", SSH_VERSION, SSLeay_version(SSLEAY_VERSION));
829
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000830 /*
831 * Read per-user configuration file. Ignore the system wide config
832 * file if the user specifies a config file on the command line.
833 */
834 if (config != NULL) {
Damien Miller508b6c32013-04-23 15:18:28 +1000835 if (strcasecmp(config, "none") != 0 &&
Damien Miller194fd902013-10-15 12:13:05 +1100836 !read_config_file(config, pw, host, &options,
837 SSHCONF_USERCONF))
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000838 fatal("Can't open user config file %.100s: "
839 "%.100s", config, strerror(errno));
Damien Miller80163902007-01-05 16:30:16 +1100840 } else {
Darren Tucker199b1342009-07-06 07:16:56 +1000841 r = snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir,
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000842 _PATH_SSH_USER_CONFFILE);
Darren Tucker199b1342009-07-06 07:16:56 +1000843 if (r > 0 && (size_t)r < sizeof(buf))
Damien Miller194fd902013-10-15 12:13:05 +1100844 (void)read_config_file(buf, pw, host, &options,
Darren Tuckeraefa3682013-04-05 11:18:35 +1100845 SSHCONF_CHECKPERM|SSHCONF_USERCONF);
Ben Lindstrom83f07d12001-10-03 17:22:29 +0000846
Damien Miller765f8c42011-08-06 06:18:16 +1000847 /* Read systemwide configuration file after user config. */
Damien Miller194fd902013-10-15 12:13:05 +1100848 (void)read_config_file(_PATH_HOST_CONFIG_FILE, pw, host,
Damien Miller57a44762004-04-20 20:11:57 +1000849 &options, 0);
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000850 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000851
Damien Miller95def091999-11-25 00:26:21 +1100852 /* Fill configuration defaults. */
853 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000854
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000855 channel_set_af(options.address_family);
856
Damien Millere9fc72e2013-10-15 12:14:12 +1100857 /* Tidy and check options */
858 if (options.host_key_alias != NULL)
859 lowercase(options.host_key_alias);
860 if (options.proxy_command != NULL &&
861 strcmp(options.proxy_command, "-") == 0 &&
862 options.proxy_use_fdpass)
863 fatal("ProxyCommand=- and ProxyUseFDPass are incompatible");
Damien Miller0faf7472013-10-17 11:47:23 +1100864#ifndef HAVE_CYGWIN
865 if (original_effective_uid != 0)
866 options.use_privileged_port = 0;
867#endif
Damien Millere9fc72e2013-10-15 12:14:12 +1100868
Damien Miller95def091999-11-25 00:26:21 +1100869 /* reinit */
Darren Tucker72efd742009-06-21 17:48:00 +1000870 log_init(argv0, options.log_level, SYSLOG_FACILITY_USER, !use_syslog);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000871
Damien Millerfff9f092012-07-06 13:45:01 +1000872 if (options.request_tty == REQUEST_TTY_YES ||
873 options.request_tty == REQUEST_TTY_FORCE)
874 tty_flag = 1;
875
876 /* Allocate a tty by default if no command specified. */
877 if (buffer_len(&command) == 0)
878 tty_flag = options.request_tty != REQUEST_TTY_NO;
879
880 /* Force no tty */
881 if (options.request_tty == REQUEST_TTY_NO || muxclient_command != 0)
882 tty_flag = 0;
883 /* Do not allocate a tty if stdin is not a tty. */
884 if ((!isatty(fileno(stdin)) || stdin_null_flag) &&
885 options.request_tty != REQUEST_TTY_FORCE) {
886 if (tty_flag)
887 logit("Pseudo-terminal will not be allocated because "
888 "stdin is not a terminal.");
889 tty_flag = 0;
890 }
891
Damien Miller60bc5172001-03-19 09:38:15 +1100892 seed_rng();
893
Damien Miller95def091999-11-25 00:26:21 +1100894 if (options.user == NULL)
895 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000896
Darren Tuckerf6b01b72008-06-13 04:56:37 +1000897 /* Get default port if port has not been set. */
Damien Miller194fd902013-10-15 12:13:05 +1100898 if (options.port == 0)
899 options.port = default_ssh_port();
Darren Tuckerf6b01b72008-06-13 04:56:37 +1000900
Damien Miller83f8a402011-01-07 09:51:17 +1100901 /* preserve host name given on command line for %n expansion */
Damien Millerd0244d42010-07-16 13:56:43 +1000902 if (options.hostname != NULL) {
Damien Miller0faf7472013-10-17 11:47:23 +1100903 cp = percent_expand(options.hostname,
Damien Millerd0244d42010-07-16 13:56:43 +1000904 "h", host, (char *)NULL);
Damien Miller0faf7472013-10-17 11:47:23 +1100905 free(host);
906 host = cp;
907 }
908
909 /* If canonicalisation requested then try to apply it */
910 if (options.canonicalise_hostname != SSH_CANONICALISE_NO)
911 addrs = resolve_canonicalise(&host, options.port);
912 /*
913 * If canonicalisation not requested, or if it failed then try to
914 * resolve the bare hostname name using the system resolver's usual
915 * search rules.
916 */
917 if (addrs == NULL) {
918 if ((addrs = resolve_host(host, options.port, 1,
919 cname, sizeof(cname))) == NULL)
920 cleanup_exit(255); /* resolve_host logs the error */
921 check_follow_cname(&host, cname);
Damien Millerd0244d42010-07-16 13:56:43 +1000922 }
923
Damien Millerdfc85fa2011-05-15 08:44:02 +1000924 if (gethostname(thishost, sizeof(thishost)) == -1)
925 fatal("gethostname: %s", strerror(errno));
926 strlcpy(shorthost, thishost, sizeof(shorthost));
927 shorthost[strcspn(thishost, ".")] = '\0';
928 snprintf(portstr, sizeof(portstr), "%d", options.port);
Darren Tuckerf6b01b72008-06-13 04:56:37 +1000929
Damien Millerdfc85fa2011-05-15 08:44:02 +1000930 if (options.local_command != NULL) {
Darren Tuckerf6b01b72008-06-13 04:56:37 +1000931 debug3("expanding LocalCommand: %s", options.local_command);
932 cp = options.local_command;
933 options.local_command = percent_expand(cp, "d", pw->pw_dir,
Damien Miller83f8a402011-01-07 09:51:17 +1100934 "h", host, "l", thishost, "n", host_arg, "r", options.user,
Damien Millerdfc85fa2011-05-15 08:44:02 +1000935 "p", portstr, "u", pw->pw_name, "L", shorthost,
936 (char *)NULL);
Darren Tuckerf6b01b72008-06-13 04:56:37 +1000937 debug3("expanded LocalCommand: %s", options.local_command);
Darren Tuckera627d422013-06-02 07:31:17 +1000938 free(cp);
Darren Tuckerf6b01b72008-06-13 04:56:37 +1000939 }
940
Damien Miller0e220db2004-06-15 10:34:08 +1000941 if (options.control_path != NULL) {
Damien Miller6476cad2005-06-16 13:18:34 +1000942 cp = tilde_expand_filename(options.control_path,
943 original_real_uid);
Darren Tuckera627d422013-06-02 07:31:17 +1000944 free(options.control_path);
Damien Millerdfc85fa2011-05-15 08:44:02 +1000945 options.control_path = percent_expand(cp, "h", host,
946 "l", thishost, "n", host_arg, "r", options.user,
947 "p", portstr, "u", pw->pw_name, "L", shorthost,
948 (char *)NULL);
Darren Tuckera627d422013-06-02 07:31:17 +1000949 free(cp);
Damien Miller0e220db2004-06-15 10:34:08 +1000950 }
Damien Millerb1cbfa22008-05-19 16:00:08 +1000951 if (muxclient_command != 0 && options.control_path == NULL)
Darren Tucker0814d312005-06-01 23:08:51 +1000952 fatal("No ControlPath specified for \"-O\" command");
Damien Millerd14b1e72005-06-16 13:19:41 +1000953 if (options.control_path != NULL)
Damien Millerb1cbfa22008-05-19 16:00:08 +1000954 muxclient(options.control_path);
Damien Miller0e220db2004-06-15 10:34:08 +1000955
Damien Miller67bd0622007-09-17 12:06:57 +1000956 timeout_ms = options.connection_timeout * 1000;
957
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000958 /* Open a connection to the remote host. */
Damien Miller0faf7472013-10-17 11:47:23 +1100959 if (ssh_connect(host, addrs, &hostaddr, options.port,
960 options.address_family, options.connection_attempts,
961 &timeout_ms, options.tcp_keep_alive,
962 options.use_privileged_port) != 0)
963 exit(255);
964
965 freeaddrinfo(addrs);
966 packet_set_timeout(options.server_alive_interval,
967 options.server_alive_count_max);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000968
Damien Miller67bd0622007-09-17 12:06:57 +1000969 if (timeout_ms > 0)
970 debug3("timeout: %d ms remain after connect", timeout_ms);
971
Damien Miller5428f641999-11-25 11:54:57 +1100972 /*
973 * If we successfully made the connection, load the host private key
974 * in case we will need it later for combined rsa-rhosts
975 * authentication. This must be done before releasing extra
976 * privileges, because the file is only readable by root.
Ben Lindstrom9e5bb572002-06-06 19:58:27 +0000977 * If we cannot access the private keys, load the public keys
978 * instead and try to execute the ssh-keysign helper instead.
Damien Miller5428f641999-11-25 11:54:57 +1100979 */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000980 sensitive_data.nkeys = 0;
981 sensitive_data.keys = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000982 sensitive_data.external_keysign = 0;
Ben Lindstrom24157572002-06-12 16:09:39 +0000983 if (options.rhosts_rsa_authentication ||
984 options.hostbased_authentication) {
Damien Millereb8b60e2010-08-31 22:41:14 +1000985 sensitive_data.nkeys = 7;
Damien Millerddd63ab2006-03-31 23:10:51 +1100986 sensitive_data.keys = xcalloc(sensitive_data.nkeys,
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000987 sizeof(Key));
Damien Miller6af914a2010-09-10 11:39:26 +1000988 for (i = 0; i < sensitive_data.nkeys; i++)
989 sensitive_data.keys[i] = NULL;
Ben Lindstromf9c48842002-06-11 16:37:51 +0000990
991 PRIV_START;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000992 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
Darren Tucker232b76f2006-05-06 17:41:51 +1000993 _PATH_HOST_KEY_FILE, "", NULL, NULL);
Damien Millerc1583312010-08-05 13:04:50 +1000994 sensitive_data.keys[1] = key_load_private_cert(KEY_DSA,
995 _PATH_HOST_DSA_KEY_FILE, "", NULL);
Damien Miller6af914a2010-09-10 11:39:26 +1000996#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000997 sensitive_data.keys[2] = key_load_private_cert(KEY_ECDSA,
998 _PATH_HOST_ECDSA_KEY_FILE, "", NULL);
Damien Miller6af914a2010-09-10 11:39:26 +1000999#endif
Damien Millereb8b60e2010-08-31 22:41:14 +10001000 sensitive_data.keys[3] = key_load_private_cert(KEY_RSA,
Damien Millerc1583312010-08-05 13:04:50 +10001001 _PATH_HOST_RSA_KEY_FILE, "", NULL);
Damien Millereb8b60e2010-08-31 22:41:14 +10001002 sensitive_data.keys[4] = key_load_private_type(KEY_DSA,
Darren Tucker232b76f2006-05-06 17:41:51 +10001003 _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001004#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001005 sensitive_data.keys[5] = key_load_private_type(KEY_ECDSA,
1006 _PATH_HOST_ECDSA_KEY_FILE, "", NULL, NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001007#endif
Damien Millereb8b60e2010-08-31 22:41:14 +10001008 sensitive_data.keys[6] = key_load_private_type(KEY_RSA,
Darren Tucker232b76f2006-05-06 17:41:51 +10001009 _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL);
Ben Lindstromf9c48842002-06-11 16:37:51 +00001010 PRIV_END;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001011
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +00001012 if (options.hostbased_authentication == 1 &&
1013 sensitive_data.keys[0] == NULL &&
Damien Millereb8b60e2010-08-31 22:41:14 +10001014 sensitive_data.keys[4] == NULL &&
1015 sensitive_data.keys[5] == NULL &&
1016 sensitive_data.keys[6] == NULL) {
Damien Millerc1583312010-08-05 13:04:50 +10001017 sensitive_data.keys[1] = key_load_cert(
1018 _PATH_HOST_DSA_KEY_FILE);
Damien Miller6af914a2010-09-10 11:39:26 +10001019#ifdef OPENSSL_HAS_ECC
Damien Millerc1583312010-08-05 13:04:50 +10001020 sensitive_data.keys[2] = key_load_cert(
Damien Millereb8b60e2010-08-31 22:41:14 +10001021 _PATH_HOST_ECDSA_KEY_FILE);
Damien Miller6af914a2010-09-10 11:39:26 +10001022#endif
Damien Millereb8b60e2010-08-31 22:41:14 +10001023 sensitive_data.keys[3] = key_load_cert(
Damien Millerc1583312010-08-05 13:04:50 +10001024 _PATH_HOST_RSA_KEY_FILE);
Damien Millerc1583312010-08-05 13:04:50 +10001025 sensitive_data.keys[4] = key_load_public(
Damien Millereb8b60e2010-08-31 22:41:14 +10001026 _PATH_HOST_DSA_KEY_FILE, NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001027#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001028 sensitive_data.keys[5] = key_load_public(
1029 _PATH_HOST_ECDSA_KEY_FILE, NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001030#endif
Damien Millereb8b60e2010-08-31 22:41:14 +10001031 sensitive_data.keys[6] = key_load_public(
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001032 _PATH_HOST_RSA_KEY_FILE, NULL);
1033 sensitive_data.external_keysign = 1;
1034 }
Damien Miller95def091999-11-25 00:26:21 +11001035 }
Damien Miller5428f641999-11-25 11:54:57 +11001036 /*
1037 * Get rid of any extra privileges that we may have. We will no
1038 * longer need them. Also, extra privileges could make it very hard
1039 * to read identity files and other non-world-readable files from the
1040 * user's home directory if it happens to be on a NFS volume where
1041 * root is mapped to nobody.
1042 */
Darren Tucker25f60a72004-08-15 17:23:34 +10001043 if (original_effective_uid == 0) {
1044 PRIV_START;
1045 permanently_set_uid(pw);
1046 }
Damien Miller95def091999-11-25 00:26:21 +11001047
Damien Miller5428f641999-11-25 11:54:57 +11001048 /*
1049 * Now that we are back to our own permissions, create ~/.ssh
Damien Miller788f2122005-11-05 15:14:59 +11001050 * directory if it doesn't already exist.
Damien Miller5428f641999-11-25 11:54:57 +11001051 */
Darren Tucker45c66d72011-11-04 10:50:40 +11001052 if (config == NULL) {
1053 r = snprintf(buf, sizeof buf, "%s%s%s", pw->pw_dir,
1054 strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
1055 if (r > 0 && (size_t)r < sizeof(buf) && stat(buf, &st) < 0) {
Darren Tucker8ccb7392010-09-10 12:28:24 +10001056#ifdef WITH_SELINUX
Darren Tucker45c66d72011-11-04 10:50:40 +11001057 ssh_selinux_setfscreatecon(buf);
Darren Tucker8ccb7392010-09-10 12:28:24 +10001058#endif
Darren Tucker45c66d72011-11-04 10:50:40 +11001059 if (mkdir(buf, 0700) < 0)
1060 error("Could not create directory '%.200s'.",
1061 buf);
Darren Tucker8ccb7392010-09-10 12:28:24 +10001062#ifdef WITH_SELINUX
Darren Tucker45c66d72011-11-04 10:50:40 +11001063 ssh_selinux_setfscreatecon(NULL);
Darren Tucker8ccb7392010-09-10 12:28:24 +10001064#endif
Darren Tucker45c66d72011-11-04 10:50:40 +11001065 }
Darren Tucker8ccb7392010-09-10 12:28:24 +10001066 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001067 /* load options.identity_files */
1068 load_public_identity_files();
1069
1070 /* Expand ~ in known host file names. */
Damien Miller295ee632011-05-29 21:42:31 +10001071 tilde_expand_paths(options.system_hostfiles,
1072 options.num_system_hostfiles);
1073 tilde_expand_paths(options.user_hostfiles, options.num_user_hostfiles);
Damien Miller95def091999-11-25 00:26:21 +11001074
Damien Miller07cd5892001-11-12 10:52:03 +11001075 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
Damien Miller857b02e2010-09-24 22:02:56 +10001076 signal(SIGCHLD, main_sigchld_handler);
Damien Miller07cd5892001-11-12 10:52:03 +11001077
Darren Tuckerd6173c02008-06-13 04:52:53 +10001078 /* Log into the remote system. Never returns if the login fails. */
Damien Miller67bd0622007-09-17 12:06:57 +10001079 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr,
Damien Millerd925dcd2010-12-01 12:21:51 +11001080 options.port, pw, timeout_ms);
Damien Miller95def091999-11-25 00:26:21 +11001081
Damien Miller383ffe62010-06-26 10:02:03 +10001082 if (packet_connection_is_on_socket()) {
1083 verbose("Authenticated to %s ([%s]:%d).", host,
1084 get_remote_ipaddr(), get_remote_port());
1085 } else {
1086 verbose("Authenticated to %s (via proxy).", host);
1087 }
1088
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001089 /* We no longer need the private host keys. Clear them now. */
1090 if (sensitive_data.nkeys != 0) {
1091 for (i = 0; i < sensitive_data.nkeys; i++) {
1092 if (sensitive_data.keys[i] != NULL) {
1093 /* Destroys contents safely */
1094 debug3("clear hostkey %d", i);
1095 key_free(sensitive_data.keys[i]);
1096 sensitive_data.keys[i] = NULL;
1097 }
1098 }
Darren Tuckera627d422013-06-02 07:31:17 +10001099 free(sensitive_data.keys);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001100 }
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +00001101 for (i = 0; i < options.num_identity_files; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10001102 free(options.identity_files[i]);
1103 options.identity_files[i] = NULL;
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +00001104 if (options.identity_keys[i]) {
1105 key_free(options.identity_keys[i]);
1106 options.identity_keys[i] = NULL;
1107 }
1108 }
Damien Miller95def091999-11-25 00:26:21 +11001109
Damien Miller1383bd82000-04-06 12:32:37 +10001110 exit_status = compat20 ? ssh_session2() : ssh_session();
1111 packet_close();
Damien Miller8c4e18a2002-09-19 12:05:02 +10001112
Damien Millerb1cbfa22008-05-19 16:00:08 +10001113 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10001114 unlink(options.control_path);
1115
Damien Millera41ccca2010-10-07 22:07:32 +11001116 /* Kill ProxyCommand if it is running. */
1117 ssh_kill_proxy_command();
Damien Miller8c4e18a2002-09-19 12:05:02 +10001118
Damien Miller1383bd82000-04-06 12:32:37 +10001119 return exit_status;
1120}
1121
Damien Millere11e1ea2010-08-03 16:04:46 +10001122static void
1123control_persist_detach(void)
1124{
1125 pid_t pid;
Damien Miller00d9ae22010-08-17 01:59:31 +10001126 int devnull;
Damien Millere11e1ea2010-08-03 16:04:46 +10001127
1128 debug("%s: backgrounding master process", __func__);
1129
1130 /*
1131 * master (current process) into the background, and make the
1132 * foreground process a client of the backgrounded master.
1133 */
1134 switch ((pid = fork())) {
1135 case -1:
1136 fatal("%s: fork: %s", __func__, strerror(errno));
1137 case 0:
1138 /* Child: master process continues mainloop */
1139 break;
1140 default:
1141 /* Parent: set up mux slave to connect to backgrounded master */
1142 debug2("%s: background process is %ld", __func__, (long)pid);
1143 stdin_null_flag = ostdin_null_flag;
Damien Miller21771e22011-05-15 08:45:50 +10001144 options.request_tty = orequest_tty;
Damien Millere11e1ea2010-08-03 16:04:46 +10001145 tty_flag = otty_flag;
1146 close(muxserver_sock);
1147 muxserver_sock = -1;
Damien Miller5929c522010-09-10 11:17:02 +10001148 options.control_master = SSHCTL_MASTER_NO;
Damien Millere11e1ea2010-08-03 16:04:46 +10001149 muxclient(options.control_path);
1150 /* muxclient() doesn't return on success. */
1151 fatal("Failed to connect to new control master");
1152 }
Damien Miller00d9ae22010-08-17 01:59:31 +10001153 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
1154 error("%s: open(\"/dev/null\"): %s", __func__,
1155 strerror(errno));
1156 } else {
1157 if (dup2(devnull, STDIN_FILENO) == -1 ||
1158 dup2(devnull, STDOUT_FILENO) == -1)
1159 error("%s: dup2: %s", __func__, strerror(errno));
1160 if (devnull > STDERR_FILENO)
1161 close(devnull);
1162 }
Damien Miller98e27dc2013-07-25 11:55:52 +10001163 daemon(1, 1);
Damien Millerea2c1a42011-06-03 12:10:22 +10001164 setproctitle("%s [mux]", options.control_path);
Damien Millere11e1ea2010-08-03 16:04:46 +10001165}
1166
1167/* Do fork() after authentication. Used by "ssh -f" */
1168static void
1169fork_postauth(void)
1170{
1171 if (need_controlpersist_detach)
1172 control_persist_detach();
1173 debug("forking to background");
1174 fork_after_authentication_flag = 0;
1175 if (daemon(1, 1) < 0)
1176 fatal("daemon() failed: %.200s", strerror(errno));
1177}
1178
Darren Tucker9f407c42008-06-13 04:50:27 +10001179/* Callback for remote forward global requests */
1180static void
1181ssh_confirm_remote_forward(int type, u_int32_t seq, void *ctxt)
1182{
1183 Forward *rfwd = (Forward *)ctxt;
1184
Damien Miller4bf648f2009-02-14 16:28:21 +11001185 /* XXX verbose() on failure? */
Darren Tucker9f407c42008-06-13 04:50:27 +10001186 debug("remote forward %s for: listen %d, connect %s:%d",
1187 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
1188 rfwd->listen_port, rfwd->connect_host, rfwd->connect_port);
Darren Tucker68afb8c2011-10-02 18:59:03 +11001189 if (rfwd->listen_port == 0) {
1190 if (type == SSH2_MSG_REQUEST_SUCCESS) {
1191 rfwd->allocated_port = packet_get_int();
1192 logit("Allocated port %u for remote forward to %s:%d",
1193 rfwd->allocated_port,
1194 rfwd->connect_host, rfwd->connect_port);
1195 channel_update_permitted_opens(rfwd->handle,
1196 rfwd->allocated_port);
1197 } else {
1198 channel_update_permitted_opens(rfwd->handle, -1);
1199 }
Damien Miller4bf648f2009-02-14 16:28:21 +11001200 }
1201
Darren Tucker9f407c42008-06-13 04:50:27 +10001202 if (type == SSH2_MSG_REQUEST_FAILURE) {
1203 if (options.exit_on_forward_failure)
1204 fatal("Error: remote port forwarding failed for "
1205 "listen port %d", rfwd->listen_port);
1206 else
1207 logit("Warning: remote port forwarding failed for "
1208 "listen port %d", rfwd->listen_port);
1209 }
Darren Tucker9a2a6092008-07-04 12:53:50 +10001210 if (++remote_forward_confirms_received == options.num_remote_forwards) {
Darren Tucker9f407c42008-06-13 04:50:27 +10001211 debug("All remote forwarding requests processed");
Damien Millere11e1ea2010-08-03 16:04:46 +10001212 if (fork_after_authentication_flag)
1213 fork_postauth();
Darren Tucker9a2a6092008-07-04 12:53:50 +10001214 }
Darren Tucker9f407c42008-06-13 04:50:27 +10001215}
1216
Ben Lindstrombba81212001-06-25 05:01:22 +00001217static void
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001218client_cleanup_stdio_fwd(int id, void *arg)
1219{
1220 debug("stdio forwarding: done");
1221 cleanup_exit(0);
1222}
1223
Darren Tucker2d6665d2011-11-04 10:54:22 +11001224static void
1225ssh_init_stdio_forwarding(void)
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001226{
1227 Channel *c;
Damien Millere1537f92010-01-26 13:26:22 +11001228 int in, out;
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001229
Darren Tucker2d6665d2011-11-04 10:54:22 +11001230 if (stdio_forward_host == NULL)
1231 return;
Damien Miller386feab2013-10-15 12:14:49 +11001232 if (!compat20)
Darren Tucker2d6665d2011-11-04 10:54:22 +11001233 fatal("stdio forwarding require Protocol 2");
Damien Millere1537f92010-01-26 13:26:22 +11001234
Darren Tucker2d6665d2011-11-04 10:54:22 +11001235 debug3("%s: %s:%d", __func__, stdio_forward_host, stdio_forward_port);
1236
1237 if ((in = dup(STDIN_FILENO)) < 0 ||
1238 (out = dup(STDOUT_FILENO)) < 0)
Damien Millere1537f92010-01-26 13:26:22 +11001239 fatal("channel_connect_stdio_fwd: dup() in/out failed");
Darren Tucker2d6665d2011-11-04 10:54:22 +11001240 if ((c = channel_connect_stdio_fwd(stdio_forward_host,
1241 stdio_forward_port, in, out)) == NULL)
1242 fatal("%s: channel_connect_stdio_fwd failed", __func__);
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001243 channel_register_cleanup(c->self, client_cleanup_stdio_fwd, 0);
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001244}
1245
1246static void
Damien Miller0bc1bd82000-11-13 22:57:25 +11001247ssh_init_forwarding(void)
1248{
Kevin Steves12057502001-02-05 14:54:34 +00001249 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001250 int i;
Kevin Steves12057502001-02-05 14:54:34 +00001251
Damien Miller0bc1bd82000-11-13 22:57:25 +11001252 /* Initiate local TCP/IP port forwardings. */
1253 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001254 debug("Local connections to %.200s:%d forwarded to remote "
1255 "address %.200s:%d",
Darren Tucker47eede72005-03-14 23:08:12 +11001256 (options.local_forwards[i].listen_host == NULL) ?
1257 (options.gateway_ports ? "*" : "LOCALHOST") :
Damien Millerf91ee4c2005-03-01 21:24:33 +11001258 options.local_forwards[i].listen_host,
1259 options.local_forwards[i].listen_port,
1260 options.local_forwards[i].connect_host,
1261 options.local_forwards[i].connect_port);
Damien Millerb16461c2002-01-22 23:29:22 +11001262 success += channel_setup_local_fwd_listener(
Damien Millerf91ee4c2005-03-01 21:24:33 +11001263 options.local_forwards[i].listen_host,
1264 options.local_forwards[i].listen_port,
1265 options.local_forwards[i].connect_host,
1266 options.local_forwards[i].connect_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +11001267 options.gateway_ports);
1268 }
Darren Tuckere7d4b192006-07-12 22:17:10 +10001269 if (i > 0 && success != i && options.exit_on_forward_failure)
1270 fatal("Could not request local forwarding.");
Kevin Steves12057502001-02-05 14:54:34 +00001271 if (i > 0 && success == 0)
1272 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001273
1274 /* Initiate remote TCP/IP port forwardings. */
1275 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001276 debug("Remote connections from %.200s:%d forwarded to "
1277 "local address %.200s:%d",
Damien Miller46d38de2005-07-17 17:02:09 +10001278 (options.remote_forwards[i].listen_host == NULL) ?
Damien Milleraa3bb102005-11-05 15:12:59 +11001279 "LOCALHOST" : options.remote_forwards[i].listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +11001280 options.remote_forwards[i].listen_port,
1281 options.remote_forwards[i].connect_host,
1282 options.remote_forwards[i].connect_port);
Darren Tucker68afb8c2011-10-02 18:59:03 +11001283 options.remote_forwards[i].handle =
1284 channel_request_remote_forwarding(
Damien Millerf91ee4c2005-03-01 21:24:33 +11001285 options.remote_forwards[i].listen_host,
1286 options.remote_forwards[i].listen_port,
1287 options.remote_forwards[i].connect_host,
Darren Tucker68afb8c2011-10-02 18:59:03 +11001288 options.remote_forwards[i].connect_port);
1289 if (options.remote_forwards[i].handle < 0) {
Darren Tuckere7d4b192006-07-12 22:17:10 +10001290 if (options.exit_on_forward_failure)
1291 fatal("Could not request remote forwarding.");
1292 else
1293 logit("Warning: Could not request remote "
1294 "forwarding.");
Darren Tucker68afb8c2011-10-02 18:59:03 +11001295 } else {
1296 client_register_global_confirm(ssh_confirm_remote_forward,
1297 &options.remote_forwards[i]);
Darren Tuckere7d4b192006-07-12 22:17:10 +10001298 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001299 }
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001300
1301 /* Initiate tunnel forwarding. */
1302 if (options.tun_open != SSH_TUNMODE_NO) {
1303 if (client_request_tun_fwd(options.tun_open,
1304 options.tun_local, options.tun_remote) == -1) {
1305 if (options.exit_on_forward_failure)
1306 fatal("Could not request tunnel forwarding.");
1307 else
1308 error("Could not request tunnel forwarding.");
1309 }
1310 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001311}
1312
Ben Lindstrombba81212001-06-25 05:01:22 +00001313static void
Damien Miller0bc1bd82000-11-13 22:57:25 +11001314check_agent_present(void)
1315{
1316 if (options.forward_agent) {
Damien Miller788f2122005-11-05 15:14:59 +11001317 /* Clear agent forwarding if we don't have an agent. */
Damien Miller789e95d2002-09-12 09:52:46 +10001318 if (!ssh_agent_present())
Damien Miller0bc1bd82000-11-13 22:57:25 +11001319 options.forward_agent = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001320 }
1321}
1322
Ben Lindstrombba81212001-06-25 05:01:22 +00001323static int
Damien Miller1383bd82000-04-06 12:32:37 +10001324ssh_session(void)
1325{
1326 int type;
Damien Miller1383bd82000-04-06 12:32:37 +10001327 int interactive = 0;
1328 int have_tty = 0;
1329 struct winsize ws;
Damien Miller1383bd82000-04-06 12:32:37 +10001330 char *cp;
Damien Miller17e7ed02005-06-17 12:54:33 +10001331 const char *display;
Damien Miller1383bd82000-04-06 12:32:37 +10001332
Damien Miller95def091999-11-25 00:26:21 +11001333 /* Enable compression if requested. */
1334 if (options.compression) {
Darren Tuckerd6173c02008-06-13 04:52:53 +10001335 debug("Requesting compression at level %d.",
1336 options.compression_level);
Damien Miller95def091999-11-25 00:26:21 +11001337
Darren Tuckerd6173c02008-06-13 04:52:53 +10001338 if (options.compression_level < 1 ||
1339 options.compression_level > 9)
1340 fatal("Compression level must be from 1 (fast) to "
1341 "9 (slow, best).");
Damien Miller95def091999-11-25 00:26:21 +11001342
1343 /* Send the request. */
1344 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
1345 packet_put_int(options.compression_level);
1346 packet_send();
1347 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +11001348 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +11001349 if (type == SSH_SMSG_SUCCESS)
1350 packet_start_compression(options.compression_level);
1351 else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +10001352 logit("Warning: Remote host refused compression.");
Damien Miller95def091999-11-25 00:26:21 +11001353 else
Darren Tuckerd6173c02008-06-13 04:52:53 +10001354 packet_disconnect("Protocol error waiting for "
1355 "compression response.");
Damien Miller95def091999-11-25 00:26:21 +11001356 }
1357 /* Allocate a pseudo tty if appropriate. */
1358 if (tty_flag) {
1359 debug("Requesting pty.");
1360
1361 /* Start the packet. */
1362 packet_start(SSH_CMSG_REQUEST_PTY);
1363
1364 /* Store TERM in the packet. There is no limit on the
1365 length of the string. */
1366 cp = getenv("TERM");
1367 if (!cp)
1368 cp = "";
Ben Lindstrom664408d2001-06-09 01:42:01 +00001369 packet_put_cstring(cp);
Damien Miller95def091999-11-25 00:26:21 +11001370
1371 /* Store window size in the packet. */
1372 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
1373 memset(&ws, 0, sizeof(ws));
Damien Miller71a73672006-03-26 14:04:36 +11001374 packet_put_int((u_int)ws.ws_row);
1375 packet_put_int((u_int)ws.ws_col);
1376 packet_put_int((u_int)ws.ws_xpixel);
1377 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller95def091999-11-25 00:26:21 +11001378
1379 /* Store tty modes in the packet. */
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001380 tty_make_modes(fileno(stdin), NULL);
Damien Miller95def091999-11-25 00:26:21 +11001381
1382 /* Send the packet, and wait for it to leave. */
1383 packet_send();
1384 packet_write_wait();
1385
1386 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +11001387 type = packet_read();
Damien Miller450a7a12000-03-26 13:04:51 +10001388 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +11001389 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001390 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +10001391 } else if (type == SSH_SMSG_FAILURE)
Darren Tuckerd6173c02008-06-13 04:52:53 +10001392 logit("Warning: Remote host failed or refused to "
1393 "allocate a pseudo tty.");
Damien Miller95def091999-11-25 00:26:21 +11001394 else
Darren Tuckerd6173c02008-06-13 04:52:53 +10001395 packet_disconnect("Protocol error waiting for pty "
1396 "request response.");
Damien Miller95def091999-11-25 00:26:21 +11001397 }
1398 /* Request X11 forwarding if enabled and DISPLAY is set. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001399 display = getenv("DISPLAY");
1400 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001401 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001402 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001403 client_x11_get_proto(display, options.xauth_location,
Damien Miller386feab2013-10-15 12:14:49 +11001404 options.forward_x11_trusted,
Damien Miller1ab6a512010-06-26 10:02:24 +10001405 options.forward_x11_timeout,
1406 &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001407 /* Request forwarding with authentication spoofing. */
Darren Tuckerd6173c02008-06-13 04:52:53 +10001408 debug("Requesting X11 forwarding with authentication "
1409 "spoofing.");
Damien Miller6d7b4372011-06-23 08:31:57 +10001410 x11_request_forwarding_with_spoofing(0, display, proto,
1411 data, 0);
Damien Miller95def091999-11-25 00:26:21 +11001412 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +11001413 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +11001414 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +11001415 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001416 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller996acd22003-04-09 20:59:48 +10001417 logit("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +10001418 } else {
Darren Tuckerd6173c02008-06-13 04:52:53 +10001419 packet_disconnect("Protocol error waiting for X11 "
1420 "forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +10001421 }
Damien Miller95def091999-11-25 00:26:21 +11001422 }
1423 /* Tell the packet module whether this is an interactive session. */
Damien Miller0dac6fb2010-11-20 15:19:38 +11001424 packet_set_interactive(interactive,
1425 options.ip_qos_interactive, options.ip_qos_bulk);
Damien Miller95def091999-11-25 00:26:21 +11001426
1427 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001428 check_agent_present();
1429
Damien Miller95def091999-11-25 00:26:21 +11001430 if (options.forward_agent) {
1431 debug("Requesting authentication agent forwarding.");
1432 auth_request_forwarding();
1433
1434 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +11001435 type = packet_read();
Damien Miller48b03fc2002-01-22 23:11:40 +11001436 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +11001437 if (type != SSH_SMSG_SUCCESS)
Damien Miller996acd22003-04-09 20:59:48 +10001438 logit("Warning: Remote host denied authentication agent forwarding.");
Damien Miller95def091999-11-25 00:26:21 +11001439 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001440
Damien Miller0bc1bd82000-11-13 22:57:25 +11001441 /* Initiate port forwardings. */
Darren Tucker2d6665d2011-11-04 10:54:22 +11001442 ssh_init_stdio_forwarding();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001443 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001444
Darren Tuckerb776c852007-12-02 23:06:35 +11001445 /* Execute a local command */
1446 if (options.local_command != NULL &&
1447 options.permit_local_command)
1448 ssh_local_cmd(options.local_command);
1449
Darren Tucker9a2a6092008-07-04 12:53:50 +10001450 /*
1451 * If requested and we are not interested in replies to remote
1452 * forwarding requests, then let ssh continue in the background.
1453 */
Damien Millere11e1ea2010-08-03 16:04:46 +10001454 if (fork_after_authentication_flag) {
1455 if (options.exit_on_forward_failure &&
1456 options.num_remote_forwards > 0) {
1457 debug("deferring postauth fork until remote forward "
1458 "confirmation received");
1459 } else
1460 fork_postauth();
Darren Tucker9a2a6092008-07-04 12:53:50 +10001461 }
Damien Miller5428f641999-11-25 11:54:57 +11001462
1463 /*
1464 * If a command was specified on the command line, execute the
1465 * command now. Otherwise request the server to start a shell.
1466 */
Damien Miller95def091999-11-25 00:26:21 +11001467 if (buffer_len(&command) > 0) {
1468 int len = buffer_len(&command);
1469 if (len > 900)
1470 len = 900;
Darren Tuckerd6173c02008-06-13 04:52:53 +10001471 debug("Sending command: %.*s", len,
1472 (u_char *)buffer_ptr(&command));
Damien Miller95def091999-11-25 00:26:21 +11001473 packet_start(SSH_CMSG_EXEC_CMD);
1474 packet_put_string(buffer_ptr(&command), buffer_len(&command));
1475 packet_send();
1476 packet_write_wait();
1477 } else {
1478 debug("Requesting shell.");
1479 packet_start(SSH_CMSG_EXEC_SHELL);
1480 packet_send();
1481 packet_write_wait();
1482 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001483
Damien Miller95def091999-11-25 00:26:21 +11001484 /* Enter the interactive session. */
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001485 return client_loop(have_tty, tty_flag ?
1486 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
Damien Miller1383bd82000-04-06 12:32:37 +10001487}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001488
Ben Lindstromf558cf62001-09-20 23:13:49 +00001489/* request pty/x11/agent/tcpfwd/shell for channel */
Ben Lindstrombba81212001-06-25 05:01:22 +00001490static void
Damien Millerd530f5f2010-05-21 14:57:10 +10001491ssh_session2_setup(int id, int success, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +10001492{
Damien Miller3756dce2004-06-18 01:17:29 +10001493 extern char **environ;
Damien Miller17e7ed02005-06-17 12:54:33 +10001494 const char *display;
Damien Miller0e220db2004-06-15 10:34:08 +10001495 int interactive = tty_flag;
Damien Miller17e7ed02005-06-17 12:54:33 +10001496
Damien Millerd530f5f2010-05-21 14:57:10 +10001497 if (!success)
1498 return; /* No need for error message, channels code sens one */
1499
Damien Miller46d38de2005-07-17 17:02:09 +10001500 display = getenv("DISPLAY");
Damien Miller17e7ed02005-06-17 12:54:33 +10001501 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001502 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001503 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001504 client_x11_get_proto(display, options.xauth_location,
Damien Miller1ab6a512010-06-26 10:02:24 +10001505 options.forward_x11_trusted,
1506 options.forward_x11_timeout, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001507 /* Request forwarding with authentication spoofing. */
Darren Tuckerd6173c02008-06-13 04:52:53 +10001508 debug("Requesting X11 forwarding with authentication "
1509 "spoofing.");
Damien Miller6d7b4372011-06-23 08:31:57 +10001510 x11_request_forwarding_with_spoofing(id, display, proto,
1511 data, 1);
1512 client_expect_confirm(id, "X11 forwarding", CONFIRM_WARN);
1513 /* XXX exit_on_forward_failure */
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001514 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001515 }
1516
Damien Miller0bc1bd82000-11-13 22:57:25 +11001517 check_agent_present();
1518 if (options.forward_agent) {
1519 debug("Requesting authentication agent forwarding.");
1520 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1521 packet_send();
1522 }
1523
Darren Tucker7b305012012-07-02 18:55:09 +10001524 /* Tell the packet module whether this is an interactive session. */
1525 packet_set_interactive(interactive,
1526 options.ip_qos_interactive, options.ip_qos_bulk);
1527
Damien Miller0e220db2004-06-15 10:34:08 +10001528 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
Damien Miller5771ed72008-05-19 15:35:33 +10001529 NULL, fileno(stdin), &command, environ);
Damien Miller1383bd82000-04-06 12:32:37 +10001530}
1531
Ben Lindstromf558cf62001-09-20 23:13:49 +00001532/* open new channel for a session */
Ben Lindstrombba81212001-06-25 05:01:22 +00001533static int
Ben Lindstromf558cf62001-09-20 23:13:49 +00001534ssh_session2_open(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001535{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001536 Channel *c;
1537 int window, packetmax, in, out, err;
Damien Millerad833b32000-08-23 10:46:23 +10001538
Damien Millercaf6dd62000-08-29 11:33:50 +11001539 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001540 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001541 } else {
1542 in = dup(STDIN_FILENO);
1543 }
1544 out = dup(STDOUT_FILENO);
1545 err = dup(STDERR_FILENO);
1546
1547 if (in < 0 || out < 0 || err < 0)
1548 fatal("dup() in/out/err failed");
1549
Damien Miller69b69aa2000-10-28 14:19:58 +11001550 /* enable nonblocking unless tty */
1551 if (!isatty(in))
1552 set_nonblock(in);
1553 if (!isatty(out))
1554 set_nonblock(out);
1555 if (!isatty(err))
1556 set_nonblock(err);
1557
Damien Millere4340be2000-09-16 13:29:08 +11001558 window = CHAN_SES_WINDOW_DEFAULT;
1559 packetmax = CHAN_SES_PACKET_DEFAULT;
Damien Miller19a59452002-02-19 15:20:57 +11001560 if (tty_flag) {
1561 window >>= 1;
1562 packetmax >>= 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001563 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001564 c = channel_new(
Damien Miller1383bd82000-04-06 12:32:37 +10001565 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001566 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001567 "client-session", /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001568
Ben Lindstromf558cf62001-09-20 23:13:49 +00001569 debug3("ssh_session2_open: channel_new: %d", c->self);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001570
Ben Lindstrom5ec26452001-06-09 00:18:51 +00001571 channel_send_open(c->self);
Ben Lindstromf558cf62001-09-20 23:13:49 +00001572 if (!no_shell_flag)
Damien Millerb84886b2008-05-19 15:05:07 +10001573 channel_register_open_confirm(c->self,
1574 ssh_session2_setup, NULL);
Damien Miller1383bd82000-04-06 12:32:37 +10001575
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001576 return c->self;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001577}
1578
Ben Lindstrombba81212001-06-25 05:01:22 +00001579static int
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001580ssh_session2(void)
1581{
Ben Lindstromf558cf62001-09-20 23:13:49 +00001582 int id = -1;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001583
1584 /* XXX should be pre-session */
Darren Tucker2d6665d2011-11-04 10:54:22 +11001585 if (!options.control_persist)
1586 ssh_init_stdio_forwarding();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001587 ssh_init_forwarding();
1588
Damien Millere11e1ea2010-08-03 16:04:46 +10001589 /* Start listening for multiplex clients */
1590 muxserver_listen();
1591
1592 /*
Darren Tucker2d6665d2011-11-04 10:54:22 +11001593 * If we are in control persist mode and have a working mux listen
1594 * socket, then prepare to background ourselves and have a foreground
1595 * client attach as a control slave.
1596 * NB. we must save copies of the flags that we override for
Damien Millere11e1ea2010-08-03 16:04:46 +10001597 * the backgrounding, since we defer attachment of the slave until
1598 * after the connection is fully established (in particular,
1599 * async rfwd replies have been received for ExitOnForwardFailure).
1600 */
1601 if (options.control_persist && muxserver_sock != -1) {
1602 ostdin_null_flag = stdin_null_flag;
1603 ono_shell_flag = no_shell_flag;
Damien Miller21771e22011-05-15 08:45:50 +10001604 orequest_tty = options.request_tty;
Damien Millere11e1ea2010-08-03 16:04:46 +10001605 otty_flag = tty_flag;
1606 stdin_null_flag = 1;
1607 no_shell_flag = 1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001608 tty_flag = 0;
1609 if (!fork_after_authentication_flag)
1610 need_controlpersist_detach = 1;
1611 fork_after_authentication_flag = 1;
1612 }
Darren Tucker2d6665d2011-11-04 10:54:22 +11001613 /*
1614 * ControlPersist mux listen socket setup failed, attempt the
1615 * stdio forward setup that we skipped earlier.
1616 */
1617 if (options.control_persist && muxserver_sock == -1)
1618 ssh_init_stdio_forwarding();
Damien Millere11e1ea2010-08-03 16:04:46 +10001619
Ben Lindstromf558cf62001-09-20 23:13:49 +00001620 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1621 id = ssh_session2_open();
Damien Miller649fe022013-07-18 16:13:55 +10001622 else {
1623 packet_set_interactive(
1624 options.control_master == SSHCTL_MASTER_NO,
1625 options.ip_qos_interactive, options.ip_qos_bulk);
1626 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001627
Darren Tucker8901fa92008-06-11 09:34:01 +10001628 /* If we don't expect to open a new session, then disallow it */
Damien Miller456e6f02008-11-03 19:20:10 +11001629 if (options.control_master == SSHCTL_MASTER_NO &&
1630 (datafellows & SSH_NEW_OPENSSH)) {
Darren Tucker8901fa92008-06-11 09:34:01 +10001631 debug("Requesting no-more-sessions@openssh.com");
1632 packet_start(SSH2_MSG_GLOBAL_REQUEST);
1633 packet_put_cstring("no-more-sessions@openssh.com");
1634 packet_put_char(0);
1635 packet_send();
1636 }
1637
Damien Millerd27b9472005-12-13 19:29:02 +11001638 /* Execute a local command */
1639 if (options.local_command != NULL &&
1640 options.permit_local_command)
1641 ssh_local_cmd(options.local_command);
1642
Damien Miller1f25ab42010-07-16 13:56:23 +10001643 /*
1644 * If requested and we are not interested in replies to remote
1645 * forwarding requests, then let ssh continue in the background.
1646 */
Damien Millere11e1ea2010-08-03 16:04:46 +10001647 if (fork_after_authentication_flag) {
1648 if (options.exit_on_forward_failure &&
1649 options.num_remote_forwards > 0) {
1650 debug("deferring postauth fork until remote forward "
1651 "confirmation received");
1652 } else
1653 fork_postauth();
Darren Tucker9a2a6092008-07-04 12:53:50 +10001654 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001655
Darren Tuckerf1de4e52010-01-08 16:54:59 +11001656 if (options.use_roaming)
1657 request_roaming();
1658
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001659 return client_loop(tty_flag, tty_flag ?
1660 options.escape_char : SSH_ESCAPECHAR_NONE, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001661}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001662
Ben Lindstrombba81212001-06-25 05:01:22 +00001663static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001664load_public_identity_files(void)
1665{
Damien Miller6b1d53c2006-03-31 23:13:21 +11001666 char *filename, *cp, thishost[NI_MAXHOST];
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001667 char *pwdir = NULL, *pwname = NULL;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001668 int i = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001669 Key *public;
Damien Miller6b1d53c2006-03-31 23:13:21 +11001670 struct passwd *pw;
Damien Miller0a80ca12010-02-27 07:55:05 +11001671 u_int n_ids;
1672 char *identity_files[SSH_MAX_IDENTITY_FILES];
1673 Key *identity_keys[SSH_MAX_IDENTITY_FILES];
Damien Miller7ea845e2010-02-12 09:21:02 +11001674#ifdef ENABLE_PKCS11
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001675 Key **keys;
Damien Miller7ea845e2010-02-12 09:21:02 +11001676 int nkeys;
Damien Miller0a80ca12010-02-27 07:55:05 +11001677#endif /* PKCS11 */
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001678
Damien Miller0a80ca12010-02-27 07:55:05 +11001679 n_ids = 0;
1680 bzero(identity_files, sizeof(identity_files));
1681 bzero(identity_keys, sizeof(identity_keys));
1682
1683#ifdef ENABLE_PKCS11
Damien Miller7ea845e2010-02-12 09:21:02 +11001684 if (options.pkcs11_provider != NULL &&
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001685 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
Damien Miller7ea845e2010-02-12 09:21:02 +11001686 (pkcs11_init(!options.batch_mode) == 0) &&
1687 (nkeys = pkcs11_add_provider(options.pkcs11_provider, NULL,
1688 &keys)) > 0) {
Damien Miller7ea845e2010-02-12 09:21:02 +11001689 for (i = 0; i < nkeys; i++) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001690 if (n_ids >= SSH_MAX_IDENTITY_FILES) {
1691 key_free(keys[i]);
1692 continue;
1693 }
1694 identity_keys[n_ids] = keys[i];
1695 identity_files[n_ids] =
Damien Miller7ea845e2010-02-12 09:21:02 +11001696 xstrdup(options.pkcs11_provider); /* XXX */
Damien Miller0a80ca12010-02-27 07:55:05 +11001697 n_ids++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001698 }
Darren Tuckera627d422013-06-02 07:31:17 +10001699 free(keys);
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001700 }
Damien Miller7ea845e2010-02-12 09:21:02 +11001701#endif /* ENABLE_PKCS11 */
Damien Miller6b1d53c2006-03-31 23:13:21 +11001702 if ((pw = getpwuid(original_real_uid)) == NULL)
1703 fatal("load_public_identity_files: getpwuid failed");
Darren Tuckere143f062007-12-02 23:21:16 +11001704 pwname = xstrdup(pw->pw_name);
1705 pwdir = xstrdup(pw->pw_dir);
Damien Miller6b1d53c2006-03-31 23:13:21 +11001706 if (gethostname(thishost, sizeof(thishost)) == -1)
1707 fatal("load_public_identity_files: gethostname: %s",
1708 strerror(errno));
Damien Miller0a80ca12010-02-27 07:55:05 +11001709 for (i = 0; i < options.num_identity_files; i++) {
Darren Tucker15fd19c2013-04-05 11:22:26 +11001710 if (n_ids >= SSH_MAX_IDENTITY_FILES ||
1711 strcasecmp(options.identity_files[i], "none") == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +10001712 free(options.identity_files[i]);
Damien Miller0a80ca12010-02-27 07:55:05 +11001713 continue;
1714 }
Damien Miller6b1d53c2006-03-31 23:13:21 +11001715 cp = tilde_expand_filename(options.identity_files[i],
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001716 original_real_uid);
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001717 filename = percent_expand(cp, "d", pwdir,
1718 "u", pwname, "l", thishost, "h", host,
Damien Miller6b1d53c2006-03-31 23:13:21 +11001719 "r", options.user, (char *)NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001720 free(cp);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001721 public = key_load_public(filename, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001722 debug("identity file %s type %d", filename,
1723 public ? public->type : -1);
Darren Tuckera627d422013-06-02 07:31:17 +10001724 free(options.identity_files[i]);
Damien Miller0a80ca12010-02-27 07:55:05 +11001725 identity_files[n_ids] = filename;
1726 identity_keys[n_ids] = public;
1727
1728 if (++n_ids >= SSH_MAX_IDENTITY_FILES)
1729 continue;
1730
1731 /* Try to add the certificate variant too */
1732 xasprintf(&cp, "%s-cert", filename);
1733 public = key_load_public(cp, NULL);
1734 debug("identity file %s type %d", cp,
1735 public ? public->type : -1);
1736 if (public == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +10001737 free(cp);
Damien Miller0a80ca12010-02-27 07:55:05 +11001738 continue;
1739 }
1740 if (!key_is_cert(public)) {
1741 debug("%s: key %s type %s is not a certificate",
1742 __func__, cp, key_type(public));
1743 key_free(public);
Darren Tuckera627d422013-06-02 07:31:17 +10001744 free(cp);
Damien Miller0a80ca12010-02-27 07:55:05 +11001745 continue;
1746 }
1747 identity_keys[n_ids] = public;
1748 /* point to the original path, most likely the private key */
1749 identity_files[n_ids] = xstrdup(filename);
1750 n_ids++;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001751 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001752 options.num_identity_files = n_ids;
1753 memcpy(options.identity_files, identity_files, sizeof(identity_files));
1754 memcpy(options.identity_keys, identity_keys, sizeof(identity_keys));
1755
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001756 bzero(pwname, strlen(pwname));
Darren Tuckera627d422013-06-02 07:31:17 +10001757 free(pwname);
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001758 bzero(pwdir, strlen(pwdir));
Darren Tuckera627d422013-06-02 07:31:17 +10001759 free(pwdir);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001760}
Damien Miller857b02e2010-09-24 22:02:56 +10001761
1762static void
1763main_sigchld_handler(int sig)
1764{
1765 int save_errno = errno;
1766 pid_t pid;
1767 int status;
1768
1769 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
1770 (pid < 0 && errno == EINTR))
1771 ;
1772
1773 signal(sig, main_sigchld_handler);
1774 errno = save_errno;
1775}