blob: 9961baf6e7db5c988f4c19e7ef26e61b5c95b89c [file] [log] [blame]
Darren Tuckerba724052006-07-12 22:24:22 +10001/* $OpenBSD: ssh.c,v 1.285 2006/07/11 20:27:56 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 Millere3b60b52006-07-10 21:08:03 +100051#include <sys/socket.h>
Damien Miller574c41f2006-03-15 11:40:10 +110052#include <sys/un.h>
Damien Miller03e20032006-03-15 11:16:59 +110053
Damien Millerc7b06362006-03-15 11:53:45 +110054#include <ctype.h>
Darren Tuckerba724052006-07-12 22:24:22 +100055#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100056#include <fcntl.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110057#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110058#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110059#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100060#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110061#include <signal.h>
Damien Millereba71ba2000-04-29 23:57:08 +100062
63#include <openssl/evp.h>
Damien Miller0bc1bd82000-11-13 22:57:25 +110064#include <openssl/err.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000067#include "ssh1.h"
Damien Miller1383bd82000-04-06 12:32:37 +100068#include "ssh2.h"
69#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000070#include "cipher.h"
71#include "xmalloc.h"
72#include "packet.h"
73#include "buffer.h"
Damien Miller0e220db2004-06-15 10:34:08 +100074#include "bufaux.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000075#include "channels.h"
Damien Millereba71ba2000-04-29 23:57:08 +100076#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100077#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100078#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000079#include "pathnames.h"
Damien Miller0e220db2004-06-15 10:34:08 +100080#include "dispatch.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +000081#include "clientloop.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000082#include "log.h"
83#include "readconf.h"
84#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000085#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000086#include "kex.h"
87#include "mac.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +100088#include "sshpty.h"
Darren Tucker46bc0752004-05-02 22:11:30 +100089#include "match.h"
Damien Miller0e220db2004-06-15 10:34:08 +100090#include "msg.h"
91#include "monitor_fdpass.h"
Darren Tucker25f60a72004-08-15 17:23:34 +100092#include "uidswap.h"
Damien Millerb7576772006-07-10 20:23:39 +100093#include "version.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094
Ben Lindstromc5b68002001-07-04 04:52:03 +000095#ifdef SMARTCARD
Ben Lindstromc5b68002001-07-04 04:52:03 +000096#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000097#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +000098
Damien Miller3f905871999-11-15 17:10:57 +110099extern char *__progname;
Damien Miller3f905871999-11-15 17:10:57 +1100100
Damien Miller95def091999-11-25 00:26:21 +1100101/* Flag indicating whether debug mode is on. This can be set on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102int debug_flag = 0;
103
Damien Miller78928792000-04-12 20:17:38 +1000104/* Flag indicating whether a tty should be allocated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105int tty_flag = 0;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000106int no_tty_flag = 0;
107int force_tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108
Damien Miller1383bd82000-04-06 12:32:37 +1000109/* don't exec a shell */
110int no_shell_flag = 0;
Damien Miller1383bd82000-04-06 12:32:37 +1000111
Damien Miller5428f641999-11-25 11:54:57 +1100112/*
113 * Flag indicating that nothing should be read from stdin. This can be set
114 * on the command line.
115 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116int stdin_null_flag = 0;
117
Damien Miller5428f641999-11-25 11:54:57 +1100118/*
119 * Flag indicating that ssh should fork after authentication. This is useful
Ben Lindstromf666fec2002-06-06 19:51:58 +0000120 * so that the passphrase can be entered manually, and then ssh goes to the
Damien Miller5428f641999-11-25 11:54:57 +1100121 * background.
122 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123int fork_after_authentication_flag = 0;
124
Damien Miller5428f641999-11-25 11:54:57 +1100125/*
126 * General data structure for command line options and options configurable
127 * in configuration files. See readconf.h.
128 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129Options options;
130
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000131/* optional user configfile */
132char *config = NULL;
133
Damien Miller5428f641999-11-25 11:54:57 +1100134/*
135 * Name of the host we are connecting to. This is the name given on the
136 * command line, or the HostName specified for the user-supplied name in a
137 * configuration file.
138 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139char *host;
140
141/* socket address the host resolves to */
Damien Miller34132e52000-01-14 15:45:46 +1100142struct sockaddr_storage hostaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000144/* Private host keys. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000145Sensitive sensitive_data;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146
147/* Original real UID. */
148uid_t original_real_uid;
Ben Lindstromf9c48842002-06-11 16:37:51 +0000149uid_t original_effective_uid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150
Damien Miller1383bd82000-04-06 12:32:37 +1000151/* command to be executed */
152Buffer command;
153
Damien Miller832562e2001-01-30 09:30:01 +1100154/* Should we execute a command or invoke a subsystem? */
155int subsystem_flag = 0;
156
Damien Miller2797f7f2002-04-23 21:09:44 +1000157/* # of replies received for global requests */
158static int client_global_request_id = 0;
159
Damien Miller8c4e18a2002-09-19 12:05:02 +1000160/* pid of proxycommand child process */
161pid_t proxy_command_pid = 0;
162
Damien Miller0e220db2004-06-15 10:34:08 +1000163/* fd to control socket */
164int control_fd = -1;
165
Darren Tucker7ebfc102004-11-07 20:06:19 +1100166/* Multiplexing control command */
Darren Tucker0814d312005-06-01 23:08:51 +1000167static u_int mux_command = 0;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100168
Damien Miller0e220db2004-06-15 10:34:08 +1000169/* Only used in control client mode */
170volatile sig_atomic_t control_client_terminate = 0;
171u_int control_server_pid = 0;
172
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173/* Prints a help message to the user. This function never returns. */
174
Ben Lindstrombba81212001-06-25 05:01:22 +0000175static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000176usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000177{
Damien Miller50955102004-03-22 09:34:58 +1100178 fprintf(stderr,
Darren Tucker9c6bf322004-12-03 14:10:19 +1100179"usage: ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n"
Darren Tucker895d6982005-10-03 18:18:05 +1000180" [-D [bind_address:]port] [-e escape_char] [-F configfile]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100181" [-i identity_file] [-L [bind_address:]port:host:hostport]\n"
Damien Millerf91ee4c2005-03-01 21:24:33 +1100182" [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100183" [-R [bind_address:]port:host:hostport] [-S ctl_path]\n"
Damien Miller991dba42006-07-10 20:16:27 +1000184" [-w local_tun[:remote_tun]] [user@]hostname [command]\n"
Damien Miller50955102004-03-22 09:34:58 +1100185 );
Darren Tuckere9a9b712005-12-20 16:15:51 +1100186 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000187}
188
Ben Lindstrombba81212001-06-25 05:01:22 +0000189static int ssh_session(void);
190static int ssh_session2(void);
191static void load_public_identity_files(void);
Damien Miller0e220db2004-06-15 10:34:08 +1000192static void control_client(const char *path);
Damien Miller1383bd82000-04-06 12:32:37 +1000193
Damien Miller95def091999-11-25 00:26:21 +1100194/*
195 * Main program for the ssh client.
196 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000197int
198main(int ac, char **av)
199{
Ben Lindstrom24157572002-06-12 16:09:39 +0000200 int i, opt, exit_status;
Damien Miller9836cf82003-12-17 16:30:06 +1100201 char *p, *cp, *line, buf[256];
Damien Miller95def091999-11-25 00:26:21 +1100202 struct stat st;
Ben Lindstrom086cf212001-03-05 05:56:40 +0000203 struct passwd *pw;
Damien Miller1383bd82000-04-06 12:32:37 +1000204 int dummy;
Ben Lindstrom5ccf63a2001-09-24 20:00:10 +0000205 extern int optind, optreset;
Damien Miller4f8e6692001-07-14 13:22:53 +1000206 extern char *optarg;
Damien Miller9651fe62005-06-26 08:55:25 +1000207 struct servent *sp;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100208 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209
Darren Tuckerce321d82005-10-03 18:11:24 +1000210 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
211 sanitise_stdfd();
212
Damien Miller59d3d5b2003-08-22 09:34:41 +1000213 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000214 init_rng();
215
Damien Miller5428f641999-11-25 11:54:57 +1100216 /*
217 * Save the original real uid. It will be needed later (uid-swapping
218 * may clobber the real uid).
219 */
Damien Miller95def091999-11-25 00:26:21 +1100220 original_real_uid = getuid();
221 original_effective_uid = geteuid();
Damien Millera8e06ce2003-11-21 23:48:55 +1100222
Damien Miller50b9a602002-09-04 16:50:06 +1000223 /*
224 * Use uid-swapping to give up root privileges for the duration of
225 * option processing. We will re-instantiate the rights when we are
226 * ready to create the privileged port, and will permanently drop
227 * them when the port has been created (actually, when the connection
228 * has been made, as we may need to create the port several times).
229 */
230 PRIV_END;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000231
Damien Miller05dd7952000-10-01 00:42:48 +1100232#ifdef HAVE_SETRLIMIT
Damien Miller95def091999-11-25 00:26:21 +1100233 /* If we are installed setuid root be careful to not drop core. */
234 if (original_real_uid != original_effective_uid) {
235 struct rlimit rlim;
236 rlim.rlim_cur = rlim.rlim_max = 0;
237 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
238 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100240#endif
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000241 /* Get user data. */
242 pw = getpwuid(original_real_uid);
243 if (!pw) {
Damien Miller996acd22003-04-09 20:59:48 +1000244 logit("You don't exist, go away!");
Darren Tuckere9a9b712005-12-20 16:15:51 +1100245 exit(255);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000246 }
247 /* Take a copy of the returned structure. */
248 pw = pwcopy(pw);
249
Damien Miller5428f641999-11-25 11:54:57 +1100250 /*
Damien Miller5428f641999-11-25 11:54:57 +1100251 * Set our umask to something reasonable, as some files are created
252 * with the default umask. This will make them world-readable but
253 * writable only by the owner, which is ok for all files for which we
254 * don't set the modes explicitly.
255 */
Damien Miller95def091999-11-25 00:26:21 +1100256 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000257
Damien Miller95def091999-11-25 00:26:21 +1100258 /* Initialize option structure to indicate that no values have been set. */
259 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000260
Damien Miller95def091999-11-25 00:26:21 +1100261 /* Parse command-line arguments. */
262 host = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000263
Damien Miller2ecb6bd2006-03-15 12:03:53 +1100264 again:
Damien Millerf4614452001-07-14 12:18:10 +1000265 while ((opt = getopt(ac, av,
Damien Millerd27b9472005-12-13 19:29:02 +1100266 "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 +1100267 switch (opt) {
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000268 case '1':
269 options.protocol = SSH_PROTO_1;
270 break;
Damien Miller4af51302000-04-16 11:18:38 +1000271 case '2':
272 options.protocol = SSH_PROTO_2;
273 break;
Damien Miller34132e52000-01-14 15:45:46 +1100274 case '4':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000275 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100276 break;
Damien Miller34132e52000-01-14 15:45:46 +1100277 case '6':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000278 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100279 break;
Damien Miller95def091999-11-25 00:26:21 +1100280 case 'n':
281 stdin_null_flag = 1;
282 break;
Damien Miller95def091999-11-25 00:26:21 +1100283 case 'f':
284 fork_after_authentication_flag = 1;
285 stdin_null_flag = 1;
286 break;
Damien Miller95def091999-11-25 00:26:21 +1100287 case 'x':
288 options.forward_x11 = 0;
289 break;
Damien Miller95def091999-11-25 00:26:21 +1100290 case 'X':
291 options.forward_x11 = 1;
292 break;
Darren Tucker0a118da2003-10-15 15:54:32 +1000293 case 'Y':
294 options.forward_x11 = 1;
295 options.forward_x11_trusted = 1;
296 break;
Damien Miller95def091999-11-25 00:26:21 +1100297 case 'g':
298 options.gateway_ports = 1;
299 break;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100300 case 'O':
301 if (strcmp(optarg, "check") == 0)
302 mux_command = SSHMUX_COMMAND_ALIVE_CHECK;
303 else if (strcmp(optarg, "exit") == 0)
304 mux_command = SSHMUX_COMMAND_TERMINATE;
305 else
306 fatal("Invalid multiplex command.");
307 break;
Damien Miller147bba32002-09-04 16:46:06 +1000308 case 'P': /* deprecated */
Damien Miller95def091999-11-25 00:26:21 +1100309 options.use_privileged_port = 0;
310 break;
Damien Miller95def091999-11-25 00:26:21 +1100311 case 'a':
312 options.forward_agent = 0;
313 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000314 case 'A':
315 options.forward_agent = 1;
316 break;
Damien Miller95def091999-11-25 00:26:21 +1100317 case 'k':
Damien Millere0113cc2003-11-24 13:10:09 +1100318 options.gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100319 break;
Damien Miller95def091999-11-25 00:26:21 +1100320 case 'i':
321 if (stat(optarg, &st) < 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000322 fprintf(stderr, "Warning: Identity file %s "
Damien Miller3eb48b62005-03-01 21:15:46 +1100323 "not accessible: %s.\n", optarg,
324 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100325 break;
326 }
Damien Millerf4614452001-07-14 12:18:10 +1000327 if (options.num_identity_files >=
328 SSH_MAX_IDENTITY_FILES)
329 fatal("Too many identity files specified "
330 "(max %d)", SSH_MAX_IDENTITY_FILES);
331 options.identity_files[options.num_identity_files++] =
332 xstrdup(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100333 break;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000334 case 'I':
335#ifdef SMARTCARD
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000336 options.smartcard_device = xstrdup(optarg);
Ben Lindstrombcc18082001-08-06 21:59:25 +0000337#else
Ben Lindstromc5b68002001-07-04 04:52:03 +0000338 fprintf(stderr, "no support for smartcards.\n");
Ben Lindstrombcc18082001-08-06 21:59:25 +0000339#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000340 break;
Damien Miller95def091999-11-25 00:26:21 +1100341 case 't':
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000342 if (tty_flag)
343 force_tty_flag = 1;
Damien Miller95def091999-11-25 00:26:21 +1100344 tty_flag = 1;
345 break;
Damien Miller95def091999-11-25 00:26:21 +1100346 case 'v':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000347 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100348 debug_flag = 1;
349 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000350 } else {
351 if (options.log_level < SYSLOG_LEVEL_DEBUG3)
352 options.log_level++;
Damien Millere4340be2000-09-16 13:29:08 +1100353 break;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000354 }
Darren Tuckere9bf9842004-11-05 20:05:32 +1100355 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100356 case 'V':
Damien Miller0c889cd2004-03-22 09:36:00 +1100357 fprintf(stderr, "%s, %s\n",
Damien Miller2aa6d3c2004-09-12 16:53:04 +1000358 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
Damien Miller95def091999-11-25 00:26:21 +1100359 if (opt == 'V')
360 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100361 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100362 case 'w':
Damien Miller7b58e802005-12-13 19:33:19 +1100363 if (options.tun_open == -1)
364 options.tun_open = SSH_TUNMODE_DEFAULT;
Damien Millerd27b9472005-12-13 19:29:02 +1100365 options.tun_local = a2tun(optarg, &options.tun_remote);
Damien Miller7b58e802005-12-13 19:33:19 +1100366 if (options.tun_local == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100367 fprintf(stderr, "Bad tun device '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100368 exit(255);
Damien Millerd27b9472005-12-13 19:29:02 +1100369 }
370 break;
Damien Miller95def091999-11-25 00:26:21 +1100371 case 'q':
372 options.log_level = SYSLOG_LEVEL_QUIET;
373 break;
Damien Miller95def091999-11-25 00:26:21 +1100374 case 'e':
375 if (optarg[0] == '^' && optarg[2] == 0 &&
Damien Millerf4614452001-07-14 12:18:10 +1000376 (u_char) optarg[1] >= 64 &&
377 (u_char) optarg[1] < 128)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000378 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100379 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000380 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100381 else if (strcmp(optarg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000382 options.escape_char = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100383 else {
Damien Millerf4614452001-07-14 12:18:10 +1000384 fprintf(stderr, "Bad escape character '%s'.\n",
385 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100386 exit(255);
Damien Miller95def091999-11-25 00:26:21 +1100387 }
388 break;
Damien Miller95def091999-11-25 00:26:21 +1100389 case 'c':
Damien Millereba71ba2000-04-29 23:57:08 +1000390 if (ciphers_valid(optarg)) {
391 /* SSH2 only */
392 options.ciphers = xstrdup(optarg);
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000393 options.cipher = SSH_CIPHER_INVALID;
Damien Millereba71ba2000-04-29 23:57:08 +1000394 } else {
395 /* SSH1 only */
Damien Millere39cacc2000-11-29 12:18:44 +1100396 options.cipher = cipher_number(optarg);
397 if (options.cipher == -1) {
Damien Millerf4614452001-07-14 12:18:10 +1000398 fprintf(stderr,
399 "Unknown cipher type '%s'\n",
400 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100401 exit(255);
Damien Millereba71ba2000-04-29 23:57:08 +1000402 }
Damien Millerf4614452001-07-14 12:18:10 +1000403 if (options.cipher == SSH_CIPHER_3DES)
Damien Millere39cacc2000-11-29 12:18:44 +1100404 options.ciphers = "3des-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000405 else if (options.cipher == SSH_CIPHER_BLOWFISH)
Damien Millere39cacc2000-11-29 12:18:44 +1100406 options.ciphers = "blowfish-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000407 else
Damien Millere39cacc2000-11-29 12:18:44 +1100408 options.ciphers = (char *)-1;
Damien Miller95def091999-11-25 00:26:21 +1100409 }
410 break;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000411 case 'm':
412 if (mac_valid(optarg))
413 options.macs = xstrdup(optarg);
414 else {
Damien Millerf4614452001-07-14 12:18:10 +1000415 fprintf(stderr, "Unknown mac type '%s'\n",
416 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100417 exit(255);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000418 }
419 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000420 case 'M':
Damien Millerd14b1e72005-06-16 13:19:41 +1000421 if (options.control_master == SSHCTL_MASTER_YES)
422 options.control_master = SSHCTL_MASTER_ASK;
423 else
424 options.control_master = SSHCTL_MASTER_YES;
Damien Miller0e220db2004-06-15 10:34:08 +1000425 break;
Damien Miller95def091999-11-25 00:26:21 +1100426 case 'p':
Ben Lindstrom19066a12001-04-12 23:39:26 +0000427 options.port = a2port(optarg);
428 if (options.port == 0) {
Ben Lindstrom146edb92001-04-11 23:06:28 +0000429 fprintf(stderr, "Bad port '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100430 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000431 }
Damien Miller95def091999-11-25 00:26:21 +1100432 break;
Damien Miller95def091999-11-25 00:26:21 +1100433 case 'l':
434 options.user = optarg;
435 break;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000436
Damien Miller95def091999-11-25 00:26:21 +1100437 case 'L':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100438 if (parse_forward(&fwd, optarg))
439 add_local_forward(&options, &fwd);
440 else {
Damien Millerf4614452001-07-14 12:18:10 +1000441 fprintf(stderr,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100442 "Bad local forwarding specification '%s'\n",
Damien Millerf4614452001-07-14 12:18:10 +1000443 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100444 exit(255);
Ben Lindstrom1a174712001-09-12 17:56:15 +0000445 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100446 break;
447
448 case 'R':
449 if (parse_forward(&fwd, optarg)) {
450 add_remote_forward(&options, &fwd);
451 } else {
452 fprintf(stderr,
453 "Bad remote forwarding specification "
454 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100455 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100456 }
Damien Miller95def091999-11-25 00:26:21 +1100457 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000458
459 case 'D':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100460 cp = p = xstrdup(optarg);
461 memset(&fwd, '\0', sizeof(fwd));
462 fwd.connect_host = "socks";
463 if ((fwd.listen_host = hpdelim(&cp)) == NULL) {
464 fprintf(stderr, "Bad dynamic forwarding "
465 "specification '%.100s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100466 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100467 }
468 if (cp != NULL) {
469 fwd.listen_port = a2port(cp);
470 fwd.listen_host = cleanhostname(fwd.listen_host);
471 } else {
472 fwd.listen_port = a2port(fwd.listen_host);
Damien Millerbe1045d2005-08-12 22:10:56 +1000473 fwd.listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100474 }
475
476 if (fwd.listen_port == 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000477 fprintf(stderr, "Bad dynamic port '%s'\n",
478 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100479 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000480 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100481 add_local_forward(&options, &fwd);
482 xfree(p);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000483 break;
484
Damien Miller95def091999-11-25 00:26:21 +1100485 case 'C':
486 options.compression = 1;
487 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000488 case 'N':
489 no_shell_flag = 1;
490 no_tty_flag = 1;
491 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000492 case 'T':
493 no_tty_flag = 1;
494 break;
Damien Miller95def091999-11-25 00:26:21 +1100495 case 'o':
496 dummy = 1;
Damien Miller9836cf82003-12-17 16:30:06 +1100497 line = xstrdup(optarg);
Damien Millerf4614452001-07-14 12:18:10 +1000498 if (process_config_line(&options, host ? host : "",
Damien Miller9836cf82003-12-17 16:30:06 +1100499 line, "command-line", 0, &dummy) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100500 exit(255);
Damien Miller9836cf82003-12-17 16:30:06 +1100501 xfree(line);
Damien Miller95def091999-11-25 00:26:21 +1100502 break;
Damien Miller832562e2001-01-30 09:30:01 +1100503 case 's':
504 subsystem_flag = 1;
505 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000506 case 'S':
507 if (options.control_path != NULL)
508 free(options.control_path);
509 options.control_path = xstrdup(optarg);
Damien Miller0e220db2004-06-15 10:34:08 +1000510 break;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000511 case 'b':
512 options.bind_address = optarg;
513 break;
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000514 case 'F':
515 config = optarg;
516 break;
Damien Miller95def091999-11-25 00:26:21 +1100517 default:
518 usage();
519 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000520 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000521
Damien Millerf4614452001-07-14 12:18:10 +1000522 ac -= optind;
523 av += optind;
524
525 if (ac > 0 && !host && **av != '-') {
Ben Lindstromc276c122002-12-23 02:14:51 +0000526 if (strrchr(*av, '@')) {
Damien Millerf4614452001-07-14 12:18:10 +1000527 p = xstrdup(*av);
Ben Lindstromc276c122002-12-23 02:14:51 +0000528 cp = strrchr(p, '@');
Damien Millerf4614452001-07-14 12:18:10 +1000529 if (cp == NULL || cp == p)
530 usage();
531 options.user = p;
532 *cp = '\0';
533 host = ++cp;
534 } else
535 host = *av;
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000536 if (ac > 1) {
537 optind = optreset = 1;
Damien Millerf4614452001-07-14 12:18:10 +1000538 goto again;
539 }
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000540 ac--, av++;
Damien Millerf4614452001-07-14 12:18:10 +1000541 }
542
Damien Miller95def091999-11-25 00:26:21 +1100543 /* Check that we got a host name. */
544 if (!host)
545 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000546
Damien Millercb5e44a2000-09-29 12:12:36 +1100547 SSLeay_add_all_algorithms();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100548 ERR_load_crypto_strings();
Damien Millercb5e44a2000-09-29 12:12:36 +1100549
Damien Miller95def091999-11-25 00:26:21 +1100550 /* Initialize the command to execute on remote host. */
551 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000552
Damien Miller5428f641999-11-25 11:54:57 +1100553 /*
554 * Save the command to execute on the remote host in a buffer. There
555 * is no limit on the length of the command, except by the maximum
556 * packet size. Also sets the tty flag if there is no command.
557 */
Damien Millerf4614452001-07-14 12:18:10 +1000558 if (!ac) {
Damien Miller95def091999-11-25 00:26:21 +1100559 /* No command specified - execute shell on a tty. */
560 tty_flag = 1;
Damien Miller832562e2001-01-30 09:30:01 +1100561 if (subsystem_flag) {
Damien Millerf4614452001-07-14 12:18:10 +1000562 fprintf(stderr,
563 "You must specify a subsystem to invoke.\n");
Damien Miller832562e2001-01-30 09:30:01 +1100564 usage();
565 }
Damien Miller95def091999-11-25 00:26:21 +1100566 } else {
Damien Millerf4614452001-07-14 12:18:10 +1000567 /* A command has been specified. Store it into the buffer. */
568 for (i = 0; i < ac; i++) {
569 if (i)
Damien Miller95def091999-11-25 00:26:21 +1100570 buffer_append(&command, " ", 1);
571 buffer_append(&command, av[i], strlen(av[i]));
572 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000573 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000574
Damien Miller95def091999-11-25 00:26:21 +1100575 /* Cannot fork to background if no command. */
Damien Millercaf6dd62000-08-29 11:33:50 +1100576 if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
Damien Miller95def091999-11-25 00:26:21 +1100577 fatal("Cannot fork into background without a command to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000578
Damien Miller95def091999-11-25 00:26:21 +1100579 /* Allocate a tty by default if no command specified. */
580 if (buffer_len(&command) == 0)
581 tty_flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000582
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000583 /* Force no tty */
Ben Lindstromc72745a2000-12-02 19:03:54 +0000584 if (no_tty_flag)
585 tty_flag = 0;
Damien Miller95def091999-11-25 00:26:21 +1100586 /* Do not allocate a tty if stdin is not a tty. */
Damien Millerddee5752005-05-26 12:05:05 +1000587 if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100588 if (tty_flag)
Damien Miller996acd22003-04-09 20:59:48 +1000589 logit("Pseudo-terminal will not be allocated because stdin is not a terminal.");
Damien Miller95def091999-11-25 00:26:21 +1100590 tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000591 }
Damien Miller1383bd82000-04-06 12:32:37 +1000592
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000593 /*
594 * Initialize "log" output. Since we are the client all output
595 * actually goes to stderr.
596 */
Ben Lindstrom2b646522001-04-12 16:16:57 +0000597 log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
598 SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000599
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000600 /*
601 * Read per-user configuration file. Ignore the system wide config
602 * file if the user specifies a config file on the command line.
603 */
604 if (config != NULL) {
Damien Miller914420f2004-04-20 20:14:07 +1000605 if (!read_config_file(config, host, &options, 0))
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000606 fatal("Can't open user config file %.100s: "
607 "%.100s", config, strerror(errno));
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000608 } else {
609 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
610 _PATH_SSH_USER_CONFFILE);
Damien Miller57a44762004-04-20 20:11:57 +1000611 (void)read_config_file(buf, host, &options, 1);
Ben Lindstrom83f07d12001-10-03 17:22:29 +0000612
613 /* Read systemwide configuration file after use config. */
Darren Tuckerfc959702004-07-17 16:12:08 +1000614 (void)read_config_file(_PATH_HOST_CONFIG_FILE, host,
Damien Miller57a44762004-04-20 20:11:57 +1000615 &options, 0);
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000616 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000617
Damien Miller95def091999-11-25 00:26:21 +1100618 /* Fill configuration defaults. */
619 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000620
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000621 channel_set_af(options.address_family);
622
Damien Miller95def091999-11-25 00:26:21 +1100623 /* reinit */
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000624 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000625
Damien Miller60bc5172001-03-19 09:38:15 +1100626 seed_rng();
627
Damien Miller95def091999-11-25 00:26:21 +1100628 if (options.user == NULL)
629 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000630
Damien Miller95def091999-11-25 00:26:21 +1100631 if (options.hostname != NULL)
632 host = options.hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000633
Darren Tucker3f521e22003-07-03 16:20:42 +1000634 /* force lowercase for hostkey matching */
635 if (options.host_key_alias != NULL) {
636 for (p = options.host_key_alias; *p; p++)
637 if (isupper(*p))
Damien Miller1d2b6702006-03-26 14:09:54 +1100638 *p = (char)tolower(*p);
Darren Tucker3f521e22003-07-03 16:20:42 +1000639 }
640
Damien Miller7c71cc72005-06-26 08:56:31 +1000641 /* Get default port if port has not been set. */
642 if (options.port == 0) {
643 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
644 options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
645 }
646
Damien Miller9f1e33a2003-02-24 11:57:32 +1100647 if (options.proxy_command != NULL &&
648 strcmp(options.proxy_command, "none") == 0)
649 options.proxy_command = NULL;
Damien Miller8f74c8f2005-06-26 08:56:03 +1000650 if (options.control_path != NULL &&
651 strcmp(options.control_path, "none") == 0)
652 options.control_path = NULL;
Damien Miller9f1e33a2003-02-24 11:57:32 +1100653
Damien Miller0e220db2004-06-15 10:34:08 +1000654 if (options.control_path != NULL) {
Damien Miller6b1d53c2006-03-31 23:13:21 +1100655 char thishost[NI_MAXHOST];
Damien Miller3ec54c72006-03-15 11:30:13 +1100656
Damien Miller6b1d53c2006-03-31 23:13:21 +1100657 if (gethostname(thishost, sizeof(thishost)) == -1)
Damien Miller3ec54c72006-03-15 11:30:13 +1100658 fatal("gethostname: %s", strerror(errno));
Damien Miller6476cad2005-06-16 13:18:34 +1000659 snprintf(buf, sizeof(buf), "%d", options.port);
660 cp = tilde_expand_filename(options.control_path,
661 original_real_uid);
662 options.control_path = percent_expand(cp, "p", buf, "h", host,
Damien Miller6b1d53c2006-03-31 23:13:21 +1100663 "r", options.user, "l", thishost, (char *)NULL);
Damien Miller6476cad2005-06-16 13:18:34 +1000664 xfree(cp);
Damien Miller0e220db2004-06-15 10:34:08 +1000665 }
Darren Tucker0814d312005-06-01 23:08:51 +1000666 if (mux_command != 0 && options.control_path == NULL)
667 fatal("No ControlPath specified for \"-O\" command");
Damien Millerd14b1e72005-06-16 13:19:41 +1000668 if (options.control_path != NULL)
Damien Millerdadfd4d2005-05-26 12:07:13 +1000669 control_client(options.control_path);
Damien Miller0e220db2004-06-15 10:34:08 +1000670
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000671 /* Open a connection to the remote host. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000672 if (ssh_connect(host, &hostaddr, options.port,
673 options.address_family, options.connection_attempts,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000674#ifdef HAVE_CYGWIN
675 options.use_privileged_port,
676#else
Ben Lindstromf9c48842002-06-11 16:37:51 +0000677 original_effective_uid == 0 && options.use_privileged_port,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000678#endif
Ben Lindstromda394ca2002-06-12 16:11:12 +0000679 options.proxy_command) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100680 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000681
Damien Miller5428f641999-11-25 11:54:57 +1100682 /*
683 * If we successfully made the connection, load the host private key
684 * in case we will need it later for combined rsa-rhosts
685 * authentication. This must be done before releasing extra
686 * privileges, because the file is only readable by root.
Ben Lindstrom9e5bb572002-06-06 19:58:27 +0000687 * If we cannot access the private keys, load the public keys
688 * instead and try to execute the ssh-keysign helper instead.
Damien Miller5428f641999-11-25 11:54:57 +1100689 */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000690 sensitive_data.nkeys = 0;
691 sensitive_data.keys = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000692 sensitive_data.external_keysign = 0;
Ben Lindstrom24157572002-06-12 16:09:39 +0000693 if (options.rhosts_rsa_authentication ||
694 options.hostbased_authentication) {
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000695 sensitive_data.nkeys = 3;
Damien Millerddd63ab2006-03-31 23:10:51 +1100696 sensitive_data.keys = xcalloc(sensitive_data.nkeys,
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000697 sizeof(Key));
Ben Lindstromf9c48842002-06-11 16:37:51 +0000698
699 PRIV_START;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000700 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
Darren Tucker232b76f2006-05-06 17:41:51 +1000701 _PATH_HOST_KEY_FILE, "", NULL, NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000702 sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
Darren Tucker232b76f2006-05-06 17:41:51 +1000703 _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000704 sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
Darren Tucker232b76f2006-05-06 17:41:51 +1000705 _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL);
Ben Lindstromf9c48842002-06-11 16:37:51 +0000706 PRIV_END;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000707
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +0000708 if (options.hostbased_authentication == 1 &&
709 sensitive_data.keys[0] == NULL &&
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000710 sensitive_data.keys[1] == NULL &&
711 sensitive_data.keys[2] == NULL) {
712 sensitive_data.keys[1] = key_load_public(
713 _PATH_HOST_DSA_KEY_FILE, NULL);
714 sensitive_data.keys[2] = key_load_public(
715 _PATH_HOST_RSA_KEY_FILE, NULL);
716 sensitive_data.external_keysign = 1;
717 }
Damien Miller95def091999-11-25 00:26:21 +1100718 }
Damien Miller5428f641999-11-25 11:54:57 +1100719 /*
720 * Get rid of any extra privileges that we may have. We will no
721 * longer need them. Also, extra privileges could make it very hard
722 * to read identity files and other non-world-readable files from the
723 * user's home directory if it happens to be on a NFS volume where
724 * root is mapped to nobody.
725 */
Darren Tucker25f60a72004-08-15 17:23:34 +1000726 if (original_effective_uid == 0) {
727 PRIV_START;
728 permanently_set_uid(pw);
729 }
Damien Miller95def091999-11-25 00:26:21 +1100730
Damien Miller5428f641999-11-25 11:54:57 +1100731 /*
732 * Now that we are back to our own permissions, create ~/.ssh
Damien Miller788f2122005-11-05 15:14:59 +1100733 * directory if it doesn't already exist.
Damien Miller5428f641999-11-25 11:54:57 +1100734 */
Ben Lindstrom930b14a2001-08-15 23:19:21 +0000735 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 +1100736 if (stat(buf, &st) < 0)
Damien Millerbe484b52000-07-15 14:14:16 +1000737 if (mkdir(buf, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100738 error("Could not create directory '%.200s'.", buf);
739
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000740 /* load options.identity_files */
741 load_public_identity_files();
742
743 /* Expand ~ in known host file names. */
744 /* XXX mem-leaks: */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100745 options.system_hostfile =
746 tilde_expand_filename(options.system_hostfile, original_real_uid);
747 options.user_hostfile =
748 tilde_expand_filename(options.user_hostfile, original_real_uid);
749 options.system_hostfile2 =
750 tilde_expand_filename(options.system_hostfile2, original_real_uid);
751 options.user_hostfile2 =
752 tilde_expand_filename(options.user_hostfile2, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100753
Damien Miller07cd5892001-11-12 10:52:03 +1100754 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
755
Damien Miller95def091999-11-25 00:26:21 +1100756 /* Log into the remote system. This never returns if the login fails. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000757 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, pw);
Damien Miller95def091999-11-25 00:26:21 +1100758
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000759 /* We no longer need the private host keys. Clear them now. */
760 if (sensitive_data.nkeys != 0) {
761 for (i = 0; i < sensitive_data.nkeys; i++) {
762 if (sensitive_data.keys[i] != NULL) {
763 /* Destroys contents safely */
764 debug3("clear hostkey %d", i);
765 key_free(sensitive_data.keys[i]);
766 sensitive_data.keys[i] = NULL;
767 }
768 }
769 xfree(sensitive_data.keys);
770 }
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +0000771 for (i = 0; i < options.num_identity_files; i++) {
772 if (options.identity_files[i]) {
773 xfree(options.identity_files[i]);
774 options.identity_files[i] = NULL;
775 }
776 if (options.identity_keys[i]) {
777 key_free(options.identity_keys[i]);
778 options.identity_keys[i] = NULL;
779 }
780 }
Damien Miller95def091999-11-25 00:26:21 +1100781
Damien Miller1383bd82000-04-06 12:32:37 +1000782 exit_status = compat20 ? ssh_session2() : ssh_session();
783 packet_close();
Damien Miller8c4e18a2002-09-19 12:05:02 +1000784
Damien Miller0e220db2004-06-15 10:34:08 +1000785 if (options.control_path != NULL && control_fd != -1)
786 unlink(options.control_path);
787
Damien Miller8c4e18a2002-09-19 12:05:02 +1000788 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100789 * Send SIGHUP to proxy command if used. We don't wait() in
Damien Miller8c4e18a2002-09-19 12:05:02 +1000790 * case it hangs and instead rely on init to reap the child
791 */
792 if (proxy_command_pid > 1)
793 kill(proxy_command_pid, SIGHUP);
794
Damien Miller1383bd82000-04-06 12:32:37 +1000795 return exit_status;
796}
797
Ben Lindstrombba81212001-06-25 05:01:22 +0000798static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100799ssh_init_forwarding(void)
800{
Kevin Steves12057502001-02-05 14:54:34 +0000801 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100802 int i;
Kevin Steves12057502001-02-05 14:54:34 +0000803
Damien Miller0bc1bd82000-11-13 22:57:25 +1100804 /* Initiate local TCP/IP port forwardings. */
805 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100806 debug("Local connections to %.200s:%d forwarded to remote "
807 "address %.200s:%d",
Darren Tucker47eede72005-03-14 23:08:12 +1100808 (options.local_forwards[i].listen_host == NULL) ?
809 (options.gateway_ports ? "*" : "LOCALHOST") :
Damien Millerf91ee4c2005-03-01 21:24:33 +1100810 options.local_forwards[i].listen_host,
811 options.local_forwards[i].listen_port,
812 options.local_forwards[i].connect_host,
813 options.local_forwards[i].connect_port);
Damien Millerb16461c2002-01-22 23:29:22 +1100814 success += channel_setup_local_fwd_listener(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100815 options.local_forwards[i].listen_host,
816 options.local_forwards[i].listen_port,
817 options.local_forwards[i].connect_host,
818 options.local_forwards[i].connect_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100819 options.gateway_ports);
820 }
Darren Tuckere7d4b192006-07-12 22:17:10 +1000821 if (i > 0 && success != i && options.exit_on_forward_failure)
822 fatal("Could not request local forwarding.");
Kevin Steves12057502001-02-05 14:54:34 +0000823 if (i > 0 && success == 0)
824 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100825
826 /* Initiate remote TCP/IP port forwardings. */
827 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100828 debug("Remote connections from %.200s:%d forwarded to "
829 "local address %.200s:%d",
Damien Miller46d38de2005-07-17 17:02:09 +1000830 (options.remote_forwards[i].listen_host == NULL) ?
Damien Milleraa3bb102005-11-05 15:12:59 +1100831 "LOCALHOST" : options.remote_forwards[i].listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100832 options.remote_forwards[i].listen_port,
833 options.remote_forwards[i].connect_host,
834 options.remote_forwards[i].connect_port);
Darren Tuckere7d4b192006-07-12 22:17:10 +1000835 if (channel_request_remote_forwarding(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100836 options.remote_forwards[i].listen_host,
837 options.remote_forwards[i].listen_port,
838 options.remote_forwards[i].connect_host,
Darren Tuckere7d4b192006-07-12 22:17:10 +1000839 options.remote_forwards[i].connect_port) < 0) {
840 if (options.exit_on_forward_failure)
841 fatal("Could not request remote forwarding.");
842 else
843 logit("Warning: Could not request remote "
844 "forwarding.");
845 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100846 }
847}
848
Ben Lindstrombba81212001-06-25 05:01:22 +0000849static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100850check_agent_present(void)
851{
852 if (options.forward_agent) {
Damien Miller788f2122005-11-05 15:14:59 +1100853 /* Clear agent forwarding if we don't have an agent. */
Damien Miller789e95d2002-09-12 09:52:46 +1000854 if (!ssh_agent_present())
Damien Miller0bc1bd82000-11-13 22:57:25 +1100855 options.forward_agent = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100856 }
857}
858
Ben Lindstrombba81212001-06-25 05:01:22 +0000859static int
Damien Miller1383bd82000-04-06 12:32:37 +1000860ssh_session(void)
861{
862 int type;
Damien Miller1383bd82000-04-06 12:32:37 +1000863 int interactive = 0;
864 int have_tty = 0;
865 struct winsize ws;
Damien Miller1383bd82000-04-06 12:32:37 +1000866 char *cp;
Damien Miller17e7ed02005-06-17 12:54:33 +1000867 const char *display;
Damien Miller1383bd82000-04-06 12:32:37 +1000868
Damien Miller95def091999-11-25 00:26:21 +1100869 /* Enable compression if requested. */
870 if (options.compression) {
871 debug("Requesting compression at level %d.", options.compression_level);
872
873 if (options.compression_level < 1 || options.compression_level > 9)
874 fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
875
876 /* Send the request. */
877 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
878 packet_put_int(options.compression_level);
879 packet_send();
880 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100881 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100882 if (type == SSH_SMSG_SUCCESS)
883 packet_start_compression(options.compression_level);
884 else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000885 logit("Warning: Remote host refused compression.");
Damien Miller95def091999-11-25 00:26:21 +1100886 else
887 packet_disconnect("Protocol error waiting for compression response.");
888 }
889 /* Allocate a pseudo tty if appropriate. */
890 if (tty_flag) {
891 debug("Requesting pty.");
892
893 /* Start the packet. */
894 packet_start(SSH_CMSG_REQUEST_PTY);
895
896 /* Store TERM in the packet. There is no limit on the
897 length of the string. */
898 cp = getenv("TERM");
899 if (!cp)
900 cp = "";
Ben Lindstrom664408d2001-06-09 01:42:01 +0000901 packet_put_cstring(cp);
Damien Miller95def091999-11-25 00:26:21 +1100902
903 /* Store window size in the packet. */
904 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
905 memset(&ws, 0, sizeof(ws));
Damien Miller71a73672006-03-26 14:04:36 +1100906 packet_put_int((u_int)ws.ws_row);
907 packet_put_int((u_int)ws.ws_col);
908 packet_put_int((u_int)ws.ws_xpixel);
909 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller95def091999-11-25 00:26:21 +1100910
911 /* Store tty modes in the packet. */
Ben Lindstromae8e2d32001-04-14 23:13:02 +0000912 tty_make_modes(fileno(stdin), NULL);
Damien Miller95def091999-11-25 00:26:21 +1100913
914 /* Send the packet, and wait for it to leave. */
915 packet_send();
916 packet_write_wait();
917
918 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100919 type = packet_read();
Damien Miller450a7a12000-03-26 13:04:51 +1000920 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100921 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +1000922 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000923 } else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000924 logit("Warning: Remote host failed or refused to allocate a pseudo tty.");
Damien Miller95def091999-11-25 00:26:21 +1100925 else
926 packet_disconnect("Protocol error waiting for pty request response.");
927 }
928 /* Request X11 forwarding if enabled and DISPLAY is set. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000929 display = getenv("DISPLAY");
930 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000931 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +1000932 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000933 client_x11_get_proto(display, options.xauth_location,
934 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +1000935 /* Request forwarding with authentication spoofing. */
Damien Miller95def091999-11-25 00:26:21 +1100936 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +1000937 x11_request_forwarding_with_spoofing(0, display, proto, data);
Damien Miller95def091999-11-25 00:26:21 +1100938
939 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100940 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100941 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100942 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +1000943 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller996acd22003-04-09 20:59:48 +1000944 logit("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +1000945 } else {
Damien Miller95def091999-11-25 00:26:21 +1100946 packet_disconnect("Protocol error waiting for X11 forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +1000947 }
Damien Miller95def091999-11-25 00:26:21 +1100948 }
949 /* Tell the packet module whether this is an interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000950 packet_set_interactive(interactive);
Damien Miller95def091999-11-25 00:26:21 +1100951
952 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100953 check_agent_present();
954
Damien Miller95def091999-11-25 00:26:21 +1100955 if (options.forward_agent) {
956 debug("Requesting authentication agent forwarding.");
957 auth_request_forwarding();
958
959 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100960 type = packet_read();
Damien Miller48b03fc2002-01-22 23:11:40 +1100961 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100962 if (type != SSH_SMSG_SUCCESS)
Damien Miller996acd22003-04-09 20:59:48 +1000963 logit("Warning: Remote host denied authentication agent forwarding.");
Damien Miller95def091999-11-25 00:26:21 +1100964 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000965
Damien Miller0bc1bd82000-11-13 22:57:25 +1100966 /* Initiate port forwardings. */
967 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000968
Damien Miller5428f641999-11-25 11:54:57 +1100969 /* If requested, let ssh continue in the background. */
Damien Miller4af51302000-04-16 11:18:38 +1000970 if (fork_after_authentication_flag)
Damien Miller5428f641999-11-25 11:54:57 +1100971 if (daemon(1, 1) < 0)
972 fatal("daemon() failed: %.200s", strerror(errno));
973
974 /*
975 * If a command was specified on the command line, execute the
976 * command now. Otherwise request the server to start a shell.
977 */
Damien Miller95def091999-11-25 00:26:21 +1100978 if (buffer_len(&command) > 0) {
979 int len = buffer_len(&command);
980 if (len > 900)
981 len = 900;
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100982 debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
Damien Miller95def091999-11-25 00:26:21 +1100983 packet_start(SSH_CMSG_EXEC_CMD);
984 packet_put_string(buffer_ptr(&command), buffer_len(&command));
985 packet_send();
986 packet_write_wait();
987 } else {
988 debug("Requesting shell.");
989 packet_start(SSH_CMSG_EXEC_SHELL);
990 packet_send();
991 packet_write_wait();
992 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000993
Damien Miller95def091999-11-25 00:26:21 +1100994 /* Enter the interactive session. */
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000995 return client_loop(have_tty, tty_flag ?
996 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
Damien Miller1383bd82000-04-06 12:32:37 +1000997}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000998
Ben Lindstrombba81212001-06-25 05:01:22 +0000999static void
Damien Miller0e220db2004-06-15 10:34:08 +10001000ssh_subsystem_reply(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001001{
1002 int id, len;
1003
1004 id = packet_get_int();
1005 len = buffer_len(&command);
Ben Lindstrom4040fe12001-03-05 06:52:57 +00001006 if (len > 900)
1007 len = 900;
Damien Miller48b03fc2002-01-22 23:11:40 +11001008 packet_check_eom();
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001009 if (type == SSH2_MSG_CHANNEL_FAILURE)
1010 fatal("Request for subsystem '%.*s' failed on channel %d",
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001011 len, (u_char *)buffer_ptr(&command), id);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001012}
1013
Damien Miller2797f7f2002-04-23 21:09:44 +10001014void
Damien Miller509b0102003-12-17 16:33:10 +11001015client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt)
Damien Miller2797f7f2002-04-23 21:09:44 +10001016{
1017 int i;
1018
1019 i = client_global_request_id++;
Damien Miller509b0102003-12-17 16:33:10 +11001020 if (i >= options.num_remote_forwards)
Damien Miller2797f7f2002-04-23 21:09:44 +10001021 return;
Damien Miller2797f7f2002-04-23 21:09:44 +10001022 debug("remote forward %s for: listen %d, connect %s:%d",
1023 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
Damien Millerf91ee4c2005-03-01 21:24:33 +11001024 options.remote_forwards[i].listen_port,
1025 options.remote_forwards[i].connect_host,
1026 options.remote_forwards[i].connect_port);
Darren Tuckere7d4b192006-07-12 22:17:10 +10001027 if (type == SSH2_MSG_REQUEST_FAILURE) {
1028 if (options.exit_on_forward_failure)
1029 fatal("Error: remote port forwarding failed for "
1030 "listen port %d",
1031 options.remote_forwards[i].listen_port);
1032 else
1033 logit("Warning: remote port forwarding failed for "
1034 "listen port %d",
1035 options.remote_forwards[i].listen_port);
1036 }
Damien Miller2797f7f2002-04-23 21:09:44 +10001037}
1038
Damien Miller0e220db2004-06-15 10:34:08 +10001039static void
1040ssh_control_listener(void)
1041{
1042 struct sockaddr_un addr;
1043 mode_t old_umask;
Damien Miller07b6ff12004-06-15 11:14:45 +10001044 int addr_len;
1045
Damien Millerd14b1e72005-06-16 13:19:41 +10001046 if (options.control_path == NULL ||
1047 options.control_master == SSHCTL_MASTER_NO)
Damien Miller0e220db2004-06-15 10:34:08 +10001048 return;
1049
Damien Millerd14b1e72005-06-16 13:19:41 +10001050 debug("setting up multiplex master socket");
1051
Damien Miller0e220db2004-06-15 10:34:08 +10001052 memset(&addr, '\0', sizeof(addr));
1053 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001054 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001055 strlen(options.control_path) + 1;
1056
1057 if (strlcpy(addr.sun_path, options.control_path,
1058 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1059 fatal("ControlPath too long");
1060
1061 if ((control_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001062 fatal("%s socket(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001063
1064 old_umask = umask(0177);
Damien Miller90967402006-03-26 14:07:26 +11001065 if (bind(control_fd, (struct sockaddr *)&addr, addr_len) == -1) {
Damien Miller0e220db2004-06-15 10:34:08 +10001066 control_fd = -1;
Damien Miller4f10e252005-05-04 15:33:09 +10001067 if (errno == EINVAL || errno == EADDRINUSE)
Damien Miller0e220db2004-06-15 10:34:08 +10001068 fatal("ControlSocket %s already exists",
1069 options.control_path);
1070 else
Damien Miller15d72a02005-11-05 15:07:33 +11001071 fatal("%s bind(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001072 }
1073 umask(old_umask);
1074
1075 if (listen(control_fd, 64) == -1)
Damien Miller15d72a02005-11-05 15:07:33 +11001076 fatal("%s listen(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001077
1078 set_nonblock(control_fd);
1079}
1080
Ben Lindstromf558cf62001-09-20 23:13:49 +00001081/* request pty/x11/agent/tcpfwd/shell for channel */
Ben Lindstrombba81212001-06-25 05:01:22 +00001082static void
Ben Lindstromf558cf62001-09-20 23:13:49 +00001083ssh_session2_setup(int id, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +10001084{
Damien Miller3756dce2004-06-18 01:17:29 +10001085 extern char **environ;
Damien Miller17e7ed02005-06-17 12:54:33 +10001086 const char *display;
Damien Miller0e220db2004-06-15 10:34:08 +10001087 int interactive = tty_flag;
Damien Miller17e7ed02005-06-17 12:54:33 +10001088
Damien Miller46d38de2005-07-17 17:02:09 +10001089 display = getenv("DISPLAY");
Damien Miller17e7ed02005-06-17 12:54:33 +10001090 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001091 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001092 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001093 client_x11_get_proto(display, options.xauth_location,
1094 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001095 /* Request forwarding with authentication spoofing. */
1096 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +10001097 x11_request_forwarding_with_spoofing(id, display, proto, data);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001098 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001099 /* XXX wait for reply */
1100 }
1101
Damien Miller0bc1bd82000-11-13 22:57:25 +11001102 check_agent_present();
1103 if (options.forward_agent) {
1104 debug("Requesting authentication agent forwarding.");
1105 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1106 packet_send();
1107 }
1108
Damien Miller7b58e802005-12-13 19:33:19 +11001109 if (options.tun_open != SSH_TUNMODE_NO) {
Damien Millerd27b9472005-12-13 19:29:02 +11001110 Channel *c;
1111 int fd;
1112
1113 debug("Requesting tun.");
Damien Miller7b58e802005-12-13 19:33:19 +11001114 if ((fd = tun_open(options.tun_local,
1115 options.tun_open)) >= 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11001116 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1117 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1118 0, "tun", 1);
1119 c->datagram = 1;
Damien Miller598bbc22005-12-31 16:33:36 +11001120#if defined(SSH_TUN_FILTER)
1121 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1122 channel_register_filter(c->self, sys_tun_infilter,
1123 sys_tun_outfilter);
1124#endif
Damien Millerd27b9472005-12-13 19:29:02 +11001125 packet_start(SSH2_MSG_CHANNEL_OPEN);
1126 packet_put_cstring("tun@openssh.com");
1127 packet_put_int(c->self);
1128 packet_put_int(c->local_window_max);
1129 packet_put_int(c->local_maxpacket);
Damien Miller7b58e802005-12-13 19:33:19 +11001130 packet_put_int(options.tun_open);
Damien Millerd27b9472005-12-13 19:29:02 +11001131 packet_put_int(options.tun_remote);
1132 packet_send();
1133 }
1134 }
1135
Damien Miller0e220db2004-06-15 10:34:08 +10001136 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
Damien Miller3756dce2004-06-18 01:17:29 +10001137 NULL, fileno(stdin), &command, environ, &ssh_subsystem_reply);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001138
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001139 packet_set_interactive(interactive);
Damien Miller1383bd82000-04-06 12:32:37 +10001140}
1141
Ben Lindstromf558cf62001-09-20 23:13:49 +00001142/* open new channel for a session */
Ben Lindstrombba81212001-06-25 05:01:22 +00001143static int
Ben Lindstromf558cf62001-09-20 23:13:49 +00001144ssh_session2_open(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001145{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001146 Channel *c;
1147 int window, packetmax, in, out, err;
Damien Millerad833b32000-08-23 10:46:23 +10001148
Damien Millercaf6dd62000-08-29 11:33:50 +11001149 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001150 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001151 } else {
1152 in = dup(STDIN_FILENO);
1153 }
1154 out = dup(STDOUT_FILENO);
1155 err = dup(STDERR_FILENO);
1156
1157 if (in < 0 || out < 0 || err < 0)
1158 fatal("dup() in/out/err failed");
1159
Damien Miller69b69aa2000-10-28 14:19:58 +11001160 /* enable nonblocking unless tty */
1161 if (!isatty(in))
1162 set_nonblock(in);
1163 if (!isatty(out))
1164 set_nonblock(out);
1165 if (!isatty(err))
1166 set_nonblock(err);
1167
Damien Millere4340be2000-09-16 13:29:08 +11001168 window = CHAN_SES_WINDOW_DEFAULT;
1169 packetmax = CHAN_SES_PACKET_DEFAULT;
Damien Miller19a59452002-02-19 15:20:57 +11001170 if (tty_flag) {
1171 window >>= 1;
1172 packetmax >>= 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001173 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001174 c = channel_new(
Damien Miller1383bd82000-04-06 12:32:37 +10001175 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001176 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001177 "client-session", /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001178
Ben Lindstromf558cf62001-09-20 23:13:49 +00001179 debug3("ssh_session2_open: channel_new: %d", c->self);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001180
Ben Lindstrom5ec26452001-06-09 00:18:51 +00001181 channel_send_open(c->self);
Ben Lindstromf558cf62001-09-20 23:13:49 +00001182 if (!no_shell_flag)
Damien Miller0e220db2004-06-15 10:34:08 +10001183 channel_register_confirm(c->self, ssh_session2_setup, NULL);
Damien Miller1383bd82000-04-06 12:32:37 +10001184
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001185 return c->self;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001186}
1187
Ben Lindstrombba81212001-06-25 05:01:22 +00001188static int
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001189ssh_session2(void)
1190{
Ben Lindstromf558cf62001-09-20 23:13:49 +00001191 int id = -1;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001192
1193 /* XXX should be pre-session */
1194 ssh_init_forwarding();
Damien Miller0e220db2004-06-15 10:34:08 +10001195 ssh_control_listener();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001196
Ben Lindstromf558cf62001-09-20 23:13:49 +00001197 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1198 id = ssh_session2_open();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001199
Damien Millerd27b9472005-12-13 19:29:02 +11001200 /* Execute a local command */
1201 if (options.local_command != NULL &&
1202 options.permit_local_command)
1203 ssh_local_cmd(options.local_command);
1204
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001205 /* If requested, let ssh continue in the background. */
1206 if (fork_after_authentication_flag)
1207 if (daemon(1, 1) < 0)
1208 fatal("daemon() failed: %.200s", strerror(errno));
1209
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001210 return client_loop(tty_flag, tty_flag ?
1211 options.escape_char : SSH_ESCAPECHAR_NONE, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001212}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001213
Ben Lindstrombba81212001-06-25 05:01:22 +00001214static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001215load_public_identity_files(void)
1216{
Damien Miller6b1d53c2006-03-31 23:13:21 +11001217 char *filename, *cp, thishost[NI_MAXHOST];
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001218 int i = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001219 Key *public;
Damien Miller6b1d53c2006-03-31 23:13:21 +11001220 struct passwd *pw;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001221#ifdef SMARTCARD
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001222 Key **keys;
1223
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001224 if (options.smartcard_device != NULL &&
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001225 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1226 (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL ) {
1227 int count = 0;
1228 for (i = 0; keys[i] != NULL; i++) {
1229 count++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001230 memmove(&options.identity_files[1], &options.identity_files[0],
1231 sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
1232 memmove(&options.identity_keys[1], &options.identity_keys[0],
1233 sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1234 options.num_identity_files++;
1235 options.identity_keys[0] = keys[i];
Damien Miller56a0bb02003-06-18 20:28:40 +10001236 options.identity_files[0] = sc_get_key_label(keys[i]);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001237 }
Ben Lindstrom4f054602002-03-26 03:23:00 +00001238 if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1239 options.num_identity_files = SSH_MAX_IDENTITY_FILES;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001240 i = count;
1241 xfree(keys);
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001242 }
Ben Lindstromffce1472001-08-06 21:57:31 +00001243#endif /* SMARTCARD */
Damien Miller6b1d53c2006-03-31 23:13:21 +11001244 if ((pw = getpwuid(original_real_uid)) == NULL)
1245 fatal("load_public_identity_files: getpwuid failed");
1246 if (gethostname(thishost, sizeof(thishost)) == -1)
1247 fatal("load_public_identity_files: gethostname: %s",
1248 strerror(errno));
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001249 for (; i < options.num_identity_files; i++) {
Damien Miller6b1d53c2006-03-31 23:13:21 +11001250 cp = tilde_expand_filename(options.identity_files[i],
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001251 original_real_uid);
Damien Miller6b1d53c2006-03-31 23:13:21 +11001252 filename = percent_expand(cp, "d", pw->pw_dir,
1253 "u", pw->pw_name, "l", thishost, "h", host,
1254 "r", options.user, (char *)NULL);
1255 xfree(cp);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001256 public = key_load_public(filename, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001257 debug("identity file %s type %d", filename,
1258 public ? public->type : -1);
1259 xfree(options.identity_files[i]);
1260 options.identity_files[i] = filename;
1261 options.identity_keys[i] = public;
1262 }
1263}
Damien Miller0e220db2004-06-15 10:34:08 +10001264
1265static void
1266control_client_sighandler(int signo)
1267{
1268 control_client_terminate = signo;
1269}
1270
1271static void
1272control_client_sigrelay(int signo)
1273{
1274 if (control_server_pid > 1)
1275 kill(control_server_pid, signo);
1276}
1277
Darren Tucker365433f2004-06-22 12:29:23 +10001278static int
1279env_permitted(char *env)
1280{
Darren Tucker284706a2006-07-12 22:16:23 +10001281 int i, ret;
Darren Tucker365433f2004-06-22 12:29:23 +10001282 char name[1024], *cp;
1283
Darren Tucker284706a2006-07-12 22:16:23 +10001284 if ((cp = strchr(env, '=')) == NULL || cp == env)
Darren Tucker365433f2004-06-22 12:29:23 +10001285 return (0);
Darren Tucker57f42242006-07-12 22:23:35 +10001286 ret = snprintf(name, sizeof(name), "%.*s", (int)(cp - env), env);
Darren Tucker284706a2006-07-12 22:16:23 +10001287 if (ret <= 0 || (size_t)ret >= sizeof(name))
1288 fatal("env_permitted: name '%.100s...' too long", env);
Darren Tucker365433f2004-06-22 12:29:23 +10001289
1290 for (i = 0; i < options.num_send_env; i++)
1291 if (match_pattern(name, options.send_env[i]))
1292 return (1);
1293
1294 return (0);
1295}
1296
Damien Miller0e220db2004-06-15 10:34:08 +10001297static void
1298control_client(const char *path)
1299{
1300 struct sockaddr_un addr;
Darren Tucker39207a42004-11-05 20:19:51 +11001301 int i, r, fd, sock, exitval, num_env, addr_len;
Damien Miller0e220db2004-06-15 10:34:08 +10001302 Buffer m;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001303 char *term;
Damien Miller3756dce2004-06-18 01:17:29 +10001304 extern char **environ;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001305 u_int flags;
Darren Tuckerfc959702004-07-17 16:12:08 +10001306
Damien Millerd14b1e72005-06-16 13:19:41 +10001307 if (mux_command == 0)
1308 mux_command = SSHMUX_COMMAND_OPEN;
1309
1310 switch (options.control_master) {
1311 case SSHCTL_MASTER_AUTO:
1312 case SSHCTL_MASTER_AUTO_ASK:
1313 debug("auto-mux: Trying existing master");
1314 /* FALLTHROUGH */
1315 case SSHCTL_MASTER_NO:
1316 break;
1317 default:
1318 return;
1319 }
1320
Damien Miller0e220db2004-06-15 10:34:08 +10001321 memset(&addr, '\0', sizeof(addr));
1322 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001323 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001324 strlen(path) + 1;
1325
1326 if (strlcpy(addr.sun_path, path,
1327 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1328 fatal("ControlPath too long");
1329
1330 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1331 fatal("%s socket(): %s", __func__, strerror(errno));
1332
Damien Miller90967402006-03-26 14:07:26 +11001333 if (connect(sock, (struct sockaddr *)&addr, addr_len) == -1) {
Darren Tucker0814d312005-06-01 23:08:51 +10001334 if (mux_command != SSHMUX_COMMAND_OPEN) {
1335 fatal("Control socket connect(%.100s): %s", path,
1336 strerror(errno));
1337 }
Damien Miller538c9b72005-05-26 12:11:28 +10001338 if (errno == ENOENT)
Damien Miller4662d342006-03-26 13:59:59 +11001339 debug("Control socket \"%.100s\" does not exist", path);
Damien Miller538c9b72005-05-26 12:11:28 +10001340 else {
Damien Miller4662d342006-03-26 13:59:59 +11001341 error("Control socket connect(%.100s): %s", path,
Damien Miller538c9b72005-05-26 12:11:28 +10001342 strerror(errno));
1343 }
Damien Miller4662d342006-03-26 13:59:59 +11001344 close(sock);
1345 return;
1346 }
Damien Miller46d38de2005-07-17 17:02:09 +10001347
Damien Miller4662d342006-03-26 13:59:59 +11001348 if (stdin_null_flag) {
1349 if ((fd = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1350 fatal("open(/dev/null): %s", strerror(errno));
1351 if (dup2(fd, STDIN_FILENO) == -1)
1352 fatal("dup2: %s", strerror(errno));
1353 if (fd > STDERR_FILENO)
1354 close(fd);
1355 }
Damien Miller46d38de2005-07-17 17:02:09 +10001356
Damien Miller13390022005-07-06 09:44:19 +10001357 term = getenv("TERM");
Darren Tucker7ebfc102004-11-07 20:06:19 +11001358
1359 flags = 0;
1360 if (tty_flag)
1361 flags |= SSHMUX_FLAG_TTY;
1362 if (subsystem_flag)
1363 flags |= SSHMUX_FLAG_SUBSYS;
Damien Miller13390022005-07-06 09:44:19 +10001364 if (options.forward_x11)
1365 flags |= SSHMUX_FLAG_X11_FWD;
1366 if (options.forward_agent)
1367 flags |= SSHMUX_FLAG_AGENT_FWD;
Damien Miller0e220db2004-06-15 10:34:08 +10001368
Damien Miller0e220db2004-06-15 10:34:08 +10001369 buffer_init(&m);
1370
Darren Tucker7ebfc102004-11-07 20:06:19 +11001371 /* Send our command to server */
1372 buffer_put_int(&m, mux_command);
1373 buffer_put_int(&m, flags);
Damien Miller13390022005-07-06 09:44:19 +10001374 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001375 fatal("%s: msg_send", __func__);
1376 buffer_clear(&m);
1377
1378 /* Get authorisation status and PID of controlee */
Damien Miller0e220db2004-06-15 10:34:08 +10001379 if (ssh_msg_recv(sock, &m) == -1)
1380 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001381 if (buffer_get_char(&m) != SSHMUX_VER)
Damien Miller0e220db2004-06-15 10:34:08 +10001382 fatal("%s: wrong version", __func__);
Damien Miller23f07702004-06-18 01:19:03 +10001383 if (buffer_get_int(&m) != 1)
1384 fatal("Connection to master denied");
Damien Miller0e220db2004-06-15 10:34:08 +10001385 control_server_pid = buffer_get_int(&m);
1386
Damien Miller0e220db2004-06-15 10:34:08 +10001387 buffer_clear(&m);
Damien Miller0e220db2004-06-15 10:34:08 +10001388
Darren Tucker7ebfc102004-11-07 20:06:19 +11001389 switch (mux_command) {
1390 case SSHMUX_COMMAND_ALIVE_CHECK:
Darren Tucker47eede72005-03-14 23:08:12 +11001391 fprintf(stderr, "Master running (pid=%d)\r\n",
Darren Tucker7ebfc102004-11-07 20:06:19 +11001392 control_server_pid);
1393 exit(0);
1394 case SSHMUX_COMMAND_TERMINATE:
1395 fprintf(stderr, "Exit request sent.\r\n");
1396 exit(0);
1397 case SSHMUX_COMMAND_OPEN:
1398 /* continue below */
1399 break;
1400 default:
1401 fatal("silly mux_command %d", mux_command);
1402 }
1403
1404 /* SSHMUX_COMMAND_OPEN */
Damien Miller13390022005-07-06 09:44:19 +10001405 buffer_put_cstring(&m, term ? term : "");
Damien Miller0e220db2004-06-15 10:34:08 +10001406 buffer_append(&command, "\0", 1);
1407 buffer_put_cstring(&m, buffer_ptr(&command));
1408
Darren Tucker365433f2004-06-22 12:29:23 +10001409 if (options.num_send_env == 0 || environ == NULL) {
1410 buffer_put_int(&m, 0);
Darren Tuckerfc959702004-07-17 16:12:08 +10001411 } else {
Darren Tucker365433f2004-06-22 12:29:23 +10001412 /* Pass environment */
1413 num_env = 0;
1414 for (i = 0; environ[i] != NULL; i++)
1415 if (env_permitted(environ[i]))
1416 num_env++; /* Count */
Darren Tuckerfc959702004-07-17 16:12:08 +10001417
Darren Tucker365433f2004-06-22 12:29:23 +10001418 buffer_put_int(&m, num_env);
1419
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001420 for (i = 0; environ[i] != NULL && num_env >= 0; i++)
1421 if (env_permitted(environ[i])) {
1422 num_env--;
Darren Tucker365433f2004-06-22 12:29:23 +10001423 buffer_put_cstring(&m, environ[i]);
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001424 }
Darren Tucker365433f2004-06-22 12:29:23 +10001425 }
Damien Miller3756dce2004-06-18 01:17:29 +10001426
Damien Miller13390022005-07-06 09:44:19 +10001427 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Damien Miller0e220db2004-06-15 10:34:08 +10001428 fatal("%s: msg_send", __func__);
1429
1430 mm_send_fd(sock, STDIN_FILENO);
1431 mm_send_fd(sock, STDOUT_FILENO);
1432 mm_send_fd(sock, STDERR_FILENO);
1433
1434 /* Wait for reply, so master has a chance to gather ttymodes */
1435 buffer_clear(&m);
1436 if (ssh_msg_recv(sock, &m) == -1)
1437 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001438 if (buffer_get_char(&m) != SSHMUX_VER)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001439 fatal("%s: wrong version", __func__);
Damien Miller0e220db2004-06-15 10:34:08 +10001440 buffer_free(&m);
1441
Darren Tucker07336da2004-11-05 20:02:16 +11001442 signal(SIGHUP, control_client_sighandler);
Damien Miller0809e232004-06-18 22:20:57 +10001443 signal(SIGINT, control_client_sighandler);
1444 signal(SIGTERM, control_client_sighandler);
1445 signal(SIGWINCH, control_client_sigrelay);
1446
Damien Miller0e220db2004-06-15 10:34:08 +10001447 if (tty_flag)
1448 enter_raw_mode();
1449
1450 /* Stick around until the controlee closes the client_fd */
1451 exitval = 0;
1452 for (;!control_client_terminate;) {
1453 r = read(sock, &exitval, sizeof(exitval));
1454 if (r == 0) {
1455 debug2("Received EOF from master");
1456 break;
1457 }
1458 if (r > 0)
1459 debug2("Received exit status from master %d", exitval);
1460 if (r == -1 && errno != EINTR)
1461 fatal("%s: read %s", __func__, strerror(errno));
1462 }
1463
1464 if (control_client_terminate)
1465 debug2("Exiting on signal %d", control_client_terminate);
1466
1467 close(sock);
1468
1469 leave_raw_mode();
1470
1471 if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
1472 fprintf(stderr, "Connection to master closed.\r\n");
1473
1474 exit(exitval);
1475}