blob: ef94591b1fed0785f4f0b73cc7eea19cd4844e49 [file] [log] [blame]
Damien Miller28631ce2013-10-26 10:07:56 +11001/* $OpenBSD: ssh.c,v 1.391 2013/10/25 23:04:51 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Ssh client program. This program can be used to log into a remote machine.
7 * The software supports strong authentication, encryption, and forwarding
8 * of X11, TCP/IP, and authentication connections.
9 *
Damien Millere4340be2000-09-16 13:29:08 +110010 * As far as I am concerned, the code I have written for this software
11 * can be used freely for any purpose. Any derived versions of this
12 * software must be clearly marked as such, and if the derived work is
13 * incompatible with the protocol description in the RFC file, it must be
14 * called by a name other than "ssh" or "Secure Shell".
15 *
16 * Copyright (c) 1999 Niels Provos. All rights reserved.
Darren Tucker0a118da2003-10-15 15:54:32 +100017 * Copyright (c) 2000, 2001, 2002, 2003 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110018 *
19 * Modified to work with SSL by Niels Provos <provos@citi.umich.edu>
20 * in Canada (German citizen).
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110041 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100042
43#include "includes.h"
Damien Millercd4223c2006-03-15 11:22:47 +110044
Damien Millerf17883e2006-03-15 11:45:54 +110045#include <sys/types.h>
46#ifdef HAVE_SYS_STAT_H
47# include <sys/stat.h>
48#endif
Damien Millercd4223c2006-03-15 11:22:47 +110049#include <sys/resource.h>
Damien Miller17e91c02006-03-15 11:28:34 +110050#include <sys/ioctl.h>
Darren Tucker199b1342009-07-06 07:16:56 +100051#include <sys/param.h>
Damien Millere3b60b52006-07-10 21:08:03 +100052#include <sys/socket.h>
Damien Miller857b02e2010-09-24 22:02:56 +100053#include <sys/wait.h>
Damien Miller03e20032006-03-15 11:16:59 +110054
Damien Millerc7b06362006-03-15 11:53:45 +110055#include <ctype.h>
Darren Tuckerba724052006-07-12 22:24:22 +100056#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100057#include <fcntl.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100058#include <netdb.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110059#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110060#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110061#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100062#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110063#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100064#include <stdarg.h>
Damien Miller2d00e632006-07-24 13:53:19 +100065#include <stddef.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100066#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100067#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100068#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100069#include <unistd.h>
Damien Millereba71ba2000-04-29 23:57:08 +100070
Darren Tucker46aa3e02006-09-02 15:32:40 +100071#include <netinet/in.h>
72#include <arpa/inet.h>
73
Damien Millereba71ba2000-04-29 23:57:08 +100074#include <openssl/evp.h>
Damien Miller0bc1bd82000-11-13 22:57:25 +110075#include <openssl/err.h>
Darren Tuckerbfaaf962008-02-28 19:13:52 +110076#include "openbsd-compat/openssl-compat.h"
Damien Millerb84886b2008-05-19 15:05:07 +100077#include "openbsd-compat/sys-queue.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078
Damien Millerd7834352006-08-05 12:39:39 +100079#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000081#include "ssh1.h"
Damien Miller1383bd82000-04-06 12:32:37 +100082#include "ssh2.h"
Damien Millerb96c4412010-07-02 13:34:24 +100083#include "canohost.h"
Damien Miller1383bd82000-04-06 12:32:37 +100084#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000085#include "cipher.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000086#include "packet.h"
87#include "buffer.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000088#include "channels.h"
Damien Millereba71ba2000-04-29 23:57:08 +100089#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100090#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100091#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000092#include "pathnames.h"
Damien Miller0e220db2004-06-15 10:34:08 +100093#include "dispatch.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +000094#include "clientloop.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000095#include "log.h"
96#include "readconf.h"
97#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000098#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000099#include "kex.h"
100#include "mac.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +1000101#include "sshpty.h"
Darren Tucker46bc0752004-05-02 22:11:30 +1000102#include "match.h"
Damien Miller0e220db2004-06-15 10:34:08 +1000103#include "msg.h"
Darren Tucker25f60a72004-08-15 17:23:34 +1000104#include "uidswap.h"
Darren Tuckerf1de4e52010-01-08 16:54:59 +1100105#include "roaming.h"
Damien Millerb7576772006-07-10 20:23:39 +1000106#include "version.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107
Damien Miller7ea845e2010-02-12 09:21:02 +1100108#ifdef ENABLE_PKCS11
109#include "ssh-pkcs11.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +0000110#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000111
Damien Miller3f905871999-11-15 17:10:57 +1100112extern char *__progname;
Damien Miller3f905871999-11-15 17:10:57 +1100113
Damien Millerea2c1a42011-06-03 12:10:22 +1000114/* Saves a copy of argv for setproctitle emulation */
115#ifndef HAVE_SETPROCTITLE
116static char **saved_av;
117#endif
118
Darren Tuckerd6173c02008-06-13 04:52:53 +1000119/* Flag indicating whether debug mode is on. May be set on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120int debug_flag = 0;
121
Damien Miller21771e22011-05-15 08:45:50 +1000122/* Flag indicating whether a tty should be requested */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123int tty_flag = 0;
124
Damien Miller1383bd82000-04-06 12:32:37 +1000125/* don't exec a shell */
126int no_shell_flag = 0;
Damien Miller1383bd82000-04-06 12:32:37 +1000127
Damien Miller5428f641999-11-25 11:54:57 +1100128/*
129 * Flag indicating that nothing should be read from stdin. This can be set
130 * on the command line.
131 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132int stdin_null_flag = 0;
133
Damien Miller5428f641999-11-25 11:54:57 +1100134/*
Damien Millere11e1ea2010-08-03 16:04:46 +1000135 * Flag indicating that the current process should be backgrounded and
136 * a new slave launched in the foreground for ControlPersist.
137 */
138int need_controlpersist_detach = 0;
139
140/* Copies of flags for ControlPersist foreground slave */
Damien Miller21771e22011-05-15 08:45:50 +1000141int ostdin_null_flag, ono_shell_flag, otty_flag, orequest_tty;
Damien Millere11e1ea2010-08-03 16:04:46 +1000142
143/*
Damien Miller5428f641999-11-25 11:54:57 +1100144 * Flag indicating that ssh should fork after authentication. This is useful
Ben Lindstromf666fec2002-06-06 19:51:58 +0000145 * so that the passphrase can be entered manually, and then ssh goes to the
Damien Miller5428f641999-11-25 11:54:57 +1100146 * background.
147 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148int fork_after_authentication_flag = 0;
149
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100150/* forward stdio to remote host and port */
151char *stdio_forward_host = NULL;
152int stdio_forward_port = 0;
153
Damien Miller5428f641999-11-25 11:54:57 +1100154/*
155 * General data structure for command line options and options configurable
156 * in configuration files. See readconf.h.
157 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158Options options;
159
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000160/* optional user configfile */
161char *config = NULL;
162
Damien Miller5428f641999-11-25 11:54:57 +1100163/*
164 * Name of the host we are connecting to. This is the name given on the
165 * command line, or the HostName specified for the user-supplied name in a
166 * configuration file.
167 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168char *host;
169
170/* socket address the host resolves to */
Damien Miller34132e52000-01-14 15:45:46 +1100171struct sockaddr_storage hostaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000172
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000173/* Private host keys. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000174Sensitive sensitive_data;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000175
176/* Original real UID. */
177uid_t original_real_uid;
Ben Lindstromf9c48842002-06-11 16:37:51 +0000178uid_t original_effective_uid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000179
Damien Miller1383bd82000-04-06 12:32:37 +1000180/* command to be executed */
181Buffer command;
182
Damien Miller832562e2001-01-30 09:30:01 +1100183/* Should we execute a command or invoke a subsystem? */
184int subsystem_flag = 0;
185
Damien Miller2797f7f2002-04-23 21:09:44 +1000186/* # of replies received for global requests */
Darren Tucker9f407c42008-06-13 04:50:27 +1000187static int remote_forward_confirms_received = 0;
Damien Miller2797f7f2002-04-23 21:09:44 +1000188
Damien Millerb1cbfa22008-05-19 16:00:08 +1000189/* mux.c */
190extern int muxserver_sock;
191extern u_int muxclient_command;
Damien Miller0e220db2004-06-15 10:34:08 +1000192
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000193/* Prints a help message to the user. This function never returns. */
194
Ben Lindstrombba81212001-06-25 05:01:22 +0000195static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000196usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000197{
Damien Miller50955102004-03-22 09:34:58 +1100198 fprintf(stderr,
Damien Millerc13c3ee2008-11-03 19:23:28 +1100199"usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]\n"
Damien Miller03d4d7e2013-04-23 15:21:06 +1000200" [-D [bind_address:]port] [-E log_file] [-e escape_char]\n"
201" [-F configfile] [-I pkcs11] [-i identity_file]\n"
Damien Miller34bd20a2013-04-23 19:25:00 +1000202" [-L [bind_address:]port:host:hostport] [-Q protocol_feature]\n"
Damien Millerf91ee4c2005-03-01 21:24:33 +1100203" [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100204" [-R [bind_address:]port:host:hostport] [-S ctl_path]\n"
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100205" [-W host:port] [-w local_tun[:remote_tun]]\n"
206" [user@]hostname [command]\n"
Damien Miller50955102004-03-22 09:34:58 +1100207 );
Darren Tuckere9a9b712005-12-20 16:15:51 +1100208 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209}
210
Ben Lindstrombba81212001-06-25 05:01:22 +0000211static int ssh_session(void);
212static int ssh_session2(void);
213static void load_public_identity_files(void);
Damien Miller857b02e2010-09-24 22:02:56 +1000214static void main_sigchld_handler(int);
Damien Millerb1cbfa22008-05-19 16:00:08 +1000215
216/* from muxclient.c */
217void muxclient(const char *);
218void muxserver_listen(void);
Damien Miller1383bd82000-04-06 12:32:37 +1000219
Damien Miller295ee632011-05-29 21:42:31 +1000220/* ~/ expand a list of paths. NB. assumes path[n] is heap-allocated. */
221static void
222tilde_expand_paths(char **paths, u_int num_paths)
223{
224 u_int i;
225 char *cp;
226
227 for (i = 0; i < num_paths; i++) {
228 cp = tilde_expand_filename(paths[i], original_real_uid);
Darren Tuckera627d422013-06-02 07:31:17 +1000229 free(paths[i]);
Damien Miller295ee632011-05-29 21:42:31 +1000230 paths[i] = cp;
231 }
232}
233
Damien Miller0faf7472013-10-17 11:47:23 +1100234static struct addrinfo *
235resolve_host(const char *name, u_int port, int logerr, char *cname, size_t clen)
236{
237 char strport[NI_MAXSERV];
238 struct addrinfo hints, *res;
239 int gaierr, loglevel = SYSLOG_LEVEL_DEBUG1;
240
241 snprintf(strport, sizeof strport, "%u", port);
242 bzero(&hints, sizeof(hints));
243 hints.ai_family = options.address_family;
244 hints.ai_socktype = SOCK_STREAM;
245 if (cname != NULL)
246 hints.ai_flags = AI_CANONNAME;
247 if ((gaierr = getaddrinfo(name, strport, &hints, &res)) != 0) {
248 if (logerr || (gaierr != EAI_NONAME && gaierr != EAI_NODATA))
249 loglevel = SYSLOG_LEVEL_ERROR;
250 do_log2(loglevel, "%s: Could not resolve hostname %.100s: %s",
251 __progname, name, ssh_gai_strerror(gaierr));
252 return NULL;
253 }
254 if (cname != NULL && res->ai_canonname != NULL) {
255 if (strlcpy(cname, res->ai_canonname, clen) >= clen) {
256 error("%s: host \"%s\" cname \"%s\" too long (max %lu)",
257 __func__, name, res->ai_canonname, (u_long)clen);
258 if (clen > 0)
259 *cname = '\0';
260 }
261 }
262 return res;
263}
264
265/*
266 * Check whether the cname is a permitted replacement for the hostname
267 * and perform the replacement if it is.
268 */
269static int
270check_follow_cname(char **namep, const char *cname)
271{
272 int i;
273 struct allowed_cname *rule;
274
275 if (*cname == '\0' || options.num_permitted_cnames == 0 ||
276 strcmp(*namep, cname) == 0)
277 return 0;
Damien Miller38505592013-10-17 11:48:13 +1100278 if (options.canonicalize_hostname == SSH_CANONICALISE_NO)
Damien Miller0faf7472013-10-17 11:47:23 +1100279 return 0;
280 /*
Damien Miller38505592013-10-17 11:48:13 +1100281 * Don't attempt to canonicalize names that will be interpreted by
Damien Miller0faf7472013-10-17 11:47:23 +1100282 * a proxy unless the user specifically requests so.
283 */
284 if (options.proxy_command != NULL &&
Damien Miller38505592013-10-17 11:48:13 +1100285 options.canonicalize_hostname != SSH_CANONICALISE_ALWAYS)
Damien Miller0faf7472013-10-17 11:47:23 +1100286 return 0;
287 debug3("%s: check \"%s\" CNAME \"%s\"", __func__, *namep, cname);
288 for (i = 0; i < options.num_permitted_cnames; i++) {
289 rule = options.permitted_cnames + i;
290 if (match_pattern_list(*namep, rule->source_list,
291 strlen(rule->source_list), 1) != 1 ||
292 match_pattern_list(cname, rule->target_list,
293 strlen(rule->target_list), 1) != 1)
294 continue;
Damien Miller38505592013-10-17 11:48:13 +1100295 verbose("Canonicalized DNS aliased hostname "
Damien Miller0faf7472013-10-17 11:47:23 +1100296 "\"%s\" => \"%s\"", *namep, cname);
297 free(*namep);
298 *namep = xstrdup(cname);
299 return 1;
300 }
301 return 0;
302}
303
304/*
305 * Attempt to resolve the supplied hostname after applying the user's
Damien Miller51682fa2013-10-17 11:48:31 +1100306 * canonicalization rules. Returns the address list for the host or NULL
307 * if no name was found after canonicalization.
Damien Miller0faf7472013-10-17 11:47:23 +1100308 */
309static struct addrinfo *
Damien Miller38505592013-10-17 11:48:13 +1100310resolve_canonicalize(char **hostp, u_int port)
Damien Miller0faf7472013-10-17 11:47:23 +1100311{
312 int i, ndots;
313 char *cp, *fullhost, cname_target[NI_MAXHOST];
314 struct addrinfo *addrs;
315
Damien Miller38505592013-10-17 11:48:13 +1100316 if (options.canonicalize_hostname == SSH_CANONICALISE_NO)
Damien Miller0faf7472013-10-17 11:47:23 +1100317 return NULL;
318 /*
Damien Miller38505592013-10-17 11:48:13 +1100319 * Don't attempt to canonicalize names that will be interpreted by
Damien Miller0faf7472013-10-17 11:47:23 +1100320 * a proxy unless the user specifically requests so.
321 */
322 if (options.proxy_command != NULL &&
Damien Miller38505592013-10-17 11:48:13 +1100323 options.canonicalize_hostname != SSH_CANONICALISE_ALWAYS)
Damien Miller0faf7472013-10-17 11:47:23 +1100324 return NULL;
Damien Miller51682fa2013-10-17 11:48:31 +1100325 /* Don't apply canonicalization to sufficiently-qualified hostnames */
Damien Miller0faf7472013-10-17 11:47:23 +1100326 ndots = 0;
327 for (cp = *hostp; *cp != '\0'; cp++) {
328 if (*cp == '.')
329 ndots++;
330 }
Damien Miller38505592013-10-17 11:48:13 +1100331 if (ndots > options.canonicalize_max_dots) {
332 debug3("%s: not canonicalizing hostname \"%s\" (max dots %d)",
333 __func__, *hostp, options.canonicalize_max_dots);
Damien Miller0faf7472013-10-17 11:47:23 +1100334 return NULL;
335 }
336 /* Attempt each supplied suffix */
337 for (i = 0; i < options.num_canonical_domains; i++) {
338 *cname_target = '\0';
339 xasprintf(&fullhost, "%s.%s.", *hostp,
340 options.canonical_domains[i]);
341 if ((addrs = resolve_host(fullhost, options.port, 0,
342 cname_target, sizeof(cname_target))) == NULL) {
343 free(fullhost);
344 continue;
345 }
346 /* Remove trailing '.' */
347 fullhost[strlen(fullhost) - 1] = '\0';
348 /* Follow CNAME if requested */
349 if (!check_follow_cname(&fullhost, cname_target)) {
Damien Miller38505592013-10-17 11:48:13 +1100350 debug("Canonicalized hostname \"%s\" => \"%s\"",
Damien Miller0faf7472013-10-17 11:47:23 +1100351 *hostp, fullhost);
352 }
353 free(*hostp);
354 *hostp = fullhost;
355 return addrs;
356 }
Damien Miller38505592013-10-17 11:48:13 +1100357 if (!options.canonicalize_fallback_local)
Damien Miller0faf7472013-10-17 11:47:23 +1100358 fatal("%s: Could not resolve host \"%s\"", __progname, host);
359 return NULL;
360}
361
Damien Miller95def091999-11-25 00:26:21 +1100362/*
363 * Main program for the ssh client.
364 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000365int
366main(int ac, char **av)
367{
Darren Tucker199b1342009-07-06 07:16:56 +1000368 int i, r, opt, exit_status, use_syslog;
Damien Miller03d4d7e2013-04-23 15:21:06 +1000369 char *p, *cp, *line, *argv0, buf[MAXPATHLEN], *host_arg, *logfile;
Damien Millerdfc85fa2011-05-15 08:44:02 +1000370 char thishost[NI_MAXHOST], shorthost[NI_MAXHOST], portstr[NI_MAXSERV];
Damien Miller0faf7472013-10-17 11:47:23 +1100371 char cname[NI_MAXHOST];
Damien Miller95def091999-11-25 00:26:21 +1100372 struct stat st;
Ben Lindstrom086cf212001-03-05 05:56:40 +0000373 struct passwd *pw;
Damien Miller194fd902013-10-15 12:13:05 +1100374 int timeout_ms;
Ben Lindstrom5ccf63a2001-09-24 20:00:10 +0000375 extern int optind, optreset;
Damien Miller4f8e6692001-07-14 13:22:53 +1000376 extern char *optarg;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100377 Forward fwd;
Damien Miller0faf7472013-10-17 11:47:23 +1100378 struct addrinfo *addrs = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000379
Darren Tuckerce321d82005-10-03 18:11:24 +1000380 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
381 sanitise_stdfd();
382
Damien Miller59d3d5b2003-08-22 09:34:41 +1000383 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000384
Damien Millerea2c1a42011-06-03 12:10:22 +1000385#ifndef HAVE_SETPROCTITLE
386 /* Prepare for later setproctitle emulation */
387 /* Save argv so it isn't clobbered by setproctitle() emulation */
388 saved_av = xcalloc(ac + 1, sizeof(*saved_av));
389 for (i = 0; i < ac; i++)
390 saved_av[i] = xstrdup(av[i]);
391 saved_av[i] = NULL;
392 compat_init_setproctitle(ac, av);
393 av = saved_av;
394#endif
395
Damien Miller5428f641999-11-25 11:54:57 +1100396 /*
Damien Miller00d9ae22010-08-17 01:59:31 +1000397 * Discard other fds that are hanging around. These can cause problem
398 * with backgrounded ssh processes started by ControlPersist.
399 */
400 closefrom(STDERR_FILENO + 1);
401
402 /*
Damien Miller5428f641999-11-25 11:54:57 +1100403 * Save the original real uid. It will be needed later (uid-swapping
404 * may clobber the real uid).
405 */
Damien Miller95def091999-11-25 00:26:21 +1100406 original_real_uid = getuid();
407 original_effective_uid = geteuid();
Damien Millera8e06ce2003-11-21 23:48:55 +1100408
Damien Miller50b9a602002-09-04 16:50:06 +1000409 /*
410 * Use uid-swapping to give up root privileges for the duration of
411 * option processing. We will re-instantiate the rights when we are
412 * ready to create the privileged port, and will permanently drop
413 * them when the port has been created (actually, when the connection
414 * has been made, as we may need to create the port several times).
415 */
416 PRIV_END;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000417
Damien Miller05dd7952000-10-01 00:42:48 +1100418#ifdef HAVE_SETRLIMIT
Damien Miller95def091999-11-25 00:26:21 +1100419 /* If we are installed setuid root be careful to not drop core. */
420 if (original_real_uid != original_effective_uid) {
421 struct rlimit rlim;
422 rlim.rlim_cur = rlim.rlim_max = 0;
423 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
424 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000425 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100426#endif
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000427 /* Get user data. */
428 pw = getpwuid(original_real_uid);
429 if (!pw) {
Damien Miller3009d3c2013-07-20 13:22:31 +1000430 logit("No user exists for uid %lu", (u_long)original_real_uid);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100431 exit(255);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000432 }
433 /* Take a copy of the returned structure. */
434 pw = pwcopy(pw);
435
Damien Miller5428f641999-11-25 11:54:57 +1100436 /*
Damien Miller5428f641999-11-25 11:54:57 +1100437 * Set our umask to something reasonable, as some files are created
438 * with the default umask. This will make them world-readable but
439 * writable only by the owner, which is ok for all files for which we
440 * don't set the modes explicitly.
441 */
Damien Miller95def091999-11-25 00:26:21 +1100442 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000443
Darren Tuckerd6173c02008-06-13 04:52:53 +1000444 /*
445 * Initialize option structure to indicate that no values have been
446 * set.
447 */
Damien Miller95def091999-11-25 00:26:21 +1100448 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000449
Damien Miller95def091999-11-25 00:26:21 +1100450 /* Parse command-line arguments. */
451 host = NULL;
Damien Millere272a5b2008-11-03 19:22:37 +1100452 use_syslog = 0;
Damien Miller03d4d7e2013-04-23 15:21:06 +1000453 logfile = NULL;
Darren Tucker72efd742009-06-21 17:48:00 +1000454 argv0 = av[0];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000455
Damien Miller2ecb6bd2006-03-15 12:03:53 +1100456 again:
Darren Tuckerd6173c02008-06-13 04:52:53 +1000457 while ((opt = getopt(ac, av, "1246ab:c:e:fgi:kl:m:no:p:qstvx"
Damien Millerea111192013-04-23 19:24:32 +1000458 "ACD:E:F:I:KL:MNO:PQ:R:S:TVw:W:XYy")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100459 switch (opt) {
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000460 case '1':
461 options.protocol = SSH_PROTO_1;
462 break;
Damien Miller4af51302000-04-16 11:18:38 +1000463 case '2':
464 options.protocol = SSH_PROTO_2;
465 break;
Damien Miller34132e52000-01-14 15:45:46 +1100466 case '4':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000467 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100468 break;
Damien Miller34132e52000-01-14 15:45:46 +1100469 case '6':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000470 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100471 break;
Damien Miller95def091999-11-25 00:26:21 +1100472 case 'n':
473 stdin_null_flag = 1;
474 break;
Damien Miller95def091999-11-25 00:26:21 +1100475 case 'f':
476 fork_after_authentication_flag = 1;
477 stdin_null_flag = 1;
478 break;
Damien Miller95def091999-11-25 00:26:21 +1100479 case 'x':
480 options.forward_x11 = 0;
481 break;
Damien Miller95def091999-11-25 00:26:21 +1100482 case 'X':
483 options.forward_x11 = 1;
484 break;
Damien Millere272a5b2008-11-03 19:22:37 +1100485 case 'y':
486 use_syslog = 1;
487 break;
Damien Miller03d4d7e2013-04-23 15:21:06 +1000488 case 'E':
489 logfile = xstrdup(optarg);
490 break;
Darren Tucker0a118da2003-10-15 15:54:32 +1000491 case 'Y':
492 options.forward_x11 = 1;
493 options.forward_x11_trusted = 1;
494 break;
Damien Miller95def091999-11-25 00:26:21 +1100495 case 'g':
496 options.gateway_ports = 1;
497 break;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100498 case 'O':
Damien Millere1537f92010-01-26 13:26:22 +1100499 if (stdio_forward_host != NULL)
500 fatal("Cannot specify multiplexing "
501 "command with -W");
502 else if (muxclient_command != 0)
503 fatal("Multiplexing command already specified");
Darren Tucker7ebfc102004-11-07 20:06:19 +1100504 if (strcmp(optarg, "check") == 0)
Damien Millerb1cbfa22008-05-19 16:00:08 +1000505 muxclient_command = SSHMUX_COMMAND_ALIVE_CHECK;
Damien Miller388f6fc2010-05-21 14:57:35 +1000506 else if (strcmp(optarg, "forward") == 0)
507 muxclient_command = SSHMUX_COMMAND_FORWARD;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100508 else if (strcmp(optarg, "exit") == 0)
Damien Millerb1cbfa22008-05-19 16:00:08 +1000509 muxclient_command = SSHMUX_COMMAND_TERMINATE;
Damien Miller6c3eec72011-05-05 14:16:22 +1000510 else if (strcmp(optarg, "stop") == 0)
511 muxclient_command = SSHMUX_COMMAND_STOP;
Damien Millerf6dff7c2011-09-22 21:38:52 +1000512 else if (strcmp(optarg, "cancel") == 0)
513 muxclient_command = SSHMUX_COMMAND_CANCEL_FWD;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100514 else
515 fatal("Invalid multiplex command.");
516 break;
Damien Miller147bba32002-09-04 16:46:06 +1000517 case 'P': /* deprecated */
Damien Miller95def091999-11-25 00:26:21 +1100518 options.use_privileged_port = 0;
519 break;
Damien Millerea111192013-04-23 19:24:32 +1000520 case 'Q': /* deprecated */
521 cp = NULL;
522 if (strcasecmp(optarg, "cipher") == 0)
523 cp = cipher_alg_list();
524 else if (strcasecmp(optarg, "mac") == 0)
525 cp = mac_alg_list();
526 else if (strcasecmp(optarg, "kex") == 0)
527 cp = kex_alg_list();
528 else if (strcasecmp(optarg, "key") == 0)
529 cp = key_alg_list();
530 if (cp == NULL)
531 fatal("Unsupported query \"%s\"", optarg);
532 printf("%s\n", cp);
533 free(cp);
534 exit(0);
535 break;
Damien Miller95def091999-11-25 00:26:21 +1100536 case 'a':
537 options.forward_agent = 0;
538 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000539 case 'A':
540 options.forward_agent = 1;
541 break;
Damien Miller95def091999-11-25 00:26:21 +1100542 case 'k':
Damien Millere0113cc2003-11-24 13:10:09 +1100543 options.gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100544 break;
Darren Tucker415bddc2007-06-12 23:43:16 +1000545 case 'K':
546 options.gss_authentication = 1;
547 options.gss_deleg_creds = 1;
548 break;
Damien Miller95def091999-11-25 00:26:21 +1100549 case 'i':
550 if (stat(optarg, &st) < 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000551 fprintf(stderr, "Warning: Identity file %s "
Damien Miller3eb48b62005-03-01 21:15:46 +1100552 "not accessible: %s.\n", optarg,
553 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100554 break;
555 }
Darren Tucker19104782013-04-05 11:13:08 +1100556 add_identity_file(&options, NULL, optarg, 1);
Damien Miller95def091999-11-25 00:26:21 +1100557 break;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000558 case 'I':
Damien Miller7ea845e2010-02-12 09:21:02 +1100559#ifdef ENABLE_PKCS11
560 options.pkcs11_provider = xstrdup(optarg);
Ben Lindstrombcc18082001-08-06 21:59:25 +0000561#else
Damien Miller7ea845e2010-02-12 09:21:02 +1100562 fprintf(stderr, "no support for PKCS#11.\n");
Ben Lindstrombcc18082001-08-06 21:59:25 +0000563#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000564 break;
Damien Miller95def091999-11-25 00:26:21 +1100565 case 't':
Damien Miller21771e22011-05-15 08:45:50 +1000566 if (options.request_tty == REQUEST_TTY_YES)
567 options.request_tty = REQUEST_TTY_FORCE;
568 else
569 options.request_tty = REQUEST_TTY_YES;
Damien Miller95def091999-11-25 00:26:21 +1100570 break;
Damien Miller95def091999-11-25 00:26:21 +1100571 case 'v':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000572 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100573 debug_flag = 1;
574 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000575 } else {
576 if (options.log_level < SYSLOG_LEVEL_DEBUG3)
577 options.log_level++;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000578 }
Damien Miller03d4d7e2013-04-23 15:21:06 +1000579 break;
Damien Miller95def091999-11-25 00:26:21 +1100580 case 'V':
Damien Miller0c889cd2004-03-22 09:36:00 +1100581 fprintf(stderr, "%s, %s\n",
Damien Miller2aa6d3c2004-09-12 16:53:04 +1000582 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
Damien Miller95def091999-11-25 00:26:21 +1100583 if (opt == 'V')
584 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100585 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100586 case 'w':
Damien Miller7b58e802005-12-13 19:33:19 +1100587 if (options.tun_open == -1)
588 options.tun_open = SSH_TUNMODE_DEFAULT;
Damien Millerd27b9472005-12-13 19:29:02 +1100589 options.tun_local = a2tun(optarg, &options.tun_remote);
Damien Miller7b58e802005-12-13 19:33:19 +1100590 if (options.tun_local == SSH_TUNID_ERR) {
Darren Tuckerd6173c02008-06-13 04:52:53 +1000591 fprintf(stderr,
592 "Bad tun device '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100593 exit(255);
Damien Millerd27b9472005-12-13 19:29:02 +1100594 }
595 break;
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100596 case 'W':
Damien Millere1537f92010-01-26 13:26:22 +1100597 if (stdio_forward_host != NULL)
598 fatal("stdio forward already specified");
599 if (muxclient_command != 0)
600 fatal("Cannot specify stdio forward with -O");
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100601 if (parse_forward(&fwd, optarg, 1, 0)) {
602 stdio_forward_host = fwd.listen_host;
603 stdio_forward_port = fwd.listen_port;
Darren Tuckera627d422013-06-02 07:31:17 +1000604 free(fwd.connect_host);
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100605 } else {
606 fprintf(stderr,
607 "Bad stdio forwarding specification '%s'\n",
608 optarg);
609 exit(255);
610 }
Damien Miller21771e22011-05-15 08:45:50 +1000611 options.request_tty = REQUEST_TTY_NO;
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100612 no_shell_flag = 1;
613 options.clear_forwardings = 1;
614 options.exit_on_forward_failure = 1;
615 break;
Damien Miller95def091999-11-25 00:26:21 +1100616 case 'q':
617 options.log_level = SYSLOG_LEVEL_QUIET;
618 break;
Damien Miller95def091999-11-25 00:26:21 +1100619 case 'e':
620 if (optarg[0] == '^' && optarg[2] == 0 &&
Damien Millerf4614452001-07-14 12:18:10 +1000621 (u_char) optarg[1] >= 64 &&
622 (u_char) optarg[1] < 128)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000623 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100624 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000625 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100626 else if (strcmp(optarg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000627 options.escape_char = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100628 else {
Damien Millerf4614452001-07-14 12:18:10 +1000629 fprintf(stderr, "Bad escape character '%s'.\n",
630 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100631 exit(255);
Damien Miller95def091999-11-25 00:26:21 +1100632 }
633 break;
Damien Miller95def091999-11-25 00:26:21 +1100634 case 'c':
Damien Millereba71ba2000-04-29 23:57:08 +1000635 if (ciphers_valid(optarg)) {
636 /* SSH2 only */
637 options.ciphers = xstrdup(optarg);
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000638 options.cipher = SSH_CIPHER_INVALID;
Damien Millereba71ba2000-04-29 23:57:08 +1000639 } else {
640 /* SSH1 only */
Damien Millere39cacc2000-11-29 12:18:44 +1100641 options.cipher = cipher_number(optarg);
642 if (options.cipher == -1) {
Damien Millerf4614452001-07-14 12:18:10 +1000643 fprintf(stderr,
644 "Unknown cipher type '%s'\n",
645 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100646 exit(255);
Damien Millereba71ba2000-04-29 23:57:08 +1000647 }
Damien Millerf4614452001-07-14 12:18:10 +1000648 if (options.cipher == SSH_CIPHER_3DES)
Damien Millere39cacc2000-11-29 12:18:44 +1100649 options.ciphers = "3des-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000650 else if (options.cipher == SSH_CIPHER_BLOWFISH)
Damien Millere39cacc2000-11-29 12:18:44 +1100651 options.ciphers = "blowfish-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000652 else
Damien Millere39cacc2000-11-29 12:18:44 +1100653 options.ciphers = (char *)-1;
Damien Miller95def091999-11-25 00:26:21 +1100654 }
655 break;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000656 case 'm':
657 if (mac_valid(optarg))
658 options.macs = xstrdup(optarg);
659 else {
Damien Millerf4614452001-07-14 12:18:10 +1000660 fprintf(stderr, "Unknown mac type '%s'\n",
661 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100662 exit(255);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000663 }
664 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000665 case 'M':
Damien Millerd14b1e72005-06-16 13:19:41 +1000666 if (options.control_master == SSHCTL_MASTER_YES)
667 options.control_master = SSHCTL_MASTER_ASK;
668 else
669 options.control_master = SSHCTL_MASTER_YES;
Damien Miller0e220db2004-06-15 10:34:08 +1000670 break;
Damien Miller95def091999-11-25 00:26:21 +1100671 case 'p':
Ben Lindstrom19066a12001-04-12 23:39:26 +0000672 options.port = a2port(optarg);
Damien Miller3dc71ad2009-01-28 16:31:22 +1100673 if (options.port <= 0) {
Ben Lindstrom146edb92001-04-11 23:06:28 +0000674 fprintf(stderr, "Bad port '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100675 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000676 }
Damien Miller95def091999-11-25 00:26:21 +1100677 break;
Damien Miller95def091999-11-25 00:26:21 +1100678 case 'l':
679 options.user = optarg;
680 break;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000681
Damien Miller95def091999-11-25 00:26:21 +1100682 case 'L':
Damien Miller4bf648f2009-02-14 16:28:21 +1100683 if (parse_forward(&fwd, optarg, 0, 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +1100684 add_local_forward(&options, &fwd);
685 else {
Damien Millerf4614452001-07-14 12:18:10 +1000686 fprintf(stderr,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100687 "Bad local forwarding specification '%s'\n",
Damien Millerf4614452001-07-14 12:18:10 +1000688 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100689 exit(255);
Ben Lindstrom1a174712001-09-12 17:56:15 +0000690 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100691 break;
692
693 case 'R':
Damien Miller4bf648f2009-02-14 16:28:21 +1100694 if (parse_forward(&fwd, optarg, 0, 1)) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100695 add_remote_forward(&options, &fwd);
696 } else {
697 fprintf(stderr,
698 "Bad remote forwarding specification "
699 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100700 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100701 }
Damien Miller95def091999-11-25 00:26:21 +1100702 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000703
704 case 'D':
Damien Miller4bf648f2009-02-14 16:28:21 +1100705 if (parse_forward(&fwd, optarg, 1, 0)) {
Damien Millera699d952008-11-03 19:27:34 +1100706 add_local_forward(&options, &fwd);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100707 } else {
Damien Millera699d952008-11-03 19:27:34 +1100708 fprintf(stderr,
709 "Bad dynamic forwarding specification "
710 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100711 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000712 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000713 break;
714
Damien Miller95def091999-11-25 00:26:21 +1100715 case 'C':
716 options.compression = 1;
717 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000718 case 'N':
719 no_shell_flag = 1;
Damien Miller21771e22011-05-15 08:45:50 +1000720 options.request_tty = REQUEST_TTY_NO;
Damien Miller1383bd82000-04-06 12:32:37 +1000721 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000722 case 'T':
Damien Miller21771e22011-05-15 08:45:50 +1000723 options.request_tty = REQUEST_TTY_NO;
Damien Miller1383bd82000-04-06 12:32:37 +1000724 break;
Damien Miller95def091999-11-25 00:26:21 +1100725 case 'o':
Damien Miller9836cf82003-12-17 16:30:06 +1100726 line = xstrdup(optarg);
Damien Miller194fd902013-10-15 12:13:05 +1100727 if (process_config_line(&options, pw, host ? host : "",
728 line, "command-line", 0, NULL, SSHCONF_USERCONF)
Darren Tuckeraefa3682013-04-05 11:18:35 +1100729 != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100730 exit(255);
Darren Tuckera627d422013-06-02 07:31:17 +1000731 free(line);
Damien Miller95def091999-11-25 00:26:21 +1100732 break;
Damien Miller832562e2001-01-30 09:30:01 +1100733 case 's':
734 subsystem_flag = 1;
735 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000736 case 'S':
737 if (options.control_path != NULL)
738 free(options.control_path);
739 options.control_path = xstrdup(optarg);
Damien Miller0e220db2004-06-15 10:34:08 +1000740 break;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000741 case 'b':
742 options.bind_address = optarg;
743 break;
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000744 case 'F':
745 config = optarg;
746 break;
Damien Miller95def091999-11-25 00:26:21 +1100747 default:
748 usage();
749 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000750 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000751
Damien Millerf4614452001-07-14 12:18:10 +1000752 ac -= optind;
753 av += optind;
754
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100755 if (ac > 0 && !host) {
Ben Lindstromc276c122002-12-23 02:14:51 +0000756 if (strrchr(*av, '@')) {
Damien Millerf4614452001-07-14 12:18:10 +1000757 p = xstrdup(*av);
Ben Lindstromc276c122002-12-23 02:14:51 +0000758 cp = strrchr(p, '@');
Damien Millerf4614452001-07-14 12:18:10 +1000759 if (cp == NULL || cp == p)
760 usage();
761 options.user = p;
762 *cp = '\0';
Damien Miller0faf7472013-10-17 11:47:23 +1100763 host = xstrdup(++cp);
Damien Millerf4614452001-07-14 12:18:10 +1000764 } else
Damien Miller0faf7472013-10-17 11:47:23 +1100765 host = xstrdup(*av);
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000766 if (ac > 1) {
767 optind = optreset = 1;
Damien Millerf4614452001-07-14 12:18:10 +1000768 goto again;
769 }
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000770 ac--, av++;
Damien Millerf4614452001-07-14 12:18:10 +1000771 }
772
Damien Miller95def091999-11-25 00:26:21 +1100773 /* Check that we got a host name. */
774 if (!host)
775 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000776
Damien Miller0faf7472013-10-17 11:47:23 +1100777 lowercase(host);
778 host_arg = xstrdup(host);
779
Damien Miller4314c2b2010-09-10 11:12:09 +1000780 OpenSSL_add_all_algorithms();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100781 ERR_load_crypto_strings();
Damien Millercb5e44a2000-09-29 12:12:36 +1100782
Damien Miller95def091999-11-25 00:26:21 +1100783 /* Initialize the command to execute on remote host. */
784 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000785
Damien Miller5428f641999-11-25 11:54:57 +1100786 /*
787 * Save the command to execute on the remote host in a buffer. There
788 * is no limit on the length of the command, except by the maximum
789 * packet size. Also sets the tty flag if there is no command.
790 */
Damien Millerf4614452001-07-14 12:18:10 +1000791 if (!ac) {
Damien Miller95def091999-11-25 00:26:21 +1100792 /* No command specified - execute shell on a tty. */
Damien Miller832562e2001-01-30 09:30:01 +1100793 if (subsystem_flag) {
Damien Millerf4614452001-07-14 12:18:10 +1000794 fprintf(stderr,
795 "You must specify a subsystem to invoke.\n");
Damien Miller832562e2001-01-30 09:30:01 +1100796 usage();
797 }
Damien Miller95def091999-11-25 00:26:21 +1100798 } else {
Damien Millerf4614452001-07-14 12:18:10 +1000799 /* A command has been specified. Store it into the buffer. */
800 for (i = 0; i < ac; i++) {
801 if (i)
Damien Miller95def091999-11-25 00:26:21 +1100802 buffer_append(&command, " ", 1);
803 buffer_append(&command, av[i], strlen(av[i]));
804 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000805 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000806
Damien Miller95def091999-11-25 00:26:21 +1100807 /* Cannot fork to background if no command. */
Darren Tuckerd6173c02008-06-13 04:52:53 +1000808 if (fork_after_authentication_flag && buffer_len(&command) == 0 &&
809 !no_shell_flag)
810 fatal("Cannot fork into background without a command "
811 "to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000812
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000813 /*
814 * Initialize "log" output. Since we are the client all output
Damien Miller03d4d7e2013-04-23 15:21:06 +1000815 * goes to stderr unless otherwise specified by -y or -E.
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000816 */
Damien Miller03d4d7e2013-04-23 15:21:06 +1000817 if (use_syslog && logfile != NULL)
818 fatal("Can't specify both -y and -E");
819 if (logfile != NULL) {
820 log_redirect_stderr_to(logfile);
Darren Tuckera627d422013-06-02 07:31:17 +1000821 free(logfile);
Damien Miller03d4d7e2013-04-23 15:21:06 +1000822 }
Darren Tucker72efd742009-06-21 17:48:00 +1000823 log_init(argv0,
Darren Tuckerd6173c02008-06-13 04:52:53 +1000824 options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
Damien Millere272a5b2008-11-03 19:22:37 +1100825 SYSLOG_FACILITY_USER, !use_syslog);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000826
Damien Miller03d4d7e2013-04-23 15:21:06 +1000827 if (debug_flag)
828 logit("%s, %s", SSH_VERSION, SSLeay_version(SSLEAY_VERSION));
829
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000830 /*
831 * Read per-user configuration file. Ignore the system wide config
832 * file if the user specifies a config file on the command line.
833 */
834 if (config != NULL) {
Damien Miller508b6c32013-04-23 15:18:28 +1000835 if (strcasecmp(config, "none") != 0 &&
Damien Miller194fd902013-10-15 12:13:05 +1100836 !read_config_file(config, pw, host, &options,
837 SSHCONF_USERCONF))
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000838 fatal("Can't open user config file %.100s: "
839 "%.100s", config, strerror(errno));
Damien Miller80163902007-01-05 16:30:16 +1100840 } else {
Darren Tucker199b1342009-07-06 07:16:56 +1000841 r = snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir,
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000842 _PATH_SSH_USER_CONFFILE);
Darren Tucker199b1342009-07-06 07:16:56 +1000843 if (r > 0 && (size_t)r < sizeof(buf))
Damien Miller194fd902013-10-15 12:13:05 +1100844 (void)read_config_file(buf, pw, host, &options,
Darren Tuckeraefa3682013-04-05 11:18:35 +1100845 SSHCONF_CHECKPERM|SSHCONF_USERCONF);
Ben Lindstrom83f07d12001-10-03 17:22:29 +0000846
Damien Miller765f8c42011-08-06 06:18:16 +1000847 /* Read systemwide configuration file after user config. */
Damien Miller194fd902013-10-15 12:13:05 +1100848 (void)read_config_file(_PATH_HOST_CONFIG_FILE, pw, host,
Damien Miller57a44762004-04-20 20:11:57 +1000849 &options, 0);
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000850 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000851
Damien Miller95def091999-11-25 00:26:21 +1100852 /* Fill configuration defaults. */
853 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000854
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000855 channel_set_af(options.address_family);
856
Damien Millere9fc72e2013-10-15 12:14:12 +1100857 /* Tidy and check options */
858 if (options.host_key_alias != NULL)
859 lowercase(options.host_key_alias);
860 if (options.proxy_command != NULL &&
861 strcmp(options.proxy_command, "-") == 0 &&
862 options.proxy_use_fdpass)
863 fatal("ProxyCommand=- and ProxyUseFDPass are incompatible");
Damien Miller0faf7472013-10-17 11:47:23 +1100864#ifndef HAVE_CYGWIN
865 if (original_effective_uid != 0)
866 options.use_privileged_port = 0;
867#endif
Damien Millere9fc72e2013-10-15 12:14:12 +1100868
Damien Miller95def091999-11-25 00:26:21 +1100869 /* reinit */
Darren Tucker72efd742009-06-21 17:48:00 +1000870 log_init(argv0, options.log_level, SYSLOG_FACILITY_USER, !use_syslog);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000871
Damien Millerfff9f092012-07-06 13:45:01 +1000872 if (options.request_tty == REQUEST_TTY_YES ||
873 options.request_tty == REQUEST_TTY_FORCE)
874 tty_flag = 1;
875
876 /* Allocate a tty by default if no command specified. */
877 if (buffer_len(&command) == 0)
878 tty_flag = options.request_tty != REQUEST_TTY_NO;
879
880 /* Force no tty */
881 if (options.request_tty == REQUEST_TTY_NO || muxclient_command != 0)
882 tty_flag = 0;
883 /* Do not allocate a tty if stdin is not a tty. */
884 if ((!isatty(fileno(stdin)) || stdin_null_flag) &&
885 options.request_tty != REQUEST_TTY_FORCE) {
886 if (tty_flag)
887 logit("Pseudo-terminal will not be allocated because "
888 "stdin is not a terminal.");
889 tty_flag = 0;
890 }
891
Damien Miller60bc5172001-03-19 09:38:15 +1100892 seed_rng();
893
Damien Miller95def091999-11-25 00:26:21 +1100894 if (options.user == NULL)
895 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000896
Darren Tuckerf6b01b72008-06-13 04:56:37 +1000897 /* Get default port if port has not been set. */
Damien Miller194fd902013-10-15 12:13:05 +1100898 if (options.port == 0)
899 options.port = default_ssh_port();
Darren Tuckerf6b01b72008-06-13 04:56:37 +1000900
Damien Miller83f8a402011-01-07 09:51:17 +1100901 /* preserve host name given on command line for %n expansion */
Damien Millerd0244d42010-07-16 13:56:43 +1000902 if (options.hostname != NULL) {
Damien Millereff5cad2013-10-23 16:31:10 +1100903 /* NB. Please keep in sync with readconf.c:match_cfg_line() */
Damien Miller0faf7472013-10-17 11:47:23 +1100904 cp = percent_expand(options.hostname,
Damien Millerd0244d42010-07-16 13:56:43 +1000905 "h", host, (char *)NULL);
Damien Miller0faf7472013-10-17 11:47:23 +1100906 free(host);
907 host = cp;
908 }
909
Damien Miller51682fa2013-10-17 11:48:31 +1100910 /* If canonicalization requested then try to apply it */
Damien Miller38505592013-10-17 11:48:13 +1100911 if (options.canonicalize_hostname != SSH_CANONICALISE_NO)
912 addrs = resolve_canonicalize(&host, options.port);
Damien Miller0faf7472013-10-17 11:47:23 +1100913 /*
Damien Miller51682fa2013-10-17 11:48:31 +1100914 * If canonicalization not requested, or if it failed then try to
Damien Miller0faf7472013-10-17 11:47:23 +1100915 * resolve the bare hostname name using the system resolver's usual
Damien Millera90c0332013-10-24 21:03:17 +1100916 * search rules. Skip the lookup if a ProxyCommand is being used
917 * unless the user has specifically requested canonicalisation.
Damien Miller0faf7472013-10-17 11:47:23 +1100918 */
Damien Millera90c0332013-10-24 21:03:17 +1100919 if (addrs == NULL && (options.proxy_command == NULL ||
920 options.canonicalize_hostname == SSH_CANONICALISE_ALWAYS)) {
Damien Miller0faf7472013-10-17 11:47:23 +1100921 if ((addrs = resolve_host(host, options.port, 1,
922 cname, sizeof(cname))) == NULL)
923 cleanup_exit(255); /* resolve_host logs the error */
924 check_follow_cname(&host, cname);
Damien Millerd0244d42010-07-16 13:56:43 +1000925 }
926
Damien Millerdfc85fa2011-05-15 08:44:02 +1000927 if (gethostname(thishost, sizeof(thishost)) == -1)
928 fatal("gethostname: %s", strerror(errno));
929 strlcpy(shorthost, thishost, sizeof(shorthost));
930 shorthost[strcspn(thishost, ".")] = '\0';
931 snprintf(portstr, sizeof(portstr), "%d", options.port);
Darren Tuckerf6b01b72008-06-13 04:56:37 +1000932
Damien Millerdfc85fa2011-05-15 08:44:02 +1000933 if (options.local_command != NULL) {
Darren Tuckerf6b01b72008-06-13 04:56:37 +1000934 debug3("expanding LocalCommand: %s", options.local_command);
935 cp = options.local_command;
936 options.local_command = percent_expand(cp, "d", pw->pw_dir,
Damien Miller83f8a402011-01-07 09:51:17 +1100937 "h", host, "l", thishost, "n", host_arg, "r", options.user,
Damien Millerdfc85fa2011-05-15 08:44:02 +1000938 "p", portstr, "u", pw->pw_name, "L", shorthost,
939 (char *)NULL);
Darren Tuckerf6b01b72008-06-13 04:56:37 +1000940 debug3("expanded LocalCommand: %s", options.local_command);
Darren Tuckera627d422013-06-02 07:31:17 +1000941 free(cp);
Darren Tuckerf6b01b72008-06-13 04:56:37 +1000942 }
943
Damien Miller0e220db2004-06-15 10:34:08 +1000944 if (options.control_path != NULL) {
Damien Miller6476cad2005-06-16 13:18:34 +1000945 cp = tilde_expand_filename(options.control_path,
946 original_real_uid);
Darren Tuckera627d422013-06-02 07:31:17 +1000947 free(options.control_path);
Damien Millerdfc85fa2011-05-15 08:44:02 +1000948 options.control_path = percent_expand(cp, "h", host,
949 "l", thishost, "n", host_arg, "r", options.user,
950 "p", portstr, "u", pw->pw_name, "L", shorthost,
951 (char *)NULL);
Darren Tuckera627d422013-06-02 07:31:17 +1000952 free(cp);
Damien Miller0e220db2004-06-15 10:34:08 +1000953 }
Damien Millerb1cbfa22008-05-19 16:00:08 +1000954 if (muxclient_command != 0 && options.control_path == NULL)
Darren Tucker0814d312005-06-01 23:08:51 +1000955 fatal("No ControlPath specified for \"-O\" command");
Damien Millerd14b1e72005-06-16 13:19:41 +1000956 if (options.control_path != NULL)
Damien Millerb1cbfa22008-05-19 16:00:08 +1000957 muxclient(options.control_path);
Damien Miller0e220db2004-06-15 10:34:08 +1000958
Damien Miller67bd0622007-09-17 12:06:57 +1000959 timeout_ms = options.connection_timeout * 1000;
960
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000961 /* Open a connection to the remote host. */
Damien Miller0faf7472013-10-17 11:47:23 +1100962 if (ssh_connect(host, addrs, &hostaddr, options.port,
963 options.address_family, options.connection_attempts,
964 &timeout_ms, options.tcp_keep_alive,
965 options.use_privileged_port) != 0)
966 exit(255);
967
Damien Miller28631ce2013-10-26 10:07:56 +1100968 if (addrs != NULL)
969 freeaddrinfo(addrs);
970
Damien Miller0faf7472013-10-17 11:47:23 +1100971 packet_set_timeout(options.server_alive_interval,
972 options.server_alive_count_max);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000973
Damien Miller67bd0622007-09-17 12:06:57 +1000974 if (timeout_ms > 0)
975 debug3("timeout: %d ms remain after connect", timeout_ms);
976
Damien Miller5428f641999-11-25 11:54:57 +1100977 /*
978 * If we successfully made the connection, load the host private key
979 * in case we will need it later for combined rsa-rhosts
980 * authentication. This must be done before releasing extra
981 * privileges, because the file is only readable by root.
Ben Lindstrom9e5bb572002-06-06 19:58:27 +0000982 * If we cannot access the private keys, load the public keys
983 * instead and try to execute the ssh-keysign helper instead.
Damien Miller5428f641999-11-25 11:54:57 +1100984 */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000985 sensitive_data.nkeys = 0;
986 sensitive_data.keys = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000987 sensitive_data.external_keysign = 0;
Ben Lindstrom24157572002-06-12 16:09:39 +0000988 if (options.rhosts_rsa_authentication ||
989 options.hostbased_authentication) {
Damien Millereb8b60e2010-08-31 22:41:14 +1000990 sensitive_data.nkeys = 7;
Damien Millerddd63ab2006-03-31 23:10:51 +1100991 sensitive_data.keys = xcalloc(sensitive_data.nkeys,
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000992 sizeof(Key));
Damien Miller6af914a2010-09-10 11:39:26 +1000993 for (i = 0; i < sensitive_data.nkeys; i++)
994 sensitive_data.keys[i] = NULL;
Ben Lindstromf9c48842002-06-11 16:37:51 +0000995
996 PRIV_START;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000997 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
Darren Tucker232b76f2006-05-06 17:41:51 +1000998 _PATH_HOST_KEY_FILE, "", NULL, NULL);
Damien Millerc1583312010-08-05 13:04:50 +1000999 sensitive_data.keys[1] = key_load_private_cert(KEY_DSA,
1000 _PATH_HOST_DSA_KEY_FILE, "", NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001001#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001002 sensitive_data.keys[2] = key_load_private_cert(KEY_ECDSA,
1003 _PATH_HOST_ECDSA_KEY_FILE, "", NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001004#endif
Damien Millereb8b60e2010-08-31 22:41:14 +10001005 sensitive_data.keys[3] = key_load_private_cert(KEY_RSA,
Damien Millerc1583312010-08-05 13:04:50 +10001006 _PATH_HOST_RSA_KEY_FILE, "", NULL);
Damien Millereb8b60e2010-08-31 22:41:14 +10001007 sensitive_data.keys[4] = key_load_private_type(KEY_DSA,
Darren Tucker232b76f2006-05-06 17:41:51 +10001008 _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001009#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001010 sensitive_data.keys[5] = key_load_private_type(KEY_ECDSA,
1011 _PATH_HOST_ECDSA_KEY_FILE, "", NULL, NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001012#endif
Damien Millereb8b60e2010-08-31 22:41:14 +10001013 sensitive_data.keys[6] = key_load_private_type(KEY_RSA,
Darren Tucker232b76f2006-05-06 17:41:51 +10001014 _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL);
Ben Lindstromf9c48842002-06-11 16:37:51 +00001015 PRIV_END;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001016
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +00001017 if (options.hostbased_authentication == 1 &&
1018 sensitive_data.keys[0] == NULL &&
Damien Millereb8b60e2010-08-31 22:41:14 +10001019 sensitive_data.keys[4] == NULL &&
1020 sensitive_data.keys[5] == NULL &&
1021 sensitive_data.keys[6] == NULL) {
Damien Millerc1583312010-08-05 13:04:50 +10001022 sensitive_data.keys[1] = key_load_cert(
1023 _PATH_HOST_DSA_KEY_FILE);
Damien Miller6af914a2010-09-10 11:39:26 +10001024#ifdef OPENSSL_HAS_ECC
Damien Millerc1583312010-08-05 13:04:50 +10001025 sensitive_data.keys[2] = key_load_cert(
Damien Millereb8b60e2010-08-31 22:41:14 +10001026 _PATH_HOST_ECDSA_KEY_FILE);
Damien Miller6af914a2010-09-10 11:39:26 +10001027#endif
Damien Millereb8b60e2010-08-31 22:41:14 +10001028 sensitive_data.keys[3] = key_load_cert(
Damien Millerc1583312010-08-05 13:04:50 +10001029 _PATH_HOST_RSA_KEY_FILE);
Damien Millerc1583312010-08-05 13:04:50 +10001030 sensitive_data.keys[4] = key_load_public(
Damien Millereb8b60e2010-08-31 22:41:14 +10001031 _PATH_HOST_DSA_KEY_FILE, NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001032#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +10001033 sensitive_data.keys[5] = key_load_public(
1034 _PATH_HOST_ECDSA_KEY_FILE, NULL);
Damien Miller6af914a2010-09-10 11:39:26 +10001035#endif
Damien Millereb8b60e2010-08-31 22:41:14 +10001036 sensitive_data.keys[6] = key_load_public(
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001037 _PATH_HOST_RSA_KEY_FILE, NULL);
1038 sensitive_data.external_keysign = 1;
1039 }
Damien Miller95def091999-11-25 00:26:21 +11001040 }
Damien Miller5428f641999-11-25 11:54:57 +11001041 /*
1042 * Get rid of any extra privileges that we may have. We will no
1043 * longer need them. Also, extra privileges could make it very hard
1044 * to read identity files and other non-world-readable files from the
1045 * user's home directory if it happens to be on a NFS volume where
1046 * root is mapped to nobody.
1047 */
Darren Tucker25f60a72004-08-15 17:23:34 +10001048 if (original_effective_uid == 0) {
1049 PRIV_START;
1050 permanently_set_uid(pw);
1051 }
Damien Miller95def091999-11-25 00:26:21 +11001052
Damien Miller5428f641999-11-25 11:54:57 +11001053 /*
1054 * Now that we are back to our own permissions, create ~/.ssh
Damien Miller788f2122005-11-05 15:14:59 +11001055 * directory if it doesn't already exist.
Damien Miller5428f641999-11-25 11:54:57 +11001056 */
Darren Tucker45c66d72011-11-04 10:50:40 +11001057 if (config == NULL) {
1058 r = snprintf(buf, sizeof buf, "%s%s%s", pw->pw_dir,
1059 strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
1060 if (r > 0 && (size_t)r < sizeof(buf) && stat(buf, &st) < 0) {
Darren Tucker8ccb7392010-09-10 12:28:24 +10001061#ifdef WITH_SELINUX
Darren Tucker45c66d72011-11-04 10:50:40 +11001062 ssh_selinux_setfscreatecon(buf);
Darren Tucker8ccb7392010-09-10 12:28:24 +10001063#endif
Darren Tucker45c66d72011-11-04 10:50:40 +11001064 if (mkdir(buf, 0700) < 0)
1065 error("Could not create directory '%.200s'.",
1066 buf);
Darren Tucker8ccb7392010-09-10 12:28:24 +10001067#ifdef WITH_SELINUX
Darren Tucker45c66d72011-11-04 10:50:40 +11001068 ssh_selinux_setfscreatecon(NULL);
Darren Tucker8ccb7392010-09-10 12:28:24 +10001069#endif
Darren Tucker45c66d72011-11-04 10:50:40 +11001070 }
Darren Tucker8ccb7392010-09-10 12:28:24 +10001071 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001072 /* load options.identity_files */
1073 load_public_identity_files();
1074
1075 /* Expand ~ in known host file names. */
Damien Miller295ee632011-05-29 21:42:31 +10001076 tilde_expand_paths(options.system_hostfiles,
1077 options.num_system_hostfiles);
1078 tilde_expand_paths(options.user_hostfiles, options.num_user_hostfiles);
Damien Miller95def091999-11-25 00:26:21 +11001079
Damien Miller07cd5892001-11-12 10:52:03 +11001080 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
Damien Miller857b02e2010-09-24 22:02:56 +10001081 signal(SIGCHLD, main_sigchld_handler);
Damien Miller07cd5892001-11-12 10:52:03 +11001082
Darren Tuckerd6173c02008-06-13 04:52:53 +10001083 /* Log into the remote system. Never returns if the login fails. */
Damien Miller67bd0622007-09-17 12:06:57 +10001084 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr,
Damien Millerd925dcd2010-12-01 12:21:51 +11001085 options.port, pw, timeout_ms);
Damien Miller95def091999-11-25 00:26:21 +11001086
Damien Miller383ffe62010-06-26 10:02:03 +10001087 if (packet_connection_is_on_socket()) {
1088 verbose("Authenticated to %s ([%s]:%d).", host,
1089 get_remote_ipaddr(), get_remote_port());
1090 } else {
1091 verbose("Authenticated to %s (via proxy).", host);
1092 }
1093
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001094 /* We no longer need the private host keys. Clear them now. */
1095 if (sensitive_data.nkeys != 0) {
1096 for (i = 0; i < sensitive_data.nkeys; i++) {
1097 if (sensitive_data.keys[i] != NULL) {
1098 /* Destroys contents safely */
1099 debug3("clear hostkey %d", i);
1100 key_free(sensitive_data.keys[i]);
1101 sensitive_data.keys[i] = NULL;
1102 }
1103 }
Darren Tuckera627d422013-06-02 07:31:17 +10001104 free(sensitive_data.keys);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001105 }
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +00001106 for (i = 0; i < options.num_identity_files; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10001107 free(options.identity_files[i]);
1108 options.identity_files[i] = NULL;
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +00001109 if (options.identity_keys[i]) {
1110 key_free(options.identity_keys[i]);
1111 options.identity_keys[i] = NULL;
1112 }
1113 }
Damien Miller95def091999-11-25 00:26:21 +11001114
Damien Miller1383bd82000-04-06 12:32:37 +10001115 exit_status = compat20 ? ssh_session2() : ssh_session();
1116 packet_close();
Damien Miller8c4e18a2002-09-19 12:05:02 +10001117
Damien Millerb1cbfa22008-05-19 16:00:08 +10001118 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +10001119 unlink(options.control_path);
1120
Damien Millera41ccca2010-10-07 22:07:32 +11001121 /* Kill ProxyCommand if it is running. */
1122 ssh_kill_proxy_command();
Damien Miller8c4e18a2002-09-19 12:05:02 +10001123
Damien Miller1383bd82000-04-06 12:32:37 +10001124 return exit_status;
1125}
1126
Damien Millere11e1ea2010-08-03 16:04:46 +10001127static void
1128control_persist_detach(void)
1129{
1130 pid_t pid;
Damien Miller00d9ae22010-08-17 01:59:31 +10001131 int devnull;
Damien Millere11e1ea2010-08-03 16:04:46 +10001132
1133 debug("%s: backgrounding master process", __func__);
1134
1135 /*
1136 * master (current process) into the background, and make the
1137 * foreground process a client of the backgrounded master.
1138 */
1139 switch ((pid = fork())) {
1140 case -1:
1141 fatal("%s: fork: %s", __func__, strerror(errno));
1142 case 0:
1143 /* Child: master process continues mainloop */
1144 break;
1145 default:
1146 /* Parent: set up mux slave to connect to backgrounded master */
1147 debug2("%s: background process is %ld", __func__, (long)pid);
1148 stdin_null_flag = ostdin_null_flag;
Damien Miller21771e22011-05-15 08:45:50 +10001149 options.request_tty = orequest_tty;
Damien Millere11e1ea2010-08-03 16:04:46 +10001150 tty_flag = otty_flag;
1151 close(muxserver_sock);
1152 muxserver_sock = -1;
Damien Miller5929c522010-09-10 11:17:02 +10001153 options.control_master = SSHCTL_MASTER_NO;
Damien Millere11e1ea2010-08-03 16:04:46 +10001154 muxclient(options.control_path);
1155 /* muxclient() doesn't return on success. */
1156 fatal("Failed to connect to new control master");
1157 }
Damien Miller00d9ae22010-08-17 01:59:31 +10001158 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
1159 error("%s: open(\"/dev/null\"): %s", __func__,
1160 strerror(errno));
1161 } else {
1162 if (dup2(devnull, STDIN_FILENO) == -1 ||
1163 dup2(devnull, STDOUT_FILENO) == -1)
1164 error("%s: dup2: %s", __func__, strerror(errno));
1165 if (devnull > STDERR_FILENO)
1166 close(devnull);
1167 }
Damien Miller98e27dc2013-07-25 11:55:52 +10001168 daemon(1, 1);
Damien Millerea2c1a42011-06-03 12:10:22 +10001169 setproctitle("%s [mux]", options.control_path);
Damien Millere11e1ea2010-08-03 16:04:46 +10001170}
1171
1172/* Do fork() after authentication. Used by "ssh -f" */
1173static void
1174fork_postauth(void)
1175{
1176 if (need_controlpersist_detach)
1177 control_persist_detach();
1178 debug("forking to background");
1179 fork_after_authentication_flag = 0;
1180 if (daemon(1, 1) < 0)
1181 fatal("daemon() failed: %.200s", strerror(errno));
1182}
1183
Darren Tucker9f407c42008-06-13 04:50:27 +10001184/* Callback for remote forward global requests */
1185static void
1186ssh_confirm_remote_forward(int type, u_int32_t seq, void *ctxt)
1187{
1188 Forward *rfwd = (Forward *)ctxt;
1189
Damien Miller4bf648f2009-02-14 16:28:21 +11001190 /* XXX verbose() on failure? */
Darren Tucker9f407c42008-06-13 04:50:27 +10001191 debug("remote forward %s for: listen %d, connect %s:%d",
1192 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
1193 rfwd->listen_port, rfwd->connect_host, rfwd->connect_port);
Darren Tucker68afb8c2011-10-02 18:59:03 +11001194 if (rfwd->listen_port == 0) {
1195 if (type == SSH2_MSG_REQUEST_SUCCESS) {
1196 rfwd->allocated_port = packet_get_int();
1197 logit("Allocated port %u for remote forward to %s:%d",
1198 rfwd->allocated_port,
1199 rfwd->connect_host, rfwd->connect_port);
1200 channel_update_permitted_opens(rfwd->handle,
1201 rfwd->allocated_port);
1202 } else {
1203 channel_update_permitted_opens(rfwd->handle, -1);
1204 }
Damien Miller4bf648f2009-02-14 16:28:21 +11001205 }
1206
Darren Tucker9f407c42008-06-13 04:50:27 +10001207 if (type == SSH2_MSG_REQUEST_FAILURE) {
1208 if (options.exit_on_forward_failure)
1209 fatal("Error: remote port forwarding failed for "
1210 "listen port %d", rfwd->listen_port);
1211 else
1212 logit("Warning: remote port forwarding failed for "
1213 "listen port %d", rfwd->listen_port);
1214 }
Darren Tucker9a2a6092008-07-04 12:53:50 +10001215 if (++remote_forward_confirms_received == options.num_remote_forwards) {
Darren Tucker9f407c42008-06-13 04:50:27 +10001216 debug("All remote forwarding requests processed");
Damien Millere11e1ea2010-08-03 16:04:46 +10001217 if (fork_after_authentication_flag)
1218 fork_postauth();
Darren Tucker9a2a6092008-07-04 12:53:50 +10001219 }
Darren Tucker9f407c42008-06-13 04:50:27 +10001220}
1221
Ben Lindstrombba81212001-06-25 05:01:22 +00001222static void
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001223client_cleanup_stdio_fwd(int id, void *arg)
1224{
1225 debug("stdio forwarding: done");
1226 cleanup_exit(0);
1227}
1228
Darren Tucker2d6665d2011-11-04 10:54:22 +11001229static void
1230ssh_init_stdio_forwarding(void)
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001231{
1232 Channel *c;
Damien Millere1537f92010-01-26 13:26:22 +11001233 int in, out;
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001234
Darren Tucker2d6665d2011-11-04 10:54:22 +11001235 if (stdio_forward_host == NULL)
1236 return;
Damien Miller386feab2013-10-15 12:14:49 +11001237 if (!compat20)
Darren Tucker2d6665d2011-11-04 10:54:22 +11001238 fatal("stdio forwarding require Protocol 2");
Damien Millere1537f92010-01-26 13:26:22 +11001239
Darren Tucker2d6665d2011-11-04 10:54:22 +11001240 debug3("%s: %s:%d", __func__, stdio_forward_host, stdio_forward_port);
1241
1242 if ((in = dup(STDIN_FILENO)) < 0 ||
1243 (out = dup(STDOUT_FILENO)) < 0)
Damien Millere1537f92010-01-26 13:26:22 +11001244 fatal("channel_connect_stdio_fwd: dup() in/out failed");
Darren Tucker2d6665d2011-11-04 10:54:22 +11001245 if ((c = channel_connect_stdio_fwd(stdio_forward_host,
1246 stdio_forward_port, in, out)) == NULL)
1247 fatal("%s: channel_connect_stdio_fwd failed", __func__);
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001248 channel_register_cleanup(c->self, client_cleanup_stdio_fwd, 0);
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001249}
1250
1251static void
Damien Miller0bc1bd82000-11-13 22:57:25 +11001252ssh_init_forwarding(void)
1253{
Kevin Steves12057502001-02-05 14:54:34 +00001254 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001255 int i;
Kevin Steves12057502001-02-05 14:54:34 +00001256
Damien Miller0bc1bd82000-11-13 22:57:25 +11001257 /* Initiate local TCP/IP port forwardings. */
1258 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001259 debug("Local connections to %.200s:%d forwarded to remote "
1260 "address %.200s:%d",
Darren Tucker47eede72005-03-14 23:08:12 +11001261 (options.local_forwards[i].listen_host == NULL) ?
1262 (options.gateway_ports ? "*" : "LOCALHOST") :
Damien Millerf91ee4c2005-03-01 21:24:33 +11001263 options.local_forwards[i].listen_host,
1264 options.local_forwards[i].listen_port,
1265 options.local_forwards[i].connect_host,
1266 options.local_forwards[i].connect_port);
Damien Millerb16461c2002-01-22 23:29:22 +11001267 success += channel_setup_local_fwd_listener(
Damien Millerf91ee4c2005-03-01 21:24:33 +11001268 options.local_forwards[i].listen_host,
1269 options.local_forwards[i].listen_port,
1270 options.local_forwards[i].connect_host,
1271 options.local_forwards[i].connect_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +11001272 options.gateway_ports);
1273 }
Darren Tuckere7d4b192006-07-12 22:17:10 +10001274 if (i > 0 && success != i && options.exit_on_forward_failure)
1275 fatal("Could not request local forwarding.");
Kevin Steves12057502001-02-05 14:54:34 +00001276 if (i > 0 && success == 0)
1277 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001278
1279 /* Initiate remote TCP/IP port forwardings. */
1280 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11001281 debug("Remote connections from %.200s:%d forwarded to "
1282 "local address %.200s:%d",
Damien Miller46d38de2005-07-17 17:02:09 +10001283 (options.remote_forwards[i].listen_host == NULL) ?
Damien Milleraa3bb102005-11-05 15:12:59 +11001284 "LOCALHOST" : options.remote_forwards[i].listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +11001285 options.remote_forwards[i].listen_port,
1286 options.remote_forwards[i].connect_host,
1287 options.remote_forwards[i].connect_port);
Darren Tucker68afb8c2011-10-02 18:59:03 +11001288 options.remote_forwards[i].handle =
1289 channel_request_remote_forwarding(
Damien Millerf91ee4c2005-03-01 21:24:33 +11001290 options.remote_forwards[i].listen_host,
1291 options.remote_forwards[i].listen_port,
1292 options.remote_forwards[i].connect_host,
Darren Tucker68afb8c2011-10-02 18:59:03 +11001293 options.remote_forwards[i].connect_port);
1294 if (options.remote_forwards[i].handle < 0) {
Darren Tuckere7d4b192006-07-12 22:17:10 +10001295 if (options.exit_on_forward_failure)
1296 fatal("Could not request remote forwarding.");
1297 else
1298 logit("Warning: Could not request remote "
1299 "forwarding.");
Darren Tucker68afb8c2011-10-02 18:59:03 +11001300 } else {
1301 client_register_global_confirm(ssh_confirm_remote_forward,
1302 &options.remote_forwards[i]);
Darren Tuckere7d4b192006-07-12 22:17:10 +10001303 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001304 }
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001305
1306 /* Initiate tunnel forwarding. */
1307 if (options.tun_open != SSH_TUNMODE_NO) {
1308 if (client_request_tun_fwd(options.tun_open,
1309 options.tun_local, options.tun_remote) == -1) {
1310 if (options.exit_on_forward_failure)
1311 fatal("Could not request tunnel forwarding.");
1312 else
1313 error("Could not request tunnel forwarding.");
1314 }
1315 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001316}
1317
Ben Lindstrombba81212001-06-25 05:01:22 +00001318static void
Damien Miller0bc1bd82000-11-13 22:57:25 +11001319check_agent_present(void)
1320{
1321 if (options.forward_agent) {
Damien Miller788f2122005-11-05 15:14:59 +11001322 /* Clear agent forwarding if we don't have an agent. */
Damien Miller789e95d2002-09-12 09:52:46 +10001323 if (!ssh_agent_present())
Damien Miller0bc1bd82000-11-13 22:57:25 +11001324 options.forward_agent = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001325 }
1326}
1327
Ben Lindstrombba81212001-06-25 05:01:22 +00001328static int
Damien Miller1383bd82000-04-06 12:32:37 +10001329ssh_session(void)
1330{
1331 int type;
Damien Miller1383bd82000-04-06 12:32:37 +10001332 int interactive = 0;
1333 int have_tty = 0;
1334 struct winsize ws;
Damien Miller1383bd82000-04-06 12:32:37 +10001335 char *cp;
Damien Miller17e7ed02005-06-17 12:54:33 +10001336 const char *display;
Damien Miller1383bd82000-04-06 12:32:37 +10001337
Damien Miller95def091999-11-25 00:26:21 +11001338 /* Enable compression if requested. */
1339 if (options.compression) {
Darren Tuckerd6173c02008-06-13 04:52:53 +10001340 debug("Requesting compression at level %d.",
1341 options.compression_level);
Damien Miller95def091999-11-25 00:26:21 +11001342
Darren Tuckerd6173c02008-06-13 04:52:53 +10001343 if (options.compression_level < 1 ||
1344 options.compression_level > 9)
1345 fatal("Compression level must be from 1 (fast) to "
1346 "9 (slow, best).");
Damien Miller95def091999-11-25 00:26:21 +11001347
1348 /* Send the request. */
1349 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
1350 packet_put_int(options.compression_level);
1351 packet_send();
1352 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +11001353 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +11001354 if (type == SSH_SMSG_SUCCESS)
1355 packet_start_compression(options.compression_level);
1356 else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +10001357 logit("Warning: Remote host refused compression.");
Damien Miller95def091999-11-25 00:26:21 +11001358 else
Darren Tuckerd6173c02008-06-13 04:52:53 +10001359 packet_disconnect("Protocol error waiting for "
1360 "compression response.");
Damien Miller95def091999-11-25 00:26:21 +11001361 }
1362 /* Allocate a pseudo tty if appropriate. */
1363 if (tty_flag) {
1364 debug("Requesting pty.");
1365
1366 /* Start the packet. */
1367 packet_start(SSH_CMSG_REQUEST_PTY);
1368
1369 /* Store TERM in the packet. There is no limit on the
1370 length of the string. */
1371 cp = getenv("TERM");
1372 if (!cp)
1373 cp = "";
Ben Lindstrom664408d2001-06-09 01:42:01 +00001374 packet_put_cstring(cp);
Damien Miller95def091999-11-25 00:26:21 +11001375
1376 /* Store window size in the packet. */
1377 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
1378 memset(&ws, 0, sizeof(ws));
Damien Miller71a73672006-03-26 14:04:36 +11001379 packet_put_int((u_int)ws.ws_row);
1380 packet_put_int((u_int)ws.ws_col);
1381 packet_put_int((u_int)ws.ws_xpixel);
1382 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller95def091999-11-25 00:26:21 +11001383
1384 /* Store tty modes in the packet. */
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001385 tty_make_modes(fileno(stdin), NULL);
Damien Miller95def091999-11-25 00:26:21 +11001386
1387 /* Send the packet, and wait for it to leave. */
1388 packet_send();
1389 packet_write_wait();
1390
1391 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +11001392 type = packet_read();
Damien Miller450a7a12000-03-26 13:04:51 +10001393 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +11001394 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001395 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +10001396 } else if (type == SSH_SMSG_FAILURE)
Darren Tuckerd6173c02008-06-13 04:52:53 +10001397 logit("Warning: Remote host failed or refused to "
1398 "allocate a pseudo tty.");
Damien Miller95def091999-11-25 00:26:21 +11001399 else
Darren Tuckerd6173c02008-06-13 04:52:53 +10001400 packet_disconnect("Protocol error waiting for pty "
1401 "request response.");
Damien Miller95def091999-11-25 00:26:21 +11001402 }
1403 /* Request X11 forwarding if enabled and DISPLAY is set. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001404 display = getenv("DISPLAY");
1405 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001406 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001407 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001408 client_x11_get_proto(display, options.xauth_location,
Damien Miller386feab2013-10-15 12:14:49 +11001409 options.forward_x11_trusted,
Damien Miller1ab6a512010-06-26 10:02:24 +10001410 options.forward_x11_timeout,
1411 &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001412 /* Request forwarding with authentication spoofing. */
Darren Tuckerd6173c02008-06-13 04:52:53 +10001413 debug("Requesting X11 forwarding with authentication "
1414 "spoofing.");
Damien Miller6d7b4372011-06-23 08:31:57 +10001415 x11_request_forwarding_with_spoofing(0, display, proto,
1416 data, 0);
Damien Miller95def091999-11-25 00:26:21 +11001417 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +11001418 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +11001419 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +11001420 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001421 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller996acd22003-04-09 20:59:48 +10001422 logit("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +10001423 } else {
Darren Tuckerd6173c02008-06-13 04:52:53 +10001424 packet_disconnect("Protocol error waiting for X11 "
1425 "forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +10001426 }
Damien Miller95def091999-11-25 00:26:21 +11001427 }
1428 /* Tell the packet module whether this is an interactive session. */
Damien Miller0dac6fb2010-11-20 15:19:38 +11001429 packet_set_interactive(interactive,
1430 options.ip_qos_interactive, options.ip_qos_bulk);
Damien Miller95def091999-11-25 00:26:21 +11001431
1432 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001433 check_agent_present();
1434
Damien Miller95def091999-11-25 00:26:21 +11001435 if (options.forward_agent) {
1436 debug("Requesting authentication agent forwarding.");
1437 auth_request_forwarding();
1438
1439 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +11001440 type = packet_read();
Damien Miller48b03fc2002-01-22 23:11:40 +11001441 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +11001442 if (type != SSH_SMSG_SUCCESS)
Damien Miller996acd22003-04-09 20:59:48 +10001443 logit("Warning: Remote host denied authentication agent forwarding.");
Damien Miller95def091999-11-25 00:26:21 +11001444 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001445
Damien Miller0bc1bd82000-11-13 22:57:25 +11001446 /* Initiate port forwardings. */
Darren Tucker2d6665d2011-11-04 10:54:22 +11001447 ssh_init_stdio_forwarding();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001448 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001449
Darren Tuckerb776c852007-12-02 23:06:35 +11001450 /* Execute a local command */
1451 if (options.local_command != NULL &&
1452 options.permit_local_command)
1453 ssh_local_cmd(options.local_command);
1454
Darren Tucker9a2a6092008-07-04 12:53:50 +10001455 /*
1456 * If requested and we are not interested in replies to remote
1457 * forwarding requests, then let ssh continue in the background.
1458 */
Damien Millere11e1ea2010-08-03 16:04:46 +10001459 if (fork_after_authentication_flag) {
1460 if (options.exit_on_forward_failure &&
1461 options.num_remote_forwards > 0) {
1462 debug("deferring postauth fork until remote forward "
1463 "confirmation received");
1464 } else
1465 fork_postauth();
Darren Tucker9a2a6092008-07-04 12:53:50 +10001466 }
Damien Miller5428f641999-11-25 11:54:57 +11001467
1468 /*
1469 * If a command was specified on the command line, execute the
1470 * command now. Otherwise request the server to start a shell.
1471 */
Damien Miller95def091999-11-25 00:26:21 +11001472 if (buffer_len(&command) > 0) {
1473 int len = buffer_len(&command);
1474 if (len > 900)
1475 len = 900;
Darren Tuckerd6173c02008-06-13 04:52:53 +10001476 debug("Sending command: %.*s", len,
1477 (u_char *)buffer_ptr(&command));
Damien Miller95def091999-11-25 00:26:21 +11001478 packet_start(SSH_CMSG_EXEC_CMD);
1479 packet_put_string(buffer_ptr(&command), buffer_len(&command));
1480 packet_send();
1481 packet_write_wait();
1482 } else {
1483 debug("Requesting shell.");
1484 packet_start(SSH_CMSG_EXEC_SHELL);
1485 packet_send();
1486 packet_write_wait();
1487 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001488
Damien Miller95def091999-11-25 00:26:21 +11001489 /* Enter the interactive session. */
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001490 return client_loop(have_tty, tty_flag ?
1491 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
Damien Miller1383bd82000-04-06 12:32:37 +10001492}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001493
Ben Lindstromf558cf62001-09-20 23:13:49 +00001494/* request pty/x11/agent/tcpfwd/shell for channel */
Ben Lindstrombba81212001-06-25 05:01:22 +00001495static void
Damien Millerd530f5f2010-05-21 14:57:10 +10001496ssh_session2_setup(int id, int success, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +10001497{
Damien Miller3756dce2004-06-18 01:17:29 +10001498 extern char **environ;
Damien Miller17e7ed02005-06-17 12:54:33 +10001499 const char *display;
Damien Miller0e220db2004-06-15 10:34:08 +10001500 int interactive = tty_flag;
Damien Miller17e7ed02005-06-17 12:54:33 +10001501
Damien Millerd530f5f2010-05-21 14:57:10 +10001502 if (!success)
1503 return; /* No need for error message, channels code sens one */
1504
Damien Miller46d38de2005-07-17 17:02:09 +10001505 display = getenv("DISPLAY");
Damien Miller17e7ed02005-06-17 12:54:33 +10001506 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001507 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001508 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001509 client_x11_get_proto(display, options.xauth_location,
Damien Miller1ab6a512010-06-26 10:02:24 +10001510 options.forward_x11_trusted,
1511 options.forward_x11_timeout, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001512 /* Request forwarding with authentication spoofing. */
Darren Tuckerd6173c02008-06-13 04:52:53 +10001513 debug("Requesting X11 forwarding with authentication "
1514 "spoofing.");
Damien Miller6d7b4372011-06-23 08:31:57 +10001515 x11_request_forwarding_with_spoofing(id, display, proto,
1516 data, 1);
1517 client_expect_confirm(id, "X11 forwarding", CONFIRM_WARN);
1518 /* XXX exit_on_forward_failure */
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001519 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001520 }
1521
Damien Miller0bc1bd82000-11-13 22:57:25 +11001522 check_agent_present();
1523 if (options.forward_agent) {
1524 debug("Requesting authentication agent forwarding.");
1525 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1526 packet_send();
1527 }
1528
Darren Tucker7b305012012-07-02 18:55:09 +10001529 /* Tell the packet module whether this is an interactive session. */
1530 packet_set_interactive(interactive,
1531 options.ip_qos_interactive, options.ip_qos_bulk);
1532
Damien Miller0e220db2004-06-15 10:34:08 +10001533 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
Damien Miller5771ed72008-05-19 15:35:33 +10001534 NULL, fileno(stdin), &command, environ);
Damien Miller1383bd82000-04-06 12:32:37 +10001535}
1536
Ben Lindstromf558cf62001-09-20 23:13:49 +00001537/* open new channel for a session */
Ben Lindstrombba81212001-06-25 05:01:22 +00001538static int
Ben Lindstromf558cf62001-09-20 23:13:49 +00001539ssh_session2_open(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001540{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001541 Channel *c;
1542 int window, packetmax, in, out, err;
Damien Millerad833b32000-08-23 10:46:23 +10001543
Damien Millercaf6dd62000-08-29 11:33:50 +11001544 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001545 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001546 } else {
1547 in = dup(STDIN_FILENO);
1548 }
1549 out = dup(STDOUT_FILENO);
1550 err = dup(STDERR_FILENO);
1551
1552 if (in < 0 || out < 0 || err < 0)
1553 fatal("dup() in/out/err failed");
1554
Damien Miller69b69aa2000-10-28 14:19:58 +11001555 /* enable nonblocking unless tty */
1556 if (!isatty(in))
1557 set_nonblock(in);
1558 if (!isatty(out))
1559 set_nonblock(out);
1560 if (!isatty(err))
1561 set_nonblock(err);
1562
Damien Millere4340be2000-09-16 13:29:08 +11001563 window = CHAN_SES_WINDOW_DEFAULT;
1564 packetmax = CHAN_SES_PACKET_DEFAULT;
Damien Miller19a59452002-02-19 15:20:57 +11001565 if (tty_flag) {
1566 window >>= 1;
1567 packetmax >>= 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001568 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001569 c = channel_new(
Damien Miller1383bd82000-04-06 12:32:37 +10001570 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001571 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001572 "client-session", /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001573
Ben Lindstromf558cf62001-09-20 23:13:49 +00001574 debug3("ssh_session2_open: channel_new: %d", c->self);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001575
Ben Lindstrom5ec26452001-06-09 00:18:51 +00001576 channel_send_open(c->self);
Ben Lindstromf558cf62001-09-20 23:13:49 +00001577 if (!no_shell_flag)
Damien Millerb84886b2008-05-19 15:05:07 +10001578 channel_register_open_confirm(c->self,
1579 ssh_session2_setup, NULL);
Damien Miller1383bd82000-04-06 12:32:37 +10001580
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001581 return c->self;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001582}
1583
Ben Lindstrombba81212001-06-25 05:01:22 +00001584static int
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001585ssh_session2(void)
1586{
Ben Lindstromf558cf62001-09-20 23:13:49 +00001587 int id = -1;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001588
1589 /* XXX should be pre-session */
Darren Tucker2d6665d2011-11-04 10:54:22 +11001590 if (!options.control_persist)
1591 ssh_init_stdio_forwarding();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001592 ssh_init_forwarding();
1593
Damien Millere11e1ea2010-08-03 16:04:46 +10001594 /* Start listening for multiplex clients */
1595 muxserver_listen();
1596
1597 /*
Darren Tucker2d6665d2011-11-04 10:54:22 +11001598 * If we are in control persist mode and have a working mux listen
1599 * socket, then prepare to background ourselves and have a foreground
1600 * client attach as a control slave.
1601 * NB. we must save copies of the flags that we override for
Damien Millere11e1ea2010-08-03 16:04:46 +10001602 * the backgrounding, since we defer attachment of the slave until
1603 * after the connection is fully established (in particular,
1604 * async rfwd replies have been received for ExitOnForwardFailure).
1605 */
1606 if (options.control_persist && muxserver_sock != -1) {
1607 ostdin_null_flag = stdin_null_flag;
1608 ono_shell_flag = no_shell_flag;
Damien Miller21771e22011-05-15 08:45:50 +10001609 orequest_tty = options.request_tty;
Damien Millere11e1ea2010-08-03 16:04:46 +10001610 otty_flag = tty_flag;
1611 stdin_null_flag = 1;
1612 no_shell_flag = 1;
Damien Millere11e1ea2010-08-03 16:04:46 +10001613 tty_flag = 0;
1614 if (!fork_after_authentication_flag)
1615 need_controlpersist_detach = 1;
1616 fork_after_authentication_flag = 1;
1617 }
Darren Tucker2d6665d2011-11-04 10:54:22 +11001618 /*
1619 * ControlPersist mux listen socket setup failed, attempt the
1620 * stdio forward setup that we skipped earlier.
1621 */
1622 if (options.control_persist && muxserver_sock == -1)
1623 ssh_init_stdio_forwarding();
Damien Millere11e1ea2010-08-03 16:04:46 +10001624
Ben Lindstromf558cf62001-09-20 23:13:49 +00001625 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1626 id = ssh_session2_open();
Damien Miller649fe022013-07-18 16:13:55 +10001627 else {
1628 packet_set_interactive(
1629 options.control_master == SSHCTL_MASTER_NO,
1630 options.ip_qos_interactive, options.ip_qos_bulk);
1631 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001632
Darren Tucker8901fa92008-06-11 09:34:01 +10001633 /* If we don't expect to open a new session, then disallow it */
Damien Miller456e6f02008-11-03 19:20:10 +11001634 if (options.control_master == SSHCTL_MASTER_NO &&
1635 (datafellows & SSH_NEW_OPENSSH)) {
Darren Tucker8901fa92008-06-11 09:34:01 +10001636 debug("Requesting no-more-sessions@openssh.com");
1637 packet_start(SSH2_MSG_GLOBAL_REQUEST);
1638 packet_put_cstring("no-more-sessions@openssh.com");
1639 packet_put_char(0);
1640 packet_send();
1641 }
1642
Damien Millerd27b9472005-12-13 19:29:02 +11001643 /* Execute a local command */
1644 if (options.local_command != NULL &&
1645 options.permit_local_command)
1646 ssh_local_cmd(options.local_command);
1647
Damien Miller1f25ab42010-07-16 13:56:23 +10001648 /*
1649 * If requested and we are not interested in replies to remote
1650 * forwarding requests, then let ssh continue in the background.
1651 */
Damien Millere11e1ea2010-08-03 16:04:46 +10001652 if (fork_after_authentication_flag) {
1653 if (options.exit_on_forward_failure &&
1654 options.num_remote_forwards > 0) {
1655 debug("deferring postauth fork until remote forward "
1656 "confirmation received");
1657 } else
1658 fork_postauth();
Darren Tucker9a2a6092008-07-04 12:53:50 +10001659 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001660
Darren Tuckerf1de4e52010-01-08 16:54:59 +11001661 if (options.use_roaming)
1662 request_roaming();
1663
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001664 return client_loop(tty_flag, tty_flag ?
1665 options.escape_char : SSH_ESCAPECHAR_NONE, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001666}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001667
Ben Lindstrombba81212001-06-25 05:01:22 +00001668static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001669load_public_identity_files(void)
1670{
Damien Miller6b1d53c2006-03-31 23:13:21 +11001671 char *filename, *cp, thishost[NI_MAXHOST];
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001672 char *pwdir = NULL, *pwname = NULL;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001673 int i = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001674 Key *public;
Damien Miller6b1d53c2006-03-31 23:13:21 +11001675 struct passwd *pw;
Damien Miller0a80ca12010-02-27 07:55:05 +11001676 u_int n_ids;
1677 char *identity_files[SSH_MAX_IDENTITY_FILES];
1678 Key *identity_keys[SSH_MAX_IDENTITY_FILES];
Damien Miller7ea845e2010-02-12 09:21:02 +11001679#ifdef ENABLE_PKCS11
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001680 Key **keys;
Damien Miller7ea845e2010-02-12 09:21:02 +11001681 int nkeys;
Damien Miller0a80ca12010-02-27 07:55:05 +11001682#endif /* PKCS11 */
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001683
Damien Miller0a80ca12010-02-27 07:55:05 +11001684 n_ids = 0;
1685 bzero(identity_files, sizeof(identity_files));
1686 bzero(identity_keys, sizeof(identity_keys));
1687
1688#ifdef ENABLE_PKCS11
Damien Miller7ea845e2010-02-12 09:21:02 +11001689 if (options.pkcs11_provider != NULL &&
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001690 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
Damien Miller7ea845e2010-02-12 09:21:02 +11001691 (pkcs11_init(!options.batch_mode) == 0) &&
1692 (nkeys = pkcs11_add_provider(options.pkcs11_provider, NULL,
1693 &keys)) > 0) {
Damien Miller7ea845e2010-02-12 09:21:02 +11001694 for (i = 0; i < nkeys; i++) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001695 if (n_ids >= SSH_MAX_IDENTITY_FILES) {
1696 key_free(keys[i]);
1697 continue;
1698 }
1699 identity_keys[n_ids] = keys[i];
1700 identity_files[n_ids] =
Damien Miller7ea845e2010-02-12 09:21:02 +11001701 xstrdup(options.pkcs11_provider); /* XXX */
Damien Miller0a80ca12010-02-27 07:55:05 +11001702 n_ids++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001703 }
Darren Tuckera627d422013-06-02 07:31:17 +10001704 free(keys);
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001705 }
Damien Miller7ea845e2010-02-12 09:21:02 +11001706#endif /* ENABLE_PKCS11 */
Damien Miller6b1d53c2006-03-31 23:13:21 +11001707 if ((pw = getpwuid(original_real_uid)) == NULL)
1708 fatal("load_public_identity_files: getpwuid failed");
Darren Tuckere143f062007-12-02 23:21:16 +11001709 pwname = xstrdup(pw->pw_name);
1710 pwdir = xstrdup(pw->pw_dir);
Damien Miller6b1d53c2006-03-31 23:13:21 +11001711 if (gethostname(thishost, sizeof(thishost)) == -1)
1712 fatal("load_public_identity_files: gethostname: %s",
1713 strerror(errno));
Damien Miller0a80ca12010-02-27 07:55:05 +11001714 for (i = 0; i < options.num_identity_files; i++) {
Darren Tucker15fd19c2013-04-05 11:22:26 +11001715 if (n_ids >= SSH_MAX_IDENTITY_FILES ||
1716 strcasecmp(options.identity_files[i], "none") == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +10001717 free(options.identity_files[i]);
Damien Miller0a80ca12010-02-27 07:55:05 +11001718 continue;
1719 }
Damien Miller6b1d53c2006-03-31 23:13:21 +11001720 cp = tilde_expand_filename(options.identity_files[i],
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001721 original_real_uid);
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001722 filename = percent_expand(cp, "d", pwdir,
1723 "u", pwname, "l", thishost, "h", host,
Damien Miller6b1d53c2006-03-31 23:13:21 +11001724 "r", options.user, (char *)NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001725 free(cp);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001726 public = key_load_public(filename, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001727 debug("identity file %s type %d", filename,
1728 public ? public->type : -1);
Darren Tuckera627d422013-06-02 07:31:17 +10001729 free(options.identity_files[i]);
Damien Miller0a80ca12010-02-27 07:55:05 +11001730 identity_files[n_ids] = filename;
1731 identity_keys[n_ids] = public;
1732
1733 if (++n_ids >= SSH_MAX_IDENTITY_FILES)
1734 continue;
1735
1736 /* Try to add the certificate variant too */
1737 xasprintf(&cp, "%s-cert", filename);
1738 public = key_load_public(cp, NULL);
1739 debug("identity file %s type %d", cp,
1740 public ? public->type : -1);
1741 if (public == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +10001742 free(cp);
Damien Miller0a80ca12010-02-27 07:55:05 +11001743 continue;
1744 }
1745 if (!key_is_cert(public)) {
1746 debug("%s: key %s type %s is not a certificate",
1747 __func__, cp, key_type(public));
1748 key_free(public);
Darren Tuckera627d422013-06-02 07:31:17 +10001749 free(cp);
Damien Miller0a80ca12010-02-27 07:55:05 +11001750 continue;
1751 }
1752 identity_keys[n_ids] = public;
1753 /* point to the original path, most likely the private key */
1754 identity_files[n_ids] = xstrdup(filename);
1755 n_ids++;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001756 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001757 options.num_identity_files = n_ids;
1758 memcpy(options.identity_files, identity_files, sizeof(identity_files));
1759 memcpy(options.identity_keys, identity_keys, sizeof(identity_keys));
1760
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001761 bzero(pwname, strlen(pwname));
Darren Tuckera627d422013-06-02 07:31:17 +10001762 free(pwname);
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001763 bzero(pwdir, strlen(pwdir));
Darren Tuckera627d422013-06-02 07:31:17 +10001764 free(pwdir);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001765}
Damien Miller857b02e2010-09-24 22:02:56 +10001766
1767static void
1768main_sigchld_handler(int sig)
1769{
1770 int save_errno = errno;
1771 pid_t pid;
1772 int status;
1773
1774 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
1775 (pid < 0 && errno == EINTR))
1776 ;
1777
1778 signal(sig, main_sigchld_handler);
1779 errno = save_errno;
1780}