blob: 6f8114d53b063dba464adac309481e4321daa432 [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 Miller23f07702004-06-18 01:19:03 +100043RCSID("$OpenBSD: ssh.c,v 1.216 2004/06/17 15:10:14 djm 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 +110079#ifdef HAVE___PROGNAME
80extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000081#else
82char *__progname;
83#endif
Damien Miller3f905871999-11-15 17:10:57 +110084
Damien Miller95def091999-11-25 00:26:21 +110085/* Flag indicating whether debug mode is on. This can be set on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086int debug_flag = 0;
87
Damien Miller78928792000-04-12 20:17:38 +100088/* Flag indicating whether a tty should be allocated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089int tty_flag = 0;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +000090int no_tty_flag = 0;
91int force_tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092
Damien Miller1383bd82000-04-06 12:32:37 +100093/* don't exec a shell */
94int no_shell_flag = 0;
Damien Miller1383bd82000-04-06 12:32:37 +100095
Damien Miller5428f641999-11-25 11:54:57 +110096/*
97 * Flag indicating that nothing should be read from stdin. This can be set
98 * on the command line.
99 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100int stdin_null_flag = 0;
101
Damien Miller5428f641999-11-25 11:54:57 +1100102/*
103 * Flag indicating that ssh should fork after authentication. This is useful
Ben Lindstromf666fec2002-06-06 19:51:58 +0000104 * so that the passphrase can be entered manually, and then ssh goes to the
Damien Miller5428f641999-11-25 11:54:57 +1100105 * background.
106 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107int fork_after_authentication_flag = 0;
108
Damien Miller5428f641999-11-25 11:54:57 +1100109/*
110 * General data structure for command line options and options configurable
111 * in configuration files. See readconf.h.
112 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113Options options;
114
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000115/* optional user configfile */
116char *config = NULL;
117
Damien Miller5428f641999-11-25 11:54:57 +1100118/*
119 * Name of the host we are connecting to. This is the name given on the
120 * command line, or the HostName specified for the user-supplied name in a
121 * configuration file.
122 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123char *host;
124
125/* socket address the host resolves to */
Damien Miller34132e52000-01-14 15:45:46 +1100126struct sockaddr_storage hostaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000128/* Private host keys. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000129Sensitive sensitive_data;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000130
131/* Original real UID. */
132uid_t original_real_uid;
Ben Lindstromf9c48842002-06-11 16:37:51 +0000133uid_t original_effective_uid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134
Damien Miller1383bd82000-04-06 12:32:37 +1000135/* command to be executed */
136Buffer command;
137
Damien Miller832562e2001-01-30 09:30:01 +1100138/* Should we execute a command or invoke a subsystem? */
139int subsystem_flag = 0;
140
Damien Miller2797f7f2002-04-23 21:09:44 +1000141/* # of replies received for global requests */
142static int client_global_request_id = 0;
143
Damien Miller8c4e18a2002-09-19 12:05:02 +1000144/* pid of proxycommand child process */
145pid_t proxy_command_pid = 0;
146
Damien Miller0e220db2004-06-15 10:34:08 +1000147/* fd to control socket */
148int control_fd = -1;
149
150/* Only used in control client mode */
151volatile sig_atomic_t control_client_terminate = 0;
152u_int control_server_pid = 0;
153
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154/* Prints a help message to the user. This function never returns. */
155
Ben Lindstrombba81212001-06-25 05:01:22 +0000156static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000157usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158{
Damien Miller50955102004-03-22 09:34:58 +1100159 fprintf(stderr,
160"usage: ssh [-1246AaCfghkNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n"
161" [-D port] [-e escape_char] [-F configfile] [-i identity_file]\n"
162" [-L port:host:hostport] [-l login_name] [-m mac_spec] [-o option]\n"
163" [-p port] [-R port:host:hostport] [user@]hostname [command]\n"
164 );
Damien Miller95def091999-11-25 00:26:21 +1100165 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000166}
167
Ben Lindstrombba81212001-06-25 05:01:22 +0000168static int ssh_session(void);
169static int ssh_session2(void);
170static void load_public_identity_files(void);
Damien Miller0e220db2004-06-15 10:34:08 +1000171static void control_client(const char *path);
Damien Miller1383bd82000-04-06 12:32:37 +1000172
Damien Miller95def091999-11-25 00:26:21 +1100173/*
174 * Main program for the ssh client.
175 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000176int
177main(int ac, char **av)
178{
Ben Lindstrom24157572002-06-12 16:09:39 +0000179 int i, opt, exit_status;
Damien Milleraae6c611999-12-06 11:47:28 +1100180 u_short fwd_port, fwd_host_port;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000181 char sfwd_port[6], sfwd_host_port[6];
Damien Miller9836cf82003-12-17 16:30:06 +1100182 char *p, *cp, *line, buf[256];
Damien Miller95def091999-11-25 00:26:21 +1100183 struct stat st;
Ben Lindstrom086cf212001-03-05 05:56:40 +0000184 struct passwd *pw;
Damien Miller1383bd82000-04-06 12:32:37 +1000185 int dummy;
Ben Lindstrom5ccf63a2001-09-24 20:00:10 +0000186 extern int optind, optreset;
Damien Miller4f8e6692001-07-14 13:22:53 +1000187 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000188
Damien Miller59d3d5b2003-08-22 09:34:41 +1000189 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000190 init_rng();
191
Damien Miller5428f641999-11-25 11:54:57 +1100192 /*
193 * Save the original real uid. It will be needed later (uid-swapping
194 * may clobber the real uid).
195 */
Damien Miller95def091999-11-25 00:26:21 +1100196 original_real_uid = getuid();
197 original_effective_uid = geteuid();
Damien Millera8e06ce2003-11-21 23:48:55 +1100198
Damien Miller50b9a602002-09-04 16:50:06 +1000199 /*
200 * Use uid-swapping to give up root privileges for the duration of
201 * option processing. We will re-instantiate the rights when we are
202 * ready to create the privileged port, and will permanently drop
203 * them when the port has been created (actually, when the connection
204 * has been made, as we may need to create the port several times).
205 */
206 PRIV_END;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000207
Damien Miller05dd7952000-10-01 00:42:48 +1100208#ifdef HAVE_SETRLIMIT
Damien Miller95def091999-11-25 00:26:21 +1100209 /* If we are installed setuid root be careful to not drop core. */
210 if (original_real_uid != original_effective_uid) {
211 struct rlimit rlim;
212 rlim.rlim_cur = rlim.rlim_max = 0;
213 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
214 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100216#endif
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000217 /* Get user data. */
218 pw = getpwuid(original_real_uid);
219 if (!pw) {
Damien Miller996acd22003-04-09 20:59:48 +1000220 logit("You don't exist, go away!");
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000221 exit(1);
222 }
223 /* Take a copy of the returned structure. */
224 pw = pwcopy(pw);
225
Damien Miller5428f641999-11-25 11:54:57 +1100226 /*
Damien Miller5428f641999-11-25 11:54:57 +1100227 * Set our umask to something reasonable, as some files are created
228 * with the default umask. This will make them world-readable but
229 * writable only by the owner, which is ok for all files for which we
230 * don't set the modes explicitly.
231 */
Damien Miller95def091999-11-25 00:26:21 +1100232 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000233
Damien Miller95def091999-11-25 00:26:21 +1100234 /* Initialize option structure to indicate that no values have been set. */
235 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000236
Damien Miller95def091999-11-25 00:26:21 +1100237 /* Parse command-line arguments. */
238 host = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239
Damien Millerf4614452001-07-14 12:18:10 +1000240again:
241 while ((opt = getopt(ac, av,
Damien Miller0e220db2004-06-15 10:34:08 +1000242 "1246ab:c:e:fgi:kl:m:no:p:qstvxACD:F:I:L:MNPR:S:TVXY")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100243 switch (opt) {
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000244 case '1':
245 options.protocol = SSH_PROTO_1;
246 break;
Damien Miller4af51302000-04-16 11:18:38 +1000247 case '2':
248 options.protocol = SSH_PROTO_2;
249 break;
Damien Miller34132e52000-01-14 15:45:46 +1100250 case '4':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000251 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100252 break;
Damien Miller34132e52000-01-14 15:45:46 +1100253 case '6':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000254 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100255 break;
Damien Miller95def091999-11-25 00:26:21 +1100256 case 'n':
257 stdin_null_flag = 1;
258 break;
Damien Miller95def091999-11-25 00:26:21 +1100259 case 'f':
260 fork_after_authentication_flag = 1;
261 stdin_null_flag = 1;
262 break;
Damien Miller95def091999-11-25 00:26:21 +1100263 case 'x':
264 options.forward_x11 = 0;
265 break;
Damien Miller95def091999-11-25 00:26:21 +1100266 case 'X':
267 options.forward_x11 = 1;
268 break;
Darren Tucker0a118da2003-10-15 15:54:32 +1000269 case 'Y':
270 options.forward_x11 = 1;
271 options.forward_x11_trusted = 1;
272 break;
Damien Miller95def091999-11-25 00:26:21 +1100273 case 'g':
274 options.gateway_ports = 1;
275 break;
Damien Miller147bba32002-09-04 16:46:06 +1000276 case 'P': /* deprecated */
Damien Miller95def091999-11-25 00:26:21 +1100277 options.use_privileged_port = 0;
278 break;
Damien Miller95def091999-11-25 00:26:21 +1100279 case 'a':
280 options.forward_agent = 0;
281 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000282 case 'A':
283 options.forward_agent = 1;
284 break;
Damien Miller95def091999-11-25 00:26:21 +1100285 case 'k':
Damien Millere0113cc2003-11-24 13:10:09 +1100286 options.gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100287 break;
Damien Miller95def091999-11-25 00:26:21 +1100288 case 'i':
289 if (stat(optarg, &st) < 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000290 fprintf(stderr, "Warning: Identity file %s "
291 "does not exist.\n", optarg);
Damien Miller95def091999-11-25 00:26:21 +1100292 break;
293 }
Damien Millerf4614452001-07-14 12:18:10 +1000294 if (options.num_identity_files >=
295 SSH_MAX_IDENTITY_FILES)
296 fatal("Too many identity files specified "
297 "(max %d)", SSH_MAX_IDENTITY_FILES);
298 options.identity_files[options.num_identity_files++] =
299 xstrdup(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100300 break;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000301 case 'I':
302#ifdef SMARTCARD
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000303 options.smartcard_device = xstrdup(optarg);
Ben Lindstrombcc18082001-08-06 21:59:25 +0000304#else
Ben Lindstromc5b68002001-07-04 04:52:03 +0000305 fprintf(stderr, "no support for smartcards.\n");
Ben Lindstrombcc18082001-08-06 21:59:25 +0000306#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000307 break;
Damien Miller95def091999-11-25 00:26:21 +1100308 case 't':
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000309 if (tty_flag)
310 force_tty_flag = 1;
Damien Miller95def091999-11-25 00:26:21 +1100311 tty_flag = 1;
312 break;
Damien Miller95def091999-11-25 00:26:21 +1100313 case 'v':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000314 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100315 debug_flag = 1;
316 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000317 } else {
318 if (options.log_level < SYSLOG_LEVEL_DEBUG3)
319 options.log_level++;
Damien Millere4340be2000-09-16 13:29:08 +1100320 break;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000321 }
Damien Millere4340be2000-09-16 13:29:08 +1100322 /* fallthrough */
Damien Miller95def091999-11-25 00:26:21 +1100323 case 'V':
Damien Miller0c889cd2004-03-22 09:36:00 +1100324 fprintf(stderr, "%s, %s\n",
325 SSH_VERSION, SSLeay_version(SSLEAY_VERSION));
Damien Miller95def091999-11-25 00:26:21 +1100326 if (opt == 'V')
327 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100328 break;
Damien Miller95def091999-11-25 00:26:21 +1100329 case 'q':
330 options.log_level = SYSLOG_LEVEL_QUIET;
331 break;
Damien Miller95def091999-11-25 00:26:21 +1100332 case 'e':
333 if (optarg[0] == '^' && optarg[2] == 0 &&
Damien Millerf4614452001-07-14 12:18:10 +1000334 (u_char) optarg[1] >= 64 &&
335 (u_char) optarg[1] < 128)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000336 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100337 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000338 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100339 else if (strcmp(optarg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000340 options.escape_char = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100341 else {
Damien Millerf4614452001-07-14 12:18:10 +1000342 fprintf(stderr, "Bad escape character '%s'.\n",
343 optarg);
Damien Miller95def091999-11-25 00:26:21 +1100344 exit(1);
345 }
346 break;
Damien Miller95def091999-11-25 00:26:21 +1100347 case 'c':
Damien Millereba71ba2000-04-29 23:57:08 +1000348 if (ciphers_valid(optarg)) {
349 /* SSH2 only */
350 options.ciphers = xstrdup(optarg);
Damien Miller30c3d422000-05-09 11:02:59 +1000351 options.cipher = SSH_CIPHER_ILLEGAL;
Damien Millereba71ba2000-04-29 23:57:08 +1000352 } else {
353 /* SSH1 only */
Damien Millere39cacc2000-11-29 12:18:44 +1100354 options.cipher = cipher_number(optarg);
355 if (options.cipher == -1) {
Damien Millerf4614452001-07-14 12:18:10 +1000356 fprintf(stderr,
357 "Unknown cipher type '%s'\n",
358 optarg);
Damien Millereba71ba2000-04-29 23:57:08 +1000359 exit(1);
360 }
Damien Millerf4614452001-07-14 12:18:10 +1000361 if (options.cipher == SSH_CIPHER_3DES)
Damien Millere39cacc2000-11-29 12:18:44 +1100362 options.ciphers = "3des-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000363 else if (options.cipher == SSH_CIPHER_BLOWFISH)
Damien Millere39cacc2000-11-29 12:18:44 +1100364 options.ciphers = "blowfish-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000365 else
Damien Millere39cacc2000-11-29 12:18:44 +1100366 options.ciphers = (char *)-1;
Damien Miller95def091999-11-25 00:26:21 +1100367 }
368 break;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000369 case 'm':
370 if (mac_valid(optarg))
371 options.macs = xstrdup(optarg);
372 else {
Damien Millerf4614452001-07-14 12:18:10 +1000373 fprintf(stderr, "Unknown mac type '%s'\n",
374 optarg);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000375 exit(1);
376 }
377 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000378 case 'M':
379 options.control_master = 1;
380 break;
Damien Miller95def091999-11-25 00:26:21 +1100381 case 'p':
Ben Lindstrom19066a12001-04-12 23:39:26 +0000382 options.port = a2port(optarg);
383 if (options.port == 0) {
Ben Lindstrom146edb92001-04-11 23:06:28 +0000384 fprintf(stderr, "Bad port '%s'\n", optarg);
385 exit(1);
386 }
Damien Miller95def091999-11-25 00:26:21 +1100387 break;
Damien Miller95def091999-11-25 00:26:21 +1100388 case 'l':
389 options.user = optarg;
390 break;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000391
Damien Miller95def091999-11-25 00:26:21 +1100392 case 'L':
Ben Lindstrom1a174712001-09-12 17:56:15 +0000393 case 'R':
Damien Miller1ea71662003-05-14 22:33:58 +1000394 if (sscanf(optarg, "%5[0123456789]:%255[^:]:%5[0123456789]",
Ben Lindstrom1a174712001-09-12 17:56:15 +0000395 sfwd_port, buf, sfwd_host_port) != 3 &&
Damien Miller1ea71662003-05-14 22:33:58 +1000396 sscanf(optarg, "%5[0123456789]/%255[^/]/%5[0123456789]",
Ben Lindstrom1a174712001-09-12 17:56:15 +0000397 sfwd_port, buf, sfwd_host_port) != 3) {
Damien Millerf4614452001-07-14 12:18:10 +1000398 fprintf(stderr,
Ben Lindstrom1a174712001-09-12 17:56:15 +0000399 "Bad forwarding specification '%s'\n",
Damien Millerf4614452001-07-14 12:18:10 +1000400 optarg);
Damien Miller95def091999-11-25 00:26:21 +1100401 usage();
402 /* NOTREACHED */
403 }
Ben Lindstrom1a174712001-09-12 17:56:15 +0000404 if ((fwd_port = a2port(sfwd_port)) == 0 ||
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000405 (fwd_host_port = a2port(sfwd_host_port)) == 0) {
Ben Lindstrom1a174712001-09-12 17:56:15 +0000406 fprintf(stderr,
407 "Bad forwarding port(s) '%s'\n", optarg);
408 exit(1);
409 }
410 if (opt == 'L')
411 add_local_forward(&options, fwd_port, buf,
412 fwd_host_port);
413 else if (opt == 'R')
414 add_remote_forward(&options, fwd_port, buf,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100415 fwd_host_port);
Damien Miller95def091999-11-25 00:26:21 +1100416 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000417
418 case 'D':
Ben Lindstrom19066a12001-04-12 23:39:26 +0000419 fwd_port = a2port(optarg);
420 if (fwd_port == 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000421 fprintf(stderr, "Bad dynamic port '%s'\n",
422 optarg);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000423 exit(1);
424 }
Darren Tucker1c52ee32003-08-13 20:38:36 +1000425 add_local_forward(&options, fwd_port, "socks", 0);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000426 break;
427
Damien Miller95def091999-11-25 00:26:21 +1100428 case 'C':
429 options.compression = 1;
430 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000431 case 'N':
432 no_shell_flag = 1;
433 no_tty_flag = 1;
434 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000435 case 'T':
436 no_tty_flag = 1;
437 break;
Damien Miller95def091999-11-25 00:26:21 +1100438 case 'o':
439 dummy = 1;
Damien Miller9836cf82003-12-17 16:30:06 +1100440 line = xstrdup(optarg);
Damien Millerf4614452001-07-14 12:18:10 +1000441 if (process_config_line(&options, host ? host : "",
Damien Miller9836cf82003-12-17 16:30:06 +1100442 line, "command-line", 0, &dummy) != 0)
Damien Miller95def091999-11-25 00:26:21 +1100443 exit(1);
Damien Miller9836cf82003-12-17 16:30:06 +1100444 xfree(line);
Damien Miller95def091999-11-25 00:26:21 +1100445 break;
Damien Miller832562e2001-01-30 09:30:01 +1100446 case 's':
447 subsystem_flag = 1;
448 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000449 case 'S':
450 if (options.control_path != NULL)
451 free(options.control_path);
452 options.control_path = xstrdup(optarg);
453 if (options.control_master == -1)
454 options.control_master = 0;
455 break;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000456 case 'b':
457 options.bind_address = optarg;
458 break;
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000459 case 'F':
460 config = optarg;
461 break;
Damien Miller95def091999-11-25 00:26:21 +1100462 default:
463 usage();
464 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000465 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000466
Damien Millerf4614452001-07-14 12:18:10 +1000467 ac -= optind;
468 av += optind;
469
470 if (ac > 0 && !host && **av != '-') {
Ben Lindstromc276c122002-12-23 02:14:51 +0000471 if (strrchr(*av, '@')) {
Damien Millerf4614452001-07-14 12:18:10 +1000472 p = xstrdup(*av);
Ben Lindstromc276c122002-12-23 02:14:51 +0000473 cp = strrchr(p, '@');
Damien Millerf4614452001-07-14 12:18:10 +1000474 if (cp == NULL || cp == p)
475 usage();
476 options.user = p;
477 *cp = '\0';
478 host = ++cp;
479 } else
480 host = *av;
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000481 if (ac > 1) {
482 optind = optreset = 1;
Damien Millerf4614452001-07-14 12:18:10 +1000483 goto again;
484 }
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000485 ac--, av++;
Damien Millerf4614452001-07-14 12:18:10 +1000486 }
487
Damien Miller95def091999-11-25 00:26:21 +1100488 /* Check that we got a host name. */
489 if (!host)
490 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000491
Damien Millercb5e44a2000-09-29 12:12:36 +1100492 SSLeay_add_all_algorithms();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100493 ERR_load_crypto_strings();
Damien Millercb5e44a2000-09-29 12:12:36 +1100494
Damien Miller95def091999-11-25 00:26:21 +1100495 /* Initialize the command to execute on remote host. */
496 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000497
Damien Miller5428f641999-11-25 11:54:57 +1100498 /*
499 * Save the command to execute on the remote host in a buffer. There
500 * is no limit on the length of the command, except by the maximum
501 * packet size. Also sets the tty flag if there is no command.
502 */
Damien Millerf4614452001-07-14 12:18:10 +1000503 if (!ac) {
Damien Miller95def091999-11-25 00:26:21 +1100504 /* No command specified - execute shell on a tty. */
505 tty_flag = 1;
Damien Miller832562e2001-01-30 09:30:01 +1100506 if (subsystem_flag) {
Damien Millerf4614452001-07-14 12:18:10 +1000507 fprintf(stderr,
508 "You must specify a subsystem to invoke.\n");
Damien Miller832562e2001-01-30 09:30:01 +1100509 usage();
510 }
Damien Miller95def091999-11-25 00:26:21 +1100511 } else {
Damien Millerf4614452001-07-14 12:18:10 +1000512 /* A command has been specified. Store it into the buffer. */
513 for (i = 0; i < ac; i++) {
514 if (i)
Damien Miller95def091999-11-25 00:26:21 +1100515 buffer_append(&command, " ", 1);
516 buffer_append(&command, av[i], strlen(av[i]));
517 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000518 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000519
Damien Miller95def091999-11-25 00:26:21 +1100520 /* Cannot fork to background if no command. */
Damien Millercaf6dd62000-08-29 11:33:50 +1100521 if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
Damien Miller95def091999-11-25 00:26:21 +1100522 fatal("Cannot fork into background without a command to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000523
Damien Miller95def091999-11-25 00:26:21 +1100524 /* Allocate a tty by default if no command specified. */
525 if (buffer_len(&command) == 0)
526 tty_flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000527
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000528 /* Force no tty */
Ben Lindstromc72745a2000-12-02 19:03:54 +0000529 if (no_tty_flag)
530 tty_flag = 0;
Damien Miller95def091999-11-25 00:26:21 +1100531 /* Do not allocate a tty if stdin is not a tty. */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000532 if (!isatty(fileno(stdin)) && !force_tty_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100533 if (tty_flag)
Damien Miller996acd22003-04-09 20:59:48 +1000534 logit("Pseudo-terminal will not be allocated because stdin is not a terminal.");
Damien Miller95def091999-11-25 00:26:21 +1100535 tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000536 }
Damien Miller1383bd82000-04-06 12:32:37 +1000537
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000538 /*
539 * Initialize "log" output. Since we are the client all output
540 * actually goes to stderr.
541 */
Ben Lindstrom2b646522001-04-12 16:16:57 +0000542 log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
543 SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000544
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000545 /*
546 * Read per-user configuration file. Ignore the system wide config
547 * file if the user specifies a config file on the command line.
548 */
549 if (config != NULL) {
Damien Miller914420f2004-04-20 20:14:07 +1000550 if (!read_config_file(config, host, &options, 0))
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000551 fatal("Can't open user config file %.100s: "
552 "%.100s", config, strerror(errno));
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000553 } else {
554 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
555 _PATH_SSH_USER_CONFFILE);
Damien Miller57a44762004-04-20 20:11:57 +1000556 (void)read_config_file(buf, host, &options, 1);
Ben Lindstrom83f07d12001-10-03 17:22:29 +0000557
558 /* Read systemwide configuration file after use config. */
Damien Miller57a44762004-04-20 20:11:57 +1000559 (void)read_config_file(_PATH_HOST_CONFIG_FILE, host,
560 &options, 0);
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000561 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000562
Damien Miller95def091999-11-25 00:26:21 +1100563 /* Fill configuration defaults. */
564 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000565
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000566 channel_set_af(options.address_family);
567
Damien Miller95def091999-11-25 00:26:21 +1100568 /* reinit */
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000569 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000570
Damien Miller60bc5172001-03-19 09:38:15 +1100571 seed_rng();
572
Damien Miller95def091999-11-25 00:26:21 +1100573 if (options.user == NULL)
574 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000575
Damien Miller95def091999-11-25 00:26:21 +1100576 if (options.hostname != NULL)
577 host = options.hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000578
Darren Tucker3f521e22003-07-03 16:20:42 +1000579 /* force lowercase for hostkey matching */
580 if (options.host_key_alias != NULL) {
581 for (p = options.host_key_alias; *p; p++)
582 if (isupper(*p))
583 *p = tolower(*p);
584 }
585
Damien Miller9f1e33a2003-02-24 11:57:32 +1100586 if (options.proxy_command != NULL &&
587 strcmp(options.proxy_command, "none") == 0)
588 options.proxy_command = NULL;
589
Damien Miller0e220db2004-06-15 10:34:08 +1000590 if (options.control_path != NULL) {
591 options.control_path = tilde_expand_filename(
592 options.control_path, original_real_uid);
593 }
594 if (options.control_path != NULL && options.control_master == 0)
595 control_client(options.control_path); /* This doesn't return */
596
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000597 /* Open a connection to the remote host. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000598 if (ssh_connect(host, &hostaddr, options.port,
599 options.address_family, options.connection_attempts,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000600#ifdef HAVE_CYGWIN
601 options.use_privileged_port,
602#else
Ben Lindstromf9c48842002-06-11 16:37:51 +0000603 original_effective_uid == 0 && options.use_privileged_port,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000604#endif
Ben Lindstromda394ca2002-06-12 16:11:12 +0000605 options.proxy_command) != 0)
Ben Lindstrom24157572002-06-12 16:09:39 +0000606 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000607
Damien Miller5428f641999-11-25 11:54:57 +1100608 /*
609 * If we successfully made the connection, load the host private key
610 * in case we will need it later for combined rsa-rhosts
611 * authentication. This must be done before releasing extra
612 * privileges, because the file is only readable by root.
Ben Lindstrom9e5bb572002-06-06 19:58:27 +0000613 * If we cannot access the private keys, load the public keys
614 * instead and try to execute the ssh-keysign helper instead.
Damien Miller5428f641999-11-25 11:54:57 +1100615 */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000616 sensitive_data.nkeys = 0;
617 sensitive_data.keys = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000618 sensitive_data.external_keysign = 0;
Ben Lindstrom24157572002-06-12 16:09:39 +0000619 if (options.rhosts_rsa_authentication ||
620 options.hostbased_authentication) {
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000621 sensitive_data.nkeys = 3;
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000622 sensitive_data.keys = xmalloc(sensitive_data.nkeys *
623 sizeof(Key));
Ben Lindstromf9c48842002-06-11 16:37:51 +0000624
625 PRIV_START;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000626 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
Ben Lindstromd0fca422001-03-26 13:44:06 +0000627 _PATH_HOST_KEY_FILE, "", NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000628 sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
629 _PATH_HOST_DSA_KEY_FILE, "", NULL);
630 sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
631 _PATH_HOST_RSA_KEY_FILE, "", NULL);
Ben Lindstromf9c48842002-06-11 16:37:51 +0000632 PRIV_END;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000633
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +0000634 if (options.hostbased_authentication == 1 &&
635 sensitive_data.keys[0] == NULL &&
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000636 sensitive_data.keys[1] == NULL &&
637 sensitive_data.keys[2] == NULL) {
638 sensitive_data.keys[1] = key_load_public(
639 _PATH_HOST_DSA_KEY_FILE, NULL);
640 sensitive_data.keys[2] = key_load_public(
641 _PATH_HOST_RSA_KEY_FILE, NULL);
642 sensitive_data.external_keysign = 1;
643 }
Damien Miller95def091999-11-25 00:26:21 +1100644 }
Damien Miller5428f641999-11-25 11:54:57 +1100645 /*
646 * Get rid of any extra privileges that we may have. We will no
647 * longer need them. Also, extra privileges could make it very hard
648 * to read identity files and other non-world-readable files from the
649 * user's home directory if it happens to be on a NFS volume where
650 * root is mapped to nobody.
651 */
Ben Lindstromf9c48842002-06-11 16:37:51 +0000652 seteuid(original_real_uid);
653 setuid(original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100654
Damien Miller5428f641999-11-25 11:54:57 +1100655 /*
656 * Now that we are back to our own permissions, create ~/.ssh
657 * directory if it doesn\'t already exist.
658 */
Ben Lindstrom930b14a2001-08-15 23:19:21 +0000659 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 +1100660 if (stat(buf, &st) < 0)
Damien Millerbe484b52000-07-15 14:14:16 +1000661 if (mkdir(buf, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100662 error("Could not create directory '%.200s'.", buf);
663
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000664 /* load options.identity_files */
665 load_public_identity_files();
666
667 /* Expand ~ in known host file names. */
668 /* XXX mem-leaks: */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100669 options.system_hostfile =
670 tilde_expand_filename(options.system_hostfile, original_real_uid);
671 options.user_hostfile =
672 tilde_expand_filename(options.user_hostfile, original_real_uid);
673 options.system_hostfile2 =
674 tilde_expand_filename(options.system_hostfile2, original_real_uid);
675 options.user_hostfile2 =
676 tilde_expand_filename(options.user_hostfile2, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100677
Damien Miller07cd5892001-11-12 10:52:03 +1100678 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
679
Damien Miller95def091999-11-25 00:26:21 +1100680 /* Log into the remote system. This never returns if the login fails. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000681 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, pw);
Damien Miller95def091999-11-25 00:26:21 +1100682
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000683 /* We no longer need the private host keys. Clear them now. */
684 if (sensitive_data.nkeys != 0) {
685 for (i = 0; i < sensitive_data.nkeys; i++) {
686 if (sensitive_data.keys[i] != NULL) {
687 /* Destroys contents safely */
688 debug3("clear hostkey %d", i);
689 key_free(sensitive_data.keys[i]);
690 sensitive_data.keys[i] = NULL;
691 }
692 }
693 xfree(sensitive_data.keys);
694 }
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +0000695 for (i = 0; i < options.num_identity_files; i++) {
696 if (options.identity_files[i]) {
697 xfree(options.identity_files[i]);
698 options.identity_files[i] = NULL;
699 }
700 if (options.identity_keys[i]) {
701 key_free(options.identity_keys[i]);
702 options.identity_keys[i] = NULL;
703 }
704 }
Damien Miller95def091999-11-25 00:26:21 +1100705
Damien Miller1383bd82000-04-06 12:32:37 +1000706 exit_status = compat20 ? ssh_session2() : ssh_session();
707 packet_close();
Damien Miller8c4e18a2002-09-19 12:05:02 +1000708
Damien Miller0e220db2004-06-15 10:34:08 +1000709 if (options.control_path != NULL && control_fd != -1)
710 unlink(options.control_path);
711
Damien Miller8c4e18a2002-09-19 12:05:02 +1000712 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100713 * Send SIGHUP to proxy command if used. We don't wait() in
Damien Miller8c4e18a2002-09-19 12:05:02 +1000714 * case it hangs and instead rely on init to reap the child
715 */
716 if (proxy_command_pid > 1)
717 kill(proxy_command_pid, SIGHUP);
718
Damien Miller1383bd82000-04-06 12:32:37 +1000719 return exit_status;
720}
721
Darren Tucker0a118da2003-10-15 15:54:32 +1000722#define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
723
Ben Lindstrombba81212001-06-25 05:01:22 +0000724static void
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000725x11_get_proto(char **_proto, char **_data)
Damien Millerbd483e72000-04-30 10:00:53 +1000726{
Darren Tucker0a118da2003-10-15 15:54:32 +1000727 char cmd[1024];
Damien Millerbd483e72000-04-30 10:00:53 +1000728 char line[512];
Darren Tucker0a118da2003-10-15 15:54:32 +1000729 char xdisplay[512];
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000730 static char proto[512], data[512];
Damien Millerbd483e72000-04-30 10:00:53 +1000731 FILE *f;
Darren Tucker0a118da2003-10-15 15:54:32 +1000732 int got_data = 0, generated = 0, do_unlink = 0, i;
733 char *display, *xauthdir, *xauthfile;
Ben Lindstromee8d52d2002-07-23 21:03:02 +0000734 struct stat st;
Damien Millerbd483e72000-04-30 10:00:53 +1000735
Darren Tucker0a118da2003-10-15 15:54:32 +1000736 xauthdir = xauthfile = NULL;
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000737 *_proto = proto;
738 *_data = data;
739 proto[0] = data[0] = '\0';
Darren Tucker0a118da2003-10-15 15:54:32 +1000740
Ben Lindstromee8d52d2002-07-23 21:03:02 +0000741 if (!options.xauth_location ||
742 (stat(options.xauth_location, &st) == -1)) {
743 debug("No xauth program.");
744 } else {
745 if ((display = getenv("DISPLAY")) == NULL) {
746 debug("x11_get_proto: DISPLAY not set");
747 return;
748 }
Darren Tucker0a118da2003-10-15 15:54:32 +1000749 /*
750 * Handle FamilyLocal case where $DISPLAY does
751 * not match an authorization entry. For this we
752 * just try "xauth list unix:displaynum.screennum".
753 * XXX: "localhost" match to determine FamilyLocal
754 * is not perfect.
755 */
756 if (strncmp(display, "localhost:", 10) == 0) {
757 snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
758 display + 10);
759 display = xdisplay;
760 }
761 if (options.forward_x11_trusted == 0) {
762 xauthdir = xmalloc(MAXPATHLEN);
763 xauthfile = xmalloc(MAXPATHLEN);
764 strlcpy(xauthdir, "/tmp/ssh-XXXXXXXXXX", MAXPATHLEN);
765 if (mkdtemp(xauthdir) != NULL) {
766 do_unlink = 1;
767 snprintf(xauthfile, MAXPATHLEN, "%s/xauthfile",
768 xauthdir);
769 snprintf(cmd, sizeof(cmd),
770 "%s -f %s generate %s " SSH_X11_PROTO
Damien Miller3df755e2004-03-22 09:34:26 +1100771 " untrusted timeout 1200 2>" _PATH_DEVNULL,
Darren Tucker0a118da2003-10-15 15:54:32 +1000772 options.xauth_location, xauthfile, display);
773 debug2("x11_get_proto: %s", cmd);
774 if (system(cmd) == 0)
775 generated = 1;
776 }
777 }
778 snprintf(cmd, sizeof(cmd),
779 "%s %s%s list %s . 2>" _PATH_DEVNULL,
780 options.xauth_location,
781 generated ? "-f " : "" ,
782 generated ? xauthfile : "",
783 display);
784 debug2("x11_get_proto: %s", cmd);
785 f = popen(cmd, "r");
Damien Millerd3a18572000-06-07 19:55:44 +1000786 if (f && fgets(line, sizeof(line), f) &&
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000787 sscanf(line, "%*s %511s %511s", proto, data) == 2)
Damien Millerd3a18572000-06-07 19:55:44 +1000788 got_data = 1;
789 if (f)
790 pclose(f);
791 }
Darren Tucker0a118da2003-10-15 15:54:32 +1000792
793 if (do_unlink) {
794 unlink(xauthfile);
795 rmdir(xauthdir);
796 }
797 if (xauthdir)
798 xfree(xauthdir);
799 if (xauthfile)
800 xfree(xauthfile);
801
Damien Millerbd483e72000-04-30 10:00:53 +1000802 /*
803 * If we didn't get authentication data, just make up some
804 * data. The forwarding code will check the validity of the
805 * response anyway, and substitute this data. The X11
806 * server, however, will ignore this fake data and use
807 * whatever authentication mechanisms it was using otherwise
808 * for the local connection.
809 */
810 if (!got_data) {
811 u_int32_t rand = 0;
812
Darren Tucker0a118da2003-10-15 15:54:32 +1000813 logit("Warning: No xauth data; "
814 "using fake authentication data for X11 forwarding.");
815 strlcpy(proto, SSH_X11_PROTO, sizeof proto);
Damien Millerbd483e72000-04-30 10:00:53 +1000816 for (i = 0; i < 16; i++) {
817 if (i % 4 == 0)
818 rand = arc4random();
Darren Tucker0a118da2003-10-15 15:54:32 +1000819 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
820 rand & 0xff);
Damien Millerbd483e72000-04-30 10:00:53 +1000821 rand >>= 8;
822 }
823 }
824}
825
Ben Lindstrombba81212001-06-25 05:01:22 +0000826static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100827ssh_init_forwarding(void)
828{
Kevin Steves12057502001-02-05 14:54:34 +0000829 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100830 int i;
Kevin Steves12057502001-02-05 14:54:34 +0000831
Damien Miller0bc1bd82000-11-13 22:57:25 +1100832 /* Initiate local TCP/IP port forwardings. */
833 for (i = 0; i < options.num_local_forwards; i++) {
834 debug("Connections to local port %d forwarded to remote address %.200s:%d",
835 options.local_forwards[i].port,
836 options.local_forwards[i].host,
837 options.local_forwards[i].host_port);
Damien Millerb16461c2002-01-22 23:29:22 +1100838 success += channel_setup_local_fwd_listener(
Damien Miller0bc1bd82000-11-13 22:57:25 +1100839 options.local_forwards[i].port,
840 options.local_forwards[i].host,
841 options.local_forwards[i].host_port,
842 options.gateway_ports);
843 }
Kevin Steves12057502001-02-05 14:54:34 +0000844 if (i > 0 && success == 0)
845 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100846
847 /* Initiate remote TCP/IP port forwardings. */
848 for (i = 0; i < options.num_remote_forwards; i++) {
849 debug("Connections to remote port %d forwarded to local address %.200s:%d",
850 options.remote_forwards[i].port,
851 options.remote_forwards[i].host,
852 options.remote_forwards[i].host_port);
853 channel_request_remote_forwarding(
854 options.remote_forwards[i].port,
855 options.remote_forwards[i].host,
856 options.remote_forwards[i].host_port);
857 }
858}
859
Ben Lindstrombba81212001-06-25 05:01:22 +0000860static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100861check_agent_present(void)
862{
863 if (options.forward_agent) {
864 /* Clear agent forwarding if we don\'t have an agent. */
Damien Miller789e95d2002-09-12 09:52:46 +1000865 if (!ssh_agent_present())
Damien Miller0bc1bd82000-11-13 22:57:25 +1100866 options.forward_agent = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100867 }
868}
869
Ben Lindstrombba81212001-06-25 05:01:22 +0000870static int
Damien Miller1383bd82000-04-06 12:32:37 +1000871ssh_session(void)
872{
873 int type;
Damien Miller1383bd82000-04-06 12:32:37 +1000874 int interactive = 0;
875 int have_tty = 0;
876 struct winsize ws;
Damien Miller1383bd82000-04-06 12:32:37 +1000877 char *cp;
878
Damien Miller95def091999-11-25 00:26:21 +1100879 /* Enable compression if requested. */
880 if (options.compression) {
881 debug("Requesting compression at level %d.", options.compression_level);
882
883 if (options.compression_level < 1 || options.compression_level > 9)
884 fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
885
886 /* Send the request. */
887 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
888 packet_put_int(options.compression_level);
889 packet_send();
890 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100891 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100892 if (type == SSH_SMSG_SUCCESS)
893 packet_start_compression(options.compression_level);
894 else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000895 logit("Warning: Remote host refused compression.");
Damien Miller95def091999-11-25 00:26:21 +1100896 else
897 packet_disconnect("Protocol error waiting for compression response.");
898 }
899 /* Allocate a pseudo tty if appropriate. */
900 if (tty_flag) {
901 debug("Requesting pty.");
902
903 /* Start the packet. */
904 packet_start(SSH_CMSG_REQUEST_PTY);
905
906 /* Store TERM in the packet. There is no limit on the
907 length of the string. */
908 cp = getenv("TERM");
909 if (!cp)
910 cp = "";
Ben Lindstrom664408d2001-06-09 01:42:01 +0000911 packet_put_cstring(cp);
Damien Miller95def091999-11-25 00:26:21 +1100912
913 /* Store window size in the packet. */
914 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
915 memset(&ws, 0, sizeof(ws));
916 packet_put_int(ws.ws_row);
917 packet_put_int(ws.ws_col);
918 packet_put_int(ws.ws_xpixel);
919 packet_put_int(ws.ws_ypixel);
920
921 /* Store tty modes in the packet. */
Ben Lindstromae8e2d32001-04-14 23:13:02 +0000922 tty_make_modes(fileno(stdin), NULL);
Damien Miller95def091999-11-25 00:26:21 +1100923
924 /* Send the packet, and wait for it to leave. */
925 packet_send();
926 packet_write_wait();
927
928 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100929 type = packet_read();
Damien Miller450a7a12000-03-26 13:04:51 +1000930 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100931 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +1000932 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000933 } else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000934 logit("Warning: Remote host failed or refused to allocate a pseudo tty.");
Damien Miller95def091999-11-25 00:26:21 +1100935 else
936 packet_disconnect("Protocol error waiting for pty request response.");
937 }
938 /* Request X11 forwarding if enabled and DISPLAY is set. */
939 if (options.forward_x11 && getenv("DISPLAY") != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000940 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +1000941 /* Get reasonable local authentication information. */
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000942 x11_get_proto(&proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +1000943 /* Request forwarding with authentication spoofing. */
Damien Miller95def091999-11-25 00:26:21 +1100944 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Millerbd483e72000-04-30 10:00:53 +1000945 x11_request_forwarding_with_spoofing(0, proto, data);
Damien Miller95def091999-11-25 00:26:21 +1100946
947 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100948 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100949 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100950 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +1000951 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller996acd22003-04-09 20:59:48 +1000952 logit("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +1000953 } else {
Damien Miller95def091999-11-25 00:26:21 +1100954 packet_disconnect("Protocol error waiting for X11 forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +1000955 }
Damien Miller95def091999-11-25 00:26:21 +1100956 }
957 /* Tell the packet module whether this is an interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000958 packet_set_interactive(interactive);
Damien Miller95def091999-11-25 00:26:21 +1100959
960 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100961 check_agent_present();
962
Damien Miller95def091999-11-25 00:26:21 +1100963 if (options.forward_agent) {
964 debug("Requesting authentication agent forwarding.");
965 auth_request_forwarding();
966
967 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100968 type = packet_read();
Damien Miller48b03fc2002-01-22 23:11:40 +1100969 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100970 if (type != SSH_SMSG_SUCCESS)
Damien Miller996acd22003-04-09 20:59:48 +1000971 logit("Warning: Remote host denied authentication agent forwarding.");
Damien Miller95def091999-11-25 00:26:21 +1100972 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000973
Damien Miller0bc1bd82000-11-13 22:57:25 +1100974 /* Initiate port forwardings. */
975 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000976
Damien Miller5428f641999-11-25 11:54:57 +1100977 /* If requested, let ssh continue in the background. */
Damien Miller4af51302000-04-16 11:18:38 +1000978 if (fork_after_authentication_flag)
Damien Miller5428f641999-11-25 11:54:57 +1100979 if (daemon(1, 1) < 0)
980 fatal("daemon() failed: %.200s", strerror(errno));
981
982 /*
983 * If a command was specified on the command line, execute the
984 * command now. Otherwise request the server to start a shell.
985 */
Damien Miller95def091999-11-25 00:26:21 +1100986 if (buffer_len(&command) > 0) {
987 int len = buffer_len(&command);
988 if (len > 900)
989 len = 900;
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100990 debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
Damien Miller95def091999-11-25 00:26:21 +1100991 packet_start(SSH_CMSG_EXEC_CMD);
992 packet_put_string(buffer_ptr(&command), buffer_len(&command));
993 packet_send();
994 packet_write_wait();
995 } else {
996 debug("Requesting shell.");
997 packet_start(SSH_CMSG_EXEC_SHELL);
998 packet_send();
999 packet_write_wait();
1000 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001001
Damien Miller95def091999-11-25 00:26:21 +11001002 /* Enter the interactive session. */
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001003 return client_loop(have_tty, tty_flag ?
1004 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
Damien Miller1383bd82000-04-06 12:32:37 +10001005}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001006
Ben Lindstrombba81212001-06-25 05:01:22 +00001007static void
Damien Miller0e220db2004-06-15 10:34:08 +10001008ssh_subsystem_reply(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001009{
1010 int id, len;
1011
1012 id = packet_get_int();
1013 len = buffer_len(&command);
Ben Lindstrom4040fe12001-03-05 06:52:57 +00001014 if (len > 900)
1015 len = 900;
Damien Miller48b03fc2002-01-22 23:11:40 +11001016 packet_check_eom();
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001017 if (type == SSH2_MSG_CHANNEL_FAILURE)
1018 fatal("Request for subsystem '%.*s' failed on channel %d",
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001019 len, (u_char *)buffer_ptr(&command), id);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001020}
1021
Damien Miller2797f7f2002-04-23 21:09:44 +10001022void
Damien Miller509b0102003-12-17 16:33:10 +11001023client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt)
Damien Miller2797f7f2002-04-23 21:09:44 +10001024{
1025 int i;
1026
1027 i = client_global_request_id++;
Damien Miller509b0102003-12-17 16:33:10 +11001028 if (i >= options.num_remote_forwards)
Damien Miller2797f7f2002-04-23 21:09:44 +10001029 return;
Damien Miller2797f7f2002-04-23 21:09:44 +10001030 debug("remote forward %s for: listen %d, connect %s:%d",
1031 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
1032 options.remote_forwards[i].port,
1033 options.remote_forwards[i].host,
1034 options.remote_forwards[i].host_port);
1035 if (type == SSH2_MSG_REQUEST_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +10001036 logit("Warning: remote port forwarding failed for listen port %d",
Damien Miller2797f7f2002-04-23 21:09:44 +10001037 options.remote_forwards[i].port);
1038}
1039
Damien Miller0e220db2004-06-15 10:34:08 +10001040static void
1041ssh_control_listener(void)
1042{
1043 struct sockaddr_un addr;
1044 mode_t old_umask;
Damien Miller07b6ff12004-06-15 11:14:45 +10001045 int addr_len;
1046
Damien Miller23f07702004-06-18 01:19:03 +10001047 if (options.control_path == NULL || options.control_master <= 0)
Damien Miller0e220db2004-06-15 10:34:08 +10001048 return;
1049
1050 memset(&addr, '\0', sizeof(addr));
1051 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001052 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001053 strlen(options.control_path) + 1;
1054
1055 if (strlcpy(addr.sun_path, options.control_path,
1056 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1057 fatal("ControlPath too long");
1058
1059 if ((control_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1060 fatal("%s socket(): %s\n", __func__, strerror(errno));
1061
1062 old_umask = umask(0177);
Damien Miller07b6ff12004-06-15 11:14:45 +10001063 if (bind(control_fd, (struct sockaddr*)&addr, addr_len) == -1) {
Damien Miller0e220db2004-06-15 10:34:08 +10001064 control_fd = -1;
1065 if (errno == EINVAL)
1066 fatal("ControlSocket %s already exists",
1067 options.control_path);
1068 else
1069 fatal("%s bind(): %s\n", __func__, strerror(errno));
1070 }
1071 umask(old_umask);
1072
1073 if (listen(control_fd, 64) == -1)
1074 fatal("%s listen(): %s\n", __func__, strerror(errno));
1075
1076 set_nonblock(control_fd);
1077}
1078
Ben Lindstromf558cf62001-09-20 23:13:49 +00001079/* request pty/x11/agent/tcpfwd/shell for channel */
Ben Lindstrombba81212001-06-25 05:01:22 +00001080static void
Ben Lindstromf558cf62001-09-20 23:13:49 +00001081ssh_session2_setup(int id, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +10001082{
Damien Miller3756dce2004-06-18 01:17:29 +10001083 extern char **environ;
1084
Damien Miller0e220db2004-06-15 10:34:08 +10001085 int interactive = tty_flag;
1086 if (options.forward_x11 && getenv("DISPLAY") != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001087 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001088 /* Get reasonable local authentication information. */
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001089 x11_get_proto(&proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001090 /* Request forwarding with authentication spoofing. */
1091 debug("Requesting X11 forwarding with authentication spoofing.");
1092 x11_request_forwarding_with_spoofing(id, proto, data);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001093 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001094 /* XXX wait for reply */
1095 }
1096
Damien Miller0bc1bd82000-11-13 22:57:25 +11001097 check_agent_present();
1098 if (options.forward_agent) {
1099 debug("Requesting authentication agent forwarding.");
1100 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1101 packet_send();
1102 }
1103
Damien Miller0e220db2004-06-15 10:34:08 +10001104 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
Damien Miller3756dce2004-06-18 01:17:29 +10001105 NULL, fileno(stdin), &command, environ, &ssh_subsystem_reply);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001106
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001107 packet_set_interactive(interactive);
Damien Miller1383bd82000-04-06 12:32:37 +10001108}
1109
Ben Lindstromf558cf62001-09-20 23:13:49 +00001110/* open new channel for a session */
Ben Lindstrombba81212001-06-25 05:01:22 +00001111static int
Ben Lindstromf558cf62001-09-20 23:13:49 +00001112ssh_session2_open(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001113{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001114 Channel *c;
1115 int window, packetmax, in, out, err;
Damien Millerad833b32000-08-23 10:46:23 +10001116
Damien Millercaf6dd62000-08-29 11:33:50 +11001117 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001118 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001119 } else {
1120 in = dup(STDIN_FILENO);
1121 }
1122 out = dup(STDOUT_FILENO);
1123 err = dup(STDERR_FILENO);
1124
1125 if (in < 0 || out < 0 || err < 0)
1126 fatal("dup() in/out/err failed");
1127
Damien Miller69b69aa2000-10-28 14:19:58 +11001128 /* enable nonblocking unless tty */
1129 if (!isatty(in))
1130 set_nonblock(in);
1131 if (!isatty(out))
1132 set_nonblock(out);
1133 if (!isatty(err))
1134 set_nonblock(err);
1135
Damien Millere4340be2000-09-16 13:29:08 +11001136 window = CHAN_SES_WINDOW_DEFAULT;
1137 packetmax = CHAN_SES_PACKET_DEFAULT;
Damien Miller19a59452002-02-19 15:20:57 +11001138 if (tty_flag) {
1139 window >>= 1;
1140 packetmax >>= 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001141 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001142 c = channel_new(
Damien Miller1383bd82000-04-06 12:32:37 +10001143 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001144 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001145 "client-session", /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001146
Ben Lindstromf558cf62001-09-20 23:13:49 +00001147 debug3("ssh_session2_open: channel_new: %d", c->self);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001148
Ben Lindstrom5ec26452001-06-09 00:18:51 +00001149 channel_send_open(c->self);
Ben Lindstromf558cf62001-09-20 23:13:49 +00001150 if (!no_shell_flag)
Damien Miller0e220db2004-06-15 10:34:08 +10001151 channel_register_confirm(c->self, ssh_session2_setup, NULL);
Damien Miller1383bd82000-04-06 12:32:37 +10001152
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001153 return c->self;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001154}
1155
Ben Lindstrombba81212001-06-25 05:01:22 +00001156static int
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001157ssh_session2(void)
1158{
Ben Lindstromf558cf62001-09-20 23:13:49 +00001159 int id = -1;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001160
1161 /* XXX should be pre-session */
1162 ssh_init_forwarding();
Damien Miller0e220db2004-06-15 10:34:08 +10001163 ssh_control_listener();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001164
Ben Lindstromf558cf62001-09-20 23:13:49 +00001165 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1166 id = ssh_session2_open();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001167
1168 /* If requested, let ssh continue in the background. */
1169 if (fork_after_authentication_flag)
1170 if (daemon(1, 1) < 0)
1171 fatal("daemon() failed: %.200s", strerror(errno));
1172
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001173 return client_loop(tty_flag, tty_flag ?
1174 options.escape_char : SSH_ESCAPECHAR_NONE, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001175}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001176
Ben Lindstrombba81212001-06-25 05:01:22 +00001177static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001178load_public_identity_files(void)
1179{
1180 char *filename;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001181 int i = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001182 Key *public;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001183#ifdef SMARTCARD
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001184 Key **keys;
1185
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001186 if (options.smartcard_device != NULL &&
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001187 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1188 (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL ) {
1189 int count = 0;
1190 for (i = 0; keys[i] != NULL; i++) {
1191 count++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001192 memmove(&options.identity_files[1], &options.identity_files[0],
1193 sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
1194 memmove(&options.identity_keys[1], &options.identity_keys[0],
1195 sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1196 options.num_identity_files++;
1197 options.identity_keys[0] = keys[i];
Damien Miller56a0bb02003-06-18 20:28:40 +10001198 options.identity_files[0] = sc_get_key_label(keys[i]);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001199 }
Ben Lindstrom4f054602002-03-26 03:23:00 +00001200 if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1201 options.num_identity_files = SSH_MAX_IDENTITY_FILES;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001202 i = count;
1203 xfree(keys);
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001204 }
Ben Lindstromffce1472001-08-06 21:57:31 +00001205#endif /* SMARTCARD */
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001206 for (; i < options.num_identity_files; i++) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001207 filename = tilde_expand_filename(options.identity_files[i],
1208 original_real_uid);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001209 public = key_load_public(filename, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001210 debug("identity file %s type %d", filename,
1211 public ? public->type : -1);
1212 xfree(options.identity_files[i]);
1213 options.identity_files[i] = filename;
1214 options.identity_keys[i] = public;
1215 }
1216}
Damien Miller0e220db2004-06-15 10:34:08 +10001217
1218static void
1219control_client_sighandler(int signo)
1220{
1221 control_client_terminate = signo;
1222}
1223
1224static void
1225control_client_sigrelay(int signo)
1226{
1227 if (control_server_pid > 1)
1228 kill(control_server_pid, signo);
1229}
1230
1231static void
1232control_client(const char *path)
1233{
1234 struct sockaddr_un addr;
Damien Miller3756dce2004-06-18 01:17:29 +10001235 int i, r, sock, exitval, addr_len;
Damien Miller0e220db2004-06-15 10:34:08 +10001236 Buffer m;
1237 char *cp;
Damien Miller3756dce2004-06-18 01:17:29 +10001238 extern char **environ;
Damien Miller0e220db2004-06-15 10:34:08 +10001239
1240 memset(&addr, '\0', sizeof(addr));
1241 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001242 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001243 strlen(path) + 1;
1244
1245 if (strlcpy(addr.sun_path, path,
1246 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1247 fatal("ControlPath too long");
1248
1249 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1250 fatal("%s socket(): %s", __func__, strerror(errno));
1251
Damien Miller07b6ff12004-06-15 11:14:45 +10001252 if (connect(sock, (struct sockaddr*)&addr, addr_len) == -1)
Damien Miller0e220db2004-06-15 10:34:08 +10001253 fatal("Couldn't connect to %s: %s", path, strerror(errno));
1254
1255 if ((cp = getenv("TERM")) == NULL)
1256 cp = "";
1257
1258 signal(SIGINT, control_client_sighandler);
1259 signal(SIGTERM, control_client_sighandler);
1260 signal(SIGWINCH, control_client_sigrelay);
1261
1262 buffer_init(&m);
1263
1264 /* Get PID of controlee */
1265 if (ssh_msg_recv(sock, &m) == -1)
1266 fatal("%s: msg_recv", __func__);
1267 if (buffer_get_char(&m) != 0)
1268 fatal("%s: wrong version", __func__);
Damien Miller23f07702004-06-18 01:19:03 +10001269 /* Connection allowed? */
1270 if (buffer_get_int(&m) != 1)
1271 fatal("Connection to master denied");
Damien Miller0e220db2004-06-15 10:34:08 +10001272 control_server_pid = buffer_get_int(&m);
1273
Damien Miller0e220db2004-06-15 10:34:08 +10001274 buffer_clear(&m);
1275 buffer_put_int(&m, tty_flag);
1276 buffer_put_int(&m, subsystem_flag);
1277 buffer_put_cstring(&m, cp);
1278
1279 buffer_append(&command, "\0", 1);
1280 buffer_put_cstring(&m, buffer_ptr(&command));
1281
Damien Miller3756dce2004-06-18 01:17:29 +10001282 /* Pass environment */
1283 for (i = 0; environ != NULL && environ[i] != NULL; i++)
1284 ;
1285 buffer_put_int(&m, i);
1286 for (i = 0; environ != NULL && environ[i] != NULL; i++)
1287 buffer_put_cstring(&m, environ[i]);
1288
Damien Miller0e220db2004-06-15 10:34:08 +10001289 if (ssh_msg_send(sock, /* version */0, &m) == -1)
1290 fatal("%s: msg_send", __func__);
1291
1292 mm_send_fd(sock, STDIN_FILENO);
1293 mm_send_fd(sock, STDOUT_FILENO);
1294 mm_send_fd(sock, STDERR_FILENO);
1295
1296 /* Wait for reply, so master has a chance to gather ttymodes */
1297 buffer_clear(&m);
1298 if (ssh_msg_recv(sock, &m) == -1)
1299 fatal("%s: msg_recv", __func__);
1300 if (buffer_get_char(&m) != 0)
1301 fatal("%s: master returned error", __func__);
1302 buffer_free(&m);
1303
1304 if (tty_flag)
1305 enter_raw_mode();
1306
1307 /* Stick around until the controlee closes the client_fd */
1308 exitval = 0;
1309 for (;!control_client_terminate;) {
1310 r = read(sock, &exitval, sizeof(exitval));
1311 if (r == 0) {
1312 debug2("Received EOF from master");
1313 break;
1314 }
1315 if (r > 0)
1316 debug2("Received exit status from master %d", exitval);
1317 if (r == -1 && errno != EINTR)
1318 fatal("%s: read %s", __func__, strerror(errno));
1319 }
1320
1321 if (control_client_terminate)
1322 debug2("Exiting on signal %d", control_client_terminate);
1323
1324 close(sock);
1325
1326 leave_raw_mode();
1327
1328 if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
1329 fprintf(stderr, "Connection to master closed.\r\n");
1330
1331 exit(exitval);
1332}