blob: 47b1b71950e78a489a2a8dbc2b3c2bf687cd7e35 [file] [log] [blame]
Damien Millerb7576772006-07-10 20:23:39 +10001/* $OpenBSD: ssh.c,v 1.278 2006/07/03 08:54:20 stevesk Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Ssh client program. This program can be used to log into a remote machine.
7 * The software supports strong authentication, encryption, and forwarding
8 * of X11, TCP/IP, and authentication connections.
9 *
Damien Millere4340be2000-09-16 13:29:08 +110010 * As far as I am concerned, the code I have written for this software
11 * can be used freely for any purpose. Any derived versions of this
12 * software must be clearly marked as such, and if the derived work is
13 * incompatible with the protocol description in the RFC file, it must be
14 * called by a name other than "ssh" or "Secure Shell".
15 *
16 * Copyright (c) 1999 Niels Provos. All rights reserved.
Darren Tucker0a118da2003-10-15 15:54:32 +100017 * Copyright (c) 2000, 2001, 2002, 2003 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110018 *
19 * Modified to work with SSL by Niels Provos <provos@citi.umich.edu>
20 * in Canada (German citizen).
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110041 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100042
43#include "includes.h"
Damien Millercd4223c2006-03-15 11:22:47 +110044
Damien Millerf17883e2006-03-15 11:45:54 +110045#include <sys/types.h>
46#ifdef HAVE_SYS_STAT_H
47# include <sys/stat.h>
48#endif
Damien Millercd4223c2006-03-15 11:22:47 +110049#include <sys/resource.h>
Damien Miller17e91c02006-03-15 11:28:34 +110050#include <sys/ioctl.h>
Damien Miller574c41f2006-03-15 11:40:10 +110051#include <sys/un.h>
Damien Miller03e20032006-03-15 11:16:59 +110052
Damien Millerc7b06362006-03-15 11:53:45 +110053#include <ctype.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110054#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110055#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110056#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110057#include <signal.h>
Damien Millereba71ba2000-04-29 23:57:08 +100058
59#include <openssl/evp.h>
Damien Miller0bc1bd82000-11-13 22:57:25 +110060#include <openssl/err.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063#include "ssh1.h"
Damien Miller1383bd82000-04-06 12:32:37 +100064#include "ssh2.h"
65#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000066#include "cipher.h"
67#include "xmalloc.h"
68#include "packet.h"
69#include "buffer.h"
Damien Miller0e220db2004-06-15 10:34:08 +100070#include "bufaux.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000071#include "channels.h"
Damien Millereba71ba2000-04-29 23:57:08 +100072#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100073#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100074#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000075#include "pathnames.h"
Damien Miller0e220db2004-06-15 10:34:08 +100076#include "dispatch.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +000077#include "clientloop.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000078#include "log.h"
79#include "readconf.h"
80#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000081#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000082#include "kex.h"
83#include "mac.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +100084#include "sshpty.h"
Darren Tucker46bc0752004-05-02 22:11:30 +100085#include "match.h"
Damien Miller0e220db2004-06-15 10:34:08 +100086#include "msg.h"
87#include "monitor_fdpass.h"
Darren Tucker25f60a72004-08-15 17:23:34 +100088#include "uidswap.h"
Damien Millerb7576772006-07-10 20:23:39 +100089#include "version.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090
Ben Lindstromc5b68002001-07-04 04:52:03 +000091#ifdef SMARTCARD
Ben Lindstromc5b68002001-07-04 04:52:03 +000092#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000093#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +000094
Damien Miller3f905871999-11-15 17:10:57 +110095extern char *__progname;
Damien Miller3f905871999-11-15 17:10:57 +110096
Damien Miller95def091999-11-25 00:26:21 +110097/* Flag indicating whether debug mode is on. This can be set on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098int debug_flag = 0;
99
Damien Miller78928792000-04-12 20:17:38 +1000100/* Flag indicating whether a tty should be allocated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101int tty_flag = 0;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000102int no_tty_flag = 0;
103int force_tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104
Damien Miller1383bd82000-04-06 12:32:37 +1000105/* don't exec a shell */
106int no_shell_flag = 0;
Damien Miller1383bd82000-04-06 12:32:37 +1000107
Damien Miller5428f641999-11-25 11:54:57 +1100108/*
109 * Flag indicating that nothing should be read from stdin. This can be set
110 * on the command line.
111 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112int stdin_null_flag = 0;
113
Damien Miller5428f641999-11-25 11:54:57 +1100114/*
115 * Flag indicating that ssh should fork after authentication. This is useful
Ben Lindstromf666fec2002-06-06 19:51:58 +0000116 * so that the passphrase can be entered manually, and then ssh goes to the
Damien Miller5428f641999-11-25 11:54:57 +1100117 * background.
118 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119int fork_after_authentication_flag = 0;
120
Damien Miller5428f641999-11-25 11:54:57 +1100121/*
122 * General data structure for command line options and options configurable
123 * in configuration files. See readconf.h.
124 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125Options options;
126
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000127/* optional user configfile */
128char *config = NULL;
129
Damien Miller5428f641999-11-25 11:54:57 +1100130/*
131 * Name of the host we are connecting to. This is the name given on the
132 * command line, or the HostName specified for the user-supplied name in a
133 * configuration file.
134 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135char *host;
136
137/* socket address the host resolves to */
Damien Miller34132e52000-01-14 15:45:46 +1100138struct sockaddr_storage hostaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000140/* Private host keys. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000141Sensitive sensitive_data;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142
143/* Original real UID. */
144uid_t original_real_uid;
Ben Lindstromf9c48842002-06-11 16:37:51 +0000145uid_t original_effective_uid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146
Damien Miller1383bd82000-04-06 12:32:37 +1000147/* command to be executed */
148Buffer command;
149
Damien Miller832562e2001-01-30 09:30:01 +1100150/* Should we execute a command or invoke a subsystem? */
151int subsystem_flag = 0;
152
Damien Miller2797f7f2002-04-23 21:09:44 +1000153/* # of replies received for global requests */
154static int client_global_request_id = 0;
155
Damien Miller8c4e18a2002-09-19 12:05:02 +1000156/* pid of proxycommand child process */
157pid_t proxy_command_pid = 0;
158
Damien Miller0e220db2004-06-15 10:34:08 +1000159/* fd to control socket */
160int control_fd = -1;
161
Darren Tucker7ebfc102004-11-07 20:06:19 +1100162/* Multiplexing control command */
Darren Tucker0814d312005-06-01 23:08:51 +1000163static u_int mux_command = 0;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100164
Damien Miller0e220db2004-06-15 10:34:08 +1000165/* Only used in control client mode */
166volatile sig_atomic_t control_client_terminate = 0;
167u_int control_server_pid = 0;
168
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000169/* Prints a help message to the user. This function never returns. */
170
Ben Lindstrombba81212001-06-25 05:01:22 +0000171static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000172usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173{
Damien Miller50955102004-03-22 09:34:58 +1100174 fprintf(stderr,
Darren Tucker9c6bf322004-12-03 14:10:19 +1100175"usage: ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n"
Darren Tucker895d6982005-10-03 18:18:05 +1000176" [-D [bind_address:]port] [-e escape_char] [-F configfile]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100177" [-i identity_file] [-L [bind_address:]port:host:hostport]\n"
Damien Millerf91ee4c2005-03-01 21:24:33 +1100178" [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100179" [-R [bind_address:]port:host:hostport] [-S ctl_path]\n"
Damien Miller991dba42006-07-10 20:16:27 +1000180" [-w local_tun[:remote_tun]] [user@]hostname [command]\n"
Damien Miller50955102004-03-22 09:34:58 +1100181 );
Darren Tuckere9a9b712005-12-20 16:15:51 +1100182 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000183}
184
Ben Lindstrombba81212001-06-25 05:01:22 +0000185static int ssh_session(void);
186static int ssh_session2(void);
187static void load_public_identity_files(void);
Damien Miller0e220db2004-06-15 10:34:08 +1000188static void control_client(const char *path);
Damien Miller1383bd82000-04-06 12:32:37 +1000189
Damien Miller95def091999-11-25 00:26:21 +1100190/*
191 * Main program for the ssh client.
192 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000193int
194main(int ac, char **av)
195{
Ben Lindstrom24157572002-06-12 16:09:39 +0000196 int i, opt, exit_status;
Damien Miller9836cf82003-12-17 16:30:06 +1100197 char *p, *cp, *line, buf[256];
Damien Miller95def091999-11-25 00:26:21 +1100198 struct stat st;
Ben Lindstrom086cf212001-03-05 05:56:40 +0000199 struct passwd *pw;
Damien Miller1383bd82000-04-06 12:32:37 +1000200 int dummy;
Ben Lindstrom5ccf63a2001-09-24 20:00:10 +0000201 extern int optind, optreset;
Damien Miller4f8e6692001-07-14 13:22:53 +1000202 extern char *optarg;
Damien Miller9651fe62005-06-26 08:55:25 +1000203 struct servent *sp;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100204 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000205
Darren Tuckerce321d82005-10-03 18:11:24 +1000206 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
207 sanitise_stdfd();
208
Damien Miller59d3d5b2003-08-22 09:34:41 +1000209 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000210 init_rng();
211
Damien Miller5428f641999-11-25 11:54:57 +1100212 /*
213 * Save the original real uid. It will be needed later (uid-swapping
214 * may clobber the real uid).
215 */
Damien Miller95def091999-11-25 00:26:21 +1100216 original_real_uid = getuid();
217 original_effective_uid = geteuid();
Damien Millera8e06ce2003-11-21 23:48:55 +1100218
Damien Miller50b9a602002-09-04 16:50:06 +1000219 /*
220 * Use uid-swapping to give up root privileges for the duration of
221 * option processing. We will re-instantiate the rights when we are
222 * ready to create the privileged port, and will permanently drop
223 * them when the port has been created (actually, when the connection
224 * has been made, as we may need to create the port several times).
225 */
226 PRIV_END;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000227
Damien Miller05dd7952000-10-01 00:42:48 +1100228#ifdef HAVE_SETRLIMIT
Damien Miller95def091999-11-25 00:26:21 +1100229 /* If we are installed setuid root be careful to not drop core. */
230 if (original_real_uid != original_effective_uid) {
231 struct rlimit rlim;
232 rlim.rlim_cur = rlim.rlim_max = 0;
233 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
234 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000235 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100236#endif
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000237 /* Get user data. */
238 pw = getpwuid(original_real_uid);
239 if (!pw) {
Damien Miller996acd22003-04-09 20:59:48 +1000240 logit("You don't exist, go away!");
Darren Tuckere9a9b712005-12-20 16:15:51 +1100241 exit(255);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000242 }
243 /* Take a copy of the returned structure. */
244 pw = pwcopy(pw);
245
Damien Miller5428f641999-11-25 11:54:57 +1100246 /*
Damien Miller5428f641999-11-25 11:54:57 +1100247 * Set our umask to something reasonable, as some files are created
248 * with the default umask. This will make them world-readable but
249 * writable only by the owner, which is ok for all files for which we
250 * don't set the modes explicitly.
251 */
Damien Miller95def091999-11-25 00:26:21 +1100252 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000253
Damien Miller95def091999-11-25 00:26:21 +1100254 /* Initialize option structure to indicate that no values have been set. */
255 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000256
Damien Miller95def091999-11-25 00:26:21 +1100257 /* Parse command-line arguments. */
258 host = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000259
Damien Miller2ecb6bd2006-03-15 12:03:53 +1100260 again:
Damien Millerf4614452001-07-14 12:18:10 +1000261 while ((opt = getopt(ac, av,
Damien Millerd27b9472005-12-13 19:29:02 +1100262 "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 +1100263 switch (opt) {
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000264 case '1':
265 options.protocol = SSH_PROTO_1;
266 break;
Damien Miller4af51302000-04-16 11:18:38 +1000267 case '2':
268 options.protocol = SSH_PROTO_2;
269 break;
Damien Miller34132e52000-01-14 15:45:46 +1100270 case '4':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000271 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100272 break;
Damien Miller34132e52000-01-14 15:45:46 +1100273 case '6':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000274 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100275 break;
Damien Miller95def091999-11-25 00:26:21 +1100276 case 'n':
277 stdin_null_flag = 1;
278 break;
Damien Miller95def091999-11-25 00:26:21 +1100279 case 'f':
280 fork_after_authentication_flag = 1;
281 stdin_null_flag = 1;
282 break;
Damien Miller95def091999-11-25 00:26:21 +1100283 case 'x':
284 options.forward_x11 = 0;
285 break;
Damien Miller95def091999-11-25 00:26:21 +1100286 case 'X':
287 options.forward_x11 = 1;
288 break;
Darren Tucker0a118da2003-10-15 15:54:32 +1000289 case 'Y':
290 options.forward_x11 = 1;
291 options.forward_x11_trusted = 1;
292 break;
Damien Miller95def091999-11-25 00:26:21 +1100293 case 'g':
294 options.gateway_ports = 1;
295 break;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100296 case 'O':
297 if (strcmp(optarg, "check") == 0)
298 mux_command = SSHMUX_COMMAND_ALIVE_CHECK;
299 else if (strcmp(optarg, "exit") == 0)
300 mux_command = SSHMUX_COMMAND_TERMINATE;
301 else
302 fatal("Invalid multiplex command.");
303 break;
Damien Miller147bba32002-09-04 16:46:06 +1000304 case 'P': /* deprecated */
Damien Miller95def091999-11-25 00:26:21 +1100305 options.use_privileged_port = 0;
306 break;
Damien Miller95def091999-11-25 00:26:21 +1100307 case 'a':
308 options.forward_agent = 0;
309 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000310 case 'A':
311 options.forward_agent = 1;
312 break;
Damien Miller95def091999-11-25 00:26:21 +1100313 case 'k':
Damien Millere0113cc2003-11-24 13:10:09 +1100314 options.gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100315 break;
Damien Miller95def091999-11-25 00:26:21 +1100316 case 'i':
317 if (stat(optarg, &st) < 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000318 fprintf(stderr, "Warning: Identity file %s "
Damien Miller3eb48b62005-03-01 21:15:46 +1100319 "not accessible: %s.\n", optarg,
320 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100321 break;
322 }
Damien Millerf4614452001-07-14 12:18:10 +1000323 if (options.num_identity_files >=
324 SSH_MAX_IDENTITY_FILES)
325 fatal("Too many identity files specified "
326 "(max %d)", SSH_MAX_IDENTITY_FILES);
327 options.identity_files[options.num_identity_files++] =
328 xstrdup(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100329 break;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000330 case 'I':
331#ifdef SMARTCARD
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000332 options.smartcard_device = xstrdup(optarg);
Ben Lindstrombcc18082001-08-06 21:59:25 +0000333#else
Ben Lindstromc5b68002001-07-04 04:52:03 +0000334 fprintf(stderr, "no support for smartcards.\n");
Ben Lindstrombcc18082001-08-06 21:59:25 +0000335#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000336 break;
Damien Miller95def091999-11-25 00:26:21 +1100337 case 't':
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000338 if (tty_flag)
339 force_tty_flag = 1;
Damien Miller95def091999-11-25 00:26:21 +1100340 tty_flag = 1;
341 break;
Damien Miller95def091999-11-25 00:26:21 +1100342 case 'v':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000343 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100344 debug_flag = 1;
345 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000346 } else {
347 if (options.log_level < SYSLOG_LEVEL_DEBUG3)
348 options.log_level++;
Damien Millere4340be2000-09-16 13:29:08 +1100349 break;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000350 }
Darren Tuckere9bf9842004-11-05 20:05:32 +1100351 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100352 case 'V':
Damien Miller0c889cd2004-03-22 09:36:00 +1100353 fprintf(stderr, "%s, %s\n",
Damien Miller2aa6d3c2004-09-12 16:53:04 +1000354 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
Damien Miller95def091999-11-25 00:26:21 +1100355 if (opt == 'V')
356 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100357 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100358 case 'w':
Damien Miller7b58e802005-12-13 19:33:19 +1100359 if (options.tun_open == -1)
360 options.tun_open = SSH_TUNMODE_DEFAULT;
Damien Millerd27b9472005-12-13 19:29:02 +1100361 options.tun_local = a2tun(optarg, &options.tun_remote);
Damien Miller7b58e802005-12-13 19:33:19 +1100362 if (options.tun_local == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100363 fprintf(stderr, "Bad tun device '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100364 exit(255);
Damien Millerd27b9472005-12-13 19:29:02 +1100365 }
366 break;
Damien Miller95def091999-11-25 00:26:21 +1100367 case 'q':
368 options.log_level = SYSLOG_LEVEL_QUIET;
369 break;
Damien Miller95def091999-11-25 00:26:21 +1100370 case 'e':
371 if (optarg[0] == '^' && optarg[2] == 0 &&
Damien Millerf4614452001-07-14 12:18:10 +1000372 (u_char) optarg[1] >= 64 &&
373 (u_char) optarg[1] < 128)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000374 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100375 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000376 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100377 else if (strcmp(optarg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000378 options.escape_char = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100379 else {
Damien Millerf4614452001-07-14 12:18:10 +1000380 fprintf(stderr, "Bad escape character '%s'.\n",
381 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100382 exit(255);
Damien Miller95def091999-11-25 00:26:21 +1100383 }
384 break;
Damien Miller95def091999-11-25 00:26:21 +1100385 case 'c':
Damien Millereba71ba2000-04-29 23:57:08 +1000386 if (ciphers_valid(optarg)) {
387 /* SSH2 only */
388 options.ciphers = xstrdup(optarg);
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000389 options.cipher = SSH_CIPHER_INVALID;
Damien Millereba71ba2000-04-29 23:57:08 +1000390 } else {
391 /* SSH1 only */
Damien Millere39cacc2000-11-29 12:18:44 +1100392 options.cipher = cipher_number(optarg);
393 if (options.cipher == -1) {
Damien Millerf4614452001-07-14 12:18:10 +1000394 fprintf(stderr,
395 "Unknown cipher type '%s'\n",
396 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100397 exit(255);
Damien Millereba71ba2000-04-29 23:57:08 +1000398 }
Damien Millerf4614452001-07-14 12:18:10 +1000399 if (options.cipher == SSH_CIPHER_3DES)
Damien Millere39cacc2000-11-29 12:18:44 +1100400 options.ciphers = "3des-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000401 else if (options.cipher == SSH_CIPHER_BLOWFISH)
Damien Millere39cacc2000-11-29 12:18:44 +1100402 options.ciphers = "blowfish-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000403 else
Damien Millere39cacc2000-11-29 12:18:44 +1100404 options.ciphers = (char *)-1;
Damien Miller95def091999-11-25 00:26:21 +1100405 }
406 break;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000407 case 'm':
408 if (mac_valid(optarg))
409 options.macs = xstrdup(optarg);
410 else {
Damien Millerf4614452001-07-14 12:18:10 +1000411 fprintf(stderr, "Unknown mac type '%s'\n",
412 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100413 exit(255);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000414 }
415 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000416 case 'M':
Damien Millerd14b1e72005-06-16 13:19:41 +1000417 if (options.control_master == SSHCTL_MASTER_YES)
418 options.control_master = SSHCTL_MASTER_ASK;
419 else
420 options.control_master = SSHCTL_MASTER_YES;
Damien Miller0e220db2004-06-15 10:34:08 +1000421 break;
Damien Miller95def091999-11-25 00:26:21 +1100422 case 'p':
Ben Lindstrom19066a12001-04-12 23:39:26 +0000423 options.port = a2port(optarg);
424 if (options.port == 0) {
Ben Lindstrom146edb92001-04-11 23:06:28 +0000425 fprintf(stderr, "Bad port '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100426 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000427 }
Damien Miller95def091999-11-25 00:26:21 +1100428 break;
Damien Miller95def091999-11-25 00:26:21 +1100429 case 'l':
430 options.user = optarg;
431 break;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000432
Damien Miller95def091999-11-25 00:26:21 +1100433 case 'L':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100434 if (parse_forward(&fwd, optarg))
435 add_local_forward(&options, &fwd);
436 else {
Damien Millerf4614452001-07-14 12:18:10 +1000437 fprintf(stderr,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100438 "Bad local forwarding specification '%s'\n",
Damien Millerf4614452001-07-14 12:18:10 +1000439 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100440 exit(255);
Ben Lindstrom1a174712001-09-12 17:56:15 +0000441 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100442 break;
443
444 case 'R':
445 if (parse_forward(&fwd, optarg)) {
446 add_remote_forward(&options, &fwd);
447 } else {
448 fprintf(stderr,
449 "Bad remote forwarding specification "
450 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100451 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100452 }
Damien Miller95def091999-11-25 00:26:21 +1100453 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000454
455 case 'D':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100456 cp = p = xstrdup(optarg);
457 memset(&fwd, '\0', sizeof(fwd));
458 fwd.connect_host = "socks";
459 if ((fwd.listen_host = hpdelim(&cp)) == NULL) {
460 fprintf(stderr, "Bad dynamic forwarding "
461 "specification '%.100s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100462 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100463 }
464 if (cp != NULL) {
465 fwd.listen_port = a2port(cp);
466 fwd.listen_host = cleanhostname(fwd.listen_host);
467 } else {
468 fwd.listen_port = a2port(fwd.listen_host);
Damien Millerbe1045d2005-08-12 22:10:56 +1000469 fwd.listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100470 }
471
472 if (fwd.listen_port == 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000473 fprintf(stderr, "Bad dynamic port '%s'\n",
474 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100475 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000476 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100477 add_local_forward(&options, &fwd);
478 xfree(p);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000479 break;
480
Damien Miller95def091999-11-25 00:26:21 +1100481 case 'C':
482 options.compression = 1;
483 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000484 case 'N':
485 no_shell_flag = 1;
486 no_tty_flag = 1;
487 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000488 case 'T':
489 no_tty_flag = 1;
490 break;
Damien Miller95def091999-11-25 00:26:21 +1100491 case 'o':
492 dummy = 1;
Damien Miller9836cf82003-12-17 16:30:06 +1100493 line = xstrdup(optarg);
Damien Millerf4614452001-07-14 12:18:10 +1000494 if (process_config_line(&options, host ? host : "",
Damien Miller9836cf82003-12-17 16:30:06 +1100495 line, "command-line", 0, &dummy) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100496 exit(255);
Damien Miller9836cf82003-12-17 16:30:06 +1100497 xfree(line);
Damien Miller95def091999-11-25 00:26:21 +1100498 break;
Damien Miller832562e2001-01-30 09:30:01 +1100499 case 's':
500 subsystem_flag = 1;
501 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000502 case 'S':
503 if (options.control_path != NULL)
504 free(options.control_path);
505 options.control_path = xstrdup(optarg);
Damien Miller0e220db2004-06-15 10:34:08 +1000506 break;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000507 case 'b':
508 options.bind_address = optarg;
509 break;
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000510 case 'F':
511 config = optarg;
512 break;
Damien Miller95def091999-11-25 00:26:21 +1100513 default:
514 usage();
515 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000516 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000517
Damien Millerf4614452001-07-14 12:18:10 +1000518 ac -= optind;
519 av += optind;
520
521 if (ac > 0 && !host && **av != '-') {
Ben Lindstromc276c122002-12-23 02:14:51 +0000522 if (strrchr(*av, '@')) {
Damien Millerf4614452001-07-14 12:18:10 +1000523 p = xstrdup(*av);
Ben Lindstromc276c122002-12-23 02:14:51 +0000524 cp = strrchr(p, '@');
Damien Millerf4614452001-07-14 12:18:10 +1000525 if (cp == NULL || cp == p)
526 usage();
527 options.user = p;
528 *cp = '\0';
529 host = ++cp;
530 } else
531 host = *av;
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000532 if (ac > 1) {
533 optind = optreset = 1;
Damien Millerf4614452001-07-14 12:18:10 +1000534 goto again;
535 }
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000536 ac--, av++;
Damien Millerf4614452001-07-14 12:18:10 +1000537 }
538
Damien Miller95def091999-11-25 00:26:21 +1100539 /* Check that we got a host name. */
540 if (!host)
541 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000542
Damien Millercb5e44a2000-09-29 12:12:36 +1100543 SSLeay_add_all_algorithms();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100544 ERR_load_crypto_strings();
Damien Millercb5e44a2000-09-29 12:12:36 +1100545
Damien Miller95def091999-11-25 00:26:21 +1100546 /* Initialize the command to execute on remote host. */
547 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000548
Damien Miller5428f641999-11-25 11:54:57 +1100549 /*
550 * Save the command to execute on the remote host in a buffer. There
551 * is no limit on the length of the command, except by the maximum
552 * packet size. Also sets the tty flag if there is no command.
553 */
Damien Millerf4614452001-07-14 12:18:10 +1000554 if (!ac) {
Damien Miller95def091999-11-25 00:26:21 +1100555 /* No command specified - execute shell on a tty. */
556 tty_flag = 1;
Damien Miller832562e2001-01-30 09:30:01 +1100557 if (subsystem_flag) {
Damien Millerf4614452001-07-14 12:18:10 +1000558 fprintf(stderr,
559 "You must specify a subsystem to invoke.\n");
Damien Miller832562e2001-01-30 09:30:01 +1100560 usage();
561 }
Damien Miller95def091999-11-25 00:26:21 +1100562 } else {
Damien Millerf4614452001-07-14 12:18:10 +1000563 /* A command has been specified. Store it into the buffer. */
564 for (i = 0; i < ac; i++) {
565 if (i)
Damien Miller95def091999-11-25 00:26:21 +1100566 buffer_append(&command, " ", 1);
567 buffer_append(&command, av[i], strlen(av[i]));
568 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000569 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000570
Damien Miller95def091999-11-25 00:26:21 +1100571 /* Cannot fork to background if no command. */
Damien Millercaf6dd62000-08-29 11:33:50 +1100572 if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
Damien Miller95def091999-11-25 00:26:21 +1100573 fatal("Cannot fork into background without a command to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000574
Damien Miller95def091999-11-25 00:26:21 +1100575 /* Allocate a tty by default if no command specified. */
576 if (buffer_len(&command) == 0)
577 tty_flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000578
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000579 /* Force no tty */
Ben Lindstromc72745a2000-12-02 19:03:54 +0000580 if (no_tty_flag)
581 tty_flag = 0;
Damien Miller95def091999-11-25 00:26:21 +1100582 /* Do not allocate a tty if stdin is not a tty. */
Damien Millerddee5752005-05-26 12:05:05 +1000583 if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100584 if (tty_flag)
Damien Miller996acd22003-04-09 20:59:48 +1000585 logit("Pseudo-terminal will not be allocated because stdin is not a terminal.");
Damien Miller95def091999-11-25 00:26:21 +1100586 tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000587 }
Damien Miller1383bd82000-04-06 12:32:37 +1000588
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000589 /*
590 * Initialize "log" output. Since we are the client all output
591 * actually goes to stderr.
592 */
Ben Lindstrom2b646522001-04-12 16:16:57 +0000593 log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
594 SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000595
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000596 /*
597 * Read per-user configuration file. Ignore the system wide config
598 * file if the user specifies a config file on the command line.
599 */
600 if (config != NULL) {
Damien Miller914420f2004-04-20 20:14:07 +1000601 if (!read_config_file(config, host, &options, 0))
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000602 fatal("Can't open user config file %.100s: "
603 "%.100s", config, strerror(errno));
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000604 } else {
605 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
606 _PATH_SSH_USER_CONFFILE);
Damien Miller57a44762004-04-20 20:11:57 +1000607 (void)read_config_file(buf, host, &options, 1);
Ben Lindstrom83f07d12001-10-03 17:22:29 +0000608
609 /* Read systemwide configuration file after use config. */
Darren Tuckerfc959702004-07-17 16:12:08 +1000610 (void)read_config_file(_PATH_HOST_CONFIG_FILE, host,
Damien Miller57a44762004-04-20 20:11:57 +1000611 &options, 0);
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000612 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000613
Damien Miller95def091999-11-25 00:26:21 +1100614 /* Fill configuration defaults. */
615 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000616
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000617 channel_set_af(options.address_family);
618
Damien Miller95def091999-11-25 00:26:21 +1100619 /* reinit */
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000620 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000621
Damien Miller60bc5172001-03-19 09:38:15 +1100622 seed_rng();
623
Damien Miller95def091999-11-25 00:26:21 +1100624 if (options.user == NULL)
625 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000626
Damien Miller95def091999-11-25 00:26:21 +1100627 if (options.hostname != NULL)
628 host = options.hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000629
Darren Tucker3f521e22003-07-03 16:20:42 +1000630 /* force lowercase for hostkey matching */
631 if (options.host_key_alias != NULL) {
632 for (p = options.host_key_alias; *p; p++)
633 if (isupper(*p))
Damien Miller1d2b6702006-03-26 14:09:54 +1100634 *p = (char)tolower(*p);
Darren Tucker3f521e22003-07-03 16:20:42 +1000635 }
636
Damien Miller7c71cc72005-06-26 08:56:31 +1000637 /* Get default port if port has not been set. */
638 if (options.port == 0) {
639 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
640 options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
641 }
642
Damien Miller9f1e33a2003-02-24 11:57:32 +1100643 if (options.proxy_command != NULL &&
644 strcmp(options.proxy_command, "none") == 0)
645 options.proxy_command = NULL;
Damien Miller8f74c8f2005-06-26 08:56:03 +1000646 if (options.control_path != NULL &&
647 strcmp(options.control_path, "none") == 0)
648 options.control_path = NULL;
Damien Miller9f1e33a2003-02-24 11:57:32 +1100649
Damien Miller0e220db2004-06-15 10:34:08 +1000650 if (options.control_path != NULL) {
Damien Miller6b1d53c2006-03-31 23:13:21 +1100651 char thishost[NI_MAXHOST];
Damien Miller3ec54c72006-03-15 11:30:13 +1100652
Damien Miller6b1d53c2006-03-31 23:13:21 +1100653 if (gethostname(thishost, sizeof(thishost)) == -1)
Damien Miller3ec54c72006-03-15 11:30:13 +1100654 fatal("gethostname: %s", strerror(errno));
Damien Miller6476cad2005-06-16 13:18:34 +1000655 snprintf(buf, sizeof(buf), "%d", options.port);
656 cp = tilde_expand_filename(options.control_path,
657 original_real_uid);
658 options.control_path = percent_expand(cp, "p", buf, "h", host,
Damien Miller6b1d53c2006-03-31 23:13:21 +1100659 "r", options.user, "l", thishost, (char *)NULL);
Damien Miller6476cad2005-06-16 13:18:34 +1000660 xfree(cp);
Damien Miller0e220db2004-06-15 10:34:08 +1000661 }
Darren Tucker0814d312005-06-01 23:08:51 +1000662 if (mux_command != 0 && options.control_path == NULL)
663 fatal("No ControlPath specified for \"-O\" command");
Damien Millerd14b1e72005-06-16 13:19:41 +1000664 if (options.control_path != NULL)
Damien Millerdadfd4d2005-05-26 12:07:13 +1000665 control_client(options.control_path);
Damien Miller0e220db2004-06-15 10:34:08 +1000666
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000667 /* Open a connection to the remote host. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000668 if (ssh_connect(host, &hostaddr, options.port,
669 options.address_family, options.connection_attempts,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000670#ifdef HAVE_CYGWIN
671 options.use_privileged_port,
672#else
Ben Lindstromf9c48842002-06-11 16:37:51 +0000673 original_effective_uid == 0 && options.use_privileged_port,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000674#endif
Ben Lindstromda394ca2002-06-12 16:11:12 +0000675 options.proxy_command) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100676 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000677
Damien Miller5428f641999-11-25 11:54:57 +1100678 /*
679 * If we successfully made the connection, load the host private key
680 * in case we will need it later for combined rsa-rhosts
681 * authentication. This must be done before releasing extra
682 * privileges, because the file is only readable by root.
Ben Lindstrom9e5bb572002-06-06 19:58:27 +0000683 * If we cannot access the private keys, load the public keys
684 * instead and try to execute the ssh-keysign helper instead.
Damien Miller5428f641999-11-25 11:54:57 +1100685 */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000686 sensitive_data.nkeys = 0;
687 sensitive_data.keys = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000688 sensitive_data.external_keysign = 0;
Ben Lindstrom24157572002-06-12 16:09:39 +0000689 if (options.rhosts_rsa_authentication ||
690 options.hostbased_authentication) {
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000691 sensitive_data.nkeys = 3;
Damien Millerddd63ab2006-03-31 23:10:51 +1100692 sensitive_data.keys = xcalloc(sensitive_data.nkeys,
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000693 sizeof(Key));
Ben Lindstromf9c48842002-06-11 16:37:51 +0000694
695 PRIV_START;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000696 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
Darren Tucker232b76f2006-05-06 17:41:51 +1000697 _PATH_HOST_KEY_FILE, "", NULL, NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000698 sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
Darren Tucker232b76f2006-05-06 17:41:51 +1000699 _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000700 sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
Darren Tucker232b76f2006-05-06 17:41:51 +1000701 _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL);
Ben Lindstromf9c48842002-06-11 16:37:51 +0000702 PRIV_END;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000703
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +0000704 if (options.hostbased_authentication == 1 &&
705 sensitive_data.keys[0] == NULL &&
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000706 sensitive_data.keys[1] == NULL &&
707 sensitive_data.keys[2] == NULL) {
708 sensitive_data.keys[1] = key_load_public(
709 _PATH_HOST_DSA_KEY_FILE, NULL);
710 sensitive_data.keys[2] = key_load_public(
711 _PATH_HOST_RSA_KEY_FILE, NULL);
712 sensitive_data.external_keysign = 1;
713 }
Damien Miller95def091999-11-25 00:26:21 +1100714 }
Damien Miller5428f641999-11-25 11:54:57 +1100715 /*
716 * Get rid of any extra privileges that we may have. We will no
717 * longer need them. Also, extra privileges could make it very hard
718 * to read identity files and other non-world-readable files from the
719 * user's home directory if it happens to be on a NFS volume where
720 * root is mapped to nobody.
721 */
Darren Tucker25f60a72004-08-15 17:23:34 +1000722 if (original_effective_uid == 0) {
723 PRIV_START;
724 permanently_set_uid(pw);
725 }
Damien Miller95def091999-11-25 00:26:21 +1100726
Damien Miller5428f641999-11-25 11:54:57 +1100727 /*
728 * Now that we are back to our own permissions, create ~/.ssh
Damien Miller788f2122005-11-05 15:14:59 +1100729 * directory if it doesn't already exist.
Damien Miller5428f641999-11-25 11:54:57 +1100730 */
Ben Lindstrom930b14a2001-08-15 23:19:21 +0000731 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 +1100732 if (stat(buf, &st) < 0)
Damien Millerbe484b52000-07-15 14:14:16 +1000733 if (mkdir(buf, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100734 error("Could not create directory '%.200s'.", buf);
735
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000736 /* load options.identity_files */
737 load_public_identity_files();
738
739 /* Expand ~ in known host file names. */
740 /* XXX mem-leaks: */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100741 options.system_hostfile =
742 tilde_expand_filename(options.system_hostfile, original_real_uid);
743 options.user_hostfile =
744 tilde_expand_filename(options.user_hostfile, original_real_uid);
745 options.system_hostfile2 =
746 tilde_expand_filename(options.system_hostfile2, original_real_uid);
747 options.user_hostfile2 =
748 tilde_expand_filename(options.user_hostfile2, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100749
Damien Miller07cd5892001-11-12 10:52:03 +1100750 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
751
Damien Miller95def091999-11-25 00:26:21 +1100752 /* Log into the remote system. This never returns if the login fails. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000753 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, pw);
Damien Miller95def091999-11-25 00:26:21 +1100754
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000755 /* We no longer need the private host keys. Clear them now. */
756 if (sensitive_data.nkeys != 0) {
757 for (i = 0; i < sensitive_data.nkeys; i++) {
758 if (sensitive_data.keys[i] != NULL) {
759 /* Destroys contents safely */
760 debug3("clear hostkey %d", i);
761 key_free(sensitive_data.keys[i]);
762 sensitive_data.keys[i] = NULL;
763 }
764 }
765 xfree(sensitive_data.keys);
766 }
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +0000767 for (i = 0; i < options.num_identity_files; i++) {
768 if (options.identity_files[i]) {
769 xfree(options.identity_files[i]);
770 options.identity_files[i] = NULL;
771 }
772 if (options.identity_keys[i]) {
773 key_free(options.identity_keys[i]);
774 options.identity_keys[i] = NULL;
775 }
776 }
Damien Miller95def091999-11-25 00:26:21 +1100777
Damien Miller1383bd82000-04-06 12:32:37 +1000778 exit_status = compat20 ? ssh_session2() : ssh_session();
779 packet_close();
Damien Miller8c4e18a2002-09-19 12:05:02 +1000780
Damien Miller0e220db2004-06-15 10:34:08 +1000781 if (options.control_path != NULL && control_fd != -1)
782 unlink(options.control_path);
783
Damien Miller8c4e18a2002-09-19 12:05:02 +1000784 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100785 * Send SIGHUP to proxy command if used. We don't wait() in
Damien Miller8c4e18a2002-09-19 12:05:02 +1000786 * case it hangs and instead rely on init to reap the child
787 */
788 if (proxy_command_pid > 1)
789 kill(proxy_command_pid, SIGHUP);
790
Damien Miller1383bd82000-04-06 12:32:37 +1000791 return exit_status;
792}
793
Ben Lindstrombba81212001-06-25 05:01:22 +0000794static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100795ssh_init_forwarding(void)
796{
Kevin Steves12057502001-02-05 14:54:34 +0000797 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100798 int i;
Kevin Steves12057502001-02-05 14:54:34 +0000799
Damien Miller0bc1bd82000-11-13 22:57:25 +1100800 /* Initiate local TCP/IP port forwardings. */
801 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100802 debug("Local connections to %.200s:%d forwarded to remote "
803 "address %.200s:%d",
Darren Tucker47eede72005-03-14 23:08:12 +1100804 (options.local_forwards[i].listen_host == NULL) ?
805 (options.gateway_ports ? "*" : "LOCALHOST") :
Damien Millerf91ee4c2005-03-01 21:24:33 +1100806 options.local_forwards[i].listen_host,
807 options.local_forwards[i].listen_port,
808 options.local_forwards[i].connect_host,
809 options.local_forwards[i].connect_port);
Damien Millerb16461c2002-01-22 23:29:22 +1100810 success += channel_setup_local_fwd_listener(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100811 options.local_forwards[i].listen_host,
812 options.local_forwards[i].listen_port,
813 options.local_forwards[i].connect_host,
814 options.local_forwards[i].connect_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100815 options.gateway_ports);
816 }
Kevin Steves12057502001-02-05 14:54:34 +0000817 if (i > 0 && success == 0)
818 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100819
820 /* Initiate remote TCP/IP port forwardings. */
821 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100822 debug("Remote connections from %.200s:%d forwarded to "
823 "local address %.200s:%d",
Damien Miller46d38de2005-07-17 17:02:09 +1000824 (options.remote_forwards[i].listen_host == NULL) ?
Damien Milleraa3bb102005-11-05 15:12:59 +1100825 "LOCALHOST" : options.remote_forwards[i].listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100826 options.remote_forwards[i].listen_port,
827 options.remote_forwards[i].connect_host,
828 options.remote_forwards[i].connect_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100829 channel_request_remote_forwarding(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100830 options.remote_forwards[i].listen_host,
831 options.remote_forwards[i].listen_port,
832 options.remote_forwards[i].connect_host,
833 options.remote_forwards[i].connect_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100834 }
835}
836
Ben Lindstrombba81212001-06-25 05:01:22 +0000837static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100838check_agent_present(void)
839{
840 if (options.forward_agent) {
Damien Miller788f2122005-11-05 15:14:59 +1100841 /* Clear agent forwarding if we don't have an agent. */
Damien Miller789e95d2002-09-12 09:52:46 +1000842 if (!ssh_agent_present())
Damien Miller0bc1bd82000-11-13 22:57:25 +1100843 options.forward_agent = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100844 }
845}
846
Ben Lindstrombba81212001-06-25 05:01:22 +0000847static int
Damien Miller1383bd82000-04-06 12:32:37 +1000848ssh_session(void)
849{
850 int type;
Damien Miller1383bd82000-04-06 12:32:37 +1000851 int interactive = 0;
852 int have_tty = 0;
853 struct winsize ws;
Damien Miller1383bd82000-04-06 12:32:37 +1000854 char *cp;
Damien Miller17e7ed02005-06-17 12:54:33 +1000855 const char *display;
Damien Miller1383bd82000-04-06 12:32:37 +1000856
Damien Miller95def091999-11-25 00:26:21 +1100857 /* Enable compression if requested. */
858 if (options.compression) {
859 debug("Requesting compression at level %d.", options.compression_level);
860
861 if (options.compression_level < 1 || options.compression_level > 9)
862 fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
863
864 /* Send the request. */
865 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
866 packet_put_int(options.compression_level);
867 packet_send();
868 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100869 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100870 if (type == SSH_SMSG_SUCCESS)
871 packet_start_compression(options.compression_level);
872 else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000873 logit("Warning: Remote host refused compression.");
Damien Miller95def091999-11-25 00:26:21 +1100874 else
875 packet_disconnect("Protocol error waiting for compression response.");
876 }
877 /* Allocate a pseudo tty if appropriate. */
878 if (tty_flag) {
879 debug("Requesting pty.");
880
881 /* Start the packet. */
882 packet_start(SSH_CMSG_REQUEST_PTY);
883
884 /* Store TERM in the packet. There is no limit on the
885 length of the string. */
886 cp = getenv("TERM");
887 if (!cp)
888 cp = "";
Ben Lindstrom664408d2001-06-09 01:42:01 +0000889 packet_put_cstring(cp);
Damien Miller95def091999-11-25 00:26:21 +1100890
891 /* Store window size in the packet. */
892 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
893 memset(&ws, 0, sizeof(ws));
Damien Miller71a73672006-03-26 14:04:36 +1100894 packet_put_int((u_int)ws.ws_row);
895 packet_put_int((u_int)ws.ws_col);
896 packet_put_int((u_int)ws.ws_xpixel);
897 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller95def091999-11-25 00:26:21 +1100898
899 /* Store tty modes in the packet. */
Ben Lindstromae8e2d32001-04-14 23:13:02 +0000900 tty_make_modes(fileno(stdin), NULL);
Damien Miller95def091999-11-25 00:26:21 +1100901
902 /* Send the packet, and wait for it to leave. */
903 packet_send();
904 packet_write_wait();
905
906 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100907 type = packet_read();
Damien Miller450a7a12000-03-26 13:04:51 +1000908 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100909 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +1000910 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000911 } else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000912 logit("Warning: Remote host failed or refused to allocate a pseudo tty.");
Damien Miller95def091999-11-25 00:26:21 +1100913 else
914 packet_disconnect("Protocol error waiting for pty request response.");
915 }
916 /* Request X11 forwarding if enabled and DISPLAY is set. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000917 display = getenv("DISPLAY");
918 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000919 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +1000920 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000921 client_x11_get_proto(display, options.xauth_location,
922 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +1000923 /* Request forwarding with authentication spoofing. */
Damien Miller95def091999-11-25 00:26:21 +1100924 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +1000925 x11_request_forwarding_with_spoofing(0, display, proto, data);
Damien Miller95def091999-11-25 00:26:21 +1100926
927 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100928 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100929 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100930 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +1000931 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller996acd22003-04-09 20:59:48 +1000932 logit("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +1000933 } else {
Damien Miller95def091999-11-25 00:26:21 +1100934 packet_disconnect("Protocol error waiting for X11 forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +1000935 }
Damien Miller95def091999-11-25 00:26:21 +1100936 }
937 /* Tell the packet module whether this is an interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000938 packet_set_interactive(interactive);
Damien Miller95def091999-11-25 00:26:21 +1100939
940 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100941 check_agent_present();
942
Damien Miller95def091999-11-25 00:26:21 +1100943 if (options.forward_agent) {
944 debug("Requesting authentication agent forwarding.");
945 auth_request_forwarding();
946
947 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100948 type = packet_read();
Damien Miller48b03fc2002-01-22 23:11:40 +1100949 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100950 if (type != SSH_SMSG_SUCCESS)
Damien Miller996acd22003-04-09 20:59:48 +1000951 logit("Warning: Remote host denied authentication agent forwarding.");
Damien Miller95def091999-11-25 00:26:21 +1100952 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000953
Damien Miller0bc1bd82000-11-13 22:57:25 +1100954 /* Initiate port forwardings. */
955 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000956
Damien Miller5428f641999-11-25 11:54:57 +1100957 /* If requested, let ssh continue in the background. */
Damien Miller4af51302000-04-16 11:18:38 +1000958 if (fork_after_authentication_flag)
Damien Miller5428f641999-11-25 11:54:57 +1100959 if (daemon(1, 1) < 0)
960 fatal("daemon() failed: %.200s", strerror(errno));
961
962 /*
963 * If a command was specified on the command line, execute the
964 * command now. Otherwise request the server to start a shell.
965 */
Damien Miller95def091999-11-25 00:26:21 +1100966 if (buffer_len(&command) > 0) {
967 int len = buffer_len(&command);
968 if (len > 900)
969 len = 900;
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100970 debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
Damien Miller95def091999-11-25 00:26:21 +1100971 packet_start(SSH_CMSG_EXEC_CMD);
972 packet_put_string(buffer_ptr(&command), buffer_len(&command));
973 packet_send();
974 packet_write_wait();
975 } else {
976 debug("Requesting shell.");
977 packet_start(SSH_CMSG_EXEC_SHELL);
978 packet_send();
979 packet_write_wait();
980 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000981
Damien Miller95def091999-11-25 00:26:21 +1100982 /* Enter the interactive session. */
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000983 return client_loop(have_tty, tty_flag ?
984 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
Damien Miller1383bd82000-04-06 12:32:37 +1000985}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000986
Ben Lindstrombba81212001-06-25 05:01:22 +0000987static void
Damien Miller0e220db2004-06-15 10:34:08 +1000988ssh_subsystem_reply(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000989{
990 int id, len;
991
992 id = packet_get_int();
993 len = buffer_len(&command);
Ben Lindstrom4040fe12001-03-05 06:52:57 +0000994 if (len > 900)
995 len = 900;
Damien Miller48b03fc2002-01-22 23:11:40 +1100996 packet_check_eom();
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000997 if (type == SSH2_MSG_CHANNEL_FAILURE)
998 fatal("Request for subsystem '%.*s' failed on channel %d",
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100999 len, (u_char *)buffer_ptr(&command), id);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001000}
1001
Damien Miller2797f7f2002-04-23 21:09:44 +10001002void
Damien Miller509b0102003-12-17 16:33:10 +11001003client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt)
Damien Miller2797f7f2002-04-23 21:09:44 +10001004{
1005 int i;
1006
1007 i = client_global_request_id++;
Damien Miller509b0102003-12-17 16:33:10 +11001008 if (i >= options.num_remote_forwards)
Damien Miller2797f7f2002-04-23 21:09:44 +10001009 return;
Damien Miller2797f7f2002-04-23 21:09:44 +10001010 debug("remote forward %s for: listen %d, connect %s:%d",
1011 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
Damien Millerf91ee4c2005-03-01 21:24:33 +11001012 options.remote_forwards[i].listen_port,
1013 options.remote_forwards[i].connect_host,
1014 options.remote_forwards[i].connect_port);
Damien Miller2797f7f2002-04-23 21:09:44 +10001015 if (type == SSH2_MSG_REQUEST_FAILURE)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001016 logit("Warning: remote port forwarding failed for listen "
1017 "port %d", options.remote_forwards[i].listen_port);
Damien Miller2797f7f2002-04-23 21:09:44 +10001018}
1019
Damien Miller0e220db2004-06-15 10:34:08 +10001020static void
1021ssh_control_listener(void)
1022{
1023 struct sockaddr_un addr;
1024 mode_t old_umask;
Damien Miller07b6ff12004-06-15 11:14:45 +10001025 int addr_len;
1026
Damien Millerd14b1e72005-06-16 13:19:41 +10001027 if (options.control_path == NULL ||
1028 options.control_master == SSHCTL_MASTER_NO)
Damien Miller0e220db2004-06-15 10:34:08 +10001029 return;
1030
Damien Millerd14b1e72005-06-16 13:19:41 +10001031 debug("setting up multiplex master socket");
1032
Damien Miller0e220db2004-06-15 10:34:08 +10001033 memset(&addr, '\0', sizeof(addr));
1034 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001035 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001036 strlen(options.control_path) + 1;
1037
1038 if (strlcpy(addr.sun_path, options.control_path,
1039 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1040 fatal("ControlPath too long");
1041
1042 if ((control_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001043 fatal("%s socket(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001044
1045 old_umask = umask(0177);
Damien Miller90967402006-03-26 14:07:26 +11001046 if (bind(control_fd, (struct sockaddr *)&addr, addr_len) == -1) {
Damien Miller0e220db2004-06-15 10:34:08 +10001047 control_fd = -1;
Damien Miller4f10e252005-05-04 15:33:09 +10001048 if (errno == EINVAL || errno == EADDRINUSE)
Damien Miller0e220db2004-06-15 10:34:08 +10001049 fatal("ControlSocket %s already exists",
1050 options.control_path);
1051 else
Damien Miller15d72a02005-11-05 15:07:33 +11001052 fatal("%s bind(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001053 }
1054 umask(old_umask);
1055
1056 if (listen(control_fd, 64) == -1)
Damien Miller15d72a02005-11-05 15:07:33 +11001057 fatal("%s listen(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001058
1059 set_nonblock(control_fd);
1060}
1061
Ben Lindstromf558cf62001-09-20 23:13:49 +00001062/* request pty/x11/agent/tcpfwd/shell for channel */
Ben Lindstrombba81212001-06-25 05:01:22 +00001063static void
Ben Lindstromf558cf62001-09-20 23:13:49 +00001064ssh_session2_setup(int id, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +10001065{
Damien Miller3756dce2004-06-18 01:17:29 +10001066 extern char **environ;
Damien Miller17e7ed02005-06-17 12:54:33 +10001067 const char *display;
Damien Miller0e220db2004-06-15 10:34:08 +10001068 int interactive = tty_flag;
Damien Miller17e7ed02005-06-17 12:54:33 +10001069
Damien Miller46d38de2005-07-17 17:02:09 +10001070 display = getenv("DISPLAY");
Damien Miller17e7ed02005-06-17 12:54:33 +10001071 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001072 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001073 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001074 client_x11_get_proto(display, options.xauth_location,
1075 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001076 /* Request forwarding with authentication spoofing. */
1077 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +10001078 x11_request_forwarding_with_spoofing(id, display, proto, data);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001079 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001080 /* XXX wait for reply */
1081 }
1082
Damien Miller0bc1bd82000-11-13 22:57:25 +11001083 check_agent_present();
1084 if (options.forward_agent) {
1085 debug("Requesting authentication agent forwarding.");
1086 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1087 packet_send();
1088 }
1089
Damien Miller7b58e802005-12-13 19:33:19 +11001090 if (options.tun_open != SSH_TUNMODE_NO) {
Damien Millerd27b9472005-12-13 19:29:02 +11001091 Channel *c;
1092 int fd;
1093
1094 debug("Requesting tun.");
Damien Miller7b58e802005-12-13 19:33:19 +11001095 if ((fd = tun_open(options.tun_local,
1096 options.tun_open)) >= 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11001097 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1098 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1099 0, "tun", 1);
1100 c->datagram = 1;
Damien Miller598bbc22005-12-31 16:33:36 +11001101#if defined(SSH_TUN_FILTER)
1102 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1103 channel_register_filter(c->self, sys_tun_infilter,
1104 sys_tun_outfilter);
1105#endif
Damien Millerd27b9472005-12-13 19:29:02 +11001106 packet_start(SSH2_MSG_CHANNEL_OPEN);
1107 packet_put_cstring("tun@openssh.com");
1108 packet_put_int(c->self);
1109 packet_put_int(c->local_window_max);
1110 packet_put_int(c->local_maxpacket);
Damien Miller7b58e802005-12-13 19:33:19 +11001111 packet_put_int(options.tun_open);
Damien Millerd27b9472005-12-13 19:29:02 +11001112 packet_put_int(options.tun_remote);
1113 packet_send();
1114 }
1115 }
1116
Damien Miller0e220db2004-06-15 10:34:08 +10001117 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
Damien Miller3756dce2004-06-18 01:17:29 +10001118 NULL, fileno(stdin), &command, environ, &ssh_subsystem_reply);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001119
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001120 packet_set_interactive(interactive);
Damien Miller1383bd82000-04-06 12:32:37 +10001121}
1122
Ben Lindstromf558cf62001-09-20 23:13:49 +00001123/* open new channel for a session */
Ben Lindstrombba81212001-06-25 05:01:22 +00001124static int
Ben Lindstromf558cf62001-09-20 23:13:49 +00001125ssh_session2_open(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001126{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001127 Channel *c;
1128 int window, packetmax, in, out, err;
Damien Millerad833b32000-08-23 10:46:23 +10001129
Damien Millercaf6dd62000-08-29 11:33:50 +11001130 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001131 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001132 } else {
1133 in = dup(STDIN_FILENO);
1134 }
1135 out = dup(STDOUT_FILENO);
1136 err = dup(STDERR_FILENO);
1137
1138 if (in < 0 || out < 0 || err < 0)
1139 fatal("dup() in/out/err failed");
1140
Damien Miller69b69aa2000-10-28 14:19:58 +11001141 /* enable nonblocking unless tty */
1142 if (!isatty(in))
1143 set_nonblock(in);
1144 if (!isatty(out))
1145 set_nonblock(out);
1146 if (!isatty(err))
1147 set_nonblock(err);
1148
Damien Millere4340be2000-09-16 13:29:08 +11001149 window = CHAN_SES_WINDOW_DEFAULT;
1150 packetmax = CHAN_SES_PACKET_DEFAULT;
Damien Miller19a59452002-02-19 15:20:57 +11001151 if (tty_flag) {
1152 window >>= 1;
1153 packetmax >>= 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001154 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001155 c = channel_new(
Damien Miller1383bd82000-04-06 12:32:37 +10001156 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001157 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001158 "client-session", /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001159
Ben Lindstromf558cf62001-09-20 23:13:49 +00001160 debug3("ssh_session2_open: channel_new: %d", c->self);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001161
Ben Lindstrom5ec26452001-06-09 00:18:51 +00001162 channel_send_open(c->self);
Ben Lindstromf558cf62001-09-20 23:13:49 +00001163 if (!no_shell_flag)
Damien Miller0e220db2004-06-15 10:34:08 +10001164 channel_register_confirm(c->self, ssh_session2_setup, NULL);
Damien Miller1383bd82000-04-06 12:32:37 +10001165
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001166 return c->self;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001167}
1168
Ben Lindstrombba81212001-06-25 05:01:22 +00001169static int
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001170ssh_session2(void)
1171{
Ben Lindstromf558cf62001-09-20 23:13:49 +00001172 int id = -1;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001173
1174 /* XXX should be pre-session */
1175 ssh_init_forwarding();
Damien Miller0e220db2004-06-15 10:34:08 +10001176 ssh_control_listener();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001177
Ben Lindstromf558cf62001-09-20 23:13:49 +00001178 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1179 id = ssh_session2_open();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001180
Damien Millerd27b9472005-12-13 19:29:02 +11001181 /* Execute a local command */
1182 if (options.local_command != NULL &&
1183 options.permit_local_command)
1184 ssh_local_cmd(options.local_command);
1185
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001186 /* If requested, let ssh continue in the background. */
1187 if (fork_after_authentication_flag)
1188 if (daemon(1, 1) < 0)
1189 fatal("daemon() failed: %.200s", strerror(errno));
1190
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001191 return client_loop(tty_flag, tty_flag ?
1192 options.escape_char : SSH_ESCAPECHAR_NONE, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001193}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001194
Ben Lindstrombba81212001-06-25 05:01:22 +00001195static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001196load_public_identity_files(void)
1197{
Damien Miller6b1d53c2006-03-31 23:13:21 +11001198 char *filename, *cp, thishost[NI_MAXHOST];
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001199 int i = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001200 Key *public;
Damien Miller6b1d53c2006-03-31 23:13:21 +11001201 struct passwd *pw;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001202#ifdef SMARTCARD
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001203 Key **keys;
1204
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001205 if (options.smartcard_device != NULL &&
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001206 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1207 (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL ) {
1208 int count = 0;
1209 for (i = 0; keys[i] != NULL; i++) {
1210 count++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001211 memmove(&options.identity_files[1], &options.identity_files[0],
1212 sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
1213 memmove(&options.identity_keys[1], &options.identity_keys[0],
1214 sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1215 options.num_identity_files++;
1216 options.identity_keys[0] = keys[i];
Damien Miller56a0bb02003-06-18 20:28:40 +10001217 options.identity_files[0] = sc_get_key_label(keys[i]);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001218 }
Ben Lindstrom4f054602002-03-26 03:23:00 +00001219 if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1220 options.num_identity_files = SSH_MAX_IDENTITY_FILES;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001221 i = count;
1222 xfree(keys);
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001223 }
Ben Lindstromffce1472001-08-06 21:57:31 +00001224#endif /* SMARTCARD */
Damien Miller6b1d53c2006-03-31 23:13:21 +11001225 if ((pw = getpwuid(original_real_uid)) == NULL)
1226 fatal("load_public_identity_files: getpwuid failed");
1227 if (gethostname(thishost, sizeof(thishost)) == -1)
1228 fatal("load_public_identity_files: gethostname: %s",
1229 strerror(errno));
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001230 for (; i < options.num_identity_files; i++) {
Damien Miller6b1d53c2006-03-31 23:13:21 +11001231 cp = tilde_expand_filename(options.identity_files[i],
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001232 original_real_uid);
Damien Miller6b1d53c2006-03-31 23:13:21 +11001233 filename = percent_expand(cp, "d", pw->pw_dir,
1234 "u", pw->pw_name, "l", thishost, "h", host,
1235 "r", options.user, (char *)NULL);
1236 xfree(cp);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001237 public = key_load_public(filename, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001238 debug("identity file %s type %d", filename,
1239 public ? public->type : -1);
1240 xfree(options.identity_files[i]);
1241 options.identity_files[i] = filename;
1242 options.identity_keys[i] = public;
1243 }
1244}
Damien Miller0e220db2004-06-15 10:34:08 +10001245
1246static void
1247control_client_sighandler(int signo)
1248{
1249 control_client_terminate = signo;
1250}
1251
1252static void
1253control_client_sigrelay(int signo)
1254{
1255 if (control_server_pid > 1)
1256 kill(control_server_pid, signo);
1257}
1258
Darren Tucker365433f2004-06-22 12:29:23 +10001259static int
1260env_permitted(char *env)
1261{
1262 int i;
1263 char name[1024], *cp;
1264
Damien Miller07d86be2006-03-26 14:19:21 +11001265 if (strlcpy(name, env, sizeof(name)) >= sizeof(name))
1266 fatal("env_permitted: name too long");
Darren Tucker365433f2004-06-22 12:29:23 +10001267 if ((cp = strchr(name, '=')) == NULL)
1268 return (0);
1269
1270 *cp = '\0';
1271
1272 for (i = 0; i < options.num_send_env; i++)
1273 if (match_pattern(name, options.send_env[i]))
1274 return (1);
1275
1276 return (0);
1277}
1278
Damien Miller0e220db2004-06-15 10:34:08 +10001279static void
1280control_client(const char *path)
1281{
1282 struct sockaddr_un addr;
Darren Tucker39207a42004-11-05 20:19:51 +11001283 int i, r, fd, sock, exitval, num_env, addr_len;
Damien Miller0e220db2004-06-15 10:34:08 +10001284 Buffer m;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001285 char *term;
Damien Miller3756dce2004-06-18 01:17:29 +10001286 extern char **environ;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001287 u_int flags;
Darren Tuckerfc959702004-07-17 16:12:08 +10001288
Damien Millerd14b1e72005-06-16 13:19:41 +10001289 if (mux_command == 0)
1290 mux_command = SSHMUX_COMMAND_OPEN;
1291
1292 switch (options.control_master) {
1293 case SSHCTL_MASTER_AUTO:
1294 case SSHCTL_MASTER_AUTO_ASK:
1295 debug("auto-mux: Trying existing master");
1296 /* FALLTHROUGH */
1297 case SSHCTL_MASTER_NO:
1298 break;
1299 default:
1300 return;
1301 }
1302
Damien Miller0e220db2004-06-15 10:34:08 +10001303 memset(&addr, '\0', sizeof(addr));
1304 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001305 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001306 strlen(path) + 1;
1307
1308 if (strlcpy(addr.sun_path, path,
1309 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1310 fatal("ControlPath too long");
1311
1312 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1313 fatal("%s socket(): %s", __func__, strerror(errno));
1314
Damien Miller90967402006-03-26 14:07:26 +11001315 if (connect(sock, (struct sockaddr *)&addr, addr_len) == -1) {
Darren Tucker0814d312005-06-01 23:08:51 +10001316 if (mux_command != SSHMUX_COMMAND_OPEN) {
1317 fatal("Control socket connect(%.100s): %s", path,
1318 strerror(errno));
1319 }
Damien Miller538c9b72005-05-26 12:11:28 +10001320 if (errno == ENOENT)
Damien Miller4662d342006-03-26 13:59:59 +11001321 debug("Control socket \"%.100s\" does not exist", path);
Damien Miller538c9b72005-05-26 12:11:28 +10001322 else {
Damien Miller4662d342006-03-26 13:59:59 +11001323 error("Control socket connect(%.100s): %s", path,
Damien Miller538c9b72005-05-26 12:11:28 +10001324 strerror(errno));
1325 }
Damien Miller4662d342006-03-26 13:59:59 +11001326 close(sock);
1327 return;
1328 }
Damien Miller46d38de2005-07-17 17:02:09 +10001329
Damien Miller4662d342006-03-26 13:59:59 +11001330 if (stdin_null_flag) {
1331 if ((fd = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1332 fatal("open(/dev/null): %s", strerror(errno));
1333 if (dup2(fd, STDIN_FILENO) == -1)
1334 fatal("dup2: %s", strerror(errno));
1335 if (fd > STDERR_FILENO)
1336 close(fd);
1337 }
Damien Miller46d38de2005-07-17 17:02:09 +10001338
Damien Miller13390022005-07-06 09:44:19 +10001339 term = getenv("TERM");
Darren Tucker7ebfc102004-11-07 20:06:19 +11001340
1341 flags = 0;
1342 if (tty_flag)
1343 flags |= SSHMUX_FLAG_TTY;
1344 if (subsystem_flag)
1345 flags |= SSHMUX_FLAG_SUBSYS;
Damien Miller13390022005-07-06 09:44:19 +10001346 if (options.forward_x11)
1347 flags |= SSHMUX_FLAG_X11_FWD;
1348 if (options.forward_agent)
1349 flags |= SSHMUX_FLAG_AGENT_FWD;
Damien Miller0e220db2004-06-15 10:34:08 +10001350
Damien Miller0e220db2004-06-15 10:34:08 +10001351 buffer_init(&m);
1352
Darren Tucker7ebfc102004-11-07 20:06:19 +11001353 /* Send our command to server */
1354 buffer_put_int(&m, mux_command);
1355 buffer_put_int(&m, flags);
Damien Miller13390022005-07-06 09:44:19 +10001356 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001357 fatal("%s: msg_send", __func__);
1358 buffer_clear(&m);
1359
1360 /* Get authorisation status and PID of controlee */
Damien Miller0e220db2004-06-15 10:34:08 +10001361 if (ssh_msg_recv(sock, &m) == -1)
1362 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001363 if (buffer_get_char(&m) != SSHMUX_VER)
Damien Miller0e220db2004-06-15 10:34:08 +10001364 fatal("%s: wrong version", __func__);
Damien Miller23f07702004-06-18 01:19:03 +10001365 if (buffer_get_int(&m) != 1)
1366 fatal("Connection to master denied");
Damien Miller0e220db2004-06-15 10:34:08 +10001367 control_server_pid = buffer_get_int(&m);
1368
Damien Miller0e220db2004-06-15 10:34:08 +10001369 buffer_clear(&m);
Damien Miller0e220db2004-06-15 10:34:08 +10001370
Darren Tucker7ebfc102004-11-07 20:06:19 +11001371 switch (mux_command) {
1372 case SSHMUX_COMMAND_ALIVE_CHECK:
Darren Tucker47eede72005-03-14 23:08:12 +11001373 fprintf(stderr, "Master running (pid=%d)\r\n",
Darren Tucker7ebfc102004-11-07 20:06:19 +11001374 control_server_pid);
1375 exit(0);
1376 case SSHMUX_COMMAND_TERMINATE:
1377 fprintf(stderr, "Exit request sent.\r\n");
1378 exit(0);
1379 case SSHMUX_COMMAND_OPEN:
1380 /* continue below */
1381 break;
1382 default:
1383 fatal("silly mux_command %d", mux_command);
1384 }
1385
1386 /* SSHMUX_COMMAND_OPEN */
Damien Miller13390022005-07-06 09:44:19 +10001387 buffer_put_cstring(&m, term ? term : "");
Damien Miller0e220db2004-06-15 10:34:08 +10001388 buffer_append(&command, "\0", 1);
1389 buffer_put_cstring(&m, buffer_ptr(&command));
1390
Darren Tucker365433f2004-06-22 12:29:23 +10001391 if (options.num_send_env == 0 || environ == NULL) {
1392 buffer_put_int(&m, 0);
Darren Tuckerfc959702004-07-17 16:12:08 +10001393 } else {
Darren Tucker365433f2004-06-22 12:29:23 +10001394 /* Pass environment */
1395 num_env = 0;
1396 for (i = 0; environ[i] != NULL; i++)
1397 if (env_permitted(environ[i]))
1398 num_env++; /* Count */
Darren Tuckerfc959702004-07-17 16:12:08 +10001399
Darren Tucker365433f2004-06-22 12:29:23 +10001400 buffer_put_int(&m, num_env);
1401
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001402 for (i = 0; environ[i] != NULL && num_env >= 0; i++)
1403 if (env_permitted(environ[i])) {
1404 num_env--;
Darren Tucker365433f2004-06-22 12:29:23 +10001405 buffer_put_cstring(&m, environ[i]);
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001406 }
Darren Tucker365433f2004-06-22 12:29:23 +10001407 }
Damien Miller3756dce2004-06-18 01:17:29 +10001408
Damien Miller13390022005-07-06 09:44:19 +10001409 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Damien Miller0e220db2004-06-15 10:34:08 +10001410 fatal("%s: msg_send", __func__);
1411
1412 mm_send_fd(sock, STDIN_FILENO);
1413 mm_send_fd(sock, STDOUT_FILENO);
1414 mm_send_fd(sock, STDERR_FILENO);
1415
1416 /* Wait for reply, so master has a chance to gather ttymodes */
1417 buffer_clear(&m);
1418 if (ssh_msg_recv(sock, &m) == -1)
1419 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001420 if (buffer_get_char(&m) != SSHMUX_VER)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001421 fatal("%s: wrong version", __func__);
Damien Miller0e220db2004-06-15 10:34:08 +10001422 buffer_free(&m);
1423
Darren Tucker07336da2004-11-05 20:02:16 +11001424 signal(SIGHUP, control_client_sighandler);
Damien Miller0809e232004-06-18 22:20:57 +10001425 signal(SIGINT, control_client_sighandler);
1426 signal(SIGTERM, control_client_sighandler);
1427 signal(SIGWINCH, control_client_sigrelay);
1428
Damien Miller0e220db2004-06-15 10:34:08 +10001429 if (tty_flag)
1430 enter_raw_mode();
1431
1432 /* Stick around until the controlee closes the client_fd */
1433 exitval = 0;
1434 for (;!control_client_terminate;) {
1435 r = read(sock, &exitval, sizeof(exitval));
1436 if (r == 0) {
1437 debug2("Received EOF from master");
1438 break;
1439 }
1440 if (r > 0)
1441 debug2("Received exit status from master %d", exitval);
1442 if (r == -1 && errno != EINTR)
1443 fatal("%s: read %s", __func__, strerror(errno));
1444 }
1445
1446 if (control_client_terminate)
1447 debug2("Exiting on signal %d", control_client_terminate);
1448
1449 close(sock);
1450
1451 leave_raw_mode();
1452
1453 if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
1454 fprintf(stderr, "Connection to master closed.\r\n");
1455
1456 exit(exitval);
1457}