blob: 6dff591f115452675763608f135ae8437317c6b5 [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"
Darren Tuckerfc959702004-07-17 16:12:08 +100043RCSID("$OpenBSD: ssh.c,v 1.223 2004/07/11 17:48:47 deraadt Exp $");
Damien Millereba71ba2000-04-29 23:57:08 +100044
45#include <openssl/evp.h>
Damien Miller0bc1bd82000-11-13 22:57:25 +110046#include <openssl/err.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000049#include "ssh1.h"
Damien Miller1383bd82000-04-06 12:32:37 +100050#include "ssh2.h"
51#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000052#include "cipher.h"
53#include "xmalloc.h"
54#include "packet.h"
55#include "buffer.h"
Damien Miller0e220db2004-06-15 10:34:08 +100056#include "bufaux.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000057#include "channels.h"
Damien Millereba71ba2000-04-29 23:57:08 +100058#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100059#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100060#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000061#include "pathnames.h"
Damien Miller0e220db2004-06-15 10:34:08 +100062#include "dispatch.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +000063#include "clientloop.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000064#include "log.h"
65#include "readconf.h"
66#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000067#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000068#include "kex.h"
69#include "mac.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +100070#include "sshpty.h"
Darren Tucker46bc0752004-05-02 22:11:30 +100071#include "match.h"
Damien Miller0e220db2004-06-15 10:34:08 +100072#include "msg.h"
73#include "monitor_fdpass.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074
Ben Lindstromc5b68002001-07-04 04:52:03 +000075#ifdef SMARTCARD
Ben Lindstromc5b68002001-07-04 04:52:03 +000076#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000077#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +000078
Damien Miller3f905871999-11-15 17:10:57 +110079extern char *__progname;
Damien Miller3f905871999-11-15 17:10:57 +110080
Damien Miller95def091999-11-25 00:26:21 +110081/* Flag indicating whether debug mode is on. This can be set on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082int debug_flag = 0;
83
Damien Miller78928792000-04-12 20:17:38 +100084/* Flag indicating whether a tty should be allocated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085int tty_flag = 0;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +000086int no_tty_flag = 0;
87int force_tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
Damien Miller1383bd82000-04-06 12:32:37 +100089/* don't exec a shell */
90int no_shell_flag = 0;
Damien Miller1383bd82000-04-06 12:32:37 +100091
Damien Miller5428f641999-11-25 11:54:57 +110092/*
93 * Flag indicating that nothing should be read from stdin. This can be set
94 * on the command line.
95 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096int stdin_null_flag = 0;
97
Damien Miller5428f641999-11-25 11:54:57 +110098/*
99 * Flag indicating that ssh should fork after authentication. This is useful
Ben Lindstromf666fec2002-06-06 19:51:58 +0000100 * so that the passphrase can be entered manually, and then ssh goes to the
Damien Miller5428f641999-11-25 11:54:57 +1100101 * background.
102 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103int fork_after_authentication_flag = 0;
104
Damien Miller5428f641999-11-25 11:54:57 +1100105/*
106 * General data structure for command line options and options configurable
107 * in configuration files. See readconf.h.
108 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109Options options;
110
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000111/* optional user configfile */
112char *config = NULL;
113
Damien Miller5428f641999-11-25 11:54:57 +1100114/*
115 * Name of the host we are connecting to. This is the name given on the
116 * command line, or the HostName specified for the user-supplied name in a
117 * configuration file.
118 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119char *host;
120
121/* socket address the host resolves to */
Damien Miller34132e52000-01-14 15:45:46 +1100122struct sockaddr_storage hostaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000124/* Private host keys. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000125Sensitive sensitive_data;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126
127/* Original real UID. */
128uid_t original_real_uid;
Ben Lindstromf9c48842002-06-11 16:37:51 +0000129uid_t original_effective_uid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000130
Damien Miller1383bd82000-04-06 12:32:37 +1000131/* command to be executed */
132Buffer command;
133
Damien Miller832562e2001-01-30 09:30:01 +1100134/* Should we execute a command or invoke a subsystem? */
135int subsystem_flag = 0;
136
Damien Miller2797f7f2002-04-23 21:09:44 +1000137/* # of replies received for global requests */
138static int client_global_request_id = 0;
139
Damien Miller8c4e18a2002-09-19 12:05:02 +1000140/* pid of proxycommand child process */
141pid_t proxy_command_pid = 0;
142
Damien Miller0e220db2004-06-15 10:34:08 +1000143/* fd to control socket */
144int control_fd = -1;
145
146/* Only used in control client mode */
147volatile sig_atomic_t control_client_terminate = 0;
148u_int control_server_pid = 0;
149
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150/* Prints a help message to the user. This function never returns. */
151
Ben Lindstrombba81212001-06-25 05:01:22 +0000152static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000153usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154{
Damien Miller50955102004-03-22 09:34:58 +1100155 fprintf(stderr,
Darren Tuckerba5c5922004-06-18 16:22:39 +1000156"usage: ssh [-1246AaCfghkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n"
Damien Miller50955102004-03-22 09:34:58 +1100157" [-D port] [-e escape_char] [-F configfile] [-i identity_file]\n"
158" [-L port:host:hostport] [-l login_name] [-m mac_spec] [-o option]\n"
Damien Millerb8ea2482004-06-18 22:21:55 +1000159" [-p port] [-R port:host:hostport] [-S ctl] [user@]hostname [command]\n"
Damien Miller50955102004-03-22 09:34:58 +1100160 );
Damien Miller95def091999-11-25 00:26:21 +1100161 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000162}
163
Ben Lindstrombba81212001-06-25 05:01:22 +0000164static int ssh_session(void);
165static int ssh_session2(void);
166static void load_public_identity_files(void);
Damien Miller0e220db2004-06-15 10:34:08 +1000167static void control_client(const char *path);
Damien Miller1383bd82000-04-06 12:32:37 +1000168
Damien Miller95def091999-11-25 00:26:21 +1100169/*
170 * Main program for the ssh client.
171 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000172int
173main(int ac, char **av)
174{
Ben Lindstrom24157572002-06-12 16:09:39 +0000175 int i, opt, exit_status;
Damien Milleraae6c611999-12-06 11:47:28 +1100176 u_short fwd_port, fwd_host_port;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000177 char sfwd_port[6], sfwd_host_port[6];
Damien Miller9836cf82003-12-17 16:30:06 +1100178 char *p, *cp, *line, buf[256];
Damien Miller95def091999-11-25 00:26:21 +1100179 struct stat st;
Ben Lindstrom086cf212001-03-05 05:56:40 +0000180 struct passwd *pw;
Damien Miller1383bd82000-04-06 12:32:37 +1000181 int dummy;
Ben Lindstrom5ccf63a2001-09-24 20:00:10 +0000182 extern int optind, optreset;
Damien Miller4f8e6692001-07-14 13:22:53 +1000183 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000184
Damien Miller59d3d5b2003-08-22 09:34:41 +1000185 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000186 init_rng();
187
Damien Miller5428f641999-11-25 11:54:57 +1100188 /*
189 * Save the original real uid. It will be needed later (uid-swapping
190 * may clobber the real uid).
191 */
Damien Miller95def091999-11-25 00:26:21 +1100192 original_real_uid = getuid();
193 original_effective_uid = geteuid();
Damien Millera8e06ce2003-11-21 23:48:55 +1100194
Damien Miller50b9a602002-09-04 16:50:06 +1000195 /*
196 * Use uid-swapping to give up root privileges for the duration of
197 * option processing. We will re-instantiate the rights when we are
198 * ready to create the privileged port, and will permanently drop
199 * them when the port has been created (actually, when the connection
200 * has been made, as we may need to create the port several times).
201 */
202 PRIV_END;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203
Damien Miller05dd7952000-10-01 00:42:48 +1100204#ifdef HAVE_SETRLIMIT
Damien Miller95def091999-11-25 00:26:21 +1100205 /* If we are installed setuid root be careful to not drop core. */
206 if (original_real_uid != original_effective_uid) {
207 struct rlimit rlim;
208 rlim.rlim_cur = rlim.rlim_max = 0;
209 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
210 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000211 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100212#endif
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000213 /* Get user data. */
214 pw = getpwuid(original_real_uid);
215 if (!pw) {
Damien Miller996acd22003-04-09 20:59:48 +1000216 logit("You don't exist, go away!");
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000217 exit(1);
218 }
219 /* Take a copy of the returned structure. */
220 pw = pwcopy(pw);
221
Damien Miller5428f641999-11-25 11:54:57 +1100222 /*
Damien Miller5428f641999-11-25 11:54:57 +1100223 * Set our umask to something reasonable, as some files are created
224 * with the default umask. This will make them world-readable but
225 * writable only by the owner, which is ok for all files for which we
226 * don't set the modes explicitly.
227 */
Damien Miller95def091999-11-25 00:26:21 +1100228 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000229
Damien Miller95def091999-11-25 00:26:21 +1100230 /* Initialize option structure to indicate that no values have been set. */
231 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000232
Damien Miller95def091999-11-25 00:26:21 +1100233 /* Parse command-line arguments. */
234 host = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000235
Damien Millerf4614452001-07-14 12:18:10 +1000236again:
237 while ((opt = getopt(ac, av,
Damien Miller0e220db2004-06-15 10:34:08 +1000238 "1246ab:c:e:fgi:kl:m:no:p:qstvxACD:F:I:L:MNPR:S:TVXY")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100239 switch (opt) {
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000240 case '1':
241 options.protocol = SSH_PROTO_1;
242 break;
Damien Miller4af51302000-04-16 11:18:38 +1000243 case '2':
244 options.protocol = SSH_PROTO_2;
245 break;
Damien Miller34132e52000-01-14 15:45:46 +1100246 case '4':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000247 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100248 break;
Damien Miller34132e52000-01-14 15:45:46 +1100249 case '6':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000250 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100251 break;
Damien Miller95def091999-11-25 00:26:21 +1100252 case 'n':
253 stdin_null_flag = 1;
254 break;
Damien Miller95def091999-11-25 00:26:21 +1100255 case 'f':
256 fork_after_authentication_flag = 1;
257 stdin_null_flag = 1;
258 break;
Damien Miller95def091999-11-25 00:26:21 +1100259 case 'x':
260 options.forward_x11 = 0;
261 break;
Damien Miller95def091999-11-25 00:26:21 +1100262 case 'X':
263 options.forward_x11 = 1;
264 break;
Darren Tucker0a118da2003-10-15 15:54:32 +1000265 case 'Y':
266 options.forward_x11 = 1;
267 options.forward_x11_trusted = 1;
268 break;
Damien Miller95def091999-11-25 00:26:21 +1100269 case 'g':
270 options.gateway_ports = 1;
271 break;
Damien Miller147bba32002-09-04 16:46:06 +1000272 case 'P': /* deprecated */
Damien Miller95def091999-11-25 00:26:21 +1100273 options.use_privileged_port = 0;
274 break;
Damien Miller95def091999-11-25 00:26:21 +1100275 case 'a':
276 options.forward_agent = 0;
277 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000278 case 'A':
279 options.forward_agent = 1;
280 break;
Damien Miller95def091999-11-25 00:26:21 +1100281 case 'k':
Damien Millere0113cc2003-11-24 13:10:09 +1100282 options.gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100283 break;
Damien Miller95def091999-11-25 00:26:21 +1100284 case 'i':
285 if (stat(optarg, &st) < 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000286 fprintf(stderr, "Warning: Identity file %s "
287 "does not exist.\n", optarg);
Damien Miller95def091999-11-25 00:26:21 +1100288 break;
289 }
Damien Millerf4614452001-07-14 12:18:10 +1000290 if (options.num_identity_files >=
291 SSH_MAX_IDENTITY_FILES)
292 fatal("Too many identity files specified "
293 "(max %d)", SSH_MAX_IDENTITY_FILES);
294 options.identity_files[options.num_identity_files++] =
295 xstrdup(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100296 break;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000297 case 'I':
298#ifdef SMARTCARD
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000299 options.smartcard_device = xstrdup(optarg);
Ben Lindstrombcc18082001-08-06 21:59:25 +0000300#else
Ben Lindstromc5b68002001-07-04 04:52:03 +0000301 fprintf(stderr, "no support for smartcards.\n");
Ben Lindstrombcc18082001-08-06 21:59:25 +0000302#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000303 break;
Damien Miller95def091999-11-25 00:26:21 +1100304 case 't':
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000305 if (tty_flag)
306 force_tty_flag = 1;
Damien Miller95def091999-11-25 00:26:21 +1100307 tty_flag = 1;
308 break;
Damien Miller95def091999-11-25 00:26:21 +1100309 case 'v':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000310 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100311 debug_flag = 1;
312 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000313 } else {
314 if (options.log_level < SYSLOG_LEVEL_DEBUG3)
315 options.log_level++;
Damien Millere4340be2000-09-16 13:29:08 +1100316 break;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000317 }
Damien Millere4340be2000-09-16 13:29:08 +1100318 /* fallthrough */
Damien Miller95def091999-11-25 00:26:21 +1100319 case 'V':
Damien Miller0c889cd2004-03-22 09:36:00 +1100320 fprintf(stderr, "%s, %s\n",
321 SSH_VERSION, SSLeay_version(SSLEAY_VERSION));
Damien Miller95def091999-11-25 00:26:21 +1100322 if (opt == 'V')
323 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100324 break;
Damien Miller95def091999-11-25 00:26:21 +1100325 case 'q':
326 options.log_level = SYSLOG_LEVEL_QUIET;
327 break;
Damien Miller95def091999-11-25 00:26:21 +1100328 case 'e':
329 if (optarg[0] == '^' && optarg[2] == 0 &&
Damien Millerf4614452001-07-14 12:18:10 +1000330 (u_char) optarg[1] >= 64 &&
331 (u_char) optarg[1] < 128)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000332 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100333 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000334 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100335 else if (strcmp(optarg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000336 options.escape_char = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100337 else {
Damien Millerf4614452001-07-14 12:18:10 +1000338 fprintf(stderr, "Bad escape character '%s'.\n",
339 optarg);
Damien Miller95def091999-11-25 00:26:21 +1100340 exit(1);
341 }
342 break;
Damien Miller95def091999-11-25 00:26:21 +1100343 case 'c':
Damien Millereba71ba2000-04-29 23:57:08 +1000344 if (ciphers_valid(optarg)) {
345 /* SSH2 only */
346 options.ciphers = xstrdup(optarg);
Damien Miller30c3d422000-05-09 11:02:59 +1000347 options.cipher = SSH_CIPHER_ILLEGAL;
Damien Millereba71ba2000-04-29 23:57:08 +1000348 } else {
349 /* SSH1 only */
Damien Millere39cacc2000-11-29 12:18:44 +1100350 options.cipher = cipher_number(optarg);
351 if (options.cipher == -1) {
Damien Millerf4614452001-07-14 12:18:10 +1000352 fprintf(stderr,
353 "Unknown cipher type '%s'\n",
354 optarg);
Damien Millereba71ba2000-04-29 23:57:08 +1000355 exit(1);
356 }
Damien Millerf4614452001-07-14 12:18:10 +1000357 if (options.cipher == SSH_CIPHER_3DES)
Damien Millere39cacc2000-11-29 12:18:44 +1100358 options.ciphers = "3des-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000359 else if (options.cipher == SSH_CIPHER_BLOWFISH)
Damien Millere39cacc2000-11-29 12:18:44 +1100360 options.ciphers = "blowfish-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000361 else
Damien Millere39cacc2000-11-29 12:18:44 +1100362 options.ciphers = (char *)-1;
Damien Miller95def091999-11-25 00:26:21 +1100363 }
364 break;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000365 case 'm':
366 if (mac_valid(optarg))
367 options.macs = xstrdup(optarg);
368 else {
Damien Millerf4614452001-07-14 12:18:10 +1000369 fprintf(stderr, "Unknown mac type '%s'\n",
370 optarg);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000371 exit(1);
372 }
373 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000374 case 'M':
Damien Millerb8ea2482004-06-18 22:21:55 +1000375 options.control_master =
376 (options.control_master >= 1) ? 2 : 1;
Damien Miller0e220db2004-06-15 10:34:08 +1000377 break;
Damien Miller95def091999-11-25 00:26:21 +1100378 case 'p':
Ben Lindstrom19066a12001-04-12 23:39:26 +0000379 options.port = a2port(optarg);
380 if (options.port == 0) {
Ben Lindstrom146edb92001-04-11 23:06:28 +0000381 fprintf(stderr, "Bad port '%s'\n", optarg);
382 exit(1);
383 }
Damien Miller95def091999-11-25 00:26:21 +1100384 break;
Damien Miller95def091999-11-25 00:26:21 +1100385 case 'l':
386 options.user = optarg;
387 break;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000388
Damien Miller95def091999-11-25 00:26:21 +1100389 case 'L':
Ben Lindstrom1a174712001-09-12 17:56:15 +0000390 case 'R':
Damien Miller1ea71662003-05-14 22:33:58 +1000391 if (sscanf(optarg, "%5[0123456789]:%255[^:]:%5[0123456789]",
Ben Lindstrom1a174712001-09-12 17:56:15 +0000392 sfwd_port, buf, sfwd_host_port) != 3 &&
Damien Miller1ea71662003-05-14 22:33:58 +1000393 sscanf(optarg, "%5[0123456789]/%255[^/]/%5[0123456789]",
Ben Lindstrom1a174712001-09-12 17:56:15 +0000394 sfwd_port, buf, sfwd_host_port) != 3) {
Damien Millerf4614452001-07-14 12:18:10 +1000395 fprintf(stderr,
Ben Lindstrom1a174712001-09-12 17:56:15 +0000396 "Bad forwarding specification '%s'\n",
Damien Millerf4614452001-07-14 12:18:10 +1000397 optarg);
Damien Miller95def091999-11-25 00:26:21 +1100398 usage();
399 /* NOTREACHED */
400 }
Ben Lindstrom1a174712001-09-12 17:56:15 +0000401 if ((fwd_port = a2port(sfwd_port)) == 0 ||
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000402 (fwd_host_port = a2port(sfwd_host_port)) == 0) {
Ben Lindstrom1a174712001-09-12 17:56:15 +0000403 fprintf(stderr,
404 "Bad forwarding port(s) '%s'\n", optarg);
405 exit(1);
406 }
407 if (opt == 'L')
408 add_local_forward(&options, fwd_port, buf,
409 fwd_host_port);
410 else if (opt == 'R')
411 add_remote_forward(&options, fwd_port, buf,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100412 fwd_host_port);
Damien Miller95def091999-11-25 00:26:21 +1100413 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000414
415 case 'D':
Ben Lindstrom19066a12001-04-12 23:39:26 +0000416 fwd_port = a2port(optarg);
417 if (fwd_port == 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000418 fprintf(stderr, "Bad dynamic port '%s'\n",
419 optarg);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000420 exit(1);
421 }
Darren Tucker1c52ee32003-08-13 20:38:36 +1000422 add_local_forward(&options, fwd_port, "socks", 0);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000423 break;
424
Damien Miller95def091999-11-25 00:26:21 +1100425 case 'C':
426 options.compression = 1;
427 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000428 case 'N':
429 no_shell_flag = 1;
430 no_tty_flag = 1;
431 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000432 case 'T':
433 no_tty_flag = 1;
434 break;
Damien Miller95def091999-11-25 00:26:21 +1100435 case 'o':
436 dummy = 1;
Damien Miller9836cf82003-12-17 16:30:06 +1100437 line = xstrdup(optarg);
Damien Millerf4614452001-07-14 12:18:10 +1000438 if (process_config_line(&options, host ? host : "",
Damien Miller9836cf82003-12-17 16:30:06 +1100439 line, "command-line", 0, &dummy) != 0)
Damien Miller95def091999-11-25 00:26:21 +1100440 exit(1);
Damien Miller9836cf82003-12-17 16:30:06 +1100441 xfree(line);
Damien Miller95def091999-11-25 00:26:21 +1100442 break;
Damien Miller832562e2001-01-30 09:30:01 +1100443 case 's':
444 subsystem_flag = 1;
445 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000446 case 'S':
447 if (options.control_path != NULL)
448 free(options.control_path);
449 options.control_path = xstrdup(optarg);
Damien Miller0e220db2004-06-15 10:34:08 +1000450 break;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000451 case 'b':
452 options.bind_address = optarg;
453 break;
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000454 case 'F':
455 config = optarg;
456 break;
Damien Miller95def091999-11-25 00:26:21 +1100457 default:
458 usage();
459 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000460 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000461
Damien Millerf4614452001-07-14 12:18:10 +1000462 ac -= optind;
463 av += optind;
464
465 if (ac > 0 && !host && **av != '-') {
Ben Lindstromc276c122002-12-23 02:14:51 +0000466 if (strrchr(*av, '@')) {
Damien Millerf4614452001-07-14 12:18:10 +1000467 p = xstrdup(*av);
Ben Lindstromc276c122002-12-23 02:14:51 +0000468 cp = strrchr(p, '@');
Damien Millerf4614452001-07-14 12:18:10 +1000469 if (cp == NULL || cp == p)
470 usage();
471 options.user = p;
472 *cp = '\0';
473 host = ++cp;
474 } else
475 host = *av;
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000476 if (ac > 1) {
477 optind = optreset = 1;
Damien Millerf4614452001-07-14 12:18:10 +1000478 goto again;
479 }
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000480 ac--, av++;
Damien Millerf4614452001-07-14 12:18:10 +1000481 }
482
Damien Miller95def091999-11-25 00:26:21 +1100483 /* Check that we got a host name. */
484 if (!host)
485 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000486
Damien Millercb5e44a2000-09-29 12:12:36 +1100487 SSLeay_add_all_algorithms();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100488 ERR_load_crypto_strings();
Damien Millercb5e44a2000-09-29 12:12:36 +1100489
Damien Miller95def091999-11-25 00:26:21 +1100490 /* Initialize the command to execute on remote host. */
491 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000492
Damien Miller5428f641999-11-25 11:54:57 +1100493 /*
494 * Save the command to execute on the remote host in a buffer. There
495 * is no limit on the length of the command, except by the maximum
496 * packet size. Also sets the tty flag if there is no command.
497 */
Damien Millerf4614452001-07-14 12:18:10 +1000498 if (!ac) {
Damien Miller95def091999-11-25 00:26:21 +1100499 /* No command specified - execute shell on a tty. */
500 tty_flag = 1;
Damien Miller832562e2001-01-30 09:30:01 +1100501 if (subsystem_flag) {
Damien Millerf4614452001-07-14 12:18:10 +1000502 fprintf(stderr,
503 "You must specify a subsystem to invoke.\n");
Damien Miller832562e2001-01-30 09:30:01 +1100504 usage();
505 }
Damien Miller95def091999-11-25 00:26:21 +1100506 } else {
Damien Millerf4614452001-07-14 12:18:10 +1000507 /* A command has been specified. Store it into the buffer. */
508 for (i = 0; i < ac; i++) {
509 if (i)
Damien Miller95def091999-11-25 00:26:21 +1100510 buffer_append(&command, " ", 1);
511 buffer_append(&command, av[i], strlen(av[i]));
512 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000513 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000514
Damien Miller95def091999-11-25 00:26:21 +1100515 /* Cannot fork to background if no command. */
Damien Millercaf6dd62000-08-29 11:33:50 +1100516 if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
Damien Miller95def091999-11-25 00:26:21 +1100517 fatal("Cannot fork into background without a command to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000518
Damien Miller95def091999-11-25 00:26:21 +1100519 /* Allocate a tty by default if no command specified. */
520 if (buffer_len(&command) == 0)
521 tty_flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000522
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000523 /* Force no tty */
Ben Lindstromc72745a2000-12-02 19:03:54 +0000524 if (no_tty_flag)
525 tty_flag = 0;
Damien Miller95def091999-11-25 00:26:21 +1100526 /* Do not allocate a tty if stdin is not a tty. */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000527 if (!isatty(fileno(stdin)) && !force_tty_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100528 if (tty_flag)
Damien Miller996acd22003-04-09 20:59:48 +1000529 logit("Pseudo-terminal will not be allocated because stdin is not a terminal.");
Damien Miller95def091999-11-25 00:26:21 +1100530 tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000531 }
Damien Miller1383bd82000-04-06 12:32:37 +1000532
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000533 /*
534 * Initialize "log" output. Since we are the client all output
535 * actually goes to stderr.
536 */
Ben Lindstrom2b646522001-04-12 16:16:57 +0000537 log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
538 SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000539
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000540 /*
541 * Read per-user configuration file. Ignore the system wide config
542 * file if the user specifies a config file on the command line.
543 */
544 if (config != NULL) {
Damien Miller914420f2004-04-20 20:14:07 +1000545 if (!read_config_file(config, host, &options, 0))
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000546 fatal("Can't open user config file %.100s: "
547 "%.100s", config, strerror(errno));
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000548 } else {
549 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
550 _PATH_SSH_USER_CONFFILE);
Damien Miller57a44762004-04-20 20:11:57 +1000551 (void)read_config_file(buf, host, &options, 1);
Ben Lindstrom83f07d12001-10-03 17:22:29 +0000552
553 /* Read systemwide configuration file after use config. */
Darren Tuckerfc959702004-07-17 16:12:08 +1000554 (void)read_config_file(_PATH_HOST_CONFIG_FILE, host,
Damien Miller57a44762004-04-20 20:11:57 +1000555 &options, 0);
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000556 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000557
Damien Miller95def091999-11-25 00:26:21 +1100558 /* Fill configuration defaults. */
559 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000560
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000561 channel_set_af(options.address_family);
562
Damien Miller95def091999-11-25 00:26:21 +1100563 /* reinit */
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000564 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000565
Damien Miller60bc5172001-03-19 09:38:15 +1100566 seed_rng();
567
Damien Miller95def091999-11-25 00:26:21 +1100568 if (options.user == NULL)
569 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000570
Damien Miller95def091999-11-25 00:26:21 +1100571 if (options.hostname != NULL)
572 host = options.hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000573
Darren Tucker3f521e22003-07-03 16:20:42 +1000574 /* force lowercase for hostkey matching */
575 if (options.host_key_alias != NULL) {
576 for (p = options.host_key_alias; *p; p++)
577 if (isupper(*p))
578 *p = tolower(*p);
579 }
580
Damien Miller9f1e33a2003-02-24 11:57:32 +1100581 if (options.proxy_command != NULL &&
582 strcmp(options.proxy_command, "none") == 0)
583 options.proxy_command = NULL;
584
Damien Miller0e220db2004-06-15 10:34:08 +1000585 if (options.control_path != NULL) {
586 options.control_path = tilde_expand_filename(
587 options.control_path, original_real_uid);
588 }
589 if (options.control_path != NULL && options.control_master == 0)
590 control_client(options.control_path); /* This doesn't return */
591
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000592 /* Open a connection to the remote host. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000593 if (ssh_connect(host, &hostaddr, options.port,
594 options.address_family, options.connection_attempts,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000595#ifdef HAVE_CYGWIN
596 options.use_privileged_port,
597#else
Ben Lindstromf9c48842002-06-11 16:37:51 +0000598 original_effective_uid == 0 && options.use_privileged_port,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000599#endif
Ben Lindstromda394ca2002-06-12 16:11:12 +0000600 options.proxy_command) != 0)
Ben Lindstrom24157572002-06-12 16:09:39 +0000601 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000602
Damien Miller5428f641999-11-25 11:54:57 +1100603 /*
604 * If we successfully made the connection, load the host private key
605 * in case we will need it later for combined rsa-rhosts
606 * authentication. This must be done before releasing extra
607 * privileges, because the file is only readable by root.
Ben Lindstrom9e5bb572002-06-06 19:58:27 +0000608 * If we cannot access the private keys, load the public keys
609 * instead and try to execute the ssh-keysign helper instead.
Damien Miller5428f641999-11-25 11:54:57 +1100610 */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000611 sensitive_data.nkeys = 0;
612 sensitive_data.keys = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000613 sensitive_data.external_keysign = 0;
Ben Lindstrom24157572002-06-12 16:09:39 +0000614 if (options.rhosts_rsa_authentication ||
615 options.hostbased_authentication) {
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000616 sensitive_data.nkeys = 3;
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000617 sensitive_data.keys = xmalloc(sensitive_data.nkeys *
618 sizeof(Key));
Ben Lindstromf9c48842002-06-11 16:37:51 +0000619
620 PRIV_START;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000621 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
Ben Lindstromd0fca422001-03-26 13:44:06 +0000622 _PATH_HOST_KEY_FILE, "", NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000623 sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
624 _PATH_HOST_DSA_KEY_FILE, "", NULL);
625 sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
626 _PATH_HOST_RSA_KEY_FILE, "", NULL);
Ben Lindstromf9c48842002-06-11 16:37:51 +0000627 PRIV_END;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000628
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +0000629 if (options.hostbased_authentication == 1 &&
630 sensitive_data.keys[0] == NULL &&
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000631 sensitive_data.keys[1] == NULL &&
632 sensitive_data.keys[2] == NULL) {
633 sensitive_data.keys[1] = key_load_public(
634 _PATH_HOST_DSA_KEY_FILE, NULL);
635 sensitive_data.keys[2] = key_load_public(
636 _PATH_HOST_RSA_KEY_FILE, NULL);
637 sensitive_data.external_keysign = 1;
638 }
Damien Miller95def091999-11-25 00:26:21 +1100639 }
Damien Miller5428f641999-11-25 11:54:57 +1100640 /*
641 * Get rid of any extra privileges that we may have. We will no
642 * longer need them. Also, extra privileges could make it very hard
643 * to read identity files and other non-world-readable files from the
644 * user's home directory if it happens to be on a NFS volume where
645 * root is mapped to nobody.
646 */
Ben Lindstromf9c48842002-06-11 16:37:51 +0000647 seteuid(original_real_uid);
648 setuid(original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100649
Damien Miller5428f641999-11-25 11:54:57 +1100650 /*
651 * Now that we are back to our own permissions, create ~/.ssh
652 * directory if it doesn\'t already exist.
653 */
Ben Lindstrom930b14a2001-08-15 23:19:21 +0000654 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 +1100655 if (stat(buf, &st) < 0)
Damien Millerbe484b52000-07-15 14:14:16 +1000656 if (mkdir(buf, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100657 error("Could not create directory '%.200s'.", buf);
658
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000659 /* load options.identity_files */
660 load_public_identity_files();
661
662 /* Expand ~ in known host file names. */
663 /* XXX mem-leaks: */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100664 options.system_hostfile =
665 tilde_expand_filename(options.system_hostfile, original_real_uid);
666 options.user_hostfile =
667 tilde_expand_filename(options.user_hostfile, original_real_uid);
668 options.system_hostfile2 =
669 tilde_expand_filename(options.system_hostfile2, original_real_uid);
670 options.user_hostfile2 =
671 tilde_expand_filename(options.user_hostfile2, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100672
Damien Miller07cd5892001-11-12 10:52:03 +1100673 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
674
Damien Miller95def091999-11-25 00:26:21 +1100675 /* Log into the remote system. This never returns if the login fails. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000676 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, pw);
Damien Miller95def091999-11-25 00:26:21 +1100677
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000678 /* We no longer need the private host keys. Clear them now. */
679 if (sensitive_data.nkeys != 0) {
680 for (i = 0; i < sensitive_data.nkeys; i++) {
681 if (sensitive_data.keys[i] != NULL) {
682 /* Destroys contents safely */
683 debug3("clear hostkey %d", i);
684 key_free(sensitive_data.keys[i]);
685 sensitive_data.keys[i] = NULL;
686 }
687 }
688 xfree(sensitive_data.keys);
689 }
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +0000690 for (i = 0; i < options.num_identity_files; i++) {
691 if (options.identity_files[i]) {
692 xfree(options.identity_files[i]);
693 options.identity_files[i] = NULL;
694 }
695 if (options.identity_keys[i]) {
696 key_free(options.identity_keys[i]);
697 options.identity_keys[i] = NULL;
698 }
699 }
Damien Miller95def091999-11-25 00:26:21 +1100700
Damien Miller1383bd82000-04-06 12:32:37 +1000701 exit_status = compat20 ? ssh_session2() : ssh_session();
702 packet_close();
Damien Miller8c4e18a2002-09-19 12:05:02 +1000703
Damien Miller0e220db2004-06-15 10:34:08 +1000704 if (options.control_path != NULL && control_fd != -1)
705 unlink(options.control_path);
706
Damien Miller8c4e18a2002-09-19 12:05:02 +1000707 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100708 * Send SIGHUP to proxy command if used. We don't wait() in
Damien Miller8c4e18a2002-09-19 12:05:02 +1000709 * case it hangs and instead rely on init to reap the child
710 */
711 if (proxy_command_pid > 1)
712 kill(proxy_command_pid, SIGHUP);
713
Damien Miller1383bd82000-04-06 12:32:37 +1000714 return exit_status;
715}
716
Darren Tucker0a118da2003-10-15 15:54:32 +1000717#define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
718
Ben Lindstrombba81212001-06-25 05:01:22 +0000719static void
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000720x11_get_proto(char **_proto, char **_data)
Damien Millerbd483e72000-04-30 10:00:53 +1000721{
Darren Tucker0a118da2003-10-15 15:54:32 +1000722 char cmd[1024];
Damien Millerbd483e72000-04-30 10:00:53 +1000723 char line[512];
Darren Tucker0a118da2003-10-15 15:54:32 +1000724 char xdisplay[512];
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000725 static char proto[512], data[512];
Damien Millerbd483e72000-04-30 10:00:53 +1000726 FILE *f;
Darren Tucker0a118da2003-10-15 15:54:32 +1000727 int got_data = 0, generated = 0, do_unlink = 0, i;
728 char *display, *xauthdir, *xauthfile;
Ben Lindstromee8d52d2002-07-23 21:03:02 +0000729 struct stat st;
Damien Millerbd483e72000-04-30 10:00:53 +1000730
Darren Tucker0a118da2003-10-15 15:54:32 +1000731 xauthdir = xauthfile = NULL;
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000732 *_proto = proto;
733 *_data = data;
734 proto[0] = data[0] = '\0';
Darren Tucker0a118da2003-10-15 15:54:32 +1000735
Ben Lindstromee8d52d2002-07-23 21:03:02 +0000736 if (!options.xauth_location ||
737 (stat(options.xauth_location, &st) == -1)) {
738 debug("No xauth program.");
739 } else {
740 if ((display = getenv("DISPLAY")) == NULL) {
741 debug("x11_get_proto: DISPLAY not set");
742 return;
743 }
Darren Tucker0a118da2003-10-15 15:54:32 +1000744 /*
745 * Handle FamilyLocal case where $DISPLAY does
746 * not match an authorization entry. For this we
747 * just try "xauth list unix:displaynum.screennum".
748 * XXX: "localhost" match to determine FamilyLocal
749 * is not perfect.
750 */
751 if (strncmp(display, "localhost:", 10) == 0) {
752 snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
753 display + 10);
754 display = xdisplay;
755 }
756 if (options.forward_x11_trusted == 0) {
757 xauthdir = xmalloc(MAXPATHLEN);
758 xauthfile = xmalloc(MAXPATHLEN);
759 strlcpy(xauthdir, "/tmp/ssh-XXXXXXXXXX", MAXPATHLEN);
760 if (mkdtemp(xauthdir) != NULL) {
761 do_unlink = 1;
762 snprintf(xauthfile, MAXPATHLEN, "%s/xauthfile",
763 xauthdir);
764 snprintf(cmd, sizeof(cmd),
765 "%s -f %s generate %s " SSH_X11_PROTO
Damien Miller3df755e2004-03-22 09:34:26 +1100766 " untrusted timeout 1200 2>" _PATH_DEVNULL,
Darren Tucker0a118da2003-10-15 15:54:32 +1000767 options.xauth_location, xauthfile, display);
768 debug2("x11_get_proto: %s", cmd);
769 if (system(cmd) == 0)
770 generated = 1;
771 }
772 }
773 snprintf(cmd, sizeof(cmd),
774 "%s %s%s list %s . 2>" _PATH_DEVNULL,
775 options.xauth_location,
776 generated ? "-f " : "" ,
777 generated ? xauthfile : "",
778 display);
779 debug2("x11_get_proto: %s", cmd);
780 f = popen(cmd, "r");
Damien Millerd3a18572000-06-07 19:55:44 +1000781 if (f && fgets(line, sizeof(line), f) &&
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000782 sscanf(line, "%*s %511s %511s", proto, data) == 2)
Damien Millerd3a18572000-06-07 19:55:44 +1000783 got_data = 1;
784 if (f)
785 pclose(f);
786 }
Darren Tucker0a118da2003-10-15 15:54:32 +1000787
788 if (do_unlink) {
789 unlink(xauthfile);
790 rmdir(xauthdir);
791 }
792 if (xauthdir)
793 xfree(xauthdir);
794 if (xauthfile)
795 xfree(xauthfile);
796
Damien Millerbd483e72000-04-30 10:00:53 +1000797 /*
798 * If we didn't get authentication data, just make up some
799 * data. The forwarding code will check the validity of the
800 * response anyway, and substitute this data. The X11
801 * server, however, will ignore this fake data and use
802 * whatever authentication mechanisms it was using otherwise
803 * for the local connection.
804 */
805 if (!got_data) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000806 u_int32_t rnd = 0;
Damien Millerbd483e72000-04-30 10:00:53 +1000807
Darren Tucker0a118da2003-10-15 15:54:32 +1000808 logit("Warning: No xauth data; "
809 "using fake authentication data for X11 forwarding.");
810 strlcpy(proto, SSH_X11_PROTO, sizeof proto);
Damien Millerbd483e72000-04-30 10:00:53 +1000811 for (i = 0; i < 16; i++) {
812 if (i % 4 == 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000813 rnd = arc4random();
Darren Tucker0a118da2003-10-15 15:54:32 +1000814 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000815 rnd & 0xff);
816 rnd >>= 8;
Damien Millerbd483e72000-04-30 10:00:53 +1000817 }
818 }
819}
820
Ben Lindstrombba81212001-06-25 05:01:22 +0000821static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100822ssh_init_forwarding(void)
823{
Kevin Steves12057502001-02-05 14:54:34 +0000824 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100825 int i;
Kevin Steves12057502001-02-05 14:54:34 +0000826
Damien Miller0bc1bd82000-11-13 22:57:25 +1100827 /* Initiate local TCP/IP port forwardings. */
828 for (i = 0; i < options.num_local_forwards; i++) {
829 debug("Connections to local port %d forwarded to remote address %.200s:%d",
830 options.local_forwards[i].port,
831 options.local_forwards[i].host,
832 options.local_forwards[i].host_port);
Damien Millerb16461c2002-01-22 23:29:22 +1100833 success += channel_setup_local_fwd_listener(
Damien Miller0bc1bd82000-11-13 22:57:25 +1100834 options.local_forwards[i].port,
835 options.local_forwards[i].host,
836 options.local_forwards[i].host_port,
837 options.gateway_ports);
838 }
Kevin Steves12057502001-02-05 14:54:34 +0000839 if (i > 0 && success == 0)
840 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100841
842 /* Initiate remote TCP/IP port forwardings. */
843 for (i = 0; i < options.num_remote_forwards; i++) {
844 debug("Connections to remote port %d forwarded to local address %.200s:%d",
845 options.remote_forwards[i].port,
846 options.remote_forwards[i].host,
847 options.remote_forwards[i].host_port);
848 channel_request_remote_forwarding(
849 options.remote_forwards[i].port,
850 options.remote_forwards[i].host,
851 options.remote_forwards[i].host_port);
852 }
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) {
859 /* 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;
873
Damien Miller95def091999-11-25 00:26:21 +1100874 /* Enable compression if requested. */
875 if (options.compression) {
876 debug("Requesting compression at level %d.", options.compression_level);
877
878 if (options.compression_level < 1 || options.compression_level > 9)
879 fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
880
881 /* Send the request. */
882 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
883 packet_put_int(options.compression_level);
884 packet_send();
885 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100886 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100887 if (type == SSH_SMSG_SUCCESS)
888 packet_start_compression(options.compression_level);
889 else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000890 logit("Warning: Remote host refused compression.");
Damien Miller95def091999-11-25 00:26:21 +1100891 else
892 packet_disconnect("Protocol error waiting for compression response.");
893 }
894 /* Allocate a pseudo tty if appropriate. */
895 if (tty_flag) {
896 debug("Requesting pty.");
897
898 /* Start the packet. */
899 packet_start(SSH_CMSG_REQUEST_PTY);
900
901 /* Store TERM in the packet. There is no limit on the
902 length of the string. */
903 cp = getenv("TERM");
904 if (!cp)
905 cp = "";
Ben Lindstrom664408d2001-06-09 01:42:01 +0000906 packet_put_cstring(cp);
Damien Miller95def091999-11-25 00:26:21 +1100907
908 /* Store window size in the packet. */
909 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
910 memset(&ws, 0, sizeof(ws));
911 packet_put_int(ws.ws_row);
912 packet_put_int(ws.ws_col);
913 packet_put_int(ws.ws_xpixel);
914 packet_put_int(ws.ws_ypixel);
915
916 /* Store tty modes in the packet. */
Ben Lindstromae8e2d32001-04-14 23:13:02 +0000917 tty_make_modes(fileno(stdin), NULL);
Damien Miller95def091999-11-25 00:26:21 +1100918
919 /* Send the packet, and wait for it to leave. */
920 packet_send();
921 packet_write_wait();
922
923 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100924 type = packet_read();
Damien Miller450a7a12000-03-26 13:04:51 +1000925 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100926 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +1000927 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000928 } else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000929 logit("Warning: Remote host failed or refused to allocate a pseudo tty.");
Damien Miller95def091999-11-25 00:26:21 +1100930 else
931 packet_disconnect("Protocol error waiting for pty request response.");
932 }
933 /* Request X11 forwarding if enabled and DISPLAY is set. */
934 if (options.forward_x11 && getenv("DISPLAY") != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000935 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +1000936 /* Get reasonable local authentication information. */
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000937 x11_get_proto(&proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +1000938 /* Request forwarding with authentication spoofing. */
Damien Miller95def091999-11-25 00:26:21 +1100939 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Millerbd483e72000-04-30 10:00:53 +1000940 x11_request_forwarding_with_spoofing(0, proto, data);
Damien Miller95def091999-11-25 00:26:21 +1100941
942 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100943 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100944 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100945 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +1000946 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller996acd22003-04-09 20:59:48 +1000947 logit("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +1000948 } else {
Damien Miller95def091999-11-25 00:26:21 +1100949 packet_disconnect("Protocol error waiting for X11 forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +1000950 }
Damien Miller95def091999-11-25 00:26:21 +1100951 }
952 /* Tell the packet module whether this is an interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000953 packet_set_interactive(interactive);
Damien Miller95def091999-11-25 00:26:21 +1100954
955 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100956 check_agent_present();
957
Damien Miller95def091999-11-25 00:26:21 +1100958 if (options.forward_agent) {
959 debug("Requesting authentication agent forwarding.");
960 auth_request_forwarding();
961
962 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100963 type = packet_read();
Damien Miller48b03fc2002-01-22 23:11:40 +1100964 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100965 if (type != SSH_SMSG_SUCCESS)
Damien Miller996acd22003-04-09 20:59:48 +1000966 logit("Warning: Remote host denied authentication agent forwarding.");
Damien Miller95def091999-11-25 00:26:21 +1100967 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000968
Damien Miller0bc1bd82000-11-13 22:57:25 +1100969 /* Initiate port forwardings. */
970 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000971
Damien Miller5428f641999-11-25 11:54:57 +1100972 /* If requested, let ssh continue in the background. */
Damien Miller4af51302000-04-16 11:18:38 +1000973 if (fork_after_authentication_flag)
Damien Miller5428f641999-11-25 11:54:57 +1100974 if (daemon(1, 1) < 0)
975 fatal("daemon() failed: %.200s", strerror(errno));
976
977 /*
978 * If a command was specified on the command line, execute the
979 * command now. Otherwise request the server to start a shell.
980 */
Damien Miller95def091999-11-25 00:26:21 +1100981 if (buffer_len(&command) > 0) {
982 int len = buffer_len(&command);
983 if (len > 900)
984 len = 900;
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100985 debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
Damien Miller95def091999-11-25 00:26:21 +1100986 packet_start(SSH_CMSG_EXEC_CMD);
987 packet_put_string(buffer_ptr(&command), buffer_len(&command));
988 packet_send();
989 packet_write_wait();
990 } else {
991 debug("Requesting shell.");
992 packet_start(SSH_CMSG_EXEC_SHELL);
993 packet_send();
994 packet_write_wait();
995 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000996
Damien Miller95def091999-11-25 00:26:21 +1100997 /* Enter the interactive session. */
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000998 return client_loop(have_tty, tty_flag ?
999 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
Damien Miller1383bd82000-04-06 12:32:37 +10001000}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001001
Ben Lindstrombba81212001-06-25 05:01:22 +00001002static void
Damien Miller0e220db2004-06-15 10:34:08 +10001003ssh_subsystem_reply(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001004{
1005 int id, len;
1006
1007 id = packet_get_int();
1008 len = buffer_len(&command);
Ben Lindstrom4040fe12001-03-05 06:52:57 +00001009 if (len > 900)
1010 len = 900;
Damien Miller48b03fc2002-01-22 23:11:40 +11001011 packet_check_eom();
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001012 if (type == SSH2_MSG_CHANNEL_FAILURE)
1013 fatal("Request for subsystem '%.*s' failed on channel %d",
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001014 len, (u_char *)buffer_ptr(&command), id);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001015}
1016
Damien Miller2797f7f2002-04-23 21:09:44 +10001017void
Damien Miller509b0102003-12-17 16:33:10 +11001018client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt)
Damien Miller2797f7f2002-04-23 21:09:44 +10001019{
1020 int i;
1021
1022 i = client_global_request_id++;
Damien Miller509b0102003-12-17 16:33:10 +11001023 if (i >= options.num_remote_forwards)
Damien Miller2797f7f2002-04-23 21:09:44 +10001024 return;
Damien Miller2797f7f2002-04-23 21:09:44 +10001025 debug("remote forward %s for: listen %d, connect %s:%d",
1026 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
1027 options.remote_forwards[i].port,
1028 options.remote_forwards[i].host,
1029 options.remote_forwards[i].host_port);
1030 if (type == SSH2_MSG_REQUEST_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +10001031 logit("Warning: remote port forwarding failed for listen port %d",
Damien Miller2797f7f2002-04-23 21:09:44 +10001032 options.remote_forwards[i].port);
1033}
1034
Damien Miller0e220db2004-06-15 10:34:08 +10001035static void
1036ssh_control_listener(void)
1037{
1038 struct sockaddr_un addr;
1039 mode_t old_umask;
Damien Miller07b6ff12004-06-15 11:14:45 +10001040 int addr_len;
1041
Damien Miller23f07702004-06-18 01:19:03 +10001042 if (options.control_path == NULL || options.control_master <= 0)
Damien Miller0e220db2004-06-15 10:34:08 +10001043 return;
1044
1045 memset(&addr, '\0', sizeof(addr));
1046 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001047 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001048 strlen(options.control_path) + 1;
1049
1050 if (strlcpy(addr.sun_path, options.control_path,
1051 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1052 fatal("ControlPath too long");
1053
1054 if ((control_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1055 fatal("%s socket(): %s\n", __func__, strerror(errno));
1056
1057 old_umask = umask(0177);
Damien Miller07b6ff12004-06-15 11:14:45 +10001058 if (bind(control_fd, (struct sockaddr*)&addr, addr_len) == -1) {
Damien Miller0e220db2004-06-15 10:34:08 +10001059 control_fd = -1;
1060 if (errno == EINVAL)
1061 fatal("ControlSocket %s already exists",
1062 options.control_path);
1063 else
1064 fatal("%s bind(): %s\n", __func__, strerror(errno));
1065 }
1066 umask(old_umask);
1067
1068 if (listen(control_fd, 64) == -1)
1069 fatal("%s listen(): %s\n", __func__, strerror(errno));
1070
1071 set_nonblock(control_fd);
1072}
1073
Ben Lindstromf558cf62001-09-20 23:13:49 +00001074/* request pty/x11/agent/tcpfwd/shell for channel */
Ben Lindstrombba81212001-06-25 05:01:22 +00001075static void
Ben Lindstromf558cf62001-09-20 23:13:49 +00001076ssh_session2_setup(int id, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +10001077{
Damien Miller3756dce2004-06-18 01:17:29 +10001078 extern char **environ;
1079
Damien Miller0e220db2004-06-15 10:34:08 +10001080 int interactive = tty_flag;
1081 if (options.forward_x11 && getenv("DISPLAY") != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001082 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001083 /* Get reasonable local authentication information. */
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001084 x11_get_proto(&proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001085 /* Request forwarding with authentication spoofing. */
1086 debug("Requesting X11 forwarding with authentication spoofing.");
1087 x11_request_forwarding_with_spoofing(id, proto, data);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001088 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001089 /* XXX wait for reply */
1090 }
1091
Damien Miller0bc1bd82000-11-13 22:57:25 +11001092 check_agent_present();
1093 if (options.forward_agent) {
1094 debug("Requesting authentication agent forwarding.");
1095 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1096 packet_send();
1097 }
1098
Damien Miller0e220db2004-06-15 10:34:08 +10001099 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
Damien Miller3756dce2004-06-18 01:17:29 +10001100 NULL, fileno(stdin), &command, environ, &ssh_subsystem_reply);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001101
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001102 packet_set_interactive(interactive);
Damien Miller1383bd82000-04-06 12:32:37 +10001103}
1104
Ben Lindstromf558cf62001-09-20 23:13:49 +00001105/* open new channel for a session */
Ben Lindstrombba81212001-06-25 05:01:22 +00001106static int
Ben Lindstromf558cf62001-09-20 23:13:49 +00001107ssh_session2_open(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001108{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001109 Channel *c;
1110 int window, packetmax, in, out, err;
Damien Millerad833b32000-08-23 10:46:23 +10001111
Damien Millercaf6dd62000-08-29 11:33:50 +11001112 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001113 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001114 } else {
1115 in = dup(STDIN_FILENO);
1116 }
1117 out = dup(STDOUT_FILENO);
1118 err = dup(STDERR_FILENO);
1119
1120 if (in < 0 || out < 0 || err < 0)
1121 fatal("dup() in/out/err failed");
1122
Damien Miller69b69aa2000-10-28 14:19:58 +11001123 /* enable nonblocking unless tty */
1124 if (!isatty(in))
1125 set_nonblock(in);
1126 if (!isatty(out))
1127 set_nonblock(out);
1128 if (!isatty(err))
1129 set_nonblock(err);
1130
Damien Millere4340be2000-09-16 13:29:08 +11001131 window = CHAN_SES_WINDOW_DEFAULT;
1132 packetmax = CHAN_SES_PACKET_DEFAULT;
Damien Miller19a59452002-02-19 15:20:57 +11001133 if (tty_flag) {
1134 window >>= 1;
1135 packetmax >>= 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001136 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001137 c = channel_new(
Damien Miller1383bd82000-04-06 12:32:37 +10001138 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001139 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001140 "client-session", /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001141
Ben Lindstromf558cf62001-09-20 23:13:49 +00001142 debug3("ssh_session2_open: channel_new: %d", c->self);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001143
Ben Lindstrom5ec26452001-06-09 00:18:51 +00001144 channel_send_open(c->self);
Ben Lindstromf558cf62001-09-20 23:13:49 +00001145 if (!no_shell_flag)
Damien Miller0e220db2004-06-15 10:34:08 +10001146 channel_register_confirm(c->self, ssh_session2_setup, NULL);
Damien Miller1383bd82000-04-06 12:32:37 +10001147
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001148 return c->self;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001149}
1150
Ben Lindstrombba81212001-06-25 05:01:22 +00001151static int
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001152ssh_session2(void)
1153{
Ben Lindstromf558cf62001-09-20 23:13:49 +00001154 int id = -1;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001155
1156 /* XXX should be pre-session */
1157 ssh_init_forwarding();
Damien Miller0e220db2004-06-15 10:34:08 +10001158 ssh_control_listener();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001159
Ben Lindstromf558cf62001-09-20 23:13:49 +00001160 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1161 id = ssh_session2_open();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001162
1163 /* If requested, let ssh continue in the background. */
1164 if (fork_after_authentication_flag)
1165 if (daemon(1, 1) < 0)
1166 fatal("daemon() failed: %.200s", strerror(errno));
1167
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001168 return client_loop(tty_flag, tty_flag ?
1169 options.escape_char : SSH_ESCAPECHAR_NONE, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001170}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001171
Ben Lindstrombba81212001-06-25 05:01:22 +00001172static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001173load_public_identity_files(void)
1174{
1175 char *filename;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001176 int i = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001177 Key *public;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001178#ifdef SMARTCARD
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001179 Key **keys;
1180
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001181 if (options.smartcard_device != NULL &&
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001182 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1183 (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL ) {
1184 int count = 0;
1185 for (i = 0; keys[i] != NULL; i++) {
1186 count++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001187 memmove(&options.identity_files[1], &options.identity_files[0],
1188 sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
1189 memmove(&options.identity_keys[1], &options.identity_keys[0],
1190 sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1191 options.num_identity_files++;
1192 options.identity_keys[0] = keys[i];
Damien Miller56a0bb02003-06-18 20:28:40 +10001193 options.identity_files[0] = sc_get_key_label(keys[i]);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001194 }
Ben Lindstrom4f054602002-03-26 03:23:00 +00001195 if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1196 options.num_identity_files = SSH_MAX_IDENTITY_FILES;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001197 i = count;
1198 xfree(keys);
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001199 }
Ben Lindstromffce1472001-08-06 21:57:31 +00001200#endif /* SMARTCARD */
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001201 for (; i < options.num_identity_files; i++) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001202 filename = tilde_expand_filename(options.identity_files[i],
1203 original_real_uid);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001204 public = key_load_public(filename, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001205 debug("identity file %s type %d", filename,
1206 public ? public->type : -1);
1207 xfree(options.identity_files[i]);
1208 options.identity_files[i] = filename;
1209 options.identity_keys[i] = public;
1210 }
1211}
Damien Miller0e220db2004-06-15 10:34:08 +10001212
1213static void
1214control_client_sighandler(int signo)
1215{
1216 control_client_terminate = signo;
1217}
1218
1219static void
1220control_client_sigrelay(int signo)
1221{
1222 if (control_server_pid > 1)
1223 kill(control_server_pid, signo);
1224}
1225
Darren Tucker365433f2004-06-22 12:29:23 +10001226static int
1227env_permitted(char *env)
1228{
1229 int i;
1230 char name[1024], *cp;
1231
1232 strlcpy(name, env, sizeof(name));
1233 if ((cp = strchr(name, '=')) == NULL)
1234 return (0);
1235
1236 *cp = '\0';
1237
1238 for (i = 0; i < options.num_send_env; i++)
1239 if (match_pattern(name, options.send_env[i]))
1240 return (1);
1241
1242 return (0);
1243}
1244
Damien Miller0e220db2004-06-15 10:34:08 +10001245static void
1246control_client(const char *path)
1247{
1248 struct sockaddr_un addr;
Darren Tucker365433f2004-06-22 12:29:23 +10001249 int i, r, sock, exitval, num_env, addr_len;
Damien Miller0e220db2004-06-15 10:34:08 +10001250 Buffer m;
1251 char *cp;
Damien Miller3756dce2004-06-18 01:17:29 +10001252 extern char **environ;
Darren Tuckerfc959702004-07-17 16:12:08 +10001253
Damien Miller0e220db2004-06-15 10:34:08 +10001254 memset(&addr, '\0', sizeof(addr));
1255 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001256 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001257 strlen(path) + 1;
1258
1259 if (strlcpy(addr.sun_path, path,
1260 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1261 fatal("ControlPath too long");
1262
1263 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1264 fatal("%s socket(): %s", __func__, strerror(errno));
1265
Damien Miller07b6ff12004-06-15 11:14:45 +10001266 if (connect(sock, (struct sockaddr*)&addr, addr_len) == -1)
Damien Miller0e220db2004-06-15 10:34:08 +10001267 fatal("Couldn't connect to %s: %s", path, strerror(errno));
1268
1269 if ((cp = getenv("TERM")) == NULL)
1270 cp = "";
1271
Damien Miller0e220db2004-06-15 10:34:08 +10001272 buffer_init(&m);
1273
1274 /* Get PID of controlee */
1275 if (ssh_msg_recv(sock, &m) == -1)
1276 fatal("%s: msg_recv", __func__);
1277 if (buffer_get_char(&m) != 0)
1278 fatal("%s: wrong version", __func__);
Damien Miller23f07702004-06-18 01:19:03 +10001279 /* Connection allowed? */
1280 if (buffer_get_int(&m) != 1)
1281 fatal("Connection to master denied");
Damien Miller0e220db2004-06-15 10:34:08 +10001282 control_server_pid = buffer_get_int(&m);
1283
Damien Miller0e220db2004-06-15 10:34:08 +10001284 buffer_clear(&m);
1285 buffer_put_int(&m, tty_flag);
1286 buffer_put_int(&m, subsystem_flag);
1287 buffer_put_cstring(&m, cp);
1288
1289 buffer_append(&command, "\0", 1);
1290 buffer_put_cstring(&m, buffer_ptr(&command));
1291
Darren Tucker365433f2004-06-22 12:29:23 +10001292 if (options.num_send_env == 0 || environ == NULL) {
1293 buffer_put_int(&m, 0);
Darren Tuckerfc959702004-07-17 16:12:08 +10001294 } else {
Darren Tucker365433f2004-06-22 12:29:23 +10001295 /* Pass environment */
1296 num_env = 0;
1297 for (i = 0; environ[i] != NULL; i++)
1298 if (env_permitted(environ[i]))
1299 num_env++; /* Count */
Darren Tuckerfc959702004-07-17 16:12:08 +10001300
Darren Tucker365433f2004-06-22 12:29:23 +10001301 buffer_put_int(&m, num_env);
1302
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001303 for (i = 0; environ[i] != NULL && num_env >= 0; i++)
1304 if (env_permitted(environ[i])) {
1305 num_env--;
Darren Tucker365433f2004-06-22 12:29:23 +10001306 buffer_put_cstring(&m, environ[i]);
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001307 }
Darren Tucker365433f2004-06-22 12:29:23 +10001308 }
Damien Miller3756dce2004-06-18 01:17:29 +10001309
Damien Miller0e220db2004-06-15 10:34:08 +10001310 if (ssh_msg_send(sock, /* version */0, &m) == -1)
1311 fatal("%s: msg_send", __func__);
1312
1313 mm_send_fd(sock, STDIN_FILENO);
1314 mm_send_fd(sock, STDOUT_FILENO);
1315 mm_send_fd(sock, STDERR_FILENO);
1316
1317 /* Wait for reply, so master has a chance to gather ttymodes */
1318 buffer_clear(&m);
1319 if (ssh_msg_recv(sock, &m) == -1)
1320 fatal("%s: msg_recv", __func__);
1321 if (buffer_get_char(&m) != 0)
1322 fatal("%s: master returned error", __func__);
1323 buffer_free(&m);
1324
Damien Miller0809e232004-06-18 22:20:57 +10001325 signal(SIGINT, control_client_sighandler);
1326 signal(SIGTERM, control_client_sighandler);
1327 signal(SIGWINCH, control_client_sigrelay);
1328
Damien Miller0e220db2004-06-15 10:34:08 +10001329 if (tty_flag)
1330 enter_raw_mode();
1331
1332 /* Stick around until the controlee closes the client_fd */
1333 exitval = 0;
1334 for (;!control_client_terminate;) {
1335 r = read(sock, &exitval, sizeof(exitval));
1336 if (r == 0) {
1337 debug2("Received EOF from master");
1338 break;
1339 }
1340 if (r > 0)
1341 debug2("Received exit status from master %d", exitval);
1342 if (r == -1 && errno != EINTR)
1343 fatal("%s: read %s", __func__, strerror(errno));
1344 }
1345
1346 if (control_client_terminate)
1347 debug2("Exiting on signal %d", control_client_terminate);
1348
1349 close(sock);
1350
1351 leave_raw_mode();
1352
1353 if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
1354 fprintf(stderr, "Connection to master closed.\r\n");
1355
1356 exit(exitval);
1357}