blob: cfefadef95f5bfe5a468f4d30a6fe22f2a181aba [file] [log] [blame]
Damien Miller5771ed72008-05-19 15:35:33 +10001/* $OpenBSD: ssh.c,v 1.311 2008/05/08 13:06:11 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Ssh client program. This program can be used to log into a remote machine.
7 * The software supports strong authentication, encryption, and forwarding
8 * of X11, TCP/IP, and authentication connections.
9 *
Damien Millere4340be2000-09-16 13:29:08 +110010 * As far as I am concerned, the code I have written for this software
11 * can be used freely for any purpose. Any derived versions of this
12 * software must be clearly marked as such, and if the derived work is
13 * incompatible with the protocol description in the RFC file, it must be
14 * called by a name other than "ssh" or "Secure Shell".
15 *
16 * Copyright (c) 1999 Niels Provos. All rights reserved.
Darren Tucker0a118da2003-10-15 15:54:32 +100017 * Copyright (c) 2000, 2001, 2002, 2003 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110018 *
19 * Modified to work with SSL by Niels Provos <provos@citi.umich.edu>
20 * in Canada (German citizen).
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110041 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100042
43#include "includes.h"
Damien Millercd4223c2006-03-15 11:22:47 +110044
Damien Millerf17883e2006-03-15 11:45:54 +110045#include <sys/types.h>
46#ifdef HAVE_SYS_STAT_H
47# include <sys/stat.h>
48#endif
Damien Millercd4223c2006-03-15 11:22:47 +110049#include <sys/resource.h>
Damien Miller17e91c02006-03-15 11:28:34 +110050#include <sys/ioctl.h>
Damien Millere3b60b52006-07-10 21:08:03 +100051#include <sys/socket.h>
Damien Miller574c41f2006-03-15 11:40:10 +110052#include <sys/un.h>
Damien Miller03e20032006-03-15 11:16:59 +110053
Damien Millerc7b06362006-03-15 11:53:45 +110054#include <ctype.h>
Darren Tuckerba724052006-07-12 22:24:22 +100055#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100056#include <fcntl.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100057#include <netdb.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110058#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110059#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110060#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100061#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110062#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100063#include <stdarg.h>
Damien Miller2d00e632006-07-24 13:53:19 +100064#include <stddef.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100065#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100066#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100067#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100068#include <unistd.h>
Damien Millereba71ba2000-04-29 23:57:08 +100069
Darren Tucker46aa3e02006-09-02 15:32:40 +100070#include <netinet/in.h>
71#include <arpa/inet.h>
72
Damien Millereba71ba2000-04-29 23:57:08 +100073#include <openssl/evp.h>
Damien Miller0bc1bd82000-11-13 22:57:25 +110074#include <openssl/err.h>
Darren Tuckerbfaaf962008-02-28 19:13:52 +110075#include "openbsd-compat/openssl-compat.h"
Damien Millerb84886b2008-05-19 15:05:07 +100076#include "openbsd-compat/sys-queue.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077
Damien Millerd7834352006-08-05 12:39:39 +100078#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000080#include "ssh1.h"
Damien Miller1383bd82000-04-06 12:32:37 +100081#include "ssh2.h"
82#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000083#include "cipher.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000084#include "packet.h"
85#include "buffer.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000086#include "channels.h"
Damien Millereba71ba2000-04-29 23:57:08 +100087#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100088#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100089#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000090#include "pathnames.h"
Damien Miller0e220db2004-06-15 10:34:08 +100091#include "dispatch.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +000092#include "clientloop.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000093#include "log.h"
94#include "readconf.h"
95#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000096#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000097#include "kex.h"
98#include "mac.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +100099#include "sshpty.h"
Darren Tucker46bc0752004-05-02 22:11:30 +1000100#include "match.h"
Damien Miller0e220db2004-06-15 10:34:08 +1000101#include "msg.h"
102#include "monitor_fdpass.h"
Darren Tucker25f60a72004-08-15 17:23:34 +1000103#include "uidswap.h"
Damien Millerb7576772006-07-10 20:23:39 +1000104#include "version.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105
Ben Lindstromc5b68002001-07-04 04:52:03 +0000106#ifdef SMARTCARD
Ben Lindstromc5b68002001-07-04 04:52:03 +0000107#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +0000108#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000109
Damien Miller3f905871999-11-15 17:10:57 +1100110extern char *__progname;
Damien Miller3f905871999-11-15 17:10:57 +1100111
Damien Miller95def091999-11-25 00:26:21 +1100112/* Flag indicating whether debug mode is on. This can be set on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113int debug_flag = 0;
114
Damien Miller78928792000-04-12 20:17:38 +1000115/* Flag indicating whether a tty should be allocated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116int tty_flag = 0;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000117int no_tty_flag = 0;
118int force_tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119
Damien Miller1383bd82000-04-06 12:32:37 +1000120/* don't exec a shell */
121int no_shell_flag = 0;
Damien Miller1383bd82000-04-06 12:32:37 +1000122
Damien Miller5428f641999-11-25 11:54:57 +1100123/*
124 * Flag indicating that nothing should be read from stdin. This can be set
125 * on the command line.
126 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127int stdin_null_flag = 0;
128
Damien Miller5428f641999-11-25 11:54:57 +1100129/*
130 * Flag indicating that ssh should fork after authentication. This is useful
Ben Lindstromf666fec2002-06-06 19:51:58 +0000131 * so that the passphrase can be entered manually, and then ssh goes to the
Damien Miller5428f641999-11-25 11:54:57 +1100132 * background.
133 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134int fork_after_authentication_flag = 0;
135
Damien Miller5428f641999-11-25 11:54:57 +1100136/*
137 * General data structure for command line options and options configurable
138 * in configuration files. See readconf.h.
139 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140Options options;
141
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000142/* optional user configfile */
143char *config = NULL;
144
Damien Miller5428f641999-11-25 11:54:57 +1100145/*
146 * Name of the host we are connecting to. This is the name given on the
147 * command line, or the HostName specified for the user-supplied name in a
148 * configuration file.
149 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150char *host;
151
152/* socket address the host resolves to */
Damien Miller34132e52000-01-14 15:45:46 +1100153struct sockaddr_storage hostaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000155/* Private host keys. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000156Sensitive sensitive_data;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000157
158/* Original real UID. */
159uid_t original_real_uid;
Ben Lindstromf9c48842002-06-11 16:37:51 +0000160uid_t original_effective_uid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000161
Damien Miller1383bd82000-04-06 12:32:37 +1000162/* command to be executed */
163Buffer command;
164
Damien Miller832562e2001-01-30 09:30:01 +1100165/* Should we execute a command or invoke a subsystem? */
166int subsystem_flag = 0;
167
Damien Miller2797f7f2002-04-23 21:09:44 +1000168/* # of replies received for global requests */
169static int client_global_request_id = 0;
170
Damien Miller8c4e18a2002-09-19 12:05:02 +1000171/* pid of proxycommand child process */
172pid_t proxy_command_pid = 0;
173
Damien Miller0e220db2004-06-15 10:34:08 +1000174/* fd to control socket */
175int control_fd = -1;
176
Darren Tucker7ebfc102004-11-07 20:06:19 +1100177/* Multiplexing control command */
Darren Tucker0814d312005-06-01 23:08:51 +1000178static u_int mux_command = 0;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100179
Damien Miller0e220db2004-06-15 10:34:08 +1000180/* Only used in control client mode */
181volatile sig_atomic_t control_client_terminate = 0;
182u_int control_server_pid = 0;
183
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000184/* Prints a help message to the user. This function never returns. */
185
Ben Lindstrombba81212001-06-25 05:01:22 +0000186static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000187usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000188{
Damien Miller50955102004-03-22 09:34:58 +1100189 fprintf(stderr,
Darren Tucker415bddc2007-06-12 23:43:16 +1000190"usage: ssh [-1246AaCfgKkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n"
Darren Tucker895d6982005-10-03 18:18:05 +1000191" [-D [bind_address:]port] [-e escape_char] [-F configfile]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100192" [-i identity_file] [-L [bind_address:]port:host:hostport]\n"
Damien Millerf91ee4c2005-03-01 21:24:33 +1100193" [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100194" [-R [bind_address:]port:host:hostport] [-S ctl_path]\n"
Damien Miller991dba42006-07-10 20:16:27 +1000195" [-w local_tun[:remote_tun]] [user@]hostname [command]\n"
Damien Miller50955102004-03-22 09:34:58 +1100196 );
Darren Tuckere9a9b712005-12-20 16:15:51 +1100197 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000198}
199
Ben Lindstrombba81212001-06-25 05:01:22 +0000200static int ssh_session(void);
201static int ssh_session2(void);
202static void load_public_identity_files(void);
Damien Miller0e220db2004-06-15 10:34:08 +1000203static void control_client(const char *path);
Damien Miller1383bd82000-04-06 12:32:37 +1000204
Damien Miller95def091999-11-25 00:26:21 +1100205/*
206 * Main program for the ssh client.
207 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000208int
209main(int ac, char **av)
210{
Ben Lindstrom24157572002-06-12 16:09:39 +0000211 int i, opt, exit_status;
Damien Miller9836cf82003-12-17 16:30:06 +1100212 char *p, *cp, *line, buf[256];
Damien Miller95def091999-11-25 00:26:21 +1100213 struct stat st;
Ben Lindstrom086cf212001-03-05 05:56:40 +0000214 struct passwd *pw;
Damien Miller67bd0622007-09-17 12:06:57 +1000215 int dummy, timeout_ms;
Ben Lindstrom5ccf63a2001-09-24 20:00:10 +0000216 extern int optind, optreset;
Damien Miller4f8e6692001-07-14 13:22:53 +1000217 extern char *optarg;
Damien Miller9651fe62005-06-26 08:55:25 +1000218 struct servent *sp;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100219 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000220
Darren Tuckerce321d82005-10-03 18:11:24 +1000221 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
222 sanitise_stdfd();
223
Damien Miller59d3d5b2003-08-22 09:34:41 +1000224 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000225 init_rng();
226
Damien Miller5428f641999-11-25 11:54:57 +1100227 /*
228 * Save the original real uid. It will be needed later (uid-swapping
229 * may clobber the real uid).
230 */
Damien Miller95def091999-11-25 00:26:21 +1100231 original_real_uid = getuid();
232 original_effective_uid = geteuid();
Damien Millera8e06ce2003-11-21 23:48:55 +1100233
Damien Miller50b9a602002-09-04 16:50:06 +1000234 /*
235 * Use uid-swapping to give up root privileges for the duration of
236 * option processing. We will re-instantiate the rights when we are
237 * ready to create the privileged port, and will permanently drop
238 * them when the port has been created (actually, when the connection
239 * has been made, as we may need to create the port several times).
240 */
241 PRIV_END;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000242
Damien Miller05dd7952000-10-01 00:42:48 +1100243#ifdef HAVE_SETRLIMIT
Damien Miller95def091999-11-25 00:26:21 +1100244 /* If we are installed setuid root be careful to not drop core. */
245 if (original_real_uid != original_effective_uid) {
246 struct rlimit rlim;
247 rlim.rlim_cur = rlim.rlim_max = 0;
248 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
249 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100251#endif
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000252 /* Get user data. */
253 pw = getpwuid(original_real_uid);
254 if (!pw) {
Damien Miller996acd22003-04-09 20:59:48 +1000255 logit("You don't exist, go away!");
Darren Tuckere9a9b712005-12-20 16:15:51 +1100256 exit(255);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000257 }
258 /* Take a copy of the returned structure. */
259 pw = pwcopy(pw);
260
Damien Miller5428f641999-11-25 11:54:57 +1100261 /*
Damien Miller5428f641999-11-25 11:54:57 +1100262 * Set our umask to something reasonable, as some files are created
263 * with the default umask. This will make them world-readable but
264 * writable only by the owner, which is ok for all files for which we
265 * don't set the modes explicitly.
266 */
Damien Miller95def091999-11-25 00:26:21 +1100267 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000268
Damien Miller95def091999-11-25 00:26:21 +1100269 /* Initialize option structure to indicate that no values have been set. */
270 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000271
Damien Miller95def091999-11-25 00:26:21 +1100272 /* Parse command-line arguments. */
273 host = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000274
Damien Miller2ecb6bd2006-03-15 12:03:53 +1100275 again:
Damien Millerf4614452001-07-14 12:18:10 +1000276 while ((opt = getopt(ac, av,
Darren Tucker415bddc2007-06-12 23:43:16 +1000277 "1246ab:c:e:fgi:kl:m:no:p:qstvxACD:F:I:KL:MNO:PR:S:TVw:XY")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100278 switch (opt) {
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000279 case '1':
280 options.protocol = SSH_PROTO_1;
281 break;
Damien Miller4af51302000-04-16 11:18:38 +1000282 case '2':
283 options.protocol = SSH_PROTO_2;
284 break;
Damien Miller34132e52000-01-14 15:45:46 +1100285 case '4':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000286 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100287 break;
Damien Miller34132e52000-01-14 15:45:46 +1100288 case '6':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000289 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100290 break;
Damien Miller95def091999-11-25 00:26:21 +1100291 case 'n':
292 stdin_null_flag = 1;
293 break;
Damien Miller95def091999-11-25 00:26:21 +1100294 case 'f':
295 fork_after_authentication_flag = 1;
296 stdin_null_flag = 1;
297 break;
Damien Miller95def091999-11-25 00:26:21 +1100298 case 'x':
299 options.forward_x11 = 0;
300 break;
Damien Miller95def091999-11-25 00:26:21 +1100301 case 'X':
302 options.forward_x11 = 1;
303 break;
Darren Tucker0a118da2003-10-15 15:54:32 +1000304 case 'Y':
305 options.forward_x11 = 1;
306 options.forward_x11_trusted = 1;
307 break;
Damien Miller95def091999-11-25 00:26:21 +1100308 case 'g':
309 options.gateway_ports = 1;
310 break;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100311 case 'O':
312 if (strcmp(optarg, "check") == 0)
313 mux_command = SSHMUX_COMMAND_ALIVE_CHECK;
314 else if (strcmp(optarg, "exit") == 0)
315 mux_command = SSHMUX_COMMAND_TERMINATE;
316 else
317 fatal("Invalid multiplex command.");
318 break;
Damien Miller147bba32002-09-04 16:46:06 +1000319 case 'P': /* deprecated */
Damien Miller95def091999-11-25 00:26:21 +1100320 options.use_privileged_port = 0;
321 break;
Damien Miller95def091999-11-25 00:26:21 +1100322 case 'a':
323 options.forward_agent = 0;
324 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000325 case 'A':
326 options.forward_agent = 1;
327 break;
Damien Miller95def091999-11-25 00:26:21 +1100328 case 'k':
Damien Millere0113cc2003-11-24 13:10:09 +1100329 options.gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100330 break;
Darren Tucker415bddc2007-06-12 23:43:16 +1000331 case 'K':
332 options.gss_authentication = 1;
333 options.gss_deleg_creds = 1;
334 break;
Damien Miller95def091999-11-25 00:26:21 +1100335 case 'i':
336 if (stat(optarg, &st) < 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000337 fprintf(stderr, "Warning: Identity file %s "
Damien Miller3eb48b62005-03-01 21:15:46 +1100338 "not accessible: %s.\n", optarg,
339 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100340 break;
341 }
Damien Millerf4614452001-07-14 12:18:10 +1000342 if (options.num_identity_files >=
343 SSH_MAX_IDENTITY_FILES)
344 fatal("Too many identity files specified "
345 "(max %d)", SSH_MAX_IDENTITY_FILES);
346 options.identity_files[options.num_identity_files++] =
347 xstrdup(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100348 break;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000349 case 'I':
350#ifdef SMARTCARD
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000351 options.smartcard_device = xstrdup(optarg);
Ben Lindstrombcc18082001-08-06 21:59:25 +0000352#else
Ben Lindstromc5b68002001-07-04 04:52:03 +0000353 fprintf(stderr, "no support for smartcards.\n");
Ben Lindstrombcc18082001-08-06 21:59:25 +0000354#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000355 break;
Damien Miller95def091999-11-25 00:26:21 +1100356 case 't':
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000357 if (tty_flag)
358 force_tty_flag = 1;
Damien Miller95def091999-11-25 00:26:21 +1100359 tty_flag = 1;
360 break;
Damien Miller95def091999-11-25 00:26:21 +1100361 case 'v':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000362 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100363 debug_flag = 1;
364 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000365 } else {
366 if (options.log_level < SYSLOG_LEVEL_DEBUG3)
367 options.log_level++;
Damien Millere4340be2000-09-16 13:29:08 +1100368 break;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000369 }
Darren Tuckere9bf9842004-11-05 20:05:32 +1100370 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100371 case 'V':
Damien Miller0c889cd2004-03-22 09:36:00 +1100372 fprintf(stderr, "%s, %s\n",
Damien Miller2aa6d3c2004-09-12 16:53:04 +1000373 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
Damien Miller95def091999-11-25 00:26:21 +1100374 if (opt == 'V')
375 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100376 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100377 case 'w':
Damien Miller7b58e802005-12-13 19:33:19 +1100378 if (options.tun_open == -1)
379 options.tun_open = SSH_TUNMODE_DEFAULT;
Damien Millerd27b9472005-12-13 19:29:02 +1100380 options.tun_local = a2tun(optarg, &options.tun_remote);
Damien Miller7b58e802005-12-13 19:33:19 +1100381 if (options.tun_local == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100382 fprintf(stderr, "Bad tun device '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100383 exit(255);
Damien Millerd27b9472005-12-13 19:29:02 +1100384 }
385 break;
Damien Miller95def091999-11-25 00:26:21 +1100386 case 'q':
387 options.log_level = SYSLOG_LEVEL_QUIET;
388 break;
Damien Miller95def091999-11-25 00:26:21 +1100389 case 'e':
390 if (optarg[0] == '^' && optarg[2] == 0 &&
Damien Millerf4614452001-07-14 12:18:10 +1000391 (u_char) optarg[1] >= 64 &&
392 (u_char) optarg[1] < 128)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000393 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100394 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000395 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100396 else if (strcmp(optarg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000397 options.escape_char = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100398 else {
Damien Millerf4614452001-07-14 12:18:10 +1000399 fprintf(stderr, "Bad escape character '%s'.\n",
400 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100401 exit(255);
Damien Miller95def091999-11-25 00:26:21 +1100402 }
403 break;
Damien Miller95def091999-11-25 00:26:21 +1100404 case 'c':
Damien Millereba71ba2000-04-29 23:57:08 +1000405 if (ciphers_valid(optarg)) {
406 /* SSH2 only */
407 options.ciphers = xstrdup(optarg);
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000408 options.cipher = SSH_CIPHER_INVALID;
Damien Millereba71ba2000-04-29 23:57:08 +1000409 } else {
410 /* SSH1 only */
Damien Millere39cacc2000-11-29 12:18:44 +1100411 options.cipher = cipher_number(optarg);
412 if (options.cipher == -1) {
Damien Millerf4614452001-07-14 12:18:10 +1000413 fprintf(stderr,
414 "Unknown cipher type '%s'\n",
415 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100416 exit(255);
Damien Millereba71ba2000-04-29 23:57:08 +1000417 }
Damien Millerf4614452001-07-14 12:18:10 +1000418 if (options.cipher == SSH_CIPHER_3DES)
Damien Millere39cacc2000-11-29 12:18:44 +1100419 options.ciphers = "3des-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000420 else if (options.cipher == SSH_CIPHER_BLOWFISH)
Damien Millere39cacc2000-11-29 12:18:44 +1100421 options.ciphers = "blowfish-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000422 else
Damien Millere39cacc2000-11-29 12:18:44 +1100423 options.ciphers = (char *)-1;
Damien Miller95def091999-11-25 00:26:21 +1100424 }
425 break;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000426 case 'm':
427 if (mac_valid(optarg))
428 options.macs = xstrdup(optarg);
429 else {
Damien Millerf4614452001-07-14 12:18:10 +1000430 fprintf(stderr, "Unknown mac type '%s'\n",
431 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100432 exit(255);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000433 }
434 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000435 case 'M':
Damien Millerd14b1e72005-06-16 13:19:41 +1000436 if (options.control_master == SSHCTL_MASTER_YES)
437 options.control_master = SSHCTL_MASTER_ASK;
438 else
439 options.control_master = SSHCTL_MASTER_YES;
Damien Miller0e220db2004-06-15 10:34:08 +1000440 break;
Damien Miller95def091999-11-25 00:26:21 +1100441 case 'p':
Ben Lindstrom19066a12001-04-12 23:39:26 +0000442 options.port = a2port(optarg);
443 if (options.port == 0) {
Ben Lindstrom146edb92001-04-11 23:06:28 +0000444 fprintf(stderr, "Bad port '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100445 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000446 }
Damien Miller95def091999-11-25 00:26:21 +1100447 break;
Damien Miller95def091999-11-25 00:26:21 +1100448 case 'l':
449 options.user = optarg;
450 break;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000451
Damien Miller95def091999-11-25 00:26:21 +1100452 case 'L':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100453 if (parse_forward(&fwd, optarg))
454 add_local_forward(&options, &fwd);
455 else {
Damien Millerf4614452001-07-14 12:18:10 +1000456 fprintf(stderr,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100457 "Bad local forwarding specification '%s'\n",
Damien Millerf4614452001-07-14 12:18:10 +1000458 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100459 exit(255);
Ben Lindstrom1a174712001-09-12 17:56:15 +0000460 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100461 break;
462
463 case 'R':
464 if (parse_forward(&fwd, optarg)) {
465 add_remote_forward(&options, &fwd);
466 } else {
467 fprintf(stderr,
468 "Bad remote forwarding specification "
469 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100470 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100471 }
Damien Miller95def091999-11-25 00:26:21 +1100472 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000473
474 case 'D':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100475 cp = p = xstrdup(optarg);
476 memset(&fwd, '\0', sizeof(fwd));
477 fwd.connect_host = "socks";
478 if ((fwd.listen_host = hpdelim(&cp)) == NULL) {
479 fprintf(stderr, "Bad dynamic forwarding "
480 "specification '%.100s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100481 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100482 }
483 if (cp != NULL) {
484 fwd.listen_port = a2port(cp);
485 fwd.listen_host = cleanhostname(fwd.listen_host);
486 } else {
487 fwd.listen_port = a2port(fwd.listen_host);
Damien Millerbe1045d2005-08-12 22:10:56 +1000488 fwd.listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100489 }
490
491 if (fwd.listen_port == 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000492 fprintf(stderr, "Bad dynamic port '%s'\n",
493 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100494 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000495 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100496 add_local_forward(&options, &fwd);
497 xfree(p);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000498 break;
499
Damien Miller95def091999-11-25 00:26:21 +1100500 case 'C':
501 options.compression = 1;
502 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000503 case 'N':
504 no_shell_flag = 1;
505 no_tty_flag = 1;
506 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000507 case 'T':
508 no_tty_flag = 1;
509 break;
Damien Miller95def091999-11-25 00:26:21 +1100510 case 'o':
511 dummy = 1;
Damien Miller9836cf82003-12-17 16:30:06 +1100512 line = xstrdup(optarg);
Damien Millerf4614452001-07-14 12:18:10 +1000513 if (process_config_line(&options, host ? host : "",
Damien Miller9836cf82003-12-17 16:30:06 +1100514 line, "command-line", 0, &dummy) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100515 exit(255);
Damien Miller9836cf82003-12-17 16:30:06 +1100516 xfree(line);
Damien Miller95def091999-11-25 00:26:21 +1100517 break;
Damien Miller832562e2001-01-30 09:30:01 +1100518 case 's':
519 subsystem_flag = 1;
520 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000521 case 'S':
522 if (options.control_path != NULL)
523 free(options.control_path);
524 options.control_path = xstrdup(optarg);
Damien Miller0e220db2004-06-15 10:34:08 +1000525 break;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000526 case 'b':
527 options.bind_address = optarg;
528 break;
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000529 case 'F':
530 config = optarg;
531 break;
Damien Miller95def091999-11-25 00:26:21 +1100532 default:
533 usage();
534 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000535 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000536
Damien Millerf4614452001-07-14 12:18:10 +1000537 ac -= optind;
538 av += optind;
539
540 if (ac > 0 && !host && **av != '-') {
Ben Lindstromc276c122002-12-23 02:14:51 +0000541 if (strrchr(*av, '@')) {
Damien Millerf4614452001-07-14 12:18:10 +1000542 p = xstrdup(*av);
Ben Lindstromc276c122002-12-23 02:14:51 +0000543 cp = strrchr(p, '@');
Damien Millerf4614452001-07-14 12:18:10 +1000544 if (cp == NULL || cp == p)
545 usage();
546 options.user = p;
547 *cp = '\0';
548 host = ++cp;
549 } else
550 host = *av;
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000551 if (ac > 1) {
552 optind = optreset = 1;
Damien Millerf4614452001-07-14 12:18:10 +1000553 goto again;
554 }
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000555 ac--, av++;
Damien Millerf4614452001-07-14 12:18:10 +1000556 }
557
Damien Miller95def091999-11-25 00:26:21 +1100558 /* Check that we got a host name. */
559 if (!host)
560 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000561
Damien Millercb5e44a2000-09-29 12:12:36 +1100562 SSLeay_add_all_algorithms();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100563 ERR_load_crypto_strings();
Damien Millercb5e44a2000-09-29 12:12:36 +1100564
Damien Miller95def091999-11-25 00:26:21 +1100565 /* Initialize the command to execute on remote host. */
566 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000567
Damien Miller5428f641999-11-25 11:54:57 +1100568 /*
569 * Save the command to execute on the remote host in a buffer. There
570 * is no limit on the length of the command, except by the maximum
571 * packet size. Also sets the tty flag if there is no command.
572 */
Damien Millerf4614452001-07-14 12:18:10 +1000573 if (!ac) {
Damien Miller95def091999-11-25 00:26:21 +1100574 /* No command specified - execute shell on a tty. */
575 tty_flag = 1;
Damien Miller832562e2001-01-30 09:30:01 +1100576 if (subsystem_flag) {
Damien Millerf4614452001-07-14 12:18:10 +1000577 fprintf(stderr,
578 "You must specify a subsystem to invoke.\n");
Damien Miller832562e2001-01-30 09:30:01 +1100579 usage();
580 }
Damien Miller95def091999-11-25 00:26:21 +1100581 } else {
Damien Millerf4614452001-07-14 12:18:10 +1000582 /* A command has been specified. Store it into the buffer. */
583 for (i = 0; i < ac; i++) {
584 if (i)
Damien Miller95def091999-11-25 00:26:21 +1100585 buffer_append(&command, " ", 1);
586 buffer_append(&command, av[i], strlen(av[i]));
587 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000588 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000589
Damien Miller95def091999-11-25 00:26:21 +1100590 /* Cannot fork to background if no command. */
Damien Millercaf6dd62000-08-29 11:33:50 +1100591 if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
Damien Miller95def091999-11-25 00:26:21 +1100592 fatal("Cannot fork into background without a command to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000593
Damien Miller95def091999-11-25 00:26:21 +1100594 /* Allocate a tty by default if no command specified. */
595 if (buffer_len(&command) == 0)
596 tty_flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000597
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000598 /* Force no tty */
Ben Lindstromc72745a2000-12-02 19:03:54 +0000599 if (no_tty_flag)
600 tty_flag = 0;
Damien Miller95def091999-11-25 00:26:21 +1100601 /* Do not allocate a tty if stdin is not a tty. */
Damien Millerddee5752005-05-26 12:05:05 +1000602 if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100603 if (tty_flag)
Damien Miller996acd22003-04-09 20:59:48 +1000604 logit("Pseudo-terminal will not be allocated because stdin is not a terminal.");
Damien Miller95def091999-11-25 00:26:21 +1100605 tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000606 }
Damien Miller1383bd82000-04-06 12:32:37 +1000607
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000608 /*
609 * Initialize "log" output. Since we are the client all output
610 * actually goes to stderr.
611 */
Ben Lindstrom2b646522001-04-12 16:16:57 +0000612 log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
613 SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000614
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000615 /*
616 * Read per-user configuration file. Ignore the system wide config
617 * file if the user specifies a config file on the command line.
618 */
619 if (config != NULL) {
Damien Miller914420f2004-04-20 20:14:07 +1000620 if (!read_config_file(config, host, &options, 0))
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000621 fatal("Can't open user config file %.100s: "
622 "%.100s", config, strerror(errno));
Damien Miller80163902007-01-05 16:30:16 +1100623 } else {
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000624 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
625 _PATH_SSH_USER_CONFFILE);
Damien Miller57a44762004-04-20 20:11:57 +1000626 (void)read_config_file(buf, host, &options, 1);
Ben Lindstrom83f07d12001-10-03 17:22:29 +0000627
628 /* Read systemwide configuration file after use config. */
Darren Tuckerfc959702004-07-17 16:12:08 +1000629 (void)read_config_file(_PATH_HOST_CONFIG_FILE, host,
Damien Miller57a44762004-04-20 20:11:57 +1000630 &options, 0);
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000631 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000632
Damien Miller95def091999-11-25 00:26:21 +1100633 /* Fill configuration defaults. */
634 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000635
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000636 channel_set_af(options.address_family);
637
Damien Miller95def091999-11-25 00:26:21 +1100638 /* reinit */
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000639 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000640
Damien Miller60bc5172001-03-19 09:38:15 +1100641 seed_rng();
642
Damien Miller95def091999-11-25 00:26:21 +1100643 if (options.user == NULL)
644 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000645
Damien Miller95def091999-11-25 00:26:21 +1100646 if (options.hostname != NULL)
647 host = options.hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000648
Darren Tucker3f521e22003-07-03 16:20:42 +1000649 /* force lowercase for hostkey matching */
650 if (options.host_key_alias != NULL) {
651 for (p = options.host_key_alias; *p; p++)
652 if (isupper(*p))
Damien Miller1d2b6702006-03-26 14:09:54 +1100653 *p = (char)tolower(*p);
Darren Tucker3f521e22003-07-03 16:20:42 +1000654 }
655
Damien Miller7c71cc72005-06-26 08:56:31 +1000656 /* Get default port if port has not been set. */
657 if (options.port == 0) {
658 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
659 options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
660 }
661
Damien Miller9f1e33a2003-02-24 11:57:32 +1100662 if (options.proxy_command != NULL &&
Darren Tucker32e42c72007-12-02 23:01:03 +1100663 strcmp(options.proxy_command, "none") == 0) {
664 xfree(options.proxy_command);
Damien Miller9f1e33a2003-02-24 11:57:32 +1100665 options.proxy_command = NULL;
Darren Tucker32e42c72007-12-02 23:01:03 +1100666 }
Damien Miller8f74c8f2005-06-26 08:56:03 +1000667 if (options.control_path != NULL &&
Darren Tucker32e42c72007-12-02 23:01:03 +1100668 strcmp(options.control_path, "none") == 0) {
669 xfree(options.control_path);
Damien Miller8f74c8f2005-06-26 08:56:03 +1000670 options.control_path = NULL;
Darren Tucker32e42c72007-12-02 23:01:03 +1100671 }
Damien Miller9f1e33a2003-02-24 11:57:32 +1100672
Damien Miller0e220db2004-06-15 10:34:08 +1000673 if (options.control_path != NULL) {
Damien Miller6b1d53c2006-03-31 23:13:21 +1100674 char thishost[NI_MAXHOST];
Damien Miller3ec54c72006-03-15 11:30:13 +1100675
Damien Miller6b1d53c2006-03-31 23:13:21 +1100676 if (gethostname(thishost, sizeof(thishost)) == -1)
Damien Miller3ec54c72006-03-15 11:30:13 +1100677 fatal("gethostname: %s", strerror(errno));
Damien Miller6476cad2005-06-16 13:18:34 +1000678 snprintf(buf, sizeof(buf), "%d", options.port);
679 cp = tilde_expand_filename(options.control_path,
680 original_real_uid);
Darren Tucker32e42c72007-12-02 23:01:03 +1100681 xfree(options.control_path);
Damien Miller6476cad2005-06-16 13:18:34 +1000682 options.control_path = percent_expand(cp, "p", buf, "h", host,
Damien Miller6b1d53c2006-03-31 23:13:21 +1100683 "r", options.user, "l", thishost, (char *)NULL);
Damien Miller6476cad2005-06-16 13:18:34 +1000684 xfree(cp);
Damien Miller0e220db2004-06-15 10:34:08 +1000685 }
Darren Tucker0814d312005-06-01 23:08:51 +1000686 if (mux_command != 0 && options.control_path == NULL)
687 fatal("No ControlPath specified for \"-O\" command");
Damien Millerd14b1e72005-06-16 13:19:41 +1000688 if (options.control_path != NULL)
Damien Millerdadfd4d2005-05-26 12:07:13 +1000689 control_client(options.control_path);
Damien Miller0e220db2004-06-15 10:34:08 +1000690
Damien Miller67bd0622007-09-17 12:06:57 +1000691 timeout_ms = options.connection_timeout * 1000;
692
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000693 /* Open a connection to the remote host. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000694 if (ssh_connect(host, &hostaddr, options.port,
Damien Miller67bd0622007-09-17 12:06:57 +1000695 options.address_family, options.connection_attempts, &timeout_ms,
696 options.tcp_keep_alive,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000697#ifdef HAVE_CYGWIN
698 options.use_privileged_port,
699#else
Ben Lindstromf9c48842002-06-11 16:37:51 +0000700 original_effective_uid == 0 && options.use_privileged_port,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000701#endif
Ben Lindstromda394ca2002-06-12 16:11:12 +0000702 options.proxy_command) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100703 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000704
Damien Miller67bd0622007-09-17 12:06:57 +1000705 if (timeout_ms > 0)
706 debug3("timeout: %d ms remain after connect", timeout_ms);
707
Damien Miller5428f641999-11-25 11:54:57 +1100708 /*
709 * If we successfully made the connection, load the host private key
710 * in case we will need it later for combined rsa-rhosts
711 * authentication. This must be done before releasing extra
712 * privileges, because the file is only readable by root.
Ben Lindstrom9e5bb572002-06-06 19:58:27 +0000713 * If we cannot access the private keys, load the public keys
714 * instead and try to execute the ssh-keysign helper instead.
Damien Miller5428f641999-11-25 11:54:57 +1100715 */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000716 sensitive_data.nkeys = 0;
717 sensitive_data.keys = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000718 sensitive_data.external_keysign = 0;
Ben Lindstrom24157572002-06-12 16:09:39 +0000719 if (options.rhosts_rsa_authentication ||
720 options.hostbased_authentication) {
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000721 sensitive_data.nkeys = 3;
Damien Millerddd63ab2006-03-31 23:10:51 +1100722 sensitive_data.keys = xcalloc(sensitive_data.nkeys,
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000723 sizeof(Key));
Ben Lindstromf9c48842002-06-11 16:37:51 +0000724
725 PRIV_START;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000726 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
Darren Tucker232b76f2006-05-06 17:41:51 +1000727 _PATH_HOST_KEY_FILE, "", NULL, NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000728 sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
Darren Tucker232b76f2006-05-06 17:41:51 +1000729 _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000730 sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
Darren Tucker232b76f2006-05-06 17:41:51 +1000731 _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL);
Ben Lindstromf9c48842002-06-11 16:37:51 +0000732 PRIV_END;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000733
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +0000734 if (options.hostbased_authentication == 1 &&
735 sensitive_data.keys[0] == NULL &&
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000736 sensitive_data.keys[1] == NULL &&
737 sensitive_data.keys[2] == NULL) {
738 sensitive_data.keys[1] = key_load_public(
739 _PATH_HOST_DSA_KEY_FILE, NULL);
740 sensitive_data.keys[2] = key_load_public(
741 _PATH_HOST_RSA_KEY_FILE, NULL);
742 sensitive_data.external_keysign = 1;
743 }
Damien Miller95def091999-11-25 00:26:21 +1100744 }
Damien Miller5428f641999-11-25 11:54:57 +1100745 /*
746 * Get rid of any extra privileges that we may have. We will no
747 * longer need them. Also, extra privileges could make it very hard
748 * to read identity files and other non-world-readable files from the
749 * user's home directory if it happens to be on a NFS volume where
750 * root is mapped to nobody.
751 */
Darren Tucker25f60a72004-08-15 17:23:34 +1000752 if (original_effective_uid == 0) {
753 PRIV_START;
754 permanently_set_uid(pw);
755 }
Damien Miller95def091999-11-25 00:26:21 +1100756
Damien Miller5428f641999-11-25 11:54:57 +1100757 /*
758 * Now that we are back to our own permissions, create ~/.ssh
Damien Miller788f2122005-11-05 15:14:59 +1100759 * directory if it doesn't already exist.
Damien Miller5428f641999-11-25 11:54:57 +1100760 */
Ben Lindstrom930b14a2001-08-15 23:19:21 +0000761 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 +1100762 if (stat(buf, &st) < 0)
Damien Millerbe484b52000-07-15 14:14:16 +1000763 if (mkdir(buf, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100764 error("Could not create directory '%.200s'.", buf);
765
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000766 /* load options.identity_files */
767 load_public_identity_files();
768
769 /* Expand ~ in known host file names. */
770 /* XXX mem-leaks: */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100771 options.system_hostfile =
772 tilde_expand_filename(options.system_hostfile, original_real_uid);
773 options.user_hostfile =
774 tilde_expand_filename(options.user_hostfile, original_real_uid);
775 options.system_hostfile2 =
776 tilde_expand_filename(options.system_hostfile2, original_real_uid);
777 options.user_hostfile2 =
778 tilde_expand_filename(options.user_hostfile2, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100779
Damien Miller07cd5892001-11-12 10:52:03 +1100780 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
781
Damien Miller95def091999-11-25 00:26:21 +1100782 /* Log into the remote system. This never returns if the login fails. */
Damien Miller67bd0622007-09-17 12:06:57 +1000783 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr,
784 pw, timeout_ms);
Damien Miller95def091999-11-25 00:26:21 +1100785
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000786 /* We no longer need the private host keys. Clear them now. */
787 if (sensitive_data.nkeys != 0) {
788 for (i = 0; i < sensitive_data.nkeys; i++) {
789 if (sensitive_data.keys[i] != NULL) {
790 /* Destroys contents safely */
791 debug3("clear hostkey %d", i);
792 key_free(sensitive_data.keys[i]);
793 sensitive_data.keys[i] = NULL;
794 }
795 }
796 xfree(sensitive_data.keys);
797 }
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +0000798 for (i = 0; i < options.num_identity_files; i++) {
799 if (options.identity_files[i]) {
800 xfree(options.identity_files[i]);
801 options.identity_files[i] = NULL;
802 }
803 if (options.identity_keys[i]) {
804 key_free(options.identity_keys[i]);
805 options.identity_keys[i] = NULL;
806 }
807 }
Damien Miller95def091999-11-25 00:26:21 +1100808
Damien Miller1383bd82000-04-06 12:32:37 +1000809 exit_status = compat20 ? ssh_session2() : ssh_session();
810 packet_close();
Damien Miller8c4e18a2002-09-19 12:05:02 +1000811
Damien Miller0e220db2004-06-15 10:34:08 +1000812 if (options.control_path != NULL && control_fd != -1)
813 unlink(options.control_path);
814
Damien Miller8c4e18a2002-09-19 12:05:02 +1000815 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100816 * Send SIGHUP to proxy command if used. We don't wait() in
Damien Miller8c4e18a2002-09-19 12:05:02 +1000817 * case it hangs and instead rely on init to reap the child
818 */
819 if (proxy_command_pid > 1)
820 kill(proxy_command_pid, SIGHUP);
821
Damien Miller1383bd82000-04-06 12:32:37 +1000822 return exit_status;
823}
824
Ben Lindstrombba81212001-06-25 05:01:22 +0000825static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100826ssh_init_forwarding(void)
827{
Kevin Steves12057502001-02-05 14:54:34 +0000828 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100829 int i;
Kevin Steves12057502001-02-05 14:54:34 +0000830
Damien Miller0bc1bd82000-11-13 22:57:25 +1100831 /* Initiate local TCP/IP port forwardings. */
832 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100833 debug("Local connections to %.200s:%d forwarded to remote "
834 "address %.200s:%d",
Darren Tucker47eede72005-03-14 23:08:12 +1100835 (options.local_forwards[i].listen_host == NULL) ?
836 (options.gateway_ports ? "*" : "LOCALHOST") :
Damien Millerf91ee4c2005-03-01 21:24:33 +1100837 options.local_forwards[i].listen_host,
838 options.local_forwards[i].listen_port,
839 options.local_forwards[i].connect_host,
840 options.local_forwards[i].connect_port);
Damien Millerb16461c2002-01-22 23:29:22 +1100841 success += channel_setup_local_fwd_listener(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100842 options.local_forwards[i].listen_host,
843 options.local_forwards[i].listen_port,
844 options.local_forwards[i].connect_host,
845 options.local_forwards[i].connect_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100846 options.gateway_ports);
847 }
Darren Tuckere7d4b192006-07-12 22:17:10 +1000848 if (i > 0 && success != i && options.exit_on_forward_failure)
849 fatal("Could not request local forwarding.");
Kevin Steves12057502001-02-05 14:54:34 +0000850 if (i > 0 && success == 0)
851 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100852
853 /* Initiate remote TCP/IP port forwardings. */
854 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100855 debug("Remote connections from %.200s:%d forwarded to "
856 "local address %.200s:%d",
Damien Miller46d38de2005-07-17 17:02:09 +1000857 (options.remote_forwards[i].listen_host == NULL) ?
Damien Milleraa3bb102005-11-05 15:12:59 +1100858 "LOCALHOST" : options.remote_forwards[i].listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100859 options.remote_forwards[i].listen_port,
860 options.remote_forwards[i].connect_host,
861 options.remote_forwards[i].connect_port);
Darren Tuckere7d4b192006-07-12 22:17:10 +1000862 if (channel_request_remote_forwarding(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100863 options.remote_forwards[i].listen_host,
864 options.remote_forwards[i].listen_port,
865 options.remote_forwards[i].connect_host,
Darren Tuckere7d4b192006-07-12 22:17:10 +1000866 options.remote_forwards[i].connect_port) < 0) {
867 if (options.exit_on_forward_failure)
868 fatal("Could not request remote forwarding.");
869 else
870 logit("Warning: Could not request remote "
871 "forwarding.");
872 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100873 }
Damien Millerb3ce9fe2007-08-08 14:32:41 +1000874
875 /* Initiate tunnel forwarding. */
876 if (options.tun_open != SSH_TUNMODE_NO) {
877 if (client_request_tun_fwd(options.tun_open,
878 options.tun_local, options.tun_remote) == -1) {
879 if (options.exit_on_forward_failure)
880 fatal("Could not request tunnel forwarding.");
881 else
882 error("Could not request tunnel forwarding.");
883 }
884 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100885}
886
Ben Lindstrombba81212001-06-25 05:01:22 +0000887static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100888check_agent_present(void)
889{
890 if (options.forward_agent) {
Damien Miller788f2122005-11-05 15:14:59 +1100891 /* Clear agent forwarding if we don't have an agent. */
Damien Miller789e95d2002-09-12 09:52:46 +1000892 if (!ssh_agent_present())
Damien Miller0bc1bd82000-11-13 22:57:25 +1100893 options.forward_agent = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100894 }
895}
896
Ben Lindstrombba81212001-06-25 05:01:22 +0000897static int
Damien Miller1383bd82000-04-06 12:32:37 +1000898ssh_session(void)
899{
900 int type;
Damien Miller1383bd82000-04-06 12:32:37 +1000901 int interactive = 0;
902 int have_tty = 0;
903 struct winsize ws;
Damien Miller1383bd82000-04-06 12:32:37 +1000904 char *cp;
Damien Miller17e7ed02005-06-17 12:54:33 +1000905 const char *display;
Damien Miller1383bd82000-04-06 12:32:37 +1000906
Damien Miller95def091999-11-25 00:26:21 +1100907 /* Enable compression if requested. */
908 if (options.compression) {
909 debug("Requesting compression at level %d.", options.compression_level);
910
911 if (options.compression_level < 1 || options.compression_level > 9)
912 fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
913
914 /* Send the request. */
915 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
916 packet_put_int(options.compression_level);
917 packet_send();
918 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100919 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100920 if (type == SSH_SMSG_SUCCESS)
921 packet_start_compression(options.compression_level);
922 else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000923 logit("Warning: Remote host refused compression.");
Damien Miller95def091999-11-25 00:26:21 +1100924 else
925 packet_disconnect("Protocol error waiting for compression response.");
926 }
927 /* Allocate a pseudo tty if appropriate. */
928 if (tty_flag) {
929 debug("Requesting pty.");
930
931 /* Start the packet. */
932 packet_start(SSH_CMSG_REQUEST_PTY);
933
934 /* Store TERM in the packet. There is no limit on the
935 length of the string. */
936 cp = getenv("TERM");
937 if (!cp)
938 cp = "";
Ben Lindstrom664408d2001-06-09 01:42:01 +0000939 packet_put_cstring(cp);
Damien Miller95def091999-11-25 00:26:21 +1100940
941 /* Store window size in the packet. */
942 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
943 memset(&ws, 0, sizeof(ws));
Damien Miller71a73672006-03-26 14:04:36 +1100944 packet_put_int((u_int)ws.ws_row);
945 packet_put_int((u_int)ws.ws_col);
946 packet_put_int((u_int)ws.ws_xpixel);
947 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller95def091999-11-25 00:26:21 +1100948
949 /* Store tty modes in the packet. */
Ben Lindstromae8e2d32001-04-14 23:13:02 +0000950 tty_make_modes(fileno(stdin), NULL);
Damien Miller95def091999-11-25 00:26:21 +1100951
952 /* Send the packet, and wait for it to leave. */
953 packet_send();
954 packet_write_wait();
955
956 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100957 type = packet_read();
Damien Miller450a7a12000-03-26 13:04:51 +1000958 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100959 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +1000960 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000961 } else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000962 logit("Warning: Remote host failed or refused to allocate a pseudo tty.");
Damien Miller95def091999-11-25 00:26:21 +1100963 else
964 packet_disconnect("Protocol error waiting for pty request response.");
965 }
966 /* Request X11 forwarding if enabled and DISPLAY is set. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000967 display = getenv("DISPLAY");
968 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000969 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +1000970 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000971 client_x11_get_proto(display, options.xauth_location,
972 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +1000973 /* Request forwarding with authentication spoofing. */
Damien Miller95def091999-11-25 00:26:21 +1100974 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +1000975 x11_request_forwarding_with_spoofing(0, display, proto, data);
Damien Miller95def091999-11-25 00:26:21 +1100976
977 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100978 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100979 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100980 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +1000981 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller996acd22003-04-09 20:59:48 +1000982 logit("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +1000983 } else {
Damien Miller95def091999-11-25 00:26:21 +1100984 packet_disconnect("Protocol error waiting for X11 forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +1000985 }
Damien Miller95def091999-11-25 00:26:21 +1100986 }
987 /* Tell the packet module whether this is an interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000988 packet_set_interactive(interactive);
Damien Miller95def091999-11-25 00:26:21 +1100989
990 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100991 check_agent_present();
992
Damien Miller95def091999-11-25 00:26:21 +1100993 if (options.forward_agent) {
994 debug("Requesting authentication agent forwarding.");
995 auth_request_forwarding();
996
997 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100998 type = packet_read();
Damien Miller48b03fc2002-01-22 23:11:40 +1100999 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +11001000 if (type != SSH_SMSG_SUCCESS)
Damien Miller996acd22003-04-09 20:59:48 +10001001 logit("Warning: Remote host denied authentication agent forwarding.");
Damien Miller95def091999-11-25 00:26:21 +11001002 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001003
Damien Miller0bc1bd82000-11-13 22:57:25 +11001004 /* Initiate port forwardings. */
1005 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001006
Darren Tuckerb776c852007-12-02 23:06:35 +11001007 /* Execute a local command */
1008 if (options.local_command != NULL &&
1009 options.permit_local_command)
1010 ssh_local_cmd(options.local_command);
1011
Damien Miller5428f641999-11-25 11:54:57 +11001012 /* If requested, let ssh continue in the background. */
Damien Miller4af51302000-04-16 11:18:38 +10001013 if (fork_after_authentication_flag)
Damien Miller5428f641999-11-25 11:54:57 +11001014 if (daemon(1, 1) < 0)
1015 fatal("daemon() failed: %.200s", strerror(errno));
1016
1017 /*
1018 * If a command was specified on the command line, execute the
1019 * command now. Otherwise request the server to start a shell.
1020 */
Damien Miller95def091999-11-25 00:26:21 +11001021 if (buffer_len(&command) > 0) {
1022 int len = buffer_len(&command);
1023 if (len > 900)
1024 len = 900;
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001025 debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
Damien Miller95def091999-11-25 00:26:21 +11001026 packet_start(SSH_CMSG_EXEC_CMD);
1027 packet_put_string(buffer_ptr(&command), buffer_len(&command));
1028 packet_send();
1029 packet_write_wait();
1030 } else {
1031 debug("Requesting shell.");
1032 packet_start(SSH_CMSG_EXEC_SHELL);
1033 packet_send();
1034 packet_write_wait();
1035 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001036
Damien Miller95def091999-11-25 00:26:21 +11001037 /* Enter the interactive session. */
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001038 return client_loop(have_tty, tty_flag ?
1039 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
Damien Miller1383bd82000-04-06 12:32:37 +10001040}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001041
Damien Miller2797f7f2002-04-23 21:09:44 +10001042void
Damien Miller509b0102003-12-17 16:33:10 +11001043client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt)
Damien Miller2797f7f2002-04-23 21:09:44 +10001044{
1045 int i;
1046
1047 i = client_global_request_id++;
Damien Miller509b0102003-12-17 16:33:10 +11001048 if (i >= options.num_remote_forwards)
Damien Miller2797f7f2002-04-23 21:09:44 +10001049 return;
Damien Miller2797f7f2002-04-23 21:09:44 +10001050 debug("remote forward %s for: listen %d, connect %s:%d",
1051 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
Damien Millerf91ee4c2005-03-01 21:24:33 +11001052 options.remote_forwards[i].listen_port,
1053 options.remote_forwards[i].connect_host,
1054 options.remote_forwards[i].connect_port);
Darren Tuckere7d4b192006-07-12 22:17:10 +10001055 if (type == SSH2_MSG_REQUEST_FAILURE) {
1056 if (options.exit_on_forward_failure)
1057 fatal("Error: remote port forwarding failed for "
1058 "listen port %d",
1059 options.remote_forwards[i].listen_port);
1060 else
1061 logit("Warning: remote port forwarding failed for "
1062 "listen port %d",
1063 options.remote_forwards[i].listen_port);
1064 }
Damien Miller2797f7f2002-04-23 21:09:44 +10001065}
1066
Damien Miller0e220db2004-06-15 10:34:08 +10001067static void
1068ssh_control_listener(void)
1069{
1070 struct sockaddr_un addr;
1071 mode_t old_umask;
Damien Miller07b6ff12004-06-15 11:14:45 +10001072 int addr_len;
1073
Damien Millerd14b1e72005-06-16 13:19:41 +10001074 if (options.control_path == NULL ||
1075 options.control_master == SSHCTL_MASTER_NO)
Damien Miller0e220db2004-06-15 10:34:08 +10001076 return;
1077
Damien Millerd14b1e72005-06-16 13:19:41 +10001078 debug("setting up multiplex master socket");
1079
Damien Miller0e220db2004-06-15 10:34:08 +10001080 memset(&addr, '\0', sizeof(addr));
1081 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001082 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001083 strlen(options.control_path) + 1;
1084
1085 if (strlcpy(addr.sun_path, options.control_path,
1086 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1087 fatal("ControlPath too long");
1088
1089 if ((control_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001090 fatal("%s socket(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001091
1092 old_umask = umask(0177);
Damien Miller90967402006-03-26 14:07:26 +11001093 if (bind(control_fd, (struct sockaddr *)&addr, addr_len) == -1) {
Damien Miller0e220db2004-06-15 10:34:08 +10001094 control_fd = -1;
Damien Miller4f10e252005-05-04 15:33:09 +10001095 if (errno == EINVAL || errno == EADDRINUSE)
Damien Miller0e220db2004-06-15 10:34:08 +10001096 fatal("ControlSocket %s already exists",
1097 options.control_path);
1098 else
Damien Miller15d72a02005-11-05 15:07:33 +11001099 fatal("%s bind(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001100 }
1101 umask(old_umask);
1102
1103 if (listen(control_fd, 64) == -1)
Damien Miller15d72a02005-11-05 15:07:33 +11001104 fatal("%s listen(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001105
1106 set_nonblock(control_fd);
1107}
1108
Ben Lindstromf558cf62001-09-20 23:13:49 +00001109/* request pty/x11/agent/tcpfwd/shell for channel */
Ben Lindstrombba81212001-06-25 05:01:22 +00001110static void
Ben Lindstromf558cf62001-09-20 23:13:49 +00001111ssh_session2_setup(int id, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +10001112{
Damien Miller3756dce2004-06-18 01:17:29 +10001113 extern char **environ;
Damien Miller17e7ed02005-06-17 12:54:33 +10001114 const char *display;
Damien Miller0e220db2004-06-15 10:34:08 +10001115 int interactive = tty_flag;
Damien Miller17e7ed02005-06-17 12:54:33 +10001116
Damien Miller46d38de2005-07-17 17:02:09 +10001117 display = getenv("DISPLAY");
Damien Miller17e7ed02005-06-17 12:54:33 +10001118 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001119 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001120 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001121 client_x11_get_proto(display, options.xauth_location,
1122 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001123 /* Request forwarding with authentication spoofing. */
1124 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +10001125 x11_request_forwarding_with_spoofing(id, display, proto, data);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001126 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001127 /* XXX wait for reply */
1128 }
1129
Damien Miller0bc1bd82000-11-13 22:57:25 +11001130 check_agent_present();
1131 if (options.forward_agent) {
1132 debug("Requesting authentication agent forwarding.");
1133 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1134 packet_send();
1135 }
1136
Damien Miller0e220db2004-06-15 10:34:08 +10001137 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
Damien Miller5771ed72008-05-19 15:35:33 +10001138 NULL, fileno(stdin), &command, environ);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001139
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001140 packet_set_interactive(interactive);
Damien Miller1383bd82000-04-06 12:32:37 +10001141}
1142
Ben Lindstromf558cf62001-09-20 23:13:49 +00001143/* open new channel for a session */
Ben Lindstrombba81212001-06-25 05:01:22 +00001144static int
Ben Lindstromf558cf62001-09-20 23:13:49 +00001145ssh_session2_open(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001146{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001147 Channel *c;
1148 int window, packetmax, in, out, err;
Damien Millerad833b32000-08-23 10:46:23 +10001149
Damien Millercaf6dd62000-08-29 11:33:50 +11001150 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001151 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001152 } else {
1153 in = dup(STDIN_FILENO);
1154 }
1155 out = dup(STDOUT_FILENO);
1156 err = dup(STDERR_FILENO);
1157
1158 if (in < 0 || out < 0 || err < 0)
1159 fatal("dup() in/out/err failed");
1160
Damien Miller69b69aa2000-10-28 14:19:58 +11001161 /* enable nonblocking unless tty */
1162 if (!isatty(in))
1163 set_nonblock(in);
1164 if (!isatty(out))
1165 set_nonblock(out);
1166 if (!isatty(err))
1167 set_nonblock(err);
1168
Damien Millere4340be2000-09-16 13:29:08 +11001169 window = CHAN_SES_WINDOW_DEFAULT;
1170 packetmax = CHAN_SES_PACKET_DEFAULT;
Damien Miller19a59452002-02-19 15:20:57 +11001171 if (tty_flag) {
1172 window >>= 1;
1173 packetmax >>= 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001174 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001175 c = channel_new(
Damien Miller1383bd82000-04-06 12:32:37 +10001176 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001177 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001178 "client-session", /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001179
Ben Lindstromf558cf62001-09-20 23:13:49 +00001180 debug3("ssh_session2_open: channel_new: %d", c->self);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001181
Ben Lindstrom5ec26452001-06-09 00:18:51 +00001182 channel_send_open(c->self);
Ben Lindstromf558cf62001-09-20 23:13:49 +00001183 if (!no_shell_flag)
Damien Millerb84886b2008-05-19 15:05:07 +10001184 channel_register_open_confirm(c->self,
1185 ssh_session2_setup, NULL);
Damien Miller1383bd82000-04-06 12:32:37 +10001186
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001187 return c->self;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001188}
1189
Ben Lindstrombba81212001-06-25 05:01:22 +00001190static int
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001191ssh_session2(void)
1192{
Ben Lindstromf558cf62001-09-20 23:13:49 +00001193 int id = -1;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001194
1195 /* XXX should be pre-session */
1196 ssh_init_forwarding();
1197
Ben Lindstromf558cf62001-09-20 23:13:49 +00001198 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1199 id = ssh_session2_open();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001200
Damien Millerd27b9472005-12-13 19:29:02 +11001201 /* Execute a local command */
1202 if (options.local_command != NULL &&
1203 options.permit_local_command)
1204 ssh_local_cmd(options.local_command);
1205
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001206 /* Start listening for multiplex clients */
1207 ssh_control_listener();
1208
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001209 /* If requested, let ssh continue in the background. */
1210 if (fork_after_authentication_flag)
1211 if (daemon(1, 1) < 0)
1212 fatal("daemon() failed: %.200s", strerror(errno));
1213
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001214 return client_loop(tty_flag, tty_flag ?
1215 options.escape_char : SSH_ESCAPECHAR_NONE, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001216}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001217
Ben Lindstrombba81212001-06-25 05:01:22 +00001218static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001219load_public_identity_files(void)
1220{
Damien Miller6b1d53c2006-03-31 23:13:21 +11001221 char *filename, *cp, thishost[NI_MAXHOST];
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001222 char *pwdir = NULL, *pwname = NULL;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001223 int i = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001224 Key *public;
Damien Miller6b1d53c2006-03-31 23:13:21 +11001225 struct passwd *pw;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001226#ifdef SMARTCARD
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001227 Key **keys;
1228
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001229 if (options.smartcard_device != NULL &&
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001230 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
Damien Miller4dec5d72006-08-05 11:38:40 +10001231 (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001232 int count = 0;
1233 for (i = 0; keys[i] != NULL; i++) {
1234 count++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001235 memmove(&options.identity_files[1], &options.identity_files[0],
1236 sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
1237 memmove(&options.identity_keys[1], &options.identity_keys[0],
1238 sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1239 options.num_identity_files++;
1240 options.identity_keys[0] = keys[i];
Damien Miller56a0bb02003-06-18 20:28:40 +10001241 options.identity_files[0] = sc_get_key_label(keys[i]);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001242 }
Ben Lindstrom4f054602002-03-26 03:23:00 +00001243 if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1244 options.num_identity_files = SSH_MAX_IDENTITY_FILES;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001245 i = count;
1246 xfree(keys);
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001247 }
Ben Lindstromffce1472001-08-06 21:57:31 +00001248#endif /* SMARTCARD */
Damien Miller6b1d53c2006-03-31 23:13:21 +11001249 if ((pw = getpwuid(original_real_uid)) == NULL)
1250 fatal("load_public_identity_files: getpwuid failed");
Darren Tuckere143f062007-12-02 23:21:16 +11001251 pwname = xstrdup(pw->pw_name);
1252 pwdir = xstrdup(pw->pw_dir);
Damien Miller6b1d53c2006-03-31 23:13:21 +11001253 if (gethostname(thishost, sizeof(thishost)) == -1)
1254 fatal("load_public_identity_files: gethostname: %s",
1255 strerror(errno));
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001256 for (; i < options.num_identity_files; i++) {
Damien Miller6b1d53c2006-03-31 23:13:21 +11001257 cp = tilde_expand_filename(options.identity_files[i],
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001258 original_real_uid);
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001259 filename = percent_expand(cp, "d", pwdir,
1260 "u", pwname, "l", thishost, "h", host,
Damien Miller6b1d53c2006-03-31 23:13:21 +11001261 "r", options.user, (char *)NULL);
1262 xfree(cp);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001263 public = key_load_public(filename, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001264 debug("identity file %s type %d", filename,
1265 public ? public->type : -1);
1266 xfree(options.identity_files[i]);
1267 options.identity_files[i] = filename;
1268 options.identity_keys[i] = public;
1269 }
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001270 bzero(pwname, strlen(pwname));
Darren Tuckere143f062007-12-02 23:21:16 +11001271 xfree(pwname);
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001272 bzero(pwdir, strlen(pwdir));
Darren Tuckere143f062007-12-02 23:21:16 +11001273 xfree(pwdir);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001274}
Damien Miller0e220db2004-06-15 10:34:08 +10001275
1276static void
1277control_client_sighandler(int signo)
1278{
1279 control_client_terminate = signo;
1280}
1281
1282static void
1283control_client_sigrelay(int signo)
1284{
Darren Tucker06321f52007-12-02 23:22:52 +11001285 int save_errno = errno;
1286
Damien Miller0e220db2004-06-15 10:34:08 +10001287 if (control_server_pid > 1)
1288 kill(control_server_pid, signo);
Darren Tucker06321f52007-12-02 23:22:52 +11001289
1290 errno = save_errno;
Damien Miller0e220db2004-06-15 10:34:08 +10001291}
1292
Darren Tucker365433f2004-06-22 12:29:23 +10001293static int
1294env_permitted(char *env)
1295{
Darren Tucker284706a2006-07-12 22:16:23 +10001296 int i, ret;
Darren Tucker365433f2004-06-22 12:29:23 +10001297 char name[1024], *cp;
1298
Darren Tucker284706a2006-07-12 22:16:23 +10001299 if ((cp = strchr(env, '=')) == NULL || cp == env)
Darren Tucker365433f2004-06-22 12:29:23 +10001300 return (0);
Darren Tucker57f42242006-07-12 22:23:35 +10001301 ret = snprintf(name, sizeof(name), "%.*s", (int)(cp - env), env);
Darren Tucker284706a2006-07-12 22:16:23 +10001302 if (ret <= 0 || (size_t)ret >= sizeof(name))
1303 fatal("env_permitted: name '%.100s...' too long", env);
Darren Tucker365433f2004-06-22 12:29:23 +10001304
1305 for (i = 0; i < options.num_send_env; i++)
1306 if (match_pattern(name, options.send_env[i]))
1307 return (1);
1308
1309 return (0);
1310}
1311
Damien Miller0e220db2004-06-15 10:34:08 +10001312static void
1313control_client(const char *path)
1314{
1315 struct sockaddr_un addr;
Darren Tucker132367f2007-06-25 18:59:17 +10001316 int i, r, fd, sock, exitval[2], num_env, addr_len;
Damien Miller0e220db2004-06-15 10:34:08 +10001317 Buffer m;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001318 char *term;
Damien Miller3756dce2004-06-18 01:17:29 +10001319 extern char **environ;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001320 u_int flags;
Darren Tuckerfc959702004-07-17 16:12:08 +10001321
Damien Millerd14b1e72005-06-16 13:19:41 +10001322 if (mux_command == 0)
1323 mux_command = SSHMUX_COMMAND_OPEN;
1324
1325 switch (options.control_master) {
1326 case SSHCTL_MASTER_AUTO:
1327 case SSHCTL_MASTER_AUTO_ASK:
1328 debug("auto-mux: Trying existing master");
1329 /* FALLTHROUGH */
1330 case SSHCTL_MASTER_NO:
1331 break;
1332 default:
1333 return;
1334 }
1335
Damien Miller0e220db2004-06-15 10:34:08 +10001336 memset(&addr, '\0', sizeof(addr));
1337 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001338 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001339 strlen(path) + 1;
1340
1341 if (strlcpy(addr.sun_path, path,
1342 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1343 fatal("ControlPath too long");
1344
1345 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1346 fatal("%s socket(): %s", __func__, strerror(errno));
1347
Damien Miller90967402006-03-26 14:07:26 +11001348 if (connect(sock, (struct sockaddr *)&addr, addr_len) == -1) {
Darren Tucker0814d312005-06-01 23:08:51 +10001349 if (mux_command != SSHMUX_COMMAND_OPEN) {
1350 fatal("Control socket connect(%.100s): %s", path,
1351 strerror(errno));
1352 }
Damien Miller538c9b72005-05-26 12:11:28 +10001353 if (errno == ENOENT)
Damien Miller4662d342006-03-26 13:59:59 +11001354 debug("Control socket \"%.100s\" does not exist", path);
Damien Miller538c9b72005-05-26 12:11:28 +10001355 else {
Damien Miller4662d342006-03-26 13:59:59 +11001356 error("Control socket connect(%.100s): %s", path,
Damien Miller538c9b72005-05-26 12:11:28 +10001357 strerror(errno));
1358 }
Damien Miller4662d342006-03-26 13:59:59 +11001359 close(sock);
1360 return;
1361 }
Damien Miller46d38de2005-07-17 17:02:09 +10001362
Damien Miller4662d342006-03-26 13:59:59 +11001363 if (stdin_null_flag) {
1364 if ((fd = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1365 fatal("open(/dev/null): %s", strerror(errno));
1366 if (dup2(fd, STDIN_FILENO) == -1)
1367 fatal("dup2: %s", strerror(errno));
1368 if (fd > STDERR_FILENO)
1369 close(fd);
1370 }
Damien Miller46d38de2005-07-17 17:02:09 +10001371
Damien Miller13390022005-07-06 09:44:19 +10001372 term = getenv("TERM");
Darren Tucker7ebfc102004-11-07 20:06:19 +11001373
1374 flags = 0;
1375 if (tty_flag)
1376 flags |= SSHMUX_FLAG_TTY;
1377 if (subsystem_flag)
1378 flags |= SSHMUX_FLAG_SUBSYS;
Damien Miller13390022005-07-06 09:44:19 +10001379 if (options.forward_x11)
1380 flags |= SSHMUX_FLAG_X11_FWD;
1381 if (options.forward_agent)
1382 flags |= SSHMUX_FLAG_AGENT_FWD;
Damien Miller0e220db2004-06-15 10:34:08 +10001383
Damien Millerd39a3cf2008-02-10 22:23:18 +11001384 signal(SIGPIPE, SIG_IGN);
1385
Damien Miller0e220db2004-06-15 10:34:08 +10001386 buffer_init(&m);
1387
Darren Tucker7ebfc102004-11-07 20:06:19 +11001388 /* Send our command to server */
1389 buffer_put_int(&m, mux_command);
1390 buffer_put_int(&m, flags);
Damien Miller13390022005-07-06 09:44:19 +10001391 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001392 fatal("%s: msg_send", __func__);
1393 buffer_clear(&m);
1394
1395 /* Get authorisation status and PID of controlee */
Damien Miller0e220db2004-06-15 10:34:08 +10001396 if (ssh_msg_recv(sock, &m) == -1)
1397 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001398 if (buffer_get_char(&m) != SSHMUX_VER)
Damien Miller0e220db2004-06-15 10:34:08 +10001399 fatal("%s: wrong version", __func__);
Damien Miller23f07702004-06-18 01:19:03 +10001400 if (buffer_get_int(&m) != 1)
1401 fatal("Connection to master denied");
Damien Miller0e220db2004-06-15 10:34:08 +10001402 control_server_pid = buffer_get_int(&m);
1403
Damien Miller0e220db2004-06-15 10:34:08 +10001404 buffer_clear(&m);
Damien Miller0e220db2004-06-15 10:34:08 +10001405
Darren Tucker7ebfc102004-11-07 20:06:19 +11001406 switch (mux_command) {
1407 case SSHMUX_COMMAND_ALIVE_CHECK:
Darren Tucker47eede72005-03-14 23:08:12 +11001408 fprintf(stderr, "Master running (pid=%d)\r\n",
Darren Tucker7ebfc102004-11-07 20:06:19 +11001409 control_server_pid);
1410 exit(0);
1411 case SSHMUX_COMMAND_TERMINATE:
1412 fprintf(stderr, "Exit request sent.\r\n");
1413 exit(0);
1414 case SSHMUX_COMMAND_OPEN:
1415 /* continue below */
1416 break;
1417 default:
1418 fatal("silly mux_command %d", mux_command);
1419 }
1420
1421 /* SSHMUX_COMMAND_OPEN */
Damien Miller13390022005-07-06 09:44:19 +10001422 buffer_put_cstring(&m, term ? term : "");
Damien Miller0e220db2004-06-15 10:34:08 +10001423 buffer_append(&command, "\0", 1);
1424 buffer_put_cstring(&m, buffer_ptr(&command));
1425
Darren Tucker365433f2004-06-22 12:29:23 +10001426 if (options.num_send_env == 0 || environ == NULL) {
1427 buffer_put_int(&m, 0);
Darren Tuckerfc959702004-07-17 16:12:08 +10001428 } else {
Darren Tucker365433f2004-06-22 12:29:23 +10001429 /* Pass environment */
1430 num_env = 0;
1431 for (i = 0; environ[i] != NULL; i++)
1432 if (env_permitted(environ[i]))
1433 num_env++; /* Count */
Darren Tuckerfc959702004-07-17 16:12:08 +10001434
Darren Tucker365433f2004-06-22 12:29:23 +10001435 buffer_put_int(&m, num_env);
1436
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001437 for (i = 0; environ[i] != NULL && num_env >= 0; i++)
1438 if (env_permitted(environ[i])) {
1439 num_env--;
Darren Tucker365433f2004-06-22 12:29:23 +10001440 buffer_put_cstring(&m, environ[i]);
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001441 }
Darren Tucker365433f2004-06-22 12:29:23 +10001442 }
Damien Miller3756dce2004-06-18 01:17:29 +10001443
Damien Miller13390022005-07-06 09:44:19 +10001444 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Damien Miller0e220db2004-06-15 10:34:08 +10001445 fatal("%s: msg_send", __func__);
1446
Damien Miller54fd7cf2007-09-17 12:04:08 +10001447 if (mm_send_fd(sock, STDIN_FILENO) == -1 ||
1448 mm_send_fd(sock, STDOUT_FILENO) == -1 ||
1449 mm_send_fd(sock, STDERR_FILENO) == -1)
1450 fatal("%s: send fds failed", __func__);
Damien Miller0e220db2004-06-15 10:34:08 +10001451
1452 /* Wait for reply, so master has a chance to gather ttymodes */
1453 buffer_clear(&m);
1454 if (ssh_msg_recv(sock, &m) == -1)
1455 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001456 if (buffer_get_char(&m) != SSHMUX_VER)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001457 fatal("%s: wrong version", __func__);
Damien Miller0e220db2004-06-15 10:34:08 +10001458 buffer_free(&m);
1459
Darren Tucker07336da2004-11-05 20:02:16 +11001460 signal(SIGHUP, control_client_sighandler);
Damien Miller0809e232004-06-18 22:20:57 +10001461 signal(SIGINT, control_client_sighandler);
1462 signal(SIGTERM, control_client_sighandler);
1463 signal(SIGWINCH, control_client_sigrelay);
1464
Damien Miller0e220db2004-06-15 10:34:08 +10001465 if (tty_flag)
1466 enter_raw_mode();
1467
Darren Tucker132367f2007-06-25 18:59:17 +10001468 /*
1469 * Stick around until the controlee closes the client_fd.
1470 * Before it does, it is expected to write this process' exit
1471 * value (one int). This process must read the value and wait for
1472 * the closure of the client_fd; if this one closes early, the
1473 * multiplex master will terminate early too (possibly losing data).
1474 */
1475 exitval[0] = 0;
Darren Tucker2cbec742007-06-12 23:41:33 +10001476 for (i = 0; !control_client_terminate && i < (int)sizeof(exitval);) {
Darren Tucker132367f2007-06-25 18:59:17 +10001477 r = read(sock, (char *)exitval + i, sizeof(exitval) - i);
Damien Miller0e220db2004-06-15 10:34:08 +10001478 if (r == 0) {
1479 debug2("Received EOF from master");
1480 break;
1481 }
Darren Tuckerd989ada2007-06-25 18:34:43 +10001482 if (r == -1) {
1483 if (errno == EINTR)
1484 continue;
Damien Miller0e220db2004-06-15 10:34:08 +10001485 fatal("%s: read %s", __func__, strerror(errno));
Darren Tuckerd989ada2007-06-25 18:34:43 +10001486 }
Darren Tucker2cbec742007-06-12 23:41:33 +10001487 i += r;
Damien Miller0e220db2004-06-15 10:34:08 +10001488 }
Darren Tucker132367f2007-06-25 18:59:17 +10001489
Damien Miller0e220db2004-06-15 10:34:08 +10001490 close(sock);
Damien Miller0e220db2004-06-15 10:34:08 +10001491 leave_raw_mode();
Darren Tucker132367f2007-06-25 18:59:17 +10001492 if (i > (int)sizeof(int))
1493 fatal("%s: master returned too much data (%d > %lu)",
1494 __func__, i, sizeof(int));
Darren Tucker2cbec742007-06-12 23:41:33 +10001495 if (control_client_terminate) {
1496 debug2("Exiting on signal %d", control_client_terminate);
Darren Tucker132367f2007-06-25 18:59:17 +10001497 exitval[0] = 255;
1498 } else if (i < (int)sizeof(int)) {
Darren Tucker2cbec742007-06-12 23:41:33 +10001499 debug2("Control master terminated unexpectedly");
Darren Tucker132367f2007-06-25 18:59:17 +10001500 exitval[0] = 255;
Darren Tucker2cbec742007-06-12 23:41:33 +10001501 } else
Darren Tucker132367f2007-06-25 18:59:17 +10001502 debug2("Received exit status from master %d", exitval[0]);
Darren Tucker2cbec742007-06-12 23:41:33 +10001503
Damien Miller0e220db2004-06-15 10:34:08 +10001504 if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
Darren Tucker132367f2007-06-25 18:59:17 +10001505 fprintf(stderr, "Shared connection to %s closed.\r\n", host);
Damien Miller0e220db2004-06-15 10:34:08 +10001506
Darren Tucker132367f2007-06-25 18:59:17 +10001507 exit(exitval[0]);
Damien Miller0e220db2004-06-15 10:34:08 +10001508}