blob: 0c950745bdecb38e2a3b0928741a37c10a9e0f44 [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 Millercd4223c2006-03-15 11:22:47 +110043
Damien Millerf17883e2006-03-15 11:45:54 +110044#include <sys/types.h>
45#ifdef HAVE_SYS_STAT_H
46# include <sys/stat.h>
47#endif
Damien Millercd4223c2006-03-15 11:22:47 +110048#include <sys/resource.h>
Damien Miller17e91c02006-03-15 11:28:34 +110049#include <sys/ioctl.h>
Damien Miller574c41f2006-03-15 11:40:10 +110050#include <sys/un.h>
Damien Miller03e20032006-03-15 11:16:59 +110051
Damien Millerc7b06362006-03-15 11:53:45 +110052#include <ctype.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110053#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110054#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110055#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110056#include <signal.h>
Damien Millereba71ba2000-04-29 23:57:08 +100057
58#include <openssl/evp.h>
Damien Miller0bc1bd82000-11-13 22:57:25 +110059#include <openssl/err.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000062#include "ssh1.h"
Damien Miller1383bd82000-04-06 12:32:37 +100063#include "ssh2.h"
64#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000065#include "cipher.h"
66#include "xmalloc.h"
67#include "packet.h"
68#include "buffer.h"
Damien Miller0e220db2004-06-15 10:34:08 +100069#include "bufaux.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000070#include "channels.h"
Damien Millereba71ba2000-04-29 23:57:08 +100071#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100072#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100073#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000074#include "pathnames.h"
Damien Miller0e220db2004-06-15 10:34:08 +100075#include "dispatch.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +000076#include "clientloop.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000077#include "log.h"
78#include "readconf.h"
79#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000080#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000081#include "kex.h"
82#include "mac.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +100083#include "sshpty.h"
Darren Tucker46bc0752004-05-02 22:11:30 +100084#include "match.h"
Damien Miller0e220db2004-06-15 10:34:08 +100085#include "msg.h"
86#include "monitor_fdpass.h"
Darren Tucker25f60a72004-08-15 17:23:34 +100087#include "uidswap.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
Ben Lindstromc5b68002001-07-04 04:52:03 +000089#ifdef SMARTCARD
Ben Lindstromc5b68002001-07-04 04:52:03 +000090#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000091#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +000092
Damien Miller3f905871999-11-15 17:10:57 +110093extern char *__progname;
Damien Miller3f905871999-11-15 17:10:57 +110094
Damien Miller95def091999-11-25 00:26:21 +110095/* Flag indicating whether debug mode is on. This can be set on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096int debug_flag = 0;
97
Damien Miller78928792000-04-12 20:17:38 +100098/* Flag indicating whether a tty should be allocated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099int tty_flag = 0;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000100int no_tty_flag = 0;
101int force_tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102
Damien Miller1383bd82000-04-06 12:32:37 +1000103/* don't exec a shell */
104int no_shell_flag = 0;
Damien Miller1383bd82000-04-06 12:32:37 +1000105
Damien Miller5428f641999-11-25 11:54:57 +1100106/*
107 * Flag indicating that nothing should be read from stdin. This can be set
108 * on the command line.
109 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110int stdin_null_flag = 0;
111
Damien Miller5428f641999-11-25 11:54:57 +1100112/*
113 * Flag indicating that ssh should fork after authentication. This is useful
Ben Lindstromf666fec2002-06-06 19:51:58 +0000114 * so that the passphrase can be entered manually, and then ssh goes to the
Damien Miller5428f641999-11-25 11:54:57 +1100115 * background.
116 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117int fork_after_authentication_flag = 0;
118
Damien Miller5428f641999-11-25 11:54:57 +1100119/*
120 * General data structure for command line options and options configurable
121 * in configuration files. See readconf.h.
122 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123Options options;
124
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000125/* optional user configfile */
126char *config = NULL;
127
Damien Miller5428f641999-11-25 11:54:57 +1100128/*
129 * Name of the host we are connecting to. This is the name given on the
130 * command line, or the HostName specified for the user-supplied name in a
131 * configuration file.
132 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000133char *host;
134
135/* socket address the host resolves to */
Damien Miller34132e52000-01-14 15:45:46 +1100136struct sockaddr_storage hostaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000138/* Private host keys. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000139Sensitive sensitive_data;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140
141/* Original real UID. */
142uid_t original_real_uid;
Ben Lindstromf9c48842002-06-11 16:37:51 +0000143uid_t original_effective_uid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144
Damien Miller1383bd82000-04-06 12:32:37 +1000145/* command to be executed */
146Buffer command;
147
Damien Miller832562e2001-01-30 09:30:01 +1100148/* Should we execute a command or invoke a subsystem? */
149int subsystem_flag = 0;
150
Damien Miller2797f7f2002-04-23 21:09:44 +1000151/* # of replies received for global requests */
152static int client_global_request_id = 0;
153
Damien Miller8c4e18a2002-09-19 12:05:02 +1000154/* pid of proxycommand child process */
155pid_t proxy_command_pid = 0;
156
Damien Miller0e220db2004-06-15 10:34:08 +1000157/* fd to control socket */
158int control_fd = -1;
159
Darren Tucker7ebfc102004-11-07 20:06:19 +1100160/* Multiplexing control command */
Darren Tucker0814d312005-06-01 23:08:51 +1000161static u_int mux_command = 0;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100162
Damien Miller0e220db2004-06-15 10:34:08 +1000163/* Only used in control client mode */
164volatile sig_atomic_t control_client_terminate = 0;
165u_int control_server_pid = 0;
166
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000167/* Prints a help message to the user. This function never returns. */
168
Ben Lindstrombba81212001-06-25 05:01:22 +0000169static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000170usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000171{
Damien Miller50955102004-03-22 09:34:58 +1100172 fprintf(stderr,
Darren Tucker9c6bf322004-12-03 14:10:19 +1100173"usage: ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n"
Darren Tucker895d6982005-10-03 18:18:05 +1000174" [-D [bind_address:]port] [-e escape_char] [-F configfile]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100175" [-i identity_file] [-L [bind_address:]port:host:hostport]\n"
Damien Millerf91ee4c2005-03-01 21:24:33 +1100176" [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100177" [-R [bind_address:]port:host:hostport] [-S ctl_path]\n"
Damien Millerd27b9472005-12-13 19:29:02 +1100178" [-w tunnel:tunnel] [user@]hostname [command]\n"
Damien Miller50955102004-03-22 09:34:58 +1100179 );
Darren Tuckere9a9b712005-12-20 16:15:51 +1100180 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000181}
182
Ben Lindstrombba81212001-06-25 05:01:22 +0000183static int ssh_session(void);
184static int ssh_session2(void);
185static void load_public_identity_files(void);
Damien Miller0e220db2004-06-15 10:34:08 +1000186static void control_client(const char *path);
Damien Miller1383bd82000-04-06 12:32:37 +1000187
Damien Miller95def091999-11-25 00:26:21 +1100188/*
189 * Main program for the ssh client.
190 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000191int
192main(int ac, char **av)
193{
Ben Lindstrom24157572002-06-12 16:09:39 +0000194 int i, opt, exit_status;
Damien Miller9836cf82003-12-17 16:30:06 +1100195 char *p, *cp, *line, buf[256];
Damien Miller95def091999-11-25 00:26:21 +1100196 struct stat st;
Ben Lindstrom086cf212001-03-05 05:56:40 +0000197 struct passwd *pw;
Damien Miller1383bd82000-04-06 12:32:37 +1000198 int dummy;
Ben Lindstrom5ccf63a2001-09-24 20:00:10 +0000199 extern int optind, optreset;
Damien Miller4f8e6692001-07-14 13:22:53 +1000200 extern char *optarg;
Damien Miller9651fe62005-06-26 08:55:25 +1000201 struct servent *sp;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100202 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203
Darren Tuckerce321d82005-10-03 18:11:24 +1000204 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
205 sanitise_stdfd();
206
Damien Miller59d3d5b2003-08-22 09:34:41 +1000207 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000208 init_rng();
209
Damien Miller5428f641999-11-25 11:54:57 +1100210 /*
211 * Save the original real uid. It will be needed later (uid-swapping
212 * may clobber the real uid).
213 */
Damien Miller95def091999-11-25 00:26:21 +1100214 original_real_uid = getuid();
215 original_effective_uid = geteuid();
Damien Millera8e06ce2003-11-21 23:48:55 +1100216
Damien Miller50b9a602002-09-04 16:50:06 +1000217 /*
218 * Use uid-swapping to give up root privileges for the duration of
219 * option processing. We will re-instantiate the rights when we are
220 * ready to create the privileged port, and will permanently drop
221 * them when the port has been created (actually, when the connection
222 * has been made, as we may need to create the port several times).
223 */
224 PRIV_END;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000225
Damien Miller05dd7952000-10-01 00:42:48 +1100226#ifdef HAVE_SETRLIMIT
Damien Miller95def091999-11-25 00:26:21 +1100227 /* If we are installed setuid root be careful to not drop core. */
228 if (original_real_uid != original_effective_uid) {
229 struct rlimit rlim;
230 rlim.rlim_cur = rlim.rlim_max = 0;
231 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
232 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000233 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100234#endif
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000235 /* Get user data. */
236 pw = getpwuid(original_real_uid);
237 if (!pw) {
Damien Miller996acd22003-04-09 20:59:48 +1000238 logit("You don't exist, go away!");
Darren Tuckere9a9b712005-12-20 16:15:51 +1100239 exit(255);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000240 }
241 /* Take a copy of the returned structure. */
242 pw = pwcopy(pw);
243
Damien Miller5428f641999-11-25 11:54:57 +1100244 /*
Damien Miller5428f641999-11-25 11:54:57 +1100245 * Set our umask to something reasonable, as some files are created
246 * with the default umask. This will make them world-readable but
247 * writable only by the owner, which is ok for all files for which we
248 * don't set the modes explicitly.
249 */
Damien Miller95def091999-11-25 00:26:21 +1100250 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000251
Damien Miller95def091999-11-25 00:26:21 +1100252 /* Initialize option structure to indicate that no values have been set. */
253 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000254
Damien Miller95def091999-11-25 00:26:21 +1100255 /* Parse command-line arguments. */
256 host = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000257
Damien Miller2ecb6bd2006-03-15 12:03:53 +1100258 again:
Damien Millerf4614452001-07-14 12:18:10 +1000259 while ((opt = getopt(ac, av,
Damien Millerd27b9472005-12-13 19:29:02 +1100260 "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 +1100261 switch (opt) {
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000262 case '1':
263 options.protocol = SSH_PROTO_1;
264 break;
Damien Miller4af51302000-04-16 11:18:38 +1000265 case '2':
266 options.protocol = SSH_PROTO_2;
267 break;
Damien Miller34132e52000-01-14 15:45:46 +1100268 case '4':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000269 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100270 break;
Damien Miller34132e52000-01-14 15:45:46 +1100271 case '6':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000272 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100273 break;
Damien Miller95def091999-11-25 00:26:21 +1100274 case 'n':
275 stdin_null_flag = 1;
276 break;
Damien Miller95def091999-11-25 00:26:21 +1100277 case 'f':
278 fork_after_authentication_flag = 1;
279 stdin_null_flag = 1;
280 break;
Damien Miller95def091999-11-25 00:26:21 +1100281 case 'x':
282 options.forward_x11 = 0;
283 break;
Damien Miller95def091999-11-25 00:26:21 +1100284 case 'X':
285 options.forward_x11 = 1;
286 break;
Darren Tucker0a118da2003-10-15 15:54:32 +1000287 case 'Y':
288 options.forward_x11 = 1;
289 options.forward_x11_trusted = 1;
290 break;
Damien Miller95def091999-11-25 00:26:21 +1100291 case 'g':
292 options.gateway_ports = 1;
293 break;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100294 case 'O':
295 if (strcmp(optarg, "check") == 0)
296 mux_command = SSHMUX_COMMAND_ALIVE_CHECK;
297 else if (strcmp(optarg, "exit") == 0)
298 mux_command = SSHMUX_COMMAND_TERMINATE;
299 else
300 fatal("Invalid multiplex command.");
301 break;
Damien Miller147bba32002-09-04 16:46:06 +1000302 case 'P': /* deprecated */
Damien Miller95def091999-11-25 00:26:21 +1100303 options.use_privileged_port = 0;
304 break;
Damien Miller95def091999-11-25 00:26:21 +1100305 case 'a':
306 options.forward_agent = 0;
307 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000308 case 'A':
309 options.forward_agent = 1;
310 break;
Damien Miller95def091999-11-25 00:26:21 +1100311 case 'k':
Damien Millere0113cc2003-11-24 13:10:09 +1100312 options.gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100313 break;
Damien Miller95def091999-11-25 00:26:21 +1100314 case 'i':
315 if (stat(optarg, &st) < 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000316 fprintf(stderr, "Warning: Identity file %s "
Damien Miller3eb48b62005-03-01 21:15:46 +1100317 "not accessible: %s.\n", optarg,
318 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100319 break;
320 }
Damien Millerf4614452001-07-14 12:18:10 +1000321 if (options.num_identity_files >=
322 SSH_MAX_IDENTITY_FILES)
323 fatal("Too many identity files specified "
324 "(max %d)", SSH_MAX_IDENTITY_FILES);
325 options.identity_files[options.num_identity_files++] =
326 xstrdup(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100327 break;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000328 case 'I':
329#ifdef SMARTCARD
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000330 options.smartcard_device = xstrdup(optarg);
Ben Lindstrombcc18082001-08-06 21:59:25 +0000331#else
Ben Lindstromc5b68002001-07-04 04:52:03 +0000332 fprintf(stderr, "no support for smartcards.\n");
Ben Lindstrombcc18082001-08-06 21:59:25 +0000333#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000334 break;
Damien Miller95def091999-11-25 00:26:21 +1100335 case 't':
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000336 if (tty_flag)
337 force_tty_flag = 1;
Damien Miller95def091999-11-25 00:26:21 +1100338 tty_flag = 1;
339 break;
Damien Miller95def091999-11-25 00:26:21 +1100340 case 'v':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000341 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100342 debug_flag = 1;
343 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000344 } else {
345 if (options.log_level < SYSLOG_LEVEL_DEBUG3)
346 options.log_level++;
Damien Millere4340be2000-09-16 13:29:08 +1100347 break;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000348 }
Darren Tuckere9bf9842004-11-05 20:05:32 +1100349 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100350 case 'V':
Damien Miller0c889cd2004-03-22 09:36:00 +1100351 fprintf(stderr, "%s, %s\n",
Damien Miller2aa6d3c2004-09-12 16:53:04 +1000352 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
Damien Miller95def091999-11-25 00:26:21 +1100353 if (opt == 'V')
354 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100355 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100356 case 'w':
Damien Miller7b58e802005-12-13 19:33:19 +1100357 if (options.tun_open == -1)
358 options.tun_open = SSH_TUNMODE_DEFAULT;
Damien Millerd27b9472005-12-13 19:29:02 +1100359 options.tun_local = a2tun(optarg, &options.tun_remote);
Damien Miller7b58e802005-12-13 19:33:19 +1100360 if (options.tun_local == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100361 fprintf(stderr, "Bad tun device '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100362 exit(255);
Damien Millerd27b9472005-12-13 19:29:02 +1100363 }
364 break;
Damien Miller95def091999-11-25 00:26:21 +1100365 case 'q':
366 options.log_level = SYSLOG_LEVEL_QUIET;
367 break;
Damien Miller95def091999-11-25 00:26:21 +1100368 case 'e':
369 if (optarg[0] == '^' && optarg[2] == 0 &&
Damien Millerf4614452001-07-14 12:18:10 +1000370 (u_char) optarg[1] >= 64 &&
371 (u_char) optarg[1] < 128)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000372 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100373 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000374 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100375 else if (strcmp(optarg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000376 options.escape_char = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100377 else {
Damien Millerf4614452001-07-14 12:18:10 +1000378 fprintf(stderr, "Bad escape character '%s'.\n",
379 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100380 exit(255);
Damien Miller95def091999-11-25 00:26:21 +1100381 }
382 break;
Damien Miller95def091999-11-25 00:26:21 +1100383 case 'c':
Damien Millereba71ba2000-04-29 23:57:08 +1000384 if (ciphers_valid(optarg)) {
385 /* SSH2 only */
386 options.ciphers = xstrdup(optarg);
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000387 options.cipher = SSH_CIPHER_INVALID;
Damien Millereba71ba2000-04-29 23:57:08 +1000388 } else {
389 /* SSH1 only */
Damien Millere39cacc2000-11-29 12:18:44 +1100390 options.cipher = cipher_number(optarg);
391 if (options.cipher == -1) {
Damien Millerf4614452001-07-14 12:18:10 +1000392 fprintf(stderr,
393 "Unknown cipher type '%s'\n",
394 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100395 exit(255);
Damien Millereba71ba2000-04-29 23:57:08 +1000396 }
Damien Millerf4614452001-07-14 12:18:10 +1000397 if (options.cipher == SSH_CIPHER_3DES)
Damien Millere39cacc2000-11-29 12:18:44 +1100398 options.ciphers = "3des-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000399 else if (options.cipher == SSH_CIPHER_BLOWFISH)
Damien Millere39cacc2000-11-29 12:18:44 +1100400 options.ciphers = "blowfish-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000401 else
Damien Millere39cacc2000-11-29 12:18:44 +1100402 options.ciphers = (char *)-1;
Damien Miller95def091999-11-25 00:26:21 +1100403 }
404 break;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000405 case 'm':
406 if (mac_valid(optarg))
407 options.macs = xstrdup(optarg);
408 else {
Damien Millerf4614452001-07-14 12:18:10 +1000409 fprintf(stderr, "Unknown mac type '%s'\n",
410 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100411 exit(255);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000412 }
413 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000414 case 'M':
Damien Millerd14b1e72005-06-16 13:19:41 +1000415 if (options.control_master == SSHCTL_MASTER_YES)
416 options.control_master = SSHCTL_MASTER_ASK;
417 else
418 options.control_master = SSHCTL_MASTER_YES;
Damien Miller0e220db2004-06-15 10:34:08 +1000419 break;
Damien Miller95def091999-11-25 00:26:21 +1100420 case 'p':
Ben Lindstrom19066a12001-04-12 23:39:26 +0000421 options.port = a2port(optarg);
422 if (options.port == 0) {
Ben Lindstrom146edb92001-04-11 23:06:28 +0000423 fprintf(stderr, "Bad port '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100424 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000425 }
Damien Miller95def091999-11-25 00:26:21 +1100426 break;
Damien Miller95def091999-11-25 00:26:21 +1100427 case 'l':
428 options.user = optarg;
429 break;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000430
Damien Miller95def091999-11-25 00:26:21 +1100431 case 'L':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100432 if (parse_forward(&fwd, optarg))
433 add_local_forward(&options, &fwd);
434 else {
Damien Millerf4614452001-07-14 12:18:10 +1000435 fprintf(stderr,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100436 "Bad local forwarding specification '%s'\n",
Damien Millerf4614452001-07-14 12:18:10 +1000437 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100438 exit(255);
Ben Lindstrom1a174712001-09-12 17:56:15 +0000439 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100440 break;
441
442 case 'R':
443 if (parse_forward(&fwd, optarg)) {
444 add_remote_forward(&options, &fwd);
445 } else {
446 fprintf(stderr,
447 "Bad remote forwarding specification "
448 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100449 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100450 }
Damien Miller95def091999-11-25 00:26:21 +1100451 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000452
453 case 'D':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100454 cp = p = xstrdup(optarg);
455 memset(&fwd, '\0', sizeof(fwd));
456 fwd.connect_host = "socks";
457 if ((fwd.listen_host = hpdelim(&cp)) == NULL) {
458 fprintf(stderr, "Bad dynamic forwarding "
459 "specification '%.100s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100460 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100461 }
462 if (cp != NULL) {
463 fwd.listen_port = a2port(cp);
464 fwd.listen_host = cleanhostname(fwd.listen_host);
465 } else {
466 fwd.listen_port = a2port(fwd.listen_host);
Damien Millerbe1045d2005-08-12 22:10:56 +1000467 fwd.listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100468 }
469
470 if (fwd.listen_port == 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000471 fprintf(stderr, "Bad dynamic port '%s'\n",
472 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100473 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000474 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100475 add_local_forward(&options, &fwd);
476 xfree(p);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000477 break;
478
Damien Miller95def091999-11-25 00:26:21 +1100479 case 'C':
480 options.compression = 1;
481 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000482 case 'N':
483 no_shell_flag = 1;
484 no_tty_flag = 1;
485 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000486 case 'T':
487 no_tty_flag = 1;
488 break;
Damien Miller95def091999-11-25 00:26:21 +1100489 case 'o':
490 dummy = 1;
Damien Miller9836cf82003-12-17 16:30:06 +1100491 line = xstrdup(optarg);
Damien Millerf4614452001-07-14 12:18:10 +1000492 if (process_config_line(&options, host ? host : "",
Damien Miller9836cf82003-12-17 16:30:06 +1100493 line, "command-line", 0, &dummy) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100494 exit(255);
Damien Miller9836cf82003-12-17 16:30:06 +1100495 xfree(line);
Damien Miller95def091999-11-25 00:26:21 +1100496 break;
Damien Miller832562e2001-01-30 09:30:01 +1100497 case 's':
498 subsystem_flag = 1;
499 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000500 case 'S':
501 if (options.control_path != NULL)
502 free(options.control_path);
503 options.control_path = xstrdup(optarg);
Damien Miller0e220db2004-06-15 10:34:08 +1000504 break;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000505 case 'b':
506 options.bind_address = optarg;
507 break;
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000508 case 'F':
509 config = optarg;
510 break;
Damien Miller95def091999-11-25 00:26:21 +1100511 default:
512 usage();
513 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000514 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000515
Damien Millerf4614452001-07-14 12:18:10 +1000516 ac -= optind;
517 av += optind;
518
519 if (ac > 0 && !host && **av != '-') {
Ben Lindstromc276c122002-12-23 02:14:51 +0000520 if (strrchr(*av, '@')) {
Damien Millerf4614452001-07-14 12:18:10 +1000521 p = xstrdup(*av);
Ben Lindstromc276c122002-12-23 02:14:51 +0000522 cp = strrchr(p, '@');
Damien Millerf4614452001-07-14 12:18:10 +1000523 if (cp == NULL || cp == p)
524 usage();
525 options.user = p;
526 *cp = '\0';
527 host = ++cp;
528 } else
529 host = *av;
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000530 if (ac > 1) {
531 optind = optreset = 1;
Damien Millerf4614452001-07-14 12:18:10 +1000532 goto again;
533 }
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000534 ac--, av++;
Damien Millerf4614452001-07-14 12:18:10 +1000535 }
536
Damien Miller95def091999-11-25 00:26:21 +1100537 /* Check that we got a host name. */
538 if (!host)
539 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000540
Damien Millercb5e44a2000-09-29 12:12:36 +1100541 SSLeay_add_all_algorithms();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100542 ERR_load_crypto_strings();
Damien Millercb5e44a2000-09-29 12:12:36 +1100543
Damien Miller95def091999-11-25 00:26:21 +1100544 /* Initialize the command to execute on remote host. */
545 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000546
Damien Miller5428f641999-11-25 11:54:57 +1100547 /*
548 * Save the command to execute on the remote host in a buffer. There
549 * is no limit on the length of the command, except by the maximum
550 * packet size. Also sets the tty flag if there is no command.
551 */
Damien Millerf4614452001-07-14 12:18:10 +1000552 if (!ac) {
Damien Miller95def091999-11-25 00:26:21 +1100553 /* No command specified - execute shell on a tty. */
554 tty_flag = 1;
Damien Miller832562e2001-01-30 09:30:01 +1100555 if (subsystem_flag) {
Damien Millerf4614452001-07-14 12:18:10 +1000556 fprintf(stderr,
557 "You must specify a subsystem to invoke.\n");
Damien Miller832562e2001-01-30 09:30:01 +1100558 usage();
559 }
Damien Miller95def091999-11-25 00:26:21 +1100560 } else {
Damien Millerf4614452001-07-14 12:18:10 +1000561 /* A command has been specified. Store it into the buffer. */
562 for (i = 0; i < ac; i++) {
563 if (i)
Damien Miller95def091999-11-25 00:26:21 +1100564 buffer_append(&command, " ", 1);
565 buffer_append(&command, av[i], strlen(av[i]));
566 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000567 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000568
Damien Miller95def091999-11-25 00:26:21 +1100569 /* Cannot fork to background if no command. */
Damien Millercaf6dd62000-08-29 11:33:50 +1100570 if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
Damien Miller95def091999-11-25 00:26:21 +1100571 fatal("Cannot fork into background without a command to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000572
Damien Miller95def091999-11-25 00:26:21 +1100573 /* Allocate a tty by default if no command specified. */
574 if (buffer_len(&command) == 0)
575 tty_flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000576
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000577 /* Force no tty */
Ben Lindstromc72745a2000-12-02 19:03:54 +0000578 if (no_tty_flag)
579 tty_flag = 0;
Damien Miller95def091999-11-25 00:26:21 +1100580 /* Do not allocate a tty if stdin is not a tty. */
Damien Millerddee5752005-05-26 12:05:05 +1000581 if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100582 if (tty_flag)
Damien Miller996acd22003-04-09 20:59:48 +1000583 logit("Pseudo-terminal will not be allocated because stdin is not a terminal.");
Damien Miller95def091999-11-25 00:26:21 +1100584 tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000585 }
Damien Miller1383bd82000-04-06 12:32:37 +1000586
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000587 /*
588 * Initialize "log" output. Since we are the client all output
589 * actually goes to stderr.
590 */
Ben Lindstrom2b646522001-04-12 16:16:57 +0000591 log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
592 SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000593
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000594 /*
595 * Read per-user configuration file. Ignore the system wide config
596 * file if the user specifies a config file on the command line.
597 */
598 if (config != NULL) {
Damien Miller914420f2004-04-20 20:14:07 +1000599 if (!read_config_file(config, host, &options, 0))
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000600 fatal("Can't open user config file %.100s: "
601 "%.100s", config, strerror(errno));
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000602 } else {
603 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
604 _PATH_SSH_USER_CONFFILE);
Damien Miller57a44762004-04-20 20:11:57 +1000605 (void)read_config_file(buf, host, &options, 1);
Ben Lindstrom83f07d12001-10-03 17:22:29 +0000606
607 /* Read systemwide configuration file after use config. */
Darren Tuckerfc959702004-07-17 16:12:08 +1000608 (void)read_config_file(_PATH_HOST_CONFIG_FILE, host,
Damien Miller57a44762004-04-20 20:11:57 +1000609 &options, 0);
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000610 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000611
Damien Miller95def091999-11-25 00:26:21 +1100612 /* Fill configuration defaults. */
613 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000614
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000615 channel_set_af(options.address_family);
616
Damien Miller95def091999-11-25 00:26:21 +1100617 /* reinit */
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000618 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000619
Damien Miller60bc5172001-03-19 09:38:15 +1100620 seed_rng();
621
Damien Miller95def091999-11-25 00:26:21 +1100622 if (options.user == NULL)
623 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000624
Damien Miller95def091999-11-25 00:26:21 +1100625 if (options.hostname != NULL)
626 host = options.hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000627
Darren Tucker3f521e22003-07-03 16:20:42 +1000628 /* force lowercase for hostkey matching */
629 if (options.host_key_alias != NULL) {
630 for (p = options.host_key_alias; *p; p++)
631 if (isupper(*p))
Damien Miller1d2b6702006-03-26 14:09:54 +1100632 *p = (char)tolower(*p);
Darren Tucker3f521e22003-07-03 16:20:42 +1000633 }
634
Damien Miller7c71cc72005-06-26 08:56:31 +1000635 /* Get default port if port has not been set. */
636 if (options.port == 0) {
637 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
638 options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
639 }
640
Damien Miller9f1e33a2003-02-24 11:57:32 +1100641 if (options.proxy_command != NULL &&
642 strcmp(options.proxy_command, "none") == 0)
643 options.proxy_command = NULL;
Damien Miller8f74c8f2005-06-26 08:56:03 +1000644 if (options.control_path != NULL &&
645 strcmp(options.control_path, "none") == 0)
646 options.control_path = NULL;
Damien Miller9f1e33a2003-02-24 11:57:32 +1100647
Damien Miller0e220db2004-06-15 10:34:08 +1000648 if (options.control_path != NULL) {
Damien Miller3ec54c72006-03-15 11:30:13 +1100649 char me[NI_MAXHOST];
650
651 if (gethostname(me, sizeof(me)) == -1)
652 fatal("gethostname: %s", strerror(errno));
Damien Miller6476cad2005-06-16 13:18:34 +1000653 snprintf(buf, sizeof(buf), "%d", options.port);
654 cp = tilde_expand_filename(options.control_path,
655 original_real_uid);
656 options.control_path = percent_expand(cp, "p", buf, "h", host,
Damien Miller3ec54c72006-03-15 11:30:13 +1100657 "r", options.user, "l", me, (char *)NULL);
Damien Miller6476cad2005-06-16 13:18:34 +1000658 xfree(cp);
Damien Miller0e220db2004-06-15 10:34:08 +1000659 }
Darren Tucker0814d312005-06-01 23:08:51 +1000660 if (mux_command != 0 && options.control_path == NULL)
661 fatal("No ControlPath specified for \"-O\" command");
Damien Millerd14b1e72005-06-16 13:19:41 +1000662 if (options.control_path != NULL)
Damien Millerdadfd4d2005-05-26 12:07:13 +1000663 control_client(options.control_path);
Damien Miller0e220db2004-06-15 10:34:08 +1000664
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000665 /* Open a connection to the remote host. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000666 if (ssh_connect(host, &hostaddr, options.port,
667 options.address_family, options.connection_attempts,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000668#ifdef HAVE_CYGWIN
669 options.use_privileged_port,
670#else
Ben Lindstromf9c48842002-06-11 16:37:51 +0000671 original_effective_uid == 0 && options.use_privileged_port,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000672#endif
Ben Lindstromda394ca2002-06-12 16:11:12 +0000673 options.proxy_command) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100674 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000675
Damien Miller5428f641999-11-25 11:54:57 +1100676 /*
677 * If we successfully made the connection, load the host private key
678 * in case we will need it later for combined rsa-rhosts
679 * authentication. This must be done before releasing extra
680 * privileges, because the file is only readable by root.
Ben Lindstrom9e5bb572002-06-06 19:58:27 +0000681 * If we cannot access the private keys, load the public keys
682 * instead and try to execute the ssh-keysign helper instead.
Damien Miller5428f641999-11-25 11:54:57 +1100683 */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000684 sensitive_data.nkeys = 0;
685 sensitive_data.keys = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000686 sensitive_data.external_keysign = 0;
Ben Lindstrom24157572002-06-12 16:09:39 +0000687 if (options.rhosts_rsa_authentication ||
688 options.hostbased_authentication) {
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000689 sensitive_data.nkeys = 3;
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000690 sensitive_data.keys = xmalloc(sensitive_data.nkeys *
691 sizeof(Key));
Ben Lindstromf9c48842002-06-11 16:37:51 +0000692
693 PRIV_START;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000694 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
Ben Lindstromd0fca422001-03-26 13:44:06 +0000695 _PATH_HOST_KEY_FILE, "", NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000696 sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
697 _PATH_HOST_DSA_KEY_FILE, "", NULL);
698 sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
699 _PATH_HOST_RSA_KEY_FILE, "", NULL);
Ben Lindstromf9c48842002-06-11 16:37:51 +0000700 PRIV_END;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000701
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +0000702 if (options.hostbased_authentication == 1 &&
703 sensitive_data.keys[0] == NULL &&
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000704 sensitive_data.keys[1] == NULL &&
705 sensitive_data.keys[2] == NULL) {
706 sensitive_data.keys[1] = key_load_public(
707 _PATH_HOST_DSA_KEY_FILE, NULL);
708 sensitive_data.keys[2] = key_load_public(
709 _PATH_HOST_RSA_KEY_FILE, NULL);
710 sensitive_data.external_keysign = 1;
711 }
Damien Miller95def091999-11-25 00:26:21 +1100712 }
Damien Miller5428f641999-11-25 11:54:57 +1100713 /*
714 * Get rid of any extra privileges that we may have. We will no
715 * longer need them. Also, extra privileges could make it very hard
716 * to read identity files and other non-world-readable files from the
717 * user's home directory if it happens to be on a NFS volume where
718 * root is mapped to nobody.
719 */
Darren Tucker25f60a72004-08-15 17:23:34 +1000720 if (original_effective_uid == 0) {
721 PRIV_START;
722 permanently_set_uid(pw);
723 }
Damien Miller95def091999-11-25 00:26:21 +1100724
Damien Miller5428f641999-11-25 11:54:57 +1100725 /*
726 * Now that we are back to our own permissions, create ~/.ssh
Damien Miller788f2122005-11-05 15:14:59 +1100727 * directory if it doesn't already exist.
Damien Miller5428f641999-11-25 11:54:57 +1100728 */
Ben Lindstrom930b14a2001-08-15 23:19:21 +0000729 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 +1100730 if (stat(buf, &st) < 0)
Damien Millerbe484b52000-07-15 14:14:16 +1000731 if (mkdir(buf, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100732 error("Could not create directory '%.200s'.", buf);
733
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000734 /* load options.identity_files */
735 load_public_identity_files();
736
737 /* Expand ~ in known host file names. */
738 /* XXX mem-leaks: */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100739 options.system_hostfile =
740 tilde_expand_filename(options.system_hostfile, original_real_uid);
741 options.user_hostfile =
742 tilde_expand_filename(options.user_hostfile, original_real_uid);
743 options.system_hostfile2 =
744 tilde_expand_filename(options.system_hostfile2, original_real_uid);
745 options.user_hostfile2 =
746 tilde_expand_filename(options.user_hostfile2, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100747
Damien Miller07cd5892001-11-12 10:52:03 +1100748 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
749
Damien Miller95def091999-11-25 00:26:21 +1100750 /* Log into the remote system. This never returns if the login fails. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000751 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, pw);
Damien Miller95def091999-11-25 00:26:21 +1100752
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000753 /* We no longer need the private host keys. Clear them now. */
754 if (sensitive_data.nkeys != 0) {
755 for (i = 0; i < sensitive_data.nkeys; i++) {
756 if (sensitive_data.keys[i] != NULL) {
757 /* Destroys contents safely */
758 debug3("clear hostkey %d", i);
759 key_free(sensitive_data.keys[i]);
760 sensitive_data.keys[i] = NULL;
761 }
762 }
763 xfree(sensitive_data.keys);
764 }
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +0000765 for (i = 0; i < options.num_identity_files; i++) {
766 if (options.identity_files[i]) {
767 xfree(options.identity_files[i]);
768 options.identity_files[i] = NULL;
769 }
770 if (options.identity_keys[i]) {
771 key_free(options.identity_keys[i]);
772 options.identity_keys[i] = NULL;
773 }
774 }
Damien Miller95def091999-11-25 00:26:21 +1100775
Damien Miller1383bd82000-04-06 12:32:37 +1000776 exit_status = compat20 ? ssh_session2() : ssh_session();
777 packet_close();
Damien Miller8c4e18a2002-09-19 12:05:02 +1000778
Damien Miller0e220db2004-06-15 10:34:08 +1000779 if (options.control_path != NULL && control_fd != -1)
780 unlink(options.control_path);
781
Damien Miller8c4e18a2002-09-19 12:05:02 +1000782 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100783 * Send SIGHUP to proxy command if used. We don't wait() in
Damien Miller8c4e18a2002-09-19 12:05:02 +1000784 * case it hangs and instead rely on init to reap the child
785 */
786 if (proxy_command_pid > 1)
787 kill(proxy_command_pid, SIGHUP);
788
Damien Miller1383bd82000-04-06 12:32:37 +1000789 return exit_status;
790}
791
Ben Lindstrombba81212001-06-25 05:01:22 +0000792static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100793ssh_init_forwarding(void)
794{
Kevin Steves12057502001-02-05 14:54:34 +0000795 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100796 int i;
Kevin Steves12057502001-02-05 14:54:34 +0000797
Damien Miller0bc1bd82000-11-13 22:57:25 +1100798 /* Initiate local TCP/IP port forwardings. */
799 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100800 debug("Local connections to %.200s:%d forwarded to remote "
801 "address %.200s:%d",
Darren Tucker47eede72005-03-14 23:08:12 +1100802 (options.local_forwards[i].listen_host == NULL) ?
803 (options.gateway_ports ? "*" : "LOCALHOST") :
Damien Millerf91ee4c2005-03-01 21:24:33 +1100804 options.local_forwards[i].listen_host,
805 options.local_forwards[i].listen_port,
806 options.local_forwards[i].connect_host,
807 options.local_forwards[i].connect_port);
Damien Millerb16461c2002-01-22 23:29:22 +1100808 success += channel_setup_local_fwd_listener(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100809 options.local_forwards[i].listen_host,
810 options.local_forwards[i].listen_port,
811 options.local_forwards[i].connect_host,
812 options.local_forwards[i].connect_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100813 options.gateway_ports);
814 }
Kevin Steves12057502001-02-05 14:54:34 +0000815 if (i > 0 && success == 0)
816 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100817
818 /* Initiate remote TCP/IP port forwardings. */
819 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100820 debug("Remote connections from %.200s:%d forwarded to "
821 "local address %.200s:%d",
Damien Miller46d38de2005-07-17 17:02:09 +1000822 (options.remote_forwards[i].listen_host == NULL) ?
Damien Milleraa3bb102005-11-05 15:12:59 +1100823 "LOCALHOST" : options.remote_forwards[i].listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100824 options.remote_forwards[i].listen_port,
825 options.remote_forwards[i].connect_host,
826 options.remote_forwards[i].connect_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100827 channel_request_remote_forwarding(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100828 options.remote_forwards[i].listen_host,
829 options.remote_forwards[i].listen_port,
830 options.remote_forwards[i].connect_host,
831 options.remote_forwards[i].connect_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100832 }
833}
834
Ben Lindstrombba81212001-06-25 05:01:22 +0000835static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100836check_agent_present(void)
837{
838 if (options.forward_agent) {
Damien Miller788f2122005-11-05 15:14:59 +1100839 /* Clear agent forwarding if we don't have an agent. */
Damien Miller789e95d2002-09-12 09:52:46 +1000840 if (!ssh_agent_present())
Damien Miller0bc1bd82000-11-13 22:57:25 +1100841 options.forward_agent = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100842 }
843}
844
Ben Lindstrombba81212001-06-25 05:01:22 +0000845static int
Damien Miller1383bd82000-04-06 12:32:37 +1000846ssh_session(void)
847{
848 int type;
Damien Miller1383bd82000-04-06 12:32:37 +1000849 int interactive = 0;
850 int have_tty = 0;
851 struct winsize ws;
Damien Miller1383bd82000-04-06 12:32:37 +1000852 char *cp;
Damien Miller17e7ed02005-06-17 12:54:33 +1000853 const char *display;
Damien Miller1383bd82000-04-06 12:32:37 +1000854
Damien Miller95def091999-11-25 00:26:21 +1100855 /* Enable compression if requested. */
856 if (options.compression) {
857 debug("Requesting compression at level %d.", options.compression_level);
858
859 if (options.compression_level < 1 || options.compression_level > 9)
860 fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
861
862 /* Send the request. */
863 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
864 packet_put_int(options.compression_level);
865 packet_send();
866 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100867 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100868 if (type == SSH_SMSG_SUCCESS)
869 packet_start_compression(options.compression_level);
870 else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000871 logit("Warning: Remote host refused compression.");
Damien Miller95def091999-11-25 00:26:21 +1100872 else
873 packet_disconnect("Protocol error waiting for compression response.");
874 }
875 /* Allocate a pseudo tty if appropriate. */
876 if (tty_flag) {
877 debug("Requesting pty.");
878
879 /* Start the packet. */
880 packet_start(SSH_CMSG_REQUEST_PTY);
881
882 /* Store TERM in the packet. There is no limit on the
883 length of the string. */
884 cp = getenv("TERM");
885 if (!cp)
886 cp = "";
Ben Lindstrom664408d2001-06-09 01:42:01 +0000887 packet_put_cstring(cp);
Damien Miller95def091999-11-25 00:26:21 +1100888
889 /* Store window size in the packet. */
890 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
891 memset(&ws, 0, sizeof(ws));
Damien Miller71a73672006-03-26 14:04:36 +1100892 packet_put_int((u_int)ws.ws_row);
893 packet_put_int((u_int)ws.ws_col);
894 packet_put_int((u_int)ws.ws_xpixel);
895 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller95def091999-11-25 00:26:21 +1100896
897 /* Store tty modes in the packet. */
Ben Lindstromae8e2d32001-04-14 23:13:02 +0000898 tty_make_modes(fileno(stdin), NULL);
Damien Miller95def091999-11-25 00:26:21 +1100899
900 /* Send the packet, and wait for it to leave. */
901 packet_send();
902 packet_write_wait();
903
904 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100905 type = packet_read();
Damien Miller450a7a12000-03-26 13:04:51 +1000906 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100907 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +1000908 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000909 } else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000910 logit("Warning: Remote host failed or refused to allocate a pseudo tty.");
Damien Miller95def091999-11-25 00:26:21 +1100911 else
912 packet_disconnect("Protocol error waiting for pty request response.");
913 }
914 /* Request X11 forwarding if enabled and DISPLAY is set. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000915 display = getenv("DISPLAY");
916 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000917 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +1000918 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000919 client_x11_get_proto(display, options.xauth_location,
920 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +1000921 /* Request forwarding with authentication spoofing. */
Damien Miller95def091999-11-25 00:26:21 +1100922 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +1000923 x11_request_forwarding_with_spoofing(0, display, proto, data);
Damien Miller95def091999-11-25 00:26:21 +1100924
925 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100926 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100927 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100928 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +1000929 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller996acd22003-04-09 20:59:48 +1000930 logit("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +1000931 } else {
Damien Miller95def091999-11-25 00:26:21 +1100932 packet_disconnect("Protocol error waiting for X11 forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +1000933 }
Damien Miller95def091999-11-25 00:26:21 +1100934 }
935 /* Tell the packet module whether this is an interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000936 packet_set_interactive(interactive);
Damien Miller95def091999-11-25 00:26:21 +1100937
938 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100939 check_agent_present();
940
Damien Miller95def091999-11-25 00:26:21 +1100941 if (options.forward_agent) {
942 debug("Requesting authentication agent forwarding.");
943 auth_request_forwarding();
944
945 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100946 type = packet_read();
Damien Miller48b03fc2002-01-22 23:11:40 +1100947 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100948 if (type != SSH_SMSG_SUCCESS)
Damien Miller996acd22003-04-09 20:59:48 +1000949 logit("Warning: Remote host denied authentication agent forwarding.");
Damien Miller95def091999-11-25 00:26:21 +1100950 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000951
Damien Miller0bc1bd82000-11-13 22:57:25 +1100952 /* Initiate port forwardings. */
953 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000954
Damien Miller5428f641999-11-25 11:54:57 +1100955 /* If requested, let ssh continue in the background. */
Damien Miller4af51302000-04-16 11:18:38 +1000956 if (fork_after_authentication_flag)
Damien Miller5428f641999-11-25 11:54:57 +1100957 if (daemon(1, 1) < 0)
958 fatal("daemon() failed: %.200s", strerror(errno));
959
960 /*
961 * If a command was specified on the command line, execute the
962 * command now. Otherwise request the server to start a shell.
963 */
Damien Miller95def091999-11-25 00:26:21 +1100964 if (buffer_len(&command) > 0) {
965 int len = buffer_len(&command);
966 if (len > 900)
967 len = 900;
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100968 debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
Damien Miller95def091999-11-25 00:26:21 +1100969 packet_start(SSH_CMSG_EXEC_CMD);
970 packet_put_string(buffer_ptr(&command), buffer_len(&command));
971 packet_send();
972 packet_write_wait();
973 } else {
974 debug("Requesting shell.");
975 packet_start(SSH_CMSG_EXEC_SHELL);
976 packet_send();
977 packet_write_wait();
978 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000979
Damien Miller95def091999-11-25 00:26:21 +1100980 /* Enter the interactive session. */
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000981 return client_loop(have_tty, tty_flag ?
982 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
Damien Miller1383bd82000-04-06 12:32:37 +1000983}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000984
Ben Lindstrombba81212001-06-25 05:01:22 +0000985static void
Damien Miller0e220db2004-06-15 10:34:08 +1000986ssh_subsystem_reply(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000987{
988 int id, len;
989
990 id = packet_get_int();
991 len = buffer_len(&command);
Ben Lindstrom4040fe12001-03-05 06:52:57 +0000992 if (len > 900)
993 len = 900;
Damien Miller48b03fc2002-01-22 23:11:40 +1100994 packet_check_eom();
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000995 if (type == SSH2_MSG_CHANNEL_FAILURE)
996 fatal("Request for subsystem '%.*s' failed on channel %d",
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100997 len, (u_char *)buffer_ptr(&command), id);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000998}
999
Damien Miller2797f7f2002-04-23 21:09:44 +10001000void
Damien Miller509b0102003-12-17 16:33:10 +11001001client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt)
Damien Miller2797f7f2002-04-23 21:09:44 +10001002{
1003 int i;
1004
1005 i = client_global_request_id++;
Damien Miller509b0102003-12-17 16:33:10 +11001006 if (i >= options.num_remote_forwards)
Damien Miller2797f7f2002-04-23 21:09:44 +10001007 return;
Damien Miller2797f7f2002-04-23 21:09:44 +10001008 debug("remote forward %s for: listen %d, connect %s:%d",
1009 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
Damien Millerf91ee4c2005-03-01 21:24:33 +11001010 options.remote_forwards[i].listen_port,
1011 options.remote_forwards[i].connect_host,
1012 options.remote_forwards[i].connect_port);
Damien Miller2797f7f2002-04-23 21:09:44 +10001013 if (type == SSH2_MSG_REQUEST_FAILURE)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001014 logit("Warning: remote port forwarding failed for listen "
1015 "port %d", options.remote_forwards[i].listen_port);
Damien Miller2797f7f2002-04-23 21:09:44 +10001016}
1017
Damien Miller0e220db2004-06-15 10:34:08 +10001018static void
1019ssh_control_listener(void)
1020{
1021 struct sockaddr_un addr;
1022 mode_t old_umask;
Damien Miller07b6ff12004-06-15 11:14:45 +10001023 int addr_len;
1024
Damien Millerd14b1e72005-06-16 13:19:41 +10001025 if (options.control_path == NULL ||
1026 options.control_master == SSHCTL_MASTER_NO)
Damien Miller0e220db2004-06-15 10:34:08 +10001027 return;
1028
Damien Millerd14b1e72005-06-16 13:19:41 +10001029 debug("setting up multiplex master socket");
1030
Damien Miller0e220db2004-06-15 10:34:08 +10001031 memset(&addr, '\0', sizeof(addr));
1032 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001033 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001034 strlen(options.control_path) + 1;
1035
1036 if (strlcpy(addr.sun_path, options.control_path,
1037 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1038 fatal("ControlPath too long");
1039
1040 if ((control_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001041 fatal("%s socket(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001042
1043 old_umask = umask(0177);
Damien Miller90967402006-03-26 14:07:26 +11001044 if (bind(control_fd, (struct sockaddr *)&addr, addr_len) == -1) {
Damien Miller0e220db2004-06-15 10:34:08 +10001045 control_fd = -1;
Damien Miller4f10e252005-05-04 15:33:09 +10001046 if (errno == EINVAL || errno == EADDRINUSE)
Damien Miller0e220db2004-06-15 10:34:08 +10001047 fatal("ControlSocket %s already exists",
1048 options.control_path);
1049 else
Damien Miller15d72a02005-11-05 15:07:33 +11001050 fatal("%s bind(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001051 }
1052 umask(old_umask);
1053
1054 if (listen(control_fd, 64) == -1)
Damien Miller15d72a02005-11-05 15:07:33 +11001055 fatal("%s listen(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001056
1057 set_nonblock(control_fd);
1058}
1059
Ben Lindstromf558cf62001-09-20 23:13:49 +00001060/* request pty/x11/agent/tcpfwd/shell for channel */
Ben Lindstrombba81212001-06-25 05:01:22 +00001061static void
Ben Lindstromf558cf62001-09-20 23:13:49 +00001062ssh_session2_setup(int id, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +10001063{
Damien Miller3756dce2004-06-18 01:17:29 +10001064 extern char **environ;
Damien Miller17e7ed02005-06-17 12:54:33 +10001065 const char *display;
Damien Miller0e220db2004-06-15 10:34:08 +10001066 int interactive = tty_flag;
Damien Miller17e7ed02005-06-17 12:54:33 +10001067
Damien Miller46d38de2005-07-17 17:02:09 +10001068 display = getenv("DISPLAY");
Damien Miller17e7ed02005-06-17 12:54:33 +10001069 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001070 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001071 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001072 client_x11_get_proto(display, options.xauth_location,
1073 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001074 /* Request forwarding with authentication spoofing. */
1075 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +10001076 x11_request_forwarding_with_spoofing(id, display, proto, data);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001077 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001078 /* XXX wait for reply */
1079 }
1080
Damien Miller0bc1bd82000-11-13 22:57:25 +11001081 check_agent_present();
1082 if (options.forward_agent) {
1083 debug("Requesting authentication agent forwarding.");
1084 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1085 packet_send();
1086 }
1087
Damien Miller7b58e802005-12-13 19:33:19 +11001088 if (options.tun_open != SSH_TUNMODE_NO) {
Damien Millerd27b9472005-12-13 19:29:02 +11001089 Channel *c;
1090 int fd;
1091
1092 debug("Requesting tun.");
Damien Miller7b58e802005-12-13 19:33:19 +11001093 if ((fd = tun_open(options.tun_local,
1094 options.tun_open)) >= 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11001095 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1096 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1097 0, "tun", 1);
1098 c->datagram = 1;
Damien Miller598bbc22005-12-31 16:33:36 +11001099#if defined(SSH_TUN_FILTER)
1100 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1101 channel_register_filter(c->self, sys_tun_infilter,
1102 sys_tun_outfilter);
1103#endif
Damien Millerd27b9472005-12-13 19:29:02 +11001104 packet_start(SSH2_MSG_CHANNEL_OPEN);
1105 packet_put_cstring("tun@openssh.com");
1106 packet_put_int(c->self);
1107 packet_put_int(c->local_window_max);
1108 packet_put_int(c->local_maxpacket);
Damien Miller7b58e802005-12-13 19:33:19 +11001109 packet_put_int(options.tun_open);
Damien Millerd27b9472005-12-13 19:29:02 +11001110 packet_put_int(options.tun_remote);
1111 packet_send();
1112 }
1113 }
1114
Damien Miller0e220db2004-06-15 10:34:08 +10001115 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
Damien Miller3756dce2004-06-18 01:17:29 +10001116 NULL, fileno(stdin), &command, environ, &ssh_subsystem_reply);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001117
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001118 packet_set_interactive(interactive);
Damien Miller1383bd82000-04-06 12:32:37 +10001119}
1120
Ben Lindstromf558cf62001-09-20 23:13:49 +00001121/* open new channel for a session */
Ben Lindstrombba81212001-06-25 05:01:22 +00001122static int
Ben Lindstromf558cf62001-09-20 23:13:49 +00001123ssh_session2_open(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001124{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001125 Channel *c;
1126 int window, packetmax, in, out, err;
Damien Millerad833b32000-08-23 10:46:23 +10001127
Damien Millercaf6dd62000-08-29 11:33:50 +11001128 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001129 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001130 } else {
1131 in = dup(STDIN_FILENO);
1132 }
1133 out = dup(STDOUT_FILENO);
1134 err = dup(STDERR_FILENO);
1135
1136 if (in < 0 || out < 0 || err < 0)
1137 fatal("dup() in/out/err failed");
1138
Damien Miller69b69aa2000-10-28 14:19:58 +11001139 /* enable nonblocking unless tty */
1140 if (!isatty(in))
1141 set_nonblock(in);
1142 if (!isatty(out))
1143 set_nonblock(out);
1144 if (!isatty(err))
1145 set_nonblock(err);
1146
Damien Millere4340be2000-09-16 13:29:08 +11001147 window = CHAN_SES_WINDOW_DEFAULT;
1148 packetmax = CHAN_SES_PACKET_DEFAULT;
Damien Miller19a59452002-02-19 15:20:57 +11001149 if (tty_flag) {
1150 window >>= 1;
1151 packetmax >>= 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001152 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001153 c = channel_new(
Damien Miller1383bd82000-04-06 12:32:37 +10001154 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001155 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001156 "client-session", /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001157
Ben Lindstromf558cf62001-09-20 23:13:49 +00001158 debug3("ssh_session2_open: channel_new: %d", c->self);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001159
Ben Lindstrom5ec26452001-06-09 00:18:51 +00001160 channel_send_open(c->self);
Ben Lindstromf558cf62001-09-20 23:13:49 +00001161 if (!no_shell_flag)
Damien Miller0e220db2004-06-15 10:34:08 +10001162 channel_register_confirm(c->self, ssh_session2_setup, NULL);
Damien Miller1383bd82000-04-06 12:32:37 +10001163
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001164 return c->self;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001165}
1166
Ben Lindstrombba81212001-06-25 05:01:22 +00001167static int
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001168ssh_session2(void)
1169{
Ben Lindstromf558cf62001-09-20 23:13:49 +00001170 int id = -1;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001171
1172 /* XXX should be pre-session */
1173 ssh_init_forwarding();
Damien Miller0e220db2004-06-15 10:34:08 +10001174 ssh_control_listener();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001175
Ben Lindstromf558cf62001-09-20 23:13:49 +00001176 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1177 id = ssh_session2_open();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001178
Damien Millerd27b9472005-12-13 19:29:02 +11001179 /* Execute a local command */
1180 if (options.local_command != NULL &&
1181 options.permit_local_command)
1182 ssh_local_cmd(options.local_command);
1183
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001184 /* If requested, let ssh continue in the background. */
1185 if (fork_after_authentication_flag)
1186 if (daemon(1, 1) < 0)
1187 fatal("daemon() failed: %.200s", strerror(errno));
1188
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001189 return client_loop(tty_flag, tty_flag ?
1190 options.escape_char : SSH_ESCAPECHAR_NONE, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001191}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001192
Ben Lindstrombba81212001-06-25 05:01:22 +00001193static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001194load_public_identity_files(void)
1195{
1196 char *filename;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001197 int i = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001198 Key *public;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001199#ifdef SMARTCARD
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001200 Key **keys;
1201
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001202 if (options.smartcard_device != NULL &&
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001203 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1204 (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL ) {
1205 int count = 0;
1206 for (i = 0; keys[i] != NULL; i++) {
1207 count++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001208 memmove(&options.identity_files[1], &options.identity_files[0],
1209 sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
1210 memmove(&options.identity_keys[1], &options.identity_keys[0],
1211 sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1212 options.num_identity_files++;
1213 options.identity_keys[0] = keys[i];
Damien Miller56a0bb02003-06-18 20:28:40 +10001214 options.identity_files[0] = sc_get_key_label(keys[i]);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001215 }
Ben Lindstrom4f054602002-03-26 03:23:00 +00001216 if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1217 options.num_identity_files = SSH_MAX_IDENTITY_FILES;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001218 i = count;
1219 xfree(keys);
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001220 }
Ben Lindstromffce1472001-08-06 21:57:31 +00001221#endif /* SMARTCARD */
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001222 for (; i < options.num_identity_files; i++) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001223 filename = tilde_expand_filename(options.identity_files[i],
1224 original_real_uid);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001225 public = key_load_public(filename, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001226 debug("identity file %s type %d", filename,
1227 public ? public->type : -1);
1228 xfree(options.identity_files[i]);
1229 options.identity_files[i] = filename;
1230 options.identity_keys[i] = public;
1231 }
1232}
Damien Miller0e220db2004-06-15 10:34:08 +10001233
1234static void
1235control_client_sighandler(int signo)
1236{
1237 control_client_terminate = signo;
1238}
1239
1240static void
1241control_client_sigrelay(int signo)
1242{
1243 if (control_server_pid > 1)
1244 kill(control_server_pid, signo);
1245}
1246
Darren Tucker365433f2004-06-22 12:29:23 +10001247static int
1248env_permitted(char *env)
1249{
1250 int i;
1251 char name[1024], *cp;
1252
1253 strlcpy(name, env, sizeof(name));
1254 if ((cp = strchr(name, '=')) == NULL)
1255 return (0);
1256
1257 *cp = '\0';
1258
1259 for (i = 0; i < options.num_send_env; i++)
1260 if (match_pattern(name, options.send_env[i]))
1261 return (1);
1262
1263 return (0);
1264}
1265
Damien Miller0e220db2004-06-15 10:34:08 +10001266static void
1267control_client(const char *path)
1268{
1269 struct sockaddr_un addr;
Darren Tucker39207a42004-11-05 20:19:51 +11001270 int i, r, fd, sock, exitval, num_env, addr_len;
Damien Miller0e220db2004-06-15 10:34:08 +10001271 Buffer m;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001272 char *term;
Damien Miller3756dce2004-06-18 01:17:29 +10001273 extern char **environ;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001274 u_int flags;
Darren Tuckerfc959702004-07-17 16:12:08 +10001275
Damien Millerd14b1e72005-06-16 13:19:41 +10001276 if (mux_command == 0)
1277 mux_command = SSHMUX_COMMAND_OPEN;
1278
1279 switch (options.control_master) {
1280 case SSHCTL_MASTER_AUTO:
1281 case SSHCTL_MASTER_AUTO_ASK:
1282 debug("auto-mux: Trying existing master");
1283 /* FALLTHROUGH */
1284 case SSHCTL_MASTER_NO:
1285 break;
1286 default:
1287 return;
1288 }
1289
Damien Miller0e220db2004-06-15 10:34:08 +10001290 memset(&addr, '\0', sizeof(addr));
1291 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001292 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001293 strlen(path) + 1;
1294
1295 if (strlcpy(addr.sun_path, path,
1296 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1297 fatal("ControlPath too long");
1298
1299 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1300 fatal("%s socket(): %s", __func__, strerror(errno));
1301
Damien Miller90967402006-03-26 14:07:26 +11001302 if (connect(sock, (struct sockaddr *)&addr, addr_len) == -1) {
Darren Tucker0814d312005-06-01 23:08:51 +10001303 if (mux_command != SSHMUX_COMMAND_OPEN) {
1304 fatal("Control socket connect(%.100s): %s", path,
1305 strerror(errno));
1306 }
Damien Miller538c9b72005-05-26 12:11:28 +10001307 if (errno == ENOENT)
Damien Miller4662d342006-03-26 13:59:59 +11001308 debug("Control socket \"%.100s\" does not exist", path);
Damien Miller538c9b72005-05-26 12:11:28 +10001309 else {
Damien Miller4662d342006-03-26 13:59:59 +11001310 error("Control socket connect(%.100s): %s", path,
Damien Miller538c9b72005-05-26 12:11:28 +10001311 strerror(errno));
1312 }
Damien Miller4662d342006-03-26 13:59:59 +11001313 close(sock);
1314 return;
1315 }
Damien Miller46d38de2005-07-17 17:02:09 +10001316
Damien Miller4662d342006-03-26 13:59:59 +11001317 if (stdin_null_flag) {
1318 if ((fd = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1319 fatal("open(/dev/null): %s", strerror(errno));
1320 if (dup2(fd, STDIN_FILENO) == -1)
1321 fatal("dup2: %s", strerror(errno));
1322 if (fd > STDERR_FILENO)
1323 close(fd);
1324 }
Damien Miller46d38de2005-07-17 17:02:09 +10001325
Damien Miller13390022005-07-06 09:44:19 +10001326 term = getenv("TERM");
Darren Tucker7ebfc102004-11-07 20:06:19 +11001327
1328 flags = 0;
1329 if (tty_flag)
1330 flags |= SSHMUX_FLAG_TTY;
1331 if (subsystem_flag)
1332 flags |= SSHMUX_FLAG_SUBSYS;
Damien Miller13390022005-07-06 09:44:19 +10001333 if (options.forward_x11)
1334 flags |= SSHMUX_FLAG_X11_FWD;
1335 if (options.forward_agent)
1336 flags |= SSHMUX_FLAG_AGENT_FWD;
Damien Miller0e220db2004-06-15 10:34:08 +10001337
Damien Miller0e220db2004-06-15 10:34:08 +10001338 buffer_init(&m);
1339
Darren Tucker7ebfc102004-11-07 20:06:19 +11001340 /* Send our command to server */
1341 buffer_put_int(&m, mux_command);
1342 buffer_put_int(&m, flags);
Damien Miller13390022005-07-06 09:44:19 +10001343 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001344 fatal("%s: msg_send", __func__);
1345 buffer_clear(&m);
1346
1347 /* Get authorisation status and PID of controlee */
Damien Miller0e220db2004-06-15 10:34:08 +10001348 if (ssh_msg_recv(sock, &m) == -1)
1349 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001350 if (buffer_get_char(&m) != SSHMUX_VER)
Damien Miller0e220db2004-06-15 10:34:08 +10001351 fatal("%s: wrong version", __func__);
Damien Miller23f07702004-06-18 01:19:03 +10001352 if (buffer_get_int(&m) != 1)
1353 fatal("Connection to master denied");
Damien Miller0e220db2004-06-15 10:34:08 +10001354 control_server_pid = buffer_get_int(&m);
1355
Damien Miller0e220db2004-06-15 10:34:08 +10001356 buffer_clear(&m);
Damien Miller0e220db2004-06-15 10:34:08 +10001357
Darren Tucker7ebfc102004-11-07 20:06:19 +11001358 switch (mux_command) {
1359 case SSHMUX_COMMAND_ALIVE_CHECK:
Darren Tucker47eede72005-03-14 23:08:12 +11001360 fprintf(stderr, "Master running (pid=%d)\r\n",
Darren Tucker7ebfc102004-11-07 20:06:19 +11001361 control_server_pid);
1362 exit(0);
1363 case SSHMUX_COMMAND_TERMINATE:
1364 fprintf(stderr, "Exit request sent.\r\n");
1365 exit(0);
1366 case SSHMUX_COMMAND_OPEN:
1367 /* continue below */
1368 break;
1369 default:
1370 fatal("silly mux_command %d", mux_command);
1371 }
1372
1373 /* SSHMUX_COMMAND_OPEN */
Damien Miller13390022005-07-06 09:44:19 +10001374 buffer_put_cstring(&m, term ? term : "");
Damien Miller0e220db2004-06-15 10:34:08 +10001375 buffer_append(&command, "\0", 1);
1376 buffer_put_cstring(&m, buffer_ptr(&command));
1377
Darren Tucker365433f2004-06-22 12:29:23 +10001378 if (options.num_send_env == 0 || environ == NULL) {
1379 buffer_put_int(&m, 0);
Darren Tuckerfc959702004-07-17 16:12:08 +10001380 } else {
Darren Tucker365433f2004-06-22 12:29:23 +10001381 /* Pass environment */
1382 num_env = 0;
1383 for (i = 0; environ[i] != NULL; i++)
1384 if (env_permitted(environ[i]))
1385 num_env++; /* Count */
Darren Tuckerfc959702004-07-17 16:12:08 +10001386
Darren Tucker365433f2004-06-22 12:29:23 +10001387 buffer_put_int(&m, num_env);
1388
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001389 for (i = 0; environ[i] != NULL && num_env >= 0; i++)
1390 if (env_permitted(environ[i])) {
1391 num_env--;
Darren Tucker365433f2004-06-22 12:29:23 +10001392 buffer_put_cstring(&m, environ[i]);
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001393 }
Darren Tucker365433f2004-06-22 12:29:23 +10001394 }
Damien Miller3756dce2004-06-18 01:17:29 +10001395
Damien Miller13390022005-07-06 09:44:19 +10001396 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Damien Miller0e220db2004-06-15 10:34:08 +10001397 fatal("%s: msg_send", __func__);
1398
1399 mm_send_fd(sock, STDIN_FILENO);
1400 mm_send_fd(sock, STDOUT_FILENO);
1401 mm_send_fd(sock, STDERR_FILENO);
1402
1403 /* Wait for reply, so master has a chance to gather ttymodes */
1404 buffer_clear(&m);
1405 if (ssh_msg_recv(sock, &m) == -1)
1406 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001407 if (buffer_get_char(&m) != SSHMUX_VER)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001408 fatal("%s: wrong version", __func__);
Damien Miller0e220db2004-06-15 10:34:08 +10001409 buffer_free(&m);
1410
Darren Tucker07336da2004-11-05 20:02:16 +11001411 signal(SIGHUP, control_client_sighandler);
Damien Miller0809e232004-06-18 22:20:57 +10001412 signal(SIGINT, control_client_sighandler);
1413 signal(SIGTERM, control_client_sighandler);
1414 signal(SIGWINCH, control_client_sigrelay);
1415
Damien Miller0e220db2004-06-15 10:34:08 +10001416 if (tty_flag)
1417 enter_raw_mode();
1418
1419 /* Stick around until the controlee closes the client_fd */
1420 exitval = 0;
1421 for (;!control_client_terminate;) {
1422 r = read(sock, &exitval, sizeof(exitval));
1423 if (r == 0) {
1424 debug2("Received EOF from master");
1425 break;
1426 }
1427 if (r > 0)
1428 debug2("Received exit status from master %d", exitval);
1429 if (r == -1 && errno != EINTR)
1430 fatal("%s: read %s", __func__, strerror(errno));
1431 }
1432
1433 if (control_client_terminate)
1434 debug2("Exiting on signal %d", control_client_terminate);
1435
1436 close(sock);
1437
1438 leave_raw_mode();
1439
1440 if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
1441 fprintf(stderr, "Connection to master closed.\r\n");
1442
1443 exit(exitval);
1444}