blob: b248c0280e22de1bfeb5b26f12325ae3fbd8a165 [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 Miller03e20032006-03-15 11:16:59 +110043RCSID("$OpenBSD: ssh.c,v 1.258 2006/02/08 12:15:27 stevesk Exp $");
44
45#include <paths.h>
Damien Millereba71ba2000-04-29 23:57:08 +100046
47#include <openssl/evp.h>
Damien Miller0bc1bd82000-11-13 22:57:25 +110048#include <openssl/err.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051#include "ssh1.h"
Damien Miller1383bd82000-04-06 12:32:37 +100052#include "ssh2.h"
53#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000054#include "cipher.h"
55#include "xmalloc.h"
56#include "packet.h"
57#include "buffer.h"
Damien Miller0e220db2004-06-15 10:34:08 +100058#include "bufaux.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000059#include "channels.h"
Damien Millereba71ba2000-04-29 23:57:08 +100060#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100061#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100062#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063#include "pathnames.h"
Damien Miller0e220db2004-06-15 10:34:08 +100064#include "dispatch.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +000065#include "clientloop.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000066#include "log.h"
67#include "readconf.h"
68#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000069#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000070#include "kex.h"
71#include "mac.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +100072#include "sshpty.h"
Darren Tucker46bc0752004-05-02 22:11:30 +100073#include "match.h"
Damien Miller0e220db2004-06-15 10:34:08 +100074#include "msg.h"
75#include "monitor_fdpass.h"
Darren Tucker25f60a72004-08-15 17:23:34 +100076#include "uidswap.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077
Ben Lindstromc5b68002001-07-04 04:52:03 +000078#ifdef SMARTCARD
Ben Lindstromc5b68002001-07-04 04:52:03 +000079#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000080#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +000081
Damien Miller3f905871999-11-15 17:10:57 +110082extern char *__progname;
Damien Miller3f905871999-11-15 17:10:57 +110083
Damien Miller95def091999-11-25 00:26:21 +110084/* Flag indicating whether debug mode is on. This can be set on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085int debug_flag = 0;
86
Damien Miller78928792000-04-12 20:17:38 +100087/* Flag indicating whether a tty should be allocated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088int tty_flag = 0;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +000089int no_tty_flag = 0;
90int force_tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091
Damien Miller1383bd82000-04-06 12:32:37 +100092/* don't exec a shell */
93int no_shell_flag = 0;
Damien Miller1383bd82000-04-06 12:32:37 +100094
Damien Miller5428f641999-11-25 11:54:57 +110095/*
96 * Flag indicating that nothing should be read from stdin. This can be set
97 * on the command line.
98 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099int stdin_null_flag = 0;
100
Damien Miller5428f641999-11-25 11:54:57 +1100101/*
102 * Flag indicating that ssh should fork after authentication. This is useful
Ben Lindstromf666fec2002-06-06 19:51:58 +0000103 * so that the passphrase can be entered manually, and then ssh goes to the
Damien Miller5428f641999-11-25 11:54:57 +1100104 * background.
105 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106int fork_after_authentication_flag = 0;
107
Damien Miller5428f641999-11-25 11:54:57 +1100108/*
109 * General data structure for command line options and options configurable
110 * in configuration files. See readconf.h.
111 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112Options options;
113
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000114/* optional user configfile */
115char *config = NULL;
116
Damien Miller5428f641999-11-25 11:54:57 +1100117/*
118 * Name of the host we are connecting to. This is the name given on the
119 * command line, or the HostName specified for the user-supplied name in a
120 * configuration file.
121 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122char *host;
123
124/* socket address the host resolves to */
Damien Miller34132e52000-01-14 15:45:46 +1100125struct sockaddr_storage hostaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000127/* Private host keys. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000128Sensitive sensitive_data;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129
130/* Original real UID. */
131uid_t original_real_uid;
Ben Lindstromf9c48842002-06-11 16:37:51 +0000132uid_t original_effective_uid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000133
Damien Miller1383bd82000-04-06 12:32:37 +1000134/* command to be executed */
135Buffer command;
136
Damien Miller832562e2001-01-30 09:30:01 +1100137/* Should we execute a command or invoke a subsystem? */
138int subsystem_flag = 0;
139
Damien Miller2797f7f2002-04-23 21:09:44 +1000140/* # of replies received for global requests */
141static int client_global_request_id = 0;
142
Damien Miller8c4e18a2002-09-19 12:05:02 +1000143/* pid of proxycommand child process */
144pid_t proxy_command_pid = 0;
145
Damien Miller0e220db2004-06-15 10:34:08 +1000146/* fd to control socket */
147int control_fd = -1;
148
Darren Tucker7ebfc102004-11-07 20:06:19 +1100149/* Multiplexing control command */
Darren Tucker0814d312005-06-01 23:08:51 +1000150static u_int mux_command = 0;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100151
Damien Miller0e220db2004-06-15 10:34:08 +1000152/* Only used in control client mode */
153volatile sig_atomic_t control_client_terminate = 0;
154u_int control_server_pid = 0;
155
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000156/* Prints a help message to the user. This function never returns. */
157
Ben Lindstrombba81212001-06-25 05:01:22 +0000158static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000159usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160{
Damien Miller50955102004-03-22 09:34:58 +1100161 fprintf(stderr,
Darren Tucker9c6bf322004-12-03 14:10:19 +1100162"usage: ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n"
Darren Tucker895d6982005-10-03 18:18:05 +1000163" [-D [bind_address:]port] [-e escape_char] [-F configfile]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100164" [-i identity_file] [-L [bind_address:]port:host:hostport]\n"
Damien Millerf91ee4c2005-03-01 21:24:33 +1100165" [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100166" [-R [bind_address:]port:host:hostport] [-S ctl_path]\n"
Damien Millerd27b9472005-12-13 19:29:02 +1100167" [-w tunnel:tunnel] [user@]hostname [command]\n"
Damien Miller50955102004-03-22 09:34:58 +1100168 );
Darren Tuckere9a9b712005-12-20 16:15:51 +1100169 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170}
171
Ben Lindstrombba81212001-06-25 05:01:22 +0000172static int ssh_session(void);
173static int ssh_session2(void);
174static void load_public_identity_files(void);
Damien Miller0e220db2004-06-15 10:34:08 +1000175static void control_client(const char *path);
Damien Miller1383bd82000-04-06 12:32:37 +1000176
Damien Miller95def091999-11-25 00:26:21 +1100177/*
178 * Main program for the ssh client.
179 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000180int
181main(int ac, char **av)
182{
Ben Lindstrom24157572002-06-12 16:09:39 +0000183 int i, opt, exit_status;
Damien Miller9836cf82003-12-17 16:30:06 +1100184 char *p, *cp, *line, buf[256];
Damien Miller95def091999-11-25 00:26:21 +1100185 struct stat st;
Ben Lindstrom086cf212001-03-05 05:56:40 +0000186 struct passwd *pw;
Damien Miller1383bd82000-04-06 12:32:37 +1000187 int dummy;
Ben Lindstrom5ccf63a2001-09-24 20:00:10 +0000188 extern int optind, optreset;
Damien Miller4f8e6692001-07-14 13:22:53 +1000189 extern char *optarg;
Damien Miller9651fe62005-06-26 08:55:25 +1000190 struct servent *sp;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100191 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000192
Darren Tuckerce321d82005-10-03 18:11:24 +1000193 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
194 sanitise_stdfd();
195
Damien Miller59d3d5b2003-08-22 09:34:41 +1000196 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000197 init_rng();
198
Damien Miller5428f641999-11-25 11:54:57 +1100199 /*
200 * Save the original real uid. It will be needed later (uid-swapping
201 * may clobber the real uid).
202 */
Damien Miller95def091999-11-25 00:26:21 +1100203 original_real_uid = getuid();
204 original_effective_uid = geteuid();
Damien Millera8e06ce2003-11-21 23:48:55 +1100205
Damien Miller50b9a602002-09-04 16:50:06 +1000206 /*
207 * Use uid-swapping to give up root privileges for the duration of
208 * option processing. We will re-instantiate the rights when we are
209 * ready to create the privileged port, and will permanently drop
210 * them when the port has been created (actually, when the connection
211 * has been made, as we may need to create the port several times).
212 */
213 PRIV_END;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000214
Damien Miller05dd7952000-10-01 00:42:48 +1100215#ifdef HAVE_SETRLIMIT
Damien Miller95def091999-11-25 00:26:21 +1100216 /* If we are installed setuid root be careful to not drop core. */
217 if (original_real_uid != original_effective_uid) {
218 struct rlimit rlim;
219 rlim.rlim_cur = rlim.rlim_max = 0;
220 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
221 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000222 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100223#endif
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000224 /* Get user data. */
225 pw = getpwuid(original_real_uid);
226 if (!pw) {
Damien Miller996acd22003-04-09 20:59:48 +1000227 logit("You don't exist, go away!");
Darren Tuckere9a9b712005-12-20 16:15:51 +1100228 exit(255);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000229 }
230 /* Take a copy of the returned structure. */
231 pw = pwcopy(pw);
232
Damien Miller5428f641999-11-25 11:54:57 +1100233 /*
Damien Miller5428f641999-11-25 11:54:57 +1100234 * Set our umask to something reasonable, as some files are created
235 * with the default umask. This will make them world-readable but
236 * writable only by the owner, which is ok for all files for which we
237 * don't set the modes explicitly.
238 */
Damien Miller95def091999-11-25 00:26:21 +1100239 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240
Damien Miller95def091999-11-25 00:26:21 +1100241 /* Initialize option structure to indicate that no values have been set. */
242 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000243
Damien Miller95def091999-11-25 00:26:21 +1100244 /* Parse command-line arguments. */
245 host = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000246
Damien Millerf4614452001-07-14 12:18:10 +1000247again:
248 while ((opt = getopt(ac, av,
Damien Millerd27b9472005-12-13 19:29:02 +1100249 "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 +1100250 switch (opt) {
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000251 case '1':
252 options.protocol = SSH_PROTO_1;
253 break;
Damien Miller4af51302000-04-16 11:18:38 +1000254 case '2':
255 options.protocol = SSH_PROTO_2;
256 break;
Damien Miller34132e52000-01-14 15:45:46 +1100257 case '4':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000258 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100259 break;
Damien Miller34132e52000-01-14 15:45:46 +1100260 case '6':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000261 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100262 break;
Damien Miller95def091999-11-25 00:26:21 +1100263 case 'n':
264 stdin_null_flag = 1;
265 break;
Damien Miller95def091999-11-25 00:26:21 +1100266 case 'f':
267 fork_after_authentication_flag = 1;
268 stdin_null_flag = 1;
269 break;
Damien Miller95def091999-11-25 00:26:21 +1100270 case 'x':
271 options.forward_x11 = 0;
272 break;
Damien Miller95def091999-11-25 00:26:21 +1100273 case 'X':
274 options.forward_x11 = 1;
275 break;
Darren Tucker0a118da2003-10-15 15:54:32 +1000276 case 'Y':
277 options.forward_x11 = 1;
278 options.forward_x11_trusted = 1;
279 break;
Damien Miller95def091999-11-25 00:26:21 +1100280 case 'g':
281 options.gateway_ports = 1;
282 break;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100283 case 'O':
284 if (strcmp(optarg, "check") == 0)
285 mux_command = SSHMUX_COMMAND_ALIVE_CHECK;
286 else if (strcmp(optarg, "exit") == 0)
287 mux_command = SSHMUX_COMMAND_TERMINATE;
288 else
289 fatal("Invalid multiplex command.");
290 break;
Damien Miller147bba32002-09-04 16:46:06 +1000291 case 'P': /* deprecated */
Damien Miller95def091999-11-25 00:26:21 +1100292 options.use_privileged_port = 0;
293 break;
Damien Miller95def091999-11-25 00:26:21 +1100294 case 'a':
295 options.forward_agent = 0;
296 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000297 case 'A':
298 options.forward_agent = 1;
299 break;
Damien Miller95def091999-11-25 00:26:21 +1100300 case 'k':
Damien Millere0113cc2003-11-24 13:10:09 +1100301 options.gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100302 break;
Damien Miller95def091999-11-25 00:26:21 +1100303 case 'i':
304 if (stat(optarg, &st) < 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000305 fprintf(stderr, "Warning: Identity file %s "
Damien Miller3eb48b62005-03-01 21:15:46 +1100306 "not accessible: %s.\n", optarg,
307 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100308 break;
309 }
Damien Millerf4614452001-07-14 12:18:10 +1000310 if (options.num_identity_files >=
311 SSH_MAX_IDENTITY_FILES)
312 fatal("Too many identity files specified "
313 "(max %d)", SSH_MAX_IDENTITY_FILES);
314 options.identity_files[options.num_identity_files++] =
315 xstrdup(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100316 break;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000317 case 'I':
318#ifdef SMARTCARD
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000319 options.smartcard_device = xstrdup(optarg);
Ben Lindstrombcc18082001-08-06 21:59:25 +0000320#else
Ben Lindstromc5b68002001-07-04 04:52:03 +0000321 fprintf(stderr, "no support for smartcards.\n");
Ben Lindstrombcc18082001-08-06 21:59:25 +0000322#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000323 break;
Damien Miller95def091999-11-25 00:26:21 +1100324 case 't':
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000325 if (tty_flag)
326 force_tty_flag = 1;
Damien Miller95def091999-11-25 00:26:21 +1100327 tty_flag = 1;
328 break;
Damien Miller95def091999-11-25 00:26:21 +1100329 case 'v':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000330 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100331 debug_flag = 1;
332 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000333 } else {
334 if (options.log_level < SYSLOG_LEVEL_DEBUG3)
335 options.log_level++;
Damien Millere4340be2000-09-16 13:29:08 +1100336 break;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000337 }
Darren Tuckere9bf9842004-11-05 20:05:32 +1100338 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100339 case 'V':
Damien Miller0c889cd2004-03-22 09:36:00 +1100340 fprintf(stderr, "%s, %s\n",
Damien Miller2aa6d3c2004-09-12 16:53:04 +1000341 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
Damien Miller95def091999-11-25 00:26:21 +1100342 if (opt == 'V')
343 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100344 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100345 case 'w':
Damien Miller7b58e802005-12-13 19:33:19 +1100346 if (options.tun_open == -1)
347 options.tun_open = SSH_TUNMODE_DEFAULT;
Damien Millerd27b9472005-12-13 19:29:02 +1100348 options.tun_local = a2tun(optarg, &options.tun_remote);
Damien Miller7b58e802005-12-13 19:33:19 +1100349 if (options.tun_local == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100350 fprintf(stderr, "Bad tun device '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100351 exit(255);
Damien Millerd27b9472005-12-13 19:29:02 +1100352 }
353 break;
Damien Miller95def091999-11-25 00:26:21 +1100354 case 'q':
355 options.log_level = SYSLOG_LEVEL_QUIET;
356 break;
Damien Miller95def091999-11-25 00:26:21 +1100357 case 'e':
358 if (optarg[0] == '^' && optarg[2] == 0 &&
Damien Millerf4614452001-07-14 12:18:10 +1000359 (u_char) optarg[1] >= 64 &&
360 (u_char) optarg[1] < 128)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000361 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100362 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000363 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100364 else if (strcmp(optarg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000365 options.escape_char = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100366 else {
Damien Millerf4614452001-07-14 12:18:10 +1000367 fprintf(stderr, "Bad escape character '%s'.\n",
368 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100369 exit(255);
Damien Miller95def091999-11-25 00:26:21 +1100370 }
371 break;
Damien Miller95def091999-11-25 00:26:21 +1100372 case 'c':
Damien Millereba71ba2000-04-29 23:57:08 +1000373 if (ciphers_valid(optarg)) {
374 /* SSH2 only */
375 options.ciphers = xstrdup(optarg);
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000376 options.cipher = SSH_CIPHER_INVALID;
Damien Millereba71ba2000-04-29 23:57:08 +1000377 } else {
378 /* SSH1 only */
Damien Millere39cacc2000-11-29 12:18:44 +1100379 options.cipher = cipher_number(optarg);
380 if (options.cipher == -1) {
Damien Millerf4614452001-07-14 12:18:10 +1000381 fprintf(stderr,
382 "Unknown cipher type '%s'\n",
383 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100384 exit(255);
Damien Millereba71ba2000-04-29 23:57:08 +1000385 }
Damien Millerf4614452001-07-14 12:18:10 +1000386 if (options.cipher == SSH_CIPHER_3DES)
Damien Millere39cacc2000-11-29 12:18:44 +1100387 options.ciphers = "3des-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000388 else if (options.cipher == SSH_CIPHER_BLOWFISH)
Damien Millere39cacc2000-11-29 12:18:44 +1100389 options.ciphers = "blowfish-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000390 else
Damien Millere39cacc2000-11-29 12:18:44 +1100391 options.ciphers = (char *)-1;
Damien Miller95def091999-11-25 00:26:21 +1100392 }
393 break;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000394 case 'm':
395 if (mac_valid(optarg))
396 options.macs = xstrdup(optarg);
397 else {
Damien Millerf4614452001-07-14 12:18:10 +1000398 fprintf(stderr, "Unknown mac type '%s'\n",
399 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100400 exit(255);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000401 }
402 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000403 case 'M':
Damien Millerd14b1e72005-06-16 13:19:41 +1000404 if (options.control_master == SSHCTL_MASTER_YES)
405 options.control_master = SSHCTL_MASTER_ASK;
406 else
407 options.control_master = SSHCTL_MASTER_YES;
Damien Miller0e220db2004-06-15 10:34:08 +1000408 break;
Damien Miller95def091999-11-25 00:26:21 +1100409 case 'p':
Ben Lindstrom19066a12001-04-12 23:39:26 +0000410 options.port = a2port(optarg);
411 if (options.port == 0) {
Ben Lindstrom146edb92001-04-11 23:06:28 +0000412 fprintf(stderr, "Bad port '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100413 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000414 }
Damien Miller95def091999-11-25 00:26:21 +1100415 break;
Damien Miller95def091999-11-25 00:26:21 +1100416 case 'l':
417 options.user = optarg;
418 break;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000419
Damien Miller95def091999-11-25 00:26:21 +1100420 case 'L':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100421 if (parse_forward(&fwd, optarg))
422 add_local_forward(&options, &fwd);
423 else {
Damien Millerf4614452001-07-14 12:18:10 +1000424 fprintf(stderr,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100425 "Bad local forwarding specification '%s'\n",
Damien Millerf4614452001-07-14 12:18:10 +1000426 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100427 exit(255);
Ben Lindstrom1a174712001-09-12 17:56:15 +0000428 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100429 break;
430
431 case 'R':
432 if (parse_forward(&fwd, optarg)) {
433 add_remote_forward(&options, &fwd);
434 } else {
435 fprintf(stderr,
436 "Bad remote forwarding specification "
437 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100438 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100439 }
Damien Miller95def091999-11-25 00:26:21 +1100440 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000441
442 case 'D':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100443 cp = p = xstrdup(optarg);
444 memset(&fwd, '\0', sizeof(fwd));
445 fwd.connect_host = "socks";
446 if ((fwd.listen_host = hpdelim(&cp)) == NULL) {
447 fprintf(stderr, "Bad dynamic forwarding "
448 "specification '%.100s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100449 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100450 }
451 if (cp != NULL) {
452 fwd.listen_port = a2port(cp);
453 fwd.listen_host = cleanhostname(fwd.listen_host);
454 } else {
455 fwd.listen_port = a2port(fwd.listen_host);
Damien Millerbe1045d2005-08-12 22:10:56 +1000456 fwd.listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100457 }
458
459 if (fwd.listen_port == 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000460 fprintf(stderr, "Bad dynamic port '%s'\n",
461 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100462 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000463 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100464 add_local_forward(&options, &fwd);
465 xfree(p);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000466 break;
467
Damien Miller95def091999-11-25 00:26:21 +1100468 case 'C':
469 options.compression = 1;
470 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000471 case 'N':
472 no_shell_flag = 1;
473 no_tty_flag = 1;
474 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000475 case 'T':
476 no_tty_flag = 1;
477 break;
Damien Miller95def091999-11-25 00:26:21 +1100478 case 'o':
479 dummy = 1;
Damien Miller9836cf82003-12-17 16:30:06 +1100480 line = xstrdup(optarg);
Damien Millerf4614452001-07-14 12:18:10 +1000481 if (process_config_line(&options, host ? host : "",
Damien Miller9836cf82003-12-17 16:30:06 +1100482 line, "command-line", 0, &dummy) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100483 exit(255);
Damien Miller9836cf82003-12-17 16:30:06 +1100484 xfree(line);
Damien Miller95def091999-11-25 00:26:21 +1100485 break;
Damien Miller832562e2001-01-30 09:30:01 +1100486 case 's':
487 subsystem_flag = 1;
488 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000489 case 'S':
490 if (options.control_path != NULL)
491 free(options.control_path);
492 options.control_path = xstrdup(optarg);
Damien Miller0e220db2004-06-15 10:34:08 +1000493 break;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000494 case 'b':
495 options.bind_address = optarg;
496 break;
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000497 case 'F':
498 config = optarg;
499 break;
Damien Miller95def091999-11-25 00:26:21 +1100500 default:
501 usage();
502 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000503 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000504
Damien Millerf4614452001-07-14 12:18:10 +1000505 ac -= optind;
506 av += optind;
507
508 if (ac > 0 && !host && **av != '-') {
Ben Lindstromc276c122002-12-23 02:14:51 +0000509 if (strrchr(*av, '@')) {
Damien Millerf4614452001-07-14 12:18:10 +1000510 p = xstrdup(*av);
Ben Lindstromc276c122002-12-23 02:14:51 +0000511 cp = strrchr(p, '@');
Damien Millerf4614452001-07-14 12:18:10 +1000512 if (cp == NULL || cp == p)
513 usage();
514 options.user = p;
515 *cp = '\0';
516 host = ++cp;
517 } else
518 host = *av;
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000519 if (ac > 1) {
520 optind = optreset = 1;
Damien Millerf4614452001-07-14 12:18:10 +1000521 goto again;
522 }
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000523 ac--, av++;
Damien Millerf4614452001-07-14 12:18:10 +1000524 }
525
Damien Miller95def091999-11-25 00:26:21 +1100526 /* Check that we got a host name. */
527 if (!host)
528 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000529
Damien Millercb5e44a2000-09-29 12:12:36 +1100530 SSLeay_add_all_algorithms();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100531 ERR_load_crypto_strings();
Damien Millercb5e44a2000-09-29 12:12:36 +1100532
Damien Miller95def091999-11-25 00:26:21 +1100533 /* Initialize the command to execute on remote host. */
534 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000535
Damien Miller5428f641999-11-25 11:54:57 +1100536 /*
537 * Save the command to execute on the remote host in a buffer. There
538 * is no limit on the length of the command, except by the maximum
539 * packet size. Also sets the tty flag if there is no command.
540 */
Damien Millerf4614452001-07-14 12:18:10 +1000541 if (!ac) {
Damien Miller95def091999-11-25 00:26:21 +1100542 /* No command specified - execute shell on a tty. */
543 tty_flag = 1;
Damien Miller832562e2001-01-30 09:30:01 +1100544 if (subsystem_flag) {
Damien Millerf4614452001-07-14 12:18:10 +1000545 fprintf(stderr,
546 "You must specify a subsystem to invoke.\n");
Damien Miller832562e2001-01-30 09:30:01 +1100547 usage();
548 }
Damien Miller95def091999-11-25 00:26:21 +1100549 } else {
Damien Millerf4614452001-07-14 12:18:10 +1000550 /* A command has been specified. Store it into the buffer. */
551 for (i = 0; i < ac; i++) {
552 if (i)
Damien Miller95def091999-11-25 00:26:21 +1100553 buffer_append(&command, " ", 1);
554 buffer_append(&command, av[i], strlen(av[i]));
555 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000556 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000557
Damien Miller95def091999-11-25 00:26:21 +1100558 /* Cannot fork to background if no command. */
Damien Millercaf6dd62000-08-29 11:33:50 +1100559 if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
Damien Miller95def091999-11-25 00:26:21 +1100560 fatal("Cannot fork into background without a command to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000561
Damien Miller95def091999-11-25 00:26:21 +1100562 /* Allocate a tty by default if no command specified. */
563 if (buffer_len(&command) == 0)
564 tty_flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000565
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000566 /* Force no tty */
Ben Lindstromc72745a2000-12-02 19:03:54 +0000567 if (no_tty_flag)
568 tty_flag = 0;
Damien Miller95def091999-11-25 00:26:21 +1100569 /* Do not allocate a tty if stdin is not a tty. */
Damien Millerddee5752005-05-26 12:05:05 +1000570 if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100571 if (tty_flag)
Damien Miller996acd22003-04-09 20:59:48 +1000572 logit("Pseudo-terminal will not be allocated because stdin is not a terminal.");
Damien Miller95def091999-11-25 00:26:21 +1100573 tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000574 }
Damien Miller1383bd82000-04-06 12:32:37 +1000575
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000576 /*
577 * Initialize "log" output. Since we are the client all output
578 * actually goes to stderr.
579 */
Ben Lindstrom2b646522001-04-12 16:16:57 +0000580 log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
581 SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000582
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000583 /*
584 * Read per-user configuration file. Ignore the system wide config
585 * file if the user specifies a config file on the command line.
586 */
587 if (config != NULL) {
Damien Miller914420f2004-04-20 20:14:07 +1000588 if (!read_config_file(config, host, &options, 0))
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000589 fatal("Can't open user config file %.100s: "
590 "%.100s", config, strerror(errno));
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000591 } else {
592 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
593 _PATH_SSH_USER_CONFFILE);
Damien Miller57a44762004-04-20 20:11:57 +1000594 (void)read_config_file(buf, host, &options, 1);
Ben Lindstrom83f07d12001-10-03 17:22:29 +0000595
596 /* Read systemwide configuration file after use config. */
Darren Tuckerfc959702004-07-17 16:12:08 +1000597 (void)read_config_file(_PATH_HOST_CONFIG_FILE, host,
Damien Miller57a44762004-04-20 20:11:57 +1000598 &options, 0);
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000599 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000600
Damien Miller95def091999-11-25 00:26:21 +1100601 /* Fill configuration defaults. */
602 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000603
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000604 channel_set_af(options.address_family);
605
Damien Miller95def091999-11-25 00:26:21 +1100606 /* reinit */
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000607 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000608
Damien Miller60bc5172001-03-19 09:38:15 +1100609 seed_rng();
610
Damien Miller95def091999-11-25 00:26:21 +1100611 if (options.user == NULL)
612 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000613
Damien Miller95def091999-11-25 00:26:21 +1100614 if (options.hostname != NULL)
615 host = options.hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000616
Darren Tucker3f521e22003-07-03 16:20:42 +1000617 /* force lowercase for hostkey matching */
618 if (options.host_key_alias != NULL) {
619 for (p = options.host_key_alias; *p; p++)
620 if (isupper(*p))
621 *p = tolower(*p);
622 }
623
Damien Miller7c71cc72005-06-26 08:56:31 +1000624 /* Get default port if port has not been set. */
625 if (options.port == 0) {
626 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
627 options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
628 }
629
Damien Miller9f1e33a2003-02-24 11:57:32 +1100630 if (options.proxy_command != NULL &&
631 strcmp(options.proxy_command, "none") == 0)
632 options.proxy_command = NULL;
Damien Miller8f74c8f2005-06-26 08:56:03 +1000633 if (options.control_path != NULL &&
634 strcmp(options.control_path, "none") == 0)
635 options.control_path = NULL;
Damien Miller9f1e33a2003-02-24 11:57:32 +1100636
Damien Miller0e220db2004-06-15 10:34:08 +1000637 if (options.control_path != NULL) {
Damien Miller6476cad2005-06-16 13:18:34 +1000638 snprintf(buf, sizeof(buf), "%d", options.port);
639 cp = tilde_expand_filename(options.control_path,
640 original_real_uid);
641 options.control_path = percent_expand(cp, "p", buf, "h", host,
642 "r", options.user, (char *)NULL);
643 xfree(cp);
Damien Miller0e220db2004-06-15 10:34:08 +1000644 }
Darren Tucker0814d312005-06-01 23:08:51 +1000645 if (mux_command != 0 && options.control_path == NULL)
646 fatal("No ControlPath specified for \"-O\" command");
Damien Millerd14b1e72005-06-16 13:19:41 +1000647 if (options.control_path != NULL)
Damien Millerdadfd4d2005-05-26 12:07:13 +1000648 control_client(options.control_path);
Damien Miller0e220db2004-06-15 10:34:08 +1000649
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000650 /* Open a connection to the remote host. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000651 if (ssh_connect(host, &hostaddr, options.port,
652 options.address_family, options.connection_attempts,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000653#ifdef HAVE_CYGWIN
654 options.use_privileged_port,
655#else
Ben Lindstromf9c48842002-06-11 16:37:51 +0000656 original_effective_uid == 0 && options.use_privileged_port,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000657#endif
Ben Lindstromda394ca2002-06-12 16:11:12 +0000658 options.proxy_command) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100659 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000660
Damien Miller5428f641999-11-25 11:54:57 +1100661 /*
662 * If we successfully made the connection, load the host private key
663 * in case we will need it later for combined rsa-rhosts
664 * authentication. This must be done before releasing extra
665 * privileges, because the file is only readable by root.
Ben Lindstrom9e5bb572002-06-06 19:58:27 +0000666 * If we cannot access the private keys, load the public keys
667 * instead and try to execute the ssh-keysign helper instead.
Damien Miller5428f641999-11-25 11:54:57 +1100668 */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000669 sensitive_data.nkeys = 0;
670 sensitive_data.keys = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000671 sensitive_data.external_keysign = 0;
Ben Lindstrom24157572002-06-12 16:09:39 +0000672 if (options.rhosts_rsa_authentication ||
673 options.hostbased_authentication) {
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000674 sensitive_data.nkeys = 3;
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000675 sensitive_data.keys = xmalloc(sensitive_data.nkeys *
676 sizeof(Key));
Ben Lindstromf9c48842002-06-11 16:37:51 +0000677
678 PRIV_START;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000679 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
Ben Lindstromd0fca422001-03-26 13:44:06 +0000680 _PATH_HOST_KEY_FILE, "", NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000681 sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
682 _PATH_HOST_DSA_KEY_FILE, "", NULL);
683 sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
684 _PATH_HOST_RSA_KEY_FILE, "", NULL);
Ben Lindstromf9c48842002-06-11 16:37:51 +0000685 PRIV_END;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000686
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +0000687 if (options.hostbased_authentication == 1 &&
688 sensitive_data.keys[0] == NULL &&
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000689 sensitive_data.keys[1] == NULL &&
690 sensitive_data.keys[2] == NULL) {
691 sensitive_data.keys[1] = key_load_public(
692 _PATH_HOST_DSA_KEY_FILE, NULL);
693 sensitive_data.keys[2] = key_load_public(
694 _PATH_HOST_RSA_KEY_FILE, NULL);
695 sensitive_data.external_keysign = 1;
696 }
Damien Miller95def091999-11-25 00:26:21 +1100697 }
Damien Miller5428f641999-11-25 11:54:57 +1100698 /*
699 * Get rid of any extra privileges that we may have. We will no
700 * longer need them. Also, extra privileges could make it very hard
701 * to read identity files and other non-world-readable files from the
702 * user's home directory if it happens to be on a NFS volume where
703 * root is mapped to nobody.
704 */
Darren Tucker25f60a72004-08-15 17:23:34 +1000705 if (original_effective_uid == 0) {
706 PRIV_START;
707 permanently_set_uid(pw);
708 }
Damien Miller95def091999-11-25 00:26:21 +1100709
Damien Miller5428f641999-11-25 11:54:57 +1100710 /*
711 * Now that we are back to our own permissions, create ~/.ssh
Damien Miller788f2122005-11-05 15:14:59 +1100712 * directory if it doesn't already exist.
Damien Miller5428f641999-11-25 11:54:57 +1100713 */
Ben Lindstrom930b14a2001-08-15 23:19:21 +0000714 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 +1100715 if (stat(buf, &st) < 0)
Damien Millerbe484b52000-07-15 14:14:16 +1000716 if (mkdir(buf, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100717 error("Could not create directory '%.200s'.", buf);
718
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000719 /* load options.identity_files */
720 load_public_identity_files();
721
722 /* Expand ~ in known host file names. */
723 /* XXX mem-leaks: */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100724 options.system_hostfile =
725 tilde_expand_filename(options.system_hostfile, original_real_uid);
726 options.user_hostfile =
727 tilde_expand_filename(options.user_hostfile, original_real_uid);
728 options.system_hostfile2 =
729 tilde_expand_filename(options.system_hostfile2, original_real_uid);
730 options.user_hostfile2 =
731 tilde_expand_filename(options.user_hostfile2, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100732
Damien Miller07cd5892001-11-12 10:52:03 +1100733 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
734
Damien Miller95def091999-11-25 00:26:21 +1100735 /* Log into the remote system. This never returns if the login fails. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000736 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, pw);
Damien Miller95def091999-11-25 00:26:21 +1100737
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000738 /* We no longer need the private host keys. Clear them now. */
739 if (sensitive_data.nkeys != 0) {
740 for (i = 0; i < sensitive_data.nkeys; i++) {
741 if (sensitive_data.keys[i] != NULL) {
742 /* Destroys contents safely */
743 debug3("clear hostkey %d", i);
744 key_free(sensitive_data.keys[i]);
745 sensitive_data.keys[i] = NULL;
746 }
747 }
748 xfree(sensitive_data.keys);
749 }
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +0000750 for (i = 0; i < options.num_identity_files; i++) {
751 if (options.identity_files[i]) {
752 xfree(options.identity_files[i]);
753 options.identity_files[i] = NULL;
754 }
755 if (options.identity_keys[i]) {
756 key_free(options.identity_keys[i]);
757 options.identity_keys[i] = NULL;
758 }
759 }
Damien Miller95def091999-11-25 00:26:21 +1100760
Damien Miller1383bd82000-04-06 12:32:37 +1000761 exit_status = compat20 ? ssh_session2() : ssh_session();
762 packet_close();
Damien Miller8c4e18a2002-09-19 12:05:02 +1000763
Damien Miller0e220db2004-06-15 10:34:08 +1000764 if (options.control_path != NULL && control_fd != -1)
765 unlink(options.control_path);
766
Damien Miller8c4e18a2002-09-19 12:05:02 +1000767 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100768 * Send SIGHUP to proxy command if used. We don't wait() in
Damien Miller8c4e18a2002-09-19 12:05:02 +1000769 * case it hangs and instead rely on init to reap the child
770 */
771 if (proxy_command_pid > 1)
772 kill(proxy_command_pid, SIGHUP);
773
Damien Miller1383bd82000-04-06 12:32:37 +1000774 return exit_status;
775}
776
Ben Lindstrombba81212001-06-25 05:01:22 +0000777static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100778ssh_init_forwarding(void)
779{
Kevin Steves12057502001-02-05 14:54:34 +0000780 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100781 int i;
Kevin Steves12057502001-02-05 14:54:34 +0000782
Damien Miller0bc1bd82000-11-13 22:57:25 +1100783 /* Initiate local TCP/IP port forwardings. */
784 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100785 debug("Local connections to %.200s:%d forwarded to remote "
786 "address %.200s:%d",
Darren Tucker47eede72005-03-14 23:08:12 +1100787 (options.local_forwards[i].listen_host == NULL) ?
788 (options.gateway_ports ? "*" : "LOCALHOST") :
Damien Millerf91ee4c2005-03-01 21:24:33 +1100789 options.local_forwards[i].listen_host,
790 options.local_forwards[i].listen_port,
791 options.local_forwards[i].connect_host,
792 options.local_forwards[i].connect_port);
Damien Millerb16461c2002-01-22 23:29:22 +1100793 success += channel_setup_local_fwd_listener(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100794 options.local_forwards[i].listen_host,
795 options.local_forwards[i].listen_port,
796 options.local_forwards[i].connect_host,
797 options.local_forwards[i].connect_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100798 options.gateway_ports);
799 }
Kevin Steves12057502001-02-05 14:54:34 +0000800 if (i > 0 && success == 0)
801 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100802
803 /* Initiate remote TCP/IP port forwardings. */
804 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100805 debug("Remote connections from %.200s:%d forwarded to "
806 "local address %.200s:%d",
Damien Miller46d38de2005-07-17 17:02:09 +1000807 (options.remote_forwards[i].listen_host == NULL) ?
Damien Milleraa3bb102005-11-05 15:12:59 +1100808 "LOCALHOST" : options.remote_forwards[i].listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100809 options.remote_forwards[i].listen_port,
810 options.remote_forwards[i].connect_host,
811 options.remote_forwards[i].connect_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100812 channel_request_remote_forwarding(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100813 options.remote_forwards[i].listen_host,
814 options.remote_forwards[i].listen_port,
815 options.remote_forwards[i].connect_host,
816 options.remote_forwards[i].connect_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100817 }
818}
819
Ben Lindstrombba81212001-06-25 05:01:22 +0000820static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100821check_agent_present(void)
822{
823 if (options.forward_agent) {
Damien Miller788f2122005-11-05 15:14:59 +1100824 /* Clear agent forwarding if we don't have an agent. */
Damien Miller789e95d2002-09-12 09:52:46 +1000825 if (!ssh_agent_present())
Damien Miller0bc1bd82000-11-13 22:57:25 +1100826 options.forward_agent = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100827 }
828}
829
Ben Lindstrombba81212001-06-25 05:01:22 +0000830static int
Damien Miller1383bd82000-04-06 12:32:37 +1000831ssh_session(void)
832{
833 int type;
Damien Miller1383bd82000-04-06 12:32:37 +1000834 int interactive = 0;
835 int have_tty = 0;
836 struct winsize ws;
Damien Miller1383bd82000-04-06 12:32:37 +1000837 char *cp;
Damien Miller17e7ed02005-06-17 12:54:33 +1000838 const char *display;
Damien Miller1383bd82000-04-06 12:32:37 +1000839
Damien Miller95def091999-11-25 00:26:21 +1100840 /* Enable compression if requested. */
841 if (options.compression) {
842 debug("Requesting compression at level %d.", options.compression_level);
843
844 if (options.compression_level < 1 || options.compression_level > 9)
845 fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
846
847 /* Send the request. */
848 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
849 packet_put_int(options.compression_level);
850 packet_send();
851 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100852 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100853 if (type == SSH_SMSG_SUCCESS)
854 packet_start_compression(options.compression_level);
855 else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000856 logit("Warning: Remote host refused compression.");
Damien Miller95def091999-11-25 00:26:21 +1100857 else
858 packet_disconnect("Protocol error waiting for compression response.");
859 }
860 /* Allocate a pseudo tty if appropriate. */
861 if (tty_flag) {
862 debug("Requesting pty.");
863
864 /* Start the packet. */
865 packet_start(SSH_CMSG_REQUEST_PTY);
866
867 /* Store TERM in the packet. There is no limit on the
868 length of the string. */
869 cp = getenv("TERM");
870 if (!cp)
871 cp = "";
Ben Lindstrom664408d2001-06-09 01:42:01 +0000872 packet_put_cstring(cp);
Damien Miller95def091999-11-25 00:26:21 +1100873
874 /* Store window size in the packet. */
875 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
876 memset(&ws, 0, sizeof(ws));
877 packet_put_int(ws.ws_row);
878 packet_put_int(ws.ws_col);
879 packet_put_int(ws.ws_xpixel);
880 packet_put_int(ws.ws_ypixel);
881
882 /* Store tty modes in the packet. */
Ben Lindstromae8e2d32001-04-14 23:13:02 +0000883 tty_make_modes(fileno(stdin), NULL);
Damien Miller95def091999-11-25 00:26:21 +1100884
885 /* Send the packet, and wait for it to leave. */
886 packet_send();
887 packet_write_wait();
888
889 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100890 type = packet_read();
Damien Miller450a7a12000-03-26 13:04:51 +1000891 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100892 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +1000893 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000894 } else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000895 logit("Warning: Remote host failed or refused to allocate a pseudo tty.");
Damien Miller95def091999-11-25 00:26:21 +1100896 else
897 packet_disconnect("Protocol error waiting for pty request response.");
898 }
899 /* Request X11 forwarding if enabled and DISPLAY is set. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000900 display = getenv("DISPLAY");
901 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000902 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +1000903 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000904 client_x11_get_proto(display, options.xauth_location,
905 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +1000906 /* Request forwarding with authentication spoofing. */
Damien Miller95def091999-11-25 00:26:21 +1100907 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +1000908 x11_request_forwarding_with_spoofing(0, display, proto, data);
Damien Miller95def091999-11-25 00:26:21 +1100909
910 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100911 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100912 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100913 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +1000914 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller996acd22003-04-09 20:59:48 +1000915 logit("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +1000916 } else {
Damien Miller95def091999-11-25 00:26:21 +1100917 packet_disconnect("Protocol error waiting for X11 forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +1000918 }
Damien Miller95def091999-11-25 00:26:21 +1100919 }
920 /* Tell the packet module whether this is an interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000921 packet_set_interactive(interactive);
Damien Miller95def091999-11-25 00:26:21 +1100922
923 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100924 check_agent_present();
925
Damien Miller95def091999-11-25 00:26:21 +1100926 if (options.forward_agent) {
927 debug("Requesting authentication agent forwarding.");
928 auth_request_forwarding();
929
930 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100931 type = packet_read();
Damien Miller48b03fc2002-01-22 23:11:40 +1100932 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100933 if (type != SSH_SMSG_SUCCESS)
Damien Miller996acd22003-04-09 20:59:48 +1000934 logit("Warning: Remote host denied authentication agent forwarding.");
Damien Miller95def091999-11-25 00:26:21 +1100935 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000936
Damien Miller0bc1bd82000-11-13 22:57:25 +1100937 /* Initiate port forwardings. */
938 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000939
Damien Miller5428f641999-11-25 11:54:57 +1100940 /* If requested, let ssh continue in the background. */
Damien Miller4af51302000-04-16 11:18:38 +1000941 if (fork_after_authentication_flag)
Damien Miller5428f641999-11-25 11:54:57 +1100942 if (daemon(1, 1) < 0)
943 fatal("daemon() failed: %.200s", strerror(errno));
944
945 /*
946 * If a command was specified on the command line, execute the
947 * command now. Otherwise request the server to start a shell.
948 */
Damien Miller95def091999-11-25 00:26:21 +1100949 if (buffer_len(&command) > 0) {
950 int len = buffer_len(&command);
951 if (len > 900)
952 len = 900;
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100953 debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
Damien Miller95def091999-11-25 00:26:21 +1100954 packet_start(SSH_CMSG_EXEC_CMD);
955 packet_put_string(buffer_ptr(&command), buffer_len(&command));
956 packet_send();
957 packet_write_wait();
958 } else {
959 debug("Requesting shell.");
960 packet_start(SSH_CMSG_EXEC_SHELL);
961 packet_send();
962 packet_write_wait();
963 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000964
Damien Miller95def091999-11-25 00:26:21 +1100965 /* Enter the interactive session. */
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000966 return client_loop(have_tty, tty_flag ?
967 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
Damien Miller1383bd82000-04-06 12:32:37 +1000968}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000969
Ben Lindstrombba81212001-06-25 05:01:22 +0000970static void
Damien Miller0e220db2004-06-15 10:34:08 +1000971ssh_subsystem_reply(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000972{
973 int id, len;
974
975 id = packet_get_int();
976 len = buffer_len(&command);
Ben Lindstrom4040fe12001-03-05 06:52:57 +0000977 if (len > 900)
978 len = 900;
Damien Miller48b03fc2002-01-22 23:11:40 +1100979 packet_check_eom();
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000980 if (type == SSH2_MSG_CHANNEL_FAILURE)
981 fatal("Request for subsystem '%.*s' failed on channel %d",
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100982 len, (u_char *)buffer_ptr(&command), id);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000983}
984
Damien Miller2797f7f2002-04-23 21:09:44 +1000985void
Damien Miller509b0102003-12-17 16:33:10 +1100986client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt)
Damien Miller2797f7f2002-04-23 21:09:44 +1000987{
988 int i;
989
990 i = client_global_request_id++;
Damien Miller509b0102003-12-17 16:33:10 +1100991 if (i >= options.num_remote_forwards)
Damien Miller2797f7f2002-04-23 21:09:44 +1000992 return;
Damien Miller2797f7f2002-04-23 21:09:44 +1000993 debug("remote forward %s for: listen %d, connect %s:%d",
994 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
Damien Millerf91ee4c2005-03-01 21:24:33 +1100995 options.remote_forwards[i].listen_port,
996 options.remote_forwards[i].connect_host,
997 options.remote_forwards[i].connect_port);
Damien Miller2797f7f2002-04-23 21:09:44 +1000998 if (type == SSH2_MSG_REQUEST_FAILURE)
Damien Millerf91ee4c2005-03-01 21:24:33 +1100999 logit("Warning: remote port forwarding failed for listen "
1000 "port %d", options.remote_forwards[i].listen_port);
Damien Miller2797f7f2002-04-23 21:09:44 +10001001}
1002
Damien Miller0e220db2004-06-15 10:34:08 +10001003static void
1004ssh_control_listener(void)
1005{
1006 struct sockaddr_un addr;
1007 mode_t old_umask;
Damien Miller07b6ff12004-06-15 11:14:45 +10001008 int addr_len;
1009
Damien Millerd14b1e72005-06-16 13:19:41 +10001010 if (options.control_path == NULL ||
1011 options.control_master == SSHCTL_MASTER_NO)
Damien Miller0e220db2004-06-15 10:34:08 +10001012 return;
1013
Damien Millerd14b1e72005-06-16 13:19:41 +10001014 debug("setting up multiplex master socket");
1015
Damien Miller0e220db2004-06-15 10:34:08 +10001016 memset(&addr, '\0', sizeof(addr));
1017 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001018 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001019 strlen(options.control_path) + 1;
1020
1021 if (strlcpy(addr.sun_path, options.control_path,
1022 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1023 fatal("ControlPath too long");
1024
1025 if ((control_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001026 fatal("%s socket(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001027
1028 old_umask = umask(0177);
Damien Miller07b6ff12004-06-15 11:14:45 +10001029 if (bind(control_fd, (struct sockaddr*)&addr, addr_len) == -1) {
Damien Miller0e220db2004-06-15 10:34:08 +10001030 control_fd = -1;
Damien Miller4f10e252005-05-04 15:33:09 +10001031 if (errno == EINVAL || errno == EADDRINUSE)
Damien Miller0e220db2004-06-15 10:34:08 +10001032 fatal("ControlSocket %s already exists",
1033 options.control_path);
1034 else
Damien Miller15d72a02005-11-05 15:07:33 +11001035 fatal("%s bind(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001036 }
1037 umask(old_umask);
1038
1039 if (listen(control_fd, 64) == -1)
Damien Miller15d72a02005-11-05 15:07:33 +11001040 fatal("%s listen(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001041
1042 set_nonblock(control_fd);
1043}
1044
Ben Lindstromf558cf62001-09-20 23:13:49 +00001045/* request pty/x11/agent/tcpfwd/shell for channel */
Ben Lindstrombba81212001-06-25 05:01:22 +00001046static void
Ben Lindstromf558cf62001-09-20 23:13:49 +00001047ssh_session2_setup(int id, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +10001048{
Damien Miller3756dce2004-06-18 01:17:29 +10001049 extern char **environ;
Damien Miller17e7ed02005-06-17 12:54:33 +10001050 const char *display;
Damien Miller0e220db2004-06-15 10:34:08 +10001051 int interactive = tty_flag;
Damien Miller17e7ed02005-06-17 12:54:33 +10001052
Damien Miller46d38de2005-07-17 17:02:09 +10001053 display = getenv("DISPLAY");
Damien Miller17e7ed02005-06-17 12:54:33 +10001054 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001055 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001056 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001057 client_x11_get_proto(display, options.xauth_location,
1058 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001059 /* Request forwarding with authentication spoofing. */
1060 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +10001061 x11_request_forwarding_with_spoofing(id, display, proto, data);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001062 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001063 /* XXX wait for reply */
1064 }
1065
Damien Miller0bc1bd82000-11-13 22:57:25 +11001066 check_agent_present();
1067 if (options.forward_agent) {
1068 debug("Requesting authentication agent forwarding.");
1069 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1070 packet_send();
1071 }
1072
Damien Miller7b58e802005-12-13 19:33:19 +11001073 if (options.tun_open != SSH_TUNMODE_NO) {
Damien Millerd27b9472005-12-13 19:29:02 +11001074 Channel *c;
1075 int fd;
1076
1077 debug("Requesting tun.");
Damien Miller7b58e802005-12-13 19:33:19 +11001078 if ((fd = tun_open(options.tun_local,
1079 options.tun_open)) >= 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11001080 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1081 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1082 0, "tun", 1);
1083 c->datagram = 1;
Damien Miller598bbc22005-12-31 16:33:36 +11001084#if defined(SSH_TUN_FILTER)
1085 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1086 channel_register_filter(c->self, sys_tun_infilter,
1087 sys_tun_outfilter);
1088#endif
Damien Millerd27b9472005-12-13 19:29:02 +11001089 packet_start(SSH2_MSG_CHANNEL_OPEN);
1090 packet_put_cstring("tun@openssh.com");
1091 packet_put_int(c->self);
1092 packet_put_int(c->local_window_max);
1093 packet_put_int(c->local_maxpacket);
Damien Miller7b58e802005-12-13 19:33:19 +11001094 packet_put_int(options.tun_open);
Damien Millerd27b9472005-12-13 19:29:02 +11001095 packet_put_int(options.tun_remote);
1096 packet_send();
1097 }
1098 }
1099
Damien Miller0e220db2004-06-15 10:34:08 +10001100 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
Damien Miller3756dce2004-06-18 01:17:29 +10001101 NULL, fileno(stdin), &command, environ, &ssh_subsystem_reply);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001102
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001103 packet_set_interactive(interactive);
Damien Miller1383bd82000-04-06 12:32:37 +10001104}
1105
Ben Lindstromf558cf62001-09-20 23:13:49 +00001106/* open new channel for a session */
Ben Lindstrombba81212001-06-25 05:01:22 +00001107static int
Ben Lindstromf558cf62001-09-20 23:13:49 +00001108ssh_session2_open(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001109{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001110 Channel *c;
1111 int window, packetmax, in, out, err;
Damien Millerad833b32000-08-23 10:46:23 +10001112
Damien Millercaf6dd62000-08-29 11:33:50 +11001113 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001114 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001115 } else {
1116 in = dup(STDIN_FILENO);
1117 }
1118 out = dup(STDOUT_FILENO);
1119 err = dup(STDERR_FILENO);
1120
1121 if (in < 0 || out < 0 || err < 0)
1122 fatal("dup() in/out/err failed");
1123
Damien Miller69b69aa2000-10-28 14:19:58 +11001124 /* enable nonblocking unless tty */
1125 if (!isatty(in))
1126 set_nonblock(in);
1127 if (!isatty(out))
1128 set_nonblock(out);
1129 if (!isatty(err))
1130 set_nonblock(err);
1131
Damien Millere4340be2000-09-16 13:29:08 +11001132 window = CHAN_SES_WINDOW_DEFAULT;
1133 packetmax = CHAN_SES_PACKET_DEFAULT;
Damien Miller19a59452002-02-19 15:20:57 +11001134 if (tty_flag) {
1135 window >>= 1;
1136 packetmax >>= 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001137 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001138 c = channel_new(
Damien Miller1383bd82000-04-06 12:32:37 +10001139 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001140 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001141 "client-session", /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001142
Ben Lindstromf558cf62001-09-20 23:13:49 +00001143 debug3("ssh_session2_open: channel_new: %d", c->self);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001144
Ben Lindstrom5ec26452001-06-09 00:18:51 +00001145 channel_send_open(c->self);
Ben Lindstromf558cf62001-09-20 23:13:49 +00001146 if (!no_shell_flag)
Damien Miller0e220db2004-06-15 10:34:08 +10001147 channel_register_confirm(c->self, ssh_session2_setup, NULL);
Damien Miller1383bd82000-04-06 12:32:37 +10001148
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001149 return c->self;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001150}
1151
Ben Lindstrombba81212001-06-25 05:01:22 +00001152static int
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001153ssh_session2(void)
1154{
Ben Lindstromf558cf62001-09-20 23:13:49 +00001155 int id = -1;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001156
1157 /* XXX should be pre-session */
1158 ssh_init_forwarding();
Damien Miller0e220db2004-06-15 10:34:08 +10001159 ssh_control_listener();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001160
Ben Lindstromf558cf62001-09-20 23:13:49 +00001161 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1162 id = ssh_session2_open();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001163
Damien Millerd27b9472005-12-13 19:29:02 +11001164 /* Execute a local command */
1165 if (options.local_command != NULL &&
1166 options.permit_local_command)
1167 ssh_local_cmd(options.local_command);
1168
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001169 /* If requested, let ssh continue in the background. */
1170 if (fork_after_authentication_flag)
1171 if (daemon(1, 1) < 0)
1172 fatal("daemon() failed: %.200s", strerror(errno));
1173
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001174 return client_loop(tty_flag, tty_flag ?
1175 options.escape_char : SSH_ESCAPECHAR_NONE, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001176}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001177
Ben Lindstrombba81212001-06-25 05:01:22 +00001178static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001179load_public_identity_files(void)
1180{
1181 char *filename;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001182 int i = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001183 Key *public;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001184#ifdef SMARTCARD
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001185 Key **keys;
1186
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001187 if (options.smartcard_device != NULL &&
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001188 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1189 (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL ) {
1190 int count = 0;
1191 for (i = 0; keys[i] != NULL; i++) {
1192 count++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001193 memmove(&options.identity_files[1], &options.identity_files[0],
1194 sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
1195 memmove(&options.identity_keys[1], &options.identity_keys[0],
1196 sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1197 options.num_identity_files++;
1198 options.identity_keys[0] = keys[i];
Damien Miller56a0bb02003-06-18 20:28:40 +10001199 options.identity_files[0] = sc_get_key_label(keys[i]);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001200 }
Ben Lindstrom4f054602002-03-26 03:23:00 +00001201 if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1202 options.num_identity_files = SSH_MAX_IDENTITY_FILES;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001203 i = count;
1204 xfree(keys);
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001205 }
Ben Lindstromffce1472001-08-06 21:57:31 +00001206#endif /* SMARTCARD */
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001207 for (; i < options.num_identity_files; i++) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001208 filename = tilde_expand_filename(options.identity_files[i],
1209 original_real_uid);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001210 public = key_load_public(filename, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001211 debug("identity file %s type %d", filename,
1212 public ? public->type : -1);
1213 xfree(options.identity_files[i]);
1214 options.identity_files[i] = filename;
1215 options.identity_keys[i] = public;
1216 }
1217}
Damien Miller0e220db2004-06-15 10:34:08 +10001218
1219static void
1220control_client_sighandler(int signo)
1221{
1222 control_client_terminate = signo;
1223}
1224
1225static void
1226control_client_sigrelay(int signo)
1227{
1228 if (control_server_pid > 1)
1229 kill(control_server_pid, signo);
1230}
1231
Darren Tucker365433f2004-06-22 12:29:23 +10001232static int
1233env_permitted(char *env)
1234{
1235 int i;
1236 char name[1024], *cp;
1237
1238 strlcpy(name, env, sizeof(name));
1239 if ((cp = strchr(name, '=')) == NULL)
1240 return (0);
1241
1242 *cp = '\0';
1243
1244 for (i = 0; i < options.num_send_env; i++)
1245 if (match_pattern(name, options.send_env[i]))
1246 return (1);
1247
1248 return (0);
1249}
1250
Damien Miller0e220db2004-06-15 10:34:08 +10001251static void
1252control_client(const char *path)
1253{
1254 struct sockaddr_un addr;
Darren Tucker39207a42004-11-05 20:19:51 +11001255 int i, r, fd, sock, exitval, num_env, addr_len;
Damien Miller0e220db2004-06-15 10:34:08 +10001256 Buffer m;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001257 char *term;
Damien Miller3756dce2004-06-18 01:17:29 +10001258 extern char **environ;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001259 u_int flags;
Darren Tuckerfc959702004-07-17 16:12:08 +10001260
Damien Millerd14b1e72005-06-16 13:19:41 +10001261 if (mux_command == 0)
1262 mux_command = SSHMUX_COMMAND_OPEN;
1263
1264 switch (options.control_master) {
1265 case SSHCTL_MASTER_AUTO:
1266 case SSHCTL_MASTER_AUTO_ASK:
1267 debug("auto-mux: Trying existing master");
1268 /* FALLTHROUGH */
1269 case SSHCTL_MASTER_NO:
1270 break;
1271 default:
1272 return;
1273 }
1274
Damien Miller0e220db2004-06-15 10:34:08 +10001275 memset(&addr, '\0', sizeof(addr));
1276 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001277 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001278 strlen(path) + 1;
1279
1280 if (strlcpy(addr.sun_path, path,
1281 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1282 fatal("ControlPath too long");
1283
1284 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1285 fatal("%s socket(): %s", __func__, strerror(errno));
1286
Damien Millerdadfd4d2005-05-26 12:07:13 +10001287 if (connect(sock, (struct sockaddr*)&addr, addr_len) == -1) {
Darren Tucker0814d312005-06-01 23:08:51 +10001288 if (mux_command != SSHMUX_COMMAND_OPEN) {
1289 fatal("Control socket connect(%.100s): %s", path,
1290 strerror(errno));
1291 }
Damien Miller538c9b72005-05-26 12:11:28 +10001292 if (errno == ENOENT)
1293 debug("Control socket \"%.100s\" does not exist", path);
1294 else {
1295 error("Control socket connect(%.100s): %s", path,
1296 strerror(errno));
1297 }
Damien Miller13390022005-07-06 09:44:19 +10001298 close(sock);
1299 return;
1300 }
Damien Miller46d38de2005-07-17 17:02:09 +10001301
Damien Miller13390022005-07-06 09:44:19 +10001302 if (stdin_null_flag) {
1303 if ((fd = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1304 fatal("open(/dev/null): %s", strerror(errno));
1305 if (dup2(fd, STDIN_FILENO) == -1)
1306 fatal("dup2: %s", strerror(errno));
1307 if (fd > STDERR_FILENO)
1308 close(fd);
1309 }
Damien Miller46d38de2005-07-17 17:02:09 +10001310
Damien Miller13390022005-07-06 09:44:19 +10001311 term = getenv("TERM");
Darren Tucker7ebfc102004-11-07 20:06:19 +11001312
1313 flags = 0;
1314 if (tty_flag)
1315 flags |= SSHMUX_FLAG_TTY;
1316 if (subsystem_flag)
1317 flags |= SSHMUX_FLAG_SUBSYS;
Damien Miller13390022005-07-06 09:44:19 +10001318 if (options.forward_x11)
1319 flags |= SSHMUX_FLAG_X11_FWD;
1320 if (options.forward_agent)
1321 flags |= SSHMUX_FLAG_AGENT_FWD;
Damien Miller0e220db2004-06-15 10:34:08 +10001322
Damien Miller0e220db2004-06-15 10:34:08 +10001323 buffer_init(&m);
1324
Darren Tucker7ebfc102004-11-07 20:06:19 +11001325 /* Send our command to server */
1326 buffer_put_int(&m, mux_command);
1327 buffer_put_int(&m, flags);
Damien Miller13390022005-07-06 09:44:19 +10001328 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001329 fatal("%s: msg_send", __func__);
1330 buffer_clear(&m);
1331
1332 /* Get authorisation status and PID of controlee */
Damien Miller0e220db2004-06-15 10:34:08 +10001333 if (ssh_msg_recv(sock, &m) == -1)
1334 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001335 if (buffer_get_char(&m) != SSHMUX_VER)
Damien Miller0e220db2004-06-15 10:34:08 +10001336 fatal("%s: wrong version", __func__);
Damien Miller23f07702004-06-18 01:19:03 +10001337 if (buffer_get_int(&m) != 1)
1338 fatal("Connection to master denied");
Damien Miller0e220db2004-06-15 10:34:08 +10001339 control_server_pid = buffer_get_int(&m);
1340
Damien Miller0e220db2004-06-15 10:34:08 +10001341 buffer_clear(&m);
Damien Miller0e220db2004-06-15 10:34:08 +10001342
Darren Tucker7ebfc102004-11-07 20:06:19 +11001343 switch (mux_command) {
1344 case SSHMUX_COMMAND_ALIVE_CHECK:
Darren Tucker47eede72005-03-14 23:08:12 +11001345 fprintf(stderr, "Master running (pid=%d)\r\n",
Darren Tucker7ebfc102004-11-07 20:06:19 +11001346 control_server_pid);
1347 exit(0);
1348 case SSHMUX_COMMAND_TERMINATE:
1349 fprintf(stderr, "Exit request sent.\r\n");
1350 exit(0);
1351 case SSHMUX_COMMAND_OPEN:
1352 /* continue below */
1353 break;
1354 default:
1355 fatal("silly mux_command %d", mux_command);
1356 }
1357
1358 /* SSHMUX_COMMAND_OPEN */
Damien Miller13390022005-07-06 09:44:19 +10001359 buffer_put_cstring(&m, term ? term : "");
Damien Miller0e220db2004-06-15 10:34:08 +10001360 buffer_append(&command, "\0", 1);
1361 buffer_put_cstring(&m, buffer_ptr(&command));
1362
Darren Tucker365433f2004-06-22 12:29:23 +10001363 if (options.num_send_env == 0 || environ == NULL) {
1364 buffer_put_int(&m, 0);
Darren Tuckerfc959702004-07-17 16:12:08 +10001365 } else {
Darren Tucker365433f2004-06-22 12:29:23 +10001366 /* Pass environment */
1367 num_env = 0;
1368 for (i = 0; environ[i] != NULL; i++)
1369 if (env_permitted(environ[i]))
1370 num_env++; /* Count */
Darren Tuckerfc959702004-07-17 16:12:08 +10001371
Darren Tucker365433f2004-06-22 12:29:23 +10001372 buffer_put_int(&m, num_env);
1373
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001374 for (i = 0; environ[i] != NULL && num_env >= 0; i++)
1375 if (env_permitted(environ[i])) {
1376 num_env--;
Darren Tucker365433f2004-06-22 12:29:23 +10001377 buffer_put_cstring(&m, environ[i]);
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001378 }
Darren Tucker365433f2004-06-22 12:29:23 +10001379 }
Damien Miller3756dce2004-06-18 01:17:29 +10001380
Damien Miller13390022005-07-06 09:44:19 +10001381 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Damien Miller0e220db2004-06-15 10:34:08 +10001382 fatal("%s: msg_send", __func__);
1383
1384 mm_send_fd(sock, STDIN_FILENO);
1385 mm_send_fd(sock, STDOUT_FILENO);
1386 mm_send_fd(sock, STDERR_FILENO);
1387
1388 /* Wait for reply, so master has a chance to gather ttymodes */
1389 buffer_clear(&m);
1390 if (ssh_msg_recv(sock, &m) == -1)
1391 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001392 if (buffer_get_char(&m) != SSHMUX_VER)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001393 fatal("%s: wrong version", __func__);
Damien Miller0e220db2004-06-15 10:34:08 +10001394 buffer_free(&m);
1395
Darren Tucker07336da2004-11-05 20:02:16 +11001396 signal(SIGHUP, control_client_sighandler);
Damien Miller0809e232004-06-18 22:20:57 +10001397 signal(SIGINT, control_client_sighandler);
1398 signal(SIGTERM, control_client_sighandler);
1399 signal(SIGWINCH, control_client_sigrelay);
1400
Damien Miller0e220db2004-06-15 10:34:08 +10001401 if (tty_flag)
1402 enter_raw_mode();
1403
1404 /* Stick around until the controlee closes the client_fd */
1405 exitval = 0;
1406 for (;!control_client_terminate;) {
1407 r = read(sock, &exitval, sizeof(exitval));
1408 if (r == 0) {
1409 debug2("Received EOF from master");
1410 break;
1411 }
1412 if (r > 0)
1413 debug2("Received exit status from master %d", exitval);
1414 if (r == -1 && errno != EINTR)
1415 fatal("%s: read %s", __func__, strerror(errno));
1416 }
1417
1418 if (control_client_terminate)
1419 debug2("Exiting on signal %d", control_client_terminate);
1420
1421 close(sock);
1422
1423 leave_raw_mode();
1424
1425 if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
1426 fprintf(stderr, "Connection to master closed.\r\n");
1427
1428 exit(exitval);
1429}