blob: 5331402e501fe0f189c1cb1bc3b2f21d79b9eeea [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 Miller17e91c02006-03-15 11:28:34 +110043RCSID("$OpenBSD: ssh.c,v 1.260 2006/02/10 00:27:13 stevesk 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 Miller6476cad2005-06-16 13:18:34 +1000641 snprintf(buf, sizeof(buf), "%d", options.port);
642 cp = tilde_expand_filename(options.control_path,
643 original_real_uid);
644 options.control_path = percent_expand(cp, "p", buf, "h", host,
645 "r", options.user, (char *)NULL);
646 xfree(cp);
Damien Miller0e220db2004-06-15 10:34:08 +1000647 }
Darren Tucker0814d312005-06-01 23:08:51 +1000648 if (mux_command != 0 && options.control_path == NULL)
649 fatal("No ControlPath specified for \"-O\" command");
Damien Millerd14b1e72005-06-16 13:19:41 +1000650 if (options.control_path != NULL)
Damien Millerdadfd4d2005-05-26 12:07:13 +1000651 control_client(options.control_path);
Damien Miller0e220db2004-06-15 10:34:08 +1000652
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000653 /* Open a connection to the remote host. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000654 if (ssh_connect(host, &hostaddr, options.port,
655 options.address_family, options.connection_attempts,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000656#ifdef HAVE_CYGWIN
657 options.use_privileged_port,
658#else
Ben Lindstromf9c48842002-06-11 16:37:51 +0000659 original_effective_uid == 0 && options.use_privileged_port,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000660#endif
Ben Lindstromda394ca2002-06-12 16:11:12 +0000661 options.proxy_command) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100662 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000663
Damien Miller5428f641999-11-25 11:54:57 +1100664 /*
665 * If we successfully made the connection, load the host private key
666 * in case we will need it later for combined rsa-rhosts
667 * authentication. This must be done before releasing extra
668 * privileges, because the file is only readable by root.
Ben Lindstrom9e5bb572002-06-06 19:58:27 +0000669 * If we cannot access the private keys, load the public keys
670 * instead and try to execute the ssh-keysign helper instead.
Damien Miller5428f641999-11-25 11:54:57 +1100671 */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000672 sensitive_data.nkeys = 0;
673 sensitive_data.keys = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000674 sensitive_data.external_keysign = 0;
Ben Lindstrom24157572002-06-12 16:09:39 +0000675 if (options.rhosts_rsa_authentication ||
676 options.hostbased_authentication) {
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000677 sensitive_data.nkeys = 3;
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000678 sensitive_data.keys = xmalloc(sensitive_data.nkeys *
679 sizeof(Key));
Ben Lindstromf9c48842002-06-11 16:37:51 +0000680
681 PRIV_START;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000682 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
Ben Lindstromd0fca422001-03-26 13:44:06 +0000683 _PATH_HOST_KEY_FILE, "", NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000684 sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
685 _PATH_HOST_DSA_KEY_FILE, "", NULL);
686 sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
687 _PATH_HOST_RSA_KEY_FILE, "", NULL);
Ben Lindstromf9c48842002-06-11 16:37:51 +0000688 PRIV_END;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000689
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +0000690 if (options.hostbased_authentication == 1 &&
691 sensitive_data.keys[0] == NULL &&
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000692 sensitive_data.keys[1] == NULL &&
693 sensitive_data.keys[2] == NULL) {
694 sensitive_data.keys[1] = key_load_public(
695 _PATH_HOST_DSA_KEY_FILE, NULL);
696 sensitive_data.keys[2] = key_load_public(
697 _PATH_HOST_RSA_KEY_FILE, NULL);
698 sensitive_data.external_keysign = 1;
699 }
Damien Miller95def091999-11-25 00:26:21 +1100700 }
Damien Miller5428f641999-11-25 11:54:57 +1100701 /*
702 * Get rid of any extra privileges that we may have. We will no
703 * longer need them. Also, extra privileges could make it very hard
704 * to read identity files and other non-world-readable files from the
705 * user's home directory if it happens to be on a NFS volume where
706 * root is mapped to nobody.
707 */
Darren Tucker25f60a72004-08-15 17:23:34 +1000708 if (original_effective_uid == 0) {
709 PRIV_START;
710 permanently_set_uid(pw);
711 }
Damien Miller95def091999-11-25 00:26:21 +1100712
Damien Miller5428f641999-11-25 11:54:57 +1100713 /*
714 * Now that we are back to our own permissions, create ~/.ssh
Damien Miller788f2122005-11-05 15:14:59 +1100715 * directory if it doesn't already exist.
Damien Miller5428f641999-11-25 11:54:57 +1100716 */
Ben Lindstrom930b14a2001-08-15 23:19:21 +0000717 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 +1100718 if (stat(buf, &st) < 0)
Damien Millerbe484b52000-07-15 14:14:16 +1000719 if (mkdir(buf, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100720 error("Could not create directory '%.200s'.", buf);
721
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000722 /* load options.identity_files */
723 load_public_identity_files();
724
725 /* Expand ~ in known host file names. */
726 /* XXX mem-leaks: */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100727 options.system_hostfile =
728 tilde_expand_filename(options.system_hostfile, original_real_uid);
729 options.user_hostfile =
730 tilde_expand_filename(options.user_hostfile, original_real_uid);
731 options.system_hostfile2 =
732 tilde_expand_filename(options.system_hostfile2, original_real_uid);
733 options.user_hostfile2 =
734 tilde_expand_filename(options.user_hostfile2, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100735
Damien Miller07cd5892001-11-12 10:52:03 +1100736 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
737
Damien Miller95def091999-11-25 00:26:21 +1100738 /* Log into the remote system. This never returns if the login fails. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000739 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, pw);
Damien Miller95def091999-11-25 00:26:21 +1100740
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000741 /* We no longer need the private host keys. Clear them now. */
742 if (sensitive_data.nkeys != 0) {
743 for (i = 0; i < sensitive_data.nkeys; i++) {
744 if (sensitive_data.keys[i] != NULL) {
745 /* Destroys contents safely */
746 debug3("clear hostkey %d", i);
747 key_free(sensitive_data.keys[i]);
748 sensitive_data.keys[i] = NULL;
749 }
750 }
751 xfree(sensitive_data.keys);
752 }
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +0000753 for (i = 0; i < options.num_identity_files; i++) {
754 if (options.identity_files[i]) {
755 xfree(options.identity_files[i]);
756 options.identity_files[i] = NULL;
757 }
758 if (options.identity_keys[i]) {
759 key_free(options.identity_keys[i]);
760 options.identity_keys[i] = NULL;
761 }
762 }
Damien Miller95def091999-11-25 00:26:21 +1100763
Damien Miller1383bd82000-04-06 12:32:37 +1000764 exit_status = compat20 ? ssh_session2() : ssh_session();
765 packet_close();
Damien Miller8c4e18a2002-09-19 12:05:02 +1000766
Damien Miller0e220db2004-06-15 10:34:08 +1000767 if (options.control_path != NULL && control_fd != -1)
768 unlink(options.control_path);
769
Damien Miller8c4e18a2002-09-19 12:05:02 +1000770 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100771 * Send SIGHUP to proxy command if used. We don't wait() in
Damien Miller8c4e18a2002-09-19 12:05:02 +1000772 * case it hangs and instead rely on init to reap the child
773 */
774 if (proxy_command_pid > 1)
775 kill(proxy_command_pid, SIGHUP);
776
Damien Miller1383bd82000-04-06 12:32:37 +1000777 return exit_status;
778}
779
Ben Lindstrombba81212001-06-25 05:01:22 +0000780static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100781ssh_init_forwarding(void)
782{
Kevin Steves12057502001-02-05 14:54:34 +0000783 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100784 int i;
Kevin Steves12057502001-02-05 14:54:34 +0000785
Damien Miller0bc1bd82000-11-13 22:57:25 +1100786 /* Initiate local TCP/IP port forwardings. */
787 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100788 debug("Local connections to %.200s:%d forwarded to remote "
789 "address %.200s:%d",
Darren Tucker47eede72005-03-14 23:08:12 +1100790 (options.local_forwards[i].listen_host == NULL) ?
791 (options.gateway_ports ? "*" : "LOCALHOST") :
Damien Millerf91ee4c2005-03-01 21:24:33 +1100792 options.local_forwards[i].listen_host,
793 options.local_forwards[i].listen_port,
794 options.local_forwards[i].connect_host,
795 options.local_forwards[i].connect_port);
Damien Millerb16461c2002-01-22 23:29:22 +1100796 success += channel_setup_local_fwd_listener(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100797 options.local_forwards[i].listen_host,
798 options.local_forwards[i].listen_port,
799 options.local_forwards[i].connect_host,
800 options.local_forwards[i].connect_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100801 options.gateway_ports);
802 }
Kevin Steves12057502001-02-05 14:54:34 +0000803 if (i > 0 && success == 0)
804 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100805
806 /* Initiate remote TCP/IP port forwardings. */
807 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100808 debug("Remote connections from %.200s:%d forwarded to "
809 "local address %.200s:%d",
Damien Miller46d38de2005-07-17 17:02:09 +1000810 (options.remote_forwards[i].listen_host == NULL) ?
Damien Milleraa3bb102005-11-05 15:12:59 +1100811 "LOCALHOST" : options.remote_forwards[i].listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100812 options.remote_forwards[i].listen_port,
813 options.remote_forwards[i].connect_host,
814 options.remote_forwards[i].connect_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100815 channel_request_remote_forwarding(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100816 options.remote_forwards[i].listen_host,
817 options.remote_forwards[i].listen_port,
818 options.remote_forwards[i].connect_host,
819 options.remote_forwards[i].connect_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100820 }
821}
822
Ben Lindstrombba81212001-06-25 05:01:22 +0000823static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100824check_agent_present(void)
825{
826 if (options.forward_agent) {
Damien Miller788f2122005-11-05 15:14:59 +1100827 /* Clear agent forwarding if we don't have an agent. */
Damien Miller789e95d2002-09-12 09:52:46 +1000828 if (!ssh_agent_present())
Damien Miller0bc1bd82000-11-13 22:57:25 +1100829 options.forward_agent = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100830 }
831}
832
Ben Lindstrombba81212001-06-25 05:01:22 +0000833static int
Damien Miller1383bd82000-04-06 12:32:37 +1000834ssh_session(void)
835{
836 int type;
Damien Miller1383bd82000-04-06 12:32:37 +1000837 int interactive = 0;
838 int have_tty = 0;
839 struct winsize ws;
Damien Miller1383bd82000-04-06 12:32:37 +1000840 char *cp;
Damien Miller17e7ed02005-06-17 12:54:33 +1000841 const char *display;
Damien Miller1383bd82000-04-06 12:32:37 +1000842
Damien Miller95def091999-11-25 00:26:21 +1100843 /* Enable compression if requested. */
844 if (options.compression) {
845 debug("Requesting compression at level %d.", options.compression_level);
846
847 if (options.compression_level < 1 || options.compression_level > 9)
848 fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
849
850 /* Send the request. */
851 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
852 packet_put_int(options.compression_level);
853 packet_send();
854 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100855 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100856 if (type == SSH_SMSG_SUCCESS)
857 packet_start_compression(options.compression_level);
858 else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000859 logit("Warning: Remote host refused compression.");
Damien Miller95def091999-11-25 00:26:21 +1100860 else
861 packet_disconnect("Protocol error waiting for compression response.");
862 }
863 /* Allocate a pseudo tty if appropriate. */
864 if (tty_flag) {
865 debug("Requesting pty.");
866
867 /* Start the packet. */
868 packet_start(SSH_CMSG_REQUEST_PTY);
869
870 /* Store TERM in the packet. There is no limit on the
871 length of the string. */
872 cp = getenv("TERM");
873 if (!cp)
874 cp = "";
Ben Lindstrom664408d2001-06-09 01:42:01 +0000875 packet_put_cstring(cp);
Damien Miller95def091999-11-25 00:26:21 +1100876
877 /* Store window size in the packet. */
878 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
879 memset(&ws, 0, sizeof(ws));
880 packet_put_int(ws.ws_row);
881 packet_put_int(ws.ws_col);
882 packet_put_int(ws.ws_xpixel);
883 packet_put_int(ws.ws_ypixel);
884
885 /* Store tty modes in the packet. */
Ben Lindstromae8e2d32001-04-14 23:13:02 +0000886 tty_make_modes(fileno(stdin), NULL);
Damien Miller95def091999-11-25 00:26:21 +1100887
888 /* Send the packet, and wait for it to leave. */
889 packet_send();
890 packet_write_wait();
891
892 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100893 type = packet_read();
Damien Miller450a7a12000-03-26 13:04:51 +1000894 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100895 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +1000896 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000897 } else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000898 logit("Warning: Remote host failed or refused to allocate a pseudo tty.");
Damien Miller95def091999-11-25 00:26:21 +1100899 else
900 packet_disconnect("Protocol error waiting for pty request response.");
901 }
902 /* Request X11 forwarding if enabled and DISPLAY is set. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000903 display = getenv("DISPLAY");
904 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000905 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +1000906 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000907 client_x11_get_proto(display, options.xauth_location,
908 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +1000909 /* Request forwarding with authentication spoofing. */
Damien Miller95def091999-11-25 00:26:21 +1100910 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +1000911 x11_request_forwarding_with_spoofing(0, display, proto, data);
Damien Miller95def091999-11-25 00:26:21 +1100912
913 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100914 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100915 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100916 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +1000917 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller996acd22003-04-09 20:59:48 +1000918 logit("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +1000919 } else {
Damien Miller95def091999-11-25 00:26:21 +1100920 packet_disconnect("Protocol error waiting for X11 forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +1000921 }
Damien Miller95def091999-11-25 00:26:21 +1100922 }
923 /* Tell the packet module whether this is an interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000924 packet_set_interactive(interactive);
Damien Miller95def091999-11-25 00:26:21 +1100925
926 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100927 check_agent_present();
928
Damien Miller95def091999-11-25 00:26:21 +1100929 if (options.forward_agent) {
930 debug("Requesting authentication agent forwarding.");
931 auth_request_forwarding();
932
933 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100934 type = packet_read();
Damien Miller48b03fc2002-01-22 23:11:40 +1100935 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100936 if (type != SSH_SMSG_SUCCESS)
Damien Miller996acd22003-04-09 20:59:48 +1000937 logit("Warning: Remote host denied authentication agent forwarding.");
Damien Miller95def091999-11-25 00:26:21 +1100938 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000939
Damien Miller0bc1bd82000-11-13 22:57:25 +1100940 /* Initiate port forwardings. */
941 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000942
Damien Miller5428f641999-11-25 11:54:57 +1100943 /* If requested, let ssh continue in the background. */
Damien Miller4af51302000-04-16 11:18:38 +1000944 if (fork_after_authentication_flag)
Damien Miller5428f641999-11-25 11:54:57 +1100945 if (daemon(1, 1) < 0)
946 fatal("daemon() failed: %.200s", strerror(errno));
947
948 /*
949 * If a command was specified on the command line, execute the
950 * command now. Otherwise request the server to start a shell.
951 */
Damien Miller95def091999-11-25 00:26:21 +1100952 if (buffer_len(&command) > 0) {
953 int len = buffer_len(&command);
954 if (len > 900)
955 len = 900;
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100956 debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
Damien Miller95def091999-11-25 00:26:21 +1100957 packet_start(SSH_CMSG_EXEC_CMD);
958 packet_put_string(buffer_ptr(&command), buffer_len(&command));
959 packet_send();
960 packet_write_wait();
961 } else {
962 debug("Requesting shell.");
963 packet_start(SSH_CMSG_EXEC_SHELL);
964 packet_send();
965 packet_write_wait();
966 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000967
Damien Miller95def091999-11-25 00:26:21 +1100968 /* Enter the interactive session. */
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000969 return client_loop(have_tty, tty_flag ?
970 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
Damien Miller1383bd82000-04-06 12:32:37 +1000971}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000972
Ben Lindstrombba81212001-06-25 05:01:22 +0000973static void
Damien Miller0e220db2004-06-15 10:34:08 +1000974ssh_subsystem_reply(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000975{
976 int id, len;
977
978 id = packet_get_int();
979 len = buffer_len(&command);
Ben Lindstrom4040fe12001-03-05 06:52:57 +0000980 if (len > 900)
981 len = 900;
Damien Miller48b03fc2002-01-22 23:11:40 +1100982 packet_check_eom();
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000983 if (type == SSH2_MSG_CHANNEL_FAILURE)
984 fatal("Request for subsystem '%.*s' failed on channel %d",
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100985 len, (u_char *)buffer_ptr(&command), id);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000986}
987
Damien Miller2797f7f2002-04-23 21:09:44 +1000988void
Damien Miller509b0102003-12-17 16:33:10 +1100989client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt)
Damien Miller2797f7f2002-04-23 21:09:44 +1000990{
991 int i;
992
993 i = client_global_request_id++;
Damien Miller509b0102003-12-17 16:33:10 +1100994 if (i >= options.num_remote_forwards)
Damien Miller2797f7f2002-04-23 21:09:44 +1000995 return;
Damien Miller2797f7f2002-04-23 21:09:44 +1000996 debug("remote forward %s for: listen %d, connect %s:%d",
997 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
Damien Millerf91ee4c2005-03-01 21:24:33 +1100998 options.remote_forwards[i].listen_port,
999 options.remote_forwards[i].connect_host,
1000 options.remote_forwards[i].connect_port);
Damien Miller2797f7f2002-04-23 21:09:44 +10001001 if (type == SSH2_MSG_REQUEST_FAILURE)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001002 logit("Warning: remote port forwarding failed for listen "
1003 "port %d", options.remote_forwards[i].listen_port);
Damien Miller2797f7f2002-04-23 21:09:44 +10001004}
1005
Damien Miller0e220db2004-06-15 10:34:08 +10001006static void
1007ssh_control_listener(void)
1008{
1009 struct sockaddr_un addr;
1010 mode_t old_umask;
Damien Miller07b6ff12004-06-15 11:14:45 +10001011 int addr_len;
1012
Damien Millerd14b1e72005-06-16 13:19:41 +10001013 if (options.control_path == NULL ||
1014 options.control_master == SSHCTL_MASTER_NO)
Damien Miller0e220db2004-06-15 10:34:08 +10001015 return;
1016
Damien Millerd14b1e72005-06-16 13:19:41 +10001017 debug("setting up multiplex master socket");
1018
Damien Miller0e220db2004-06-15 10:34:08 +10001019 memset(&addr, '\0', sizeof(addr));
1020 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001021 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001022 strlen(options.control_path) + 1;
1023
1024 if (strlcpy(addr.sun_path, options.control_path,
1025 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1026 fatal("ControlPath too long");
1027
1028 if ((control_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001029 fatal("%s socket(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001030
1031 old_umask = umask(0177);
Damien Miller07b6ff12004-06-15 11:14:45 +10001032 if (bind(control_fd, (struct sockaddr*)&addr, addr_len) == -1) {
Damien Miller0e220db2004-06-15 10:34:08 +10001033 control_fd = -1;
Damien Miller4f10e252005-05-04 15:33:09 +10001034 if (errno == EINVAL || errno == EADDRINUSE)
Damien Miller0e220db2004-06-15 10:34:08 +10001035 fatal("ControlSocket %s already exists",
1036 options.control_path);
1037 else
Damien Miller15d72a02005-11-05 15:07:33 +11001038 fatal("%s bind(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001039 }
1040 umask(old_umask);
1041
1042 if (listen(control_fd, 64) == -1)
Damien Miller15d72a02005-11-05 15:07:33 +11001043 fatal("%s listen(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001044
1045 set_nonblock(control_fd);
1046}
1047
Ben Lindstromf558cf62001-09-20 23:13:49 +00001048/* request pty/x11/agent/tcpfwd/shell for channel */
Ben Lindstrombba81212001-06-25 05:01:22 +00001049static void
Ben Lindstromf558cf62001-09-20 23:13:49 +00001050ssh_session2_setup(int id, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +10001051{
Damien Miller3756dce2004-06-18 01:17:29 +10001052 extern char **environ;
Damien Miller17e7ed02005-06-17 12:54:33 +10001053 const char *display;
Damien Miller0e220db2004-06-15 10:34:08 +10001054 int interactive = tty_flag;
Damien Miller17e7ed02005-06-17 12:54:33 +10001055
Damien Miller46d38de2005-07-17 17:02:09 +10001056 display = getenv("DISPLAY");
Damien Miller17e7ed02005-06-17 12:54:33 +10001057 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001058 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001059 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001060 client_x11_get_proto(display, options.xauth_location,
1061 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001062 /* Request forwarding with authentication spoofing. */
1063 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +10001064 x11_request_forwarding_with_spoofing(id, display, proto, data);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001065 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001066 /* XXX wait for reply */
1067 }
1068
Damien Miller0bc1bd82000-11-13 22:57:25 +11001069 check_agent_present();
1070 if (options.forward_agent) {
1071 debug("Requesting authentication agent forwarding.");
1072 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1073 packet_send();
1074 }
1075
Damien Miller7b58e802005-12-13 19:33:19 +11001076 if (options.tun_open != SSH_TUNMODE_NO) {
Damien Millerd27b9472005-12-13 19:29:02 +11001077 Channel *c;
1078 int fd;
1079
1080 debug("Requesting tun.");
Damien Miller7b58e802005-12-13 19:33:19 +11001081 if ((fd = tun_open(options.tun_local,
1082 options.tun_open)) >= 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11001083 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1084 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1085 0, "tun", 1);
1086 c->datagram = 1;
Damien Miller598bbc22005-12-31 16:33:36 +11001087#if defined(SSH_TUN_FILTER)
1088 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1089 channel_register_filter(c->self, sys_tun_infilter,
1090 sys_tun_outfilter);
1091#endif
Damien Millerd27b9472005-12-13 19:29:02 +11001092 packet_start(SSH2_MSG_CHANNEL_OPEN);
1093 packet_put_cstring("tun@openssh.com");
1094 packet_put_int(c->self);
1095 packet_put_int(c->local_window_max);
1096 packet_put_int(c->local_maxpacket);
Damien Miller7b58e802005-12-13 19:33:19 +11001097 packet_put_int(options.tun_open);
Damien Millerd27b9472005-12-13 19:29:02 +11001098 packet_put_int(options.tun_remote);
1099 packet_send();
1100 }
1101 }
1102
Damien Miller0e220db2004-06-15 10:34:08 +10001103 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
Damien Miller3756dce2004-06-18 01:17:29 +10001104 NULL, fileno(stdin), &command, environ, &ssh_subsystem_reply);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001105
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001106 packet_set_interactive(interactive);
Damien Miller1383bd82000-04-06 12:32:37 +10001107}
1108
Ben Lindstromf558cf62001-09-20 23:13:49 +00001109/* open new channel for a session */
Ben Lindstrombba81212001-06-25 05:01:22 +00001110static int
Ben Lindstromf558cf62001-09-20 23:13:49 +00001111ssh_session2_open(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001112{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001113 Channel *c;
1114 int window, packetmax, in, out, err;
Damien Millerad833b32000-08-23 10:46:23 +10001115
Damien Millercaf6dd62000-08-29 11:33:50 +11001116 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001117 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001118 } else {
1119 in = dup(STDIN_FILENO);
1120 }
1121 out = dup(STDOUT_FILENO);
1122 err = dup(STDERR_FILENO);
1123
1124 if (in < 0 || out < 0 || err < 0)
1125 fatal("dup() in/out/err failed");
1126
Damien Miller69b69aa2000-10-28 14:19:58 +11001127 /* enable nonblocking unless tty */
1128 if (!isatty(in))
1129 set_nonblock(in);
1130 if (!isatty(out))
1131 set_nonblock(out);
1132 if (!isatty(err))
1133 set_nonblock(err);
1134
Damien Millere4340be2000-09-16 13:29:08 +11001135 window = CHAN_SES_WINDOW_DEFAULT;
1136 packetmax = CHAN_SES_PACKET_DEFAULT;
Damien Miller19a59452002-02-19 15:20:57 +11001137 if (tty_flag) {
1138 window >>= 1;
1139 packetmax >>= 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001140 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001141 c = channel_new(
Damien Miller1383bd82000-04-06 12:32:37 +10001142 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001143 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001144 "client-session", /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001145
Ben Lindstromf558cf62001-09-20 23:13:49 +00001146 debug3("ssh_session2_open: channel_new: %d", c->self);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001147
Ben Lindstrom5ec26452001-06-09 00:18:51 +00001148 channel_send_open(c->self);
Ben Lindstromf558cf62001-09-20 23:13:49 +00001149 if (!no_shell_flag)
Damien Miller0e220db2004-06-15 10:34:08 +10001150 channel_register_confirm(c->self, ssh_session2_setup, NULL);
Damien Miller1383bd82000-04-06 12:32:37 +10001151
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001152 return c->self;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001153}
1154
Ben Lindstrombba81212001-06-25 05:01:22 +00001155static int
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001156ssh_session2(void)
1157{
Ben Lindstromf558cf62001-09-20 23:13:49 +00001158 int id = -1;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001159
1160 /* XXX should be pre-session */
1161 ssh_init_forwarding();
Damien Miller0e220db2004-06-15 10:34:08 +10001162 ssh_control_listener();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001163
Ben Lindstromf558cf62001-09-20 23:13:49 +00001164 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1165 id = ssh_session2_open();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001166
Damien Millerd27b9472005-12-13 19:29:02 +11001167 /* Execute a local command */
1168 if (options.local_command != NULL &&
1169 options.permit_local_command)
1170 ssh_local_cmd(options.local_command);
1171
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001172 /* If requested, let ssh continue in the background. */
1173 if (fork_after_authentication_flag)
1174 if (daemon(1, 1) < 0)
1175 fatal("daemon() failed: %.200s", strerror(errno));
1176
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001177 return client_loop(tty_flag, tty_flag ?
1178 options.escape_char : SSH_ESCAPECHAR_NONE, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001179}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001180
Ben Lindstrombba81212001-06-25 05:01:22 +00001181static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001182load_public_identity_files(void)
1183{
1184 char *filename;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001185 int i = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001186 Key *public;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001187#ifdef SMARTCARD
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001188 Key **keys;
1189
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001190 if (options.smartcard_device != NULL &&
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001191 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1192 (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL ) {
1193 int count = 0;
1194 for (i = 0; keys[i] != NULL; i++) {
1195 count++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001196 memmove(&options.identity_files[1], &options.identity_files[0],
1197 sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
1198 memmove(&options.identity_keys[1], &options.identity_keys[0],
1199 sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1200 options.num_identity_files++;
1201 options.identity_keys[0] = keys[i];
Damien Miller56a0bb02003-06-18 20:28:40 +10001202 options.identity_files[0] = sc_get_key_label(keys[i]);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001203 }
Ben Lindstrom4f054602002-03-26 03:23:00 +00001204 if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1205 options.num_identity_files = SSH_MAX_IDENTITY_FILES;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001206 i = count;
1207 xfree(keys);
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001208 }
Ben Lindstromffce1472001-08-06 21:57:31 +00001209#endif /* SMARTCARD */
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001210 for (; i < options.num_identity_files; i++) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001211 filename = tilde_expand_filename(options.identity_files[i],
1212 original_real_uid);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001213 public = key_load_public(filename, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001214 debug("identity file %s type %d", filename,
1215 public ? public->type : -1);
1216 xfree(options.identity_files[i]);
1217 options.identity_files[i] = filename;
1218 options.identity_keys[i] = public;
1219 }
1220}
Damien Miller0e220db2004-06-15 10:34:08 +10001221
1222static void
1223control_client_sighandler(int signo)
1224{
1225 control_client_terminate = signo;
1226}
1227
1228static void
1229control_client_sigrelay(int signo)
1230{
1231 if (control_server_pid > 1)
1232 kill(control_server_pid, signo);
1233}
1234
Darren Tucker365433f2004-06-22 12:29:23 +10001235static int
1236env_permitted(char *env)
1237{
1238 int i;
1239 char name[1024], *cp;
1240
1241 strlcpy(name, env, sizeof(name));
1242 if ((cp = strchr(name, '=')) == NULL)
1243 return (0);
1244
1245 *cp = '\0';
1246
1247 for (i = 0; i < options.num_send_env; i++)
1248 if (match_pattern(name, options.send_env[i]))
1249 return (1);
1250
1251 return (0);
1252}
1253
Damien Miller0e220db2004-06-15 10:34:08 +10001254static void
1255control_client(const char *path)
1256{
1257 struct sockaddr_un addr;
Darren Tucker39207a42004-11-05 20:19:51 +11001258 int i, r, fd, sock, exitval, num_env, addr_len;
Damien Miller0e220db2004-06-15 10:34:08 +10001259 Buffer m;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001260 char *term;
Damien Miller3756dce2004-06-18 01:17:29 +10001261 extern char **environ;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001262 u_int flags;
Darren Tuckerfc959702004-07-17 16:12:08 +10001263
Damien Millerd14b1e72005-06-16 13:19:41 +10001264 if (mux_command == 0)
1265 mux_command = SSHMUX_COMMAND_OPEN;
1266
1267 switch (options.control_master) {
1268 case SSHCTL_MASTER_AUTO:
1269 case SSHCTL_MASTER_AUTO_ASK:
1270 debug("auto-mux: Trying existing master");
1271 /* FALLTHROUGH */
1272 case SSHCTL_MASTER_NO:
1273 break;
1274 default:
1275 return;
1276 }
1277
Damien Miller0e220db2004-06-15 10:34:08 +10001278 memset(&addr, '\0', sizeof(addr));
1279 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001280 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001281 strlen(path) + 1;
1282
1283 if (strlcpy(addr.sun_path, path,
1284 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1285 fatal("ControlPath too long");
1286
1287 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1288 fatal("%s socket(): %s", __func__, strerror(errno));
1289
Damien Millerdadfd4d2005-05-26 12:07:13 +10001290 if (connect(sock, (struct sockaddr*)&addr, addr_len) == -1) {
Darren Tucker0814d312005-06-01 23:08:51 +10001291 if (mux_command != SSHMUX_COMMAND_OPEN) {
1292 fatal("Control socket connect(%.100s): %s", path,
1293 strerror(errno));
1294 }
Damien Miller538c9b72005-05-26 12:11:28 +10001295 if (errno == ENOENT)
1296 debug("Control socket \"%.100s\" does not exist", path);
1297 else {
1298 error("Control socket connect(%.100s): %s", path,
1299 strerror(errno));
1300 }
Damien Miller13390022005-07-06 09:44:19 +10001301 close(sock);
1302 return;
1303 }
Damien Miller46d38de2005-07-17 17:02:09 +10001304
Damien Miller13390022005-07-06 09:44:19 +10001305 if (stdin_null_flag) {
1306 if ((fd = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1307 fatal("open(/dev/null): %s", strerror(errno));
1308 if (dup2(fd, STDIN_FILENO) == -1)
1309 fatal("dup2: %s", strerror(errno));
1310 if (fd > STDERR_FILENO)
1311 close(fd);
1312 }
Damien Miller46d38de2005-07-17 17:02:09 +10001313
Damien Miller13390022005-07-06 09:44:19 +10001314 term = getenv("TERM");
Darren Tucker7ebfc102004-11-07 20:06:19 +11001315
1316 flags = 0;
1317 if (tty_flag)
1318 flags |= SSHMUX_FLAG_TTY;
1319 if (subsystem_flag)
1320 flags |= SSHMUX_FLAG_SUBSYS;
Damien Miller13390022005-07-06 09:44:19 +10001321 if (options.forward_x11)
1322 flags |= SSHMUX_FLAG_X11_FWD;
1323 if (options.forward_agent)
1324 flags |= SSHMUX_FLAG_AGENT_FWD;
Damien Miller0e220db2004-06-15 10:34:08 +10001325
Damien Miller0e220db2004-06-15 10:34:08 +10001326 buffer_init(&m);
1327
Darren Tucker7ebfc102004-11-07 20:06:19 +11001328 /* Send our command to server */
1329 buffer_put_int(&m, mux_command);
1330 buffer_put_int(&m, flags);
Damien Miller13390022005-07-06 09:44:19 +10001331 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001332 fatal("%s: msg_send", __func__);
1333 buffer_clear(&m);
1334
1335 /* Get authorisation status and PID of controlee */
Damien Miller0e220db2004-06-15 10:34:08 +10001336 if (ssh_msg_recv(sock, &m) == -1)
1337 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001338 if (buffer_get_char(&m) != SSHMUX_VER)
Damien Miller0e220db2004-06-15 10:34:08 +10001339 fatal("%s: wrong version", __func__);
Damien Miller23f07702004-06-18 01:19:03 +10001340 if (buffer_get_int(&m) != 1)
1341 fatal("Connection to master denied");
Damien Miller0e220db2004-06-15 10:34:08 +10001342 control_server_pid = buffer_get_int(&m);
1343
Damien Miller0e220db2004-06-15 10:34:08 +10001344 buffer_clear(&m);
Damien Miller0e220db2004-06-15 10:34:08 +10001345
Darren Tucker7ebfc102004-11-07 20:06:19 +11001346 switch (mux_command) {
1347 case SSHMUX_COMMAND_ALIVE_CHECK:
Darren Tucker47eede72005-03-14 23:08:12 +11001348 fprintf(stderr, "Master running (pid=%d)\r\n",
Darren Tucker7ebfc102004-11-07 20:06:19 +11001349 control_server_pid);
1350 exit(0);
1351 case SSHMUX_COMMAND_TERMINATE:
1352 fprintf(stderr, "Exit request sent.\r\n");
1353 exit(0);
1354 case SSHMUX_COMMAND_OPEN:
1355 /* continue below */
1356 break;
1357 default:
1358 fatal("silly mux_command %d", mux_command);
1359 }
1360
1361 /* SSHMUX_COMMAND_OPEN */
Damien Miller13390022005-07-06 09:44:19 +10001362 buffer_put_cstring(&m, term ? term : "");
Damien Miller0e220db2004-06-15 10:34:08 +10001363 buffer_append(&command, "\0", 1);
1364 buffer_put_cstring(&m, buffer_ptr(&command));
1365
Darren Tucker365433f2004-06-22 12:29:23 +10001366 if (options.num_send_env == 0 || environ == NULL) {
1367 buffer_put_int(&m, 0);
Darren Tuckerfc959702004-07-17 16:12:08 +10001368 } else {
Darren Tucker365433f2004-06-22 12:29:23 +10001369 /* Pass environment */
1370 num_env = 0;
1371 for (i = 0; environ[i] != NULL; i++)
1372 if (env_permitted(environ[i]))
1373 num_env++; /* Count */
Darren Tuckerfc959702004-07-17 16:12:08 +10001374
Darren Tucker365433f2004-06-22 12:29:23 +10001375 buffer_put_int(&m, num_env);
1376
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001377 for (i = 0; environ[i] != NULL && num_env >= 0; i++)
1378 if (env_permitted(environ[i])) {
1379 num_env--;
Darren Tucker365433f2004-06-22 12:29:23 +10001380 buffer_put_cstring(&m, environ[i]);
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001381 }
Darren Tucker365433f2004-06-22 12:29:23 +10001382 }
Damien Miller3756dce2004-06-18 01:17:29 +10001383
Damien Miller13390022005-07-06 09:44:19 +10001384 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Damien Miller0e220db2004-06-15 10:34:08 +10001385 fatal("%s: msg_send", __func__);
1386
1387 mm_send_fd(sock, STDIN_FILENO);
1388 mm_send_fd(sock, STDOUT_FILENO);
1389 mm_send_fd(sock, STDERR_FILENO);
1390
1391 /* Wait for reply, so master has a chance to gather ttymodes */
1392 buffer_clear(&m);
1393 if (ssh_msg_recv(sock, &m) == -1)
1394 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001395 if (buffer_get_char(&m) != SSHMUX_VER)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001396 fatal("%s: wrong version", __func__);
Damien Miller0e220db2004-06-15 10:34:08 +10001397 buffer_free(&m);
1398
Darren Tucker07336da2004-11-05 20:02:16 +11001399 signal(SIGHUP, control_client_sighandler);
Damien Miller0809e232004-06-18 22:20:57 +10001400 signal(SIGINT, control_client_sighandler);
1401 signal(SIGTERM, control_client_sighandler);
1402 signal(SIGWINCH, control_client_sigrelay);
1403
Damien Miller0e220db2004-06-15 10:34:08 +10001404 if (tty_flag)
1405 enter_raw_mode();
1406
1407 /* Stick around until the controlee closes the client_fd */
1408 exitval = 0;
1409 for (;!control_client_terminate;) {
1410 r = read(sock, &exitval, sizeof(exitval));
1411 if (r == 0) {
1412 debug2("Received EOF from master");
1413 break;
1414 }
1415 if (r > 0)
1416 debug2("Received exit status from master %d", exitval);
1417 if (r == -1 && errno != EINTR)
1418 fatal("%s: read %s", __func__, strerror(errno));
1419 }
1420
1421 if (control_client_terminate)
1422 debug2("Exiting on signal %d", control_client_terminate);
1423
1424 close(sock);
1425
1426 leave_raw_mode();
1427
1428 if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
1429 fprintf(stderr, "Connection to master closed.\r\n");
1430
1431 exit(exitval);
1432}