blob: 18631f272db914abfc70ba82bf70f6eba08a6ea8 [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 Millerf17883e2006-03-15 11:45:54 +110043RCSID("$OpenBSD: ssh.c,v 1.264 2006/02/20 17:19:54 stevesk Exp $");
Damien Millercd4223c2006-03-15 11:22:47 +110044
Damien Millerf17883e2006-03-15 11:45:54 +110045#include <sys/types.h>
46#ifdef HAVE_SYS_STAT_H
47# include <sys/stat.h>
48#endif
Damien Millercd4223c2006-03-15 11:22:47 +110049#include <sys/resource.h>
Damien Miller17e91c02006-03-15 11:28:34 +110050#include <sys/ioctl.h>
Damien Miller574c41f2006-03-15 11:40:10 +110051#include <sys/un.h>
Damien Miller03e20032006-03-15 11:16:59 +110052
53#include <paths.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110054#include <signal.h>
Damien Millereba71ba2000-04-29 23:57:08 +100055
56#include <openssl/evp.h>
Damien Miller0bc1bd82000-11-13 22:57:25 +110057#include <openssl/err.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000060#include "ssh1.h"
Damien Miller1383bd82000-04-06 12:32:37 +100061#include "ssh2.h"
62#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063#include "cipher.h"
64#include "xmalloc.h"
65#include "packet.h"
66#include "buffer.h"
Damien Miller0e220db2004-06-15 10:34:08 +100067#include "bufaux.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000068#include "channels.h"
Damien Millereba71ba2000-04-29 23:57:08 +100069#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100070#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100071#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000072#include "pathnames.h"
Damien Miller0e220db2004-06-15 10:34:08 +100073#include "dispatch.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +000074#include "clientloop.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000075#include "log.h"
76#include "readconf.h"
77#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000078#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000079#include "kex.h"
80#include "mac.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +100081#include "sshpty.h"
Darren Tucker46bc0752004-05-02 22:11:30 +100082#include "match.h"
Damien Miller0e220db2004-06-15 10:34:08 +100083#include "msg.h"
84#include "monitor_fdpass.h"
Darren Tucker25f60a72004-08-15 17:23:34 +100085#include "uidswap.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086
Ben Lindstromc5b68002001-07-04 04:52:03 +000087#ifdef SMARTCARD
Ben Lindstromc5b68002001-07-04 04:52:03 +000088#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000089#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +000090
Damien Miller3f905871999-11-15 17:10:57 +110091extern char *__progname;
Damien Miller3f905871999-11-15 17:10:57 +110092
Damien Miller95def091999-11-25 00:26:21 +110093/* Flag indicating whether debug mode is on. This can be set on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094int debug_flag = 0;
95
Damien Miller78928792000-04-12 20:17:38 +100096/* Flag indicating whether a tty should be allocated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097int tty_flag = 0;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +000098int no_tty_flag = 0;
99int force_tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100
Damien Miller1383bd82000-04-06 12:32:37 +1000101/* don't exec a shell */
102int no_shell_flag = 0;
Damien Miller1383bd82000-04-06 12:32:37 +1000103
Damien Miller5428f641999-11-25 11:54:57 +1100104/*
105 * Flag indicating that nothing should be read from stdin. This can be set
106 * on the command line.
107 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108int stdin_null_flag = 0;
109
Damien Miller5428f641999-11-25 11:54:57 +1100110/*
111 * Flag indicating that ssh should fork after authentication. This is useful
Ben Lindstromf666fec2002-06-06 19:51:58 +0000112 * so that the passphrase can be entered manually, and then ssh goes to the
Damien Miller5428f641999-11-25 11:54:57 +1100113 * background.
114 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115int fork_after_authentication_flag = 0;
116
Damien Miller5428f641999-11-25 11:54:57 +1100117/*
118 * General data structure for command line options and options configurable
119 * in configuration files. See readconf.h.
120 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000121Options options;
122
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000123/* optional user configfile */
124char *config = NULL;
125
Damien Miller5428f641999-11-25 11:54:57 +1100126/*
127 * Name of the host we are connecting to. This is the name given on the
128 * command line, or the HostName specified for the user-supplied name in a
129 * configuration file.
130 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131char *host;
132
133/* socket address the host resolves to */
Damien Miller34132e52000-01-14 15:45:46 +1100134struct sockaddr_storage hostaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000136/* Private host keys. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000137Sensitive sensitive_data;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138
139/* Original real UID. */
140uid_t original_real_uid;
Ben Lindstromf9c48842002-06-11 16:37:51 +0000141uid_t original_effective_uid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142
Damien Miller1383bd82000-04-06 12:32:37 +1000143/* command to be executed */
144Buffer command;
145
Damien Miller832562e2001-01-30 09:30:01 +1100146/* Should we execute a command or invoke a subsystem? */
147int subsystem_flag = 0;
148
Damien Miller2797f7f2002-04-23 21:09:44 +1000149/* # of replies received for global requests */
150static int client_global_request_id = 0;
151
Damien Miller8c4e18a2002-09-19 12:05:02 +1000152/* pid of proxycommand child process */
153pid_t proxy_command_pid = 0;
154
Damien Miller0e220db2004-06-15 10:34:08 +1000155/* fd to control socket */
156int control_fd = -1;
157
Darren Tucker7ebfc102004-11-07 20:06:19 +1100158/* Multiplexing control command */
Darren Tucker0814d312005-06-01 23:08:51 +1000159static u_int mux_command = 0;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100160
Damien Miller0e220db2004-06-15 10:34:08 +1000161/* Only used in control client mode */
162volatile sig_atomic_t control_client_terminate = 0;
163u_int control_server_pid = 0;
164
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165/* Prints a help message to the user. This function never returns. */
166
Ben Lindstrombba81212001-06-25 05:01:22 +0000167static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000168usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000169{
Damien Miller50955102004-03-22 09:34:58 +1100170 fprintf(stderr,
Darren Tucker9c6bf322004-12-03 14:10:19 +1100171"usage: ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n"
Darren Tucker895d6982005-10-03 18:18:05 +1000172" [-D [bind_address:]port] [-e escape_char] [-F configfile]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100173" [-i identity_file] [-L [bind_address:]port:host:hostport]\n"
Damien Millerf91ee4c2005-03-01 21:24:33 +1100174" [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100175" [-R [bind_address:]port:host:hostport] [-S ctl_path]\n"
Damien Millerd27b9472005-12-13 19:29:02 +1100176" [-w tunnel:tunnel] [user@]hostname [command]\n"
Damien Miller50955102004-03-22 09:34:58 +1100177 );
Darren Tuckere9a9b712005-12-20 16:15:51 +1100178 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000179}
180
Ben Lindstrombba81212001-06-25 05:01:22 +0000181static int ssh_session(void);
182static int ssh_session2(void);
183static void load_public_identity_files(void);
Damien Miller0e220db2004-06-15 10:34:08 +1000184static void control_client(const char *path);
Damien Miller1383bd82000-04-06 12:32:37 +1000185
Damien Miller95def091999-11-25 00:26:21 +1100186/*
187 * Main program for the ssh client.
188 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000189int
190main(int ac, char **av)
191{
Ben Lindstrom24157572002-06-12 16:09:39 +0000192 int i, opt, exit_status;
Damien Miller9836cf82003-12-17 16:30:06 +1100193 char *p, *cp, *line, buf[256];
Damien Miller95def091999-11-25 00:26:21 +1100194 struct stat st;
Ben Lindstrom086cf212001-03-05 05:56:40 +0000195 struct passwd *pw;
Damien Miller1383bd82000-04-06 12:32:37 +1000196 int dummy;
Ben Lindstrom5ccf63a2001-09-24 20:00:10 +0000197 extern int optind, optreset;
Damien Miller4f8e6692001-07-14 13:22:53 +1000198 extern char *optarg;
Damien Miller9651fe62005-06-26 08:55:25 +1000199 struct servent *sp;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100200 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000201
Darren Tuckerce321d82005-10-03 18:11:24 +1000202 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
203 sanitise_stdfd();
204
Damien Miller59d3d5b2003-08-22 09:34:41 +1000205 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000206 init_rng();
207
Damien Miller5428f641999-11-25 11:54:57 +1100208 /*
209 * Save the original real uid. It will be needed later (uid-swapping
210 * may clobber the real uid).
211 */
Damien Miller95def091999-11-25 00:26:21 +1100212 original_real_uid = getuid();
213 original_effective_uid = geteuid();
Damien Millera8e06ce2003-11-21 23:48:55 +1100214
Damien Miller50b9a602002-09-04 16:50:06 +1000215 /*
216 * Use uid-swapping to give up root privileges for the duration of
217 * option processing. We will re-instantiate the rights when we are
218 * ready to create the privileged port, and will permanently drop
219 * them when the port has been created (actually, when the connection
220 * has been made, as we may need to create the port several times).
221 */
222 PRIV_END;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000223
Damien Miller05dd7952000-10-01 00:42:48 +1100224#ifdef HAVE_SETRLIMIT
Damien Miller95def091999-11-25 00:26:21 +1100225 /* If we are installed setuid root be careful to not drop core. */
226 if (original_real_uid != original_effective_uid) {
227 struct rlimit rlim;
228 rlim.rlim_cur = rlim.rlim_max = 0;
229 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
230 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000231 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100232#endif
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000233 /* Get user data. */
234 pw = getpwuid(original_real_uid);
235 if (!pw) {
Damien Miller996acd22003-04-09 20:59:48 +1000236 logit("You don't exist, go away!");
Darren Tuckere9a9b712005-12-20 16:15:51 +1100237 exit(255);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000238 }
239 /* Take a copy of the returned structure. */
240 pw = pwcopy(pw);
241
Damien Miller5428f641999-11-25 11:54:57 +1100242 /*
Damien Miller5428f641999-11-25 11:54:57 +1100243 * Set our umask to something reasonable, as some files are created
244 * with the default umask. This will make them world-readable but
245 * writable only by the owner, which is ok for all files for which we
246 * don't set the modes explicitly.
247 */
Damien Miller95def091999-11-25 00:26:21 +1100248 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000249
Damien Miller95def091999-11-25 00:26:21 +1100250 /* Initialize option structure to indicate that no values have been set. */
251 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252
Damien Miller95def091999-11-25 00:26:21 +1100253 /* Parse command-line arguments. */
254 host = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000255
Damien Millerf4614452001-07-14 12:18:10 +1000256again:
257 while ((opt = getopt(ac, av,
Damien Millerd27b9472005-12-13 19:29:02 +1100258 "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 +1100259 switch (opt) {
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000260 case '1':
261 options.protocol = SSH_PROTO_1;
262 break;
Damien Miller4af51302000-04-16 11:18:38 +1000263 case '2':
264 options.protocol = SSH_PROTO_2;
265 break;
Damien Miller34132e52000-01-14 15:45:46 +1100266 case '4':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000267 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100268 break;
Damien Miller34132e52000-01-14 15:45:46 +1100269 case '6':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000270 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100271 break;
Damien Miller95def091999-11-25 00:26:21 +1100272 case 'n':
273 stdin_null_flag = 1;
274 break;
Damien Miller95def091999-11-25 00:26:21 +1100275 case 'f':
276 fork_after_authentication_flag = 1;
277 stdin_null_flag = 1;
278 break;
Damien Miller95def091999-11-25 00:26:21 +1100279 case 'x':
280 options.forward_x11 = 0;
281 break;
Damien Miller95def091999-11-25 00:26:21 +1100282 case 'X':
283 options.forward_x11 = 1;
284 break;
Darren Tucker0a118da2003-10-15 15:54:32 +1000285 case 'Y':
286 options.forward_x11 = 1;
287 options.forward_x11_trusted = 1;
288 break;
Damien Miller95def091999-11-25 00:26:21 +1100289 case 'g':
290 options.gateway_ports = 1;
291 break;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100292 case 'O':
293 if (strcmp(optarg, "check") == 0)
294 mux_command = SSHMUX_COMMAND_ALIVE_CHECK;
295 else if (strcmp(optarg, "exit") == 0)
296 mux_command = SSHMUX_COMMAND_TERMINATE;
297 else
298 fatal("Invalid multiplex command.");
299 break;
Damien Miller147bba32002-09-04 16:46:06 +1000300 case 'P': /* deprecated */
Damien Miller95def091999-11-25 00:26:21 +1100301 options.use_privileged_port = 0;
302 break;
Damien Miller95def091999-11-25 00:26:21 +1100303 case 'a':
304 options.forward_agent = 0;
305 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000306 case 'A':
307 options.forward_agent = 1;
308 break;
Damien Miller95def091999-11-25 00:26:21 +1100309 case 'k':
Damien Millere0113cc2003-11-24 13:10:09 +1100310 options.gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100311 break;
Damien Miller95def091999-11-25 00:26:21 +1100312 case 'i':
313 if (stat(optarg, &st) < 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000314 fprintf(stderr, "Warning: Identity file %s "
Damien Miller3eb48b62005-03-01 21:15:46 +1100315 "not accessible: %s.\n", optarg,
316 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100317 break;
318 }
Damien Millerf4614452001-07-14 12:18:10 +1000319 if (options.num_identity_files >=
320 SSH_MAX_IDENTITY_FILES)
321 fatal("Too many identity files specified "
322 "(max %d)", SSH_MAX_IDENTITY_FILES);
323 options.identity_files[options.num_identity_files++] =
324 xstrdup(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100325 break;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000326 case 'I':
327#ifdef SMARTCARD
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000328 options.smartcard_device = xstrdup(optarg);
Ben Lindstrombcc18082001-08-06 21:59:25 +0000329#else
Ben Lindstromc5b68002001-07-04 04:52:03 +0000330 fprintf(stderr, "no support for smartcards.\n");
Ben Lindstrombcc18082001-08-06 21:59:25 +0000331#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000332 break;
Damien Miller95def091999-11-25 00:26:21 +1100333 case 't':
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000334 if (tty_flag)
335 force_tty_flag = 1;
Damien Miller95def091999-11-25 00:26:21 +1100336 tty_flag = 1;
337 break;
Damien Miller95def091999-11-25 00:26:21 +1100338 case 'v':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000339 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100340 debug_flag = 1;
341 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000342 } else {
343 if (options.log_level < SYSLOG_LEVEL_DEBUG3)
344 options.log_level++;
Damien Millere4340be2000-09-16 13:29:08 +1100345 break;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000346 }
Darren Tuckere9bf9842004-11-05 20:05:32 +1100347 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100348 case 'V':
Damien Miller0c889cd2004-03-22 09:36:00 +1100349 fprintf(stderr, "%s, %s\n",
Damien Miller2aa6d3c2004-09-12 16:53:04 +1000350 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
Damien Miller95def091999-11-25 00:26:21 +1100351 if (opt == 'V')
352 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100353 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100354 case 'w':
Damien Miller7b58e802005-12-13 19:33:19 +1100355 if (options.tun_open == -1)
356 options.tun_open = SSH_TUNMODE_DEFAULT;
Damien Millerd27b9472005-12-13 19:29:02 +1100357 options.tun_local = a2tun(optarg, &options.tun_remote);
Damien Miller7b58e802005-12-13 19:33:19 +1100358 if (options.tun_local == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100359 fprintf(stderr, "Bad tun device '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100360 exit(255);
Damien Millerd27b9472005-12-13 19:29:02 +1100361 }
362 break;
Damien Miller95def091999-11-25 00:26:21 +1100363 case 'q':
364 options.log_level = SYSLOG_LEVEL_QUIET;
365 break;
Damien Miller95def091999-11-25 00:26:21 +1100366 case 'e':
367 if (optarg[0] == '^' && optarg[2] == 0 &&
Damien Millerf4614452001-07-14 12:18:10 +1000368 (u_char) optarg[1] >= 64 &&
369 (u_char) optarg[1] < 128)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000370 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100371 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000372 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100373 else if (strcmp(optarg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000374 options.escape_char = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100375 else {
Damien Millerf4614452001-07-14 12:18:10 +1000376 fprintf(stderr, "Bad escape character '%s'.\n",
377 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100378 exit(255);
Damien Miller95def091999-11-25 00:26:21 +1100379 }
380 break;
Damien Miller95def091999-11-25 00:26:21 +1100381 case 'c':
Damien Millereba71ba2000-04-29 23:57:08 +1000382 if (ciphers_valid(optarg)) {
383 /* SSH2 only */
384 options.ciphers = xstrdup(optarg);
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000385 options.cipher = SSH_CIPHER_INVALID;
Damien Millereba71ba2000-04-29 23:57:08 +1000386 } else {
387 /* SSH1 only */
Damien Millere39cacc2000-11-29 12:18:44 +1100388 options.cipher = cipher_number(optarg);
389 if (options.cipher == -1) {
Damien Millerf4614452001-07-14 12:18:10 +1000390 fprintf(stderr,
391 "Unknown cipher type '%s'\n",
392 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100393 exit(255);
Damien Millereba71ba2000-04-29 23:57:08 +1000394 }
Damien Millerf4614452001-07-14 12:18:10 +1000395 if (options.cipher == SSH_CIPHER_3DES)
Damien Millere39cacc2000-11-29 12:18:44 +1100396 options.ciphers = "3des-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000397 else if (options.cipher == SSH_CIPHER_BLOWFISH)
Damien Millere39cacc2000-11-29 12:18:44 +1100398 options.ciphers = "blowfish-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000399 else
Damien Millere39cacc2000-11-29 12:18:44 +1100400 options.ciphers = (char *)-1;
Damien Miller95def091999-11-25 00:26:21 +1100401 }
402 break;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000403 case 'm':
404 if (mac_valid(optarg))
405 options.macs = xstrdup(optarg);
406 else {
Damien Millerf4614452001-07-14 12:18:10 +1000407 fprintf(stderr, "Unknown mac type '%s'\n",
408 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100409 exit(255);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000410 }
411 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000412 case 'M':
Damien Millerd14b1e72005-06-16 13:19:41 +1000413 if (options.control_master == SSHCTL_MASTER_YES)
414 options.control_master = SSHCTL_MASTER_ASK;
415 else
416 options.control_master = SSHCTL_MASTER_YES;
Damien Miller0e220db2004-06-15 10:34:08 +1000417 break;
Damien Miller95def091999-11-25 00:26:21 +1100418 case 'p':
Ben Lindstrom19066a12001-04-12 23:39:26 +0000419 options.port = a2port(optarg);
420 if (options.port == 0) {
Ben Lindstrom146edb92001-04-11 23:06:28 +0000421 fprintf(stderr, "Bad port '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100422 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000423 }
Damien Miller95def091999-11-25 00:26:21 +1100424 break;
Damien Miller95def091999-11-25 00:26:21 +1100425 case 'l':
426 options.user = optarg;
427 break;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000428
Damien Miller95def091999-11-25 00:26:21 +1100429 case 'L':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100430 if (parse_forward(&fwd, optarg))
431 add_local_forward(&options, &fwd);
432 else {
Damien Millerf4614452001-07-14 12:18:10 +1000433 fprintf(stderr,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100434 "Bad local forwarding specification '%s'\n",
Damien Millerf4614452001-07-14 12:18:10 +1000435 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100436 exit(255);
Ben Lindstrom1a174712001-09-12 17:56:15 +0000437 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100438 break;
439
440 case 'R':
441 if (parse_forward(&fwd, optarg)) {
442 add_remote_forward(&options, &fwd);
443 } else {
444 fprintf(stderr,
445 "Bad remote forwarding specification "
446 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100447 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100448 }
Damien Miller95def091999-11-25 00:26:21 +1100449 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000450
451 case 'D':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100452 cp = p = xstrdup(optarg);
453 memset(&fwd, '\0', sizeof(fwd));
454 fwd.connect_host = "socks";
455 if ((fwd.listen_host = hpdelim(&cp)) == NULL) {
456 fprintf(stderr, "Bad dynamic forwarding "
457 "specification '%.100s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100458 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100459 }
460 if (cp != NULL) {
461 fwd.listen_port = a2port(cp);
462 fwd.listen_host = cleanhostname(fwd.listen_host);
463 } else {
464 fwd.listen_port = a2port(fwd.listen_host);
Damien Millerbe1045d2005-08-12 22:10:56 +1000465 fwd.listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100466 }
467
468 if (fwd.listen_port == 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000469 fprintf(stderr, "Bad dynamic port '%s'\n",
470 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100471 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000472 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100473 add_local_forward(&options, &fwd);
474 xfree(p);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000475 break;
476
Damien Miller95def091999-11-25 00:26:21 +1100477 case 'C':
478 options.compression = 1;
479 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000480 case 'N':
481 no_shell_flag = 1;
482 no_tty_flag = 1;
483 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000484 case 'T':
485 no_tty_flag = 1;
486 break;
Damien Miller95def091999-11-25 00:26:21 +1100487 case 'o':
488 dummy = 1;
Damien Miller9836cf82003-12-17 16:30:06 +1100489 line = xstrdup(optarg);
Damien Millerf4614452001-07-14 12:18:10 +1000490 if (process_config_line(&options, host ? host : "",
Damien Miller9836cf82003-12-17 16:30:06 +1100491 line, "command-line", 0, &dummy) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100492 exit(255);
Damien Miller9836cf82003-12-17 16:30:06 +1100493 xfree(line);
Damien Miller95def091999-11-25 00:26:21 +1100494 break;
Damien Miller832562e2001-01-30 09:30:01 +1100495 case 's':
496 subsystem_flag = 1;
497 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000498 case 'S':
499 if (options.control_path != NULL)
500 free(options.control_path);
501 options.control_path = xstrdup(optarg);
Damien Miller0e220db2004-06-15 10:34:08 +1000502 break;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000503 case 'b':
504 options.bind_address = optarg;
505 break;
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000506 case 'F':
507 config = optarg;
508 break;
Damien Miller95def091999-11-25 00:26:21 +1100509 default:
510 usage();
511 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000512 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000513
Damien Millerf4614452001-07-14 12:18:10 +1000514 ac -= optind;
515 av += optind;
516
517 if (ac > 0 && !host && **av != '-') {
Ben Lindstromc276c122002-12-23 02:14:51 +0000518 if (strrchr(*av, '@')) {
Damien Millerf4614452001-07-14 12:18:10 +1000519 p = xstrdup(*av);
Ben Lindstromc276c122002-12-23 02:14:51 +0000520 cp = strrchr(p, '@');
Damien Millerf4614452001-07-14 12:18:10 +1000521 if (cp == NULL || cp == p)
522 usage();
523 options.user = p;
524 *cp = '\0';
525 host = ++cp;
526 } else
527 host = *av;
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000528 if (ac > 1) {
529 optind = optreset = 1;
Damien Millerf4614452001-07-14 12:18:10 +1000530 goto again;
531 }
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000532 ac--, av++;
Damien Millerf4614452001-07-14 12:18:10 +1000533 }
534
Damien Miller95def091999-11-25 00:26:21 +1100535 /* Check that we got a host name. */
536 if (!host)
537 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000538
Damien Millercb5e44a2000-09-29 12:12:36 +1100539 SSLeay_add_all_algorithms();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100540 ERR_load_crypto_strings();
Damien Millercb5e44a2000-09-29 12:12:36 +1100541
Damien Miller95def091999-11-25 00:26:21 +1100542 /* Initialize the command to execute on remote host. */
543 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000544
Damien Miller5428f641999-11-25 11:54:57 +1100545 /*
546 * Save the command to execute on the remote host in a buffer. There
547 * is no limit on the length of the command, except by the maximum
548 * packet size. Also sets the tty flag if there is no command.
549 */
Damien Millerf4614452001-07-14 12:18:10 +1000550 if (!ac) {
Damien Miller95def091999-11-25 00:26:21 +1100551 /* No command specified - execute shell on a tty. */
552 tty_flag = 1;
Damien Miller832562e2001-01-30 09:30:01 +1100553 if (subsystem_flag) {
Damien Millerf4614452001-07-14 12:18:10 +1000554 fprintf(stderr,
555 "You must specify a subsystem to invoke.\n");
Damien Miller832562e2001-01-30 09:30:01 +1100556 usage();
557 }
Damien Miller95def091999-11-25 00:26:21 +1100558 } else {
Damien Millerf4614452001-07-14 12:18:10 +1000559 /* A command has been specified. Store it into the buffer. */
560 for (i = 0; i < ac; i++) {
561 if (i)
Damien Miller95def091999-11-25 00:26:21 +1100562 buffer_append(&command, " ", 1);
563 buffer_append(&command, av[i], strlen(av[i]));
564 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000565 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000566
Damien Miller95def091999-11-25 00:26:21 +1100567 /* Cannot fork to background if no command. */
Damien Millercaf6dd62000-08-29 11:33:50 +1100568 if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
Damien Miller95def091999-11-25 00:26:21 +1100569 fatal("Cannot fork into background without a command to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000570
Damien Miller95def091999-11-25 00:26:21 +1100571 /* Allocate a tty by default if no command specified. */
572 if (buffer_len(&command) == 0)
573 tty_flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000574
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000575 /* Force no tty */
Ben Lindstromc72745a2000-12-02 19:03:54 +0000576 if (no_tty_flag)
577 tty_flag = 0;
Damien Miller95def091999-11-25 00:26:21 +1100578 /* Do not allocate a tty if stdin is not a tty. */
Damien Millerddee5752005-05-26 12:05:05 +1000579 if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100580 if (tty_flag)
Damien Miller996acd22003-04-09 20:59:48 +1000581 logit("Pseudo-terminal will not be allocated because stdin is not a terminal.");
Damien Miller95def091999-11-25 00:26:21 +1100582 tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000583 }
Damien Miller1383bd82000-04-06 12:32:37 +1000584
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000585 /*
586 * Initialize "log" output. Since we are the client all output
587 * actually goes to stderr.
588 */
Ben Lindstrom2b646522001-04-12 16:16:57 +0000589 log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
590 SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000591
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000592 /*
593 * Read per-user configuration file. Ignore the system wide config
594 * file if the user specifies a config file on the command line.
595 */
596 if (config != NULL) {
Damien Miller914420f2004-04-20 20:14:07 +1000597 if (!read_config_file(config, host, &options, 0))
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000598 fatal("Can't open user config file %.100s: "
599 "%.100s", config, strerror(errno));
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000600 } else {
601 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
602 _PATH_SSH_USER_CONFFILE);
Damien Miller57a44762004-04-20 20:11:57 +1000603 (void)read_config_file(buf, host, &options, 1);
Ben Lindstrom83f07d12001-10-03 17:22:29 +0000604
605 /* Read systemwide configuration file after use config. */
Darren Tuckerfc959702004-07-17 16:12:08 +1000606 (void)read_config_file(_PATH_HOST_CONFIG_FILE, host,
Damien Miller57a44762004-04-20 20:11:57 +1000607 &options, 0);
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000608 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000609
Damien Miller95def091999-11-25 00:26:21 +1100610 /* Fill configuration defaults. */
611 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000612
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000613 channel_set_af(options.address_family);
614
Damien Miller95def091999-11-25 00:26:21 +1100615 /* reinit */
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000616 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000617
Damien Miller60bc5172001-03-19 09:38:15 +1100618 seed_rng();
619
Damien Miller95def091999-11-25 00:26:21 +1100620 if (options.user == NULL)
621 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000622
Damien Miller95def091999-11-25 00:26:21 +1100623 if (options.hostname != NULL)
624 host = options.hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000625
Darren Tucker3f521e22003-07-03 16:20:42 +1000626 /* force lowercase for hostkey matching */
627 if (options.host_key_alias != NULL) {
628 for (p = options.host_key_alias; *p; p++)
629 if (isupper(*p))
630 *p = tolower(*p);
631 }
632
Damien Miller7c71cc72005-06-26 08:56:31 +1000633 /* Get default port if port has not been set. */
634 if (options.port == 0) {
635 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
636 options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
637 }
638
Damien Miller9f1e33a2003-02-24 11:57:32 +1100639 if (options.proxy_command != NULL &&
640 strcmp(options.proxy_command, "none") == 0)
641 options.proxy_command = NULL;
Damien Miller8f74c8f2005-06-26 08:56:03 +1000642 if (options.control_path != NULL &&
643 strcmp(options.control_path, "none") == 0)
644 options.control_path = NULL;
Damien Miller9f1e33a2003-02-24 11:57:32 +1100645
Damien Miller0e220db2004-06-15 10:34:08 +1000646 if (options.control_path != NULL) {
Damien Miller3ec54c72006-03-15 11:30:13 +1100647 char me[NI_MAXHOST];
648
649 if (gethostname(me, sizeof(me)) == -1)
650 fatal("gethostname: %s", strerror(errno));
Damien Miller6476cad2005-06-16 13:18:34 +1000651 snprintf(buf, sizeof(buf), "%d", options.port);
652 cp = tilde_expand_filename(options.control_path,
653 original_real_uid);
654 options.control_path = percent_expand(cp, "p", buf, "h", host,
Damien Miller3ec54c72006-03-15 11:30:13 +1100655 "r", options.user, "l", me, (char *)NULL);
Damien Miller6476cad2005-06-16 13:18:34 +1000656 xfree(cp);
Damien Miller0e220db2004-06-15 10:34:08 +1000657 }
Darren Tucker0814d312005-06-01 23:08:51 +1000658 if (mux_command != 0 && options.control_path == NULL)
659 fatal("No ControlPath specified for \"-O\" command");
Damien Millerd14b1e72005-06-16 13:19:41 +1000660 if (options.control_path != NULL)
Damien Millerdadfd4d2005-05-26 12:07:13 +1000661 control_client(options.control_path);
Damien Miller0e220db2004-06-15 10:34:08 +1000662
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000663 /* Open a connection to the remote host. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000664 if (ssh_connect(host, &hostaddr, options.port,
665 options.address_family, options.connection_attempts,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000666#ifdef HAVE_CYGWIN
667 options.use_privileged_port,
668#else
Ben Lindstromf9c48842002-06-11 16:37:51 +0000669 original_effective_uid == 0 && options.use_privileged_port,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000670#endif
Ben Lindstromda394ca2002-06-12 16:11:12 +0000671 options.proxy_command) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100672 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000673
Damien Miller5428f641999-11-25 11:54:57 +1100674 /*
675 * If we successfully made the connection, load the host private key
676 * in case we will need it later for combined rsa-rhosts
677 * authentication. This must be done before releasing extra
678 * privileges, because the file is only readable by root.
Ben Lindstrom9e5bb572002-06-06 19:58:27 +0000679 * If we cannot access the private keys, load the public keys
680 * instead and try to execute the ssh-keysign helper instead.
Damien Miller5428f641999-11-25 11:54:57 +1100681 */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000682 sensitive_data.nkeys = 0;
683 sensitive_data.keys = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000684 sensitive_data.external_keysign = 0;
Ben Lindstrom24157572002-06-12 16:09:39 +0000685 if (options.rhosts_rsa_authentication ||
686 options.hostbased_authentication) {
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000687 sensitive_data.nkeys = 3;
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000688 sensitive_data.keys = xmalloc(sensitive_data.nkeys *
689 sizeof(Key));
Ben Lindstromf9c48842002-06-11 16:37:51 +0000690
691 PRIV_START;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000692 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
Ben Lindstromd0fca422001-03-26 13:44:06 +0000693 _PATH_HOST_KEY_FILE, "", NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000694 sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
695 _PATH_HOST_DSA_KEY_FILE, "", NULL);
696 sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
697 _PATH_HOST_RSA_KEY_FILE, "", NULL);
Ben Lindstromf9c48842002-06-11 16:37:51 +0000698 PRIV_END;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000699
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +0000700 if (options.hostbased_authentication == 1 &&
701 sensitive_data.keys[0] == NULL &&
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000702 sensitive_data.keys[1] == NULL &&
703 sensitive_data.keys[2] == NULL) {
704 sensitive_data.keys[1] = key_load_public(
705 _PATH_HOST_DSA_KEY_FILE, NULL);
706 sensitive_data.keys[2] = key_load_public(
707 _PATH_HOST_RSA_KEY_FILE, NULL);
708 sensitive_data.external_keysign = 1;
709 }
Damien Miller95def091999-11-25 00:26:21 +1100710 }
Damien Miller5428f641999-11-25 11:54:57 +1100711 /*
712 * Get rid of any extra privileges that we may have. We will no
713 * longer need them. Also, extra privileges could make it very hard
714 * to read identity files and other non-world-readable files from the
715 * user's home directory if it happens to be on a NFS volume where
716 * root is mapped to nobody.
717 */
Darren Tucker25f60a72004-08-15 17:23:34 +1000718 if (original_effective_uid == 0) {
719 PRIV_START;
720 permanently_set_uid(pw);
721 }
Damien Miller95def091999-11-25 00:26:21 +1100722
Damien Miller5428f641999-11-25 11:54:57 +1100723 /*
724 * Now that we are back to our own permissions, create ~/.ssh
Damien Miller788f2122005-11-05 15:14:59 +1100725 * directory if it doesn't already exist.
Damien Miller5428f641999-11-25 11:54:57 +1100726 */
Ben Lindstrom930b14a2001-08-15 23:19:21 +0000727 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 +1100728 if (stat(buf, &st) < 0)
Damien Millerbe484b52000-07-15 14:14:16 +1000729 if (mkdir(buf, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100730 error("Could not create directory '%.200s'.", buf);
731
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000732 /* load options.identity_files */
733 load_public_identity_files();
734
735 /* Expand ~ in known host file names. */
736 /* XXX mem-leaks: */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100737 options.system_hostfile =
738 tilde_expand_filename(options.system_hostfile, original_real_uid);
739 options.user_hostfile =
740 tilde_expand_filename(options.user_hostfile, original_real_uid);
741 options.system_hostfile2 =
742 tilde_expand_filename(options.system_hostfile2, original_real_uid);
743 options.user_hostfile2 =
744 tilde_expand_filename(options.user_hostfile2, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100745
Damien Miller07cd5892001-11-12 10:52:03 +1100746 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
747
Damien Miller95def091999-11-25 00:26:21 +1100748 /* Log into the remote system. This never returns if the login fails. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000749 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, pw);
Damien Miller95def091999-11-25 00:26:21 +1100750
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000751 /* We no longer need the private host keys. Clear them now. */
752 if (sensitive_data.nkeys != 0) {
753 for (i = 0; i < sensitive_data.nkeys; i++) {
754 if (sensitive_data.keys[i] != NULL) {
755 /* Destroys contents safely */
756 debug3("clear hostkey %d", i);
757 key_free(sensitive_data.keys[i]);
758 sensitive_data.keys[i] = NULL;
759 }
760 }
761 xfree(sensitive_data.keys);
762 }
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +0000763 for (i = 0; i < options.num_identity_files; i++) {
764 if (options.identity_files[i]) {
765 xfree(options.identity_files[i]);
766 options.identity_files[i] = NULL;
767 }
768 if (options.identity_keys[i]) {
769 key_free(options.identity_keys[i]);
770 options.identity_keys[i] = NULL;
771 }
772 }
Damien Miller95def091999-11-25 00:26:21 +1100773
Damien Miller1383bd82000-04-06 12:32:37 +1000774 exit_status = compat20 ? ssh_session2() : ssh_session();
775 packet_close();
Damien Miller8c4e18a2002-09-19 12:05:02 +1000776
Damien Miller0e220db2004-06-15 10:34:08 +1000777 if (options.control_path != NULL && control_fd != -1)
778 unlink(options.control_path);
779
Damien Miller8c4e18a2002-09-19 12:05:02 +1000780 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100781 * Send SIGHUP to proxy command if used. We don't wait() in
Damien Miller8c4e18a2002-09-19 12:05:02 +1000782 * case it hangs and instead rely on init to reap the child
783 */
784 if (proxy_command_pid > 1)
785 kill(proxy_command_pid, SIGHUP);
786
Damien Miller1383bd82000-04-06 12:32:37 +1000787 return exit_status;
788}
789
Ben Lindstrombba81212001-06-25 05:01:22 +0000790static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100791ssh_init_forwarding(void)
792{
Kevin Steves12057502001-02-05 14:54:34 +0000793 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100794 int i;
Kevin Steves12057502001-02-05 14:54:34 +0000795
Damien Miller0bc1bd82000-11-13 22:57:25 +1100796 /* Initiate local TCP/IP port forwardings. */
797 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100798 debug("Local connections to %.200s:%d forwarded to remote "
799 "address %.200s:%d",
Darren Tucker47eede72005-03-14 23:08:12 +1100800 (options.local_forwards[i].listen_host == NULL) ?
801 (options.gateway_ports ? "*" : "LOCALHOST") :
Damien Millerf91ee4c2005-03-01 21:24:33 +1100802 options.local_forwards[i].listen_host,
803 options.local_forwards[i].listen_port,
804 options.local_forwards[i].connect_host,
805 options.local_forwards[i].connect_port);
Damien Millerb16461c2002-01-22 23:29:22 +1100806 success += channel_setup_local_fwd_listener(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100807 options.local_forwards[i].listen_host,
808 options.local_forwards[i].listen_port,
809 options.local_forwards[i].connect_host,
810 options.local_forwards[i].connect_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100811 options.gateway_ports);
812 }
Kevin Steves12057502001-02-05 14:54:34 +0000813 if (i > 0 && success == 0)
814 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100815
816 /* Initiate remote TCP/IP port forwardings. */
817 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100818 debug("Remote connections from %.200s:%d forwarded to "
819 "local address %.200s:%d",
Damien Miller46d38de2005-07-17 17:02:09 +1000820 (options.remote_forwards[i].listen_host == NULL) ?
Damien Milleraa3bb102005-11-05 15:12:59 +1100821 "LOCALHOST" : options.remote_forwards[i].listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100822 options.remote_forwards[i].listen_port,
823 options.remote_forwards[i].connect_host,
824 options.remote_forwards[i].connect_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100825 channel_request_remote_forwarding(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100826 options.remote_forwards[i].listen_host,
827 options.remote_forwards[i].listen_port,
828 options.remote_forwards[i].connect_host,
829 options.remote_forwards[i].connect_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100830 }
831}
832
Ben Lindstrombba81212001-06-25 05:01:22 +0000833static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100834check_agent_present(void)
835{
836 if (options.forward_agent) {
Damien Miller788f2122005-11-05 15:14:59 +1100837 /* Clear agent forwarding if we don't have an agent. */
Damien Miller789e95d2002-09-12 09:52:46 +1000838 if (!ssh_agent_present())
Damien Miller0bc1bd82000-11-13 22:57:25 +1100839 options.forward_agent = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100840 }
841}
842
Ben Lindstrombba81212001-06-25 05:01:22 +0000843static int
Damien Miller1383bd82000-04-06 12:32:37 +1000844ssh_session(void)
845{
846 int type;
Damien Miller1383bd82000-04-06 12:32:37 +1000847 int interactive = 0;
848 int have_tty = 0;
849 struct winsize ws;
Damien Miller1383bd82000-04-06 12:32:37 +1000850 char *cp;
Damien Miller17e7ed02005-06-17 12:54:33 +1000851 const char *display;
Damien Miller1383bd82000-04-06 12:32:37 +1000852
Damien Miller95def091999-11-25 00:26:21 +1100853 /* Enable compression if requested. */
854 if (options.compression) {
855 debug("Requesting compression at level %d.", options.compression_level);
856
857 if (options.compression_level < 1 || options.compression_level > 9)
858 fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
859
860 /* Send the request. */
861 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
862 packet_put_int(options.compression_level);
863 packet_send();
864 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100865 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100866 if (type == SSH_SMSG_SUCCESS)
867 packet_start_compression(options.compression_level);
868 else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000869 logit("Warning: Remote host refused compression.");
Damien Miller95def091999-11-25 00:26:21 +1100870 else
871 packet_disconnect("Protocol error waiting for compression response.");
872 }
873 /* Allocate a pseudo tty if appropriate. */
874 if (tty_flag) {
875 debug("Requesting pty.");
876
877 /* Start the packet. */
878 packet_start(SSH_CMSG_REQUEST_PTY);
879
880 /* Store TERM in the packet. There is no limit on the
881 length of the string. */
882 cp = getenv("TERM");
883 if (!cp)
884 cp = "";
Ben Lindstrom664408d2001-06-09 01:42:01 +0000885 packet_put_cstring(cp);
Damien Miller95def091999-11-25 00:26:21 +1100886
887 /* Store window size in the packet. */
888 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
889 memset(&ws, 0, sizeof(ws));
890 packet_put_int(ws.ws_row);
891 packet_put_int(ws.ws_col);
892 packet_put_int(ws.ws_xpixel);
893 packet_put_int(ws.ws_ypixel);
894
895 /* Store tty modes in the packet. */
Ben Lindstromae8e2d32001-04-14 23:13:02 +0000896 tty_make_modes(fileno(stdin), NULL);
Damien Miller95def091999-11-25 00:26:21 +1100897
898 /* Send the packet, and wait for it to leave. */
899 packet_send();
900 packet_write_wait();
901
902 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100903 type = packet_read();
Damien Miller450a7a12000-03-26 13:04:51 +1000904 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100905 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +1000906 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000907 } else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000908 logit("Warning: Remote host failed or refused to allocate a pseudo tty.");
Damien Miller95def091999-11-25 00:26:21 +1100909 else
910 packet_disconnect("Protocol error waiting for pty request response.");
911 }
912 /* Request X11 forwarding if enabled and DISPLAY is set. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000913 display = getenv("DISPLAY");
914 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000915 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +1000916 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000917 client_x11_get_proto(display, options.xauth_location,
918 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +1000919 /* Request forwarding with authentication spoofing. */
Damien Miller95def091999-11-25 00:26:21 +1100920 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +1000921 x11_request_forwarding_with_spoofing(0, display, proto, data);
Damien Miller95def091999-11-25 00:26:21 +1100922
923 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100924 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100925 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100926 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +1000927 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller996acd22003-04-09 20:59:48 +1000928 logit("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +1000929 } else {
Damien Miller95def091999-11-25 00:26:21 +1100930 packet_disconnect("Protocol error waiting for X11 forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +1000931 }
Damien Miller95def091999-11-25 00:26:21 +1100932 }
933 /* Tell the packet module whether this is an interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000934 packet_set_interactive(interactive);
Damien Miller95def091999-11-25 00:26:21 +1100935
936 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100937 check_agent_present();
938
Damien Miller95def091999-11-25 00:26:21 +1100939 if (options.forward_agent) {
940 debug("Requesting authentication agent forwarding.");
941 auth_request_forwarding();
942
943 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100944 type = packet_read();
Damien Miller48b03fc2002-01-22 23:11:40 +1100945 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100946 if (type != SSH_SMSG_SUCCESS)
Damien Miller996acd22003-04-09 20:59:48 +1000947 logit("Warning: Remote host denied authentication agent forwarding.");
Damien Miller95def091999-11-25 00:26:21 +1100948 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000949
Damien Miller0bc1bd82000-11-13 22:57:25 +1100950 /* Initiate port forwardings. */
951 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000952
Damien Miller5428f641999-11-25 11:54:57 +1100953 /* If requested, let ssh continue in the background. */
Damien Miller4af51302000-04-16 11:18:38 +1000954 if (fork_after_authentication_flag)
Damien Miller5428f641999-11-25 11:54:57 +1100955 if (daemon(1, 1) < 0)
956 fatal("daemon() failed: %.200s", strerror(errno));
957
958 /*
959 * If a command was specified on the command line, execute the
960 * command now. Otherwise request the server to start a shell.
961 */
Damien Miller95def091999-11-25 00:26:21 +1100962 if (buffer_len(&command) > 0) {
963 int len = buffer_len(&command);
964 if (len > 900)
965 len = 900;
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100966 debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
Damien Miller95def091999-11-25 00:26:21 +1100967 packet_start(SSH_CMSG_EXEC_CMD);
968 packet_put_string(buffer_ptr(&command), buffer_len(&command));
969 packet_send();
970 packet_write_wait();
971 } else {
972 debug("Requesting shell.");
973 packet_start(SSH_CMSG_EXEC_SHELL);
974 packet_send();
975 packet_write_wait();
976 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000977
Damien Miller95def091999-11-25 00:26:21 +1100978 /* Enter the interactive session. */
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000979 return client_loop(have_tty, tty_flag ?
980 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
Damien Miller1383bd82000-04-06 12:32:37 +1000981}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000982
Ben Lindstrombba81212001-06-25 05:01:22 +0000983static void
Damien Miller0e220db2004-06-15 10:34:08 +1000984ssh_subsystem_reply(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000985{
986 int id, len;
987
988 id = packet_get_int();
989 len = buffer_len(&command);
Ben Lindstrom4040fe12001-03-05 06:52:57 +0000990 if (len > 900)
991 len = 900;
Damien Miller48b03fc2002-01-22 23:11:40 +1100992 packet_check_eom();
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000993 if (type == SSH2_MSG_CHANNEL_FAILURE)
994 fatal("Request for subsystem '%.*s' failed on channel %d",
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100995 len, (u_char *)buffer_ptr(&command), id);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000996}
997
Damien Miller2797f7f2002-04-23 21:09:44 +1000998void
Damien Miller509b0102003-12-17 16:33:10 +1100999client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt)
Damien Miller2797f7f2002-04-23 21:09:44 +10001000{
1001 int i;
1002
1003 i = client_global_request_id++;
Damien Miller509b0102003-12-17 16:33:10 +11001004 if (i >= options.num_remote_forwards)
Damien Miller2797f7f2002-04-23 21:09:44 +10001005 return;
Damien Miller2797f7f2002-04-23 21:09:44 +10001006 debug("remote forward %s for: listen %d, connect %s:%d",
1007 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
Damien Millerf91ee4c2005-03-01 21:24:33 +11001008 options.remote_forwards[i].listen_port,
1009 options.remote_forwards[i].connect_host,
1010 options.remote_forwards[i].connect_port);
Damien Miller2797f7f2002-04-23 21:09:44 +10001011 if (type == SSH2_MSG_REQUEST_FAILURE)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001012 logit("Warning: remote port forwarding failed for listen "
1013 "port %d", options.remote_forwards[i].listen_port);
Damien Miller2797f7f2002-04-23 21:09:44 +10001014}
1015
Damien Miller0e220db2004-06-15 10:34:08 +10001016static void
1017ssh_control_listener(void)
1018{
1019 struct sockaddr_un addr;
1020 mode_t old_umask;
Damien Miller07b6ff12004-06-15 11:14:45 +10001021 int addr_len;
1022
Damien Millerd14b1e72005-06-16 13:19:41 +10001023 if (options.control_path == NULL ||
1024 options.control_master == SSHCTL_MASTER_NO)
Damien Miller0e220db2004-06-15 10:34:08 +10001025 return;
1026
Damien Millerd14b1e72005-06-16 13:19:41 +10001027 debug("setting up multiplex master socket");
1028
Damien Miller0e220db2004-06-15 10:34:08 +10001029 memset(&addr, '\0', sizeof(addr));
1030 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001031 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001032 strlen(options.control_path) + 1;
1033
1034 if (strlcpy(addr.sun_path, options.control_path,
1035 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1036 fatal("ControlPath too long");
1037
1038 if ((control_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001039 fatal("%s socket(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001040
1041 old_umask = umask(0177);
Damien Miller07b6ff12004-06-15 11:14:45 +10001042 if (bind(control_fd, (struct sockaddr*)&addr, addr_len) == -1) {
Damien Miller0e220db2004-06-15 10:34:08 +10001043 control_fd = -1;
Damien Miller4f10e252005-05-04 15:33:09 +10001044 if (errno == EINVAL || errno == EADDRINUSE)
Damien Miller0e220db2004-06-15 10:34:08 +10001045 fatal("ControlSocket %s already exists",
1046 options.control_path);
1047 else
Damien Miller15d72a02005-11-05 15:07:33 +11001048 fatal("%s bind(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001049 }
1050 umask(old_umask);
1051
1052 if (listen(control_fd, 64) == -1)
Damien Miller15d72a02005-11-05 15:07:33 +11001053 fatal("%s listen(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001054
1055 set_nonblock(control_fd);
1056}
1057
Ben Lindstromf558cf62001-09-20 23:13:49 +00001058/* request pty/x11/agent/tcpfwd/shell for channel */
Ben Lindstrombba81212001-06-25 05:01:22 +00001059static void
Ben Lindstromf558cf62001-09-20 23:13:49 +00001060ssh_session2_setup(int id, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +10001061{
Damien Miller3756dce2004-06-18 01:17:29 +10001062 extern char **environ;
Damien Miller17e7ed02005-06-17 12:54:33 +10001063 const char *display;
Damien Miller0e220db2004-06-15 10:34:08 +10001064 int interactive = tty_flag;
Damien Miller17e7ed02005-06-17 12:54:33 +10001065
Damien Miller46d38de2005-07-17 17:02:09 +10001066 display = getenv("DISPLAY");
Damien Miller17e7ed02005-06-17 12:54:33 +10001067 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001068 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001069 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001070 client_x11_get_proto(display, options.xauth_location,
1071 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001072 /* Request forwarding with authentication spoofing. */
1073 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +10001074 x11_request_forwarding_with_spoofing(id, display, proto, data);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001075 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001076 /* XXX wait for reply */
1077 }
1078
Damien Miller0bc1bd82000-11-13 22:57:25 +11001079 check_agent_present();
1080 if (options.forward_agent) {
1081 debug("Requesting authentication agent forwarding.");
1082 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1083 packet_send();
1084 }
1085
Damien Miller7b58e802005-12-13 19:33:19 +11001086 if (options.tun_open != SSH_TUNMODE_NO) {
Damien Millerd27b9472005-12-13 19:29:02 +11001087 Channel *c;
1088 int fd;
1089
1090 debug("Requesting tun.");
Damien Miller7b58e802005-12-13 19:33:19 +11001091 if ((fd = tun_open(options.tun_local,
1092 options.tun_open)) >= 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11001093 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1094 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1095 0, "tun", 1);
1096 c->datagram = 1;
Damien Miller598bbc22005-12-31 16:33:36 +11001097#if defined(SSH_TUN_FILTER)
1098 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1099 channel_register_filter(c->self, sys_tun_infilter,
1100 sys_tun_outfilter);
1101#endif
Damien Millerd27b9472005-12-13 19:29:02 +11001102 packet_start(SSH2_MSG_CHANNEL_OPEN);
1103 packet_put_cstring("tun@openssh.com");
1104 packet_put_int(c->self);
1105 packet_put_int(c->local_window_max);
1106 packet_put_int(c->local_maxpacket);
Damien Miller7b58e802005-12-13 19:33:19 +11001107 packet_put_int(options.tun_open);
Damien Millerd27b9472005-12-13 19:29:02 +11001108 packet_put_int(options.tun_remote);
1109 packet_send();
1110 }
1111 }
1112
Damien Miller0e220db2004-06-15 10:34:08 +10001113 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
Damien Miller3756dce2004-06-18 01:17:29 +10001114 NULL, fileno(stdin), &command, environ, &ssh_subsystem_reply);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001115
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001116 packet_set_interactive(interactive);
Damien Miller1383bd82000-04-06 12:32:37 +10001117}
1118
Ben Lindstromf558cf62001-09-20 23:13:49 +00001119/* open new channel for a session */
Ben Lindstrombba81212001-06-25 05:01:22 +00001120static int
Ben Lindstromf558cf62001-09-20 23:13:49 +00001121ssh_session2_open(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001122{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001123 Channel *c;
1124 int window, packetmax, in, out, err;
Damien Millerad833b32000-08-23 10:46:23 +10001125
Damien Millercaf6dd62000-08-29 11:33:50 +11001126 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001127 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001128 } else {
1129 in = dup(STDIN_FILENO);
1130 }
1131 out = dup(STDOUT_FILENO);
1132 err = dup(STDERR_FILENO);
1133
1134 if (in < 0 || out < 0 || err < 0)
1135 fatal("dup() in/out/err failed");
1136
Damien Miller69b69aa2000-10-28 14:19:58 +11001137 /* enable nonblocking unless tty */
1138 if (!isatty(in))
1139 set_nonblock(in);
1140 if (!isatty(out))
1141 set_nonblock(out);
1142 if (!isatty(err))
1143 set_nonblock(err);
1144
Damien Millere4340be2000-09-16 13:29:08 +11001145 window = CHAN_SES_WINDOW_DEFAULT;
1146 packetmax = CHAN_SES_PACKET_DEFAULT;
Damien Miller19a59452002-02-19 15:20:57 +11001147 if (tty_flag) {
1148 window >>= 1;
1149 packetmax >>= 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001150 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001151 c = channel_new(
Damien Miller1383bd82000-04-06 12:32:37 +10001152 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001153 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001154 "client-session", /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001155
Ben Lindstromf558cf62001-09-20 23:13:49 +00001156 debug3("ssh_session2_open: channel_new: %d", c->self);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001157
Ben Lindstrom5ec26452001-06-09 00:18:51 +00001158 channel_send_open(c->self);
Ben Lindstromf558cf62001-09-20 23:13:49 +00001159 if (!no_shell_flag)
Damien Miller0e220db2004-06-15 10:34:08 +10001160 channel_register_confirm(c->self, ssh_session2_setup, NULL);
Damien Miller1383bd82000-04-06 12:32:37 +10001161
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001162 return c->self;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001163}
1164
Ben Lindstrombba81212001-06-25 05:01:22 +00001165static int
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001166ssh_session2(void)
1167{
Ben Lindstromf558cf62001-09-20 23:13:49 +00001168 int id = -1;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001169
1170 /* XXX should be pre-session */
1171 ssh_init_forwarding();
Damien Miller0e220db2004-06-15 10:34:08 +10001172 ssh_control_listener();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001173
Ben Lindstromf558cf62001-09-20 23:13:49 +00001174 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1175 id = ssh_session2_open();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001176
Damien Millerd27b9472005-12-13 19:29:02 +11001177 /* Execute a local command */
1178 if (options.local_command != NULL &&
1179 options.permit_local_command)
1180 ssh_local_cmd(options.local_command);
1181
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001182 /* If requested, let ssh continue in the background. */
1183 if (fork_after_authentication_flag)
1184 if (daemon(1, 1) < 0)
1185 fatal("daemon() failed: %.200s", strerror(errno));
1186
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001187 return client_loop(tty_flag, tty_flag ?
1188 options.escape_char : SSH_ESCAPECHAR_NONE, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001189}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001190
Ben Lindstrombba81212001-06-25 05:01:22 +00001191static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001192load_public_identity_files(void)
1193{
1194 char *filename;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001195 int i = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001196 Key *public;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001197#ifdef SMARTCARD
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001198 Key **keys;
1199
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001200 if (options.smartcard_device != NULL &&
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001201 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1202 (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL ) {
1203 int count = 0;
1204 for (i = 0; keys[i] != NULL; i++) {
1205 count++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001206 memmove(&options.identity_files[1], &options.identity_files[0],
1207 sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
1208 memmove(&options.identity_keys[1], &options.identity_keys[0],
1209 sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1210 options.num_identity_files++;
1211 options.identity_keys[0] = keys[i];
Damien Miller56a0bb02003-06-18 20:28:40 +10001212 options.identity_files[0] = sc_get_key_label(keys[i]);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001213 }
Ben Lindstrom4f054602002-03-26 03:23:00 +00001214 if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1215 options.num_identity_files = SSH_MAX_IDENTITY_FILES;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001216 i = count;
1217 xfree(keys);
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001218 }
Ben Lindstromffce1472001-08-06 21:57:31 +00001219#endif /* SMARTCARD */
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001220 for (; i < options.num_identity_files; i++) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001221 filename = tilde_expand_filename(options.identity_files[i],
1222 original_real_uid);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001223 public = key_load_public(filename, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001224 debug("identity file %s type %d", filename,
1225 public ? public->type : -1);
1226 xfree(options.identity_files[i]);
1227 options.identity_files[i] = filename;
1228 options.identity_keys[i] = public;
1229 }
1230}
Damien Miller0e220db2004-06-15 10:34:08 +10001231
1232static void
1233control_client_sighandler(int signo)
1234{
1235 control_client_terminate = signo;
1236}
1237
1238static void
1239control_client_sigrelay(int signo)
1240{
1241 if (control_server_pid > 1)
1242 kill(control_server_pid, signo);
1243}
1244
Darren Tucker365433f2004-06-22 12:29:23 +10001245static int
1246env_permitted(char *env)
1247{
1248 int i;
1249 char name[1024], *cp;
1250
1251 strlcpy(name, env, sizeof(name));
1252 if ((cp = strchr(name, '=')) == NULL)
1253 return (0);
1254
1255 *cp = '\0';
1256
1257 for (i = 0; i < options.num_send_env; i++)
1258 if (match_pattern(name, options.send_env[i]))
1259 return (1);
1260
1261 return (0);
1262}
1263
Damien Miller0e220db2004-06-15 10:34:08 +10001264static void
1265control_client(const char *path)
1266{
1267 struct sockaddr_un addr;
Darren Tucker39207a42004-11-05 20:19:51 +11001268 int i, r, fd, sock, exitval, num_env, addr_len;
Damien Miller0e220db2004-06-15 10:34:08 +10001269 Buffer m;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001270 char *term;
Damien Miller3756dce2004-06-18 01:17:29 +10001271 extern char **environ;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001272 u_int flags;
Darren Tuckerfc959702004-07-17 16:12:08 +10001273
Damien Millerd14b1e72005-06-16 13:19:41 +10001274 if (mux_command == 0)
1275 mux_command = SSHMUX_COMMAND_OPEN;
1276
1277 switch (options.control_master) {
1278 case SSHCTL_MASTER_AUTO:
1279 case SSHCTL_MASTER_AUTO_ASK:
1280 debug("auto-mux: Trying existing master");
1281 /* FALLTHROUGH */
1282 case SSHCTL_MASTER_NO:
1283 break;
1284 default:
1285 return;
1286 }
1287
Damien Miller0e220db2004-06-15 10:34:08 +10001288 memset(&addr, '\0', sizeof(addr));
1289 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001290 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001291 strlen(path) + 1;
1292
1293 if (strlcpy(addr.sun_path, path,
1294 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1295 fatal("ControlPath too long");
1296
1297 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1298 fatal("%s socket(): %s", __func__, strerror(errno));
1299
Damien Millerdadfd4d2005-05-26 12:07:13 +10001300 if (connect(sock, (struct sockaddr*)&addr, addr_len) == -1) {
Darren Tucker0814d312005-06-01 23:08:51 +10001301 if (mux_command != SSHMUX_COMMAND_OPEN) {
1302 fatal("Control socket connect(%.100s): %s", path,
1303 strerror(errno));
1304 }
Damien Miller538c9b72005-05-26 12:11:28 +10001305 if (errno == ENOENT)
1306 debug("Control socket \"%.100s\" does not exist", path);
1307 else {
1308 error("Control socket connect(%.100s): %s", path,
1309 strerror(errno));
1310 }
Damien Miller13390022005-07-06 09:44:19 +10001311 close(sock);
1312 return;
1313 }
Damien Miller46d38de2005-07-17 17:02:09 +10001314
Damien Miller13390022005-07-06 09:44:19 +10001315 if (stdin_null_flag) {
1316 if ((fd = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1317 fatal("open(/dev/null): %s", strerror(errno));
1318 if (dup2(fd, STDIN_FILENO) == -1)
1319 fatal("dup2: %s", strerror(errno));
1320 if (fd > STDERR_FILENO)
1321 close(fd);
1322 }
Damien Miller46d38de2005-07-17 17:02:09 +10001323
Damien Miller13390022005-07-06 09:44:19 +10001324 term = getenv("TERM");
Darren Tucker7ebfc102004-11-07 20:06:19 +11001325
1326 flags = 0;
1327 if (tty_flag)
1328 flags |= SSHMUX_FLAG_TTY;
1329 if (subsystem_flag)
1330 flags |= SSHMUX_FLAG_SUBSYS;
Damien Miller13390022005-07-06 09:44:19 +10001331 if (options.forward_x11)
1332 flags |= SSHMUX_FLAG_X11_FWD;
1333 if (options.forward_agent)
1334 flags |= SSHMUX_FLAG_AGENT_FWD;
Damien Miller0e220db2004-06-15 10:34:08 +10001335
Damien Miller0e220db2004-06-15 10:34:08 +10001336 buffer_init(&m);
1337
Darren Tucker7ebfc102004-11-07 20:06:19 +11001338 /* Send our command to server */
1339 buffer_put_int(&m, mux_command);
1340 buffer_put_int(&m, flags);
Damien Miller13390022005-07-06 09:44:19 +10001341 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001342 fatal("%s: msg_send", __func__);
1343 buffer_clear(&m);
1344
1345 /* Get authorisation status and PID of controlee */
Damien Miller0e220db2004-06-15 10:34:08 +10001346 if (ssh_msg_recv(sock, &m) == -1)
1347 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001348 if (buffer_get_char(&m) != SSHMUX_VER)
Damien Miller0e220db2004-06-15 10:34:08 +10001349 fatal("%s: wrong version", __func__);
Damien Miller23f07702004-06-18 01:19:03 +10001350 if (buffer_get_int(&m) != 1)
1351 fatal("Connection to master denied");
Damien Miller0e220db2004-06-15 10:34:08 +10001352 control_server_pid = buffer_get_int(&m);
1353
Damien Miller0e220db2004-06-15 10:34:08 +10001354 buffer_clear(&m);
Damien Miller0e220db2004-06-15 10:34:08 +10001355
Darren Tucker7ebfc102004-11-07 20:06:19 +11001356 switch (mux_command) {
1357 case SSHMUX_COMMAND_ALIVE_CHECK:
Darren Tucker47eede72005-03-14 23:08:12 +11001358 fprintf(stderr, "Master running (pid=%d)\r\n",
Darren Tucker7ebfc102004-11-07 20:06:19 +11001359 control_server_pid);
1360 exit(0);
1361 case SSHMUX_COMMAND_TERMINATE:
1362 fprintf(stderr, "Exit request sent.\r\n");
1363 exit(0);
1364 case SSHMUX_COMMAND_OPEN:
1365 /* continue below */
1366 break;
1367 default:
1368 fatal("silly mux_command %d", mux_command);
1369 }
1370
1371 /* SSHMUX_COMMAND_OPEN */
Damien Miller13390022005-07-06 09:44:19 +10001372 buffer_put_cstring(&m, term ? term : "");
Damien Miller0e220db2004-06-15 10:34:08 +10001373 buffer_append(&command, "\0", 1);
1374 buffer_put_cstring(&m, buffer_ptr(&command));
1375
Darren Tucker365433f2004-06-22 12:29:23 +10001376 if (options.num_send_env == 0 || environ == NULL) {
1377 buffer_put_int(&m, 0);
Darren Tuckerfc959702004-07-17 16:12:08 +10001378 } else {
Darren Tucker365433f2004-06-22 12:29:23 +10001379 /* Pass environment */
1380 num_env = 0;
1381 for (i = 0; environ[i] != NULL; i++)
1382 if (env_permitted(environ[i]))
1383 num_env++; /* Count */
Darren Tuckerfc959702004-07-17 16:12:08 +10001384
Darren Tucker365433f2004-06-22 12:29:23 +10001385 buffer_put_int(&m, num_env);
1386
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001387 for (i = 0; environ[i] != NULL && num_env >= 0; i++)
1388 if (env_permitted(environ[i])) {
1389 num_env--;
Darren Tucker365433f2004-06-22 12:29:23 +10001390 buffer_put_cstring(&m, environ[i]);
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001391 }
Darren Tucker365433f2004-06-22 12:29:23 +10001392 }
Damien Miller3756dce2004-06-18 01:17:29 +10001393
Damien Miller13390022005-07-06 09:44:19 +10001394 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Damien Miller0e220db2004-06-15 10:34:08 +10001395 fatal("%s: msg_send", __func__);
1396
1397 mm_send_fd(sock, STDIN_FILENO);
1398 mm_send_fd(sock, STDOUT_FILENO);
1399 mm_send_fd(sock, STDERR_FILENO);
1400
1401 /* Wait for reply, so master has a chance to gather ttymodes */
1402 buffer_clear(&m);
1403 if (ssh_msg_recv(sock, &m) == -1)
1404 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001405 if (buffer_get_char(&m) != SSHMUX_VER)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001406 fatal("%s: wrong version", __func__);
Damien Miller0e220db2004-06-15 10:34:08 +10001407 buffer_free(&m);
1408
Darren Tucker07336da2004-11-05 20:02:16 +11001409 signal(SIGHUP, control_client_sighandler);
Damien Miller0809e232004-06-18 22:20:57 +10001410 signal(SIGINT, control_client_sighandler);
1411 signal(SIGTERM, control_client_sighandler);
1412 signal(SIGWINCH, control_client_sigrelay);
1413
Damien Miller0e220db2004-06-15 10:34:08 +10001414 if (tty_flag)
1415 enter_raw_mode();
1416
1417 /* Stick around until the controlee closes the client_fd */
1418 exitval = 0;
1419 for (;!control_client_terminate;) {
1420 r = read(sock, &exitval, sizeof(exitval));
1421 if (r == 0) {
1422 debug2("Received EOF from master");
1423 break;
1424 }
1425 if (r > 0)
1426 debug2("Received exit status from master %d", exitval);
1427 if (r == -1 && errno != EINTR)
1428 fatal("%s: read %s", __func__, strerror(errno));
1429 }
1430
1431 if (control_client_terminate)
1432 debug2("Exiting on signal %d", control_client_terminate);
1433
1434 close(sock);
1435
1436 leave_raw_mode();
1437
1438 if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
1439 fprintf(stderr, "Connection to master closed.\r\n");
1440
1441 exit(exitval);
1442}