blob: 6f185b4a7ef37f16ae211b1f934febcd7e29fbe5 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * Ssh client program. This program can be used to log into a remote machine.
6 * The software supports strong authentication, encryption, and forwarding
7 * of X11, TCP/IP, and authentication connections.
8 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
14 *
15 * Copyright (c) 1999 Niels Provos. All rights reserved.
Darren Tucker0a118da2003-10-15 15:54:32 +100016 * Copyright (c) 2000, 2001, 2002, 2003 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110017 *
18 * Modified to work with SSL by Niels Provos <provos@citi.umich.edu>
19 * in Canada (German citizen).
20 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110040 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
42#include "includes.h"
Damien Miller2ecb6bd2006-03-15 12:03:53 +110043RCSID("$OpenBSD: ssh.c,v 1.266 2006/03/12 04:23:07 djm Exp $");
Damien Millercd4223c2006-03-15 11:22:47 +110044
Damien Millerf17883e2006-03-15 11:45:54 +110045#include <sys/types.h>
46#ifdef HAVE_SYS_STAT_H
47# include <sys/stat.h>
48#endif
Damien Millercd4223c2006-03-15 11:22:47 +110049#include <sys/resource.h>
Damien Miller17e91c02006-03-15 11:28:34 +110050#include <sys/ioctl.h>
Damien Miller574c41f2006-03-15 11:40:10 +110051#include <sys/un.h>
Damien Miller03e20032006-03-15 11:16:59 +110052
Damien Millerc7b06362006-03-15 11:53:45 +110053#include <ctype.h>
Damien Miller03e20032006-03-15 11:16:59 +110054#include <paths.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110055#include <signal.h>
Damien Millereba71ba2000-04-29 23:57:08 +100056
57#include <openssl/evp.h>
Damien Miller0bc1bd82000-11-13 22:57:25 +110058#include <openssl/err.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000061#include "ssh1.h"
Damien Miller1383bd82000-04-06 12:32:37 +100062#include "ssh2.h"
63#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000064#include "cipher.h"
65#include "xmalloc.h"
66#include "packet.h"
67#include "buffer.h"
Damien Miller0e220db2004-06-15 10:34:08 +100068#include "bufaux.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000069#include "channels.h"
Damien Millereba71ba2000-04-29 23:57:08 +100070#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100071#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100072#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000073#include "pathnames.h"
Damien Miller0e220db2004-06-15 10:34:08 +100074#include "dispatch.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +000075#include "clientloop.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000076#include "log.h"
77#include "readconf.h"
78#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000079#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000080#include "kex.h"
81#include "mac.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +100082#include "sshpty.h"
Darren Tucker46bc0752004-05-02 22:11:30 +100083#include "match.h"
Damien Miller0e220db2004-06-15 10:34:08 +100084#include "msg.h"
85#include "monitor_fdpass.h"
Darren Tucker25f60a72004-08-15 17:23:34 +100086#include "uidswap.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087
Ben Lindstromc5b68002001-07-04 04:52:03 +000088#ifdef SMARTCARD
Ben Lindstromc5b68002001-07-04 04:52:03 +000089#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000090#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +000091
Damien Miller3f905871999-11-15 17:10:57 +110092extern char *__progname;
Damien Miller3f905871999-11-15 17:10:57 +110093
Damien Miller95def091999-11-25 00:26:21 +110094/* Flag indicating whether debug mode is on. This can be set on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095int debug_flag = 0;
96
Damien Miller78928792000-04-12 20:17:38 +100097/* Flag indicating whether a tty should be allocated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098int tty_flag = 0;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +000099int no_tty_flag = 0;
100int force_tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101
Damien Miller1383bd82000-04-06 12:32:37 +1000102/* don't exec a shell */
103int no_shell_flag = 0;
Damien Miller1383bd82000-04-06 12:32:37 +1000104
Damien Miller5428f641999-11-25 11:54:57 +1100105/*
106 * Flag indicating that nothing should be read from stdin. This can be set
107 * on the command line.
108 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109int stdin_null_flag = 0;
110
Damien Miller5428f641999-11-25 11:54:57 +1100111/*
112 * Flag indicating that ssh should fork after authentication. This is useful
Ben Lindstromf666fec2002-06-06 19:51:58 +0000113 * so that the passphrase can be entered manually, and then ssh goes to the
Damien Miller5428f641999-11-25 11:54:57 +1100114 * background.
115 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116int fork_after_authentication_flag = 0;
117
Damien Miller5428f641999-11-25 11:54:57 +1100118/*
119 * General data structure for command line options and options configurable
120 * in configuration files. See readconf.h.
121 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122Options options;
123
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000124/* optional user configfile */
125char *config = NULL;
126
Damien Miller5428f641999-11-25 11:54:57 +1100127/*
128 * Name of the host we are connecting to. This is the name given on the
129 * command line, or the HostName specified for the user-supplied name in a
130 * configuration file.
131 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132char *host;
133
134/* socket address the host resolves to */
Damien Miller34132e52000-01-14 15:45:46 +1100135struct sockaddr_storage hostaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000137/* Private host keys. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000138Sensitive sensitive_data;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139
140/* Original real UID. */
141uid_t original_real_uid;
Ben Lindstromf9c48842002-06-11 16:37:51 +0000142uid_t original_effective_uid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143
Damien Miller1383bd82000-04-06 12:32:37 +1000144/* command to be executed */
145Buffer command;
146
Damien Miller832562e2001-01-30 09:30:01 +1100147/* Should we execute a command or invoke a subsystem? */
148int subsystem_flag = 0;
149
Damien Miller2797f7f2002-04-23 21:09:44 +1000150/* # of replies received for global requests */
151static int client_global_request_id = 0;
152
Damien Miller8c4e18a2002-09-19 12:05:02 +1000153/* pid of proxycommand child process */
154pid_t proxy_command_pid = 0;
155
Damien Miller0e220db2004-06-15 10:34:08 +1000156/* fd to control socket */
157int control_fd = -1;
158
Darren Tucker7ebfc102004-11-07 20:06:19 +1100159/* Multiplexing control command */
Darren Tucker0814d312005-06-01 23:08:51 +1000160static u_int mux_command = 0;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100161
Damien Miller0e220db2004-06-15 10:34:08 +1000162/* Only used in control client mode */
163volatile sig_atomic_t control_client_terminate = 0;
164u_int control_server_pid = 0;
165
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000166/* Prints a help message to the user. This function never returns. */
167
Ben Lindstrombba81212001-06-25 05:01:22 +0000168static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000169usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170{
Damien Miller50955102004-03-22 09:34:58 +1100171 fprintf(stderr,
Darren Tucker9c6bf322004-12-03 14:10:19 +1100172"usage: ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n"
Darren Tucker895d6982005-10-03 18:18:05 +1000173" [-D [bind_address:]port] [-e escape_char] [-F configfile]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100174" [-i identity_file] [-L [bind_address:]port:host:hostport]\n"
Damien Millerf91ee4c2005-03-01 21:24:33 +1100175" [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100176" [-R [bind_address:]port:host:hostport] [-S ctl_path]\n"
Damien Millerd27b9472005-12-13 19:29:02 +1100177" [-w tunnel:tunnel] [user@]hostname [command]\n"
Damien Miller50955102004-03-22 09:34:58 +1100178 );
Darren Tuckere9a9b712005-12-20 16:15:51 +1100179 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000180}
181
Ben Lindstrombba81212001-06-25 05:01:22 +0000182static int ssh_session(void);
183static int ssh_session2(void);
184static void load_public_identity_files(void);
Damien Miller0e220db2004-06-15 10:34:08 +1000185static void control_client(const char *path);
Damien Miller1383bd82000-04-06 12:32:37 +1000186
Damien Miller95def091999-11-25 00:26:21 +1100187/*
188 * Main program for the ssh client.
189 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000190int
191main(int ac, char **av)
192{
Ben Lindstrom24157572002-06-12 16:09:39 +0000193 int i, opt, exit_status;
Damien Miller9836cf82003-12-17 16:30:06 +1100194 char *p, *cp, *line, buf[256];
Damien Miller95def091999-11-25 00:26:21 +1100195 struct stat st;
Ben Lindstrom086cf212001-03-05 05:56:40 +0000196 struct passwd *pw;
Damien Miller1383bd82000-04-06 12:32:37 +1000197 int dummy;
Ben Lindstrom5ccf63a2001-09-24 20:00:10 +0000198 extern int optind, optreset;
Damien Miller4f8e6692001-07-14 13:22:53 +1000199 extern char *optarg;
Damien Miller9651fe62005-06-26 08:55:25 +1000200 struct servent *sp;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100201 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202
Darren Tuckerce321d82005-10-03 18:11:24 +1000203 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
204 sanitise_stdfd();
205
Damien Miller59d3d5b2003-08-22 09:34:41 +1000206 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000207 init_rng();
208
Damien Miller5428f641999-11-25 11:54:57 +1100209 /*
210 * Save the original real uid. It will be needed later (uid-swapping
211 * may clobber the real uid).
212 */
Damien Miller95def091999-11-25 00:26:21 +1100213 original_real_uid = getuid();
214 original_effective_uid = geteuid();
Damien Millera8e06ce2003-11-21 23:48:55 +1100215
Damien Miller50b9a602002-09-04 16:50:06 +1000216 /*
217 * Use uid-swapping to give up root privileges for the duration of
218 * option processing. We will re-instantiate the rights when we are
219 * ready to create the privileged port, and will permanently drop
220 * them when the port has been created (actually, when the connection
221 * has been made, as we may need to create the port several times).
222 */
223 PRIV_END;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000224
Damien Miller05dd7952000-10-01 00:42:48 +1100225#ifdef HAVE_SETRLIMIT
Damien Miller95def091999-11-25 00:26:21 +1100226 /* If we are installed setuid root be careful to not drop core. */
227 if (original_real_uid != original_effective_uid) {
228 struct rlimit rlim;
229 rlim.rlim_cur = rlim.rlim_max = 0;
230 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
231 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000232 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100233#endif
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000234 /* Get user data. */
235 pw = getpwuid(original_real_uid);
236 if (!pw) {
Damien Miller996acd22003-04-09 20:59:48 +1000237 logit("You don't exist, go away!");
Darren Tuckere9a9b712005-12-20 16:15:51 +1100238 exit(255);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000239 }
240 /* Take a copy of the returned structure. */
241 pw = pwcopy(pw);
242
Damien Miller5428f641999-11-25 11:54:57 +1100243 /*
Damien Miller5428f641999-11-25 11:54:57 +1100244 * Set our umask to something reasonable, as some files are created
245 * with the default umask. This will make them world-readable but
246 * writable only by the owner, which is ok for all files for which we
247 * don't set the modes explicitly.
248 */
Damien Miller95def091999-11-25 00:26:21 +1100249 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250
Damien Miller95def091999-11-25 00:26:21 +1100251 /* Initialize option structure to indicate that no values have been set. */
252 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000253
Damien Miller95def091999-11-25 00:26:21 +1100254 /* Parse command-line arguments. */
255 host = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000256
Damien Miller2ecb6bd2006-03-15 12:03:53 +1100257 again:
Damien Millerf4614452001-07-14 12:18:10 +1000258 while ((opt = getopt(ac, av,
Damien Millerd27b9472005-12-13 19:29:02 +1100259 "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 +1100260 switch (opt) {
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000261 case '1':
262 options.protocol = SSH_PROTO_1;
263 break;
Damien Miller4af51302000-04-16 11:18:38 +1000264 case '2':
265 options.protocol = SSH_PROTO_2;
266 break;
Damien Miller34132e52000-01-14 15:45:46 +1100267 case '4':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000268 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100269 break;
Damien Miller34132e52000-01-14 15:45:46 +1100270 case '6':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000271 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100272 break;
Damien Miller95def091999-11-25 00:26:21 +1100273 case 'n':
274 stdin_null_flag = 1;
275 break;
Damien Miller95def091999-11-25 00:26:21 +1100276 case 'f':
277 fork_after_authentication_flag = 1;
278 stdin_null_flag = 1;
279 break;
Damien Miller95def091999-11-25 00:26:21 +1100280 case 'x':
281 options.forward_x11 = 0;
282 break;
Damien Miller95def091999-11-25 00:26:21 +1100283 case 'X':
284 options.forward_x11 = 1;
285 break;
Darren Tucker0a118da2003-10-15 15:54:32 +1000286 case 'Y':
287 options.forward_x11 = 1;
288 options.forward_x11_trusted = 1;
289 break;
Damien Miller95def091999-11-25 00:26:21 +1100290 case 'g':
291 options.gateway_ports = 1;
292 break;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100293 case 'O':
294 if (strcmp(optarg, "check") == 0)
295 mux_command = SSHMUX_COMMAND_ALIVE_CHECK;
296 else if (strcmp(optarg, "exit") == 0)
297 mux_command = SSHMUX_COMMAND_TERMINATE;
298 else
299 fatal("Invalid multiplex command.");
300 break;
Damien Miller147bba32002-09-04 16:46:06 +1000301 case 'P': /* deprecated */
Damien Miller95def091999-11-25 00:26:21 +1100302 options.use_privileged_port = 0;
303 break;
Damien Miller95def091999-11-25 00:26:21 +1100304 case 'a':
305 options.forward_agent = 0;
306 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000307 case 'A':
308 options.forward_agent = 1;
309 break;
Damien Miller95def091999-11-25 00:26:21 +1100310 case 'k':
Damien Millere0113cc2003-11-24 13:10:09 +1100311 options.gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100312 break;
Damien Miller95def091999-11-25 00:26:21 +1100313 case 'i':
314 if (stat(optarg, &st) < 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000315 fprintf(stderr, "Warning: Identity file %s "
Damien Miller3eb48b62005-03-01 21:15:46 +1100316 "not accessible: %s.\n", optarg,
317 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100318 break;
319 }
Damien Millerf4614452001-07-14 12:18:10 +1000320 if (options.num_identity_files >=
321 SSH_MAX_IDENTITY_FILES)
322 fatal("Too many identity files specified "
323 "(max %d)", SSH_MAX_IDENTITY_FILES);
324 options.identity_files[options.num_identity_files++] =
325 xstrdup(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100326 break;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000327 case 'I':
328#ifdef SMARTCARD
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000329 options.smartcard_device = xstrdup(optarg);
Ben Lindstrombcc18082001-08-06 21:59:25 +0000330#else
Ben Lindstromc5b68002001-07-04 04:52:03 +0000331 fprintf(stderr, "no support for smartcards.\n");
Ben Lindstrombcc18082001-08-06 21:59:25 +0000332#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000333 break;
Damien Miller95def091999-11-25 00:26:21 +1100334 case 't':
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000335 if (tty_flag)
336 force_tty_flag = 1;
Damien Miller95def091999-11-25 00:26:21 +1100337 tty_flag = 1;
338 break;
Damien Miller95def091999-11-25 00:26:21 +1100339 case 'v':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000340 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100341 debug_flag = 1;
342 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000343 } else {
344 if (options.log_level < SYSLOG_LEVEL_DEBUG3)
345 options.log_level++;
Damien Millere4340be2000-09-16 13:29:08 +1100346 break;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000347 }
Darren Tuckere9bf9842004-11-05 20:05:32 +1100348 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100349 case 'V':
Damien Miller0c889cd2004-03-22 09:36:00 +1100350 fprintf(stderr, "%s, %s\n",
Damien Miller2aa6d3c2004-09-12 16:53:04 +1000351 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
Damien Miller95def091999-11-25 00:26:21 +1100352 if (opt == 'V')
353 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100354 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100355 case 'w':
Damien Miller7b58e802005-12-13 19:33:19 +1100356 if (options.tun_open == -1)
357 options.tun_open = SSH_TUNMODE_DEFAULT;
Damien Millerd27b9472005-12-13 19:29:02 +1100358 options.tun_local = a2tun(optarg, &options.tun_remote);
Damien Miller7b58e802005-12-13 19:33:19 +1100359 if (options.tun_local == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100360 fprintf(stderr, "Bad tun device '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100361 exit(255);
Damien Millerd27b9472005-12-13 19:29:02 +1100362 }
363 break;
Damien Miller95def091999-11-25 00:26:21 +1100364 case 'q':
365 options.log_level = SYSLOG_LEVEL_QUIET;
366 break;
Damien Miller95def091999-11-25 00:26:21 +1100367 case 'e':
368 if (optarg[0] == '^' && optarg[2] == 0 &&
Damien Millerf4614452001-07-14 12:18:10 +1000369 (u_char) optarg[1] >= 64 &&
370 (u_char) optarg[1] < 128)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000371 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100372 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000373 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100374 else if (strcmp(optarg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000375 options.escape_char = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100376 else {
Damien Millerf4614452001-07-14 12:18:10 +1000377 fprintf(stderr, "Bad escape character '%s'.\n",
378 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100379 exit(255);
Damien Miller95def091999-11-25 00:26:21 +1100380 }
381 break;
Damien Miller95def091999-11-25 00:26:21 +1100382 case 'c':
Damien Millereba71ba2000-04-29 23:57:08 +1000383 if (ciphers_valid(optarg)) {
384 /* SSH2 only */
385 options.ciphers = xstrdup(optarg);
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000386 options.cipher = SSH_CIPHER_INVALID;
Damien Millereba71ba2000-04-29 23:57:08 +1000387 } else {
388 /* SSH1 only */
Damien Millere39cacc2000-11-29 12:18:44 +1100389 options.cipher = cipher_number(optarg);
390 if (options.cipher == -1) {
Damien Millerf4614452001-07-14 12:18:10 +1000391 fprintf(stderr,
392 "Unknown cipher type '%s'\n",
393 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100394 exit(255);
Damien Millereba71ba2000-04-29 23:57:08 +1000395 }
Damien Millerf4614452001-07-14 12:18:10 +1000396 if (options.cipher == SSH_CIPHER_3DES)
Damien Millere39cacc2000-11-29 12:18:44 +1100397 options.ciphers = "3des-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000398 else if (options.cipher == SSH_CIPHER_BLOWFISH)
Damien Millere39cacc2000-11-29 12:18:44 +1100399 options.ciphers = "blowfish-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000400 else
Damien Millere39cacc2000-11-29 12:18:44 +1100401 options.ciphers = (char *)-1;
Damien Miller95def091999-11-25 00:26:21 +1100402 }
403 break;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000404 case 'm':
405 if (mac_valid(optarg))
406 options.macs = xstrdup(optarg);
407 else {
Damien Millerf4614452001-07-14 12:18:10 +1000408 fprintf(stderr, "Unknown mac type '%s'\n",
409 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100410 exit(255);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000411 }
412 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000413 case 'M':
Damien Millerd14b1e72005-06-16 13:19:41 +1000414 if (options.control_master == SSHCTL_MASTER_YES)
415 options.control_master = SSHCTL_MASTER_ASK;
416 else
417 options.control_master = SSHCTL_MASTER_YES;
Damien Miller0e220db2004-06-15 10:34:08 +1000418 break;
Damien Miller95def091999-11-25 00:26:21 +1100419 case 'p':
Ben Lindstrom19066a12001-04-12 23:39:26 +0000420 options.port = a2port(optarg);
421 if (options.port == 0) {
Ben Lindstrom146edb92001-04-11 23:06:28 +0000422 fprintf(stderr, "Bad port '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100423 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000424 }
Damien Miller95def091999-11-25 00:26:21 +1100425 break;
Damien Miller95def091999-11-25 00:26:21 +1100426 case 'l':
427 options.user = optarg;
428 break;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000429
Damien Miller95def091999-11-25 00:26:21 +1100430 case 'L':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100431 if (parse_forward(&fwd, optarg))
432 add_local_forward(&options, &fwd);
433 else {
Damien Millerf4614452001-07-14 12:18:10 +1000434 fprintf(stderr,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100435 "Bad local forwarding specification '%s'\n",
Damien Millerf4614452001-07-14 12:18:10 +1000436 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100437 exit(255);
Ben Lindstrom1a174712001-09-12 17:56:15 +0000438 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100439 break;
440
441 case 'R':
442 if (parse_forward(&fwd, optarg)) {
443 add_remote_forward(&options, &fwd);
444 } else {
445 fprintf(stderr,
446 "Bad remote forwarding specification "
447 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100448 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100449 }
Damien Miller95def091999-11-25 00:26:21 +1100450 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000451
452 case 'D':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100453 cp = p = xstrdup(optarg);
454 memset(&fwd, '\0', sizeof(fwd));
455 fwd.connect_host = "socks";
456 if ((fwd.listen_host = hpdelim(&cp)) == NULL) {
457 fprintf(stderr, "Bad dynamic forwarding "
458 "specification '%.100s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100459 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100460 }
461 if (cp != NULL) {
462 fwd.listen_port = a2port(cp);
463 fwd.listen_host = cleanhostname(fwd.listen_host);
464 } else {
465 fwd.listen_port = a2port(fwd.listen_host);
Damien Millerbe1045d2005-08-12 22:10:56 +1000466 fwd.listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100467 }
468
469 if (fwd.listen_port == 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000470 fprintf(stderr, "Bad dynamic port '%s'\n",
471 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100472 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000473 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100474 add_local_forward(&options, &fwd);
475 xfree(p);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000476 break;
477
Damien Miller95def091999-11-25 00:26:21 +1100478 case 'C':
479 options.compression = 1;
480 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000481 case 'N':
482 no_shell_flag = 1;
483 no_tty_flag = 1;
484 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000485 case 'T':
486 no_tty_flag = 1;
487 break;
Damien Miller95def091999-11-25 00:26:21 +1100488 case 'o':
489 dummy = 1;
Damien Miller9836cf82003-12-17 16:30:06 +1100490 line = xstrdup(optarg);
Damien Millerf4614452001-07-14 12:18:10 +1000491 if (process_config_line(&options, host ? host : "",
Damien Miller9836cf82003-12-17 16:30:06 +1100492 line, "command-line", 0, &dummy) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100493 exit(255);
Damien Miller9836cf82003-12-17 16:30:06 +1100494 xfree(line);
Damien Miller95def091999-11-25 00:26:21 +1100495 break;
Damien Miller832562e2001-01-30 09:30:01 +1100496 case 's':
497 subsystem_flag = 1;
498 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000499 case 'S':
500 if (options.control_path != NULL)
501 free(options.control_path);
502 options.control_path = xstrdup(optarg);
Damien Miller0e220db2004-06-15 10:34:08 +1000503 break;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000504 case 'b':
505 options.bind_address = optarg;
506 break;
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000507 case 'F':
508 config = optarg;
509 break;
Damien Miller95def091999-11-25 00:26:21 +1100510 default:
511 usage();
512 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000513 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000514
Damien Millerf4614452001-07-14 12:18:10 +1000515 ac -= optind;
516 av += optind;
517
518 if (ac > 0 && !host && **av != '-') {
Ben Lindstromc276c122002-12-23 02:14:51 +0000519 if (strrchr(*av, '@')) {
Damien Millerf4614452001-07-14 12:18:10 +1000520 p = xstrdup(*av);
Ben Lindstromc276c122002-12-23 02:14:51 +0000521 cp = strrchr(p, '@');
Damien Millerf4614452001-07-14 12:18:10 +1000522 if (cp == NULL || cp == p)
523 usage();
524 options.user = p;
525 *cp = '\0';
526 host = ++cp;
527 } else
528 host = *av;
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000529 if (ac > 1) {
530 optind = optreset = 1;
Damien Millerf4614452001-07-14 12:18:10 +1000531 goto again;
532 }
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000533 ac--, av++;
Damien Millerf4614452001-07-14 12:18:10 +1000534 }
535
Damien Miller95def091999-11-25 00:26:21 +1100536 /* Check that we got a host name. */
537 if (!host)
538 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000539
Damien Millercb5e44a2000-09-29 12:12:36 +1100540 SSLeay_add_all_algorithms();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100541 ERR_load_crypto_strings();
Damien Millercb5e44a2000-09-29 12:12:36 +1100542
Damien Miller95def091999-11-25 00:26:21 +1100543 /* Initialize the command to execute on remote host. */
544 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000545
Damien Miller5428f641999-11-25 11:54:57 +1100546 /*
547 * Save the command to execute on the remote host in a buffer. There
548 * is no limit on the length of the command, except by the maximum
549 * packet size. Also sets the tty flag if there is no command.
550 */
Damien Millerf4614452001-07-14 12:18:10 +1000551 if (!ac) {
Damien Miller95def091999-11-25 00:26:21 +1100552 /* No command specified - execute shell on a tty. */
553 tty_flag = 1;
Damien Miller832562e2001-01-30 09:30:01 +1100554 if (subsystem_flag) {
Damien Millerf4614452001-07-14 12:18:10 +1000555 fprintf(stderr,
556 "You must specify a subsystem to invoke.\n");
Damien Miller832562e2001-01-30 09:30:01 +1100557 usage();
558 }
Damien Miller95def091999-11-25 00:26:21 +1100559 } else {
Damien Millerf4614452001-07-14 12:18:10 +1000560 /* A command has been specified. Store it into the buffer. */
561 for (i = 0; i < ac; i++) {
562 if (i)
Damien Miller95def091999-11-25 00:26:21 +1100563 buffer_append(&command, " ", 1);
564 buffer_append(&command, av[i], strlen(av[i]));
565 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000566 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000567
Damien Miller95def091999-11-25 00:26:21 +1100568 /* Cannot fork to background if no command. */
Damien Millercaf6dd62000-08-29 11:33:50 +1100569 if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
Damien Miller95def091999-11-25 00:26:21 +1100570 fatal("Cannot fork into background without a command to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000571
Damien Miller95def091999-11-25 00:26:21 +1100572 /* Allocate a tty by default if no command specified. */
573 if (buffer_len(&command) == 0)
574 tty_flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000575
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000576 /* Force no tty */
Ben Lindstromc72745a2000-12-02 19:03:54 +0000577 if (no_tty_flag)
578 tty_flag = 0;
Damien Miller95def091999-11-25 00:26:21 +1100579 /* Do not allocate a tty if stdin is not a tty. */
Damien Millerddee5752005-05-26 12:05:05 +1000580 if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100581 if (tty_flag)
Damien Miller996acd22003-04-09 20:59:48 +1000582 logit("Pseudo-terminal will not be allocated because stdin is not a terminal.");
Damien Miller95def091999-11-25 00:26:21 +1100583 tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000584 }
Damien Miller1383bd82000-04-06 12:32:37 +1000585
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000586 /*
587 * Initialize "log" output. Since we are the client all output
588 * actually goes to stderr.
589 */
Ben Lindstrom2b646522001-04-12 16:16:57 +0000590 log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
591 SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000592
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000593 /*
594 * Read per-user configuration file. Ignore the system wide config
595 * file if the user specifies a config file on the command line.
596 */
597 if (config != NULL) {
Damien Miller914420f2004-04-20 20:14:07 +1000598 if (!read_config_file(config, host, &options, 0))
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000599 fatal("Can't open user config file %.100s: "
600 "%.100s", config, strerror(errno));
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000601 } else {
602 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
603 _PATH_SSH_USER_CONFFILE);
Damien Miller57a44762004-04-20 20:11:57 +1000604 (void)read_config_file(buf, host, &options, 1);
Ben Lindstrom83f07d12001-10-03 17:22:29 +0000605
606 /* Read systemwide configuration file after use config. */
Darren Tuckerfc959702004-07-17 16:12:08 +1000607 (void)read_config_file(_PATH_HOST_CONFIG_FILE, host,
Damien Miller57a44762004-04-20 20:11:57 +1000608 &options, 0);
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000609 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000610
Damien Miller95def091999-11-25 00:26:21 +1100611 /* Fill configuration defaults. */
612 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000613
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000614 channel_set_af(options.address_family);
615
Damien Miller95def091999-11-25 00:26:21 +1100616 /* reinit */
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000617 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000618
Damien Miller60bc5172001-03-19 09:38:15 +1100619 seed_rng();
620
Damien Miller95def091999-11-25 00:26:21 +1100621 if (options.user == NULL)
622 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000623
Damien Miller95def091999-11-25 00:26:21 +1100624 if (options.hostname != NULL)
625 host = options.hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000626
Darren Tucker3f521e22003-07-03 16:20:42 +1000627 /* force lowercase for hostkey matching */
628 if (options.host_key_alias != NULL) {
629 for (p = options.host_key_alias; *p; p++)
630 if (isupper(*p))
631 *p = tolower(*p);
632 }
633
Damien Miller7c71cc72005-06-26 08:56:31 +1000634 /* Get default port if port has not been set. */
635 if (options.port == 0) {
636 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
637 options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
638 }
639
Damien Miller9f1e33a2003-02-24 11:57:32 +1100640 if (options.proxy_command != NULL &&
641 strcmp(options.proxy_command, "none") == 0)
642 options.proxy_command = NULL;
Damien Miller8f74c8f2005-06-26 08:56:03 +1000643 if (options.control_path != NULL &&
644 strcmp(options.control_path, "none") == 0)
645 options.control_path = NULL;
Damien Miller9f1e33a2003-02-24 11:57:32 +1100646
Damien Miller0e220db2004-06-15 10:34:08 +1000647 if (options.control_path != NULL) {
Damien Miller3ec54c72006-03-15 11:30:13 +1100648 char me[NI_MAXHOST];
649
650 if (gethostname(me, sizeof(me)) == -1)
651 fatal("gethostname: %s", strerror(errno));
Damien Miller6476cad2005-06-16 13:18:34 +1000652 snprintf(buf, sizeof(buf), "%d", options.port);
653 cp = tilde_expand_filename(options.control_path,
654 original_real_uid);
655 options.control_path = percent_expand(cp, "p", buf, "h", host,
Damien Miller3ec54c72006-03-15 11:30:13 +1100656 "r", options.user, "l", me, (char *)NULL);
Damien Miller6476cad2005-06-16 13:18:34 +1000657 xfree(cp);
Damien Miller0e220db2004-06-15 10:34:08 +1000658 }
Darren Tucker0814d312005-06-01 23:08:51 +1000659 if (mux_command != 0 && options.control_path == NULL)
660 fatal("No ControlPath specified for \"-O\" command");
Damien Millerd14b1e72005-06-16 13:19:41 +1000661 if (options.control_path != NULL)
Damien Millerdadfd4d2005-05-26 12:07:13 +1000662 control_client(options.control_path);
Damien Miller0e220db2004-06-15 10:34:08 +1000663
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000664 /* Open a connection to the remote host. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000665 if (ssh_connect(host, &hostaddr, options.port,
666 options.address_family, options.connection_attempts,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000667#ifdef HAVE_CYGWIN
668 options.use_privileged_port,
669#else
Ben Lindstromf9c48842002-06-11 16:37:51 +0000670 original_effective_uid == 0 && options.use_privileged_port,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000671#endif
Ben Lindstromda394ca2002-06-12 16:11:12 +0000672 options.proxy_command) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100673 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000674
Damien Miller5428f641999-11-25 11:54:57 +1100675 /*
676 * If we successfully made the connection, load the host private key
677 * in case we will need it later for combined rsa-rhosts
678 * authentication. This must be done before releasing extra
679 * privileges, because the file is only readable by root.
Ben Lindstrom9e5bb572002-06-06 19:58:27 +0000680 * If we cannot access the private keys, load the public keys
681 * instead and try to execute the ssh-keysign helper instead.
Damien Miller5428f641999-11-25 11:54:57 +1100682 */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000683 sensitive_data.nkeys = 0;
684 sensitive_data.keys = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000685 sensitive_data.external_keysign = 0;
Ben Lindstrom24157572002-06-12 16:09:39 +0000686 if (options.rhosts_rsa_authentication ||
687 options.hostbased_authentication) {
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000688 sensitive_data.nkeys = 3;
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000689 sensitive_data.keys = xmalloc(sensitive_data.nkeys *
690 sizeof(Key));
Ben Lindstromf9c48842002-06-11 16:37:51 +0000691
692 PRIV_START;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000693 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
Ben Lindstromd0fca422001-03-26 13:44:06 +0000694 _PATH_HOST_KEY_FILE, "", NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000695 sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
696 _PATH_HOST_DSA_KEY_FILE, "", NULL);
697 sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
698 _PATH_HOST_RSA_KEY_FILE, "", NULL);
Ben Lindstromf9c48842002-06-11 16:37:51 +0000699 PRIV_END;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000700
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +0000701 if (options.hostbased_authentication == 1 &&
702 sensitive_data.keys[0] == NULL &&
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000703 sensitive_data.keys[1] == NULL &&
704 sensitive_data.keys[2] == NULL) {
705 sensitive_data.keys[1] = key_load_public(
706 _PATH_HOST_DSA_KEY_FILE, NULL);
707 sensitive_data.keys[2] = key_load_public(
708 _PATH_HOST_RSA_KEY_FILE, NULL);
709 sensitive_data.external_keysign = 1;
710 }
Damien Miller95def091999-11-25 00:26:21 +1100711 }
Damien Miller5428f641999-11-25 11:54:57 +1100712 /*
713 * Get rid of any extra privileges that we may have. We will no
714 * longer need them. Also, extra privileges could make it very hard
715 * to read identity files and other non-world-readable files from the
716 * user's home directory if it happens to be on a NFS volume where
717 * root is mapped to nobody.
718 */
Darren Tucker25f60a72004-08-15 17:23:34 +1000719 if (original_effective_uid == 0) {
720 PRIV_START;
721 permanently_set_uid(pw);
722 }
Damien Miller95def091999-11-25 00:26:21 +1100723
Damien Miller5428f641999-11-25 11:54:57 +1100724 /*
725 * Now that we are back to our own permissions, create ~/.ssh
Damien Miller788f2122005-11-05 15:14:59 +1100726 * directory if it doesn't already exist.
Damien Miller5428f641999-11-25 11:54:57 +1100727 */
Ben Lindstrom930b14a2001-08-15 23:19:21 +0000728 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 +1100729 if (stat(buf, &st) < 0)
Damien Millerbe484b52000-07-15 14:14:16 +1000730 if (mkdir(buf, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100731 error("Could not create directory '%.200s'.", buf);
732
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000733 /* load options.identity_files */
734 load_public_identity_files();
735
736 /* Expand ~ in known host file names. */
737 /* XXX mem-leaks: */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100738 options.system_hostfile =
739 tilde_expand_filename(options.system_hostfile, original_real_uid);
740 options.user_hostfile =
741 tilde_expand_filename(options.user_hostfile, original_real_uid);
742 options.system_hostfile2 =
743 tilde_expand_filename(options.system_hostfile2, original_real_uid);
744 options.user_hostfile2 =
745 tilde_expand_filename(options.user_hostfile2, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100746
Damien Miller07cd5892001-11-12 10:52:03 +1100747 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
748
Damien Miller95def091999-11-25 00:26:21 +1100749 /* Log into the remote system. This never returns if the login fails. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000750 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, pw);
Damien Miller95def091999-11-25 00:26:21 +1100751
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000752 /* We no longer need the private host keys. Clear them now. */
753 if (sensitive_data.nkeys != 0) {
754 for (i = 0; i < sensitive_data.nkeys; i++) {
755 if (sensitive_data.keys[i] != NULL) {
756 /* Destroys contents safely */
757 debug3("clear hostkey %d", i);
758 key_free(sensitive_data.keys[i]);
759 sensitive_data.keys[i] = NULL;
760 }
761 }
762 xfree(sensitive_data.keys);
763 }
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +0000764 for (i = 0; i < options.num_identity_files; i++) {
765 if (options.identity_files[i]) {
766 xfree(options.identity_files[i]);
767 options.identity_files[i] = NULL;
768 }
769 if (options.identity_keys[i]) {
770 key_free(options.identity_keys[i]);
771 options.identity_keys[i] = NULL;
772 }
773 }
Damien Miller95def091999-11-25 00:26:21 +1100774
Damien Miller1383bd82000-04-06 12:32:37 +1000775 exit_status = compat20 ? ssh_session2() : ssh_session();
776 packet_close();
Damien Miller8c4e18a2002-09-19 12:05:02 +1000777
Damien Miller0e220db2004-06-15 10:34:08 +1000778 if (options.control_path != NULL && control_fd != -1)
779 unlink(options.control_path);
780
Damien Miller8c4e18a2002-09-19 12:05:02 +1000781 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100782 * Send SIGHUP to proxy command if used. We don't wait() in
Damien Miller8c4e18a2002-09-19 12:05:02 +1000783 * case it hangs and instead rely on init to reap the child
784 */
785 if (proxy_command_pid > 1)
786 kill(proxy_command_pid, SIGHUP);
787
Damien Miller1383bd82000-04-06 12:32:37 +1000788 return exit_status;
789}
790
Ben Lindstrombba81212001-06-25 05:01:22 +0000791static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100792ssh_init_forwarding(void)
793{
Kevin Steves12057502001-02-05 14:54:34 +0000794 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100795 int i;
Kevin Steves12057502001-02-05 14:54:34 +0000796
Damien Miller0bc1bd82000-11-13 22:57:25 +1100797 /* Initiate local TCP/IP port forwardings. */
798 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100799 debug("Local connections to %.200s:%d forwarded to remote "
800 "address %.200s:%d",
Darren Tucker47eede72005-03-14 23:08:12 +1100801 (options.local_forwards[i].listen_host == NULL) ?
802 (options.gateway_ports ? "*" : "LOCALHOST") :
Damien Millerf91ee4c2005-03-01 21:24:33 +1100803 options.local_forwards[i].listen_host,
804 options.local_forwards[i].listen_port,
805 options.local_forwards[i].connect_host,
806 options.local_forwards[i].connect_port);
Damien Millerb16461c2002-01-22 23:29:22 +1100807 success += channel_setup_local_fwd_listener(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100808 options.local_forwards[i].listen_host,
809 options.local_forwards[i].listen_port,
810 options.local_forwards[i].connect_host,
811 options.local_forwards[i].connect_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100812 options.gateway_ports);
813 }
Kevin Steves12057502001-02-05 14:54:34 +0000814 if (i > 0 && success == 0)
815 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100816
817 /* Initiate remote TCP/IP port forwardings. */
818 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100819 debug("Remote connections from %.200s:%d forwarded to "
820 "local address %.200s:%d",
Damien Miller46d38de2005-07-17 17:02:09 +1000821 (options.remote_forwards[i].listen_host == NULL) ?
Damien Milleraa3bb102005-11-05 15:12:59 +1100822 "LOCALHOST" : options.remote_forwards[i].listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100823 options.remote_forwards[i].listen_port,
824 options.remote_forwards[i].connect_host,
825 options.remote_forwards[i].connect_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100826 channel_request_remote_forwarding(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100827 options.remote_forwards[i].listen_host,
828 options.remote_forwards[i].listen_port,
829 options.remote_forwards[i].connect_host,
830 options.remote_forwards[i].connect_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100831 }
832}
833
Ben Lindstrombba81212001-06-25 05:01:22 +0000834static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100835check_agent_present(void)
836{
837 if (options.forward_agent) {
Damien Miller788f2122005-11-05 15:14:59 +1100838 /* Clear agent forwarding if we don't have an agent. */
Damien Miller789e95d2002-09-12 09:52:46 +1000839 if (!ssh_agent_present())
Damien Miller0bc1bd82000-11-13 22:57:25 +1100840 options.forward_agent = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100841 }
842}
843
Ben Lindstrombba81212001-06-25 05:01:22 +0000844static int
Damien Miller1383bd82000-04-06 12:32:37 +1000845ssh_session(void)
846{
847 int type;
Damien Miller1383bd82000-04-06 12:32:37 +1000848 int interactive = 0;
849 int have_tty = 0;
850 struct winsize ws;
Damien Miller1383bd82000-04-06 12:32:37 +1000851 char *cp;
Damien Miller17e7ed02005-06-17 12:54:33 +1000852 const char *display;
Damien Miller1383bd82000-04-06 12:32:37 +1000853
Damien Miller95def091999-11-25 00:26:21 +1100854 /* Enable compression if requested. */
855 if (options.compression) {
856 debug("Requesting compression at level %d.", options.compression_level);
857
858 if (options.compression_level < 1 || options.compression_level > 9)
859 fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
860
861 /* Send the request. */
862 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
863 packet_put_int(options.compression_level);
864 packet_send();
865 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100866 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100867 if (type == SSH_SMSG_SUCCESS)
868 packet_start_compression(options.compression_level);
869 else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000870 logit("Warning: Remote host refused compression.");
Damien Miller95def091999-11-25 00:26:21 +1100871 else
872 packet_disconnect("Protocol error waiting for compression response.");
873 }
874 /* Allocate a pseudo tty if appropriate. */
875 if (tty_flag) {
876 debug("Requesting pty.");
877
878 /* Start the packet. */
879 packet_start(SSH_CMSG_REQUEST_PTY);
880
881 /* Store TERM in the packet. There is no limit on the
882 length of the string. */
883 cp = getenv("TERM");
884 if (!cp)
885 cp = "";
Ben Lindstrom664408d2001-06-09 01:42:01 +0000886 packet_put_cstring(cp);
Damien Miller95def091999-11-25 00:26:21 +1100887
888 /* Store window size in the packet. */
889 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
890 memset(&ws, 0, sizeof(ws));
891 packet_put_int(ws.ws_row);
892 packet_put_int(ws.ws_col);
893 packet_put_int(ws.ws_xpixel);
894 packet_put_int(ws.ws_ypixel);
895
896 /* Store tty modes in the packet. */
Ben Lindstromae8e2d32001-04-14 23:13:02 +0000897 tty_make_modes(fileno(stdin), NULL);
Damien Miller95def091999-11-25 00:26:21 +1100898
899 /* Send the packet, and wait for it to leave. */
900 packet_send();
901 packet_write_wait();
902
903 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100904 type = packet_read();
Damien Miller450a7a12000-03-26 13:04:51 +1000905 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100906 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +1000907 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000908 } else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000909 logit("Warning: Remote host failed or refused to allocate a pseudo tty.");
Damien Miller95def091999-11-25 00:26:21 +1100910 else
911 packet_disconnect("Protocol error waiting for pty request response.");
912 }
913 /* Request X11 forwarding if enabled and DISPLAY is set. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000914 display = getenv("DISPLAY");
915 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000916 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +1000917 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000918 client_x11_get_proto(display, options.xauth_location,
919 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +1000920 /* Request forwarding with authentication spoofing. */
Damien Miller95def091999-11-25 00:26:21 +1100921 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +1000922 x11_request_forwarding_with_spoofing(0, display, proto, data);
Damien Miller95def091999-11-25 00:26:21 +1100923
924 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100925 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100926 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100927 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +1000928 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller996acd22003-04-09 20:59:48 +1000929 logit("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +1000930 } else {
Damien Miller95def091999-11-25 00:26:21 +1100931 packet_disconnect("Protocol error waiting for X11 forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +1000932 }
Damien Miller95def091999-11-25 00:26:21 +1100933 }
934 /* Tell the packet module whether this is an interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000935 packet_set_interactive(interactive);
Damien Miller95def091999-11-25 00:26:21 +1100936
937 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100938 check_agent_present();
939
Damien Miller95def091999-11-25 00:26:21 +1100940 if (options.forward_agent) {
941 debug("Requesting authentication agent forwarding.");
942 auth_request_forwarding();
943
944 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100945 type = packet_read();
Damien Miller48b03fc2002-01-22 23:11:40 +1100946 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100947 if (type != SSH_SMSG_SUCCESS)
Damien Miller996acd22003-04-09 20:59:48 +1000948 logit("Warning: Remote host denied authentication agent forwarding.");
Damien Miller95def091999-11-25 00:26:21 +1100949 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000950
Damien Miller0bc1bd82000-11-13 22:57:25 +1100951 /* Initiate port forwardings. */
952 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000953
Damien Miller5428f641999-11-25 11:54:57 +1100954 /* If requested, let ssh continue in the background. */
Damien Miller4af51302000-04-16 11:18:38 +1000955 if (fork_after_authentication_flag)
Damien Miller5428f641999-11-25 11:54:57 +1100956 if (daemon(1, 1) < 0)
957 fatal("daemon() failed: %.200s", strerror(errno));
958
959 /*
960 * If a command was specified on the command line, execute the
961 * command now. Otherwise request the server to start a shell.
962 */
Damien Miller95def091999-11-25 00:26:21 +1100963 if (buffer_len(&command) > 0) {
964 int len = buffer_len(&command);
965 if (len > 900)
966 len = 900;
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100967 debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
Damien Miller95def091999-11-25 00:26:21 +1100968 packet_start(SSH_CMSG_EXEC_CMD);
969 packet_put_string(buffer_ptr(&command), buffer_len(&command));
970 packet_send();
971 packet_write_wait();
972 } else {
973 debug("Requesting shell.");
974 packet_start(SSH_CMSG_EXEC_SHELL);
975 packet_send();
976 packet_write_wait();
977 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000978
Damien Miller95def091999-11-25 00:26:21 +1100979 /* Enter the interactive session. */
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000980 return client_loop(have_tty, tty_flag ?
981 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
Damien Miller1383bd82000-04-06 12:32:37 +1000982}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000983
Ben Lindstrombba81212001-06-25 05:01:22 +0000984static void
Damien Miller0e220db2004-06-15 10:34:08 +1000985ssh_subsystem_reply(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000986{
987 int id, len;
988
989 id = packet_get_int();
990 len = buffer_len(&command);
Ben Lindstrom4040fe12001-03-05 06:52:57 +0000991 if (len > 900)
992 len = 900;
Damien Miller48b03fc2002-01-22 23:11:40 +1100993 packet_check_eom();
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000994 if (type == SSH2_MSG_CHANNEL_FAILURE)
995 fatal("Request for subsystem '%.*s' failed on channel %d",
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100996 len, (u_char *)buffer_ptr(&command), id);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000997}
998
Damien Miller2797f7f2002-04-23 21:09:44 +1000999void
Damien Miller509b0102003-12-17 16:33:10 +11001000client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt)
Damien Miller2797f7f2002-04-23 21:09:44 +10001001{
1002 int i;
1003
1004 i = client_global_request_id++;
Damien Miller509b0102003-12-17 16:33:10 +11001005 if (i >= options.num_remote_forwards)
Damien Miller2797f7f2002-04-23 21:09:44 +10001006 return;
Damien Miller2797f7f2002-04-23 21:09:44 +10001007 debug("remote forward %s for: listen %d, connect %s:%d",
1008 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
Damien Millerf91ee4c2005-03-01 21:24:33 +11001009 options.remote_forwards[i].listen_port,
1010 options.remote_forwards[i].connect_host,
1011 options.remote_forwards[i].connect_port);
Damien Miller2797f7f2002-04-23 21:09:44 +10001012 if (type == SSH2_MSG_REQUEST_FAILURE)
Damien Millerf91ee4c2005-03-01 21:24:33 +11001013 logit("Warning: remote port forwarding failed for listen "
1014 "port %d", options.remote_forwards[i].listen_port);
Damien Miller2797f7f2002-04-23 21:09:44 +10001015}
1016
Damien Miller0e220db2004-06-15 10:34:08 +10001017static void
1018ssh_control_listener(void)
1019{
1020 struct sockaddr_un addr;
1021 mode_t old_umask;
Damien Miller07b6ff12004-06-15 11:14:45 +10001022 int addr_len;
1023
Damien Millerd14b1e72005-06-16 13:19:41 +10001024 if (options.control_path == NULL ||
1025 options.control_master == SSHCTL_MASTER_NO)
Damien Miller0e220db2004-06-15 10:34:08 +10001026 return;
1027
Damien Millerd14b1e72005-06-16 13:19:41 +10001028 debug("setting up multiplex master socket");
1029
Damien Miller0e220db2004-06-15 10:34:08 +10001030 memset(&addr, '\0', sizeof(addr));
1031 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001032 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001033 strlen(options.control_path) + 1;
1034
1035 if (strlcpy(addr.sun_path, options.control_path,
1036 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1037 fatal("ControlPath too long");
1038
1039 if ((control_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001040 fatal("%s socket(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001041
1042 old_umask = umask(0177);
Damien Miller07b6ff12004-06-15 11:14:45 +10001043 if (bind(control_fd, (struct sockaddr*)&addr, addr_len) == -1) {
Damien Miller0e220db2004-06-15 10:34:08 +10001044 control_fd = -1;
Damien Miller4f10e252005-05-04 15:33:09 +10001045 if (errno == EINVAL || errno == EADDRINUSE)
Damien Miller0e220db2004-06-15 10:34:08 +10001046 fatal("ControlSocket %s already exists",
1047 options.control_path);
1048 else
Damien Miller15d72a02005-11-05 15:07:33 +11001049 fatal("%s bind(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001050 }
1051 umask(old_umask);
1052
1053 if (listen(control_fd, 64) == -1)
Damien Miller15d72a02005-11-05 15:07:33 +11001054 fatal("%s listen(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001055
1056 set_nonblock(control_fd);
1057}
1058
Ben Lindstromf558cf62001-09-20 23:13:49 +00001059/* request pty/x11/agent/tcpfwd/shell for channel */
Ben Lindstrombba81212001-06-25 05:01:22 +00001060static void
Ben Lindstromf558cf62001-09-20 23:13:49 +00001061ssh_session2_setup(int id, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +10001062{
Damien Miller3756dce2004-06-18 01:17:29 +10001063 extern char **environ;
Damien Miller17e7ed02005-06-17 12:54:33 +10001064 const char *display;
Damien Miller0e220db2004-06-15 10:34:08 +10001065 int interactive = tty_flag;
Damien Miller17e7ed02005-06-17 12:54:33 +10001066
Damien Miller46d38de2005-07-17 17:02:09 +10001067 display = getenv("DISPLAY");
Damien Miller17e7ed02005-06-17 12:54:33 +10001068 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001069 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001070 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001071 client_x11_get_proto(display, options.xauth_location,
1072 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001073 /* Request forwarding with authentication spoofing. */
1074 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +10001075 x11_request_forwarding_with_spoofing(id, display, proto, data);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001076 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001077 /* XXX wait for reply */
1078 }
1079
Damien Miller0bc1bd82000-11-13 22:57:25 +11001080 check_agent_present();
1081 if (options.forward_agent) {
1082 debug("Requesting authentication agent forwarding.");
1083 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1084 packet_send();
1085 }
1086
Damien Miller7b58e802005-12-13 19:33:19 +11001087 if (options.tun_open != SSH_TUNMODE_NO) {
Damien Millerd27b9472005-12-13 19:29:02 +11001088 Channel *c;
1089 int fd;
1090
1091 debug("Requesting tun.");
Damien Miller7b58e802005-12-13 19:33:19 +11001092 if ((fd = tun_open(options.tun_local,
1093 options.tun_open)) >= 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11001094 c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1095 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1096 0, "tun", 1);
1097 c->datagram = 1;
Damien Miller598bbc22005-12-31 16:33:36 +11001098#if defined(SSH_TUN_FILTER)
1099 if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
1100 channel_register_filter(c->self, sys_tun_infilter,
1101 sys_tun_outfilter);
1102#endif
Damien Millerd27b9472005-12-13 19:29:02 +11001103 packet_start(SSH2_MSG_CHANNEL_OPEN);
1104 packet_put_cstring("tun@openssh.com");
1105 packet_put_int(c->self);
1106 packet_put_int(c->local_window_max);
1107 packet_put_int(c->local_maxpacket);
Damien Miller7b58e802005-12-13 19:33:19 +11001108 packet_put_int(options.tun_open);
Damien Millerd27b9472005-12-13 19:29:02 +11001109 packet_put_int(options.tun_remote);
1110 packet_send();
1111 }
1112 }
1113
Damien Miller0e220db2004-06-15 10:34:08 +10001114 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
Damien Miller3756dce2004-06-18 01:17:29 +10001115 NULL, fileno(stdin), &command, environ, &ssh_subsystem_reply);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001116
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001117 packet_set_interactive(interactive);
Damien Miller1383bd82000-04-06 12:32:37 +10001118}
1119
Ben Lindstromf558cf62001-09-20 23:13:49 +00001120/* open new channel for a session */
Ben Lindstrombba81212001-06-25 05:01:22 +00001121static int
Ben Lindstromf558cf62001-09-20 23:13:49 +00001122ssh_session2_open(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001123{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001124 Channel *c;
1125 int window, packetmax, in, out, err;
Damien Millerad833b32000-08-23 10:46:23 +10001126
Damien Millercaf6dd62000-08-29 11:33:50 +11001127 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001128 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001129 } else {
1130 in = dup(STDIN_FILENO);
1131 }
1132 out = dup(STDOUT_FILENO);
1133 err = dup(STDERR_FILENO);
1134
1135 if (in < 0 || out < 0 || err < 0)
1136 fatal("dup() in/out/err failed");
1137
Damien Miller69b69aa2000-10-28 14:19:58 +11001138 /* enable nonblocking unless tty */
1139 if (!isatty(in))
1140 set_nonblock(in);
1141 if (!isatty(out))
1142 set_nonblock(out);
1143 if (!isatty(err))
1144 set_nonblock(err);
1145
Damien Millere4340be2000-09-16 13:29:08 +11001146 window = CHAN_SES_WINDOW_DEFAULT;
1147 packetmax = CHAN_SES_PACKET_DEFAULT;
Damien Miller19a59452002-02-19 15:20:57 +11001148 if (tty_flag) {
1149 window >>= 1;
1150 packetmax >>= 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001151 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001152 c = channel_new(
Damien Miller1383bd82000-04-06 12:32:37 +10001153 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001154 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001155 "client-session", /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001156
Ben Lindstromf558cf62001-09-20 23:13:49 +00001157 debug3("ssh_session2_open: channel_new: %d", c->self);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001158
Ben Lindstrom5ec26452001-06-09 00:18:51 +00001159 channel_send_open(c->self);
Ben Lindstromf558cf62001-09-20 23:13:49 +00001160 if (!no_shell_flag)
Damien Miller0e220db2004-06-15 10:34:08 +10001161 channel_register_confirm(c->self, ssh_session2_setup, NULL);
Damien Miller1383bd82000-04-06 12:32:37 +10001162
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001163 return c->self;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001164}
1165
Ben Lindstrombba81212001-06-25 05:01:22 +00001166static int
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001167ssh_session2(void)
1168{
Ben Lindstromf558cf62001-09-20 23:13:49 +00001169 int id = -1;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001170
1171 /* XXX should be pre-session */
1172 ssh_init_forwarding();
Damien Miller0e220db2004-06-15 10:34:08 +10001173 ssh_control_listener();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001174
Ben Lindstromf558cf62001-09-20 23:13:49 +00001175 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1176 id = ssh_session2_open();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001177
Damien Millerd27b9472005-12-13 19:29:02 +11001178 /* Execute a local command */
1179 if (options.local_command != NULL &&
1180 options.permit_local_command)
1181 ssh_local_cmd(options.local_command);
1182
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001183 /* If requested, let ssh continue in the background. */
1184 if (fork_after_authentication_flag)
1185 if (daemon(1, 1) < 0)
1186 fatal("daemon() failed: %.200s", strerror(errno));
1187
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001188 return client_loop(tty_flag, tty_flag ?
1189 options.escape_char : SSH_ESCAPECHAR_NONE, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001190}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001191
Ben Lindstrombba81212001-06-25 05:01:22 +00001192static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001193load_public_identity_files(void)
1194{
1195 char *filename;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001196 int i = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001197 Key *public;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001198#ifdef SMARTCARD
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001199 Key **keys;
1200
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001201 if (options.smartcard_device != NULL &&
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001202 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1203 (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL ) {
1204 int count = 0;
1205 for (i = 0; keys[i] != NULL; i++) {
1206 count++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001207 memmove(&options.identity_files[1], &options.identity_files[0],
1208 sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
1209 memmove(&options.identity_keys[1], &options.identity_keys[0],
1210 sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1211 options.num_identity_files++;
1212 options.identity_keys[0] = keys[i];
Damien Miller56a0bb02003-06-18 20:28:40 +10001213 options.identity_files[0] = sc_get_key_label(keys[i]);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001214 }
Ben Lindstrom4f054602002-03-26 03:23:00 +00001215 if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1216 options.num_identity_files = SSH_MAX_IDENTITY_FILES;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001217 i = count;
1218 xfree(keys);
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001219 }
Ben Lindstromffce1472001-08-06 21:57:31 +00001220#endif /* SMARTCARD */
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001221 for (; i < options.num_identity_files; i++) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001222 filename = tilde_expand_filename(options.identity_files[i],
1223 original_real_uid);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001224 public = key_load_public(filename, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001225 debug("identity file %s type %d", filename,
1226 public ? public->type : -1);
1227 xfree(options.identity_files[i]);
1228 options.identity_files[i] = filename;
1229 options.identity_keys[i] = public;
1230 }
1231}
Damien Miller0e220db2004-06-15 10:34:08 +10001232
1233static void
1234control_client_sighandler(int signo)
1235{
1236 control_client_terminate = signo;
1237}
1238
1239static void
1240control_client_sigrelay(int signo)
1241{
1242 if (control_server_pid > 1)
1243 kill(control_server_pid, signo);
1244}
1245
Darren Tucker365433f2004-06-22 12:29:23 +10001246static int
1247env_permitted(char *env)
1248{
1249 int i;
1250 char name[1024], *cp;
1251
1252 strlcpy(name, env, sizeof(name));
1253 if ((cp = strchr(name, '=')) == NULL)
1254 return (0);
1255
1256 *cp = '\0';
1257
1258 for (i = 0; i < options.num_send_env; i++)
1259 if (match_pattern(name, options.send_env[i]))
1260 return (1);
1261
1262 return (0);
1263}
1264
Damien Miller0e220db2004-06-15 10:34:08 +10001265static void
1266control_client(const char *path)
1267{
1268 struct sockaddr_un addr;
Darren Tucker39207a42004-11-05 20:19:51 +11001269 int i, r, fd, sock, exitval, num_env, addr_len;
Damien Miller0e220db2004-06-15 10:34:08 +10001270 Buffer m;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001271 char *term;
Damien Miller3756dce2004-06-18 01:17:29 +10001272 extern char **environ;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001273 u_int flags;
Darren Tuckerfc959702004-07-17 16:12:08 +10001274
Damien Millerd14b1e72005-06-16 13:19:41 +10001275 if (mux_command == 0)
1276 mux_command = SSHMUX_COMMAND_OPEN;
1277
1278 switch (options.control_master) {
1279 case SSHCTL_MASTER_AUTO:
1280 case SSHCTL_MASTER_AUTO_ASK:
1281 debug("auto-mux: Trying existing master");
1282 /* FALLTHROUGH */
1283 case SSHCTL_MASTER_NO:
1284 break;
1285 default:
1286 return;
1287 }
1288
Damien Miller0e220db2004-06-15 10:34:08 +10001289 memset(&addr, '\0', sizeof(addr));
1290 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001291 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001292 strlen(path) + 1;
1293
1294 if (strlcpy(addr.sun_path, path,
1295 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1296 fatal("ControlPath too long");
1297
1298 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1299 fatal("%s socket(): %s", __func__, strerror(errno));
1300
Damien Millerdadfd4d2005-05-26 12:07:13 +10001301 if (connect(sock, (struct sockaddr*)&addr, addr_len) == -1) {
Darren Tucker0814d312005-06-01 23:08:51 +10001302 if (mux_command != SSHMUX_COMMAND_OPEN) {
1303 fatal("Control socket connect(%.100s): %s", path,
1304 strerror(errno));
1305 }
Damien Miller538c9b72005-05-26 12:11:28 +10001306 if (errno == ENOENT)
1307 debug("Control socket \"%.100s\" does not exist", path);
1308 else {
1309 error("Control socket connect(%.100s): %s", path,
1310 strerror(errno));
1311 }
Damien Miller13390022005-07-06 09:44:19 +10001312 close(sock);
1313 return;
1314 }
Damien Miller46d38de2005-07-17 17:02:09 +10001315
Damien Miller13390022005-07-06 09:44:19 +10001316 if (stdin_null_flag) {
1317 if ((fd = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1318 fatal("open(/dev/null): %s", strerror(errno));
1319 if (dup2(fd, STDIN_FILENO) == -1)
1320 fatal("dup2: %s", strerror(errno));
1321 if (fd > STDERR_FILENO)
1322 close(fd);
1323 }
Damien Miller46d38de2005-07-17 17:02:09 +10001324
Damien Miller13390022005-07-06 09:44:19 +10001325 term = getenv("TERM");
Darren Tucker7ebfc102004-11-07 20:06:19 +11001326
1327 flags = 0;
1328 if (tty_flag)
1329 flags |= SSHMUX_FLAG_TTY;
1330 if (subsystem_flag)
1331 flags |= SSHMUX_FLAG_SUBSYS;
Damien Miller13390022005-07-06 09:44:19 +10001332 if (options.forward_x11)
1333 flags |= SSHMUX_FLAG_X11_FWD;
1334 if (options.forward_agent)
1335 flags |= SSHMUX_FLAG_AGENT_FWD;
Damien Miller0e220db2004-06-15 10:34:08 +10001336
Damien Miller0e220db2004-06-15 10:34:08 +10001337 buffer_init(&m);
1338
Darren Tucker7ebfc102004-11-07 20:06:19 +11001339 /* Send our command to server */
1340 buffer_put_int(&m, mux_command);
1341 buffer_put_int(&m, flags);
Damien Miller13390022005-07-06 09:44:19 +10001342 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001343 fatal("%s: msg_send", __func__);
1344 buffer_clear(&m);
1345
1346 /* Get authorisation status and PID of controlee */
Damien Miller0e220db2004-06-15 10:34:08 +10001347 if (ssh_msg_recv(sock, &m) == -1)
1348 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001349 if (buffer_get_char(&m) != SSHMUX_VER)
Damien Miller0e220db2004-06-15 10:34:08 +10001350 fatal("%s: wrong version", __func__);
Damien Miller23f07702004-06-18 01:19:03 +10001351 if (buffer_get_int(&m) != 1)
1352 fatal("Connection to master denied");
Damien Miller0e220db2004-06-15 10:34:08 +10001353 control_server_pid = buffer_get_int(&m);
1354
Damien Miller0e220db2004-06-15 10:34:08 +10001355 buffer_clear(&m);
Damien Miller0e220db2004-06-15 10:34:08 +10001356
Darren Tucker7ebfc102004-11-07 20:06:19 +11001357 switch (mux_command) {
1358 case SSHMUX_COMMAND_ALIVE_CHECK:
Darren Tucker47eede72005-03-14 23:08:12 +11001359 fprintf(stderr, "Master running (pid=%d)\r\n",
Darren Tucker7ebfc102004-11-07 20:06:19 +11001360 control_server_pid);
1361 exit(0);
1362 case SSHMUX_COMMAND_TERMINATE:
1363 fprintf(stderr, "Exit request sent.\r\n");
1364 exit(0);
1365 case SSHMUX_COMMAND_OPEN:
1366 /* continue below */
1367 break;
1368 default:
1369 fatal("silly mux_command %d", mux_command);
1370 }
1371
1372 /* SSHMUX_COMMAND_OPEN */
Damien Miller13390022005-07-06 09:44:19 +10001373 buffer_put_cstring(&m, term ? term : "");
Damien Miller0e220db2004-06-15 10:34:08 +10001374 buffer_append(&command, "\0", 1);
1375 buffer_put_cstring(&m, buffer_ptr(&command));
1376
Darren Tucker365433f2004-06-22 12:29:23 +10001377 if (options.num_send_env == 0 || environ == NULL) {
1378 buffer_put_int(&m, 0);
Darren Tuckerfc959702004-07-17 16:12:08 +10001379 } else {
Darren Tucker365433f2004-06-22 12:29:23 +10001380 /* Pass environment */
1381 num_env = 0;
1382 for (i = 0; environ[i] != NULL; i++)
1383 if (env_permitted(environ[i]))
1384 num_env++; /* Count */
Darren Tuckerfc959702004-07-17 16:12:08 +10001385
Darren Tucker365433f2004-06-22 12:29:23 +10001386 buffer_put_int(&m, num_env);
1387
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001388 for (i = 0; environ[i] != NULL && num_env >= 0; i++)
1389 if (env_permitted(environ[i])) {
1390 num_env--;
Darren Tucker365433f2004-06-22 12:29:23 +10001391 buffer_put_cstring(&m, environ[i]);
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001392 }
Darren Tucker365433f2004-06-22 12:29:23 +10001393 }
Damien Miller3756dce2004-06-18 01:17:29 +10001394
Damien Miller13390022005-07-06 09:44:19 +10001395 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Damien Miller0e220db2004-06-15 10:34:08 +10001396 fatal("%s: msg_send", __func__);
1397
1398 mm_send_fd(sock, STDIN_FILENO);
1399 mm_send_fd(sock, STDOUT_FILENO);
1400 mm_send_fd(sock, STDERR_FILENO);
1401
1402 /* Wait for reply, so master has a chance to gather ttymodes */
1403 buffer_clear(&m);
1404 if (ssh_msg_recv(sock, &m) == -1)
1405 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001406 if (buffer_get_char(&m) != SSHMUX_VER)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001407 fatal("%s: wrong version", __func__);
Damien Miller0e220db2004-06-15 10:34:08 +10001408 buffer_free(&m);
1409
Darren Tucker07336da2004-11-05 20:02:16 +11001410 signal(SIGHUP, control_client_sighandler);
Damien Miller0809e232004-06-18 22:20:57 +10001411 signal(SIGINT, control_client_sighandler);
1412 signal(SIGTERM, control_client_sighandler);
1413 signal(SIGWINCH, control_client_sigrelay);
1414
Damien Miller0e220db2004-06-15 10:34:08 +10001415 if (tty_flag)
1416 enter_raw_mode();
1417
1418 /* Stick around until the controlee closes the client_fd */
1419 exitval = 0;
1420 for (;!control_client_terminate;) {
1421 r = read(sock, &exitval, sizeof(exitval));
1422 if (r == 0) {
1423 debug2("Received EOF from master");
1424 break;
1425 }
1426 if (r > 0)
1427 debug2("Received exit status from master %d", exitval);
1428 if (r == -1 && errno != EINTR)
1429 fatal("%s: read %s", __func__, strerror(errno));
1430 }
1431
1432 if (control_client_terminate)
1433 debug2("Exiting on signal %d", control_client_terminate);
1434
1435 close(sock);
1436
1437 leave_raw_mode();
1438
1439 if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
1440 fprintf(stderr, "Connection to master closed.\r\n");
1441
1442 exit(exitval);
1443}