blob: 00995e5dc84a8ce08d6bb1c2af13b36883d31fff [file] [log] [blame]
Damien Miller57c30112006-03-26 14:24:48 +11001/* $OpenBSD: ssh.c,v 1.273 2006/03/25 13:17:02 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Ssh client program. This program can be used to log into a remote machine.
7 * The software supports strong authentication, encryption, and forwarding
8 * of X11, TCP/IP, and authentication connections.
9 *
Damien Millere4340be2000-09-16 13:29:08 +110010 * As far as I am concerned, the code I have written for this software
11 * can be used freely for any purpose. Any derived versions of this
12 * software must be clearly marked as such, and if the derived work is
13 * incompatible with the protocol description in the RFC file, it must be
14 * called by a name other than "ssh" or "Secure Shell".
15 *
16 * Copyright (c) 1999 Niels Provos. All rights reserved.
Darren Tucker0a118da2003-10-15 15:54:32 +100017 * Copyright (c) 2000, 2001, 2002, 2003 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110018 *
19 * Modified to work with SSL by Niels Provos <provos@citi.umich.edu>
20 * in Canada (German citizen).
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110041 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100042
43#include "includes.h"
Damien Millercd4223c2006-03-15 11:22:47 +110044
Damien Millerf17883e2006-03-15 11:45:54 +110045#include <sys/types.h>
46#ifdef HAVE_SYS_STAT_H
47# include <sys/stat.h>
48#endif
Damien Millercd4223c2006-03-15 11:22:47 +110049#include <sys/resource.h>
Damien Miller17e91c02006-03-15 11:28:34 +110050#include <sys/ioctl.h>
Damien Miller574c41f2006-03-15 11:40:10 +110051#include <sys/un.h>
Damien Miller03e20032006-03-15 11:16:59 +110052
Damien Millerc7b06362006-03-15 11:53:45 +110053#include <ctype.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110054#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110055#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110056#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110057#include <signal.h>
Damien Millereba71ba2000-04-29 23:57:08 +100058
59#include <openssl/evp.h>
Damien Miller0bc1bd82000-11-13 22:57:25 +110060#include <openssl/err.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063#include "ssh1.h"
Damien Miller1383bd82000-04-06 12:32:37 +100064#include "ssh2.h"
65#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000066#include "cipher.h"
67#include "xmalloc.h"
68#include "packet.h"
69#include "buffer.h"
Damien Miller0e220db2004-06-15 10:34:08 +100070#include "bufaux.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000071#include "channels.h"
Damien Millereba71ba2000-04-29 23:57:08 +100072#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100073#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100074#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000075#include "pathnames.h"
Damien Miller0e220db2004-06-15 10:34:08 +100076#include "dispatch.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +000077#include "clientloop.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000078#include "log.h"
79#include "readconf.h"
80#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000081#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000082#include "kex.h"
83#include "mac.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +100084#include "sshpty.h"
Darren Tucker46bc0752004-05-02 22:11:30 +100085#include "match.h"
Damien Miller0e220db2004-06-15 10:34:08 +100086#include "msg.h"
87#include "monitor_fdpass.h"
Darren Tucker25f60a72004-08-15 17:23:34 +100088#include "uidswap.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089
Ben Lindstromc5b68002001-07-04 04:52:03 +000090#ifdef SMARTCARD
Ben Lindstromc5b68002001-07-04 04:52:03 +000091#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000092#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +000093
Damien Miller3f905871999-11-15 17:10:57 +110094extern char *__progname;
Damien Miller3f905871999-11-15 17:10:57 +110095
Damien Miller95def091999-11-25 00:26:21 +110096/* Flag indicating whether debug mode is on. This can be set on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097int debug_flag = 0;
98
Damien Miller78928792000-04-12 20:17:38 +100099/* Flag indicating whether a tty should be allocated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100int tty_flag = 0;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000101int no_tty_flag = 0;
102int force_tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103
Damien Miller1383bd82000-04-06 12:32:37 +1000104/* don't exec a shell */
105int no_shell_flag = 0;
Damien Miller1383bd82000-04-06 12:32:37 +1000106
Damien Miller5428f641999-11-25 11:54:57 +1100107/*
108 * Flag indicating that nothing should be read from stdin. This can be set
109 * on the command line.
110 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111int stdin_null_flag = 0;
112
Damien Miller5428f641999-11-25 11:54:57 +1100113/*
114 * Flag indicating that ssh should fork after authentication. This is useful
Ben Lindstromf666fec2002-06-06 19:51:58 +0000115 * so that the passphrase can be entered manually, and then ssh goes to the
Damien Miller5428f641999-11-25 11:54:57 +1100116 * background.
117 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118int fork_after_authentication_flag = 0;
119
Damien Miller5428f641999-11-25 11:54:57 +1100120/*
121 * General data structure for command line options and options configurable
122 * in configuration files. See readconf.h.
123 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124Options options;
125
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000126/* optional user configfile */
127char *config = NULL;
128
Damien Miller5428f641999-11-25 11:54:57 +1100129/*
130 * Name of the host we are connecting to. This is the name given on the
131 * command line, or the HostName specified for the user-supplied name in a
132 * configuration file.
133 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134char *host;
135
136/* socket address the host resolves to */
Damien Miller34132e52000-01-14 15:45:46 +1100137struct sockaddr_storage hostaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000139/* Private host keys. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000140Sensitive sensitive_data;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141
142/* Original real UID. */
143uid_t original_real_uid;
Ben Lindstromf9c48842002-06-11 16:37:51 +0000144uid_t original_effective_uid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000145
Damien Miller1383bd82000-04-06 12:32:37 +1000146/* command to be executed */
147Buffer command;
148
Damien Miller832562e2001-01-30 09:30:01 +1100149/* Should we execute a command or invoke a subsystem? */
150int subsystem_flag = 0;
151
Damien Miller2797f7f2002-04-23 21:09:44 +1000152/* # of replies received for global requests */
153static int client_global_request_id = 0;
154
Damien Miller8c4e18a2002-09-19 12:05:02 +1000155/* pid of proxycommand child process */
156pid_t proxy_command_pid = 0;
157
Damien Miller0e220db2004-06-15 10:34:08 +1000158/* fd to control socket */
159int control_fd = -1;
160
Darren Tucker7ebfc102004-11-07 20:06:19 +1100161/* Multiplexing control command */
Darren Tucker0814d312005-06-01 23:08:51 +1000162static u_int mux_command = 0;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100163
Damien Miller0e220db2004-06-15 10:34:08 +1000164/* Only used in control client mode */
165volatile sig_atomic_t control_client_terminate = 0;
166u_int control_server_pid = 0;
167
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168/* Prints a help message to the user. This function never returns. */
169
Ben Lindstrombba81212001-06-25 05:01:22 +0000170static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000171usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000172{
Damien Miller50955102004-03-22 09:34:58 +1100173 fprintf(stderr,
Darren Tucker9c6bf322004-12-03 14:10:19 +1100174"usage: ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n"
Darren Tucker895d6982005-10-03 18:18:05 +1000175" [-D [bind_address:]port] [-e escape_char] [-F configfile]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100176" [-i identity_file] [-L [bind_address:]port:host:hostport]\n"
Damien Millerf91ee4c2005-03-01 21:24:33 +1100177" [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100178" [-R [bind_address:]port:host:hostport] [-S ctl_path]\n"
Damien Millerd27b9472005-12-13 19:29:02 +1100179" [-w tunnel:tunnel] [user@]hostname [command]\n"
Damien Miller50955102004-03-22 09:34:58 +1100180 );
Darren Tuckere9a9b712005-12-20 16:15:51 +1100181 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000182}
183
Ben Lindstrombba81212001-06-25 05:01:22 +0000184static int ssh_session(void);
185static int ssh_session2(void);
186static void load_public_identity_files(void);
Damien Miller0e220db2004-06-15 10:34:08 +1000187static void control_client(const char *path);
Damien Miller1383bd82000-04-06 12:32:37 +1000188
Damien Miller95def091999-11-25 00:26:21 +1100189/*
190 * Main program for the ssh client.
191 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000192int
193main(int ac, char **av)
194{
Ben Lindstrom24157572002-06-12 16:09:39 +0000195 int i, opt, exit_status;
Damien Miller9836cf82003-12-17 16:30:06 +1100196 char *p, *cp, *line, buf[256];
Damien Miller95def091999-11-25 00:26:21 +1100197 struct stat st;
Ben Lindstrom086cf212001-03-05 05:56:40 +0000198 struct passwd *pw;
Damien Miller1383bd82000-04-06 12:32:37 +1000199 int dummy;
Ben Lindstrom5ccf63a2001-09-24 20:00:10 +0000200 extern int optind, optreset;
Damien Miller4f8e6692001-07-14 13:22:53 +1000201 extern char *optarg;
Damien Miller9651fe62005-06-26 08:55:25 +1000202 struct servent *sp;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100203 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000204
Darren Tuckerce321d82005-10-03 18:11:24 +1000205 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
206 sanitise_stdfd();
207
Damien Miller59d3d5b2003-08-22 09:34:41 +1000208 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000209 init_rng();
210
Damien Miller5428f641999-11-25 11:54:57 +1100211 /*
212 * Save the original real uid. It will be needed later (uid-swapping
213 * may clobber the real uid).
214 */
Damien Miller95def091999-11-25 00:26:21 +1100215 original_real_uid = getuid();
216 original_effective_uid = geteuid();
Damien Millera8e06ce2003-11-21 23:48:55 +1100217
Damien Miller50b9a602002-09-04 16:50:06 +1000218 /*
219 * Use uid-swapping to give up root privileges for the duration of
220 * option processing. We will re-instantiate the rights when we are
221 * ready to create the privileged port, and will permanently drop
222 * them when the port has been created (actually, when the connection
223 * has been made, as we may need to create the port several times).
224 */
225 PRIV_END;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000226
Damien Miller05dd7952000-10-01 00:42:48 +1100227#ifdef HAVE_SETRLIMIT
Damien Miller95def091999-11-25 00:26:21 +1100228 /* If we are installed setuid root be careful to not drop core. */
229 if (original_real_uid != original_effective_uid) {
230 struct rlimit rlim;
231 rlim.rlim_cur = rlim.rlim_max = 0;
232 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
233 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000234 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100235#endif
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000236 /* Get user data. */
237 pw = getpwuid(original_real_uid);
238 if (!pw) {
Damien Miller996acd22003-04-09 20:59:48 +1000239 logit("You don't exist, go away!");
Darren Tuckere9a9b712005-12-20 16:15:51 +1100240 exit(255);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000241 }
242 /* Take a copy of the returned structure. */
243 pw = pwcopy(pw);
244
Damien Miller5428f641999-11-25 11:54:57 +1100245 /*
Damien Miller5428f641999-11-25 11:54:57 +1100246 * Set our umask to something reasonable, as some files are created
247 * with the default umask. This will make them world-readable but
248 * writable only by the owner, which is ok for all files for which we
249 * don't set the modes explicitly.
250 */
Damien Miller95def091999-11-25 00:26:21 +1100251 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252
Damien Miller95def091999-11-25 00:26:21 +1100253 /* Initialize option structure to indicate that no values have been set. */
254 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000255
Damien Miller95def091999-11-25 00:26:21 +1100256 /* Parse command-line arguments. */
257 host = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000258
Damien Miller2ecb6bd2006-03-15 12:03:53 +1100259 again:
Damien Millerf4614452001-07-14 12:18:10 +1000260 while ((opt = getopt(ac, av,
Damien Millerd27b9472005-12-13 19:29:02 +1100261 "1246ab:c:e:fgi:kl:m:no:p:qstvxACD:F:I:L:MNO:PR:S:TVw:XY")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100262 switch (opt) {
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000263 case '1':
264 options.protocol = SSH_PROTO_1;
265 break;
Damien Miller4af51302000-04-16 11:18:38 +1000266 case '2':
267 options.protocol = SSH_PROTO_2;
268 break;
Damien Miller34132e52000-01-14 15:45:46 +1100269 case '4':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000270 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100271 break;
Damien Miller34132e52000-01-14 15:45:46 +1100272 case '6':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000273 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100274 break;
Damien Miller95def091999-11-25 00:26:21 +1100275 case 'n':
276 stdin_null_flag = 1;
277 break;
Damien Miller95def091999-11-25 00:26:21 +1100278 case 'f':
279 fork_after_authentication_flag = 1;
280 stdin_null_flag = 1;
281 break;
Damien Miller95def091999-11-25 00:26:21 +1100282 case 'x':
283 options.forward_x11 = 0;
284 break;
Damien Miller95def091999-11-25 00:26:21 +1100285 case 'X':
286 options.forward_x11 = 1;
287 break;
Darren Tucker0a118da2003-10-15 15:54:32 +1000288 case 'Y':
289 options.forward_x11 = 1;
290 options.forward_x11_trusted = 1;
291 break;
Damien Miller95def091999-11-25 00:26:21 +1100292 case 'g':
293 options.gateway_ports = 1;
294 break;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100295 case 'O':
296 if (strcmp(optarg, "check") == 0)
297 mux_command = SSHMUX_COMMAND_ALIVE_CHECK;
298 else if (strcmp(optarg, "exit") == 0)
299 mux_command = SSHMUX_COMMAND_TERMINATE;
300 else
301 fatal("Invalid multiplex command.");
302 break;
Damien Miller147bba32002-09-04 16:46:06 +1000303 case 'P': /* deprecated */
Damien Miller95def091999-11-25 00:26:21 +1100304 options.use_privileged_port = 0;
305 break;
Damien Miller95def091999-11-25 00:26:21 +1100306 case 'a':
307 options.forward_agent = 0;
308 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000309 case 'A':
310 options.forward_agent = 1;
311 break;
Damien Miller95def091999-11-25 00:26:21 +1100312 case 'k':
Damien Millere0113cc2003-11-24 13:10:09 +1100313 options.gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100314 break;
Damien Miller95def091999-11-25 00:26:21 +1100315 case 'i':
316 if (stat(optarg, &st) < 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000317 fprintf(stderr, "Warning: Identity file %s "
Damien Miller3eb48b62005-03-01 21:15:46 +1100318 "not accessible: %s.\n", optarg,
319 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100320 break;
321 }
Damien Millerf4614452001-07-14 12:18:10 +1000322 if (options.num_identity_files >=
323 SSH_MAX_IDENTITY_FILES)
324 fatal("Too many identity files specified "
325 "(max %d)", SSH_MAX_IDENTITY_FILES);
326 options.identity_files[options.num_identity_files++] =
327 xstrdup(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100328 break;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000329 case 'I':
330#ifdef SMARTCARD
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000331 options.smartcard_device = xstrdup(optarg);
Ben Lindstrombcc18082001-08-06 21:59:25 +0000332#else
Ben Lindstromc5b68002001-07-04 04:52:03 +0000333 fprintf(stderr, "no support for smartcards.\n");
Ben Lindstrombcc18082001-08-06 21:59:25 +0000334#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000335 break;
Damien Miller95def091999-11-25 00:26:21 +1100336 case 't':
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000337 if (tty_flag)
338 force_tty_flag = 1;
Damien Miller95def091999-11-25 00:26:21 +1100339 tty_flag = 1;
340 break;
Damien Miller95def091999-11-25 00:26:21 +1100341 case 'v':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000342 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100343 debug_flag = 1;
344 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000345 } else {
346 if (options.log_level < SYSLOG_LEVEL_DEBUG3)
347 options.log_level++;
Damien Millere4340be2000-09-16 13:29:08 +1100348 break;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000349 }
Darren Tuckere9bf9842004-11-05 20:05:32 +1100350 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100351 case 'V':
Damien Miller0c889cd2004-03-22 09:36:00 +1100352 fprintf(stderr, "%s, %s\n",
Damien Miller2aa6d3c2004-09-12 16:53:04 +1000353 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
Damien Miller95def091999-11-25 00:26:21 +1100354 if (opt == 'V')
355 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100356 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100357 case 'w':
Damien Miller7b58e802005-12-13 19:33:19 +1100358 if (options.tun_open == -1)
359 options.tun_open = SSH_TUNMODE_DEFAULT;
Damien Millerd27b9472005-12-13 19:29:02 +1100360 options.tun_local = a2tun(optarg, &options.tun_remote);
Damien Miller7b58e802005-12-13 19:33:19 +1100361 if (options.tun_local == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100362 fprintf(stderr, "Bad tun device '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100363 exit(255);
Damien Millerd27b9472005-12-13 19:29:02 +1100364 }
365 break;
Damien Miller95def091999-11-25 00:26:21 +1100366 case 'q':
367 options.log_level = SYSLOG_LEVEL_QUIET;
368 break;
Damien Miller95def091999-11-25 00:26:21 +1100369 case 'e':
370 if (optarg[0] == '^' && optarg[2] == 0 &&
Damien Millerf4614452001-07-14 12:18:10 +1000371 (u_char) optarg[1] >= 64 &&
372 (u_char) optarg[1] < 128)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000373 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100374 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000375 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100376 else if (strcmp(optarg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000377 options.escape_char = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100378 else {
Damien Millerf4614452001-07-14 12:18:10 +1000379 fprintf(stderr, "Bad escape character '%s'.\n",
380 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100381 exit(255);
Damien Miller95def091999-11-25 00:26:21 +1100382 }
383 break;
Damien Miller95def091999-11-25 00:26:21 +1100384 case 'c':
Damien Millereba71ba2000-04-29 23:57:08 +1000385 if (ciphers_valid(optarg)) {
386 /* SSH2 only */
387 options.ciphers = xstrdup(optarg);
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000388 options.cipher = SSH_CIPHER_INVALID;
Damien Millereba71ba2000-04-29 23:57:08 +1000389 } else {
390 /* SSH1 only */
Damien Millere39cacc2000-11-29 12:18:44 +1100391 options.cipher = cipher_number(optarg);
392 if (options.cipher == -1) {
Damien Millerf4614452001-07-14 12:18:10 +1000393 fprintf(stderr,
394 "Unknown cipher type '%s'\n",
395 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100396 exit(255);
Damien Millereba71ba2000-04-29 23:57:08 +1000397 }
Damien Millerf4614452001-07-14 12:18:10 +1000398 if (options.cipher == SSH_CIPHER_3DES)
Damien Millere39cacc2000-11-29 12:18:44 +1100399 options.ciphers = "3des-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000400 else if (options.cipher == SSH_CIPHER_BLOWFISH)
Damien Millere39cacc2000-11-29 12:18:44 +1100401 options.ciphers = "blowfish-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000402 else
Damien Millere39cacc2000-11-29 12:18:44 +1100403 options.ciphers = (char *)-1;
Damien Miller95def091999-11-25 00:26:21 +1100404 }
405 break;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000406 case 'm':
407 if (mac_valid(optarg))
408 options.macs = xstrdup(optarg);
409 else {
Damien Millerf4614452001-07-14 12:18:10 +1000410 fprintf(stderr, "Unknown mac type '%s'\n",
411 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100412 exit(255);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000413 }
414 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000415 case 'M':
Damien Millerd14b1e72005-06-16 13:19:41 +1000416 if (options.control_master == SSHCTL_MASTER_YES)
417 options.control_master = SSHCTL_MASTER_ASK;
418 else
419 options.control_master = SSHCTL_MASTER_YES;
Damien Miller0e220db2004-06-15 10:34:08 +1000420 break;
Damien Miller95def091999-11-25 00:26:21 +1100421 case 'p':
Ben Lindstrom19066a12001-04-12 23:39:26 +0000422 options.port = a2port(optarg);
423 if (options.port == 0) {
Ben Lindstrom146edb92001-04-11 23:06:28 +0000424 fprintf(stderr, "Bad port '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100425 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000426 }
Damien Miller95def091999-11-25 00:26:21 +1100427 break;
Damien Miller95def091999-11-25 00:26:21 +1100428 case 'l':
429 options.user = optarg;
430 break;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000431
Damien Miller95def091999-11-25 00:26:21 +1100432 case 'L':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100433 if (parse_forward(&fwd, optarg))
434 add_local_forward(&options, &fwd);
435 else {
Damien Millerf4614452001-07-14 12:18:10 +1000436 fprintf(stderr,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100437 "Bad local forwarding specification '%s'\n",
Damien Millerf4614452001-07-14 12:18:10 +1000438 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100439 exit(255);
Ben Lindstrom1a174712001-09-12 17:56:15 +0000440 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100441 break;
442
443 case 'R':
444 if (parse_forward(&fwd, optarg)) {
445 add_remote_forward(&options, &fwd);
446 } else {
447 fprintf(stderr,
448 "Bad remote forwarding specification "
449 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100450 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100451 }
Damien Miller95def091999-11-25 00:26:21 +1100452 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000453
454 case 'D':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100455 cp = p = xstrdup(optarg);
456 memset(&fwd, '\0', sizeof(fwd));
457 fwd.connect_host = "socks";
458 if ((fwd.listen_host = hpdelim(&cp)) == NULL) {
459 fprintf(stderr, "Bad dynamic forwarding "
460 "specification '%.100s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100461 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100462 }
463 if (cp != NULL) {
464 fwd.listen_port = a2port(cp);
465 fwd.listen_host = cleanhostname(fwd.listen_host);
466 } else {
467 fwd.listen_port = a2port(fwd.listen_host);
Damien Millerbe1045d2005-08-12 22:10:56 +1000468 fwd.listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100469 }
470
471 if (fwd.listen_port == 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000472 fprintf(stderr, "Bad dynamic port '%s'\n",
473 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100474 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000475 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100476 add_local_forward(&options, &fwd);
477 xfree(p);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000478 break;
479
Damien Miller95def091999-11-25 00:26:21 +1100480 case 'C':
481 options.compression = 1;
482 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000483 case 'N':
484 no_shell_flag = 1;
485 no_tty_flag = 1;
486 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000487 case 'T':
488 no_tty_flag = 1;
489 break;
Damien Miller95def091999-11-25 00:26:21 +1100490 case 'o':
491 dummy = 1;
Damien Miller9836cf82003-12-17 16:30:06 +1100492 line = xstrdup(optarg);
Damien Millerf4614452001-07-14 12:18:10 +1000493 if (process_config_line(&options, host ? host : "",
Damien Miller9836cf82003-12-17 16:30:06 +1100494 line, "command-line", 0, &dummy) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100495 exit(255);
Damien Miller9836cf82003-12-17 16:30:06 +1100496 xfree(line);
Damien Miller95def091999-11-25 00:26:21 +1100497 break;
Damien Miller832562e2001-01-30 09:30:01 +1100498 case 's':
499 subsystem_flag = 1;
500 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000501 case 'S':
502 if (options.control_path != NULL)
503 free(options.control_path);
504 options.control_path = xstrdup(optarg);
Damien Miller0e220db2004-06-15 10:34:08 +1000505 break;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000506 case 'b':
507 options.bind_address = optarg;
508 break;
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000509 case 'F':
510 config = optarg;
511 break;
Damien Miller95def091999-11-25 00:26:21 +1100512 default:
513 usage();
514 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000515 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000516
Damien Millerf4614452001-07-14 12:18:10 +1000517 ac -= optind;
518 av += optind;
519
520 if (ac > 0 && !host && **av != '-') {
Ben Lindstromc276c122002-12-23 02:14:51 +0000521 if (strrchr(*av, '@')) {
Damien Millerf4614452001-07-14 12:18:10 +1000522 p = xstrdup(*av);
Ben Lindstromc276c122002-12-23 02:14:51 +0000523 cp = strrchr(p, '@');
Damien Millerf4614452001-07-14 12:18:10 +1000524 if (cp == NULL || cp == p)
525 usage();
526 options.user = p;
527 *cp = '\0';
528 host = ++cp;
529 } else
530 host = *av;
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000531 if (ac > 1) {
532 optind = optreset = 1;
Damien Millerf4614452001-07-14 12:18:10 +1000533 goto again;
534 }
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000535 ac--, av++;
Damien Millerf4614452001-07-14 12:18:10 +1000536 }
537
Damien Miller95def091999-11-25 00:26:21 +1100538 /* Check that we got a host name. */
539 if (!host)
540 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000541
Damien Millercb5e44a2000-09-29 12:12:36 +1100542 SSLeay_add_all_algorithms();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100543 ERR_load_crypto_strings();
Damien Millercb5e44a2000-09-29 12:12:36 +1100544
Damien Miller95def091999-11-25 00:26:21 +1100545 /* Initialize the command to execute on remote host. */
546 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000547
Damien Miller5428f641999-11-25 11:54:57 +1100548 /*
549 * Save the command to execute on the remote host in a buffer. There
550 * is no limit on the length of the command, except by the maximum
551 * packet size. Also sets the tty flag if there is no command.
552 */
Damien Millerf4614452001-07-14 12:18:10 +1000553 if (!ac) {
Damien Miller95def091999-11-25 00:26:21 +1100554 /* No command specified - execute shell on a tty. */
555 tty_flag = 1;
Damien Miller832562e2001-01-30 09:30:01 +1100556 if (subsystem_flag) {
Damien Millerf4614452001-07-14 12:18:10 +1000557 fprintf(stderr,
558 "You must specify a subsystem to invoke.\n");
Damien Miller832562e2001-01-30 09:30:01 +1100559 usage();
560 }
Damien Miller95def091999-11-25 00:26:21 +1100561 } else {
Damien Millerf4614452001-07-14 12:18:10 +1000562 /* A command has been specified. Store it into the buffer. */
563 for (i = 0; i < ac; i++) {
564 if (i)
Damien Miller95def091999-11-25 00:26:21 +1100565 buffer_append(&command, " ", 1);
566 buffer_append(&command, av[i], strlen(av[i]));
567 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000568 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000569
Damien Miller95def091999-11-25 00:26:21 +1100570 /* Cannot fork to background if no command. */
Damien Millercaf6dd62000-08-29 11:33:50 +1100571 if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
Damien Miller95def091999-11-25 00:26:21 +1100572 fatal("Cannot fork into background without a command to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000573
Damien Miller95def091999-11-25 00:26:21 +1100574 /* Allocate a tty by default if no command specified. */
575 if (buffer_len(&command) == 0)
576 tty_flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000577
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000578 /* Force no tty */
Ben Lindstromc72745a2000-12-02 19:03:54 +0000579 if (no_tty_flag)
580 tty_flag = 0;
Damien Miller95def091999-11-25 00:26:21 +1100581 /* Do not allocate a tty if stdin is not a tty. */
Damien Millerddee5752005-05-26 12:05:05 +1000582 if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100583 if (tty_flag)
Damien Miller996acd22003-04-09 20:59:48 +1000584 logit("Pseudo-terminal will not be allocated because stdin is not a terminal.");
Damien Miller95def091999-11-25 00:26:21 +1100585 tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000586 }
Damien Miller1383bd82000-04-06 12:32:37 +1000587
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000588 /*
589 * Initialize "log" output. Since we are the client all output
590 * actually goes to stderr.
591 */
Ben Lindstrom2b646522001-04-12 16:16:57 +0000592 log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
593 SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000594
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000595 /*
596 * Read per-user configuration file. Ignore the system wide config
597 * file if the user specifies a config file on the command line.
598 */
599 if (config != NULL) {
Damien Miller914420f2004-04-20 20:14:07 +1000600 if (!read_config_file(config, host, &options, 0))
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000601 fatal("Can't open user config file %.100s: "
602 "%.100s", config, strerror(errno));
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000603 } else {
604 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
605 _PATH_SSH_USER_CONFFILE);
Damien Miller57a44762004-04-20 20:11:57 +1000606 (void)read_config_file(buf, host, &options, 1);
Ben Lindstrom83f07d12001-10-03 17:22:29 +0000607
608 /* Read systemwide configuration file after use config. */
Darren Tuckerfc959702004-07-17 16:12:08 +1000609 (void)read_config_file(_PATH_HOST_CONFIG_FILE, host,
Damien Miller57a44762004-04-20 20:11:57 +1000610 &options, 0);
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000611 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000612
Damien Miller95def091999-11-25 00:26:21 +1100613 /* Fill configuration defaults. */
614 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000615
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000616 channel_set_af(options.address_family);
617
Damien Miller95def091999-11-25 00:26:21 +1100618 /* reinit */
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000619 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000620
Damien Miller60bc5172001-03-19 09:38:15 +1100621 seed_rng();
622
Damien Miller95def091999-11-25 00:26:21 +1100623 if (options.user == NULL)
624 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000625
Damien Miller95def091999-11-25 00:26:21 +1100626 if (options.hostname != NULL)
627 host = options.hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000628
Darren Tucker3f521e22003-07-03 16:20:42 +1000629 /* force lowercase for hostkey matching */
630 if (options.host_key_alias != NULL) {
631 for (p = options.host_key_alias; *p; p++)
632 if (isupper(*p))
Damien Miller1d2b6702006-03-26 14:09:54 +1100633 *p = (char)tolower(*p);
Darren Tucker3f521e22003-07-03 16:20:42 +1000634 }
635
Damien Miller7c71cc72005-06-26 08:56:31 +1000636 /* Get default port if port has not been set. */
637 if (options.port == 0) {
638 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
639 options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
640 }
641
Damien Miller9f1e33a2003-02-24 11:57:32 +1100642 if (options.proxy_command != NULL &&
643 strcmp(options.proxy_command, "none") == 0)
644 options.proxy_command = NULL;
Damien Miller8f74c8f2005-06-26 08:56:03 +1000645 if (options.control_path != NULL &&
646 strcmp(options.control_path, "none") == 0)
647 options.control_path = NULL;
Damien Miller9f1e33a2003-02-24 11:57:32 +1100648
Damien Miller0e220db2004-06-15 10:34:08 +1000649 if (options.control_path != NULL) {
Damien Miller3ec54c72006-03-15 11:30:13 +1100650 char me[NI_MAXHOST];
651
652 if (gethostname(me, sizeof(me)) == -1)
653 fatal("gethostname: %s", strerror(errno));
Damien Miller6476cad2005-06-16 13:18:34 +1000654 snprintf(buf, sizeof(buf), "%d", options.port);
655 cp = tilde_expand_filename(options.control_path,
656 original_real_uid);
657 options.control_path = percent_expand(cp, "p", buf, "h", host,
Damien Miller3ec54c72006-03-15 11:30:13 +1100658 "r", options.user, "l", me, (char *)NULL);
Damien Miller6476cad2005-06-16 13:18:34 +1000659 xfree(cp);
Damien Miller0e220db2004-06-15 10:34:08 +1000660 }
Darren Tucker0814d312005-06-01 23:08:51 +1000661 if (mux_command != 0 && options.control_path == NULL)
662 fatal("No ControlPath specified for \"-O\" command");
Damien Millerd14b1e72005-06-16 13:19:41 +1000663 if (options.control_path != NULL)
Damien Millerdadfd4d2005-05-26 12:07:13 +1000664 control_client(options.control_path);
Damien Miller0e220db2004-06-15 10:34:08 +1000665
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000666 /* Open a connection to the remote host. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000667 if (ssh_connect(host, &hostaddr, options.port,
668 options.address_family, options.connection_attempts,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000669#ifdef HAVE_CYGWIN
670 options.use_privileged_port,
671#else
Ben Lindstromf9c48842002-06-11 16:37:51 +0000672 original_effective_uid == 0 && options.use_privileged_port,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000673#endif
Ben Lindstromda394ca2002-06-12 16:11:12 +0000674 options.proxy_command) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100675 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000676
Damien Miller5428f641999-11-25 11:54:57 +1100677 /*
678 * If we successfully made the connection, load the host private key
679 * in case we will need it later for combined rsa-rhosts
680 * authentication. This must be done before releasing extra
681 * privileges, because the file is only readable by root.
Ben Lindstrom9e5bb572002-06-06 19:58:27 +0000682 * If we cannot access the private keys, load the public keys
683 * instead and try to execute the ssh-keysign helper instead.
Damien Miller5428f641999-11-25 11:54:57 +1100684 */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000685 sensitive_data.nkeys = 0;
686 sensitive_data.keys = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000687 sensitive_data.external_keysign = 0;
Ben Lindstrom24157572002-06-12 16:09:39 +0000688 if (options.rhosts_rsa_authentication ||
689 options.hostbased_authentication) {
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000690 sensitive_data.nkeys = 3;
Damien Miller07d86be2006-03-26 14:19:21 +1100691 sensitive_data.keys = xcalloc(sensitive_data.nkeys,
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000692 sizeof(Key));
Ben Lindstromf9c48842002-06-11 16:37:51 +0000693
694 PRIV_START;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000695 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
Ben Lindstromd0fca422001-03-26 13:44:06 +0000696 _PATH_HOST_KEY_FILE, "", NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000697 sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
698 _PATH_HOST_DSA_KEY_FILE, "", NULL);
699 sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
700 _PATH_HOST_RSA_KEY_FILE, "", NULL);
Ben Lindstromf9c48842002-06-11 16:37:51 +0000701 PRIV_END;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000702
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +0000703 if (options.hostbased_authentication == 1 &&
704 sensitive_data.keys[0] == NULL &&
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000705 sensitive_data.keys[1] == NULL &&
706 sensitive_data.keys[2] == NULL) {
707 sensitive_data.keys[1] = key_load_public(
708 _PATH_HOST_DSA_KEY_FILE, NULL);
709 sensitive_data.keys[2] = key_load_public(
710 _PATH_HOST_RSA_KEY_FILE, NULL);
711 sensitive_data.external_keysign = 1;
712 }
Damien Miller95def091999-11-25 00:26:21 +1100713 }
Damien Miller5428f641999-11-25 11:54:57 +1100714 /*
715 * Get rid of any extra privileges that we may have. We will no
716 * longer need them. Also, extra privileges could make it very hard
717 * to read identity files and other non-world-readable files from the
718 * user's home directory if it happens to be on a NFS volume where
719 * root is mapped to nobody.
720 */
Darren Tucker25f60a72004-08-15 17:23:34 +1000721 if (original_effective_uid == 0) {
722 PRIV_START;
723 permanently_set_uid(pw);
724 }
Damien Miller95def091999-11-25 00:26:21 +1100725
Damien Miller5428f641999-11-25 11:54:57 +1100726 /*
727 * Now that we are back to our own permissions, create ~/.ssh
Damien Miller788f2122005-11-05 15:14:59 +1100728 * directory if it doesn't already exist.
Damien Miller5428f641999-11-25 11:54:57 +1100729 */
Ben Lindstrom930b14a2001-08-15 23:19:21 +0000730 snprintf(buf, sizeof buf, "%.100s%s%.100s", pw->pw_dir, strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
Damien Miller95def091999-11-25 00:26:21 +1100731 if (stat(buf, &st) < 0)
Damien Millerbe484b52000-07-15 14:14:16 +1000732 if (mkdir(buf, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100733 error("Could not create directory '%.200s'.", buf);
734
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000735 /* load options.identity_files */
736 load_public_identity_files();
737
738 /* Expand ~ in known host file names. */
739 /* XXX mem-leaks: */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100740 options.system_hostfile =
741 tilde_expand_filename(options.system_hostfile, original_real_uid);
742 options.user_hostfile =
743 tilde_expand_filename(options.user_hostfile, original_real_uid);
744 options.system_hostfile2 =
745 tilde_expand_filename(options.system_hostfile2, original_real_uid);
746 options.user_hostfile2 =
747 tilde_expand_filename(options.user_hostfile2, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100748
Damien Miller07cd5892001-11-12 10:52:03 +1100749 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
750
Damien Miller95def091999-11-25 00:26:21 +1100751 /* Log into the remote system. This never returns if the login fails. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000752 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, pw);
Damien Miller95def091999-11-25 00:26:21 +1100753
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000754 /* We no longer need the private host keys. Clear them now. */
755 if (sensitive_data.nkeys != 0) {
756 for (i = 0; i < sensitive_data.nkeys; i++) {
757 if (sensitive_data.keys[i] != NULL) {
758 /* Destroys contents safely */
759 debug3("clear hostkey %d", i);
760 key_free(sensitive_data.keys[i]);
761 sensitive_data.keys[i] = NULL;
762 }
763 }
764 xfree(sensitive_data.keys);
765 }
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +0000766 for (i = 0; i < options.num_identity_files; i++) {
767 if (options.identity_files[i]) {
768 xfree(options.identity_files[i]);
769 options.identity_files[i] = NULL;
770 }
771 if (options.identity_keys[i]) {
772 key_free(options.identity_keys[i]);
773 options.identity_keys[i] = NULL;
774 }
775 }
Damien Miller95def091999-11-25 00:26:21 +1100776
Damien Miller1383bd82000-04-06 12:32:37 +1000777 exit_status = compat20 ? ssh_session2() : ssh_session();
778 packet_close();
Damien Miller8c4e18a2002-09-19 12:05:02 +1000779
Damien Miller0e220db2004-06-15 10:34:08 +1000780 if (options.control_path != NULL && control_fd != -1)
781 unlink(options.control_path);
782
Damien Miller8c4e18a2002-09-19 12:05:02 +1000783 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100784 * Send SIGHUP to proxy command if used. We don't wait() in
Damien Miller8c4e18a2002-09-19 12:05:02 +1000785 * case it hangs and instead rely on init to reap the child
786 */
787 if (proxy_command_pid > 1)
788 kill(proxy_command_pid, SIGHUP);
789
Damien Miller1383bd82000-04-06 12:32:37 +1000790 return exit_status;
791}
792
Ben Lindstrombba81212001-06-25 05:01:22 +0000793static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100794ssh_init_forwarding(void)
795{
Kevin Steves12057502001-02-05 14:54:34 +0000796 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100797 int i;
Kevin Steves12057502001-02-05 14:54:34 +0000798
Damien Miller0bc1bd82000-11-13 22:57:25 +1100799 /* Initiate local TCP/IP port forwardings. */
800 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100801 debug("Local connections to %.200s:%d forwarded to remote "
802 "address %.200s:%d",
Darren Tucker47eede72005-03-14 23:08:12 +1100803 (options.local_forwards[i].listen_host == NULL) ?
804 (options.gateway_ports ? "*" : "LOCALHOST") :
Damien Millerf91ee4c2005-03-01 21:24:33 +1100805 options.local_forwards[i].listen_host,
806 options.local_forwards[i].listen_port,
807 options.local_forwards[i].connect_host,
808 options.local_forwards[i].connect_port);
Damien Millerb16461c2002-01-22 23:29:22 +1100809 success += channel_setup_local_fwd_listener(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100810 options.local_forwards[i].listen_host,
811 options.local_forwards[i].listen_port,
812 options.local_forwards[i].connect_host,
813 options.local_forwards[i].connect_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100814 options.gateway_ports);
815 }
Kevin Steves12057502001-02-05 14:54:34 +0000816 if (i > 0 && success == 0)
817 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100818
819 /* Initiate remote TCP/IP port forwardings. */
820 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100821 debug("Remote connections from %.200s:%d forwarded to "
822 "local address %.200s:%d",
Damien Miller46d38de2005-07-17 17:02:09 +1000823 (options.remote_forwards[i].listen_host == NULL) ?
Damien Milleraa3bb102005-11-05 15:12:59 +1100824 "LOCALHOST" : options.remote_forwards[i].listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100825 options.remote_forwards[i].listen_port,
826 options.remote_forwards[i].connect_host,
827 options.remote_forwards[i].connect_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100828 channel_request_remote_forwarding(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100829 options.remote_forwards[i].listen_host,
830 options.remote_forwards[i].listen_port,
831 options.remote_forwards[i].connect_host,
832 options.remote_forwards[i].connect_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100833 }
834}
835
Ben Lindstrombba81212001-06-25 05:01:22 +0000836static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100837check_agent_present(void)
838{
839 if (options.forward_agent) {
Damien Miller788f2122005-11-05 15:14:59 +1100840 /* Clear agent forwarding if we don't have an agent. */
Damien Miller789e95d2002-09-12 09:52:46 +1000841 if (!ssh_agent_present())
Damien Miller0bc1bd82000-11-13 22:57:25 +1100842 options.forward_agent = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100843 }
844}
845
Ben Lindstrombba81212001-06-25 05:01:22 +0000846static int
Damien Miller1383bd82000-04-06 12:32:37 +1000847ssh_session(void)
848{
849 int type;
Damien Miller1383bd82000-04-06 12:32:37 +1000850 int interactive = 0;
851 int have_tty = 0;
852 struct winsize ws;
Damien Miller1383bd82000-04-06 12:32:37 +1000853 char *cp;
Damien Miller17e7ed02005-06-17 12:54:33 +1000854 const char *display;
Damien Miller1383bd82000-04-06 12:32:37 +1000855
Damien Miller95def091999-11-25 00:26:21 +1100856 /* Enable compression if requested. */
857 if (options.compression) {
858 debug("Requesting compression at level %d.", options.compression_level);
859
860 if (options.compression_level < 1 || options.compression_level > 9)
861 fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
862
863 /* Send the request. */
864 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
865 packet_put_int(options.compression_level);
866 packet_send();
867 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100868 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100869 if (type == SSH_SMSG_SUCCESS)
870 packet_start_compression(options.compression_level);
871 else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000872 logit("Warning: Remote host refused compression.");
Damien Miller95def091999-11-25 00:26:21 +1100873 else
874 packet_disconnect("Protocol error waiting for compression response.");
875 }
876 /* Allocate a pseudo tty if appropriate. */
877 if (tty_flag) {
878 debug("Requesting pty.");
879
880 /* Start the packet. */
881 packet_start(SSH_CMSG_REQUEST_PTY);
882
883 /* Store TERM in the packet. There is no limit on the
884 length of the string. */
885 cp = getenv("TERM");
886 if (!cp)
887 cp = "";
Ben Lindstrom664408d2001-06-09 01:42:01 +0000888 packet_put_cstring(cp);
Damien Miller95def091999-11-25 00:26:21 +1100889
890 /* Store window size in the packet. */
891 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
892 memset(&ws, 0, sizeof(ws));
Damien Miller71a73672006-03-26 14:04:36 +1100893 packet_put_int((u_int)ws.ws_row);
894 packet_put_int((u_int)ws.ws_col);
895 packet_put_int((u_int)ws.ws_xpixel);
896 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller95def091999-11-25 00:26:21 +1100897
898 /* Store tty modes in the packet. */
Ben Lindstromae8e2d32001-04-14 23:13:02 +0000899 tty_make_modes(fileno(stdin), NULL);
Damien Miller95def091999-11-25 00:26:21 +1100900
901 /* Send the packet, and wait for it to leave. */
902 packet_send();
903 packet_write_wait();
904
905 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100906 type = packet_read();
Damien Miller450a7a12000-03-26 13:04:51 +1000907 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100908 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +1000909 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000910 } else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000911 logit("Warning: Remote host failed or refused to allocate a pseudo tty.");
Damien Miller95def091999-11-25 00:26:21 +1100912 else
913 packet_disconnect("Protocol error waiting for pty request response.");
914 }
915 /* Request X11 forwarding if enabled and DISPLAY is set. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000916 display = getenv("DISPLAY");
917 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000918 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +1000919 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000920 client_x11_get_proto(display, options.xauth_location,
921 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +1000922 /* Request forwarding with authentication spoofing. */
Damien Miller95def091999-11-25 00:26:21 +1100923 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +1000924 x11_request_forwarding_with_spoofing(0, display, proto, data);
Damien Miller95def091999-11-25 00:26:21 +1100925
926 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100927 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100928 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100929 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +1000930 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller996acd22003-04-09 20:59:48 +1000931 logit("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +1000932 } else {
Damien Miller95def091999-11-25 00:26:21 +1100933 packet_disconnect("Protocol error waiting for X11 forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +1000934 }
Damien Miller95def091999-11-25 00:26:21 +1100935 }
936 /* Tell the packet module whether this is an interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000937 packet_set_interactive(interactive);
Damien Miller95def091999-11-25 00:26:21 +1100938
939 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100940 check_agent_present();
941
Damien Miller95def091999-11-25 00:26:21 +1100942 if (options.forward_agent) {
943 debug("Requesting authentication agent forwarding.");
944 auth_request_forwarding();
945
946 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100947 type = packet_read();
Damien Miller48b03fc2002-01-22 23:11:40 +1100948 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100949 if (type != SSH_SMSG_SUCCESS)
Damien Miller996acd22003-04-09 20:59:48 +1000950 logit("Warning: Remote host denied authentication agent forwarding.");
Damien Miller95def091999-11-25 00:26:21 +1100951 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000952
Damien Miller0bc1bd82000-11-13 22:57:25 +1100953 /* Initiate port forwardings. */
954 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000955
Damien Miller5428f641999-11-25 11:54:57 +1100956 /* If requested, let ssh continue in the background. */
Damien Miller4af51302000-04-16 11:18:38 +1000957 if (fork_after_authentication_flag)
Damien Miller5428f641999-11-25 11:54:57 +1100958 if (daemon(1, 1) < 0)
959 fatal("daemon() failed: %.200s", strerror(errno));
960
961 /*
962 * If a command was specified on the command line, execute the
963 * command now. Otherwise request the server to start a shell.
964 */
Damien Miller95def091999-11-25 00:26:21 +1100965 if (buffer_len(&command) > 0) {
966 int len = buffer_len(&command);
967 if (len > 900)
968 len = 900;
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100969 debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
Damien Miller95def091999-11-25 00:26:21 +1100970 packet_start(SSH_CMSG_EXEC_CMD);
971 packet_put_string(buffer_ptr(&command), buffer_len(&command));
972 packet_send();
973 packet_write_wait();
974 } else {
975 debug("Requesting shell.");
976 packet_start(SSH_CMSG_EXEC_SHELL);
977 packet_send();
978 packet_write_wait();
979 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000980
Damien Miller95def091999-11-25 00:26:21 +1100981 /* Enter the interactive session. */
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000982 return client_loop(have_tty, tty_flag ?
983 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
Damien Miller1383bd82000-04-06 12:32:37 +1000984}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000985
Ben Lindstrombba81212001-06-25 05:01:22 +0000986static void
Damien Miller0e220db2004-06-15 10:34:08 +1000987ssh_subsystem_reply(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000988{
989 int id, len;
990
991 id = packet_get_int();
992 len = buffer_len(&command);
Ben Lindstrom4040fe12001-03-05 06:52:57 +0000993 if (len > 900)
994 len = 900;
Damien Miller48b03fc2002-01-22 23:11:40 +1100995 packet_check_eom();
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000996 if (type == SSH2_MSG_CHANNEL_FAILURE)
997 fatal("Request for subsystem '%.*s' failed on channel %d",
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100998 len, (u_char *)buffer_ptr(&command), id);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000999}
1000
Damien Miller2797f7f2002-04-23 21:09:44 +10001001void
Damien Miller509b0102003-12-17 16:33:10 +11001002client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt)
Damien Miller2797f7f2002-04-23 21:09:44 +10001003{
1004 int i;
1005
1006 i = client_global_request_id++;
Damien Miller509b0102003-12-17 16:33:10 +11001007 if (i >= options.num_remote_forwards)
Damien Miller2797f7f2002-04-23 21:09:44 +10001008 return;
Damien Miller2797f7f2002-04-23 21:09:44 +10001009 debug("remote forward %s for: listen %d, connect %s:%d",
1010 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
Damien Millerf91ee4c2005-03-01 21:24:33 +11001011 options.remote_forwards[i].listen_port,
1012 options.remote_forwards[i].connect_host,
1013 options.remote_forwards[i].connect_port);
Damien Miller2797f7f2002-04-23 21:09:44 +10001014 if (type == SSH2_MSG_REQUEST_FAILURE)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001015 logit("Warning: remote port forwarding failed for listen "
1016 "port %d", options.remote_forwards[i].listen_port);
Damien Miller2797f7f2002-04-23 21:09:44 +10001017}
1018
Damien Miller0e220db2004-06-15 10:34:08 +10001019static void
1020ssh_control_listener(void)
1021{
1022 struct sockaddr_un addr;
1023 mode_t old_umask;
Damien Miller07b6ff12004-06-15 11:14:45 +10001024 int addr_len;
1025
Damien Millerd14b1e72005-06-16 13:19:41 +10001026 if (options.control_path == NULL ||
1027 options.control_master == SSHCTL_MASTER_NO)
Damien Miller0e220db2004-06-15 10:34:08 +10001028 return;
1029
Damien Millerd14b1e72005-06-16 13:19:41 +10001030 debug("setting up multiplex master socket");
1031
Damien Miller0e220db2004-06-15 10:34:08 +10001032 memset(&addr, '\0', sizeof(addr));
1033 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001034 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001035 strlen(options.control_path) + 1;
1036
1037 if (strlcpy(addr.sun_path, options.control_path,
1038 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1039 fatal("ControlPath too long");
1040
1041 if ((control_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001042 fatal("%s socket(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001043
1044 old_umask = umask(0177);
Damien Miller90967402006-03-26 14:07:26 +11001045 if (bind(control_fd, (struct sockaddr *)&addr, addr_len) == -1) {
Damien Miller0e220db2004-06-15 10:34:08 +10001046 control_fd = -1;
Damien Miller4f10e252005-05-04 15:33:09 +10001047 if (errno == EINVAL || errno == EADDRINUSE)
Damien Miller0e220db2004-06-15 10:34:08 +10001048 fatal("ControlSocket %s already exists",
1049 options.control_path);
1050 else
Damien Miller15d72a02005-11-05 15:07:33 +11001051 fatal("%s bind(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001052 }
1053 umask(old_umask);
1054
1055 if (listen(control_fd, 64) == -1)
Damien Miller15d72a02005-11-05 15:07:33 +11001056 fatal("%s listen(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001057
1058 set_nonblock(control_fd);
1059}
1060
Ben Lindstromf558cf62001-09-20 23:13:49 +00001061/* request pty/x11/agent/tcpfwd/shell for channel */
Ben Lindstrombba81212001-06-25 05:01:22 +00001062static void
Ben Lindstromf558cf62001-09-20 23:13:49 +00001063ssh_session2_setup(int id, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +10001064{
Damien Miller3756dce2004-06-18 01:17:29 +10001065 extern char **environ;
Damien Miller17e7ed02005-06-17 12:54:33 +10001066 const char *display;
Damien Miller0e220db2004-06-15 10:34:08 +10001067 int interactive = tty_flag;
Damien Miller17e7ed02005-06-17 12:54:33 +10001068
Damien Miller46d38de2005-07-17 17:02:09 +10001069 display = getenv("DISPLAY");
Damien Miller17e7ed02005-06-17 12:54:33 +10001070 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001071 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001072 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001073 client_x11_get_proto(display, options.xauth_location,
1074 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001075 /* Request forwarding with authentication spoofing. */
1076 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +10001077 x11_request_forwarding_with_spoofing(id, display, proto, data);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001078 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001079 /* XXX wait for reply */
1080 }
1081
Damien Miller0bc1bd82000-11-13 22:57:25 +11001082 check_agent_present();
1083 if (options.forward_agent) {
1084 debug("Requesting authentication agent forwarding.");
1085 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1086 packet_send();
1087 }
1088
Damien Miller7b58e802005-12-13 19:33:19 +11001089 if (options.tun_open != SSH_TUNMODE_NO) {
Damien Millerd27b9472005-12-13 19:29:02 +11001090 Channel *c;
1091 int fd;
1092
1093 debug("Requesting tun.");
Damien Miller7b58e802005-12-13 19:33:19 +11001094 if ((fd = tun_open(options.tun_local,
1095 options.tun_open)) >= 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11001096 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1097 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1098 0, "tun", 1);
1099 c->datagram = 1;
Damien Miller598bbc22005-12-31 16:33:36 +11001100#if defined(SSH_TUN_FILTER)
1101 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1102 channel_register_filter(c->self, sys_tun_infilter,
1103 sys_tun_outfilter);
1104#endif
Damien Millerd27b9472005-12-13 19:29:02 +11001105 packet_start(SSH2_MSG_CHANNEL_OPEN);
1106 packet_put_cstring("tun@openssh.com");
1107 packet_put_int(c->self);
1108 packet_put_int(c->local_window_max);
1109 packet_put_int(c->local_maxpacket);
Damien Miller7b58e802005-12-13 19:33:19 +11001110 packet_put_int(options.tun_open);
Damien Millerd27b9472005-12-13 19:29:02 +11001111 packet_put_int(options.tun_remote);
1112 packet_send();
1113 }
1114 }
1115
Damien Miller0e220db2004-06-15 10:34:08 +10001116 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
Damien Miller3756dce2004-06-18 01:17:29 +10001117 NULL, fileno(stdin), &command, environ, &ssh_subsystem_reply);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001118
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001119 packet_set_interactive(interactive);
Damien Miller1383bd82000-04-06 12:32:37 +10001120}
1121
Ben Lindstromf558cf62001-09-20 23:13:49 +00001122/* open new channel for a session */
Ben Lindstrombba81212001-06-25 05:01:22 +00001123static int
Ben Lindstromf558cf62001-09-20 23:13:49 +00001124ssh_session2_open(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001125{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001126 Channel *c;
1127 int window, packetmax, in, out, err;
Damien Millerad833b32000-08-23 10:46:23 +10001128
Damien Millercaf6dd62000-08-29 11:33:50 +11001129 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001130 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001131 } else {
1132 in = dup(STDIN_FILENO);
1133 }
1134 out = dup(STDOUT_FILENO);
1135 err = dup(STDERR_FILENO);
1136
1137 if (in < 0 || out < 0 || err < 0)
1138 fatal("dup() in/out/err failed");
1139
Damien Miller69b69aa2000-10-28 14:19:58 +11001140 /* enable nonblocking unless tty */
1141 if (!isatty(in))
1142 set_nonblock(in);
1143 if (!isatty(out))
1144 set_nonblock(out);
1145 if (!isatty(err))
1146 set_nonblock(err);
1147
Damien Millere4340be2000-09-16 13:29:08 +11001148 window = CHAN_SES_WINDOW_DEFAULT;
1149 packetmax = CHAN_SES_PACKET_DEFAULT;
Damien Miller19a59452002-02-19 15:20:57 +11001150 if (tty_flag) {
1151 window >>= 1;
1152 packetmax >>= 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001153 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001154 c = channel_new(
Damien Miller1383bd82000-04-06 12:32:37 +10001155 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001156 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001157 "client-session", /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001158
Ben Lindstromf558cf62001-09-20 23:13:49 +00001159 debug3("ssh_session2_open: channel_new: %d", c->self);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001160
Ben Lindstrom5ec26452001-06-09 00:18:51 +00001161 channel_send_open(c->self);
Ben Lindstromf558cf62001-09-20 23:13:49 +00001162 if (!no_shell_flag)
Damien Miller0e220db2004-06-15 10:34:08 +10001163 channel_register_confirm(c->self, ssh_session2_setup, NULL);
Damien Miller1383bd82000-04-06 12:32:37 +10001164
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001165 return c->self;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001166}
1167
Ben Lindstrombba81212001-06-25 05:01:22 +00001168static int
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001169ssh_session2(void)
1170{
Ben Lindstromf558cf62001-09-20 23:13:49 +00001171 int id = -1;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001172
1173 /* XXX should be pre-session */
1174 ssh_init_forwarding();
Damien Miller0e220db2004-06-15 10:34:08 +10001175 ssh_control_listener();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001176
Ben Lindstromf558cf62001-09-20 23:13:49 +00001177 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1178 id = ssh_session2_open();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001179
Damien Millerd27b9472005-12-13 19:29:02 +11001180 /* Execute a local command */
1181 if (options.local_command != NULL &&
1182 options.permit_local_command)
1183 ssh_local_cmd(options.local_command);
1184
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001185 /* If requested, let ssh continue in the background. */
1186 if (fork_after_authentication_flag)
1187 if (daemon(1, 1) < 0)
1188 fatal("daemon() failed: %.200s", strerror(errno));
1189
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001190 return client_loop(tty_flag, tty_flag ?
1191 options.escape_char : SSH_ESCAPECHAR_NONE, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001192}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001193
Ben Lindstrombba81212001-06-25 05:01:22 +00001194static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001195load_public_identity_files(void)
1196{
1197 char *filename;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001198 int i = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001199 Key *public;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001200#ifdef SMARTCARD
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001201 Key **keys;
1202
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001203 if (options.smartcard_device != NULL &&
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001204 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1205 (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL ) {
1206 int count = 0;
1207 for (i = 0; keys[i] != NULL; i++) {
1208 count++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001209 memmove(&options.identity_files[1], &options.identity_files[0],
1210 sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
1211 memmove(&options.identity_keys[1], &options.identity_keys[0],
1212 sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1213 options.num_identity_files++;
1214 options.identity_keys[0] = keys[i];
Damien Miller56a0bb02003-06-18 20:28:40 +10001215 options.identity_files[0] = sc_get_key_label(keys[i]);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001216 }
Ben Lindstrom4f054602002-03-26 03:23:00 +00001217 if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1218 options.num_identity_files = SSH_MAX_IDENTITY_FILES;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001219 i = count;
1220 xfree(keys);
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001221 }
Ben Lindstromffce1472001-08-06 21:57:31 +00001222#endif /* SMARTCARD */
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001223 for (; i < options.num_identity_files; i++) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001224 filename = tilde_expand_filename(options.identity_files[i],
1225 original_real_uid);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001226 public = key_load_public(filename, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001227 debug("identity file %s type %d", filename,
1228 public ? public->type : -1);
1229 xfree(options.identity_files[i]);
1230 options.identity_files[i] = filename;
1231 options.identity_keys[i] = public;
1232 }
1233}
Damien Miller0e220db2004-06-15 10:34:08 +10001234
1235static void
1236control_client_sighandler(int signo)
1237{
1238 control_client_terminate = signo;
1239}
1240
1241static void
1242control_client_sigrelay(int signo)
1243{
1244 if (control_server_pid > 1)
1245 kill(control_server_pid, signo);
1246}
1247
Darren Tucker365433f2004-06-22 12:29:23 +10001248static int
1249env_permitted(char *env)
1250{
1251 int i;
1252 char name[1024], *cp;
1253
Damien Miller07d86be2006-03-26 14:19:21 +11001254 if (strlcpy(name, env, sizeof(name)) >= sizeof(name))
1255 fatal("env_permitted: name too long");
Darren Tucker365433f2004-06-22 12:29:23 +10001256 if ((cp = strchr(name, '=')) == NULL)
1257 return (0);
1258
1259 *cp = '\0';
1260
1261 for (i = 0; i < options.num_send_env; i++)
1262 if (match_pattern(name, options.send_env[i]))
1263 return (1);
1264
1265 return (0);
1266}
1267
Damien Miller0e220db2004-06-15 10:34:08 +10001268static void
1269control_client(const char *path)
1270{
1271 struct sockaddr_un addr;
Darren Tucker39207a42004-11-05 20:19:51 +11001272 int i, r, fd, sock, exitval, num_env, addr_len;
Damien Miller0e220db2004-06-15 10:34:08 +10001273 Buffer m;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001274 char *term;
Damien Miller3756dce2004-06-18 01:17:29 +10001275 extern char **environ;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001276 u_int flags;
Darren Tuckerfc959702004-07-17 16:12:08 +10001277
Damien Millerd14b1e72005-06-16 13:19:41 +10001278 if (mux_command == 0)
1279 mux_command = SSHMUX_COMMAND_OPEN;
1280
1281 switch (options.control_master) {
1282 case SSHCTL_MASTER_AUTO:
1283 case SSHCTL_MASTER_AUTO_ASK:
1284 debug("auto-mux: Trying existing master");
1285 /* FALLTHROUGH */
1286 case SSHCTL_MASTER_NO:
1287 break;
1288 default:
1289 return;
1290 }
1291
Damien Miller0e220db2004-06-15 10:34:08 +10001292 memset(&addr, '\0', sizeof(addr));
1293 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001294 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001295 strlen(path) + 1;
1296
1297 if (strlcpy(addr.sun_path, path,
1298 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1299 fatal("ControlPath too long");
1300
1301 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1302 fatal("%s socket(): %s", __func__, strerror(errno));
1303
Damien Miller90967402006-03-26 14:07:26 +11001304 if (connect(sock, (struct sockaddr *)&addr, addr_len) == -1) {
Darren Tucker0814d312005-06-01 23:08:51 +10001305 if (mux_command != SSHMUX_COMMAND_OPEN) {
1306 fatal("Control socket connect(%.100s): %s", path,
1307 strerror(errno));
1308 }
Damien Miller538c9b72005-05-26 12:11:28 +10001309 if (errno == ENOENT)
Damien Miller4662d342006-03-26 13:59:59 +11001310 debug("Control socket \"%.100s\" does not exist", path);
Damien Miller538c9b72005-05-26 12:11:28 +10001311 else {
Damien Miller4662d342006-03-26 13:59:59 +11001312 error("Control socket connect(%.100s): %s", path,
Damien Miller538c9b72005-05-26 12:11:28 +10001313 strerror(errno));
1314 }
Damien Miller4662d342006-03-26 13:59:59 +11001315 close(sock);
1316 return;
1317 }
Damien Miller46d38de2005-07-17 17:02:09 +10001318
Damien Miller4662d342006-03-26 13:59:59 +11001319 if (stdin_null_flag) {
1320 if ((fd = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1321 fatal("open(/dev/null): %s", strerror(errno));
1322 if (dup2(fd, STDIN_FILENO) == -1)
1323 fatal("dup2: %s", strerror(errno));
1324 if (fd > STDERR_FILENO)
1325 close(fd);
1326 }
Damien Miller46d38de2005-07-17 17:02:09 +10001327
Damien Miller13390022005-07-06 09:44:19 +10001328 term = getenv("TERM");
Darren Tucker7ebfc102004-11-07 20:06:19 +11001329
1330 flags = 0;
1331 if (tty_flag)
1332 flags |= SSHMUX_FLAG_TTY;
1333 if (subsystem_flag)
1334 flags |= SSHMUX_FLAG_SUBSYS;
Damien Miller13390022005-07-06 09:44:19 +10001335 if (options.forward_x11)
1336 flags |= SSHMUX_FLAG_X11_FWD;
1337 if (options.forward_agent)
1338 flags |= SSHMUX_FLAG_AGENT_FWD;
Damien Miller0e220db2004-06-15 10:34:08 +10001339
Damien Miller0e220db2004-06-15 10:34:08 +10001340 buffer_init(&m);
1341
Darren Tucker7ebfc102004-11-07 20:06:19 +11001342 /* Send our command to server */
1343 buffer_put_int(&m, mux_command);
1344 buffer_put_int(&m, flags);
Damien Miller13390022005-07-06 09:44:19 +10001345 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001346 fatal("%s: msg_send", __func__);
1347 buffer_clear(&m);
1348
1349 /* Get authorisation status and PID of controlee */
Damien Miller0e220db2004-06-15 10:34:08 +10001350 if (ssh_msg_recv(sock, &m) == -1)
1351 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001352 if (buffer_get_char(&m) != SSHMUX_VER)
Damien Miller0e220db2004-06-15 10:34:08 +10001353 fatal("%s: wrong version", __func__);
Damien Miller23f07702004-06-18 01:19:03 +10001354 if (buffer_get_int(&m) != 1)
1355 fatal("Connection to master denied");
Damien Miller0e220db2004-06-15 10:34:08 +10001356 control_server_pid = buffer_get_int(&m);
1357
Damien Miller0e220db2004-06-15 10:34:08 +10001358 buffer_clear(&m);
Damien Miller0e220db2004-06-15 10:34:08 +10001359
Darren Tucker7ebfc102004-11-07 20:06:19 +11001360 switch (mux_command) {
1361 case SSHMUX_COMMAND_ALIVE_CHECK:
Darren Tucker47eede72005-03-14 23:08:12 +11001362 fprintf(stderr, "Master running (pid=%d)\r\n",
Darren Tucker7ebfc102004-11-07 20:06:19 +11001363 control_server_pid);
1364 exit(0);
1365 case SSHMUX_COMMAND_TERMINATE:
1366 fprintf(stderr, "Exit request sent.\r\n");
1367 exit(0);
1368 case SSHMUX_COMMAND_OPEN:
1369 /* continue below */
1370 break;
1371 default:
1372 fatal("silly mux_command %d", mux_command);
1373 }
1374
1375 /* SSHMUX_COMMAND_OPEN */
Damien Miller13390022005-07-06 09:44:19 +10001376 buffer_put_cstring(&m, term ? term : "");
Damien Miller0e220db2004-06-15 10:34:08 +10001377 buffer_append(&command, "\0", 1);
1378 buffer_put_cstring(&m, buffer_ptr(&command));
1379
Darren Tucker365433f2004-06-22 12:29:23 +10001380 if (options.num_send_env == 0 || environ == NULL) {
1381 buffer_put_int(&m, 0);
Darren Tuckerfc959702004-07-17 16:12:08 +10001382 } else {
Darren Tucker365433f2004-06-22 12:29:23 +10001383 /* Pass environment */
1384 num_env = 0;
1385 for (i = 0; environ[i] != NULL; i++)
1386 if (env_permitted(environ[i]))
1387 num_env++; /* Count */
Darren Tuckerfc959702004-07-17 16:12:08 +10001388
Darren Tucker365433f2004-06-22 12:29:23 +10001389 buffer_put_int(&m, num_env);
1390
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001391 for (i = 0; environ[i] != NULL && num_env >= 0; i++)
1392 if (env_permitted(environ[i])) {
1393 num_env--;
Darren Tucker365433f2004-06-22 12:29:23 +10001394 buffer_put_cstring(&m, environ[i]);
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001395 }
Darren Tucker365433f2004-06-22 12:29:23 +10001396 }
Damien Miller3756dce2004-06-18 01:17:29 +10001397
Damien Miller13390022005-07-06 09:44:19 +10001398 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Damien Miller0e220db2004-06-15 10:34:08 +10001399 fatal("%s: msg_send", __func__);
1400
1401 mm_send_fd(sock, STDIN_FILENO);
1402 mm_send_fd(sock, STDOUT_FILENO);
1403 mm_send_fd(sock, STDERR_FILENO);
1404
1405 /* Wait for reply, so master has a chance to gather ttymodes */
1406 buffer_clear(&m);
1407 if (ssh_msg_recv(sock, &m) == -1)
1408 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001409 if (buffer_get_char(&m) != SSHMUX_VER)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001410 fatal("%s: wrong version", __func__);
Damien Miller0e220db2004-06-15 10:34:08 +10001411 buffer_free(&m);
1412
Darren Tucker07336da2004-11-05 20:02:16 +11001413 signal(SIGHUP, control_client_sighandler);
Damien Miller0809e232004-06-18 22:20:57 +10001414 signal(SIGINT, control_client_sighandler);
1415 signal(SIGTERM, control_client_sighandler);
1416 signal(SIGWINCH, control_client_sigrelay);
1417
Damien Miller0e220db2004-06-15 10:34:08 +10001418 if (tty_flag)
1419 enter_raw_mode();
1420
1421 /* Stick around until the controlee closes the client_fd */
1422 exitval = 0;
1423 for (;!control_client_terminate;) {
1424 r = read(sock, &exitval, sizeof(exitval));
1425 if (r == 0) {
1426 debug2("Received EOF from master");
1427 break;
1428 }
1429 if (r > 0)
1430 debug2("Received exit status from master %d", exitval);
1431 if (r == -1 && errno != EINTR)
1432 fatal("%s: read %s", __func__, strerror(errno));
1433 }
1434
1435 if (control_client_terminate)
1436 debug2("Exiting on signal %d", control_client_terminate);
1437
1438 close(sock);
1439
1440 leave_raw_mode();
1441
1442 if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
1443 fprintf(stderr, "Connection to master closed.\r\n");
1444
1445 exit(exitval);
1446}