blob: d9837f1620c785fba061f990fa162785e71a85d0 [file] [log] [blame]
Damien Miller9f2abc42006-07-10 20:53:08 +10001/* $OpenBSD: ssh.c,v 1.279 2006/07/06 16:03:53 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 Miller9f2abc42006-07-10 20:53:08 +100057#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110058#include <signal.h>
Damien Millereba71ba2000-04-29 23:57:08 +100059
60#include <openssl/evp.h>
Damien Miller0bc1bd82000-11-13 22:57:25 +110061#include <openssl/err.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000064#include "ssh1.h"
Damien Miller1383bd82000-04-06 12:32:37 +100065#include "ssh2.h"
66#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000067#include "cipher.h"
68#include "xmalloc.h"
69#include "packet.h"
70#include "buffer.h"
Damien Miller0e220db2004-06-15 10:34:08 +100071#include "bufaux.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000072#include "channels.h"
Damien Millereba71ba2000-04-29 23:57:08 +100073#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100074#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100075#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000076#include "pathnames.h"
Damien Miller0e220db2004-06-15 10:34:08 +100077#include "dispatch.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +000078#include "clientloop.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000079#include "log.h"
80#include "readconf.h"
81#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000082#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000083#include "kex.h"
84#include "mac.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +100085#include "sshpty.h"
Darren Tucker46bc0752004-05-02 22:11:30 +100086#include "match.h"
Damien Miller0e220db2004-06-15 10:34:08 +100087#include "msg.h"
88#include "monitor_fdpass.h"
Darren Tucker25f60a72004-08-15 17:23:34 +100089#include "uidswap.h"
Damien Millerb7576772006-07-10 20:23:39 +100090#include "version.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091
Ben Lindstromc5b68002001-07-04 04:52:03 +000092#ifdef SMARTCARD
Ben Lindstromc5b68002001-07-04 04:52:03 +000093#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000094#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +000095
Damien Miller3f905871999-11-15 17:10:57 +110096extern char *__progname;
Damien Miller3f905871999-11-15 17:10:57 +110097
Damien Miller95def091999-11-25 00:26:21 +110098/* Flag indicating whether debug mode is on. This can be set on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099int debug_flag = 0;
100
Damien Miller78928792000-04-12 20:17:38 +1000101/* Flag indicating whether a tty should be allocated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102int tty_flag = 0;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000103int no_tty_flag = 0;
104int force_tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105
Damien Miller1383bd82000-04-06 12:32:37 +1000106/* don't exec a shell */
107int no_shell_flag = 0;
Damien Miller1383bd82000-04-06 12:32:37 +1000108
Damien Miller5428f641999-11-25 11:54:57 +1100109/*
110 * Flag indicating that nothing should be read from stdin. This can be set
111 * on the command line.
112 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113int stdin_null_flag = 0;
114
Damien Miller5428f641999-11-25 11:54:57 +1100115/*
116 * Flag indicating that ssh should fork after authentication. This is useful
Ben Lindstromf666fec2002-06-06 19:51:58 +0000117 * so that the passphrase can be entered manually, and then ssh goes to the
Damien Miller5428f641999-11-25 11:54:57 +1100118 * background.
119 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120int fork_after_authentication_flag = 0;
121
Damien Miller5428f641999-11-25 11:54:57 +1100122/*
123 * General data structure for command line options and options configurable
124 * in configuration files. See readconf.h.
125 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126Options options;
127
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000128/* optional user configfile */
129char *config = NULL;
130
Damien Miller5428f641999-11-25 11:54:57 +1100131/*
132 * Name of the host we are connecting to. This is the name given on the
133 * command line, or the HostName specified for the user-supplied name in a
134 * configuration file.
135 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136char *host;
137
138/* socket address the host resolves to */
Damien Miller34132e52000-01-14 15:45:46 +1100139struct sockaddr_storage hostaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000141/* Private host keys. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000142Sensitive sensitive_data;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143
144/* Original real UID. */
145uid_t original_real_uid;
Ben Lindstromf9c48842002-06-11 16:37:51 +0000146uid_t original_effective_uid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000147
Damien Miller1383bd82000-04-06 12:32:37 +1000148/* command to be executed */
149Buffer command;
150
Damien Miller832562e2001-01-30 09:30:01 +1100151/* Should we execute a command or invoke a subsystem? */
152int subsystem_flag = 0;
153
Damien Miller2797f7f2002-04-23 21:09:44 +1000154/* # of replies received for global requests */
155static int client_global_request_id = 0;
156
Damien Miller8c4e18a2002-09-19 12:05:02 +1000157/* pid of proxycommand child process */
158pid_t proxy_command_pid = 0;
159
Damien Miller0e220db2004-06-15 10:34:08 +1000160/* fd to control socket */
161int control_fd = -1;
162
Darren Tucker7ebfc102004-11-07 20:06:19 +1100163/* Multiplexing control command */
Darren Tucker0814d312005-06-01 23:08:51 +1000164static u_int mux_command = 0;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100165
Damien Miller0e220db2004-06-15 10:34:08 +1000166/* Only used in control client mode */
167volatile sig_atomic_t control_client_terminate = 0;
168u_int control_server_pid = 0;
169
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170/* Prints a help message to the user. This function never returns. */
171
Ben Lindstrombba81212001-06-25 05:01:22 +0000172static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000173usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000174{
Damien Miller50955102004-03-22 09:34:58 +1100175 fprintf(stderr,
Darren Tucker9c6bf322004-12-03 14:10:19 +1100176"usage: ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n"
Darren Tucker895d6982005-10-03 18:18:05 +1000177" [-D [bind_address:]port] [-e escape_char] [-F configfile]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100178" [-i identity_file] [-L [bind_address:]port:host:hostport]\n"
Damien Millerf91ee4c2005-03-01 21:24:33 +1100179" [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100180" [-R [bind_address:]port:host:hostport] [-S ctl_path]\n"
Damien Miller991dba42006-07-10 20:16:27 +1000181" [-w local_tun[:remote_tun]] [user@]hostname [command]\n"
Damien Miller50955102004-03-22 09:34:58 +1100182 );
Darren Tuckere9a9b712005-12-20 16:15:51 +1100183 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000184}
185
Ben Lindstrombba81212001-06-25 05:01:22 +0000186static int ssh_session(void);
187static int ssh_session2(void);
188static void load_public_identity_files(void);
Damien Miller0e220db2004-06-15 10:34:08 +1000189static void control_client(const char *path);
Damien Miller1383bd82000-04-06 12:32:37 +1000190
Damien Miller95def091999-11-25 00:26:21 +1100191/*
192 * Main program for the ssh client.
193 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000194int
195main(int ac, char **av)
196{
Ben Lindstrom24157572002-06-12 16:09:39 +0000197 int i, opt, exit_status;
Damien Miller9836cf82003-12-17 16:30:06 +1100198 char *p, *cp, *line, buf[256];
Damien Miller95def091999-11-25 00:26:21 +1100199 struct stat st;
Ben Lindstrom086cf212001-03-05 05:56:40 +0000200 struct passwd *pw;
Damien Miller1383bd82000-04-06 12:32:37 +1000201 int dummy;
Ben Lindstrom5ccf63a2001-09-24 20:00:10 +0000202 extern int optind, optreset;
Damien Miller4f8e6692001-07-14 13:22:53 +1000203 extern char *optarg;
Damien Miller9651fe62005-06-26 08:55:25 +1000204 struct servent *sp;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100205 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000206
Darren Tuckerce321d82005-10-03 18:11:24 +1000207 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
208 sanitise_stdfd();
209
Damien Miller59d3d5b2003-08-22 09:34:41 +1000210 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000211 init_rng();
212
Damien Miller5428f641999-11-25 11:54:57 +1100213 /*
214 * Save the original real uid. It will be needed later (uid-swapping
215 * may clobber the real uid).
216 */
Damien Miller95def091999-11-25 00:26:21 +1100217 original_real_uid = getuid();
218 original_effective_uid = geteuid();
Damien Millera8e06ce2003-11-21 23:48:55 +1100219
Damien Miller50b9a602002-09-04 16:50:06 +1000220 /*
221 * Use uid-swapping to give up root privileges for the duration of
222 * option processing. We will re-instantiate the rights when we are
223 * ready to create the privileged port, and will permanently drop
224 * them when the port has been created (actually, when the connection
225 * has been made, as we may need to create the port several times).
226 */
227 PRIV_END;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000228
Damien Miller05dd7952000-10-01 00:42:48 +1100229#ifdef HAVE_SETRLIMIT
Damien Miller95def091999-11-25 00:26:21 +1100230 /* If we are installed setuid root be careful to not drop core. */
231 if (original_real_uid != original_effective_uid) {
232 struct rlimit rlim;
233 rlim.rlim_cur = rlim.rlim_max = 0;
234 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
235 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000236 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100237#endif
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000238 /* Get user data. */
239 pw = getpwuid(original_real_uid);
240 if (!pw) {
Damien Miller996acd22003-04-09 20:59:48 +1000241 logit("You don't exist, go away!");
Darren Tuckere9a9b712005-12-20 16:15:51 +1100242 exit(255);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000243 }
244 /* Take a copy of the returned structure. */
245 pw = pwcopy(pw);
246
Damien Miller5428f641999-11-25 11:54:57 +1100247 /*
Damien Miller5428f641999-11-25 11:54:57 +1100248 * Set our umask to something reasonable, as some files are created
249 * with the default umask. This will make them world-readable but
250 * writable only by the owner, which is ok for all files for which we
251 * don't set the modes explicitly.
252 */
Damien Miller95def091999-11-25 00:26:21 +1100253 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000254
Damien Miller95def091999-11-25 00:26:21 +1100255 /* Initialize option structure to indicate that no values have been set. */
256 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000257
Damien Miller95def091999-11-25 00:26:21 +1100258 /* Parse command-line arguments. */
259 host = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000260
Damien Miller2ecb6bd2006-03-15 12:03:53 +1100261 again:
Damien Millerf4614452001-07-14 12:18:10 +1000262 while ((opt = getopt(ac, av,
Damien Millerd27b9472005-12-13 19:29:02 +1100263 "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 +1100264 switch (opt) {
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000265 case '1':
266 options.protocol = SSH_PROTO_1;
267 break;
Damien Miller4af51302000-04-16 11:18:38 +1000268 case '2':
269 options.protocol = SSH_PROTO_2;
270 break;
Damien Miller34132e52000-01-14 15:45:46 +1100271 case '4':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000272 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100273 break;
Damien Miller34132e52000-01-14 15:45:46 +1100274 case '6':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000275 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100276 break;
Damien Miller95def091999-11-25 00:26:21 +1100277 case 'n':
278 stdin_null_flag = 1;
279 break;
Damien Miller95def091999-11-25 00:26:21 +1100280 case 'f':
281 fork_after_authentication_flag = 1;
282 stdin_null_flag = 1;
283 break;
Damien Miller95def091999-11-25 00:26:21 +1100284 case 'x':
285 options.forward_x11 = 0;
286 break;
Damien Miller95def091999-11-25 00:26:21 +1100287 case 'X':
288 options.forward_x11 = 1;
289 break;
Darren Tucker0a118da2003-10-15 15:54:32 +1000290 case 'Y':
291 options.forward_x11 = 1;
292 options.forward_x11_trusted = 1;
293 break;
Damien Miller95def091999-11-25 00:26:21 +1100294 case 'g':
295 options.gateway_ports = 1;
296 break;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100297 case 'O':
298 if (strcmp(optarg, "check") == 0)
299 mux_command = SSHMUX_COMMAND_ALIVE_CHECK;
300 else if (strcmp(optarg, "exit") == 0)
301 mux_command = SSHMUX_COMMAND_TERMINATE;
302 else
303 fatal("Invalid multiplex command.");
304 break;
Damien Miller147bba32002-09-04 16:46:06 +1000305 case 'P': /* deprecated */
Damien Miller95def091999-11-25 00:26:21 +1100306 options.use_privileged_port = 0;
307 break;
Damien Miller95def091999-11-25 00:26:21 +1100308 case 'a':
309 options.forward_agent = 0;
310 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000311 case 'A':
312 options.forward_agent = 1;
313 break;
Damien Miller95def091999-11-25 00:26:21 +1100314 case 'k':
Damien Millere0113cc2003-11-24 13:10:09 +1100315 options.gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100316 break;
Damien Miller95def091999-11-25 00:26:21 +1100317 case 'i':
318 if (stat(optarg, &st) < 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000319 fprintf(stderr, "Warning: Identity file %s "
Damien Miller3eb48b62005-03-01 21:15:46 +1100320 "not accessible: %s.\n", optarg,
321 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100322 break;
323 }
Damien Millerf4614452001-07-14 12:18:10 +1000324 if (options.num_identity_files >=
325 SSH_MAX_IDENTITY_FILES)
326 fatal("Too many identity files specified "
327 "(max %d)", SSH_MAX_IDENTITY_FILES);
328 options.identity_files[options.num_identity_files++] =
329 xstrdup(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100330 break;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000331 case 'I':
332#ifdef SMARTCARD
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000333 options.smartcard_device = xstrdup(optarg);
Ben Lindstrombcc18082001-08-06 21:59:25 +0000334#else
Ben Lindstromc5b68002001-07-04 04:52:03 +0000335 fprintf(stderr, "no support for smartcards.\n");
Ben Lindstrombcc18082001-08-06 21:59:25 +0000336#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000337 break;
Damien Miller95def091999-11-25 00:26:21 +1100338 case 't':
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000339 if (tty_flag)
340 force_tty_flag = 1;
Damien Miller95def091999-11-25 00:26:21 +1100341 tty_flag = 1;
342 break;
Damien Miller95def091999-11-25 00:26:21 +1100343 case 'v':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000344 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100345 debug_flag = 1;
346 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000347 } else {
348 if (options.log_level < SYSLOG_LEVEL_DEBUG3)
349 options.log_level++;
Damien Millere4340be2000-09-16 13:29:08 +1100350 break;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000351 }
Darren Tuckere9bf9842004-11-05 20:05:32 +1100352 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100353 case 'V':
Damien Miller0c889cd2004-03-22 09:36:00 +1100354 fprintf(stderr, "%s, %s\n",
Damien Miller2aa6d3c2004-09-12 16:53:04 +1000355 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
Damien Miller95def091999-11-25 00:26:21 +1100356 if (opt == 'V')
357 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100358 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100359 case 'w':
Damien Miller7b58e802005-12-13 19:33:19 +1100360 if (options.tun_open == -1)
361 options.tun_open = SSH_TUNMODE_DEFAULT;
Damien Millerd27b9472005-12-13 19:29:02 +1100362 options.tun_local = a2tun(optarg, &options.tun_remote);
Damien Miller7b58e802005-12-13 19:33:19 +1100363 if (options.tun_local == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100364 fprintf(stderr, "Bad tun device '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100365 exit(255);
Damien Millerd27b9472005-12-13 19:29:02 +1100366 }
367 break;
Damien Miller95def091999-11-25 00:26:21 +1100368 case 'q':
369 options.log_level = SYSLOG_LEVEL_QUIET;
370 break;
Damien Miller95def091999-11-25 00:26:21 +1100371 case 'e':
372 if (optarg[0] == '^' && optarg[2] == 0 &&
Damien Millerf4614452001-07-14 12:18:10 +1000373 (u_char) optarg[1] >= 64 &&
374 (u_char) optarg[1] < 128)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000375 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100376 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000377 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100378 else if (strcmp(optarg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000379 options.escape_char = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100380 else {
Damien Millerf4614452001-07-14 12:18:10 +1000381 fprintf(stderr, "Bad escape character '%s'.\n",
382 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100383 exit(255);
Damien Miller95def091999-11-25 00:26:21 +1100384 }
385 break;
Damien Miller95def091999-11-25 00:26:21 +1100386 case 'c':
Damien Millereba71ba2000-04-29 23:57:08 +1000387 if (ciphers_valid(optarg)) {
388 /* SSH2 only */
389 options.ciphers = xstrdup(optarg);
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000390 options.cipher = SSH_CIPHER_INVALID;
Damien Millereba71ba2000-04-29 23:57:08 +1000391 } else {
392 /* SSH1 only */
Damien Millere39cacc2000-11-29 12:18:44 +1100393 options.cipher = cipher_number(optarg);
394 if (options.cipher == -1) {
Damien Millerf4614452001-07-14 12:18:10 +1000395 fprintf(stderr,
396 "Unknown cipher type '%s'\n",
397 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100398 exit(255);
Damien Millereba71ba2000-04-29 23:57:08 +1000399 }
Damien Millerf4614452001-07-14 12:18:10 +1000400 if (options.cipher == SSH_CIPHER_3DES)
Damien Millere39cacc2000-11-29 12:18:44 +1100401 options.ciphers = "3des-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000402 else if (options.cipher == SSH_CIPHER_BLOWFISH)
Damien Millere39cacc2000-11-29 12:18:44 +1100403 options.ciphers = "blowfish-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000404 else
Damien Millere39cacc2000-11-29 12:18:44 +1100405 options.ciphers = (char *)-1;
Damien Miller95def091999-11-25 00:26:21 +1100406 }
407 break;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000408 case 'm':
409 if (mac_valid(optarg))
410 options.macs = xstrdup(optarg);
411 else {
Damien Millerf4614452001-07-14 12:18:10 +1000412 fprintf(stderr, "Unknown mac type '%s'\n",
413 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100414 exit(255);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000415 }
416 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000417 case 'M':
Damien Millerd14b1e72005-06-16 13:19:41 +1000418 if (options.control_master == SSHCTL_MASTER_YES)
419 options.control_master = SSHCTL_MASTER_ASK;
420 else
421 options.control_master = SSHCTL_MASTER_YES;
Damien Miller0e220db2004-06-15 10:34:08 +1000422 break;
Damien Miller95def091999-11-25 00:26:21 +1100423 case 'p':
Ben Lindstrom19066a12001-04-12 23:39:26 +0000424 options.port = a2port(optarg);
425 if (options.port == 0) {
Ben Lindstrom146edb92001-04-11 23:06:28 +0000426 fprintf(stderr, "Bad port '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100427 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000428 }
Damien Miller95def091999-11-25 00:26:21 +1100429 break;
Damien Miller95def091999-11-25 00:26:21 +1100430 case 'l':
431 options.user = optarg;
432 break;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000433
Damien Miller95def091999-11-25 00:26:21 +1100434 case 'L':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100435 if (parse_forward(&fwd, optarg))
436 add_local_forward(&options, &fwd);
437 else {
Damien Millerf4614452001-07-14 12:18:10 +1000438 fprintf(stderr,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100439 "Bad local forwarding specification '%s'\n",
Damien Millerf4614452001-07-14 12:18:10 +1000440 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100441 exit(255);
Ben Lindstrom1a174712001-09-12 17:56:15 +0000442 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100443 break;
444
445 case 'R':
446 if (parse_forward(&fwd, optarg)) {
447 add_remote_forward(&options, &fwd);
448 } else {
449 fprintf(stderr,
450 "Bad remote forwarding specification "
451 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100452 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100453 }
Damien Miller95def091999-11-25 00:26:21 +1100454 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000455
456 case 'D':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100457 cp = p = xstrdup(optarg);
458 memset(&fwd, '\0', sizeof(fwd));
459 fwd.connect_host = "socks";
460 if ((fwd.listen_host = hpdelim(&cp)) == NULL) {
461 fprintf(stderr, "Bad dynamic forwarding "
462 "specification '%.100s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100463 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100464 }
465 if (cp != NULL) {
466 fwd.listen_port = a2port(cp);
467 fwd.listen_host = cleanhostname(fwd.listen_host);
468 } else {
469 fwd.listen_port = a2port(fwd.listen_host);
Damien Millerbe1045d2005-08-12 22:10:56 +1000470 fwd.listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100471 }
472
473 if (fwd.listen_port == 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000474 fprintf(stderr, "Bad dynamic port '%s'\n",
475 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100476 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000477 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100478 add_local_forward(&options, &fwd);
479 xfree(p);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000480 break;
481
Damien Miller95def091999-11-25 00:26:21 +1100482 case 'C':
483 options.compression = 1;
484 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000485 case 'N':
486 no_shell_flag = 1;
487 no_tty_flag = 1;
488 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000489 case 'T':
490 no_tty_flag = 1;
491 break;
Damien Miller95def091999-11-25 00:26:21 +1100492 case 'o':
493 dummy = 1;
Damien Miller9836cf82003-12-17 16:30:06 +1100494 line = xstrdup(optarg);
Damien Millerf4614452001-07-14 12:18:10 +1000495 if (process_config_line(&options, host ? host : "",
Damien Miller9836cf82003-12-17 16:30:06 +1100496 line, "command-line", 0, &dummy) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100497 exit(255);
Damien Miller9836cf82003-12-17 16:30:06 +1100498 xfree(line);
Damien Miller95def091999-11-25 00:26:21 +1100499 break;
Damien Miller832562e2001-01-30 09:30:01 +1100500 case 's':
501 subsystem_flag = 1;
502 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000503 case 'S':
504 if (options.control_path != NULL)
505 free(options.control_path);
506 options.control_path = xstrdup(optarg);
Damien Miller0e220db2004-06-15 10:34:08 +1000507 break;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000508 case 'b':
509 options.bind_address = optarg;
510 break;
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000511 case 'F':
512 config = optarg;
513 break;
Damien Miller95def091999-11-25 00:26:21 +1100514 default:
515 usage();
516 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000517 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000518
Damien Millerf4614452001-07-14 12:18:10 +1000519 ac -= optind;
520 av += optind;
521
522 if (ac > 0 && !host && **av != '-') {
Ben Lindstromc276c122002-12-23 02:14:51 +0000523 if (strrchr(*av, '@')) {
Damien Millerf4614452001-07-14 12:18:10 +1000524 p = xstrdup(*av);
Ben Lindstromc276c122002-12-23 02:14:51 +0000525 cp = strrchr(p, '@');
Damien Millerf4614452001-07-14 12:18:10 +1000526 if (cp == NULL || cp == p)
527 usage();
528 options.user = p;
529 *cp = '\0';
530 host = ++cp;
531 } else
532 host = *av;
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000533 if (ac > 1) {
534 optind = optreset = 1;
Damien Millerf4614452001-07-14 12:18:10 +1000535 goto again;
536 }
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000537 ac--, av++;
Damien Millerf4614452001-07-14 12:18:10 +1000538 }
539
Damien Miller95def091999-11-25 00:26:21 +1100540 /* Check that we got a host name. */
541 if (!host)
542 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000543
Damien Millercb5e44a2000-09-29 12:12:36 +1100544 SSLeay_add_all_algorithms();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100545 ERR_load_crypto_strings();
Damien Millercb5e44a2000-09-29 12:12:36 +1100546
Damien Miller95def091999-11-25 00:26:21 +1100547 /* Initialize the command to execute on remote host. */
548 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000549
Damien Miller5428f641999-11-25 11:54:57 +1100550 /*
551 * Save the command to execute on the remote host in a buffer. There
552 * is no limit on the length of the command, except by the maximum
553 * packet size. Also sets the tty flag if there is no command.
554 */
Damien Millerf4614452001-07-14 12:18:10 +1000555 if (!ac) {
Damien Miller95def091999-11-25 00:26:21 +1100556 /* No command specified - execute shell on a tty. */
557 tty_flag = 1;
Damien Miller832562e2001-01-30 09:30:01 +1100558 if (subsystem_flag) {
Damien Millerf4614452001-07-14 12:18:10 +1000559 fprintf(stderr,
560 "You must specify a subsystem to invoke.\n");
Damien Miller832562e2001-01-30 09:30:01 +1100561 usage();
562 }
Damien Miller95def091999-11-25 00:26:21 +1100563 } else {
Damien Millerf4614452001-07-14 12:18:10 +1000564 /* A command has been specified. Store it into the buffer. */
565 for (i = 0; i < ac; i++) {
566 if (i)
Damien Miller95def091999-11-25 00:26:21 +1100567 buffer_append(&command, " ", 1);
568 buffer_append(&command, av[i], strlen(av[i]));
569 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000570 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000571
Damien Miller95def091999-11-25 00:26:21 +1100572 /* Cannot fork to background if no command. */
Damien Millercaf6dd62000-08-29 11:33:50 +1100573 if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
Damien Miller95def091999-11-25 00:26:21 +1100574 fatal("Cannot fork into background without a command to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000575
Damien Miller95def091999-11-25 00:26:21 +1100576 /* Allocate a tty by default if no command specified. */
577 if (buffer_len(&command) == 0)
578 tty_flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000579
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000580 /* Force no tty */
Ben Lindstromc72745a2000-12-02 19:03:54 +0000581 if (no_tty_flag)
582 tty_flag = 0;
Damien Miller95def091999-11-25 00:26:21 +1100583 /* Do not allocate a tty if stdin is not a tty. */
Damien Millerddee5752005-05-26 12:05:05 +1000584 if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100585 if (tty_flag)
Damien Miller996acd22003-04-09 20:59:48 +1000586 logit("Pseudo-terminal will not be allocated because stdin is not a terminal.");
Damien Miller95def091999-11-25 00:26:21 +1100587 tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000588 }
Damien Miller1383bd82000-04-06 12:32:37 +1000589
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000590 /*
591 * Initialize "log" output. Since we are the client all output
592 * actually goes to stderr.
593 */
Ben Lindstrom2b646522001-04-12 16:16:57 +0000594 log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
595 SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000596
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000597 /*
598 * Read per-user configuration file. Ignore the system wide config
599 * file if the user specifies a config file on the command line.
600 */
601 if (config != NULL) {
Damien Miller914420f2004-04-20 20:14:07 +1000602 if (!read_config_file(config, host, &options, 0))
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000603 fatal("Can't open user config file %.100s: "
604 "%.100s", config, strerror(errno));
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000605 } else {
606 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
607 _PATH_SSH_USER_CONFFILE);
Damien Miller57a44762004-04-20 20:11:57 +1000608 (void)read_config_file(buf, host, &options, 1);
Ben Lindstrom83f07d12001-10-03 17:22:29 +0000609
610 /* Read systemwide configuration file after use config. */
Darren Tuckerfc959702004-07-17 16:12:08 +1000611 (void)read_config_file(_PATH_HOST_CONFIG_FILE, host,
Damien Miller57a44762004-04-20 20:11:57 +1000612 &options, 0);
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000613 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000614
Damien Miller95def091999-11-25 00:26:21 +1100615 /* Fill configuration defaults. */
616 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000617
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000618 channel_set_af(options.address_family);
619
Damien Miller95def091999-11-25 00:26:21 +1100620 /* reinit */
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000621 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000622
Damien Miller60bc5172001-03-19 09:38:15 +1100623 seed_rng();
624
Damien Miller95def091999-11-25 00:26:21 +1100625 if (options.user == NULL)
626 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000627
Damien Miller95def091999-11-25 00:26:21 +1100628 if (options.hostname != NULL)
629 host = options.hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000630
Darren Tucker3f521e22003-07-03 16:20:42 +1000631 /* force lowercase for hostkey matching */
632 if (options.host_key_alias != NULL) {
633 for (p = options.host_key_alias; *p; p++)
634 if (isupper(*p))
Damien Miller1d2b6702006-03-26 14:09:54 +1100635 *p = (char)tolower(*p);
Darren Tucker3f521e22003-07-03 16:20:42 +1000636 }
637
Damien Miller7c71cc72005-06-26 08:56:31 +1000638 /* Get default port if port has not been set. */
639 if (options.port == 0) {
640 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
641 options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
642 }
643
Damien Miller9f1e33a2003-02-24 11:57:32 +1100644 if (options.proxy_command != NULL &&
645 strcmp(options.proxy_command, "none") == 0)
646 options.proxy_command = NULL;
Damien Miller8f74c8f2005-06-26 08:56:03 +1000647 if (options.control_path != NULL &&
648 strcmp(options.control_path, "none") == 0)
649 options.control_path = NULL;
Damien Miller9f1e33a2003-02-24 11:57:32 +1100650
Damien Miller0e220db2004-06-15 10:34:08 +1000651 if (options.control_path != NULL) {
Damien Miller6b1d53c2006-03-31 23:13:21 +1100652 char thishost[NI_MAXHOST];
Damien Miller3ec54c72006-03-15 11:30:13 +1100653
Damien Miller6b1d53c2006-03-31 23:13:21 +1100654 if (gethostname(thishost, sizeof(thishost)) == -1)
Damien Miller3ec54c72006-03-15 11:30:13 +1100655 fatal("gethostname: %s", strerror(errno));
Damien Miller6476cad2005-06-16 13:18:34 +1000656 snprintf(buf, sizeof(buf), "%d", options.port);
657 cp = tilde_expand_filename(options.control_path,
658 original_real_uid);
659 options.control_path = percent_expand(cp, "p", buf, "h", host,
Damien Miller6b1d53c2006-03-31 23:13:21 +1100660 "r", options.user, "l", thishost, (char *)NULL);
Damien Miller6476cad2005-06-16 13:18:34 +1000661 xfree(cp);
Damien Miller0e220db2004-06-15 10:34:08 +1000662 }
Darren Tucker0814d312005-06-01 23:08:51 +1000663 if (mux_command != 0 && options.control_path == NULL)
664 fatal("No ControlPath specified for \"-O\" command");
Damien Millerd14b1e72005-06-16 13:19:41 +1000665 if (options.control_path != NULL)
Damien Millerdadfd4d2005-05-26 12:07:13 +1000666 control_client(options.control_path);
Damien Miller0e220db2004-06-15 10:34:08 +1000667
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000668 /* Open a connection to the remote host. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000669 if (ssh_connect(host, &hostaddr, options.port,
670 options.address_family, options.connection_attempts,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000671#ifdef HAVE_CYGWIN
672 options.use_privileged_port,
673#else
Ben Lindstromf9c48842002-06-11 16:37:51 +0000674 original_effective_uid == 0 && options.use_privileged_port,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000675#endif
Ben Lindstromda394ca2002-06-12 16:11:12 +0000676 options.proxy_command) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100677 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000678
Damien Miller5428f641999-11-25 11:54:57 +1100679 /*
680 * If we successfully made the connection, load the host private key
681 * in case we will need it later for combined rsa-rhosts
682 * authentication. This must be done before releasing extra
683 * privileges, because the file is only readable by root.
Ben Lindstrom9e5bb572002-06-06 19:58:27 +0000684 * If we cannot access the private keys, load the public keys
685 * instead and try to execute the ssh-keysign helper instead.
Damien Miller5428f641999-11-25 11:54:57 +1100686 */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000687 sensitive_data.nkeys = 0;
688 sensitive_data.keys = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000689 sensitive_data.external_keysign = 0;
Ben Lindstrom24157572002-06-12 16:09:39 +0000690 if (options.rhosts_rsa_authentication ||
691 options.hostbased_authentication) {
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000692 sensitive_data.nkeys = 3;
Damien Millerddd63ab2006-03-31 23:10:51 +1100693 sensitive_data.keys = xcalloc(sensitive_data.nkeys,
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000694 sizeof(Key));
Ben Lindstromf9c48842002-06-11 16:37:51 +0000695
696 PRIV_START;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000697 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
Darren Tucker232b76f2006-05-06 17:41:51 +1000698 _PATH_HOST_KEY_FILE, "", NULL, NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000699 sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
Darren Tucker232b76f2006-05-06 17:41:51 +1000700 _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000701 sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
Darren Tucker232b76f2006-05-06 17:41:51 +1000702 _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL);
Ben Lindstromf9c48842002-06-11 16:37:51 +0000703 PRIV_END;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000704
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +0000705 if (options.hostbased_authentication == 1 &&
706 sensitive_data.keys[0] == NULL &&
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000707 sensitive_data.keys[1] == NULL &&
708 sensitive_data.keys[2] == NULL) {
709 sensitive_data.keys[1] = key_load_public(
710 _PATH_HOST_DSA_KEY_FILE, NULL);
711 sensitive_data.keys[2] = key_load_public(
712 _PATH_HOST_RSA_KEY_FILE, NULL);
713 sensitive_data.external_keysign = 1;
714 }
Damien Miller95def091999-11-25 00:26:21 +1100715 }
Damien Miller5428f641999-11-25 11:54:57 +1100716 /*
717 * Get rid of any extra privileges that we may have. We will no
718 * longer need them. Also, extra privileges could make it very hard
719 * to read identity files and other non-world-readable files from the
720 * user's home directory if it happens to be on a NFS volume where
721 * root is mapped to nobody.
722 */
Darren Tucker25f60a72004-08-15 17:23:34 +1000723 if (original_effective_uid == 0) {
724 PRIV_START;
725 permanently_set_uid(pw);
726 }
Damien Miller95def091999-11-25 00:26:21 +1100727
Damien Miller5428f641999-11-25 11:54:57 +1100728 /*
729 * Now that we are back to our own permissions, create ~/.ssh
Damien Miller788f2122005-11-05 15:14:59 +1100730 * directory if it doesn't already exist.
Damien Miller5428f641999-11-25 11:54:57 +1100731 */
Ben Lindstrom930b14a2001-08-15 23:19:21 +0000732 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 +1100733 if (stat(buf, &st) < 0)
Damien Millerbe484b52000-07-15 14:14:16 +1000734 if (mkdir(buf, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100735 error("Could not create directory '%.200s'.", buf);
736
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000737 /* load options.identity_files */
738 load_public_identity_files();
739
740 /* Expand ~ in known host file names. */
741 /* XXX mem-leaks: */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100742 options.system_hostfile =
743 tilde_expand_filename(options.system_hostfile, original_real_uid);
744 options.user_hostfile =
745 tilde_expand_filename(options.user_hostfile, original_real_uid);
746 options.system_hostfile2 =
747 tilde_expand_filename(options.system_hostfile2, original_real_uid);
748 options.user_hostfile2 =
749 tilde_expand_filename(options.user_hostfile2, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100750
Damien Miller07cd5892001-11-12 10:52:03 +1100751 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
752
Damien Miller95def091999-11-25 00:26:21 +1100753 /* Log into the remote system. This never returns if the login fails. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000754 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, pw);
Damien Miller95def091999-11-25 00:26:21 +1100755
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000756 /* We no longer need the private host keys. Clear them now. */
757 if (sensitive_data.nkeys != 0) {
758 for (i = 0; i < sensitive_data.nkeys; i++) {
759 if (sensitive_data.keys[i] != NULL) {
760 /* Destroys contents safely */
761 debug3("clear hostkey %d", i);
762 key_free(sensitive_data.keys[i]);
763 sensitive_data.keys[i] = NULL;
764 }
765 }
766 xfree(sensitive_data.keys);
767 }
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +0000768 for (i = 0; i < options.num_identity_files; i++) {
769 if (options.identity_files[i]) {
770 xfree(options.identity_files[i]);
771 options.identity_files[i] = NULL;
772 }
773 if (options.identity_keys[i]) {
774 key_free(options.identity_keys[i]);
775 options.identity_keys[i] = NULL;
776 }
777 }
Damien Miller95def091999-11-25 00:26:21 +1100778
Damien Miller1383bd82000-04-06 12:32:37 +1000779 exit_status = compat20 ? ssh_session2() : ssh_session();
780 packet_close();
Damien Miller8c4e18a2002-09-19 12:05:02 +1000781
Damien Miller0e220db2004-06-15 10:34:08 +1000782 if (options.control_path != NULL && control_fd != -1)
783 unlink(options.control_path);
784
Damien Miller8c4e18a2002-09-19 12:05:02 +1000785 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100786 * Send SIGHUP to proxy command if used. We don't wait() in
Damien Miller8c4e18a2002-09-19 12:05:02 +1000787 * case it hangs and instead rely on init to reap the child
788 */
789 if (proxy_command_pid > 1)
790 kill(proxy_command_pid, SIGHUP);
791
Damien Miller1383bd82000-04-06 12:32:37 +1000792 return exit_status;
793}
794
Ben Lindstrombba81212001-06-25 05:01:22 +0000795static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100796ssh_init_forwarding(void)
797{
Kevin Steves12057502001-02-05 14:54:34 +0000798 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100799 int i;
Kevin Steves12057502001-02-05 14:54:34 +0000800
Damien Miller0bc1bd82000-11-13 22:57:25 +1100801 /* Initiate local TCP/IP port forwardings. */
802 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100803 debug("Local connections to %.200s:%d forwarded to remote "
804 "address %.200s:%d",
Darren Tucker47eede72005-03-14 23:08:12 +1100805 (options.local_forwards[i].listen_host == NULL) ?
806 (options.gateway_ports ? "*" : "LOCALHOST") :
Damien Millerf91ee4c2005-03-01 21:24:33 +1100807 options.local_forwards[i].listen_host,
808 options.local_forwards[i].listen_port,
809 options.local_forwards[i].connect_host,
810 options.local_forwards[i].connect_port);
Damien Millerb16461c2002-01-22 23:29:22 +1100811 success += channel_setup_local_fwd_listener(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100812 options.local_forwards[i].listen_host,
813 options.local_forwards[i].listen_port,
814 options.local_forwards[i].connect_host,
815 options.local_forwards[i].connect_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100816 options.gateway_ports);
817 }
Kevin Steves12057502001-02-05 14:54:34 +0000818 if (i > 0 && success == 0)
819 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100820
821 /* Initiate remote TCP/IP port forwardings. */
822 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100823 debug("Remote connections from %.200s:%d forwarded to "
824 "local address %.200s:%d",
Damien Miller46d38de2005-07-17 17:02:09 +1000825 (options.remote_forwards[i].listen_host == NULL) ?
Damien Milleraa3bb102005-11-05 15:12:59 +1100826 "LOCALHOST" : options.remote_forwards[i].listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100827 options.remote_forwards[i].listen_port,
828 options.remote_forwards[i].connect_host,
829 options.remote_forwards[i].connect_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100830 channel_request_remote_forwarding(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100831 options.remote_forwards[i].listen_host,
832 options.remote_forwards[i].listen_port,
833 options.remote_forwards[i].connect_host,
834 options.remote_forwards[i].connect_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100835 }
836}
837
Ben Lindstrombba81212001-06-25 05:01:22 +0000838static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100839check_agent_present(void)
840{
841 if (options.forward_agent) {
Damien Miller788f2122005-11-05 15:14:59 +1100842 /* Clear agent forwarding if we don't have an agent. */
Damien Miller789e95d2002-09-12 09:52:46 +1000843 if (!ssh_agent_present())
Damien Miller0bc1bd82000-11-13 22:57:25 +1100844 options.forward_agent = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100845 }
846}
847
Ben Lindstrombba81212001-06-25 05:01:22 +0000848static int
Damien Miller1383bd82000-04-06 12:32:37 +1000849ssh_session(void)
850{
851 int type;
Damien Miller1383bd82000-04-06 12:32:37 +1000852 int interactive = 0;
853 int have_tty = 0;
854 struct winsize ws;
Damien Miller1383bd82000-04-06 12:32:37 +1000855 char *cp;
Damien Miller17e7ed02005-06-17 12:54:33 +1000856 const char *display;
Damien Miller1383bd82000-04-06 12:32:37 +1000857
Damien Miller95def091999-11-25 00:26:21 +1100858 /* Enable compression if requested. */
859 if (options.compression) {
860 debug("Requesting compression at level %d.", options.compression_level);
861
862 if (options.compression_level < 1 || options.compression_level > 9)
863 fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
864
865 /* Send the request. */
866 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
867 packet_put_int(options.compression_level);
868 packet_send();
869 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100870 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100871 if (type == SSH_SMSG_SUCCESS)
872 packet_start_compression(options.compression_level);
873 else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000874 logit("Warning: Remote host refused compression.");
Damien Miller95def091999-11-25 00:26:21 +1100875 else
876 packet_disconnect("Protocol error waiting for compression response.");
877 }
878 /* Allocate a pseudo tty if appropriate. */
879 if (tty_flag) {
880 debug("Requesting pty.");
881
882 /* Start the packet. */
883 packet_start(SSH_CMSG_REQUEST_PTY);
884
885 /* Store TERM in the packet. There is no limit on the
886 length of the string. */
887 cp = getenv("TERM");
888 if (!cp)
889 cp = "";
Ben Lindstrom664408d2001-06-09 01:42:01 +0000890 packet_put_cstring(cp);
Damien Miller95def091999-11-25 00:26:21 +1100891
892 /* Store window size in the packet. */
893 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
894 memset(&ws, 0, sizeof(ws));
Damien Miller71a73672006-03-26 14:04:36 +1100895 packet_put_int((u_int)ws.ws_row);
896 packet_put_int((u_int)ws.ws_col);
897 packet_put_int((u_int)ws.ws_xpixel);
898 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller95def091999-11-25 00:26:21 +1100899
900 /* Store tty modes in the packet. */
Ben Lindstromae8e2d32001-04-14 23:13:02 +0000901 tty_make_modes(fileno(stdin), NULL);
Damien Miller95def091999-11-25 00:26:21 +1100902
903 /* Send the packet, and wait for it to leave. */
904 packet_send();
905 packet_write_wait();
906
907 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100908 type = packet_read();
Damien Miller450a7a12000-03-26 13:04:51 +1000909 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100910 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +1000911 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000912 } else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000913 logit("Warning: Remote host failed or refused to allocate a pseudo tty.");
Damien Miller95def091999-11-25 00:26:21 +1100914 else
915 packet_disconnect("Protocol error waiting for pty request response.");
916 }
917 /* Request X11 forwarding if enabled and DISPLAY is set. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000918 display = getenv("DISPLAY");
919 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000920 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +1000921 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000922 client_x11_get_proto(display, options.xauth_location,
923 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +1000924 /* Request forwarding with authentication spoofing. */
Damien Miller95def091999-11-25 00:26:21 +1100925 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +1000926 x11_request_forwarding_with_spoofing(0, display, proto, data);
Damien Miller95def091999-11-25 00:26:21 +1100927
928 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100929 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100930 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100931 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +1000932 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller996acd22003-04-09 20:59:48 +1000933 logit("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +1000934 } else {
Damien Miller95def091999-11-25 00:26:21 +1100935 packet_disconnect("Protocol error waiting for X11 forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +1000936 }
Damien Miller95def091999-11-25 00:26:21 +1100937 }
938 /* Tell the packet module whether this is an interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000939 packet_set_interactive(interactive);
Damien Miller95def091999-11-25 00:26:21 +1100940
941 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100942 check_agent_present();
943
Damien Miller95def091999-11-25 00:26:21 +1100944 if (options.forward_agent) {
945 debug("Requesting authentication agent forwarding.");
946 auth_request_forwarding();
947
948 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100949 type = packet_read();
Damien Miller48b03fc2002-01-22 23:11:40 +1100950 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100951 if (type != SSH_SMSG_SUCCESS)
Damien Miller996acd22003-04-09 20:59:48 +1000952 logit("Warning: Remote host denied authentication agent forwarding.");
Damien Miller95def091999-11-25 00:26:21 +1100953 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000954
Damien Miller0bc1bd82000-11-13 22:57:25 +1100955 /* Initiate port forwardings. */
956 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000957
Damien Miller5428f641999-11-25 11:54:57 +1100958 /* If requested, let ssh continue in the background. */
Damien Miller4af51302000-04-16 11:18:38 +1000959 if (fork_after_authentication_flag)
Damien Miller5428f641999-11-25 11:54:57 +1100960 if (daemon(1, 1) < 0)
961 fatal("daemon() failed: %.200s", strerror(errno));
962
963 /*
964 * If a command was specified on the command line, execute the
965 * command now. Otherwise request the server to start a shell.
966 */
Damien Miller95def091999-11-25 00:26:21 +1100967 if (buffer_len(&command) > 0) {
968 int len = buffer_len(&command);
969 if (len > 900)
970 len = 900;
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100971 debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
Damien Miller95def091999-11-25 00:26:21 +1100972 packet_start(SSH_CMSG_EXEC_CMD);
973 packet_put_string(buffer_ptr(&command), buffer_len(&command));
974 packet_send();
975 packet_write_wait();
976 } else {
977 debug("Requesting shell.");
978 packet_start(SSH_CMSG_EXEC_SHELL);
979 packet_send();
980 packet_write_wait();
981 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000982
Damien Miller95def091999-11-25 00:26:21 +1100983 /* Enter the interactive session. */
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000984 return client_loop(have_tty, tty_flag ?
985 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
Damien Miller1383bd82000-04-06 12:32:37 +1000986}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000987
Ben Lindstrombba81212001-06-25 05:01:22 +0000988static void
Damien Miller0e220db2004-06-15 10:34:08 +1000989ssh_subsystem_reply(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000990{
991 int id, len;
992
993 id = packet_get_int();
994 len = buffer_len(&command);
Ben Lindstrom4040fe12001-03-05 06:52:57 +0000995 if (len > 900)
996 len = 900;
Damien Miller48b03fc2002-01-22 23:11:40 +1100997 packet_check_eom();
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000998 if (type == SSH2_MSG_CHANNEL_FAILURE)
999 fatal("Request for subsystem '%.*s' failed on channel %d",
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001000 len, (u_char *)buffer_ptr(&command), id);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001001}
1002
Damien Miller2797f7f2002-04-23 21:09:44 +10001003void
Damien Miller509b0102003-12-17 16:33:10 +11001004client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt)
Damien Miller2797f7f2002-04-23 21:09:44 +10001005{
1006 int i;
1007
1008 i = client_global_request_id++;
Damien Miller509b0102003-12-17 16:33:10 +11001009 if (i >= options.num_remote_forwards)
Damien Miller2797f7f2002-04-23 21:09:44 +10001010 return;
Damien Miller2797f7f2002-04-23 21:09:44 +10001011 debug("remote forward %s for: listen %d, connect %s:%d",
1012 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
Damien Millerf91ee4c2005-03-01 21:24:33 +11001013 options.remote_forwards[i].listen_port,
1014 options.remote_forwards[i].connect_host,
1015 options.remote_forwards[i].connect_port);
Damien Miller2797f7f2002-04-23 21:09:44 +10001016 if (type == SSH2_MSG_REQUEST_FAILURE)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001017 logit("Warning: remote port forwarding failed for listen "
1018 "port %d", options.remote_forwards[i].listen_port);
Damien Miller2797f7f2002-04-23 21:09:44 +10001019}
1020
Damien Miller0e220db2004-06-15 10:34:08 +10001021static void
1022ssh_control_listener(void)
1023{
1024 struct sockaddr_un addr;
1025 mode_t old_umask;
Damien Miller07b6ff12004-06-15 11:14:45 +10001026 int addr_len;
1027
Damien Millerd14b1e72005-06-16 13:19:41 +10001028 if (options.control_path == NULL ||
1029 options.control_master == SSHCTL_MASTER_NO)
Damien Miller0e220db2004-06-15 10:34:08 +10001030 return;
1031
Damien Millerd14b1e72005-06-16 13:19:41 +10001032 debug("setting up multiplex master socket");
1033
Damien Miller0e220db2004-06-15 10:34:08 +10001034 memset(&addr, '\0', sizeof(addr));
1035 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001036 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001037 strlen(options.control_path) + 1;
1038
1039 if (strlcpy(addr.sun_path, options.control_path,
1040 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1041 fatal("ControlPath too long");
1042
1043 if ((control_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001044 fatal("%s socket(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001045
1046 old_umask = umask(0177);
Damien Miller90967402006-03-26 14:07:26 +11001047 if (bind(control_fd, (struct sockaddr *)&addr, addr_len) == -1) {
Damien Miller0e220db2004-06-15 10:34:08 +10001048 control_fd = -1;
Damien Miller4f10e252005-05-04 15:33:09 +10001049 if (errno == EINVAL || errno == EADDRINUSE)
Damien Miller0e220db2004-06-15 10:34:08 +10001050 fatal("ControlSocket %s already exists",
1051 options.control_path);
1052 else
Damien Miller15d72a02005-11-05 15:07:33 +11001053 fatal("%s bind(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001054 }
1055 umask(old_umask);
1056
1057 if (listen(control_fd, 64) == -1)
Damien Miller15d72a02005-11-05 15:07:33 +11001058 fatal("%s listen(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001059
1060 set_nonblock(control_fd);
1061}
1062
Ben Lindstromf558cf62001-09-20 23:13:49 +00001063/* request pty/x11/agent/tcpfwd/shell for channel */
Ben Lindstrombba81212001-06-25 05:01:22 +00001064static void
Ben Lindstromf558cf62001-09-20 23:13:49 +00001065ssh_session2_setup(int id, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +10001066{
Damien Miller3756dce2004-06-18 01:17:29 +10001067 extern char **environ;
Damien Miller17e7ed02005-06-17 12:54:33 +10001068 const char *display;
Damien Miller0e220db2004-06-15 10:34:08 +10001069 int interactive = tty_flag;
Damien Miller17e7ed02005-06-17 12:54:33 +10001070
Damien Miller46d38de2005-07-17 17:02:09 +10001071 display = getenv("DISPLAY");
Damien Miller17e7ed02005-06-17 12:54:33 +10001072 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001073 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001074 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001075 client_x11_get_proto(display, options.xauth_location,
1076 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001077 /* Request forwarding with authentication spoofing. */
1078 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +10001079 x11_request_forwarding_with_spoofing(id, display, proto, data);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001080 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001081 /* XXX wait for reply */
1082 }
1083
Damien Miller0bc1bd82000-11-13 22:57:25 +11001084 check_agent_present();
1085 if (options.forward_agent) {
1086 debug("Requesting authentication agent forwarding.");
1087 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1088 packet_send();
1089 }
1090
Damien Miller7b58e802005-12-13 19:33:19 +11001091 if (options.tun_open != SSH_TUNMODE_NO) {
Damien Millerd27b9472005-12-13 19:29:02 +11001092 Channel *c;
1093 int fd;
1094
1095 debug("Requesting tun.");
Damien Miller7b58e802005-12-13 19:33:19 +11001096 if ((fd = tun_open(options.tun_local,
1097 options.tun_open)) >= 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11001098 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1099 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1100 0, "tun", 1);
1101 c->datagram = 1;
Damien Miller598bbc22005-12-31 16:33:36 +11001102#if defined(SSH_TUN_FILTER)
1103 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1104 channel_register_filter(c->self, sys_tun_infilter,
1105 sys_tun_outfilter);
1106#endif
Damien Millerd27b9472005-12-13 19:29:02 +11001107 packet_start(SSH2_MSG_CHANNEL_OPEN);
1108 packet_put_cstring("tun@openssh.com");
1109 packet_put_int(c->self);
1110 packet_put_int(c->local_window_max);
1111 packet_put_int(c->local_maxpacket);
Damien Miller7b58e802005-12-13 19:33:19 +11001112 packet_put_int(options.tun_open);
Damien Millerd27b9472005-12-13 19:29:02 +11001113 packet_put_int(options.tun_remote);
1114 packet_send();
1115 }
1116 }
1117
Damien Miller0e220db2004-06-15 10:34:08 +10001118 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
Damien Miller3756dce2004-06-18 01:17:29 +10001119 NULL, fileno(stdin), &command, environ, &ssh_subsystem_reply);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001120
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001121 packet_set_interactive(interactive);
Damien Miller1383bd82000-04-06 12:32:37 +10001122}
1123
Ben Lindstromf558cf62001-09-20 23:13:49 +00001124/* open new channel for a session */
Ben Lindstrombba81212001-06-25 05:01:22 +00001125static int
Ben Lindstromf558cf62001-09-20 23:13:49 +00001126ssh_session2_open(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001127{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001128 Channel *c;
1129 int window, packetmax, in, out, err;
Damien Millerad833b32000-08-23 10:46:23 +10001130
Damien Millercaf6dd62000-08-29 11:33:50 +11001131 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001132 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001133 } else {
1134 in = dup(STDIN_FILENO);
1135 }
1136 out = dup(STDOUT_FILENO);
1137 err = dup(STDERR_FILENO);
1138
1139 if (in < 0 || out < 0 || err < 0)
1140 fatal("dup() in/out/err failed");
1141
Damien Miller69b69aa2000-10-28 14:19:58 +11001142 /* enable nonblocking unless tty */
1143 if (!isatty(in))
1144 set_nonblock(in);
1145 if (!isatty(out))
1146 set_nonblock(out);
1147 if (!isatty(err))
1148 set_nonblock(err);
1149
Damien Millere4340be2000-09-16 13:29:08 +11001150 window = CHAN_SES_WINDOW_DEFAULT;
1151 packetmax = CHAN_SES_PACKET_DEFAULT;
Damien Miller19a59452002-02-19 15:20:57 +11001152 if (tty_flag) {
1153 window >>= 1;
1154 packetmax >>= 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001155 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001156 c = channel_new(
Damien Miller1383bd82000-04-06 12:32:37 +10001157 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001158 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001159 "client-session", /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001160
Ben Lindstromf558cf62001-09-20 23:13:49 +00001161 debug3("ssh_session2_open: channel_new: %d", c->self);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001162
Ben Lindstrom5ec26452001-06-09 00:18:51 +00001163 channel_send_open(c->self);
Ben Lindstromf558cf62001-09-20 23:13:49 +00001164 if (!no_shell_flag)
Damien Miller0e220db2004-06-15 10:34:08 +10001165 channel_register_confirm(c->self, ssh_session2_setup, NULL);
Damien Miller1383bd82000-04-06 12:32:37 +10001166
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001167 return c->self;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001168}
1169
Ben Lindstrombba81212001-06-25 05:01:22 +00001170static int
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001171ssh_session2(void)
1172{
Ben Lindstromf558cf62001-09-20 23:13:49 +00001173 int id = -1;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001174
1175 /* XXX should be pre-session */
1176 ssh_init_forwarding();
Damien Miller0e220db2004-06-15 10:34:08 +10001177 ssh_control_listener();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001178
Ben Lindstromf558cf62001-09-20 23:13:49 +00001179 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1180 id = ssh_session2_open();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001181
Damien Millerd27b9472005-12-13 19:29:02 +11001182 /* Execute a local command */
1183 if (options.local_command != NULL &&
1184 options.permit_local_command)
1185 ssh_local_cmd(options.local_command);
1186
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001187 /* If requested, let ssh continue in the background. */
1188 if (fork_after_authentication_flag)
1189 if (daemon(1, 1) < 0)
1190 fatal("daemon() failed: %.200s", strerror(errno));
1191
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001192 return client_loop(tty_flag, tty_flag ?
1193 options.escape_char : SSH_ESCAPECHAR_NONE, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001194}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001195
Ben Lindstrombba81212001-06-25 05:01:22 +00001196static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001197load_public_identity_files(void)
1198{
Damien Miller6b1d53c2006-03-31 23:13:21 +11001199 char *filename, *cp, thishost[NI_MAXHOST];
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001200 int i = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001201 Key *public;
Damien Miller6b1d53c2006-03-31 23:13:21 +11001202 struct passwd *pw;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001203#ifdef SMARTCARD
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001204 Key **keys;
1205
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001206 if (options.smartcard_device != NULL &&
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001207 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1208 (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL ) {
1209 int count = 0;
1210 for (i = 0; keys[i] != NULL; i++) {
1211 count++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001212 memmove(&options.identity_files[1], &options.identity_files[0],
1213 sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
1214 memmove(&options.identity_keys[1], &options.identity_keys[0],
1215 sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1216 options.num_identity_files++;
1217 options.identity_keys[0] = keys[i];
Damien Miller56a0bb02003-06-18 20:28:40 +10001218 options.identity_files[0] = sc_get_key_label(keys[i]);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001219 }
Ben Lindstrom4f054602002-03-26 03:23:00 +00001220 if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1221 options.num_identity_files = SSH_MAX_IDENTITY_FILES;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001222 i = count;
1223 xfree(keys);
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001224 }
Ben Lindstromffce1472001-08-06 21:57:31 +00001225#endif /* SMARTCARD */
Damien Miller6b1d53c2006-03-31 23:13:21 +11001226 if ((pw = getpwuid(original_real_uid)) == NULL)
1227 fatal("load_public_identity_files: getpwuid failed");
1228 if (gethostname(thishost, sizeof(thishost)) == -1)
1229 fatal("load_public_identity_files: gethostname: %s",
1230 strerror(errno));
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001231 for (; i < options.num_identity_files; i++) {
Damien Miller6b1d53c2006-03-31 23:13:21 +11001232 cp = tilde_expand_filename(options.identity_files[i],
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001233 original_real_uid);
Damien Miller6b1d53c2006-03-31 23:13:21 +11001234 filename = percent_expand(cp, "d", pw->pw_dir,
1235 "u", pw->pw_name, "l", thishost, "h", host,
1236 "r", options.user, (char *)NULL);
1237 xfree(cp);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001238 public = key_load_public(filename, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001239 debug("identity file %s type %d", filename,
1240 public ? public->type : -1);
1241 xfree(options.identity_files[i]);
1242 options.identity_files[i] = filename;
1243 options.identity_keys[i] = public;
1244 }
1245}
Damien Miller0e220db2004-06-15 10:34:08 +10001246
1247static void
1248control_client_sighandler(int signo)
1249{
1250 control_client_terminate = signo;
1251}
1252
1253static void
1254control_client_sigrelay(int signo)
1255{
1256 if (control_server_pid > 1)
1257 kill(control_server_pid, signo);
1258}
1259
Darren Tucker365433f2004-06-22 12:29:23 +10001260static int
1261env_permitted(char *env)
1262{
1263 int i;
1264 char name[1024], *cp;
1265
Damien Miller07d86be2006-03-26 14:19:21 +11001266 if (strlcpy(name, env, sizeof(name)) >= sizeof(name))
1267 fatal("env_permitted: name too long");
Darren Tucker365433f2004-06-22 12:29:23 +10001268 if ((cp = strchr(name, '=')) == NULL)
1269 return (0);
1270
1271 *cp = '\0';
1272
1273 for (i = 0; i < options.num_send_env; i++)
1274 if (match_pattern(name, options.send_env[i]))
1275 return (1);
1276
1277 return (0);
1278}
1279
Damien Miller0e220db2004-06-15 10:34:08 +10001280static void
1281control_client(const char *path)
1282{
1283 struct sockaddr_un addr;
Darren Tucker39207a42004-11-05 20:19:51 +11001284 int i, r, fd, sock, exitval, num_env, addr_len;
Damien Miller0e220db2004-06-15 10:34:08 +10001285 Buffer m;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001286 char *term;
Damien Miller3756dce2004-06-18 01:17:29 +10001287 extern char **environ;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001288 u_int flags;
Darren Tuckerfc959702004-07-17 16:12:08 +10001289
Damien Millerd14b1e72005-06-16 13:19:41 +10001290 if (mux_command == 0)
1291 mux_command = SSHMUX_COMMAND_OPEN;
1292
1293 switch (options.control_master) {
1294 case SSHCTL_MASTER_AUTO:
1295 case SSHCTL_MASTER_AUTO_ASK:
1296 debug("auto-mux: Trying existing master");
1297 /* FALLTHROUGH */
1298 case SSHCTL_MASTER_NO:
1299 break;
1300 default:
1301 return;
1302 }
1303
Damien Miller0e220db2004-06-15 10:34:08 +10001304 memset(&addr, '\0', sizeof(addr));
1305 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001306 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001307 strlen(path) + 1;
1308
1309 if (strlcpy(addr.sun_path, path,
1310 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1311 fatal("ControlPath too long");
1312
1313 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1314 fatal("%s socket(): %s", __func__, strerror(errno));
1315
Damien Miller90967402006-03-26 14:07:26 +11001316 if (connect(sock, (struct sockaddr *)&addr, addr_len) == -1) {
Darren Tucker0814d312005-06-01 23:08:51 +10001317 if (mux_command != SSHMUX_COMMAND_OPEN) {
1318 fatal("Control socket connect(%.100s): %s", path,
1319 strerror(errno));
1320 }
Damien Miller538c9b72005-05-26 12:11:28 +10001321 if (errno == ENOENT)
Damien Miller4662d342006-03-26 13:59:59 +11001322 debug("Control socket \"%.100s\" does not exist", path);
Damien Miller538c9b72005-05-26 12:11:28 +10001323 else {
Damien Miller4662d342006-03-26 13:59:59 +11001324 error("Control socket connect(%.100s): %s", path,
Damien Miller538c9b72005-05-26 12:11:28 +10001325 strerror(errno));
1326 }
Damien Miller4662d342006-03-26 13:59:59 +11001327 close(sock);
1328 return;
1329 }
Damien Miller46d38de2005-07-17 17:02:09 +10001330
Damien Miller4662d342006-03-26 13:59:59 +11001331 if (stdin_null_flag) {
1332 if ((fd = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1333 fatal("open(/dev/null): %s", strerror(errno));
1334 if (dup2(fd, STDIN_FILENO) == -1)
1335 fatal("dup2: %s", strerror(errno));
1336 if (fd > STDERR_FILENO)
1337 close(fd);
1338 }
Damien Miller46d38de2005-07-17 17:02:09 +10001339
Damien Miller13390022005-07-06 09:44:19 +10001340 term = getenv("TERM");
Darren Tucker7ebfc102004-11-07 20:06:19 +11001341
1342 flags = 0;
1343 if (tty_flag)
1344 flags |= SSHMUX_FLAG_TTY;
1345 if (subsystem_flag)
1346 flags |= SSHMUX_FLAG_SUBSYS;
Damien Miller13390022005-07-06 09:44:19 +10001347 if (options.forward_x11)
1348 flags |= SSHMUX_FLAG_X11_FWD;
1349 if (options.forward_agent)
1350 flags |= SSHMUX_FLAG_AGENT_FWD;
Damien Miller0e220db2004-06-15 10:34:08 +10001351
Damien Miller0e220db2004-06-15 10:34:08 +10001352 buffer_init(&m);
1353
Darren Tucker7ebfc102004-11-07 20:06:19 +11001354 /* Send our command to server */
1355 buffer_put_int(&m, mux_command);
1356 buffer_put_int(&m, flags);
Damien Miller13390022005-07-06 09:44:19 +10001357 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001358 fatal("%s: msg_send", __func__);
1359 buffer_clear(&m);
1360
1361 /* Get authorisation status and PID of controlee */
Damien Miller0e220db2004-06-15 10:34:08 +10001362 if (ssh_msg_recv(sock, &m) == -1)
1363 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001364 if (buffer_get_char(&m) != SSHMUX_VER)
Damien Miller0e220db2004-06-15 10:34:08 +10001365 fatal("%s: wrong version", __func__);
Damien Miller23f07702004-06-18 01:19:03 +10001366 if (buffer_get_int(&m) != 1)
1367 fatal("Connection to master denied");
Damien Miller0e220db2004-06-15 10:34:08 +10001368 control_server_pid = buffer_get_int(&m);
1369
Damien Miller0e220db2004-06-15 10:34:08 +10001370 buffer_clear(&m);
Damien Miller0e220db2004-06-15 10:34:08 +10001371
Darren Tucker7ebfc102004-11-07 20:06:19 +11001372 switch (mux_command) {
1373 case SSHMUX_COMMAND_ALIVE_CHECK:
Darren Tucker47eede72005-03-14 23:08:12 +11001374 fprintf(stderr, "Master running (pid=%d)\r\n",
Darren Tucker7ebfc102004-11-07 20:06:19 +11001375 control_server_pid);
1376 exit(0);
1377 case SSHMUX_COMMAND_TERMINATE:
1378 fprintf(stderr, "Exit request sent.\r\n");
1379 exit(0);
1380 case SSHMUX_COMMAND_OPEN:
1381 /* continue below */
1382 break;
1383 default:
1384 fatal("silly mux_command %d", mux_command);
1385 }
1386
1387 /* SSHMUX_COMMAND_OPEN */
Damien Miller13390022005-07-06 09:44:19 +10001388 buffer_put_cstring(&m, term ? term : "");
Damien Miller0e220db2004-06-15 10:34:08 +10001389 buffer_append(&command, "\0", 1);
1390 buffer_put_cstring(&m, buffer_ptr(&command));
1391
Darren Tucker365433f2004-06-22 12:29:23 +10001392 if (options.num_send_env == 0 || environ == NULL) {
1393 buffer_put_int(&m, 0);
Darren Tuckerfc959702004-07-17 16:12:08 +10001394 } else {
Darren Tucker365433f2004-06-22 12:29:23 +10001395 /* Pass environment */
1396 num_env = 0;
1397 for (i = 0; environ[i] != NULL; i++)
1398 if (env_permitted(environ[i]))
1399 num_env++; /* Count */
Darren Tuckerfc959702004-07-17 16:12:08 +10001400
Darren Tucker365433f2004-06-22 12:29:23 +10001401 buffer_put_int(&m, num_env);
1402
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001403 for (i = 0; environ[i] != NULL && num_env >= 0; i++)
1404 if (env_permitted(environ[i])) {
1405 num_env--;
Darren Tucker365433f2004-06-22 12:29:23 +10001406 buffer_put_cstring(&m, environ[i]);
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001407 }
Darren Tucker365433f2004-06-22 12:29:23 +10001408 }
Damien Miller3756dce2004-06-18 01:17:29 +10001409
Damien Miller13390022005-07-06 09:44:19 +10001410 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Damien Miller0e220db2004-06-15 10:34:08 +10001411 fatal("%s: msg_send", __func__);
1412
1413 mm_send_fd(sock, STDIN_FILENO);
1414 mm_send_fd(sock, STDOUT_FILENO);
1415 mm_send_fd(sock, STDERR_FILENO);
1416
1417 /* Wait for reply, so master has a chance to gather ttymodes */
1418 buffer_clear(&m);
1419 if (ssh_msg_recv(sock, &m) == -1)
1420 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001421 if (buffer_get_char(&m) != SSHMUX_VER)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001422 fatal("%s: wrong version", __func__);
Damien Miller0e220db2004-06-15 10:34:08 +10001423 buffer_free(&m);
1424
Darren Tucker07336da2004-11-05 20:02:16 +11001425 signal(SIGHUP, control_client_sighandler);
Damien Miller0809e232004-06-18 22:20:57 +10001426 signal(SIGINT, control_client_sighandler);
1427 signal(SIGTERM, control_client_sighandler);
1428 signal(SIGWINCH, control_client_sigrelay);
1429
Damien Miller0e220db2004-06-15 10:34:08 +10001430 if (tty_flag)
1431 enter_raw_mode();
1432
1433 /* Stick around until the controlee closes the client_fd */
1434 exitval = 0;
1435 for (;!control_client_terminate;) {
1436 r = read(sock, &exitval, sizeof(exitval));
1437 if (r == 0) {
1438 debug2("Received EOF from master");
1439 break;
1440 }
1441 if (r > 0)
1442 debug2("Received exit status from master %d", exitval);
1443 if (r == -1 && errno != EINTR)
1444 fatal("%s: read %s", __func__, strerror(errno));
1445 }
1446
1447 if (control_client_terminate)
1448 debug2("Exiting on signal %d", control_client_terminate);
1449
1450 close(sock);
1451
1452 leave_raw_mode();
1453
1454 if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
1455 fprintf(stderr, "Connection to master closed.\r\n");
1456
1457 exit(exitval);
1458}