blob: 58becd7085ebab71d3ac5a97a9d7d697ea9e2ce4 [file] [log] [blame]
Damien Miller0fde8ac2013-11-21 14:12:23 +11001/* $OpenBSD: ssh.c,v 1.393 2013/11/21 00:45:44 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;
Damien Miller38505592013-10-17 11:48:13 +1100278 if (options.canonicalize_hostname == SSH_CANONICALISE_NO)
Damien Miller0faf7472013-10-17 11:47:23 +1100279 return 0;
280 /*
Damien Miller38505592013-10-17 11:48:13 +1100281 * Don't attempt to canonicalize names that will be interpreted by
Damien Miller0faf7472013-10-17 11:47:23 +1100282 * a proxy unless the user specifically requests so.
283 */
284 if (options.proxy_command != NULL &&
Damien Miller38505592013-10-17 11:48:13 +1100285 options.canonicalize_hostname != SSH_CANONICALISE_ALWAYS)
Damien Miller0faf7472013-10-17 11:47:23 +1100286 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;
Damien Miller38505592013-10-17 11:48:13 +1100295 verbose("Canonicalized DNS aliased hostname "
Damien Miller0faf7472013-10-17 11:47:23 +1100296 "\"%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
Damien Miller51682fa2013-10-17 11:48:31 +1100306 * canonicalization rules. Returns the address list for the host or NULL
307 * if no name was found after canonicalization.
Damien Miller0faf7472013-10-17 11:47:23 +1100308 */
309static struct addrinfo *
Damien Miller38505592013-10-17 11:48:13 +1100310resolve_canonicalize(char **hostp, u_int port)
Damien Miller0faf7472013-10-17 11:47:23 +1100311{
312 int i, ndots;
313 char *cp, *fullhost, cname_target[NI_MAXHOST];
314 struct addrinfo *addrs;
315
Damien Miller38505592013-10-17 11:48:13 +1100316 if (options.canonicalize_hostname == SSH_CANONICALISE_NO)
Damien Miller0faf7472013-10-17 11:47:23 +1100317 return NULL;
318 /*
Damien Miller38505592013-10-17 11:48:13 +1100319 * Don't attempt to canonicalize names that will be interpreted by
Damien Miller0faf7472013-10-17 11:47:23 +1100320 * a proxy unless the user specifically requests so.
321 */
322 if (options.proxy_command != NULL &&
Damien Miller38505592013-10-17 11:48:13 +1100323 options.canonicalize_hostname != SSH_CANONICALISE_ALWAYS)
Damien Miller0faf7472013-10-17 11:47:23 +1100324 return NULL;
Damien Miller51682fa2013-10-17 11:48:31 +1100325 /* Don't apply canonicalization to sufficiently-qualified hostnames */
Damien Miller0faf7472013-10-17 11:47:23 +1100326 ndots = 0;
327 for (cp = *hostp; *cp != '\0'; cp++) {
328 if (*cp == '.')
329 ndots++;
330 }
Damien Miller38505592013-10-17 11:48:13 +1100331 if (ndots > options.canonicalize_max_dots) {
332 debug3("%s: not canonicalizing hostname \"%s\" (max dots %d)",
333 __func__, *hostp, options.canonicalize_max_dots);
Damien Miller0faf7472013-10-17 11:47:23 +1100334 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)) {
Damien Miller38505592013-10-17 11:48:13 +1100350 debug("Canonicalized hostname \"%s\" => \"%s\"",
Damien Miller0faf7472013-10-17 11:47:23 +1100351 *hostp, fullhost);
352 }
353 free(*hostp);
354 *hostp = fullhost;
355 return addrs;
356 }
Damien Miller38505592013-10-17 11:48:13 +1100357 if (!options.canonicalize_fallback_local)
Damien Miller0faf7472013-10-17 11:47:23 +1100358 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)
Damien Miller0fde8ac2013-11-21 14:12:23 +1100523 cp = cipher_alg_list('\n', 0);
524 else if (strcasecmp(optarg, "cipher-auth") == 0)
525 cp = cipher_alg_list('\n', 1);
Damien Millerea111192013-04-23 19:24:32 +1000526 else if (strcasecmp(optarg, "mac") == 0)
Damien Miller690d9892013-11-08 12:16:49 +1100527 cp = mac_alg_list('\n');
Damien Millerea111192013-04-23 19:24:32 +1000528 else if (strcasecmp(optarg, "kex") == 0)
Damien Miller690d9892013-11-08 12:16:49 +1100529 cp = kex_alg_list('\n');
Damien Millerea111192013-04-23 19:24:32 +1000530 else if (strcasecmp(optarg, "key") == 0)
531 cp = key_alg_list();
532 if (cp == NULL)
533 fatal("Unsupported query \"%s\"", optarg);
534 printf("%s\n", cp);
535 free(cp);
536 exit(0);
537 break;
Damien Miller95def091999-11-25 00:26:21 +1100538 case 'a':
539 options.forward_agent = 0;
540 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000541 case 'A':
542 options.forward_agent = 1;
543 break;
Damien Miller95def091999-11-25 00:26:21 +1100544 case 'k':
Damien Millere0113cc2003-11-24 13:10:09 +1100545 options.gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100546 break;
Darren Tucker415bddc2007-06-12 23:43:16 +1000547 case 'K':
548 options.gss_authentication = 1;
549 options.gss_deleg_creds = 1;
550 break;
Damien Miller95def091999-11-25 00:26:21 +1100551 case 'i':
552 if (stat(optarg, &st) < 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000553 fprintf(stderr, "Warning: Identity file %s "
Damien Miller3eb48b62005-03-01 21:15:46 +1100554 "not accessible: %s.\n", optarg,
555 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100556 break;
557 }
Darren Tucker19104782013-04-05 11:13:08 +1100558 add_identity_file(&options, NULL, optarg, 1);
Damien Miller95def091999-11-25 00:26:21 +1100559 break;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000560 case 'I':
Damien Miller7ea845e2010-02-12 09:21:02 +1100561#ifdef ENABLE_PKCS11
562 options.pkcs11_provider = xstrdup(optarg);
Ben Lindstrombcc18082001-08-06 21:59:25 +0000563#else
Damien Miller7ea845e2010-02-12 09:21:02 +1100564 fprintf(stderr, "no support for PKCS#11.\n");
Ben Lindstrombcc18082001-08-06 21:59:25 +0000565#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000566 break;
Damien Miller95def091999-11-25 00:26:21 +1100567 case 't':
Damien Miller21771e22011-05-15 08:45:50 +1000568 if (options.request_tty == REQUEST_TTY_YES)
569 options.request_tty = REQUEST_TTY_FORCE;
570 else
571 options.request_tty = REQUEST_TTY_YES;
Damien Miller95def091999-11-25 00:26:21 +1100572 break;
Damien Miller95def091999-11-25 00:26:21 +1100573 case 'v':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000574 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100575 debug_flag = 1;
576 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000577 } else {
578 if (options.log_level < SYSLOG_LEVEL_DEBUG3)
579 options.log_level++;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000580 }
Damien Miller03d4d7e2013-04-23 15:21:06 +1000581 break;
Damien Miller95def091999-11-25 00:26:21 +1100582 case 'V':
Damien Miller0c889cd2004-03-22 09:36:00 +1100583 fprintf(stderr, "%s, %s\n",
Damien Miller2aa6d3c2004-09-12 16:53:04 +1000584 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
Damien Miller95def091999-11-25 00:26:21 +1100585 if (opt == 'V')
586 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100587 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100588 case 'w':
Damien Miller7b58e802005-12-13 19:33:19 +1100589 if (options.tun_open == -1)
590 options.tun_open = SSH_TUNMODE_DEFAULT;
Damien Millerd27b9472005-12-13 19:29:02 +1100591 options.tun_local = a2tun(optarg, &options.tun_remote);
Damien Miller7b58e802005-12-13 19:33:19 +1100592 if (options.tun_local == SSH_TUNID_ERR) {
Darren Tuckerd6173c02008-06-13 04:52:53 +1000593 fprintf(stderr,
594 "Bad tun device '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100595 exit(255);
Damien Millerd27b9472005-12-13 19:29:02 +1100596 }
597 break;
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100598 case 'W':
Damien Millere1537f92010-01-26 13:26:22 +1100599 if (stdio_forward_host != NULL)
600 fatal("stdio forward already specified");
601 if (muxclient_command != 0)
602 fatal("Cannot specify stdio forward with -O");
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100603 if (parse_forward(&fwd, optarg, 1, 0)) {
604 stdio_forward_host = fwd.listen_host;
605 stdio_forward_port = fwd.listen_port;
Darren Tuckera627d422013-06-02 07:31:17 +1000606 free(fwd.connect_host);
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100607 } else {
608 fprintf(stderr,
609 "Bad stdio forwarding specification '%s'\n",
610 optarg);
611 exit(255);
612 }
Damien Miller21771e22011-05-15 08:45:50 +1000613 options.request_tty = REQUEST_TTY_NO;
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100614 no_shell_flag = 1;
615 options.clear_forwardings = 1;
616 options.exit_on_forward_failure = 1;
617 break;
Damien Miller95def091999-11-25 00:26:21 +1100618 case 'q':
619 options.log_level = SYSLOG_LEVEL_QUIET;
620 break;
Damien Miller95def091999-11-25 00:26:21 +1100621 case 'e':
622 if (optarg[0] == '^' && optarg[2] == 0 &&
Damien Millerf4614452001-07-14 12:18:10 +1000623 (u_char) optarg[1] >= 64 &&
624 (u_char) optarg[1] < 128)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000625 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100626 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000627 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100628 else if (strcmp(optarg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000629 options.escape_char = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100630 else {
Damien Millerf4614452001-07-14 12:18:10 +1000631 fprintf(stderr, "Bad escape character '%s'.\n",
632 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100633 exit(255);
Damien Miller95def091999-11-25 00:26:21 +1100634 }
635 break;
Damien Miller95def091999-11-25 00:26:21 +1100636 case 'c':
Damien Millereba71ba2000-04-29 23:57:08 +1000637 if (ciphers_valid(optarg)) {
638 /* SSH2 only */
639 options.ciphers = xstrdup(optarg);
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000640 options.cipher = SSH_CIPHER_INVALID;
Damien Millereba71ba2000-04-29 23:57:08 +1000641 } else {
642 /* SSH1 only */
Damien Millere39cacc2000-11-29 12:18:44 +1100643 options.cipher = cipher_number(optarg);
644 if (options.cipher == -1) {
Damien Millerf4614452001-07-14 12:18:10 +1000645 fprintf(stderr,
646 "Unknown cipher type '%s'\n",
647 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100648 exit(255);
Damien Millereba71ba2000-04-29 23:57:08 +1000649 }
Damien Millerf4614452001-07-14 12:18:10 +1000650 if (options.cipher == SSH_CIPHER_3DES)
Damien Millere39cacc2000-11-29 12:18:44 +1100651 options.ciphers = "3des-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000652 else if (options.cipher == SSH_CIPHER_BLOWFISH)
Damien Millere39cacc2000-11-29 12:18:44 +1100653 options.ciphers = "blowfish-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000654 else
Damien Millere39cacc2000-11-29 12:18:44 +1100655 options.ciphers = (char *)-1;
Damien Miller95def091999-11-25 00:26:21 +1100656 }
657 break;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000658 case 'm':
659 if (mac_valid(optarg))
660 options.macs = xstrdup(optarg);
661 else {
Damien Millerf4614452001-07-14 12:18:10 +1000662 fprintf(stderr, "Unknown mac type '%s'\n",
663 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100664 exit(255);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000665 }
666 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000667 case 'M':
Damien Millerd14b1e72005-06-16 13:19:41 +1000668 if (options.control_master == SSHCTL_MASTER_YES)
669 options.control_master = SSHCTL_MASTER_ASK;
670 else
671 options.control_master = SSHCTL_MASTER_YES;
Damien Miller0e220db2004-06-15 10:34:08 +1000672 break;
Damien Miller95def091999-11-25 00:26:21 +1100673 case 'p':
Ben Lindstrom19066a12001-04-12 23:39:26 +0000674 options.port = a2port(optarg);
Damien Miller3dc71ad2009-01-28 16:31:22 +1100675 if (options.port <= 0) {
Ben Lindstrom146edb92001-04-11 23:06:28 +0000676 fprintf(stderr, "Bad port '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100677 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000678 }
Damien Miller95def091999-11-25 00:26:21 +1100679 break;
Damien Miller95def091999-11-25 00:26:21 +1100680 case 'l':
681 options.user = optarg;
682 break;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000683
Damien Miller95def091999-11-25 00:26:21 +1100684 case 'L':
Damien Miller4bf648f2009-02-14 16:28:21 +1100685 if (parse_forward(&fwd, optarg, 0, 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +1100686 add_local_forward(&options, &fwd);
687 else {
Damien Millerf4614452001-07-14 12:18:10 +1000688 fprintf(stderr,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100689 "Bad local forwarding specification '%s'\n",
Damien Millerf4614452001-07-14 12:18:10 +1000690 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100691 exit(255);
Ben Lindstrom1a174712001-09-12 17:56:15 +0000692 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100693 break;
694
695 case 'R':
Damien Miller4bf648f2009-02-14 16:28:21 +1100696 if (parse_forward(&fwd, optarg, 0, 1)) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100697 add_remote_forward(&options, &fwd);
698 } else {
699 fprintf(stderr,
700 "Bad remote forwarding specification "
701 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100702 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100703 }
Damien Miller95def091999-11-25 00:26:21 +1100704 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000705
706 case 'D':
Damien Miller4bf648f2009-02-14 16:28:21 +1100707 if (parse_forward(&fwd, optarg, 1, 0)) {
Damien Millera699d952008-11-03 19:27:34 +1100708 add_local_forward(&options, &fwd);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100709 } else {
Damien Millera699d952008-11-03 19:27:34 +1100710 fprintf(stderr,
711 "Bad dynamic forwarding specification "
712 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100713 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000714 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000715 break;
716
Damien Miller95def091999-11-25 00:26:21 +1100717 case 'C':
718 options.compression = 1;
719 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000720 case 'N':
721 no_shell_flag = 1;
Damien Miller21771e22011-05-15 08:45:50 +1000722 options.request_tty = REQUEST_TTY_NO;
Damien Miller1383bd82000-04-06 12:32:37 +1000723 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000724 case 'T':
Damien Miller21771e22011-05-15 08:45:50 +1000725 options.request_tty = REQUEST_TTY_NO;
Damien Miller1383bd82000-04-06 12:32:37 +1000726 break;
Damien Miller95def091999-11-25 00:26:21 +1100727 case 'o':
Damien Miller9836cf82003-12-17 16:30:06 +1100728 line = xstrdup(optarg);
Damien Miller194fd902013-10-15 12:13:05 +1100729 if (process_config_line(&options, pw, host ? host : "",
730 line, "command-line", 0, NULL, SSHCONF_USERCONF)
Darren Tuckeraefa3682013-04-05 11:18:35 +1100731 != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100732 exit(255);
Darren Tuckera627d422013-06-02 07:31:17 +1000733 free(line);
Damien Miller95def091999-11-25 00:26:21 +1100734 break;
Damien Miller832562e2001-01-30 09:30:01 +1100735 case 's':
736 subsystem_flag = 1;
737 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000738 case 'S':
739 if (options.control_path != NULL)
740 free(options.control_path);
741 options.control_path = xstrdup(optarg);
Damien Miller0e220db2004-06-15 10:34:08 +1000742 break;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000743 case 'b':
744 options.bind_address = optarg;
745 break;
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000746 case 'F':
747 config = optarg;
748 break;
Damien Miller95def091999-11-25 00:26:21 +1100749 default:
750 usage();
751 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000752 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000753
Damien Millerf4614452001-07-14 12:18:10 +1000754 ac -= optind;
755 av += optind;
756
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100757 if (ac > 0 && !host) {
Ben Lindstromc276c122002-12-23 02:14:51 +0000758 if (strrchr(*av, '@')) {
Damien Millerf4614452001-07-14 12:18:10 +1000759 p = xstrdup(*av);
Ben Lindstromc276c122002-12-23 02:14:51 +0000760 cp = strrchr(p, '@');
Damien Millerf4614452001-07-14 12:18:10 +1000761 if (cp == NULL || cp == p)
762 usage();
763 options.user = p;
764 *cp = '\0';
Damien Miller0faf7472013-10-17 11:47:23 +1100765 host = xstrdup(++cp);
Damien Millerf4614452001-07-14 12:18:10 +1000766 } else
Damien Miller0faf7472013-10-17 11:47:23 +1100767 host = xstrdup(*av);
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000768 if (ac > 1) {
769 optind = optreset = 1;
Damien Millerf4614452001-07-14 12:18:10 +1000770 goto again;
771 }
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000772 ac--, av++;
Damien Millerf4614452001-07-14 12:18:10 +1000773 }
774
Damien Miller95def091999-11-25 00:26:21 +1100775 /* Check that we got a host name. */
776 if (!host)
777 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000778
Damien Miller0faf7472013-10-17 11:47:23 +1100779 lowercase(host);
780 host_arg = xstrdup(host);
781
Damien Miller4314c2b2010-09-10 11:12:09 +1000782 OpenSSL_add_all_algorithms();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100783 ERR_load_crypto_strings();
Damien Millercb5e44a2000-09-29 12:12:36 +1100784
Damien Miller95def091999-11-25 00:26:21 +1100785 /* Initialize the command to execute on remote host. */
786 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000787
Damien Miller5428f641999-11-25 11:54:57 +1100788 /*
789 * Save the command to execute on the remote host in a buffer. There
790 * is no limit on the length of the command, except by the maximum
791 * packet size. Also sets the tty flag if there is no command.
792 */
Damien Millerf4614452001-07-14 12:18:10 +1000793 if (!ac) {
Damien Miller95def091999-11-25 00:26:21 +1100794 /* No command specified - execute shell on a tty. */
Damien Miller832562e2001-01-30 09:30:01 +1100795 if (subsystem_flag) {
Damien Millerf4614452001-07-14 12:18:10 +1000796 fprintf(stderr,
797 "You must specify a subsystem to invoke.\n");
Damien Miller832562e2001-01-30 09:30:01 +1100798 usage();
799 }
Damien Miller95def091999-11-25 00:26:21 +1100800 } else {
Damien Millerf4614452001-07-14 12:18:10 +1000801 /* A command has been specified. Store it into the buffer. */
802 for (i = 0; i < ac; i++) {
803 if (i)
Damien Miller95def091999-11-25 00:26:21 +1100804 buffer_append(&command, " ", 1);
805 buffer_append(&command, av[i], strlen(av[i]));
806 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000807 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000808
Damien Miller95def091999-11-25 00:26:21 +1100809 /* Cannot fork to background if no command. */
Darren Tuckerd6173c02008-06-13 04:52:53 +1000810 if (fork_after_authentication_flag && buffer_len(&command) == 0 &&
811 !no_shell_flag)
812 fatal("Cannot fork into background without a command "
813 "to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000814
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000815 /*
816 * Initialize "log" output. Since we are the client all output
Damien Miller03d4d7e2013-04-23 15:21:06 +1000817 * goes to stderr unless otherwise specified by -y or -E.
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000818 */
Damien Miller03d4d7e2013-04-23 15:21:06 +1000819 if (use_syslog && logfile != NULL)
820 fatal("Can't specify both -y and -E");
821 if (logfile != NULL) {
822 log_redirect_stderr_to(logfile);
Darren Tuckera627d422013-06-02 07:31:17 +1000823 free(logfile);
Damien Miller03d4d7e2013-04-23 15:21:06 +1000824 }
Darren Tucker72efd742009-06-21 17:48:00 +1000825 log_init(argv0,
Darren Tuckerd6173c02008-06-13 04:52:53 +1000826 options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
Damien Millere272a5b2008-11-03 19:22:37 +1100827 SYSLOG_FACILITY_USER, !use_syslog);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000828
Damien Miller03d4d7e2013-04-23 15:21:06 +1000829 if (debug_flag)
830 logit("%s, %s", SSH_VERSION, SSLeay_version(SSLEAY_VERSION));
831
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000832 /*
833 * Read per-user configuration file. Ignore the system wide config
834 * file if the user specifies a config file on the command line.
835 */
836 if (config != NULL) {
Damien Miller508b6c32013-04-23 15:18:28 +1000837 if (strcasecmp(config, "none") != 0 &&
Damien Miller194fd902013-10-15 12:13:05 +1100838 !read_config_file(config, pw, host, &options,
839 SSHCONF_USERCONF))
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000840 fatal("Can't open user config file %.100s: "
841 "%.100s", config, strerror(errno));
Damien Miller80163902007-01-05 16:30:16 +1100842 } else {
Darren Tucker199b1342009-07-06 07:16:56 +1000843 r = snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir,
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000844 _PATH_SSH_USER_CONFFILE);
Darren Tucker199b1342009-07-06 07:16:56 +1000845 if (r > 0 && (size_t)r < sizeof(buf))
Damien Miller194fd902013-10-15 12:13:05 +1100846 (void)read_config_file(buf, pw, host, &options,
Darren Tuckeraefa3682013-04-05 11:18:35 +1100847 SSHCONF_CHECKPERM|SSHCONF_USERCONF);
Ben Lindstrom83f07d12001-10-03 17:22:29 +0000848
Damien Miller765f8c42011-08-06 06:18:16 +1000849 /* Read systemwide configuration file after user config. */
Damien Miller194fd902013-10-15 12:13:05 +1100850 (void)read_config_file(_PATH_HOST_CONFIG_FILE, pw, host,
Damien Miller57a44762004-04-20 20:11:57 +1000851 &options, 0);
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000852 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000853
Damien Miller95def091999-11-25 00:26:21 +1100854 /* Fill configuration defaults. */
855 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000856
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000857 channel_set_af(options.address_family);
858
Damien Millere9fc72e2013-10-15 12:14:12 +1100859 /* Tidy and check options */
860 if (options.host_key_alias != NULL)
861 lowercase(options.host_key_alias);
862 if (options.proxy_command != NULL &&
863 strcmp(options.proxy_command, "-") == 0 &&
864 options.proxy_use_fdpass)
865 fatal("ProxyCommand=- and ProxyUseFDPass are incompatible");
Damien Miller0faf7472013-10-17 11:47:23 +1100866#ifndef HAVE_CYGWIN
867 if (original_effective_uid != 0)
868 options.use_privileged_port = 0;
869#endif
Damien Millere9fc72e2013-10-15 12:14:12 +1100870
Damien Miller95def091999-11-25 00:26:21 +1100871 /* reinit */
Darren Tucker72efd742009-06-21 17:48:00 +1000872 log_init(argv0, options.log_level, SYSLOG_FACILITY_USER, !use_syslog);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000873
Damien Millerfff9f092012-07-06 13:45:01 +1000874 if (options.request_tty == REQUEST_TTY_YES ||
875 options.request_tty == REQUEST_TTY_FORCE)
876 tty_flag = 1;
877
878 /* Allocate a tty by default if no command specified. */
879 if (buffer_len(&command) == 0)
880 tty_flag = options.request_tty != REQUEST_TTY_NO;
881
882 /* Force no tty */
883 if (options.request_tty == REQUEST_TTY_NO || muxclient_command != 0)
884 tty_flag = 0;
885 /* Do not allocate a tty if stdin is not a tty. */
886 if ((!isatty(fileno(stdin)) || stdin_null_flag) &&
887 options.request_tty != REQUEST_TTY_FORCE) {
888 if (tty_flag)
889 logit("Pseudo-terminal will not be allocated because "
890 "stdin is not a terminal.");
891 tty_flag = 0;
892 }
893
Damien Miller60bc5172001-03-19 09:38:15 +1100894 seed_rng();
895
Damien Miller95def091999-11-25 00:26:21 +1100896 if (options.user == NULL)
897 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000898
Darren Tuckerf6b01b72008-06-13 04:56:37 +1000899 /* Get default port if port has not been set. */
Damien Miller194fd902013-10-15 12:13:05 +1100900 if (options.port == 0)
901 options.port = default_ssh_port();
Darren Tuckerf6b01b72008-06-13 04:56:37 +1000902
Damien Miller83f8a402011-01-07 09:51:17 +1100903 /* preserve host name given on command line for %n expansion */
Damien Millerd0244d42010-07-16 13:56:43 +1000904 if (options.hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100905 /* NB. Please keep in sync with readconf.c:match_cfg_line() */
Damien Miller0faf7472013-10-17 11:47:23 +1100906 cp = percent_expand(options.hostname,
Damien Millerd0244d42010-07-16 13:56:43 +1000907 "h", host, (char *)NULL);
Damien Miller0faf7472013-10-17 11:47:23 +1100908 free(host);
909 host = cp;
910 }
911
Damien Miller51682fa2013-10-17 11:48:31 +1100912 /* If canonicalization requested then try to apply it */
Damien Miller38505592013-10-17 11:48:13 +1100913 if (options.canonicalize_hostname != SSH_CANONICALISE_NO)
914 addrs = resolve_canonicalize(&host, options.port);
Damien Miller0faf7472013-10-17 11:47:23 +1100915 /*
Damien Miller51682fa2013-10-17 11:48:31 +1100916 * If canonicalization not requested, or if it failed then try to
Damien Miller0faf7472013-10-17 11:47:23 +1100917 * resolve the bare hostname name using the system resolver's usual
Damien Millera90c0332013-10-24 21:03:17 +1100918 * search rules. Skip the lookup if a ProxyCommand is being used
919 * unless the user has specifically requested canonicalisation.
Damien Miller0faf7472013-10-17 11:47:23 +1100920 */
Damien Millera90c0332013-10-24 21:03:17 +1100921 if (addrs == NULL && (options.proxy_command == NULL ||
922 options.canonicalize_hostname == SSH_CANONICALISE_ALWAYS)) {
Damien Miller0faf7472013-10-17 11:47:23 +1100923 if ((addrs = resolve_host(host, options.port, 1,
924 cname, sizeof(cname))) == NULL)
925 cleanup_exit(255); /* resolve_host logs the error */
926 check_follow_cname(&host, cname);
Damien Millerd0244d42010-07-16 13:56:43 +1000927 }
928
Damien Millerdfc85fa2011-05-15 08:44:02 +1000929 if (gethostname(thishost, sizeof(thishost)) == -1)
930 fatal("gethostname: %s", strerror(errno));
931 strlcpy(shorthost, thishost, sizeof(shorthost));
932 shorthost[strcspn(thishost, ".")] = '\0';
933 snprintf(portstr, sizeof(portstr), "%d", options.port);
Darren Tuckerf6b01b72008-06-13 04:56:37 +1000934
Damien Millerdfc85fa2011-05-15 08:44:02 +1000935 if (options.local_command != NULL) {
Darren Tuckerf6b01b72008-06-13 04:56:37 +1000936 debug3("expanding LocalCommand: %s", options.local_command);
937 cp = options.local_command;
938 options.local_command = percent_expand(cp, "d", pw->pw_dir,
Damien Miller83f8a402011-01-07 09:51:17 +1100939 "h", host, "l", thishost, "n", host_arg, "r", options.user,
Damien Millerdfc85fa2011-05-15 08:44:02 +1000940 "p", portstr, "u", pw->pw_name, "L", shorthost,
941 (char *)NULL);
Darren Tuckerf6b01b72008-06-13 04:56:37 +1000942 debug3("expanded LocalCommand: %s", options.local_command);
Darren Tuckera627d422013-06-02 07:31:17 +1000943 free(cp);
Darren Tuckerf6b01b72008-06-13 04:56:37 +1000944 }
945
Damien Miller0e220db2004-06-15 10:34:08 +1000946 if (options.control_path != NULL) {
Damien Miller6476cad2005-06-16 13:18:34 +1000947 cp = tilde_expand_filename(options.control_path,
948 original_real_uid);
Darren Tuckera627d422013-06-02 07:31:17 +1000949 free(options.control_path);
Damien Millerdfc85fa2011-05-15 08:44:02 +1000950 options.control_path = percent_expand(cp, "h", host,
951 "l", thishost, "n", host_arg, "r", options.user,
952 "p", portstr, "u", pw->pw_name, "L", shorthost,
953 (char *)NULL);
Darren Tuckera627d422013-06-02 07:31:17 +1000954 free(cp);
Damien Miller0e220db2004-06-15 10:34:08 +1000955 }
Damien Millerb1cbfa22008-05-19 16:00:08 +1000956 if (muxclient_command != 0 && options.control_path == NULL)
Darren Tucker0814d312005-06-01 23:08:51 +1000957 fatal("No ControlPath specified for \"-O\" command");
Damien Millerd14b1e72005-06-16 13:19:41 +1000958 if (options.control_path != NULL)
Damien Millerb1cbfa22008-05-19 16:00:08 +1000959 muxclient(options.control_path);
Damien Miller0e220db2004-06-15 10:34:08 +1000960
Damien Miller67bd0622007-09-17 12:06:57 +1000961 timeout_ms = options.connection_timeout * 1000;
962
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000963 /* Open a connection to the remote host. */
Damien Miller0faf7472013-10-17 11:47:23 +1100964 if (ssh_connect(host, addrs, &hostaddr, options.port,
965 options.address_family, options.connection_attempts,
966 &timeout_ms, options.tcp_keep_alive,
967 options.use_privileged_port) != 0)
968 exit(255);
969
Damien Miller28631ce2013-10-26 10:07:56 +1100970 if (addrs != NULL)
971 freeaddrinfo(addrs);
972
Damien Miller0faf7472013-10-17 11:47:23 +1100973 packet_set_timeout(options.server_alive_interval,
974 options.server_alive_count_max);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000975
Damien Miller67bd0622007-09-17 12:06:57 +1000976 if (timeout_ms > 0)
977 debug3("timeout: %d ms remain after connect", timeout_ms);
978
Damien Miller5428f641999-11-25 11:54:57 +1100979 /*
980 * If we successfully made the connection, load the host private key
981 * in case we will need it later for combined rsa-rhosts
982 * authentication. This must be done before releasing extra
983 * privileges, because the file is only readable by root.
Ben Lindstrom9e5bb572002-06-06 19:58:27 +0000984 * If we cannot access the private keys, load the public keys
985 * instead and try to execute the ssh-keysign helper instead.
Damien Miller5428f641999-11-25 11:54:57 +1100986 */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000987 sensitive_data.nkeys = 0;
988 sensitive_data.keys = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000989 sensitive_data.external_keysign = 0;
Ben Lindstrom24157572002-06-12 16:09:39 +0000990 if (options.rhosts_rsa_authentication ||
991 options.hostbased_authentication) {
Damien Millereb8b60e2010-08-31 22:41:14 +1000992 sensitive_data.nkeys = 7;
Damien Millerddd63ab2006-03-31 23:10:51 +1100993 sensitive_data.keys = xcalloc(sensitive_data.nkeys,
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000994 sizeof(Key));
Damien Miller6af914a2010-09-10 11:39:26 +1000995 for (i = 0; i < sensitive_data.nkeys; i++)
996 sensitive_data.keys[i] = NULL;
Ben Lindstromf9c48842002-06-11 16:37:51 +0000997
998 PRIV_START;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000999 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
Darren Tucker232b76f2006-05-06 17:41:51 +10001000 _PATH_HOST_KEY_FILE, "", NULL, NULL);
Damien Millerc1583312010-08-05 13:04:50 +10001001 sensitive_data.keys[1] = key_load_private_cert(KEY_DSA,
1002 _PATH_HOST_DSA_KEY_FILE, "", NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001003#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001004 sensitive_data.keys[2] = key_load_private_cert(KEY_ECDSA,
1005 _PATH_HOST_ECDSA_KEY_FILE, "", NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001006#endif
Damien Millereb8b60e2010-08-31 22:41:14 +10001007 sensitive_data.keys[3] = key_load_private_cert(KEY_RSA,
Damien Millerc1583312010-08-05 13:04:50 +10001008 _PATH_HOST_RSA_KEY_FILE, "", NULL);
Damien Millereb8b60e2010-08-31 22:41:14 +10001009 sensitive_data.keys[4] = key_load_private_type(KEY_DSA,
Darren Tucker232b76f2006-05-06 17:41:51 +10001010 _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001011#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001012 sensitive_data.keys[5] = key_load_private_type(KEY_ECDSA,
1013 _PATH_HOST_ECDSA_KEY_FILE, "", NULL, NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001014#endif
Damien Millereb8b60e2010-08-31 22:41:14 +10001015 sensitive_data.keys[6] = key_load_private_type(KEY_RSA,
Darren Tucker232b76f2006-05-06 17:41:51 +10001016 _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL);
Ben Lindstromf9c48842002-06-11 16:37:51 +00001017 PRIV_END;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001018
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +00001019 if (options.hostbased_authentication == 1 &&
1020 sensitive_data.keys[0] == NULL &&
Damien Millereb8b60e2010-08-31 22:41:14 +10001021 sensitive_data.keys[4] == NULL &&
1022 sensitive_data.keys[5] == NULL &&
1023 sensitive_data.keys[6] == NULL) {
Damien Millerc1583312010-08-05 13:04:50 +10001024 sensitive_data.keys[1] = key_load_cert(
1025 _PATH_HOST_DSA_KEY_FILE);
Damien Miller6af914a2010-09-10 11:39:26 +10001026#ifdef OPENSSL_HAS_ECC
Damien Millerc1583312010-08-05 13:04:50 +10001027 sensitive_data.keys[2] = key_load_cert(
Damien Millereb8b60e2010-08-31 22:41:14 +10001028 _PATH_HOST_ECDSA_KEY_FILE);
Damien Miller6af914a2010-09-10 11:39:26 +10001029#endif
Damien Millereb8b60e2010-08-31 22:41:14 +10001030 sensitive_data.keys[3] = key_load_cert(
Damien Millerc1583312010-08-05 13:04:50 +10001031 _PATH_HOST_RSA_KEY_FILE);
Damien Millerc1583312010-08-05 13:04:50 +10001032 sensitive_data.keys[4] = key_load_public(
Damien Millereb8b60e2010-08-31 22:41:14 +10001033 _PATH_HOST_DSA_KEY_FILE, NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001034#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001035 sensitive_data.keys[5] = key_load_public(
1036 _PATH_HOST_ECDSA_KEY_FILE, NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001037#endif
Damien Millereb8b60e2010-08-31 22:41:14 +10001038 sensitive_data.keys[6] = key_load_public(
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001039 _PATH_HOST_RSA_KEY_FILE, NULL);
1040 sensitive_data.external_keysign = 1;
1041 }
Damien Miller95def091999-11-25 00:26:21 +11001042 }
Damien Miller5428f641999-11-25 11:54:57 +11001043 /*
1044 * Get rid of any extra privileges that we may have. We will no
1045 * longer need them. Also, extra privileges could make it very hard
1046 * to read identity files and other non-world-readable files from the
1047 * user's home directory if it happens to be on a NFS volume where
1048 * root is mapped to nobody.
1049 */
Darren Tucker25f60a72004-08-15 17:23:34 +10001050 if (original_effective_uid == 0) {
1051 PRIV_START;
1052 permanently_set_uid(pw);
1053 }
Damien Miller95def091999-11-25 00:26:21 +11001054
Damien Miller5428f641999-11-25 11:54:57 +11001055 /*
1056 * Now that we are back to our own permissions, create ~/.ssh
Damien Miller788f2122005-11-05 15:14:59 +11001057 * directory if it doesn't already exist.
Damien Miller5428f641999-11-25 11:54:57 +11001058 */
Darren Tucker45c66d72011-11-04 10:50:40 +11001059 if (config == NULL) {
1060 r = snprintf(buf, sizeof buf, "%s%s%s", pw->pw_dir,
1061 strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
1062 if (r > 0 && (size_t)r < sizeof(buf) && stat(buf, &st) < 0) {
Darren Tucker8ccb7392010-09-10 12:28:24 +10001063#ifdef WITH_SELINUX
Darren Tucker45c66d72011-11-04 10:50:40 +11001064 ssh_selinux_setfscreatecon(buf);
Darren Tucker8ccb7392010-09-10 12:28:24 +10001065#endif
Darren Tucker45c66d72011-11-04 10:50:40 +11001066 if (mkdir(buf, 0700) < 0)
1067 error("Could not create directory '%.200s'.",
1068 buf);
Darren Tucker8ccb7392010-09-10 12:28:24 +10001069#ifdef WITH_SELINUX
Darren Tucker45c66d72011-11-04 10:50:40 +11001070 ssh_selinux_setfscreatecon(NULL);
Darren Tucker8ccb7392010-09-10 12:28:24 +10001071#endif
Darren Tucker45c66d72011-11-04 10:50:40 +11001072 }
Darren Tucker8ccb7392010-09-10 12:28:24 +10001073 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001074 /* load options.identity_files */
1075 load_public_identity_files();
1076
1077 /* Expand ~ in known host file names. */
Damien Miller295ee632011-05-29 21:42:31 +10001078 tilde_expand_paths(options.system_hostfiles,
1079 options.num_system_hostfiles);
1080 tilde_expand_paths(options.user_hostfiles, options.num_user_hostfiles);
Damien Miller95def091999-11-25 00:26:21 +11001081
Damien Miller07cd5892001-11-12 10:52:03 +11001082 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
Damien Miller857b02e2010-09-24 22:02:56 +10001083 signal(SIGCHLD, main_sigchld_handler);
Damien Miller07cd5892001-11-12 10:52:03 +11001084
Darren Tuckerd6173c02008-06-13 04:52:53 +10001085 /* Log into the remote system. Never returns if the login fails. */
Damien Miller67bd0622007-09-17 12:06:57 +10001086 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr,
Damien Millerd925dcd2010-12-01 12:21:51 +11001087 options.port, pw, timeout_ms);
Damien Miller95def091999-11-25 00:26:21 +11001088
Damien Miller383ffe62010-06-26 10:02:03 +10001089 if (packet_connection_is_on_socket()) {
1090 verbose("Authenticated to %s ([%s]:%d).", host,
1091 get_remote_ipaddr(), get_remote_port());
1092 } else {
1093 verbose("Authenticated to %s (via proxy).", host);
1094 }
1095
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001096 /* We no longer need the private host keys. Clear them now. */
1097 if (sensitive_data.nkeys != 0) {
1098 for (i = 0; i < sensitive_data.nkeys; i++) {
1099 if (sensitive_data.keys[i] != NULL) {
1100 /* Destroys contents safely */
1101 debug3("clear hostkey %d", i);
1102 key_free(sensitive_data.keys[i]);
1103 sensitive_data.keys[i] = NULL;
1104 }
1105 }
Darren Tuckera627d422013-06-02 07:31:17 +10001106 free(sensitive_data.keys);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001107 }
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +00001108 for (i = 0; i < options.num_identity_files; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10001109 free(options.identity_files[i]);
1110 options.identity_files[i] = NULL;
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +00001111 if (options.identity_keys[i]) {
1112 key_free(options.identity_keys[i]);
1113 options.identity_keys[i] = NULL;
1114 }
1115 }
Damien Miller95def091999-11-25 00:26:21 +11001116
Damien Miller1383bd82000-04-06 12:32:37 +10001117 exit_status = compat20 ? ssh_session2() : ssh_session();
1118 packet_close();
Damien Miller8c4e18a2002-09-19 12:05:02 +10001119
Damien Millerb1cbfa22008-05-19 16:00:08 +10001120 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10001121 unlink(options.control_path);
1122
Damien Millera41ccca2010-10-07 22:07:32 +11001123 /* Kill ProxyCommand if it is running. */
1124 ssh_kill_proxy_command();
Damien Miller8c4e18a2002-09-19 12:05:02 +10001125
Damien Miller1383bd82000-04-06 12:32:37 +10001126 return exit_status;
1127}
1128
Damien Millere11e1ea2010-08-03 16:04:46 +10001129static void
1130control_persist_detach(void)
1131{
1132 pid_t pid;
Damien Miller00d9ae22010-08-17 01:59:31 +10001133 int devnull;
Damien Millere11e1ea2010-08-03 16:04:46 +10001134
1135 debug("%s: backgrounding master process", __func__);
1136
1137 /*
1138 * master (current process) into the background, and make the
1139 * foreground process a client of the backgrounded master.
1140 */
1141 switch ((pid = fork())) {
1142 case -1:
1143 fatal("%s: fork: %s", __func__, strerror(errno));
1144 case 0:
1145 /* Child: master process continues mainloop */
1146 break;
1147 default:
1148 /* Parent: set up mux slave to connect to backgrounded master */
1149 debug2("%s: background process is %ld", __func__, (long)pid);
1150 stdin_null_flag = ostdin_null_flag;
Damien Miller21771e22011-05-15 08:45:50 +10001151 options.request_tty = orequest_tty;
Damien Millere11e1ea2010-08-03 16:04:46 +10001152 tty_flag = otty_flag;
1153 close(muxserver_sock);
1154 muxserver_sock = -1;
Damien Miller5929c522010-09-10 11:17:02 +10001155 options.control_master = SSHCTL_MASTER_NO;
Damien Millere11e1ea2010-08-03 16:04:46 +10001156 muxclient(options.control_path);
1157 /* muxclient() doesn't return on success. */
1158 fatal("Failed to connect to new control master");
1159 }
Damien Miller00d9ae22010-08-17 01:59:31 +10001160 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
1161 error("%s: open(\"/dev/null\"): %s", __func__,
1162 strerror(errno));
1163 } else {
1164 if (dup2(devnull, STDIN_FILENO) == -1 ||
1165 dup2(devnull, STDOUT_FILENO) == -1)
1166 error("%s: dup2: %s", __func__, strerror(errno));
1167 if (devnull > STDERR_FILENO)
1168 close(devnull);
1169 }
Damien Miller98e27dc2013-07-25 11:55:52 +10001170 daemon(1, 1);
Damien Millerea2c1a42011-06-03 12:10:22 +10001171 setproctitle("%s [mux]", options.control_path);
Damien Millere11e1ea2010-08-03 16:04:46 +10001172}
1173
1174/* Do fork() after authentication. Used by "ssh -f" */
1175static void
1176fork_postauth(void)
1177{
1178 if (need_controlpersist_detach)
1179 control_persist_detach();
1180 debug("forking to background");
1181 fork_after_authentication_flag = 0;
1182 if (daemon(1, 1) < 0)
1183 fatal("daemon() failed: %.200s", strerror(errno));
1184}
1185
Darren Tucker9f407c42008-06-13 04:50:27 +10001186/* Callback for remote forward global requests */
1187static void
1188ssh_confirm_remote_forward(int type, u_int32_t seq, void *ctxt)
1189{
1190 Forward *rfwd = (Forward *)ctxt;
1191
Damien Miller4bf648f2009-02-14 16:28:21 +11001192 /* XXX verbose() on failure? */
Darren Tucker9f407c42008-06-13 04:50:27 +10001193 debug("remote forward %s for: listen %d, connect %s:%d",
1194 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
1195 rfwd->listen_port, rfwd->connect_host, rfwd->connect_port);
Darren Tucker68afb8c2011-10-02 18:59:03 +11001196 if (rfwd->listen_port == 0) {
1197 if (type == SSH2_MSG_REQUEST_SUCCESS) {
1198 rfwd->allocated_port = packet_get_int();
1199 logit("Allocated port %u for remote forward to %s:%d",
1200 rfwd->allocated_port,
1201 rfwd->connect_host, rfwd->connect_port);
1202 channel_update_permitted_opens(rfwd->handle,
1203 rfwd->allocated_port);
1204 } else {
1205 channel_update_permitted_opens(rfwd->handle, -1);
1206 }
Damien Miller4bf648f2009-02-14 16:28:21 +11001207 }
1208
Darren Tucker9f407c42008-06-13 04:50:27 +10001209 if (type == SSH2_MSG_REQUEST_FAILURE) {
1210 if (options.exit_on_forward_failure)
1211 fatal("Error: remote port forwarding failed for "
1212 "listen port %d", rfwd->listen_port);
1213 else
1214 logit("Warning: remote port forwarding failed for "
1215 "listen port %d", rfwd->listen_port);
1216 }
Darren Tucker9a2a6092008-07-04 12:53:50 +10001217 if (++remote_forward_confirms_received == options.num_remote_forwards) {
Darren Tucker9f407c42008-06-13 04:50:27 +10001218 debug("All remote forwarding requests processed");
Damien Millere11e1ea2010-08-03 16:04:46 +10001219 if (fork_after_authentication_flag)
1220 fork_postauth();
Darren Tucker9a2a6092008-07-04 12:53:50 +10001221 }
Darren Tucker9f407c42008-06-13 04:50:27 +10001222}
1223
Ben Lindstrombba81212001-06-25 05:01:22 +00001224static void
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001225client_cleanup_stdio_fwd(int id, void *arg)
1226{
1227 debug("stdio forwarding: done");
1228 cleanup_exit(0);
1229}
1230
Darren Tucker2d6665d2011-11-04 10:54:22 +11001231static void
1232ssh_init_stdio_forwarding(void)
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001233{
1234 Channel *c;
Damien Millere1537f92010-01-26 13:26:22 +11001235 int in, out;
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001236
Darren Tucker2d6665d2011-11-04 10:54:22 +11001237 if (stdio_forward_host == NULL)
1238 return;
Damien Miller386feab2013-10-15 12:14:49 +11001239 if (!compat20)
Darren Tucker2d6665d2011-11-04 10:54:22 +11001240 fatal("stdio forwarding require Protocol 2");
Damien Millere1537f92010-01-26 13:26:22 +11001241
Darren Tucker2d6665d2011-11-04 10:54:22 +11001242 debug3("%s: %s:%d", __func__, stdio_forward_host, stdio_forward_port);
1243
1244 if ((in = dup(STDIN_FILENO)) < 0 ||
1245 (out = dup(STDOUT_FILENO)) < 0)
Damien Millere1537f92010-01-26 13:26:22 +11001246 fatal("channel_connect_stdio_fwd: dup() in/out failed");
Darren Tucker2d6665d2011-11-04 10:54:22 +11001247 if ((c = channel_connect_stdio_fwd(stdio_forward_host,
1248 stdio_forward_port, in, out)) == NULL)
1249 fatal("%s: channel_connect_stdio_fwd failed", __func__);
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001250 channel_register_cleanup(c->self, client_cleanup_stdio_fwd, 0);
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001251}
1252
1253static void
Damien Miller0bc1bd82000-11-13 22:57:25 +11001254ssh_init_forwarding(void)
1255{
Kevin Steves12057502001-02-05 14:54:34 +00001256 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001257 int i;
Kevin Steves12057502001-02-05 14:54:34 +00001258
Damien Miller0bc1bd82000-11-13 22:57:25 +11001259 /* Initiate local TCP/IP port forwardings. */
1260 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001261 debug("Local connections to %.200s:%d forwarded to remote "
1262 "address %.200s:%d",
Darren Tucker47eede72005-03-14 23:08:12 +11001263 (options.local_forwards[i].listen_host == NULL) ?
1264 (options.gateway_ports ? "*" : "LOCALHOST") :
Damien Millerf91ee4c2005-03-01 21:24:33 +11001265 options.local_forwards[i].listen_host,
1266 options.local_forwards[i].listen_port,
1267 options.local_forwards[i].connect_host,
1268 options.local_forwards[i].connect_port);
Damien Millerb16461c2002-01-22 23:29:22 +11001269 success += channel_setup_local_fwd_listener(
Damien Millerf91ee4c2005-03-01 21:24:33 +11001270 options.local_forwards[i].listen_host,
1271 options.local_forwards[i].listen_port,
1272 options.local_forwards[i].connect_host,
1273 options.local_forwards[i].connect_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +11001274 options.gateway_ports);
1275 }
Darren Tuckere7d4b192006-07-12 22:17:10 +10001276 if (i > 0 && success != i && options.exit_on_forward_failure)
1277 fatal("Could not request local forwarding.");
Kevin Steves12057502001-02-05 14:54:34 +00001278 if (i > 0 && success == 0)
1279 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001280
1281 /* Initiate remote TCP/IP port forwardings. */
1282 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001283 debug("Remote connections from %.200s:%d forwarded to "
1284 "local address %.200s:%d",
Damien Miller46d38de2005-07-17 17:02:09 +10001285 (options.remote_forwards[i].listen_host == NULL) ?
Damien Milleraa3bb102005-11-05 15:12:59 +11001286 "LOCALHOST" : options.remote_forwards[i].listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +11001287 options.remote_forwards[i].listen_port,
1288 options.remote_forwards[i].connect_host,
1289 options.remote_forwards[i].connect_port);
Darren Tucker68afb8c2011-10-02 18:59:03 +11001290 options.remote_forwards[i].handle =
1291 channel_request_remote_forwarding(
Damien Millerf91ee4c2005-03-01 21:24:33 +11001292 options.remote_forwards[i].listen_host,
1293 options.remote_forwards[i].listen_port,
1294 options.remote_forwards[i].connect_host,
Darren Tucker68afb8c2011-10-02 18:59:03 +11001295 options.remote_forwards[i].connect_port);
1296 if (options.remote_forwards[i].handle < 0) {
Darren Tuckere7d4b192006-07-12 22:17:10 +10001297 if (options.exit_on_forward_failure)
1298 fatal("Could not request remote forwarding.");
1299 else
1300 logit("Warning: Could not request remote "
1301 "forwarding.");
Darren Tucker68afb8c2011-10-02 18:59:03 +11001302 } else {
1303 client_register_global_confirm(ssh_confirm_remote_forward,
1304 &options.remote_forwards[i]);
Darren Tuckere7d4b192006-07-12 22:17:10 +10001305 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001306 }
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001307
1308 /* Initiate tunnel forwarding. */
1309 if (options.tun_open != SSH_TUNMODE_NO) {
1310 if (client_request_tun_fwd(options.tun_open,
1311 options.tun_local, options.tun_remote) == -1) {
1312 if (options.exit_on_forward_failure)
1313 fatal("Could not request tunnel forwarding.");
1314 else
1315 error("Could not request tunnel forwarding.");
1316 }
1317 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001318}
1319
Ben Lindstrombba81212001-06-25 05:01:22 +00001320static void
Damien Miller0bc1bd82000-11-13 22:57:25 +11001321check_agent_present(void)
1322{
1323 if (options.forward_agent) {
Damien Miller788f2122005-11-05 15:14:59 +11001324 /* Clear agent forwarding if we don't have an agent. */
Damien Miller789e95d2002-09-12 09:52:46 +10001325 if (!ssh_agent_present())
Damien Miller0bc1bd82000-11-13 22:57:25 +11001326 options.forward_agent = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001327 }
1328}
1329
Ben Lindstrombba81212001-06-25 05:01:22 +00001330static int
Damien Miller1383bd82000-04-06 12:32:37 +10001331ssh_session(void)
1332{
1333 int type;
Damien Miller1383bd82000-04-06 12:32:37 +10001334 int interactive = 0;
1335 int have_tty = 0;
1336 struct winsize ws;
Damien Miller1383bd82000-04-06 12:32:37 +10001337 char *cp;
Damien Miller17e7ed02005-06-17 12:54:33 +10001338 const char *display;
Damien Miller1383bd82000-04-06 12:32:37 +10001339
Damien Miller95def091999-11-25 00:26:21 +11001340 /* Enable compression if requested. */
1341 if (options.compression) {
Darren Tuckerd6173c02008-06-13 04:52:53 +10001342 debug("Requesting compression at level %d.",
1343 options.compression_level);
Damien Miller95def091999-11-25 00:26:21 +11001344
Darren Tuckerd6173c02008-06-13 04:52:53 +10001345 if (options.compression_level < 1 ||
1346 options.compression_level > 9)
1347 fatal("Compression level must be from 1 (fast) to "
1348 "9 (slow, best).");
Damien Miller95def091999-11-25 00:26:21 +11001349
1350 /* Send the request. */
1351 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
1352 packet_put_int(options.compression_level);
1353 packet_send();
1354 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +11001355 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +11001356 if (type == SSH_SMSG_SUCCESS)
1357 packet_start_compression(options.compression_level);
1358 else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +10001359 logit("Warning: Remote host refused compression.");
Damien Miller95def091999-11-25 00:26:21 +11001360 else
Darren Tuckerd6173c02008-06-13 04:52:53 +10001361 packet_disconnect("Protocol error waiting for "
1362 "compression response.");
Damien Miller95def091999-11-25 00:26:21 +11001363 }
1364 /* Allocate a pseudo tty if appropriate. */
1365 if (tty_flag) {
1366 debug("Requesting pty.");
1367
1368 /* Start the packet. */
1369 packet_start(SSH_CMSG_REQUEST_PTY);
1370
1371 /* Store TERM in the packet. There is no limit on the
1372 length of the string. */
1373 cp = getenv("TERM");
1374 if (!cp)
1375 cp = "";
Ben Lindstrom664408d2001-06-09 01:42:01 +00001376 packet_put_cstring(cp);
Damien Miller95def091999-11-25 00:26:21 +11001377
1378 /* Store window size in the packet. */
1379 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
1380 memset(&ws, 0, sizeof(ws));
Damien Miller71a73672006-03-26 14:04:36 +11001381 packet_put_int((u_int)ws.ws_row);
1382 packet_put_int((u_int)ws.ws_col);
1383 packet_put_int((u_int)ws.ws_xpixel);
1384 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller95def091999-11-25 00:26:21 +11001385
1386 /* Store tty modes in the packet. */
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001387 tty_make_modes(fileno(stdin), NULL);
Damien Miller95def091999-11-25 00:26:21 +11001388
1389 /* Send the packet, and wait for it to leave. */
1390 packet_send();
1391 packet_write_wait();
1392
1393 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +11001394 type = packet_read();
Damien Miller450a7a12000-03-26 13:04:51 +10001395 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +11001396 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001397 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +10001398 } else if (type == SSH_SMSG_FAILURE)
Darren Tuckerd6173c02008-06-13 04:52:53 +10001399 logit("Warning: Remote host failed or refused to "
1400 "allocate a pseudo tty.");
Damien Miller95def091999-11-25 00:26:21 +11001401 else
Darren Tuckerd6173c02008-06-13 04:52:53 +10001402 packet_disconnect("Protocol error waiting for pty "
1403 "request response.");
Damien Miller95def091999-11-25 00:26:21 +11001404 }
1405 /* Request X11 forwarding if enabled and DISPLAY is set. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001406 display = getenv("DISPLAY");
1407 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001408 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001409 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001410 client_x11_get_proto(display, options.xauth_location,
Damien Miller386feab2013-10-15 12:14:49 +11001411 options.forward_x11_trusted,
Damien Miller1ab6a512010-06-26 10:02:24 +10001412 options.forward_x11_timeout,
1413 &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001414 /* Request forwarding with authentication spoofing. */
Darren Tuckerd6173c02008-06-13 04:52:53 +10001415 debug("Requesting X11 forwarding with authentication "
1416 "spoofing.");
Damien Miller6d7b4372011-06-23 08:31:57 +10001417 x11_request_forwarding_with_spoofing(0, display, proto,
1418 data, 0);
Damien Miller95def091999-11-25 00:26:21 +11001419 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +11001420 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +11001421 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +11001422 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001423 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller996acd22003-04-09 20:59:48 +10001424 logit("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +10001425 } else {
Darren Tuckerd6173c02008-06-13 04:52:53 +10001426 packet_disconnect("Protocol error waiting for X11 "
1427 "forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +10001428 }
Damien Miller95def091999-11-25 00:26:21 +11001429 }
1430 /* Tell the packet module whether this is an interactive session. */
Damien Miller0dac6fb2010-11-20 15:19:38 +11001431 packet_set_interactive(interactive,
1432 options.ip_qos_interactive, options.ip_qos_bulk);
Damien Miller95def091999-11-25 00:26:21 +11001433
1434 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001435 check_agent_present();
1436
Damien Miller95def091999-11-25 00:26:21 +11001437 if (options.forward_agent) {
1438 debug("Requesting authentication agent forwarding.");
1439 auth_request_forwarding();
1440
1441 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +11001442 type = packet_read();
Damien Miller48b03fc2002-01-22 23:11:40 +11001443 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +11001444 if (type != SSH_SMSG_SUCCESS)
Damien Miller996acd22003-04-09 20:59:48 +10001445 logit("Warning: Remote host denied authentication agent forwarding.");
Damien Miller95def091999-11-25 00:26:21 +11001446 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001447
Damien Miller0bc1bd82000-11-13 22:57:25 +11001448 /* Initiate port forwardings. */
Darren Tucker2d6665d2011-11-04 10:54:22 +11001449 ssh_init_stdio_forwarding();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001450 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001451
Darren Tuckerb776c852007-12-02 23:06:35 +11001452 /* Execute a local command */
1453 if (options.local_command != NULL &&
1454 options.permit_local_command)
1455 ssh_local_cmd(options.local_command);
1456
Darren Tucker9a2a6092008-07-04 12:53:50 +10001457 /*
1458 * If requested and we are not interested in replies to remote
1459 * forwarding requests, then let ssh continue in the background.
1460 */
Damien Millere11e1ea2010-08-03 16:04:46 +10001461 if (fork_after_authentication_flag) {
1462 if (options.exit_on_forward_failure &&
1463 options.num_remote_forwards > 0) {
1464 debug("deferring postauth fork until remote forward "
1465 "confirmation received");
1466 } else
1467 fork_postauth();
Darren Tucker9a2a6092008-07-04 12:53:50 +10001468 }
Damien Miller5428f641999-11-25 11:54:57 +11001469
1470 /*
1471 * If a command was specified on the command line, execute the
1472 * command now. Otherwise request the server to start a shell.
1473 */
Damien Miller95def091999-11-25 00:26:21 +11001474 if (buffer_len(&command) > 0) {
1475 int len = buffer_len(&command);
1476 if (len > 900)
1477 len = 900;
Darren Tuckerd6173c02008-06-13 04:52:53 +10001478 debug("Sending command: %.*s", len,
1479 (u_char *)buffer_ptr(&command));
Damien Miller95def091999-11-25 00:26:21 +11001480 packet_start(SSH_CMSG_EXEC_CMD);
1481 packet_put_string(buffer_ptr(&command), buffer_len(&command));
1482 packet_send();
1483 packet_write_wait();
1484 } else {
1485 debug("Requesting shell.");
1486 packet_start(SSH_CMSG_EXEC_SHELL);
1487 packet_send();
1488 packet_write_wait();
1489 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001490
Damien Miller95def091999-11-25 00:26:21 +11001491 /* Enter the interactive session. */
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001492 return client_loop(have_tty, tty_flag ?
1493 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
Damien Miller1383bd82000-04-06 12:32:37 +10001494}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001495
Ben Lindstromf558cf62001-09-20 23:13:49 +00001496/* request pty/x11/agent/tcpfwd/shell for channel */
Ben Lindstrombba81212001-06-25 05:01:22 +00001497static void
Damien Millerd530f5f2010-05-21 14:57:10 +10001498ssh_session2_setup(int id, int success, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +10001499{
Damien Miller3756dce2004-06-18 01:17:29 +10001500 extern char **environ;
Damien Miller17e7ed02005-06-17 12:54:33 +10001501 const char *display;
Damien Miller0e220db2004-06-15 10:34:08 +10001502 int interactive = tty_flag;
Damien Miller17e7ed02005-06-17 12:54:33 +10001503
Damien Millerd530f5f2010-05-21 14:57:10 +10001504 if (!success)
1505 return; /* No need for error message, channels code sens one */
1506
Damien Miller46d38de2005-07-17 17:02:09 +10001507 display = getenv("DISPLAY");
Damien Miller17e7ed02005-06-17 12:54:33 +10001508 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001509 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001510 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001511 client_x11_get_proto(display, options.xauth_location,
Damien Miller1ab6a512010-06-26 10:02:24 +10001512 options.forward_x11_trusted,
1513 options.forward_x11_timeout, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001514 /* Request forwarding with authentication spoofing. */
Darren Tuckerd6173c02008-06-13 04:52:53 +10001515 debug("Requesting X11 forwarding with authentication "
1516 "spoofing.");
Damien Miller6d7b4372011-06-23 08:31:57 +10001517 x11_request_forwarding_with_spoofing(id, display, proto,
1518 data, 1);
1519 client_expect_confirm(id, "X11 forwarding", CONFIRM_WARN);
1520 /* XXX exit_on_forward_failure */
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001521 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001522 }
1523
Damien Miller0bc1bd82000-11-13 22:57:25 +11001524 check_agent_present();
1525 if (options.forward_agent) {
1526 debug("Requesting authentication agent forwarding.");
1527 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1528 packet_send();
1529 }
1530
Darren Tucker7b305012012-07-02 18:55:09 +10001531 /* Tell the packet module whether this is an interactive session. */
1532 packet_set_interactive(interactive,
1533 options.ip_qos_interactive, options.ip_qos_bulk);
1534
Damien Miller0e220db2004-06-15 10:34:08 +10001535 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
Damien Miller5771ed72008-05-19 15:35:33 +10001536 NULL, fileno(stdin), &command, environ);
Damien Miller1383bd82000-04-06 12:32:37 +10001537}
1538
Ben Lindstromf558cf62001-09-20 23:13:49 +00001539/* open new channel for a session */
Ben Lindstrombba81212001-06-25 05:01:22 +00001540static int
Ben Lindstromf558cf62001-09-20 23:13:49 +00001541ssh_session2_open(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001542{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001543 Channel *c;
1544 int window, packetmax, in, out, err;
Damien Millerad833b32000-08-23 10:46:23 +10001545
Damien Millercaf6dd62000-08-29 11:33:50 +11001546 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001547 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001548 } else {
1549 in = dup(STDIN_FILENO);
1550 }
1551 out = dup(STDOUT_FILENO);
1552 err = dup(STDERR_FILENO);
1553
1554 if (in < 0 || out < 0 || err < 0)
1555 fatal("dup() in/out/err failed");
1556
Damien Miller69b69aa2000-10-28 14:19:58 +11001557 /* enable nonblocking unless tty */
1558 if (!isatty(in))
1559 set_nonblock(in);
1560 if (!isatty(out))
1561 set_nonblock(out);
1562 if (!isatty(err))
1563 set_nonblock(err);
1564
Damien Millere4340be2000-09-16 13:29:08 +11001565 window = CHAN_SES_WINDOW_DEFAULT;
1566 packetmax = CHAN_SES_PACKET_DEFAULT;
Damien Miller19a59452002-02-19 15:20:57 +11001567 if (tty_flag) {
1568 window >>= 1;
1569 packetmax >>= 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001570 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001571 c = channel_new(
Damien Miller1383bd82000-04-06 12:32:37 +10001572 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001573 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001574 "client-session", /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001575
Ben Lindstromf558cf62001-09-20 23:13:49 +00001576 debug3("ssh_session2_open: channel_new: %d", c->self);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001577
Ben Lindstrom5ec26452001-06-09 00:18:51 +00001578 channel_send_open(c->self);
Ben Lindstromf558cf62001-09-20 23:13:49 +00001579 if (!no_shell_flag)
Damien Millerb84886b2008-05-19 15:05:07 +10001580 channel_register_open_confirm(c->self,
1581 ssh_session2_setup, NULL);
Damien Miller1383bd82000-04-06 12:32:37 +10001582
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001583 return c->self;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001584}
1585
Ben Lindstrombba81212001-06-25 05:01:22 +00001586static int
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001587ssh_session2(void)
1588{
Ben Lindstromf558cf62001-09-20 23:13:49 +00001589 int id = -1;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001590
1591 /* XXX should be pre-session */
Darren Tucker2d6665d2011-11-04 10:54:22 +11001592 if (!options.control_persist)
1593 ssh_init_stdio_forwarding();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001594 ssh_init_forwarding();
1595
Damien Millere11e1ea2010-08-03 16:04:46 +10001596 /* Start listening for multiplex clients */
1597 muxserver_listen();
1598
1599 /*
Darren Tucker2d6665d2011-11-04 10:54:22 +11001600 * If we are in control persist mode and have a working mux listen
1601 * socket, then prepare to background ourselves and have a foreground
1602 * client attach as a control slave.
1603 * NB. we must save copies of the flags that we override for
Damien Millere11e1ea2010-08-03 16:04:46 +10001604 * the backgrounding, since we defer attachment of the slave until
1605 * after the connection is fully established (in particular,
1606 * async rfwd replies have been received for ExitOnForwardFailure).
1607 */
1608 if (options.control_persist && muxserver_sock != -1) {
1609 ostdin_null_flag = stdin_null_flag;
1610 ono_shell_flag = no_shell_flag;
Damien Miller21771e22011-05-15 08:45:50 +10001611 orequest_tty = options.request_tty;
Damien Millere11e1ea2010-08-03 16:04:46 +10001612 otty_flag = tty_flag;
1613 stdin_null_flag = 1;
1614 no_shell_flag = 1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001615 tty_flag = 0;
1616 if (!fork_after_authentication_flag)
1617 need_controlpersist_detach = 1;
1618 fork_after_authentication_flag = 1;
1619 }
Darren Tucker2d6665d2011-11-04 10:54:22 +11001620 /*
1621 * ControlPersist mux listen socket setup failed, attempt the
1622 * stdio forward setup that we skipped earlier.
1623 */
1624 if (options.control_persist && muxserver_sock == -1)
1625 ssh_init_stdio_forwarding();
Damien Millere11e1ea2010-08-03 16:04:46 +10001626
Ben Lindstromf558cf62001-09-20 23:13:49 +00001627 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1628 id = ssh_session2_open();
Damien Miller649fe022013-07-18 16:13:55 +10001629 else {
1630 packet_set_interactive(
1631 options.control_master == SSHCTL_MASTER_NO,
1632 options.ip_qos_interactive, options.ip_qos_bulk);
1633 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001634
Darren Tucker8901fa92008-06-11 09:34:01 +10001635 /* If we don't expect to open a new session, then disallow it */
Damien Miller456e6f02008-11-03 19:20:10 +11001636 if (options.control_master == SSHCTL_MASTER_NO &&
1637 (datafellows & SSH_NEW_OPENSSH)) {
Darren Tucker8901fa92008-06-11 09:34:01 +10001638 debug("Requesting no-more-sessions@openssh.com");
1639 packet_start(SSH2_MSG_GLOBAL_REQUEST);
1640 packet_put_cstring("no-more-sessions@openssh.com");
1641 packet_put_char(0);
1642 packet_send();
1643 }
1644
Damien Millerd27b9472005-12-13 19:29:02 +11001645 /* Execute a local command */
1646 if (options.local_command != NULL &&
1647 options.permit_local_command)
1648 ssh_local_cmd(options.local_command);
1649
Damien Miller1f25ab42010-07-16 13:56:23 +10001650 /*
1651 * If requested and we are not interested in replies to remote
1652 * forwarding requests, then let ssh continue in the background.
1653 */
Damien Millere11e1ea2010-08-03 16:04:46 +10001654 if (fork_after_authentication_flag) {
1655 if (options.exit_on_forward_failure &&
1656 options.num_remote_forwards > 0) {
1657 debug("deferring postauth fork until remote forward "
1658 "confirmation received");
1659 } else
1660 fork_postauth();
Darren Tucker9a2a6092008-07-04 12:53:50 +10001661 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001662
Darren Tuckerf1de4e52010-01-08 16:54:59 +11001663 if (options.use_roaming)
1664 request_roaming();
1665
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001666 return client_loop(tty_flag, tty_flag ?
1667 options.escape_char : SSH_ESCAPECHAR_NONE, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001668}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001669
Ben Lindstrombba81212001-06-25 05:01:22 +00001670static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001671load_public_identity_files(void)
1672{
Damien Miller6b1d53c2006-03-31 23:13:21 +11001673 char *filename, *cp, thishost[NI_MAXHOST];
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001674 char *pwdir = NULL, *pwname = NULL;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001675 int i = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001676 Key *public;
Damien Miller6b1d53c2006-03-31 23:13:21 +11001677 struct passwd *pw;
Damien Miller0a80ca12010-02-27 07:55:05 +11001678 u_int n_ids;
1679 char *identity_files[SSH_MAX_IDENTITY_FILES];
1680 Key *identity_keys[SSH_MAX_IDENTITY_FILES];
Damien Miller7ea845e2010-02-12 09:21:02 +11001681#ifdef ENABLE_PKCS11
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001682 Key **keys;
Damien Miller7ea845e2010-02-12 09:21:02 +11001683 int nkeys;
Damien Miller0a80ca12010-02-27 07:55:05 +11001684#endif /* PKCS11 */
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001685
Damien Miller0a80ca12010-02-27 07:55:05 +11001686 n_ids = 0;
1687 bzero(identity_files, sizeof(identity_files));
1688 bzero(identity_keys, sizeof(identity_keys));
1689
1690#ifdef ENABLE_PKCS11
Damien Miller7ea845e2010-02-12 09:21:02 +11001691 if (options.pkcs11_provider != NULL &&
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001692 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
Damien Miller7ea845e2010-02-12 09:21:02 +11001693 (pkcs11_init(!options.batch_mode) == 0) &&
1694 (nkeys = pkcs11_add_provider(options.pkcs11_provider, NULL,
1695 &keys)) > 0) {
Damien Miller7ea845e2010-02-12 09:21:02 +11001696 for (i = 0; i < nkeys; i++) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001697 if (n_ids >= SSH_MAX_IDENTITY_FILES) {
1698 key_free(keys[i]);
1699 continue;
1700 }
1701 identity_keys[n_ids] = keys[i];
1702 identity_files[n_ids] =
Damien Miller7ea845e2010-02-12 09:21:02 +11001703 xstrdup(options.pkcs11_provider); /* XXX */
Damien Miller0a80ca12010-02-27 07:55:05 +11001704 n_ids++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001705 }
Darren Tuckera627d422013-06-02 07:31:17 +10001706 free(keys);
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001707 }
Damien Miller7ea845e2010-02-12 09:21:02 +11001708#endif /* ENABLE_PKCS11 */
Damien Miller6b1d53c2006-03-31 23:13:21 +11001709 if ((pw = getpwuid(original_real_uid)) == NULL)
1710 fatal("load_public_identity_files: getpwuid failed");
Darren Tuckere143f062007-12-02 23:21:16 +11001711 pwname = xstrdup(pw->pw_name);
1712 pwdir = xstrdup(pw->pw_dir);
Damien Miller6b1d53c2006-03-31 23:13:21 +11001713 if (gethostname(thishost, sizeof(thishost)) == -1)
1714 fatal("load_public_identity_files: gethostname: %s",
1715 strerror(errno));
Damien Miller0a80ca12010-02-27 07:55:05 +11001716 for (i = 0; i < options.num_identity_files; i++) {
Darren Tucker15fd19c2013-04-05 11:22:26 +11001717 if (n_ids >= SSH_MAX_IDENTITY_FILES ||
1718 strcasecmp(options.identity_files[i], "none") == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +10001719 free(options.identity_files[i]);
Damien Miller0a80ca12010-02-27 07:55:05 +11001720 continue;
1721 }
Damien Miller6b1d53c2006-03-31 23:13:21 +11001722 cp = tilde_expand_filename(options.identity_files[i],
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001723 original_real_uid);
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001724 filename = percent_expand(cp, "d", pwdir,
1725 "u", pwname, "l", thishost, "h", host,
Damien Miller6b1d53c2006-03-31 23:13:21 +11001726 "r", options.user, (char *)NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001727 free(cp);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001728 public = key_load_public(filename, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001729 debug("identity file %s type %d", filename,
1730 public ? public->type : -1);
Darren Tuckera627d422013-06-02 07:31:17 +10001731 free(options.identity_files[i]);
Damien Miller0a80ca12010-02-27 07:55:05 +11001732 identity_files[n_ids] = filename;
1733 identity_keys[n_ids] = public;
1734
1735 if (++n_ids >= SSH_MAX_IDENTITY_FILES)
1736 continue;
1737
1738 /* Try to add the certificate variant too */
1739 xasprintf(&cp, "%s-cert", filename);
1740 public = key_load_public(cp, NULL);
1741 debug("identity file %s type %d", cp,
1742 public ? public->type : -1);
1743 if (public == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +10001744 free(cp);
Damien Miller0a80ca12010-02-27 07:55:05 +11001745 continue;
1746 }
1747 if (!key_is_cert(public)) {
1748 debug("%s: key %s type %s is not a certificate",
1749 __func__, cp, key_type(public));
1750 key_free(public);
Darren Tuckera627d422013-06-02 07:31:17 +10001751 free(cp);
Damien Miller0a80ca12010-02-27 07:55:05 +11001752 continue;
1753 }
1754 identity_keys[n_ids] = public;
1755 /* point to the original path, most likely the private key */
1756 identity_files[n_ids] = xstrdup(filename);
1757 n_ids++;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001758 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001759 options.num_identity_files = n_ids;
1760 memcpy(options.identity_files, identity_files, sizeof(identity_files));
1761 memcpy(options.identity_keys, identity_keys, sizeof(identity_keys));
1762
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001763 bzero(pwname, strlen(pwname));
Darren Tuckera627d422013-06-02 07:31:17 +10001764 free(pwname);
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001765 bzero(pwdir, strlen(pwdir));
Darren Tuckera627d422013-06-02 07:31:17 +10001766 free(pwdir);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001767}
Damien Miller857b02e2010-09-24 22:02:56 +10001768
1769static void
1770main_sigchld_handler(int sig)
1771{
1772 int save_errno = errno;
1773 pid_t pid;
1774 int status;
1775
1776 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
1777 (pid < 0 && errno == EINTR))
1778 ;
1779
1780 signal(sig, main_sigchld_handler);
1781 errno = save_errno;
1782}