blob: 196da80c5fdf496f2454ee7df21190d5720c99f3 [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 Millereba71ba2000-04-29 23:57:08 +100054
55#include <openssl/evp.h>
Damien Miller0bc1bd82000-11-13 22:57:25 +110056#include <openssl/err.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000059#include "ssh1.h"
Damien Miller1383bd82000-04-06 12:32:37 +100060#include "ssh2.h"
61#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000062#include "cipher.h"
63#include "xmalloc.h"
64#include "packet.h"
65#include "buffer.h"
Damien Miller0e220db2004-06-15 10:34:08 +100066#include "bufaux.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000067#include "channels.h"
Damien Millereba71ba2000-04-29 23:57:08 +100068#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100069#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100070#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000071#include "pathnames.h"
Damien Miller0e220db2004-06-15 10:34:08 +100072#include "dispatch.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +000073#include "clientloop.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000074#include "log.h"
75#include "readconf.h"
76#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000077#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000078#include "kex.h"
79#include "mac.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +100080#include "sshpty.h"
Darren Tucker46bc0752004-05-02 22:11:30 +100081#include "match.h"
Damien Miller0e220db2004-06-15 10:34:08 +100082#include "msg.h"
83#include "monitor_fdpass.h"
Darren Tucker25f60a72004-08-15 17:23:34 +100084#include "uidswap.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
Ben Lindstromc5b68002001-07-04 04:52:03 +000086#ifdef SMARTCARD
Ben Lindstromc5b68002001-07-04 04:52:03 +000087#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000088#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +000089
Damien Miller3f905871999-11-15 17:10:57 +110090extern char *__progname;
Damien Miller3f905871999-11-15 17:10:57 +110091
Damien Miller95def091999-11-25 00:26:21 +110092/* Flag indicating whether debug mode is on. This can be set on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093int debug_flag = 0;
94
Damien Miller78928792000-04-12 20:17:38 +100095/* Flag indicating whether a tty should be allocated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096int tty_flag = 0;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +000097int no_tty_flag = 0;
98int force_tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099
Damien Miller1383bd82000-04-06 12:32:37 +1000100/* don't exec a shell */
101int no_shell_flag = 0;
Damien Miller1383bd82000-04-06 12:32:37 +1000102
Damien Miller5428f641999-11-25 11:54:57 +1100103/*
104 * Flag indicating that nothing should be read from stdin. This can be set
105 * on the command line.
106 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107int stdin_null_flag = 0;
108
Damien Miller5428f641999-11-25 11:54:57 +1100109/*
110 * Flag indicating that ssh should fork after authentication. This is useful
Ben Lindstromf666fec2002-06-06 19:51:58 +0000111 * so that the passphrase can be entered manually, and then ssh goes to the
Damien Miller5428f641999-11-25 11:54:57 +1100112 * background.
113 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114int fork_after_authentication_flag = 0;
115
Damien Miller5428f641999-11-25 11:54:57 +1100116/*
117 * General data structure for command line options and options configurable
118 * in configuration files. See readconf.h.
119 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120Options options;
121
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000122/* optional user configfile */
123char *config = NULL;
124
Damien Miller5428f641999-11-25 11:54:57 +1100125/*
126 * Name of the host we are connecting to. This is the name given on the
127 * command line, or the HostName specified for the user-supplied name in a
128 * configuration file.
129 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000130char *host;
131
132/* socket address the host resolves to */
Damien Miller34132e52000-01-14 15:45:46 +1100133struct sockaddr_storage hostaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000135/* Private host keys. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000136Sensitive sensitive_data;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137
138/* Original real UID. */
139uid_t original_real_uid;
Ben Lindstromf9c48842002-06-11 16:37:51 +0000140uid_t original_effective_uid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141
Damien Miller1383bd82000-04-06 12:32:37 +1000142/* command to be executed */
143Buffer command;
144
Damien Miller832562e2001-01-30 09:30:01 +1100145/* Should we execute a command or invoke a subsystem? */
146int subsystem_flag = 0;
147
Damien Miller2797f7f2002-04-23 21:09:44 +1000148/* # of replies received for global requests */
149static int client_global_request_id = 0;
150
Damien Miller8c4e18a2002-09-19 12:05:02 +1000151/* pid of proxycommand child process */
152pid_t proxy_command_pid = 0;
153
Damien Miller0e220db2004-06-15 10:34:08 +1000154/* fd to control socket */
155int control_fd = -1;
156
Darren Tucker7ebfc102004-11-07 20:06:19 +1100157/* Multiplexing control command */
Darren Tucker0814d312005-06-01 23:08:51 +1000158static u_int mux_command = 0;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100159
Damien Miller0e220db2004-06-15 10:34:08 +1000160/* Only used in control client mode */
161volatile sig_atomic_t control_client_terminate = 0;
162u_int control_server_pid = 0;
163
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164/* Prints a help message to the user. This function never returns. */
165
Ben Lindstrombba81212001-06-25 05:01:22 +0000166static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000167usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168{
Damien Miller50955102004-03-22 09:34:58 +1100169 fprintf(stderr,
Darren Tucker9c6bf322004-12-03 14:10:19 +1100170"usage: ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n"
Darren Tucker895d6982005-10-03 18:18:05 +1000171" [-D [bind_address:]port] [-e escape_char] [-F configfile]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100172" [-i identity_file] [-L [bind_address:]port:host:hostport]\n"
Damien Millerf91ee4c2005-03-01 21:24:33 +1100173" [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100174" [-R [bind_address:]port:host:hostport] [-S ctl_path]\n"
Damien Millerd27b9472005-12-13 19:29:02 +1100175" [-w tunnel:tunnel] [user@]hostname [command]\n"
Damien Miller50955102004-03-22 09:34:58 +1100176 );
Darren Tuckere9a9b712005-12-20 16:15:51 +1100177 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000178}
179
Ben Lindstrombba81212001-06-25 05:01:22 +0000180static int ssh_session(void);
181static int ssh_session2(void);
182static void load_public_identity_files(void);
Damien Miller0e220db2004-06-15 10:34:08 +1000183static void control_client(const char *path);
Damien Miller1383bd82000-04-06 12:32:37 +1000184
Damien Miller95def091999-11-25 00:26:21 +1100185/*
186 * Main program for the ssh client.
187 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000188int
189main(int ac, char **av)
190{
Ben Lindstrom24157572002-06-12 16:09:39 +0000191 int i, opt, exit_status;
Damien Miller9836cf82003-12-17 16:30:06 +1100192 char *p, *cp, *line, buf[256];
Damien Miller95def091999-11-25 00:26:21 +1100193 struct stat st;
Ben Lindstrom086cf212001-03-05 05:56:40 +0000194 struct passwd *pw;
Damien Miller1383bd82000-04-06 12:32:37 +1000195 int dummy;
Ben Lindstrom5ccf63a2001-09-24 20:00:10 +0000196 extern int optind, optreset;
Damien Miller4f8e6692001-07-14 13:22:53 +1000197 extern char *optarg;
Damien Miller9651fe62005-06-26 08:55:25 +1000198 struct servent *sp;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100199 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000200
Darren Tuckerce321d82005-10-03 18:11:24 +1000201 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
202 sanitise_stdfd();
203
Damien Miller59d3d5b2003-08-22 09:34:41 +1000204 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000205 init_rng();
206
Damien Miller5428f641999-11-25 11:54:57 +1100207 /*
208 * Save the original real uid. It will be needed later (uid-swapping
209 * may clobber the real uid).
210 */
Damien Miller95def091999-11-25 00:26:21 +1100211 original_real_uid = getuid();
212 original_effective_uid = geteuid();
Damien Millera8e06ce2003-11-21 23:48:55 +1100213
Damien Miller50b9a602002-09-04 16:50:06 +1000214 /*
215 * Use uid-swapping to give up root privileges for the duration of
216 * option processing. We will re-instantiate the rights when we are
217 * ready to create the privileged port, and will permanently drop
218 * them when the port has been created (actually, when the connection
219 * has been made, as we may need to create the port several times).
220 */
221 PRIV_END;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000222
Damien Miller05dd7952000-10-01 00:42:48 +1100223#ifdef HAVE_SETRLIMIT
Damien Miller95def091999-11-25 00:26:21 +1100224 /* If we are installed setuid root be careful to not drop core. */
225 if (original_real_uid != original_effective_uid) {
226 struct rlimit rlim;
227 rlim.rlim_cur = rlim.rlim_max = 0;
228 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
229 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000230 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100231#endif
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000232 /* Get user data. */
233 pw = getpwuid(original_real_uid);
234 if (!pw) {
Damien Miller996acd22003-04-09 20:59:48 +1000235 logit("You don't exist, go away!");
Darren Tuckere9a9b712005-12-20 16:15:51 +1100236 exit(255);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000237 }
238 /* Take a copy of the returned structure. */
239 pw = pwcopy(pw);
240
Damien Miller5428f641999-11-25 11:54:57 +1100241 /*
Damien Miller5428f641999-11-25 11:54:57 +1100242 * Set our umask to something reasonable, as some files are created
243 * with the default umask. This will make them world-readable but
244 * writable only by the owner, which is ok for all files for which we
245 * don't set the modes explicitly.
246 */
Damien Miller95def091999-11-25 00:26:21 +1100247 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248
Damien Miller95def091999-11-25 00:26:21 +1100249 /* Initialize option structure to indicate that no values have been set. */
250 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000251
Damien Miller95def091999-11-25 00:26:21 +1100252 /* Parse command-line arguments. */
253 host = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000254
Damien Millerf4614452001-07-14 12:18:10 +1000255again:
256 while ((opt = getopt(ac, av,
Damien Millerd27b9472005-12-13 19:29:02 +1100257 "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 +1100258 switch (opt) {
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000259 case '1':
260 options.protocol = SSH_PROTO_1;
261 break;
Damien Miller4af51302000-04-16 11:18:38 +1000262 case '2':
263 options.protocol = SSH_PROTO_2;
264 break;
Damien Miller34132e52000-01-14 15:45:46 +1100265 case '4':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000266 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100267 break;
Damien Miller34132e52000-01-14 15:45:46 +1100268 case '6':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000269 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100270 break;
Damien Miller95def091999-11-25 00:26:21 +1100271 case 'n':
272 stdin_null_flag = 1;
273 break;
Damien Miller95def091999-11-25 00:26:21 +1100274 case 'f':
275 fork_after_authentication_flag = 1;
276 stdin_null_flag = 1;
277 break;
Damien Miller95def091999-11-25 00:26:21 +1100278 case 'x':
279 options.forward_x11 = 0;
280 break;
Damien Miller95def091999-11-25 00:26:21 +1100281 case 'X':
282 options.forward_x11 = 1;
283 break;
Darren Tucker0a118da2003-10-15 15:54:32 +1000284 case 'Y':
285 options.forward_x11 = 1;
286 options.forward_x11_trusted = 1;
287 break;
Damien Miller95def091999-11-25 00:26:21 +1100288 case 'g':
289 options.gateway_ports = 1;
290 break;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100291 case 'O':
292 if (strcmp(optarg, "check") == 0)
293 mux_command = SSHMUX_COMMAND_ALIVE_CHECK;
294 else if (strcmp(optarg, "exit") == 0)
295 mux_command = SSHMUX_COMMAND_TERMINATE;
296 else
297 fatal("Invalid multiplex command.");
298 break;
Damien Miller147bba32002-09-04 16:46:06 +1000299 case 'P': /* deprecated */
Damien Miller95def091999-11-25 00:26:21 +1100300 options.use_privileged_port = 0;
301 break;
Damien Miller95def091999-11-25 00:26:21 +1100302 case 'a':
303 options.forward_agent = 0;
304 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000305 case 'A':
306 options.forward_agent = 1;
307 break;
Damien Miller95def091999-11-25 00:26:21 +1100308 case 'k':
Damien Millere0113cc2003-11-24 13:10:09 +1100309 options.gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100310 break;
Damien Miller95def091999-11-25 00:26:21 +1100311 case 'i':
312 if (stat(optarg, &st) < 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000313 fprintf(stderr, "Warning: Identity file %s "
Damien Miller3eb48b62005-03-01 21:15:46 +1100314 "not accessible: %s.\n", optarg,
315 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100316 break;
317 }
Damien Millerf4614452001-07-14 12:18:10 +1000318 if (options.num_identity_files >=
319 SSH_MAX_IDENTITY_FILES)
320 fatal("Too many identity files specified "
321 "(max %d)", SSH_MAX_IDENTITY_FILES);
322 options.identity_files[options.num_identity_files++] =
323 xstrdup(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100324 break;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000325 case 'I':
326#ifdef SMARTCARD
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000327 options.smartcard_device = xstrdup(optarg);
Ben Lindstrombcc18082001-08-06 21:59:25 +0000328#else
Ben Lindstromc5b68002001-07-04 04:52:03 +0000329 fprintf(stderr, "no support for smartcards.\n");
Ben Lindstrombcc18082001-08-06 21:59:25 +0000330#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000331 break;
Damien Miller95def091999-11-25 00:26:21 +1100332 case 't':
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000333 if (tty_flag)
334 force_tty_flag = 1;
Damien Miller95def091999-11-25 00:26:21 +1100335 tty_flag = 1;
336 break;
Damien Miller95def091999-11-25 00:26:21 +1100337 case 'v':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000338 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100339 debug_flag = 1;
340 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000341 } else {
342 if (options.log_level < SYSLOG_LEVEL_DEBUG3)
343 options.log_level++;
Damien Millere4340be2000-09-16 13:29:08 +1100344 break;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000345 }
Darren Tuckere9bf9842004-11-05 20:05:32 +1100346 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100347 case 'V':
Damien Miller0c889cd2004-03-22 09:36:00 +1100348 fprintf(stderr, "%s, %s\n",
Damien Miller2aa6d3c2004-09-12 16:53:04 +1000349 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
Damien Miller95def091999-11-25 00:26:21 +1100350 if (opt == 'V')
351 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100352 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100353 case 'w':
Damien Miller7b58e802005-12-13 19:33:19 +1100354 if (options.tun_open == -1)
355 options.tun_open = SSH_TUNMODE_DEFAULT;
Damien Millerd27b9472005-12-13 19:29:02 +1100356 options.tun_local = a2tun(optarg, &options.tun_remote);
Damien Miller7b58e802005-12-13 19:33:19 +1100357 if (options.tun_local == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100358 fprintf(stderr, "Bad tun device '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100359 exit(255);
Damien Millerd27b9472005-12-13 19:29:02 +1100360 }
361 break;
Damien Miller95def091999-11-25 00:26:21 +1100362 case 'q':
363 options.log_level = SYSLOG_LEVEL_QUIET;
364 break;
Damien Miller95def091999-11-25 00:26:21 +1100365 case 'e':
366 if (optarg[0] == '^' && optarg[2] == 0 &&
Damien Millerf4614452001-07-14 12:18:10 +1000367 (u_char) optarg[1] >= 64 &&
368 (u_char) optarg[1] < 128)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000369 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100370 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000371 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100372 else if (strcmp(optarg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000373 options.escape_char = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100374 else {
Damien Millerf4614452001-07-14 12:18:10 +1000375 fprintf(stderr, "Bad escape character '%s'.\n",
376 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100377 exit(255);
Damien Miller95def091999-11-25 00:26:21 +1100378 }
379 break;
Damien Miller95def091999-11-25 00:26:21 +1100380 case 'c':
Damien Millereba71ba2000-04-29 23:57:08 +1000381 if (ciphers_valid(optarg)) {
382 /* SSH2 only */
383 options.ciphers = xstrdup(optarg);
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000384 options.cipher = SSH_CIPHER_INVALID;
Damien Millereba71ba2000-04-29 23:57:08 +1000385 } else {
386 /* SSH1 only */
Damien Millere39cacc2000-11-29 12:18:44 +1100387 options.cipher = cipher_number(optarg);
388 if (options.cipher == -1) {
Damien Millerf4614452001-07-14 12:18:10 +1000389 fprintf(stderr,
390 "Unknown cipher type '%s'\n",
391 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100392 exit(255);
Damien Millereba71ba2000-04-29 23:57:08 +1000393 }
Damien Millerf4614452001-07-14 12:18:10 +1000394 if (options.cipher == SSH_CIPHER_3DES)
Damien Millere39cacc2000-11-29 12:18:44 +1100395 options.ciphers = "3des-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000396 else if (options.cipher == SSH_CIPHER_BLOWFISH)
Damien Millere39cacc2000-11-29 12:18:44 +1100397 options.ciphers = "blowfish-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000398 else
Damien Millere39cacc2000-11-29 12:18:44 +1100399 options.ciphers = (char *)-1;
Damien Miller95def091999-11-25 00:26:21 +1100400 }
401 break;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000402 case 'm':
403 if (mac_valid(optarg))
404 options.macs = xstrdup(optarg);
405 else {
Damien Millerf4614452001-07-14 12:18:10 +1000406 fprintf(stderr, "Unknown mac type '%s'\n",
407 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100408 exit(255);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000409 }
410 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000411 case 'M':
Damien Millerd14b1e72005-06-16 13:19:41 +1000412 if (options.control_master == SSHCTL_MASTER_YES)
413 options.control_master = SSHCTL_MASTER_ASK;
414 else
415 options.control_master = SSHCTL_MASTER_YES;
Damien Miller0e220db2004-06-15 10:34:08 +1000416 break;
Damien Miller95def091999-11-25 00:26:21 +1100417 case 'p':
Ben Lindstrom19066a12001-04-12 23:39:26 +0000418 options.port = a2port(optarg);
419 if (options.port == 0) {
Ben Lindstrom146edb92001-04-11 23:06:28 +0000420 fprintf(stderr, "Bad port '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100421 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000422 }
Damien Miller95def091999-11-25 00:26:21 +1100423 break;
Damien Miller95def091999-11-25 00:26:21 +1100424 case 'l':
425 options.user = optarg;
426 break;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000427
Damien Miller95def091999-11-25 00:26:21 +1100428 case 'L':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100429 if (parse_forward(&fwd, optarg))
430 add_local_forward(&options, &fwd);
431 else {
Damien Millerf4614452001-07-14 12:18:10 +1000432 fprintf(stderr,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100433 "Bad local forwarding specification '%s'\n",
Damien Millerf4614452001-07-14 12:18:10 +1000434 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100435 exit(255);
Ben Lindstrom1a174712001-09-12 17:56:15 +0000436 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100437 break;
438
439 case 'R':
440 if (parse_forward(&fwd, optarg)) {
441 add_remote_forward(&options, &fwd);
442 } else {
443 fprintf(stderr,
444 "Bad remote forwarding specification "
445 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100446 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100447 }
Damien Miller95def091999-11-25 00:26:21 +1100448 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000449
450 case 'D':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100451 cp = p = xstrdup(optarg);
452 memset(&fwd, '\0', sizeof(fwd));
453 fwd.connect_host = "socks";
454 if ((fwd.listen_host = hpdelim(&cp)) == NULL) {
455 fprintf(stderr, "Bad dynamic forwarding "
456 "specification '%.100s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100457 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100458 }
459 if (cp != NULL) {
460 fwd.listen_port = a2port(cp);
461 fwd.listen_host = cleanhostname(fwd.listen_host);
462 } else {
463 fwd.listen_port = a2port(fwd.listen_host);
Damien Millerbe1045d2005-08-12 22:10:56 +1000464 fwd.listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100465 }
466
467 if (fwd.listen_port == 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000468 fprintf(stderr, "Bad dynamic port '%s'\n",
469 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100470 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000471 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100472 add_local_forward(&options, &fwd);
473 xfree(p);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000474 break;
475
Damien Miller95def091999-11-25 00:26:21 +1100476 case 'C':
477 options.compression = 1;
478 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000479 case 'N':
480 no_shell_flag = 1;
481 no_tty_flag = 1;
482 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000483 case 'T':
484 no_tty_flag = 1;
485 break;
Damien Miller95def091999-11-25 00:26:21 +1100486 case 'o':
487 dummy = 1;
Damien Miller9836cf82003-12-17 16:30:06 +1100488 line = xstrdup(optarg);
Damien Millerf4614452001-07-14 12:18:10 +1000489 if (process_config_line(&options, host ? host : "",
Damien Miller9836cf82003-12-17 16:30:06 +1100490 line, "command-line", 0, &dummy) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100491 exit(255);
Damien Miller9836cf82003-12-17 16:30:06 +1100492 xfree(line);
Damien Miller95def091999-11-25 00:26:21 +1100493 break;
Damien Miller832562e2001-01-30 09:30:01 +1100494 case 's':
495 subsystem_flag = 1;
496 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000497 case 'S':
498 if (options.control_path != NULL)
499 free(options.control_path);
500 options.control_path = xstrdup(optarg);
Damien Miller0e220db2004-06-15 10:34:08 +1000501 break;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000502 case 'b':
503 options.bind_address = optarg;
504 break;
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000505 case 'F':
506 config = optarg;
507 break;
Damien Miller95def091999-11-25 00:26:21 +1100508 default:
509 usage();
510 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000511 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000512
Damien Millerf4614452001-07-14 12:18:10 +1000513 ac -= optind;
514 av += optind;
515
516 if (ac > 0 && !host && **av != '-') {
Ben Lindstromc276c122002-12-23 02:14:51 +0000517 if (strrchr(*av, '@')) {
Damien Millerf4614452001-07-14 12:18:10 +1000518 p = xstrdup(*av);
Ben Lindstromc276c122002-12-23 02:14:51 +0000519 cp = strrchr(p, '@');
Damien Millerf4614452001-07-14 12:18:10 +1000520 if (cp == NULL || cp == p)
521 usage();
522 options.user = p;
523 *cp = '\0';
524 host = ++cp;
525 } else
526 host = *av;
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000527 if (ac > 1) {
528 optind = optreset = 1;
Damien Millerf4614452001-07-14 12:18:10 +1000529 goto again;
530 }
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000531 ac--, av++;
Damien Millerf4614452001-07-14 12:18:10 +1000532 }
533
Damien Miller95def091999-11-25 00:26:21 +1100534 /* Check that we got a host name. */
535 if (!host)
536 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000537
Damien Millercb5e44a2000-09-29 12:12:36 +1100538 SSLeay_add_all_algorithms();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100539 ERR_load_crypto_strings();
Damien Millercb5e44a2000-09-29 12:12:36 +1100540
Damien Miller95def091999-11-25 00:26:21 +1100541 /* Initialize the command to execute on remote host. */
542 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000543
Damien Miller5428f641999-11-25 11:54:57 +1100544 /*
545 * Save the command to execute on the remote host in a buffer. There
546 * is no limit on the length of the command, except by the maximum
547 * packet size. Also sets the tty flag if there is no command.
548 */
Damien Millerf4614452001-07-14 12:18:10 +1000549 if (!ac) {
Damien Miller95def091999-11-25 00:26:21 +1100550 /* No command specified - execute shell on a tty. */
551 tty_flag = 1;
Damien Miller832562e2001-01-30 09:30:01 +1100552 if (subsystem_flag) {
Damien Millerf4614452001-07-14 12:18:10 +1000553 fprintf(stderr,
554 "You must specify a subsystem to invoke.\n");
Damien Miller832562e2001-01-30 09:30:01 +1100555 usage();
556 }
Damien Miller95def091999-11-25 00:26:21 +1100557 } else {
Damien Millerf4614452001-07-14 12:18:10 +1000558 /* A command has been specified. Store it into the buffer. */
559 for (i = 0; i < ac; i++) {
560 if (i)
Damien Miller95def091999-11-25 00:26:21 +1100561 buffer_append(&command, " ", 1);
562 buffer_append(&command, av[i], strlen(av[i]));
563 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000564 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000565
Damien Miller95def091999-11-25 00:26:21 +1100566 /* Cannot fork to background if no command. */
Damien Millercaf6dd62000-08-29 11:33:50 +1100567 if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
Damien Miller95def091999-11-25 00:26:21 +1100568 fatal("Cannot fork into background without a command to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000569
Damien Miller95def091999-11-25 00:26:21 +1100570 /* Allocate a tty by default if no command specified. */
571 if (buffer_len(&command) == 0)
572 tty_flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000573
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000574 /* Force no tty */
Ben Lindstromc72745a2000-12-02 19:03:54 +0000575 if (no_tty_flag)
576 tty_flag = 0;
Damien Miller95def091999-11-25 00:26:21 +1100577 /* Do not allocate a tty if stdin is not a tty. */
Damien Millerddee5752005-05-26 12:05:05 +1000578 if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100579 if (tty_flag)
Damien Miller996acd22003-04-09 20:59:48 +1000580 logit("Pseudo-terminal will not be allocated because stdin is not a terminal.");
Damien Miller95def091999-11-25 00:26:21 +1100581 tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000582 }
Damien Miller1383bd82000-04-06 12:32:37 +1000583
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000584 /*
585 * Initialize "log" output. Since we are the client all output
586 * actually goes to stderr.
587 */
Ben Lindstrom2b646522001-04-12 16:16:57 +0000588 log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
589 SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000590
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000591 /*
592 * Read per-user configuration file. Ignore the system wide config
593 * file if the user specifies a config file on the command line.
594 */
595 if (config != NULL) {
Damien Miller914420f2004-04-20 20:14:07 +1000596 if (!read_config_file(config, host, &options, 0))
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000597 fatal("Can't open user config file %.100s: "
598 "%.100s", config, strerror(errno));
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000599 } else {
600 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
601 _PATH_SSH_USER_CONFFILE);
Damien Miller57a44762004-04-20 20:11:57 +1000602 (void)read_config_file(buf, host, &options, 1);
Ben Lindstrom83f07d12001-10-03 17:22:29 +0000603
604 /* Read systemwide configuration file after use config. */
Darren Tuckerfc959702004-07-17 16:12:08 +1000605 (void)read_config_file(_PATH_HOST_CONFIG_FILE, host,
Damien Miller57a44762004-04-20 20:11:57 +1000606 &options, 0);
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000607 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000608
Damien Miller95def091999-11-25 00:26:21 +1100609 /* Fill configuration defaults. */
610 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000611
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000612 channel_set_af(options.address_family);
613
Damien Miller95def091999-11-25 00:26:21 +1100614 /* reinit */
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000615 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000616
Damien Miller60bc5172001-03-19 09:38:15 +1100617 seed_rng();
618
Damien Miller95def091999-11-25 00:26:21 +1100619 if (options.user == NULL)
620 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000621
Damien Miller95def091999-11-25 00:26:21 +1100622 if (options.hostname != NULL)
623 host = options.hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000624
Darren Tucker3f521e22003-07-03 16:20:42 +1000625 /* force lowercase for hostkey matching */
626 if (options.host_key_alias != NULL) {
627 for (p = options.host_key_alias; *p; p++)
628 if (isupper(*p))
629 *p = tolower(*p);
630 }
631
Damien Miller7c71cc72005-06-26 08:56:31 +1000632 /* Get default port if port has not been set. */
633 if (options.port == 0) {
634 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
635 options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
636 }
637
Damien Miller9f1e33a2003-02-24 11:57:32 +1100638 if (options.proxy_command != NULL &&
639 strcmp(options.proxy_command, "none") == 0)
640 options.proxy_command = NULL;
Damien Miller8f74c8f2005-06-26 08:56:03 +1000641 if (options.control_path != NULL &&
642 strcmp(options.control_path, "none") == 0)
643 options.control_path = NULL;
Damien Miller9f1e33a2003-02-24 11:57:32 +1100644
Damien Miller0e220db2004-06-15 10:34:08 +1000645 if (options.control_path != NULL) {
Damien Miller3ec54c72006-03-15 11:30:13 +1100646 char me[NI_MAXHOST];
647
648 if (gethostname(me, sizeof(me)) == -1)
649 fatal("gethostname: %s", strerror(errno));
Damien Miller6476cad2005-06-16 13:18:34 +1000650 snprintf(buf, sizeof(buf), "%d", options.port);
651 cp = tilde_expand_filename(options.control_path,
652 original_real_uid);
653 options.control_path = percent_expand(cp, "p", buf, "h", host,
Damien Miller3ec54c72006-03-15 11:30:13 +1100654 "r", options.user, "l", me, (char *)NULL);
Damien Miller6476cad2005-06-16 13:18:34 +1000655 xfree(cp);
Damien Miller0e220db2004-06-15 10:34:08 +1000656 }
Darren Tucker0814d312005-06-01 23:08:51 +1000657 if (mux_command != 0 && options.control_path == NULL)
658 fatal("No ControlPath specified for \"-O\" command");
Damien Millerd14b1e72005-06-16 13:19:41 +1000659 if (options.control_path != NULL)
Damien Millerdadfd4d2005-05-26 12:07:13 +1000660 control_client(options.control_path);
Damien Miller0e220db2004-06-15 10:34:08 +1000661
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000662 /* Open a connection to the remote host. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000663 if (ssh_connect(host, &hostaddr, options.port,
664 options.address_family, options.connection_attempts,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000665#ifdef HAVE_CYGWIN
666 options.use_privileged_port,
667#else
Ben Lindstromf9c48842002-06-11 16:37:51 +0000668 original_effective_uid == 0 && options.use_privileged_port,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000669#endif
Ben Lindstromda394ca2002-06-12 16:11:12 +0000670 options.proxy_command) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100671 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000672
Damien Miller5428f641999-11-25 11:54:57 +1100673 /*
674 * If we successfully made the connection, load the host private key
675 * in case we will need it later for combined rsa-rhosts
676 * authentication. This must be done before releasing extra
677 * privileges, because the file is only readable by root.
Ben Lindstrom9e5bb572002-06-06 19:58:27 +0000678 * If we cannot access the private keys, load the public keys
679 * instead and try to execute the ssh-keysign helper instead.
Damien Miller5428f641999-11-25 11:54:57 +1100680 */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000681 sensitive_data.nkeys = 0;
682 sensitive_data.keys = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000683 sensitive_data.external_keysign = 0;
Ben Lindstrom24157572002-06-12 16:09:39 +0000684 if (options.rhosts_rsa_authentication ||
685 options.hostbased_authentication) {
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000686 sensitive_data.nkeys = 3;
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000687 sensitive_data.keys = xmalloc(sensitive_data.nkeys *
688 sizeof(Key));
Ben Lindstromf9c48842002-06-11 16:37:51 +0000689
690 PRIV_START;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000691 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
Ben Lindstromd0fca422001-03-26 13:44:06 +0000692 _PATH_HOST_KEY_FILE, "", NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000693 sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
694 _PATH_HOST_DSA_KEY_FILE, "", NULL);
695 sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
696 _PATH_HOST_RSA_KEY_FILE, "", NULL);
Ben Lindstromf9c48842002-06-11 16:37:51 +0000697 PRIV_END;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000698
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +0000699 if (options.hostbased_authentication == 1 &&
700 sensitive_data.keys[0] == NULL &&
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000701 sensitive_data.keys[1] == NULL &&
702 sensitive_data.keys[2] == NULL) {
703 sensitive_data.keys[1] = key_load_public(
704 _PATH_HOST_DSA_KEY_FILE, NULL);
705 sensitive_data.keys[2] = key_load_public(
706 _PATH_HOST_RSA_KEY_FILE, NULL);
707 sensitive_data.external_keysign = 1;
708 }
Damien Miller95def091999-11-25 00:26:21 +1100709 }
Damien Miller5428f641999-11-25 11:54:57 +1100710 /*
711 * Get rid of any extra privileges that we may have. We will no
712 * longer need them. Also, extra privileges could make it very hard
713 * to read identity files and other non-world-readable files from the
714 * user's home directory if it happens to be on a NFS volume where
715 * root is mapped to nobody.
716 */
Darren Tucker25f60a72004-08-15 17:23:34 +1000717 if (original_effective_uid == 0) {
718 PRIV_START;
719 permanently_set_uid(pw);
720 }
Damien Miller95def091999-11-25 00:26:21 +1100721
Damien Miller5428f641999-11-25 11:54:57 +1100722 /*
723 * Now that we are back to our own permissions, create ~/.ssh
Damien Miller788f2122005-11-05 15:14:59 +1100724 * directory if it doesn't already exist.
Damien Miller5428f641999-11-25 11:54:57 +1100725 */
Ben Lindstrom930b14a2001-08-15 23:19:21 +0000726 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 +1100727 if (stat(buf, &st) < 0)
Damien Millerbe484b52000-07-15 14:14:16 +1000728 if (mkdir(buf, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100729 error("Could not create directory '%.200s'.", buf);
730
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000731 /* load options.identity_files */
732 load_public_identity_files();
733
734 /* Expand ~ in known host file names. */
735 /* XXX mem-leaks: */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100736 options.system_hostfile =
737 tilde_expand_filename(options.system_hostfile, original_real_uid);
738 options.user_hostfile =
739 tilde_expand_filename(options.user_hostfile, original_real_uid);
740 options.system_hostfile2 =
741 tilde_expand_filename(options.system_hostfile2, original_real_uid);
742 options.user_hostfile2 =
743 tilde_expand_filename(options.user_hostfile2, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100744
Damien Miller07cd5892001-11-12 10:52:03 +1100745 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
746
Damien Miller95def091999-11-25 00:26:21 +1100747 /* Log into the remote system. This never returns if the login fails. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000748 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, pw);
Damien Miller95def091999-11-25 00:26:21 +1100749
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000750 /* We no longer need the private host keys. Clear them now. */
751 if (sensitive_data.nkeys != 0) {
752 for (i = 0; i < sensitive_data.nkeys; i++) {
753 if (sensitive_data.keys[i] != NULL) {
754 /* Destroys contents safely */
755 debug3("clear hostkey %d", i);
756 key_free(sensitive_data.keys[i]);
757 sensitive_data.keys[i] = NULL;
758 }
759 }
760 xfree(sensitive_data.keys);
761 }
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +0000762 for (i = 0; i < options.num_identity_files; i++) {
763 if (options.identity_files[i]) {
764 xfree(options.identity_files[i]);
765 options.identity_files[i] = NULL;
766 }
767 if (options.identity_keys[i]) {
768 key_free(options.identity_keys[i]);
769 options.identity_keys[i] = NULL;
770 }
771 }
Damien Miller95def091999-11-25 00:26:21 +1100772
Damien Miller1383bd82000-04-06 12:32:37 +1000773 exit_status = compat20 ? ssh_session2() : ssh_session();
774 packet_close();
Damien Miller8c4e18a2002-09-19 12:05:02 +1000775
Damien Miller0e220db2004-06-15 10:34:08 +1000776 if (options.control_path != NULL && control_fd != -1)
777 unlink(options.control_path);
778
Damien Miller8c4e18a2002-09-19 12:05:02 +1000779 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100780 * Send SIGHUP to proxy command if used. We don't wait() in
Damien Miller8c4e18a2002-09-19 12:05:02 +1000781 * case it hangs and instead rely on init to reap the child
782 */
783 if (proxy_command_pid > 1)
784 kill(proxy_command_pid, SIGHUP);
785
Damien Miller1383bd82000-04-06 12:32:37 +1000786 return exit_status;
787}
788
Ben Lindstrombba81212001-06-25 05:01:22 +0000789static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100790ssh_init_forwarding(void)
791{
Kevin Steves12057502001-02-05 14:54:34 +0000792 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100793 int i;
Kevin Steves12057502001-02-05 14:54:34 +0000794
Damien Miller0bc1bd82000-11-13 22:57:25 +1100795 /* Initiate local TCP/IP port forwardings. */
796 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100797 debug("Local connections to %.200s:%d forwarded to remote "
798 "address %.200s:%d",
Darren Tucker47eede72005-03-14 23:08:12 +1100799 (options.local_forwards[i].listen_host == NULL) ?
800 (options.gateway_ports ? "*" : "LOCALHOST") :
Damien Millerf91ee4c2005-03-01 21:24:33 +1100801 options.local_forwards[i].listen_host,
802 options.local_forwards[i].listen_port,
803 options.local_forwards[i].connect_host,
804 options.local_forwards[i].connect_port);
Damien Millerb16461c2002-01-22 23:29:22 +1100805 success += channel_setup_local_fwd_listener(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100806 options.local_forwards[i].listen_host,
807 options.local_forwards[i].listen_port,
808 options.local_forwards[i].connect_host,
809 options.local_forwards[i].connect_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100810 options.gateway_ports);
811 }
Kevin Steves12057502001-02-05 14:54:34 +0000812 if (i > 0 && success == 0)
813 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100814
815 /* Initiate remote TCP/IP port forwardings. */
816 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100817 debug("Remote connections from %.200s:%d forwarded to "
818 "local address %.200s:%d",
Damien Miller46d38de2005-07-17 17:02:09 +1000819 (options.remote_forwards[i].listen_host == NULL) ?
Damien Milleraa3bb102005-11-05 15:12:59 +1100820 "LOCALHOST" : options.remote_forwards[i].listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100821 options.remote_forwards[i].listen_port,
822 options.remote_forwards[i].connect_host,
823 options.remote_forwards[i].connect_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100824 channel_request_remote_forwarding(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100825 options.remote_forwards[i].listen_host,
826 options.remote_forwards[i].listen_port,
827 options.remote_forwards[i].connect_host,
828 options.remote_forwards[i].connect_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100829 }
830}
831
Ben Lindstrombba81212001-06-25 05:01:22 +0000832static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100833check_agent_present(void)
834{
835 if (options.forward_agent) {
Damien Miller788f2122005-11-05 15:14:59 +1100836 /* Clear agent forwarding if we don't have an agent. */
Damien Miller789e95d2002-09-12 09:52:46 +1000837 if (!ssh_agent_present())
Damien Miller0bc1bd82000-11-13 22:57:25 +1100838 options.forward_agent = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100839 }
840}
841
Ben Lindstrombba81212001-06-25 05:01:22 +0000842static int
Damien Miller1383bd82000-04-06 12:32:37 +1000843ssh_session(void)
844{
845 int type;
Damien Miller1383bd82000-04-06 12:32:37 +1000846 int interactive = 0;
847 int have_tty = 0;
848 struct winsize ws;
Damien Miller1383bd82000-04-06 12:32:37 +1000849 char *cp;
Damien Miller17e7ed02005-06-17 12:54:33 +1000850 const char *display;
Damien Miller1383bd82000-04-06 12:32:37 +1000851
Damien Miller95def091999-11-25 00:26:21 +1100852 /* Enable compression if requested. */
853 if (options.compression) {
854 debug("Requesting compression at level %d.", options.compression_level);
855
856 if (options.compression_level < 1 || options.compression_level > 9)
857 fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
858
859 /* Send the request. */
860 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
861 packet_put_int(options.compression_level);
862 packet_send();
863 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100864 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100865 if (type == SSH_SMSG_SUCCESS)
866 packet_start_compression(options.compression_level);
867 else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000868 logit("Warning: Remote host refused compression.");
Damien Miller95def091999-11-25 00:26:21 +1100869 else
870 packet_disconnect("Protocol error waiting for compression response.");
871 }
872 /* Allocate a pseudo tty if appropriate. */
873 if (tty_flag) {
874 debug("Requesting pty.");
875
876 /* Start the packet. */
877 packet_start(SSH_CMSG_REQUEST_PTY);
878
879 /* Store TERM in the packet. There is no limit on the
880 length of the string. */
881 cp = getenv("TERM");
882 if (!cp)
883 cp = "";
Ben Lindstrom664408d2001-06-09 01:42:01 +0000884 packet_put_cstring(cp);
Damien Miller95def091999-11-25 00:26:21 +1100885
886 /* Store window size in the packet. */
887 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
888 memset(&ws, 0, sizeof(ws));
889 packet_put_int(ws.ws_row);
890 packet_put_int(ws.ws_col);
891 packet_put_int(ws.ws_xpixel);
892 packet_put_int(ws.ws_ypixel);
893
894 /* Store tty modes in the packet. */
Ben Lindstromae8e2d32001-04-14 23:13:02 +0000895 tty_make_modes(fileno(stdin), NULL);
Damien Miller95def091999-11-25 00:26:21 +1100896
897 /* Send the packet, and wait for it to leave. */
898 packet_send();
899 packet_write_wait();
900
901 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100902 type = packet_read();
Damien Miller450a7a12000-03-26 13:04:51 +1000903 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100904 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +1000905 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000906 } else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000907 logit("Warning: Remote host failed or refused to allocate a pseudo tty.");
Damien Miller95def091999-11-25 00:26:21 +1100908 else
909 packet_disconnect("Protocol error waiting for pty request response.");
910 }
911 /* Request X11 forwarding if enabled and DISPLAY is set. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000912 display = getenv("DISPLAY");
913 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000914 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +1000915 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000916 client_x11_get_proto(display, options.xauth_location,
917 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +1000918 /* Request forwarding with authentication spoofing. */
Damien Miller95def091999-11-25 00:26:21 +1100919 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +1000920 x11_request_forwarding_with_spoofing(0, display, proto, data);
Damien Miller95def091999-11-25 00:26:21 +1100921
922 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100923 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100924 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100925 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +1000926 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller996acd22003-04-09 20:59:48 +1000927 logit("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +1000928 } else {
Damien Miller95def091999-11-25 00:26:21 +1100929 packet_disconnect("Protocol error waiting for X11 forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +1000930 }
Damien Miller95def091999-11-25 00:26:21 +1100931 }
932 /* Tell the packet module whether this is an interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000933 packet_set_interactive(interactive);
Damien Miller95def091999-11-25 00:26:21 +1100934
935 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100936 check_agent_present();
937
Damien Miller95def091999-11-25 00:26:21 +1100938 if (options.forward_agent) {
939 debug("Requesting authentication agent forwarding.");
940 auth_request_forwarding();
941
942 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100943 type = packet_read();
Damien Miller48b03fc2002-01-22 23:11:40 +1100944 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100945 if (type != SSH_SMSG_SUCCESS)
Damien Miller996acd22003-04-09 20:59:48 +1000946 logit("Warning: Remote host denied authentication agent forwarding.");
Damien Miller95def091999-11-25 00:26:21 +1100947 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000948
Damien Miller0bc1bd82000-11-13 22:57:25 +1100949 /* Initiate port forwardings. */
950 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000951
Damien Miller5428f641999-11-25 11:54:57 +1100952 /* If requested, let ssh continue in the background. */
Damien Miller4af51302000-04-16 11:18:38 +1000953 if (fork_after_authentication_flag)
Damien Miller5428f641999-11-25 11:54:57 +1100954 if (daemon(1, 1) < 0)
955 fatal("daemon() failed: %.200s", strerror(errno));
956
957 /*
958 * If a command was specified on the command line, execute the
959 * command now. Otherwise request the server to start a shell.
960 */
Damien Miller95def091999-11-25 00:26:21 +1100961 if (buffer_len(&command) > 0) {
962 int len = buffer_len(&command);
963 if (len > 900)
964 len = 900;
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100965 debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
Damien Miller95def091999-11-25 00:26:21 +1100966 packet_start(SSH_CMSG_EXEC_CMD);
967 packet_put_string(buffer_ptr(&command), buffer_len(&command));
968 packet_send();
969 packet_write_wait();
970 } else {
971 debug("Requesting shell.");
972 packet_start(SSH_CMSG_EXEC_SHELL);
973 packet_send();
974 packet_write_wait();
975 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000976
Damien Miller95def091999-11-25 00:26:21 +1100977 /* Enter the interactive session. */
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000978 return client_loop(have_tty, tty_flag ?
979 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
Damien Miller1383bd82000-04-06 12:32:37 +1000980}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000981
Ben Lindstrombba81212001-06-25 05:01:22 +0000982static void
Damien Miller0e220db2004-06-15 10:34:08 +1000983ssh_subsystem_reply(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000984{
985 int id, len;
986
987 id = packet_get_int();
988 len = buffer_len(&command);
Ben Lindstrom4040fe12001-03-05 06:52:57 +0000989 if (len > 900)
990 len = 900;
Damien Miller48b03fc2002-01-22 23:11:40 +1100991 packet_check_eom();
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000992 if (type == SSH2_MSG_CHANNEL_FAILURE)
993 fatal("Request for subsystem '%.*s' failed on channel %d",
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100994 len, (u_char *)buffer_ptr(&command), id);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000995}
996
Damien Miller2797f7f2002-04-23 21:09:44 +1000997void
Damien Miller509b0102003-12-17 16:33:10 +1100998client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt)
Damien Miller2797f7f2002-04-23 21:09:44 +1000999{
1000 int i;
1001
1002 i = client_global_request_id++;
Damien Miller509b0102003-12-17 16:33:10 +11001003 if (i >= options.num_remote_forwards)
Damien Miller2797f7f2002-04-23 21:09:44 +10001004 return;
Damien Miller2797f7f2002-04-23 21:09:44 +10001005 debug("remote forward %s for: listen %d, connect %s:%d",
1006 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
Damien Millerf91ee4c2005-03-01 21:24:33 +11001007 options.remote_forwards[i].listen_port,
1008 options.remote_forwards[i].connect_host,
1009 options.remote_forwards[i].connect_port);
Damien Miller2797f7f2002-04-23 21:09:44 +10001010 if (type == SSH2_MSG_REQUEST_FAILURE)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001011 logit("Warning: remote port forwarding failed for listen "
1012 "port %d", options.remote_forwards[i].listen_port);
Damien Miller2797f7f2002-04-23 21:09:44 +10001013}
1014
Damien Miller0e220db2004-06-15 10:34:08 +10001015static void
1016ssh_control_listener(void)
1017{
1018 struct sockaddr_un addr;
1019 mode_t old_umask;
Damien Miller07b6ff12004-06-15 11:14:45 +10001020 int addr_len;
1021
Damien Millerd14b1e72005-06-16 13:19:41 +10001022 if (options.control_path == NULL ||
1023 options.control_master == SSHCTL_MASTER_NO)
Damien Miller0e220db2004-06-15 10:34:08 +10001024 return;
1025
Damien Millerd14b1e72005-06-16 13:19:41 +10001026 debug("setting up multiplex master socket");
1027
Damien Miller0e220db2004-06-15 10:34:08 +10001028 memset(&addr, '\0', sizeof(addr));
1029 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001030 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001031 strlen(options.control_path) + 1;
1032
1033 if (strlcpy(addr.sun_path, options.control_path,
1034 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1035 fatal("ControlPath too long");
1036
1037 if ((control_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001038 fatal("%s socket(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001039
1040 old_umask = umask(0177);
Damien Miller07b6ff12004-06-15 11:14:45 +10001041 if (bind(control_fd, (struct sockaddr*)&addr, addr_len) == -1) {
Damien Miller0e220db2004-06-15 10:34:08 +10001042 control_fd = -1;
Damien Miller4f10e252005-05-04 15:33:09 +10001043 if (errno == EINVAL || errno == EADDRINUSE)
Damien Miller0e220db2004-06-15 10:34:08 +10001044 fatal("ControlSocket %s already exists",
1045 options.control_path);
1046 else
Damien Miller15d72a02005-11-05 15:07:33 +11001047 fatal("%s bind(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001048 }
1049 umask(old_umask);
1050
1051 if (listen(control_fd, 64) == -1)
Damien Miller15d72a02005-11-05 15:07:33 +11001052 fatal("%s listen(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001053
1054 set_nonblock(control_fd);
1055}
1056
Ben Lindstromf558cf62001-09-20 23:13:49 +00001057/* request pty/x11/agent/tcpfwd/shell for channel */
Ben Lindstrombba81212001-06-25 05:01:22 +00001058static void
Ben Lindstromf558cf62001-09-20 23:13:49 +00001059ssh_session2_setup(int id, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +10001060{
Damien Miller3756dce2004-06-18 01:17:29 +10001061 extern char **environ;
Damien Miller17e7ed02005-06-17 12:54:33 +10001062 const char *display;
Damien Miller0e220db2004-06-15 10:34:08 +10001063 int interactive = tty_flag;
Damien Miller17e7ed02005-06-17 12:54:33 +10001064
Damien Miller46d38de2005-07-17 17:02:09 +10001065 display = getenv("DISPLAY");
Damien Miller17e7ed02005-06-17 12:54:33 +10001066 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001067 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001068 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001069 client_x11_get_proto(display, options.xauth_location,
1070 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001071 /* Request forwarding with authentication spoofing. */
1072 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +10001073 x11_request_forwarding_with_spoofing(id, display, proto, data);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001074 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001075 /* XXX wait for reply */
1076 }
1077
Damien Miller0bc1bd82000-11-13 22:57:25 +11001078 check_agent_present();
1079 if (options.forward_agent) {
1080 debug("Requesting authentication agent forwarding.");
1081 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1082 packet_send();
1083 }
1084
Damien Miller7b58e802005-12-13 19:33:19 +11001085 if (options.tun_open != SSH_TUNMODE_NO) {
Damien Millerd27b9472005-12-13 19:29:02 +11001086 Channel *c;
1087 int fd;
1088
1089 debug("Requesting tun.");
Damien Miller7b58e802005-12-13 19:33:19 +11001090 if ((fd = tun_open(options.tun_local,
1091 options.tun_open)) >= 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11001092 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1093 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1094 0, "tun", 1);
1095 c->datagram = 1;
Damien Miller598bbc22005-12-31 16:33:36 +11001096#if defined(SSH_TUN_FILTER)
1097 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1098 channel_register_filter(c->self, sys_tun_infilter,
1099 sys_tun_outfilter);
1100#endif
Damien Millerd27b9472005-12-13 19:29:02 +11001101 packet_start(SSH2_MSG_CHANNEL_OPEN);
1102 packet_put_cstring("tun@openssh.com");
1103 packet_put_int(c->self);
1104 packet_put_int(c->local_window_max);
1105 packet_put_int(c->local_maxpacket);
Damien Miller7b58e802005-12-13 19:33:19 +11001106 packet_put_int(options.tun_open);
Damien Millerd27b9472005-12-13 19:29:02 +11001107 packet_put_int(options.tun_remote);
1108 packet_send();
1109 }
1110 }
1111
Damien Miller0e220db2004-06-15 10:34:08 +10001112 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
Damien Miller3756dce2004-06-18 01:17:29 +10001113 NULL, fileno(stdin), &command, environ, &ssh_subsystem_reply);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001114
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001115 packet_set_interactive(interactive);
Damien Miller1383bd82000-04-06 12:32:37 +10001116}
1117
Ben Lindstromf558cf62001-09-20 23:13:49 +00001118/* open new channel for a session */
Ben Lindstrombba81212001-06-25 05:01:22 +00001119static int
Ben Lindstromf558cf62001-09-20 23:13:49 +00001120ssh_session2_open(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001121{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001122 Channel *c;
1123 int window, packetmax, in, out, err;
Damien Millerad833b32000-08-23 10:46:23 +10001124
Damien Millercaf6dd62000-08-29 11:33:50 +11001125 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001126 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001127 } else {
1128 in = dup(STDIN_FILENO);
1129 }
1130 out = dup(STDOUT_FILENO);
1131 err = dup(STDERR_FILENO);
1132
1133 if (in < 0 || out < 0 || err < 0)
1134 fatal("dup() in/out/err failed");
1135
Damien Miller69b69aa2000-10-28 14:19:58 +11001136 /* enable nonblocking unless tty */
1137 if (!isatty(in))
1138 set_nonblock(in);
1139 if (!isatty(out))
1140 set_nonblock(out);
1141 if (!isatty(err))
1142 set_nonblock(err);
1143
Damien Millere4340be2000-09-16 13:29:08 +11001144 window = CHAN_SES_WINDOW_DEFAULT;
1145 packetmax = CHAN_SES_PACKET_DEFAULT;
Damien Miller19a59452002-02-19 15:20:57 +11001146 if (tty_flag) {
1147 window >>= 1;
1148 packetmax >>= 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001149 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001150 c = channel_new(
Damien Miller1383bd82000-04-06 12:32:37 +10001151 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001152 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001153 "client-session", /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001154
Ben Lindstromf558cf62001-09-20 23:13:49 +00001155 debug3("ssh_session2_open: channel_new: %d", c->self);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001156
Ben Lindstrom5ec26452001-06-09 00:18:51 +00001157 channel_send_open(c->self);
Ben Lindstromf558cf62001-09-20 23:13:49 +00001158 if (!no_shell_flag)
Damien Miller0e220db2004-06-15 10:34:08 +10001159 channel_register_confirm(c->self, ssh_session2_setup, NULL);
Damien Miller1383bd82000-04-06 12:32:37 +10001160
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001161 return c->self;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001162}
1163
Ben Lindstrombba81212001-06-25 05:01:22 +00001164static int
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001165ssh_session2(void)
1166{
Ben Lindstromf558cf62001-09-20 23:13:49 +00001167 int id = -1;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001168
1169 /* XXX should be pre-session */
1170 ssh_init_forwarding();
Damien Miller0e220db2004-06-15 10:34:08 +10001171 ssh_control_listener();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001172
Ben Lindstromf558cf62001-09-20 23:13:49 +00001173 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1174 id = ssh_session2_open();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001175
Damien Millerd27b9472005-12-13 19:29:02 +11001176 /* Execute a local command */
1177 if (options.local_command != NULL &&
1178 options.permit_local_command)
1179 ssh_local_cmd(options.local_command);
1180
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001181 /* If requested, let ssh continue in the background. */
1182 if (fork_after_authentication_flag)
1183 if (daemon(1, 1) < 0)
1184 fatal("daemon() failed: %.200s", strerror(errno));
1185
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001186 return client_loop(tty_flag, tty_flag ?
1187 options.escape_char : SSH_ESCAPECHAR_NONE, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001188}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001189
Ben Lindstrombba81212001-06-25 05:01:22 +00001190static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001191load_public_identity_files(void)
1192{
1193 char *filename;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001194 int i = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001195 Key *public;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001196#ifdef SMARTCARD
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001197 Key **keys;
1198
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001199 if (options.smartcard_device != NULL &&
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001200 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1201 (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL ) {
1202 int count = 0;
1203 for (i = 0; keys[i] != NULL; i++) {
1204 count++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001205 memmove(&options.identity_files[1], &options.identity_files[0],
1206 sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
1207 memmove(&options.identity_keys[1], &options.identity_keys[0],
1208 sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1209 options.num_identity_files++;
1210 options.identity_keys[0] = keys[i];
Damien Miller56a0bb02003-06-18 20:28:40 +10001211 options.identity_files[0] = sc_get_key_label(keys[i]);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001212 }
Ben Lindstrom4f054602002-03-26 03:23:00 +00001213 if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1214 options.num_identity_files = SSH_MAX_IDENTITY_FILES;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001215 i = count;
1216 xfree(keys);
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001217 }
Ben Lindstromffce1472001-08-06 21:57:31 +00001218#endif /* SMARTCARD */
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001219 for (; i < options.num_identity_files; i++) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001220 filename = tilde_expand_filename(options.identity_files[i],
1221 original_real_uid);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001222 public = key_load_public(filename, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001223 debug("identity file %s type %d", filename,
1224 public ? public->type : -1);
1225 xfree(options.identity_files[i]);
1226 options.identity_files[i] = filename;
1227 options.identity_keys[i] = public;
1228 }
1229}
Damien Miller0e220db2004-06-15 10:34:08 +10001230
1231static void
1232control_client_sighandler(int signo)
1233{
1234 control_client_terminate = signo;
1235}
1236
1237static void
1238control_client_sigrelay(int signo)
1239{
1240 if (control_server_pid > 1)
1241 kill(control_server_pid, signo);
1242}
1243
Darren Tucker365433f2004-06-22 12:29:23 +10001244static int
1245env_permitted(char *env)
1246{
1247 int i;
1248 char name[1024], *cp;
1249
1250 strlcpy(name, env, sizeof(name));
1251 if ((cp = strchr(name, '=')) == NULL)
1252 return (0);
1253
1254 *cp = '\0';
1255
1256 for (i = 0; i < options.num_send_env; i++)
1257 if (match_pattern(name, options.send_env[i]))
1258 return (1);
1259
1260 return (0);
1261}
1262
Damien Miller0e220db2004-06-15 10:34:08 +10001263static void
1264control_client(const char *path)
1265{
1266 struct sockaddr_un addr;
Darren Tucker39207a42004-11-05 20:19:51 +11001267 int i, r, fd, sock, exitval, num_env, addr_len;
Damien Miller0e220db2004-06-15 10:34:08 +10001268 Buffer m;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001269 char *term;
Damien Miller3756dce2004-06-18 01:17:29 +10001270 extern char **environ;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001271 u_int flags;
Darren Tuckerfc959702004-07-17 16:12:08 +10001272
Damien Millerd14b1e72005-06-16 13:19:41 +10001273 if (mux_command == 0)
1274 mux_command = SSHMUX_COMMAND_OPEN;
1275
1276 switch (options.control_master) {
1277 case SSHCTL_MASTER_AUTO:
1278 case SSHCTL_MASTER_AUTO_ASK:
1279 debug("auto-mux: Trying existing master");
1280 /* FALLTHROUGH */
1281 case SSHCTL_MASTER_NO:
1282 break;
1283 default:
1284 return;
1285 }
1286
Damien Miller0e220db2004-06-15 10:34:08 +10001287 memset(&addr, '\0', sizeof(addr));
1288 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001289 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001290 strlen(path) + 1;
1291
1292 if (strlcpy(addr.sun_path, path,
1293 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1294 fatal("ControlPath too long");
1295
1296 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1297 fatal("%s socket(): %s", __func__, strerror(errno));
1298
Damien Millerdadfd4d2005-05-26 12:07:13 +10001299 if (connect(sock, (struct sockaddr*)&addr, addr_len) == -1) {
Darren Tucker0814d312005-06-01 23:08:51 +10001300 if (mux_command != SSHMUX_COMMAND_OPEN) {
1301 fatal("Control socket connect(%.100s): %s", path,
1302 strerror(errno));
1303 }
Damien Miller538c9b72005-05-26 12:11:28 +10001304 if (errno == ENOENT)
1305 debug("Control socket \"%.100s\" does not exist", path);
1306 else {
1307 error("Control socket connect(%.100s): %s", path,
1308 strerror(errno));
1309 }
Damien Miller13390022005-07-06 09:44:19 +10001310 close(sock);
1311 return;
1312 }
Damien Miller46d38de2005-07-17 17:02:09 +10001313
Damien Miller13390022005-07-06 09:44:19 +10001314 if (stdin_null_flag) {
1315 if ((fd = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1316 fatal("open(/dev/null): %s", strerror(errno));
1317 if (dup2(fd, STDIN_FILENO) == -1)
1318 fatal("dup2: %s", strerror(errno));
1319 if (fd > STDERR_FILENO)
1320 close(fd);
1321 }
Damien Miller46d38de2005-07-17 17:02:09 +10001322
Damien Miller13390022005-07-06 09:44:19 +10001323 term = getenv("TERM");
Darren Tucker7ebfc102004-11-07 20:06:19 +11001324
1325 flags = 0;
1326 if (tty_flag)
1327 flags |= SSHMUX_FLAG_TTY;
1328 if (subsystem_flag)
1329 flags |= SSHMUX_FLAG_SUBSYS;
Damien Miller13390022005-07-06 09:44:19 +10001330 if (options.forward_x11)
1331 flags |= SSHMUX_FLAG_X11_FWD;
1332 if (options.forward_agent)
1333 flags |= SSHMUX_FLAG_AGENT_FWD;
Damien Miller0e220db2004-06-15 10:34:08 +10001334
Damien Miller0e220db2004-06-15 10:34:08 +10001335 buffer_init(&m);
1336
Darren Tucker7ebfc102004-11-07 20:06:19 +11001337 /* Send our command to server */
1338 buffer_put_int(&m, mux_command);
1339 buffer_put_int(&m, flags);
Damien Miller13390022005-07-06 09:44:19 +10001340 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001341 fatal("%s: msg_send", __func__);
1342 buffer_clear(&m);
1343
1344 /* Get authorisation status and PID of controlee */
Damien Miller0e220db2004-06-15 10:34:08 +10001345 if (ssh_msg_recv(sock, &m) == -1)
1346 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001347 if (buffer_get_char(&m) != SSHMUX_VER)
Damien Miller0e220db2004-06-15 10:34:08 +10001348 fatal("%s: wrong version", __func__);
Damien Miller23f07702004-06-18 01:19:03 +10001349 if (buffer_get_int(&m) != 1)
1350 fatal("Connection to master denied");
Damien Miller0e220db2004-06-15 10:34:08 +10001351 control_server_pid = buffer_get_int(&m);
1352
Damien Miller0e220db2004-06-15 10:34:08 +10001353 buffer_clear(&m);
Damien Miller0e220db2004-06-15 10:34:08 +10001354
Darren Tucker7ebfc102004-11-07 20:06:19 +11001355 switch (mux_command) {
1356 case SSHMUX_COMMAND_ALIVE_CHECK:
Darren Tucker47eede72005-03-14 23:08:12 +11001357 fprintf(stderr, "Master running (pid=%d)\r\n",
Darren Tucker7ebfc102004-11-07 20:06:19 +11001358 control_server_pid);
1359 exit(0);
1360 case SSHMUX_COMMAND_TERMINATE:
1361 fprintf(stderr, "Exit request sent.\r\n");
1362 exit(0);
1363 case SSHMUX_COMMAND_OPEN:
1364 /* continue below */
1365 break;
1366 default:
1367 fatal("silly mux_command %d", mux_command);
1368 }
1369
1370 /* SSHMUX_COMMAND_OPEN */
Damien Miller13390022005-07-06 09:44:19 +10001371 buffer_put_cstring(&m, term ? term : "");
Damien Miller0e220db2004-06-15 10:34:08 +10001372 buffer_append(&command, "\0", 1);
1373 buffer_put_cstring(&m, buffer_ptr(&command));
1374
Darren Tucker365433f2004-06-22 12:29:23 +10001375 if (options.num_send_env == 0 || environ == NULL) {
1376 buffer_put_int(&m, 0);
Darren Tuckerfc959702004-07-17 16:12:08 +10001377 } else {
Darren Tucker365433f2004-06-22 12:29:23 +10001378 /* Pass environment */
1379 num_env = 0;
1380 for (i = 0; environ[i] != NULL; i++)
1381 if (env_permitted(environ[i]))
1382 num_env++; /* Count */
Darren Tuckerfc959702004-07-17 16:12:08 +10001383
Darren Tucker365433f2004-06-22 12:29:23 +10001384 buffer_put_int(&m, num_env);
1385
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001386 for (i = 0; environ[i] != NULL && num_env >= 0; i++)
1387 if (env_permitted(environ[i])) {
1388 num_env--;
Darren Tucker365433f2004-06-22 12:29:23 +10001389 buffer_put_cstring(&m, environ[i]);
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001390 }
Darren Tucker365433f2004-06-22 12:29:23 +10001391 }
Damien Miller3756dce2004-06-18 01:17:29 +10001392
Damien Miller13390022005-07-06 09:44:19 +10001393 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Damien Miller0e220db2004-06-15 10:34:08 +10001394 fatal("%s: msg_send", __func__);
1395
1396 mm_send_fd(sock, STDIN_FILENO);
1397 mm_send_fd(sock, STDOUT_FILENO);
1398 mm_send_fd(sock, STDERR_FILENO);
1399
1400 /* Wait for reply, so master has a chance to gather ttymodes */
1401 buffer_clear(&m);
1402 if (ssh_msg_recv(sock, &m) == -1)
1403 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001404 if (buffer_get_char(&m) != SSHMUX_VER)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001405 fatal("%s: wrong version", __func__);
Damien Miller0e220db2004-06-15 10:34:08 +10001406 buffer_free(&m);
1407
Darren Tucker07336da2004-11-05 20:02:16 +11001408 signal(SIGHUP, control_client_sighandler);
Damien Miller0809e232004-06-18 22:20:57 +10001409 signal(SIGINT, control_client_sighandler);
1410 signal(SIGTERM, control_client_sighandler);
1411 signal(SIGWINCH, control_client_sigrelay);
1412
Damien Miller0e220db2004-06-15 10:34:08 +10001413 if (tty_flag)
1414 enter_raw_mode();
1415
1416 /* Stick around until the controlee closes the client_fd */
1417 exitval = 0;
1418 for (;!control_client_terminate;) {
1419 r = read(sock, &exitval, sizeof(exitval));
1420 if (r == 0) {
1421 debug2("Received EOF from master");
1422 break;
1423 }
1424 if (r > 0)
1425 debug2("Received exit status from master %d", exitval);
1426 if (r == -1 && errno != EINTR)
1427 fatal("%s: read %s", __func__, strerror(errno));
1428 }
1429
1430 if (control_client_terminate)
1431 debug2("Exiting on signal %d", control_client_terminate);
1432
1433 close(sock);
1434
1435 leave_raw_mode();
1436
1437 if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
1438 fprintf(stderr, "Connection to master closed.\r\n");
1439
1440 exit(exitval);
1441}