blob: dd926b7e7ac8870da807e5ef638597a276a608fd [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * Ssh client program. This program can be used to log into a remote machine.
6 * The software supports strong authentication, encryption, and forwarding
7 * of X11, TCP/IP, and authentication connections.
8 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
14 *
15 * Copyright (c) 1999 Niels Provos. All rights reserved.
16 *
17 * Modified to work with SSL by Niels Provos <provos@citi.umich.edu>
18 * in Canada (German citizen).
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
30 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
31 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
33 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
34 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
38 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110039 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040
41#include "includes.h"
Ben Lindstrom0936a5b2002-03-26 03:17:42 +000042RCSID("$OpenBSD: ssh.c,v 1.167 2002/03/25 17:34:27 markus Exp $");
Damien Millereba71ba2000-04-29 23:57:08 +100043
44#include <openssl/evp.h>
Damien Miller0bc1bd82000-11-13 22:57:25 +110045#include <openssl/err.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100046
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000048#include "ssh1.h"
Damien Miller1383bd82000-04-06 12:32:37 +100049#include "ssh2.h"
50#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051#include "cipher.h"
52#include "xmalloc.h"
53#include "packet.h"
54#include "buffer.h"
55#include "uidswap.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000056#include "channels.h"
Damien Millereba71ba2000-04-29 23:57:08 +100057#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100058#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100059#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000060#include "pathnames.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +000061#include "clientloop.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000062#include "log.h"
63#include "readconf.h"
64#include "sshconnect.h"
65#include "tildexpand.h"
Ben Lindstrom1e7d3062001-02-09 02:36:43 +000066#include "dispatch.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000067#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000068#include "kex.h"
69#include "mac.h"
Ben Lindstromae8e2d32001-04-14 23:13:02 +000070#include "sshtty.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071
Ben Lindstromc5b68002001-07-04 04:52:03 +000072#ifdef SMARTCARD
Ben Lindstromc5b68002001-07-04 04:52:03 +000073#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000074#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +000075
Damien Miller3f905871999-11-15 17:10:57 +110076#ifdef HAVE___PROGNAME
77extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000078#else
79char *__progname;
80#endif
Damien Miller3f905871999-11-15 17:10:57 +110081
Damien Miller34132e52000-01-14 15:45:46 +110082/* Flag indicating whether IPv4 or IPv6. This can be set on the command line.
83 Default value is AF_UNSPEC means both IPv4 and IPv6. */
Damien Miller7d80e342000-01-19 14:36:49 +110084#ifdef IPV4_DEFAULT
85int IPv4or6 = AF_INET;
86#else
Damien Miller34132e52000-01-14 15:45:46 +110087int IPv4or6 = AF_UNSPEC;
Damien Miller7d80e342000-01-19 14:36:49 +110088#endif
Damien Miller34132e52000-01-14 15:45:46 +110089
Damien Miller95def091999-11-25 00:26:21 +110090/* Flag indicating whether debug mode is on. This can be set on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091int debug_flag = 0;
92
Damien Miller78928792000-04-12 20:17:38 +100093/* Flag indicating whether a tty should be allocated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094int tty_flag = 0;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +000095int no_tty_flag = 0;
96int force_tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097
Damien Miller1383bd82000-04-06 12:32:37 +100098/* don't exec a shell */
99int no_shell_flag = 0;
Damien Miller1383bd82000-04-06 12:32:37 +1000100
Damien Miller5428f641999-11-25 11:54:57 +1100101/*
102 * Flag indicating that nothing should be read from stdin. This can be set
103 * on the command line.
104 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105int stdin_null_flag = 0;
106
Damien Miller5428f641999-11-25 11:54:57 +1100107/*
108 * Flag indicating that ssh should fork after authentication. This is useful
109 * so that the pasphrase can be entered manually, and then ssh goes to the
110 * background.
111 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112int fork_after_authentication_flag = 0;
113
Damien Miller5428f641999-11-25 11:54:57 +1100114/*
115 * General data structure for command line options and options configurable
116 * in configuration files. See readconf.h.
117 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118Options options;
119
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000120/* optional user configfile */
121char *config = NULL;
122
Damien Miller5428f641999-11-25 11:54:57 +1100123/*
124 * Name of the host we are connecting to. This is the name given on the
125 * command line, or the HostName specified for the user-supplied name in a
126 * configuration file.
127 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128char *host;
129
130/* socket address the host resolves to */
Damien Miller34132e52000-01-14 15:45:46 +1100131struct sockaddr_storage hostaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000133/* Private host keys. */
134struct {
135 Key **keys;
136 int nkeys;
137} sensitive_data;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138
139/* Original real UID. */
140uid_t original_real_uid;
141
Damien Miller1383bd82000-04-06 12:32:37 +1000142/* command to be executed */
143Buffer command;
144
Damien Miller832562e2001-01-30 09:30:01 +1100145/* Should we execute a command or invoke a subsystem? */
146int subsystem_flag = 0;
147
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148/* Prints a help message to the user. This function never returns. */
149
Ben Lindstrombba81212001-06-25 05:01:22 +0000150static void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000151usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152{
Kevin Stevesec84dc12000-12-13 17:45:15 +0000153 fprintf(stderr, "Usage: %s [options] host [command]\n", __progname);
Damien Miller95def091999-11-25 00:26:21 +1100154 fprintf(stderr, "Options:\n");
155 fprintf(stderr, " -l user Log in using this user name.\n");
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000156 fprintf(stderr, " -n Redirect input from " _PATH_DEVNULL ".\n");
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000157 fprintf(stderr, " -F config Config file (default: ~/%s).\n",
158 _PATH_SSH_USER_CONFFILE);
Damien Millerb1715dc2000-05-30 13:44:51 +1000159 fprintf(stderr, " -A Enable authentication agent forwarding.\n");
Ben Lindstrom3b89c5e2001-06-05 20:44:16 +0000160 fprintf(stderr, " -a Disable authentication agent forwarding (default).\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000161#ifdef AFS
Damien Miller95def091999-11-25 00:26:21 +1100162 fprintf(stderr, " -k Disable Kerberos ticket and AFS token forwarding.\n");
163#endif /* AFS */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000164 fprintf(stderr, " -X Enable X11 connection forwarding.\n");
Ben Lindstrom3b89c5e2001-06-05 20:44:16 +0000165 fprintf(stderr, " -x Disable X11 connection forwarding (default).\n");
Ben Lindstrom0ab2a012001-03-05 06:45:21 +0000166 fprintf(stderr, " -i file Identity for public key authentication "
167 "(default: ~/.ssh/identity)\n");
Ben Lindstrom61eb9562001-08-06 21:53:42 +0000168#ifdef SMARTCARD
169 fprintf(stderr, " -I reader Set smartcard reader.\n");
Ben Lindstrombcc18082001-08-06 21:59:25 +0000170#endif
Damien Miller95def091999-11-25 00:26:21 +1100171 fprintf(stderr, " -t Tty; allocate a tty even if command is given.\n");
Damien Miller1383bd82000-04-06 12:32:37 +1000172 fprintf(stderr, " -T Do not allocate a tty.\n");
Damien Miller95def091999-11-25 00:26:21 +1100173 fprintf(stderr, " -v Verbose; display verbose debugging messages.\n");
Damien Millere4340be2000-09-16 13:29:08 +1100174 fprintf(stderr, " Multiple -v increases verbosity.\n");
Damien Miller95def091999-11-25 00:26:21 +1100175 fprintf(stderr, " -V Display version number only.\n");
176 fprintf(stderr, " -P Don't allocate a privileged port.\n");
177 fprintf(stderr, " -q Quiet; don't display any warning messages.\n");
178 fprintf(stderr, " -f Fork into background after authentication.\n");
179 fprintf(stderr, " -e char Set escape character; ``none'' = disable (default: ~).\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000180
Ben Lindstrom3b89c5e2001-06-05 20:44:16 +0000181 fprintf(stderr, " -c cipher Select encryption algorithm\n");
Ben Lindstrom3d73a342001-03-05 07:39:01 +0000182 fprintf(stderr, " -m macs Specify MAC algorithms for protocol version 2.\n");
Damien Miller95def091999-11-25 00:26:21 +1100183 fprintf(stderr, " -p port Connect to this port. Server must be on the same port.\n");
184 fprintf(stderr, " -L listen-port:host:port Forward local port to remote address\n");
185 fprintf(stderr, " -R listen-port:host:port Forward remote port to local address\n");
Kevin Stevesec84dc12000-12-13 17:45:15 +0000186 fprintf(stderr, " These cause %s to listen for connections on a port, and\n", __progname);
Damien Miller95def091999-11-25 00:26:21 +1100187 fprintf(stderr, " forward them to the other side by connecting to host:port.\n");
Ben Lindstrom19ceb172001-09-12 17:54:24 +0000188 fprintf(stderr, " -D port Enable dynamic application-level port forwarding.\n");
Damien Miller95def091999-11-25 00:26:21 +1100189 fprintf(stderr, " -C Enable compression.\n");
Damien Miller1383bd82000-04-06 12:32:37 +1000190 fprintf(stderr, " -N Do not execute a shell or command.\n");
Damien Miller95def091999-11-25 00:26:21 +1100191 fprintf(stderr, " -g Allow remote hosts to connect to forwarded ports.\n");
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000192 fprintf(stderr, " -1 Force protocol version 1.\n");
193 fprintf(stderr, " -2 Force protocol version 2.\n");
Damien Miller34132e52000-01-14 15:45:46 +1100194 fprintf(stderr, " -4 Use IPv4 only.\n");
195 fprintf(stderr, " -6 Use IPv6 only.\n");
Damien Miller95def091999-11-25 00:26:21 +1100196 fprintf(stderr, " -o 'option' Process the option as if it was read from a configuration file.\n");
Damien Miller832562e2001-01-30 09:30:01 +1100197 fprintf(stderr, " -s Invoke command (mandatory) as SSH2 subsystem.\n");
Ben Lindstrom3b89c5e2001-06-05 20:44:16 +0000198 fprintf(stderr, " -b addr Local IP address.\n");
Damien Miller95def091999-11-25 00:26:21 +1100199 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000200}
201
Damien Miller95def091999-11-25 00:26:21 +1100202/*
203 * Connects to the given host using rsh (or prints an error message and exits
204 * if rsh is not available). This function never returns.
205 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000206static void
Damien Miller95def091999-11-25 00:26:21 +1100207rsh_connect(char *host, char *user, Buffer * command)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000208{
Damien Miller95def091999-11-25 00:26:21 +1100209 char *args[10];
210 int i;
211
212 log("Using rsh. WARNING: Connection will not be encrypted.");
213 /* Build argument list for rsh. */
214 i = 0;
215 args[i++] = _PATH_RSH;
216 /* host may have to come after user on some systems */
217 args[i++] = host;
218 if (user) {
219 args[i++] = "-l";
220 args[i++] = user;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000221 }
Damien Miller95def091999-11-25 00:26:21 +1100222 if (buffer_len(command) > 0) {
223 buffer_append(command, "\0", 1);
224 args[i++] = buffer_ptr(command);
225 }
226 args[i++] = NULL;
227 if (debug_flag) {
228 for (i = 0; args[i]; i++) {
229 if (i != 0)
230 fprintf(stderr, " ");
231 fprintf(stderr, "%s", args[i]);
232 }
233 fprintf(stderr, "\n");
234 }
235 execv(_PATH_RSH, args);
236 perror(_PATH_RSH);
237 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000238}
239
Ben Lindstrombba81212001-06-25 05:01:22 +0000240static int ssh_session(void);
241static int ssh_session2(void);
242static void load_public_identity_files(void);
Damien Miller1383bd82000-04-06 12:32:37 +1000243
Damien Miller95def091999-11-25 00:26:21 +1100244/*
245 * Main program for the ssh client.
246 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000247int
248main(int ac, char **av)
249{
Ben Lindstromf9cedb92001-08-06 21:07:11 +0000250 int i, opt, exit_status, cerr;
Damien Milleraae6c611999-12-06 11:47:28 +1100251 u_short fwd_port, fwd_host_port;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000252 char sfwd_port[6], sfwd_host_port[6];
Damien Millerf4614452001-07-14 12:18:10 +1000253 char *p, *cp, buf[256];
Damien Miller95def091999-11-25 00:26:21 +1100254 struct stat st;
Ben Lindstrom086cf212001-03-05 05:56:40 +0000255 struct passwd *pw;
Damien Miller1383bd82000-04-06 12:32:37 +1000256 int dummy;
Damien Miller95def091999-11-25 00:26:21 +1100257 uid_t original_effective_uid;
Ben Lindstrom5ccf63a2001-09-24 20:00:10 +0000258 extern int optind, optreset;
Damien Miller4f8e6692001-07-14 13:22:53 +1000259 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000260
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000261 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000262 init_rng();
263
Damien Miller5428f641999-11-25 11:54:57 +1100264 /*
265 * Save the original real uid. It will be needed later (uid-swapping
266 * may clobber the real uid).
267 */
Damien Miller95def091999-11-25 00:26:21 +1100268 original_real_uid = getuid();
269 original_effective_uid = geteuid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270
Damien Miller05dd7952000-10-01 00:42:48 +1100271#ifdef HAVE_SETRLIMIT
Damien Miller95def091999-11-25 00:26:21 +1100272 /* If we are installed setuid root be careful to not drop core. */
273 if (original_real_uid != original_effective_uid) {
274 struct rlimit rlim;
275 rlim.rlim_cur = rlim.rlim_max = 0;
276 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
277 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000278 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100279#endif
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000280 /* Get user data. */
281 pw = getpwuid(original_real_uid);
282 if (!pw) {
283 log("You don't exist, go away!");
284 exit(1);
285 }
286 /* Take a copy of the returned structure. */
287 pw = pwcopy(pw);
288
Damien Miller5428f641999-11-25 11:54:57 +1100289 /*
290 * Use uid-swapping to give up root privileges for the duration of
291 * option processing. We will re-instantiate the rights when we are
292 * ready to create the privileged port, and will permanently drop
293 * them when the port has been created (actually, when the connection
294 * has been made, as we may need to create the port several times).
295 */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000296 temporarily_use_uid(pw);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000297
Damien Miller5428f641999-11-25 11:54:57 +1100298 /*
299 * Set our umask to something reasonable, as some files are created
300 * with the default umask. This will make them world-readable but
301 * writable only by the owner, which is ok for all files for which we
302 * don't set the modes explicitly.
303 */
Damien Miller95def091999-11-25 00:26:21 +1100304 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000305
Damien Miller95def091999-11-25 00:26:21 +1100306 /* Initialize option structure to indicate that no values have been set. */
307 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000308
Damien Miller95def091999-11-25 00:26:21 +1100309 /* Parse command-line arguments. */
310 host = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000311
Damien Millerf4614452001-07-14 12:18:10 +1000312again:
313 while ((opt = getopt(ac, av,
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000314 "1246ab:c:e:fgi:kl:m:no:p:qstvxACD:F:I:L:NPR:TVX")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100315 switch (opt) {
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000316 case '1':
317 options.protocol = SSH_PROTO_1;
318 break;
Damien Miller4af51302000-04-16 11:18:38 +1000319 case '2':
320 options.protocol = SSH_PROTO_2;
321 break;
Damien Miller34132e52000-01-14 15:45:46 +1100322 case '4':
323 IPv4or6 = AF_INET;
324 break;
Damien Miller34132e52000-01-14 15:45:46 +1100325 case '6':
326 IPv4or6 = AF_INET6;
327 break;
Damien Miller95def091999-11-25 00:26:21 +1100328 case 'n':
329 stdin_null_flag = 1;
330 break;
Damien Miller95def091999-11-25 00:26:21 +1100331 case 'f':
332 fork_after_authentication_flag = 1;
333 stdin_null_flag = 1;
334 break;
Damien Miller95def091999-11-25 00:26:21 +1100335 case 'x':
336 options.forward_x11 = 0;
337 break;
Damien Miller95def091999-11-25 00:26:21 +1100338 case 'X':
339 options.forward_x11 = 1;
340 break;
Damien Miller95def091999-11-25 00:26:21 +1100341 case 'g':
342 options.gateway_ports = 1;
343 break;
Damien Miller95def091999-11-25 00:26:21 +1100344 case 'P':
345 options.use_privileged_port = 0;
346 break;
Damien Miller95def091999-11-25 00:26:21 +1100347 case 'a':
348 options.forward_agent = 0;
349 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000350 case 'A':
351 options.forward_agent = 1;
352 break;
Damien Miller95def091999-11-25 00:26:21 +1100353#ifdef AFS
354 case 'k':
355 options.kerberos_tgt_passing = 0;
356 options.afs_token_passing = 0;
357 break;
358#endif
359 case 'i':
360 if (stat(optarg, &st) < 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000361 fprintf(stderr, "Warning: Identity file %s "
362 "does not exist.\n", optarg);
Damien Miller95def091999-11-25 00:26:21 +1100363 break;
364 }
Damien Millerf4614452001-07-14 12:18:10 +1000365 if (options.num_identity_files >=
366 SSH_MAX_IDENTITY_FILES)
367 fatal("Too many identity files specified "
368 "(max %d)", SSH_MAX_IDENTITY_FILES);
369 options.identity_files[options.num_identity_files++] =
370 xstrdup(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100371 break;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000372 case 'I':
373#ifdef SMARTCARD
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000374 options.smartcard_device = xstrdup(optarg);
Ben Lindstrombcc18082001-08-06 21:59:25 +0000375#else
Ben Lindstromc5b68002001-07-04 04:52:03 +0000376 fprintf(stderr, "no support for smartcards.\n");
Ben Lindstrombcc18082001-08-06 21:59:25 +0000377#endif
Ben Lindstromc5b68002001-07-04 04:52:03 +0000378 break;
Damien Miller95def091999-11-25 00:26:21 +1100379 case 't':
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000380 if (tty_flag)
381 force_tty_flag = 1;
Damien Miller95def091999-11-25 00:26:21 +1100382 tty_flag = 1;
383 break;
Damien Miller95def091999-11-25 00:26:21 +1100384 case 'v':
Damien Millere4340be2000-09-16 13:29:08 +1100385 if (0 == debug_flag) {
386 debug_flag = 1;
387 options.log_level = SYSLOG_LEVEL_DEBUG1;
388 } else if (options.log_level < SYSLOG_LEVEL_DEBUG3) {
389 options.log_level++;
390 break;
Damien Millerf4614452001-07-14 12:18:10 +1000391 } else
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000392 fatal("Too high debugging level.");
Damien Millere4340be2000-09-16 13:29:08 +1100393 /* fallthrough */
Damien Miller95def091999-11-25 00:26:21 +1100394 case 'V':
Damien Miller225736c2001-02-19 21:51:08 +1100395 fprintf(stderr,
396 "%s, SSH protocols %d.%d/%d.%d, OpenSSL 0x%8.8lx\n",
Damien Miller78928792000-04-12 20:17:38 +1000397 SSH_VERSION,
398 PROTOCOL_MAJOR_1, PROTOCOL_MINOR_1,
Damien Miller225736c2001-02-19 21:51:08 +1100399 PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2,
400 SSLeay());
Damien Miller95def091999-11-25 00:26:21 +1100401 if (opt == 'V')
402 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100403 break;
Damien Miller95def091999-11-25 00:26:21 +1100404 case 'q':
405 options.log_level = SYSLOG_LEVEL_QUIET;
406 break;
Damien Miller95def091999-11-25 00:26:21 +1100407 case 'e':
408 if (optarg[0] == '^' && optarg[2] == 0 &&
Damien Millerf4614452001-07-14 12:18:10 +1000409 (u_char) optarg[1] >= 64 &&
410 (u_char) optarg[1] < 128)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000411 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100412 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000413 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100414 else if (strcmp(optarg, "none") == 0)
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +0000415 options.escape_char = SSH_ESCAPECHAR_NONE;
Damien Miller95def091999-11-25 00:26:21 +1100416 else {
Damien Millerf4614452001-07-14 12:18:10 +1000417 fprintf(stderr, "Bad escape character '%s'.\n",
418 optarg);
Damien Miller95def091999-11-25 00:26:21 +1100419 exit(1);
420 }
421 break;
Damien Miller95def091999-11-25 00:26:21 +1100422 case 'c':
Damien Millereba71ba2000-04-29 23:57:08 +1000423 if (ciphers_valid(optarg)) {
424 /* SSH2 only */
425 options.ciphers = xstrdup(optarg);
Damien Miller30c3d422000-05-09 11:02:59 +1000426 options.cipher = SSH_CIPHER_ILLEGAL;
Damien Millereba71ba2000-04-29 23:57:08 +1000427 } else {
428 /* SSH1 only */
Damien Millere39cacc2000-11-29 12:18:44 +1100429 options.cipher = cipher_number(optarg);
430 if (options.cipher == -1) {
Damien Millerf4614452001-07-14 12:18:10 +1000431 fprintf(stderr,
432 "Unknown cipher type '%s'\n",
433 optarg);
Damien Millereba71ba2000-04-29 23:57:08 +1000434 exit(1);
435 }
Damien Millerf4614452001-07-14 12:18:10 +1000436 if (options.cipher == SSH_CIPHER_3DES)
Damien Millere39cacc2000-11-29 12:18:44 +1100437 options.ciphers = "3des-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000438 else if (options.cipher == SSH_CIPHER_BLOWFISH)
Damien Millere39cacc2000-11-29 12:18:44 +1100439 options.ciphers = "blowfish-cbc";
Damien Millerf4614452001-07-14 12:18:10 +1000440 else
Damien Millere39cacc2000-11-29 12:18:44 +1100441 options.ciphers = (char *)-1;
Damien Miller95def091999-11-25 00:26:21 +1100442 }
443 break;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000444 case 'm':
445 if (mac_valid(optarg))
446 options.macs = xstrdup(optarg);
447 else {
Damien Millerf4614452001-07-14 12:18:10 +1000448 fprintf(stderr, "Unknown mac type '%s'\n",
449 optarg);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000450 exit(1);
451 }
452 break;
Damien Miller95def091999-11-25 00:26:21 +1100453 case 'p':
Ben Lindstrom19066a12001-04-12 23:39:26 +0000454 options.port = a2port(optarg);
455 if (options.port == 0) {
Ben Lindstrom146edb92001-04-11 23:06:28 +0000456 fprintf(stderr, "Bad port '%s'\n", optarg);
457 exit(1);
458 }
Damien Miller95def091999-11-25 00:26:21 +1100459 break;
Damien Miller95def091999-11-25 00:26:21 +1100460 case 'l':
461 options.user = optarg;
462 break;
Ben Lindstrom1a174712001-09-12 17:56:15 +0000463
Damien Miller95def091999-11-25 00:26:21 +1100464 case 'L':
Ben Lindstrom1a174712001-09-12 17:56:15 +0000465 case 'R':
466 if (sscanf(optarg, "%5[0-9]:%255[^:]:%5[0-9]",
467 sfwd_port, buf, sfwd_host_port) != 3 &&
468 sscanf(optarg, "%5[0-9]/%255[^/]/%5[0-9]",
469 sfwd_port, buf, sfwd_host_port) != 3) {
Damien Millerf4614452001-07-14 12:18:10 +1000470 fprintf(stderr,
Ben Lindstrom1a174712001-09-12 17:56:15 +0000471 "Bad forwarding specification '%s'\n",
Damien Millerf4614452001-07-14 12:18:10 +1000472 optarg);
Damien Miller95def091999-11-25 00:26:21 +1100473 usage();
474 /* NOTREACHED */
475 }
Ben Lindstrom1a174712001-09-12 17:56:15 +0000476 if ((fwd_port = a2port(sfwd_port)) == 0 ||
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000477 (fwd_host_port = a2port(sfwd_host_port)) == 0) {
Ben Lindstrom1a174712001-09-12 17:56:15 +0000478 fprintf(stderr,
479 "Bad forwarding port(s) '%s'\n", optarg);
480 exit(1);
481 }
482 if (opt == 'L')
483 add_local_forward(&options, fwd_port, buf,
484 fwd_host_port);
485 else if (opt == 'R')
486 add_remote_forward(&options, fwd_port, buf,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100487 fwd_host_port);
Damien Miller95def091999-11-25 00:26:21 +1100488 break;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000489
490 case 'D':
Ben Lindstrom19066a12001-04-12 23:39:26 +0000491 fwd_port = a2port(optarg);
492 if (fwd_port == 0) {
Damien Millerf4614452001-07-14 12:18:10 +1000493 fprintf(stderr, "Bad dynamic port '%s'\n",
494 optarg);
Ben Lindstrom146edb92001-04-11 23:06:28 +0000495 exit(1);
496 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000497 add_local_forward(&options, fwd_port, "socks4", 0);
498 break;
499
Damien Miller95def091999-11-25 00:26:21 +1100500 case 'C':
501 options.compression = 1;
502 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000503 case 'N':
504 no_shell_flag = 1;
505 no_tty_flag = 1;
506 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000507 case 'T':
508 no_tty_flag = 1;
509 break;
Damien Miller95def091999-11-25 00:26:21 +1100510 case 'o':
511 dummy = 1;
Damien Millerf4614452001-07-14 12:18:10 +1000512 if (process_config_line(&options, host ? host : "",
513 optarg, "command-line", 0, &dummy) != 0)
Damien Miller95def091999-11-25 00:26:21 +1100514 exit(1);
515 break;
Damien Miller832562e2001-01-30 09:30:01 +1100516 case 's':
517 subsystem_flag = 1;
518 break;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000519 case 'b':
520 options.bind_address = optarg;
521 break;
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000522 case 'F':
523 config = optarg;
524 break;
Damien Miller95def091999-11-25 00:26:21 +1100525 default:
526 usage();
527 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000528 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000529
Damien Millerf4614452001-07-14 12:18:10 +1000530 ac -= optind;
531 av += optind;
532
533 if (ac > 0 && !host && **av != '-') {
534 if (strchr(*av, '@')) {
535 p = xstrdup(*av);
536 cp = strchr(p, '@');
537 if (cp == NULL || cp == p)
538 usage();
539 options.user = p;
540 *cp = '\0';
541 host = ++cp;
542 } else
543 host = *av;
544 ac--, av++;
545 if (ac > 0) {
546 optind = 0;
547 optreset = 1;
548 goto again;
549 }
550 }
551
Damien Miller95def091999-11-25 00:26:21 +1100552 /* Check that we got a host name. */
553 if (!host)
554 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000555
Damien Millercb5e44a2000-09-29 12:12:36 +1100556 SSLeay_add_all_algorithms();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100557 ERR_load_crypto_strings();
Ben Lindstrom908afed2001-10-03 17:34:59 +0000558 channel_set_af(IPv4or6);
Damien Millercb5e44a2000-09-29 12:12:36 +1100559
Damien Miller95def091999-11-25 00:26:21 +1100560 /* Initialize the command to execute on remote host. */
561 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000562
Damien Miller5428f641999-11-25 11:54:57 +1100563 /*
564 * Save the command to execute on the remote host in a buffer. There
565 * is no limit on the length of the command, except by the maximum
566 * packet size. Also sets the tty flag if there is no command.
567 */
Damien Millerf4614452001-07-14 12:18:10 +1000568 if (!ac) {
Damien Miller95def091999-11-25 00:26:21 +1100569 /* No command specified - execute shell on a tty. */
570 tty_flag = 1;
Damien Miller832562e2001-01-30 09:30:01 +1100571 if (subsystem_flag) {
Damien Millerf4614452001-07-14 12:18:10 +1000572 fprintf(stderr,
573 "You must specify a subsystem to invoke.\n");
Damien Miller832562e2001-01-30 09:30:01 +1100574 usage();
575 }
Damien Miller95def091999-11-25 00:26:21 +1100576 } else {
Damien Millerf4614452001-07-14 12:18:10 +1000577 /* A command has been specified. Store it into the buffer. */
578 for (i = 0; i < ac; i++) {
579 if (i)
Damien Miller95def091999-11-25 00:26:21 +1100580 buffer_append(&command, " ", 1);
581 buffer_append(&command, av[i], strlen(av[i]));
582 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000583 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000584
Damien Miller95def091999-11-25 00:26:21 +1100585 /* Cannot fork to background if no command. */
Damien Millercaf6dd62000-08-29 11:33:50 +1100586 if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
Damien Miller95def091999-11-25 00:26:21 +1100587 fatal("Cannot fork into background without a command to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000588
Damien Miller95def091999-11-25 00:26:21 +1100589 /* Allocate a tty by default if no command specified. */
590 if (buffer_len(&command) == 0)
591 tty_flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000592
Ben Lindstromc72745a2000-12-02 19:03:54 +0000593 /* Force no tty*/
594 if (no_tty_flag)
595 tty_flag = 0;
Damien Miller95def091999-11-25 00:26:21 +1100596 /* Do not allocate a tty if stdin is not a tty. */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000597 if (!isatty(fileno(stdin)) && !force_tty_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100598 if (tty_flag)
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000599 log("Pseudo-terminal will not be allocated because stdin is not a terminal.");
Damien Miller95def091999-11-25 00:26:21 +1100600 tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000601 }
Damien Miller1383bd82000-04-06 12:32:37 +1000602
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000603 /*
604 * Initialize "log" output. Since we are the client all output
605 * actually goes to stderr.
606 */
Ben Lindstrom2b646522001-04-12 16:16:57 +0000607 log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
608 SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000609
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000610 /*
611 * Read per-user configuration file. Ignore the system wide config
612 * file if the user specifies a config file on the command line.
613 */
614 if (config != NULL) {
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000615 if (!read_config_file(config, host, &options))
616 fatal("Can't open user config file %.100s: "
617 "%.100s", config, strerror(errno));
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000618 } else {
619 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
620 _PATH_SSH_USER_CONFFILE);
Ben Lindstromedc0cf22001-09-12 18:32:20 +0000621 (void)read_config_file(buf, host, &options);
Ben Lindstrom83f07d12001-10-03 17:22:29 +0000622
623 /* Read systemwide configuration file after use config. */
624 (void)read_config_file(_PATH_HOST_CONFIG_FILE, host, &options);
Ben Lindstrom14f31ab2001-09-12 17:48:04 +0000625 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000626
Damien Miller95def091999-11-25 00:26:21 +1100627 /* Fill configuration defaults. */
628 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000629
Damien Miller95def091999-11-25 00:26:21 +1100630 /* reinit */
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000631 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000632
Damien Miller60bc5172001-03-19 09:38:15 +1100633 seed_rng();
634
Damien Miller95def091999-11-25 00:26:21 +1100635 if (options.user == NULL)
636 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000637
Damien Miller95def091999-11-25 00:26:21 +1100638 if (options.hostname != NULL)
639 host = options.hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000640
Damien Miller95def091999-11-25 00:26:21 +1100641 /* Disable rhosts authentication if not running as root. */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100642#ifdef HAVE_CYGWIN
643 /* Ignore uid if running under Windows */
644 if (!options.use_privileged_port) {
645#else
Damien Miller95def091999-11-25 00:26:21 +1100646 if (original_effective_uid != 0 || !options.use_privileged_port) {
Damien Millerbac2d8a2000-09-05 16:13:06 +1100647#endif
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000648 debug("Rhosts Authentication disabled, "
649 "originating port will not be trusted.");
Damien Miller95def091999-11-25 00:26:21 +1100650 options.rhosts_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100651 }
Damien Miller5428f641999-11-25 11:54:57 +1100652 /*
653 * If using rsh has been selected, exec it now (without trying
654 * anything else). Note that we must release privileges first.
655 */
Damien Miller95def091999-11-25 00:26:21 +1100656 if (options.use_rsh) {
Damien Miller5428f641999-11-25 11:54:57 +1100657 /*
658 * Restore our superuser privileges. This must be done
659 * before permanently setting the uid.
660 */
Damien Miller95def091999-11-25 00:26:21 +1100661 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000662
Damien Miller95def091999-11-25 00:26:21 +1100663 /* Switch to the original uid permanently. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000664 permanently_set_uid(pw);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000665
Damien Miller95def091999-11-25 00:26:21 +1100666 /* Execute rsh. */
667 rsh_connect(host, options.user, &command);
668 fatal("rsh_connect returned");
669 }
670 /* Restore our superuser privileges. */
671 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000672
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000673 /* Open a connection to the remote host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000674
Damien Miller139d4cd2001-10-10 15:07:44 +1000675 cerr = ssh_connect(host, &hostaddr, options.port, IPv4or6,
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000676 options.connection_attempts,
677 original_effective_uid != 0 || !options.use_privileged_port,
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000678 pw, options.proxy_command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000679
Damien Miller5428f641999-11-25 11:54:57 +1100680 /*
681 * If we successfully made the connection, load the host private key
682 * in case we will need it later for combined rsa-rhosts
683 * authentication. This must be done before releasing extra
684 * privileges, because the file is only readable by root.
685 */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000686 sensitive_data.nkeys = 0;
687 sensitive_data.keys = NULL;
Ben Lindstromf9cedb92001-08-06 21:07:11 +0000688 if (!cerr && (options.rhosts_rsa_authentication ||
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000689 options.hostbased_authentication)) {
690 sensitive_data.nkeys = 3;
691 sensitive_data.keys = xmalloc(sensitive_data.nkeys*sizeof(Key));
692 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
Ben Lindstromd0fca422001-03-26 13:44:06 +0000693 _PATH_HOST_KEY_FILE, "", NULL);
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000694 sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
695 _PATH_HOST_DSA_KEY_FILE, "", NULL);
696 sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
697 _PATH_HOST_RSA_KEY_FILE, "", NULL);
Damien Miller95def091999-11-25 00:26:21 +1100698 }
Damien Miller5428f641999-11-25 11:54:57 +1100699 /*
700 * Get rid of any extra privileges that we may have. We will no
701 * longer need them. Also, extra privileges could make it very hard
702 * to read identity files and other non-world-readable files from the
703 * user's home directory if it happens to be on a NFS volume where
704 * root is mapped to nobody.
705 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000706
Damien Miller5428f641999-11-25 11:54:57 +1100707 /*
708 * Note that some legacy systems need to postpone the following call
709 * to permanently_set_uid() until the private hostkey is destroyed
710 * with RSA_free(). Otherwise the calling user could ptrace() the
711 * process, read the private hostkey and impersonate the host.
712 * OpenBSD does not allow ptracing of setuid processes.
713 */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000714 permanently_set_uid(pw);
Damien Miller95def091999-11-25 00:26:21 +1100715
Damien Miller5428f641999-11-25 11:54:57 +1100716 /*
717 * Now that we are back to our own permissions, create ~/.ssh
718 * directory if it doesn\'t already exist.
719 */
Ben Lindstrom930b14a2001-08-15 23:19:21 +0000720 snprintf(buf, sizeof buf, "%.100s%s%.100s", pw->pw_dir, strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
Damien Miller95def091999-11-25 00:26:21 +1100721 if (stat(buf, &st) < 0)
Damien Millerbe484b52000-07-15 14:14:16 +1000722 if (mkdir(buf, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100723 error("Could not create directory '%.200s'.", buf);
724
725 /* Check if the connection failed, and try "rsh" if appropriate. */
Ben Lindstromf9cedb92001-08-06 21:07:11 +0000726 if (cerr) {
727 if (!options.fallback_to_rsh)
728 exit(1);
Damien Miller95def091999-11-25 00:26:21 +1100729 if (options.port != 0)
Ben Lindstromf9cedb92001-08-06 21:07:11 +0000730 log("Secure connection to %.100s on port %hu refused; "
731 "reverting to insecure method",
732 host, options.port);
Damien Miller95def091999-11-25 00:26:21 +1100733 else
Ben Lindstromf9cedb92001-08-06 21:07:11 +0000734 log("Secure connection to %.100s refused; "
735 "reverting to insecure method.", host);
Damien Miller95def091999-11-25 00:26:21 +1100736
Ben Lindstromf9cedb92001-08-06 21:07:11 +0000737 rsh_connect(host, options.user, &command);
738 fatal("rsh_connect returned");
Damien Miller95def091999-11-25 00:26:21 +1100739 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000740 /* load options.identity_files */
741 load_public_identity_files();
742
743 /* Expand ~ in known host file names. */
744 /* XXX mem-leaks: */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100745 options.system_hostfile =
746 tilde_expand_filename(options.system_hostfile, original_real_uid);
747 options.user_hostfile =
748 tilde_expand_filename(options.user_hostfile, original_real_uid);
749 options.system_hostfile2 =
750 tilde_expand_filename(options.system_hostfile2, original_real_uid);
751 options.user_hostfile2 =
752 tilde_expand_filename(options.user_hostfile2, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100753
Damien Miller07cd5892001-11-12 10:52:03 +1100754 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
755
Damien Miller95def091999-11-25 00:26:21 +1100756 /* Log into the remote system. This never returns if the login fails. */
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000757 ssh_login(sensitive_data.keys, sensitive_data.nkeys,
758 host, (struct sockaddr *)&hostaddr, pw);
Damien Miller95def091999-11-25 00:26:21 +1100759
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000760 /* We no longer need the private host keys. Clear them now. */
761 if (sensitive_data.nkeys != 0) {
762 for (i = 0; i < sensitive_data.nkeys; i++) {
763 if (sensitive_data.keys[i] != NULL) {
764 /* Destroys contents safely */
765 debug3("clear hostkey %d", i);
766 key_free(sensitive_data.keys[i]);
767 sensitive_data.keys[i] = NULL;
768 }
769 }
770 xfree(sensitive_data.keys);
771 }
Ben Lindstroma6c8a8d2001-08-06 21:42:00 +0000772 for (i = 0; i < options.num_identity_files; i++) {
773 if (options.identity_files[i]) {
774 xfree(options.identity_files[i]);
775 options.identity_files[i] = NULL;
776 }
777 if (options.identity_keys[i]) {
778 key_free(options.identity_keys[i]);
779 options.identity_keys[i] = NULL;
780 }
781 }
Damien Miller95def091999-11-25 00:26:21 +1100782
Damien Miller1383bd82000-04-06 12:32:37 +1000783 exit_status = compat20 ? ssh_session2() : ssh_session();
784 packet_close();
785 return exit_status;
786}
787
Ben Lindstrombba81212001-06-25 05:01:22 +0000788static void
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000789x11_get_proto(char **_proto, char **_data)
Damien Millerbd483e72000-04-30 10:00:53 +1000790{
791 char line[512];
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000792 static char proto[512], data[512];
Damien Millerbd483e72000-04-30 10:00:53 +1000793 FILE *f;
794 int got_data = 0, i;
Damien Miller35b13d62002-02-05 12:12:09 +1100795 char *display;
Damien Millerbd483e72000-04-30 10:00:53 +1000796
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000797 *_proto = proto;
798 *_data = data;
799 proto[0] = data[0] = '\0';
Damien Miller35b13d62002-02-05 12:12:09 +1100800 if (options.xauth_location && (display = getenv("DISPLAY"))) {
Damien Millerd3a18572000-06-07 19:55:44 +1000801 /* Try to get Xauthority information for the display. */
Damien Miller35b13d62002-02-05 12:12:09 +1100802 if (strncmp(display, "localhost:", 10) == 0)
803 /*
804 * Handle FamilyLocal case where $DISPLAY does
805 * not match an authorization entry. For this we
806 * just try "xauth list unix:displaynum.screennum".
807 * XXX: "localhost" match to determine FamilyLocal
808 * is not perfect.
809 */
810 snprintf(line, sizeof line, "%.100s list unix:%s 2>"
811 _PATH_DEVNULL, options.xauth_location, display+10);
812 else
813 snprintf(line, sizeof line, "%.100s list %.200s 2>"
814 _PATH_DEVNULL, options.xauth_location, display);
815 debug2("x11_get_proto %s", line);
Damien Millerd3a18572000-06-07 19:55:44 +1000816 f = popen(line, "r");
817 if (f && fgets(line, sizeof(line), f) &&
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000818 sscanf(line, "%*s %511s %511s", proto, data) == 2)
Damien Millerd3a18572000-06-07 19:55:44 +1000819 got_data = 1;
820 if (f)
821 pclose(f);
822 }
Damien Millerbd483e72000-04-30 10:00:53 +1000823 /*
824 * If we didn't get authentication data, just make up some
825 * data. The forwarding code will check the validity of the
826 * response anyway, and substitute this data. The X11
827 * server, however, will ignore this fake data and use
828 * whatever authentication mechanisms it was using otherwise
829 * for the local connection.
830 */
831 if (!got_data) {
832 u_int32_t rand = 0;
833
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000834 strlcpy(proto, "MIT-MAGIC-COOKIE-1", sizeof proto);
Damien Millerbd483e72000-04-30 10:00:53 +1000835 for (i = 0; i < 16; i++) {
836 if (i % 4 == 0)
837 rand = arc4random();
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000838 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x", rand & 0xff);
Damien Millerbd483e72000-04-30 10:00:53 +1000839 rand >>= 8;
840 }
841 }
842}
843
Ben Lindstrombba81212001-06-25 05:01:22 +0000844static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100845ssh_init_forwarding(void)
846{
Kevin Steves12057502001-02-05 14:54:34 +0000847 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100848 int i;
Kevin Steves12057502001-02-05 14:54:34 +0000849
Damien Miller0bc1bd82000-11-13 22:57:25 +1100850 /* Initiate local TCP/IP port forwardings. */
851 for (i = 0; i < options.num_local_forwards; i++) {
852 debug("Connections to local port %d forwarded to remote address %.200s:%d",
853 options.local_forwards[i].port,
854 options.local_forwards[i].host,
855 options.local_forwards[i].host_port);
Damien Millerb16461c2002-01-22 23:29:22 +1100856 success += channel_setup_local_fwd_listener(
Damien Miller0bc1bd82000-11-13 22:57:25 +1100857 options.local_forwards[i].port,
858 options.local_forwards[i].host,
859 options.local_forwards[i].host_port,
860 options.gateway_ports);
861 }
Kevin Steves12057502001-02-05 14:54:34 +0000862 if (i > 0 && success == 0)
863 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100864
865 /* Initiate remote TCP/IP port forwardings. */
866 for (i = 0; i < options.num_remote_forwards; i++) {
867 debug("Connections to remote port %d forwarded to local address %.200s:%d",
868 options.remote_forwards[i].port,
869 options.remote_forwards[i].host,
870 options.remote_forwards[i].host_port);
871 channel_request_remote_forwarding(
872 options.remote_forwards[i].port,
873 options.remote_forwards[i].host,
874 options.remote_forwards[i].host_port);
875 }
876}
877
Ben Lindstrombba81212001-06-25 05:01:22 +0000878static void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100879check_agent_present(void)
880{
881 if (options.forward_agent) {
882 /* Clear agent forwarding if we don\'t have an agent. */
883 int authfd = ssh_get_authentication_socket();
884 if (authfd < 0)
885 options.forward_agent = 0;
886 else
887 ssh_close_authentication_socket(authfd);
888 }
889}
890
Ben Lindstrombba81212001-06-25 05:01:22 +0000891static int
Damien Miller1383bd82000-04-06 12:32:37 +1000892ssh_session(void)
893{
894 int type;
Damien Miller1383bd82000-04-06 12:32:37 +1000895 int interactive = 0;
896 int have_tty = 0;
897 struct winsize ws;
Damien Miller1383bd82000-04-06 12:32:37 +1000898 char *cp;
899
Damien Miller95def091999-11-25 00:26:21 +1100900 /* Enable compression if requested. */
901 if (options.compression) {
902 debug("Requesting compression at level %d.", options.compression_level);
903
904 if (options.compression_level < 1 || options.compression_level > 9)
905 fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
906
907 /* Send the request. */
908 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
909 packet_put_int(options.compression_level);
910 packet_send();
911 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100912 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100913 if (type == SSH_SMSG_SUCCESS)
914 packet_start_compression(options.compression_level);
915 else if (type == SSH_SMSG_FAILURE)
916 log("Warning: Remote host refused compression.");
917 else
918 packet_disconnect("Protocol error waiting for compression response.");
919 }
920 /* Allocate a pseudo tty if appropriate. */
921 if (tty_flag) {
922 debug("Requesting pty.");
923
924 /* Start the packet. */
925 packet_start(SSH_CMSG_REQUEST_PTY);
926
927 /* Store TERM in the packet. There is no limit on the
928 length of the string. */
929 cp = getenv("TERM");
930 if (!cp)
931 cp = "";
Ben Lindstrom664408d2001-06-09 01:42:01 +0000932 packet_put_cstring(cp);
Damien Miller95def091999-11-25 00:26:21 +1100933
934 /* Store window size in the packet. */
935 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
936 memset(&ws, 0, sizeof(ws));
937 packet_put_int(ws.ws_row);
938 packet_put_int(ws.ws_col);
939 packet_put_int(ws.ws_xpixel);
940 packet_put_int(ws.ws_ypixel);
941
942 /* Store tty modes in the packet. */
Ben Lindstromae8e2d32001-04-14 23:13:02 +0000943 tty_make_modes(fileno(stdin), NULL);
Damien Miller95def091999-11-25 00:26:21 +1100944
945 /* Send the packet, and wait for it to leave. */
946 packet_send();
947 packet_write_wait();
948
949 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100950 type = packet_read();
Damien Miller450a7a12000-03-26 13:04:51 +1000951 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100952 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +1000953 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000954 } else if (type == SSH_SMSG_FAILURE)
Damien Miller95def091999-11-25 00:26:21 +1100955 log("Warning: Remote host failed or refused to allocate a pseudo tty.");
956 else
957 packet_disconnect("Protocol error waiting for pty request response.");
958 }
959 /* Request X11 forwarding if enabled and DISPLAY is set. */
960 if (options.forward_x11 && getenv("DISPLAY") != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000961 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +1000962 /* Get reasonable local authentication information. */
Ben Lindstrom4a4bd712001-12-06 17:45:19 +0000963 x11_get_proto(&proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +1000964 /* Request forwarding with authentication spoofing. */
Damien Miller95def091999-11-25 00:26:21 +1100965 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Millerbd483e72000-04-30 10:00:53 +1000966 x11_request_forwarding_with_spoofing(0, proto, data);
Damien Miller95def091999-11-25 00:26:21 +1100967
968 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100969 type = packet_read();
Damien Miller95def091999-11-25 00:26:21 +1100970 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100971 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +1000972 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller95def091999-11-25 00:26:21 +1100973 log("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +1000974 } else {
Damien Miller95def091999-11-25 00:26:21 +1100975 packet_disconnect("Protocol error waiting for X11 forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +1000976 }
Damien Miller95def091999-11-25 00:26:21 +1100977 }
978 /* Tell the packet module whether this is an interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000979 packet_set_interactive(interactive);
Damien Miller95def091999-11-25 00:26:21 +1100980
981 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100982 check_agent_present();
983
Damien Miller95def091999-11-25 00:26:21 +1100984 if (options.forward_agent) {
985 debug("Requesting authentication agent forwarding.");
986 auth_request_forwarding();
987
988 /* Read response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100989 type = packet_read();
Damien Miller48b03fc2002-01-22 23:11:40 +1100990 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +1100991 if (type != SSH_SMSG_SUCCESS)
992 log("Warning: Remote host denied authentication agent forwarding.");
993 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000994
Damien Miller0bc1bd82000-11-13 22:57:25 +1100995 /* Initiate port forwardings. */
996 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000997
Damien Miller5428f641999-11-25 11:54:57 +1100998 /* If requested, let ssh continue in the background. */
Damien Miller4af51302000-04-16 11:18:38 +1000999 if (fork_after_authentication_flag)
Damien Miller5428f641999-11-25 11:54:57 +11001000 if (daemon(1, 1) < 0)
1001 fatal("daemon() failed: %.200s", strerror(errno));
1002
1003 /*
1004 * If a command was specified on the command line, execute the
1005 * command now. Otherwise request the server to start a shell.
1006 */
Damien Miller95def091999-11-25 00:26:21 +11001007 if (buffer_len(&command) > 0) {
1008 int len = buffer_len(&command);
1009 if (len > 900)
1010 len = 900;
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001011 debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
Damien Miller95def091999-11-25 00:26:21 +11001012 packet_start(SSH_CMSG_EXEC_CMD);
1013 packet_put_string(buffer_ptr(&command), buffer_len(&command));
1014 packet_send();
1015 packet_write_wait();
1016 } else {
1017 debug("Requesting shell.");
1018 packet_start(SSH_CMSG_EXEC_SHELL);
1019 packet_send();
1020 packet_write_wait();
1021 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001022
Damien Miller95def091999-11-25 00:26:21 +11001023 /* Enter the interactive session. */
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001024 return client_loop(have_tty, tty_flag ?
1025 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
Damien Miller1383bd82000-04-06 12:32:37 +10001026}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001027
Ben Lindstrombba81212001-06-25 05:01:22 +00001028static void
Damien Miller630d6f42002-01-22 23:17:30 +11001029client_subsystem_reply(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001030{
1031 int id, len;
1032
1033 id = packet_get_int();
1034 len = buffer_len(&command);
Ben Lindstrom4040fe12001-03-05 06:52:57 +00001035 if (len > 900)
1036 len = 900;
Damien Miller48b03fc2002-01-22 23:11:40 +11001037 packet_check_eom();
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001038 if (type == SSH2_MSG_CHANNEL_FAILURE)
1039 fatal("Request for subsystem '%.*s' failed on channel %d",
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001040 len, (u_char *)buffer_ptr(&command), id);
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001041}
1042
Ben Lindstromf558cf62001-09-20 23:13:49 +00001043/* request pty/x11/agent/tcpfwd/shell for channel */
Ben Lindstrombba81212001-06-25 05:01:22 +00001044static void
Ben Lindstromf558cf62001-09-20 23:13:49 +00001045ssh_session2_setup(int id, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +10001046{
1047 int len;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001048 int interactive = 0;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001049 struct termios tio;
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001050
Ben Lindstromf558cf62001-09-20 23:13:49 +00001051 debug("ssh_session2_setup: id %d", id);
Damien Miller1383bd82000-04-06 12:32:37 +10001052
Damien Miller1383bd82000-04-06 12:32:37 +10001053 if (tty_flag) {
1054 struct winsize ws;
1055 char *cp;
1056 cp = getenv("TERM");
1057 if (!cp)
1058 cp = "";
1059 /* Store window size in the packet. */
1060 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
1061 memset(&ws, 0, sizeof(ws));
1062
1063 channel_request_start(id, "pty-req", 0);
1064 packet_put_cstring(cp);
1065 packet_put_int(ws.ws_col);
1066 packet_put_int(ws.ws_row);
1067 packet_put_int(ws.ws_xpixel);
1068 packet_put_int(ws.ws_ypixel);
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001069 tio = get_saved_tio();
1070 tty_make_modes(/*ignored*/ 0, &tio);
Damien Miller1383bd82000-04-06 12:32:37 +10001071 packet_send();
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001072 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001073 /* XXX wait for reply */
1074 }
Damien Millerbd483e72000-04-30 10:00:53 +10001075 if (options.forward_x11 &&
1076 getenv("DISPLAY") != NULL) {
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001077 char *proto, *data;
Damien Millerbd483e72000-04-30 10:00:53 +10001078 /* Get reasonable local authentication information. */
Ben Lindstrom4a4bd712001-12-06 17:45:19 +00001079 x11_get_proto(&proto, &data);
Damien Millerbd483e72000-04-30 10:00:53 +10001080 /* Request forwarding with authentication spoofing. */
1081 debug("Requesting X11 forwarding with authentication spoofing.");
1082 x11_request_forwarding_with_spoofing(id, proto, data);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001083 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001084 /* XXX wait for reply */
1085 }
1086
Damien Miller0bc1bd82000-11-13 22:57:25 +11001087 check_agent_present();
1088 if (options.forward_agent) {
1089 debug("Requesting authentication agent forwarding.");
1090 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1091 packet_send();
1092 }
1093
Damien Miller1383bd82000-04-06 12:32:37 +10001094 len = buffer_len(&command);
1095 if (len > 0) {
1096 if (len > 900)
1097 len = 900;
Damien Miller832562e2001-01-30 09:30:01 +11001098 if (subsystem_flag) {
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001099 debug("Sending subsystem: %.*s", len, (u_char *)buffer_ptr(&command));
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001100 channel_request_start(id, "subsystem", /*want reply*/ 1);
1101 /* register callback for reply */
1102 /* XXX we asume that client_loop has already been called */
1103 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &client_subsystem_reply);
1104 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &client_subsystem_reply);
Damien Miller832562e2001-01-30 09:30:01 +11001105 } else {
Damien Miller5a6b4fe2001-12-21 14:56:54 +11001106 debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
Damien Miller832562e2001-01-30 09:30:01 +11001107 channel_request_start(id, "exec", 0);
1108 }
Ben Lindstrom4040fe12001-03-05 06:52:57 +00001109 packet_put_string(buffer_ptr(&command), buffer_len(&command));
Damien Miller1383bd82000-04-06 12:32:37 +10001110 packet_send();
1111 } else {
Damien Millera500cd62002-02-08 22:04:26 +11001112 channel_request_start(id, "shell", 0);
1113 packet_send();
Damien Miller1383bd82000-04-06 12:32:37 +10001114 }
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001115
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001116 packet_set_interactive(interactive);
Damien Miller1383bd82000-04-06 12:32:37 +10001117}
1118
Ben Lindstromf558cf62001-09-20 23:13:49 +00001119/* open new channel for a session */
Ben Lindstrombba81212001-06-25 05:01:22 +00001120static int
Ben Lindstromf558cf62001-09-20 23:13:49 +00001121ssh_session2_open(void)
Damien Miller1383bd82000-04-06 12:32:37 +10001122{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001123 Channel *c;
1124 int window, packetmax, in, out, err;
Damien Millerad833b32000-08-23 10:46:23 +10001125
Damien Millercaf6dd62000-08-29 11:33:50 +11001126 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001127 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001128 } else {
1129 in = dup(STDIN_FILENO);
1130 }
1131 out = dup(STDOUT_FILENO);
1132 err = dup(STDERR_FILENO);
1133
1134 if (in < 0 || out < 0 || err < 0)
1135 fatal("dup() in/out/err failed");
1136
Damien Miller69b69aa2000-10-28 14:19:58 +11001137 /* enable nonblocking unless tty */
1138 if (!isatty(in))
1139 set_nonblock(in);
1140 if (!isatty(out))
1141 set_nonblock(out);
1142 if (!isatty(err))
1143 set_nonblock(err);
1144
Damien Millere4340be2000-09-16 13:29:08 +11001145 window = CHAN_SES_WINDOW_DEFAULT;
1146 packetmax = CHAN_SES_PACKET_DEFAULT;
Damien Miller19a59452002-02-19 15:20:57 +11001147 if (tty_flag) {
1148 window >>= 1;
1149 packetmax >>= 1;
Damien Miller1383bd82000-04-06 12:32:37 +10001150 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001151 c = channel_new(
Damien Miller1383bd82000-04-06 12:32:37 +10001152 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001153 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Miller69b69aa2000-10-28 14:19:58 +11001154 xstrdup("client-session"), /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001155
Ben Lindstromf558cf62001-09-20 23:13:49 +00001156 debug3("ssh_session2_open: channel_new: %d", c->self);
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001157
Ben Lindstrom5ec26452001-06-09 00:18:51 +00001158 channel_send_open(c->self);
Ben Lindstromf558cf62001-09-20 23:13:49 +00001159 if (!no_shell_flag)
Damien Miller67f0bc02002-02-05 12:23:08 +11001160 channel_register_confirm(c->self, ssh_session2_setup);
Damien Miller1383bd82000-04-06 12:32:37 +10001161
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001162 return c->self;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001163}
1164
Ben Lindstrombba81212001-06-25 05:01:22 +00001165static int
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001166ssh_session2(void)
1167{
Ben Lindstromf558cf62001-09-20 23:13:49 +00001168 int id = -1;
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001169
1170 /* XXX should be pre-session */
1171 ssh_init_forwarding();
1172
Ben Lindstromf558cf62001-09-20 23:13:49 +00001173 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1174 id = ssh_session2_open();
Ben Lindstrom4c3f77d2001-04-05 23:37:36 +00001175
1176 /* If requested, let ssh continue in the background. */
1177 if (fork_after_authentication_flag)
1178 if (daemon(1, 1) < 0)
1179 fatal("daemon() failed: %.200s", strerror(errno));
1180
Ben Lindstrom2b1f71b2001-06-05 20:32:21 +00001181 return client_loop(tty_flag, tty_flag ?
1182 options.escape_char : SSH_ESCAPECHAR_NONE, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001183}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001184
Ben Lindstrombba81212001-06-25 05:01:22 +00001185static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001186load_public_identity_files(void)
1187{
1188 char *filename;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001189 int i = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001190 Key *public;
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001191#ifdef SMARTCARD
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001192 Key **keys;
1193
Ben Lindstromf7db3bb2001-08-06 21:35:51 +00001194 if (options.smartcard_device != NULL &&
Ben Lindstrom0936a5b2002-03-26 03:17:42 +00001195 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1196 (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL ) {
1197 int count = 0;
1198 for (i = 0; keys[i] != NULL; i++) {
1199 count++;
1200 if (options.num_identity_files + 1 > SSH_MAX_IDENTITY_FILES)
1201 options.num_identity_files = SSH_MAX_IDENTITY_FILES - 1;
1202 memmove(&options.identity_files[1], &options.identity_files[0],
1203 sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
1204 memmove(&options.identity_keys[1], &options.identity_keys[0],
1205 sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1206 options.num_identity_files++;
1207 options.identity_keys[0] = keys[i];
1208 options.identity_files[0] = xstrdup("smartcard key");;
1209 }
1210 i = count;
1211 xfree(keys);
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001212 }
Ben Lindstromffce1472001-08-06 21:57:31 +00001213#endif /* SMARTCARD */
Ben Lindstrom711b04a2001-08-06 21:12:42 +00001214 for (; i < options.num_identity_files; i++) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001215 filename = tilde_expand_filename(options.identity_files[i],
1216 original_real_uid);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001217 public = key_load_public(filename, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001218 debug("identity file %s type %d", filename,
1219 public ? public->type : -1);
1220 xfree(options.identity_files[i]);
1221 options.identity_files[i] = filename;
1222 options.identity_keys[i] = public;
1223 }
1224}