blob: df787e45f7d0a2ccca267625b900fae3e9d8062a [file] [log] [blame]
Damien Millere3476ed2006-07-24 14:13:33 +10001/* $OpenBSD: ssh.c,v 1.289 2006/07/22 20:48:23 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 Millerb8fe89c2006-07-24 14:51:00 +100057#include <netdb.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110058#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110059#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110060#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100061#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110062#include <signal.h>
Damien Miller2d00e632006-07-24 13:53:19 +100063#include <stddef.h>
Damien Millere3476ed2006-07-24 14:13:33 +100064#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100065#include <unistd.h>
Damien Millereba71ba2000-04-29 23:57:08 +100066
67#include <openssl/evp.h>
Damien Miller0bc1bd82000-11-13 22:57:25 +110068#include <openssl/err.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000071#include "ssh1.h"
Damien Miller1383bd82000-04-06 12:32:37 +100072#include "ssh2.h"
73#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000074#include "cipher.h"
75#include "xmalloc.h"
76#include "packet.h"
77#include "buffer.h"
Damien Miller0e220db2004-06-15 10:34:08 +100078#include "bufaux.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000079#include "channels.h"
Damien Millereba71ba2000-04-29 23:57:08 +100080#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100081#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100082#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000083#include "pathnames.h"
Damien Miller0e220db2004-06-15 10:34:08 +100084#include "dispatch.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +000085#include "clientloop.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000086#include "log.h"
87#include "readconf.h"
88#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000089#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000090#include "kex.h"
91#include "mac.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +100092#include "sshpty.h"
Darren Tucker46bc0752004-05-02 22:11:30 +100093#include "match.h"
Damien Miller0e220db2004-06-15 10:34:08 +100094#include "msg.h"
95#include "monitor_fdpass.h"
Darren Tucker25f60a72004-08-15 17:23:34 +100096#include "uidswap.h"
Damien Millerb7576772006-07-10 20:23:39 +100097#include "version.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
Ben Lindstromc5b68002001-07-04 04:52:03 +000099#ifdef SMARTCARD
Ben Lindstromc5b68002001-07-04 04:52:03 +0000100#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +0000101#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000102
Damien Miller3f905871999-11-15 17:10:57 +1100103extern char *__progname;
Damien Miller3f905871999-11-15 17:10:57 +1100104
Damien Miller95def091999-11-25 00:26:21 +1100105/* Flag indicating whether debug mode is on. This can be set on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106int debug_flag = 0;
107
Damien Miller78928792000-04-12 20:17:38 +1000108/* Flag indicating whether a tty should be allocated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109int tty_flag = 0;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000110int no_tty_flag = 0;
111int force_tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112
Damien Miller1383bd82000-04-06 12:32:37 +1000113/* don't exec a shell */
114int no_shell_flag = 0;
Damien Miller1383bd82000-04-06 12:32:37 +1000115
Damien Miller5428f641999-11-25 11:54:57 +1100116/*
117 * Flag indicating that nothing should be read from stdin. This can be set
118 * on the command line.
119 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120int stdin_null_flag = 0;
121
Damien Miller5428f641999-11-25 11:54:57 +1100122/*
123 * Flag indicating that ssh should fork after authentication. This is useful
Ben Lindstromf666fec2002-06-06 19:51:58 +0000124 * so that the passphrase can be entered manually, and then ssh goes to the
Damien Miller5428f641999-11-25 11:54:57 +1100125 * background.
126 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127int fork_after_authentication_flag = 0;
128
Damien Miller5428f641999-11-25 11:54:57 +1100129/*
130 * General data structure for command line options and options configurable
131 * in configuration files. See readconf.h.
132 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000133Options options;
134
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000135/* optional user configfile */
136char *config = NULL;
137
Damien Miller5428f641999-11-25 11:54:57 +1100138/*
139 * Name of the host we are connecting to. This is the name given on the
140 * command line, or the HostName specified for the user-supplied name in a
141 * configuration file.
142 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143char *host;
144
145/* socket address the host resolves to */
Damien Miller34132e52000-01-14 15:45:46 +1100146struct sockaddr_storage hostaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000147
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000148/* Private host keys. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000149Sensitive sensitive_data;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150
151/* Original real UID. */
152uid_t original_real_uid;
Ben Lindstromf9c48842002-06-11 16:37:51 +0000153uid_t original_effective_uid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154
Damien Miller1383bd82000-04-06 12:32:37 +1000155/* command to be executed */
156Buffer command;
157
Damien Miller832562e2001-01-30 09:30:01 +1100158/* Should we execute a command or invoke a subsystem? */
159int subsystem_flag = 0;
160
Damien Miller2797f7f2002-04-23 21:09:44 +1000161/* # of replies received for global requests */
162static int client_global_request_id = 0;
163
Damien Miller8c4e18a2002-09-19 12:05:02 +1000164/* pid of proxycommand child process */
165pid_t proxy_command_pid = 0;
166
Damien Miller0e220db2004-06-15 10:34:08 +1000167/* fd to control socket */
168int control_fd = -1;
169
Darren Tucker7ebfc102004-11-07 20:06:19 +1100170/* Multiplexing control command */
Darren Tucker0814d312005-06-01 23:08:51 +1000171static u_int mux_command = 0;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100172
Damien Miller0e220db2004-06-15 10:34:08 +1000173/* Only used in control client mode */
174volatile sig_atomic_t control_client_terminate = 0;
175u_int control_server_pid = 0;
176
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000177/* Prints a help message to the user. This function never returns. */
178
Ben Lindstrombba81212001-06-25 05:01:22 +0000179static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000180usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000181{
Damien Miller50955102004-03-22 09:34:58 +1100182 fprintf(stderr,
Darren Tucker9c6bf322004-12-03 14:10:19 +1100183"usage: ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n"
Darren Tucker895d6982005-10-03 18:18:05 +1000184" [-D [bind_address:]port] [-e escape_char] [-F configfile]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100185" [-i identity_file] [-L [bind_address:]port:host:hostport]\n"
Damien Millerf91ee4c2005-03-01 21:24:33 +1100186" [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100187" [-R [bind_address:]port:host:hostport] [-S ctl_path]\n"
Damien Miller991dba42006-07-10 20:16:27 +1000188" [-w local_tun[:remote_tun]] [user@]hostname [command]\n"
Damien Miller50955102004-03-22 09:34:58 +1100189 );
Darren Tuckere9a9b712005-12-20 16:15:51 +1100190 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000191}
192
Ben Lindstrombba81212001-06-25 05:01:22 +0000193static int ssh_session(void);
194static int ssh_session2(void);
195static void load_public_identity_files(void);
Damien Miller0e220db2004-06-15 10:34:08 +1000196static void control_client(const char *path);
Damien Miller1383bd82000-04-06 12:32:37 +1000197
Damien Miller95def091999-11-25 00:26:21 +1100198/*
199 * Main program for the ssh client.
200 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000201int
202main(int ac, char **av)
203{
Ben Lindstrom24157572002-06-12 16:09:39 +0000204 int i, opt, exit_status;
Damien Miller9836cf82003-12-17 16:30:06 +1100205 char *p, *cp, *line, buf[256];
Damien Miller95def091999-11-25 00:26:21 +1100206 struct stat st;
Ben Lindstrom086cf212001-03-05 05:56:40 +0000207 struct passwd *pw;
Damien Miller1383bd82000-04-06 12:32:37 +1000208 int dummy;
Ben Lindstrom5ccf63a2001-09-24 20:00:10 +0000209 extern int optind, optreset;
Damien Miller4f8e6692001-07-14 13:22:53 +1000210 extern char *optarg;
Damien Miller9651fe62005-06-26 08:55:25 +1000211 struct servent *sp;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100212 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000213
Darren Tuckerce321d82005-10-03 18:11:24 +1000214 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
215 sanitise_stdfd();
216
Damien Miller59d3d5b2003-08-22 09:34:41 +1000217 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000218 init_rng();
219
Damien Miller5428f641999-11-25 11:54:57 +1100220 /*
221 * Save the original real uid. It will be needed later (uid-swapping
222 * may clobber the real uid).
223 */
Damien Miller95def091999-11-25 00:26:21 +1100224 original_real_uid = getuid();
225 original_effective_uid = geteuid();
Damien Millera8e06ce2003-11-21 23:48:55 +1100226
Damien Miller50b9a602002-09-04 16:50:06 +1000227 /*
228 * Use uid-swapping to give up root privileges for the duration of
229 * option processing. We will re-instantiate the rights when we are
230 * ready to create the privileged port, and will permanently drop
231 * them when the port has been created (actually, when the connection
232 * has been made, as we may need to create the port several times).
233 */
234 PRIV_END;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000235
Damien Miller05dd7952000-10-01 00:42:48 +1100236#ifdef HAVE_SETRLIMIT
Damien Miller95def091999-11-25 00:26:21 +1100237 /* If we are installed setuid root be careful to not drop core. */
238 if (original_real_uid != original_effective_uid) {
239 struct rlimit rlim;
240 rlim.rlim_cur = rlim.rlim_max = 0;
241 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
242 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000243 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100244#endif
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000245 /* Get user data. */
246 pw = getpwuid(original_real_uid);
247 if (!pw) {
Damien Miller996acd22003-04-09 20:59:48 +1000248 logit("You don't exist, go away!");
Darren Tuckere9a9b712005-12-20 16:15:51 +1100249 exit(255);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000250 }
251 /* Take a copy of the returned structure. */
252 pw = pwcopy(pw);
253
Damien Miller5428f641999-11-25 11:54:57 +1100254 /*
Damien Miller5428f641999-11-25 11:54:57 +1100255 * Set our umask to something reasonable, as some files are created
256 * with the default umask. This will make them world-readable but
257 * writable only by the owner, which is ok for all files for which we
258 * don't set the modes explicitly.
259 */
Damien Miller95def091999-11-25 00:26:21 +1100260 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000261
Damien Miller95def091999-11-25 00:26:21 +1100262 /* Initialize option structure to indicate that no values have been set. */
263 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000264
Damien Miller95def091999-11-25 00:26:21 +1100265 /* Parse command-line arguments. */
266 host = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000267
Damien Miller2ecb6bd2006-03-15 12:03:53 +1100268 again:
Damien Millerf4614452001-07-14 12:18:10 +1000269 while ((opt = getopt(ac, av,
Damien Millerd27b9472005-12-13 19:29:02 +1100270 "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 +1100271 switch (opt) {
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000272 case '1':
273 options.protocol = SSH_PROTO_1;
274 break;
Damien Miller4af51302000-04-16 11:18:38 +1000275 case '2':
276 options.protocol = SSH_PROTO_2;
277 break;
Damien Miller34132e52000-01-14 15:45:46 +1100278 case '4':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000279 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100280 break;
Damien Miller34132e52000-01-14 15:45:46 +1100281 case '6':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000282 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100283 break;
Damien Miller95def091999-11-25 00:26:21 +1100284 case 'n':
285 stdin_null_flag = 1;
286 break;
Damien Miller95def091999-11-25 00:26:21 +1100287 case 'f':
288 fork_after_authentication_flag = 1;
289 stdin_null_flag = 1;
290 break;
Damien Miller95def091999-11-25 00:26:21 +1100291 case 'x':
292 options.forward_x11 = 0;
293 break;
Damien Miller95def091999-11-25 00:26:21 +1100294 case 'X':
295 options.forward_x11 = 1;
296 break;
Darren Tucker0a118da2003-10-15 15:54:32 +1000297 case 'Y':
298 options.forward_x11 = 1;
299 options.forward_x11_trusted = 1;
300 break;
Damien Miller95def091999-11-25 00:26:21 +1100301 case 'g':
302 options.gateway_ports = 1;
303 break;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100304 case 'O':
305 if (strcmp(optarg, "check") == 0)
306 mux_command = SSHMUX_COMMAND_ALIVE_CHECK;
307 else if (strcmp(optarg, "exit") == 0)
308 mux_command = SSHMUX_COMMAND_TERMINATE;
309 else
310 fatal("Invalid multiplex command.");
311 break;
Damien Miller147bba32002-09-04 16:46:06 +1000312 case 'P': /* deprecated */
Damien Miller95def091999-11-25 00:26:21 +1100313 options.use_privileged_port = 0;
314 break;
Damien Miller95def091999-11-25 00:26:21 +1100315 case 'a':
316 options.forward_agent = 0;
317 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000318 case 'A':
319 options.forward_agent = 1;
320 break;
Damien Miller95def091999-11-25 00:26:21 +1100321 case 'k':
Damien Millere0113cc2003-11-24 13:10:09 +1100322 options.gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100323 break;
Damien Miller95def091999-11-25 00:26:21 +1100324 case 'i':
325 if (stat(optarg, &st) < 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000326 fprintf(stderr, "Warning: Identity file %s "
Damien Miller3eb48b62005-03-01 21:15:46 +1100327 "not accessible: %s.\n", optarg,
328 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100329 break;
330 }
Damien Millerf4614452001-07-14 12:18:10 +1000331 if (options.num_identity_files >=
332 SSH_MAX_IDENTITY_FILES)
333 fatal("Too many identity files specified "
334 "(max %d)", SSH_MAX_IDENTITY_FILES);
335 options.identity_files[options.num_identity_files++] =
336 xstrdup(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100337 break;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000338 case 'I':
339#ifdef SMARTCARD
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000340 options.smartcard_device = xstrdup(optarg);
Ben Lindstrombcc18082001-08-06 21:59:25 +0000341#else
Ben Lindstromc5b68002001-07-04 04:52:03 +0000342 fprintf(stderr, "no support for smartcards.\n");
Ben Lindstrombcc18082001-08-06 21:59:25 +0000343#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000344 break;
Damien Miller95def091999-11-25 00:26:21 +1100345 case 't':
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000346 if (tty_flag)
347 force_tty_flag = 1;
Damien Miller95def091999-11-25 00:26:21 +1100348 tty_flag = 1;
349 break;
Damien Miller95def091999-11-25 00:26:21 +1100350 case 'v':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000351 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100352 debug_flag = 1;
353 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000354 } else {
355 if (options.log_level < SYSLOG_LEVEL_DEBUG3)
356 options.log_level++;
Damien Millere4340be2000-09-16 13:29:08 +1100357 break;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000358 }
Darren Tuckere9bf9842004-11-05 20:05:32 +1100359 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100360 case 'V':
Damien Miller0c889cd2004-03-22 09:36:00 +1100361 fprintf(stderr, "%s, %s\n",
Damien Miller2aa6d3c2004-09-12 16:53:04 +1000362 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
Damien Miller95def091999-11-25 00:26:21 +1100363 if (opt == 'V')
364 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100365 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100366 case 'w':
Damien Miller7b58e802005-12-13 19:33:19 +1100367 if (options.tun_open == -1)
368 options.tun_open = SSH_TUNMODE_DEFAULT;
Damien Millerd27b9472005-12-13 19:29:02 +1100369 options.tun_local = a2tun(optarg, &options.tun_remote);
Damien Miller7b58e802005-12-13 19:33:19 +1100370 if (options.tun_local == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100371 fprintf(stderr, "Bad tun device '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100372 exit(255);
Damien Millerd27b9472005-12-13 19:29:02 +1100373 }
374 break;
Damien Miller95def091999-11-25 00:26:21 +1100375 case 'q':
376 options.log_level = SYSLOG_LEVEL_QUIET;
377 break;
Damien Miller95def091999-11-25 00:26:21 +1100378 case 'e':
379 if (optarg[0] == '^' && optarg[2] == 0 &&
Damien Millerf4614452001-07-14 12:18:10 +1000380 (u_char) optarg[1] >= 64 &&
381 (u_char) optarg[1] < 128)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000382 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100383 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000384 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100385 else if (strcmp(optarg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000386 options.escape_char = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100387 else {
Damien Millerf4614452001-07-14 12:18:10 +1000388 fprintf(stderr, "Bad escape character '%s'.\n",
389 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100390 exit(255);
Damien Miller95def091999-11-25 00:26:21 +1100391 }
392 break;
Damien Miller95def091999-11-25 00:26:21 +1100393 case 'c':
Damien Millereba71ba2000-04-29 23:57:08 +1000394 if (ciphers_valid(optarg)) {
395 /* SSH2 only */
396 options.ciphers = xstrdup(optarg);
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000397 options.cipher = SSH_CIPHER_INVALID;
Damien Millereba71ba2000-04-29 23:57:08 +1000398 } else {
399 /* SSH1 only */
Damien Millere39cacc2000-11-29 12:18:44 +1100400 options.cipher = cipher_number(optarg);
401 if (options.cipher == -1) {
Damien Millerf4614452001-07-14 12:18:10 +1000402 fprintf(stderr,
403 "Unknown cipher type '%s'\n",
404 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100405 exit(255);
Damien Millereba71ba2000-04-29 23:57:08 +1000406 }
Damien Millerf4614452001-07-14 12:18:10 +1000407 if (options.cipher == SSH_CIPHER_3DES)
Damien Millere39cacc2000-11-29 12:18:44 +1100408 options.ciphers = "3des-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000409 else if (options.cipher == SSH_CIPHER_BLOWFISH)
Damien Millere39cacc2000-11-29 12:18:44 +1100410 options.ciphers = "blowfish-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000411 else
Damien Millere39cacc2000-11-29 12:18:44 +1100412 options.ciphers = (char *)-1;
Damien Miller95def091999-11-25 00:26:21 +1100413 }
414 break;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000415 case 'm':
416 if (mac_valid(optarg))
417 options.macs = xstrdup(optarg);
418 else {
Damien Millerf4614452001-07-14 12:18:10 +1000419 fprintf(stderr, "Unknown mac type '%s'\n",
420 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100421 exit(255);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000422 }
423 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000424 case 'M':
Damien Millerd14b1e72005-06-16 13:19:41 +1000425 if (options.control_master == SSHCTL_MASTER_YES)
426 options.control_master = SSHCTL_MASTER_ASK;
427 else
428 options.control_master = SSHCTL_MASTER_YES;
Damien Miller0e220db2004-06-15 10:34:08 +1000429 break;
Damien Miller95def091999-11-25 00:26:21 +1100430 case 'p':
Ben Lindstrom19066a12001-04-12 23:39:26 +0000431 options.port = a2port(optarg);
432 if (options.port == 0) {
Ben Lindstrom146edb92001-04-11 23:06:28 +0000433 fprintf(stderr, "Bad port '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100434 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000435 }
Damien Miller95def091999-11-25 00:26:21 +1100436 break;
Damien Miller95def091999-11-25 00:26:21 +1100437 case 'l':
438 options.user = optarg;
439 break;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000440
Damien Miller95def091999-11-25 00:26:21 +1100441 case 'L':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100442 if (parse_forward(&fwd, optarg))
443 add_local_forward(&options, &fwd);
444 else {
Damien Millerf4614452001-07-14 12:18:10 +1000445 fprintf(stderr,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100446 "Bad local forwarding specification '%s'\n",
Damien Millerf4614452001-07-14 12:18:10 +1000447 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100448 exit(255);
Ben Lindstrom1a174712001-09-12 17:56:15 +0000449 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100450 break;
451
452 case 'R':
453 if (parse_forward(&fwd, optarg)) {
454 add_remote_forward(&options, &fwd);
455 } else {
456 fprintf(stderr,
457 "Bad remote forwarding specification "
458 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100459 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100460 }
Damien Miller95def091999-11-25 00:26:21 +1100461 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000462
463 case 'D':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100464 cp = p = xstrdup(optarg);
465 memset(&fwd, '\0', sizeof(fwd));
466 fwd.connect_host = "socks";
467 if ((fwd.listen_host = hpdelim(&cp)) == NULL) {
468 fprintf(stderr, "Bad dynamic forwarding "
469 "specification '%.100s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100470 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100471 }
472 if (cp != NULL) {
473 fwd.listen_port = a2port(cp);
474 fwd.listen_host = cleanhostname(fwd.listen_host);
475 } else {
476 fwd.listen_port = a2port(fwd.listen_host);
Damien Millerbe1045d2005-08-12 22:10:56 +1000477 fwd.listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100478 }
479
480 if (fwd.listen_port == 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000481 fprintf(stderr, "Bad dynamic port '%s'\n",
482 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100483 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000484 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100485 add_local_forward(&options, &fwd);
486 xfree(p);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000487 break;
488
Damien Miller95def091999-11-25 00:26:21 +1100489 case 'C':
490 options.compression = 1;
491 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000492 case 'N':
493 no_shell_flag = 1;
494 no_tty_flag = 1;
495 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000496 case 'T':
497 no_tty_flag = 1;
498 break;
Damien Miller95def091999-11-25 00:26:21 +1100499 case 'o':
500 dummy = 1;
Damien Miller9836cf82003-12-17 16:30:06 +1100501 line = xstrdup(optarg);
Damien Millerf4614452001-07-14 12:18:10 +1000502 if (process_config_line(&options, host ? host : "",
Damien Miller9836cf82003-12-17 16:30:06 +1100503 line, "command-line", 0, &dummy) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100504 exit(255);
Damien Miller9836cf82003-12-17 16:30:06 +1100505 xfree(line);
Damien Miller95def091999-11-25 00:26:21 +1100506 break;
Damien Miller832562e2001-01-30 09:30:01 +1100507 case 's':
508 subsystem_flag = 1;
509 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000510 case 'S':
511 if (options.control_path != NULL)
512 free(options.control_path);
513 options.control_path = xstrdup(optarg);
Damien Miller0e220db2004-06-15 10:34:08 +1000514 break;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000515 case 'b':
516 options.bind_address = optarg;
517 break;
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000518 case 'F':
519 config = optarg;
520 break;
Damien Miller95def091999-11-25 00:26:21 +1100521 default:
522 usage();
523 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000524 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000525
Damien Millerf4614452001-07-14 12:18:10 +1000526 ac -= optind;
527 av += optind;
528
529 if (ac > 0 && !host && **av != '-') {
Ben Lindstromc276c122002-12-23 02:14:51 +0000530 if (strrchr(*av, '@')) {
Damien Millerf4614452001-07-14 12:18:10 +1000531 p = xstrdup(*av);
Ben Lindstromc276c122002-12-23 02:14:51 +0000532 cp = strrchr(p, '@');
Damien Millerf4614452001-07-14 12:18:10 +1000533 if (cp == NULL || cp == p)
534 usage();
535 options.user = p;
536 *cp = '\0';
537 host = ++cp;
538 } else
539 host = *av;
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000540 if (ac > 1) {
541 optind = optreset = 1;
Damien Millerf4614452001-07-14 12:18:10 +1000542 goto again;
543 }
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000544 ac--, av++;
Damien Millerf4614452001-07-14 12:18:10 +1000545 }
546
Damien Miller95def091999-11-25 00:26:21 +1100547 /* Check that we got a host name. */
548 if (!host)
549 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000550
Damien Millercb5e44a2000-09-29 12:12:36 +1100551 SSLeay_add_all_algorithms();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100552 ERR_load_crypto_strings();
Damien Millercb5e44a2000-09-29 12:12:36 +1100553
Damien Miller95def091999-11-25 00:26:21 +1100554 /* Initialize the command to execute on remote host. */
555 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000556
Damien Miller5428f641999-11-25 11:54:57 +1100557 /*
558 * Save the command to execute on the remote host in a buffer. There
559 * is no limit on the length of the command, except by the maximum
560 * packet size. Also sets the tty flag if there is no command.
561 */
Damien Millerf4614452001-07-14 12:18:10 +1000562 if (!ac) {
Damien Miller95def091999-11-25 00:26:21 +1100563 /* No command specified - execute shell on a tty. */
564 tty_flag = 1;
Damien Miller832562e2001-01-30 09:30:01 +1100565 if (subsystem_flag) {
Damien Millerf4614452001-07-14 12:18:10 +1000566 fprintf(stderr,
567 "You must specify a subsystem to invoke.\n");
Damien Miller832562e2001-01-30 09:30:01 +1100568 usage();
569 }
Damien Miller95def091999-11-25 00:26:21 +1100570 } else {
Damien Millerf4614452001-07-14 12:18:10 +1000571 /* A command has been specified. Store it into the buffer. */
572 for (i = 0; i < ac; i++) {
573 if (i)
Damien Miller95def091999-11-25 00:26:21 +1100574 buffer_append(&command, " ", 1);
575 buffer_append(&command, av[i], strlen(av[i]));
576 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000577 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000578
Damien Miller95def091999-11-25 00:26:21 +1100579 /* Cannot fork to background if no command. */
Damien Millercaf6dd62000-08-29 11:33:50 +1100580 if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
Damien Miller95def091999-11-25 00:26:21 +1100581 fatal("Cannot fork into background without a command to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000582
Damien Miller95def091999-11-25 00:26:21 +1100583 /* Allocate a tty by default if no command specified. */
584 if (buffer_len(&command) == 0)
585 tty_flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000586
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000587 /* Force no tty */
Ben Lindstromc72745a2000-12-02 19:03:54 +0000588 if (no_tty_flag)
589 tty_flag = 0;
Damien Miller95def091999-11-25 00:26:21 +1100590 /* Do not allocate a tty if stdin is not a tty. */
Damien Millerddee5752005-05-26 12:05:05 +1000591 if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100592 if (tty_flag)
Damien Miller996acd22003-04-09 20:59:48 +1000593 logit("Pseudo-terminal will not be allocated because stdin is not a terminal.");
Damien Miller95def091999-11-25 00:26:21 +1100594 tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000595 }
Damien Miller1383bd82000-04-06 12:32:37 +1000596
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000597 /*
598 * Initialize "log" output. Since we are the client all output
599 * actually goes to stderr.
600 */
Ben Lindstrom2b646522001-04-12 16:16:57 +0000601 log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
602 SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000603
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000604 /*
605 * Read per-user configuration file. Ignore the system wide config
606 * file if the user specifies a config file on the command line.
607 */
608 if (config != NULL) {
Damien Miller914420f2004-04-20 20:14:07 +1000609 if (!read_config_file(config, host, &options, 0))
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000610 fatal("Can't open user config file %.100s: "
611 "%.100s", config, strerror(errno));
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000612 } else {
613 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
614 _PATH_SSH_USER_CONFFILE);
Damien Miller57a44762004-04-20 20:11:57 +1000615 (void)read_config_file(buf, host, &options, 1);
Ben Lindstrom83f07d12001-10-03 17:22:29 +0000616
617 /* Read systemwide configuration file after use config. */
Darren Tuckerfc959702004-07-17 16:12:08 +1000618 (void)read_config_file(_PATH_HOST_CONFIG_FILE, host,
Damien Miller57a44762004-04-20 20:11:57 +1000619 &options, 0);
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000620 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000621
Damien Miller95def091999-11-25 00:26:21 +1100622 /* Fill configuration defaults. */
623 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000624
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000625 channel_set_af(options.address_family);
626
Damien Miller95def091999-11-25 00:26:21 +1100627 /* reinit */
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000628 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000629
Damien Miller60bc5172001-03-19 09:38:15 +1100630 seed_rng();
631
Damien Miller95def091999-11-25 00:26:21 +1100632 if (options.user == NULL)
633 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000634
Damien Miller95def091999-11-25 00:26:21 +1100635 if (options.hostname != NULL)
636 host = options.hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000637
Darren Tucker3f521e22003-07-03 16:20:42 +1000638 /* force lowercase for hostkey matching */
639 if (options.host_key_alias != NULL) {
640 for (p = options.host_key_alias; *p; p++)
641 if (isupper(*p))
Damien Miller1d2b6702006-03-26 14:09:54 +1100642 *p = (char)tolower(*p);
Darren Tucker3f521e22003-07-03 16:20:42 +1000643 }
644
Damien Miller7c71cc72005-06-26 08:56:31 +1000645 /* Get default port if port has not been set. */
646 if (options.port == 0) {
647 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
648 options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
649 }
650
Damien Miller9f1e33a2003-02-24 11:57:32 +1100651 if (options.proxy_command != NULL &&
652 strcmp(options.proxy_command, "none") == 0)
653 options.proxy_command = NULL;
Damien Miller8f74c8f2005-06-26 08:56:03 +1000654 if (options.control_path != NULL &&
655 strcmp(options.control_path, "none") == 0)
656 options.control_path = NULL;
Damien Miller9f1e33a2003-02-24 11:57:32 +1100657
Damien Miller0e220db2004-06-15 10:34:08 +1000658 if (options.control_path != NULL) {
Damien Miller6b1d53c2006-03-31 23:13:21 +1100659 char thishost[NI_MAXHOST];
Damien Miller3ec54c72006-03-15 11:30:13 +1100660
Damien Miller6b1d53c2006-03-31 23:13:21 +1100661 if (gethostname(thishost, sizeof(thishost)) == -1)
Damien Miller3ec54c72006-03-15 11:30:13 +1100662 fatal("gethostname: %s", strerror(errno));
Damien Miller6476cad2005-06-16 13:18:34 +1000663 snprintf(buf, sizeof(buf), "%d", options.port);
664 cp = tilde_expand_filename(options.control_path,
665 original_real_uid);
666 options.control_path = percent_expand(cp, "p", buf, "h", host,
Damien Miller6b1d53c2006-03-31 23:13:21 +1100667 "r", options.user, "l", thishost, (char *)NULL);
Damien Miller6476cad2005-06-16 13:18:34 +1000668 xfree(cp);
Damien Miller0e220db2004-06-15 10:34:08 +1000669 }
Darren Tucker0814d312005-06-01 23:08:51 +1000670 if (mux_command != 0 && options.control_path == NULL)
671 fatal("No ControlPath specified for \"-O\" command");
Damien Millerd14b1e72005-06-16 13:19:41 +1000672 if (options.control_path != NULL)
Damien Millerdadfd4d2005-05-26 12:07:13 +1000673 control_client(options.control_path);
Damien Miller0e220db2004-06-15 10:34:08 +1000674
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000675 /* Open a connection to the remote host. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000676 if (ssh_connect(host, &hostaddr, options.port,
677 options.address_family, options.connection_attempts,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000678#ifdef HAVE_CYGWIN
679 options.use_privileged_port,
680#else
Ben Lindstromf9c48842002-06-11 16:37:51 +0000681 original_effective_uid == 0 && options.use_privileged_port,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000682#endif
Ben Lindstromda394ca2002-06-12 16:11:12 +0000683 options.proxy_command) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100684 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000685
Damien Miller5428f641999-11-25 11:54:57 +1100686 /*
687 * If we successfully made the connection, load the host private key
688 * in case we will need it later for combined rsa-rhosts
689 * authentication. This must be done before releasing extra
690 * privileges, because the file is only readable by root.
Ben Lindstrom9e5bb572002-06-06 19:58:27 +0000691 * If we cannot access the private keys, load the public keys
692 * instead and try to execute the ssh-keysign helper instead.
Damien Miller5428f641999-11-25 11:54:57 +1100693 */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000694 sensitive_data.nkeys = 0;
695 sensitive_data.keys = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000696 sensitive_data.external_keysign = 0;
Ben Lindstrom24157572002-06-12 16:09:39 +0000697 if (options.rhosts_rsa_authentication ||
698 options.hostbased_authentication) {
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000699 sensitive_data.nkeys = 3;
Damien Millerddd63ab2006-03-31 23:10:51 +1100700 sensitive_data.keys = xcalloc(sensitive_data.nkeys,
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000701 sizeof(Key));
Ben Lindstromf9c48842002-06-11 16:37:51 +0000702
703 PRIV_START;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000704 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
Darren Tucker232b76f2006-05-06 17:41:51 +1000705 _PATH_HOST_KEY_FILE, "", NULL, NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000706 sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
Darren Tucker232b76f2006-05-06 17:41:51 +1000707 _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000708 sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
Darren Tucker232b76f2006-05-06 17:41:51 +1000709 _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL);
Ben Lindstromf9c48842002-06-11 16:37:51 +0000710 PRIV_END;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000711
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +0000712 if (options.hostbased_authentication == 1 &&
713 sensitive_data.keys[0] == NULL &&
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000714 sensitive_data.keys[1] == NULL &&
715 sensitive_data.keys[2] == NULL) {
716 sensitive_data.keys[1] = key_load_public(
717 _PATH_HOST_DSA_KEY_FILE, NULL);
718 sensitive_data.keys[2] = key_load_public(
719 _PATH_HOST_RSA_KEY_FILE, NULL);
720 sensitive_data.external_keysign = 1;
721 }
Damien Miller95def091999-11-25 00:26:21 +1100722 }
Damien Miller5428f641999-11-25 11:54:57 +1100723 /*
724 * Get rid of any extra privileges that we may have. We will no
725 * longer need them. Also, extra privileges could make it very hard
726 * to read identity files and other non-world-readable files from the
727 * user's home directory if it happens to be on a NFS volume where
728 * root is mapped to nobody.
729 */
Darren Tucker25f60a72004-08-15 17:23:34 +1000730 if (original_effective_uid == 0) {
731 PRIV_START;
732 permanently_set_uid(pw);
733 }
Damien Miller95def091999-11-25 00:26:21 +1100734
Damien Miller5428f641999-11-25 11:54:57 +1100735 /*
736 * Now that we are back to our own permissions, create ~/.ssh
Damien Miller788f2122005-11-05 15:14:59 +1100737 * directory if it doesn't already exist.
Damien Miller5428f641999-11-25 11:54:57 +1100738 */
Ben Lindstrom930b14a2001-08-15 23:19:21 +0000739 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 +1100740 if (stat(buf, &st) < 0)
Damien Millerbe484b52000-07-15 14:14:16 +1000741 if (mkdir(buf, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100742 error("Could not create directory '%.200s'.", buf);
743
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000744 /* load options.identity_files */
745 load_public_identity_files();
746
747 /* Expand ~ in known host file names. */
748 /* XXX mem-leaks: */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100749 options.system_hostfile =
750 tilde_expand_filename(options.system_hostfile, original_real_uid);
751 options.user_hostfile =
752 tilde_expand_filename(options.user_hostfile, original_real_uid);
753 options.system_hostfile2 =
754 tilde_expand_filename(options.system_hostfile2, original_real_uid);
755 options.user_hostfile2 =
756 tilde_expand_filename(options.user_hostfile2, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100757
Damien Miller07cd5892001-11-12 10:52:03 +1100758 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
759
Damien Miller95def091999-11-25 00:26:21 +1100760 /* Log into the remote system. This never returns if the login fails. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000761 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, pw);
Damien Miller95def091999-11-25 00:26:21 +1100762
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000763 /* We no longer need the private host keys. Clear them now. */
764 if (sensitive_data.nkeys != 0) {
765 for (i = 0; i < sensitive_data.nkeys; i++) {
766 if (sensitive_data.keys[i] != NULL) {
767 /* Destroys contents safely */
768 debug3("clear hostkey %d", i);
769 key_free(sensitive_data.keys[i]);
770 sensitive_data.keys[i] = NULL;
771 }
772 }
773 xfree(sensitive_data.keys);
774 }
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +0000775 for (i = 0; i < options.num_identity_files; i++) {
776 if (options.identity_files[i]) {
777 xfree(options.identity_files[i]);
778 options.identity_files[i] = NULL;
779 }
780 if (options.identity_keys[i]) {
781 key_free(options.identity_keys[i]);
782 options.identity_keys[i] = NULL;
783 }
784 }
Damien Miller95def091999-11-25 00:26:21 +1100785
Damien Miller1383bd82000-04-06 12:32:37 +1000786 exit_status = compat20 ? ssh_session2() : ssh_session();
787 packet_close();
Damien Miller8c4e18a2002-09-19 12:05:02 +1000788
Damien Miller0e220db2004-06-15 10:34:08 +1000789 if (options.control_path != NULL && control_fd != -1)
790 unlink(options.control_path);
791
Damien Miller8c4e18a2002-09-19 12:05:02 +1000792 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100793 * Send SIGHUP to proxy command if used. We don't wait() in
Damien Miller8c4e18a2002-09-19 12:05:02 +1000794 * case it hangs and instead rely on init to reap the child
795 */
796 if (proxy_command_pid > 1)
797 kill(proxy_command_pid, SIGHUP);
798
Damien Miller1383bd82000-04-06 12:32:37 +1000799 return exit_status;
800}
801
Ben Lindstrombba81212001-06-25 05:01:22 +0000802static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100803ssh_init_forwarding(void)
804{
Kevin Steves12057502001-02-05 14:54:34 +0000805 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100806 int i;
Kevin Steves12057502001-02-05 14:54:34 +0000807
Damien Miller0bc1bd82000-11-13 22:57:25 +1100808 /* Initiate local TCP/IP port forwardings. */
809 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100810 debug("Local connections to %.200s:%d forwarded to remote "
811 "address %.200s:%d",
Darren Tucker47eede72005-03-14 23:08:12 +1100812 (options.local_forwards[i].listen_host == NULL) ?
813 (options.gateway_ports ? "*" : "LOCALHOST") :
Damien Millerf91ee4c2005-03-01 21:24:33 +1100814 options.local_forwards[i].listen_host,
815 options.local_forwards[i].listen_port,
816 options.local_forwards[i].connect_host,
817 options.local_forwards[i].connect_port);
Damien Millerb16461c2002-01-22 23:29:22 +1100818 success += channel_setup_local_fwd_listener(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100819 options.local_forwards[i].listen_host,
820 options.local_forwards[i].listen_port,
821 options.local_forwards[i].connect_host,
822 options.local_forwards[i].connect_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100823 options.gateway_ports);
824 }
Darren Tuckere7d4b192006-07-12 22:17:10 +1000825 if (i > 0 && success != i && options.exit_on_forward_failure)
826 fatal("Could not request local forwarding.");
Kevin Steves12057502001-02-05 14:54:34 +0000827 if (i > 0 && success == 0)
828 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100829
830 /* Initiate remote TCP/IP port forwardings. */
831 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100832 debug("Remote connections from %.200s:%d forwarded to "
833 "local address %.200s:%d",
Damien Miller46d38de2005-07-17 17:02:09 +1000834 (options.remote_forwards[i].listen_host == NULL) ?
Damien Milleraa3bb102005-11-05 15:12:59 +1100835 "LOCALHOST" : options.remote_forwards[i].listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100836 options.remote_forwards[i].listen_port,
837 options.remote_forwards[i].connect_host,
838 options.remote_forwards[i].connect_port);
Darren Tuckere7d4b192006-07-12 22:17:10 +1000839 if (channel_request_remote_forwarding(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100840 options.remote_forwards[i].listen_host,
841 options.remote_forwards[i].listen_port,
842 options.remote_forwards[i].connect_host,
Darren Tuckere7d4b192006-07-12 22:17:10 +1000843 options.remote_forwards[i].connect_port) < 0) {
844 if (options.exit_on_forward_failure)
845 fatal("Could not request remote forwarding.");
846 else
847 logit("Warning: Could not request remote "
848 "forwarding.");
849 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100850 }
851}
852
Ben Lindstrombba81212001-06-25 05:01:22 +0000853static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100854check_agent_present(void)
855{
856 if (options.forward_agent) {
Damien Miller788f2122005-11-05 15:14:59 +1100857 /* Clear agent forwarding if we don't have an agent. */
Damien Miller789e95d2002-09-12 09:52:46 +1000858 if (!ssh_agent_present())
Damien Miller0bc1bd82000-11-13 22:57:25 +1100859 options.forward_agent = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100860 }
861}
862
Ben Lindstrombba81212001-06-25 05:01:22 +0000863static int
Damien Miller1383bd82000-04-06 12:32:37 +1000864ssh_session(void)
865{
866 int type;
Damien Miller1383bd82000-04-06 12:32:37 +1000867 int interactive = 0;
868 int have_tty = 0;
869 struct winsize ws;
Damien Miller1383bd82000-04-06 12:32:37 +1000870 char *cp;
Damien Miller17e7ed02005-06-17 12:54:33 +1000871 const char *display;
Damien Miller1383bd82000-04-06 12:32:37 +1000872
Damien Miller95def091999-11-25 00:26:21 +1100873 /* Enable compression if requested. */
874 if (options.compression) {
875 debug("Requesting compression at level %d.", options.compression_level);
876
877 if (options.compression_level < 1 || options.compression_level > 9)
878 fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
879
880 /* Send the request. */
881 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
882 packet_put_int(options.compression_level);
883 packet_send();
884 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100885 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100886 if (type == SSH_SMSG_SUCCESS)
887 packet_start_compression(options.compression_level);
888 else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000889 logit("Warning: Remote host refused compression.");
Damien Miller95def091999-11-25 00:26:21 +1100890 else
891 packet_disconnect("Protocol error waiting for compression response.");
892 }
893 /* Allocate a pseudo tty if appropriate. */
894 if (tty_flag) {
895 debug("Requesting pty.");
896
897 /* Start the packet. */
898 packet_start(SSH_CMSG_REQUEST_PTY);
899
900 /* Store TERM in the packet. There is no limit on the
901 length of the string. */
902 cp = getenv("TERM");
903 if (!cp)
904 cp = "";
Ben Lindstrom664408d2001-06-09 01:42:01 +0000905 packet_put_cstring(cp);
Damien Miller95def091999-11-25 00:26:21 +1100906
907 /* Store window size in the packet. */
908 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
909 memset(&ws, 0, sizeof(ws));
Damien Miller71a73672006-03-26 14:04:36 +1100910 packet_put_int((u_int)ws.ws_row);
911 packet_put_int((u_int)ws.ws_col);
912 packet_put_int((u_int)ws.ws_xpixel);
913 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller95def091999-11-25 00:26:21 +1100914
915 /* Store tty modes in the packet. */
Ben Lindstromae8e2d32001-04-14 23:13:02 +0000916 tty_make_modes(fileno(stdin), NULL);
Damien Miller95def091999-11-25 00:26:21 +1100917
918 /* Send the packet, and wait for it to leave. */
919 packet_send();
920 packet_write_wait();
921
922 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100923 type = packet_read();
Damien Miller450a7a12000-03-26 13:04:51 +1000924 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100925 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +1000926 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000927 } else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000928 logit("Warning: Remote host failed or refused to allocate a pseudo tty.");
Damien Miller95def091999-11-25 00:26:21 +1100929 else
930 packet_disconnect("Protocol error waiting for pty request response.");
931 }
932 /* Request X11 forwarding if enabled and DISPLAY is set. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000933 display = getenv("DISPLAY");
934 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000935 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +1000936 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000937 client_x11_get_proto(display, options.xauth_location,
938 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +1000939 /* Request forwarding with authentication spoofing. */
Damien Miller95def091999-11-25 00:26:21 +1100940 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +1000941 x11_request_forwarding_with_spoofing(0, display, proto, data);
Damien Miller95def091999-11-25 00:26:21 +1100942
943 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100944 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100945 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100946 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +1000947 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller996acd22003-04-09 20:59:48 +1000948 logit("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +1000949 } else {
Damien Miller95def091999-11-25 00:26:21 +1100950 packet_disconnect("Protocol error waiting for X11 forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +1000951 }
Damien Miller95def091999-11-25 00:26:21 +1100952 }
953 /* Tell the packet module whether this is an interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000954 packet_set_interactive(interactive);
Damien Miller95def091999-11-25 00:26:21 +1100955
956 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100957 check_agent_present();
958
Damien Miller95def091999-11-25 00:26:21 +1100959 if (options.forward_agent) {
960 debug("Requesting authentication agent forwarding.");
961 auth_request_forwarding();
962
963 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100964 type = packet_read();
Damien Miller48b03fc2002-01-22 23:11:40 +1100965 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100966 if (type != SSH_SMSG_SUCCESS)
Damien Miller996acd22003-04-09 20:59:48 +1000967 logit("Warning: Remote host denied authentication agent forwarding.");
Damien Miller95def091999-11-25 00:26:21 +1100968 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000969
Damien Miller0bc1bd82000-11-13 22:57:25 +1100970 /* Initiate port forwardings. */
971 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000972
Damien Miller5428f641999-11-25 11:54:57 +1100973 /* If requested, let ssh continue in the background. */
Damien Miller4af51302000-04-16 11:18:38 +1000974 if (fork_after_authentication_flag)
Damien Miller5428f641999-11-25 11:54:57 +1100975 if (daemon(1, 1) < 0)
976 fatal("daemon() failed: %.200s", strerror(errno));
977
978 /*
979 * If a command was specified on the command line, execute the
980 * command now. Otherwise request the server to start a shell.
981 */
Damien Miller95def091999-11-25 00:26:21 +1100982 if (buffer_len(&command) > 0) {
983 int len = buffer_len(&command);
984 if (len > 900)
985 len = 900;
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100986 debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
Damien Miller95def091999-11-25 00:26:21 +1100987 packet_start(SSH_CMSG_EXEC_CMD);
988 packet_put_string(buffer_ptr(&command), buffer_len(&command));
989 packet_send();
990 packet_write_wait();
991 } else {
992 debug("Requesting shell.");
993 packet_start(SSH_CMSG_EXEC_SHELL);
994 packet_send();
995 packet_write_wait();
996 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000997
Damien Miller95def091999-11-25 00:26:21 +1100998 /* Enter the interactive session. */
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000999 return client_loop(have_tty, tty_flag ?
1000 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
Damien Miller1383bd82000-04-06 12:32:37 +10001001}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001002
Ben Lindstrombba81212001-06-25 05:01:22 +00001003static void
Damien Miller0e220db2004-06-15 10:34:08 +10001004ssh_subsystem_reply(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001005{
1006 int id, len;
1007
1008 id = packet_get_int();
1009 len = buffer_len(&command);
Ben Lindstrom4040fe12001-03-05 06:52:57 +00001010 if (len > 900)
1011 len = 900;
Damien Miller48b03fc2002-01-22 23:11:40 +11001012 packet_check_eom();
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001013 if (type == SSH2_MSG_CHANNEL_FAILURE)
1014 fatal("Request for subsystem '%.*s' failed on channel %d",
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001015 len, (u_char *)buffer_ptr(&command), id);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001016}
1017
Damien Miller2797f7f2002-04-23 21:09:44 +10001018void
Damien Miller509b0102003-12-17 16:33:10 +11001019client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt)
Damien Miller2797f7f2002-04-23 21:09:44 +10001020{
1021 int i;
1022
1023 i = client_global_request_id++;
Damien Miller509b0102003-12-17 16:33:10 +11001024 if (i >= options.num_remote_forwards)
Damien Miller2797f7f2002-04-23 21:09:44 +10001025 return;
Damien Miller2797f7f2002-04-23 21:09:44 +10001026 debug("remote forward %s for: listen %d, connect %s:%d",
1027 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
Damien Millerf91ee4c2005-03-01 21:24:33 +11001028 options.remote_forwards[i].listen_port,
1029 options.remote_forwards[i].connect_host,
1030 options.remote_forwards[i].connect_port);
Darren Tuckere7d4b192006-07-12 22:17:10 +10001031 if (type == SSH2_MSG_REQUEST_FAILURE) {
1032 if (options.exit_on_forward_failure)
1033 fatal("Error: remote port forwarding failed for "
1034 "listen port %d",
1035 options.remote_forwards[i].listen_port);
1036 else
1037 logit("Warning: remote port forwarding failed for "
1038 "listen port %d",
1039 options.remote_forwards[i].listen_port);
1040 }
Damien Miller2797f7f2002-04-23 21:09:44 +10001041}
1042
Damien Miller0e220db2004-06-15 10:34:08 +10001043static void
1044ssh_control_listener(void)
1045{
1046 struct sockaddr_un addr;
1047 mode_t old_umask;
Damien Miller07b6ff12004-06-15 11:14:45 +10001048 int addr_len;
1049
Damien Millerd14b1e72005-06-16 13:19:41 +10001050 if (options.control_path == NULL ||
1051 options.control_master == SSHCTL_MASTER_NO)
Damien Miller0e220db2004-06-15 10:34:08 +10001052 return;
1053
Damien Millerd14b1e72005-06-16 13:19:41 +10001054 debug("setting up multiplex master socket");
1055
Damien Miller0e220db2004-06-15 10:34:08 +10001056 memset(&addr, '\0', sizeof(addr));
1057 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001058 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001059 strlen(options.control_path) + 1;
1060
1061 if (strlcpy(addr.sun_path, options.control_path,
1062 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1063 fatal("ControlPath too long");
1064
1065 if ((control_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001066 fatal("%s socket(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001067
1068 old_umask = umask(0177);
Damien Miller90967402006-03-26 14:07:26 +11001069 if (bind(control_fd, (struct sockaddr *)&addr, addr_len) == -1) {
Damien Miller0e220db2004-06-15 10:34:08 +10001070 control_fd = -1;
Damien Miller4f10e252005-05-04 15:33:09 +10001071 if (errno == EINVAL || errno == EADDRINUSE)
Damien Miller0e220db2004-06-15 10:34:08 +10001072 fatal("ControlSocket %s already exists",
1073 options.control_path);
1074 else
Damien Miller15d72a02005-11-05 15:07:33 +11001075 fatal("%s bind(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001076 }
1077 umask(old_umask);
1078
1079 if (listen(control_fd, 64) == -1)
Damien Miller15d72a02005-11-05 15:07:33 +11001080 fatal("%s listen(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001081
1082 set_nonblock(control_fd);
1083}
1084
Ben Lindstromf558cf62001-09-20 23:13:49 +00001085/* request pty/x11/agent/tcpfwd/shell for channel */
Ben Lindstrombba81212001-06-25 05:01:22 +00001086static void
Ben Lindstromf558cf62001-09-20 23:13:49 +00001087ssh_session2_setup(int id, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +10001088{
Damien Miller3756dce2004-06-18 01:17:29 +10001089 extern char **environ;
Damien Miller17e7ed02005-06-17 12:54:33 +10001090 const char *display;
Damien Miller0e220db2004-06-15 10:34:08 +10001091 int interactive = tty_flag;
Damien Miller17e7ed02005-06-17 12:54:33 +10001092
Damien Miller46d38de2005-07-17 17:02:09 +10001093 display = getenv("DISPLAY");
Damien Miller17e7ed02005-06-17 12:54:33 +10001094 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001095 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001096 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001097 client_x11_get_proto(display, options.xauth_location,
1098 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001099 /* Request forwarding with authentication spoofing. */
1100 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +10001101 x11_request_forwarding_with_spoofing(id, display, proto, data);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001102 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001103 /* XXX wait for reply */
1104 }
1105
Damien Miller0bc1bd82000-11-13 22:57:25 +11001106 check_agent_present();
1107 if (options.forward_agent) {
1108 debug("Requesting authentication agent forwarding.");
1109 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1110 packet_send();
1111 }
1112
Damien Miller7b58e802005-12-13 19:33:19 +11001113 if (options.tun_open != SSH_TUNMODE_NO) {
Damien Millerd27b9472005-12-13 19:29:02 +11001114 Channel *c;
1115 int fd;
1116
1117 debug("Requesting tun.");
Damien Miller7b58e802005-12-13 19:33:19 +11001118 if ((fd = tun_open(options.tun_local,
1119 options.tun_open)) >= 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11001120 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1121 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1122 0, "tun", 1);
1123 c->datagram = 1;
Damien Miller598bbc22005-12-31 16:33:36 +11001124#if defined(SSH_TUN_FILTER)
1125 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1126 channel_register_filter(c->self, sys_tun_infilter,
1127 sys_tun_outfilter);
1128#endif
Damien Millerd27b9472005-12-13 19:29:02 +11001129 packet_start(SSH2_MSG_CHANNEL_OPEN);
1130 packet_put_cstring("tun@openssh.com");
1131 packet_put_int(c->self);
1132 packet_put_int(c->local_window_max);
1133 packet_put_int(c->local_maxpacket);
Damien Miller7b58e802005-12-13 19:33:19 +11001134 packet_put_int(options.tun_open);
Damien Millerd27b9472005-12-13 19:29:02 +11001135 packet_put_int(options.tun_remote);
1136 packet_send();
1137 }
1138 }
1139
Damien Miller0e220db2004-06-15 10:34:08 +10001140 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
Damien Miller3756dce2004-06-18 01:17:29 +10001141 NULL, fileno(stdin), &command, environ, &ssh_subsystem_reply);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001142
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001143 packet_set_interactive(interactive);
Damien Miller1383bd82000-04-06 12:32:37 +10001144}
1145
Ben Lindstromf558cf62001-09-20 23:13:49 +00001146/* open new channel for a session */
Ben Lindstrombba81212001-06-25 05:01:22 +00001147static int
Ben Lindstromf558cf62001-09-20 23:13:49 +00001148ssh_session2_open(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001149{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001150 Channel *c;
1151 int window, packetmax, in, out, err;
Damien Millerad833b32000-08-23 10:46:23 +10001152
Damien Millercaf6dd62000-08-29 11:33:50 +11001153 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001154 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001155 } else {
1156 in = dup(STDIN_FILENO);
1157 }
1158 out = dup(STDOUT_FILENO);
1159 err = dup(STDERR_FILENO);
1160
1161 if (in < 0 || out < 0 || err < 0)
1162 fatal("dup() in/out/err failed");
1163
Damien Miller69b69aa2000-10-28 14:19:58 +11001164 /* enable nonblocking unless tty */
1165 if (!isatty(in))
1166 set_nonblock(in);
1167 if (!isatty(out))
1168 set_nonblock(out);
1169 if (!isatty(err))
1170 set_nonblock(err);
1171
Damien Millere4340be2000-09-16 13:29:08 +11001172 window = CHAN_SES_WINDOW_DEFAULT;
1173 packetmax = CHAN_SES_PACKET_DEFAULT;
Damien Miller19a59452002-02-19 15:20:57 +11001174 if (tty_flag) {
1175 window >>= 1;
1176 packetmax >>= 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001177 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001178 c = channel_new(
Damien Miller1383bd82000-04-06 12:32:37 +10001179 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001180 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001181 "client-session", /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001182
Ben Lindstromf558cf62001-09-20 23:13:49 +00001183 debug3("ssh_session2_open: channel_new: %d", c->self);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001184
Ben Lindstrom5ec26452001-06-09 00:18:51 +00001185 channel_send_open(c->self);
Ben Lindstromf558cf62001-09-20 23:13:49 +00001186 if (!no_shell_flag)
Damien Miller0e220db2004-06-15 10:34:08 +10001187 channel_register_confirm(c->self, ssh_session2_setup, NULL);
Damien Miller1383bd82000-04-06 12:32:37 +10001188
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001189 return c->self;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001190}
1191
Ben Lindstrombba81212001-06-25 05:01:22 +00001192static int
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001193ssh_session2(void)
1194{
Ben Lindstromf558cf62001-09-20 23:13:49 +00001195 int id = -1;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001196
1197 /* XXX should be pre-session */
1198 ssh_init_forwarding();
Damien Miller0e220db2004-06-15 10:34:08 +10001199 ssh_control_listener();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001200
Ben Lindstromf558cf62001-09-20 23:13:49 +00001201 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1202 id = ssh_session2_open();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001203
Damien Millerd27b9472005-12-13 19:29:02 +11001204 /* Execute a local command */
1205 if (options.local_command != NULL &&
1206 options.permit_local_command)
1207 ssh_local_cmd(options.local_command);
1208
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001209 /* If requested, let ssh continue in the background. */
1210 if (fork_after_authentication_flag)
1211 if (daemon(1, 1) < 0)
1212 fatal("daemon() failed: %.200s", strerror(errno));
1213
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001214 return client_loop(tty_flag, tty_flag ?
1215 options.escape_char : SSH_ESCAPECHAR_NONE, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001216}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001217
Ben Lindstrombba81212001-06-25 05:01:22 +00001218static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001219load_public_identity_files(void)
1220{
Damien Miller6b1d53c2006-03-31 23:13:21 +11001221 char *filename, *cp, thishost[NI_MAXHOST];
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001222 int i = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001223 Key *public;
Damien Miller6b1d53c2006-03-31 23:13:21 +11001224 struct passwd *pw;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001225#ifdef SMARTCARD
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001226 Key **keys;
1227
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001228 if (options.smartcard_device != NULL &&
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001229 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1230 (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL ) {
1231 int count = 0;
1232 for (i = 0; keys[i] != NULL; i++) {
1233 count++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001234 memmove(&options.identity_files[1], &options.identity_files[0],
1235 sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
1236 memmove(&options.identity_keys[1], &options.identity_keys[0],
1237 sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1238 options.num_identity_files++;
1239 options.identity_keys[0] = keys[i];
Damien Miller56a0bb02003-06-18 20:28:40 +10001240 options.identity_files[0] = sc_get_key_label(keys[i]);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001241 }
Ben Lindstrom4f054602002-03-26 03:23:00 +00001242 if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1243 options.num_identity_files = SSH_MAX_IDENTITY_FILES;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001244 i = count;
1245 xfree(keys);
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001246 }
Ben Lindstromffce1472001-08-06 21:57:31 +00001247#endif /* SMARTCARD */
Damien Miller6b1d53c2006-03-31 23:13:21 +11001248 if ((pw = getpwuid(original_real_uid)) == NULL)
1249 fatal("load_public_identity_files: getpwuid failed");
1250 if (gethostname(thishost, sizeof(thishost)) == -1)
1251 fatal("load_public_identity_files: gethostname: %s",
1252 strerror(errno));
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001253 for (; i < options.num_identity_files; i++) {
Damien Miller6b1d53c2006-03-31 23:13:21 +11001254 cp = tilde_expand_filename(options.identity_files[i],
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001255 original_real_uid);
Damien Miller6b1d53c2006-03-31 23:13:21 +11001256 filename = percent_expand(cp, "d", pw->pw_dir,
1257 "u", pw->pw_name, "l", thishost, "h", host,
1258 "r", options.user, (char *)NULL);
1259 xfree(cp);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001260 public = key_load_public(filename, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001261 debug("identity file %s type %d", filename,
1262 public ? public->type : -1);
1263 xfree(options.identity_files[i]);
1264 options.identity_files[i] = filename;
1265 options.identity_keys[i] = public;
1266 }
1267}
Damien Miller0e220db2004-06-15 10:34:08 +10001268
1269static void
1270control_client_sighandler(int signo)
1271{
1272 control_client_terminate = signo;
1273}
1274
1275static void
1276control_client_sigrelay(int signo)
1277{
1278 if (control_server_pid > 1)
1279 kill(control_server_pid, signo);
1280}
1281
Darren Tucker365433f2004-06-22 12:29:23 +10001282static int
1283env_permitted(char *env)
1284{
Darren Tucker284706a2006-07-12 22:16:23 +10001285 int i, ret;
Darren Tucker365433f2004-06-22 12:29:23 +10001286 char name[1024], *cp;
1287
Darren Tucker284706a2006-07-12 22:16:23 +10001288 if ((cp = strchr(env, '=')) == NULL || cp == env)
Darren Tucker365433f2004-06-22 12:29:23 +10001289 return (0);
Darren Tucker57f42242006-07-12 22:23:35 +10001290 ret = snprintf(name, sizeof(name), "%.*s", (int)(cp - env), env);
Darren Tucker284706a2006-07-12 22:16:23 +10001291 if (ret <= 0 || (size_t)ret >= sizeof(name))
1292 fatal("env_permitted: name '%.100s...' too long", env);
Darren Tucker365433f2004-06-22 12:29:23 +10001293
1294 for (i = 0; i < options.num_send_env; i++)
1295 if (match_pattern(name, options.send_env[i]))
1296 return (1);
1297
1298 return (0);
1299}
1300
Damien Miller0e220db2004-06-15 10:34:08 +10001301static void
1302control_client(const char *path)
1303{
1304 struct sockaddr_un addr;
Darren Tucker39207a42004-11-05 20:19:51 +11001305 int i, r, fd, sock, exitval, num_env, addr_len;
Damien Miller0e220db2004-06-15 10:34:08 +10001306 Buffer m;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001307 char *term;
Damien Miller3756dce2004-06-18 01:17:29 +10001308 extern char **environ;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001309 u_int flags;
Darren Tuckerfc959702004-07-17 16:12:08 +10001310
Damien Millerd14b1e72005-06-16 13:19:41 +10001311 if (mux_command == 0)
1312 mux_command = SSHMUX_COMMAND_OPEN;
1313
1314 switch (options.control_master) {
1315 case SSHCTL_MASTER_AUTO:
1316 case SSHCTL_MASTER_AUTO_ASK:
1317 debug("auto-mux: Trying existing master");
1318 /* FALLTHROUGH */
1319 case SSHCTL_MASTER_NO:
1320 break;
1321 default:
1322 return;
1323 }
1324
Damien Miller0e220db2004-06-15 10:34:08 +10001325 memset(&addr, '\0', sizeof(addr));
1326 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001327 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001328 strlen(path) + 1;
1329
1330 if (strlcpy(addr.sun_path, path,
1331 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1332 fatal("ControlPath too long");
1333
1334 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1335 fatal("%s socket(): %s", __func__, strerror(errno));
1336
Damien Miller90967402006-03-26 14:07:26 +11001337 if (connect(sock, (struct sockaddr *)&addr, addr_len) == -1) {
Darren Tucker0814d312005-06-01 23:08:51 +10001338 if (mux_command != SSHMUX_COMMAND_OPEN) {
1339 fatal("Control socket connect(%.100s): %s", path,
1340 strerror(errno));
1341 }
Damien Miller538c9b72005-05-26 12:11:28 +10001342 if (errno == ENOENT)
Damien Miller4662d342006-03-26 13:59:59 +11001343 debug("Control socket \"%.100s\" does not exist", path);
Damien Miller538c9b72005-05-26 12:11:28 +10001344 else {
Damien Miller4662d342006-03-26 13:59:59 +11001345 error("Control socket connect(%.100s): %s", path,
Damien Miller538c9b72005-05-26 12:11:28 +10001346 strerror(errno));
1347 }
Damien Miller4662d342006-03-26 13:59:59 +11001348 close(sock);
1349 return;
1350 }
Damien Miller46d38de2005-07-17 17:02:09 +10001351
Damien Miller4662d342006-03-26 13:59:59 +11001352 if (stdin_null_flag) {
1353 if ((fd = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1354 fatal("open(/dev/null): %s", strerror(errno));
1355 if (dup2(fd, STDIN_FILENO) == -1)
1356 fatal("dup2: %s", strerror(errno));
1357 if (fd > STDERR_FILENO)
1358 close(fd);
1359 }
Damien Miller46d38de2005-07-17 17:02:09 +10001360
Damien Miller13390022005-07-06 09:44:19 +10001361 term = getenv("TERM");
Darren Tucker7ebfc102004-11-07 20:06:19 +11001362
1363 flags = 0;
1364 if (tty_flag)
1365 flags |= SSHMUX_FLAG_TTY;
1366 if (subsystem_flag)
1367 flags |= SSHMUX_FLAG_SUBSYS;
Damien Miller13390022005-07-06 09:44:19 +10001368 if (options.forward_x11)
1369 flags |= SSHMUX_FLAG_X11_FWD;
1370 if (options.forward_agent)
1371 flags |= SSHMUX_FLAG_AGENT_FWD;
Damien Miller0e220db2004-06-15 10:34:08 +10001372
Damien Miller0e220db2004-06-15 10:34:08 +10001373 buffer_init(&m);
1374
Darren Tucker7ebfc102004-11-07 20:06:19 +11001375 /* Send our command to server */
1376 buffer_put_int(&m, mux_command);
1377 buffer_put_int(&m, flags);
Damien Miller13390022005-07-06 09:44:19 +10001378 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001379 fatal("%s: msg_send", __func__);
1380 buffer_clear(&m);
1381
1382 /* Get authorisation status and PID of controlee */
Damien Miller0e220db2004-06-15 10:34:08 +10001383 if (ssh_msg_recv(sock, &m) == -1)
1384 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001385 if (buffer_get_char(&m) != SSHMUX_VER)
Damien Miller0e220db2004-06-15 10:34:08 +10001386 fatal("%s: wrong version", __func__);
Damien Miller23f07702004-06-18 01:19:03 +10001387 if (buffer_get_int(&m) != 1)
1388 fatal("Connection to master denied");
Damien Miller0e220db2004-06-15 10:34:08 +10001389 control_server_pid = buffer_get_int(&m);
1390
Damien Miller0e220db2004-06-15 10:34:08 +10001391 buffer_clear(&m);
Damien Miller0e220db2004-06-15 10:34:08 +10001392
Darren Tucker7ebfc102004-11-07 20:06:19 +11001393 switch (mux_command) {
1394 case SSHMUX_COMMAND_ALIVE_CHECK:
Darren Tucker47eede72005-03-14 23:08:12 +11001395 fprintf(stderr, "Master running (pid=%d)\r\n",
Darren Tucker7ebfc102004-11-07 20:06:19 +11001396 control_server_pid);
1397 exit(0);
1398 case SSHMUX_COMMAND_TERMINATE:
1399 fprintf(stderr, "Exit request sent.\r\n");
1400 exit(0);
1401 case SSHMUX_COMMAND_OPEN:
1402 /* continue below */
1403 break;
1404 default:
1405 fatal("silly mux_command %d", mux_command);
1406 }
1407
1408 /* SSHMUX_COMMAND_OPEN */
Damien Miller13390022005-07-06 09:44:19 +10001409 buffer_put_cstring(&m, term ? term : "");
Damien Miller0e220db2004-06-15 10:34:08 +10001410 buffer_append(&command, "\0", 1);
1411 buffer_put_cstring(&m, buffer_ptr(&command));
1412
Darren Tucker365433f2004-06-22 12:29:23 +10001413 if (options.num_send_env == 0 || environ == NULL) {
1414 buffer_put_int(&m, 0);
Darren Tuckerfc959702004-07-17 16:12:08 +10001415 } else {
Darren Tucker365433f2004-06-22 12:29:23 +10001416 /* Pass environment */
1417 num_env = 0;
1418 for (i = 0; environ[i] != NULL; i++)
1419 if (env_permitted(environ[i]))
1420 num_env++; /* Count */
Darren Tuckerfc959702004-07-17 16:12:08 +10001421
Darren Tucker365433f2004-06-22 12:29:23 +10001422 buffer_put_int(&m, num_env);
1423
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001424 for (i = 0; environ[i] != NULL && num_env >= 0; i++)
1425 if (env_permitted(environ[i])) {
1426 num_env--;
Darren Tucker365433f2004-06-22 12:29:23 +10001427 buffer_put_cstring(&m, environ[i]);
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001428 }
Darren Tucker365433f2004-06-22 12:29:23 +10001429 }
Damien Miller3756dce2004-06-18 01:17:29 +10001430
Damien Miller13390022005-07-06 09:44:19 +10001431 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Damien Miller0e220db2004-06-15 10:34:08 +10001432 fatal("%s: msg_send", __func__);
1433
1434 mm_send_fd(sock, STDIN_FILENO);
1435 mm_send_fd(sock, STDOUT_FILENO);
1436 mm_send_fd(sock, STDERR_FILENO);
1437
1438 /* Wait for reply, so master has a chance to gather ttymodes */
1439 buffer_clear(&m);
1440 if (ssh_msg_recv(sock, &m) == -1)
1441 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001442 if (buffer_get_char(&m) != SSHMUX_VER)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001443 fatal("%s: wrong version", __func__);
Damien Miller0e220db2004-06-15 10:34:08 +10001444 buffer_free(&m);
1445
Darren Tucker07336da2004-11-05 20:02:16 +11001446 signal(SIGHUP, control_client_sighandler);
Damien Miller0809e232004-06-18 22:20:57 +10001447 signal(SIGINT, control_client_sighandler);
1448 signal(SIGTERM, control_client_sighandler);
1449 signal(SIGWINCH, control_client_sigrelay);
1450
Damien Miller0e220db2004-06-15 10:34:08 +10001451 if (tty_flag)
1452 enter_raw_mode();
1453
1454 /* Stick around until the controlee closes the client_fd */
1455 exitval = 0;
1456 for (;!control_client_terminate;) {
1457 r = read(sock, &exitval, sizeof(exitval));
1458 if (r == 0) {
1459 debug2("Received EOF from master");
1460 break;
1461 }
1462 if (r > 0)
1463 debug2("Received exit status from master %d", exitval);
1464 if (r == -1 && errno != EINTR)
1465 fatal("%s: read %s", __func__, strerror(errno));
1466 }
1467
1468 if (control_client_terminate)
1469 debug2("Exiting on signal %d", control_client_terminate);
1470
1471 close(sock);
1472
1473 leave_raw_mode();
1474
1475 if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
1476 fprintf(stderr, "Connection to master closed.\r\n");
1477
1478 exit(exitval);
1479}