blob: 543a3bafd509ee4761446d11f5f2d4d37eb5399a [file] [log] [blame]
Damien Miller5be9d9e2013-12-07 11:24:01 +11001/* $OpenBSD: ssh.c,v 1.396 2013/12/06 13:39:49 markus 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 Millerd937dc02013-12-05 10:19:54 +1100202" [-L [bind_address:]port:host:hostport] [-l login_name] [-m mac_spec]\n"
Damien Millerbdb352a2013-12-05 10:20:52 +1100203" [-O ctl_cmd] [-o option] [-p port]\n"
204" [-Q cipher | cipher-auth | mac | kex | key]\n"
205" [-R [bind_address:]port:host:hostport] [-S ctl_path] [-W host:port]\n"
206" [-w local_tun[:remote_tun]] [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 Millerd937dc02013-12-05 10:19:54 +1100520 case 'Q':
Damien Millerea111192013-04-23 19:24:32 +1000521 cp = NULL;
Damien Millerd937dc02013-12-05 10:19:54 +1100522 if (strcmp(optarg, "cipher") == 0)
Damien Miller0fde8ac2013-11-21 14:12:23 +1100523 cp = cipher_alg_list('\n', 0);
Damien Millerd937dc02013-12-05 10:19:54 +1100524 else if (strcmp(optarg, "cipher-auth") == 0)
Damien Miller0fde8ac2013-11-21 14:12:23 +1100525 cp = cipher_alg_list('\n', 1);
Damien Millerd937dc02013-12-05 10:19:54 +1100526 else if (strcmp(optarg, "mac") == 0)
Damien Miller690d9892013-11-08 12:16:49 +1100527 cp = mac_alg_list('\n');
Damien Millerd937dc02013-12-05 10:19:54 +1100528 else if (strcmp(optarg, "kex") == 0)
Damien Miller690d9892013-11-08 12:16:49 +1100529 cp = kex_alg_list('\n');
Damien Millerd937dc02013-12-05 10:19:54 +1100530 else if (strcmp(optarg, "key") == 0)
Damien Miller5be9d9e2013-12-07 11:24:01 +1100531 cp = key_alg_list(0, 0);
532 else if (strcmp(optarg, "key-cert") == 0)
533 cp = key_alg_list(1, 0);
534 else if (strcmp(optarg, "key-plain") == 0)
535 cp = key_alg_list(0, 1);
Damien Millerea111192013-04-23 19:24:32 +1000536 if (cp == NULL)
537 fatal("Unsupported query \"%s\"", optarg);
538 printf("%s\n", cp);
539 free(cp);
540 exit(0);
541 break;
Damien Miller95def091999-11-25 00:26:21 +1100542 case 'a':
543 options.forward_agent = 0;
544 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000545 case 'A':
546 options.forward_agent = 1;
547 break;
Damien Miller95def091999-11-25 00:26:21 +1100548 case 'k':
Damien Millere0113cc2003-11-24 13:10:09 +1100549 options.gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100550 break;
Darren Tucker415bddc2007-06-12 23:43:16 +1000551 case 'K':
552 options.gss_authentication = 1;
553 options.gss_deleg_creds = 1;
554 break;
Damien Miller95def091999-11-25 00:26:21 +1100555 case 'i':
556 if (stat(optarg, &st) < 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000557 fprintf(stderr, "Warning: Identity file %s "
Damien Miller3eb48b62005-03-01 21:15:46 +1100558 "not accessible: %s.\n", optarg,
559 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100560 break;
561 }
Darren Tucker19104782013-04-05 11:13:08 +1100562 add_identity_file(&options, NULL, optarg, 1);
Damien Miller95def091999-11-25 00:26:21 +1100563 break;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000564 case 'I':
Damien Miller7ea845e2010-02-12 09:21:02 +1100565#ifdef ENABLE_PKCS11
566 options.pkcs11_provider = xstrdup(optarg);
Ben Lindstrombcc18082001-08-06 21:59:25 +0000567#else
Damien Miller7ea845e2010-02-12 09:21:02 +1100568 fprintf(stderr, "no support for PKCS#11.\n");
Ben Lindstrombcc18082001-08-06 21:59:25 +0000569#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000570 break;
Damien Miller95def091999-11-25 00:26:21 +1100571 case 't':
Damien Miller21771e22011-05-15 08:45:50 +1000572 if (options.request_tty == REQUEST_TTY_YES)
573 options.request_tty = REQUEST_TTY_FORCE;
574 else
575 options.request_tty = REQUEST_TTY_YES;
Damien Miller95def091999-11-25 00:26:21 +1100576 break;
Damien Miller95def091999-11-25 00:26:21 +1100577 case 'v':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000578 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100579 debug_flag = 1;
580 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000581 } else {
582 if (options.log_level < SYSLOG_LEVEL_DEBUG3)
583 options.log_level++;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000584 }
Damien Miller03d4d7e2013-04-23 15:21:06 +1000585 break;
Damien Miller95def091999-11-25 00:26:21 +1100586 case 'V':
Damien Miller0c889cd2004-03-22 09:36:00 +1100587 fprintf(stderr, "%s, %s\n",
Damien Miller2aa6d3c2004-09-12 16:53:04 +1000588 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
Damien Miller95def091999-11-25 00:26:21 +1100589 if (opt == 'V')
590 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100591 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100592 case 'w':
Damien Miller7b58e802005-12-13 19:33:19 +1100593 if (options.tun_open == -1)
594 options.tun_open = SSH_TUNMODE_DEFAULT;
Damien Millerd27b9472005-12-13 19:29:02 +1100595 options.tun_local = a2tun(optarg, &options.tun_remote);
Damien Miller7b58e802005-12-13 19:33:19 +1100596 if (options.tun_local == SSH_TUNID_ERR) {
Darren Tuckerd6173c02008-06-13 04:52:53 +1000597 fprintf(stderr,
598 "Bad tun device '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100599 exit(255);
Damien Millerd27b9472005-12-13 19:29:02 +1100600 }
601 break;
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100602 case 'W':
Damien Millere1537f92010-01-26 13:26:22 +1100603 if (stdio_forward_host != NULL)
604 fatal("stdio forward already specified");
605 if (muxclient_command != 0)
606 fatal("Cannot specify stdio forward with -O");
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100607 if (parse_forward(&fwd, optarg, 1, 0)) {
608 stdio_forward_host = fwd.listen_host;
609 stdio_forward_port = fwd.listen_port;
Darren Tuckera627d422013-06-02 07:31:17 +1000610 free(fwd.connect_host);
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100611 } else {
612 fprintf(stderr,
613 "Bad stdio forwarding specification '%s'\n",
614 optarg);
615 exit(255);
616 }
Damien Miller21771e22011-05-15 08:45:50 +1000617 options.request_tty = REQUEST_TTY_NO;
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100618 no_shell_flag = 1;
619 options.clear_forwardings = 1;
620 options.exit_on_forward_failure = 1;
621 break;
Damien Miller95def091999-11-25 00:26:21 +1100622 case 'q':
623 options.log_level = SYSLOG_LEVEL_QUIET;
624 break;
Damien Miller95def091999-11-25 00:26:21 +1100625 case 'e':
626 if (optarg[0] == '^' && optarg[2] == 0 &&
Damien Millerf4614452001-07-14 12:18:10 +1000627 (u_char) optarg[1] >= 64 &&
628 (u_char) optarg[1] < 128)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000629 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100630 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000631 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100632 else if (strcmp(optarg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000633 options.escape_char = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100634 else {
Damien Millerf4614452001-07-14 12:18:10 +1000635 fprintf(stderr, "Bad escape character '%s'.\n",
636 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100637 exit(255);
Damien Miller95def091999-11-25 00:26:21 +1100638 }
639 break;
Damien Miller95def091999-11-25 00:26:21 +1100640 case 'c':
Damien Millereba71ba2000-04-29 23:57:08 +1000641 if (ciphers_valid(optarg)) {
642 /* SSH2 only */
643 options.ciphers = xstrdup(optarg);
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000644 options.cipher = SSH_CIPHER_INVALID;
Damien Millereba71ba2000-04-29 23:57:08 +1000645 } else {
646 /* SSH1 only */
Damien Millere39cacc2000-11-29 12:18:44 +1100647 options.cipher = cipher_number(optarg);
648 if (options.cipher == -1) {
Damien Millerf4614452001-07-14 12:18:10 +1000649 fprintf(stderr,
650 "Unknown cipher type '%s'\n",
651 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100652 exit(255);
Damien Millereba71ba2000-04-29 23:57:08 +1000653 }
Damien Millerf4614452001-07-14 12:18:10 +1000654 if (options.cipher == SSH_CIPHER_3DES)
Damien Millere39cacc2000-11-29 12:18:44 +1100655 options.ciphers = "3des-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000656 else if (options.cipher == SSH_CIPHER_BLOWFISH)
Damien Millere39cacc2000-11-29 12:18:44 +1100657 options.ciphers = "blowfish-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000658 else
Damien Millere39cacc2000-11-29 12:18:44 +1100659 options.ciphers = (char *)-1;
Damien Miller95def091999-11-25 00:26:21 +1100660 }
661 break;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000662 case 'm':
663 if (mac_valid(optarg))
664 options.macs = xstrdup(optarg);
665 else {
Damien Millerf4614452001-07-14 12:18:10 +1000666 fprintf(stderr, "Unknown mac type '%s'\n",
667 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100668 exit(255);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000669 }
670 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000671 case 'M':
Damien Millerd14b1e72005-06-16 13:19:41 +1000672 if (options.control_master == SSHCTL_MASTER_YES)
673 options.control_master = SSHCTL_MASTER_ASK;
674 else
675 options.control_master = SSHCTL_MASTER_YES;
Damien Miller0e220db2004-06-15 10:34:08 +1000676 break;
Damien Miller95def091999-11-25 00:26:21 +1100677 case 'p':
Ben Lindstrom19066a12001-04-12 23:39:26 +0000678 options.port = a2port(optarg);
Damien Miller3dc71ad2009-01-28 16:31:22 +1100679 if (options.port <= 0) {
Ben Lindstrom146edb92001-04-11 23:06:28 +0000680 fprintf(stderr, "Bad port '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100681 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000682 }
Damien Miller95def091999-11-25 00:26:21 +1100683 break;
Damien Miller95def091999-11-25 00:26:21 +1100684 case 'l':
685 options.user = optarg;
686 break;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000687
Damien Miller95def091999-11-25 00:26:21 +1100688 case 'L':
Damien Miller4bf648f2009-02-14 16:28:21 +1100689 if (parse_forward(&fwd, optarg, 0, 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +1100690 add_local_forward(&options, &fwd);
691 else {
Damien Millerf4614452001-07-14 12:18:10 +1000692 fprintf(stderr,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100693 "Bad local forwarding specification '%s'\n",
Damien Millerf4614452001-07-14 12:18:10 +1000694 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100695 exit(255);
Ben Lindstrom1a174712001-09-12 17:56:15 +0000696 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100697 break;
698
699 case 'R':
Damien Miller4bf648f2009-02-14 16:28:21 +1100700 if (parse_forward(&fwd, optarg, 0, 1)) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100701 add_remote_forward(&options, &fwd);
702 } else {
703 fprintf(stderr,
704 "Bad remote forwarding specification "
705 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100706 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100707 }
Damien Miller95def091999-11-25 00:26:21 +1100708 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000709
710 case 'D':
Damien Miller4bf648f2009-02-14 16:28:21 +1100711 if (parse_forward(&fwd, optarg, 1, 0)) {
Damien Millera699d952008-11-03 19:27:34 +1100712 add_local_forward(&options, &fwd);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100713 } else {
Damien Millera699d952008-11-03 19:27:34 +1100714 fprintf(stderr,
715 "Bad dynamic forwarding specification "
716 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100717 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000718 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000719 break;
720
Damien Miller95def091999-11-25 00:26:21 +1100721 case 'C':
722 options.compression = 1;
723 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000724 case 'N':
725 no_shell_flag = 1;
Damien Miller21771e22011-05-15 08:45:50 +1000726 options.request_tty = REQUEST_TTY_NO;
Damien Miller1383bd82000-04-06 12:32:37 +1000727 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000728 case 'T':
Damien Miller21771e22011-05-15 08:45:50 +1000729 options.request_tty = REQUEST_TTY_NO;
Damien Miller1383bd82000-04-06 12:32:37 +1000730 break;
Damien Miller95def091999-11-25 00:26:21 +1100731 case 'o':
Damien Miller9836cf82003-12-17 16:30:06 +1100732 line = xstrdup(optarg);
Damien Miller194fd902013-10-15 12:13:05 +1100733 if (process_config_line(&options, pw, host ? host : "",
734 line, "command-line", 0, NULL, SSHCONF_USERCONF)
Darren Tuckeraefa3682013-04-05 11:18:35 +1100735 != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100736 exit(255);
Darren Tuckera627d422013-06-02 07:31:17 +1000737 free(line);
Damien Miller95def091999-11-25 00:26:21 +1100738 break;
Damien Miller832562e2001-01-30 09:30:01 +1100739 case 's':
740 subsystem_flag = 1;
741 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000742 case 'S':
743 if (options.control_path != NULL)
744 free(options.control_path);
745 options.control_path = xstrdup(optarg);
Damien Miller0e220db2004-06-15 10:34:08 +1000746 break;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000747 case 'b':
748 options.bind_address = optarg;
749 break;
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000750 case 'F':
751 config = optarg;
752 break;
Damien Miller95def091999-11-25 00:26:21 +1100753 default:
754 usage();
755 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000756 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000757
Damien Millerf4614452001-07-14 12:18:10 +1000758 ac -= optind;
759 av += optind;
760
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100761 if (ac > 0 && !host) {
Ben Lindstromc276c122002-12-23 02:14:51 +0000762 if (strrchr(*av, '@')) {
Damien Millerf4614452001-07-14 12:18:10 +1000763 p = xstrdup(*av);
Ben Lindstromc276c122002-12-23 02:14:51 +0000764 cp = strrchr(p, '@');
Damien Millerf4614452001-07-14 12:18:10 +1000765 if (cp == NULL || cp == p)
766 usage();
767 options.user = p;
768 *cp = '\0';
Damien Miller0faf7472013-10-17 11:47:23 +1100769 host = xstrdup(++cp);
Damien Millerf4614452001-07-14 12:18:10 +1000770 } else
Damien Miller0faf7472013-10-17 11:47:23 +1100771 host = xstrdup(*av);
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000772 if (ac > 1) {
773 optind = optreset = 1;
Damien Millerf4614452001-07-14 12:18:10 +1000774 goto again;
775 }
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000776 ac--, av++;
Damien Millerf4614452001-07-14 12:18:10 +1000777 }
778
Damien Miller95def091999-11-25 00:26:21 +1100779 /* Check that we got a host name. */
780 if (!host)
781 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000782
Damien Miller0faf7472013-10-17 11:47:23 +1100783 lowercase(host);
784 host_arg = xstrdup(host);
785
Damien Miller4314c2b2010-09-10 11:12:09 +1000786 OpenSSL_add_all_algorithms();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100787 ERR_load_crypto_strings();
Damien Millercb5e44a2000-09-29 12:12:36 +1100788
Damien Miller95def091999-11-25 00:26:21 +1100789 /* Initialize the command to execute on remote host. */
790 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000791
Damien Miller5428f641999-11-25 11:54:57 +1100792 /*
793 * Save the command to execute on the remote host in a buffer. There
794 * is no limit on the length of the command, except by the maximum
795 * packet size. Also sets the tty flag if there is no command.
796 */
Damien Millerf4614452001-07-14 12:18:10 +1000797 if (!ac) {
Damien Miller95def091999-11-25 00:26:21 +1100798 /* No command specified - execute shell on a tty. */
Damien Miller832562e2001-01-30 09:30:01 +1100799 if (subsystem_flag) {
Damien Millerf4614452001-07-14 12:18:10 +1000800 fprintf(stderr,
801 "You must specify a subsystem to invoke.\n");
Damien Miller832562e2001-01-30 09:30:01 +1100802 usage();
803 }
Damien Miller95def091999-11-25 00:26:21 +1100804 } else {
Damien Millerf4614452001-07-14 12:18:10 +1000805 /* A command has been specified. Store it into the buffer. */
806 for (i = 0; i < ac; i++) {
807 if (i)
Damien Miller95def091999-11-25 00:26:21 +1100808 buffer_append(&command, " ", 1);
809 buffer_append(&command, av[i], strlen(av[i]));
810 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000811 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000812
Damien Miller95def091999-11-25 00:26:21 +1100813 /* Cannot fork to background if no command. */
Darren Tuckerd6173c02008-06-13 04:52:53 +1000814 if (fork_after_authentication_flag && buffer_len(&command) == 0 &&
815 !no_shell_flag)
816 fatal("Cannot fork into background without a command "
817 "to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000818
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000819 /*
820 * Initialize "log" output. Since we are the client all output
Damien Miller03d4d7e2013-04-23 15:21:06 +1000821 * goes to stderr unless otherwise specified by -y or -E.
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000822 */
Damien Miller03d4d7e2013-04-23 15:21:06 +1000823 if (use_syslog && logfile != NULL)
824 fatal("Can't specify both -y and -E");
825 if (logfile != NULL) {
826 log_redirect_stderr_to(logfile);
Darren Tuckera627d422013-06-02 07:31:17 +1000827 free(logfile);
Damien Miller03d4d7e2013-04-23 15:21:06 +1000828 }
Darren Tucker72efd742009-06-21 17:48:00 +1000829 log_init(argv0,
Darren Tuckerd6173c02008-06-13 04:52:53 +1000830 options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
Damien Millere272a5b2008-11-03 19:22:37 +1100831 SYSLOG_FACILITY_USER, !use_syslog);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000832
Damien Miller03d4d7e2013-04-23 15:21:06 +1000833 if (debug_flag)
834 logit("%s, %s", SSH_VERSION, SSLeay_version(SSLEAY_VERSION));
835
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000836 /*
837 * Read per-user configuration file. Ignore the system wide config
838 * file if the user specifies a config file on the command line.
839 */
840 if (config != NULL) {
Damien Miller508b6c32013-04-23 15:18:28 +1000841 if (strcasecmp(config, "none") != 0 &&
Damien Miller194fd902013-10-15 12:13:05 +1100842 !read_config_file(config, pw, host, &options,
843 SSHCONF_USERCONF))
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000844 fatal("Can't open user config file %.100s: "
845 "%.100s", config, strerror(errno));
Damien Miller80163902007-01-05 16:30:16 +1100846 } else {
Darren Tucker199b1342009-07-06 07:16:56 +1000847 r = snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir,
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000848 _PATH_SSH_USER_CONFFILE);
Darren Tucker199b1342009-07-06 07:16:56 +1000849 if (r > 0 && (size_t)r < sizeof(buf))
Damien Miller194fd902013-10-15 12:13:05 +1100850 (void)read_config_file(buf, pw, host, &options,
Darren Tuckeraefa3682013-04-05 11:18:35 +1100851 SSHCONF_CHECKPERM|SSHCONF_USERCONF);
Ben Lindstrom83f07d12001-10-03 17:22:29 +0000852
Damien Miller765f8c42011-08-06 06:18:16 +1000853 /* Read systemwide configuration file after user config. */
Damien Miller194fd902013-10-15 12:13:05 +1100854 (void)read_config_file(_PATH_HOST_CONFIG_FILE, pw, host,
Damien Miller57a44762004-04-20 20:11:57 +1000855 &options, 0);
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000856 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000857
Damien Miller95def091999-11-25 00:26:21 +1100858 /* Fill configuration defaults. */
859 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000860
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000861 channel_set_af(options.address_family);
862
Damien Millere9fc72e2013-10-15 12:14:12 +1100863 /* Tidy and check options */
864 if (options.host_key_alias != NULL)
865 lowercase(options.host_key_alias);
866 if (options.proxy_command != NULL &&
867 strcmp(options.proxy_command, "-") == 0 &&
868 options.proxy_use_fdpass)
869 fatal("ProxyCommand=- and ProxyUseFDPass are incompatible");
Damien Miller0faf7472013-10-17 11:47:23 +1100870#ifndef HAVE_CYGWIN
871 if (original_effective_uid != 0)
872 options.use_privileged_port = 0;
873#endif
Damien Millere9fc72e2013-10-15 12:14:12 +1100874
Damien Miller95def091999-11-25 00:26:21 +1100875 /* reinit */
Darren Tucker72efd742009-06-21 17:48:00 +1000876 log_init(argv0, options.log_level, SYSLOG_FACILITY_USER, !use_syslog);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000877
Damien Millerfff9f092012-07-06 13:45:01 +1000878 if (options.request_tty == REQUEST_TTY_YES ||
879 options.request_tty == REQUEST_TTY_FORCE)
880 tty_flag = 1;
881
882 /* Allocate a tty by default if no command specified. */
883 if (buffer_len(&command) == 0)
884 tty_flag = options.request_tty != REQUEST_TTY_NO;
885
886 /* Force no tty */
887 if (options.request_tty == REQUEST_TTY_NO || muxclient_command != 0)
888 tty_flag = 0;
889 /* Do not allocate a tty if stdin is not a tty. */
890 if ((!isatty(fileno(stdin)) || stdin_null_flag) &&
891 options.request_tty != REQUEST_TTY_FORCE) {
892 if (tty_flag)
893 logit("Pseudo-terminal will not be allocated because "
894 "stdin is not a terminal.");
895 tty_flag = 0;
896 }
897
Damien Miller60bc5172001-03-19 09:38:15 +1100898 seed_rng();
899
Damien Miller95def091999-11-25 00:26:21 +1100900 if (options.user == NULL)
901 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000902
Darren Tuckerf6b01b72008-06-13 04:56:37 +1000903 /* Get default port if port has not been set. */
Damien Miller194fd902013-10-15 12:13:05 +1100904 if (options.port == 0)
905 options.port = default_ssh_port();
Darren Tuckerf6b01b72008-06-13 04:56:37 +1000906
Damien Miller83f8a402011-01-07 09:51:17 +1100907 /* preserve host name given on command line for %n expansion */
Damien Millerd0244d42010-07-16 13:56:43 +1000908 if (options.hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100909 /* NB. Please keep in sync with readconf.c:match_cfg_line() */
Damien Miller0faf7472013-10-17 11:47:23 +1100910 cp = percent_expand(options.hostname,
Damien Millerd0244d42010-07-16 13:56:43 +1000911 "h", host, (char *)NULL);
Damien Miller0faf7472013-10-17 11:47:23 +1100912 free(host);
913 host = cp;
914 }
915
Damien Miller51682fa2013-10-17 11:48:31 +1100916 /* If canonicalization requested then try to apply it */
Damien Miller38505592013-10-17 11:48:13 +1100917 if (options.canonicalize_hostname != SSH_CANONICALISE_NO)
918 addrs = resolve_canonicalize(&host, options.port);
Damien Miller0faf7472013-10-17 11:47:23 +1100919 /*
Damien Miller51682fa2013-10-17 11:48:31 +1100920 * If canonicalization not requested, or if it failed then try to
Damien Miller0faf7472013-10-17 11:47:23 +1100921 * resolve the bare hostname name using the system resolver's usual
Damien Millera90c0332013-10-24 21:03:17 +1100922 * search rules. Skip the lookup if a ProxyCommand is being used
923 * unless the user has specifically requested canonicalisation.
Damien Miller0faf7472013-10-17 11:47:23 +1100924 */
Damien Millera90c0332013-10-24 21:03:17 +1100925 if (addrs == NULL && (options.proxy_command == NULL ||
926 options.canonicalize_hostname == SSH_CANONICALISE_ALWAYS)) {
Damien Miller0faf7472013-10-17 11:47:23 +1100927 if ((addrs = resolve_host(host, options.port, 1,
928 cname, sizeof(cname))) == NULL)
929 cleanup_exit(255); /* resolve_host logs the error */
930 check_follow_cname(&host, cname);
Damien Millerd0244d42010-07-16 13:56:43 +1000931 }
932
Damien Millerdfc85fa2011-05-15 08:44:02 +1000933 if (gethostname(thishost, sizeof(thishost)) == -1)
934 fatal("gethostname: %s", strerror(errno));
935 strlcpy(shorthost, thishost, sizeof(shorthost));
936 shorthost[strcspn(thishost, ".")] = '\0';
937 snprintf(portstr, sizeof(portstr), "%d", options.port);
Darren Tuckerf6b01b72008-06-13 04:56:37 +1000938
Damien Millerdfc85fa2011-05-15 08:44:02 +1000939 if (options.local_command != NULL) {
Darren Tuckerf6b01b72008-06-13 04:56:37 +1000940 debug3("expanding LocalCommand: %s", options.local_command);
941 cp = options.local_command;
942 options.local_command = percent_expand(cp, "d", pw->pw_dir,
Damien Miller83f8a402011-01-07 09:51:17 +1100943 "h", host, "l", thishost, "n", host_arg, "r", options.user,
Damien Millerdfc85fa2011-05-15 08:44:02 +1000944 "p", portstr, "u", pw->pw_name, "L", shorthost,
945 (char *)NULL);
Darren Tuckerf6b01b72008-06-13 04:56:37 +1000946 debug3("expanded LocalCommand: %s", options.local_command);
Darren Tuckera627d422013-06-02 07:31:17 +1000947 free(cp);
Darren Tuckerf6b01b72008-06-13 04:56:37 +1000948 }
949
Damien Miller0e220db2004-06-15 10:34:08 +1000950 if (options.control_path != NULL) {
Damien Miller6476cad2005-06-16 13:18:34 +1000951 cp = tilde_expand_filename(options.control_path,
952 original_real_uid);
Darren Tuckera627d422013-06-02 07:31:17 +1000953 free(options.control_path);
Damien Millerdfc85fa2011-05-15 08:44:02 +1000954 options.control_path = percent_expand(cp, "h", host,
955 "l", thishost, "n", host_arg, "r", options.user,
956 "p", portstr, "u", pw->pw_name, "L", shorthost,
957 (char *)NULL);
Darren Tuckera627d422013-06-02 07:31:17 +1000958 free(cp);
Damien Miller0e220db2004-06-15 10:34:08 +1000959 }
Damien Millerb1cbfa22008-05-19 16:00:08 +1000960 if (muxclient_command != 0 && options.control_path == NULL)
Darren Tucker0814d312005-06-01 23:08:51 +1000961 fatal("No ControlPath specified for \"-O\" command");
Damien Millerd14b1e72005-06-16 13:19:41 +1000962 if (options.control_path != NULL)
Damien Millerb1cbfa22008-05-19 16:00:08 +1000963 muxclient(options.control_path);
Damien Miller0e220db2004-06-15 10:34:08 +1000964
Damien Miller67bd0622007-09-17 12:06:57 +1000965 timeout_ms = options.connection_timeout * 1000;
966
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000967 /* Open a connection to the remote host. */
Damien Miller0faf7472013-10-17 11:47:23 +1100968 if (ssh_connect(host, addrs, &hostaddr, options.port,
969 options.address_family, options.connection_attempts,
970 &timeout_ms, options.tcp_keep_alive,
971 options.use_privileged_port) != 0)
972 exit(255);
973
Damien Miller28631ce2013-10-26 10:07:56 +1100974 if (addrs != NULL)
975 freeaddrinfo(addrs);
976
Damien Miller0faf7472013-10-17 11:47:23 +1100977 packet_set_timeout(options.server_alive_interval,
978 options.server_alive_count_max);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000979
Damien Miller67bd0622007-09-17 12:06:57 +1000980 if (timeout_ms > 0)
981 debug3("timeout: %d ms remain after connect", timeout_ms);
982
Damien Miller5428f641999-11-25 11:54:57 +1100983 /*
984 * If we successfully made the connection, load the host private key
985 * in case we will need it later for combined rsa-rhosts
986 * authentication. This must be done before releasing extra
987 * privileges, because the file is only readable by root.
Ben Lindstrom9e5bb572002-06-06 19:58:27 +0000988 * If we cannot access the private keys, load the public keys
989 * instead and try to execute the ssh-keysign helper instead.
Damien Miller5428f641999-11-25 11:54:57 +1100990 */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000991 sensitive_data.nkeys = 0;
992 sensitive_data.keys = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000993 sensitive_data.external_keysign = 0;
Ben Lindstrom24157572002-06-12 16:09:39 +0000994 if (options.rhosts_rsa_authentication ||
995 options.hostbased_authentication) {
Damien Miller5be9d9e2013-12-07 11:24:01 +1100996 sensitive_data.nkeys = 8;
Damien Millerddd63ab2006-03-31 23:10:51 +1100997 sensitive_data.keys = xcalloc(sensitive_data.nkeys,
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000998 sizeof(Key));
Damien Miller6af914a2010-09-10 11:39:26 +1000999 for (i = 0; i < sensitive_data.nkeys; i++)
1000 sensitive_data.keys[i] = NULL;
Ben Lindstromf9c48842002-06-11 16:37:51 +00001001
1002 PRIV_START;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001003 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
Darren Tucker232b76f2006-05-06 17:41:51 +10001004 _PATH_HOST_KEY_FILE, "", NULL, NULL);
Damien Millerc1583312010-08-05 13:04:50 +10001005 sensitive_data.keys[1] = key_load_private_cert(KEY_DSA,
1006 _PATH_HOST_DSA_KEY_FILE, "", NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001007#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001008 sensitive_data.keys[2] = key_load_private_cert(KEY_ECDSA,
1009 _PATH_HOST_ECDSA_KEY_FILE, "", NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001010#endif
Damien Millereb8b60e2010-08-31 22:41:14 +10001011 sensitive_data.keys[3] = key_load_private_cert(KEY_RSA,
Damien Millerc1583312010-08-05 13:04:50 +10001012 _PATH_HOST_RSA_KEY_FILE, "", NULL);
Damien Millereb8b60e2010-08-31 22:41:14 +10001013 sensitive_data.keys[4] = key_load_private_type(KEY_DSA,
Darren Tucker232b76f2006-05-06 17:41:51 +10001014 _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001015#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001016 sensitive_data.keys[5] = key_load_private_type(KEY_ECDSA,
1017 _PATH_HOST_ECDSA_KEY_FILE, "", NULL, NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001018#endif
Damien Millereb8b60e2010-08-31 22:41:14 +10001019 sensitive_data.keys[6] = key_load_private_type(KEY_RSA,
Darren Tucker232b76f2006-05-06 17:41:51 +10001020 _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL);
Damien Miller5be9d9e2013-12-07 11:24:01 +11001021 sensitive_data.keys[7] = key_load_private_type(KEY_ED25519,
1022 _PATH_HOST_ED25519_KEY_FILE, "", NULL, NULL);
Ben Lindstromf9c48842002-06-11 16:37:51 +00001023 PRIV_END;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001024
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +00001025 if (options.hostbased_authentication == 1 &&
1026 sensitive_data.keys[0] == NULL &&
Damien Millereb8b60e2010-08-31 22:41:14 +10001027 sensitive_data.keys[4] == NULL &&
1028 sensitive_data.keys[5] == NULL &&
Damien Miller5be9d9e2013-12-07 11:24:01 +11001029 sensitive_data.keys[6] == NULL &&
1030 sensitive_data.keys[7] == NULL) {
Damien Millerc1583312010-08-05 13:04:50 +10001031 sensitive_data.keys[1] = key_load_cert(
1032 _PATH_HOST_DSA_KEY_FILE);
Damien Miller6af914a2010-09-10 11:39:26 +10001033#ifdef OPENSSL_HAS_ECC
Damien Millerc1583312010-08-05 13:04:50 +10001034 sensitive_data.keys[2] = key_load_cert(
Damien Millereb8b60e2010-08-31 22:41:14 +10001035 _PATH_HOST_ECDSA_KEY_FILE);
Damien Miller6af914a2010-09-10 11:39:26 +10001036#endif
Damien Millereb8b60e2010-08-31 22:41:14 +10001037 sensitive_data.keys[3] = key_load_cert(
Damien Millerc1583312010-08-05 13:04:50 +10001038 _PATH_HOST_RSA_KEY_FILE);
Damien Millerc1583312010-08-05 13:04:50 +10001039 sensitive_data.keys[4] = key_load_public(
Damien Millereb8b60e2010-08-31 22:41:14 +10001040 _PATH_HOST_DSA_KEY_FILE, NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001041#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001042 sensitive_data.keys[5] = key_load_public(
1043 _PATH_HOST_ECDSA_KEY_FILE, NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001044#endif
Damien Millereb8b60e2010-08-31 22:41:14 +10001045 sensitive_data.keys[6] = key_load_public(
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001046 _PATH_HOST_RSA_KEY_FILE, NULL);
Damien Miller5be9d9e2013-12-07 11:24:01 +11001047 sensitive_data.keys[7] = key_load_public(
1048 _PATH_HOST_ED25519_KEY_FILE, NULL);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001049 sensitive_data.external_keysign = 1;
1050 }
Damien Miller95def091999-11-25 00:26:21 +11001051 }
Damien Miller5428f641999-11-25 11:54:57 +11001052 /*
1053 * Get rid of any extra privileges that we may have. We will no
1054 * longer need them. Also, extra privileges could make it very hard
1055 * to read identity files and other non-world-readable files from the
1056 * user's home directory if it happens to be on a NFS volume where
1057 * root is mapped to nobody.
1058 */
Darren Tucker25f60a72004-08-15 17:23:34 +10001059 if (original_effective_uid == 0) {
1060 PRIV_START;
1061 permanently_set_uid(pw);
1062 }
Damien Miller95def091999-11-25 00:26:21 +11001063
Damien Miller5428f641999-11-25 11:54:57 +11001064 /*
1065 * Now that we are back to our own permissions, create ~/.ssh
Damien Miller788f2122005-11-05 15:14:59 +11001066 * directory if it doesn't already exist.
Damien Miller5428f641999-11-25 11:54:57 +11001067 */
Darren Tucker45c66d72011-11-04 10:50:40 +11001068 if (config == NULL) {
1069 r = snprintf(buf, sizeof buf, "%s%s%s", pw->pw_dir,
1070 strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
1071 if (r > 0 && (size_t)r < sizeof(buf) && stat(buf, &st) < 0) {
Darren Tucker8ccb7392010-09-10 12:28:24 +10001072#ifdef WITH_SELINUX
Darren Tucker45c66d72011-11-04 10:50:40 +11001073 ssh_selinux_setfscreatecon(buf);
Darren Tucker8ccb7392010-09-10 12:28:24 +10001074#endif
Darren Tucker45c66d72011-11-04 10:50:40 +11001075 if (mkdir(buf, 0700) < 0)
1076 error("Could not create directory '%.200s'.",
1077 buf);
Darren Tucker8ccb7392010-09-10 12:28:24 +10001078#ifdef WITH_SELINUX
Darren Tucker45c66d72011-11-04 10:50:40 +11001079 ssh_selinux_setfscreatecon(NULL);
Darren Tucker8ccb7392010-09-10 12:28:24 +10001080#endif
Darren Tucker45c66d72011-11-04 10:50:40 +11001081 }
Darren Tucker8ccb7392010-09-10 12:28:24 +10001082 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001083 /* load options.identity_files */
1084 load_public_identity_files();
1085
1086 /* Expand ~ in known host file names. */
Damien Miller295ee632011-05-29 21:42:31 +10001087 tilde_expand_paths(options.system_hostfiles,
1088 options.num_system_hostfiles);
1089 tilde_expand_paths(options.user_hostfiles, options.num_user_hostfiles);
Damien Miller95def091999-11-25 00:26:21 +11001090
Damien Miller07cd5892001-11-12 10:52:03 +11001091 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
Damien Miller857b02e2010-09-24 22:02:56 +10001092 signal(SIGCHLD, main_sigchld_handler);
Damien Miller07cd5892001-11-12 10:52:03 +11001093
Darren Tuckerd6173c02008-06-13 04:52:53 +10001094 /* Log into the remote system. Never returns if the login fails. */
Damien Miller67bd0622007-09-17 12:06:57 +10001095 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr,
Damien Millerd925dcd2010-12-01 12:21:51 +11001096 options.port, pw, timeout_ms);
Damien Miller95def091999-11-25 00:26:21 +11001097
Damien Miller383ffe62010-06-26 10:02:03 +10001098 if (packet_connection_is_on_socket()) {
1099 verbose("Authenticated to %s ([%s]:%d).", host,
1100 get_remote_ipaddr(), get_remote_port());
1101 } else {
1102 verbose("Authenticated to %s (via proxy).", host);
1103 }
1104
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001105 /* We no longer need the private host keys. Clear them now. */
1106 if (sensitive_data.nkeys != 0) {
1107 for (i = 0; i < sensitive_data.nkeys; i++) {
1108 if (sensitive_data.keys[i] != NULL) {
1109 /* Destroys contents safely */
1110 debug3("clear hostkey %d", i);
1111 key_free(sensitive_data.keys[i]);
1112 sensitive_data.keys[i] = NULL;
1113 }
1114 }
Darren Tuckera627d422013-06-02 07:31:17 +10001115 free(sensitive_data.keys);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001116 }
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +00001117 for (i = 0; i < options.num_identity_files; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10001118 free(options.identity_files[i]);
1119 options.identity_files[i] = NULL;
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +00001120 if (options.identity_keys[i]) {
1121 key_free(options.identity_keys[i]);
1122 options.identity_keys[i] = NULL;
1123 }
1124 }
Damien Miller95def091999-11-25 00:26:21 +11001125
Damien Miller1383bd82000-04-06 12:32:37 +10001126 exit_status = compat20 ? ssh_session2() : ssh_session();
1127 packet_close();
Damien Miller8c4e18a2002-09-19 12:05:02 +10001128
Damien Millerb1cbfa22008-05-19 16:00:08 +10001129 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10001130 unlink(options.control_path);
1131
Damien Millera41ccca2010-10-07 22:07:32 +11001132 /* Kill ProxyCommand if it is running. */
1133 ssh_kill_proxy_command();
Damien Miller8c4e18a2002-09-19 12:05:02 +10001134
Damien Miller1383bd82000-04-06 12:32:37 +10001135 return exit_status;
1136}
1137
Damien Millere11e1ea2010-08-03 16:04:46 +10001138static void
1139control_persist_detach(void)
1140{
1141 pid_t pid;
Damien Miller00d9ae22010-08-17 01:59:31 +10001142 int devnull;
Damien Millere11e1ea2010-08-03 16:04:46 +10001143
1144 debug("%s: backgrounding master process", __func__);
1145
1146 /*
1147 * master (current process) into the background, and make the
1148 * foreground process a client of the backgrounded master.
1149 */
1150 switch ((pid = fork())) {
1151 case -1:
1152 fatal("%s: fork: %s", __func__, strerror(errno));
1153 case 0:
1154 /* Child: master process continues mainloop */
1155 break;
1156 default:
1157 /* Parent: set up mux slave to connect to backgrounded master */
1158 debug2("%s: background process is %ld", __func__, (long)pid);
1159 stdin_null_flag = ostdin_null_flag;
Damien Miller21771e22011-05-15 08:45:50 +10001160 options.request_tty = orequest_tty;
Damien Millere11e1ea2010-08-03 16:04:46 +10001161 tty_flag = otty_flag;
1162 close(muxserver_sock);
1163 muxserver_sock = -1;
Damien Miller5929c522010-09-10 11:17:02 +10001164 options.control_master = SSHCTL_MASTER_NO;
Damien Millere11e1ea2010-08-03 16:04:46 +10001165 muxclient(options.control_path);
1166 /* muxclient() doesn't return on success. */
1167 fatal("Failed to connect to new control master");
1168 }
Damien Miller00d9ae22010-08-17 01:59:31 +10001169 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
1170 error("%s: open(\"/dev/null\"): %s", __func__,
1171 strerror(errno));
1172 } else {
1173 if (dup2(devnull, STDIN_FILENO) == -1 ||
1174 dup2(devnull, STDOUT_FILENO) == -1)
1175 error("%s: dup2: %s", __func__, strerror(errno));
1176 if (devnull > STDERR_FILENO)
1177 close(devnull);
1178 }
Damien Miller98e27dc2013-07-25 11:55:52 +10001179 daemon(1, 1);
Damien Millerea2c1a42011-06-03 12:10:22 +10001180 setproctitle("%s [mux]", options.control_path);
Damien Millere11e1ea2010-08-03 16:04:46 +10001181}
1182
1183/* Do fork() after authentication. Used by "ssh -f" */
1184static void
1185fork_postauth(void)
1186{
1187 if (need_controlpersist_detach)
1188 control_persist_detach();
1189 debug("forking to background");
1190 fork_after_authentication_flag = 0;
1191 if (daemon(1, 1) < 0)
1192 fatal("daemon() failed: %.200s", strerror(errno));
1193}
1194
Darren Tucker9f407c42008-06-13 04:50:27 +10001195/* Callback for remote forward global requests */
1196static void
1197ssh_confirm_remote_forward(int type, u_int32_t seq, void *ctxt)
1198{
1199 Forward *rfwd = (Forward *)ctxt;
1200
Damien Miller4bf648f2009-02-14 16:28:21 +11001201 /* XXX verbose() on failure? */
Darren Tucker9f407c42008-06-13 04:50:27 +10001202 debug("remote forward %s for: listen %d, connect %s:%d",
1203 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
1204 rfwd->listen_port, rfwd->connect_host, rfwd->connect_port);
Darren Tucker68afb8c2011-10-02 18:59:03 +11001205 if (rfwd->listen_port == 0) {
1206 if (type == SSH2_MSG_REQUEST_SUCCESS) {
1207 rfwd->allocated_port = packet_get_int();
1208 logit("Allocated port %u for remote forward to %s:%d",
1209 rfwd->allocated_port,
1210 rfwd->connect_host, rfwd->connect_port);
1211 channel_update_permitted_opens(rfwd->handle,
1212 rfwd->allocated_port);
1213 } else {
1214 channel_update_permitted_opens(rfwd->handle, -1);
1215 }
Damien Miller4bf648f2009-02-14 16:28:21 +11001216 }
1217
Darren Tucker9f407c42008-06-13 04:50:27 +10001218 if (type == SSH2_MSG_REQUEST_FAILURE) {
1219 if (options.exit_on_forward_failure)
1220 fatal("Error: remote port forwarding failed for "
1221 "listen port %d", rfwd->listen_port);
1222 else
1223 logit("Warning: remote port forwarding failed for "
1224 "listen port %d", rfwd->listen_port);
1225 }
Darren Tucker9a2a6092008-07-04 12:53:50 +10001226 if (++remote_forward_confirms_received == options.num_remote_forwards) {
Darren Tucker9f407c42008-06-13 04:50:27 +10001227 debug("All remote forwarding requests processed");
Damien Millere11e1ea2010-08-03 16:04:46 +10001228 if (fork_after_authentication_flag)
1229 fork_postauth();
Darren Tucker9a2a6092008-07-04 12:53:50 +10001230 }
Darren Tucker9f407c42008-06-13 04:50:27 +10001231}
1232
Ben Lindstrombba81212001-06-25 05:01:22 +00001233static void
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001234client_cleanup_stdio_fwd(int id, void *arg)
1235{
1236 debug("stdio forwarding: done");
1237 cleanup_exit(0);
1238}
1239
Darren Tucker2d6665d2011-11-04 10:54:22 +11001240static void
1241ssh_init_stdio_forwarding(void)
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001242{
1243 Channel *c;
Damien Millere1537f92010-01-26 13:26:22 +11001244 int in, out;
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001245
Darren Tucker2d6665d2011-11-04 10:54:22 +11001246 if (stdio_forward_host == NULL)
1247 return;
Damien Miller386feab2013-10-15 12:14:49 +11001248 if (!compat20)
Darren Tucker2d6665d2011-11-04 10:54:22 +11001249 fatal("stdio forwarding require Protocol 2");
Damien Millere1537f92010-01-26 13:26:22 +11001250
Darren Tucker2d6665d2011-11-04 10:54:22 +11001251 debug3("%s: %s:%d", __func__, stdio_forward_host, stdio_forward_port);
1252
1253 if ((in = dup(STDIN_FILENO)) < 0 ||
1254 (out = dup(STDOUT_FILENO)) < 0)
Damien Millere1537f92010-01-26 13:26:22 +11001255 fatal("channel_connect_stdio_fwd: dup() in/out failed");
Darren Tucker2d6665d2011-11-04 10:54:22 +11001256 if ((c = channel_connect_stdio_fwd(stdio_forward_host,
1257 stdio_forward_port, in, out)) == NULL)
1258 fatal("%s: channel_connect_stdio_fwd failed", __func__);
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001259 channel_register_cleanup(c->self, client_cleanup_stdio_fwd, 0);
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001260}
1261
1262static void
Damien Miller0bc1bd82000-11-13 22:57:25 +11001263ssh_init_forwarding(void)
1264{
Kevin Steves12057502001-02-05 14:54:34 +00001265 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001266 int i;
Kevin Steves12057502001-02-05 14:54:34 +00001267
Damien Miller0bc1bd82000-11-13 22:57:25 +11001268 /* Initiate local TCP/IP port forwardings. */
1269 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001270 debug("Local connections to %.200s:%d forwarded to remote "
1271 "address %.200s:%d",
Darren Tucker47eede72005-03-14 23:08:12 +11001272 (options.local_forwards[i].listen_host == NULL) ?
1273 (options.gateway_ports ? "*" : "LOCALHOST") :
Damien Millerf91ee4c2005-03-01 21:24:33 +11001274 options.local_forwards[i].listen_host,
1275 options.local_forwards[i].listen_port,
1276 options.local_forwards[i].connect_host,
1277 options.local_forwards[i].connect_port);
Damien Millerb16461c2002-01-22 23:29:22 +11001278 success += channel_setup_local_fwd_listener(
Damien Millerf91ee4c2005-03-01 21:24:33 +11001279 options.local_forwards[i].listen_host,
1280 options.local_forwards[i].listen_port,
1281 options.local_forwards[i].connect_host,
1282 options.local_forwards[i].connect_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +11001283 options.gateway_ports);
1284 }
Darren Tuckere7d4b192006-07-12 22:17:10 +10001285 if (i > 0 && success != i && options.exit_on_forward_failure)
1286 fatal("Could not request local forwarding.");
Kevin Steves12057502001-02-05 14:54:34 +00001287 if (i > 0 && success == 0)
1288 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001289
1290 /* Initiate remote TCP/IP port forwardings. */
1291 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001292 debug("Remote connections from %.200s:%d forwarded to "
1293 "local address %.200s:%d",
Damien Miller46d38de2005-07-17 17:02:09 +10001294 (options.remote_forwards[i].listen_host == NULL) ?
Damien Milleraa3bb102005-11-05 15:12:59 +11001295 "LOCALHOST" : options.remote_forwards[i].listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +11001296 options.remote_forwards[i].listen_port,
1297 options.remote_forwards[i].connect_host,
1298 options.remote_forwards[i].connect_port);
Darren Tucker68afb8c2011-10-02 18:59:03 +11001299 options.remote_forwards[i].handle =
1300 channel_request_remote_forwarding(
Damien Millerf91ee4c2005-03-01 21:24:33 +11001301 options.remote_forwards[i].listen_host,
1302 options.remote_forwards[i].listen_port,
1303 options.remote_forwards[i].connect_host,
Darren Tucker68afb8c2011-10-02 18:59:03 +11001304 options.remote_forwards[i].connect_port);
1305 if (options.remote_forwards[i].handle < 0) {
Darren Tuckere7d4b192006-07-12 22:17:10 +10001306 if (options.exit_on_forward_failure)
1307 fatal("Could not request remote forwarding.");
1308 else
1309 logit("Warning: Could not request remote "
1310 "forwarding.");
Darren Tucker68afb8c2011-10-02 18:59:03 +11001311 } else {
1312 client_register_global_confirm(ssh_confirm_remote_forward,
1313 &options.remote_forwards[i]);
Darren Tuckere7d4b192006-07-12 22:17:10 +10001314 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001315 }
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001316
1317 /* Initiate tunnel forwarding. */
1318 if (options.tun_open != SSH_TUNMODE_NO) {
1319 if (client_request_tun_fwd(options.tun_open,
1320 options.tun_local, options.tun_remote) == -1) {
1321 if (options.exit_on_forward_failure)
1322 fatal("Could not request tunnel forwarding.");
1323 else
1324 error("Could not request tunnel forwarding.");
1325 }
1326 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001327}
1328
Ben Lindstrombba81212001-06-25 05:01:22 +00001329static void
Damien Miller0bc1bd82000-11-13 22:57:25 +11001330check_agent_present(void)
1331{
1332 if (options.forward_agent) {
Damien Miller788f2122005-11-05 15:14:59 +11001333 /* Clear agent forwarding if we don't have an agent. */
Damien Miller789e95d2002-09-12 09:52:46 +10001334 if (!ssh_agent_present())
Damien Miller0bc1bd82000-11-13 22:57:25 +11001335 options.forward_agent = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001336 }
1337}
1338
Ben Lindstrombba81212001-06-25 05:01:22 +00001339static int
Damien Miller1383bd82000-04-06 12:32:37 +10001340ssh_session(void)
1341{
1342 int type;
Damien Miller1383bd82000-04-06 12:32:37 +10001343 int interactive = 0;
1344 int have_tty = 0;
1345 struct winsize ws;
Damien Miller1383bd82000-04-06 12:32:37 +10001346 char *cp;
Damien Miller17e7ed02005-06-17 12:54:33 +10001347 const char *display;
Damien Miller1383bd82000-04-06 12:32:37 +10001348
Damien Miller95def091999-11-25 00:26:21 +11001349 /* Enable compression if requested. */
1350 if (options.compression) {
Darren Tuckerd6173c02008-06-13 04:52:53 +10001351 debug("Requesting compression at level %d.",
1352 options.compression_level);
Damien Miller95def091999-11-25 00:26:21 +11001353
Darren Tuckerd6173c02008-06-13 04:52:53 +10001354 if (options.compression_level < 1 ||
1355 options.compression_level > 9)
1356 fatal("Compression level must be from 1 (fast) to "
1357 "9 (slow, best).");
Damien Miller95def091999-11-25 00:26:21 +11001358
1359 /* Send the request. */
1360 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
1361 packet_put_int(options.compression_level);
1362 packet_send();
1363 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +11001364 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +11001365 if (type == SSH_SMSG_SUCCESS)
1366 packet_start_compression(options.compression_level);
1367 else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +10001368 logit("Warning: Remote host refused compression.");
Damien Miller95def091999-11-25 00:26:21 +11001369 else
Darren Tuckerd6173c02008-06-13 04:52:53 +10001370 packet_disconnect("Protocol error waiting for "
1371 "compression response.");
Damien Miller95def091999-11-25 00:26:21 +11001372 }
1373 /* Allocate a pseudo tty if appropriate. */
1374 if (tty_flag) {
1375 debug("Requesting pty.");
1376
1377 /* Start the packet. */
1378 packet_start(SSH_CMSG_REQUEST_PTY);
1379
1380 /* Store TERM in the packet. There is no limit on the
1381 length of the string. */
1382 cp = getenv("TERM");
1383 if (!cp)
1384 cp = "";
Ben Lindstrom664408d2001-06-09 01:42:01 +00001385 packet_put_cstring(cp);
Damien Miller95def091999-11-25 00:26:21 +11001386
1387 /* Store window size in the packet. */
1388 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
1389 memset(&ws, 0, sizeof(ws));
Damien Miller71a73672006-03-26 14:04:36 +11001390 packet_put_int((u_int)ws.ws_row);
1391 packet_put_int((u_int)ws.ws_col);
1392 packet_put_int((u_int)ws.ws_xpixel);
1393 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller95def091999-11-25 00:26:21 +11001394
1395 /* Store tty modes in the packet. */
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001396 tty_make_modes(fileno(stdin), NULL);
Damien Miller95def091999-11-25 00:26:21 +11001397
1398 /* Send the packet, and wait for it to leave. */
1399 packet_send();
1400 packet_write_wait();
1401
1402 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +11001403 type = packet_read();
Damien Miller450a7a12000-03-26 13:04:51 +10001404 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +11001405 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001406 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +10001407 } else if (type == SSH_SMSG_FAILURE)
Darren Tuckerd6173c02008-06-13 04:52:53 +10001408 logit("Warning: Remote host failed or refused to "
1409 "allocate a pseudo tty.");
Damien Miller95def091999-11-25 00:26:21 +11001410 else
Darren Tuckerd6173c02008-06-13 04:52:53 +10001411 packet_disconnect("Protocol error waiting for pty "
1412 "request response.");
Damien Miller95def091999-11-25 00:26:21 +11001413 }
1414 /* Request X11 forwarding if enabled and DISPLAY is set. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001415 display = getenv("DISPLAY");
1416 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001417 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001418 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001419 client_x11_get_proto(display, options.xauth_location,
Damien Miller386feab2013-10-15 12:14:49 +11001420 options.forward_x11_trusted,
Damien Miller1ab6a512010-06-26 10:02:24 +10001421 options.forward_x11_timeout,
1422 &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001423 /* Request forwarding with authentication spoofing. */
Darren Tuckerd6173c02008-06-13 04:52:53 +10001424 debug("Requesting X11 forwarding with authentication "
1425 "spoofing.");
Damien Miller6d7b4372011-06-23 08:31:57 +10001426 x11_request_forwarding_with_spoofing(0, display, proto,
1427 data, 0);
Damien Miller95def091999-11-25 00:26:21 +11001428 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +11001429 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +11001430 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +11001431 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001432 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller996acd22003-04-09 20:59:48 +10001433 logit("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +10001434 } else {
Darren Tuckerd6173c02008-06-13 04:52:53 +10001435 packet_disconnect("Protocol error waiting for X11 "
1436 "forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +10001437 }
Damien Miller95def091999-11-25 00:26:21 +11001438 }
1439 /* Tell the packet module whether this is an interactive session. */
Damien Miller0dac6fb2010-11-20 15:19:38 +11001440 packet_set_interactive(interactive,
1441 options.ip_qos_interactive, options.ip_qos_bulk);
Damien Miller95def091999-11-25 00:26:21 +11001442
1443 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001444 check_agent_present();
1445
Damien Miller95def091999-11-25 00:26:21 +11001446 if (options.forward_agent) {
1447 debug("Requesting authentication agent forwarding.");
1448 auth_request_forwarding();
1449
1450 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +11001451 type = packet_read();
Damien Miller48b03fc2002-01-22 23:11:40 +11001452 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +11001453 if (type != SSH_SMSG_SUCCESS)
Damien Miller996acd22003-04-09 20:59:48 +10001454 logit("Warning: Remote host denied authentication agent forwarding.");
Damien Miller95def091999-11-25 00:26:21 +11001455 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001456
Damien Miller0bc1bd82000-11-13 22:57:25 +11001457 /* Initiate port forwardings. */
Darren Tucker2d6665d2011-11-04 10:54:22 +11001458 ssh_init_stdio_forwarding();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001459 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001460
Darren Tuckerb776c852007-12-02 23:06:35 +11001461 /* Execute a local command */
1462 if (options.local_command != NULL &&
1463 options.permit_local_command)
1464 ssh_local_cmd(options.local_command);
1465
Darren Tucker9a2a6092008-07-04 12:53:50 +10001466 /*
1467 * If requested and we are not interested in replies to remote
1468 * forwarding requests, then let ssh continue in the background.
1469 */
Damien Millere11e1ea2010-08-03 16:04:46 +10001470 if (fork_after_authentication_flag) {
1471 if (options.exit_on_forward_failure &&
1472 options.num_remote_forwards > 0) {
1473 debug("deferring postauth fork until remote forward "
1474 "confirmation received");
1475 } else
1476 fork_postauth();
Darren Tucker9a2a6092008-07-04 12:53:50 +10001477 }
Damien Miller5428f641999-11-25 11:54:57 +11001478
1479 /*
1480 * If a command was specified on the command line, execute the
1481 * command now. Otherwise request the server to start a shell.
1482 */
Damien Miller95def091999-11-25 00:26:21 +11001483 if (buffer_len(&command) > 0) {
1484 int len = buffer_len(&command);
1485 if (len > 900)
1486 len = 900;
Darren Tuckerd6173c02008-06-13 04:52:53 +10001487 debug("Sending command: %.*s", len,
1488 (u_char *)buffer_ptr(&command));
Damien Miller95def091999-11-25 00:26:21 +11001489 packet_start(SSH_CMSG_EXEC_CMD);
1490 packet_put_string(buffer_ptr(&command), buffer_len(&command));
1491 packet_send();
1492 packet_write_wait();
1493 } else {
1494 debug("Requesting shell.");
1495 packet_start(SSH_CMSG_EXEC_SHELL);
1496 packet_send();
1497 packet_write_wait();
1498 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001499
Damien Miller95def091999-11-25 00:26:21 +11001500 /* Enter the interactive session. */
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001501 return client_loop(have_tty, tty_flag ?
1502 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
Damien Miller1383bd82000-04-06 12:32:37 +10001503}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001504
Ben Lindstromf558cf62001-09-20 23:13:49 +00001505/* request pty/x11/agent/tcpfwd/shell for channel */
Ben Lindstrombba81212001-06-25 05:01:22 +00001506static void
Damien Millerd530f5f2010-05-21 14:57:10 +10001507ssh_session2_setup(int id, int success, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +10001508{
Damien Miller3756dce2004-06-18 01:17:29 +10001509 extern char **environ;
Damien Miller17e7ed02005-06-17 12:54:33 +10001510 const char *display;
Damien Miller0e220db2004-06-15 10:34:08 +10001511 int interactive = tty_flag;
Damien Miller17e7ed02005-06-17 12:54:33 +10001512
Damien Millerd530f5f2010-05-21 14:57:10 +10001513 if (!success)
1514 return; /* No need for error message, channels code sens one */
1515
Damien Miller46d38de2005-07-17 17:02:09 +10001516 display = getenv("DISPLAY");
Damien Miller17e7ed02005-06-17 12:54:33 +10001517 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001518 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001519 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001520 client_x11_get_proto(display, options.xauth_location,
Damien Miller1ab6a512010-06-26 10:02:24 +10001521 options.forward_x11_trusted,
1522 options.forward_x11_timeout, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001523 /* Request forwarding with authentication spoofing. */
Darren Tuckerd6173c02008-06-13 04:52:53 +10001524 debug("Requesting X11 forwarding with authentication "
1525 "spoofing.");
Damien Miller6d7b4372011-06-23 08:31:57 +10001526 x11_request_forwarding_with_spoofing(id, display, proto,
1527 data, 1);
1528 client_expect_confirm(id, "X11 forwarding", CONFIRM_WARN);
1529 /* XXX exit_on_forward_failure */
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001530 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001531 }
1532
Damien Miller0bc1bd82000-11-13 22:57:25 +11001533 check_agent_present();
1534 if (options.forward_agent) {
1535 debug("Requesting authentication agent forwarding.");
1536 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1537 packet_send();
1538 }
1539
Darren Tucker7b305012012-07-02 18:55:09 +10001540 /* Tell the packet module whether this is an interactive session. */
1541 packet_set_interactive(interactive,
1542 options.ip_qos_interactive, options.ip_qos_bulk);
1543
Damien Miller0e220db2004-06-15 10:34:08 +10001544 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
Damien Miller5771ed72008-05-19 15:35:33 +10001545 NULL, fileno(stdin), &command, environ);
Damien Miller1383bd82000-04-06 12:32:37 +10001546}
1547
Ben Lindstromf558cf62001-09-20 23:13:49 +00001548/* open new channel for a session */
Ben Lindstrombba81212001-06-25 05:01:22 +00001549static int
Ben Lindstromf558cf62001-09-20 23:13:49 +00001550ssh_session2_open(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001551{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001552 Channel *c;
1553 int window, packetmax, in, out, err;
Damien Millerad833b32000-08-23 10:46:23 +10001554
Damien Millercaf6dd62000-08-29 11:33:50 +11001555 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001556 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001557 } else {
1558 in = dup(STDIN_FILENO);
1559 }
1560 out = dup(STDOUT_FILENO);
1561 err = dup(STDERR_FILENO);
1562
1563 if (in < 0 || out < 0 || err < 0)
1564 fatal("dup() in/out/err failed");
1565
Damien Miller69b69aa2000-10-28 14:19:58 +11001566 /* enable nonblocking unless tty */
1567 if (!isatty(in))
1568 set_nonblock(in);
1569 if (!isatty(out))
1570 set_nonblock(out);
1571 if (!isatty(err))
1572 set_nonblock(err);
1573
Damien Millere4340be2000-09-16 13:29:08 +11001574 window = CHAN_SES_WINDOW_DEFAULT;
1575 packetmax = CHAN_SES_PACKET_DEFAULT;
Damien Miller19a59452002-02-19 15:20:57 +11001576 if (tty_flag) {
1577 window >>= 1;
1578 packetmax >>= 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001579 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001580 c = channel_new(
Damien Miller1383bd82000-04-06 12:32:37 +10001581 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001582 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001583 "client-session", /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001584
Ben Lindstromf558cf62001-09-20 23:13:49 +00001585 debug3("ssh_session2_open: channel_new: %d", c->self);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001586
Ben Lindstrom5ec26452001-06-09 00:18:51 +00001587 channel_send_open(c->self);
Ben Lindstromf558cf62001-09-20 23:13:49 +00001588 if (!no_shell_flag)
Damien Millerb84886b2008-05-19 15:05:07 +10001589 channel_register_open_confirm(c->self,
1590 ssh_session2_setup, NULL);
Damien Miller1383bd82000-04-06 12:32:37 +10001591
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001592 return c->self;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001593}
1594
Ben Lindstrombba81212001-06-25 05:01:22 +00001595static int
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001596ssh_session2(void)
1597{
Ben Lindstromf558cf62001-09-20 23:13:49 +00001598 int id = -1;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001599
1600 /* XXX should be pre-session */
Darren Tucker2d6665d2011-11-04 10:54:22 +11001601 if (!options.control_persist)
1602 ssh_init_stdio_forwarding();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001603 ssh_init_forwarding();
1604
Damien Millere11e1ea2010-08-03 16:04:46 +10001605 /* Start listening for multiplex clients */
1606 muxserver_listen();
1607
1608 /*
Darren Tucker2d6665d2011-11-04 10:54:22 +11001609 * If we are in control persist mode and have a working mux listen
1610 * socket, then prepare to background ourselves and have a foreground
1611 * client attach as a control slave.
1612 * NB. we must save copies of the flags that we override for
Damien Millere11e1ea2010-08-03 16:04:46 +10001613 * the backgrounding, since we defer attachment of the slave until
1614 * after the connection is fully established (in particular,
1615 * async rfwd replies have been received for ExitOnForwardFailure).
1616 */
1617 if (options.control_persist && muxserver_sock != -1) {
1618 ostdin_null_flag = stdin_null_flag;
1619 ono_shell_flag = no_shell_flag;
Damien Miller21771e22011-05-15 08:45:50 +10001620 orequest_tty = options.request_tty;
Damien Millere11e1ea2010-08-03 16:04:46 +10001621 otty_flag = tty_flag;
1622 stdin_null_flag = 1;
1623 no_shell_flag = 1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001624 tty_flag = 0;
1625 if (!fork_after_authentication_flag)
1626 need_controlpersist_detach = 1;
1627 fork_after_authentication_flag = 1;
1628 }
Darren Tucker2d6665d2011-11-04 10:54:22 +11001629 /*
1630 * ControlPersist mux listen socket setup failed, attempt the
1631 * stdio forward setup that we skipped earlier.
1632 */
1633 if (options.control_persist && muxserver_sock == -1)
1634 ssh_init_stdio_forwarding();
Damien Millere11e1ea2010-08-03 16:04:46 +10001635
Ben Lindstromf558cf62001-09-20 23:13:49 +00001636 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1637 id = ssh_session2_open();
Damien Miller649fe022013-07-18 16:13:55 +10001638 else {
1639 packet_set_interactive(
1640 options.control_master == SSHCTL_MASTER_NO,
1641 options.ip_qos_interactive, options.ip_qos_bulk);
1642 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001643
Darren Tucker8901fa92008-06-11 09:34:01 +10001644 /* If we don't expect to open a new session, then disallow it */
Damien Miller456e6f02008-11-03 19:20:10 +11001645 if (options.control_master == SSHCTL_MASTER_NO &&
1646 (datafellows & SSH_NEW_OPENSSH)) {
Darren Tucker8901fa92008-06-11 09:34:01 +10001647 debug("Requesting no-more-sessions@openssh.com");
1648 packet_start(SSH2_MSG_GLOBAL_REQUEST);
1649 packet_put_cstring("no-more-sessions@openssh.com");
1650 packet_put_char(0);
1651 packet_send();
1652 }
1653
Damien Millerd27b9472005-12-13 19:29:02 +11001654 /* Execute a local command */
1655 if (options.local_command != NULL &&
1656 options.permit_local_command)
1657 ssh_local_cmd(options.local_command);
1658
Damien Miller1f25ab42010-07-16 13:56:23 +10001659 /*
1660 * If requested and we are not interested in replies to remote
1661 * forwarding requests, then let ssh continue in the background.
1662 */
Damien Millere11e1ea2010-08-03 16:04:46 +10001663 if (fork_after_authentication_flag) {
1664 if (options.exit_on_forward_failure &&
1665 options.num_remote_forwards > 0) {
1666 debug("deferring postauth fork until remote forward "
1667 "confirmation received");
1668 } else
1669 fork_postauth();
Darren Tucker9a2a6092008-07-04 12:53:50 +10001670 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001671
Darren Tuckerf1de4e52010-01-08 16:54:59 +11001672 if (options.use_roaming)
1673 request_roaming();
1674
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001675 return client_loop(tty_flag, tty_flag ?
1676 options.escape_char : SSH_ESCAPECHAR_NONE, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001677}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001678
Ben Lindstrombba81212001-06-25 05:01:22 +00001679static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001680load_public_identity_files(void)
1681{
Damien Miller6b1d53c2006-03-31 23:13:21 +11001682 char *filename, *cp, thishost[NI_MAXHOST];
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001683 char *pwdir = NULL, *pwname = NULL;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001684 int i = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001685 Key *public;
Damien Miller6b1d53c2006-03-31 23:13:21 +11001686 struct passwd *pw;
Damien Miller0a80ca12010-02-27 07:55:05 +11001687 u_int n_ids;
1688 char *identity_files[SSH_MAX_IDENTITY_FILES];
1689 Key *identity_keys[SSH_MAX_IDENTITY_FILES];
Damien Miller7ea845e2010-02-12 09:21:02 +11001690#ifdef ENABLE_PKCS11
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001691 Key **keys;
Damien Miller7ea845e2010-02-12 09:21:02 +11001692 int nkeys;
Damien Miller0a80ca12010-02-27 07:55:05 +11001693#endif /* PKCS11 */
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001694
Damien Miller0a80ca12010-02-27 07:55:05 +11001695 n_ids = 0;
1696 bzero(identity_files, sizeof(identity_files));
1697 bzero(identity_keys, sizeof(identity_keys));
1698
1699#ifdef ENABLE_PKCS11
Damien Miller7ea845e2010-02-12 09:21:02 +11001700 if (options.pkcs11_provider != NULL &&
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001701 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
Damien Miller7ea845e2010-02-12 09:21:02 +11001702 (pkcs11_init(!options.batch_mode) == 0) &&
1703 (nkeys = pkcs11_add_provider(options.pkcs11_provider, NULL,
1704 &keys)) > 0) {
Damien Miller7ea845e2010-02-12 09:21:02 +11001705 for (i = 0; i < nkeys; i++) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001706 if (n_ids >= SSH_MAX_IDENTITY_FILES) {
1707 key_free(keys[i]);
1708 continue;
1709 }
1710 identity_keys[n_ids] = keys[i];
1711 identity_files[n_ids] =
Damien Miller7ea845e2010-02-12 09:21:02 +11001712 xstrdup(options.pkcs11_provider); /* XXX */
Damien Miller0a80ca12010-02-27 07:55:05 +11001713 n_ids++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001714 }
Darren Tuckera627d422013-06-02 07:31:17 +10001715 free(keys);
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001716 }
Damien Miller7ea845e2010-02-12 09:21:02 +11001717#endif /* ENABLE_PKCS11 */
Damien Miller6b1d53c2006-03-31 23:13:21 +11001718 if ((pw = getpwuid(original_real_uid)) == NULL)
1719 fatal("load_public_identity_files: getpwuid failed");
Darren Tuckere143f062007-12-02 23:21:16 +11001720 pwname = xstrdup(pw->pw_name);
1721 pwdir = xstrdup(pw->pw_dir);
Damien Miller6b1d53c2006-03-31 23:13:21 +11001722 if (gethostname(thishost, sizeof(thishost)) == -1)
1723 fatal("load_public_identity_files: gethostname: %s",
1724 strerror(errno));
Damien Miller0a80ca12010-02-27 07:55:05 +11001725 for (i = 0; i < options.num_identity_files; i++) {
Darren Tucker15fd19c2013-04-05 11:22:26 +11001726 if (n_ids >= SSH_MAX_IDENTITY_FILES ||
1727 strcasecmp(options.identity_files[i], "none") == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +10001728 free(options.identity_files[i]);
Damien Miller0a80ca12010-02-27 07:55:05 +11001729 continue;
1730 }
Damien Miller6b1d53c2006-03-31 23:13:21 +11001731 cp = tilde_expand_filename(options.identity_files[i],
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001732 original_real_uid);
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001733 filename = percent_expand(cp, "d", pwdir,
1734 "u", pwname, "l", thishost, "h", host,
Damien Miller6b1d53c2006-03-31 23:13:21 +11001735 "r", options.user, (char *)NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001736 free(cp);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001737 public = key_load_public(filename, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001738 debug("identity file %s type %d", filename,
1739 public ? public->type : -1);
Darren Tuckera627d422013-06-02 07:31:17 +10001740 free(options.identity_files[i]);
Damien Miller0a80ca12010-02-27 07:55:05 +11001741 identity_files[n_ids] = filename;
1742 identity_keys[n_ids] = public;
1743
1744 if (++n_ids >= SSH_MAX_IDENTITY_FILES)
1745 continue;
1746
1747 /* Try to add the certificate variant too */
1748 xasprintf(&cp, "%s-cert", filename);
1749 public = key_load_public(cp, NULL);
1750 debug("identity file %s type %d", cp,
1751 public ? public->type : -1);
1752 if (public == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +10001753 free(cp);
Damien Miller0a80ca12010-02-27 07:55:05 +11001754 continue;
1755 }
1756 if (!key_is_cert(public)) {
1757 debug("%s: key %s type %s is not a certificate",
1758 __func__, cp, key_type(public));
1759 key_free(public);
Darren Tuckera627d422013-06-02 07:31:17 +10001760 free(cp);
Damien Miller0a80ca12010-02-27 07:55:05 +11001761 continue;
1762 }
1763 identity_keys[n_ids] = public;
1764 /* point to the original path, most likely the private key */
1765 identity_files[n_ids] = xstrdup(filename);
1766 n_ids++;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001767 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001768 options.num_identity_files = n_ids;
1769 memcpy(options.identity_files, identity_files, sizeof(identity_files));
1770 memcpy(options.identity_keys, identity_keys, sizeof(identity_keys));
1771
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001772 bzero(pwname, strlen(pwname));
Darren Tuckera627d422013-06-02 07:31:17 +10001773 free(pwname);
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001774 bzero(pwdir, strlen(pwdir));
Darren Tuckera627d422013-06-02 07:31:17 +10001775 free(pwdir);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001776}
Damien Miller857b02e2010-09-24 22:02:56 +10001777
1778static void
1779main_sigchld_handler(int sig)
1780{
1781 int save_errno = errno;
1782 pid_t pid;
1783 int status;
1784
1785 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
1786 (pid < 0 && errno == EINTR))
1787 ;
1788
1789 signal(sig, main_sigchld_handler);
1790 errno = save_errno;
1791}