blob: e6073af7b5d3eb4fe9e976b5d0b363487835e2b9 [file] [log] [blame]
Damien Miller1f25ab42010-07-16 13:56:23 +10001/* $OpenBSD: ssh.c,v 1.342 2010/07/12 22:38:52 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>
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"
Damien Millerb96c4412010-07-02 13:34:24 +100082#include "canohost.h"
Damien Miller1383bd82000-04-06 12:32:37 +100083#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000084#include "cipher.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000085#include "packet.h"
86#include "buffer.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000087#include "channels.h"
Damien Millereba71ba2000-04-29 23:57:08 +100088#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100089#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100090#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000091#include "pathnames.h"
Damien Miller0e220db2004-06-15 10:34:08 +100092#include "dispatch.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +000093#include "clientloop.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000094#include "log.h"
95#include "readconf.h"
96#include "sshconnect.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000097#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000098#include "kex.h"
99#include "mac.h"
Darren Tucker06f2bd82004-05-13 16:06:46 +1000100#include "sshpty.h"
Darren Tucker46bc0752004-05-02 22:11:30 +1000101#include "match.h"
Damien Miller0e220db2004-06-15 10:34:08 +1000102#include "msg.h"
Darren Tucker25f60a72004-08-15 17:23:34 +1000103#include "uidswap.h"
Darren Tuckerf1de4e52010-01-08 16:54:59 +1100104#include "roaming.h"
Damien Millerb7576772006-07-10 20:23:39 +1000105#include "version.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106
Damien Miller7ea845e2010-02-12 09:21:02 +1100107#ifdef ENABLE_PKCS11
108#include "ssh-pkcs11.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +0000109#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000110
Damien Miller3f905871999-11-15 17:10:57 +1100111extern char *__progname;
Damien Miller3f905871999-11-15 17:10:57 +1100112
Darren Tuckerd6173c02008-06-13 04:52:53 +1000113/* Flag indicating whether debug mode is on. May be set on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114int debug_flag = 0;
115
Damien Miller78928792000-04-12 20:17:38 +1000116/* Flag indicating whether a tty should be allocated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117int tty_flag = 0;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000118int no_tty_flag = 0;
119int force_tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120
Damien Miller1383bd82000-04-06 12:32:37 +1000121/* don't exec a shell */
122int no_shell_flag = 0;
Damien Miller1383bd82000-04-06 12:32:37 +1000123
Damien Miller5428f641999-11-25 11:54:57 +1100124/*
125 * Flag indicating that nothing should be read from stdin. This can be set
126 * on the command line.
127 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128int stdin_null_flag = 0;
129
Damien Miller5428f641999-11-25 11:54:57 +1100130/*
131 * Flag indicating that ssh should fork after authentication. This is useful
Ben Lindstromf666fec2002-06-06 19:51:58 +0000132 * so that the passphrase can be entered manually, and then ssh goes to the
Damien Miller5428f641999-11-25 11:54:57 +1100133 * background.
134 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135int fork_after_authentication_flag = 0;
136
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100137/* forward stdio to remote host and port */
138char *stdio_forward_host = NULL;
139int stdio_forward_port = 0;
140
Damien Miller5428f641999-11-25 11:54:57 +1100141/*
142 * General data structure for command line options and options configurable
143 * in configuration files. See readconf.h.
144 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000145Options options;
146
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000147/* optional user configfile */
148char *config = NULL;
149
Damien Miller5428f641999-11-25 11:54:57 +1100150/*
151 * Name of the host we are connecting to. This is the name given on the
152 * command line, or the HostName specified for the user-supplied name in a
153 * configuration file.
154 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155char *host;
156
157/* socket address the host resolves to */
Damien Miller34132e52000-01-14 15:45:46 +1100158struct sockaddr_storage hostaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000159
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000160/* Private host keys. */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000161Sensitive sensitive_data;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000162
163/* Original real UID. */
164uid_t original_real_uid;
Ben Lindstromf9c48842002-06-11 16:37:51 +0000165uid_t original_effective_uid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000166
Damien Miller1383bd82000-04-06 12:32:37 +1000167/* command to be executed */
168Buffer command;
169
Damien Miller832562e2001-01-30 09:30:01 +1100170/* Should we execute a command or invoke a subsystem? */
171int subsystem_flag = 0;
172
Damien Miller2797f7f2002-04-23 21:09:44 +1000173/* # of replies received for global requests */
Darren Tucker9f407c42008-06-13 04:50:27 +1000174static int remote_forward_confirms_received = 0;
Damien Miller2797f7f2002-04-23 21:09:44 +1000175
Damien Miller8c4e18a2002-09-19 12:05:02 +1000176/* pid of proxycommand child process */
177pid_t proxy_command_pid = 0;
178
Damien Millerb1cbfa22008-05-19 16:00:08 +1000179/* mux.c */
180extern int muxserver_sock;
181extern u_int muxclient_command;
Damien Miller0e220db2004-06-15 10:34:08 +1000182
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,
Damien Millerc13c3ee2008-11-03 19:23:28 +1100189"usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-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 Miller048dc932010-02-12 09:22:04 +1100191" [-I pkcs11] [-i identity_file]\n"
192" [-L [bind_address:]port:host:hostport]\n"
Damien Millerf91ee4c2005-03-01 21:24:33 +1100193" [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
Damien Miller02faece2005-03-02 12:04:32 +1100194" [-R [bind_address:]port:host:hostport] [-S ctl_path]\n"
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100195" [-W host:port] [-w local_tun[:remote_tun]]\n"
196" [user@]hostname [command]\n"
Damien Miller50955102004-03-22 09:34:58 +1100197 );
Darren Tuckere9a9b712005-12-20 16:15:51 +1100198 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199}
200
Ben Lindstrombba81212001-06-25 05:01:22 +0000201static int ssh_session(void);
202static int ssh_session2(void);
203static void load_public_identity_files(void);
Damien Millerb1cbfa22008-05-19 16:00:08 +1000204
205/* from muxclient.c */
206void muxclient(const char *);
207void muxserver_listen(void);
Damien Miller1383bd82000-04-06 12:32:37 +1000208
Damien Miller95def091999-11-25 00:26:21 +1100209/*
210 * Main program for the ssh client.
211 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000212int
213main(int ac, char **av)
214{
Darren Tucker199b1342009-07-06 07:16:56 +1000215 int i, r, opt, exit_status, use_syslog;
216 char *p, *cp, *line, *argv0, buf[MAXPATHLEN];
Damien Miller95def091999-11-25 00:26:21 +1100217 struct stat st;
Ben Lindstrom086cf212001-03-05 05:56:40 +0000218 struct passwd *pw;
Damien Miller67bd0622007-09-17 12:06:57 +1000219 int dummy, timeout_ms;
Ben Lindstrom5ccf63a2001-09-24 20:00:10 +0000220 extern int optind, optreset;
Damien Miller4f8e6692001-07-14 13:22:53 +1000221 extern char *optarg;
Damien Miller9651fe62005-06-26 08:55:25 +1000222 struct servent *sp;
Damien Millerf91ee4c2005-03-01 21:24:33 +1100223 Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000224
Darren Tuckerce321d82005-10-03 18:11:24 +1000225 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
226 sanitise_stdfd();
227
Damien Miller59d3d5b2003-08-22 09:34:41 +1000228 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000229 init_rng();
230
Damien Miller5428f641999-11-25 11:54:57 +1100231 /*
232 * Save the original real uid. It will be needed later (uid-swapping
233 * may clobber the real uid).
234 */
Damien Miller95def091999-11-25 00:26:21 +1100235 original_real_uid = getuid();
236 original_effective_uid = geteuid();
Damien Millera8e06ce2003-11-21 23:48:55 +1100237
Damien Miller50b9a602002-09-04 16:50:06 +1000238 /*
239 * Use uid-swapping to give up root privileges for the duration of
240 * option processing. We will re-instantiate the rights when we are
241 * ready to create the privileged port, and will permanently drop
242 * them when the port has been created (actually, when the connection
243 * has been made, as we may need to create the port several times).
244 */
245 PRIV_END;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000246
Damien Miller05dd7952000-10-01 00:42:48 +1100247#ifdef HAVE_SETRLIMIT
Damien Miller95def091999-11-25 00:26:21 +1100248 /* If we are installed setuid root be careful to not drop core. */
249 if (original_real_uid != original_effective_uid) {
250 struct rlimit rlim;
251 rlim.rlim_cur = rlim.rlim_max = 0;
252 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
253 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000254 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100255#endif
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000256 /* Get user data. */
257 pw = getpwuid(original_real_uid);
258 if (!pw) {
Damien Miller996acd22003-04-09 20:59:48 +1000259 logit("You don't exist, go away!");
Darren Tuckere9a9b712005-12-20 16:15:51 +1100260 exit(255);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000261 }
262 /* Take a copy of the returned structure. */
263 pw = pwcopy(pw);
264
Damien Miller5428f641999-11-25 11:54:57 +1100265 /*
Damien Miller5428f641999-11-25 11:54:57 +1100266 * Set our umask to something reasonable, as some files are created
267 * with the default umask. This will make them world-readable but
268 * writable only by the owner, which is ok for all files for which we
269 * don't set the modes explicitly.
270 */
Damien Miller95def091999-11-25 00:26:21 +1100271 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000272
Darren Tuckerd6173c02008-06-13 04:52:53 +1000273 /*
274 * Initialize option structure to indicate that no values have been
275 * set.
276 */
Damien Miller95def091999-11-25 00:26:21 +1100277 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000278
Damien Miller95def091999-11-25 00:26:21 +1100279 /* Parse command-line arguments. */
280 host = NULL;
Damien Millere272a5b2008-11-03 19:22:37 +1100281 use_syslog = 0;
Darren Tucker72efd742009-06-21 17:48:00 +1000282 argv0 = av[0];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000283
Damien Miller2ecb6bd2006-03-15 12:03:53 +1100284 again:
Darren Tuckerd6173c02008-06-13 04:52:53 +1000285 while ((opt = getopt(ac, av, "1246ab:c:e:fgi:kl:m:no:p:qstvx"
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100286 "ACD:F:I:KL:MNO:PR:S:TVw:W:XYy")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100287 switch (opt) {
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000288 case '1':
289 options.protocol = SSH_PROTO_1;
290 break;
Damien Miller4af51302000-04-16 11:18:38 +1000291 case '2':
292 options.protocol = SSH_PROTO_2;
293 break;
Damien Miller34132e52000-01-14 15:45:46 +1100294 case '4':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000295 options.address_family = AF_INET;
Damien Miller34132e52000-01-14 15:45:46 +1100296 break;
Damien Miller34132e52000-01-14 15:45:46 +1100297 case '6':
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000298 options.address_family = AF_INET6;
Damien Miller34132e52000-01-14 15:45:46 +1100299 break;
Damien Miller95def091999-11-25 00:26:21 +1100300 case 'n':
301 stdin_null_flag = 1;
302 break;
Damien Miller95def091999-11-25 00:26:21 +1100303 case 'f':
304 fork_after_authentication_flag = 1;
305 stdin_null_flag = 1;
306 break;
Damien Miller95def091999-11-25 00:26:21 +1100307 case 'x':
308 options.forward_x11 = 0;
309 break;
Damien Miller95def091999-11-25 00:26:21 +1100310 case 'X':
311 options.forward_x11 = 1;
312 break;
Damien Millere272a5b2008-11-03 19:22:37 +1100313 case 'y':
314 use_syslog = 1;
315 break;
Darren Tucker0a118da2003-10-15 15:54:32 +1000316 case 'Y':
317 options.forward_x11 = 1;
318 options.forward_x11_trusted = 1;
319 break;
Damien Miller95def091999-11-25 00:26:21 +1100320 case 'g':
321 options.gateway_ports = 1;
322 break;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100323 case 'O':
Damien Millere1537f92010-01-26 13:26:22 +1100324 if (stdio_forward_host != NULL)
325 fatal("Cannot specify multiplexing "
326 "command with -W");
327 else if (muxclient_command != 0)
328 fatal("Multiplexing command already specified");
Darren Tucker7ebfc102004-11-07 20:06:19 +1100329 if (strcmp(optarg, "check") == 0)
Damien Millerb1cbfa22008-05-19 16:00:08 +1000330 muxclient_command = SSHMUX_COMMAND_ALIVE_CHECK;
Damien Miller388f6fc2010-05-21 14:57:35 +1000331 else if (strcmp(optarg, "forward") == 0)
332 muxclient_command = SSHMUX_COMMAND_FORWARD;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100333 else if (strcmp(optarg, "exit") == 0)
Damien Millerb1cbfa22008-05-19 16:00:08 +1000334 muxclient_command = SSHMUX_COMMAND_TERMINATE;
Darren Tucker7ebfc102004-11-07 20:06:19 +1100335 else
336 fatal("Invalid multiplex command.");
337 break;
Damien Miller147bba32002-09-04 16:46:06 +1000338 case 'P': /* deprecated */
Damien Miller95def091999-11-25 00:26:21 +1100339 options.use_privileged_port = 0;
340 break;
Damien Miller95def091999-11-25 00:26:21 +1100341 case 'a':
342 options.forward_agent = 0;
343 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000344 case 'A':
345 options.forward_agent = 1;
346 break;
Damien Miller95def091999-11-25 00:26:21 +1100347 case 'k':
Damien Millere0113cc2003-11-24 13:10:09 +1100348 options.gss_deleg_creds = 0;
Damien Miller95def091999-11-25 00:26:21 +1100349 break;
Darren Tucker415bddc2007-06-12 23:43:16 +1000350 case 'K':
351 options.gss_authentication = 1;
352 options.gss_deleg_creds = 1;
353 break;
Damien Miller95def091999-11-25 00:26:21 +1100354 case 'i':
355 if (stat(optarg, &st) < 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000356 fprintf(stderr, "Warning: Identity file %s "
Damien Miller3eb48b62005-03-01 21:15:46 +1100357 "not accessible: %s.\n", optarg,
358 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100359 break;
360 }
Damien Millerf4614452001-07-14 12:18:10 +1000361 if (options.num_identity_files >=
362 SSH_MAX_IDENTITY_FILES)
363 fatal("Too many identity files specified "
364 "(max %d)", SSH_MAX_IDENTITY_FILES);
365 options.identity_files[options.num_identity_files++] =
366 xstrdup(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100367 break;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000368 case 'I':
Damien Miller7ea845e2010-02-12 09:21:02 +1100369#ifdef ENABLE_PKCS11
370 options.pkcs11_provider = xstrdup(optarg);
Ben Lindstrombcc18082001-08-06 21:59:25 +0000371#else
Damien Miller7ea845e2010-02-12 09:21:02 +1100372 fprintf(stderr, "no support for PKCS#11.\n");
Ben Lindstrombcc18082001-08-06 21:59:25 +0000373#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000374 break;
Damien Miller95def091999-11-25 00:26:21 +1100375 case 't':
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000376 if (tty_flag)
377 force_tty_flag = 1;
Damien Miller95def091999-11-25 00:26:21 +1100378 tty_flag = 1;
379 break;
Damien Miller95def091999-11-25 00:26:21 +1100380 case 'v':
Darren Tuckere98dfa32003-07-19 19:54:31 +1000381 if (debug_flag == 0) {
Damien Millere4340be2000-09-16 13:29:08 +1100382 debug_flag = 1;
383 options.log_level = SYSLOG_LEVEL_DEBUG1;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000384 } else {
385 if (options.log_level < SYSLOG_LEVEL_DEBUG3)
386 options.log_level++;
Damien Millere4340be2000-09-16 13:29:08 +1100387 break;
Darren Tuckere98dfa32003-07-19 19:54:31 +1000388 }
Darren Tuckere9bf9842004-11-05 20:05:32 +1100389 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100390 case 'V':
Damien Miller0c889cd2004-03-22 09:36:00 +1100391 fprintf(stderr, "%s, %s\n",
Damien Miller2aa6d3c2004-09-12 16:53:04 +1000392 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
Damien Miller95def091999-11-25 00:26:21 +1100393 if (opt == 'V')
394 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100395 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100396 case 'w':
Damien Miller7b58e802005-12-13 19:33:19 +1100397 if (options.tun_open == -1)
398 options.tun_open = SSH_TUNMODE_DEFAULT;
Damien Millerd27b9472005-12-13 19:29:02 +1100399 options.tun_local = a2tun(optarg, &options.tun_remote);
Damien Miller7b58e802005-12-13 19:33:19 +1100400 if (options.tun_local == SSH_TUNID_ERR) {
Darren Tuckerd6173c02008-06-13 04:52:53 +1000401 fprintf(stderr,
402 "Bad tun device '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100403 exit(255);
Damien Millerd27b9472005-12-13 19:29:02 +1100404 }
405 break;
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100406 case 'W':
Damien Millere1537f92010-01-26 13:26:22 +1100407 if (stdio_forward_host != NULL)
408 fatal("stdio forward already specified");
409 if (muxclient_command != 0)
410 fatal("Cannot specify stdio forward with -O");
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100411 if (parse_forward(&fwd, optarg, 1, 0)) {
412 stdio_forward_host = fwd.listen_host;
413 stdio_forward_port = fwd.listen_port;
414 xfree(fwd.connect_host);
415 } else {
416 fprintf(stderr,
417 "Bad stdio forwarding specification '%s'\n",
418 optarg);
419 exit(255);
420 }
421 no_tty_flag = 1;
422 no_shell_flag = 1;
423 options.clear_forwardings = 1;
424 options.exit_on_forward_failure = 1;
425 break;
Damien Miller95def091999-11-25 00:26:21 +1100426 case 'q':
427 options.log_level = SYSLOG_LEVEL_QUIET;
428 break;
Damien Miller95def091999-11-25 00:26:21 +1100429 case 'e':
430 if (optarg[0] == '^' && optarg[2] == 0 &&
Damien Millerf4614452001-07-14 12:18:10 +1000431 (u_char) optarg[1] >= 64 &&
432 (u_char) optarg[1] < 128)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000433 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100434 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000435 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100436 else if (strcmp(optarg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000437 options.escape_char = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100438 else {
Damien Millerf4614452001-07-14 12:18:10 +1000439 fprintf(stderr, "Bad escape character '%s'.\n",
440 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100441 exit(255);
Damien Miller95def091999-11-25 00:26:21 +1100442 }
443 break;
Damien Miller95def091999-11-25 00:26:21 +1100444 case 'c':
Damien Millereba71ba2000-04-29 23:57:08 +1000445 if (ciphers_valid(optarg)) {
446 /* SSH2 only */
447 options.ciphers = xstrdup(optarg);
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000448 options.cipher = SSH_CIPHER_INVALID;
Damien Millereba71ba2000-04-29 23:57:08 +1000449 } else {
450 /* SSH1 only */
Damien Millere39cacc2000-11-29 12:18:44 +1100451 options.cipher = cipher_number(optarg);
452 if (options.cipher == -1) {
Damien Millerf4614452001-07-14 12:18:10 +1000453 fprintf(stderr,
454 "Unknown cipher type '%s'\n",
455 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100456 exit(255);
Damien Millereba71ba2000-04-29 23:57:08 +1000457 }
Damien Millerf4614452001-07-14 12:18:10 +1000458 if (options.cipher == SSH_CIPHER_3DES)
Damien Millere39cacc2000-11-29 12:18:44 +1100459 options.ciphers = "3des-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000460 else if (options.cipher == SSH_CIPHER_BLOWFISH)
Damien Millere39cacc2000-11-29 12:18:44 +1100461 options.ciphers = "blowfish-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000462 else
Damien Millere39cacc2000-11-29 12:18:44 +1100463 options.ciphers = (char *)-1;
Damien Miller95def091999-11-25 00:26:21 +1100464 }
465 break;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000466 case 'm':
467 if (mac_valid(optarg))
468 options.macs = xstrdup(optarg);
469 else {
Damien Millerf4614452001-07-14 12:18:10 +1000470 fprintf(stderr, "Unknown mac type '%s'\n",
471 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100472 exit(255);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000473 }
474 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000475 case 'M':
Damien Millerd14b1e72005-06-16 13:19:41 +1000476 if (options.control_master == SSHCTL_MASTER_YES)
477 options.control_master = SSHCTL_MASTER_ASK;
478 else
479 options.control_master = SSHCTL_MASTER_YES;
Damien Miller0e220db2004-06-15 10:34:08 +1000480 break;
Damien Miller95def091999-11-25 00:26:21 +1100481 case 'p':
Ben Lindstrom19066a12001-04-12 23:39:26 +0000482 options.port = a2port(optarg);
Damien Miller3dc71ad2009-01-28 16:31:22 +1100483 if (options.port <= 0) {
Ben Lindstrom146edb92001-04-11 23:06:28 +0000484 fprintf(stderr, "Bad port '%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100485 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000486 }
Damien Miller95def091999-11-25 00:26:21 +1100487 break;
Damien Miller95def091999-11-25 00:26:21 +1100488 case 'l':
489 options.user = optarg;
490 break;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000491
Damien Miller95def091999-11-25 00:26:21 +1100492 case 'L':
Damien Miller4bf648f2009-02-14 16:28:21 +1100493 if (parse_forward(&fwd, optarg, 0, 0))
Damien Millerf91ee4c2005-03-01 21:24:33 +1100494 add_local_forward(&options, &fwd);
495 else {
Damien Millerf4614452001-07-14 12:18:10 +1000496 fprintf(stderr,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100497 "Bad local forwarding specification '%s'\n",
Damien Millerf4614452001-07-14 12:18:10 +1000498 optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100499 exit(255);
Ben Lindstrom1a174712001-09-12 17:56:15 +0000500 }
Damien Millerf91ee4c2005-03-01 21:24:33 +1100501 break;
502
503 case 'R':
Damien Miller4bf648f2009-02-14 16:28:21 +1100504 if (parse_forward(&fwd, optarg, 0, 1)) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100505 add_remote_forward(&options, &fwd);
506 } else {
507 fprintf(stderr,
508 "Bad remote forwarding specification "
509 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100510 exit(255);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100511 }
Damien Miller95def091999-11-25 00:26:21 +1100512 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000513
514 case 'D':
Damien Miller4bf648f2009-02-14 16:28:21 +1100515 if (parse_forward(&fwd, optarg, 1, 0)) {
Damien Millera699d952008-11-03 19:27:34 +1100516 add_local_forward(&options, &fwd);
Damien Millerf91ee4c2005-03-01 21:24:33 +1100517 } else {
Damien Millera699d952008-11-03 19:27:34 +1100518 fprintf(stderr,
519 "Bad dynamic forwarding specification "
520 "'%s'\n", optarg);
Darren Tuckere9a9b712005-12-20 16:15:51 +1100521 exit(255);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000522 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000523 break;
524
Damien Miller95def091999-11-25 00:26:21 +1100525 case 'C':
526 options.compression = 1;
527 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000528 case 'N':
529 no_shell_flag = 1;
530 no_tty_flag = 1;
531 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000532 case 'T':
533 no_tty_flag = 1;
534 break;
Damien Miller95def091999-11-25 00:26:21 +1100535 case 'o':
536 dummy = 1;
Damien Miller9836cf82003-12-17 16:30:06 +1100537 line = xstrdup(optarg);
Damien Millerf4614452001-07-14 12:18:10 +1000538 if (process_config_line(&options, host ? host : "",
Damien Miller9836cf82003-12-17 16:30:06 +1100539 line, "command-line", 0, &dummy) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100540 exit(255);
Damien Miller9836cf82003-12-17 16:30:06 +1100541 xfree(line);
Damien Miller95def091999-11-25 00:26:21 +1100542 break;
Damien Miller832562e2001-01-30 09:30:01 +1100543 case 's':
544 subsystem_flag = 1;
545 break;
Damien Miller0e220db2004-06-15 10:34:08 +1000546 case 'S':
547 if (options.control_path != NULL)
548 free(options.control_path);
549 options.control_path = xstrdup(optarg);
Damien Miller0e220db2004-06-15 10:34:08 +1000550 break;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000551 case 'b':
552 options.bind_address = optarg;
553 break;
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000554 case 'F':
555 config = optarg;
556 break;
Damien Miller95def091999-11-25 00:26:21 +1100557 default:
558 usage();
559 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000560 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000561
Damien Millerf4614452001-07-14 12:18:10 +1000562 ac -= optind;
563 av += optind;
564
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100565 if (ac > 0 && !host) {
Ben Lindstromc276c122002-12-23 02:14:51 +0000566 if (strrchr(*av, '@')) {
Damien Millerf4614452001-07-14 12:18:10 +1000567 p = xstrdup(*av);
Ben Lindstromc276c122002-12-23 02:14:51 +0000568 cp = strrchr(p, '@');
Damien Millerf4614452001-07-14 12:18:10 +1000569 if (cp == NULL || cp == p)
570 usage();
571 options.user = p;
572 *cp = '\0';
573 host = ++cp;
574 } else
575 host = *av;
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000576 if (ac > 1) {
577 optind = optreset = 1;
Damien Millerf4614452001-07-14 12:18:10 +1000578 goto again;
579 }
Ben Lindstromb9fa6912002-12-23 02:24:54 +0000580 ac--, av++;
Damien Millerf4614452001-07-14 12:18:10 +1000581 }
582
Damien Miller95def091999-11-25 00:26:21 +1100583 /* Check that we got a host name. */
584 if (!host)
585 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000586
Damien Millercb5e44a2000-09-29 12:12:36 +1100587 SSLeay_add_all_algorithms();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100588 ERR_load_crypto_strings();
Damien Millercb5e44a2000-09-29 12:12:36 +1100589
Damien Miller95def091999-11-25 00:26:21 +1100590 /* Initialize the command to execute on remote host. */
591 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000592
Damien Miller5428f641999-11-25 11:54:57 +1100593 /*
594 * Save the command to execute on the remote host in a buffer. There
595 * is no limit on the length of the command, except by the maximum
596 * packet size. Also sets the tty flag if there is no command.
597 */
Damien Millerf4614452001-07-14 12:18:10 +1000598 if (!ac) {
Damien Miller95def091999-11-25 00:26:21 +1100599 /* No command specified - execute shell on a tty. */
600 tty_flag = 1;
Damien Miller832562e2001-01-30 09:30:01 +1100601 if (subsystem_flag) {
Damien Millerf4614452001-07-14 12:18:10 +1000602 fprintf(stderr,
603 "You must specify a subsystem to invoke.\n");
Damien Miller832562e2001-01-30 09:30:01 +1100604 usage();
605 }
Damien Miller95def091999-11-25 00:26:21 +1100606 } else {
Damien Millerf4614452001-07-14 12:18:10 +1000607 /* A command has been specified. Store it into the buffer. */
608 for (i = 0; i < ac; i++) {
609 if (i)
Damien Miller95def091999-11-25 00:26:21 +1100610 buffer_append(&command, " ", 1);
611 buffer_append(&command, av[i], strlen(av[i]));
612 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000613 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000614
Damien Miller95def091999-11-25 00:26:21 +1100615 /* Cannot fork to background if no command. */
Darren Tuckerd6173c02008-06-13 04:52:53 +1000616 if (fork_after_authentication_flag && buffer_len(&command) == 0 &&
617 !no_shell_flag)
618 fatal("Cannot fork into background without a command "
619 "to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000620
Damien Miller95def091999-11-25 00:26:21 +1100621 /* Allocate a tty by default if no command specified. */
622 if (buffer_len(&command) == 0)
623 tty_flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000624
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000625 /* Force no tty */
Damien Millerdeb5a142010-04-16 15:52:43 +1000626 if (no_tty_flag || muxclient_command != 0)
Ben Lindstromc72745a2000-12-02 19:03:54 +0000627 tty_flag = 0;
Damien Miller95def091999-11-25 00:26:21 +1100628 /* Do not allocate a tty if stdin is not a tty. */
Damien Millerddee5752005-05-26 12:05:05 +1000629 if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100630 if (tty_flag)
Darren Tuckerd6173c02008-06-13 04:52:53 +1000631 logit("Pseudo-terminal will not be allocated because "
632 "stdin is not a terminal.");
Damien Miller95def091999-11-25 00:26:21 +1100633 tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000634 }
Damien Miller1383bd82000-04-06 12:32:37 +1000635
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000636 /*
637 * Initialize "log" output. Since we are the client all output
638 * actually goes to stderr.
639 */
Darren Tucker72efd742009-06-21 17:48:00 +1000640 log_init(argv0,
Darren Tuckerd6173c02008-06-13 04:52:53 +1000641 options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
Damien Millere272a5b2008-11-03 19:22:37 +1100642 SYSLOG_FACILITY_USER, !use_syslog);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000643
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000644 /*
645 * Read per-user configuration file. Ignore the system wide config
646 * file if the user specifies a config file on the command line.
647 */
648 if (config != NULL) {
Damien Miller914420f2004-04-20 20:14:07 +1000649 if (!read_config_file(config, host, &options, 0))
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000650 fatal("Can't open user config file %.100s: "
651 "%.100s", config, strerror(errno));
Damien Miller80163902007-01-05 16:30:16 +1100652 } else {
Darren Tucker199b1342009-07-06 07:16:56 +1000653 r = snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir,
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000654 _PATH_SSH_USER_CONFFILE);
Darren Tucker199b1342009-07-06 07:16:56 +1000655 if (r > 0 && (size_t)r < sizeof(buf))
656 (void)read_config_file(buf, host, &options, 1);
Ben Lindstrom83f07d12001-10-03 17:22:29 +0000657
658 /* Read systemwide configuration file after use config. */
Darren Tuckerfc959702004-07-17 16:12:08 +1000659 (void)read_config_file(_PATH_HOST_CONFIG_FILE, host,
Damien Miller57a44762004-04-20 20:11:57 +1000660 &options, 0);
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000661 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000662
Damien Miller95def091999-11-25 00:26:21 +1100663 /* Fill configuration defaults. */
664 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000665
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000666 channel_set_af(options.address_family);
667
Damien Miller95def091999-11-25 00:26:21 +1100668 /* reinit */
Darren Tucker72efd742009-06-21 17:48:00 +1000669 log_init(argv0, options.log_level, SYSLOG_FACILITY_USER, !use_syslog);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000670
Damien Miller60bc5172001-03-19 09:38:15 +1100671 seed_rng();
672
Damien Miller95def091999-11-25 00:26:21 +1100673 if (options.user == NULL)
674 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000675
Darren Tuckerf6b01b72008-06-13 04:56:37 +1000676 /* Get default port if port has not been set. */
677 if (options.port == 0) {
678 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
679 options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
680 }
681
682 if (options.local_command != NULL) {
683 char thishost[NI_MAXHOST];
684
685 if (gethostname(thishost, sizeof(thishost)) == -1)
686 fatal("gethostname: %s", strerror(errno));
687 snprintf(buf, sizeof(buf), "%d", options.port);
688 debug3("expanding LocalCommand: %s", options.local_command);
689 cp = options.local_command;
690 options.local_command = percent_expand(cp, "d", pw->pw_dir,
691 "h", options.hostname? options.hostname : host,
692 "l", thishost, "n", host, "r", options.user, "p", buf,
693 "u", pw->pw_name, (char *)NULL);
694 debug3("expanded LocalCommand: %s", options.local_command);
695 xfree(cp);
696 }
697
Damien Miller95def091999-11-25 00:26:21 +1100698 if (options.hostname != NULL)
699 host = options.hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000700
Darren Tucker3f521e22003-07-03 16:20:42 +1000701 /* force lowercase for hostkey matching */
702 if (options.host_key_alias != NULL) {
703 for (p = options.host_key_alias; *p; p++)
704 if (isupper(*p))
Damien Miller1d2b6702006-03-26 14:09:54 +1100705 *p = (char)tolower(*p);
Darren Tucker3f521e22003-07-03 16:20:42 +1000706 }
707
Damien Miller9f1e33a2003-02-24 11:57:32 +1100708 if (options.proxy_command != NULL &&
Darren Tucker32e42c72007-12-02 23:01:03 +1100709 strcmp(options.proxy_command, "none") == 0) {
710 xfree(options.proxy_command);
Damien Miller9f1e33a2003-02-24 11:57:32 +1100711 options.proxy_command = NULL;
Darren Tucker32e42c72007-12-02 23:01:03 +1100712 }
Damien Miller8f74c8f2005-06-26 08:56:03 +1000713 if (options.control_path != NULL &&
Darren Tucker32e42c72007-12-02 23:01:03 +1100714 strcmp(options.control_path, "none") == 0) {
715 xfree(options.control_path);
Damien Miller8f74c8f2005-06-26 08:56:03 +1000716 options.control_path = NULL;
Darren Tucker32e42c72007-12-02 23:01:03 +1100717 }
Damien Miller9f1e33a2003-02-24 11:57:32 +1100718
Damien Miller0e220db2004-06-15 10:34:08 +1000719 if (options.control_path != NULL) {
Damien Miller6b1d53c2006-03-31 23:13:21 +1100720 char thishost[NI_MAXHOST];
Damien Miller3ec54c72006-03-15 11:30:13 +1100721
Damien Miller6b1d53c2006-03-31 23:13:21 +1100722 if (gethostname(thishost, sizeof(thishost)) == -1)
Damien Miller3ec54c72006-03-15 11:30:13 +1100723 fatal("gethostname: %s", strerror(errno));
Damien Miller6476cad2005-06-16 13:18:34 +1000724 snprintf(buf, sizeof(buf), "%d", options.port);
725 cp = tilde_expand_filename(options.control_path,
726 original_real_uid);
Darren Tucker32e42c72007-12-02 23:01:03 +1100727 xfree(options.control_path);
Damien Miller6476cad2005-06-16 13:18:34 +1000728 options.control_path = percent_expand(cp, "p", buf, "h", host,
Damien Miller6b1d53c2006-03-31 23:13:21 +1100729 "r", options.user, "l", thishost, (char *)NULL);
Damien Miller6476cad2005-06-16 13:18:34 +1000730 xfree(cp);
Damien Miller0e220db2004-06-15 10:34:08 +1000731 }
Damien Millerb1cbfa22008-05-19 16:00:08 +1000732 if (muxclient_command != 0 && options.control_path == NULL)
Darren Tucker0814d312005-06-01 23:08:51 +1000733 fatal("No ControlPath specified for \"-O\" command");
Damien Millerd14b1e72005-06-16 13:19:41 +1000734 if (options.control_path != NULL)
Damien Millerb1cbfa22008-05-19 16:00:08 +1000735 muxclient(options.control_path);
Damien Miller0e220db2004-06-15 10:34:08 +1000736
Damien Miller67bd0622007-09-17 12:06:57 +1000737 timeout_ms = options.connection_timeout * 1000;
738
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000739 /* Open a connection to the remote host. */
Darren Tucker0a4f04b2003-07-03 20:37:47 +1000740 if (ssh_connect(host, &hostaddr, options.port,
Damien Miller67bd0622007-09-17 12:06:57 +1000741 options.address_family, options.connection_attempts, &timeout_ms,
742 options.tcp_keep_alive,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000743#ifdef HAVE_CYGWIN
744 options.use_privileged_port,
745#else
Ben Lindstromf9c48842002-06-11 16:37:51 +0000746 original_effective_uid == 0 && options.use_privileged_port,
Ben Lindstrom1aa64272002-06-11 20:28:05 +0000747#endif
Ben Lindstromda394ca2002-06-12 16:11:12 +0000748 options.proxy_command) != 0)
Darren Tuckere9a9b712005-12-20 16:15:51 +1100749 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000750
Damien Miller67bd0622007-09-17 12:06:57 +1000751 if (timeout_ms > 0)
752 debug3("timeout: %d ms remain after connect", timeout_ms);
753
Damien Miller5428f641999-11-25 11:54:57 +1100754 /*
755 * If we successfully made the connection, load the host private key
756 * in case we will need it later for combined rsa-rhosts
757 * authentication. This must be done before releasing extra
758 * privileges, because the file is only readable by root.
Ben Lindstrom9e5bb572002-06-06 19:58:27 +0000759 * If we cannot access the private keys, load the public keys
760 * instead and try to execute the ssh-keysign helper instead.
Damien Miller5428f641999-11-25 11:54:57 +1100761 */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000762 sensitive_data.nkeys = 0;
763 sensitive_data.keys = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000764 sensitive_data.external_keysign = 0;
Ben Lindstrom24157572002-06-12 16:09:39 +0000765 if (options.rhosts_rsa_authentication ||
766 options.hostbased_authentication) {
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000767 sensitive_data.nkeys = 3;
Damien Millerddd63ab2006-03-31 23:10:51 +1100768 sensitive_data.keys = xcalloc(sensitive_data.nkeys,
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000769 sizeof(Key));
Ben Lindstromf9c48842002-06-11 16:37:51 +0000770
771 PRIV_START;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000772 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
Darren Tucker232b76f2006-05-06 17:41:51 +1000773 _PATH_HOST_KEY_FILE, "", NULL, NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000774 sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
Darren Tucker232b76f2006-05-06 17:41:51 +1000775 _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000776 sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
Darren Tucker232b76f2006-05-06 17:41:51 +1000777 _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL);
Ben Lindstromf9c48842002-06-11 16:37:51 +0000778 PRIV_END;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000779
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +0000780 if (options.hostbased_authentication == 1 &&
781 sensitive_data.keys[0] == NULL &&
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000782 sensitive_data.keys[1] == NULL &&
783 sensitive_data.keys[2] == NULL) {
784 sensitive_data.keys[1] = key_load_public(
785 _PATH_HOST_DSA_KEY_FILE, NULL);
786 sensitive_data.keys[2] = key_load_public(
787 _PATH_HOST_RSA_KEY_FILE, NULL);
788 sensitive_data.external_keysign = 1;
789 }
Damien Miller95def091999-11-25 00:26:21 +1100790 }
Damien Miller5428f641999-11-25 11:54:57 +1100791 /*
792 * Get rid of any extra privileges that we may have. We will no
793 * longer need them. Also, extra privileges could make it very hard
794 * to read identity files and other non-world-readable files from the
795 * user's home directory if it happens to be on a NFS volume where
796 * root is mapped to nobody.
797 */
Darren Tucker25f60a72004-08-15 17:23:34 +1000798 if (original_effective_uid == 0) {
799 PRIV_START;
800 permanently_set_uid(pw);
801 }
Damien Miller95def091999-11-25 00:26:21 +1100802
Damien Miller5428f641999-11-25 11:54:57 +1100803 /*
804 * Now that we are back to our own permissions, create ~/.ssh
Damien Miller788f2122005-11-05 15:14:59 +1100805 * directory if it doesn't already exist.
Damien Miller5428f641999-11-25 11:54:57 +1100806 */
Darren Tucker199b1342009-07-06 07:16:56 +1000807 r = snprintf(buf, sizeof buf, "%s%s%s", pw->pw_dir,
Darren Tuckerd6173c02008-06-13 04:52:53 +1000808 strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
Darren Tucker199b1342009-07-06 07:16:56 +1000809 if (r > 0 && (size_t)r < sizeof(buf) && stat(buf, &st) < 0)
Damien Millerbe484b52000-07-15 14:14:16 +1000810 if (mkdir(buf, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100811 error("Could not create directory '%.200s'.", buf);
812
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000813 /* load options.identity_files */
814 load_public_identity_files();
815
816 /* Expand ~ in known host file names. */
817 /* XXX mem-leaks: */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100818 options.system_hostfile =
819 tilde_expand_filename(options.system_hostfile, original_real_uid);
820 options.user_hostfile =
821 tilde_expand_filename(options.user_hostfile, original_real_uid);
822 options.system_hostfile2 =
823 tilde_expand_filename(options.system_hostfile2, original_real_uid);
824 options.user_hostfile2 =
825 tilde_expand_filename(options.user_hostfile2, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100826
Damien Miller07cd5892001-11-12 10:52:03 +1100827 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
828
Darren Tuckerd6173c02008-06-13 04:52:53 +1000829 /* Log into the remote system. Never returns if the login fails. */
Damien Miller67bd0622007-09-17 12:06:57 +1000830 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr,
831 pw, timeout_ms);
Damien Miller95def091999-11-25 00:26:21 +1100832
Damien Miller383ffe62010-06-26 10:02:03 +1000833 if (packet_connection_is_on_socket()) {
834 verbose("Authenticated to %s ([%s]:%d).", host,
835 get_remote_ipaddr(), get_remote_port());
836 } else {
837 verbose("Authenticated to %s (via proxy).", host);
838 }
839
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000840 /* We no longer need the private host keys. Clear them now. */
841 if (sensitive_data.nkeys != 0) {
842 for (i = 0; i < sensitive_data.nkeys; i++) {
843 if (sensitive_data.keys[i] != NULL) {
844 /* Destroys contents safely */
845 debug3("clear hostkey %d", i);
846 key_free(sensitive_data.keys[i]);
847 sensitive_data.keys[i] = NULL;
848 }
849 }
850 xfree(sensitive_data.keys);
851 }
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +0000852 for (i = 0; i < options.num_identity_files; i++) {
853 if (options.identity_files[i]) {
854 xfree(options.identity_files[i]);
855 options.identity_files[i] = NULL;
856 }
857 if (options.identity_keys[i]) {
858 key_free(options.identity_keys[i]);
859 options.identity_keys[i] = NULL;
860 }
861 }
Damien Miller95def091999-11-25 00:26:21 +1100862
Damien Miller1383bd82000-04-06 12:32:37 +1000863 exit_status = compat20 ? ssh_session2() : ssh_session();
864 packet_close();
Damien Miller8c4e18a2002-09-19 12:05:02 +1000865
Damien Millerb1cbfa22008-05-19 16:00:08 +1000866 if (options.control_path != NULL && muxserver_sock != -1)
Damien Miller0e220db2004-06-15 10:34:08 +1000867 unlink(options.control_path);
868
Damien Miller8c4e18a2002-09-19 12:05:02 +1000869 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100870 * Send SIGHUP to proxy command if used. We don't wait() in
Damien Miller8c4e18a2002-09-19 12:05:02 +1000871 * case it hangs and instead rely on init to reap the child
872 */
873 if (proxy_command_pid > 1)
874 kill(proxy_command_pid, SIGHUP);
875
Damien Miller1383bd82000-04-06 12:32:37 +1000876 return exit_status;
877}
878
Darren Tucker9f407c42008-06-13 04:50:27 +1000879/* Callback for remote forward global requests */
880static void
881ssh_confirm_remote_forward(int type, u_int32_t seq, void *ctxt)
882{
883 Forward *rfwd = (Forward *)ctxt;
884
Damien Miller4bf648f2009-02-14 16:28:21 +1100885 /* XXX verbose() on failure? */
Darren Tucker9f407c42008-06-13 04:50:27 +1000886 debug("remote forward %s for: listen %d, connect %s:%d",
887 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
888 rfwd->listen_port, rfwd->connect_host, rfwd->connect_port);
Damien Miller4bf648f2009-02-14 16:28:21 +1100889 if (type == SSH2_MSG_REQUEST_SUCCESS && rfwd->listen_port == 0) {
Damien Miller388f6fc2010-05-21 14:57:35 +1000890 rfwd->allocated_port = packet_get_int();
Damien Miller4bf648f2009-02-14 16:28:21 +1100891 logit("Allocated port %u for remote forward to %s:%d",
Damien Miller388f6fc2010-05-21 14:57:35 +1000892 rfwd->allocated_port,
893 rfwd->connect_host, rfwd->connect_port);
Damien Miller4bf648f2009-02-14 16:28:21 +1100894 }
895
Darren Tucker9f407c42008-06-13 04:50:27 +1000896 if (type == SSH2_MSG_REQUEST_FAILURE) {
897 if (options.exit_on_forward_failure)
898 fatal("Error: remote port forwarding failed for "
899 "listen port %d", rfwd->listen_port);
900 else
901 logit("Warning: remote port forwarding failed for "
902 "listen port %d", rfwd->listen_port);
903 }
Darren Tucker9a2a6092008-07-04 12:53:50 +1000904 if (++remote_forward_confirms_received == options.num_remote_forwards) {
Darren Tucker9f407c42008-06-13 04:50:27 +1000905 debug("All remote forwarding requests processed");
Darren Tucker9a2a6092008-07-04 12:53:50 +1000906 if (fork_after_authentication_flag) {
907 fork_after_authentication_flag = 0;
908 if (daemon(1, 1) < 0)
909 fatal("daemon() failed: %.200s",
910 strerror(errno));
911 }
912 }
Darren Tucker9f407c42008-06-13 04:50:27 +1000913}
914
Ben Lindstrombba81212001-06-25 05:01:22 +0000915static void
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100916client_cleanup_stdio_fwd(int id, void *arg)
917{
918 debug("stdio forwarding: done");
919 cleanup_exit(0);
920}
921
922static int
923client_setup_stdio_fwd(const char *host_to_connect, u_short port_to_connect)
924{
925 Channel *c;
Damien Millere1537f92010-01-26 13:26:22 +1100926 int in, out;
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100927
928 debug3("client_setup_stdio_fwd %s:%d", host_to_connect,
929 port_to_connect);
Damien Millere1537f92010-01-26 13:26:22 +1100930
931 in = dup(STDIN_FILENO);
932 out = dup(STDOUT_FILENO);
933 if (in < 0 || out < 0)
934 fatal("channel_connect_stdio_fwd: dup() in/out failed");
935
936 if ((c = channel_connect_stdio_fwd(host_to_connect, port_to_connect,
937 in, out)) == NULL)
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100938 return 0;
939 channel_register_cleanup(c->self, client_cleanup_stdio_fwd, 0);
940 return 1;
941}
942
943static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100944ssh_init_forwarding(void)
945{
Kevin Steves12057502001-02-05 14:54:34 +0000946 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100947 int i;
Kevin Steves12057502001-02-05 14:54:34 +0000948
Darren Tucker7ad8dd22010-01-12 19:40:27 +1100949 if (stdio_forward_host != NULL) {
950 if (!compat20) {
951 fatal("stdio forwarding require Protocol 2");
952 }
953 if (!client_setup_stdio_fwd(stdio_forward_host,
954 stdio_forward_port))
955 fatal("Failed to connect in stdio forward mode.");
956 }
957
Damien Miller0bc1bd82000-11-13 22:57:25 +1100958 /* Initiate local TCP/IP port forwardings. */
959 for (i = 0; i < options.num_local_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100960 debug("Local connections to %.200s:%d forwarded to remote "
961 "address %.200s:%d",
Darren Tucker47eede72005-03-14 23:08:12 +1100962 (options.local_forwards[i].listen_host == NULL) ?
963 (options.gateway_ports ? "*" : "LOCALHOST") :
Damien Millerf91ee4c2005-03-01 21:24:33 +1100964 options.local_forwards[i].listen_host,
965 options.local_forwards[i].listen_port,
966 options.local_forwards[i].connect_host,
967 options.local_forwards[i].connect_port);
Damien Millerb16461c2002-01-22 23:29:22 +1100968 success += channel_setup_local_fwd_listener(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100969 options.local_forwards[i].listen_host,
970 options.local_forwards[i].listen_port,
971 options.local_forwards[i].connect_host,
972 options.local_forwards[i].connect_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100973 options.gateway_ports);
974 }
Darren Tuckere7d4b192006-07-12 22:17:10 +1000975 if (i > 0 && success != i && options.exit_on_forward_failure)
976 fatal("Could not request local forwarding.");
Kevin Steves12057502001-02-05 14:54:34 +0000977 if (i > 0 && success == 0)
978 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100979
980 /* Initiate remote TCP/IP port forwardings. */
981 for (i = 0; i < options.num_remote_forwards; i++) {
Damien Millerf91ee4c2005-03-01 21:24:33 +1100982 debug("Remote connections from %.200s:%d forwarded to "
983 "local address %.200s:%d",
Damien Miller46d38de2005-07-17 17:02:09 +1000984 (options.remote_forwards[i].listen_host == NULL) ?
Damien Milleraa3bb102005-11-05 15:12:59 +1100985 "LOCALHOST" : options.remote_forwards[i].listen_host,
Damien Millerf91ee4c2005-03-01 21:24:33 +1100986 options.remote_forwards[i].listen_port,
987 options.remote_forwards[i].connect_host,
988 options.remote_forwards[i].connect_port);
Darren Tuckere7d4b192006-07-12 22:17:10 +1000989 if (channel_request_remote_forwarding(
Damien Millerf91ee4c2005-03-01 21:24:33 +1100990 options.remote_forwards[i].listen_host,
991 options.remote_forwards[i].listen_port,
992 options.remote_forwards[i].connect_host,
Darren Tuckere7d4b192006-07-12 22:17:10 +1000993 options.remote_forwards[i].connect_port) < 0) {
994 if (options.exit_on_forward_failure)
995 fatal("Could not request remote forwarding.");
996 else
997 logit("Warning: Could not request remote "
998 "forwarding.");
999 }
Darren Tucker9f407c42008-06-13 04:50:27 +10001000 client_register_global_confirm(ssh_confirm_remote_forward,
1001 &options.remote_forwards[i]);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001002 }
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001003
1004 /* Initiate tunnel forwarding. */
1005 if (options.tun_open != SSH_TUNMODE_NO) {
1006 if (client_request_tun_fwd(options.tun_open,
1007 options.tun_local, options.tun_remote) == -1) {
1008 if (options.exit_on_forward_failure)
1009 fatal("Could not request tunnel forwarding.");
1010 else
1011 error("Could not request tunnel forwarding.");
1012 }
1013 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001014}
1015
Ben Lindstrombba81212001-06-25 05:01:22 +00001016static void
Damien Miller0bc1bd82000-11-13 22:57:25 +11001017check_agent_present(void)
1018{
1019 if (options.forward_agent) {
Damien Miller788f2122005-11-05 15:14:59 +11001020 /* Clear agent forwarding if we don't have an agent. */
Damien Miller789e95d2002-09-12 09:52:46 +10001021 if (!ssh_agent_present())
Damien Miller0bc1bd82000-11-13 22:57:25 +11001022 options.forward_agent = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001023 }
1024}
1025
Ben Lindstrombba81212001-06-25 05:01:22 +00001026static int
Damien Miller1383bd82000-04-06 12:32:37 +10001027ssh_session(void)
1028{
1029 int type;
Damien Miller1383bd82000-04-06 12:32:37 +10001030 int interactive = 0;
1031 int have_tty = 0;
1032 struct winsize ws;
Damien Miller1383bd82000-04-06 12:32:37 +10001033 char *cp;
Damien Miller17e7ed02005-06-17 12:54:33 +10001034 const char *display;
Damien Miller1383bd82000-04-06 12:32:37 +10001035
Damien Miller95def091999-11-25 00:26:21 +11001036 /* Enable compression if requested. */
1037 if (options.compression) {
Darren Tuckerd6173c02008-06-13 04:52:53 +10001038 debug("Requesting compression at level %d.",
1039 options.compression_level);
Damien Miller95def091999-11-25 00:26:21 +11001040
Darren Tuckerd6173c02008-06-13 04:52:53 +10001041 if (options.compression_level < 1 ||
1042 options.compression_level > 9)
1043 fatal("Compression level must be from 1 (fast) to "
1044 "9 (slow, best).");
Damien Miller95def091999-11-25 00:26:21 +11001045
1046 /* Send the request. */
1047 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
1048 packet_put_int(options.compression_level);
1049 packet_send();
1050 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +11001051 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +11001052 if (type == SSH_SMSG_SUCCESS)
1053 packet_start_compression(options.compression_level);
1054 else if (type == SSH_SMSG_FAILURE)
Damien Miller996acd22003-04-09 20:59:48 +10001055 logit("Warning: Remote host refused compression.");
Damien Miller95def091999-11-25 00:26:21 +11001056 else
Darren Tuckerd6173c02008-06-13 04:52:53 +10001057 packet_disconnect("Protocol error waiting for "
1058 "compression response.");
Damien Miller95def091999-11-25 00:26:21 +11001059 }
1060 /* Allocate a pseudo tty if appropriate. */
1061 if (tty_flag) {
1062 debug("Requesting pty.");
1063
1064 /* Start the packet. */
1065 packet_start(SSH_CMSG_REQUEST_PTY);
1066
1067 /* Store TERM in the packet. There is no limit on the
1068 length of the string. */
1069 cp = getenv("TERM");
1070 if (!cp)
1071 cp = "";
Ben Lindstrom664408d2001-06-09 01:42:01 +00001072 packet_put_cstring(cp);
Damien Miller95def091999-11-25 00:26:21 +11001073
1074 /* Store window size in the packet. */
1075 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
1076 memset(&ws, 0, sizeof(ws));
Damien Miller71a73672006-03-26 14:04:36 +11001077 packet_put_int((u_int)ws.ws_row);
1078 packet_put_int((u_int)ws.ws_col);
1079 packet_put_int((u_int)ws.ws_xpixel);
1080 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller95def091999-11-25 00:26:21 +11001081
1082 /* Store tty modes in the packet. */
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001083 tty_make_modes(fileno(stdin), NULL);
Damien Miller95def091999-11-25 00:26:21 +11001084
1085 /* Send the packet, and wait for it to leave. */
1086 packet_send();
1087 packet_write_wait();
1088
1089 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +11001090 type = packet_read();
Damien Miller450a7a12000-03-26 13:04:51 +10001091 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +11001092 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001093 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +10001094 } else if (type == SSH_SMSG_FAILURE)
Darren Tuckerd6173c02008-06-13 04:52:53 +10001095 logit("Warning: Remote host failed or refused to "
1096 "allocate a pseudo tty.");
Damien Miller95def091999-11-25 00:26:21 +11001097 else
Darren Tuckerd6173c02008-06-13 04:52:53 +10001098 packet_disconnect("Protocol error waiting for pty "
1099 "request response.");
Damien Miller95def091999-11-25 00:26:21 +11001100 }
1101 /* Request X11 forwarding if enabled and DISPLAY is set. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001102 display = getenv("DISPLAY");
1103 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001104 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001105 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001106 client_x11_get_proto(display, options.xauth_location,
Damien Miller1ab6a512010-06-26 10:02:24 +10001107 options.forward_x11_trusted,
1108 options.forward_x11_timeout,
1109 &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001110 /* Request forwarding with authentication spoofing. */
Darren Tuckerd6173c02008-06-13 04:52:53 +10001111 debug("Requesting X11 forwarding with authentication "
1112 "spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +10001113 x11_request_forwarding_with_spoofing(0, display, proto, data);
Damien Miller95def091999-11-25 00:26:21 +11001114
1115 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +11001116 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +11001117 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +11001118 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001119 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller996acd22003-04-09 20:59:48 +10001120 logit("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +10001121 } else {
Darren Tuckerd6173c02008-06-13 04:52:53 +10001122 packet_disconnect("Protocol error waiting for X11 "
1123 "forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +10001124 }
Damien Miller95def091999-11-25 00:26:21 +11001125 }
1126 /* Tell the packet module whether this is an interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001127 packet_set_interactive(interactive);
Damien Miller95def091999-11-25 00:26:21 +11001128
1129 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001130 check_agent_present();
1131
Damien Miller95def091999-11-25 00:26:21 +11001132 if (options.forward_agent) {
1133 debug("Requesting authentication agent forwarding.");
1134 auth_request_forwarding();
1135
1136 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +11001137 type = packet_read();
Damien Miller48b03fc2002-01-22 23:11:40 +11001138 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +11001139 if (type != SSH_SMSG_SUCCESS)
Damien Miller996acd22003-04-09 20:59:48 +10001140 logit("Warning: Remote host denied authentication agent forwarding.");
Damien Miller95def091999-11-25 00:26:21 +11001141 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001142
Damien Miller0bc1bd82000-11-13 22:57:25 +11001143 /* Initiate port forwardings. */
1144 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001145
Darren Tuckerb776c852007-12-02 23:06:35 +11001146 /* Execute a local command */
1147 if (options.local_command != NULL &&
1148 options.permit_local_command)
1149 ssh_local_cmd(options.local_command);
1150
Darren Tucker9a2a6092008-07-04 12:53:50 +10001151 /*
1152 * If requested and we are not interested in replies to remote
1153 * forwarding requests, then let ssh continue in the background.
1154 */
1155 if (fork_after_authentication_flag &&
1156 (!options.exit_on_forward_failure ||
1157 options.num_remote_forwards == 0)) {
1158 fork_after_authentication_flag = 0;
Damien Miller5428f641999-11-25 11:54:57 +11001159 if (daemon(1, 1) < 0)
1160 fatal("daemon() failed: %.200s", strerror(errno));
Darren Tucker9a2a6092008-07-04 12:53:50 +10001161 }
Damien Miller5428f641999-11-25 11:54:57 +11001162
1163 /*
1164 * If a command was specified on the command line, execute the
1165 * command now. Otherwise request the server to start a shell.
1166 */
Damien Miller95def091999-11-25 00:26:21 +11001167 if (buffer_len(&command) > 0) {
1168 int len = buffer_len(&command);
1169 if (len > 900)
1170 len = 900;
Darren Tuckerd6173c02008-06-13 04:52:53 +10001171 debug("Sending command: %.*s", len,
1172 (u_char *)buffer_ptr(&command));
Damien Miller95def091999-11-25 00:26:21 +11001173 packet_start(SSH_CMSG_EXEC_CMD);
1174 packet_put_string(buffer_ptr(&command), buffer_len(&command));
1175 packet_send();
1176 packet_write_wait();
1177 } else {
1178 debug("Requesting shell.");
1179 packet_start(SSH_CMSG_EXEC_SHELL);
1180 packet_send();
1181 packet_write_wait();
1182 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001183
Damien Miller95def091999-11-25 00:26:21 +11001184 /* Enter the interactive session. */
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001185 return client_loop(have_tty, tty_flag ?
1186 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
Damien Miller1383bd82000-04-06 12:32:37 +10001187}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001188
Ben Lindstromf558cf62001-09-20 23:13:49 +00001189/* request pty/x11/agent/tcpfwd/shell for channel */
Ben Lindstrombba81212001-06-25 05:01:22 +00001190static void
Damien Millerd530f5f2010-05-21 14:57:10 +10001191ssh_session2_setup(int id, int success, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +10001192{
Damien Miller3756dce2004-06-18 01:17:29 +10001193 extern char **environ;
Damien Miller17e7ed02005-06-17 12:54:33 +10001194 const char *display;
Damien Miller0e220db2004-06-15 10:34:08 +10001195 int interactive = tty_flag;
Damien Miller17e7ed02005-06-17 12:54:33 +10001196
Damien Millerd530f5f2010-05-21 14:57:10 +10001197 if (!success)
1198 return; /* No need for error message, channels code sens one */
1199
Damien Miller46d38de2005-07-17 17:02:09 +10001200 display = getenv("DISPLAY");
Damien Miller17e7ed02005-06-17 12:54:33 +10001201 if (options.forward_x11 && display != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001202 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001203 /* Get reasonable local authentication information. */
Damien Miller17e7ed02005-06-17 12:54:33 +10001204 client_x11_get_proto(display, options.xauth_location,
Damien Miller1ab6a512010-06-26 10:02:24 +10001205 options.forward_x11_trusted,
1206 options.forward_x11_timeout, &proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001207 /* Request forwarding with authentication spoofing. */
Darren Tuckerd6173c02008-06-13 04:52:53 +10001208 debug("Requesting X11 forwarding with authentication "
1209 "spoofing.");
Damien Miller17e7ed02005-06-17 12:54:33 +10001210 x11_request_forwarding_with_spoofing(id, display, proto, data);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001211 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001212 /* XXX wait for reply */
1213 }
1214
Damien Miller0bc1bd82000-11-13 22:57:25 +11001215 check_agent_present();
1216 if (options.forward_agent) {
1217 debug("Requesting authentication agent forwarding.");
1218 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1219 packet_send();
1220 }
1221
Damien Miller0e220db2004-06-15 10:34:08 +10001222 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
Damien Miller5771ed72008-05-19 15:35:33 +10001223 NULL, fileno(stdin), &command, environ);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001224
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001225 packet_set_interactive(interactive);
Damien Miller1383bd82000-04-06 12:32:37 +10001226}
1227
Ben Lindstromf558cf62001-09-20 23:13:49 +00001228/* open new channel for a session */
Ben Lindstrombba81212001-06-25 05:01:22 +00001229static int
Ben Lindstromf558cf62001-09-20 23:13:49 +00001230ssh_session2_open(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001231{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001232 Channel *c;
1233 int window, packetmax, in, out, err;
Damien Millerad833b32000-08-23 10:46:23 +10001234
Damien Millercaf6dd62000-08-29 11:33:50 +11001235 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001236 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001237 } else {
1238 in = dup(STDIN_FILENO);
1239 }
1240 out = dup(STDOUT_FILENO);
1241 err = dup(STDERR_FILENO);
1242
1243 if (in < 0 || out < 0 || err < 0)
1244 fatal("dup() in/out/err failed");
1245
Damien Miller69b69aa2000-10-28 14:19:58 +11001246 /* enable nonblocking unless tty */
1247 if (!isatty(in))
1248 set_nonblock(in);
1249 if (!isatty(out))
1250 set_nonblock(out);
1251 if (!isatty(err))
1252 set_nonblock(err);
1253
Damien Millere4340be2000-09-16 13:29:08 +11001254 window = CHAN_SES_WINDOW_DEFAULT;
1255 packetmax = CHAN_SES_PACKET_DEFAULT;
Damien Miller19a59452002-02-19 15:20:57 +11001256 if (tty_flag) {
1257 window >>= 1;
1258 packetmax >>= 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001259 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001260 c = channel_new(
Damien Miller1383bd82000-04-06 12:32:37 +10001261 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001262 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001263 "client-session", /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001264
Ben Lindstromf558cf62001-09-20 23:13:49 +00001265 debug3("ssh_session2_open: channel_new: %d", c->self);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001266
Ben Lindstrom5ec26452001-06-09 00:18:51 +00001267 channel_send_open(c->self);
Ben Lindstromf558cf62001-09-20 23:13:49 +00001268 if (!no_shell_flag)
Damien Millerb84886b2008-05-19 15:05:07 +10001269 channel_register_open_confirm(c->self,
1270 ssh_session2_setup, NULL);
Damien Miller1383bd82000-04-06 12:32:37 +10001271
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001272 return c->self;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001273}
1274
Ben Lindstrombba81212001-06-25 05:01:22 +00001275static int
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001276ssh_session2(void)
1277{
Ben Lindstromf558cf62001-09-20 23:13:49 +00001278 int id = -1;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001279
1280 /* XXX should be pre-session */
1281 ssh_init_forwarding();
1282
Ben Lindstromf558cf62001-09-20 23:13:49 +00001283 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1284 id = ssh_session2_open();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001285
Darren Tucker8901fa92008-06-11 09:34:01 +10001286 /* If we don't expect to open a new session, then disallow it */
Damien Miller456e6f02008-11-03 19:20:10 +11001287 if (options.control_master == SSHCTL_MASTER_NO &&
1288 (datafellows & SSH_NEW_OPENSSH)) {
Darren Tucker8901fa92008-06-11 09:34:01 +10001289 debug("Requesting no-more-sessions@openssh.com");
1290 packet_start(SSH2_MSG_GLOBAL_REQUEST);
1291 packet_put_cstring("no-more-sessions@openssh.com");
1292 packet_put_char(0);
1293 packet_send();
1294 }
1295
Damien Millerd27b9472005-12-13 19:29:02 +11001296 /* Execute a local command */
1297 if (options.local_command != NULL &&
1298 options.permit_local_command)
1299 ssh_local_cmd(options.local_command);
1300
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001301 /* Start listening for multiplex clients */
Damien Millerb1cbfa22008-05-19 16:00:08 +10001302 muxserver_listen();
Damien Millerb3ce9fe2007-08-08 14:32:41 +10001303
Damien Miller1f25ab42010-07-16 13:56:23 +10001304 /*
1305 * If requested and we are not interested in replies to remote
1306 * forwarding requests, then let ssh continue in the background.
1307 */
1308 if (fork_after_authentication_flag &&
1309 (!options.exit_on_forward_failure ||
1310 options.num_remote_forwards == 0)) {
Darren Tucker9a2a6092008-07-04 12:53:50 +10001311 fork_after_authentication_flag = 0;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001312 if (daemon(1, 1) < 0)
1313 fatal("daemon() failed: %.200s", strerror(errno));
Darren Tucker9a2a6092008-07-04 12:53:50 +10001314 }
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001315
Darren Tuckerf1de4e52010-01-08 16:54:59 +11001316 if (options.use_roaming)
1317 request_roaming();
1318
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001319 return client_loop(tty_flag, tty_flag ?
1320 options.escape_char : SSH_ESCAPECHAR_NONE, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001321}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001322
Ben Lindstrombba81212001-06-25 05:01:22 +00001323static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001324load_public_identity_files(void)
1325{
Damien Miller6b1d53c2006-03-31 23:13:21 +11001326 char *filename, *cp, thishost[NI_MAXHOST];
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001327 char *pwdir = NULL, *pwname = NULL;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001328 int i = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001329 Key *public;
Damien Miller6b1d53c2006-03-31 23:13:21 +11001330 struct passwd *pw;
Damien Miller0a80ca12010-02-27 07:55:05 +11001331 u_int n_ids;
1332 char *identity_files[SSH_MAX_IDENTITY_FILES];
1333 Key *identity_keys[SSH_MAX_IDENTITY_FILES];
Damien Miller7ea845e2010-02-12 09:21:02 +11001334#ifdef ENABLE_PKCS11
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001335 Key **keys;
Damien Miller7ea845e2010-02-12 09:21:02 +11001336 int nkeys;
Damien Miller0a80ca12010-02-27 07:55:05 +11001337#endif /* PKCS11 */
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001338
Damien Miller0a80ca12010-02-27 07:55:05 +11001339 n_ids = 0;
1340 bzero(identity_files, sizeof(identity_files));
1341 bzero(identity_keys, sizeof(identity_keys));
1342
1343#ifdef ENABLE_PKCS11
Damien Miller7ea845e2010-02-12 09:21:02 +11001344 if (options.pkcs11_provider != NULL &&
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001345 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
Damien Miller7ea845e2010-02-12 09:21:02 +11001346 (pkcs11_init(!options.batch_mode) == 0) &&
1347 (nkeys = pkcs11_add_provider(options.pkcs11_provider, NULL,
1348 &keys)) > 0) {
Damien Miller7ea845e2010-02-12 09:21:02 +11001349 for (i = 0; i < nkeys; i++) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001350 if (n_ids >= SSH_MAX_IDENTITY_FILES) {
1351 key_free(keys[i]);
1352 continue;
1353 }
1354 identity_keys[n_ids] = keys[i];
1355 identity_files[n_ids] =
Damien Miller7ea845e2010-02-12 09:21:02 +11001356 xstrdup(options.pkcs11_provider); /* XXX */
Damien Miller0a80ca12010-02-27 07:55:05 +11001357 n_ids++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001358 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001359 xfree(keys);
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001360 }
Damien Miller7ea845e2010-02-12 09:21:02 +11001361#endif /* ENABLE_PKCS11 */
Damien Miller6b1d53c2006-03-31 23:13:21 +11001362 if ((pw = getpwuid(original_real_uid)) == NULL)
1363 fatal("load_public_identity_files: getpwuid failed");
Darren Tuckere143f062007-12-02 23:21:16 +11001364 pwname = xstrdup(pw->pw_name);
1365 pwdir = xstrdup(pw->pw_dir);
Damien Miller6b1d53c2006-03-31 23:13:21 +11001366 if (gethostname(thishost, sizeof(thishost)) == -1)
1367 fatal("load_public_identity_files: gethostname: %s",
1368 strerror(errno));
Damien Miller0a80ca12010-02-27 07:55:05 +11001369 for (i = 0; i < options.num_identity_files; i++) {
1370 if (n_ids >= SSH_MAX_IDENTITY_FILES) {
1371 xfree(options.identity_files[i]);
1372 continue;
1373 }
Damien Miller6b1d53c2006-03-31 23:13:21 +11001374 cp = tilde_expand_filename(options.identity_files[i],
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001375 original_real_uid);
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001376 filename = percent_expand(cp, "d", pwdir,
1377 "u", pwname, "l", thishost, "h", host,
Damien Miller6b1d53c2006-03-31 23:13:21 +11001378 "r", options.user, (char *)NULL);
1379 xfree(cp);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001380 public = key_load_public(filename, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001381 debug("identity file %s type %d", filename,
1382 public ? public->type : -1);
1383 xfree(options.identity_files[i]);
Damien Miller0a80ca12010-02-27 07:55:05 +11001384 identity_files[n_ids] = filename;
1385 identity_keys[n_ids] = public;
1386
1387 if (++n_ids >= SSH_MAX_IDENTITY_FILES)
1388 continue;
1389
1390 /* Try to add the certificate variant too */
1391 xasprintf(&cp, "%s-cert", filename);
1392 public = key_load_public(cp, NULL);
1393 debug("identity file %s type %d", cp,
1394 public ? public->type : -1);
1395 if (public == NULL) {
1396 xfree(cp);
1397 continue;
1398 }
1399 if (!key_is_cert(public)) {
1400 debug("%s: key %s type %s is not a certificate",
1401 __func__, cp, key_type(public));
1402 key_free(public);
1403 xfree(cp);
1404 continue;
1405 }
1406 identity_keys[n_ids] = public;
1407 /* point to the original path, most likely the private key */
1408 identity_files[n_ids] = xstrdup(filename);
1409 n_ids++;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001410 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001411 options.num_identity_files = n_ids;
1412 memcpy(options.identity_files, identity_files, sizeof(identity_files));
1413 memcpy(options.identity_keys, identity_keys, sizeof(identity_keys));
1414
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001415 bzero(pwname, strlen(pwname));
Darren Tuckere143f062007-12-02 23:21:16 +11001416 xfree(pwname);
Darren Tuckerb4fbbc62007-12-02 23:16:32 +11001417 bzero(pwdir, strlen(pwdir));
Darren Tuckere143f062007-12-02 23:21:16 +11001418 xfree(pwdir);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001419}