blob: 84246b5c4315e0285ee62e5127699ccf6ecb37c4 [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 Miller574c41f2006-03-15 11:40:10 +110043RCSID("$OpenBSD: ssh.c,v 1.262 2006/02/20 16:36:15 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 Miller574c41f2006-03-15 11:40:10 +110047#include <sys/types.h>
48#include <sys/un.h>
Damien Miller03e20032006-03-15 11:16:59 +110049
50#include <paths.h>
Damien Millereba71ba2000-04-29 23:57:08 +100051
52#include <openssl/evp.h>
Damien Miller0bc1bd82000-11-13 22:57:25 +110053#include <openssl/err.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054
Damien Millerd4a8b7e1999-10-27 13:42:43 +100055#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000056#include "ssh1.h"
Damien Miller1383bd82000-04-06 12:32:37 +100057#include "ssh2.h"
58#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000059#include "cipher.h"
60#include "xmalloc.h"
61#include "packet.h"
62#include "buffer.h"
Damien Miller0e220db2004-06-15 10:34:08 +100063#include "bufaux.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000064#include "channels.h"
Damien Millereba71ba2000-04-29 23:57:08 +100065#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100066#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100067#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000068#include "pathnames.h"
Damien Miller0e220db2004-06-15 10:34:08 +100069#include "dispatch.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +000070#include "clientloop.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000071#include "log.h"
72#include "readconf.h"
73#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000074#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000075#include "kex.h"
76#include "mac.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +100077#include "sshpty.h"
Darren Tucker46bc0752004-05-02 22:11:30 +100078#include "match.h"
Damien Miller0e220db2004-06-15 10:34:08 +100079#include "msg.h"
80#include "monitor_fdpass.h"
Darren Tucker25f60a72004-08-15 17:23:34 +100081#include "uidswap.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082
Ben Lindstromc5b68002001-07-04 04:52:03 +000083#ifdef SMARTCARD
Ben Lindstromc5b68002001-07-04 04:52:03 +000084#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000085#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +000086
Damien Miller3f905871999-11-15 17:10:57 +110087extern char *__progname;
Damien Miller3f905871999-11-15 17:10:57 +110088
Damien Miller95def091999-11-25 00:26:21 +110089/* Flag indicating whether debug mode is on. This can be set on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090int debug_flag = 0;
91
Damien Miller78928792000-04-12 20:17:38 +100092/* Flag indicating whether a tty should be allocated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093int tty_flag = 0;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +000094int no_tty_flag = 0;
95int force_tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096
Damien Miller1383bd82000-04-06 12:32:37 +100097/* don't exec a shell */
98int no_shell_flag = 0;
Damien Miller1383bd82000-04-06 12:32:37 +100099
Damien Miller5428f641999-11-25 11:54:57 +1100100/*
101 * Flag indicating that nothing should be read from stdin. This can be set
102 * on the command line.
103 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104int stdin_null_flag = 0;
105
Damien Miller5428f641999-11-25 11:54:57 +1100106/*
107 * Flag indicating that ssh should fork after authentication. This is useful
Ben Lindstromf666fec2002-06-06 19:51:58 +0000108 * so that the passphrase can be entered manually, and then ssh goes to the
Damien Miller5428f641999-11-25 11:54:57 +1100109 * background.
110 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111int fork_after_authentication_flag = 0;
112
Damien Miller5428f641999-11-25 11:54:57 +1100113/*
114 * General data structure for command line options and options configurable
115 * in configuration files. See readconf.h.
116 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117Options options;
118
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000119/* optional user configfile */
120char *config = NULL;
121
Damien Miller5428f641999-11-25 11:54:57 +1100122/*
123 * Name of the host we are connecting to. This is the name given on the
124 * command line, or the HostName specified for the user-supplied name in a
125 * configuration file.
126 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127char *host;
128
129/* socket address the host resolves to */
Damien Miller34132e52000-01-14 15:45:46 +1100130struct sockaddr_storage hostaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000132/* Private host keys. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000133Sensitive sensitive_data;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134
135/* Original real UID. */
136uid_t original_real_uid;
Ben Lindstromf9c48842002-06-11 16:37:51 +0000137uid_t original_effective_uid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138
Damien Miller1383bd82000-04-06 12:32:37 +1000139/* command to be executed */
140Buffer command;
141
Damien Miller832562e2001-01-30 09:30:01 +1100142/* Should we execute a command or invoke a subsystem? */
143int subsystem_flag = 0;
144
Damien Miller2797f7f2002-04-23 21:09:44 +1000145/* # of replies received for global requests */
146static int client_global_request_id = 0;
147
Damien Miller8c4e18a2002-09-19 12:05:02 +1000148/* pid of proxycommand child process */
149pid_t proxy_command_pid = 0;
150
Damien Miller0e220db2004-06-15 10:34:08 +1000151/* fd to control socket */
152int control_fd = -1;
153
Darren Tucker7ebfc102004-11-07 20:06:19 +1100154/* Multiplexing control command */
Darren Tucker0814d312005-06-01 23:08:51 +1000155static u_int mux_command = 0;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100156
Damien Miller0e220db2004-06-15 10:34:08 +1000157/* Only used in control client mode */
158volatile sig_atomic_t control_client_terminate = 0;
159u_int control_server_pid = 0;
160
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000161/* Prints a help message to the user. This function never returns. */
162
Ben Lindstrombba81212001-06-25 05:01:22 +0000163static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000164usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165{
Damien Miller50955102004-03-22 09:34:58 +1100166 fprintf(stderr,
Darren Tucker9c6bf322004-12-03 14:10:19 +1100167"usage: ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n"
Darren Tucker895d6982005-10-03 18:18:05 +1000168" [-D [bind_address:]port] [-e escape_char] [-F configfile]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100169" [-i identity_file] [-L [bind_address:]port:host:hostport]\n"
Damien Millerf91ee4c2005-03-01 21:24:33 +1100170" [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100171" [-R [bind_address:]port:host:hostport] [-S ctl_path]\n"
Damien Millerd27b9472005-12-13 19:29:02 +1100172" [-w tunnel:tunnel] [user@]hostname [command]\n"
Damien Miller50955102004-03-22 09:34:58 +1100173 );
Darren Tuckere9a9b712005-12-20 16:15:51 +1100174 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000175}
176
Ben Lindstrombba81212001-06-25 05:01:22 +0000177static int ssh_session(void);
178static int ssh_session2(void);
179static void load_public_identity_files(void);
Damien Miller0e220db2004-06-15 10:34:08 +1000180static void control_client(const char *path);
Damien Miller1383bd82000-04-06 12:32:37 +1000181
Damien Miller95def091999-11-25 00:26:21 +1100182/*
183 * Main program for the ssh client.
184 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000185int
186main(int ac, char **av)
187{
Ben Lindstrom24157572002-06-12 16:09:39 +0000188 int i, opt, exit_status;
Damien Miller9836cf82003-12-17 16:30:06 +1100189 char *p, *cp, *line, buf[256];
Damien Miller95def091999-11-25 00:26:21 +1100190 struct stat st;
Ben Lindstrom086cf212001-03-05 05:56:40 +0000191 struct passwd *pw;
Damien Miller1383bd82000-04-06 12:32:37 +1000192 int dummy;
Ben Lindstrom5ccf63a2001-09-24 20:00:10 +0000193 extern int optind, optreset;
Damien Miller4f8e6692001-07-14 13:22:53 +1000194 extern char *optarg;
Damien Miller9651fe62005-06-26 08:55:25 +1000195 struct servent *sp;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100196 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000197
Darren Tuckerce321d82005-10-03 18:11:24 +1000198 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
199 sanitise_stdfd();
200
Damien Miller59d3d5b2003-08-22 09:34:41 +1000201 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000202 init_rng();
203
Damien Miller5428f641999-11-25 11:54:57 +1100204 /*
205 * Save the original real uid. It will be needed later (uid-swapping
206 * may clobber the real uid).
207 */
Damien Miller95def091999-11-25 00:26:21 +1100208 original_real_uid = getuid();
209 original_effective_uid = geteuid();
Damien Millera8e06ce2003-11-21 23:48:55 +1100210
Damien Miller50b9a602002-09-04 16:50:06 +1000211 /*
212 * Use uid-swapping to give up root privileges for the duration of
213 * option processing. We will re-instantiate the rights when we are
214 * ready to create the privileged port, and will permanently drop
215 * them when the port has been created (actually, when the connection
216 * has been made, as we may need to create the port several times).
217 */
218 PRIV_END;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000219
Damien Miller05dd7952000-10-01 00:42:48 +1100220#ifdef HAVE_SETRLIMIT
Damien Miller95def091999-11-25 00:26:21 +1100221 /* If we are installed setuid root be careful to not drop core. */
222 if (original_real_uid != original_effective_uid) {
223 struct rlimit rlim;
224 rlim.rlim_cur = rlim.rlim_max = 0;
225 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
226 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000227 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100228#endif
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000229 /* Get user data. */
230 pw = getpwuid(original_real_uid);
231 if (!pw) {
Damien Miller996acd22003-04-09 20:59:48 +1000232 logit("You don't exist, go away!");
Darren Tuckere9a9b712005-12-20 16:15:51 +1100233 exit(255);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000234 }
235 /* Take a copy of the returned structure. */
236 pw = pwcopy(pw);
237
Damien Miller5428f641999-11-25 11:54:57 +1100238 /*
Damien Miller5428f641999-11-25 11:54:57 +1100239 * Set our umask to something reasonable, as some files are created
240 * with the default umask. This will make them world-readable but
241 * writable only by the owner, which is ok for all files for which we
242 * don't set the modes explicitly.
243 */
Damien Miller95def091999-11-25 00:26:21 +1100244 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000245
Damien Miller95def091999-11-25 00:26:21 +1100246 /* Initialize option structure to indicate that no values have been set. */
247 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248
Damien Miller95def091999-11-25 00:26:21 +1100249 /* Parse command-line arguments. */
250 host = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000251
Damien Millerf4614452001-07-14 12:18:10 +1000252again:
253 while ((opt = getopt(ac, av,
Damien Millerd27b9472005-12-13 19:29:02 +1100254 "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 +1100255 switch (opt) {
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000256 case '1':
257 options.protocol = SSH_PROTO_1;
258 break;
Damien Miller4af51302000-04-16 11:18:38 +1000259 case '2':
260 options.protocol = SSH_PROTO_2;
261 break;
Damien Miller34132e52000-01-14 15:45:46 +1100262 case '4':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000263 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100264 break;
Damien Miller34132e52000-01-14 15:45:46 +1100265 case '6':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000266 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100267 break;
Damien Miller95def091999-11-25 00:26:21 +1100268 case 'n':
269 stdin_null_flag = 1;
270 break;
Damien Miller95def091999-11-25 00:26:21 +1100271 case 'f':
272 fork_after_authentication_flag = 1;
273 stdin_null_flag = 1;
274 break;
Damien Miller95def091999-11-25 00:26:21 +1100275 case 'x':
276 options.forward_x11 = 0;
277 break;
Damien Miller95def091999-11-25 00:26:21 +1100278 case 'X':
279 options.forward_x11 = 1;
280 break;
Darren Tucker0a118da2003-10-15 15:54:32 +1000281 case 'Y':
282 options.forward_x11 = 1;
283 options.forward_x11_trusted = 1;
284 break;
Damien Miller95def091999-11-25 00:26:21 +1100285 case 'g':
286 options.gateway_ports = 1;
287 break;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100288 case 'O':
289 if (strcmp(optarg, "check") == 0)
290 mux_command = SSHMUX_COMMAND_ALIVE_CHECK;
291 else if (strcmp(optarg, "exit") == 0)
292 mux_command = SSHMUX_COMMAND_TERMINATE;
293 else
294 fatal("Invalid multiplex command.");
295 break;
Damien Miller147bba32002-09-04 16:46:06 +1000296 case 'P': /* deprecated */
Damien Miller95def091999-11-25 00:26:21 +1100297 options.use_privileged_port = 0;
298 break;
Damien Miller95def091999-11-25 00:26:21 +1100299 case 'a':
300 options.forward_agent = 0;
301 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000302 case 'A':
303 options.forward_agent = 1;
304 break;
Damien Miller95def091999-11-25 00:26:21 +1100305 case 'k':
Damien Millere0113cc2003-11-24 13:10:09 +1100306 options.gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100307 break;
Damien Miller95def091999-11-25 00:26:21 +1100308 case 'i':
309 if (stat(optarg, &st) < 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000310 fprintf(stderr, "Warning: Identity file %s "
Damien Miller3eb48b62005-03-01 21:15:46 +1100311 "not accessible: %s.\n", optarg,
312 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100313 break;
314 }
Damien Millerf4614452001-07-14 12:18:10 +1000315 if (options.num_identity_files >=
316 SSH_MAX_IDENTITY_FILES)
317 fatal("Too many identity files specified "
318 "(max %d)", SSH_MAX_IDENTITY_FILES);
319 options.identity_files[options.num_identity_files++] =
320 xstrdup(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100321 break;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000322 case 'I':
323#ifdef SMARTCARD
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000324 options.smartcard_device = xstrdup(optarg);
Ben Lindstrombcc18082001-08-06 21:59:25 +0000325#else
Ben Lindstromc5b68002001-07-04 04:52:03 +0000326 fprintf(stderr, "no support for smartcards.\n");
Ben Lindstrombcc18082001-08-06 21:59:25 +0000327#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000328 break;
Damien Miller95def091999-11-25 00:26:21 +1100329 case 't':
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000330 if (tty_flag)
331 force_tty_flag = 1;
Damien Miller95def091999-11-25 00:26:21 +1100332 tty_flag = 1;
333 break;
Damien Miller95def091999-11-25 00:26:21 +1100334 case 'v':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000335 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100336 debug_flag = 1;
337 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000338 } else {
339 if (options.log_level < SYSLOG_LEVEL_DEBUG3)
340 options.log_level++;
Damien Millere4340be2000-09-16 13:29:08 +1100341 break;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000342 }
Darren Tuckere9bf9842004-11-05 20:05:32 +1100343 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100344 case 'V':
Damien Miller0c889cd2004-03-22 09:36:00 +1100345 fprintf(stderr, "%s, %s\n",
Damien Miller2aa6d3c2004-09-12 16:53:04 +1000346 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
Damien Miller95def091999-11-25 00:26:21 +1100347 if (opt == 'V')
348 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100349 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100350 case 'w':
Damien Miller7b58e802005-12-13 19:33:19 +1100351 if (options.tun_open == -1)
352 options.tun_open = SSH_TUNMODE_DEFAULT;
Damien Millerd27b9472005-12-13 19:29:02 +1100353 options.tun_local = a2tun(optarg, &options.tun_remote);
Damien Miller7b58e802005-12-13 19:33:19 +1100354 if (options.tun_local == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100355 fprintf(stderr, "Bad tun device '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100356 exit(255);
Damien Millerd27b9472005-12-13 19:29:02 +1100357 }
358 break;
Damien Miller95def091999-11-25 00:26:21 +1100359 case 'q':
360 options.log_level = SYSLOG_LEVEL_QUIET;
361 break;
Damien Miller95def091999-11-25 00:26:21 +1100362 case 'e':
363 if (optarg[0] == '^' && optarg[2] == 0 &&
Damien Millerf4614452001-07-14 12:18:10 +1000364 (u_char) optarg[1] >= 64 &&
365 (u_char) optarg[1] < 128)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000366 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100367 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000368 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100369 else if (strcmp(optarg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000370 options.escape_char = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100371 else {
Damien Millerf4614452001-07-14 12:18:10 +1000372 fprintf(stderr, "Bad escape character '%s'.\n",
373 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100374 exit(255);
Damien Miller95def091999-11-25 00:26:21 +1100375 }
376 break;
Damien Miller95def091999-11-25 00:26:21 +1100377 case 'c':
Damien Millereba71ba2000-04-29 23:57:08 +1000378 if (ciphers_valid(optarg)) {
379 /* SSH2 only */
380 options.ciphers = xstrdup(optarg);
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000381 options.cipher = SSH_CIPHER_INVALID;
Damien Millereba71ba2000-04-29 23:57:08 +1000382 } else {
383 /* SSH1 only */
Damien Millere39cacc2000-11-29 12:18:44 +1100384 options.cipher = cipher_number(optarg);
385 if (options.cipher == -1) {
Damien Millerf4614452001-07-14 12:18:10 +1000386 fprintf(stderr,
387 "Unknown cipher type '%s'\n",
388 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100389 exit(255);
Damien Millereba71ba2000-04-29 23:57:08 +1000390 }
Damien Millerf4614452001-07-14 12:18:10 +1000391 if (options.cipher == SSH_CIPHER_3DES)
Damien Millere39cacc2000-11-29 12:18:44 +1100392 options.ciphers = "3des-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000393 else if (options.cipher == SSH_CIPHER_BLOWFISH)
Damien Millere39cacc2000-11-29 12:18:44 +1100394 options.ciphers = "blowfish-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000395 else
Damien Millere39cacc2000-11-29 12:18:44 +1100396 options.ciphers = (char *)-1;
Damien Miller95def091999-11-25 00:26:21 +1100397 }
398 break;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000399 case 'm':
400 if (mac_valid(optarg))
401 options.macs = xstrdup(optarg);
402 else {
Damien Millerf4614452001-07-14 12:18:10 +1000403 fprintf(stderr, "Unknown mac type '%s'\n",
404 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100405 exit(255);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000406 }
407 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000408 case 'M':
Damien Millerd14b1e72005-06-16 13:19:41 +1000409 if (options.control_master == SSHCTL_MASTER_YES)
410 options.control_master = SSHCTL_MASTER_ASK;
411 else
412 options.control_master = SSHCTL_MASTER_YES;
Damien Miller0e220db2004-06-15 10:34:08 +1000413 break;
Damien Miller95def091999-11-25 00:26:21 +1100414 case 'p':
Ben Lindstrom19066a12001-04-12 23:39:26 +0000415 options.port = a2port(optarg);
416 if (options.port == 0) {
Ben Lindstrom146edb92001-04-11 23:06:28 +0000417 fprintf(stderr, "Bad port '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100418 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000419 }
Damien Miller95def091999-11-25 00:26:21 +1100420 break;
Damien Miller95def091999-11-25 00:26:21 +1100421 case 'l':
422 options.user = optarg;
423 break;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000424
Damien Miller95def091999-11-25 00:26:21 +1100425 case 'L':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100426 if (parse_forward(&fwd, optarg))
427 add_local_forward(&options, &fwd);
428 else {
Damien Millerf4614452001-07-14 12:18:10 +1000429 fprintf(stderr,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100430 "Bad local forwarding specification '%s'\n",
Damien Millerf4614452001-07-14 12:18:10 +1000431 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100432 exit(255);
Ben Lindstrom1a174712001-09-12 17:56:15 +0000433 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100434 break;
435
436 case 'R':
437 if (parse_forward(&fwd, optarg)) {
438 add_remote_forward(&options, &fwd);
439 } else {
440 fprintf(stderr,
441 "Bad remote forwarding specification "
442 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100443 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100444 }
Damien Miller95def091999-11-25 00:26:21 +1100445 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000446
447 case 'D':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100448 cp = p = xstrdup(optarg);
449 memset(&fwd, '\0', sizeof(fwd));
450 fwd.connect_host = "socks";
451 if ((fwd.listen_host = hpdelim(&cp)) == NULL) {
452 fprintf(stderr, "Bad dynamic forwarding "
453 "specification '%.100s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100454 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100455 }
456 if (cp != NULL) {
457 fwd.listen_port = a2port(cp);
458 fwd.listen_host = cleanhostname(fwd.listen_host);
459 } else {
460 fwd.listen_port = a2port(fwd.listen_host);
Damien Millerbe1045d2005-08-12 22:10:56 +1000461 fwd.listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100462 }
463
464 if (fwd.listen_port == 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000465 fprintf(stderr, "Bad dynamic port '%s'\n",
466 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100467 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000468 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100469 add_local_forward(&options, &fwd);
470 xfree(p);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000471 break;
472
Damien Miller95def091999-11-25 00:26:21 +1100473 case 'C':
474 options.compression = 1;
475 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000476 case 'N':
477 no_shell_flag = 1;
478 no_tty_flag = 1;
479 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000480 case 'T':
481 no_tty_flag = 1;
482 break;
Damien Miller95def091999-11-25 00:26:21 +1100483 case 'o':
484 dummy = 1;
Damien Miller9836cf82003-12-17 16:30:06 +1100485 line = xstrdup(optarg);
Damien Millerf4614452001-07-14 12:18:10 +1000486 if (process_config_line(&options, host ? host : "",
Damien Miller9836cf82003-12-17 16:30:06 +1100487 line, "command-line", 0, &dummy) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100488 exit(255);
Damien Miller9836cf82003-12-17 16:30:06 +1100489 xfree(line);
Damien Miller95def091999-11-25 00:26:21 +1100490 break;
Damien Miller832562e2001-01-30 09:30:01 +1100491 case 's':
492 subsystem_flag = 1;
493 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000494 case 'S':
495 if (options.control_path != NULL)
496 free(options.control_path);
497 options.control_path = xstrdup(optarg);
Damien Miller0e220db2004-06-15 10:34:08 +1000498 break;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000499 case 'b':
500 options.bind_address = optarg;
501 break;
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000502 case 'F':
503 config = optarg;
504 break;
Damien Miller95def091999-11-25 00:26:21 +1100505 default:
506 usage();
507 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000508 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000509
Damien Millerf4614452001-07-14 12:18:10 +1000510 ac -= optind;
511 av += optind;
512
513 if (ac > 0 && !host && **av != '-') {
Ben Lindstromc276c122002-12-23 02:14:51 +0000514 if (strrchr(*av, '@')) {
Damien Millerf4614452001-07-14 12:18:10 +1000515 p = xstrdup(*av);
Ben Lindstromc276c122002-12-23 02:14:51 +0000516 cp = strrchr(p, '@');
Damien Millerf4614452001-07-14 12:18:10 +1000517 if (cp == NULL || cp == p)
518 usage();
519 options.user = p;
520 *cp = '\0';
521 host = ++cp;
522 } else
523 host = *av;
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000524 if (ac > 1) {
525 optind = optreset = 1;
Damien Millerf4614452001-07-14 12:18:10 +1000526 goto again;
527 }
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000528 ac--, av++;
Damien Millerf4614452001-07-14 12:18:10 +1000529 }
530
Damien Miller95def091999-11-25 00:26:21 +1100531 /* Check that we got a host name. */
532 if (!host)
533 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000534
Damien Millercb5e44a2000-09-29 12:12:36 +1100535 SSLeay_add_all_algorithms();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100536 ERR_load_crypto_strings();
Damien Millercb5e44a2000-09-29 12:12:36 +1100537
Damien Miller95def091999-11-25 00:26:21 +1100538 /* Initialize the command to execute on remote host. */
539 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000540
Damien Miller5428f641999-11-25 11:54:57 +1100541 /*
542 * Save the command to execute on the remote host in a buffer. There
543 * is no limit on the length of the command, except by the maximum
544 * packet size. Also sets the tty flag if there is no command.
545 */
Damien Millerf4614452001-07-14 12:18:10 +1000546 if (!ac) {
Damien Miller95def091999-11-25 00:26:21 +1100547 /* No command specified - execute shell on a tty. */
548 tty_flag = 1;
Damien Miller832562e2001-01-30 09:30:01 +1100549 if (subsystem_flag) {
Damien Millerf4614452001-07-14 12:18:10 +1000550 fprintf(stderr,
551 "You must specify a subsystem to invoke.\n");
Damien Miller832562e2001-01-30 09:30:01 +1100552 usage();
553 }
Damien Miller95def091999-11-25 00:26:21 +1100554 } else {
Damien Millerf4614452001-07-14 12:18:10 +1000555 /* A command has been specified. Store it into the buffer. */
556 for (i = 0; i < ac; i++) {
557 if (i)
Damien Miller95def091999-11-25 00:26:21 +1100558 buffer_append(&command, " ", 1);
559 buffer_append(&command, av[i], strlen(av[i]));
560 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000561 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000562
Damien Miller95def091999-11-25 00:26:21 +1100563 /* Cannot fork to background if no command. */
Damien Millercaf6dd62000-08-29 11:33:50 +1100564 if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
Damien Miller95def091999-11-25 00:26:21 +1100565 fatal("Cannot fork into background without a command to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000566
Damien Miller95def091999-11-25 00:26:21 +1100567 /* Allocate a tty by default if no command specified. */
568 if (buffer_len(&command) == 0)
569 tty_flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000570
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000571 /* Force no tty */
Ben Lindstromc72745a2000-12-02 19:03:54 +0000572 if (no_tty_flag)
573 tty_flag = 0;
Damien Miller95def091999-11-25 00:26:21 +1100574 /* Do not allocate a tty if stdin is not a tty. */
Damien Millerddee5752005-05-26 12:05:05 +1000575 if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100576 if (tty_flag)
Damien Miller996acd22003-04-09 20:59:48 +1000577 logit("Pseudo-terminal will not be allocated because stdin is not a terminal.");
Damien Miller95def091999-11-25 00:26:21 +1100578 tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000579 }
Damien Miller1383bd82000-04-06 12:32:37 +1000580
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000581 /*
582 * Initialize "log" output. Since we are the client all output
583 * actually goes to stderr.
584 */
Ben Lindstrom2b646522001-04-12 16:16:57 +0000585 log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
586 SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000587
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000588 /*
589 * Read per-user configuration file. Ignore the system wide config
590 * file if the user specifies a config file on the command line.
591 */
592 if (config != NULL) {
Damien Miller914420f2004-04-20 20:14:07 +1000593 if (!read_config_file(config, host, &options, 0))
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000594 fatal("Can't open user config file %.100s: "
595 "%.100s", config, strerror(errno));
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000596 } else {
597 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
598 _PATH_SSH_USER_CONFFILE);
Damien Miller57a44762004-04-20 20:11:57 +1000599 (void)read_config_file(buf, host, &options, 1);
Ben Lindstrom83f07d12001-10-03 17:22:29 +0000600
601 /* Read systemwide configuration file after use config. */
Darren Tuckerfc959702004-07-17 16:12:08 +1000602 (void)read_config_file(_PATH_HOST_CONFIG_FILE, host,
Damien Miller57a44762004-04-20 20:11:57 +1000603 &options, 0);
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000604 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000605
Damien Miller95def091999-11-25 00:26:21 +1100606 /* Fill configuration defaults. */
607 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000608
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000609 channel_set_af(options.address_family);
610
Damien Miller95def091999-11-25 00:26:21 +1100611 /* reinit */
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000612 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000613
Damien Miller60bc5172001-03-19 09:38:15 +1100614 seed_rng();
615
Damien Miller95def091999-11-25 00:26:21 +1100616 if (options.user == NULL)
617 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000618
Damien Miller95def091999-11-25 00:26:21 +1100619 if (options.hostname != NULL)
620 host = options.hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000621
Darren Tucker3f521e22003-07-03 16:20:42 +1000622 /* force lowercase for hostkey matching */
623 if (options.host_key_alias != NULL) {
624 for (p = options.host_key_alias; *p; p++)
625 if (isupper(*p))
626 *p = tolower(*p);
627 }
628
Damien Miller7c71cc72005-06-26 08:56:31 +1000629 /* Get default port if port has not been set. */
630 if (options.port == 0) {
631 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
632 options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
633 }
634
Damien Miller9f1e33a2003-02-24 11:57:32 +1100635 if (options.proxy_command != NULL &&
636 strcmp(options.proxy_command, "none") == 0)
637 options.proxy_command = NULL;
Damien Miller8f74c8f2005-06-26 08:56:03 +1000638 if (options.control_path != NULL &&
639 strcmp(options.control_path, "none") == 0)
640 options.control_path = NULL;
Damien Miller9f1e33a2003-02-24 11:57:32 +1100641
Damien Miller0e220db2004-06-15 10:34:08 +1000642 if (options.control_path != NULL) {
Damien Miller3ec54c72006-03-15 11:30:13 +1100643 char me[NI_MAXHOST];
644
645 if (gethostname(me, sizeof(me)) == -1)
646 fatal("gethostname: %s", strerror(errno));
Damien Miller6476cad2005-06-16 13:18:34 +1000647 snprintf(buf, sizeof(buf), "%d", options.port);
648 cp = tilde_expand_filename(options.control_path,
649 original_real_uid);
650 options.control_path = percent_expand(cp, "p", buf, "h", host,
Damien Miller3ec54c72006-03-15 11:30:13 +1100651 "r", options.user, "l", me, (char *)NULL);
Damien Miller6476cad2005-06-16 13:18:34 +1000652 xfree(cp);
Damien Miller0e220db2004-06-15 10:34:08 +1000653 }
Darren Tucker0814d312005-06-01 23:08:51 +1000654 if (mux_command != 0 && options.control_path == NULL)
655 fatal("No ControlPath specified for \"-O\" command");
Damien Millerd14b1e72005-06-16 13:19:41 +1000656 if (options.control_path != NULL)
Damien Millerdadfd4d2005-05-26 12:07:13 +1000657 control_client(options.control_path);
Damien Miller0e220db2004-06-15 10:34:08 +1000658
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000659 /* Open a connection to the remote host. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000660 if (ssh_connect(host, &hostaddr, options.port,
661 options.address_family, options.connection_attempts,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000662#ifdef HAVE_CYGWIN
663 options.use_privileged_port,
664#else
Ben Lindstromf9c48842002-06-11 16:37:51 +0000665 original_effective_uid == 0 && options.use_privileged_port,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000666#endif
Ben Lindstromda394ca2002-06-12 16:11:12 +0000667 options.proxy_command) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100668 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000669
Damien Miller5428f641999-11-25 11:54:57 +1100670 /*
671 * If we successfully made the connection, load the host private key
672 * in case we will need it later for combined rsa-rhosts
673 * authentication. This must be done before releasing extra
674 * privileges, because the file is only readable by root.
Ben Lindstrom9e5bb572002-06-06 19:58:27 +0000675 * If we cannot access the private keys, load the public keys
676 * instead and try to execute the ssh-keysign helper instead.
Damien Miller5428f641999-11-25 11:54:57 +1100677 */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000678 sensitive_data.nkeys = 0;
679 sensitive_data.keys = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000680 sensitive_data.external_keysign = 0;
Ben Lindstrom24157572002-06-12 16:09:39 +0000681 if (options.rhosts_rsa_authentication ||
682 options.hostbased_authentication) {
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000683 sensitive_data.nkeys = 3;
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000684 sensitive_data.keys = xmalloc(sensitive_data.nkeys *
685 sizeof(Key));
Ben Lindstromf9c48842002-06-11 16:37:51 +0000686
687 PRIV_START;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000688 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
Ben Lindstromd0fca422001-03-26 13:44:06 +0000689 _PATH_HOST_KEY_FILE, "", NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000690 sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
691 _PATH_HOST_DSA_KEY_FILE, "", NULL);
692 sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
693 _PATH_HOST_RSA_KEY_FILE, "", NULL);
Ben Lindstromf9c48842002-06-11 16:37:51 +0000694 PRIV_END;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000695
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +0000696 if (options.hostbased_authentication == 1 &&
697 sensitive_data.keys[0] == NULL &&
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000698 sensitive_data.keys[1] == NULL &&
699 sensitive_data.keys[2] == NULL) {
700 sensitive_data.keys[1] = key_load_public(
701 _PATH_HOST_DSA_KEY_FILE, NULL);
702 sensitive_data.keys[2] = key_load_public(
703 _PATH_HOST_RSA_KEY_FILE, NULL);
704 sensitive_data.external_keysign = 1;
705 }
Damien Miller95def091999-11-25 00:26:21 +1100706 }
Damien Miller5428f641999-11-25 11:54:57 +1100707 /*
708 * Get rid of any extra privileges that we may have. We will no
709 * longer need them. Also, extra privileges could make it very hard
710 * to read identity files and other non-world-readable files from the
711 * user's home directory if it happens to be on a NFS volume where
712 * root is mapped to nobody.
713 */
Darren Tucker25f60a72004-08-15 17:23:34 +1000714 if (original_effective_uid == 0) {
715 PRIV_START;
716 permanently_set_uid(pw);
717 }
Damien Miller95def091999-11-25 00:26:21 +1100718
Damien Miller5428f641999-11-25 11:54:57 +1100719 /*
720 * Now that we are back to our own permissions, create ~/.ssh
Damien Miller788f2122005-11-05 15:14:59 +1100721 * directory if it doesn't already exist.
Damien Miller5428f641999-11-25 11:54:57 +1100722 */
Ben Lindstrom930b14a2001-08-15 23:19:21 +0000723 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 +1100724 if (stat(buf, &st) < 0)
Damien Millerbe484b52000-07-15 14:14:16 +1000725 if (mkdir(buf, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100726 error("Could not create directory '%.200s'.", buf);
727
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000728 /* load options.identity_files */
729 load_public_identity_files();
730
731 /* Expand ~ in known host file names. */
732 /* XXX mem-leaks: */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100733 options.system_hostfile =
734 tilde_expand_filename(options.system_hostfile, original_real_uid);
735 options.user_hostfile =
736 tilde_expand_filename(options.user_hostfile, original_real_uid);
737 options.system_hostfile2 =
738 tilde_expand_filename(options.system_hostfile2, original_real_uid);
739 options.user_hostfile2 =
740 tilde_expand_filename(options.user_hostfile2, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100741
Damien Miller07cd5892001-11-12 10:52:03 +1100742 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
743
Damien Miller95def091999-11-25 00:26:21 +1100744 /* Log into the remote system. This never returns if the login fails. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000745 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, pw);
Damien Miller95def091999-11-25 00:26:21 +1100746
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000747 /* We no longer need the private host keys. Clear them now. */
748 if (sensitive_data.nkeys != 0) {
749 for (i = 0; i < sensitive_data.nkeys; i++) {
750 if (sensitive_data.keys[i] != NULL) {
751 /* Destroys contents safely */
752 debug3("clear hostkey %d", i);
753 key_free(sensitive_data.keys[i]);
754 sensitive_data.keys[i] = NULL;
755 }
756 }
757 xfree(sensitive_data.keys);
758 }
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +0000759 for (i = 0; i < options.num_identity_files; i++) {
760 if (options.identity_files[i]) {
761 xfree(options.identity_files[i]);
762 options.identity_files[i] = NULL;
763 }
764 if (options.identity_keys[i]) {
765 key_free(options.identity_keys[i]);
766 options.identity_keys[i] = NULL;
767 }
768 }
Damien Miller95def091999-11-25 00:26:21 +1100769
Damien Miller1383bd82000-04-06 12:32:37 +1000770 exit_status = compat20 ? ssh_session2() : ssh_session();
771 packet_close();
Damien Miller8c4e18a2002-09-19 12:05:02 +1000772
Damien Miller0e220db2004-06-15 10:34:08 +1000773 if (options.control_path != NULL && control_fd != -1)
774 unlink(options.control_path);
775
Damien Miller8c4e18a2002-09-19 12:05:02 +1000776 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100777 * Send SIGHUP to proxy command if used. We don't wait() in
Damien Miller8c4e18a2002-09-19 12:05:02 +1000778 * case it hangs and instead rely on init to reap the child
779 */
780 if (proxy_command_pid > 1)
781 kill(proxy_command_pid, SIGHUP);
782
Damien Miller1383bd82000-04-06 12:32:37 +1000783 return exit_status;
784}
785
Ben Lindstrombba81212001-06-25 05:01:22 +0000786static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100787ssh_init_forwarding(void)
788{
Kevin Steves12057502001-02-05 14:54:34 +0000789 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100790 int i;
Kevin Steves12057502001-02-05 14:54:34 +0000791
Damien Miller0bc1bd82000-11-13 22:57:25 +1100792 /* Initiate local TCP/IP port forwardings. */
793 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100794 debug("Local connections to %.200s:%d forwarded to remote "
795 "address %.200s:%d",
Darren Tucker47eede72005-03-14 23:08:12 +1100796 (options.local_forwards[i].listen_host == NULL) ?
797 (options.gateway_ports ? "*" : "LOCALHOST") :
Damien Millerf91ee4c2005-03-01 21:24:33 +1100798 options.local_forwards[i].listen_host,
799 options.local_forwards[i].listen_port,
800 options.local_forwards[i].connect_host,
801 options.local_forwards[i].connect_port);
Damien Millerb16461c2002-01-22 23:29:22 +1100802 success += channel_setup_local_fwd_listener(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100803 options.local_forwards[i].listen_host,
804 options.local_forwards[i].listen_port,
805 options.local_forwards[i].connect_host,
806 options.local_forwards[i].connect_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100807 options.gateway_ports);
808 }
Kevin Steves12057502001-02-05 14:54:34 +0000809 if (i > 0 && success == 0)
810 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100811
812 /* Initiate remote TCP/IP port forwardings. */
813 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100814 debug("Remote connections from %.200s:%d forwarded to "
815 "local address %.200s:%d",
Damien Miller46d38de2005-07-17 17:02:09 +1000816 (options.remote_forwards[i].listen_host == NULL) ?
Damien Milleraa3bb102005-11-05 15:12:59 +1100817 "LOCALHOST" : options.remote_forwards[i].listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100818 options.remote_forwards[i].listen_port,
819 options.remote_forwards[i].connect_host,
820 options.remote_forwards[i].connect_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100821 channel_request_remote_forwarding(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100822 options.remote_forwards[i].listen_host,
823 options.remote_forwards[i].listen_port,
824 options.remote_forwards[i].connect_host,
825 options.remote_forwards[i].connect_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100826 }
827}
828
Ben Lindstrombba81212001-06-25 05:01:22 +0000829static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100830check_agent_present(void)
831{
832 if (options.forward_agent) {
Damien Miller788f2122005-11-05 15:14:59 +1100833 /* Clear agent forwarding if we don't have an agent. */
Damien Miller789e95d2002-09-12 09:52:46 +1000834 if (!ssh_agent_present())
Damien Miller0bc1bd82000-11-13 22:57:25 +1100835 options.forward_agent = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100836 }
837}
838
Ben Lindstrombba81212001-06-25 05:01:22 +0000839static int
Damien Miller1383bd82000-04-06 12:32:37 +1000840ssh_session(void)
841{
842 int type;
Damien Miller1383bd82000-04-06 12:32:37 +1000843 int interactive = 0;
844 int have_tty = 0;
845 struct winsize ws;
Damien Miller1383bd82000-04-06 12:32:37 +1000846 char *cp;
Damien Miller17e7ed02005-06-17 12:54:33 +1000847 const char *display;
Damien Miller1383bd82000-04-06 12:32:37 +1000848
Damien Miller95def091999-11-25 00:26:21 +1100849 /* Enable compression if requested. */
850 if (options.compression) {
851 debug("Requesting compression at level %d.", options.compression_level);
852
853 if (options.compression_level < 1 || options.compression_level > 9)
854 fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
855
856 /* Send the request. */
857 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
858 packet_put_int(options.compression_level);
859 packet_send();
860 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100861 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100862 if (type == SSH_SMSG_SUCCESS)
863 packet_start_compression(options.compression_level);
864 else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000865 logit("Warning: Remote host refused compression.");
Damien Miller95def091999-11-25 00:26:21 +1100866 else
867 packet_disconnect("Protocol error waiting for compression response.");
868 }
869 /* Allocate a pseudo tty if appropriate. */
870 if (tty_flag) {
871 debug("Requesting pty.");
872
873 /* Start the packet. */
874 packet_start(SSH_CMSG_REQUEST_PTY);
875
876 /* Store TERM in the packet. There is no limit on the
877 length of the string. */
878 cp = getenv("TERM");
879 if (!cp)
880 cp = "";
Ben Lindstrom664408d2001-06-09 01:42:01 +0000881 packet_put_cstring(cp);
Damien Miller95def091999-11-25 00:26:21 +1100882
883 /* Store window size in the packet. */
884 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
885 memset(&ws, 0, sizeof(ws));
886 packet_put_int(ws.ws_row);
887 packet_put_int(ws.ws_col);
888 packet_put_int(ws.ws_xpixel);
889 packet_put_int(ws.ws_ypixel);
890
891 /* Store tty modes in the packet. */
Ben Lindstromae8e2d32001-04-14 23:13:02 +0000892 tty_make_modes(fileno(stdin), NULL);
Damien Miller95def091999-11-25 00:26:21 +1100893
894 /* Send the packet, and wait for it to leave. */
895 packet_send();
896 packet_write_wait();
897
898 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100899 type = packet_read();
Damien Miller450a7a12000-03-26 13:04:51 +1000900 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100901 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +1000902 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000903 } else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000904 logit("Warning: Remote host failed or refused to allocate a pseudo tty.");
Damien Miller95def091999-11-25 00:26:21 +1100905 else
906 packet_disconnect("Protocol error waiting for pty request response.");
907 }
908 /* Request X11 forwarding if enabled and DISPLAY is set. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000909 display = getenv("DISPLAY");
910 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000911 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +1000912 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000913 client_x11_get_proto(display, options.xauth_location,
914 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +1000915 /* Request forwarding with authentication spoofing. */
Damien Miller95def091999-11-25 00:26:21 +1100916 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +1000917 x11_request_forwarding_with_spoofing(0, display, proto, data);
Damien Miller95def091999-11-25 00:26:21 +1100918
919 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100920 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100921 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100922 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +1000923 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller996acd22003-04-09 20:59:48 +1000924 logit("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +1000925 } else {
Damien Miller95def091999-11-25 00:26:21 +1100926 packet_disconnect("Protocol error waiting for X11 forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +1000927 }
Damien Miller95def091999-11-25 00:26:21 +1100928 }
929 /* Tell the packet module whether this is an interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000930 packet_set_interactive(interactive);
Damien Miller95def091999-11-25 00:26:21 +1100931
932 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100933 check_agent_present();
934
Damien Miller95def091999-11-25 00:26:21 +1100935 if (options.forward_agent) {
936 debug("Requesting authentication agent forwarding.");
937 auth_request_forwarding();
938
939 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100940 type = packet_read();
Damien Miller48b03fc2002-01-22 23:11:40 +1100941 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100942 if (type != SSH_SMSG_SUCCESS)
Damien Miller996acd22003-04-09 20:59:48 +1000943 logit("Warning: Remote host denied authentication agent forwarding.");
Damien Miller95def091999-11-25 00:26:21 +1100944 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000945
Damien Miller0bc1bd82000-11-13 22:57:25 +1100946 /* Initiate port forwardings. */
947 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000948
Damien Miller5428f641999-11-25 11:54:57 +1100949 /* If requested, let ssh continue in the background. */
Damien Miller4af51302000-04-16 11:18:38 +1000950 if (fork_after_authentication_flag)
Damien Miller5428f641999-11-25 11:54:57 +1100951 if (daemon(1, 1) < 0)
952 fatal("daemon() failed: %.200s", strerror(errno));
953
954 /*
955 * If a command was specified on the command line, execute the
956 * command now. Otherwise request the server to start a shell.
957 */
Damien Miller95def091999-11-25 00:26:21 +1100958 if (buffer_len(&command) > 0) {
959 int len = buffer_len(&command);
960 if (len > 900)
961 len = 900;
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100962 debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
Damien Miller95def091999-11-25 00:26:21 +1100963 packet_start(SSH_CMSG_EXEC_CMD);
964 packet_put_string(buffer_ptr(&command), buffer_len(&command));
965 packet_send();
966 packet_write_wait();
967 } else {
968 debug("Requesting shell.");
969 packet_start(SSH_CMSG_EXEC_SHELL);
970 packet_send();
971 packet_write_wait();
972 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000973
Damien Miller95def091999-11-25 00:26:21 +1100974 /* Enter the interactive session. */
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000975 return client_loop(have_tty, tty_flag ?
976 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
Damien Miller1383bd82000-04-06 12:32:37 +1000977}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000978
Ben Lindstrombba81212001-06-25 05:01:22 +0000979static void
Damien Miller0e220db2004-06-15 10:34:08 +1000980ssh_subsystem_reply(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000981{
982 int id, len;
983
984 id = packet_get_int();
985 len = buffer_len(&command);
Ben Lindstrom4040fe12001-03-05 06:52:57 +0000986 if (len > 900)
987 len = 900;
Damien Miller48b03fc2002-01-22 23:11:40 +1100988 packet_check_eom();
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000989 if (type == SSH2_MSG_CHANNEL_FAILURE)
990 fatal("Request for subsystem '%.*s' failed on channel %d",
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100991 len, (u_char *)buffer_ptr(&command), id);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000992}
993
Damien Miller2797f7f2002-04-23 21:09:44 +1000994void
Damien Miller509b0102003-12-17 16:33:10 +1100995client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt)
Damien Miller2797f7f2002-04-23 21:09:44 +1000996{
997 int i;
998
999 i = client_global_request_id++;
Damien Miller509b0102003-12-17 16:33:10 +11001000 if (i >= options.num_remote_forwards)
Damien Miller2797f7f2002-04-23 21:09:44 +10001001 return;
Damien Miller2797f7f2002-04-23 21:09:44 +10001002 debug("remote forward %s for: listen %d, connect %s:%d",
1003 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
Damien Millerf91ee4c2005-03-01 21:24:33 +11001004 options.remote_forwards[i].listen_port,
1005 options.remote_forwards[i].connect_host,
1006 options.remote_forwards[i].connect_port);
Damien Miller2797f7f2002-04-23 21:09:44 +10001007 if (type == SSH2_MSG_REQUEST_FAILURE)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001008 logit("Warning: remote port forwarding failed for listen "
1009 "port %d", options.remote_forwards[i].listen_port);
Damien Miller2797f7f2002-04-23 21:09:44 +10001010}
1011
Damien Miller0e220db2004-06-15 10:34:08 +10001012static void
1013ssh_control_listener(void)
1014{
1015 struct sockaddr_un addr;
1016 mode_t old_umask;
Damien Miller07b6ff12004-06-15 11:14:45 +10001017 int addr_len;
1018
Damien Millerd14b1e72005-06-16 13:19:41 +10001019 if (options.control_path == NULL ||
1020 options.control_master == SSHCTL_MASTER_NO)
Damien Miller0e220db2004-06-15 10:34:08 +10001021 return;
1022
Damien Millerd14b1e72005-06-16 13:19:41 +10001023 debug("setting up multiplex master socket");
1024
Damien Miller0e220db2004-06-15 10:34:08 +10001025 memset(&addr, '\0', sizeof(addr));
1026 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001027 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001028 strlen(options.control_path) + 1;
1029
1030 if (strlcpy(addr.sun_path, options.control_path,
1031 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1032 fatal("ControlPath too long");
1033
1034 if ((control_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001035 fatal("%s socket(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001036
1037 old_umask = umask(0177);
Damien Miller07b6ff12004-06-15 11:14:45 +10001038 if (bind(control_fd, (struct sockaddr*)&addr, addr_len) == -1) {
Damien Miller0e220db2004-06-15 10:34:08 +10001039 control_fd = -1;
Damien Miller4f10e252005-05-04 15:33:09 +10001040 if (errno == EINVAL || errno == EADDRINUSE)
Damien Miller0e220db2004-06-15 10:34:08 +10001041 fatal("ControlSocket %s already exists",
1042 options.control_path);
1043 else
Damien Miller15d72a02005-11-05 15:07:33 +11001044 fatal("%s bind(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001045 }
1046 umask(old_umask);
1047
1048 if (listen(control_fd, 64) == -1)
Damien Miller15d72a02005-11-05 15:07:33 +11001049 fatal("%s listen(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001050
1051 set_nonblock(control_fd);
1052}
1053
Ben Lindstromf558cf62001-09-20 23:13:49 +00001054/* request pty/x11/agent/tcpfwd/shell for channel */
Ben Lindstrombba81212001-06-25 05:01:22 +00001055static void
Ben Lindstromf558cf62001-09-20 23:13:49 +00001056ssh_session2_setup(int id, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +10001057{
Damien Miller3756dce2004-06-18 01:17:29 +10001058 extern char **environ;
Damien Miller17e7ed02005-06-17 12:54:33 +10001059 const char *display;
Damien Miller0e220db2004-06-15 10:34:08 +10001060 int interactive = tty_flag;
Damien Miller17e7ed02005-06-17 12:54:33 +10001061
Damien Miller46d38de2005-07-17 17:02:09 +10001062 display = getenv("DISPLAY");
Damien Miller17e7ed02005-06-17 12:54:33 +10001063 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001064 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001065 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001066 client_x11_get_proto(display, options.xauth_location,
1067 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001068 /* Request forwarding with authentication spoofing. */
1069 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +10001070 x11_request_forwarding_with_spoofing(id, display, proto, data);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001071 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001072 /* XXX wait for reply */
1073 }
1074
Damien Miller0bc1bd82000-11-13 22:57:25 +11001075 check_agent_present();
1076 if (options.forward_agent) {
1077 debug("Requesting authentication agent forwarding.");
1078 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1079 packet_send();
1080 }
1081
Damien Miller7b58e802005-12-13 19:33:19 +11001082 if (options.tun_open != SSH_TUNMODE_NO) {
Damien Millerd27b9472005-12-13 19:29:02 +11001083 Channel *c;
1084 int fd;
1085
1086 debug("Requesting tun.");
Damien Miller7b58e802005-12-13 19:33:19 +11001087 if ((fd = tun_open(options.tun_local,
1088 options.tun_open)) >= 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11001089 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1090 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1091 0, "tun", 1);
1092 c->datagram = 1;
Damien Miller598bbc22005-12-31 16:33:36 +11001093#if defined(SSH_TUN_FILTER)
1094 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1095 channel_register_filter(c->self, sys_tun_infilter,
1096 sys_tun_outfilter);
1097#endif
Damien Millerd27b9472005-12-13 19:29:02 +11001098 packet_start(SSH2_MSG_CHANNEL_OPEN);
1099 packet_put_cstring("tun@openssh.com");
1100 packet_put_int(c->self);
1101 packet_put_int(c->local_window_max);
1102 packet_put_int(c->local_maxpacket);
Damien Miller7b58e802005-12-13 19:33:19 +11001103 packet_put_int(options.tun_open);
Damien Millerd27b9472005-12-13 19:29:02 +11001104 packet_put_int(options.tun_remote);
1105 packet_send();
1106 }
1107 }
1108
Damien Miller0e220db2004-06-15 10:34:08 +10001109 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
Damien Miller3756dce2004-06-18 01:17:29 +10001110 NULL, fileno(stdin), &command, environ, &ssh_subsystem_reply);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001111
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001112 packet_set_interactive(interactive);
Damien Miller1383bd82000-04-06 12:32:37 +10001113}
1114
Ben Lindstromf558cf62001-09-20 23:13:49 +00001115/* open new channel for a session */
Ben Lindstrombba81212001-06-25 05:01:22 +00001116static int
Ben Lindstromf558cf62001-09-20 23:13:49 +00001117ssh_session2_open(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001118{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001119 Channel *c;
1120 int window, packetmax, in, out, err;
Damien Millerad833b32000-08-23 10:46:23 +10001121
Damien Millercaf6dd62000-08-29 11:33:50 +11001122 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001123 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001124 } else {
1125 in = dup(STDIN_FILENO);
1126 }
1127 out = dup(STDOUT_FILENO);
1128 err = dup(STDERR_FILENO);
1129
1130 if (in < 0 || out < 0 || err < 0)
1131 fatal("dup() in/out/err failed");
1132
Damien Miller69b69aa2000-10-28 14:19:58 +11001133 /* enable nonblocking unless tty */
1134 if (!isatty(in))
1135 set_nonblock(in);
1136 if (!isatty(out))
1137 set_nonblock(out);
1138 if (!isatty(err))
1139 set_nonblock(err);
1140
Damien Millere4340be2000-09-16 13:29:08 +11001141 window = CHAN_SES_WINDOW_DEFAULT;
1142 packetmax = CHAN_SES_PACKET_DEFAULT;
Damien Miller19a59452002-02-19 15:20:57 +11001143 if (tty_flag) {
1144 window >>= 1;
1145 packetmax >>= 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001146 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001147 c = channel_new(
Damien Miller1383bd82000-04-06 12:32:37 +10001148 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001149 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001150 "client-session", /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001151
Ben Lindstromf558cf62001-09-20 23:13:49 +00001152 debug3("ssh_session2_open: channel_new: %d", c->self);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001153
Ben Lindstrom5ec26452001-06-09 00:18:51 +00001154 channel_send_open(c->self);
Ben Lindstromf558cf62001-09-20 23:13:49 +00001155 if (!no_shell_flag)
Damien Miller0e220db2004-06-15 10:34:08 +10001156 channel_register_confirm(c->self, ssh_session2_setup, NULL);
Damien Miller1383bd82000-04-06 12:32:37 +10001157
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001158 return c->self;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001159}
1160
Ben Lindstrombba81212001-06-25 05:01:22 +00001161static int
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001162ssh_session2(void)
1163{
Ben Lindstromf558cf62001-09-20 23:13:49 +00001164 int id = -1;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001165
1166 /* XXX should be pre-session */
1167 ssh_init_forwarding();
Damien Miller0e220db2004-06-15 10:34:08 +10001168 ssh_control_listener();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001169
Ben Lindstromf558cf62001-09-20 23:13:49 +00001170 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1171 id = ssh_session2_open();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001172
Damien Millerd27b9472005-12-13 19:29:02 +11001173 /* Execute a local command */
1174 if (options.local_command != NULL &&
1175 options.permit_local_command)
1176 ssh_local_cmd(options.local_command);
1177
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001178 /* If requested, let ssh continue in the background. */
1179 if (fork_after_authentication_flag)
1180 if (daemon(1, 1) < 0)
1181 fatal("daemon() failed: %.200s", strerror(errno));
1182
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001183 return client_loop(tty_flag, tty_flag ?
1184 options.escape_char : SSH_ESCAPECHAR_NONE, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001185}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001186
Ben Lindstrombba81212001-06-25 05:01:22 +00001187static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001188load_public_identity_files(void)
1189{
1190 char *filename;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001191 int i = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001192 Key *public;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001193#ifdef SMARTCARD
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001194 Key **keys;
1195
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001196 if (options.smartcard_device != NULL &&
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001197 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1198 (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL ) {
1199 int count = 0;
1200 for (i = 0; keys[i] != NULL; i++) {
1201 count++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001202 memmove(&options.identity_files[1], &options.identity_files[0],
1203 sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
1204 memmove(&options.identity_keys[1], &options.identity_keys[0],
1205 sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1206 options.num_identity_files++;
1207 options.identity_keys[0] = keys[i];
Damien Miller56a0bb02003-06-18 20:28:40 +10001208 options.identity_files[0] = sc_get_key_label(keys[i]);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001209 }
Ben Lindstrom4f054602002-03-26 03:23:00 +00001210 if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1211 options.num_identity_files = SSH_MAX_IDENTITY_FILES;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001212 i = count;
1213 xfree(keys);
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001214 }
Ben Lindstromffce1472001-08-06 21:57:31 +00001215#endif /* SMARTCARD */
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001216 for (; i < options.num_identity_files; i++) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001217 filename = tilde_expand_filename(options.identity_files[i],
1218 original_real_uid);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001219 public = key_load_public(filename, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001220 debug("identity file %s type %d", filename,
1221 public ? public->type : -1);
1222 xfree(options.identity_files[i]);
1223 options.identity_files[i] = filename;
1224 options.identity_keys[i] = public;
1225 }
1226}
Damien Miller0e220db2004-06-15 10:34:08 +10001227
1228static void
1229control_client_sighandler(int signo)
1230{
1231 control_client_terminate = signo;
1232}
1233
1234static void
1235control_client_sigrelay(int signo)
1236{
1237 if (control_server_pid > 1)
1238 kill(control_server_pid, signo);
1239}
1240
Darren Tucker365433f2004-06-22 12:29:23 +10001241static int
1242env_permitted(char *env)
1243{
1244 int i;
1245 char name[1024], *cp;
1246
1247 strlcpy(name, env, sizeof(name));
1248 if ((cp = strchr(name, '=')) == NULL)
1249 return (0);
1250
1251 *cp = '\0';
1252
1253 for (i = 0; i < options.num_send_env; i++)
1254 if (match_pattern(name, options.send_env[i]))
1255 return (1);
1256
1257 return (0);
1258}
1259
Damien Miller0e220db2004-06-15 10:34:08 +10001260static void
1261control_client(const char *path)
1262{
1263 struct sockaddr_un addr;
Darren Tucker39207a42004-11-05 20:19:51 +11001264 int i, r, fd, sock, exitval, num_env, addr_len;
Damien Miller0e220db2004-06-15 10:34:08 +10001265 Buffer m;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001266 char *term;
Damien Miller3756dce2004-06-18 01:17:29 +10001267 extern char **environ;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001268 u_int flags;
Darren Tuckerfc959702004-07-17 16:12:08 +10001269
Damien Millerd14b1e72005-06-16 13:19:41 +10001270 if (mux_command == 0)
1271 mux_command = SSHMUX_COMMAND_OPEN;
1272
1273 switch (options.control_master) {
1274 case SSHCTL_MASTER_AUTO:
1275 case SSHCTL_MASTER_AUTO_ASK:
1276 debug("auto-mux: Trying existing master");
1277 /* FALLTHROUGH */
1278 case SSHCTL_MASTER_NO:
1279 break;
1280 default:
1281 return;
1282 }
1283
Damien Miller0e220db2004-06-15 10:34:08 +10001284 memset(&addr, '\0', sizeof(addr));
1285 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001286 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001287 strlen(path) + 1;
1288
1289 if (strlcpy(addr.sun_path, path,
1290 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1291 fatal("ControlPath too long");
1292
1293 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1294 fatal("%s socket(): %s", __func__, strerror(errno));
1295
Damien Millerdadfd4d2005-05-26 12:07:13 +10001296 if (connect(sock, (struct sockaddr*)&addr, addr_len) == -1) {
Darren Tucker0814d312005-06-01 23:08:51 +10001297 if (mux_command != SSHMUX_COMMAND_OPEN) {
1298 fatal("Control socket connect(%.100s): %s", path,
1299 strerror(errno));
1300 }
Damien Miller538c9b72005-05-26 12:11:28 +10001301 if (errno == ENOENT)
1302 debug("Control socket \"%.100s\" does not exist", path);
1303 else {
1304 error("Control socket connect(%.100s): %s", path,
1305 strerror(errno));
1306 }
Damien Miller13390022005-07-06 09:44:19 +10001307 close(sock);
1308 return;
1309 }
Damien Miller46d38de2005-07-17 17:02:09 +10001310
Damien Miller13390022005-07-06 09:44:19 +10001311 if (stdin_null_flag) {
1312 if ((fd = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1313 fatal("open(/dev/null): %s", strerror(errno));
1314 if (dup2(fd, STDIN_FILENO) == -1)
1315 fatal("dup2: %s", strerror(errno));
1316 if (fd > STDERR_FILENO)
1317 close(fd);
1318 }
Damien Miller46d38de2005-07-17 17:02:09 +10001319
Damien Miller13390022005-07-06 09:44:19 +10001320 term = getenv("TERM");
Darren Tucker7ebfc102004-11-07 20:06:19 +11001321
1322 flags = 0;
1323 if (tty_flag)
1324 flags |= SSHMUX_FLAG_TTY;
1325 if (subsystem_flag)
1326 flags |= SSHMUX_FLAG_SUBSYS;
Damien Miller13390022005-07-06 09:44:19 +10001327 if (options.forward_x11)
1328 flags |= SSHMUX_FLAG_X11_FWD;
1329 if (options.forward_agent)
1330 flags |= SSHMUX_FLAG_AGENT_FWD;
Damien Miller0e220db2004-06-15 10:34:08 +10001331
Damien Miller0e220db2004-06-15 10:34:08 +10001332 buffer_init(&m);
1333
Darren Tucker7ebfc102004-11-07 20:06:19 +11001334 /* Send our command to server */
1335 buffer_put_int(&m, mux_command);
1336 buffer_put_int(&m, flags);
Damien Miller13390022005-07-06 09:44:19 +10001337 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001338 fatal("%s: msg_send", __func__);
1339 buffer_clear(&m);
1340
1341 /* Get authorisation status and PID of controlee */
Damien Miller0e220db2004-06-15 10:34:08 +10001342 if (ssh_msg_recv(sock, &m) == -1)
1343 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001344 if (buffer_get_char(&m) != SSHMUX_VER)
Damien Miller0e220db2004-06-15 10:34:08 +10001345 fatal("%s: wrong version", __func__);
Damien Miller23f07702004-06-18 01:19:03 +10001346 if (buffer_get_int(&m) != 1)
1347 fatal("Connection to master denied");
Damien Miller0e220db2004-06-15 10:34:08 +10001348 control_server_pid = buffer_get_int(&m);
1349
Damien Miller0e220db2004-06-15 10:34:08 +10001350 buffer_clear(&m);
Damien Miller0e220db2004-06-15 10:34:08 +10001351
Darren Tucker7ebfc102004-11-07 20:06:19 +11001352 switch (mux_command) {
1353 case SSHMUX_COMMAND_ALIVE_CHECK:
Darren Tucker47eede72005-03-14 23:08:12 +11001354 fprintf(stderr, "Master running (pid=%d)\r\n",
Darren Tucker7ebfc102004-11-07 20:06:19 +11001355 control_server_pid);
1356 exit(0);
1357 case SSHMUX_COMMAND_TERMINATE:
1358 fprintf(stderr, "Exit request sent.\r\n");
1359 exit(0);
1360 case SSHMUX_COMMAND_OPEN:
1361 /* continue below */
1362 break;
1363 default:
1364 fatal("silly mux_command %d", mux_command);
1365 }
1366
1367 /* SSHMUX_COMMAND_OPEN */
Damien Miller13390022005-07-06 09:44:19 +10001368 buffer_put_cstring(&m, term ? term : "");
Damien Miller0e220db2004-06-15 10:34:08 +10001369 buffer_append(&command, "\0", 1);
1370 buffer_put_cstring(&m, buffer_ptr(&command));
1371
Darren Tucker365433f2004-06-22 12:29:23 +10001372 if (options.num_send_env == 0 || environ == NULL) {
1373 buffer_put_int(&m, 0);
Darren Tuckerfc959702004-07-17 16:12:08 +10001374 } else {
Darren Tucker365433f2004-06-22 12:29:23 +10001375 /* Pass environment */
1376 num_env = 0;
1377 for (i = 0; environ[i] != NULL; i++)
1378 if (env_permitted(environ[i]))
1379 num_env++; /* Count */
Darren Tuckerfc959702004-07-17 16:12:08 +10001380
Darren Tucker365433f2004-06-22 12:29:23 +10001381 buffer_put_int(&m, num_env);
1382
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001383 for (i = 0; environ[i] != NULL && num_env >= 0; i++)
1384 if (env_permitted(environ[i])) {
1385 num_env--;
Darren Tucker365433f2004-06-22 12:29:23 +10001386 buffer_put_cstring(&m, environ[i]);
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001387 }
Darren Tucker365433f2004-06-22 12:29:23 +10001388 }
Damien Miller3756dce2004-06-18 01:17:29 +10001389
Damien Miller13390022005-07-06 09:44:19 +10001390 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Damien Miller0e220db2004-06-15 10:34:08 +10001391 fatal("%s: msg_send", __func__);
1392
1393 mm_send_fd(sock, STDIN_FILENO);
1394 mm_send_fd(sock, STDOUT_FILENO);
1395 mm_send_fd(sock, STDERR_FILENO);
1396
1397 /* Wait for reply, so master has a chance to gather ttymodes */
1398 buffer_clear(&m);
1399 if (ssh_msg_recv(sock, &m) == -1)
1400 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001401 if (buffer_get_char(&m) != SSHMUX_VER)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001402 fatal("%s: wrong version", __func__);
Damien Miller0e220db2004-06-15 10:34:08 +10001403 buffer_free(&m);
1404
Darren Tucker07336da2004-11-05 20:02:16 +11001405 signal(SIGHUP, control_client_sighandler);
Damien Miller0809e232004-06-18 22:20:57 +10001406 signal(SIGINT, control_client_sighandler);
1407 signal(SIGTERM, control_client_sighandler);
1408 signal(SIGWINCH, control_client_sigrelay);
1409
Damien Miller0e220db2004-06-15 10:34:08 +10001410 if (tty_flag)
1411 enter_raw_mode();
1412
1413 /* Stick around until the controlee closes the client_fd */
1414 exitval = 0;
1415 for (;!control_client_terminate;) {
1416 r = read(sock, &exitval, sizeof(exitval));
1417 if (r == 0) {
1418 debug2("Received EOF from master");
1419 break;
1420 }
1421 if (r > 0)
1422 debug2("Received exit status from master %d", exitval);
1423 if (r == -1 && errno != EINTR)
1424 fatal("%s: read %s", __func__, strerror(errno));
1425 }
1426
1427 if (control_client_terminate)
1428 debug2("Exiting on signal %d", control_client_terminate);
1429
1430 close(sock);
1431
1432 leave_raw_mode();
1433
1434 if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
1435 fprintf(stderr, "Connection to master closed.\r\n");
1436
1437 exit(exitval);
1438}