blob: 2ed76c9a122ff38c93c781b1335080450670dd81 [file] [log] [blame]
Damien Millerd39a3cf2008-02-10 22:23:18 +11001/* $OpenBSD: ssh.c,v 1.309 2008/01/19 20:51:26 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 Millerd4a8b7e1999-10-27 13:42:43 +100076
Damien Millerd7834352006-08-05 12:39:39 +100077#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000079#include "ssh1.h"
Damien Miller1383bd82000-04-06 12:32:37 +100080#include "ssh2.h"
81#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000082#include "cipher.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000083#include "packet.h"
84#include "buffer.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000085#include "channels.h"
Damien Millereba71ba2000-04-29 23:57:08 +100086#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100087#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100088#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000089#include "pathnames.h"
Damien Miller0e220db2004-06-15 10:34:08 +100090#include "dispatch.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +000091#include "clientloop.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000092#include "log.h"
93#include "readconf.h"
94#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000095#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000096#include "kex.h"
97#include "mac.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +100098#include "sshpty.h"
Darren Tucker46bc0752004-05-02 22:11:30 +100099#include "match.h"
Damien Miller0e220db2004-06-15 10:34:08 +1000100#include "msg.h"
101#include "monitor_fdpass.h"
Darren Tucker25f60a72004-08-15 17:23:34 +1000102#include "uidswap.h"
Damien Millerb7576772006-07-10 20:23:39 +1000103#include "version.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104
Ben Lindstromc5b68002001-07-04 04:52:03 +0000105#ifdef SMARTCARD
Ben Lindstromc5b68002001-07-04 04:52:03 +0000106#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +0000107#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000108
Damien Miller3f905871999-11-15 17:10:57 +1100109extern char *__progname;
Damien Miller3f905871999-11-15 17:10:57 +1100110
Damien Miller95def091999-11-25 00:26:21 +1100111/* Flag indicating whether debug mode is on. This can be set on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112int debug_flag = 0;
113
Damien Miller78928792000-04-12 20:17:38 +1000114/* Flag indicating whether a tty should be allocated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115int tty_flag = 0;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000116int no_tty_flag = 0;
117int force_tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118
Damien Miller1383bd82000-04-06 12:32:37 +1000119/* don't exec a shell */
120int no_shell_flag = 0;
Damien Miller1383bd82000-04-06 12:32:37 +1000121
Damien Miller5428f641999-11-25 11:54:57 +1100122/*
123 * Flag indicating that nothing should be read from stdin. This can be set
124 * on the command line.
125 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126int stdin_null_flag = 0;
127
Damien Miller5428f641999-11-25 11:54:57 +1100128/*
129 * Flag indicating that ssh should fork after authentication. This is useful
Ben Lindstromf666fec2002-06-06 19:51:58 +0000130 * so that the passphrase can be entered manually, and then ssh goes to the
Damien Miller5428f641999-11-25 11:54:57 +1100131 * background.
132 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000133int fork_after_authentication_flag = 0;
134
Damien Miller5428f641999-11-25 11:54:57 +1100135/*
136 * General data structure for command line options and options configurable
137 * in configuration files. See readconf.h.
138 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139Options options;
140
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000141/* optional user configfile */
142char *config = NULL;
143
Damien Miller5428f641999-11-25 11:54:57 +1100144/*
145 * Name of the host we are connecting to. This is the name given on the
146 * command line, or the HostName specified for the user-supplied name in a
147 * configuration file.
148 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000149char *host;
150
151/* socket address the host resolves to */
Damien Miller34132e52000-01-14 15:45:46 +1100152struct sockaddr_storage hostaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000153
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000154/* Private host keys. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000155Sensitive sensitive_data;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000156
157/* Original real UID. */
158uid_t original_real_uid;
Ben Lindstromf9c48842002-06-11 16:37:51 +0000159uid_t original_effective_uid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160
Damien Miller1383bd82000-04-06 12:32:37 +1000161/* command to be executed */
162Buffer command;
163
Damien Miller832562e2001-01-30 09:30:01 +1100164/* Should we execute a command or invoke a subsystem? */
165int subsystem_flag = 0;
166
Damien Miller2797f7f2002-04-23 21:09:44 +1000167/* # of replies received for global requests */
168static int client_global_request_id = 0;
169
Damien Miller8c4e18a2002-09-19 12:05:02 +1000170/* pid of proxycommand child process */
171pid_t proxy_command_pid = 0;
172
Damien Miller0e220db2004-06-15 10:34:08 +1000173/* fd to control socket */
174int control_fd = -1;
175
Darren Tucker7ebfc102004-11-07 20:06:19 +1100176/* Multiplexing control command */
Darren Tucker0814d312005-06-01 23:08:51 +1000177static u_int mux_command = 0;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100178
Damien Miller0e220db2004-06-15 10:34:08 +1000179/* Only used in control client mode */
180volatile sig_atomic_t control_client_terminate = 0;
181u_int control_server_pid = 0;
182
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000183/* Prints a help message to the user. This function never returns. */
184
Ben Lindstrombba81212001-06-25 05:01:22 +0000185static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000186usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000187{
Damien Miller50955102004-03-22 09:34:58 +1100188 fprintf(stderr,
Darren Tucker415bddc2007-06-12 23:43:16 +1000189"usage: ssh [-1246AaCfgKkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n"
Darren Tucker895d6982005-10-03 18:18:05 +1000190" [-D [bind_address:]port] [-e escape_char] [-F configfile]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100191" [-i identity_file] [-L [bind_address:]port:host:hostport]\n"
Damien Millerf91ee4c2005-03-01 21:24:33 +1100192" [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100193" [-R [bind_address:]port:host:hostport] [-S ctl_path]\n"
Damien Miller991dba42006-07-10 20:16:27 +1000194" [-w local_tun[:remote_tun]] [user@]hostname [command]\n"
Damien Miller50955102004-03-22 09:34:58 +1100195 );
Darren Tuckere9a9b712005-12-20 16:15:51 +1100196 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000197}
198
Ben Lindstrombba81212001-06-25 05:01:22 +0000199static int ssh_session(void);
200static int ssh_session2(void);
201static void load_public_identity_files(void);
Damien Miller0e220db2004-06-15 10:34:08 +1000202static void control_client(const char *path);
Damien Miller1383bd82000-04-06 12:32:37 +1000203
Damien Miller95def091999-11-25 00:26:21 +1100204/*
205 * Main program for the ssh client.
206 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000207int
208main(int ac, char **av)
209{
Ben Lindstrom24157572002-06-12 16:09:39 +0000210 int i, opt, exit_status;
Damien Miller9836cf82003-12-17 16:30:06 +1100211 char *p, *cp, *line, buf[256];
Damien Miller95def091999-11-25 00:26:21 +1100212 struct stat st;
Ben Lindstrom086cf212001-03-05 05:56:40 +0000213 struct passwd *pw;
Damien Miller67bd0622007-09-17 12:06:57 +1000214 int dummy, timeout_ms;
Ben Lindstrom5ccf63a2001-09-24 20:00:10 +0000215 extern int optind, optreset;
Damien Miller4f8e6692001-07-14 13:22:53 +1000216 extern char *optarg;
Damien Miller9651fe62005-06-26 08:55:25 +1000217 struct servent *sp;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100218 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000219
Darren Tuckerce321d82005-10-03 18:11:24 +1000220 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
221 sanitise_stdfd();
222
Damien Miller59d3d5b2003-08-22 09:34:41 +1000223 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000224 init_rng();
225
Damien Miller5428f641999-11-25 11:54:57 +1100226 /*
227 * Save the original real uid. It will be needed later (uid-swapping
228 * may clobber the real uid).
229 */
Damien Miller95def091999-11-25 00:26:21 +1100230 original_real_uid = getuid();
231 original_effective_uid = geteuid();
Damien Millera8e06ce2003-11-21 23:48:55 +1100232
Damien Miller50b9a602002-09-04 16:50:06 +1000233 /*
234 * Use uid-swapping to give up root privileges for the duration of
235 * option processing. We will re-instantiate the rights when we are
236 * ready to create the privileged port, and will permanently drop
237 * them when the port has been created (actually, when the connection
238 * has been made, as we may need to create the port several times).
239 */
240 PRIV_END;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000241
Damien Miller05dd7952000-10-01 00:42:48 +1100242#ifdef HAVE_SETRLIMIT
Damien Miller95def091999-11-25 00:26:21 +1100243 /* If we are installed setuid root be careful to not drop core. */
244 if (original_real_uid != original_effective_uid) {
245 struct rlimit rlim;
246 rlim.rlim_cur = rlim.rlim_max = 0;
247 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
248 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000249 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100250#endif
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000251 /* Get user data. */
252 pw = getpwuid(original_real_uid);
253 if (!pw) {
Damien Miller996acd22003-04-09 20:59:48 +1000254 logit("You don't exist, go away!");
Darren Tuckere9a9b712005-12-20 16:15:51 +1100255 exit(255);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000256 }
257 /* Take a copy of the returned structure. */
258 pw = pwcopy(pw);
259
Damien Miller5428f641999-11-25 11:54:57 +1100260 /*
Damien Miller5428f641999-11-25 11:54:57 +1100261 * Set our umask to something reasonable, as some files are created
262 * with the default umask. This will make them world-readable but
263 * writable only by the owner, which is ok for all files for which we
264 * don't set the modes explicitly.
265 */
Damien Miller95def091999-11-25 00:26:21 +1100266 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000267
Damien Miller95def091999-11-25 00:26:21 +1100268 /* Initialize option structure to indicate that no values have been set. */
269 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270
Damien Miller95def091999-11-25 00:26:21 +1100271 /* Parse command-line arguments. */
272 host = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000273
Damien Miller2ecb6bd2006-03-15 12:03:53 +1100274 again:
Damien Millerf4614452001-07-14 12:18:10 +1000275 while ((opt = getopt(ac, av,
Darren Tucker415bddc2007-06-12 23:43:16 +1000276 "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 +1100277 switch (opt) {
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000278 case '1':
279 options.protocol = SSH_PROTO_1;
280 break;
Damien Miller4af51302000-04-16 11:18:38 +1000281 case '2':
282 options.protocol = SSH_PROTO_2;
283 break;
Damien Miller34132e52000-01-14 15:45:46 +1100284 case '4':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000285 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100286 break;
Damien Miller34132e52000-01-14 15:45:46 +1100287 case '6':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000288 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100289 break;
Damien Miller95def091999-11-25 00:26:21 +1100290 case 'n':
291 stdin_null_flag = 1;
292 break;
Damien Miller95def091999-11-25 00:26:21 +1100293 case 'f':
294 fork_after_authentication_flag = 1;
295 stdin_null_flag = 1;
296 break;
Damien Miller95def091999-11-25 00:26:21 +1100297 case 'x':
298 options.forward_x11 = 0;
299 break;
Damien Miller95def091999-11-25 00:26:21 +1100300 case 'X':
301 options.forward_x11 = 1;
302 break;
Darren Tucker0a118da2003-10-15 15:54:32 +1000303 case 'Y':
304 options.forward_x11 = 1;
305 options.forward_x11_trusted = 1;
306 break;
Damien Miller95def091999-11-25 00:26:21 +1100307 case 'g':
308 options.gateway_ports = 1;
309 break;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100310 case 'O':
311 if (strcmp(optarg, "check") == 0)
312 mux_command = SSHMUX_COMMAND_ALIVE_CHECK;
313 else if (strcmp(optarg, "exit") == 0)
314 mux_command = SSHMUX_COMMAND_TERMINATE;
315 else
316 fatal("Invalid multiplex command.");
317 break;
Damien Miller147bba32002-09-04 16:46:06 +1000318 case 'P': /* deprecated */
Damien Miller95def091999-11-25 00:26:21 +1100319 options.use_privileged_port = 0;
320 break;
Damien Miller95def091999-11-25 00:26:21 +1100321 case 'a':
322 options.forward_agent = 0;
323 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000324 case 'A':
325 options.forward_agent = 1;
326 break;
Damien Miller95def091999-11-25 00:26:21 +1100327 case 'k':
Damien Millere0113cc2003-11-24 13:10:09 +1100328 options.gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100329 break;
Darren Tucker415bddc2007-06-12 23:43:16 +1000330 case 'K':
331 options.gss_authentication = 1;
332 options.gss_deleg_creds = 1;
333 break;
Damien Miller95def091999-11-25 00:26:21 +1100334 case 'i':
335 if (stat(optarg, &st) < 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000336 fprintf(stderr, "Warning: Identity file %s "
Damien Miller3eb48b62005-03-01 21:15:46 +1100337 "not accessible: %s.\n", optarg,
338 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100339 break;
340 }
Damien Millerf4614452001-07-14 12:18:10 +1000341 if (options.num_identity_files >=
342 SSH_MAX_IDENTITY_FILES)
343 fatal("Too many identity files specified "
344 "(max %d)", SSH_MAX_IDENTITY_FILES);
345 options.identity_files[options.num_identity_files++] =
346 xstrdup(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100347 break;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000348 case 'I':
349#ifdef SMARTCARD
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000350 options.smartcard_device = xstrdup(optarg);
Ben Lindstrombcc18082001-08-06 21:59:25 +0000351#else
Ben Lindstromc5b68002001-07-04 04:52:03 +0000352 fprintf(stderr, "no support for smartcards.\n");
Ben Lindstrombcc18082001-08-06 21:59:25 +0000353#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000354 break;
Damien Miller95def091999-11-25 00:26:21 +1100355 case 't':
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000356 if (tty_flag)
357 force_tty_flag = 1;
Damien Miller95def091999-11-25 00:26:21 +1100358 tty_flag = 1;
359 break;
Damien Miller95def091999-11-25 00:26:21 +1100360 case 'v':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000361 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100362 debug_flag = 1;
363 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000364 } else {
365 if (options.log_level < SYSLOG_LEVEL_DEBUG3)
366 options.log_level++;
Damien Millere4340be2000-09-16 13:29:08 +1100367 break;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000368 }
Darren Tuckere9bf9842004-11-05 20:05:32 +1100369 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100370 case 'V':
Damien Miller0c889cd2004-03-22 09:36:00 +1100371 fprintf(stderr, "%s, %s\n",
Damien Miller2aa6d3c2004-09-12 16:53:04 +1000372 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
Damien Miller95def091999-11-25 00:26:21 +1100373 if (opt == 'V')
374 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100375 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100376 case 'w':
Damien Miller7b58e802005-12-13 19:33:19 +1100377 if (options.tun_open == -1)
378 options.tun_open = SSH_TUNMODE_DEFAULT;
Damien Millerd27b9472005-12-13 19:29:02 +1100379 options.tun_local = a2tun(optarg, &options.tun_remote);
Damien Miller7b58e802005-12-13 19:33:19 +1100380 if (options.tun_local == SSH_TUNID_ERR) {
Damien Millerd27b9472005-12-13 19:29:02 +1100381 fprintf(stderr, "Bad tun device '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100382 exit(255);
Damien Millerd27b9472005-12-13 19:29:02 +1100383 }
384 break;
Damien Miller95def091999-11-25 00:26:21 +1100385 case 'q':
386 options.log_level = SYSLOG_LEVEL_QUIET;
387 break;
Damien Miller95def091999-11-25 00:26:21 +1100388 case 'e':
389 if (optarg[0] == '^' && optarg[2] == 0 &&
Damien Millerf4614452001-07-14 12:18:10 +1000390 (u_char) optarg[1] >= 64 &&
391 (u_char) optarg[1] < 128)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000392 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100393 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000394 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100395 else if (strcmp(optarg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000396 options.escape_char = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100397 else {
Damien Millerf4614452001-07-14 12:18:10 +1000398 fprintf(stderr, "Bad escape character '%s'.\n",
399 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100400 exit(255);
Damien Miller95def091999-11-25 00:26:21 +1100401 }
402 break;
Damien Miller95def091999-11-25 00:26:21 +1100403 case 'c':
Damien Millereba71ba2000-04-29 23:57:08 +1000404 if (ciphers_valid(optarg)) {
405 /* SSH2 only */
406 options.ciphers = xstrdup(optarg);
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000407 options.cipher = SSH_CIPHER_INVALID;
Damien Millereba71ba2000-04-29 23:57:08 +1000408 } else {
409 /* SSH1 only */
Damien Millere39cacc2000-11-29 12:18:44 +1100410 options.cipher = cipher_number(optarg);
411 if (options.cipher == -1) {
Damien Millerf4614452001-07-14 12:18:10 +1000412 fprintf(stderr,
413 "Unknown cipher type '%s'\n",
414 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100415 exit(255);
Damien Millereba71ba2000-04-29 23:57:08 +1000416 }
Damien Millerf4614452001-07-14 12:18:10 +1000417 if (options.cipher == SSH_CIPHER_3DES)
Damien Millere39cacc2000-11-29 12:18:44 +1100418 options.ciphers = "3des-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000419 else if (options.cipher == SSH_CIPHER_BLOWFISH)
Damien Millere39cacc2000-11-29 12:18:44 +1100420 options.ciphers = "blowfish-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000421 else
Damien Millere39cacc2000-11-29 12:18:44 +1100422 options.ciphers = (char *)-1;
Damien Miller95def091999-11-25 00:26:21 +1100423 }
424 break;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000425 case 'm':
426 if (mac_valid(optarg))
427 options.macs = xstrdup(optarg);
428 else {
Damien Millerf4614452001-07-14 12:18:10 +1000429 fprintf(stderr, "Unknown mac type '%s'\n",
430 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100431 exit(255);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000432 }
433 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000434 case 'M':
Damien Millerd14b1e72005-06-16 13:19:41 +1000435 if (options.control_master == SSHCTL_MASTER_YES)
436 options.control_master = SSHCTL_MASTER_ASK;
437 else
438 options.control_master = SSHCTL_MASTER_YES;
Damien Miller0e220db2004-06-15 10:34:08 +1000439 break;
Damien Miller95def091999-11-25 00:26:21 +1100440 case 'p':
Ben Lindstrom19066a12001-04-12 23:39:26 +0000441 options.port = a2port(optarg);
442 if (options.port == 0) {
Ben Lindstrom146edb92001-04-11 23:06:28 +0000443 fprintf(stderr, "Bad port '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100444 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000445 }
Damien Miller95def091999-11-25 00:26:21 +1100446 break;
Damien Miller95def091999-11-25 00:26:21 +1100447 case 'l':
448 options.user = optarg;
449 break;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000450
Damien Miller95def091999-11-25 00:26:21 +1100451 case 'L':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100452 if (parse_forward(&fwd, optarg))
453 add_local_forward(&options, &fwd);
454 else {
Damien Millerf4614452001-07-14 12:18:10 +1000455 fprintf(stderr,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100456 "Bad local forwarding specification '%s'\n",
Damien Millerf4614452001-07-14 12:18:10 +1000457 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100458 exit(255);
Ben Lindstrom1a174712001-09-12 17:56:15 +0000459 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100460 break;
461
462 case 'R':
463 if (parse_forward(&fwd, optarg)) {
464 add_remote_forward(&options, &fwd);
465 } else {
466 fprintf(stderr,
467 "Bad remote forwarding specification "
468 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100469 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100470 }
Damien Miller95def091999-11-25 00:26:21 +1100471 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000472
473 case 'D':
Damien Millerf91ee4c2005-03-01 21:24:33 +1100474 cp = p = xstrdup(optarg);
475 memset(&fwd, '\0', sizeof(fwd));
476 fwd.connect_host = "socks";
477 if ((fwd.listen_host = hpdelim(&cp)) == NULL) {
478 fprintf(stderr, "Bad dynamic forwarding "
479 "specification '%.100s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100480 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100481 }
482 if (cp != NULL) {
483 fwd.listen_port = a2port(cp);
484 fwd.listen_host = cleanhostname(fwd.listen_host);
485 } else {
486 fwd.listen_port = a2port(fwd.listen_host);
Damien Millerbe1045d2005-08-12 22:10:56 +1000487 fwd.listen_host = NULL;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100488 }
489
490 if (fwd.listen_port == 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000491 fprintf(stderr, "Bad dynamic port '%s'\n",
492 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100493 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000494 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100495 add_local_forward(&options, &fwd);
496 xfree(p);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000497 break;
498
Damien Miller95def091999-11-25 00:26:21 +1100499 case 'C':
500 options.compression = 1;
501 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000502 case 'N':
503 no_shell_flag = 1;
504 no_tty_flag = 1;
505 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000506 case 'T':
507 no_tty_flag = 1;
508 break;
Damien Miller95def091999-11-25 00:26:21 +1100509 case 'o':
510 dummy = 1;
Damien Miller9836cf82003-12-17 16:30:06 +1100511 line = xstrdup(optarg);
Damien Millerf4614452001-07-14 12:18:10 +1000512 if (process_config_line(&options, host ? host : "",
Damien Miller9836cf82003-12-17 16:30:06 +1100513 line, "command-line", 0, &dummy) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100514 exit(255);
Damien Miller9836cf82003-12-17 16:30:06 +1100515 xfree(line);
Damien Miller95def091999-11-25 00:26:21 +1100516 break;
Damien Miller832562e2001-01-30 09:30:01 +1100517 case 's':
518 subsystem_flag = 1;
519 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000520 case 'S':
521 if (options.control_path != NULL)
522 free(options.control_path);
523 options.control_path = xstrdup(optarg);
Damien Miller0e220db2004-06-15 10:34:08 +1000524 break;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000525 case 'b':
526 options.bind_address = optarg;
527 break;
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000528 case 'F':
529 config = optarg;
530 break;
Damien Miller95def091999-11-25 00:26:21 +1100531 default:
532 usage();
533 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000534 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000535
Damien Millerf4614452001-07-14 12:18:10 +1000536 ac -= optind;
537 av += optind;
538
539 if (ac > 0 && !host && **av != '-') {
Ben Lindstromc276c122002-12-23 02:14:51 +0000540 if (strrchr(*av, '@')) {
Damien Millerf4614452001-07-14 12:18:10 +1000541 p = xstrdup(*av);
Ben Lindstromc276c122002-12-23 02:14:51 +0000542 cp = strrchr(p, '@');
Damien Millerf4614452001-07-14 12:18:10 +1000543 if (cp == NULL || cp == p)
544 usage();
545 options.user = p;
546 *cp = '\0';
547 host = ++cp;
548 } else
549 host = *av;
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000550 if (ac > 1) {
551 optind = optreset = 1;
Damien Millerf4614452001-07-14 12:18:10 +1000552 goto again;
553 }
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000554 ac--, av++;
Damien Millerf4614452001-07-14 12:18:10 +1000555 }
556
Damien Miller95def091999-11-25 00:26:21 +1100557 /* Check that we got a host name. */
558 if (!host)
559 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000560
Damien Millercb5e44a2000-09-29 12:12:36 +1100561 SSLeay_add_all_algorithms();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100562 ERR_load_crypto_strings();
Damien Millercb5e44a2000-09-29 12:12:36 +1100563
Damien Miller95def091999-11-25 00:26:21 +1100564 /* Initialize the command to execute on remote host. */
565 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000566
Damien Miller5428f641999-11-25 11:54:57 +1100567 /*
568 * Save the command to execute on the remote host in a buffer. There
569 * is no limit on the length of the command, except by the maximum
570 * packet size. Also sets the tty flag if there is no command.
571 */
Damien Millerf4614452001-07-14 12:18:10 +1000572 if (!ac) {
Damien Miller95def091999-11-25 00:26:21 +1100573 /* No command specified - execute shell on a tty. */
574 tty_flag = 1;
Damien Miller832562e2001-01-30 09:30:01 +1100575 if (subsystem_flag) {
Damien Millerf4614452001-07-14 12:18:10 +1000576 fprintf(stderr,
577 "You must specify a subsystem to invoke.\n");
Damien Miller832562e2001-01-30 09:30:01 +1100578 usage();
579 }
Damien Miller95def091999-11-25 00:26:21 +1100580 } else {
Damien Millerf4614452001-07-14 12:18:10 +1000581 /* A command has been specified. Store it into the buffer. */
582 for (i = 0; i < ac; i++) {
583 if (i)
Damien Miller95def091999-11-25 00:26:21 +1100584 buffer_append(&command, " ", 1);
585 buffer_append(&command, av[i], strlen(av[i]));
586 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000587 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000588
Damien Miller95def091999-11-25 00:26:21 +1100589 /* Cannot fork to background if no command. */
Damien Millercaf6dd62000-08-29 11:33:50 +1100590 if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
Damien Miller95def091999-11-25 00:26:21 +1100591 fatal("Cannot fork into background without a command to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000592
Damien Miller95def091999-11-25 00:26:21 +1100593 /* Allocate a tty by default if no command specified. */
594 if (buffer_len(&command) == 0)
595 tty_flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000596
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000597 /* Force no tty */
Ben Lindstromc72745a2000-12-02 19:03:54 +0000598 if (no_tty_flag)
599 tty_flag = 0;
Damien Miller95def091999-11-25 00:26:21 +1100600 /* Do not allocate a tty if stdin is not a tty. */
Damien Millerddee5752005-05-26 12:05:05 +1000601 if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100602 if (tty_flag)
Damien Miller996acd22003-04-09 20:59:48 +1000603 logit("Pseudo-terminal will not be allocated because stdin is not a terminal.");
Damien Miller95def091999-11-25 00:26:21 +1100604 tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000605 }
Damien Miller1383bd82000-04-06 12:32:37 +1000606
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000607 /*
608 * Initialize "log" output. Since we are the client all output
609 * actually goes to stderr.
610 */
Ben Lindstrom2b646522001-04-12 16:16:57 +0000611 log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
612 SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000613
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000614 /*
615 * Read per-user configuration file. Ignore the system wide config
616 * file if the user specifies a config file on the command line.
617 */
618 if (config != NULL) {
Damien Miller914420f2004-04-20 20:14:07 +1000619 if (!read_config_file(config, host, &options, 0))
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000620 fatal("Can't open user config file %.100s: "
621 "%.100s", config, strerror(errno));
Damien Miller80163902007-01-05 16:30:16 +1100622 } else {
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000623 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
624 _PATH_SSH_USER_CONFFILE);
Damien Miller57a44762004-04-20 20:11:57 +1000625 (void)read_config_file(buf, host, &options, 1);
Ben Lindstrom83f07d12001-10-03 17:22:29 +0000626
627 /* Read systemwide configuration file after use config. */
Darren Tuckerfc959702004-07-17 16:12:08 +1000628 (void)read_config_file(_PATH_HOST_CONFIG_FILE, host,
Damien Miller57a44762004-04-20 20:11:57 +1000629 &options, 0);
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000630 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000631
Damien Miller95def091999-11-25 00:26:21 +1100632 /* Fill configuration defaults. */
633 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000634
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000635 channel_set_af(options.address_family);
636
Damien Miller95def091999-11-25 00:26:21 +1100637 /* reinit */
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000638 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000639
Damien Miller60bc5172001-03-19 09:38:15 +1100640 seed_rng();
641
Damien Miller95def091999-11-25 00:26:21 +1100642 if (options.user == NULL)
643 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000644
Damien Miller95def091999-11-25 00:26:21 +1100645 if (options.hostname != NULL)
646 host = options.hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000647
Darren Tucker3f521e22003-07-03 16:20:42 +1000648 /* force lowercase for hostkey matching */
649 if (options.host_key_alias != NULL) {
650 for (p = options.host_key_alias; *p; p++)
651 if (isupper(*p))
Damien Miller1d2b6702006-03-26 14:09:54 +1100652 *p = (char)tolower(*p);
Darren Tucker3f521e22003-07-03 16:20:42 +1000653 }
654
Damien Miller7c71cc72005-06-26 08:56:31 +1000655 /* Get default port if port has not been set. */
656 if (options.port == 0) {
657 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
658 options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
659 }
660
Damien Miller9f1e33a2003-02-24 11:57:32 +1100661 if (options.proxy_command != NULL &&
Darren Tucker32e42c72007-12-02 23:01:03 +1100662 strcmp(options.proxy_command, "none") == 0) {
663 xfree(options.proxy_command);
Damien Miller9f1e33a2003-02-24 11:57:32 +1100664 options.proxy_command = NULL;
Darren Tucker32e42c72007-12-02 23:01:03 +1100665 }
Damien Miller8f74c8f2005-06-26 08:56:03 +1000666 if (options.control_path != NULL &&
Darren Tucker32e42c72007-12-02 23:01:03 +1100667 strcmp(options.control_path, "none") == 0) {
668 xfree(options.control_path);
Damien Miller8f74c8f2005-06-26 08:56:03 +1000669 options.control_path = NULL;
Darren Tucker32e42c72007-12-02 23:01:03 +1100670 }
Damien Miller9f1e33a2003-02-24 11:57:32 +1100671
Damien Miller0e220db2004-06-15 10:34:08 +1000672 if (options.control_path != NULL) {
Damien Miller6b1d53c2006-03-31 23:13:21 +1100673 char thishost[NI_MAXHOST];
Damien Miller3ec54c72006-03-15 11:30:13 +1100674
Damien Miller6b1d53c2006-03-31 23:13:21 +1100675 if (gethostname(thishost, sizeof(thishost)) == -1)
Damien Miller3ec54c72006-03-15 11:30:13 +1100676 fatal("gethostname: %s", strerror(errno));
Damien Miller6476cad2005-06-16 13:18:34 +1000677 snprintf(buf, sizeof(buf), "%d", options.port);
678 cp = tilde_expand_filename(options.control_path,
679 original_real_uid);
Darren Tucker32e42c72007-12-02 23:01:03 +1100680 xfree(options.control_path);
Damien Miller6476cad2005-06-16 13:18:34 +1000681 options.control_path = percent_expand(cp, "p", buf, "h", host,
Damien Miller6b1d53c2006-03-31 23:13:21 +1100682 "r", options.user, "l", thishost, (char *)NULL);
Damien Miller6476cad2005-06-16 13:18:34 +1000683 xfree(cp);
Damien Miller0e220db2004-06-15 10:34:08 +1000684 }
Darren Tucker0814d312005-06-01 23:08:51 +1000685 if (mux_command != 0 && options.control_path == NULL)
686 fatal("No ControlPath specified for \"-O\" command");
Damien Millerd14b1e72005-06-16 13:19:41 +1000687 if (options.control_path != NULL)
Damien Millerdadfd4d2005-05-26 12:07:13 +1000688 control_client(options.control_path);
Damien Miller0e220db2004-06-15 10:34:08 +1000689
Damien Miller67bd0622007-09-17 12:06:57 +1000690 timeout_ms = options.connection_timeout * 1000;
691
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000692 /* Open a connection to the remote host. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000693 if (ssh_connect(host, &hostaddr, options.port,
Damien Miller67bd0622007-09-17 12:06:57 +1000694 options.address_family, options.connection_attempts, &timeout_ms,
695 options.tcp_keep_alive,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000696#ifdef HAVE_CYGWIN
697 options.use_privileged_port,
698#else
Ben Lindstromf9c48842002-06-11 16:37:51 +0000699 original_effective_uid == 0 && options.use_privileged_port,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000700#endif
Ben Lindstromda394ca2002-06-12 16:11:12 +0000701 options.proxy_command) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100702 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000703
Damien Miller67bd0622007-09-17 12:06:57 +1000704 if (timeout_ms > 0)
705 debug3("timeout: %d ms remain after connect", timeout_ms);
706
Damien Miller5428f641999-11-25 11:54:57 +1100707 /*
708 * If we successfully made the connection, load the host private key
709 * in case we will need it later for combined rsa-rhosts
710 * authentication. This must be done before releasing extra
711 * privileges, because the file is only readable by root.
Ben Lindstrom9e5bb572002-06-06 19:58:27 +0000712 * If we cannot access the private keys, load the public keys
713 * instead and try to execute the ssh-keysign helper instead.
Damien Miller5428f641999-11-25 11:54:57 +1100714 */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000715 sensitive_data.nkeys = 0;
716 sensitive_data.keys = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000717 sensitive_data.external_keysign = 0;
Ben Lindstrom24157572002-06-12 16:09:39 +0000718 if (options.rhosts_rsa_authentication ||
719 options.hostbased_authentication) {
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000720 sensitive_data.nkeys = 3;
Damien Millerddd63ab2006-03-31 23:10:51 +1100721 sensitive_data.keys = xcalloc(sensitive_data.nkeys,
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000722 sizeof(Key));
Ben Lindstromf9c48842002-06-11 16:37:51 +0000723
724 PRIV_START;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000725 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
Darren Tucker232b76f2006-05-06 17:41:51 +1000726 _PATH_HOST_KEY_FILE, "", NULL, NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000727 sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
Darren Tucker232b76f2006-05-06 17:41:51 +1000728 _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000729 sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
Darren Tucker232b76f2006-05-06 17:41:51 +1000730 _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL);
Ben Lindstromf9c48842002-06-11 16:37:51 +0000731 PRIV_END;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000732
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +0000733 if (options.hostbased_authentication == 1 &&
734 sensitive_data.keys[0] == NULL &&
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000735 sensitive_data.keys[1] == NULL &&
736 sensitive_data.keys[2] == NULL) {
737 sensitive_data.keys[1] = key_load_public(
738 _PATH_HOST_DSA_KEY_FILE, NULL);
739 sensitive_data.keys[2] = key_load_public(
740 _PATH_HOST_RSA_KEY_FILE, NULL);
741 sensitive_data.external_keysign = 1;
742 }
Damien Miller95def091999-11-25 00:26:21 +1100743 }
Damien Miller5428f641999-11-25 11:54:57 +1100744 /*
745 * Get rid of any extra privileges that we may have. We will no
746 * longer need them. Also, extra privileges could make it very hard
747 * to read identity files and other non-world-readable files from the
748 * user's home directory if it happens to be on a NFS volume where
749 * root is mapped to nobody.
750 */
Darren Tucker25f60a72004-08-15 17:23:34 +1000751 if (original_effective_uid == 0) {
752 PRIV_START;
753 permanently_set_uid(pw);
754 }
Damien Miller95def091999-11-25 00:26:21 +1100755
Damien Miller5428f641999-11-25 11:54:57 +1100756 /*
757 * Now that we are back to our own permissions, create ~/.ssh
Damien Miller788f2122005-11-05 15:14:59 +1100758 * directory if it doesn't already exist.
Damien Miller5428f641999-11-25 11:54:57 +1100759 */
Ben Lindstrom930b14a2001-08-15 23:19:21 +0000760 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 +1100761 if (stat(buf, &st) < 0)
Damien Millerbe484b52000-07-15 14:14:16 +1000762 if (mkdir(buf, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100763 error("Could not create directory '%.200s'.", buf);
764
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000765 /* load options.identity_files */
766 load_public_identity_files();
767
768 /* Expand ~ in known host file names. */
769 /* XXX mem-leaks: */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100770 options.system_hostfile =
771 tilde_expand_filename(options.system_hostfile, original_real_uid);
772 options.user_hostfile =
773 tilde_expand_filename(options.user_hostfile, original_real_uid);
774 options.system_hostfile2 =
775 tilde_expand_filename(options.system_hostfile2, original_real_uid);
776 options.user_hostfile2 =
777 tilde_expand_filename(options.user_hostfile2, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100778
Damien Miller07cd5892001-11-12 10:52:03 +1100779 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
780
Damien Miller95def091999-11-25 00:26:21 +1100781 /* Log into the remote system. This never returns if the login fails. */
Damien Miller67bd0622007-09-17 12:06:57 +1000782 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr,
783 pw, timeout_ms);
Damien Miller95def091999-11-25 00:26:21 +1100784
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000785 /* We no longer need the private host keys. Clear them now. */
786 if (sensitive_data.nkeys != 0) {
787 for (i = 0; i < sensitive_data.nkeys; i++) {
788 if (sensitive_data.keys[i] != NULL) {
789 /* Destroys contents safely */
790 debug3("clear hostkey %d", i);
791 key_free(sensitive_data.keys[i]);
792 sensitive_data.keys[i] = NULL;
793 }
794 }
795 xfree(sensitive_data.keys);
796 }
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +0000797 for (i = 0; i < options.num_identity_files; i++) {
798 if (options.identity_files[i]) {
799 xfree(options.identity_files[i]);
800 options.identity_files[i] = NULL;
801 }
802 if (options.identity_keys[i]) {
803 key_free(options.identity_keys[i]);
804 options.identity_keys[i] = NULL;
805 }
806 }
Damien Miller95def091999-11-25 00:26:21 +1100807
Damien Miller1383bd82000-04-06 12:32:37 +1000808 exit_status = compat20 ? ssh_session2() : ssh_session();
809 packet_close();
Damien Miller8c4e18a2002-09-19 12:05:02 +1000810
Damien Miller0e220db2004-06-15 10:34:08 +1000811 if (options.control_path != NULL && control_fd != -1)
812 unlink(options.control_path);
813
Damien Miller8c4e18a2002-09-19 12:05:02 +1000814 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100815 * Send SIGHUP to proxy command if used. We don't wait() in
Damien Miller8c4e18a2002-09-19 12:05:02 +1000816 * case it hangs and instead rely on init to reap the child
817 */
818 if (proxy_command_pid > 1)
819 kill(proxy_command_pid, SIGHUP);
820
Damien Miller1383bd82000-04-06 12:32:37 +1000821 return exit_status;
822}
823
Ben Lindstrombba81212001-06-25 05:01:22 +0000824static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100825ssh_init_forwarding(void)
826{
Kevin Steves12057502001-02-05 14:54:34 +0000827 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100828 int i;
Kevin Steves12057502001-02-05 14:54:34 +0000829
Damien Miller0bc1bd82000-11-13 22:57:25 +1100830 /* Initiate local TCP/IP port forwardings. */
831 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100832 debug("Local connections to %.200s:%d forwarded to remote "
833 "address %.200s:%d",
Darren Tucker47eede72005-03-14 23:08:12 +1100834 (options.local_forwards[i].listen_host == NULL) ?
835 (options.gateway_ports ? "*" : "LOCALHOST") :
Damien Millerf91ee4c2005-03-01 21:24:33 +1100836 options.local_forwards[i].listen_host,
837 options.local_forwards[i].listen_port,
838 options.local_forwards[i].connect_host,
839 options.local_forwards[i].connect_port);
Damien Millerb16461c2002-01-22 23:29:22 +1100840 success += channel_setup_local_fwd_listener(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100841 options.local_forwards[i].listen_host,
842 options.local_forwards[i].listen_port,
843 options.local_forwards[i].connect_host,
844 options.local_forwards[i].connect_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100845 options.gateway_ports);
846 }
Darren Tuckere7d4b192006-07-12 22:17:10 +1000847 if (i > 0 && success != i && options.exit_on_forward_failure)
848 fatal("Could not request local forwarding.");
Kevin Steves12057502001-02-05 14:54:34 +0000849 if (i > 0 && success == 0)
850 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100851
852 /* Initiate remote TCP/IP port forwardings. */
853 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100854 debug("Remote connections from %.200s:%d forwarded to "
855 "local address %.200s:%d",
Damien Miller46d38de2005-07-17 17:02:09 +1000856 (options.remote_forwards[i].listen_host == NULL) ?
Damien Milleraa3bb102005-11-05 15:12:59 +1100857 "LOCALHOST" : options.remote_forwards[i].listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100858 options.remote_forwards[i].listen_port,
859 options.remote_forwards[i].connect_host,
860 options.remote_forwards[i].connect_port);
Darren Tuckere7d4b192006-07-12 22:17:10 +1000861 if (channel_request_remote_forwarding(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100862 options.remote_forwards[i].listen_host,
863 options.remote_forwards[i].listen_port,
864 options.remote_forwards[i].connect_host,
Darren Tuckere7d4b192006-07-12 22:17:10 +1000865 options.remote_forwards[i].connect_port) < 0) {
866 if (options.exit_on_forward_failure)
867 fatal("Could not request remote forwarding.");
868 else
869 logit("Warning: Could not request remote "
870 "forwarding.");
871 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100872 }
Damien Millerb3ce9fe2007-08-08 14:32:41 +1000873
874 /* Initiate tunnel forwarding. */
875 if (options.tun_open != SSH_TUNMODE_NO) {
876 if (client_request_tun_fwd(options.tun_open,
877 options.tun_local, options.tun_remote) == -1) {
878 if (options.exit_on_forward_failure)
879 fatal("Could not request tunnel forwarding.");
880 else
881 error("Could not request tunnel forwarding.");
882 }
883 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100884}
885
Ben Lindstrombba81212001-06-25 05:01:22 +0000886static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100887check_agent_present(void)
888{
889 if (options.forward_agent) {
Damien Miller788f2122005-11-05 15:14:59 +1100890 /* Clear agent forwarding if we don't have an agent. */
Damien Miller789e95d2002-09-12 09:52:46 +1000891 if (!ssh_agent_present())
Damien Miller0bc1bd82000-11-13 22:57:25 +1100892 options.forward_agent = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100893 }
894}
895
Ben Lindstrombba81212001-06-25 05:01:22 +0000896static int
Damien Miller1383bd82000-04-06 12:32:37 +1000897ssh_session(void)
898{
899 int type;
Damien Miller1383bd82000-04-06 12:32:37 +1000900 int interactive = 0;
901 int have_tty = 0;
902 struct winsize ws;
Damien Miller1383bd82000-04-06 12:32:37 +1000903 char *cp;
Damien Miller17e7ed02005-06-17 12:54:33 +1000904 const char *display;
Damien Miller1383bd82000-04-06 12:32:37 +1000905
Damien Miller95def091999-11-25 00:26:21 +1100906 /* Enable compression if requested. */
907 if (options.compression) {
908 debug("Requesting compression at level %d.", options.compression_level);
909
910 if (options.compression_level < 1 || options.compression_level > 9)
911 fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
912
913 /* Send the request. */
914 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
915 packet_put_int(options.compression_level);
916 packet_send();
917 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100918 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100919 if (type == SSH_SMSG_SUCCESS)
920 packet_start_compression(options.compression_level);
921 else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000922 logit("Warning: Remote host refused compression.");
Damien Miller95def091999-11-25 00:26:21 +1100923 else
924 packet_disconnect("Protocol error waiting for compression response.");
925 }
926 /* Allocate a pseudo tty if appropriate. */
927 if (tty_flag) {
928 debug("Requesting pty.");
929
930 /* Start the packet. */
931 packet_start(SSH_CMSG_REQUEST_PTY);
932
933 /* Store TERM in the packet. There is no limit on the
934 length of the string. */
935 cp = getenv("TERM");
936 if (!cp)
937 cp = "";
Ben Lindstrom664408d2001-06-09 01:42:01 +0000938 packet_put_cstring(cp);
Damien Miller95def091999-11-25 00:26:21 +1100939
940 /* Store window size in the packet. */
941 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
942 memset(&ws, 0, sizeof(ws));
Damien Miller71a73672006-03-26 14:04:36 +1100943 packet_put_int((u_int)ws.ws_row);
944 packet_put_int((u_int)ws.ws_col);
945 packet_put_int((u_int)ws.ws_xpixel);
946 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller95def091999-11-25 00:26:21 +1100947
948 /* Store tty modes in the packet. */
Ben Lindstromae8e2d32001-04-14 23:13:02 +0000949 tty_make_modes(fileno(stdin), NULL);
Damien Miller95def091999-11-25 00:26:21 +1100950
951 /* Send the packet, and wait for it to leave. */
952 packet_send();
953 packet_write_wait();
954
955 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100956 type = packet_read();
Damien Miller450a7a12000-03-26 13:04:51 +1000957 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100958 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +1000959 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000960 } else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +1000961 logit("Warning: Remote host failed or refused to allocate a pseudo tty.");
Damien Miller95def091999-11-25 00:26:21 +1100962 else
963 packet_disconnect("Protocol error waiting for pty request response.");
964 }
965 /* Request X11 forwarding if enabled and DISPLAY is set. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000966 display = getenv("DISPLAY");
967 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000968 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +1000969 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +1000970 client_x11_get_proto(display, options.xauth_location,
971 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +1000972 /* Request forwarding with authentication spoofing. */
Damien Miller95def091999-11-25 00:26:21 +1100973 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +1000974 x11_request_forwarding_with_spoofing(0, display, proto, data);
Damien Miller95def091999-11-25 00:26:21 +1100975
976 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100977 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100978 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100979 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +1000980 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller996acd22003-04-09 20:59:48 +1000981 logit("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +1000982 } else {
Damien Miller95def091999-11-25 00:26:21 +1100983 packet_disconnect("Protocol error waiting for X11 forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +1000984 }
Damien Miller95def091999-11-25 00:26:21 +1100985 }
986 /* Tell the packet module whether this is an interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000987 packet_set_interactive(interactive);
Damien Miller95def091999-11-25 00:26:21 +1100988
989 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100990 check_agent_present();
991
Damien Miller95def091999-11-25 00:26:21 +1100992 if (options.forward_agent) {
993 debug("Requesting authentication agent forwarding.");
994 auth_request_forwarding();
995
996 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100997 type = packet_read();
Damien Miller48b03fc2002-01-22 23:11:40 +1100998 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100999 if (type != SSH_SMSG_SUCCESS)
Damien Miller996acd22003-04-09 20:59:48 +10001000 logit("Warning: Remote host denied authentication agent forwarding.");
Damien Miller95def091999-11-25 00:26:21 +11001001 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001002
Damien Miller0bc1bd82000-11-13 22:57:25 +11001003 /* Initiate port forwardings. */
1004 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001005
Darren Tuckerb776c852007-12-02 23:06:35 +11001006 /* Execute a local command */
1007 if (options.local_command != NULL &&
1008 options.permit_local_command)
1009 ssh_local_cmd(options.local_command);
1010
Damien Miller5428f641999-11-25 11:54:57 +11001011 /* If requested, let ssh continue in the background. */
Damien Miller4af51302000-04-16 11:18:38 +10001012 if (fork_after_authentication_flag)
Damien Miller5428f641999-11-25 11:54:57 +11001013 if (daemon(1, 1) < 0)
1014 fatal("daemon() failed: %.200s", strerror(errno));
1015
1016 /*
1017 * If a command was specified on the command line, execute the
1018 * command now. Otherwise request the server to start a shell.
1019 */
Damien Miller95def091999-11-25 00:26:21 +11001020 if (buffer_len(&command) > 0) {
1021 int len = buffer_len(&command);
1022 if (len > 900)
1023 len = 900;
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001024 debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
Damien Miller95def091999-11-25 00:26:21 +11001025 packet_start(SSH_CMSG_EXEC_CMD);
1026 packet_put_string(buffer_ptr(&command), buffer_len(&command));
1027 packet_send();
1028 packet_write_wait();
1029 } else {
1030 debug("Requesting shell.");
1031 packet_start(SSH_CMSG_EXEC_SHELL);
1032 packet_send();
1033 packet_write_wait();
1034 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001035
Damien Miller95def091999-11-25 00:26:21 +11001036 /* Enter the interactive session. */
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001037 return client_loop(have_tty, tty_flag ?
1038 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
Damien Miller1383bd82000-04-06 12:32:37 +10001039}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001040
Ben Lindstrombba81212001-06-25 05:01:22 +00001041static void
Damien Miller0e220db2004-06-15 10:34:08 +10001042ssh_subsystem_reply(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001043{
1044 int id, len;
1045
1046 id = packet_get_int();
1047 len = buffer_len(&command);
Ben Lindstrom4040fe12001-03-05 06:52:57 +00001048 if (len > 900)
1049 len = 900;
Damien Miller48b03fc2002-01-22 23:11:40 +11001050 packet_check_eom();
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001051 if (type == SSH2_MSG_CHANNEL_FAILURE)
1052 fatal("Request for subsystem '%.*s' failed on channel %d",
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001053 len, (u_char *)buffer_ptr(&command), id);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001054}
1055
Damien Miller2797f7f2002-04-23 21:09:44 +10001056void
Damien Miller509b0102003-12-17 16:33:10 +11001057client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt)
Damien Miller2797f7f2002-04-23 21:09:44 +10001058{
1059 int i;
1060
1061 i = client_global_request_id++;
Damien Miller509b0102003-12-17 16:33:10 +11001062 if (i >= options.num_remote_forwards)
Damien Miller2797f7f2002-04-23 21:09:44 +10001063 return;
Damien Miller2797f7f2002-04-23 21:09:44 +10001064 debug("remote forward %s for: listen %d, connect %s:%d",
1065 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
Damien Millerf91ee4c2005-03-01 21:24:33 +11001066 options.remote_forwards[i].listen_port,
1067 options.remote_forwards[i].connect_host,
1068 options.remote_forwards[i].connect_port);
Darren Tuckere7d4b192006-07-12 22:17:10 +10001069 if (type == SSH2_MSG_REQUEST_FAILURE) {
1070 if (options.exit_on_forward_failure)
1071 fatal("Error: remote port forwarding failed for "
1072 "listen port %d",
1073 options.remote_forwards[i].listen_port);
1074 else
1075 logit("Warning: remote port forwarding failed for "
1076 "listen port %d",
1077 options.remote_forwards[i].listen_port);
1078 }
Damien Miller2797f7f2002-04-23 21:09:44 +10001079}
1080
Damien Miller0e220db2004-06-15 10:34:08 +10001081static void
1082ssh_control_listener(void)
1083{
1084 struct sockaddr_un addr;
1085 mode_t old_umask;
Damien Miller07b6ff12004-06-15 11:14:45 +10001086 int addr_len;
1087
Damien Millerd14b1e72005-06-16 13:19:41 +10001088 if (options.control_path == NULL ||
1089 options.control_master == SSHCTL_MASTER_NO)
Damien Miller0e220db2004-06-15 10:34:08 +10001090 return;
1091
Damien Millerd14b1e72005-06-16 13:19:41 +10001092 debug("setting up multiplex master socket");
1093
Damien Miller0e220db2004-06-15 10:34:08 +10001094 memset(&addr, '\0', sizeof(addr));
1095 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001096 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001097 strlen(options.control_path) + 1;
1098
1099 if (strlcpy(addr.sun_path, options.control_path,
1100 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1101 fatal("ControlPath too long");
1102
1103 if ((control_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001104 fatal("%s socket(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001105
1106 old_umask = umask(0177);
Damien Miller90967402006-03-26 14:07:26 +11001107 if (bind(control_fd, (struct sockaddr *)&addr, addr_len) == -1) {
Damien Miller0e220db2004-06-15 10:34:08 +10001108 control_fd = -1;
Damien Miller4f10e252005-05-04 15:33:09 +10001109 if (errno == EINVAL || errno == EADDRINUSE)
Damien Miller0e220db2004-06-15 10:34:08 +10001110 fatal("ControlSocket %s already exists",
1111 options.control_path);
1112 else
Damien Miller15d72a02005-11-05 15:07:33 +11001113 fatal("%s bind(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001114 }
1115 umask(old_umask);
1116
1117 if (listen(control_fd, 64) == -1)
Damien Miller15d72a02005-11-05 15:07:33 +11001118 fatal("%s listen(): %s", __func__, strerror(errno));
Damien Miller0e220db2004-06-15 10:34:08 +10001119
1120 set_nonblock(control_fd);
1121}
1122
Ben Lindstromf558cf62001-09-20 23:13:49 +00001123/* request pty/x11/agent/tcpfwd/shell for channel */
Ben Lindstrombba81212001-06-25 05:01:22 +00001124static void
Ben Lindstromf558cf62001-09-20 23:13:49 +00001125ssh_session2_setup(int id, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +10001126{
Damien Miller3756dce2004-06-18 01:17:29 +10001127 extern char **environ;
Damien Miller17e7ed02005-06-17 12:54:33 +10001128 const char *display;
Damien Miller0e220db2004-06-15 10:34:08 +10001129 int interactive = tty_flag;
Damien Miller17e7ed02005-06-17 12:54:33 +10001130
Damien Miller46d38de2005-07-17 17:02:09 +10001131 display = getenv("DISPLAY");
Damien Miller17e7ed02005-06-17 12:54:33 +10001132 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001133 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001134 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001135 client_x11_get_proto(display, options.xauth_location,
1136 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001137 /* Request forwarding with authentication spoofing. */
1138 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +10001139 x11_request_forwarding_with_spoofing(id, display, proto, data);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001140 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001141 /* XXX wait for reply */
1142 }
1143
Damien Miller0bc1bd82000-11-13 22:57:25 +11001144 check_agent_present();
1145 if (options.forward_agent) {
1146 debug("Requesting authentication agent forwarding.");
1147 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1148 packet_send();
1149 }
1150
Damien Miller0e220db2004-06-15 10:34:08 +10001151 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
Damien Miller3756dce2004-06-18 01:17:29 +10001152 NULL, fileno(stdin), &command, environ, &ssh_subsystem_reply);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001153
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001154 packet_set_interactive(interactive);
Damien Miller1383bd82000-04-06 12:32:37 +10001155}
1156
Ben Lindstromf558cf62001-09-20 23:13:49 +00001157/* open new channel for a session */
Ben Lindstrombba81212001-06-25 05:01:22 +00001158static int
Ben Lindstromf558cf62001-09-20 23:13:49 +00001159ssh_session2_open(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001160{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001161 Channel *c;
1162 int window, packetmax, in, out, err;
Damien Millerad833b32000-08-23 10:46:23 +10001163
Damien Millercaf6dd62000-08-29 11:33:50 +11001164 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001165 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001166 } else {
1167 in = dup(STDIN_FILENO);
1168 }
1169 out = dup(STDOUT_FILENO);
1170 err = dup(STDERR_FILENO);
1171
1172 if (in < 0 || out < 0 || err < 0)
1173 fatal("dup() in/out/err failed");
1174
Damien Miller69b69aa2000-10-28 14:19:58 +11001175 /* enable nonblocking unless tty */
1176 if (!isatty(in))
1177 set_nonblock(in);
1178 if (!isatty(out))
1179 set_nonblock(out);
1180 if (!isatty(err))
1181 set_nonblock(err);
1182
Damien Millere4340be2000-09-16 13:29:08 +11001183 window = CHAN_SES_WINDOW_DEFAULT;
1184 packetmax = CHAN_SES_PACKET_DEFAULT;
Damien Miller19a59452002-02-19 15:20:57 +11001185 if (tty_flag) {
1186 window >>= 1;
1187 packetmax >>= 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001188 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001189 c = channel_new(
Damien Miller1383bd82000-04-06 12:32:37 +10001190 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001191 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001192 "client-session", /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001193
Ben Lindstromf558cf62001-09-20 23:13:49 +00001194 debug3("ssh_session2_open: channel_new: %d", c->self);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001195
Ben Lindstrom5ec26452001-06-09 00:18:51 +00001196 channel_send_open(c->self);
Ben Lindstromf558cf62001-09-20 23:13:49 +00001197 if (!no_shell_flag)
Damien Miller0e220db2004-06-15 10:34:08 +10001198 channel_register_confirm(c->self, ssh_session2_setup, NULL);
Damien Miller1383bd82000-04-06 12:32:37 +10001199
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001200 return c->self;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001201}
1202
Ben Lindstrombba81212001-06-25 05:01:22 +00001203static int
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001204ssh_session2(void)
1205{
Ben Lindstromf558cf62001-09-20 23:13:49 +00001206 int id = -1;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001207
1208 /* XXX should be pre-session */
1209 ssh_init_forwarding();
1210
Ben Lindstromf558cf62001-09-20 23:13:49 +00001211 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1212 id = ssh_session2_open();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001213
Damien Millerd27b9472005-12-13 19:29:02 +11001214 /* Execute a local command */
1215 if (options.local_command != NULL &&
1216 options.permit_local_command)
1217 ssh_local_cmd(options.local_command);
1218
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001219 /* Start listening for multiplex clients */
1220 ssh_control_listener();
1221
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001222 /* If requested, let ssh continue in the background. */
1223 if (fork_after_authentication_flag)
1224 if (daemon(1, 1) < 0)
1225 fatal("daemon() failed: %.200s", strerror(errno));
1226
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001227 return client_loop(tty_flag, tty_flag ?
1228 options.escape_char : SSH_ESCAPECHAR_NONE, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001229}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001230
Ben Lindstrombba81212001-06-25 05:01:22 +00001231static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001232load_public_identity_files(void)
1233{
Damien Miller6b1d53c2006-03-31 23:13:21 +11001234 char *filename, *cp, thishost[NI_MAXHOST];
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001235 char *pwdir = NULL, *pwname = NULL;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001236 int i = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001237 Key *public;
Damien Miller6b1d53c2006-03-31 23:13:21 +11001238 struct passwd *pw;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001239#ifdef SMARTCARD
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001240 Key **keys;
1241
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001242 if (options.smartcard_device != NULL &&
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001243 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
Damien Miller4dec5d72006-08-05 11:38:40 +10001244 (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001245 int count = 0;
1246 for (i = 0; keys[i] != NULL; i++) {
1247 count++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001248 memmove(&options.identity_files[1], &options.identity_files[0],
1249 sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
1250 memmove(&options.identity_keys[1], &options.identity_keys[0],
1251 sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1252 options.num_identity_files++;
1253 options.identity_keys[0] = keys[i];
Damien Miller56a0bb02003-06-18 20:28:40 +10001254 options.identity_files[0] = sc_get_key_label(keys[i]);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001255 }
Ben Lindstrom4f054602002-03-26 03:23:00 +00001256 if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1257 options.num_identity_files = SSH_MAX_IDENTITY_FILES;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001258 i = count;
1259 xfree(keys);
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001260 }
Ben Lindstromffce1472001-08-06 21:57:31 +00001261#endif /* SMARTCARD */
Damien Miller6b1d53c2006-03-31 23:13:21 +11001262 if ((pw = getpwuid(original_real_uid)) == NULL)
1263 fatal("load_public_identity_files: getpwuid failed");
Darren Tuckere143f062007-12-02 23:21:16 +11001264 pwname = xstrdup(pw->pw_name);
1265 pwdir = xstrdup(pw->pw_dir);
Damien Miller6b1d53c2006-03-31 23:13:21 +11001266 if (gethostname(thishost, sizeof(thishost)) == -1)
1267 fatal("load_public_identity_files: gethostname: %s",
1268 strerror(errno));
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001269 for (; i < options.num_identity_files; i++) {
Damien Miller6b1d53c2006-03-31 23:13:21 +11001270 cp = tilde_expand_filename(options.identity_files[i],
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001271 original_real_uid);
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001272 filename = percent_expand(cp, "d", pwdir,
1273 "u", pwname, "l", thishost, "h", host,
Damien Miller6b1d53c2006-03-31 23:13:21 +11001274 "r", options.user, (char *)NULL);
1275 xfree(cp);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001276 public = key_load_public(filename, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001277 debug("identity file %s type %d", filename,
1278 public ? public->type : -1);
1279 xfree(options.identity_files[i]);
1280 options.identity_files[i] = filename;
1281 options.identity_keys[i] = public;
1282 }
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001283 bzero(pwname, strlen(pwname));
Darren Tuckere143f062007-12-02 23:21:16 +11001284 xfree(pwname);
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001285 bzero(pwdir, strlen(pwdir));
Darren Tuckere143f062007-12-02 23:21:16 +11001286 xfree(pwdir);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001287}
Damien Miller0e220db2004-06-15 10:34:08 +10001288
1289static void
1290control_client_sighandler(int signo)
1291{
1292 control_client_terminate = signo;
1293}
1294
1295static void
1296control_client_sigrelay(int signo)
1297{
Darren Tucker06321f52007-12-02 23:22:52 +11001298 int save_errno = errno;
1299
Damien Miller0e220db2004-06-15 10:34:08 +10001300 if (control_server_pid > 1)
1301 kill(control_server_pid, signo);
Darren Tucker06321f52007-12-02 23:22:52 +11001302
1303 errno = save_errno;
Damien Miller0e220db2004-06-15 10:34:08 +10001304}
1305
Darren Tucker365433f2004-06-22 12:29:23 +10001306static int
1307env_permitted(char *env)
1308{
Darren Tucker284706a2006-07-12 22:16:23 +10001309 int i, ret;
Darren Tucker365433f2004-06-22 12:29:23 +10001310 char name[1024], *cp;
1311
Darren Tucker284706a2006-07-12 22:16:23 +10001312 if ((cp = strchr(env, '=')) == NULL || cp == env)
Darren Tucker365433f2004-06-22 12:29:23 +10001313 return (0);
Darren Tucker57f42242006-07-12 22:23:35 +10001314 ret = snprintf(name, sizeof(name), "%.*s", (int)(cp - env), env);
Darren Tucker284706a2006-07-12 22:16:23 +10001315 if (ret <= 0 || (size_t)ret >= sizeof(name))
1316 fatal("env_permitted: name '%.100s...' too long", env);
Darren Tucker365433f2004-06-22 12:29:23 +10001317
1318 for (i = 0; i < options.num_send_env; i++)
1319 if (match_pattern(name, options.send_env[i]))
1320 return (1);
1321
1322 return (0);
1323}
1324
Damien Miller0e220db2004-06-15 10:34:08 +10001325static void
1326control_client(const char *path)
1327{
1328 struct sockaddr_un addr;
Darren Tucker132367f2007-06-25 18:59:17 +10001329 int i, r, fd, sock, exitval[2], num_env, addr_len;
Damien Miller0e220db2004-06-15 10:34:08 +10001330 Buffer m;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001331 char *term;
Damien Miller3756dce2004-06-18 01:17:29 +10001332 extern char **environ;
Darren Tucker7ebfc102004-11-07 20:06:19 +11001333 u_int flags;
Darren Tuckerfc959702004-07-17 16:12:08 +10001334
Damien Millerd14b1e72005-06-16 13:19:41 +10001335 if (mux_command == 0)
1336 mux_command = SSHMUX_COMMAND_OPEN;
1337
1338 switch (options.control_master) {
1339 case SSHCTL_MASTER_AUTO:
1340 case SSHCTL_MASTER_AUTO_ASK:
1341 debug("auto-mux: Trying existing master");
1342 /* FALLTHROUGH */
1343 case SSHCTL_MASTER_NO:
1344 break;
1345 default:
1346 return;
1347 }
1348
Damien Miller0e220db2004-06-15 10:34:08 +10001349 memset(&addr, '\0', sizeof(addr));
1350 addr.sun_family = AF_UNIX;
Damien Miller07b6ff12004-06-15 11:14:45 +10001351 addr_len = offsetof(struct sockaddr_un, sun_path) +
Damien Miller0e220db2004-06-15 10:34:08 +10001352 strlen(path) + 1;
1353
1354 if (strlcpy(addr.sun_path, path,
1355 sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1356 fatal("ControlPath too long");
1357
1358 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1359 fatal("%s socket(): %s", __func__, strerror(errno));
1360
Damien Miller90967402006-03-26 14:07:26 +11001361 if (connect(sock, (struct sockaddr *)&addr, addr_len) == -1) {
Darren Tucker0814d312005-06-01 23:08:51 +10001362 if (mux_command != SSHMUX_COMMAND_OPEN) {
1363 fatal("Control socket connect(%.100s): %s", path,
1364 strerror(errno));
1365 }
Damien Miller538c9b72005-05-26 12:11:28 +10001366 if (errno == ENOENT)
Damien Miller4662d342006-03-26 13:59:59 +11001367 debug("Control socket \"%.100s\" does not exist", path);
Damien Miller538c9b72005-05-26 12:11:28 +10001368 else {
Damien Miller4662d342006-03-26 13:59:59 +11001369 error("Control socket connect(%.100s): %s", path,
Damien Miller538c9b72005-05-26 12:11:28 +10001370 strerror(errno));
1371 }
Damien Miller4662d342006-03-26 13:59:59 +11001372 close(sock);
1373 return;
1374 }
Damien Miller46d38de2005-07-17 17:02:09 +10001375
Damien Miller4662d342006-03-26 13:59:59 +11001376 if (stdin_null_flag) {
1377 if ((fd = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1378 fatal("open(/dev/null): %s", strerror(errno));
1379 if (dup2(fd, STDIN_FILENO) == -1)
1380 fatal("dup2: %s", strerror(errno));
1381 if (fd > STDERR_FILENO)
1382 close(fd);
1383 }
Damien Miller46d38de2005-07-17 17:02:09 +10001384
Damien Miller13390022005-07-06 09:44:19 +10001385 term = getenv("TERM");
Darren Tucker7ebfc102004-11-07 20:06:19 +11001386
1387 flags = 0;
1388 if (tty_flag)
1389 flags |= SSHMUX_FLAG_TTY;
1390 if (subsystem_flag)
1391 flags |= SSHMUX_FLAG_SUBSYS;
Damien Miller13390022005-07-06 09:44:19 +10001392 if (options.forward_x11)
1393 flags |= SSHMUX_FLAG_X11_FWD;
1394 if (options.forward_agent)
1395 flags |= SSHMUX_FLAG_AGENT_FWD;
Damien Miller0e220db2004-06-15 10:34:08 +10001396
Damien Millerd39a3cf2008-02-10 22:23:18 +11001397 signal(SIGPIPE, SIG_IGN);
1398
Damien Miller0e220db2004-06-15 10:34:08 +10001399 buffer_init(&m);
1400
Darren Tucker7ebfc102004-11-07 20:06:19 +11001401 /* Send our command to server */
1402 buffer_put_int(&m, mux_command);
1403 buffer_put_int(&m, flags);
Damien Miller13390022005-07-06 09:44:19 +10001404 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001405 fatal("%s: msg_send", __func__);
1406 buffer_clear(&m);
1407
1408 /* Get authorisation status and PID of controlee */
Damien Miller0e220db2004-06-15 10:34:08 +10001409 if (ssh_msg_recv(sock, &m) == -1)
1410 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001411 if (buffer_get_char(&m) != SSHMUX_VER)
Damien Miller0e220db2004-06-15 10:34:08 +10001412 fatal("%s: wrong version", __func__);
Damien Miller23f07702004-06-18 01:19:03 +10001413 if (buffer_get_int(&m) != 1)
1414 fatal("Connection to master denied");
Damien Miller0e220db2004-06-15 10:34:08 +10001415 control_server_pid = buffer_get_int(&m);
1416
Damien Miller0e220db2004-06-15 10:34:08 +10001417 buffer_clear(&m);
Damien Miller0e220db2004-06-15 10:34:08 +10001418
Darren Tucker7ebfc102004-11-07 20:06:19 +11001419 switch (mux_command) {
1420 case SSHMUX_COMMAND_ALIVE_CHECK:
Darren Tucker47eede72005-03-14 23:08:12 +11001421 fprintf(stderr, "Master running (pid=%d)\r\n",
Darren Tucker7ebfc102004-11-07 20:06:19 +11001422 control_server_pid);
1423 exit(0);
1424 case SSHMUX_COMMAND_TERMINATE:
1425 fprintf(stderr, "Exit request sent.\r\n");
1426 exit(0);
1427 case SSHMUX_COMMAND_OPEN:
1428 /* continue below */
1429 break;
1430 default:
1431 fatal("silly mux_command %d", mux_command);
1432 }
1433
1434 /* SSHMUX_COMMAND_OPEN */
Damien Miller13390022005-07-06 09:44:19 +10001435 buffer_put_cstring(&m, term ? term : "");
Damien Miller0e220db2004-06-15 10:34:08 +10001436 buffer_append(&command, "\0", 1);
1437 buffer_put_cstring(&m, buffer_ptr(&command));
1438
Darren Tucker365433f2004-06-22 12:29:23 +10001439 if (options.num_send_env == 0 || environ == NULL) {
1440 buffer_put_int(&m, 0);
Darren Tuckerfc959702004-07-17 16:12:08 +10001441 } else {
Darren Tucker365433f2004-06-22 12:29:23 +10001442 /* Pass environment */
1443 num_env = 0;
1444 for (i = 0; environ[i] != NULL; i++)
1445 if (env_permitted(environ[i]))
1446 num_env++; /* Count */
Darren Tuckerfc959702004-07-17 16:12:08 +10001447
Darren Tucker365433f2004-06-22 12:29:23 +10001448 buffer_put_int(&m, num_env);
1449
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001450 for (i = 0; environ[i] != NULL && num_env >= 0; i++)
1451 if (env_permitted(environ[i])) {
1452 num_env--;
Darren Tucker365433f2004-06-22 12:29:23 +10001453 buffer_put_cstring(&m, environ[i]);
Darren Tuckerb5bc1a62004-06-24 00:34:53 +10001454 }
Darren Tucker365433f2004-06-22 12:29:23 +10001455 }
Damien Miller3756dce2004-06-18 01:17:29 +10001456
Damien Miller13390022005-07-06 09:44:19 +10001457 if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
Damien Miller0e220db2004-06-15 10:34:08 +10001458 fatal("%s: msg_send", __func__);
1459
Damien Miller54fd7cf2007-09-17 12:04:08 +10001460 if (mm_send_fd(sock, STDIN_FILENO) == -1 ||
1461 mm_send_fd(sock, STDOUT_FILENO) == -1 ||
1462 mm_send_fd(sock, STDERR_FILENO) == -1)
1463 fatal("%s: send fds failed", __func__);
Damien Miller0e220db2004-06-15 10:34:08 +10001464
1465 /* Wait for reply, so master has a chance to gather ttymodes */
1466 buffer_clear(&m);
1467 if (ssh_msg_recv(sock, &m) == -1)
1468 fatal("%s: msg_recv", __func__);
Damien Miller13390022005-07-06 09:44:19 +10001469 if (buffer_get_char(&m) != SSHMUX_VER)
Darren Tucker7ebfc102004-11-07 20:06:19 +11001470 fatal("%s: wrong version", __func__);
Damien Miller0e220db2004-06-15 10:34:08 +10001471 buffer_free(&m);
1472
Darren Tucker07336da2004-11-05 20:02:16 +11001473 signal(SIGHUP, control_client_sighandler);
Damien Miller0809e232004-06-18 22:20:57 +10001474 signal(SIGINT, control_client_sighandler);
1475 signal(SIGTERM, control_client_sighandler);
1476 signal(SIGWINCH, control_client_sigrelay);
1477
Damien Miller0e220db2004-06-15 10:34:08 +10001478 if (tty_flag)
1479 enter_raw_mode();
1480
Darren Tucker132367f2007-06-25 18:59:17 +10001481 /*
1482 * Stick around until the controlee closes the client_fd.
1483 * Before it does, it is expected to write this process' exit
1484 * value (one int). This process must read the value and wait for
1485 * the closure of the client_fd; if this one closes early, the
1486 * multiplex master will terminate early too (possibly losing data).
1487 */
1488 exitval[0] = 0;
Darren Tucker2cbec742007-06-12 23:41:33 +10001489 for (i = 0; !control_client_terminate && i < (int)sizeof(exitval);) {
Darren Tucker132367f2007-06-25 18:59:17 +10001490 r = read(sock, (char *)exitval + i, sizeof(exitval) - i);
Damien Miller0e220db2004-06-15 10:34:08 +10001491 if (r == 0) {
1492 debug2("Received EOF from master");
1493 break;
1494 }
Darren Tuckerd989ada2007-06-25 18:34:43 +10001495 if (r == -1) {
1496 if (errno == EINTR)
1497 continue;
Damien Miller0e220db2004-06-15 10:34:08 +10001498 fatal("%s: read %s", __func__, strerror(errno));
Darren Tuckerd989ada2007-06-25 18:34:43 +10001499 }
Darren Tucker2cbec742007-06-12 23:41:33 +10001500 i += r;
Damien Miller0e220db2004-06-15 10:34:08 +10001501 }
Darren Tucker132367f2007-06-25 18:59:17 +10001502
Damien Miller0e220db2004-06-15 10:34:08 +10001503 close(sock);
Damien Miller0e220db2004-06-15 10:34:08 +10001504 leave_raw_mode();
Darren Tucker132367f2007-06-25 18:59:17 +10001505 if (i > (int)sizeof(int))
1506 fatal("%s: master returned too much data (%d > %lu)",
1507 __func__, i, sizeof(int));
Darren Tucker2cbec742007-06-12 23:41:33 +10001508 if (control_client_terminate) {
1509 debug2("Exiting on signal %d", control_client_terminate);
Darren Tucker132367f2007-06-25 18:59:17 +10001510 exitval[0] = 255;
1511 } else if (i < (int)sizeof(int)) {
Darren Tucker2cbec742007-06-12 23:41:33 +10001512 debug2("Control master terminated unexpectedly");
Darren Tucker132367f2007-06-25 18:59:17 +10001513 exitval[0] = 255;
Darren Tucker2cbec742007-06-12 23:41:33 +10001514 } else
Darren Tucker132367f2007-06-25 18:59:17 +10001515 debug2("Received exit status from master %d", exitval[0]);
Darren Tucker2cbec742007-06-12 23:41:33 +10001516
Damien Miller0e220db2004-06-15 10:34:08 +10001517 if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
Darren Tucker132367f2007-06-25 18:59:17 +10001518 fprintf(stderr, "Shared connection to %s closed.\r\n", host);
Damien Miller0e220db2004-06-15 10:34:08 +10001519
Darren Tucker132367f2007-06-25 18:59:17 +10001520 exit(exitval[0]);
Damien Miller0e220db2004-06-15 10:34:08 +10001521}