blob: 4a373356f6fa0c4fd053c194c36420e1a8f1f6ab [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * Ssh client program. This program can be used to log into a remote machine.
6 * The software supports strong authentication, encryption, and forwarding
7 * of X11, TCP/IP, and authentication connections.
8 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
14 *
15 * Copyright (c) 1999 Niels Provos. All rights reserved.
Darren Tucker0a118da2003-10-15 15:54:32 +100016 * Copyright (c) 2000, 2001, 2002, 2003 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110017 *
18 * Modified to work with SSL by Niels Provos <provos@citi.umich.edu>
19 * in Canada (German citizen).
20 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110040 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
42#include "includes.h"
Damien Miller3ec54c72006-03-15 11:30:13 +110043RCSID("$OpenBSD: ssh.c,v 1.261 2006/02/12 06:45:34 djm Exp $");
Damien Millercd4223c2006-03-15 11:22:47 +110044
45#include <sys/resource.h>
Damien Miller17e91c02006-03-15 11:28:34 +110046#include <sys/ioctl.h>
Damien Miller03e20032006-03-15 11:16:59 +110047
48#include <paths.h>
Damien Millereba71ba2000-04-29 23:57:08 +100049
50#include <openssl/evp.h>
Damien Miller0bc1bd82000-11-13 22:57:25 +110051#include <openssl/err.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000054#include "ssh1.h"
Damien Miller1383bd82000-04-06 12:32:37 +100055#include "ssh2.h"
56#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000057#include "cipher.h"
58#include "xmalloc.h"
59#include "packet.h"
60#include "buffer.h"
Damien Miller0e220db2004-06-15 10:34:08 +100061#include "bufaux.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000062#include "channels.h"
Damien Millereba71ba2000-04-29 23:57:08 +100063#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100064#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100065#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000066#include "pathnames.h"
Damien Miller0e220db2004-06-15 10:34:08 +100067#include "dispatch.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +000068#include "clientloop.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000069#include "log.h"
70#include "readconf.h"
71#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000072#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000073#include "kex.h"
74#include "mac.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +100075#include "sshpty.h"
Darren Tucker46bc0752004-05-02 22:11:30 +100076#include "match.h"
Damien Miller0e220db2004-06-15 10:34:08 +100077#include "msg.h"
78#include "monitor_fdpass.h"
Darren Tucker25f60a72004-08-15 17:23:34 +100079#include "uidswap.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080
Ben Lindstromc5b68002001-07-04 04:52:03 +000081#ifdef SMARTCARD
Ben Lindstromc5b68002001-07-04 04:52:03 +000082#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000083#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +000084
Damien Miller3f905871999-11-15 17:10:57 +110085extern char *__progname;
Damien Miller3f905871999-11-15 17:10:57 +110086
Damien Miller95def091999-11-25 00:26:21 +110087/* Flag indicating whether debug mode is on. This can be set on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088int debug_flag = 0;
89
Damien Miller78928792000-04-12 20:17:38 +100090/* Flag indicating whether a tty should be allocated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091int tty_flag = 0;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +000092int no_tty_flag = 0;
93int force_tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094
Damien Miller1383bd82000-04-06 12:32:37 +100095/* don't exec a shell */
96int no_shell_flag = 0;
Damien Miller1383bd82000-04-06 12:32:37 +100097
Damien Miller5428f641999-11-25 11:54:57 +110098/*
99 * Flag indicating that nothing should be read from stdin. This can be set
100 * on the command line.
101 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102int stdin_null_flag = 0;
103
Damien Miller5428f641999-11-25 11:54:57 +1100104/*
105 * Flag indicating that ssh should fork after authentication. This is useful
Ben Lindstromf666fec2002-06-06 19:51:58 +0000106 * so that the passphrase can be entered manually, and then ssh goes to the
Damien Miller5428f641999-11-25 11:54:57 +1100107 * background.
108 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109int fork_after_authentication_flag = 0;
110
Damien Miller5428f641999-11-25 11:54:57 +1100111/*
112 * General data structure for command line options and options configurable
113 * in configuration files. See readconf.h.
114 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115Options options;
116
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000117/* optional user configfile */
118char *config = NULL;
119
Damien Miller5428f641999-11-25 11:54:57 +1100120/*
121 * Name of the host we are connecting to. This is the name given on the
122 * command line, or the HostName specified for the user-supplied name in a
123 * configuration file.
124 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125char *host;
126
127/* socket address the host resolves to */
Damien Miller34132e52000-01-14 15:45:46 +1100128struct sockaddr_storage hostaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000130/* Private host keys. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000131Sensitive sensitive_data;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132
133/* Original real UID. */
134uid_t original_real_uid;
Ben Lindstromf9c48842002-06-11 16:37:51 +0000135uid_t original_effective_uid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136
Damien Miller1383bd82000-04-06 12:32:37 +1000137/* command to be executed */
138Buffer command;
139
Damien Miller832562e2001-01-30 09:30:01 +1100140/* Should we execute a command or invoke a subsystem? */
141int subsystem_flag = 0;
142
Damien Miller2797f7f2002-04-23 21:09:44 +1000143/* # of replies received for global requests */
144static int client_global_request_id = 0;
145
Damien Miller8c4e18a2002-09-19 12:05:02 +1000146/* pid of proxycommand child process */
147pid_t proxy_command_pid = 0;
148
Damien Miller0e220db2004-06-15 10:34:08 +1000149/* fd to control socket */
150int control_fd = -1;
151
Darren Tucker7ebfc102004-11-07 20:06:19 +1100152/* Multiplexing control command */
Darren Tucker0814d312005-06-01 23:08:51 +1000153static u_int mux_command = 0;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100154
Damien Miller0e220db2004-06-15 10:34:08 +1000155/* Only used in control client mode */
156volatile sig_atomic_t control_client_terminate = 0;
157u_int control_server_pid = 0;
158
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000159/* Prints a help message to the user. This function never returns. */
160
Ben Lindstrombba81212001-06-25 05:01:22 +0000161static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000162usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000163{
Damien Miller50955102004-03-22 09:34:58 +1100164 fprintf(stderr,
Darren Tucker9c6bf322004-12-03 14:10:19 +1100165"usage: ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n"
Darren Tucker895d6982005-10-03 18:18:05 +1000166" [-D [bind_address:]port] [-e escape_char] [-F configfile]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100167" [-i identity_file] [-L [bind_address:]port:host:hostport]\n"
Damien Millerf91ee4c2005-03-01 21:24:33 +1100168" [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100169" [-R [bind_address:]port:host:hostport] [-S ctl_path]\n"
Damien Millerd27b9472005-12-13 19:29:02 +1100170" [-w tunnel:tunnel] [user@]hostname [command]\n"
Damien Miller50955102004-03-22 09:34:58 +1100171 );
Darren Tuckere9a9b712005-12-20 16:15:51 +1100172 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173}
174
Ben Lindstrombba81212001-06-25 05:01:22 +0000175static int ssh_session(void);
176static int ssh_session2(void);
177static void load_public_identity_files(void);
Damien Miller0e220db2004-06-15 10:34:08 +1000178static void control_client(const char *path);
Damien Miller1383bd82000-04-06 12:32:37 +1000179
Damien Miller95def091999-11-25 00:26:21 +1100180/*
181 * Main program for the ssh client.
182 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000183int
184main(int ac, char **av)
185{
Ben Lindstrom24157572002-06-12 16:09:39 +0000186 int i, opt, exit_status;
Damien Miller9836cf82003-12-17 16:30:06 +1100187 char *p, *cp, *line, buf[256];
Damien Miller95def091999-11-25 00:26:21 +1100188 struct stat st;
Ben Lindstrom086cf212001-03-05 05:56:40 +0000189 struct passwd *pw;
Damien Miller1383bd82000-04-06 12:32:37 +1000190 int dummy;
Ben Lindstrom5ccf63a2001-09-24 20:00:10 +0000191 extern int optind, optreset;
Damien Miller4f8e6692001-07-14 13:22:53 +1000192 extern char *optarg;
Damien Miller9651fe62005-06-26 08:55:25 +1000193 struct servent *sp;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100194 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195
Darren Tuckerce321d82005-10-03 18:11:24 +1000196 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
197 sanitise_stdfd();
198
Damien Miller59d3d5b2003-08-22 09:34:41 +1000199 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000200 init_rng();
201
Damien Miller5428f641999-11-25 11:54:57 +1100202 /*
203 * Save the original real uid. It will be needed later (uid-swapping
204 * may clobber the real uid).
205 */
Damien Miller95def091999-11-25 00:26:21 +1100206 original_real_uid = getuid();
207 original_effective_uid = geteuid();
Damien Millera8e06ce2003-11-21 23:48:55 +1100208
Damien Miller50b9a602002-09-04 16:50:06 +1000209 /*
210 * Use uid-swapping to give up root privileges for the duration of
211 * option processing. We will re-instantiate the rights when we are
212 * ready to create the privileged port, and will permanently drop
213 * them when the port has been created (actually, when the connection
214 * has been made, as we may need to create the port several times).
215 */
216 PRIV_END;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000217
Damien Miller05dd7952000-10-01 00:42:48 +1100218#ifdef HAVE_SETRLIMIT
Damien Miller95def091999-11-25 00:26:21 +1100219 /* If we are installed setuid root be careful to not drop core. */
220 if (original_real_uid != original_effective_uid) {
221 struct rlimit rlim;
222 rlim.rlim_cur = rlim.rlim_max = 0;
223 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
224 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000225 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100226#endif
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000227 /* Get user data. */
228 pw = getpwuid(original_real_uid);
229 if (!pw) {
Damien Miller996acd22003-04-09 20:59:48 +1000230 logit("You don't exist, go away!");
Darren Tuckere9a9b712005-12-20 16:15:51 +1100231 exit(255);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000232 }
233 /* Take a copy of the returned structure. */
234 pw = pwcopy(pw);
235
Damien Miller5428f641999-11-25 11:54:57 +1100236 /*
Damien Miller5428f641999-11-25 11:54:57 +1100237 * Set our umask to something reasonable, as some files are created
238 * with the default umask. This will make them world-readable but
239 * writable only by the owner, which is ok for all files for which we
240 * don't set the modes explicitly.
241 */
Damien Miller95def091999-11-25 00:26:21 +1100242 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000243
Damien Miller95def091999-11-25 00:26:21 +1100244 /* Initialize option structure to indicate that no values have been set. */
245 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000246
Damien Miller95def091999-11-25 00:26:21 +1100247 /* Parse command-line arguments. */
248 host = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000249
Damien Millerf4614452001-07-14 12:18:10 +1000250again:
251 while ((opt = getopt(ac, av,
Damien Millerd27b9472005-12-13 19:29:02 +1100252 "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 +1100253 switch (opt) {
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000254 case '1':
255 options.protocol = SSH_PROTO_1;
256 break;
Damien Miller4af51302000-04-16 11:18:38 +1000257 case '2':
258 options.protocol = SSH_PROTO_2;
259 break;
Damien Miller34132e52000-01-14 15:45:46 +1100260 case '4':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000261 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100262 break;
Damien Miller34132e52000-01-14 15:45:46 +1100263 case '6':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000264 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100265 break;
Damien Miller95def091999-11-25 00:26:21 +1100266 case 'n':
267 stdin_null_flag = 1;
268 break;
Damien Miller95def091999-11-25 00:26:21 +1100269 case 'f':
270 fork_after_authentication_flag = 1;
271 stdin_null_flag = 1;
272 break;
Damien Miller95def091999-11-25 00:26:21 +1100273 case 'x':
274 options.forward_x11 = 0;
275 break;
Damien Miller95def091999-11-25 00:26:21 +1100276 case 'X':
277 options.forward_x11 = 1;
278 break;
Darren Tucker0a118da2003-10-15 15:54:32 +1000279 case 'Y':
280 options.forward_x11 = 1;
281 options.forward_x11_trusted = 1;
282 break;
Damien Miller95def091999-11-25 00:26:21 +1100283 case 'g':
284 options.gateway_ports = 1;
285 break;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100286 case 'O':
287 if (strcmp(optarg, "check") == 0)
288 mux_command = SSHMUX_COMMAND_ALIVE_CHECK;
289 else if (strcmp(optarg, "exit") == 0)
290 mux_command = SSHMUX_COMMAND_TERMINATE;
291 else
292 fatal("Invalid multiplex command.");
293 break;
Damien Miller147bba32002-09-04 16:46:06 +1000294 case 'P': /* deprecated */
Damien Miller95def091999-11-25 00:26:21 +1100295 options.use_privileged_port = 0;
296 break;
Damien Miller95def091999-11-25 00:26:21 +1100297 case 'a':
298 options.forward_agent = 0;
299 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000300 case 'A':
301 options.forward_agent = 1;
302 break;
Damien Miller95def091999-11-25 00:26:21 +1100303 case 'k':
Damien Millere0113cc2003-11-24 13:10:09 +1100304 options.gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100305 break;
Damien Miller95def091999-11-25 00:26:21 +1100306 case 'i':
307 if (stat(optarg, &st) < 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000308 fprintf(stderr, "Warning: Identity file %s "
Damien Miller3eb48b62005-03-01 21:15:46 +1100309 "not accessible: %s.\n", optarg,
310 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100311 break;
312 }
Damien Millerf4614452001-07-14 12:18:10 +1000313 if (options.num_identity_files >=
314 SSH_MAX_IDENTITY_FILES)
315 fatal("Too many identity files specified "
316 "(max %d)", SSH_MAX_IDENTITY_FILES);
317 options.identity_files[options.num_identity_files++] =
318 xstrdup(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100319 break;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000320 case 'I':
321#ifdef SMARTCARD
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000322 options.smartcard_device = xstrdup(optarg);
Ben Lindstrombcc18082001-08-06 21:59:25 +0000323#else
Ben Lindstromc5b68002001-07-04 04:52:03 +0000324 fprintf(stderr, "no support for smartcards.\n");
Ben Lindstrombcc18082001-08-06 21:59:25 +0000325#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000326 break;
Damien Miller95def091999-11-25 00:26:21 +1100327 case 't':
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000328 if (tty_flag)
329 force_tty_flag = 1;
Damien Miller95def091999-11-25 00:26:21 +1100330 tty_flag = 1;
331 break;
Damien Miller95def091999-11-25 00:26:21 +1100332 case 'v':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000333 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100334 debug_flag = 1;
335 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000336 } else {
337 if (options.log_level < SYSLOG_LEVEL_DEBUG3)
338 options.log_level++;
Damien Millere4340be2000-09-16 13:29:08 +1100339 break;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000340 }
Darren Tuckere9bf9842004-11-05 20:05:32 +1100341 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100342 case 'V':
Damien Miller0c889cd2004-03-22 09:36:00 +1100343 fprintf(stderr, "%s, %s\n",
Damien Miller2aa6d3c2004-09-12 16:53:04 +1000344 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
Damien Miller95def091999-11-25 00:26:21 +1100345 if (opt == 'V')
346 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100347 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100348 case 'w':
Damien Miller7b58e802005-12-13 19:33:19 +1100349 if (options.tun_open == -1)
350 options.tun_open = SSH_TUNMODE_DEFAULT;
Damien Millerd27b9472005-12-13 19:29:02 +1100351 options.tun_local = a2tun(optarg, &options.tun_remote);
Damien Miller7b58e802005-12-13 19:33:19 +1100352 if (options.tun_local == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100353 fprintf(stderr, "Bad tun device '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100354 exit(255);
Damien Millerd27b9472005-12-13 19:29:02 +1100355 }
356 break;
Damien Miller95def091999-11-25 00:26:21 +1100357 case 'q':
358 options.log_level = SYSLOG_LEVEL_QUIET;
359 break;
Damien Miller95def091999-11-25 00:26:21 +1100360 case 'e':
361 if (optarg[0] == '^' && optarg[2] == 0 &&
Damien Millerf4614452001-07-14 12:18:10 +1000362 (u_char) optarg[1] >= 64 &&
363 (u_char) optarg[1] < 128)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000364 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100365 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000366 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100367 else if (strcmp(optarg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000368 options.escape_char = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100369 else {
Damien Millerf4614452001-07-14 12:18:10 +1000370 fprintf(stderr, "Bad escape character '%s'.\n",
371 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100372 exit(255);
Damien Miller95def091999-11-25 00:26:21 +1100373 }
374 break;
Damien Miller95def091999-11-25 00:26:21 +1100375 case 'c':
Damien Millereba71ba2000-04-29 23:57:08 +1000376 if (ciphers_valid(optarg)) {
377 /* SSH2 only */
378 options.ciphers = xstrdup(optarg);
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000379 options.cipher = SSH_CIPHER_INVALID;
Damien Millereba71ba2000-04-29 23:57:08 +1000380 } else {
381 /* SSH1 only */
Damien Millere39cacc2000-11-29 12:18:44 +1100382 options.cipher = cipher_number(optarg);
383 if (options.cipher == -1) {
Damien Millerf4614452001-07-14 12:18:10 +1000384 fprintf(stderr,
385 "Unknown cipher type '%s'\n",
386 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100387 exit(255);
Damien Millereba71ba2000-04-29 23:57:08 +1000388 }
Damien Millerf4614452001-07-14 12:18:10 +1000389 if (options.cipher == SSH_CIPHER_3DES)
Damien Millere39cacc2000-11-29 12:18:44 +1100390 options.ciphers = "3des-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000391 else if (options.cipher == SSH_CIPHER_BLOWFISH)
Damien Millere39cacc2000-11-29 12:18:44 +1100392 options.ciphers = "blowfish-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000393 else
Damien Millere39cacc2000-11-29 12:18:44 +1100394 options.ciphers = (char *)-1;
Damien Miller95def091999-11-25 00:26:21 +1100395 }
396 break;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000397 case 'm':
398 if (mac_valid(optarg))
399 options.macs = xstrdup(optarg);
400 else {
Damien Millerf4614452001-07-14 12:18:10 +1000401 fprintf(stderr, "Unknown mac type '%s'\n",
402 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100403 exit(255);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000404 }
405 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000406 case 'M':
Damien Millerd14b1e72005-06-16 13:19:41 +1000407 if (options.control_master == SSHCTL_MASTER_YES)
408 options.control_master = SSHCTL_MASTER_ASK;
409 else
410 options.control_master = SSHCTL_MASTER_YES;
Damien Miller0e220db2004-06-15 10:34:08 +1000411 break;
Damien Miller95def091999-11-25 00:26:21 +1100412 case 'p':
Ben Lindstrom19066a12001-04-12 23:39:26 +0000413 options.port = a2port(optarg);
414 if (options.port == 0) {
Ben Lindstrom146edb92001-04-11 23:06:28 +0000415 fprintf(stderr, "Bad port '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100416 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000417 }
Damien Miller95def091999-11-25 00:26:21 +1100418 break;
Damien Miller95def091999-11-25 00:26:21 +1100419 case 'l':
420 options.user = optarg;
421 break;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000422
Damien Miller95def091999-11-25 00:26:21 +1100423 case 'L':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100424 if (parse_forward(&fwd, optarg))
425 add_local_forward(&options, &fwd);
426 else {
Damien Millerf4614452001-07-14 12:18:10 +1000427 fprintf(stderr,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100428 "Bad local forwarding specification '%s'\n",
Damien Millerf4614452001-07-14 12:18:10 +1000429 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100430 exit(255);
Ben Lindstrom1a174712001-09-12 17:56:15 +0000431 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100432 break;
433
434 case 'R':
435 if (parse_forward(&fwd, optarg)) {
436 add_remote_forward(&options, &fwd);
437 } else {
438 fprintf(stderr,
439 "Bad remote forwarding specification "
440 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100441 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100442 }
Damien Miller95def091999-11-25 00:26:21 +1100443 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000444
445 case 'D':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100446 cp = p = xstrdup(optarg);
447 memset(&fwd, '\0', sizeof(fwd));
448 fwd.connect_host = "socks";
449 if ((fwd.listen_host = hpdelim(&cp)) == NULL) {
450 fprintf(stderr, "Bad dynamic forwarding "
451 "specification '%.100s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100452 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100453 }
454 if (cp != NULL) {
455 fwd.listen_port = a2port(cp);
456 fwd.listen_host = cleanhostname(fwd.listen_host);
457 } else {
458 fwd.listen_port = a2port(fwd.listen_host);
Damien Millerbe1045d2005-08-12 22:10:56 +1000459 fwd.listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100460 }
461
462 if (fwd.listen_port == 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000463 fprintf(stderr, "Bad dynamic port '%s'\n",
464 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100465 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000466 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100467 add_local_forward(&options, &fwd);
468 xfree(p);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000469 break;
470
Damien Miller95def091999-11-25 00:26:21 +1100471 case 'C':
472 options.compression = 1;
473 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000474 case 'N':
475 no_shell_flag = 1;
476 no_tty_flag = 1;
477 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000478 case 'T':
479 no_tty_flag = 1;
480 break;
Damien Miller95def091999-11-25 00:26:21 +1100481 case 'o':
482 dummy = 1;
Damien Miller9836cf82003-12-17 16:30:06 +1100483 line = xstrdup(optarg);
Damien Millerf4614452001-07-14 12:18:10 +1000484 if (process_config_line(&options, host ? host : "",
Damien Miller9836cf82003-12-17 16:30:06 +1100485 line, "command-line", 0, &dummy) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100486 exit(255);
Damien Miller9836cf82003-12-17 16:30:06 +1100487 xfree(line);
Damien Miller95def091999-11-25 00:26:21 +1100488 break;
Damien Miller832562e2001-01-30 09:30:01 +1100489 case 's':
490 subsystem_flag = 1;
491 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000492 case 'S':
493 if (options.control_path != NULL)
494 free(options.control_path);
495 options.control_path = xstrdup(optarg);
Damien Miller0e220db2004-06-15 10:34:08 +1000496 break;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000497 case 'b':
498 options.bind_address = optarg;
499 break;
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000500 case 'F':
501 config = optarg;
502 break;
Damien Miller95def091999-11-25 00:26:21 +1100503 default:
504 usage();
505 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000506 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000507
Damien Millerf4614452001-07-14 12:18:10 +1000508 ac -= optind;
509 av += optind;
510
511 if (ac > 0 && !host && **av != '-') {
Ben Lindstromc276c122002-12-23 02:14:51 +0000512 if (strrchr(*av, '@')) {
Damien Millerf4614452001-07-14 12:18:10 +1000513 p = xstrdup(*av);
Ben Lindstromc276c122002-12-23 02:14:51 +0000514 cp = strrchr(p, '@');
Damien Millerf4614452001-07-14 12:18:10 +1000515 if (cp == NULL || cp == p)
516 usage();
517 options.user = p;
518 *cp = '\0';
519 host = ++cp;
520 } else
521 host = *av;
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000522 if (ac > 1) {
523 optind = optreset = 1;
Damien Millerf4614452001-07-14 12:18:10 +1000524 goto again;
525 }
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000526 ac--, av++;
Damien Millerf4614452001-07-14 12:18:10 +1000527 }
528
Damien Miller95def091999-11-25 00:26:21 +1100529 /* Check that we got a host name. */
530 if (!host)
531 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000532
Damien Millercb5e44a2000-09-29 12:12:36 +1100533 SSLeay_add_all_algorithms();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100534 ERR_load_crypto_strings();
Damien Millercb5e44a2000-09-29 12:12:36 +1100535
Damien Miller95def091999-11-25 00:26:21 +1100536 /* Initialize the command to execute on remote host. */
537 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000538
Damien Miller5428f641999-11-25 11:54:57 +1100539 /*
540 * Save the command to execute on the remote host in a buffer. There
541 * is no limit on the length of the command, except by the maximum
542 * packet size. Also sets the tty flag if there is no command.
543 */
Damien Millerf4614452001-07-14 12:18:10 +1000544 if (!ac) {
Damien Miller95def091999-11-25 00:26:21 +1100545 /* No command specified - execute shell on a tty. */
546 tty_flag = 1;
Damien Miller832562e2001-01-30 09:30:01 +1100547 if (subsystem_flag) {
Damien Millerf4614452001-07-14 12:18:10 +1000548 fprintf(stderr,
549 "You must specify a subsystem to invoke.\n");
Damien Miller832562e2001-01-30 09:30:01 +1100550 usage();
551 }
Damien Miller95def091999-11-25 00:26:21 +1100552 } else {
Damien Millerf4614452001-07-14 12:18:10 +1000553 /* A command has been specified. Store it into the buffer. */
554 for (i = 0; i < ac; i++) {
555 if (i)
Damien Miller95def091999-11-25 00:26:21 +1100556 buffer_append(&command, " ", 1);
557 buffer_append(&command, av[i], strlen(av[i]));
558 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000559 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000560
Damien Miller95def091999-11-25 00:26:21 +1100561 /* Cannot fork to background if no command. */
Damien Millercaf6dd62000-08-29 11:33:50 +1100562 if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
Damien Miller95def091999-11-25 00:26:21 +1100563 fatal("Cannot fork into background without a command to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000564
Damien Miller95def091999-11-25 00:26:21 +1100565 /* Allocate a tty by default if no command specified. */
566 if (buffer_len(&command) == 0)
567 tty_flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000568
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000569 /* Force no tty */
Ben Lindstromc72745a2000-12-02 19:03:54 +0000570 if (no_tty_flag)
571 tty_flag = 0;
Damien Miller95def091999-11-25 00:26:21 +1100572 /* Do not allocate a tty if stdin is not a tty. */
Damien Millerddee5752005-05-26 12:05:05 +1000573 if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100574 if (tty_flag)
Damien Miller996acd22003-04-09 20:59:48 +1000575 logit("Pseudo-terminal will not be allocated because stdin is not a terminal.");
Damien Miller95def091999-11-25 00:26:21 +1100576 tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000577 }
Damien Miller1383bd82000-04-06 12:32:37 +1000578
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000579 /*
580 * Initialize "log" output. Since we are the client all output
581 * actually goes to stderr.
582 */
Ben Lindstrom2b646522001-04-12 16:16:57 +0000583 log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
584 SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000585
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000586 /*
587 * Read per-user configuration file. Ignore the system wide config
588 * file if the user specifies a config file on the command line.
589 */
590 if (config != NULL) {
Damien Miller914420f2004-04-20 20:14:07 +1000591 if (!read_config_file(config, host, &options, 0))
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000592 fatal("Can't open user config file %.100s: "
593 "%.100s", config, strerror(errno));
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000594 } else {
595 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
596 _PATH_SSH_USER_CONFFILE);
Damien Miller57a44762004-04-20 20:11:57 +1000597 (void)read_config_file(buf, host, &options, 1);
Ben Lindstrom83f07d12001-10-03 17:22:29 +0000598
599 /* Read systemwide configuration file after use config. */
Darren Tuckerfc959702004-07-17 16:12:08 +1000600 (void)read_config_file(_PATH_HOST_CONFIG_FILE, host,
Damien Miller57a44762004-04-20 20:11:57 +1000601 &options, 0);
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000602 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000603
Damien Miller95def091999-11-25 00:26:21 +1100604 /* Fill configuration defaults. */
605 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000606
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000607 channel_set_af(options.address_family);
608
Damien Miller95def091999-11-25 00:26:21 +1100609 /* reinit */
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000610 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000611
Damien Miller60bc5172001-03-19 09:38:15 +1100612 seed_rng();
613
Damien Miller95def091999-11-25 00:26:21 +1100614 if (options.user == NULL)
615 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000616
Damien Miller95def091999-11-25 00:26:21 +1100617 if (options.hostname != NULL)
618 host = options.hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000619
Darren Tucker3f521e22003-07-03 16:20:42 +1000620 /* force lowercase for hostkey matching */
621 if (options.host_key_alias != NULL) {
622 for (p = options.host_key_alias; *p; p++)
623 if (isupper(*p))
624 *p = tolower(*p);
625 }
626
Damien Miller7c71cc72005-06-26 08:56:31 +1000627 /* Get default port if port has not been set. */
628 if (options.port == 0) {
629 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
630 options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
631 }
632
Damien Miller9f1e33a2003-02-24 11:57:32 +1100633 if (options.proxy_command != NULL &&
634 strcmp(options.proxy_command, "none") == 0)
635 options.proxy_command = NULL;
Damien Miller8f74c8f2005-06-26 08:56:03 +1000636 if (options.control_path != NULL &&
637 strcmp(options.control_path, "none") == 0)
638 options.control_path = NULL;
Damien Miller9f1e33a2003-02-24 11:57:32 +1100639
Damien Miller0e220db2004-06-15 10:34:08 +1000640 if (options.control_path != NULL) {
Damien Miller3ec54c72006-03-15 11:30:13 +1100641 char me[NI_MAXHOST];
642
643 if (gethostname(me, sizeof(me)) == -1)
644 fatal("gethostname: %s", strerror(errno));
Damien Miller6476cad2005-06-16 13:18:34 +1000645 snprintf(buf, sizeof(buf), "%d", options.port);
646 cp = tilde_expand_filename(options.control_path,
647 original_real_uid);
648 options.control_path = percent_expand(cp, "p", buf, "h", host,
Damien Miller3ec54c72006-03-15 11:30:13 +1100649 "r", options.user, "l", me, (char *)NULL);
Damien Miller6476cad2005-06-16 13:18:34 +1000650 xfree(cp);
Damien Miller0e220db2004-06-15 10:34:08 +1000651 }
Darren Tucker0814d312005-06-01 23:08:51 +1000652 if (mux_command != 0 && options.control_path == NULL)
653 fatal("No ControlPath specified for \"-O\" command");
Damien Millerd14b1e72005-06-16 13:19:41 +1000654 if (options.control_path != NULL)
Damien Millerdadfd4d2005-05-26 12:07:13 +1000655 control_client(options.control_path);
Damien Miller0e220db2004-06-15 10:34:08 +1000656
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000657 /* Open a connection to the remote host. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000658 if (ssh_connect(host, &hostaddr, options.port,
659 options.address_family, options.connection_attempts,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000660#ifdef HAVE_CYGWIN
661 options.use_privileged_port,
662#else
Ben Lindstromf9c48842002-06-11 16:37:51 +0000663 original_effective_uid == 0 && options.use_privileged_port,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000664#endif
Ben Lindstromda394ca2002-06-12 16:11:12 +0000665 options.proxy_command) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100666 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000667
Damien Miller5428f641999-11-25 11:54:57 +1100668 /*
669 * If we successfully made the connection, load the host private key
670 * in case we will need it later for combined rsa-rhosts
671 * authentication. This must be done before releasing extra
672 * privileges, because the file is only readable by root.
Ben Lindstrom9e5bb572002-06-06 19:58:27 +0000673 * If we cannot access the private keys, load the public keys
674 * instead and try to execute the ssh-keysign helper instead.
Damien Miller5428f641999-11-25 11:54:57 +1100675 */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000676 sensitive_data.nkeys = 0;
677 sensitive_data.keys = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000678 sensitive_data.external_keysign = 0;
Ben Lindstrom24157572002-06-12 16:09:39 +0000679 if (options.rhosts_rsa_authentication ||
680 options.hostbased_authentication) {
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000681 sensitive_data.nkeys = 3;
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000682 sensitive_data.keys = xmalloc(sensitive_data.nkeys *
683 sizeof(Key));
Ben Lindstromf9c48842002-06-11 16:37:51 +0000684
685 PRIV_START;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000686 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
Ben Lindstromd0fca422001-03-26 13:44:06 +0000687 _PATH_HOST_KEY_FILE, "", NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000688 sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
689 _PATH_HOST_DSA_KEY_FILE, "", NULL);
690 sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
691 _PATH_HOST_RSA_KEY_FILE, "", NULL);
Ben Lindstromf9c48842002-06-11 16:37:51 +0000692 PRIV_END;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000693
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +0000694 if (options.hostbased_authentication == 1 &&
695 sensitive_data.keys[0] == NULL &&
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000696 sensitive_data.keys[1] == NULL &&
697 sensitive_data.keys[2] == NULL) {
698 sensitive_data.keys[1] = key_load_public(
699 _PATH_HOST_DSA_KEY_FILE, NULL);
700 sensitive_data.keys[2] = key_load_public(
701 _PATH_HOST_RSA_KEY_FILE, NULL);
702 sensitive_data.external_keysign = 1;
703 }
Damien Miller95def091999-11-25 00:26:21 +1100704 }
Damien Miller5428f641999-11-25 11:54:57 +1100705 /*
706 * Get rid of any extra privileges that we may have. We will no
707 * longer need them. Also, extra privileges could make it very hard
708 * to read identity files and other non-world-readable files from the
709 * user's home directory if it happens to be on a NFS volume where
710 * root is mapped to nobody.
711 */
Darren Tucker25f60a72004-08-15 17:23:34 +1000712 if (original_effective_uid == 0) {
713 PRIV_START;
714 permanently_set_uid(pw);
715 }
Damien Miller95def091999-11-25 00:26:21 +1100716
Damien Miller5428f641999-11-25 11:54:57 +1100717 /*
718 * Now that we are back to our own permissions, create ~/.ssh
Damien Miller788f2122005-11-05 15:14:59 +1100719 * directory if it doesn't already exist.
Damien Miller5428f641999-11-25 11:54:57 +1100720 */
Ben Lindstrom930b14a2001-08-15 23:19:21 +0000721 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 +1100722 if (stat(buf, &st) < 0)
Damien Millerbe484b52000-07-15 14:14:16 +1000723 if (mkdir(buf, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100724 error("Could not create directory '%.200s'.", buf);
725
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000726 /* load options.identity_files */
727 load_public_identity_files();
728
729 /* Expand ~ in known host file names. */
730 /* XXX mem-leaks: */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100731 options.system_hostfile =
732 tilde_expand_filename(options.system_hostfile, original_real_uid);
733 options.user_hostfile =
734 tilde_expand_filename(options.user_hostfile, original_real_uid);
735 options.system_hostfile2 =
736 tilde_expand_filename(options.system_hostfile2, original_real_uid);
737 options.user_hostfile2 =
738 tilde_expand_filename(options.user_hostfile2, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100739
Damien Miller07cd5892001-11-12 10:52:03 +1100740 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
741
Damien Miller95def091999-11-25 00:26:21 +1100742 /* Log into the remote system. This never returns if the login fails. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000743 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, pw);
Damien Miller95def091999-11-25 00:26:21 +1100744
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000745 /* We no longer need the private host keys. Clear them now. */
746 if (sensitive_data.nkeys != 0) {
747 for (i = 0; i < sensitive_data.nkeys; i++) {
748 if (sensitive_data.keys[i] != NULL) {
749 /* Destroys contents safely */
750 debug3("clear hostkey %d", i);
751 key_free(sensitive_data.keys[i]);
752 sensitive_data.keys[i] = NULL;
753 }
754 }
755 xfree(sensitive_data.keys);
756 }
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +0000757 for (i = 0; i < options.num_identity_files; i++) {
758 if (options.identity_files[i]) {
759 xfree(options.identity_files[i]);
760 options.identity_files[i] = NULL;
761 }
762 if (options.identity_keys[i]) {
763 key_free(options.identity_keys[i]);
764 options.identity_keys[i] = NULL;
765 }
766 }
Damien Miller95def091999-11-25 00:26:21 +1100767
Damien Miller1383bd82000-04-06 12:32:37 +1000768 exit_status = compat20 ? ssh_session2() : ssh_session();
769 packet_close();
Damien Miller8c4e18a2002-09-19 12:05:02 +1000770
Damien Miller0e220db2004-06-15 10:34:08 +1000771 if (options.control_path != NULL && control_fd != -1)
772 unlink(options.control_path);
773
Damien Miller8c4e18a2002-09-19 12:05:02 +1000774 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100775 * Send SIGHUP to proxy command if used. We don't wait() in
Damien Miller8c4e18a2002-09-19 12:05:02 +1000776 * case it hangs and instead rely on init to reap the child
777 */
778 if (proxy_command_pid > 1)
779 kill(proxy_command_pid, SIGHUP);
780
Damien Miller1383bd82000-04-06 12:32:37 +1000781 return exit_status;
782}
783
Ben Lindstrombba81212001-06-25 05:01:22 +0000784static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100785ssh_init_forwarding(void)
786{
Kevin Steves12057502001-02-05 14:54:34 +0000787 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100788 int i;
Kevin Steves12057502001-02-05 14:54:34 +0000789
Damien Miller0bc1bd82000-11-13 22:57:25 +1100790 /* Initiate local TCP/IP port forwardings. */
791 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100792 debug("Local connections to %.200s:%d forwarded to remote "
793 "address %.200s:%d",
Darren Tucker47eede72005-03-14 23:08:12 +1100794 (options.local_forwards[i].listen_host == NULL) ?
795 (options.gateway_ports ? "*" : "LOCALHOST") :
Damien Millerf91ee4c2005-03-01 21:24:33 +1100796 options.local_forwards[i].listen_host,
797 options.local_forwards[i].listen_port,
798 options.local_forwards[i].connect_host,
799 options.local_forwards[i].connect_port);
Damien Millerb16461c2002-01-22 23:29:22 +1100800 success += channel_setup_local_fwd_listener(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100801 options.local_forwards[i].listen_host,
802 options.local_forwards[i].listen_port,
803 options.local_forwards[i].connect_host,
804 options.local_forwards[i].connect_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100805 options.gateway_ports);
806 }
Kevin Steves12057502001-02-05 14:54:34 +0000807 if (i > 0 && success == 0)
808 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100809
810 /* Initiate remote TCP/IP port forwardings. */
811 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100812 debug("Remote connections from %.200s:%d forwarded to "
813 "local address %.200s:%d",
Damien Miller46d38de2005-07-17 17:02:09 +1000814 (options.remote_forwards[i].listen_host == NULL) ?
Damien Milleraa3bb102005-11-05 15:12:59 +1100815 "LOCALHOST" : options.remote_forwards[i].listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100816 options.remote_forwards[i].listen_port,
817 options.remote_forwards[i].connect_host,
818 options.remote_forwards[i].connect_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100819 channel_request_remote_forwarding(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100820 options.remote_forwards[i].listen_host,
821 options.remote_forwards[i].listen_port,
822 options.remote_forwards[i].connect_host,
823 options.remote_forwards[i].connect_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100824 }
825}
826
Ben Lindstrombba81212001-06-25 05:01:22 +0000827static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100828check_agent_present(void)
829{
830 if (options.forward_agent) {
Damien Miller788f2122005-11-05 15:14:59 +1100831 /* Clear agent forwarding if we don't have an agent. */
Damien Miller789e95d2002-09-12 09:52:46 +1000832 if (!ssh_agent_present())
Damien Miller0bc1bd82000-11-13 22:57:25 +1100833 options.forward_agent = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100834 }
835}
836
Ben Lindstrombba81212001-06-25 05:01:22 +0000837static int
Damien Miller1383bd82000-04-06 12:32:37 +1000838ssh_session(void)
839{
840 int type;
Damien Miller1383bd82000-04-06 12:32:37 +1000841 int interactive = 0;
842 int have_tty = 0;
843 struct winsize ws;
Damien Miller1383bd82000-04-06 12:32:37 +1000844 char *cp;
Damien Miller17e7ed02005-06-17 12:54:33 +1000845 const char *display;
Damien Miller1383bd82000-04-06 12:32:37 +1000846
Damien Miller95def091999-11-25 00:26:21 +1100847 /* Enable compression if requested. */
848 if (options.compression) {
849 debug("Requesting compression at level %d.", options.compression_level);
850
851 if (options.compression_level < 1 || options.compression_level > 9)
852 fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
853
854 /* Send the request. */
855 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
856 packet_put_int(options.compression_level);
857 packet_send();
858 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100859 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100860 if (type == SSH_SMSG_SUCCESS)
861 packet_start_compression(options.compression_level);
862 else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000863 logit("Warning: Remote host refused compression.");
Damien Miller95def091999-11-25 00:26:21 +1100864 else
865 packet_disconnect("Protocol error waiting for compression response.");
866 }
867 /* Allocate a pseudo tty if appropriate. */
868 if (tty_flag) {
869 debug("Requesting pty.");
870
871 /* Start the packet. */
872 packet_start(SSH_CMSG_REQUEST_PTY);
873
874 /* Store TERM in the packet. There is no limit on the
875 length of the string. */
876 cp = getenv("TERM");
877 if (!cp)
878 cp = "";
Ben Lindstrom664408d2001-06-09 01:42:01 +0000879 packet_put_cstring(cp);
Damien Miller95def091999-11-25 00:26:21 +1100880
881 /* Store window size in the packet. */
882 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
883 memset(&ws, 0, sizeof(ws));
884 packet_put_int(ws.ws_row);
885 packet_put_int(ws.ws_col);
886 packet_put_int(ws.ws_xpixel);
887 packet_put_int(ws.ws_ypixel);
888
889 /* Store tty modes in the packet. */
Ben Lindstromae8e2d32001-04-14 23:13:02 +0000890 tty_make_modes(fileno(stdin), NULL);
Damien Miller95def091999-11-25 00:26:21 +1100891
892 /* Send the packet, and wait for it to leave. */
893 packet_send();
894 packet_write_wait();
895
896 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100897 type = packet_read();
Damien Miller450a7a12000-03-26 13:04:51 +1000898 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100899 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +1000900 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000901 } else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000902 logit("Warning: Remote host failed or refused to allocate a pseudo tty.");
Damien Miller95def091999-11-25 00:26:21 +1100903 else
904 packet_disconnect("Protocol error waiting for pty request response.");
905 }
906 /* Request X11 forwarding if enabled and DISPLAY is set. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000907 display = getenv("DISPLAY");
908 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000909 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +1000910 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000911 client_x11_get_proto(display, options.xauth_location,
912 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +1000913 /* Request forwarding with authentication spoofing. */
Damien Miller95def091999-11-25 00:26:21 +1100914 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +1000915 x11_request_forwarding_with_spoofing(0, display, proto, data);
Damien Miller95def091999-11-25 00:26:21 +1100916
917 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100918 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100919 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100920 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +1000921 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller996acd22003-04-09 20:59:48 +1000922 logit("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +1000923 } else {
Damien Miller95def091999-11-25 00:26:21 +1100924 packet_disconnect("Protocol error waiting for X11 forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +1000925 }
Damien Miller95def091999-11-25 00:26:21 +1100926 }
927 /* Tell the packet module whether this is an interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000928 packet_set_interactive(interactive);
Damien Miller95def091999-11-25 00:26:21 +1100929
930 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100931 check_agent_present();
932
Damien Miller95def091999-11-25 00:26:21 +1100933 if (options.forward_agent) {
934 debug("Requesting authentication agent forwarding.");
935 auth_request_forwarding();
936
937 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100938 type = packet_read();
Damien Miller48b03fc2002-01-22 23:11:40 +1100939 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100940 if (type != SSH_SMSG_SUCCESS)
Damien Miller996acd22003-04-09 20:59:48 +1000941 logit("Warning: Remote host denied authentication agent forwarding.");
Damien Miller95def091999-11-25 00:26:21 +1100942 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000943
Damien Miller0bc1bd82000-11-13 22:57:25 +1100944 /* Initiate port forwardings. */
945 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000946
Damien Miller5428f641999-11-25 11:54:57 +1100947 /* If requested, let ssh continue in the background. */
Damien Miller4af51302000-04-16 11:18:38 +1000948 if (fork_after_authentication_flag)
Damien Miller5428f641999-11-25 11:54:57 +1100949 if (daemon(1, 1) < 0)
950 fatal("daemon() failed: %.200s", strerror(errno));
951
952 /*
953 * If a command was specified on the command line, execute the
954 * command now. Otherwise request the server to start a shell.
955 */
Damien Miller95def091999-11-25 00:26:21 +1100956 if (buffer_len(&command) > 0) {
957 int len = buffer_len(&command);
958 if (len > 900)
959 len = 900;
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100960 debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
Damien Miller95def091999-11-25 00:26:21 +1100961 packet_start(SSH_CMSG_EXEC_CMD);
962 packet_put_string(buffer_ptr(&command), buffer_len(&command));
963 packet_send();
964 packet_write_wait();
965 } else {
966 debug("Requesting shell.");
967 packet_start(SSH_CMSG_EXEC_SHELL);
968 packet_send();
969 packet_write_wait();
970 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000971
Damien Miller95def091999-11-25 00:26:21 +1100972 /* Enter the interactive session. */
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000973 return client_loop(have_tty, tty_flag ?
974 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
Damien Miller1383bd82000-04-06 12:32:37 +1000975}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000976
Ben Lindstrombba81212001-06-25 05:01:22 +0000977static void
Damien Miller0e220db2004-06-15 10:34:08 +1000978ssh_subsystem_reply(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000979{
980 int id, len;
981
982 id = packet_get_int();
983 len = buffer_len(&command);
Ben Lindstrom4040fe12001-03-05 06:52:57 +0000984 if (len > 900)
985 len = 900;
Damien Miller48b03fc2002-01-22 23:11:40 +1100986 packet_check_eom();
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000987 if (type == SSH2_MSG_CHANNEL_FAILURE)
988 fatal("Request for subsystem '%.*s' failed on channel %d",
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100989 len, (u_char *)buffer_ptr(&command), id);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000990}
991
Damien Miller2797f7f2002-04-23 21:09:44 +1000992void
Damien Miller509b0102003-12-17 16:33:10 +1100993client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt)
Damien Miller2797f7f2002-04-23 21:09:44 +1000994{
995 int i;
996
997 i = client_global_request_id++;
Damien Miller509b0102003-12-17 16:33:10 +1100998 if (i >= options.num_remote_forwards)
Damien Miller2797f7f2002-04-23 21:09:44 +1000999 return;
Damien Miller2797f7f2002-04-23 21:09:44 +10001000 debug("remote forward %s for: listen %d, connect %s:%d",
1001 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
Damien Millerf91ee4c2005-03-01 21:24:33 +11001002 options.remote_forwards[i].listen_port,
1003 options.remote_forwards[i].connect_host,
1004 options.remote_forwards[i].connect_port);
Damien Miller2797f7f2002-04-23 21:09:44 +10001005 if (type == SSH2_MSG_REQUEST_FAILURE)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001006 logit("Warning: remote port forwarding failed for listen "
1007 "port %d", options.remote_forwards[i].listen_port);
Damien Miller2797f7f2002-04-23 21:09:44 +10001008}
1009
Damien Miller0e220db2004-06-15 10:34:08 +10001010static void
1011ssh_control_listener(void)
1012{
1013 struct sockaddr_un addr;
1014 mode_t old_umask;
Damien Miller07b6ff12004-06-15 11:14:45 +10001015 int addr_len;
1016
Damien Millerd14b1e72005-06-16 13:19:41 +10001017 if (options.control_path == NULL ||
1018 options.control_master == SSHCTL_MASTER_NO)
Damien Miller0e220db2004-06-15 10:34:08 +10001019 return;
1020
Damien Millerd14b1e72005-06-16 13:19:41 +10001021 debug("setting up multiplex master socket");
1022
Damien Miller0e220db2004-06-15 10:34:08 +10001023 memset(&addr, '\0', sizeof(addr));
1024 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001025 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001026 strlen(options.control_path) + 1;
1027
1028 if (strlcpy(addr.sun_path, options.control_path,
1029 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1030 fatal("ControlPath too long");
1031
1032 if ((control_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001033 fatal("%s socket(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001034
1035 old_umask = umask(0177);
Damien Miller07b6ff12004-06-15 11:14:45 +10001036 if (bind(control_fd, (struct sockaddr*)&addr, addr_len) == -1) {
Damien Miller0e220db2004-06-15 10:34:08 +10001037 control_fd = -1;
Damien Miller4f10e252005-05-04 15:33:09 +10001038 if (errno == EINVAL || errno == EADDRINUSE)
Damien Miller0e220db2004-06-15 10:34:08 +10001039 fatal("ControlSocket %s already exists",
1040 options.control_path);
1041 else
Damien Miller15d72a02005-11-05 15:07:33 +11001042 fatal("%s bind(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001043 }
1044 umask(old_umask);
1045
1046 if (listen(control_fd, 64) == -1)
Damien Miller15d72a02005-11-05 15:07:33 +11001047 fatal("%s listen(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001048
1049 set_nonblock(control_fd);
1050}
1051
Ben Lindstromf558cf62001-09-20 23:13:49 +00001052/* request pty/x11/agent/tcpfwd/shell for channel */
Ben Lindstrombba81212001-06-25 05:01:22 +00001053static void
Ben Lindstromf558cf62001-09-20 23:13:49 +00001054ssh_session2_setup(int id, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +10001055{
Damien Miller3756dce2004-06-18 01:17:29 +10001056 extern char **environ;
Damien Miller17e7ed02005-06-17 12:54:33 +10001057 const char *display;
Damien Miller0e220db2004-06-15 10:34:08 +10001058 int interactive = tty_flag;
Damien Miller17e7ed02005-06-17 12:54:33 +10001059
Damien Miller46d38de2005-07-17 17:02:09 +10001060 display = getenv("DISPLAY");
Damien Miller17e7ed02005-06-17 12:54:33 +10001061 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001062 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001063 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001064 client_x11_get_proto(display, options.xauth_location,
1065 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001066 /* Request forwarding with authentication spoofing. */
1067 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +10001068 x11_request_forwarding_with_spoofing(id, display, proto, data);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001069 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001070 /* XXX wait for reply */
1071 }
1072
Damien Miller0bc1bd82000-11-13 22:57:25 +11001073 check_agent_present();
1074 if (options.forward_agent) {
1075 debug("Requesting authentication agent forwarding.");
1076 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1077 packet_send();
1078 }
1079
Damien Miller7b58e802005-12-13 19:33:19 +11001080 if (options.tun_open != SSH_TUNMODE_NO) {
Damien Millerd27b9472005-12-13 19:29:02 +11001081 Channel *c;
1082 int fd;
1083
1084 debug("Requesting tun.");
Damien Miller7b58e802005-12-13 19:33:19 +11001085 if ((fd = tun_open(options.tun_local,
1086 options.tun_open)) >= 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11001087 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1088 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1089 0, "tun", 1);
1090 c->datagram = 1;
Damien Miller598bbc22005-12-31 16:33:36 +11001091#if defined(SSH_TUN_FILTER)
1092 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1093 channel_register_filter(c->self, sys_tun_infilter,
1094 sys_tun_outfilter);
1095#endif
Damien Millerd27b9472005-12-13 19:29:02 +11001096 packet_start(SSH2_MSG_CHANNEL_OPEN);
1097 packet_put_cstring("tun@openssh.com");
1098 packet_put_int(c->self);
1099 packet_put_int(c->local_window_max);
1100 packet_put_int(c->local_maxpacket);
Damien Miller7b58e802005-12-13 19:33:19 +11001101 packet_put_int(options.tun_open);
Damien Millerd27b9472005-12-13 19:29:02 +11001102 packet_put_int(options.tun_remote);
1103 packet_send();
1104 }
1105 }
1106
Damien Miller0e220db2004-06-15 10:34:08 +10001107 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
Damien Miller3756dce2004-06-18 01:17:29 +10001108 NULL, fileno(stdin), &command, environ, &ssh_subsystem_reply);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001109
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001110 packet_set_interactive(interactive);
Damien Miller1383bd82000-04-06 12:32:37 +10001111}
1112
Ben Lindstromf558cf62001-09-20 23:13:49 +00001113/* open new channel for a session */
Ben Lindstrombba81212001-06-25 05:01:22 +00001114static int
Ben Lindstromf558cf62001-09-20 23:13:49 +00001115ssh_session2_open(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001116{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001117 Channel *c;
1118 int window, packetmax, in, out, err;
Damien Millerad833b32000-08-23 10:46:23 +10001119
Damien Millercaf6dd62000-08-29 11:33:50 +11001120 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001121 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001122 } else {
1123 in = dup(STDIN_FILENO);
1124 }
1125 out = dup(STDOUT_FILENO);
1126 err = dup(STDERR_FILENO);
1127
1128 if (in < 0 || out < 0 || err < 0)
1129 fatal("dup() in/out/err failed");
1130
Damien Miller69b69aa2000-10-28 14:19:58 +11001131 /* enable nonblocking unless tty */
1132 if (!isatty(in))
1133 set_nonblock(in);
1134 if (!isatty(out))
1135 set_nonblock(out);
1136 if (!isatty(err))
1137 set_nonblock(err);
1138
Damien Millere4340be2000-09-16 13:29:08 +11001139 window = CHAN_SES_WINDOW_DEFAULT;
1140 packetmax = CHAN_SES_PACKET_DEFAULT;
Damien Miller19a59452002-02-19 15:20:57 +11001141 if (tty_flag) {
1142 window >>= 1;
1143 packetmax >>= 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001144 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001145 c = channel_new(
Damien Miller1383bd82000-04-06 12:32:37 +10001146 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001147 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001148 "client-session", /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001149
Ben Lindstromf558cf62001-09-20 23:13:49 +00001150 debug3("ssh_session2_open: channel_new: %d", c->self);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001151
Ben Lindstrom5ec26452001-06-09 00:18:51 +00001152 channel_send_open(c->self);
Ben Lindstromf558cf62001-09-20 23:13:49 +00001153 if (!no_shell_flag)
Damien Miller0e220db2004-06-15 10:34:08 +10001154 channel_register_confirm(c->self, ssh_session2_setup, NULL);
Damien Miller1383bd82000-04-06 12:32:37 +10001155
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001156 return c->self;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001157}
1158
Ben Lindstrombba81212001-06-25 05:01:22 +00001159static int
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001160ssh_session2(void)
1161{
Ben Lindstromf558cf62001-09-20 23:13:49 +00001162 int id = -1;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001163
1164 /* XXX should be pre-session */
1165 ssh_init_forwarding();
Damien Miller0e220db2004-06-15 10:34:08 +10001166 ssh_control_listener();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001167
Ben Lindstromf558cf62001-09-20 23:13:49 +00001168 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1169 id = ssh_session2_open();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001170
Damien Millerd27b9472005-12-13 19:29:02 +11001171 /* Execute a local command */
1172 if (options.local_command != NULL &&
1173 options.permit_local_command)
1174 ssh_local_cmd(options.local_command);
1175
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001176 /* If requested, let ssh continue in the background. */
1177 if (fork_after_authentication_flag)
1178 if (daemon(1, 1) < 0)
1179 fatal("daemon() failed: %.200s", strerror(errno));
1180
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001181 return client_loop(tty_flag, tty_flag ?
1182 options.escape_char : SSH_ESCAPECHAR_NONE, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001183}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001184
Ben Lindstrombba81212001-06-25 05:01:22 +00001185static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001186load_public_identity_files(void)
1187{
1188 char *filename;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001189 int i = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001190 Key *public;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001191#ifdef SMARTCARD
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001192 Key **keys;
1193
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001194 if (options.smartcard_device != NULL &&
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001195 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1196 (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL ) {
1197 int count = 0;
1198 for (i = 0; keys[i] != NULL; i++) {
1199 count++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001200 memmove(&options.identity_files[1], &options.identity_files[0],
1201 sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
1202 memmove(&options.identity_keys[1], &options.identity_keys[0],
1203 sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1204 options.num_identity_files++;
1205 options.identity_keys[0] = keys[i];
Damien Miller56a0bb02003-06-18 20:28:40 +10001206 options.identity_files[0] = sc_get_key_label(keys[i]);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001207 }
Ben Lindstrom4f054602002-03-26 03:23:00 +00001208 if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1209 options.num_identity_files = SSH_MAX_IDENTITY_FILES;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001210 i = count;
1211 xfree(keys);
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001212 }
Ben Lindstromffce1472001-08-06 21:57:31 +00001213#endif /* SMARTCARD */
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001214 for (; i < options.num_identity_files; i++) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001215 filename = tilde_expand_filename(options.identity_files[i],
1216 original_real_uid);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001217 public = key_load_public(filename, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001218 debug("identity file %s type %d", filename,
1219 public ? public->type : -1);
1220 xfree(options.identity_files[i]);
1221 options.identity_files[i] = filename;
1222 options.identity_keys[i] = public;
1223 }
1224}
Damien Miller0e220db2004-06-15 10:34:08 +10001225
1226static void
1227control_client_sighandler(int signo)
1228{
1229 control_client_terminate = signo;
1230}
1231
1232static void
1233control_client_sigrelay(int signo)
1234{
1235 if (control_server_pid > 1)
1236 kill(control_server_pid, signo);
1237}
1238
Darren Tucker365433f2004-06-22 12:29:23 +10001239static int
1240env_permitted(char *env)
1241{
1242 int i;
1243 char name[1024], *cp;
1244
1245 strlcpy(name, env, sizeof(name));
1246 if ((cp = strchr(name, '=')) == NULL)
1247 return (0);
1248
1249 *cp = '\0';
1250
1251 for (i = 0; i < options.num_send_env; i++)
1252 if (match_pattern(name, options.send_env[i]))
1253 return (1);
1254
1255 return (0);
1256}
1257
Damien Miller0e220db2004-06-15 10:34:08 +10001258static void
1259control_client(const char *path)
1260{
1261 struct sockaddr_un addr;
Darren Tucker39207a42004-11-05 20:19:51 +11001262 int i, r, fd, sock, exitval, num_env, addr_len;
Damien Miller0e220db2004-06-15 10:34:08 +10001263 Buffer m;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001264 char *term;
Damien Miller3756dce2004-06-18 01:17:29 +10001265 extern char **environ;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001266 u_int flags;
Darren Tuckerfc959702004-07-17 16:12:08 +10001267
Damien Millerd14b1e72005-06-16 13:19:41 +10001268 if (mux_command == 0)
1269 mux_command = SSHMUX_COMMAND_OPEN;
1270
1271 switch (options.control_master) {
1272 case SSHCTL_MASTER_AUTO:
1273 case SSHCTL_MASTER_AUTO_ASK:
1274 debug("auto-mux: Trying existing master");
1275 /* FALLTHROUGH */
1276 case SSHCTL_MASTER_NO:
1277 break;
1278 default:
1279 return;
1280 }
1281
Damien Miller0e220db2004-06-15 10:34:08 +10001282 memset(&addr, '\0', sizeof(addr));
1283 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001284 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001285 strlen(path) + 1;
1286
1287 if (strlcpy(addr.sun_path, path,
1288 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1289 fatal("ControlPath too long");
1290
1291 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1292 fatal("%s socket(): %s", __func__, strerror(errno));
1293
Damien Millerdadfd4d2005-05-26 12:07:13 +10001294 if (connect(sock, (struct sockaddr*)&addr, addr_len) == -1) {
Darren Tucker0814d312005-06-01 23:08:51 +10001295 if (mux_command != SSHMUX_COMMAND_OPEN) {
1296 fatal("Control socket connect(%.100s): %s", path,
1297 strerror(errno));
1298 }
Damien Miller538c9b72005-05-26 12:11:28 +10001299 if (errno == ENOENT)
1300 debug("Control socket \"%.100s\" does not exist", path);
1301 else {
1302 error("Control socket connect(%.100s): %s", path,
1303 strerror(errno));
1304 }
Damien Miller13390022005-07-06 09:44:19 +10001305 close(sock);
1306 return;
1307 }
Damien Miller46d38de2005-07-17 17:02:09 +10001308
Damien Miller13390022005-07-06 09:44:19 +10001309 if (stdin_null_flag) {
1310 if ((fd = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1311 fatal("open(/dev/null): %s", strerror(errno));
1312 if (dup2(fd, STDIN_FILENO) == -1)
1313 fatal("dup2: %s", strerror(errno));
1314 if (fd > STDERR_FILENO)
1315 close(fd);
1316 }
Damien Miller46d38de2005-07-17 17:02:09 +10001317
Damien Miller13390022005-07-06 09:44:19 +10001318 term = getenv("TERM");
Darren Tucker7ebfc102004-11-07 20:06:19 +11001319
1320 flags = 0;
1321 if (tty_flag)
1322 flags |= SSHMUX_FLAG_TTY;
1323 if (subsystem_flag)
1324 flags |= SSHMUX_FLAG_SUBSYS;
Damien Miller13390022005-07-06 09:44:19 +10001325 if (options.forward_x11)
1326 flags |= SSHMUX_FLAG_X11_FWD;
1327 if (options.forward_agent)
1328 flags |= SSHMUX_FLAG_AGENT_FWD;
Damien Miller0e220db2004-06-15 10:34:08 +10001329
Damien Miller0e220db2004-06-15 10:34:08 +10001330 buffer_init(&m);
1331
Darren Tucker7ebfc102004-11-07 20:06:19 +11001332 /* Send our command to server */
1333 buffer_put_int(&m, mux_command);
1334 buffer_put_int(&m, flags);
Damien Miller13390022005-07-06 09:44:19 +10001335 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001336 fatal("%s: msg_send", __func__);
1337 buffer_clear(&m);
1338
1339 /* Get authorisation status and PID of controlee */
Damien Miller0e220db2004-06-15 10:34:08 +10001340 if (ssh_msg_recv(sock, &m) == -1)
1341 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001342 if (buffer_get_char(&m) != SSHMUX_VER)
Damien Miller0e220db2004-06-15 10:34:08 +10001343 fatal("%s: wrong version", __func__);
Damien Miller23f07702004-06-18 01:19:03 +10001344 if (buffer_get_int(&m) != 1)
1345 fatal("Connection to master denied");
Damien Miller0e220db2004-06-15 10:34:08 +10001346 control_server_pid = buffer_get_int(&m);
1347
Damien Miller0e220db2004-06-15 10:34:08 +10001348 buffer_clear(&m);
Damien Miller0e220db2004-06-15 10:34:08 +10001349
Darren Tucker7ebfc102004-11-07 20:06:19 +11001350 switch (mux_command) {
1351 case SSHMUX_COMMAND_ALIVE_CHECK:
Darren Tucker47eede72005-03-14 23:08:12 +11001352 fprintf(stderr, "Master running (pid=%d)\r\n",
Darren Tucker7ebfc102004-11-07 20:06:19 +11001353 control_server_pid);
1354 exit(0);
1355 case SSHMUX_COMMAND_TERMINATE:
1356 fprintf(stderr, "Exit request sent.\r\n");
1357 exit(0);
1358 case SSHMUX_COMMAND_OPEN:
1359 /* continue below */
1360 break;
1361 default:
1362 fatal("silly mux_command %d", mux_command);
1363 }
1364
1365 /* SSHMUX_COMMAND_OPEN */
Damien Miller13390022005-07-06 09:44:19 +10001366 buffer_put_cstring(&m, term ? term : "");
Damien Miller0e220db2004-06-15 10:34:08 +10001367 buffer_append(&command, "\0", 1);
1368 buffer_put_cstring(&m, buffer_ptr(&command));
1369
Darren Tucker365433f2004-06-22 12:29:23 +10001370 if (options.num_send_env == 0 || environ == NULL) {
1371 buffer_put_int(&m, 0);
Darren Tuckerfc959702004-07-17 16:12:08 +10001372 } else {
Darren Tucker365433f2004-06-22 12:29:23 +10001373 /* Pass environment */
1374 num_env = 0;
1375 for (i = 0; environ[i] != NULL; i++)
1376 if (env_permitted(environ[i]))
1377 num_env++; /* Count */
Darren Tuckerfc959702004-07-17 16:12:08 +10001378
Darren Tucker365433f2004-06-22 12:29:23 +10001379 buffer_put_int(&m, num_env);
1380
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001381 for (i = 0; environ[i] != NULL && num_env >= 0; i++)
1382 if (env_permitted(environ[i])) {
1383 num_env--;
Darren Tucker365433f2004-06-22 12:29:23 +10001384 buffer_put_cstring(&m, environ[i]);
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001385 }
Darren Tucker365433f2004-06-22 12:29:23 +10001386 }
Damien Miller3756dce2004-06-18 01:17:29 +10001387
Damien Miller13390022005-07-06 09:44:19 +10001388 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Damien Miller0e220db2004-06-15 10:34:08 +10001389 fatal("%s: msg_send", __func__);
1390
1391 mm_send_fd(sock, STDIN_FILENO);
1392 mm_send_fd(sock, STDOUT_FILENO);
1393 mm_send_fd(sock, STDERR_FILENO);
1394
1395 /* Wait for reply, so master has a chance to gather ttymodes */
1396 buffer_clear(&m);
1397 if (ssh_msg_recv(sock, &m) == -1)
1398 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001399 if (buffer_get_char(&m) != SSHMUX_VER)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001400 fatal("%s: wrong version", __func__);
Damien Miller0e220db2004-06-15 10:34:08 +10001401 buffer_free(&m);
1402
Darren Tucker07336da2004-11-05 20:02:16 +11001403 signal(SIGHUP, control_client_sighandler);
Damien Miller0809e232004-06-18 22:20:57 +10001404 signal(SIGINT, control_client_sighandler);
1405 signal(SIGTERM, control_client_sighandler);
1406 signal(SIGWINCH, control_client_sigrelay);
1407
Damien Miller0e220db2004-06-15 10:34:08 +10001408 if (tty_flag)
1409 enter_raw_mode();
1410
1411 /* Stick around until the controlee closes the client_fd */
1412 exitval = 0;
1413 for (;!control_client_terminate;) {
1414 r = read(sock, &exitval, sizeof(exitval));
1415 if (r == 0) {
1416 debug2("Received EOF from master");
1417 break;
1418 }
1419 if (r > 0)
1420 debug2("Received exit status from master %d", exitval);
1421 if (r == -1 && errno != EINTR)
1422 fatal("%s: read %s", __func__, strerror(errno));
1423 }
1424
1425 if (control_client_terminate)
1426 debug2("Exiting on signal %d", control_client_terminate);
1427
1428 close(sock);
1429
1430 leave_raw_mode();
1431
1432 if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
1433 fprintf(stderr, "Connection to master closed.\r\n");
1434
1435 exit(exitval);
1436}