blob: d0d9457c276ee9bac8f54a3b1d9bf8c49a7b6aa6 [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 Millerbe43ebf2006-07-24 13:51:51 +100057#if defined(HAVE_NETDB_H)
58# include <netdb.h>
59#endif
Damien Miller6645e7a2006-03-15 14:42:54 +110060#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110061#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110062#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100063#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110064#include <signal.h>
Damien Miller2d00e632006-07-24 13:53:19 +100065#include <stddef.h>
Damien Millere3476ed2006-07-24 14:13:33 +100066#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100067#include <unistd.h>
Damien Millereba71ba2000-04-29 23:57:08 +100068
69#include <openssl/evp.h>
Damien Miller0bc1bd82000-11-13 22:57:25 +110070#include <openssl/err.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000073#include "ssh1.h"
Damien Miller1383bd82000-04-06 12:32:37 +100074#include "ssh2.h"
75#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000076#include "cipher.h"
77#include "xmalloc.h"
78#include "packet.h"
79#include "buffer.h"
Damien Miller0e220db2004-06-15 10:34:08 +100080#include "bufaux.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000081#include "channels.h"
Damien Millereba71ba2000-04-29 23:57:08 +100082#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100083#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100084#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000085#include "pathnames.h"
Damien Miller0e220db2004-06-15 10:34:08 +100086#include "dispatch.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +000087#include "clientloop.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000088#include "log.h"
89#include "readconf.h"
90#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000091#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000092#include "kex.h"
93#include "mac.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +100094#include "sshpty.h"
Darren Tucker46bc0752004-05-02 22:11:30 +100095#include "match.h"
Damien Miller0e220db2004-06-15 10:34:08 +100096#include "msg.h"
97#include "monitor_fdpass.h"
Darren Tucker25f60a72004-08-15 17:23:34 +100098#include "uidswap.h"
Damien Millerb7576772006-07-10 20:23:39 +100099#include "version.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100
Ben Lindstromc5b68002001-07-04 04:52:03 +0000101#ifdef SMARTCARD
Ben Lindstromc5b68002001-07-04 04:52:03 +0000102#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +0000103#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000104
Damien Miller3f905871999-11-15 17:10:57 +1100105extern char *__progname;
Damien Miller3f905871999-11-15 17:10:57 +1100106
Damien Miller95def091999-11-25 00:26:21 +1100107/* Flag indicating whether debug mode is on. This can be set on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108int debug_flag = 0;
109
Damien Miller78928792000-04-12 20:17:38 +1000110/* Flag indicating whether a tty should be allocated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111int tty_flag = 0;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000112int no_tty_flag = 0;
113int force_tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114
Damien Miller1383bd82000-04-06 12:32:37 +1000115/* don't exec a shell */
116int no_shell_flag = 0;
Damien Miller1383bd82000-04-06 12:32:37 +1000117
Damien Miller5428f641999-11-25 11:54:57 +1100118/*
119 * Flag indicating that nothing should be read from stdin. This can be set
120 * on the command line.
121 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122int stdin_null_flag = 0;
123
Damien Miller5428f641999-11-25 11:54:57 +1100124/*
125 * Flag indicating that ssh should fork after authentication. This is useful
Ben Lindstromf666fec2002-06-06 19:51:58 +0000126 * so that the passphrase can be entered manually, and then ssh goes to the
Damien Miller5428f641999-11-25 11:54:57 +1100127 * background.
128 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129int fork_after_authentication_flag = 0;
130
Damien Miller5428f641999-11-25 11:54:57 +1100131/*
132 * General data structure for command line options and options configurable
133 * in configuration files. See readconf.h.
134 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135Options options;
136
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000137/* optional user configfile */
138char *config = NULL;
139
Damien Miller5428f641999-11-25 11:54:57 +1100140/*
141 * Name of the host we are connecting to. This is the name given on the
142 * command line, or the HostName specified for the user-supplied name in a
143 * configuration file.
144 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000145char *host;
146
147/* socket address the host resolves to */
Damien Miller34132e52000-01-14 15:45:46 +1100148struct sockaddr_storage hostaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000149
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000150/* Private host keys. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000151Sensitive sensitive_data;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152
153/* Original real UID. */
154uid_t original_real_uid;
Ben Lindstromf9c48842002-06-11 16:37:51 +0000155uid_t original_effective_uid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000156
Damien Miller1383bd82000-04-06 12:32:37 +1000157/* command to be executed */
158Buffer command;
159
Damien Miller832562e2001-01-30 09:30:01 +1100160/* Should we execute a command or invoke a subsystem? */
161int subsystem_flag = 0;
162
Damien Miller2797f7f2002-04-23 21:09:44 +1000163/* # of replies received for global requests */
164static int client_global_request_id = 0;
165
Damien Miller8c4e18a2002-09-19 12:05:02 +1000166/* pid of proxycommand child process */
167pid_t proxy_command_pid = 0;
168
Damien Miller0e220db2004-06-15 10:34:08 +1000169/* fd to control socket */
170int control_fd = -1;
171
Darren Tucker7ebfc102004-11-07 20:06:19 +1100172/* Multiplexing control command */
Darren Tucker0814d312005-06-01 23:08:51 +1000173static u_int mux_command = 0;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100174
Damien Miller0e220db2004-06-15 10:34:08 +1000175/* Only used in control client mode */
176volatile sig_atomic_t control_client_terminate = 0;
177u_int control_server_pid = 0;
178
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000179/* Prints a help message to the user. This function never returns. */
180
Ben Lindstrombba81212001-06-25 05:01:22 +0000181static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000182usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000183{
Damien Miller50955102004-03-22 09:34:58 +1100184 fprintf(stderr,
Darren Tucker9c6bf322004-12-03 14:10:19 +1100185"usage: ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n"
Darren Tucker895d6982005-10-03 18:18:05 +1000186" [-D [bind_address:]port] [-e escape_char] [-F configfile]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100187" [-i identity_file] [-L [bind_address:]port:host:hostport]\n"
Damien Millerf91ee4c2005-03-01 21:24:33 +1100188" [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100189" [-R [bind_address:]port:host:hostport] [-S ctl_path]\n"
Damien Miller991dba42006-07-10 20:16:27 +1000190" [-w local_tun[:remote_tun]] [user@]hostname [command]\n"
Damien Miller50955102004-03-22 09:34:58 +1100191 );
Darren Tuckere9a9b712005-12-20 16:15:51 +1100192 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000193}
194
Ben Lindstrombba81212001-06-25 05:01:22 +0000195static int ssh_session(void);
196static int ssh_session2(void);
197static void load_public_identity_files(void);
Damien Miller0e220db2004-06-15 10:34:08 +1000198static void control_client(const char *path);
Damien Miller1383bd82000-04-06 12:32:37 +1000199
Damien Miller95def091999-11-25 00:26:21 +1100200/*
201 * Main program for the ssh client.
202 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203int
204main(int ac, char **av)
205{
Ben Lindstrom24157572002-06-12 16:09:39 +0000206 int i, opt, exit_status;
Damien Miller9836cf82003-12-17 16:30:06 +1100207 char *p, *cp, *line, buf[256];
Damien Miller95def091999-11-25 00:26:21 +1100208 struct stat st;
Ben Lindstrom086cf212001-03-05 05:56:40 +0000209 struct passwd *pw;
Damien Miller1383bd82000-04-06 12:32:37 +1000210 int dummy;
Ben Lindstrom5ccf63a2001-09-24 20:00:10 +0000211 extern int optind, optreset;
Damien Miller4f8e6692001-07-14 13:22:53 +1000212 extern char *optarg;
Damien Miller9651fe62005-06-26 08:55:25 +1000213 struct servent *sp;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100214 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215
Darren Tuckerce321d82005-10-03 18:11:24 +1000216 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
217 sanitise_stdfd();
218
Damien Miller59d3d5b2003-08-22 09:34:41 +1000219 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000220 init_rng();
221
Damien Miller5428f641999-11-25 11:54:57 +1100222 /*
223 * Save the original real uid. It will be needed later (uid-swapping
224 * may clobber the real uid).
225 */
Damien Miller95def091999-11-25 00:26:21 +1100226 original_real_uid = getuid();
227 original_effective_uid = geteuid();
Damien Millera8e06ce2003-11-21 23:48:55 +1100228
Damien Miller50b9a602002-09-04 16:50:06 +1000229 /*
230 * Use uid-swapping to give up root privileges for the duration of
231 * option processing. We will re-instantiate the rights when we are
232 * ready to create the privileged port, and will permanently drop
233 * them when the port has been created (actually, when the connection
234 * has been made, as we may need to create the port several times).
235 */
236 PRIV_END;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000237
Damien Miller05dd7952000-10-01 00:42:48 +1100238#ifdef HAVE_SETRLIMIT
Damien Miller95def091999-11-25 00:26:21 +1100239 /* If we are installed setuid root be careful to not drop core. */
240 if (original_real_uid != original_effective_uid) {
241 struct rlimit rlim;
242 rlim.rlim_cur = rlim.rlim_max = 0;
243 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
244 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000245 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100246#endif
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000247 /* Get user data. */
248 pw = getpwuid(original_real_uid);
249 if (!pw) {
Damien Miller996acd22003-04-09 20:59:48 +1000250 logit("You don't exist, go away!");
Darren Tuckere9a9b712005-12-20 16:15:51 +1100251 exit(255);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000252 }
253 /* Take a copy of the returned structure. */
254 pw = pwcopy(pw);
255
Damien Miller5428f641999-11-25 11:54:57 +1100256 /*
Damien Miller5428f641999-11-25 11:54:57 +1100257 * Set our umask to something reasonable, as some files are created
258 * with the default umask. This will make them world-readable but
259 * writable only by the owner, which is ok for all files for which we
260 * don't set the modes explicitly.
261 */
Damien Miller95def091999-11-25 00:26:21 +1100262 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000263
Damien Miller95def091999-11-25 00:26:21 +1100264 /* Initialize option structure to indicate that no values have been set. */
265 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000266
Damien Miller95def091999-11-25 00:26:21 +1100267 /* Parse command-line arguments. */
268 host = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000269
Damien Miller2ecb6bd2006-03-15 12:03:53 +1100270 again:
Damien Millerf4614452001-07-14 12:18:10 +1000271 while ((opt = getopt(ac, av,
Damien Millerd27b9472005-12-13 19:29:02 +1100272 "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 +1100273 switch (opt) {
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000274 case '1':
275 options.protocol = SSH_PROTO_1;
276 break;
Damien Miller4af51302000-04-16 11:18:38 +1000277 case '2':
278 options.protocol = SSH_PROTO_2;
279 break;
Damien Miller34132e52000-01-14 15:45:46 +1100280 case '4':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000281 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100282 break;
Damien Miller34132e52000-01-14 15:45:46 +1100283 case '6':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000284 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100285 break;
Damien Miller95def091999-11-25 00:26:21 +1100286 case 'n':
287 stdin_null_flag = 1;
288 break;
Damien Miller95def091999-11-25 00:26:21 +1100289 case 'f':
290 fork_after_authentication_flag = 1;
291 stdin_null_flag = 1;
292 break;
Damien Miller95def091999-11-25 00:26:21 +1100293 case 'x':
294 options.forward_x11 = 0;
295 break;
Damien Miller95def091999-11-25 00:26:21 +1100296 case 'X':
297 options.forward_x11 = 1;
298 break;
Darren Tucker0a118da2003-10-15 15:54:32 +1000299 case 'Y':
300 options.forward_x11 = 1;
301 options.forward_x11_trusted = 1;
302 break;
Damien Miller95def091999-11-25 00:26:21 +1100303 case 'g':
304 options.gateway_ports = 1;
305 break;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100306 case 'O':
307 if (strcmp(optarg, "check") == 0)
308 mux_command = SSHMUX_COMMAND_ALIVE_CHECK;
309 else if (strcmp(optarg, "exit") == 0)
310 mux_command = SSHMUX_COMMAND_TERMINATE;
311 else
312 fatal("Invalid multiplex command.");
313 break;
Damien Miller147bba32002-09-04 16:46:06 +1000314 case 'P': /* deprecated */
Damien Miller95def091999-11-25 00:26:21 +1100315 options.use_privileged_port = 0;
316 break;
Damien Miller95def091999-11-25 00:26:21 +1100317 case 'a':
318 options.forward_agent = 0;
319 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000320 case 'A':
321 options.forward_agent = 1;
322 break;
Damien Miller95def091999-11-25 00:26:21 +1100323 case 'k':
Damien Millere0113cc2003-11-24 13:10:09 +1100324 options.gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100325 break;
Damien Miller95def091999-11-25 00:26:21 +1100326 case 'i':
327 if (stat(optarg, &st) < 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000328 fprintf(stderr, "Warning: Identity file %s "
Damien Miller3eb48b62005-03-01 21:15:46 +1100329 "not accessible: %s.\n", optarg,
330 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100331 break;
332 }
Damien Millerf4614452001-07-14 12:18:10 +1000333 if (options.num_identity_files >=
334 SSH_MAX_IDENTITY_FILES)
335 fatal("Too many identity files specified "
336 "(max %d)", SSH_MAX_IDENTITY_FILES);
337 options.identity_files[options.num_identity_files++] =
338 xstrdup(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100339 break;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000340 case 'I':
341#ifdef SMARTCARD
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000342 options.smartcard_device = xstrdup(optarg);
Ben Lindstrombcc18082001-08-06 21:59:25 +0000343#else
Ben Lindstromc5b68002001-07-04 04:52:03 +0000344 fprintf(stderr, "no support for smartcards.\n");
Ben Lindstrombcc18082001-08-06 21:59:25 +0000345#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000346 break;
Damien Miller95def091999-11-25 00:26:21 +1100347 case 't':
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000348 if (tty_flag)
349 force_tty_flag = 1;
Damien Miller95def091999-11-25 00:26:21 +1100350 tty_flag = 1;
351 break;
Damien Miller95def091999-11-25 00:26:21 +1100352 case 'v':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000353 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100354 debug_flag = 1;
355 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000356 } else {
357 if (options.log_level < SYSLOG_LEVEL_DEBUG3)
358 options.log_level++;
Damien Millere4340be2000-09-16 13:29:08 +1100359 break;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000360 }
Darren Tuckere9bf9842004-11-05 20:05:32 +1100361 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100362 case 'V':
Damien Miller0c889cd2004-03-22 09:36:00 +1100363 fprintf(stderr, "%s, %s\n",
Damien Miller2aa6d3c2004-09-12 16:53:04 +1000364 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
Damien Miller95def091999-11-25 00:26:21 +1100365 if (opt == 'V')
366 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100367 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100368 case 'w':
Damien Miller7b58e802005-12-13 19:33:19 +1100369 if (options.tun_open == -1)
370 options.tun_open = SSH_TUNMODE_DEFAULT;
Damien Millerd27b9472005-12-13 19:29:02 +1100371 options.tun_local = a2tun(optarg, &options.tun_remote);
Damien Miller7b58e802005-12-13 19:33:19 +1100372 if (options.tun_local == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100373 fprintf(stderr, "Bad tun device '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100374 exit(255);
Damien Millerd27b9472005-12-13 19:29:02 +1100375 }
376 break;
Damien Miller95def091999-11-25 00:26:21 +1100377 case 'q':
378 options.log_level = SYSLOG_LEVEL_QUIET;
379 break;
Damien Miller95def091999-11-25 00:26:21 +1100380 case 'e':
381 if (optarg[0] == '^' && optarg[2] == 0 &&
Damien Millerf4614452001-07-14 12:18:10 +1000382 (u_char) optarg[1] >= 64 &&
383 (u_char) optarg[1] < 128)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000384 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100385 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000386 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100387 else if (strcmp(optarg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000388 options.escape_char = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100389 else {
Damien Millerf4614452001-07-14 12:18:10 +1000390 fprintf(stderr, "Bad escape character '%s'.\n",
391 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100392 exit(255);
Damien Miller95def091999-11-25 00:26:21 +1100393 }
394 break;
Damien Miller95def091999-11-25 00:26:21 +1100395 case 'c':
Damien Millereba71ba2000-04-29 23:57:08 +1000396 if (ciphers_valid(optarg)) {
397 /* SSH2 only */
398 options.ciphers = xstrdup(optarg);
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000399 options.cipher = SSH_CIPHER_INVALID;
Damien Millereba71ba2000-04-29 23:57:08 +1000400 } else {
401 /* SSH1 only */
Damien Millere39cacc2000-11-29 12:18:44 +1100402 options.cipher = cipher_number(optarg);
403 if (options.cipher == -1) {
Damien Millerf4614452001-07-14 12:18:10 +1000404 fprintf(stderr,
405 "Unknown cipher type '%s'\n",
406 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100407 exit(255);
Damien Millereba71ba2000-04-29 23:57:08 +1000408 }
Damien Millerf4614452001-07-14 12:18:10 +1000409 if (options.cipher == SSH_CIPHER_3DES)
Damien Millere39cacc2000-11-29 12:18:44 +1100410 options.ciphers = "3des-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000411 else if (options.cipher == SSH_CIPHER_BLOWFISH)
Damien Millere39cacc2000-11-29 12:18:44 +1100412 options.ciphers = "blowfish-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000413 else
Damien Millere39cacc2000-11-29 12:18:44 +1100414 options.ciphers = (char *)-1;
Damien Miller95def091999-11-25 00:26:21 +1100415 }
416 break;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000417 case 'm':
418 if (mac_valid(optarg))
419 options.macs = xstrdup(optarg);
420 else {
Damien Millerf4614452001-07-14 12:18:10 +1000421 fprintf(stderr, "Unknown mac type '%s'\n",
422 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100423 exit(255);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000424 }
425 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000426 case 'M':
Damien Millerd14b1e72005-06-16 13:19:41 +1000427 if (options.control_master == SSHCTL_MASTER_YES)
428 options.control_master = SSHCTL_MASTER_ASK;
429 else
430 options.control_master = SSHCTL_MASTER_YES;
Damien Miller0e220db2004-06-15 10:34:08 +1000431 break;
Damien Miller95def091999-11-25 00:26:21 +1100432 case 'p':
Ben Lindstrom19066a12001-04-12 23:39:26 +0000433 options.port = a2port(optarg);
434 if (options.port == 0) {
Ben Lindstrom146edb92001-04-11 23:06:28 +0000435 fprintf(stderr, "Bad port '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100436 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000437 }
Damien Miller95def091999-11-25 00:26:21 +1100438 break;
Damien Miller95def091999-11-25 00:26:21 +1100439 case 'l':
440 options.user = optarg;
441 break;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000442
Damien Miller95def091999-11-25 00:26:21 +1100443 case 'L':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100444 if (parse_forward(&fwd, optarg))
445 add_local_forward(&options, &fwd);
446 else {
Damien Millerf4614452001-07-14 12:18:10 +1000447 fprintf(stderr,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100448 "Bad local forwarding specification '%s'\n",
Damien Millerf4614452001-07-14 12:18:10 +1000449 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100450 exit(255);
Ben Lindstrom1a174712001-09-12 17:56:15 +0000451 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100452 break;
453
454 case 'R':
455 if (parse_forward(&fwd, optarg)) {
456 add_remote_forward(&options, &fwd);
457 } else {
458 fprintf(stderr,
459 "Bad remote forwarding specification "
460 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100461 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100462 }
Damien Miller95def091999-11-25 00:26:21 +1100463 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000464
465 case 'D':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100466 cp = p = xstrdup(optarg);
467 memset(&fwd, '\0', sizeof(fwd));
468 fwd.connect_host = "socks";
469 if ((fwd.listen_host = hpdelim(&cp)) == NULL) {
470 fprintf(stderr, "Bad dynamic forwarding "
471 "specification '%.100s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100472 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100473 }
474 if (cp != NULL) {
475 fwd.listen_port = a2port(cp);
476 fwd.listen_host = cleanhostname(fwd.listen_host);
477 } else {
478 fwd.listen_port = a2port(fwd.listen_host);
Damien Millerbe1045d2005-08-12 22:10:56 +1000479 fwd.listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100480 }
481
482 if (fwd.listen_port == 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000483 fprintf(stderr, "Bad dynamic port '%s'\n",
484 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100485 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000486 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100487 add_local_forward(&options, &fwd);
488 xfree(p);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000489 break;
490
Damien Miller95def091999-11-25 00:26:21 +1100491 case 'C':
492 options.compression = 1;
493 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000494 case 'N':
495 no_shell_flag = 1;
496 no_tty_flag = 1;
497 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000498 case 'T':
499 no_tty_flag = 1;
500 break;
Damien Miller95def091999-11-25 00:26:21 +1100501 case 'o':
502 dummy = 1;
Damien Miller9836cf82003-12-17 16:30:06 +1100503 line = xstrdup(optarg);
Damien Millerf4614452001-07-14 12:18:10 +1000504 if (process_config_line(&options, host ? host : "",
Damien Miller9836cf82003-12-17 16:30:06 +1100505 line, "command-line", 0, &dummy) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100506 exit(255);
Damien Miller9836cf82003-12-17 16:30:06 +1100507 xfree(line);
Damien Miller95def091999-11-25 00:26:21 +1100508 break;
Damien Miller832562e2001-01-30 09:30:01 +1100509 case 's':
510 subsystem_flag = 1;
511 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000512 case 'S':
513 if (options.control_path != NULL)
514 free(options.control_path);
515 options.control_path = xstrdup(optarg);
Damien Miller0e220db2004-06-15 10:34:08 +1000516 break;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000517 case 'b':
518 options.bind_address = optarg;
519 break;
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000520 case 'F':
521 config = optarg;
522 break;
Damien Miller95def091999-11-25 00:26:21 +1100523 default:
524 usage();
525 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000526 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000527
Damien Millerf4614452001-07-14 12:18:10 +1000528 ac -= optind;
529 av += optind;
530
531 if (ac > 0 && !host && **av != '-') {
Ben Lindstromc276c122002-12-23 02:14:51 +0000532 if (strrchr(*av, '@')) {
Damien Millerf4614452001-07-14 12:18:10 +1000533 p = xstrdup(*av);
Ben Lindstromc276c122002-12-23 02:14:51 +0000534 cp = strrchr(p, '@');
Damien Millerf4614452001-07-14 12:18:10 +1000535 if (cp == NULL || cp == p)
536 usage();
537 options.user = p;
538 *cp = '\0';
539 host = ++cp;
540 } else
541 host = *av;
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000542 if (ac > 1) {
543 optind = optreset = 1;
Damien Millerf4614452001-07-14 12:18:10 +1000544 goto again;
545 }
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000546 ac--, av++;
Damien Millerf4614452001-07-14 12:18:10 +1000547 }
548
Damien Miller95def091999-11-25 00:26:21 +1100549 /* Check that we got a host name. */
550 if (!host)
551 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000552
Damien Millercb5e44a2000-09-29 12:12:36 +1100553 SSLeay_add_all_algorithms();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100554 ERR_load_crypto_strings();
Damien Millercb5e44a2000-09-29 12:12:36 +1100555
Damien Miller95def091999-11-25 00:26:21 +1100556 /* Initialize the command to execute on remote host. */
557 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000558
Damien Miller5428f641999-11-25 11:54:57 +1100559 /*
560 * Save the command to execute on the remote host in a buffer. There
561 * is no limit on the length of the command, except by the maximum
562 * packet size. Also sets the tty flag if there is no command.
563 */
Damien Millerf4614452001-07-14 12:18:10 +1000564 if (!ac) {
Damien Miller95def091999-11-25 00:26:21 +1100565 /* No command specified - execute shell on a tty. */
566 tty_flag = 1;
Damien Miller832562e2001-01-30 09:30:01 +1100567 if (subsystem_flag) {
Damien Millerf4614452001-07-14 12:18:10 +1000568 fprintf(stderr,
569 "You must specify a subsystem to invoke.\n");
Damien Miller832562e2001-01-30 09:30:01 +1100570 usage();
571 }
Damien Miller95def091999-11-25 00:26:21 +1100572 } else {
Damien Millerf4614452001-07-14 12:18:10 +1000573 /* A command has been specified. Store it into the buffer. */
574 for (i = 0; i < ac; i++) {
575 if (i)
Damien Miller95def091999-11-25 00:26:21 +1100576 buffer_append(&command, " ", 1);
577 buffer_append(&command, av[i], strlen(av[i]));
578 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000579 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000580
Damien Miller95def091999-11-25 00:26:21 +1100581 /* Cannot fork to background if no command. */
Damien Millercaf6dd62000-08-29 11:33:50 +1100582 if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
Damien Miller95def091999-11-25 00:26:21 +1100583 fatal("Cannot fork into background without a command to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000584
Damien Miller95def091999-11-25 00:26:21 +1100585 /* Allocate a tty by default if no command specified. */
586 if (buffer_len(&command) == 0)
587 tty_flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000588
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000589 /* Force no tty */
Ben Lindstromc72745a2000-12-02 19:03:54 +0000590 if (no_tty_flag)
591 tty_flag = 0;
Damien Miller95def091999-11-25 00:26:21 +1100592 /* Do not allocate a tty if stdin is not a tty. */
Damien Millerddee5752005-05-26 12:05:05 +1000593 if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100594 if (tty_flag)
Damien Miller996acd22003-04-09 20:59:48 +1000595 logit("Pseudo-terminal will not be allocated because stdin is not a terminal.");
Damien Miller95def091999-11-25 00:26:21 +1100596 tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000597 }
Damien Miller1383bd82000-04-06 12:32:37 +1000598
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000599 /*
600 * Initialize "log" output. Since we are the client all output
601 * actually goes to stderr.
602 */
Ben Lindstrom2b646522001-04-12 16:16:57 +0000603 log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
604 SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000605
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000606 /*
607 * Read per-user configuration file. Ignore the system wide config
608 * file if the user specifies a config file on the command line.
609 */
610 if (config != NULL) {
Damien Miller914420f2004-04-20 20:14:07 +1000611 if (!read_config_file(config, host, &options, 0))
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000612 fatal("Can't open user config file %.100s: "
613 "%.100s", config, strerror(errno));
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000614 } else {
615 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
616 _PATH_SSH_USER_CONFFILE);
Damien Miller57a44762004-04-20 20:11:57 +1000617 (void)read_config_file(buf, host, &options, 1);
Ben Lindstrom83f07d12001-10-03 17:22:29 +0000618
619 /* Read systemwide configuration file after use config. */
Darren Tuckerfc959702004-07-17 16:12:08 +1000620 (void)read_config_file(_PATH_HOST_CONFIG_FILE, host,
Damien Miller57a44762004-04-20 20:11:57 +1000621 &options, 0);
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000622 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000623
Damien Miller95def091999-11-25 00:26:21 +1100624 /* Fill configuration defaults. */
625 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000626
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000627 channel_set_af(options.address_family);
628
Damien Miller95def091999-11-25 00:26:21 +1100629 /* reinit */
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000630 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000631
Damien Miller60bc5172001-03-19 09:38:15 +1100632 seed_rng();
633
Damien Miller95def091999-11-25 00:26:21 +1100634 if (options.user == NULL)
635 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000636
Damien Miller95def091999-11-25 00:26:21 +1100637 if (options.hostname != NULL)
638 host = options.hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000639
Darren Tucker3f521e22003-07-03 16:20:42 +1000640 /* force lowercase for hostkey matching */
641 if (options.host_key_alias != NULL) {
642 for (p = options.host_key_alias; *p; p++)
643 if (isupper(*p))
Damien Miller1d2b6702006-03-26 14:09:54 +1100644 *p = (char)tolower(*p);
Darren Tucker3f521e22003-07-03 16:20:42 +1000645 }
646
Damien Miller7c71cc72005-06-26 08:56:31 +1000647 /* Get default port if port has not been set. */
648 if (options.port == 0) {
649 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
650 options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
651 }
652
Damien Miller9f1e33a2003-02-24 11:57:32 +1100653 if (options.proxy_command != NULL &&
654 strcmp(options.proxy_command, "none") == 0)
655 options.proxy_command = NULL;
Damien Miller8f74c8f2005-06-26 08:56:03 +1000656 if (options.control_path != NULL &&
657 strcmp(options.control_path, "none") == 0)
658 options.control_path = NULL;
Damien Miller9f1e33a2003-02-24 11:57:32 +1100659
Damien Miller0e220db2004-06-15 10:34:08 +1000660 if (options.control_path != NULL) {
Damien Miller6b1d53c2006-03-31 23:13:21 +1100661 char thishost[NI_MAXHOST];
Damien Miller3ec54c72006-03-15 11:30:13 +1100662
Damien Miller6b1d53c2006-03-31 23:13:21 +1100663 if (gethostname(thishost, sizeof(thishost)) == -1)
Damien Miller3ec54c72006-03-15 11:30:13 +1100664 fatal("gethostname: %s", strerror(errno));
Damien Miller6476cad2005-06-16 13:18:34 +1000665 snprintf(buf, sizeof(buf), "%d", options.port);
666 cp = tilde_expand_filename(options.control_path,
667 original_real_uid);
668 options.control_path = percent_expand(cp, "p", buf, "h", host,
Damien Miller6b1d53c2006-03-31 23:13:21 +1100669 "r", options.user, "l", thishost, (char *)NULL);
Damien Miller6476cad2005-06-16 13:18:34 +1000670 xfree(cp);
Damien Miller0e220db2004-06-15 10:34:08 +1000671 }
Darren Tucker0814d312005-06-01 23:08:51 +1000672 if (mux_command != 0 && options.control_path == NULL)
673 fatal("No ControlPath specified for \"-O\" command");
Damien Millerd14b1e72005-06-16 13:19:41 +1000674 if (options.control_path != NULL)
Damien Millerdadfd4d2005-05-26 12:07:13 +1000675 control_client(options.control_path);
Damien Miller0e220db2004-06-15 10:34:08 +1000676
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000677 /* Open a connection to the remote host. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000678 if (ssh_connect(host, &hostaddr, options.port,
679 options.address_family, options.connection_attempts,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000680#ifdef HAVE_CYGWIN
681 options.use_privileged_port,
682#else
Ben Lindstromf9c48842002-06-11 16:37:51 +0000683 original_effective_uid == 0 && options.use_privileged_port,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000684#endif
Ben Lindstromda394ca2002-06-12 16:11:12 +0000685 options.proxy_command) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100686 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000687
Damien Miller5428f641999-11-25 11:54:57 +1100688 /*
689 * If we successfully made the connection, load the host private key
690 * in case we will need it later for combined rsa-rhosts
691 * authentication. This must be done before releasing extra
692 * privileges, because the file is only readable by root.
Ben Lindstrom9e5bb572002-06-06 19:58:27 +0000693 * If we cannot access the private keys, load the public keys
694 * instead and try to execute the ssh-keysign helper instead.
Damien Miller5428f641999-11-25 11:54:57 +1100695 */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000696 sensitive_data.nkeys = 0;
697 sensitive_data.keys = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000698 sensitive_data.external_keysign = 0;
Ben Lindstrom24157572002-06-12 16:09:39 +0000699 if (options.rhosts_rsa_authentication ||
700 options.hostbased_authentication) {
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000701 sensitive_data.nkeys = 3;
Damien Millerddd63ab2006-03-31 23:10:51 +1100702 sensitive_data.keys = xcalloc(sensitive_data.nkeys,
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000703 sizeof(Key));
Ben Lindstromf9c48842002-06-11 16:37:51 +0000704
705 PRIV_START;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000706 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
Darren Tucker232b76f2006-05-06 17:41:51 +1000707 _PATH_HOST_KEY_FILE, "", NULL, NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000708 sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
Darren Tucker232b76f2006-05-06 17:41:51 +1000709 _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000710 sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
Darren Tucker232b76f2006-05-06 17:41:51 +1000711 _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL);
Ben Lindstromf9c48842002-06-11 16:37:51 +0000712 PRIV_END;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000713
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +0000714 if (options.hostbased_authentication == 1 &&
715 sensitive_data.keys[0] == NULL &&
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000716 sensitive_data.keys[1] == NULL &&
717 sensitive_data.keys[2] == NULL) {
718 sensitive_data.keys[1] = key_load_public(
719 _PATH_HOST_DSA_KEY_FILE, NULL);
720 sensitive_data.keys[2] = key_load_public(
721 _PATH_HOST_RSA_KEY_FILE, NULL);
722 sensitive_data.external_keysign = 1;
723 }
Damien Miller95def091999-11-25 00:26:21 +1100724 }
Damien Miller5428f641999-11-25 11:54:57 +1100725 /*
726 * Get rid of any extra privileges that we may have. We will no
727 * longer need them. Also, extra privileges could make it very hard
728 * to read identity files and other non-world-readable files from the
729 * user's home directory if it happens to be on a NFS volume where
730 * root is mapped to nobody.
731 */
Darren Tucker25f60a72004-08-15 17:23:34 +1000732 if (original_effective_uid == 0) {
733 PRIV_START;
734 permanently_set_uid(pw);
735 }
Damien Miller95def091999-11-25 00:26:21 +1100736
Damien Miller5428f641999-11-25 11:54:57 +1100737 /*
738 * Now that we are back to our own permissions, create ~/.ssh
Damien Miller788f2122005-11-05 15:14:59 +1100739 * directory if it doesn't already exist.
Damien Miller5428f641999-11-25 11:54:57 +1100740 */
Ben Lindstrom930b14a2001-08-15 23:19:21 +0000741 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 +1100742 if (stat(buf, &st) < 0)
Damien Millerbe484b52000-07-15 14:14:16 +1000743 if (mkdir(buf, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100744 error("Could not create directory '%.200s'.", buf);
745
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000746 /* load options.identity_files */
747 load_public_identity_files();
748
749 /* Expand ~ in known host file names. */
750 /* XXX mem-leaks: */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100751 options.system_hostfile =
752 tilde_expand_filename(options.system_hostfile, original_real_uid);
753 options.user_hostfile =
754 tilde_expand_filename(options.user_hostfile, original_real_uid);
755 options.system_hostfile2 =
756 tilde_expand_filename(options.system_hostfile2, original_real_uid);
757 options.user_hostfile2 =
758 tilde_expand_filename(options.user_hostfile2, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100759
Damien Miller07cd5892001-11-12 10:52:03 +1100760 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
761
Damien Miller95def091999-11-25 00:26:21 +1100762 /* Log into the remote system. This never returns if the login fails. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000763 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, pw);
Damien Miller95def091999-11-25 00:26:21 +1100764
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000765 /* We no longer need the private host keys. Clear them now. */
766 if (sensitive_data.nkeys != 0) {
767 for (i = 0; i < sensitive_data.nkeys; i++) {
768 if (sensitive_data.keys[i] != NULL) {
769 /* Destroys contents safely */
770 debug3("clear hostkey %d", i);
771 key_free(sensitive_data.keys[i]);
772 sensitive_data.keys[i] = NULL;
773 }
774 }
775 xfree(sensitive_data.keys);
776 }
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +0000777 for (i = 0; i < options.num_identity_files; i++) {
778 if (options.identity_files[i]) {
779 xfree(options.identity_files[i]);
780 options.identity_files[i] = NULL;
781 }
782 if (options.identity_keys[i]) {
783 key_free(options.identity_keys[i]);
784 options.identity_keys[i] = NULL;
785 }
786 }
Damien Miller95def091999-11-25 00:26:21 +1100787
Damien Miller1383bd82000-04-06 12:32:37 +1000788 exit_status = compat20 ? ssh_session2() : ssh_session();
789 packet_close();
Damien Miller8c4e18a2002-09-19 12:05:02 +1000790
Damien Miller0e220db2004-06-15 10:34:08 +1000791 if (options.control_path != NULL && control_fd != -1)
792 unlink(options.control_path);
793
Damien Miller8c4e18a2002-09-19 12:05:02 +1000794 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100795 * Send SIGHUP to proxy command if used. We don't wait() in
Damien Miller8c4e18a2002-09-19 12:05:02 +1000796 * case it hangs and instead rely on init to reap the child
797 */
798 if (proxy_command_pid > 1)
799 kill(proxy_command_pid, SIGHUP);
800
Damien Miller1383bd82000-04-06 12:32:37 +1000801 return exit_status;
802}
803
Ben Lindstrombba81212001-06-25 05:01:22 +0000804static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100805ssh_init_forwarding(void)
806{
Kevin Steves12057502001-02-05 14:54:34 +0000807 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100808 int i;
Kevin Steves12057502001-02-05 14:54:34 +0000809
Damien Miller0bc1bd82000-11-13 22:57:25 +1100810 /* Initiate local TCP/IP port forwardings. */
811 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100812 debug("Local connections to %.200s:%d forwarded to remote "
813 "address %.200s:%d",
Darren Tucker47eede72005-03-14 23:08:12 +1100814 (options.local_forwards[i].listen_host == NULL) ?
815 (options.gateway_ports ? "*" : "LOCALHOST") :
Damien Millerf91ee4c2005-03-01 21:24:33 +1100816 options.local_forwards[i].listen_host,
817 options.local_forwards[i].listen_port,
818 options.local_forwards[i].connect_host,
819 options.local_forwards[i].connect_port);
Damien Millerb16461c2002-01-22 23:29:22 +1100820 success += channel_setup_local_fwd_listener(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100821 options.local_forwards[i].listen_host,
822 options.local_forwards[i].listen_port,
823 options.local_forwards[i].connect_host,
824 options.local_forwards[i].connect_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100825 options.gateway_ports);
826 }
Darren Tuckere7d4b192006-07-12 22:17:10 +1000827 if (i > 0 && success != i && options.exit_on_forward_failure)
828 fatal("Could not request local forwarding.");
Kevin Steves12057502001-02-05 14:54:34 +0000829 if (i > 0 && success == 0)
830 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100831
832 /* Initiate remote TCP/IP port forwardings. */
833 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100834 debug("Remote connections from %.200s:%d forwarded to "
835 "local address %.200s:%d",
Damien Miller46d38de2005-07-17 17:02:09 +1000836 (options.remote_forwards[i].listen_host == NULL) ?
Damien Milleraa3bb102005-11-05 15:12:59 +1100837 "LOCALHOST" : options.remote_forwards[i].listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100838 options.remote_forwards[i].listen_port,
839 options.remote_forwards[i].connect_host,
840 options.remote_forwards[i].connect_port);
Darren Tuckere7d4b192006-07-12 22:17:10 +1000841 if (channel_request_remote_forwarding(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100842 options.remote_forwards[i].listen_host,
843 options.remote_forwards[i].listen_port,
844 options.remote_forwards[i].connect_host,
Darren Tuckere7d4b192006-07-12 22:17:10 +1000845 options.remote_forwards[i].connect_port) < 0) {
846 if (options.exit_on_forward_failure)
847 fatal("Could not request remote forwarding.");
848 else
849 logit("Warning: Could not request remote "
850 "forwarding.");
851 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100852 }
853}
854
Ben Lindstrombba81212001-06-25 05:01:22 +0000855static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100856check_agent_present(void)
857{
858 if (options.forward_agent) {
Damien Miller788f2122005-11-05 15:14:59 +1100859 /* Clear agent forwarding if we don't have an agent. */
Damien Miller789e95d2002-09-12 09:52:46 +1000860 if (!ssh_agent_present())
Damien Miller0bc1bd82000-11-13 22:57:25 +1100861 options.forward_agent = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100862 }
863}
864
Ben Lindstrombba81212001-06-25 05:01:22 +0000865static int
Damien Miller1383bd82000-04-06 12:32:37 +1000866ssh_session(void)
867{
868 int type;
Damien Miller1383bd82000-04-06 12:32:37 +1000869 int interactive = 0;
870 int have_tty = 0;
871 struct winsize ws;
Damien Miller1383bd82000-04-06 12:32:37 +1000872 char *cp;
Damien Miller17e7ed02005-06-17 12:54:33 +1000873 const char *display;
Damien Miller1383bd82000-04-06 12:32:37 +1000874
Damien Miller95def091999-11-25 00:26:21 +1100875 /* Enable compression if requested. */
876 if (options.compression) {
877 debug("Requesting compression at level %d.", options.compression_level);
878
879 if (options.compression_level < 1 || options.compression_level > 9)
880 fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
881
882 /* Send the request. */
883 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
884 packet_put_int(options.compression_level);
885 packet_send();
886 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100887 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100888 if (type == SSH_SMSG_SUCCESS)
889 packet_start_compression(options.compression_level);
890 else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000891 logit("Warning: Remote host refused compression.");
Damien Miller95def091999-11-25 00:26:21 +1100892 else
893 packet_disconnect("Protocol error waiting for compression response.");
894 }
895 /* Allocate a pseudo tty if appropriate. */
896 if (tty_flag) {
897 debug("Requesting pty.");
898
899 /* Start the packet. */
900 packet_start(SSH_CMSG_REQUEST_PTY);
901
902 /* Store TERM in the packet. There is no limit on the
903 length of the string. */
904 cp = getenv("TERM");
905 if (!cp)
906 cp = "";
Ben Lindstrom664408d2001-06-09 01:42:01 +0000907 packet_put_cstring(cp);
Damien Miller95def091999-11-25 00:26:21 +1100908
909 /* Store window size in the packet. */
910 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
911 memset(&ws, 0, sizeof(ws));
Damien Miller71a73672006-03-26 14:04:36 +1100912 packet_put_int((u_int)ws.ws_row);
913 packet_put_int((u_int)ws.ws_col);
914 packet_put_int((u_int)ws.ws_xpixel);
915 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller95def091999-11-25 00:26:21 +1100916
917 /* Store tty modes in the packet. */
Ben Lindstromae8e2d32001-04-14 23:13:02 +0000918 tty_make_modes(fileno(stdin), NULL);
Damien Miller95def091999-11-25 00:26:21 +1100919
920 /* Send the packet, and wait for it to leave. */
921 packet_send();
922 packet_write_wait();
923
924 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100925 type = packet_read();
Damien Miller450a7a12000-03-26 13:04:51 +1000926 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100927 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +1000928 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000929 } else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000930 logit("Warning: Remote host failed or refused to allocate a pseudo tty.");
Damien Miller95def091999-11-25 00:26:21 +1100931 else
932 packet_disconnect("Protocol error waiting for pty request response.");
933 }
934 /* Request X11 forwarding if enabled and DISPLAY is set. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000935 display = getenv("DISPLAY");
936 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000937 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +1000938 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000939 client_x11_get_proto(display, options.xauth_location,
940 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +1000941 /* Request forwarding with authentication spoofing. */
Damien Miller95def091999-11-25 00:26:21 +1100942 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +1000943 x11_request_forwarding_with_spoofing(0, display, proto, data);
Damien Miller95def091999-11-25 00:26:21 +1100944
945 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100946 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100947 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100948 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +1000949 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller996acd22003-04-09 20:59:48 +1000950 logit("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +1000951 } else {
Damien Miller95def091999-11-25 00:26:21 +1100952 packet_disconnect("Protocol error waiting for X11 forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +1000953 }
Damien Miller95def091999-11-25 00:26:21 +1100954 }
955 /* Tell the packet module whether this is an interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000956 packet_set_interactive(interactive);
Damien Miller95def091999-11-25 00:26:21 +1100957
958 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100959 check_agent_present();
960
Damien Miller95def091999-11-25 00:26:21 +1100961 if (options.forward_agent) {
962 debug("Requesting authentication agent forwarding.");
963 auth_request_forwarding();
964
965 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100966 type = packet_read();
Damien Miller48b03fc2002-01-22 23:11:40 +1100967 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100968 if (type != SSH_SMSG_SUCCESS)
Damien Miller996acd22003-04-09 20:59:48 +1000969 logit("Warning: Remote host denied authentication agent forwarding.");
Damien Miller95def091999-11-25 00:26:21 +1100970 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000971
Damien Miller0bc1bd82000-11-13 22:57:25 +1100972 /* Initiate port forwardings. */
973 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000974
Damien Miller5428f641999-11-25 11:54:57 +1100975 /* If requested, let ssh continue in the background. */
Damien Miller4af51302000-04-16 11:18:38 +1000976 if (fork_after_authentication_flag)
Damien Miller5428f641999-11-25 11:54:57 +1100977 if (daemon(1, 1) < 0)
978 fatal("daemon() failed: %.200s", strerror(errno));
979
980 /*
981 * If a command was specified on the command line, execute the
982 * command now. Otherwise request the server to start a shell.
983 */
Damien Miller95def091999-11-25 00:26:21 +1100984 if (buffer_len(&command) > 0) {
985 int len = buffer_len(&command);
986 if (len > 900)
987 len = 900;
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100988 debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
Damien Miller95def091999-11-25 00:26:21 +1100989 packet_start(SSH_CMSG_EXEC_CMD);
990 packet_put_string(buffer_ptr(&command), buffer_len(&command));
991 packet_send();
992 packet_write_wait();
993 } else {
994 debug("Requesting shell.");
995 packet_start(SSH_CMSG_EXEC_SHELL);
996 packet_send();
997 packet_write_wait();
998 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000999
Damien Miller95def091999-11-25 00:26:21 +11001000 /* Enter the interactive session. */
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001001 return client_loop(have_tty, tty_flag ?
1002 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
Damien Miller1383bd82000-04-06 12:32:37 +10001003}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001004
Ben Lindstrombba81212001-06-25 05:01:22 +00001005static void
Damien Miller0e220db2004-06-15 10:34:08 +10001006ssh_subsystem_reply(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001007{
1008 int id, len;
1009
1010 id = packet_get_int();
1011 len = buffer_len(&command);
Ben Lindstrom4040fe12001-03-05 06:52:57 +00001012 if (len > 900)
1013 len = 900;
Damien Miller48b03fc2002-01-22 23:11:40 +11001014 packet_check_eom();
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001015 if (type == SSH2_MSG_CHANNEL_FAILURE)
1016 fatal("Request for subsystem '%.*s' failed on channel %d",
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001017 len, (u_char *)buffer_ptr(&command), id);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001018}
1019
Damien Miller2797f7f2002-04-23 21:09:44 +10001020void
Damien Miller509b0102003-12-17 16:33:10 +11001021client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt)
Damien Miller2797f7f2002-04-23 21:09:44 +10001022{
1023 int i;
1024
1025 i = client_global_request_id++;
Damien Miller509b0102003-12-17 16:33:10 +11001026 if (i >= options.num_remote_forwards)
Damien Miller2797f7f2002-04-23 21:09:44 +10001027 return;
Damien Miller2797f7f2002-04-23 21:09:44 +10001028 debug("remote forward %s for: listen %d, connect %s:%d",
1029 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
Damien Millerf91ee4c2005-03-01 21:24:33 +11001030 options.remote_forwards[i].listen_port,
1031 options.remote_forwards[i].connect_host,
1032 options.remote_forwards[i].connect_port);
Darren Tuckere7d4b192006-07-12 22:17:10 +10001033 if (type == SSH2_MSG_REQUEST_FAILURE) {
1034 if (options.exit_on_forward_failure)
1035 fatal("Error: remote port forwarding failed for "
1036 "listen port %d",
1037 options.remote_forwards[i].listen_port);
1038 else
1039 logit("Warning: remote port forwarding failed for "
1040 "listen port %d",
1041 options.remote_forwards[i].listen_port);
1042 }
Damien Miller2797f7f2002-04-23 21:09:44 +10001043}
1044
Damien Miller0e220db2004-06-15 10:34:08 +10001045static void
1046ssh_control_listener(void)
1047{
1048 struct sockaddr_un addr;
1049 mode_t old_umask;
Damien Miller07b6ff12004-06-15 11:14:45 +10001050 int addr_len;
1051
Damien Millerd14b1e72005-06-16 13:19:41 +10001052 if (options.control_path == NULL ||
1053 options.control_master == SSHCTL_MASTER_NO)
Damien Miller0e220db2004-06-15 10:34:08 +10001054 return;
1055
Damien Millerd14b1e72005-06-16 13:19:41 +10001056 debug("setting up multiplex master socket");
1057
Damien Miller0e220db2004-06-15 10:34:08 +10001058 memset(&addr, '\0', sizeof(addr));
1059 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001060 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001061 strlen(options.control_path) + 1;
1062
1063 if (strlcpy(addr.sun_path, options.control_path,
1064 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1065 fatal("ControlPath too long");
1066
1067 if ((control_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001068 fatal("%s socket(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001069
1070 old_umask = umask(0177);
Damien Miller90967402006-03-26 14:07:26 +11001071 if (bind(control_fd, (struct sockaddr *)&addr, addr_len) == -1) {
Damien Miller0e220db2004-06-15 10:34:08 +10001072 control_fd = -1;
Damien Miller4f10e252005-05-04 15:33:09 +10001073 if (errno == EINVAL || errno == EADDRINUSE)
Damien Miller0e220db2004-06-15 10:34:08 +10001074 fatal("ControlSocket %s already exists",
1075 options.control_path);
1076 else
Damien Miller15d72a02005-11-05 15:07:33 +11001077 fatal("%s bind(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001078 }
1079 umask(old_umask);
1080
1081 if (listen(control_fd, 64) == -1)
Damien Miller15d72a02005-11-05 15:07:33 +11001082 fatal("%s listen(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001083
1084 set_nonblock(control_fd);
1085}
1086
Ben Lindstromf558cf62001-09-20 23:13:49 +00001087/* request pty/x11/agent/tcpfwd/shell for channel */
Ben Lindstrombba81212001-06-25 05:01:22 +00001088static void
Ben Lindstromf558cf62001-09-20 23:13:49 +00001089ssh_session2_setup(int id, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +10001090{
Damien Miller3756dce2004-06-18 01:17:29 +10001091 extern char **environ;
Damien Miller17e7ed02005-06-17 12:54:33 +10001092 const char *display;
Damien Miller0e220db2004-06-15 10:34:08 +10001093 int interactive = tty_flag;
Damien Miller17e7ed02005-06-17 12:54:33 +10001094
Damien Miller46d38de2005-07-17 17:02:09 +10001095 display = getenv("DISPLAY");
Damien Miller17e7ed02005-06-17 12:54:33 +10001096 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001097 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001098 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001099 client_x11_get_proto(display, options.xauth_location,
1100 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001101 /* Request forwarding with authentication spoofing. */
1102 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +10001103 x11_request_forwarding_with_spoofing(id, display, proto, data);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001104 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001105 /* XXX wait for reply */
1106 }
1107
Damien Miller0bc1bd82000-11-13 22:57:25 +11001108 check_agent_present();
1109 if (options.forward_agent) {
1110 debug("Requesting authentication agent forwarding.");
1111 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1112 packet_send();
1113 }
1114
Damien Miller7b58e802005-12-13 19:33:19 +11001115 if (options.tun_open != SSH_TUNMODE_NO) {
Damien Millerd27b9472005-12-13 19:29:02 +11001116 Channel *c;
1117 int fd;
1118
1119 debug("Requesting tun.");
Damien Miller7b58e802005-12-13 19:33:19 +11001120 if ((fd = tun_open(options.tun_local,
1121 options.tun_open)) >= 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11001122 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1123 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1124 0, "tun", 1);
1125 c->datagram = 1;
Damien Miller598bbc22005-12-31 16:33:36 +11001126#if defined(SSH_TUN_FILTER)
1127 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1128 channel_register_filter(c->self, sys_tun_infilter,
1129 sys_tun_outfilter);
1130#endif
Damien Millerd27b9472005-12-13 19:29:02 +11001131 packet_start(SSH2_MSG_CHANNEL_OPEN);
1132 packet_put_cstring("tun@openssh.com");
1133 packet_put_int(c->self);
1134 packet_put_int(c->local_window_max);
1135 packet_put_int(c->local_maxpacket);
Damien Miller7b58e802005-12-13 19:33:19 +11001136 packet_put_int(options.tun_open);
Damien Millerd27b9472005-12-13 19:29:02 +11001137 packet_put_int(options.tun_remote);
1138 packet_send();
1139 }
1140 }
1141
Damien Miller0e220db2004-06-15 10:34:08 +10001142 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
Damien Miller3756dce2004-06-18 01:17:29 +10001143 NULL, fileno(stdin), &command, environ, &ssh_subsystem_reply);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001144
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001145 packet_set_interactive(interactive);
Damien Miller1383bd82000-04-06 12:32:37 +10001146}
1147
Ben Lindstromf558cf62001-09-20 23:13:49 +00001148/* open new channel for a session */
Ben Lindstrombba81212001-06-25 05:01:22 +00001149static int
Ben Lindstromf558cf62001-09-20 23:13:49 +00001150ssh_session2_open(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001151{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001152 Channel *c;
1153 int window, packetmax, in, out, err;
Damien Millerad833b32000-08-23 10:46:23 +10001154
Damien Millercaf6dd62000-08-29 11:33:50 +11001155 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001156 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001157 } else {
1158 in = dup(STDIN_FILENO);
1159 }
1160 out = dup(STDOUT_FILENO);
1161 err = dup(STDERR_FILENO);
1162
1163 if (in < 0 || out < 0 || err < 0)
1164 fatal("dup() in/out/err failed");
1165
Damien Miller69b69aa2000-10-28 14:19:58 +11001166 /* enable nonblocking unless tty */
1167 if (!isatty(in))
1168 set_nonblock(in);
1169 if (!isatty(out))
1170 set_nonblock(out);
1171 if (!isatty(err))
1172 set_nonblock(err);
1173
Damien Millere4340be2000-09-16 13:29:08 +11001174 window = CHAN_SES_WINDOW_DEFAULT;
1175 packetmax = CHAN_SES_PACKET_DEFAULT;
Damien Miller19a59452002-02-19 15:20:57 +11001176 if (tty_flag) {
1177 window >>= 1;
1178 packetmax >>= 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001179 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001180 c = channel_new(
Damien Miller1383bd82000-04-06 12:32:37 +10001181 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001182 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001183 "client-session", /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001184
Ben Lindstromf558cf62001-09-20 23:13:49 +00001185 debug3("ssh_session2_open: channel_new: %d", c->self);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001186
Ben Lindstrom5ec26452001-06-09 00:18:51 +00001187 channel_send_open(c->self);
Ben Lindstromf558cf62001-09-20 23:13:49 +00001188 if (!no_shell_flag)
Damien Miller0e220db2004-06-15 10:34:08 +10001189 channel_register_confirm(c->self, ssh_session2_setup, NULL);
Damien Miller1383bd82000-04-06 12:32:37 +10001190
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001191 return c->self;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001192}
1193
Ben Lindstrombba81212001-06-25 05:01:22 +00001194static int
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001195ssh_session2(void)
1196{
Ben Lindstromf558cf62001-09-20 23:13:49 +00001197 int id = -1;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001198
1199 /* XXX should be pre-session */
1200 ssh_init_forwarding();
Damien Miller0e220db2004-06-15 10:34:08 +10001201 ssh_control_listener();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001202
Ben Lindstromf558cf62001-09-20 23:13:49 +00001203 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1204 id = ssh_session2_open();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001205
Damien Millerd27b9472005-12-13 19:29:02 +11001206 /* Execute a local command */
1207 if (options.local_command != NULL &&
1208 options.permit_local_command)
1209 ssh_local_cmd(options.local_command);
1210
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001211 /* If requested, let ssh continue in the background. */
1212 if (fork_after_authentication_flag)
1213 if (daemon(1, 1) < 0)
1214 fatal("daemon() failed: %.200s", strerror(errno));
1215
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001216 return client_loop(tty_flag, tty_flag ?
1217 options.escape_char : SSH_ESCAPECHAR_NONE, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001218}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001219
Ben Lindstrombba81212001-06-25 05:01:22 +00001220static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001221load_public_identity_files(void)
1222{
Damien Miller6b1d53c2006-03-31 23:13:21 +11001223 char *filename, *cp, thishost[NI_MAXHOST];
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001224 int i = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001225 Key *public;
Damien Miller6b1d53c2006-03-31 23:13:21 +11001226 struct passwd *pw;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001227#ifdef SMARTCARD
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001228 Key **keys;
1229
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001230 if (options.smartcard_device != NULL &&
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001231 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1232 (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL ) {
1233 int count = 0;
1234 for (i = 0; keys[i] != NULL; i++) {
1235 count++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001236 memmove(&options.identity_files[1], &options.identity_files[0],
1237 sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
1238 memmove(&options.identity_keys[1], &options.identity_keys[0],
1239 sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1240 options.num_identity_files++;
1241 options.identity_keys[0] = keys[i];
Damien Miller56a0bb02003-06-18 20:28:40 +10001242 options.identity_files[0] = sc_get_key_label(keys[i]);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001243 }
Ben Lindstrom4f054602002-03-26 03:23:00 +00001244 if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1245 options.num_identity_files = SSH_MAX_IDENTITY_FILES;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001246 i = count;
1247 xfree(keys);
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001248 }
Ben Lindstromffce1472001-08-06 21:57:31 +00001249#endif /* SMARTCARD */
Damien Miller6b1d53c2006-03-31 23:13:21 +11001250 if ((pw = getpwuid(original_real_uid)) == NULL)
1251 fatal("load_public_identity_files: getpwuid failed");
1252 if (gethostname(thishost, sizeof(thishost)) == -1)
1253 fatal("load_public_identity_files: gethostname: %s",
1254 strerror(errno));
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001255 for (; i < options.num_identity_files; i++) {
Damien Miller6b1d53c2006-03-31 23:13:21 +11001256 cp = tilde_expand_filename(options.identity_files[i],
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001257 original_real_uid);
Damien Miller6b1d53c2006-03-31 23:13:21 +11001258 filename = percent_expand(cp, "d", pw->pw_dir,
1259 "u", pw->pw_name, "l", thishost, "h", host,
1260 "r", options.user, (char *)NULL);
1261 xfree(cp);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001262 public = key_load_public(filename, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001263 debug("identity file %s type %d", filename,
1264 public ? public->type : -1);
1265 xfree(options.identity_files[i]);
1266 options.identity_files[i] = filename;
1267 options.identity_keys[i] = public;
1268 }
1269}
Damien Miller0e220db2004-06-15 10:34:08 +10001270
1271static void
1272control_client_sighandler(int signo)
1273{
1274 control_client_terminate = signo;
1275}
1276
1277static void
1278control_client_sigrelay(int signo)
1279{
1280 if (control_server_pid > 1)
1281 kill(control_server_pid, signo);
1282}
1283
Darren Tucker365433f2004-06-22 12:29:23 +10001284static int
1285env_permitted(char *env)
1286{
Darren Tucker284706a2006-07-12 22:16:23 +10001287 int i, ret;
Darren Tucker365433f2004-06-22 12:29:23 +10001288 char name[1024], *cp;
1289
Darren Tucker284706a2006-07-12 22:16:23 +10001290 if ((cp = strchr(env, '=')) == NULL || cp == env)
Darren Tucker365433f2004-06-22 12:29:23 +10001291 return (0);
Darren Tucker57f42242006-07-12 22:23:35 +10001292 ret = snprintf(name, sizeof(name), "%.*s", (int)(cp - env), env);
Darren Tucker284706a2006-07-12 22:16:23 +10001293 if (ret <= 0 || (size_t)ret >= sizeof(name))
1294 fatal("env_permitted: name '%.100s...' too long", env);
Darren Tucker365433f2004-06-22 12:29:23 +10001295
1296 for (i = 0; i < options.num_send_env; i++)
1297 if (match_pattern(name, options.send_env[i]))
1298 return (1);
1299
1300 return (0);
1301}
1302
Damien Miller0e220db2004-06-15 10:34:08 +10001303static void
1304control_client(const char *path)
1305{
1306 struct sockaddr_un addr;
Darren Tucker39207a42004-11-05 20:19:51 +11001307 int i, r, fd, sock, exitval, num_env, addr_len;
Damien Miller0e220db2004-06-15 10:34:08 +10001308 Buffer m;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001309 char *term;
Damien Miller3756dce2004-06-18 01:17:29 +10001310 extern char **environ;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001311 u_int flags;
Darren Tuckerfc959702004-07-17 16:12:08 +10001312
Damien Millerd14b1e72005-06-16 13:19:41 +10001313 if (mux_command == 0)
1314 mux_command = SSHMUX_COMMAND_OPEN;
1315
1316 switch (options.control_master) {
1317 case SSHCTL_MASTER_AUTO:
1318 case SSHCTL_MASTER_AUTO_ASK:
1319 debug("auto-mux: Trying existing master");
1320 /* FALLTHROUGH */
1321 case SSHCTL_MASTER_NO:
1322 break;
1323 default:
1324 return;
1325 }
1326
Damien Miller0e220db2004-06-15 10:34:08 +10001327 memset(&addr, '\0', sizeof(addr));
1328 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001329 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001330 strlen(path) + 1;
1331
1332 if (strlcpy(addr.sun_path, path,
1333 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1334 fatal("ControlPath too long");
1335
1336 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1337 fatal("%s socket(): %s", __func__, strerror(errno));
1338
Damien Miller90967402006-03-26 14:07:26 +11001339 if (connect(sock, (struct sockaddr *)&addr, addr_len) == -1) {
Darren Tucker0814d312005-06-01 23:08:51 +10001340 if (mux_command != SSHMUX_COMMAND_OPEN) {
1341 fatal("Control socket connect(%.100s): %s", path,
1342 strerror(errno));
1343 }
Damien Miller538c9b72005-05-26 12:11:28 +10001344 if (errno == ENOENT)
Damien Miller4662d342006-03-26 13:59:59 +11001345 debug("Control socket \"%.100s\" does not exist", path);
Damien Miller538c9b72005-05-26 12:11:28 +10001346 else {
Damien Miller4662d342006-03-26 13:59:59 +11001347 error("Control socket connect(%.100s): %s", path,
Damien Miller538c9b72005-05-26 12:11:28 +10001348 strerror(errno));
1349 }
Damien Miller4662d342006-03-26 13:59:59 +11001350 close(sock);
1351 return;
1352 }
Damien Miller46d38de2005-07-17 17:02:09 +10001353
Damien Miller4662d342006-03-26 13:59:59 +11001354 if (stdin_null_flag) {
1355 if ((fd = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1356 fatal("open(/dev/null): %s", strerror(errno));
1357 if (dup2(fd, STDIN_FILENO) == -1)
1358 fatal("dup2: %s", strerror(errno));
1359 if (fd > STDERR_FILENO)
1360 close(fd);
1361 }
Damien Miller46d38de2005-07-17 17:02:09 +10001362
Damien Miller13390022005-07-06 09:44:19 +10001363 term = getenv("TERM");
Darren Tucker7ebfc102004-11-07 20:06:19 +11001364
1365 flags = 0;
1366 if (tty_flag)
1367 flags |= SSHMUX_FLAG_TTY;
1368 if (subsystem_flag)
1369 flags |= SSHMUX_FLAG_SUBSYS;
Damien Miller13390022005-07-06 09:44:19 +10001370 if (options.forward_x11)
1371 flags |= SSHMUX_FLAG_X11_FWD;
1372 if (options.forward_agent)
1373 flags |= SSHMUX_FLAG_AGENT_FWD;
Damien Miller0e220db2004-06-15 10:34:08 +10001374
Damien Miller0e220db2004-06-15 10:34:08 +10001375 buffer_init(&m);
1376
Darren Tucker7ebfc102004-11-07 20:06:19 +11001377 /* Send our command to server */
1378 buffer_put_int(&m, mux_command);
1379 buffer_put_int(&m, flags);
Damien Miller13390022005-07-06 09:44:19 +10001380 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001381 fatal("%s: msg_send", __func__);
1382 buffer_clear(&m);
1383
1384 /* Get authorisation status and PID of controlee */
Damien Miller0e220db2004-06-15 10:34:08 +10001385 if (ssh_msg_recv(sock, &m) == -1)
1386 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001387 if (buffer_get_char(&m) != SSHMUX_VER)
Damien Miller0e220db2004-06-15 10:34:08 +10001388 fatal("%s: wrong version", __func__);
Damien Miller23f07702004-06-18 01:19:03 +10001389 if (buffer_get_int(&m) != 1)
1390 fatal("Connection to master denied");
Damien Miller0e220db2004-06-15 10:34:08 +10001391 control_server_pid = buffer_get_int(&m);
1392
Damien Miller0e220db2004-06-15 10:34:08 +10001393 buffer_clear(&m);
Damien Miller0e220db2004-06-15 10:34:08 +10001394
Darren Tucker7ebfc102004-11-07 20:06:19 +11001395 switch (mux_command) {
1396 case SSHMUX_COMMAND_ALIVE_CHECK:
Darren Tucker47eede72005-03-14 23:08:12 +11001397 fprintf(stderr, "Master running (pid=%d)\r\n",
Darren Tucker7ebfc102004-11-07 20:06:19 +11001398 control_server_pid);
1399 exit(0);
1400 case SSHMUX_COMMAND_TERMINATE:
1401 fprintf(stderr, "Exit request sent.\r\n");
1402 exit(0);
1403 case SSHMUX_COMMAND_OPEN:
1404 /* continue below */
1405 break;
1406 default:
1407 fatal("silly mux_command %d", mux_command);
1408 }
1409
1410 /* SSHMUX_COMMAND_OPEN */
Damien Miller13390022005-07-06 09:44:19 +10001411 buffer_put_cstring(&m, term ? term : "");
Damien Miller0e220db2004-06-15 10:34:08 +10001412 buffer_append(&command, "\0", 1);
1413 buffer_put_cstring(&m, buffer_ptr(&command));
1414
Darren Tucker365433f2004-06-22 12:29:23 +10001415 if (options.num_send_env == 0 || environ == NULL) {
1416 buffer_put_int(&m, 0);
Darren Tuckerfc959702004-07-17 16:12:08 +10001417 } else {
Darren Tucker365433f2004-06-22 12:29:23 +10001418 /* Pass environment */
1419 num_env = 0;
1420 for (i = 0; environ[i] != NULL; i++)
1421 if (env_permitted(environ[i]))
1422 num_env++; /* Count */
Darren Tuckerfc959702004-07-17 16:12:08 +10001423
Darren Tucker365433f2004-06-22 12:29:23 +10001424 buffer_put_int(&m, num_env);
1425
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001426 for (i = 0; environ[i] != NULL && num_env >= 0; i++)
1427 if (env_permitted(environ[i])) {
1428 num_env--;
Darren Tucker365433f2004-06-22 12:29:23 +10001429 buffer_put_cstring(&m, environ[i]);
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001430 }
Darren Tucker365433f2004-06-22 12:29:23 +10001431 }
Damien Miller3756dce2004-06-18 01:17:29 +10001432
Damien Miller13390022005-07-06 09:44:19 +10001433 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Damien Miller0e220db2004-06-15 10:34:08 +10001434 fatal("%s: msg_send", __func__);
1435
1436 mm_send_fd(sock, STDIN_FILENO);
1437 mm_send_fd(sock, STDOUT_FILENO);
1438 mm_send_fd(sock, STDERR_FILENO);
1439
1440 /* Wait for reply, so master has a chance to gather ttymodes */
1441 buffer_clear(&m);
1442 if (ssh_msg_recv(sock, &m) == -1)
1443 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001444 if (buffer_get_char(&m) != SSHMUX_VER)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001445 fatal("%s: wrong version", __func__);
Damien Miller0e220db2004-06-15 10:34:08 +10001446 buffer_free(&m);
1447
Darren Tucker07336da2004-11-05 20:02:16 +11001448 signal(SIGHUP, control_client_sighandler);
Damien Miller0809e232004-06-18 22:20:57 +10001449 signal(SIGINT, control_client_sighandler);
1450 signal(SIGTERM, control_client_sighandler);
1451 signal(SIGWINCH, control_client_sigrelay);
1452
Damien Miller0e220db2004-06-15 10:34:08 +10001453 if (tty_flag)
1454 enter_raw_mode();
1455
1456 /* Stick around until the controlee closes the client_fd */
1457 exitval = 0;
1458 for (;!control_client_terminate;) {
1459 r = read(sock, &exitval, sizeof(exitval));
1460 if (r == 0) {
1461 debug2("Received EOF from master");
1462 break;
1463 }
1464 if (r > 0)
1465 debug2("Received exit status from master %d", exitval);
1466 if (r == -1 && errno != EINTR)
1467 fatal("%s: read %s", __func__, strerror(errno));
1468 }
1469
1470 if (control_client_terminate)
1471 debug2("Exiting on signal %d", control_client_terminate);
1472
1473 close(sock);
1474
1475 leave_raw_mode();
1476
1477 if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
1478 fprintf(stderr, "Connection to master closed.\r\n");
1479
1480 exit(exitval);
1481}