blob: b86a764f668fe1c4e4b20c531c3ed8ee743b575b [file] [log] [blame]
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001/* $OpenBSD: ssh.c,v 1.331 2010/01/11 01:39:46 dtucker 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>
Darren Tucker199b1342009-07-06 07:16:56 +100051#include <sys/param.h>
Damien Millere3b60b52006-07-10 21:08:03 +100052#include <sys/socket.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"
Darren Tucker25f60a72004-08-15 17:23:34 +1000102#include "uidswap.h"
Darren Tuckerf1de4e52010-01-08 16:54:59 +1100103#include "roaming.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
Darren Tuckerd6173c02008-06-13 04:52:53 +1000112/* Flag indicating whether debug mode is on. May 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
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100136/* forward stdio to remote host and port */
137char *stdio_forward_host = NULL;
138int stdio_forward_port = 0;
139
Damien Miller5428f641999-11-25 11:54:57 +1100140/*
141 * General data structure for command line options and options configurable
142 * in configuration files. See readconf.h.
143 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144Options options;
145
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000146/* optional user configfile */
147char *config = NULL;
148
Damien Miller5428f641999-11-25 11:54:57 +1100149/*
150 * Name of the host we are connecting to. This is the name given on the
151 * command line, or the HostName specified for the user-supplied name in a
152 * configuration file.
153 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154char *host;
155
156/* socket address the host resolves to */
Damien Miller34132e52000-01-14 15:45:46 +1100157struct sockaddr_storage hostaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000159/* Private host keys. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000160Sensitive sensitive_data;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000161
162/* Original real UID. */
163uid_t original_real_uid;
Ben Lindstromf9c48842002-06-11 16:37:51 +0000164uid_t original_effective_uid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165
Damien Miller1383bd82000-04-06 12:32:37 +1000166/* command to be executed */
167Buffer command;
168
Damien Miller832562e2001-01-30 09:30:01 +1100169/* Should we execute a command or invoke a subsystem? */
170int subsystem_flag = 0;
171
Damien Miller2797f7f2002-04-23 21:09:44 +1000172/* # of replies received for global requests */
Darren Tucker9f407c42008-06-13 04:50:27 +1000173static int remote_forward_confirms_received = 0;
Damien Miller2797f7f2002-04-23 21:09:44 +1000174
Damien Miller8c4e18a2002-09-19 12:05:02 +1000175/* pid of proxycommand child process */
176pid_t proxy_command_pid = 0;
177
Damien Millerb1cbfa22008-05-19 16:00:08 +1000178/* mux.c */
179extern int muxserver_sock;
180extern u_int muxclient_command;
Damien Miller0e220db2004-06-15 10:34:08 +1000181
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000182/* Prints a help message to the user. This function never returns. */
183
Ben Lindstrombba81212001-06-25 05:01:22 +0000184static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000185usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000186{
Damien Miller50955102004-03-22 09:34:58 +1100187 fprintf(stderr,
Damien Millerc13c3ee2008-11-03 19:23:28 +1100188"usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]\n"
Darren Tucker895d6982005-10-03 18:18:05 +1000189" [-D [bind_address:]port] [-e escape_char] [-F configfile]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100190" [-i identity_file] [-L [bind_address:]port:host:hostport]\n"
Damien Millerf91ee4c2005-03-01 21:24:33 +1100191" [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100192" [-R [bind_address:]port:host:hostport] [-S ctl_path]\n"
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100193" [-W host:port] [-w local_tun[:remote_tun]]\n"
194" [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 Millerb1cbfa22008-05-19 16:00:08 +1000202
203/* from muxclient.c */
204void muxclient(const char *);
205void muxserver_listen(void);
Damien Miller1383bd82000-04-06 12:32:37 +1000206
Damien Miller95def091999-11-25 00:26:21 +1100207/*
208 * Main program for the ssh client.
209 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000210int
211main(int ac, char **av)
212{
Darren Tucker199b1342009-07-06 07:16:56 +1000213 int i, r, opt, exit_status, use_syslog;
214 char *p, *cp, *line, *argv0, buf[MAXPATHLEN];
Damien Miller95def091999-11-25 00:26:21 +1100215 struct stat st;
Ben Lindstrom086cf212001-03-05 05:56:40 +0000216 struct passwd *pw;
Damien Miller67bd0622007-09-17 12:06:57 +1000217 int dummy, timeout_ms;
Ben Lindstrom5ccf63a2001-09-24 20:00:10 +0000218 extern int optind, optreset;
Damien Miller4f8e6692001-07-14 13:22:53 +1000219 extern char *optarg;
Damien Miller9651fe62005-06-26 08:55:25 +1000220 struct servent *sp;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100221 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000222
Darren Tuckerce321d82005-10-03 18:11:24 +1000223 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
224 sanitise_stdfd();
225
Damien Miller59d3d5b2003-08-22 09:34:41 +1000226 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000227 init_rng();
228
Damien Miller5428f641999-11-25 11:54:57 +1100229 /*
230 * Save the original real uid. It will be needed later (uid-swapping
231 * may clobber the real uid).
232 */
Damien Miller95def091999-11-25 00:26:21 +1100233 original_real_uid = getuid();
234 original_effective_uid = geteuid();
Damien Millera8e06ce2003-11-21 23:48:55 +1100235
Damien Miller50b9a602002-09-04 16:50:06 +1000236 /*
237 * Use uid-swapping to give up root privileges for the duration of
238 * option processing. We will re-instantiate the rights when we are
239 * ready to create the privileged port, and will permanently drop
240 * them when the port has been created (actually, when the connection
241 * has been made, as we may need to create the port several times).
242 */
243 PRIV_END;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000244
Damien Miller05dd7952000-10-01 00:42:48 +1100245#ifdef HAVE_SETRLIMIT
Damien Miller95def091999-11-25 00:26:21 +1100246 /* If we are installed setuid root be careful to not drop core. */
247 if (original_real_uid != original_effective_uid) {
248 struct rlimit rlim;
249 rlim.rlim_cur = rlim.rlim_max = 0;
250 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
251 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100253#endif
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000254 /* Get user data. */
255 pw = getpwuid(original_real_uid);
256 if (!pw) {
Damien Miller996acd22003-04-09 20:59:48 +1000257 logit("You don't exist, go away!");
Darren Tuckere9a9b712005-12-20 16:15:51 +1100258 exit(255);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000259 }
260 /* Take a copy of the returned structure. */
261 pw = pwcopy(pw);
262
Damien Miller5428f641999-11-25 11:54:57 +1100263 /*
Damien Miller5428f641999-11-25 11:54:57 +1100264 * Set our umask to something reasonable, as some files are created
265 * with the default umask. This will make them world-readable but
266 * writable only by the owner, which is ok for all files for which we
267 * don't set the modes explicitly.
268 */
Damien Miller95def091999-11-25 00:26:21 +1100269 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270
Darren Tuckerd6173c02008-06-13 04:52:53 +1000271 /*
272 * Initialize option structure to indicate that no values have been
273 * set.
274 */
Damien Miller95def091999-11-25 00:26:21 +1100275 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000276
Damien Miller95def091999-11-25 00:26:21 +1100277 /* Parse command-line arguments. */
278 host = NULL;
Damien Millere272a5b2008-11-03 19:22:37 +1100279 use_syslog = 0;
Darren Tucker72efd742009-06-21 17:48:00 +1000280 argv0 = av[0];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000281
Damien Miller2ecb6bd2006-03-15 12:03:53 +1100282 again:
Darren Tuckerd6173c02008-06-13 04:52:53 +1000283 while ((opt = getopt(ac, av, "1246ab:c:e:fgi:kl:m:no:p:qstvx"
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100284 "ACD:F:I:KL:MNO:PR:S:TVw:W:XYy")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100285 switch (opt) {
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000286 case '1':
287 options.protocol = SSH_PROTO_1;
288 break;
Damien Miller4af51302000-04-16 11:18:38 +1000289 case '2':
290 options.protocol = SSH_PROTO_2;
291 break;
Damien Miller34132e52000-01-14 15:45:46 +1100292 case '4':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000293 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100294 break;
Damien Miller34132e52000-01-14 15:45:46 +1100295 case '6':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000296 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100297 break;
Damien Miller95def091999-11-25 00:26:21 +1100298 case 'n':
299 stdin_null_flag = 1;
300 break;
Damien Miller95def091999-11-25 00:26:21 +1100301 case 'f':
302 fork_after_authentication_flag = 1;
303 stdin_null_flag = 1;
304 break;
Damien Miller95def091999-11-25 00:26:21 +1100305 case 'x':
306 options.forward_x11 = 0;
307 break;
Damien Miller95def091999-11-25 00:26:21 +1100308 case 'X':
309 options.forward_x11 = 1;
310 break;
Damien Millere272a5b2008-11-03 19:22:37 +1100311 case 'y':
312 use_syslog = 1;
313 break;
Darren Tucker0a118da2003-10-15 15:54:32 +1000314 case 'Y':
315 options.forward_x11 = 1;
316 options.forward_x11_trusted = 1;
317 break;
Damien Miller95def091999-11-25 00:26:21 +1100318 case 'g':
319 options.gateway_ports = 1;
320 break;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100321 case 'O':
322 if (strcmp(optarg, "check") == 0)
Damien Millerb1cbfa22008-05-19 16:00:08 +1000323 muxclient_command = SSHMUX_COMMAND_ALIVE_CHECK;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100324 else if (strcmp(optarg, "exit") == 0)
Damien Millerb1cbfa22008-05-19 16:00:08 +1000325 muxclient_command = SSHMUX_COMMAND_TERMINATE;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100326 else
327 fatal("Invalid multiplex command.");
328 break;
Damien Miller147bba32002-09-04 16:46:06 +1000329 case 'P': /* deprecated */
Damien Miller95def091999-11-25 00:26:21 +1100330 options.use_privileged_port = 0;
331 break;
Damien Miller95def091999-11-25 00:26:21 +1100332 case 'a':
333 options.forward_agent = 0;
334 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000335 case 'A':
336 options.forward_agent = 1;
337 break;
Damien Miller95def091999-11-25 00:26:21 +1100338 case 'k':
Damien Millere0113cc2003-11-24 13:10:09 +1100339 options.gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100340 break;
Darren Tucker415bddc2007-06-12 23:43:16 +1000341 case 'K':
342 options.gss_authentication = 1;
343 options.gss_deleg_creds = 1;
344 break;
Damien Miller95def091999-11-25 00:26:21 +1100345 case 'i':
346 if (stat(optarg, &st) < 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000347 fprintf(stderr, "Warning: Identity file %s "
Damien Miller3eb48b62005-03-01 21:15:46 +1100348 "not accessible: %s.\n", optarg,
349 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100350 break;
351 }
Damien Millerf4614452001-07-14 12:18:10 +1000352 if (options.num_identity_files >=
353 SSH_MAX_IDENTITY_FILES)
354 fatal("Too many identity files specified "
355 "(max %d)", SSH_MAX_IDENTITY_FILES);
356 options.identity_files[options.num_identity_files++] =
357 xstrdup(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100358 break;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000359 case 'I':
360#ifdef SMARTCARD
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000361 options.smartcard_device = xstrdup(optarg);
Ben Lindstrombcc18082001-08-06 21:59:25 +0000362#else
Ben Lindstromc5b68002001-07-04 04:52:03 +0000363 fprintf(stderr, "no support for smartcards.\n");
Ben Lindstrombcc18082001-08-06 21:59:25 +0000364#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000365 break;
Damien Miller95def091999-11-25 00:26:21 +1100366 case 't':
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000367 if (tty_flag)
368 force_tty_flag = 1;
Damien Miller95def091999-11-25 00:26:21 +1100369 tty_flag = 1;
370 break;
Damien Miller95def091999-11-25 00:26:21 +1100371 case 'v':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000372 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100373 debug_flag = 1;
374 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000375 } else {
376 if (options.log_level < SYSLOG_LEVEL_DEBUG3)
377 options.log_level++;
Damien Millere4340be2000-09-16 13:29:08 +1100378 break;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000379 }
Darren Tuckere9bf9842004-11-05 20:05:32 +1100380 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100381 case 'V':
Damien Miller0c889cd2004-03-22 09:36:00 +1100382 fprintf(stderr, "%s, %s\n",
Damien Miller2aa6d3c2004-09-12 16:53:04 +1000383 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
Damien Miller95def091999-11-25 00:26:21 +1100384 if (opt == 'V')
385 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100386 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100387 case 'w':
Damien Miller7b58e802005-12-13 19:33:19 +1100388 if (options.tun_open == -1)
389 options.tun_open = SSH_TUNMODE_DEFAULT;
Damien Millerd27b9472005-12-13 19:29:02 +1100390 options.tun_local = a2tun(optarg, &options.tun_remote);
Damien Miller7b58e802005-12-13 19:33:19 +1100391 if (options.tun_local == SSH_TUNID_ERR) {
Darren Tuckerd6173c02008-06-13 04:52:53 +1000392 fprintf(stderr,
393 "Bad tun device '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100394 exit(255);
Damien Millerd27b9472005-12-13 19:29:02 +1100395 }
396 break;
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100397 case 'W':
398 if (parse_forward(&fwd, optarg, 1, 0)) {
399 stdio_forward_host = fwd.listen_host;
400 stdio_forward_port = fwd.listen_port;
401 xfree(fwd.connect_host);
402 } else {
403 fprintf(stderr,
404 "Bad stdio forwarding specification '%s'\n",
405 optarg);
406 exit(255);
407 }
408 no_tty_flag = 1;
409 no_shell_flag = 1;
410 options.clear_forwardings = 1;
411 options.exit_on_forward_failure = 1;
412 break;
Damien Miller95def091999-11-25 00:26:21 +1100413 case 'q':
414 options.log_level = SYSLOG_LEVEL_QUIET;
415 break;
Damien Miller95def091999-11-25 00:26:21 +1100416 case 'e':
417 if (optarg[0] == '^' && optarg[2] == 0 &&
Damien Millerf4614452001-07-14 12:18:10 +1000418 (u_char) optarg[1] >= 64 &&
419 (u_char) optarg[1] < 128)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000420 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100421 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000422 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100423 else if (strcmp(optarg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000424 options.escape_char = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100425 else {
Damien Millerf4614452001-07-14 12:18:10 +1000426 fprintf(stderr, "Bad escape character '%s'.\n",
427 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100428 exit(255);
Damien Miller95def091999-11-25 00:26:21 +1100429 }
430 break;
Damien Miller95def091999-11-25 00:26:21 +1100431 case 'c':
Damien Millereba71ba2000-04-29 23:57:08 +1000432 if (ciphers_valid(optarg)) {
433 /* SSH2 only */
434 options.ciphers = xstrdup(optarg);
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000435 options.cipher = SSH_CIPHER_INVALID;
Damien Millereba71ba2000-04-29 23:57:08 +1000436 } else {
437 /* SSH1 only */
Damien Millere39cacc2000-11-29 12:18:44 +1100438 options.cipher = cipher_number(optarg);
439 if (options.cipher == -1) {
Damien Millerf4614452001-07-14 12:18:10 +1000440 fprintf(stderr,
441 "Unknown cipher type '%s'\n",
442 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100443 exit(255);
Damien Millereba71ba2000-04-29 23:57:08 +1000444 }
Damien Millerf4614452001-07-14 12:18:10 +1000445 if (options.cipher == SSH_CIPHER_3DES)
Damien Millere39cacc2000-11-29 12:18:44 +1100446 options.ciphers = "3des-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000447 else if (options.cipher == SSH_CIPHER_BLOWFISH)
Damien Millere39cacc2000-11-29 12:18:44 +1100448 options.ciphers = "blowfish-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000449 else
Damien Millere39cacc2000-11-29 12:18:44 +1100450 options.ciphers = (char *)-1;
Damien Miller95def091999-11-25 00:26:21 +1100451 }
452 break;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000453 case 'm':
454 if (mac_valid(optarg))
455 options.macs = xstrdup(optarg);
456 else {
Damien Millerf4614452001-07-14 12:18:10 +1000457 fprintf(stderr, "Unknown mac type '%s'\n",
458 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100459 exit(255);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000460 }
461 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000462 case 'M':
Damien Millerd14b1e72005-06-16 13:19:41 +1000463 if (options.control_master == SSHCTL_MASTER_YES)
464 options.control_master = SSHCTL_MASTER_ASK;
465 else
466 options.control_master = SSHCTL_MASTER_YES;
Damien Miller0e220db2004-06-15 10:34:08 +1000467 break;
Damien Miller95def091999-11-25 00:26:21 +1100468 case 'p':
Ben Lindstrom19066a12001-04-12 23:39:26 +0000469 options.port = a2port(optarg);
Damien Miller3dc71ad2009-01-28 16:31:22 +1100470 if (options.port <= 0) {
Ben Lindstrom146edb92001-04-11 23:06:28 +0000471 fprintf(stderr, "Bad port '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100472 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000473 }
Damien Miller95def091999-11-25 00:26:21 +1100474 break;
Damien Miller95def091999-11-25 00:26:21 +1100475 case 'l':
476 options.user = optarg;
477 break;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000478
Damien Miller95def091999-11-25 00:26:21 +1100479 case 'L':
Damien Miller4bf648f2009-02-14 16:28:21 +1100480 if (parse_forward(&fwd, optarg, 0, 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +1100481 add_local_forward(&options, &fwd);
482 else {
Damien Millerf4614452001-07-14 12:18:10 +1000483 fprintf(stderr,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100484 "Bad local forwarding specification '%s'\n",
Damien Millerf4614452001-07-14 12:18:10 +1000485 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100486 exit(255);
Ben Lindstrom1a174712001-09-12 17:56:15 +0000487 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100488 break;
489
490 case 'R':
Damien Miller4bf648f2009-02-14 16:28:21 +1100491 if (parse_forward(&fwd, optarg, 0, 1)) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100492 add_remote_forward(&options, &fwd);
493 } else {
494 fprintf(stderr,
495 "Bad remote forwarding specification "
496 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100497 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100498 }
Damien Miller95def091999-11-25 00:26:21 +1100499 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000500
501 case 'D':
Damien Miller4bf648f2009-02-14 16:28:21 +1100502 if (parse_forward(&fwd, optarg, 1, 0)) {
Damien Millera699d952008-11-03 19:27:34 +1100503 add_local_forward(&options, &fwd);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100504 } else {
Damien Millera699d952008-11-03 19:27:34 +1100505 fprintf(stderr,
506 "Bad dynamic forwarding specification "
507 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100508 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000509 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000510 break;
511
Damien Miller95def091999-11-25 00:26:21 +1100512 case 'C':
513 options.compression = 1;
514 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000515 case 'N':
516 no_shell_flag = 1;
517 no_tty_flag = 1;
518 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000519 case 'T':
520 no_tty_flag = 1;
521 break;
Damien Miller95def091999-11-25 00:26:21 +1100522 case 'o':
523 dummy = 1;
Damien Miller9836cf82003-12-17 16:30:06 +1100524 line = xstrdup(optarg);
Damien Millerf4614452001-07-14 12:18:10 +1000525 if (process_config_line(&options, host ? host : "",
Damien Miller9836cf82003-12-17 16:30:06 +1100526 line, "command-line", 0, &dummy) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100527 exit(255);
Damien Miller9836cf82003-12-17 16:30:06 +1100528 xfree(line);
Damien Miller95def091999-11-25 00:26:21 +1100529 break;
Damien Miller832562e2001-01-30 09:30:01 +1100530 case 's':
531 subsystem_flag = 1;
532 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000533 case 'S':
534 if (options.control_path != NULL)
535 free(options.control_path);
536 options.control_path = xstrdup(optarg);
Damien Miller0e220db2004-06-15 10:34:08 +1000537 break;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000538 case 'b':
539 options.bind_address = optarg;
540 break;
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000541 case 'F':
542 config = optarg;
543 break;
Damien Miller95def091999-11-25 00:26:21 +1100544 default:
545 usage();
546 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000547 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000548
Damien Millerf4614452001-07-14 12:18:10 +1000549 ac -= optind;
550 av += optind;
551
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100552 if (ac > 0 && !host) {
Ben Lindstromc276c122002-12-23 02:14:51 +0000553 if (strrchr(*av, '@')) {
Damien Millerf4614452001-07-14 12:18:10 +1000554 p = xstrdup(*av);
Ben Lindstromc276c122002-12-23 02:14:51 +0000555 cp = strrchr(p, '@');
Damien Millerf4614452001-07-14 12:18:10 +1000556 if (cp == NULL || cp == p)
557 usage();
558 options.user = p;
559 *cp = '\0';
560 host = ++cp;
561 } else
562 host = *av;
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000563 if (ac > 1) {
564 optind = optreset = 1;
Damien Millerf4614452001-07-14 12:18:10 +1000565 goto again;
566 }
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000567 ac--, av++;
Damien Millerf4614452001-07-14 12:18:10 +1000568 }
569
Damien Miller95def091999-11-25 00:26:21 +1100570 /* Check that we got a host name. */
571 if (!host)
572 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000573
Damien Millercb5e44a2000-09-29 12:12:36 +1100574 SSLeay_add_all_algorithms();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100575 ERR_load_crypto_strings();
Damien Millercb5e44a2000-09-29 12:12:36 +1100576
Damien Miller95def091999-11-25 00:26:21 +1100577 /* Initialize the command to execute on remote host. */
578 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000579
Damien Miller5428f641999-11-25 11:54:57 +1100580 /*
581 * Save the command to execute on the remote host in a buffer. There
582 * is no limit on the length of the command, except by the maximum
583 * packet size. Also sets the tty flag if there is no command.
584 */
Damien Millerf4614452001-07-14 12:18:10 +1000585 if (!ac) {
Damien Miller95def091999-11-25 00:26:21 +1100586 /* No command specified - execute shell on a tty. */
587 tty_flag = 1;
Damien Miller832562e2001-01-30 09:30:01 +1100588 if (subsystem_flag) {
Damien Millerf4614452001-07-14 12:18:10 +1000589 fprintf(stderr,
590 "You must specify a subsystem to invoke.\n");
Damien Miller832562e2001-01-30 09:30:01 +1100591 usage();
592 }
Damien Miller95def091999-11-25 00:26:21 +1100593 } else {
Damien Millerf4614452001-07-14 12:18:10 +1000594 /* A command has been specified. Store it into the buffer. */
595 for (i = 0; i < ac; i++) {
596 if (i)
Damien Miller95def091999-11-25 00:26:21 +1100597 buffer_append(&command, " ", 1);
598 buffer_append(&command, av[i], strlen(av[i]));
599 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000600 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000601
Damien Miller95def091999-11-25 00:26:21 +1100602 /* Cannot fork to background if no command. */
Darren Tuckerd6173c02008-06-13 04:52:53 +1000603 if (fork_after_authentication_flag && buffer_len(&command) == 0 &&
604 !no_shell_flag)
605 fatal("Cannot fork into background without a command "
606 "to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000607
Damien Miller95def091999-11-25 00:26:21 +1100608 /* Allocate a tty by default if no command specified. */
609 if (buffer_len(&command) == 0)
610 tty_flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000611
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000612 /* Force no tty */
Ben Lindstromc72745a2000-12-02 19:03:54 +0000613 if (no_tty_flag)
614 tty_flag = 0;
Damien Miller95def091999-11-25 00:26:21 +1100615 /* Do not allocate a tty if stdin is not a tty. */
Damien Millerddee5752005-05-26 12:05:05 +1000616 if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100617 if (tty_flag)
Darren Tuckerd6173c02008-06-13 04:52:53 +1000618 logit("Pseudo-terminal will not be allocated because "
619 "stdin is not a terminal.");
Damien Miller95def091999-11-25 00:26:21 +1100620 tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000621 }
Damien Miller1383bd82000-04-06 12:32:37 +1000622
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000623 /*
624 * Initialize "log" output. Since we are the client all output
625 * actually goes to stderr.
626 */
Darren Tucker72efd742009-06-21 17:48:00 +1000627 log_init(argv0,
Darren Tuckerd6173c02008-06-13 04:52:53 +1000628 options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
Damien Millere272a5b2008-11-03 19:22:37 +1100629 SYSLOG_FACILITY_USER, !use_syslog);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000630
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000631 /*
632 * Read per-user configuration file. Ignore the system wide config
633 * file if the user specifies a config file on the command line.
634 */
635 if (config != NULL) {
Damien Miller914420f2004-04-20 20:14:07 +1000636 if (!read_config_file(config, host, &options, 0))
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000637 fatal("Can't open user config file %.100s: "
638 "%.100s", config, strerror(errno));
Damien Miller80163902007-01-05 16:30:16 +1100639 } else {
Darren Tucker199b1342009-07-06 07:16:56 +1000640 r = snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir,
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000641 _PATH_SSH_USER_CONFFILE);
Darren Tucker199b1342009-07-06 07:16:56 +1000642 if (r > 0 && (size_t)r < sizeof(buf))
643 (void)read_config_file(buf, host, &options, 1);
Ben Lindstrom83f07d12001-10-03 17:22:29 +0000644
645 /* Read systemwide configuration file after use config. */
Darren Tuckerfc959702004-07-17 16:12:08 +1000646 (void)read_config_file(_PATH_HOST_CONFIG_FILE, host,
Damien Miller57a44762004-04-20 20:11:57 +1000647 &options, 0);
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000648 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000649
Damien Miller95def091999-11-25 00:26:21 +1100650 /* Fill configuration defaults. */
651 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000652
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000653 channel_set_af(options.address_family);
654
Damien Miller95def091999-11-25 00:26:21 +1100655 /* reinit */
Darren Tucker72efd742009-06-21 17:48:00 +1000656 log_init(argv0, options.log_level, SYSLOG_FACILITY_USER, !use_syslog);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000657
Damien Miller60bc5172001-03-19 09:38:15 +1100658 seed_rng();
659
Damien Miller95def091999-11-25 00:26:21 +1100660 if (options.user == NULL)
661 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000662
Darren Tuckerf6b01b72008-06-13 04:56:37 +1000663 /* Get default port if port has not been set. */
664 if (options.port == 0) {
665 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
666 options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
667 }
668
669 if (options.local_command != NULL) {
670 char thishost[NI_MAXHOST];
671
672 if (gethostname(thishost, sizeof(thishost)) == -1)
673 fatal("gethostname: %s", strerror(errno));
674 snprintf(buf, sizeof(buf), "%d", options.port);
675 debug3("expanding LocalCommand: %s", options.local_command);
676 cp = options.local_command;
677 options.local_command = percent_expand(cp, "d", pw->pw_dir,
678 "h", options.hostname? options.hostname : host,
679 "l", thishost, "n", host, "r", options.user, "p", buf,
680 "u", pw->pw_name, (char *)NULL);
681 debug3("expanded LocalCommand: %s", options.local_command);
682 xfree(cp);
683 }
684
Damien Miller95def091999-11-25 00:26:21 +1100685 if (options.hostname != NULL)
686 host = options.hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000687
Darren Tucker3f521e22003-07-03 16:20:42 +1000688 /* force lowercase for hostkey matching */
689 if (options.host_key_alias != NULL) {
690 for (p = options.host_key_alias; *p; p++)
691 if (isupper(*p))
Damien Miller1d2b6702006-03-26 14:09:54 +1100692 *p = (char)tolower(*p);
Darren Tucker3f521e22003-07-03 16:20:42 +1000693 }
694
Damien Miller9f1e33a2003-02-24 11:57:32 +1100695 if (options.proxy_command != NULL &&
Darren Tucker32e42c72007-12-02 23:01:03 +1100696 strcmp(options.proxy_command, "none") == 0) {
697 xfree(options.proxy_command);
Damien Miller9f1e33a2003-02-24 11:57:32 +1100698 options.proxy_command = NULL;
Darren Tucker32e42c72007-12-02 23:01:03 +1100699 }
Damien Miller8f74c8f2005-06-26 08:56:03 +1000700 if (options.control_path != NULL &&
Darren Tucker32e42c72007-12-02 23:01:03 +1100701 strcmp(options.control_path, "none") == 0) {
702 xfree(options.control_path);
Damien Miller8f74c8f2005-06-26 08:56:03 +1000703 options.control_path = NULL;
Darren Tucker32e42c72007-12-02 23:01:03 +1100704 }
Damien Miller9f1e33a2003-02-24 11:57:32 +1100705
Damien Miller0e220db2004-06-15 10:34:08 +1000706 if (options.control_path != NULL) {
Damien Miller6b1d53c2006-03-31 23:13:21 +1100707 char thishost[NI_MAXHOST];
Damien Miller3ec54c72006-03-15 11:30:13 +1100708
Damien Miller6b1d53c2006-03-31 23:13:21 +1100709 if (gethostname(thishost, sizeof(thishost)) == -1)
Damien Miller3ec54c72006-03-15 11:30:13 +1100710 fatal("gethostname: %s", strerror(errno));
Damien Miller6476cad2005-06-16 13:18:34 +1000711 snprintf(buf, sizeof(buf), "%d", options.port);
712 cp = tilde_expand_filename(options.control_path,
713 original_real_uid);
Darren Tucker32e42c72007-12-02 23:01:03 +1100714 xfree(options.control_path);
Damien Miller6476cad2005-06-16 13:18:34 +1000715 options.control_path = percent_expand(cp, "p", buf, "h", host,
Damien Miller6b1d53c2006-03-31 23:13:21 +1100716 "r", options.user, "l", thishost, (char *)NULL);
Damien Miller6476cad2005-06-16 13:18:34 +1000717 xfree(cp);
Damien Miller0e220db2004-06-15 10:34:08 +1000718 }
Damien Millerb1cbfa22008-05-19 16:00:08 +1000719 if (muxclient_command != 0 && options.control_path == NULL)
Darren Tucker0814d312005-06-01 23:08:51 +1000720 fatal("No ControlPath specified for \"-O\" command");
Damien Millerd14b1e72005-06-16 13:19:41 +1000721 if (options.control_path != NULL)
Damien Millerb1cbfa22008-05-19 16:00:08 +1000722 muxclient(options.control_path);
Damien Miller0e220db2004-06-15 10:34:08 +1000723
Damien Miller67bd0622007-09-17 12:06:57 +1000724 timeout_ms = options.connection_timeout * 1000;
725
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000726 /* Open a connection to the remote host. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000727 if (ssh_connect(host, &hostaddr, options.port,
Damien Miller67bd0622007-09-17 12:06:57 +1000728 options.address_family, options.connection_attempts, &timeout_ms,
729 options.tcp_keep_alive,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000730#ifdef HAVE_CYGWIN
731 options.use_privileged_port,
732#else
Ben Lindstromf9c48842002-06-11 16:37:51 +0000733 original_effective_uid == 0 && options.use_privileged_port,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000734#endif
Ben Lindstromda394ca2002-06-12 16:11:12 +0000735 options.proxy_command) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100736 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000737
Damien Miller67bd0622007-09-17 12:06:57 +1000738 if (timeout_ms > 0)
739 debug3("timeout: %d ms remain after connect", timeout_ms);
740
Damien Miller5428f641999-11-25 11:54:57 +1100741 /*
742 * If we successfully made the connection, load the host private key
743 * in case we will need it later for combined rsa-rhosts
744 * authentication. This must be done before releasing extra
745 * privileges, because the file is only readable by root.
Ben Lindstrom9e5bb572002-06-06 19:58:27 +0000746 * If we cannot access the private keys, load the public keys
747 * instead and try to execute the ssh-keysign helper instead.
Damien Miller5428f641999-11-25 11:54:57 +1100748 */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000749 sensitive_data.nkeys = 0;
750 sensitive_data.keys = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000751 sensitive_data.external_keysign = 0;
Ben Lindstrom24157572002-06-12 16:09:39 +0000752 if (options.rhosts_rsa_authentication ||
753 options.hostbased_authentication) {
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000754 sensitive_data.nkeys = 3;
Damien Millerddd63ab2006-03-31 23:10:51 +1100755 sensitive_data.keys = xcalloc(sensitive_data.nkeys,
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000756 sizeof(Key));
Ben Lindstromf9c48842002-06-11 16:37:51 +0000757
758 PRIV_START;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000759 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
Darren Tucker232b76f2006-05-06 17:41:51 +1000760 _PATH_HOST_KEY_FILE, "", NULL, NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000761 sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
Darren Tucker232b76f2006-05-06 17:41:51 +1000762 _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000763 sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
Darren Tucker232b76f2006-05-06 17:41:51 +1000764 _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL);
Ben Lindstromf9c48842002-06-11 16:37:51 +0000765 PRIV_END;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000766
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +0000767 if (options.hostbased_authentication == 1 &&
768 sensitive_data.keys[0] == NULL &&
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000769 sensitive_data.keys[1] == NULL &&
770 sensitive_data.keys[2] == NULL) {
771 sensitive_data.keys[1] = key_load_public(
772 _PATH_HOST_DSA_KEY_FILE, NULL);
773 sensitive_data.keys[2] = key_load_public(
774 _PATH_HOST_RSA_KEY_FILE, NULL);
775 sensitive_data.external_keysign = 1;
776 }
Damien Miller95def091999-11-25 00:26:21 +1100777 }
Damien Miller5428f641999-11-25 11:54:57 +1100778 /*
779 * Get rid of any extra privileges that we may have. We will no
780 * longer need them. Also, extra privileges could make it very hard
781 * to read identity files and other non-world-readable files from the
782 * user's home directory if it happens to be on a NFS volume where
783 * root is mapped to nobody.
784 */
Darren Tucker25f60a72004-08-15 17:23:34 +1000785 if (original_effective_uid == 0) {
786 PRIV_START;
787 permanently_set_uid(pw);
788 }
Damien Miller95def091999-11-25 00:26:21 +1100789
Damien Miller5428f641999-11-25 11:54:57 +1100790 /*
791 * Now that we are back to our own permissions, create ~/.ssh
Damien Miller788f2122005-11-05 15:14:59 +1100792 * directory if it doesn't already exist.
Damien Miller5428f641999-11-25 11:54:57 +1100793 */
Darren Tucker199b1342009-07-06 07:16:56 +1000794 r = snprintf(buf, sizeof buf, "%s%s%s", pw->pw_dir,
Darren Tuckerd6173c02008-06-13 04:52:53 +1000795 strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
Darren Tucker199b1342009-07-06 07:16:56 +1000796 if (r > 0 && (size_t)r < sizeof(buf) && stat(buf, &st) < 0)
Damien Millerbe484b52000-07-15 14:14:16 +1000797 if (mkdir(buf, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100798 error("Could not create directory '%.200s'.", buf);
799
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000800 /* load options.identity_files */
801 load_public_identity_files();
802
803 /* Expand ~ in known host file names. */
804 /* XXX mem-leaks: */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100805 options.system_hostfile =
806 tilde_expand_filename(options.system_hostfile, original_real_uid);
807 options.user_hostfile =
808 tilde_expand_filename(options.user_hostfile, original_real_uid);
809 options.system_hostfile2 =
810 tilde_expand_filename(options.system_hostfile2, original_real_uid);
811 options.user_hostfile2 =
812 tilde_expand_filename(options.user_hostfile2, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100813
Damien Miller07cd5892001-11-12 10:52:03 +1100814 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
815
Darren Tuckerd6173c02008-06-13 04:52:53 +1000816 /* Log into the remote system. Never returns if the login fails. */
Damien Miller67bd0622007-09-17 12:06:57 +1000817 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr,
818 pw, timeout_ms);
Damien Miller95def091999-11-25 00:26:21 +1100819
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000820 /* We no longer need the private host keys. Clear them now. */
821 if (sensitive_data.nkeys != 0) {
822 for (i = 0; i < sensitive_data.nkeys; i++) {
823 if (sensitive_data.keys[i] != NULL) {
824 /* Destroys contents safely */
825 debug3("clear hostkey %d", i);
826 key_free(sensitive_data.keys[i]);
827 sensitive_data.keys[i] = NULL;
828 }
829 }
830 xfree(sensitive_data.keys);
831 }
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +0000832 for (i = 0; i < options.num_identity_files; i++) {
833 if (options.identity_files[i]) {
834 xfree(options.identity_files[i]);
835 options.identity_files[i] = NULL;
836 }
837 if (options.identity_keys[i]) {
838 key_free(options.identity_keys[i]);
839 options.identity_keys[i] = NULL;
840 }
841 }
Damien Miller95def091999-11-25 00:26:21 +1100842
Damien Miller1383bd82000-04-06 12:32:37 +1000843 exit_status = compat20 ? ssh_session2() : ssh_session();
844 packet_close();
Damien Miller8c4e18a2002-09-19 12:05:02 +1000845
Damien Millerb1cbfa22008-05-19 16:00:08 +1000846 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +1000847 unlink(options.control_path);
848
Damien Miller8c4e18a2002-09-19 12:05:02 +1000849 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100850 * Send SIGHUP to proxy command if used. We don't wait() in
Damien Miller8c4e18a2002-09-19 12:05:02 +1000851 * case it hangs and instead rely on init to reap the child
852 */
853 if (proxy_command_pid > 1)
854 kill(proxy_command_pid, SIGHUP);
855
Damien Miller1383bd82000-04-06 12:32:37 +1000856 return exit_status;
857}
858
Darren Tucker9f407c42008-06-13 04:50:27 +1000859/* Callback for remote forward global requests */
860static void
861ssh_confirm_remote_forward(int type, u_int32_t seq, void *ctxt)
862{
863 Forward *rfwd = (Forward *)ctxt;
864
Damien Miller4bf648f2009-02-14 16:28:21 +1100865 /* XXX verbose() on failure? */
Darren Tucker9f407c42008-06-13 04:50:27 +1000866 debug("remote forward %s for: listen %d, connect %s:%d",
867 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
868 rfwd->listen_port, rfwd->connect_host, rfwd->connect_port);
Damien Miller4bf648f2009-02-14 16:28:21 +1100869 if (type == SSH2_MSG_REQUEST_SUCCESS && rfwd->listen_port == 0) {
870 logit("Allocated port %u for remote forward to %s:%d",
871 packet_get_int(),
872 rfwd->connect_host, rfwd->connect_port);
873 }
874
Darren Tucker9f407c42008-06-13 04:50:27 +1000875 if (type == SSH2_MSG_REQUEST_FAILURE) {
876 if (options.exit_on_forward_failure)
877 fatal("Error: remote port forwarding failed for "
878 "listen port %d", rfwd->listen_port);
879 else
880 logit("Warning: remote port forwarding failed for "
881 "listen port %d", rfwd->listen_port);
882 }
Darren Tucker9a2a6092008-07-04 12:53:50 +1000883 if (++remote_forward_confirms_received == options.num_remote_forwards) {
Darren Tucker9f407c42008-06-13 04:50:27 +1000884 debug("All remote forwarding requests processed");
Darren Tucker9a2a6092008-07-04 12:53:50 +1000885 if (fork_after_authentication_flag) {
886 fork_after_authentication_flag = 0;
887 if (daemon(1, 1) < 0)
888 fatal("daemon() failed: %.200s",
889 strerror(errno));
890 }
891 }
Darren Tucker9f407c42008-06-13 04:50:27 +1000892}
893
Ben Lindstrombba81212001-06-25 05:01:22 +0000894static void
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100895client_cleanup_stdio_fwd(int id, void *arg)
896{
897 debug("stdio forwarding: done");
898 cleanup_exit(0);
899}
900
901static int
902client_setup_stdio_fwd(const char *host_to_connect, u_short port_to_connect)
903{
904 Channel *c;
905
906 debug3("client_setup_stdio_fwd %s:%d", host_to_connect,
907 port_to_connect);
908 if ((c = channel_connect_stdio_fwd(host_to_connect, port_to_connect))
909 == NULL)
910 return 0;
911 channel_register_cleanup(c->self, client_cleanup_stdio_fwd, 0);
912 return 1;
913}
914
915static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100916ssh_init_forwarding(void)
917{
Kevin Steves12057502001-02-05 14:54:34 +0000918 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100919 int i;
Kevin Steves12057502001-02-05 14:54:34 +0000920
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100921 if (stdio_forward_host != NULL) {
922 if (!compat20) {
923 fatal("stdio forwarding require Protocol 2");
924 }
925 if (!client_setup_stdio_fwd(stdio_forward_host,
926 stdio_forward_port))
927 fatal("Failed to connect in stdio forward mode.");
928 }
929
Damien Miller0bc1bd82000-11-13 22:57:25 +1100930 /* Initiate local TCP/IP port forwardings. */
931 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100932 debug("Local connections to %.200s:%d forwarded to remote "
933 "address %.200s:%d",
Darren Tucker47eede72005-03-14 23:08:12 +1100934 (options.local_forwards[i].listen_host == NULL) ?
935 (options.gateway_ports ? "*" : "LOCALHOST") :
Damien Millerf91ee4c2005-03-01 21:24:33 +1100936 options.local_forwards[i].listen_host,
937 options.local_forwards[i].listen_port,
938 options.local_forwards[i].connect_host,
939 options.local_forwards[i].connect_port);
Damien Millerb16461c2002-01-22 23:29:22 +1100940 success += channel_setup_local_fwd_listener(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100941 options.local_forwards[i].listen_host,
942 options.local_forwards[i].listen_port,
943 options.local_forwards[i].connect_host,
944 options.local_forwards[i].connect_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100945 options.gateway_ports);
946 }
Darren Tuckere7d4b192006-07-12 22:17:10 +1000947 if (i > 0 && success != i && options.exit_on_forward_failure)
948 fatal("Could not request local forwarding.");
Kevin Steves12057502001-02-05 14:54:34 +0000949 if (i > 0 && success == 0)
950 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100951
952 /* Initiate remote TCP/IP port forwardings. */
953 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100954 debug("Remote connections from %.200s:%d forwarded to "
955 "local address %.200s:%d",
Damien Miller46d38de2005-07-17 17:02:09 +1000956 (options.remote_forwards[i].listen_host == NULL) ?
Damien Milleraa3bb102005-11-05 15:12:59 +1100957 "LOCALHOST" : options.remote_forwards[i].listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100958 options.remote_forwards[i].listen_port,
959 options.remote_forwards[i].connect_host,
960 options.remote_forwards[i].connect_port);
Darren Tuckere7d4b192006-07-12 22:17:10 +1000961 if (channel_request_remote_forwarding(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100962 options.remote_forwards[i].listen_host,
963 options.remote_forwards[i].listen_port,
964 options.remote_forwards[i].connect_host,
Darren Tuckere7d4b192006-07-12 22:17:10 +1000965 options.remote_forwards[i].connect_port) < 0) {
966 if (options.exit_on_forward_failure)
967 fatal("Could not request remote forwarding.");
968 else
969 logit("Warning: Could not request remote "
970 "forwarding.");
971 }
Darren Tucker9f407c42008-06-13 04:50:27 +1000972 client_register_global_confirm(ssh_confirm_remote_forward,
973 &options.remote_forwards[i]);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100974 }
Damien Millerb3ce9fe2007-08-08 14:32:41 +1000975
976 /* Initiate tunnel forwarding. */
977 if (options.tun_open != SSH_TUNMODE_NO) {
978 if (client_request_tun_fwd(options.tun_open,
979 options.tun_local, options.tun_remote) == -1) {
980 if (options.exit_on_forward_failure)
981 fatal("Could not request tunnel forwarding.");
982 else
983 error("Could not request tunnel forwarding.");
984 }
985 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100986}
987
Ben Lindstrombba81212001-06-25 05:01:22 +0000988static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100989check_agent_present(void)
990{
991 if (options.forward_agent) {
Damien Miller788f2122005-11-05 15:14:59 +1100992 /* Clear agent forwarding if we don't have an agent. */
Damien Miller789e95d2002-09-12 09:52:46 +1000993 if (!ssh_agent_present())
Damien Miller0bc1bd82000-11-13 22:57:25 +1100994 options.forward_agent = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100995 }
996}
997
Ben Lindstrombba81212001-06-25 05:01:22 +0000998static int
Damien Miller1383bd82000-04-06 12:32:37 +1000999ssh_session(void)
1000{
1001 int type;
Damien Miller1383bd82000-04-06 12:32:37 +10001002 int interactive = 0;
1003 int have_tty = 0;
1004 struct winsize ws;
Damien Miller1383bd82000-04-06 12:32:37 +10001005 char *cp;
Damien Miller17e7ed02005-06-17 12:54:33 +10001006 const char *display;
Damien Miller1383bd82000-04-06 12:32:37 +10001007
Damien Miller95def091999-11-25 00:26:21 +11001008 /* Enable compression if requested. */
1009 if (options.compression) {
Darren Tuckerd6173c02008-06-13 04:52:53 +10001010 debug("Requesting compression at level %d.",
1011 options.compression_level);
Damien Miller95def091999-11-25 00:26:21 +11001012
Darren Tuckerd6173c02008-06-13 04:52:53 +10001013 if (options.compression_level < 1 ||
1014 options.compression_level > 9)
1015 fatal("Compression level must be from 1 (fast) to "
1016 "9 (slow, best).");
Damien Miller95def091999-11-25 00:26:21 +11001017
1018 /* Send the request. */
1019 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
1020 packet_put_int(options.compression_level);
1021 packet_send();
1022 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +11001023 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +11001024 if (type == SSH_SMSG_SUCCESS)
1025 packet_start_compression(options.compression_level);
1026 else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +10001027 logit("Warning: Remote host refused compression.");
Damien Miller95def091999-11-25 00:26:21 +11001028 else
Darren Tuckerd6173c02008-06-13 04:52:53 +10001029 packet_disconnect("Protocol error waiting for "
1030 "compression response.");
Damien Miller95def091999-11-25 00:26:21 +11001031 }
1032 /* Allocate a pseudo tty if appropriate. */
1033 if (tty_flag) {
1034 debug("Requesting pty.");
1035
1036 /* Start the packet. */
1037 packet_start(SSH_CMSG_REQUEST_PTY);
1038
1039 /* Store TERM in the packet. There is no limit on the
1040 length of the string. */
1041 cp = getenv("TERM");
1042 if (!cp)
1043 cp = "";
Ben Lindstrom664408d2001-06-09 01:42:01 +00001044 packet_put_cstring(cp);
Damien Miller95def091999-11-25 00:26:21 +11001045
1046 /* Store window size in the packet. */
1047 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
1048 memset(&ws, 0, sizeof(ws));
Damien Miller71a73672006-03-26 14:04:36 +11001049 packet_put_int((u_int)ws.ws_row);
1050 packet_put_int((u_int)ws.ws_col);
1051 packet_put_int((u_int)ws.ws_xpixel);
1052 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller95def091999-11-25 00:26:21 +11001053
1054 /* Store tty modes in the packet. */
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001055 tty_make_modes(fileno(stdin), NULL);
Damien Miller95def091999-11-25 00:26:21 +11001056
1057 /* Send the packet, and wait for it to leave. */
1058 packet_send();
1059 packet_write_wait();
1060
1061 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +11001062 type = packet_read();
Damien Miller450a7a12000-03-26 13:04:51 +10001063 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +11001064 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001065 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +10001066 } else if (type == SSH_SMSG_FAILURE)
Darren Tuckerd6173c02008-06-13 04:52:53 +10001067 logit("Warning: Remote host failed or refused to "
1068 "allocate a pseudo tty.");
Damien Miller95def091999-11-25 00:26:21 +11001069 else
Darren Tuckerd6173c02008-06-13 04:52:53 +10001070 packet_disconnect("Protocol error waiting for pty "
1071 "request response.");
Damien Miller95def091999-11-25 00:26:21 +11001072 }
1073 /* Request X11 forwarding if enabled and DISPLAY is set. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001074 display = getenv("DISPLAY");
1075 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001076 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001077 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001078 client_x11_get_proto(display, options.xauth_location,
1079 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001080 /* Request forwarding with authentication spoofing. */
Darren Tuckerd6173c02008-06-13 04:52:53 +10001081 debug("Requesting X11 forwarding with authentication "
1082 "spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +10001083 x11_request_forwarding_with_spoofing(0, display, proto, data);
Damien Miller95def091999-11-25 00:26:21 +11001084
1085 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +11001086 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +11001087 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +11001088 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001089 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller996acd22003-04-09 20:59:48 +10001090 logit("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +10001091 } else {
Darren Tuckerd6173c02008-06-13 04:52:53 +10001092 packet_disconnect("Protocol error waiting for X11 "
1093 "forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +10001094 }
Damien Miller95def091999-11-25 00:26:21 +11001095 }
1096 /* Tell the packet module whether this is an interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001097 packet_set_interactive(interactive);
Damien Miller95def091999-11-25 00:26:21 +11001098
1099 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001100 check_agent_present();
1101
Damien Miller95def091999-11-25 00:26:21 +11001102 if (options.forward_agent) {
1103 debug("Requesting authentication agent forwarding.");
1104 auth_request_forwarding();
1105
1106 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +11001107 type = packet_read();
Damien Miller48b03fc2002-01-22 23:11:40 +11001108 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +11001109 if (type != SSH_SMSG_SUCCESS)
Damien Miller996acd22003-04-09 20:59:48 +10001110 logit("Warning: Remote host denied authentication agent forwarding.");
Damien Miller95def091999-11-25 00:26:21 +11001111 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001112
Damien Miller0bc1bd82000-11-13 22:57:25 +11001113 /* Initiate port forwardings. */
1114 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001115
Darren Tuckerb776c852007-12-02 23:06:35 +11001116 /* Execute a local command */
1117 if (options.local_command != NULL &&
1118 options.permit_local_command)
1119 ssh_local_cmd(options.local_command);
1120
Darren Tucker9a2a6092008-07-04 12:53:50 +10001121 /*
1122 * If requested and we are not interested in replies to remote
1123 * forwarding requests, then let ssh continue in the background.
1124 */
1125 if (fork_after_authentication_flag &&
1126 (!options.exit_on_forward_failure ||
1127 options.num_remote_forwards == 0)) {
1128 fork_after_authentication_flag = 0;
Damien Miller5428f641999-11-25 11:54:57 +11001129 if (daemon(1, 1) < 0)
1130 fatal("daemon() failed: %.200s", strerror(errno));
Darren Tucker9a2a6092008-07-04 12:53:50 +10001131 }
Damien Miller5428f641999-11-25 11:54:57 +11001132
1133 /*
1134 * If a command was specified on the command line, execute the
1135 * command now. Otherwise request the server to start a shell.
1136 */
Damien Miller95def091999-11-25 00:26:21 +11001137 if (buffer_len(&command) > 0) {
1138 int len = buffer_len(&command);
1139 if (len > 900)
1140 len = 900;
Darren Tuckerd6173c02008-06-13 04:52:53 +10001141 debug("Sending command: %.*s", len,
1142 (u_char *)buffer_ptr(&command));
Damien Miller95def091999-11-25 00:26:21 +11001143 packet_start(SSH_CMSG_EXEC_CMD);
1144 packet_put_string(buffer_ptr(&command), buffer_len(&command));
1145 packet_send();
1146 packet_write_wait();
1147 } else {
1148 debug("Requesting shell.");
1149 packet_start(SSH_CMSG_EXEC_SHELL);
1150 packet_send();
1151 packet_write_wait();
1152 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001153
Damien Miller95def091999-11-25 00:26:21 +11001154 /* Enter the interactive session. */
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001155 return client_loop(have_tty, tty_flag ?
1156 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
Damien Miller1383bd82000-04-06 12:32:37 +10001157}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001158
Ben Lindstromf558cf62001-09-20 23:13:49 +00001159/* request pty/x11/agent/tcpfwd/shell for channel */
Ben Lindstrombba81212001-06-25 05:01:22 +00001160static void
Ben Lindstromf558cf62001-09-20 23:13:49 +00001161ssh_session2_setup(int id, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +10001162{
Damien Miller3756dce2004-06-18 01:17:29 +10001163 extern char **environ;
Damien Miller17e7ed02005-06-17 12:54:33 +10001164 const char *display;
Damien Miller0e220db2004-06-15 10:34:08 +10001165 int interactive = tty_flag;
Damien Miller17e7ed02005-06-17 12:54:33 +10001166
Damien Miller46d38de2005-07-17 17:02:09 +10001167 display = getenv("DISPLAY");
Damien Miller17e7ed02005-06-17 12:54:33 +10001168 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001169 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001170 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001171 client_x11_get_proto(display, options.xauth_location,
1172 options.forward_x11_trusted, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001173 /* Request forwarding with authentication spoofing. */
Darren Tuckerd6173c02008-06-13 04:52:53 +10001174 debug("Requesting X11 forwarding with authentication "
1175 "spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +10001176 x11_request_forwarding_with_spoofing(id, display, proto, data);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001177 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001178 /* XXX wait for reply */
1179 }
1180
Damien Miller0bc1bd82000-11-13 22:57:25 +11001181 check_agent_present();
1182 if (options.forward_agent) {
1183 debug("Requesting authentication agent forwarding.");
1184 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1185 packet_send();
1186 }
1187
Damien Miller0e220db2004-06-15 10:34:08 +10001188 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
Damien Miller5771ed72008-05-19 15:35:33 +10001189 NULL, fileno(stdin), &command, environ);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001190
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001191 packet_set_interactive(interactive);
Damien Miller1383bd82000-04-06 12:32:37 +10001192}
1193
Ben Lindstromf558cf62001-09-20 23:13:49 +00001194/* open new channel for a session */
Ben Lindstrombba81212001-06-25 05:01:22 +00001195static int
Ben Lindstromf558cf62001-09-20 23:13:49 +00001196ssh_session2_open(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001197{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001198 Channel *c;
1199 int window, packetmax, in, out, err;
Damien Millerad833b32000-08-23 10:46:23 +10001200
Damien Millercaf6dd62000-08-29 11:33:50 +11001201 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001202 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001203 } else {
1204 in = dup(STDIN_FILENO);
1205 }
1206 out = dup(STDOUT_FILENO);
1207 err = dup(STDERR_FILENO);
1208
1209 if (in < 0 || out < 0 || err < 0)
1210 fatal("dup() in/out/err failed");
1211
Damien Miller69b69aa2000-10-28 14:19:58 +11001212 /* enable nonblocking unless tty */
1213 if (!isatty(in))
1214 set_nonblock(in);
1215 if (!isatty(out))
1216 set_nonblock(out);
1217 if (!isatty(err))
1218 set_nonblock(err);
1219
Damien Millere4340be2000-09-16 13:29:08 +11001220 window = CHAN_SES_WINDOW_DEFAULT;
1221 packetmax = CHAN_SES_PACKET_DEFAULT;
Damien Miller19a59452002-02-19 15:20:57 +11001222 if (tty_flag) {
1223 window >>= 1;
1224 packetmax >>= 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001225 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001226 c = channel_new(
Damien Miller1383bd82000-04-06 12:32:37 +10001227 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001228 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001229 "client-session", /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001230
Ben Lindstromf558cf62001-09-20 23:13:49 +00001231 debug3("ssh_session2_open: channel_new: %d", c->self);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001232
Ben Lindstrom5ec26452001-06-09 00:18:51 +00001233 channel_send_open(c->self);
Ben Lindstromf558cf62001-09-20 23:13:49 +00001234 if (!no_shell_flag)
Damien Millerb84886b2008-05-19 15:05:07 +10001235 channel_register_open_confirm(c->self,
1236 ssh_session2_setup, NULL);
Damien Miller1383bd82000-04-06 12:32:37 +10001237
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001238 return c->self;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001239}
1240
Ben Lindstrombba81212001-06-25 05:01:22 +00001241static int
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001242ssh_session2(void)
1243{
Ben Lindstromf558cf62001-09-20 23:13:49 +00001244 int id = -1;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001245
1246 /* XXX should be pre-session */
1247 ssh_init_forwarding();
1248
Ben Lindstromf558cf62001-09-20 23:13:49 +00001249 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1250 id = ssh_session2_open();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001251
Darren Tucker8901fa92008-06-11 09:34:01 +10001252 /* If we don't expect to open a new session, then disallow it */
Damien Miller456e6f02008-11-03 19:20:10 +11001253 if (options.control_master == SSHCTL_MASTER_NO &&
1254 (datafellows & SSH_NEW_OPENSSH)) {
Darren Tucker8901fa92008-06-11 09:34:01 +10001255 debug("Requesting no-more-sessions@openssh.com");
1256 packet_start(SSH2_MSG_GLOBAL_REQUEST);
1257 packet_put_cstring("no-more-sessions@openssh.com");
1258 packet_put_char(0);
1259 packet_send();
1260 }
1261
Damien Millerd27b9472005-12-13 19:29:02 +11001262 /* Execute a local command */
1263 if (options.local_command != NULL &&
1264 options.permit_local_command)
1265 ssh_local_cmd(options.local_command);
1266
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001267 /* Start listening for multiplex clients */
Damien Millerb1cbfa22008-05-19 16:00:08 +10001268 muxserver_listen();
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001269
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001270 /* If requested, let ssh continue in the background. */
Darren Tucker9a2a6092008-07-04 12:53:50 +10001271 if (fork_after_authentication_flag) {
1272 fork_after_authentication_flag = 0;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001273 if (daemon(1, 1) < 0)
1274 fatal("daemon() failed: %.200s", strerror(errno));
Darren Tucker9a2a6092008-07-04 12:53:50 +10001275 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001276
Darren Tuckerf1de4e52010-01-08 16:54:59 +11001277 if (options.use_roaming)
1278 request_roaming();
1279
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001280 return client_loop(tty_flag, tty_flag ?
1281 options.escape_char : SSH_ESCAPECHAR_NONE, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001282}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001283
Ben Lindstrombba81212001-06-25 05:01:22 +00001284static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001285load_public_identity_files(void)
1286{
Damien Miller6b1d53c2006-03-31 23:13:21 +11001287 char *filename, *cp, thishost[NI_MAXHOST];
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001288 char *pwdir = NULL, *pwname = NULL;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001289 int i = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001290 Key *public;
Damien Miller6b1d53c2006-03-31 23:13:21 +11001291 struct passwd *pw;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001292#ifdef SMARTCARD
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001293 Key **keys;
1294
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001295 if (options.smartcard_device != NULL &&
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001296 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
Damien Miller4dec5d72006-08-05 11:38:40 +10001297 (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001298 int count = 0;
1299 for (i = 0; keys[i] != NULL; i++) {
1300 count++;
Darren Tuckerd6173c02008-06-13 04:52:53 +10001301 memmove(&options.identity_files[1],
1302 &options.identity_files[0],
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001303 sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
Darren Tuckerd6173c02008-06-13 04:52:53 +10001304 memmove(&options.identity_keys[1],
1305 &options.identity_keys[0],
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001306 sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1307 options.num_identity_files++;
1308 options.identity_keys[0] = keys[i];
Damien Miller56a0bb02003-06-18 20:28:40 +10001309 options.identity_files[0] = sc_get_key_label(keys[i]);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001310 }
Ben Lindstrom4f054602002-03-26 03:23:00 +00001311 if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1312 options.num_identity_files = SSH_MAX_IDENTITY_FILES;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001313 i = count;
1314 xfree(keys);
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001315 }
Ben Lindstromffce1472001-08-06 21:57:31 +00001316#endif /* SMARTCARD */
Damien Miller6b1d53c2006-03-31 23:13:21 +11001317 if ((pw = getpwuid(original_real_uid)) == NULL)
1318 fatal("load_public_identity_files: getpwuid failed");
Darren Tuckere143f062007-12-02 23:21:16 +11001319 pwname = xstrdup(pw->pw_name);
1320 pwdir = xstrdup(pw->pw_dir);
Damien Miller6b1d53c2006-03-31 23:13:21 +11001321 if (gethostname(thishost, sizeof(thishost)) == -1)
1322 fatal("load_public_identity_files: gethostname: %s",
1323 strerror(errno));
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001324 for (; i < options.num_identity_files; i++) {
Damien Miller6b1d53c2006-03-31 23:13:21 +11001325 cp = tilde_expand_filename(options.identity_files[i],
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001326 original_real_uid);
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001327 filename = percent_expand(cp, "d", pwdir,
1328 "u", pwname, "l", thishost, "h", host,
Damien Miller6b1d53c2006-03-31 23:13:21 +11001329 "r", options.user, (char *)NULL);
1330 xfree(cp);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001331 public = key_load_public(filename, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001332 debug("identity file %s type %d", filename,
1333 public ? public->type : -1);
1334 xfree(options.identity_files[i]);
1335 options.identity_files[i] = filename;
1336 options.identity_keys[i] = public;
1337 }
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001338 bzero(pwname, strlen(pwname));
Darren Tuckere143f062007-12-02 23:21:16 +11001339 xfree(pwname);
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001340 bzero(pwdir, strlen(pwdir));
Darren Tuckere143f062007-12-02 23:21:16 +11001341 xfree(pwdir);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001342}