blob: 3384de0120e4bac2365c7f8d7ecaeca2748a7703 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * Ssh client program. This program can be used to log into a remote machine.
6 * The software supports strong authentication, encryption, and forwarding
7 * of X11, TCP/IP, and authentication connections.
8 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
14 *
15 * Copyright (c) 1999 Niels Provos. All rights reserved.
Darren Tucker0a118da2003-10-15 15:54:32 +100016 * Copyright (c) 2000, 2001, 2002, 2003 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110017 *
18 * Modified to work with SSL by Niels Provos <provos@citi.umich.edu>
19 * in Canada (German citizen).
20 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110040 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
42#include "includes.h"
Damien Millercd4223c2006-03-15 11:22:47 +110043RCSID("$OpenBSD: ssh.c,v 1.259 2006/02/08 14:31:30 stevesk Exp $");
44
45#include <sys/resource.h>
Damien Miller03e20032006-03-15 11:16:59 +110046
47#include <paths.h>
Damien Millereba71ba2000-04-29 23:57:08 +100048
49#include <openssl/evp.h>
Damien Miller0bc1bd82000-11-13 22:57:25 +110050#include <openssl/err.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000053#include "ssh1.h"
Damien Miller1383bd82000-04-06 12:32:37 +100054#include "ssh2.h"
55#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000056#include "cipher.h"
57#include "xmalloc.h"
58#include "packet.h"
59#include "buffer.h"
Damien Miller0e220db2004-06-15 10:34:08 +100060#include "bufaux.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000061#include "channels.h"
Damien Millereba71ba2000-04-29 23:57:08 +100062#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100063#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100064#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000065#include "pathnames.h"
Damien Miller0e220db2004-06-15 10:34:08 +100066#include "dispatch.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +000067#include "clientloop.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000068#include "log.h"
69#include "readconf.h"
70#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000071#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000072#include "kex.h"
73#include "mac.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +100074#include "sshpty.h"
Darren Tucker46bc0752004-05-02 22:11:30 +100075#include "match.h"
Damien Miller0e220db2004-06-15 10:34:08 +100076#include "msg.h"
77#include "monitor_fdpass.h"
Darren Tucker25f60a72004-08-15 17:23:34 +100078#include "uidswap.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079
Ben Lindstromc5b68002001-07-04 04:52:03 +000080#ifdef SMARTCARD
Ben Lindstromc5b68002001-07-04 04:52:03 +000081#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000082#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +000083
Damien Miller3f905871999-11-15 17:10:57 +110084extern char *__progname;
Damien Miller3f905871999-11-15 17:10:57 +110085
Damien Miller95def091999-11-25 00:26:21 +110086/* Flag indicating whether debug mode is on. This can be set on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087int debug_flag = 0;
88
Damien Miller78928792000-04-12 20:17:38 +100089/* Flag indicating whether a tty should be allocated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090int tty_flag = 0;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +000091int no_tty_flag = 0;
92int force_tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
Damien Miller1383bd82000-04-06 12:32:37 +100094/* don't exec a shell */
95int no_shell_flag = 0;
Damien Miller1383bd82000-04-06 12:32:37 +100096
Damien Miller5428f641999-11-25 11:54:57 +110097/*
98 * Flag indicating that nothing should be read from stdin. This can be set
99 * on the command line.
100 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101int stdin_null_flag = 0;
102
Damien Miller5428f641999-11-25 11:54:57 +1100103/*
104 * Flag indicating that ssh should fork after authentication. This is useful
Ben Lindstromf666fec2002-06-06 19:51:58 +0000105 * so that the passphrase can be entered manually, and then ssh goes to the
Damien Miller5428f641999-11-25 11:54:57 +1100106 * background.
107 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108int fork_after_authentication_flag = 0;
109
Damien Miller5428f641999-11-25 11:54:57 +1100110/*
111 * General data structure for command line options and options configurable
112 * in configuration files. See readconf.h.
113 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114Options options;
115
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000116/* optional user configfile */
117char *config = NULL;
118
Damien Miller5428f641999-11-25 11:54:57 +1100119/*
120 * Name of the host we are connecting to. This is the name given on the
121 * command line, or the HostName specified for the user-supplied name in a
122 * configuration file.
123 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124char *host;
125
126/* socket address the host resolves to */
Damien Miller34132e52000-01-14 15:45:46 +1100127struct sockaddr_storage hostaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000129/* Private host keys. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000130Sensitive sensitive_data;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131
132/* Original real UID. */
133uid_t original_real_uid;
Ben Lindstromf9c48842002-06-11 16:37:51 +0000134uid_t original_effective_uid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135
Damien Miller1383bd82000-04-06 12:32:37 +1000136/* command to be executed */
137Buffer command;
138
Damien Miller832562e2001-01-30 09:30:01 +1100139/* Should we execute a command or invoke a subsystem? */
140int subsystem_flag = 0;
141
Damien Miller2797f7f2002-04-23 21:09:44 +1000142/* # of replies received for global requests */
143static int client_global_request_id = 0;
144
Damien Miller8c4e18a2002-09-19 12:05:02 +1000145/* pid of proxycommand child process */
146pid_t proxy_command_pid = 0;
147
Damien Miller0e220db2004-06-15 10:34:08 +1000148/* fd to control socket */
149int control_fd = -1;
150
Darren Tucker7ebfc102004-11-07 20:06:19 +1100151/* Multiplexing control command */
Darren Tucker0814d312005-06-01 23:08:51 +1000152static u_int mux_command = 0;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100153
Damien Miller0e220db2004-06-15 10:34:08 +1000154/* Only used in control client mode */
155volatile sig_atomic_t control_client_terminate = 0;
156u_int control_server_pid = 0;
157
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158/* Prints a help message to the user. This function never returns. */
159
Ben Lindstrombba81212001-06-25 05:01:22 +0000160static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000161usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000162{
Damien Miller50955102004-03-22 09:34:58 +1100163 fprintf(stderr,
Darren Tucker9c6bf322004-12-03 14:10:19 +1100164"usage: ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n"
Darren Tucker895d6982005-10-03 18:18:05 +1000165" [-D [bind_address:]port] [-e escape_char] [-F configfile]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100166" [-i identity_file] [-L [bind_address:]port:host:hostport]\n"
Damien Millerf91ee4c2005-03-01 21:24:33 +1100167" [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100168" [-R [bind_address:]port:host:hostport] [-S ctl_path]\n"
Damien Millerd27b9472005-12-13 19:29:02 +1100169" [-w tunnel:tunnel] [user@]hostname [command]\n"
Damien Miller50955102004-03-22 09:34:58 +1100170 );
Darren Tuckere9a9b712005-12-20 16:15:51 +1100171 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000172}
173
Ben Lindstrombba81212001-06-25 05:01:22 +0000174static int ssh_session(void);
175static int ssh_session2(void);
176static void load_public_identity_files(void);
Damien Miller0e220db2004-06-15 10:34:08 +1000177static void control_client(const char *path);
Damien Miller1383bd82000-04-06 12:32:37 +1000178
Damien Miller95def091999-11-25 00:26:21 +1100179/*
180 * Main program for the ssh client.
181 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000182int
183main(int ac, char **av)
184{
Ben Lindstrom24157572002-06-12 16:09:39 +0000185 int i, opt, exit_status;
Damien Miller9836cf82003-12-17 16:30:06 +1100186 char *p, *cp, *line, buf[256];
Damien Miller95def091999-11-25 00:26:21 +1100187 struct stat st;
Ben Lindstrom086cf212001-03-05 05:56:40 +0000188 struct passwd *pw;
Damien Miller1383bd82000-04-06 12:32:37 +1000189 int dummy;
Ben Lindstrom5ccf63a2001-09-24 20:00:10 +0000190 extern int optind, optreset;
Damien Miller4f8e6692001-07-14 13:22:53 +1000191 extern char *optarg;
Damien Miller9651fe62005-06-26 08:55:25 +1000192 struct servent *sp;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100193 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000194
Darren Tuckerce321d82005-10-03 18:11:24 +1000195 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
196 sanitise_stdfd();
197
Damien Miller59d3d5b2003-08-22 09:34:41 +1000198 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000199 init_rng();
200
Damien Miller5428f641999-11-25 11:54:57 +1100201 /*
202 * Save the original real uid. It will be needed later (uid-swapping
203 * may clobber the real uid).
204 */
Damien Miller95def091999-11-25 00:26:21 +1100205 original_real_uid = getuid();
206 original_effective_uid = geteuid();
Damien Millera8e06ce2003-11-21 23:48:55 +1100207
Damien Miller50b9a602002-09-04 16:50:06 +1000208 /*
209 * Use uid-swapping to give up root privileges for the duration of
210 * option processing. We will re-instantiate the rights when we are
211 * ready to create the privileged port, and will permanently drop
212 * them when the port has been created (actually, when the connection
213 * has been made, as we may need to create the port several times).
214 */
215 PRIV_END;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000216
Damien Miller05dd7952000-10-01 00:42:48 +1100217#ifdef HAVE_SETRLIMIT
Damien Miller95def091999-11-25 00:26:21 +1100218 /* If we are installed setuid root be careful to not drop core. */
219 if (original_real_uid != original_effective_uid) {
220 struct rlimit rlim;
221 rlim.rlim_cur = rlim.rlim_max = 0;
222 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
223 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000224 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100225#endif
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000226 /* Get user data. */
227 pw = getpwuid(original_real_uid);
228 if (!pw) {
Damien Miller996acd22003-04-09 20:59:48 +1000229 logit("You don't exist, go away!");
Darren Tuckere9a9b712005-12-20 16:15:51 +1100230 exit(255);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000231 }
232 /* Take a copy of the returned structure. */
233 pw = pwcopy(pw);
234
Damien Miller5428f641999-11-25 11:54:57 +1100235 /*
Damien Miller5428f641999-11-25 11:54:57 +1100236 * Set our umask to something reasonable, as some files are created
237 * with the default umask. This will make them world-readable but
238 * writable only by the owner, which is ok for all files for which we
239 * don't set the modes explicitly.
240 */
Damien Miller95def091999-11-25 00:26:21 +1100241 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000242
Damien Miller95def091999-11-25 00:26:21 +1100243 /* Initialize option structure to indicate that no values have been set. */
244 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000245
Damien Miller95def091999-11-25 00:26:21 +1100246 /* Parse command-line arguments. */
247 host = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248
Damien Millerf4614452001-07-14 12:18:10 +1000249again:
250 while ((opt = getopt(ac, av,
Damien Millerd27b9472005-12-13 19:29:02 +1100251 "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 +1100252 switch (opt) {
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000253 case '1':
254 options.protocol = SSH_PROTO_1;
255 break;
Damien Miller4af51302000-04-16 11:18:38 +1000256 case '2':
257 options.protocol = SSH_PROTO_2;
258 break;
Damien Miller34132e52000-01-14 15:45:46 +1100259 case '4':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000260 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100261 break;
Damien Miller34132e52000-01-14 15:45:46 +1100262 case '6':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000263 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100264 break;
Damien Miller95def091999-11-25 00:26:21 +1100265 case 'n':
266 stdin_null_flag = 1;
267 break;
Damien Miller95def091999-11-25 00:26:21 +1100268 case 'f':
269 fork_after_authentication_flag = 1;
270 stdin_null_flag = 1;
271 break;
Damien Miller95def091999-11-25 00:26:21 +1100272 case 'x':
273 options.forward_x11 = 0;
274 break;
Damien Miller95def091999-11-25 00:26:21 +1100275 case 'X':
276 options.forward_x11 = 1;
277 break;
Darren Tucker0a118da2003-10-15 15:54:32 +1000278 case 'Y':
279 options.forward_x11 = 1;
280 options.forward_x11_trusted = 1;
281 break;
Damien Miller95def091999-11-25 00:26:21 +1100282 case 'g':
283 options.gateway_ports = 1;
284 break;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100285 case 'O':
286 if (strcmp(optarg, "check") == 0)
287 mux_command = SSHMUX_COMMAND_ALIVE_CHECK;
288 else if (strcmp(optarg, "exit") == 0)
289 mux_command = SSHMUX_COMMAND_TERMINATE;
290 else
291 fatal("Invalid multiplex command.");
292 break;
Damien Miller147bba32002-09-04 16:46:06 +1000293 case 'P': /* deprecated */
Damien Miller95def091999-11-25 00:26:21 +1100294 options.use_privileged_port = 0;
295 break;
Damien Miller95def091999-11-25 00:26:21 +1100296 case 'a':
297 options.forward_agent = 0;
298 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000299 case 'A':
300 options.forward_agent = 1;
301 break;
Damien Miller95def091999-11-25 00:26:21 +1100302 case 'k':
Damien Millere0113cc2003-11-24 13:10:09 +1100303 options.gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100304 break;
Damien Miller95def091999-11-25 00:26:21 +1100305 case 'i':
306 if (stat(optarg, &st) < 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000307 fprintf(stderr, "Warning: Identity file %s "
Damien Miller3eb48b62005-03-01 21:15:46 +1100308 "not accessible: %s.\n", optarg,
309 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100310 break;
311 }
Damien Millerf4614452001-07-14 12:18:10 +1000312 if (options.num_identity_files >=
313 SSH_MAX_IDENTITY_FILES)
314 fatal("Too many identity files specified "
315 "(max %d)", SSH_MAX_IDENTITY_FILES);
316 options.identity_files[options.num_identity_files++] =
317 xstrdup(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100318 break;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000319 case 'I':
320#ifdef SMARTCARD
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000321 options.smartcard_device = xstrdup(optarg);
Ben Lindstrombcc18082001-08-06 21:59:25 +0000322#else
Ben Lindstromc5b68002001-07-04 04:52:03 +0000323 fprintf(stderr, "no support for smartcards.\n");
Ben Lindstrombcc18082001-08-06 21:59:25 +0000324#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000325 break;
Damien Miller95def091999-11-25 00:26:21 +1100326 case 't':
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000327 if (tty_flag)
328 force_tty_flag = 1;
Damien Miller95def091999-11-25 00:26:21 +1100329 tty_flag = 1;
330 break;
Damien Miller95def091999-11-25 00:26:21 +1100331 case 'v':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000332 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100333 debug_flag = 1;
334 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000335 } else {
336 if (options.log_level < SYSLOG_LEVEL_DEBUG3)
337 options.log_level++;
Damien Millere4340be2000-09-16 13:29:08 +1100338 break;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000339 }
Darren Tuckere9bf9842004-11-05 20:05:32 +1100340 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100341 case 'V':
Damien Miller0c889cd2004-03-22 09:36:00 +1100342 fprintf(stderr, "%s, %s\n",
Damien Miller2aa6d3c2004-09-12 16:53:04 +1000343 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
Damien Miller95def091999-11-25 00:26:21 +1100344 if (opt == 'V')
345 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100346 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100347 case 'w':
Damien Miller7b58e802005-12-13 19:33:19 +1100348 if (options.tun_open == -1)
349 options.tun_open = SSH_TUNMODE_DEFAULT;
Damien Millerd27b9472005-12-13 19:29:02 +1100350 options.tun_local = a2tun(optarg, &options.tun_remote);
Damien Miller7b58e802005-12-13 19:33:19 +1100351 if (options.tun_local == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100352 fprintf(stderr, "Bad tun device '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100353 exit(255);
Damien Millerd27b9472005-12-13 19:29:02 +1100354 }
355 break;
Damien Miller95def091999-11-25 00:26:21 +1100356 case 'q':
357 options.log_level = SYSLOG_LEVEL_QUIET;
358 break;
Damien Miller95def091999-11-25 00:26:21 +1100359 case 'e':
360 if (optarg[0] == '^' && optarg[2] == 0 &&
Damien Millerf4614452001-07-14 12:18:10 +1000361 (u_char) optarg[1] >= 64 &&
362 (u_char) optarg[1] < 128)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000363 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100364 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000365 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100366 else if (strcmp(optarg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000367 options.escape_char = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100368 else {
Damien Millerf4614452001-07-14 12:18:10 +1000369 fprintf(stderr, "Bad escape character '%s'.\n",
370 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100371 exit(255);
Damien Miller95def091999-11-25 00:26:21 +1100372 }
373 break;
Damien Miller95def091999-11-25 00:26:21 +1100374 case 'c':
Damien Millereba71ba2000-04-29 23:57:08 +1000375 if (ciphers_valid(optarg)) {
376 /* SSH2 only */
377 options.ciphers = xstrdup(optarg);
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000378 options.cipher = SSH_CIPHER_INVALID;
Damien Millereba71ba2000-04-29 23:57:08 +1000379 } else {
380 /* SSH1 only */
Damien Millere39cacc2000-11-29 12:18:44 +1100381 options.cipher = cipher_number(optarg);
382 if (options.cipher == -1) {
Damien Millerf4614452001-07-14 12:18:10 +1000383 fprintf(stderr,
384 "Unknown cipher type '%s'\n",
385 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100386 exit(255);
Damien Millereba71ba2000-04-29 23:57:08 +1000387 }
Damien Millerf4614452001-07-14 12:18:10 +1000388 if (options.cipher == SSH_CIPHER_3DES)
Damien Millere39cacc2000-11-29 12:18:44 +1100389 options.ciphers = "3des-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000390 else if (options.cipher == SSH_CIPHER_BLOWFISH)
Damien Millere39cacc2000-11-29 12:18:44 +1100391 options.ciphers = "blowfish-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000392 else
Damien Millere39cacc2000-11-29 12:18:44 +1100393 options.ciphers = (char *)-1;
Damien Miller95def091999-11-25 00:26:21 +1100394 }
395 break;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000396 case 'm':
397 if (mac_valid(optarg))
398 options.macs = xstrdup(optarg);
399 else {
Damien Millerf4614452001-07-14 12:18:10 +1000400 fprintf(stderr, "Unknown mac type '%s'\n",
401 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100402 exit(255);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000403 }
404 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000405 case 'M':
Damien Millerd14b1e72005-06-16 13:19:41 +1000406 if (options.control_master == SSHCTL_MASTER_YES)
407 options.control_master = SSHCTL_MASTER_ASK;
408 else
409 options.control_master = SSHCTL_MASTER_YES;
Damien Miller0e220db2004-06-15 10:34:08 +1000410 break;
Damien Miller95def091999-11-25 00:26:21 +1100411 case 'p':
Ben Lindstrom19066a12001-04-12 23:39:26 +0000412 options.port = a2port(optarg);
413 if (options.port == 0) {
Ben Lindstrom146edb92001-04-11 23:06:28 +0000414 fprintf(stderr, "Bad port '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100415 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000416 }
Damien Miller95def091999-11-25 00:26:21 +1100417 break;
Damien Miller95def091999-11-25 00:26:21 +1100418 case 'l':
419 options.user = optarg;
420 break;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000421
Damien Miller95def091999-11-25 00:26:21 +1100422 case 'L':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100423 if (parse_forward(&fwd, optarg))
424 add_local_forward(&options, &fwd);
425 else {
Damien Millerf4614452001-07-14 12:18:10 +1000426 fprintf(stderr,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100427 "Bad local forwarding specification '%s'\n",
Damien Millerf4614452001-07-14 12:18:10 +1000428 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100429 exit(255);
Ben Lindstrom1a174712001-09-12 17:56:15 +0000430 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100431 break;
432
433 case 'R':
434 if (parse_forward(&fwd, optarg)) {
435 add_remote_forward(&options, &fwd);
436 } else {
437 fprintf(stderr,
438 "Bad remote forwarding specification "
439 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100440 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100441 }
Damien Miller95def091999-11-25 00:26:21 +1100442 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000443
444 case 'D':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100445 cp = p = xstrdup(optarg);
446 memset(&fwd, '\0', sizeof(fwd));
447 fwd.connect_host = "socks";
448 if ((fwd.listen_host = hpdelim(&cp)) == NULL) {
449 fprintf(stderr, "Bad dynamic forwarding "
450 "specification '%.100s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100451 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100452 }
453 if (cp != NULL) {
454 fwd.listen_port = a2port(cp);
455 fwd.listen_host = cleanhostname(fwd.listen_host);
456 } else {
457 fwd.listen_port = a2port(fwd.listen_host);
Damien Millerbe1045d2005-08-12 22:10:56 +1000458 fwd.listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100459 }
460
461 if (fwd.listen_port == 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000462 fprintf(stderr, "Bad dynamic port '%s'\n",
463 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100464 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000465 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100466 add_local_forward(&options, &fwd);
467 xfree(p);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000468 break;
469
Damien Miller95def091999-11-25 00:26:21 +1100470 case 'C':
471 options.compression = 1;
472 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000473 case 'N':
474 no_shell_flag = 1;
475 no_tty_flag = 1;
476 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000477 case 'T':
478 no_tty_flag = 1;
479 break;
Damien Miller95def091999-11-25 00:26:21 +1100480 case 'o':
481 dummy = 1;
Damien Miller9836cf82003-12-17 16:30:06 +1100482 line = xstrdup(optarg);
Damien Millerf4614452001-07-14 12:18:10 +1000483 if (process_config_line(&options, host ? host : "",
Damien Miller9836cf82003-12-17 16:30:06 +1100484 line, "command-line", 0, &dummy) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100485 exit(255);
Damien Miller9836cf82003-12-17 16:30:06 +1100486 xfree(line);
Damien Miller95def091999-11-25 00:26:21 +1100487 break;
Damien Miller832562e2001-01-30 09:30:01 +1100488 case 's':
489 subsystem_flag = 1;
490 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000491 case 'S':
492 if (options.control_path != NULL)
493 free(options.control_path);
494 options.control_path = xstrdup(optarg);
Damien Miller0e220db2004-06-15 10:34:08 +1000495 break;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000496 case 'b':
497 options.bind_address = optarg;
498 break;
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000499 case 'F':
500 config = optarg;
501 break;
Damien Miller95def091999-11-25 00:26:21 +1100502 default:
503 usage();
504 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000505 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000506
Damien Millerf4614452001-07-14 12:18:10 +1000507 ac -= optind;
508 av += optind;
509
510 if (ac > 0 && !host && **av != '-') {
Ben Lindstromc276c122002-12-23 02:14:51 +0000511 if (strrchr(*av, '@')) {
Damien Millerf4614452001-07-14 12:18:10 +1000512 p = xstrdup(*av);
Ben Lindstromc276c122002-12-23 02:14:51 +0000513 cp = strrchr(p, '@');
Damien Millerf4614452001-07-14 12:18:10 +1000514 if (cp == NULL || cp == p)
515 usage();
516 options.user = p;
517 *cp = '\0';
518 host = ++cp;
519 } else
520 host = *av;
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000521 if (ac > 1) {
522 optind = optreset = 1;
Damien Millerf4614452001-07-14 12:18:10 +1000523 goto again;
524 }
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000525 ac--, av++;
Damien Millerf4614452001-07-14 12:18:10 +1000526 }
527
Damien Miller95def091999-11-25 00:26:21 +1100528 /* Check that we got a host name. */
529 if (!host)
530 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000531
Damien Millercb5e44a2000-09-29 12:12:36 +1100532 SSLeay_add_all_algorithms();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100533 ERR_load_crypto_strings();
Damien Millercb5e44a2000-09-29 12:12:36 +1100534
Damien Miller95def091999-11-25 00:26:21 +1100535 /* Initialize the command to execute on remote host. */
536 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000537
Damien Miller5428f641999-11-25 11:54:57 +1100538 /*
539 * Save the command to execute on the remote host in a buffer. There
540 * is no limit on the length of the command, except by the maximum
541 * packet size. Also sets the tty flag if there is no command.
542 */
Damien Millerf4614452001-07-14 12:18:10 +1000543 if (!ac) {
Damien Miller95def091999-11-25 00:26:21 +1100544 /* No command specified - execute shell on a tty. */
545 tty_flag = 1;
Damien Miller832562e2001-01-30 09:30:01 +1100546 if (subsystem_flag) {
Damien Millerf4614452001-07-14 12:18:10 +1000547 fprintf(stderr,
548 "You must specify a subsystem to invoke.\n");
Damien Miller832562e2001-01-30 09:30:01 +1100549 usage();
550 }
Damien Miller95def091999-11-25 00:26:21 +1100551 } else {
Damien Millerf4614452001-07-14 12:18:10 +1000552 /* A command has been specified. Store it into the buffer. */
553 for (i = 0; i < ac; i++) {
554 if (i)
Damien Miller95def091999-11-25 00:26:21 +1100555 buffer_append(&command, " ", 1);
556 buffer_append(&command, av[i], strlen(av[i]));
557 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000558 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000559
Damien Miller95def091999-11-25 00:26:21 +1100560 /* Cannot fork to background if no command. */
Damien Millercaf6dd62000-08-29 11:33:50 +1100561 if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
Damien Miller95def091999-11-25 00:26:21 +1100562 fatal("Cannot fork into background without a command to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000563
Damien Miller95def091999-11-25 00:26:21 +1100564 /* Allocate a tty by default if no command specified. */
565 if (buffer_len(&command) == 0)
566 tty_flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000567
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000568 /* Force no tty */
Ben Lindstromc72745a2000-12-02 19:03:54 +0000569 if (no_tty_flag)
570 tty_flag = 0;
Damien Miller95def091999-11-25 00:26:21 +1100571 /* Do not allocate a tty if stdin is not a tty. */
Damien Millerddee5752005-05-26 12:05:05 +1000572 if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100573 if (tty_flag)
Damien Miller996acd22003-04-09 20:59:48 +1000574 logit("Pseudo-terminal will not be allocated because stdin is not a terminal.");
Damien Miller95def091999-11-25 00:26:21 +1100575 tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000576 }
Damien Miller1383bd82000-04-06 12:32:37 +1000577
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000578 /*
579 * Initialize "log" output. Since we are the client all output
580 * actually goes to stderr.
581 */
Ben Lindstrom2b646522001-04-12 16:16:57 +0000582 log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
583 SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000584
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000585 /*
586 * Read per-user configuration file. Ignore the system wide config
587 * file if the user specifies a config file on the command line.
588 */
589 if (config != NULL) {
Damien Miller914420f2004-04-20 20:14:07 +1000590 if (!read_config_file(config, host, &options, 0))
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000591 fatal("Can't open user config file %.100s: "
592 "%.100s", config, strerror(errno));
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000593 } else {
594 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
595 _PATH_SSH_USER_CONFFILE);
Damien Miller57a44762004-04-20 20:11:57 +1000596 (void)read_config_file(buf, host, &options, 1);
Ben Lindstrom83f07d12001-10-03 17:22:29 +0000597
598 /* Read systemwide configuration file after use config. */
Darren Tuckerfc959702004-07-17 16:12:08 +1000599 (void)read_config_file(_PATH_HOST_CONFIG_FILE, host,
Damien Miller57a44762004-04-20 20:11:57 +1000600 &options, 0);
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000601 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000602
Damien Miller95def091999-11-25 00:26:21 +1100603 /* Fill configuration defaults. */
604 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000605
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000606 channel_set_af(options.address_family);
607
Damien Miller95def091999-11-25 00:26:21 +1100608 /* reinit */
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000609 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000610
Damien Miller60bc5172001-03-19 09:38:15 +1100611 seed_rng();
612
Damien Miller95def091999-11-25 00:26:21 +1100613 if (options.user == NULL)
614 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000615
Damien Miller95def091999-11-25 00:26:21 +1100616 if (options.hostname != NULL)
617 host = options.hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000618
Darren Tucker3f521e22003-07-03 16:20:42 +1000619 /* force lowercase for hostkey matching */
620 if (options.host_key_alias != NULL) {
621 for (p = options.host_key_alias; *p; p++)
622 if (isupper(*p))
623 *p = tolower(*p);
624 }
625
Damien Miller7c71cc72005-06-26 08:56:31 +1000626 /* Get default port if port has not been set. */
627 if (options.port == 0) {
628 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
629 options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
630 }
631
Damien Miller9f1e33a2003-02-24 11:57:32 +1100632 if (options.proxy_command != NULL &&
633 strcmp(options.proxy_command, "none") == 0)
634 options.proxy_command = NULL;
Damien Miller8f74c8f2005-06-26 08:56:03 +1000635 if (options.control_path != NULL &&
636 strcmp(options.control_path, "none") == 0)
637 options.control_path = NULL;
Damien Miller9f1e33a2003-02-24 11:57:32 +1100638
Damien Miller0e220db2004-06-15 10:34:08 +1000639 if (options.control_path != NULL) {
Damien Miller6476cad2005-06-16 13:18:34 +1000640 snprintf(buf, sizeof(buf), "%d", options.port);
641 cp = tilde_expand_filename(options.control_path,
642 original_real_uid);
643 options.control_path = percent_expand(cp, "p", buf, "h", host,
644 "r", options.user, (char *)NULL);
645 xfree(cp);
Damien Miller0e220db2004-06-15 10:34:08 +1000646 }
Darren Tucker0814d312005-06-01 23:08:51 +1000647 if (mux_command != 0 && options.control_path == NULL)
648 fatal("No ControlPath specified for \"-O\" command");
Damien Millerd14b1e72005-06-16 13:19:41 +1000649 if (options.control_path != NULL)
Damien Millerdadfd4d2005-05-26 12:07:13 +1000650 control_client(options.control_path);
Damien Miller0e220db2004-06-15 10:34:08 +1000651
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000652 /* Open a connection to the remote host. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000653 if (ssh_connect(host, &hostaddr, options.port,
654 options.address_family, options.connection_attempts,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000655#ifdef HAVE_CYGWIN
656 options.use_privileged_port,
657#else
Ben Lindstromf9c48842002-06-11 16:37:51 +0000658 original_effective_uid == 0 && options.use_privileged_port,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000659#endif
Ben Lindstromda394ca2002-06-12 16:11:12 +0000660 options.proxy_command) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100661 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000662
Damien Miller5428f641999-11-25 11:54:57 +1100663 /*
664 * If we successfully made the connection, load the host private key
665 * in case we will need it later for combined rsa-rhosts
666 * authentication. This must be done before releasing extra
667 * privileges, because the file is only readable by root.
Ben Lindstrom9e5bb572002-06-06 19:58:27 +0000668 * If we cannot access the private keys, load the public keys
669 * instead and try to execute the ssh-keysign helper instead.
Damien Miller5428f641999-11-25 11:54:57 +1100670 */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000671 sensitive_data.nkeys = 0;
672 sensitive_data.keys = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000673 sensitive_data.external_keysign = 0;
Ben Lindstrom24157572002-06-12 16:09:39 +0000674 if (options.rhosts_rsa_authentication ||
675 options.hostbased_authentication) {
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000676 sensitive_data.nkeys = 3;
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000677 sensitive_data.keys = xmalloc(sensitive_data.nkeys *
678 sizeof(Key));
Ben Lindstromf9c48842002-06-11 16:37:51 +0000679
680 PRIV_START;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000681 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
Ben Lindstromd0fca422001-03-26 13:44:06 +0000682 _PATH_HOST_KEY_FILE, "", NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000683 sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
684 _PATH_HOST_DSA_KEY_FILE, "", NULL);
685 sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
686 _PATH_HOST_RSA_KEY_FILE, "", NULL);
Ben Lindstromf9c48842002-06-11 16:37:51 +0000687 PRIV_END;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000688
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +0000689 if (options.hostbased_authentication == 1 &&
690 sensitive_data.keys[0] == NULL &&
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000691 sensitive_data.keys[1] == NULL &&
692 sensitive_data.keys[2] == NULL) {
693 sensitive_data.keys[1] = key_load_public(
694 _PATH_HOST_DSA_KEY_FILE, NULL);
695 sensitive_data.keys[2] = key_load_public(
696 _PATH_HOST_RSA_KEY_FILE, NULL);
697 sensitive_data.external_keysign = 1;
698 }
Damien Miller95def091999-11-25 00:26:21 +1100699 }
Damien Miller5428f641999-11-25 11:54:57 +1100700 /*
701 * Get rid of any extra privileges that we may have. We will no
702 * longer need them. Also, extra privileges could make it very hard
703 * to read identity files and other non-world-readable files from the
704 * user's home directory if it happens to be on a NFS volume where
705 * root is mapped to nobody.
706 */
Darren Tucker25f60a72004-08-15 17:23:34 +1000707 if (original_effective_uid == 0) {
708 PRIV_START;
709 permanently_set_uid(pw);
710 }
Damien Miller95def091999-11-25 00:26:21 +1100711
Damien Miller5428f641999-11-25 11:54:57 +1100712 /*
713 * Now that we are back to our own permissions, create ~/.ssh
Damien Miller788f2122005-11-05 15:14:59 +1100714 * directory if it doesn't already exist.
Damien Miller5428f641999-11-25 11:54:57 +1100715 */
Ben Lindstrom930b14a2001-08-15 23:19:21 +0000716 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 +1100717 if (stat(buf, &st) < 0)
Damien Millerbe484b52000-07-15 14:14:16 +1000718 if (mkdir(buf, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100719 error("Could not create directory '%.200s'.", buf);
720
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000721 /* load options.identity_files */
722 load_public_identity_files();
723
724 /* Expand ~ in known host file names. */
725 /* XXX mem-leaks: */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100726 options.system_hostfile =
727 tilde_expand_filename(options.system_hostfile, original_real_uid);
728 options.user_hostfile =
729 tilde_expand_filename(options.user_hostfile, original_real_uid);
730 options.system_hostfile2 =
731 tilde_expand_filename(options.system_hostfile2, original_real_uid);
732 options.user_hostfile2 =
733 tilde_expand_filename(options.user_hostfile2, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100734
Damien Miller07cd5892001-11-12 10:52:03 +1100735 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
736
Damien Miller95def091999-11-25 00:26:21 +1100737 /* Log into the remote system. This never returns if the login fails. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000738 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, pw);
Damien Miller95def091999-11-25 00:26:21 +1100739
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000740 /* We no longer need the private host keys. Clear them now. */
741 if (sensitive_data.nkeys != 0) {
742 for (i = 0; i < sensitive_data.nkeys; i++) {
743 if (sensitive_data.keys[i] != NULL) {
744 /* Destroys contents safely */
745 debug3("clear hostkey %d", i);
746 key_free(sensitive_data.keys[i]);
747 sensitive_data.keys[i] = NULL;
748 }
749 }
750 xfree(sensitive_data.keys);
751 }
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +0000752 for (i = 0; i < options.num_identity_files; i++) {
753 if (options.identity_files[i]) {
754 xfree(options.identity_files[i]);
755 options.identity_files[i] = NULL;
756 }
757 if (options.identity_keys[i]) {
758 key_free(options.identity_keys[i]);
759 options.identity_keys[i] = NULL;
760 }
761 }
Damien Miller95def091999-11-25 00:26:21 +1100762
Damien Miller1383bd82000-04-06 12:32:37 +1000763 exit_status = compat20 ? ssh_session2() : ssh_session();
764 packet_close();
Damien Miller8c4e18a2002-09-19 12:05:02 +1000765
Damien Miller0e220db2004-06-15 10:34:08 +1000766 if (options.control_path != NULL && control_fd != -1)
767 unlink(options.control_path);
768
Damien Miller8c4e18a2002-09-19 12:05:02 +1000769 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100770 * Send SIGHUP to proxy command if used. We don't wait() in
Damien Miller8c4e18a2002-09-19 12:05:02 +1000771 * case it hangs and instead rely on init to reap the child
772 */
773 if (proxy_command_pid > 1)
774 kill(proxy_command_pid, SIGHUP);
775
Damien Miller1383bd82000-04-06 12:32:37 +1000776 return exit_status;
777}
778
Ben Lindstrombba81212001-06-25 05:01:22 +0000779static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100780ssh_init_forwarding(void)
781{
Kevin Steves12057502001-02-05 14:54:34 +0000782 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100783 int i;
Kevin Steves12057502001-02-05 14:54:34 +0000784
Damien Miller0bc1bd82000-11-13 22:57:25 +1100785 /* Initiate local TCP/IP port forwardings. */
786 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100787 debug("Local connections to %.200s:%d forwarded to remote "
788 "address %.200s:%d",
Darren Tucker47eede72005-03-14 23:08:12 +1100789 (options.local_forwards[i].listen_host == NULL) ?
790 (options.gateway_ports ? "*" : "LOCALHOST") :
Damien Millerf91ee4c2005-03-01 21:24:33 +1100791 options.local_forwards[i].listen_host,
792 options.local_forwards[i].listen_port,
793 options.local_forwards[i].connect_host,
794 options.local_forwards[i].connect_port);
Damien Millerb16461c2002-01-22 23:29:22 +1100795 success += channel_setup_local_fwd_listener(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100796 options.local_forwards[i].listen_host,
797 options.local_forwards[i].listen_port,
798 options.local_forwards[i].connect_host,
799 options.local_forwards[i].connect_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100800 options.gateway_ports);
801 }
Kevin Steves12057502001-02-05 14:54:34 +0000802 if (i > 0 && success == 0)
803 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100804
805 /* Initiate remote TCP/IP port forwardings. */
806 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100807 debug("Remote connections from %.200s:%d forwarded to "
808 "local address %.200s:%d",
Damien Miller46d38de2005-07-17 17:02:09 +1000809 (options.remote_forwards[i].listen_host == NULL) ?
Damien Milleraa3bb102005-11-05 15:12:59 +1100810 "LOCALHOST" : options.remote_forwards[i].listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100811 options.remote_forwards[i].listen_port,
812 options.remote_forwards[i].connect_host,
813 options.remote_forwards[i].connect_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100814 channel_request_remote_forwarding(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100815 options.remote_forwards[i].listen_host,
816 options.remote_forwards[i].listen_port,
817 options.remote_forwards[i].connect_host,
818 options.remote_forwards[i].connect_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100819 }
820}
821
Ben Lindstrombba81212001-06-25 05:01:22 +0000822static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100823check_agent_present(void)
824{
825 if (options.forward_agent) {
Damien Miller788f2122005-11-05 15:14:59 +1100826 /* Clear agent forwarding if we don't have an agent. */
Damien Miller789e95d2002-09-12 09:52:46 +1000827 if (!ssh_agent_present())
Damien Miller0bc1bd82000-11-13 22:57:25 +1100828 options.forward_agent = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100829 }
830}
831
Ben Lindstrombba81212001-06-25 05:01:22 +0000832static int
Damien Miller1383bd82000-04-06 12:32:37 +1000833ssh_session(void)
834{
835 int type;
Damien Miller1383bd82000-04-06 12:32:37 +1000836 int interactive = 0;
837 int have_tty = 0;
838 struct winsize ws;
Damien Miller1383bd82000-04-06 12:32:37 +1000839 char *cp;
Damien Miller17e7ed02005-06-17 12:54:33 +1000840 const char *display;
Damien Miller1383bd82000-04-06 12:32:37 +1000841
Damien Miller95def091999-11-25 00:26:21 +1100842 /* Enable compression if requested. */
843 if (options.compression) {
844 debug("Requesting compression at level %d.", options.compression_level);
845
846 if (options.compression_level < 1 || options.compression_level > 9)
847 fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
848
849 /* Send the request. */
850 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
851 packet_put_int(options.compression_level);
852 packet_send();
853 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100854 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100855 if (type == SSH_SMSG_SUCCESS)
856 packet_start_compression(options.compression_level);
857 else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000858 logit("Warning: Remote host refused compression.");
Damien Miller95def091999-11-25 00:26:21 +1100859 else
860 packet_disconnect("Protocol error waiting for compression response.");
861 }
862 /* Allocate a pseudo tty if appropriate. */
863 if (tty_flag) {
864 debug("Requesting pty.");
865
866 /* Start the packet. */
867 packet_start(SSH_CMSG_REQUEST_PTY);
868
869 /* Store TERM in the packet. There is no limit on the
870 length of the string. */
871 cp = getenv("TERM");
872 if (!cp)
873 cp = "";
Ben Lindstrom664408d2001-06-09 01:42:01 +0000874 packet_put_cstring(cp);
Damien Miller95def091999-11-25 00:26:21 +1100875
876 /* Store window size in the packet. */
877 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
878 memset(&ws, 0, sizeof(ws));
879 packet_put_int(ws.ws_row);
880 packet_put_int(ws.ws_col);
881 packet_put_int(ws.ws_xpixel);
882 packet_put_int(ws.ws_ypixel);
883
884 /* Store tty modes in the packet. */
Ben Lindstromae8e2d32001-04-14 23:13:02 +0000885 tty_make_modes(fileno(stdin), NULL);
Damien Miller95def091999-11-25 00:26:21 +1100886
887 /* Send the packet, and wait for it to leave. */
888 packet_send();
889 packet_write_wait();
890
891 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100892 type = packet_read();
Damien Miller450a7a12000-03-26 13:04:51 +1000893 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100894 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +1000895 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000896 } else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000897 logit("Warning: Remote host failed or refused to allocate a pseudo tty.");
Damien Miller95def091999-11-25 00:26:21 +1100898 else
899 packet_disconnect("Protocol error waiting for pty request response.");
900 }
901 /* Request X11 forwarding if enabled and DISPLAY is set. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000902 display = getenv("DISPLAY");
903 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000904 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +1000905 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000906 client_x11_get_proto(display, options.xauth_location,
907 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +1000908 /* Request forwarding with authentication spoofing. */
Damien Miller95def091999-11-25 00:26:21 +1100909 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +1000910 x11_request_forwarding_with_spoofing(0, display, proto, data);
Damien Miller95def091999-11-25 00:26:21 +1100911
912 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100913 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100914 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100915 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +1000916 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller996acd22003-04-09 20:59:48 +1000917 logit("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +1000918 } else {
Damien Miller95def091999-11-25 00:26:21 +1100919 packet_disconnect("Protocol error waiting for X11 forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +1000920 }
Damien Miller95def091999-11-25 00:26:21 +1100921 }
922 /* Tell the packet module whether this is an interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000923 packet_set_interactive(interactive);
Damien Miller95def091999-11-25 00:26:21 +1100924
925 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100926 check_agent_present();
927
Damien Miller95def091999-11-25 00:26:21 +1100928 if (options.forward_agent) {
929 debug("Requesting authentication agent forwarding.");
930 auth_request_forwarding();
931
932 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100933 type = packet_read();
Damien Miller48b03fc2002-01-22 23:11:40 +1100934 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100935 if (type != SSH_SMSG_SUCCESS)
Damien Miller996acd22003-04-09 20:59:48 +1000936 logit("Warning: Remote host denied authentication agent forwarding.");
Damien Miller95def091999-11-25 00:26:21 +1100937 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000938
Damien Miller0bc1bd82000-11-13 22:57:25 +1100939 /* Initiate port forwardings. */
940 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000941
Damien Miller5428f641999-11-25 11:54:57 +1100942 /* If requested, let ssh continue in the background. */
Damien Miller4af51302000-04-16 11:18:38 +1000943 if (fork_after_authentication_flag)
Damien Miller5428f641999-11-25 11:54:57 +1100944 if (daemon(1, 1) < 0)
945 fatal("daemon() failed: %.200s", strerror(errno));
946
947 /*
948 * If a command was specified on the command line, execute the
949 * command now. Otherwise request the server to start a shell.
950 */
Damien Miller95def091999-11-25 00:26:21 +1100951 if (buffer_len(&command) > 0) {
952 int len = buffer_len(&command);
953 if (len > 900)
954 len = 900;
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100955 debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
Damien Miller95def091999-11-25 00:26:21 +1100956 packet_start(SSH_CMSG_EXEC_CMD);
957 packet_put_string(buffer_ptr(&command), buffer_len(&command));
958 packet_send();
959 packet_write_wait();
960 } else {
961 debug("Requesting shell.");
962 packet_start(SSH_CMSG_EXEC_SHELL);
963 packet_send();
964 packet_write_wait();
965 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000966
Damien Miller95def091999-11-25 00:26:21 +1100967 /* Enter the interactive session. */
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000968 return client_loop(have_tty, tty_flag ?
969 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
Damien Miller1383bd82000-04-06 12:32:37 +1000970}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000971
Ben Lindstrombba81212001-06-25 05:01:22 +0000972static void
Damien Miller0e220db2004-06-15 10:34:08 +1000973ssh_subsystem_reply(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000974{
975 int id, len;
976
977 id = packet_get_int();
978 len = buffer_len(&command);
Ben Lindstrom4040fe12001-03-05 06:52:57 +0000979 if (len > 900)
980 len = 900;
Damien Miller48b03fc2002-01-22 23:11:40 +1100981 packet_check_eom();
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000982 if (type == SSH2_MSG_CHANNEL_FAILURE)
983 fatal("Request for subsystem '%.*s' failed on channel %d",
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100984 len, (u_char *)buffer_ptr(&command), id);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000985}
986
Damien Miller2797f7f2002-04-23 21:09:44 +1000987void
Damien Miller509b0102003-12-17 16:33:10 +1100988client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt)
Damien Miller2797f7f2002-04-23 21:09:44 +1000989{
990 int i;
991
992 i = client_global_request_id++;
Damien Miller509b0102003-12-17 16:33:10 +1100993 if (i >= options.num_remote_forwards)
Damien Miller2797f7f2002-04-23 21:09:44 +1000994 return;
Damien Miller2797f7f2002-04-23 21:09:44 +1000995 debug("remote forward %s for: listen %d, connect %s:%d",
996 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
Damien Millerf91ee4c2005-03-01 21:24:33 +1100997 options.remote_forwards[i].listen_port,
998 options.remote_forwards[i].connect_host,
999 options.remote_forwards[i].connect_port);
Damien Miller2797f7f2002-04-23 21:09:44 +10001000 if (type == SSH2_MSG_REQUEST_FAILURE)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001001 logit("Warning: remote port forwarding failed for listen "
1002 "port %d", options.remote_forwards[i].listen_port);
Damien Miller2797f7f2002-04-23 21:09:44 +10001003}
1004
Damien Miller0e220db2004-06-15 10:34:08 +10001005static void
1006ssh_control_listener(void)
1007{
1008 struct sockaddr_un addr;
1009 mode_t old_umask;
Damien Miller07b6ff12004-06-15 11:14:45 +10001010 int addr_len;
1011
Damien Millerd14b1e72005-06-16 13:19:41 +10001012 if (options.control_path == NULL ||
1013 options.control_master == SSHCTL_MASTER_NO)
Damien Miller0e220db2004-06-15 10:34:08 +10001014 return;
1015
Damien Millerd14b1e72005-06-16 13:19:41 +10001016 debug("setting up multiplex master socket");
1017
Damien Miller0e220db2004-06-15 10:34:08 +10001018 memset(&addr, '\0', sizeof(addr));
1019 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001020 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001021 strlen(options.control_path) + 1;
1022
1023 if (strlcpy(addr.sun_path, options.control_path,
1024 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1025 fatal("ControlPath too long");
1026
1027 if ((control_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001028 fatal("%s socket(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001029
1030 old_umask = umask(0177);
Damien Miller07b6ff12004-06-15 11:14:45 +10001031 if (bind(control_fd, (struct sockaddr*)&addr, addr_len) == -1) {
Damien Miller0e220db2004-06-15 10:34:08 +10001032 control_fd = -1;
Damien Miller4f10e252005-05-04 15:33:09 +10001033 if (errno == EINVAL || errno == EADDRINUSE)
Damien Miller0e220db2004-06-15 10:34:08 +10001034 fatal("ControlSocket %s already exists",
1035 options.control_path);
1036 else
Damien Miller15d72a02005-11-05 15:07:33 +11001037 fatal("%s bind(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001038 }
1039 umask(old_umask);
1040
1041 if (listen(control_fd, 64) == -1)
Damien Miller15d72a02005-11-05 15:07:33 +11001042 fatal("%s listen(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001043
1044 set_nonblock(control_fd);
1045}
1046
Ben Lindstromf558cf62001-09-20 23:13:49 +00001047/* request pty/x11/agent/tcpfwd/shell for channel */
Ben Lindstrombba81212001-06-25 05:01:22 +00001048static void
Ben Lindstromf558cf62001-09-20 23:13:49 +00001049ssh_session2_setup(int id, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +10001050{
Damien Miller3756dce2004-06-18 01:17:29 +10001051 extern char **environ;
Damien Miller17e7ed02005-06-17 12:54:33 +10001052 const char *display;
Damien Miller0e220db2004-06-15 10:34:08 +10001053 int interactive = tty_flag;
Damien Miller17e7ed02005-06-17 12:54:33 +10001054
Damien Miller46d38de2005-07-17 17:02:09 +10001055 display = getenv("DISPLAY");
Damien Miller17e7ed02005-06-17 12:54:33 +10001056 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001057 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001058 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001059 client_x11_get_proto(display, options.xauth_location,
1060 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001061 /* Request forwarding with authentication spoofing. */
1062 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +10001063 x11_request_forwarding_with_spoofing(id, display, proto, data);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001064 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001065 /* XXX wait for reply */
1066 }
1067
Damien Miller0bc1bd82000-11-13 22:57:25 +11001068 check_agent_present();
1069 if (options.forward_agent) {
1070 debug("Requesting authentication agent forwarding.");
1071 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1072 packet_send();
1073 }
1074
Damien Miller7b58e802005-12-13 19:33:19 +11001075 if (options.tun_open != SSH_TUNMODE_NO) {
Damien Millerd27b9472005-12-13 19:29:02 +11001076 Channel *c;
1077 int fd;
1078
1079 debug("Requesting tun.");
Damien Miller7b58e802005-12-13 19:33:19 +11001080 if ((fd = tun_open(options.tun_local,
1081 options.tun_open)) >= 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11001082 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1083 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1084 0, "tun", 1);
1085 c->datagram = 1;
Damien Miller598bbc22005-12-31 16:33:36 +11001086#if defined(SSH_TUN_FILTER)
1087 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1088 channel_register_filter(c->self, sys_tun_infilter,
1089 sys_tun_outfilter);
1090#endif
Damien Millerd27b9472005-12-13 19:29:02 +11001091 packet_start(SSH2_MSG_CHANNEL_OPEN);
1092 packet_put_cstring("tun@openssh.com");
1093 packet_put_int(c->self);
1094 packet_put_int(c->local_window_max);
1095 packet_put_int(c->local_maxpacket);
Damien Miller7b58e802005-12-13 19:33:19 +11001096 packet_put_int(options.tun_open);
Damien Millerd27b9472005-12-13 19:29:02 +11001097 packet_put_int(options.tun_remote);
1098 packet_send();
1099 }
1100 }
1101
Damien Miller0e220db2004-06-15 10:34:08 +10001102 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
Damien Miller3756dce2004-06-18 01:17:29 +10001103 NULL, fileno(stdin), &command, environ, &ssh_subsystem_reply);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001104
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001105 packet_set_interactive(interactive);
Damien Miller1383bd82000-04-06 12:32:37 +10001106}
1107
Ben Lindstromf558cf62001-09-20 23:13:49 +00001108/* open new channel for a session */
Ben Lindstrombba81212001-06-25 05:01:22 +00001109static int
Ben Lindstromf558cf62001-09-20 23:13:49 +00001110ssh_session2_open(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001111{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001112 Channel *c;
1113 int window, packetmax, in, out, err;
Damien Millerad833b32000-08-23 10:46:23 +10001114
Damien Millercaf6dd62000-08-29 11:33:50 +11001115 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001116 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001117 } else {
1118 in = dup(STDIN_FILENO);
1119 }
1120 out = dup(STDOUT_FILENO);
1121 err = dup(STDERR_FILENO);
1122
1123 if (in < 0 || out < 0 || err < 0)
1124 fatal("dup() in/out/err failed");
1125
Damien Miller69b69aa2000-10-28 14:19:58 +11001126 /* enable nonblocking unless tty */
1127 if (!isatty(in))
1128 set_nonblock(in);
1129 if (!isatty(out))
1130 set_nonblock(out);
1131 if (!isatty(err))
1132 set_nonblock(err);
1133
Damien Millere4340be2000-09-16 13:29:08 +11001134 window = CHAN_SES_WINDOW_DEFAULT;
1135 packetmax = CHAN_SES_PACKET_DEFAULT;
Damien Miller19a59452002-02-19 15:20:57 +11001136 if (tty_flag) {
1137 window >>= 1;
1138 packetmax >>= 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001139 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001140 c = channel_new(
Damien Miller1383bd82000-04-06 12:32:37 +10001141 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001142 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001143 "client-session", /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001144
Ben Lindstromf558cf62001-09-20 23:13:49 +00001145 debug3("ssh_session2_open: channel_new: %d", c->self);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001146
Ben Lindstrom5ec26452001-06-09 00:18:51 +00001147 channel_send_open(c->self);
Ben Lindstromf558cf62001-09-20 23:13:49 +00001148 if (!no_shell_flag)
Damien Miller0e220db2004-06-15 10:34:08 +10001149 channel_register_confirm(c->self, ssh_session2_setup, NULL);
Damien Miller1383bd82000-04-06 12:32:37 +10001150
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001151 return c->self;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001152}
1153
Ben Lindstrombba81212001-06-25 05:01:22 +00001154static int
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001155ssh_session2(void)
1156{
Ben Lindstromf558cf62001-09-20 23:13:49 +00001157 int id = -1;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001158
1159 /* XXX should be pre-session */
1160 ssh_init_forwarding();
Damien Miller0e220db2004-06-15 10:34:08 +10001161 ssh_control_listener();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001162
Ben Lindstromf558cf62001-09-20 23:13:49 +00001163 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1164 id = ssh_session2_open();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001165
Damien Millerd27b9472005-12-13 19:29:02 +11001166 /* Execute a local command */
1167 if (options.local_command != NULL &&
1168 options.permit_local_command)
1169 ssh_local_cmd(options.local_command);
1170
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001171 /* If requested, let ssh continue in the background. */
1172 if (fork_after_authentication_flag)
1173 if (daemon(1, 1) < 0)
1174 fatal("daemon() failed: %.200s", strerror(errno));
1175
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001176 return client_loop(tty_flag, tty_flag ?
1177 options.escape_char : SSH_ESCAPECHAR_NONE, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001178}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001179
Ben Lindstrombba81212001-06-25 05:01:22 +00001180static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001181load_public_identity_files(void)
1182{
1183 char *filename;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001184 int i = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001185 Key *public;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001186#ifdef SMARTCARD
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001187 Key **keys;
1188
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001189 if (options.smartcard_device != NULL &&
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001190 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1191 (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL ) {
1192 int count = 0;
1193 for (i = 0; keys[i] != NULL; i++) {
1194 count++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001195 memmove(&options.identity_files[1], &options.identity_files[0],
1196 sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
1197 memmove(&options.identity_keys[1], &options.identity_keys[0],
1198 sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1199 options.num_identity_files++;
1200 options.identity_keys[0] = keys[i];
Damien Miller56a0bb02003-06-18 20:28:40 +10001201 options.identity_files[0] = sc_get_key_label(keys[i]);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001202 }
Ben Lindstrom4f054602002-03-26 03:23:00 +00001203 if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1204 options.num_identity_files = SSH_MAX_IDENTITY_FILES;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001205 i = count;
1206 xfree(keys);
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001207 }
Ben Lindstromffce1472001-08-06 21:57:31 +00001208#endif /* SMARTCARD */
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001209 for (; i < options.num_identity_files; i++) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001210 filename = tilde_expand_filename(options.identity_files[i],
1211 original_real_uid);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001212 public = key_load_public(filename, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001213 debug("identity file %s type %d", filename,
1214 public ? public->type : -1);
1215 xfree(options.identity_files[i]);
1216 options.identity_files[i] = filename;
1217 options.identity_keys[i] = public;
1218 }
1219}
Damien Miller0e220db2004-06-15 10:34:08 +10001220
1221static void
1222control_client_sighandler(int signo)
1223{
1224 control_client_terminate = signo;
1225}
1226
1227static void
1228control_client_sigrelay(int signo)
1229{
1230 if (control_server_pid > 1)
1231 kill(control_server_pid, signo);
1232}
1233
Darren Tucker365433f2004-06-22 12:29:23 +10001234static int
1235env_permitted(char *env)
1236{
1237 int i;
1238 char name[1024], *cp;
1239
1240 strlcpy(name, env, sizeof(name));
1241 if ((cp = strchr(name, '=')) == NULL)
1242 return (0);
1243
1244 *cp = '\0';
1245
1246 for (i = 0; i < options.num_send_env; i++)
1247 if (match_pattern(name, options.send_env[i]))
1248 return (1);
1249
1250 return (0);
1251}
1252
Damien Miller0e220db2004-06-15 10:34:08 +10001253static void
1254control_client(const char *path)
1255{
1256 struct sockaddr_un addr;
Darren Tucker39207a42004-11-05 20:19:51 +11001257 int i, r, fd, sock, exitval, num_env, addr_len;
Damien Miller0e220db2004-06-15 10:34:08 +10001258 Buffer m;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001259 char *term;
Damien Miller3756dce2004-06-18 01:17:29 +10001260 extern char **environ;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001261 u_int flags;
Darren Tuckerfc959702004-07-17 16:12:08 +10001262
Damien Millerd14b1e72005-06-16 13:19:41 +10001263 if (mux_command == 0)
1264 mux_command = SSHMUX_COMMAND_OPEN;
1265
1266 switch (options.control_master) {
1267 case SSHCTL_MASTER_AUTO:
1268 case SSHCTL_MASTER_AUTO_ASK:
1269 debug("auto-mux: Trying existing master");
1270 /* FALLTHROUGH */
1271 case SSHCTL_MASTER_NO:
1272 break;
1273 default:
1274 return;
1275 }
1276
Damien Miller0e220db2004-06-15 10:34:08 +10001277 memset(&addr, '\0', sizeof(addr));
1278 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001279 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001280 strlen(path) + 1;
1281
1282 if (strlcpy(addr.sun_path, path,
1283 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1284 fatal("ControlPath too long");
1285
1286 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1287 fatal("%s socket(): %s", __func__, strerror(errno));
1288
Damien Millerdadfd4d2005-05-26 12:07:13 +10001289 if (connect(sock, (struct sockaddr*)&addr, addr_len) == -1) {
Darren Tucker0814d312005-06-01 23:08:51 +10001290 if (mux_command != SSHMUX_COMMAND_OPEN) {
1291 fatal("Control socket connect(%.100s): %s", path,
1292 strerror(errno));
1293 }
Damien Miller538c9b72005-05-26 12:11:28 +10001294 if (errno == ENOENT)
1295 debug("Control socket \"%.100s\" does not exist", path);
1296 else {
1297 error("Control socket connect(%.100s): %s", path,
1298 strerror(errno));
1299 }
Damien Miller13390022005-07-06 09:44:19 +10001300 close(sock);
1301 return;
1302 }
Damien Miller46d38de2005-07-17 17:02:09 +10001303
Damien Miller13390022005-07-06 09:44:19 +10001304 if (stdin_null_flag) {
1305 if ((fd = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1306 fatal("open(/dev/null): %s", strerror(errno));
1307 if (dup2(fd, STDIN_FILENO) == -1)
1308 fatal("dup2: %s", strerror(errno));
1309 if (fd > STDERR_FILENO)
1310 close(fd);
1311 }
Damien Miller46d38de2005-07-17 17:02:09 +10001312
Damien Miller13390022005-07-06 09:44:19 +10001313 term = getenv("TERM");
Darren Tucker7ebfc102004-11-07 20:06:19 +11001314
1315 flags = 0;
1316 if (tty_flag)
1317 flags |= SSHMUX_FLAG_TTY;
1318 if (subsystem_flag)
1319 flags |= SSHMUX_FLAG_SUBSYS;
Damien Miller13390022005-07-06 09:44:19 +10001320 if (options.forward_x11)
1321 flags |= SSHMUX_FLAG_X11_FWD;
1322 if (options.forward_agent)
1323 flags |= SSHMUX_FLAG_AGENT_FWD;
Damien Miller0e220db2004-06-15 10:34:08 +10001324
Damien Miller0e220db2004-06-15 10:34:08 +10001325 buffer_init(&m);
1326
Darren Tucker7ebfc102004-11-07 20:06:19 +11001327 /* Send our command to server */
1328 buffer_put_int(&m, mux_command);
1329 buffer_put_int(&m, flags);
Damien Miller13390022005-07-06 09:44:19 +10001330 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001331 fatal("%s: msg_send", __func__);
1332 buffer_clear(&m);
1333
1334 /* Get authorisation status and PID of controlee */
Damien Miller0e220db2004-06-15 10:34:08 +10001335 if (ssh_msg_recv(sock, &m) == -1)
1336 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001337 if (buffer_get_char(&m) != SSHMUX_VER)
Damien Miller0e220db2004-06-15 10:34:08 +10001338 fatal("%s: wrong version", __func__);
Damien Miller23f07702004-06-18 01:19:03 +10001339 if (buffer_get_int(&m) != 1)
1340 fatal("Connection to master denied");
Damien Miller0e220db2004-06-15 10:34:08 +10001341 control_server_pid = buffer_get_int(&m);
1342
Damien Miller0e220db2004-06-15 10:34:08 +10001343 buffer_clear(&m);
Damien Miller0e220db2004-06-15 10:34:08 +10001344
Darren Tucker7ebfc102004-11-07 20:06:19 +11001345 switch (mux_command) {
1346 case SSHMUX_COMMAND_ALIVE_CHECK:
Darren Tucker47eede72005-03-14 23:08:12 +11001347 fprintf(stderr, "Master running (pid=%d)\r\n",
Darren Tucker7ebfc102004-11-07 20:06:19 +11001348 control_server_pid);
1349 exit(0);
1350 case SSHMUX_COMMAND_TERMINATE:
1351 fprintf(stderr, "Exit request sent.\r\n");
1352 exit(0);
1353 case SSHMUX_COMMAND_OPEN:
1354 /* continue below */
1355 break;
1356 default:
1357 fatal("silly mux_command %d", mux_command);
1358 }
1359
1360 /* SSHMUX_COMMAND_OPEN */
Damien Miller13390022005-07-06 09:44:19 +10001361 buffer_put_cstring(&m, term ? term : "");
Damien Miller0e220db2004-06-15 10:34:08 +10001362 buffer_append(&command, "\0", 1);
1363 buffer_put_cstring(&m, buffer_ptr(&command));
1364
Darren Tucker365433f2004-06-22 12:29:23 +10001365 if (options.num_send_env == 0 || environ == NULL) {
1366 buffer_put_int(&m, 0);
Darren Tuckerfc959702004-07-17 16:12:08 +10001367 } else {
Darren Tucker365433f2004-06-22 12:29:23 +10001368 /* Pass environment */
1369 num_env = 0;
1370 for (i = 0; environ[i] != NULL; i++)
1371 if (env_permitted(environ[i]))
1372 num_env++; /* Count */
Darren Tuckerfc959702004-07-17 16:12:08 +10001373
Darren Tucker365433f2004-06-22 12:29:23 +10001374 buffer_put_int(&m, num_env);
1375
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001376 for (i = 0; environ[i] != NULL && num_env >= 0; i++)
1377 if (env_permitted(environ[i])) {
1378 num_env--;
Darren Tucker365433f2004-06-22 12:29:23 +10001379 buffer_put_cstring(&m, environ[i]);
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001380 }
Darren Tucker365433f2004-06-22 12:29:23 +10001381 }
Damien Miller3756dce2004-06-18 01:17:29 +10001382
Damien Miller13390022005-07-06 09:44:19 +10001383 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Damien Miller0e220db2004-06-15 10:34:08 +10001384 fatal("%s: msg_send", __func__);
1385
1386 mm_send_fd(sock, STDIN_FILENO);
1387 mm_send_fd(sock, STDOUT_FILENO);
1388 mm_send_fd(sock, STDERR_FILENO);
1389
1390 /* Wait for reply, so master has a chance to gather ttymodes */
1391 buffer_clear(&m);
1392 if (ssh_msg_recv(sock, &m) == -1)
1393 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001394 if (buffer_get_char(&m) != SSHMUX_VER)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001395 fatal("%s: wrong version", __func__);
Damien Miller0e220db2004-06-15 10:34:08 +10001396 buffer_free(&m);
1397
Darren Tucker07336da2004-11-05 20:02:16 +11001398 signal(SIGHUP, control_client_sighandler);
Damien Miller0809e232004-06-18 22:20:57 +10001399 signal(SIGINT, control_client_sighandler);
1400 signal(SIGTERM, control_client_sighandler);
1401 signal(SIGWINCH, control_client_sigrelay);
1402
Damien Miller0e220db2004-06-15 10:34:08 +10001403 if (tty_flag)
1404 enter_raw_mode();
1405
1406 /* Stick around until the controlee closes the client_fd */
1407 exitval = 0;
1408 for (;!control_client_terminate;) {
1409 r = read(sock, &exitval, sizeof(exitval));
1410 if (r == 0) {
1411 debug2("Received EOF from master");
1412 break;
1413 }
1414 if (r > 0)
1415 debug2("Received exit status from master %d", exitval);
1416 if (r == -1 && errno != EINTR)
1417 fatal("%s: read %s", __func__, strerror(errno));
1418 }
1419
1420 if (control_client_terminate)
1421 debug2("Exiting on signal %d", control_client_terminate);
1422
1423 close(sock);
1424
1425 leave_raw_mode();
1426
1427 if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
1428 fprintf(stderr, "Connection to master closed.\r\n");
1429
1430 exit(exitval);
1431}