blob: 4ca1e7bf879f585c09d7189c62d8f5336fc093ba [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 Lindstrom8fd10b02001-02-10 22:11:13 +000042RCSID("$OpenBSD: ssh.c,v 1.94 2001/02/10 01:46:28 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"
Damien Millerb38eff82000-04-01 11:09:21 +100056#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"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068
Damien Miller3f905871999-11-15 17:10:57 +110069#ifdef HAVE___PROGNAME
70extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000071#else
72char *__progname;
73#endif
Damien Miller3f905871999-11-15 17:10:57 +110074
Damien Miller34132e52000-01-14 15:45:46 +110075/* Flag indicating whether IPv4 or IPv6. This can be set on the command line.
76 Default value is AF_UNSPEC means both IPv4 and IPv6. */
Damien Miller7d80e342000-01-19 14:36:49 +110077#ifdef IPV4_DEFAULT
78int IPv4or6 = AF_INET;
79#else
Damien Miller34132e52000-01-14 15:45:46 +110080int IPv4or6 = AF_UNSPEC;
Damien Miller7d80e342000-01-19 14:36:49 +110081#endif
Damien Miller34132e52000-01-14 15:45:46 +110082
Damien Miller95def091999-11-25 00:26:21 +110083/* Flag indicating whether debug mode is on. This can be set on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084int debug_flag = 0;
85
Damien Miller78928792000-04-12 20:17:38 +100086/* Flag indicating whether a tty should be allocated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087int tty_flag = 0;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +000088int no_tty_flag = 0;
89int force_tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090
Damien Miller1383bd82000-04-06 12:32:37 +100091/* don't exec a shell */
92int no_shell_flag = 0;
Damien Miller1383bd82000-04-06 12:32:37 +100093
Damien Miller5428f641999-11-25 11:54:57 +110094/*
95 * Flag indicating that nothing should be read from stdin. This can be set
96 * on the command line.
97 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098int stdin_null_flag = 0;
99
Damien Miller5428f641999-11-25 11:54:57 +1100100/*
101 * Flag indicating that ssh should fork after authentication. This is useful
102 * so that the pasphrase can be entered manually, and then ssh goes to the
103 * background.
104 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105int fork_after_authentication_flag = 0;
106
Damien Miller5428f641999-11-25 11:54:57 +1100107/*
108 * General data structure for command line options and options configurable
109 * in configuration files. See readconf.h.
110 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111Options options;
112
Damien Miller5428f641999-11-25 11:54:57 +1100113/*
114 * Name of the host we are connecting to. This is the name given on the
115 * command line, or the HostName specified for the user-supplied name in a
116 * configuration file.
117 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118char *host;
119
120/* socket address the host resolves to */
Damien Miller34132e52000-01-14 15:45:46 +1100121struct sockaddr_storage hostaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122
Damien Miller5428f641999-11-25 11:54:57 +1100123/*
124 * Flag to indicate that we have received a window change signal which has
125 * not yet been processed. This will cause a message indicating the new
126 * window size to be sent to the server a little later. This is volatile
127 * because this is updated in a signal handler.
128 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129volatile int received_window_change_signal = 0;
130
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131/* Flag indicating whether we have a valid host private key loaded. */
132int host_private_key_loaded = 0;
133
134/* Host private key. */
135RSA *host_private_key = NULL;
136
137/* Original real UID. */
138uid_t original_real_uid;
139
Damien Miller1383bd82000-04-06 12:32:37 +1000140/* command to be executed */
141Buffer command;
142
Damien Miller832562e2001-01-30 09:30:01 +1100143/* Should we execute a command or invoke a subsystem? */
144int subsystem_flag = 0;
145
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146/* Prints a help message to the user. This function never returns. */
147
148void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000149usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150{
Kevin Stevesec84dc12000-12-13 17:45:15 +0000151 fprintf(stderr, "Usage: %s [options] host [command]\n", __progname);
Damien Miller95def091999-11-25 00:26:21 +1100152 fprintf(stderr, "Options:\n");
153 fprintf(stderr, " -l user Log in using this user name.\n");
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000154 fprintf(stderr, " -n Redirect input from " _PATH_DEVNULL ".\n");
Damien Millerb1715dc2000-05-30 13:44:51 +1000155 fprintf(stderr, " -A Enable authentication agent forwarding.\n");
Damien Miller95def091999-11-25 00:26:21 +1100156 fprintf(stderr, " -a Disable authentication agent forwarding.\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000157#ifdef AFS
Damien Miller95def091999-11-25 00:26:21 +1100158 fprintf(stderr, " -k Disable Kerberos ticket and AFS token forwarding.\n");
159#endif /* AFS */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000160 fprintf(stderr, " -X Enable X11 connection forwarding.\n");
Damien Miller95def091999-11-25 00:26:21 +1100161 fprintf(stderr, " -x Disable X11 connection forwarding.\n");
162 fprintf(stderr, " -i file Identity for RSA authentication (default: ~/.ssh/identity).\n");
163 fprintf(stderr, " -t Tty; allocate a tty even if command is given.\n");
Damien Miller1383bd82000-04-06 12:32:37 +1000164 fprintf(stderr, " -T Do not allocate a tty.\n");
Damien Miller95def091999-11-25 00:26:21 +1100165 fprintf(stderr, " -v Verbose; display verbose debugging messages.\n");
Damien Millere4340be2000-09-16 13:29:08 +1100166 fprintf(stderr, " Multiple -v increases verbosity.\n");
Damien Miller95def091999-11-25 00:26:21 +1100167 fprintf(stderr, " -V Display version number only.\n");
168 fprintf(stderr, " -P Don't allocate a privileged port.\n");
169 fprintf(stderr, " -q Quiet; don't display any warning messages.\n");
170 fprintf(stderr, " -f Fork into background after authentication.\n");
171 fprintf(stderr, " -e char Set escape character; ``none'' = disable (default: ~).\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000172
Damien Miller95def091999-11-25 00:26:21 +1100173 fprintf(stderr, " -c cipher Select encryption algorithm: "
174 "``3des'', "
175 "``blowfish''\n");
176 fprintf(stderr, " -p port Connect to this port. Server must be on the same port.\n");
177 fprintf(stderr, " -L listen-port:host:port Forward local port to remote address\n");
178 fprintf(stderr, " -R listen-port:host:port Forward remote port to local address\n");
Kevin Stevesec84dc12000-12-13 17:45:15 +0000179 fprintf(stderr, " These cause %s to listen for connections on a port, and\n", __progname);
Damien Miller95def091999-11-25 00:26:21 +1100180 fprintf(stderr, " forward them to the other side by connecting to host:port.\n");
181 fprintf(stderr, " -C Enable compression.\n");
Damien Miller1383bd82000-04-06 12:32:37 +1000182 fprintf(stderr, " -N Do not execute a shell or command.\n");
Damien Miller95def091999-11-25 00:26:21 +1100183 fprintf(stderr, " -g Allow remote hosts to connect to forwarded ports.\n");
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000184 fprintf(stderr, " -1 Force protocol version 1.\n");
185 fprintf(stderr, " -2 Force protocol version 2.\n");
Damien Miller34132e52000-01-14 15:45:46 +1100186 fprintf(stderr, " -4 Use IPv4 only.\n");
187 fprintf(stderr, " -6 Use IPv6 only.\n");
Damien Miller95def091999-11-25 00:26:21 +1100188 fprintf(stderr, " -o 'option' Process the option as if it was read from a configuration file.\n");
Damien Miller832562e2001-01-30 09:30:01 +1100189 fprintf(stderr, " -s Invoke command (mandatory) as SSH2 subsystem.\n");
Damien Miller95def091999-11-25 00:26:21 +1100190 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000191}
192
Damien Miller95def091999-11-25 00:26:21 +1100193/*
194 * Connects to the given host using rsh (or prints an error message and exits
195 * if rsh is not available). This function never returns.
196 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000197void
Damien Miller95def091999-11-25 00:26:21 +1100198rsh_connect(char *host, char *user, Buffer * command)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199{
Damien Miller95def091999-11-25 00:26:21 +1100200 char *args[10];
201 int i;
202
203 log("Using rsh. WARNING: Connection will not be encrypted.");
204 /* Build argument list for rsh. */
205 i = 0;
206 args[i++] = _PATH_RSH;
207 /* host may have to come after user on some systems */
208 args[i++] = host;
209 if (user) {
210 args[i++] = "-l";
211 args[i++] = user;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000212 }
Damien Miller95def091999-11-25 00:26:21 +1100213 if (buffer_len(command) > 0) {
214 buffer_append(command, "\0", 1);
215 args[i++] = buffer_ptr(command);
216 }
217 args[i++] = NULL;
218 if (debug_flag) {
219 for (i = 0; args[i]; i++) {
220 if (i != 0)
221 fprintf(stderr, " ");
222 fprintf(stderr, "%s", args[i]);
223 }
224 fprintf(stderr, "\n");
225 }
226 execv(_PATH_RSH, args);
227 perror(_PATH_RSH);
228 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000229}
230
Damien Miller0bc1bd82000-11-13 22:57:25 +1100231int ssh_session(void);
232int ssh_session2(void);
233int guess_identity_file_type(const char *filename);
Damien Miller1383bd82000-04-06 12:32:37 +1000234
Damien Miller95def091999-11-25 00:26:21 +1100235/*
236 * Main program for the ssh client.
237 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000238int
239main(int ac, char **av)
240{
Damien Miller1383bd82000-04-06 12:32:37 +1000241 int i, opt, optind, exit_status, ok;
Damien Milleraae6c611999-12-06 11:47:28 +1100242 u_short fwd_port, fwd_host_port;
Damien Miller95def091999-11-25 00:26:21 +1100243 char *optarg, *cp, buf[256];
Damien Miller95def091999-11-25 00:26:21 +1100244 struct stat st;
245 struct passwd *pw, pwcopy;
Damien Miller1383bd82000-04-06 12:32:37 +1000246 int dummy;
Damien Miller95def091999-11-25 00:26:21 +1100247 uid_t original_effective_uid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000249 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000250 init_rng();
251
Damien Miller5428f641999-11-25 11:54:57 +1100252 /*
253 * Save the original real uid. It will be needed later (uid-swapping
254 * may clobber the real uid).
255 */
Damien Miller95def091999-11-25 00:26:21 +1100256 original_real_uid = getuid();
257 original_effective_uid = geteuid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000258
Damien Miller05dd7952000-10-01 00:42:48 +1100259#ifdef HAVE_SETRLIMIT
Damien Miller95def091999-11-25 00:26:21 +1100260 /* If we are installed setuid root be careful to not drop core. */
261 if (original_real_uid != original_effective_uid) {
262 struct rlimit rlim;
263 rlim.rlim_cur = rlim.rlim_max = 0;
264 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
265 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000266 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100267#endif
Damien Miller5428f641999-11-25 11:54:57 +1100268 /*
269 * Use uid-swapping to give up root privileges for the duration of
270 * option processing. We will re-instantiate the rights when we are
271 * ready to create the privileged port, and will permanently drop
272 * them when the port has been created (actually, when the connection
273 * has been made, as we may need to create the port several times).
274 */
Damien Miller95def091999-11-25 00:26:21 +1100275 temporarily_use_uid(original_real_uid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000276
Damien Miller5428f641999-11-25 11:54:57 +1100277 /*
278 * Set our umask to something reasonable, as some files are created
279 * with the default umask. This will make them world-readable but
280 * writable only by the owner, which is ok for all files for which we
281 * don't set the modes explicitly.
282 */
Damien Miller95def091999-11-25 00:26:21 +1100283 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000284
Damien Miller95def091999-11-25 00:26:21 +1100285 /* Initialize option structure to indicate that no values have been set. */
286 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000287
Damien Miller95def091999-11-25 00:26:21 +1100288 /* Parse command-line arguments. */
289 host = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000290
Damien Miller95def091999-11-25 00:26:21 +1100291 for (optind = 1; optind < ac; optind++) {
292 if (av[optind][0] != '-') {
293 if (host)
294 break;
295 if ((cp = strchr(av[optind], '@'))) {
Damien Miller4af51302000-04-16 11:18:38 +1000296 if(cp == av[optind])
297 usage();
Damien Miller95def091999-11-25 00:26:21 +1100298 options.user = av[optind];
299 *cp = '\0';
300 host = ++cp;
301 } else
302 host = av[optind];
303 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000304 }
Damien Miller95def091999-11-25 00:26:21 +1100305 opt = av[optind][1];
306 if (!opt)
307 usage();
308 if (strchr("eilcpLRo", opt)) { /* options with arguments */
309 optarg = av[optind] + 2;
310 if (strcmp(optarg, "") == 0) {
311 if (optind >= ac - 1)
312 usage();
313 optarg = av[++optind];
314 }
315 } else {
316 if (av[optind][2])
317 usage();
318 optarg = NULL;
319 }
320 switch (opt) {
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000321 case '1':
322 options.protocol = SSH_PROTO_1;
323 break;
Damien Miller4af51302000-04-16 11:18:38 +1000324 case '2':
325 options.protocol = SSH_PROTO_2;
326 break;
Damien Miller34132e52000-01-14 15:45:46 +1100327 case '4':
328 IPv4or6 = AF_INET;
329 break;
Damien Miller34132e52000-01-14 15:45:46 +1100330 case '6':
331 IPv4or6 = AF_INET6;
332 break;
Damien Miller95def091999-11-25 00:26:21 +1100333 case 'n':
334 stdin_null_flag = 1;
335 break;
Damien Miller95def091999-11-25 00:26:21 +1100336 case 'f':
337 fork_after_authentication_flag = 1;
338 stdin_null_flag = 1;
339 break;
Damien Miller95def091999-11-25 00:26:21 +1100340 case 'x':
341 options.forward_x11 = 0;
342 break;
Damien Miller95def091999-11-25 00:26:21 +1100343 case 'X':
344 options.forward_x11 = 1;
345 break;
Damien Miller95def091999-11-25 00:26:21 +1100346 case 'g':
347 options.gateway_ports = 1;
348 break;
Damien Miller95def091999-11-25 00:26:21 +1100349 case 'P':
350 options.use_privileged_port = 0;
351 break;
Damien Miller95def091999-11-25 00:26:21 +1100352 case 'a':
353 options.forward_agent = 0;
354 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000355 case 'A':
356 options.forward_agent = 1;
357 break;
Damien Miller95def091999-11-25 00:26:21 +1100358#ifdef AFS
359 case 'k':
360 options.kerberos_tgt_passing = 0;
361 options.afs_token_passing = 0;
362 break;
363#endif
364 case 'i':
365 if (stat(optarg, &st) < 0) {
366 fprintf(stderr, "Warning: Identity file %s does not exist.\n",
Damien Miller0bc1bd82000-11-13 22:57:25 +1100367 optarg);
Damien Miller95def091999-11-25 00:26:21 +1100368 break;
369 }
370 if (options.num_identity_files >= SSH_MAX_IDENTITY_FILES)
371 fatal("Too many identity files specified (max %d)",
Damien Miller0bc1bd82000-11-13 22:57:25 +1100372 SSH_MAX_IDENTITY_FILES);
373 options.identity_files[options.num_identity_files++] = xstrdup(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100374 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':
Damien Millere4340be2000-09-16 13:29:08 +1100381 if (0 == debug_flag) {
382 debug_flag = 1;
383 options.log_level = SYSLOG_LEVEL_DEBUG1;
384 } else if (options.log_level < SYSLOG_LEVEL_DEBUG3) {
385 options.log_level++;
386 break;
387 } else {
388 fatal("Too high debugging level.\n");
389 }
390 /* fallthrough */
Damien Miller95def091999-11-25 00:26:21 +1100391 case 'V':
Damien Miller78928792000-04-12 20:17:38 +1000392 fprintf(stderr, "SSH Version %s, protocol versions %d.%d/%d.%d.\n",
393 SSH_VERSION,
394 PROTOCOL_MAJOR_1, PROTOCOL_MINOR_1,
395 PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2);
Damien Miller1383bd82000-04-06 12:32:37 +1000396 fprintf(stderr, "Compiled with SSL (0x%8.8lx).\n", SSLeay());
Damien Miller95def091999-11-25 00:26:21 +1100397 if (opt == 'V')
398 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100399 break;
Damien Miller95def091999-11-25 00:26:21 +1100400 case 'q':
401 options.log_level = SYSLOG_LEVEL_QUIET;
402 break;
Damien Miller95def091999-11-25 00:26:21 +1100403 case 'e':
404 if (optarg[0] == '^' && optarg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000405 (u_char) optarg[1] >= 64 && (u_char) optarg[1] < 128)
406 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100407 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000408 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100409 else if (strcmp(optarg, "none") == 0)
410 options.escape_char = -2;
411 else {
412 fprintf(stderr, "Bad escape character '%s'.\n", optarg);
413 exit(1);
414 }
415 break;
Damien Miller95def091999-11-25 00:26:21 +1100416 case 'c':
Damien Millereba71ba2000-04-29 23:57:08 +1000417 if (ciphers_valid(optarg)) {
418 /* SSH2 only */
419 options.ciphers = xstrdup(optarg);
Damien Miller30c3d422000-05-09 11:02:59 +1000420 options.cipher = SSH_CIPHER_ILLEGAL;
Damien Millereba71ba2000-04-29 23:57:08 +1000421 } else {
422 /* SSH1 only */
Damien Millere39cacc2000-11-29 12:18:44 +1100423 options.cipher = cipher_number(optarg);
424 if (options.cipher == -1) {
Damien Millereba71ba2000-04-29 23:57:08 +1000425 fprintf(stderr, "Unknown cipher type '%s'\n", optarg);
426 exit(1);
427 }
Damien Millere39cacc2000-11-29 12:18:44 +1100428 if (options.cipher == SSH_CIPHER_3DES) {
429 options.ciphers = "3des-cbc";
430 } else if (options.cipher == SSH_CIPHER_BLOWFISH) {
431 options.ciphers = "blowfish-cbc";
432 } else {
433 options.ciphers = (char *)-1;
434 }
Damien Miller95def091999-11-25 00:26:21 +1100435 }
436 break;
Damien Miller95def091999-11-25 00:26:21 +1100437 case 'p':
438 options.port = atoi(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100439 break;
Damien Miller95def091999-11-25 00:26:21 +1100440 case 'l':
441 options.user = optarg;
442 break;
Damien Miller95def091999-11-25 00:26:21 +1100443 case 'R':
Damien Miller34132e52000-01-14 15:45:46 +1100444 if (sscanf(optarg, "%hu/%255[^/]/%hu", &fwd_port, buf,
445 &fwd_host_port) != 3 &&
446 sscanf(optarg, "%hu:%255[^:]:%hu", &fwd_port, buf,
447 &fwd_host_port) != 3) {
Damien Miller95def091999-11-25 00:26:21 +1100448 fprintf(stderr, "Bad forwarding specification '%s'.\n", optarg);
449 usage();
450 /* NOTREACHED */
451 }
452 add_remote_forward(&options, fwd_port, buf, fwd_host_port);
453 break;
Damien Miller95def091999-11-25 00:26:21 +1100454 case 'L':
Damien Miller34132e52000-01-14 15:45:46 +1100455 if (sscanf(optarg, "%hu/%255[^/]/%hu", &fwd_port, buf,
456 &fwd_host_port) != 3 &&
457 sscanf(optarg, "%hu:%255[^:]:%hu", &fwd_port, buf,
458 &fwd_host_port) != 3) {
Damien Miller95def091999-11-25 00:26:21 +1100459 fprintf(stderr, "Bad forwarding specification '%s'.\n", optarg);
460 usage();
461 /* NOTREACHED */
462 }
463 add_local_forward(&options, fwd_port, buf, fwd_host_port);
464 break;
Damien Miller95def091999-11-25 00:26:21 +1100465 case 'C':
466 options.compression = 1;
467 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000468 case 'N':
469 no_shell_flag = 1;
470 no_tty_flag = 1;
471 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000472 case 'T':
473 no_tty_flag = 1;
474 break;
Damien Miller95def091999-11-25 00:26:21 +1100475 case 'o':
476 dummy = 1;
477 if (process_config_line(&options, host ? host : "", optarg,
478 "command-line", 0, &dummy) != 0)
479 exit(1);
480 break;
Damien Miller832562e2001-01-30 09:30:01 +1100481 case 's':
482 subsystem_flag = 1;
483 break;
Damien Miller95def091999-11-25 00:26:21 +1100484 default:
485 usage();
486 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000487 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000488
Damien Miller95def091999-11-25 00:26:21 +1100489 /* Check that we got a host name. */
490 if (!host)
491 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000492
Damien Millercb5e44a2000-09-29 12:12:36 +1100493 SSLeay_add_all_algorithms();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100494 ERR_load_crypto_strings();
Damien Millercb5e44a2000-09-29 12:12:36 +1100495
Damien Miller95def091999-11-25 00:26:21 +1100496 /* Initialize the command to execute on remote host. */
497 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000498
Damien Miller5428f641999-11-25 11:54:57 +1100499 /*
500 * Save the command to execute on the remote host in a buffer. There
501 * is no limit on the length of the command, except by the maximum
502 * packet size. Also sets the tty flag if there is no command.
503 */
Damien Miller95def091999-11-25 00:26:21 +1100504 if (optind == ac) {
505 /* No command specified - execute shell on a tty. */
506 tty_flag = 1;
Damien Miller832562e2001-01-30 09:30:01 +1100507 if (subsystem_flag) {
508 fprintf(stderr, "You must specify a subsystem to invoke.");
509 usage();
510 }
Damien Miller95def091999-11-25 00:26:21 +1100511 } else {
512 /* A command has been specified. Store it into the
513 buffer. */
514 for (i = optind; i < ac; i++) {
515 if (i > optind)
516 buffer_append(&command, " ", 1);
517 buffer_append(&command, av[i], strlen(av[i]));
518 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000519 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000520
Damien Miller95def091999-11-25 00:26:21 +1100521 /* Cannot fork to background if no command. */
Damien Millercaf6dd62000-08-29 11:33:50 +1100522 if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
Damien Miller95def091999-11-25 00:26:21 +1100523 fatal("Cannot fork into background without a command to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000524
Damien Miller95def091999-11-25 00:26:21 +1100525 /* Allocate a tty by default if no command specified. */
526 if (buffer_len(&command) == 0)
527 tty_flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000528
Ben Lindstromc72745a2000-12-02 19:03:54 +0000529 /* Force no tty*/
530 if (no_tty_flag)
531 tty_flag = 0;
Damien Miller95def091999-11-25 00:26:21 +1100532 /* Do not allocate a tty if stdin is not a tty. */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000533 if (!isatty(fileno(stdin)) && !force_tty_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100534 if (tty_flag)
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000535 log("Pseudo-terminal will not be allocated because stdin is not a terminal.\n");
Damien Miller95def091999-11-25 00:26:21 +1100536 tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000537 }
Damien Miller1383bd82000-04-06 12:32:37 +1000538
Damien Miller95def091999-11-25 00:26:21 +1100539 /* Get user data. */
540 pw = getpwuid(original_real_uid);
541 if (!pw) {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000542 log("You don't exist, go away!\n");
Damien Miller95def091999-11-25 00:26:21 +1100543 exit(1);
544 }
545 /* Take a copy of the returned structure. */
546 memset(&pwcopy, 0, sizeof(pwcopy));
547 pwcopy.pw_name = xstrdup(pw->pw_name);
548 pwcopy.pw_passwd = xstrdup(pw->pw_passwd);
549 pwcopy.pw_uid = pw->pw_uid;
550 pwcopy.pw_gid = pw->pw_gid;
Damien Millerad833b32000-08-23 10:46:23 +1000551#ifdef HAVE_PW_CLASS_IN_PASSWD
552 pwcopy.pw_class = xstrdup(pw->pw_class);
553#endif
Damien Miller95def091999-11-25 00:26:21 +1100554 pwcopy.pw_dir = xstrdup(pw->pw_dir);
555 pwcopy.pw_shell = xstrdup(pw->pw_shell);
556 pw = &pwcopy;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000557
Damien Miller95def091999-11-25 00:26:21 +1100558 /* Initialize "log" output. Since we are the client all output
559 actually goes to the terminal. */
560 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000561
Damien Miller95def091999-11-25 00:26:21 +1100562 /* Read per-user configuration file. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000563 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, _PATH_SSH_USER_CONFFILE);
Damien Miller95def091999-11-25 00:26:21 +1100564 read_config_file(buf, host, &options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000565
Damien Miller95def091999-11-25 00:26:21 +1100566 /* Read systemwide configuration file. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000567 read_config_file(_PATH_HOST_CONFIG_FILE, host, &options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000568
Damien Miller95def091999-11-25 00:26:21 +1100569 /* Fill configuration defaults. */
570 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000571
Damien Miller95def091999-11-25 00:26:21 +1100572 /* reinit */
573 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000574
Damien Miller95def091999-11-25 00:26:21 +1100575 if (options.user == NULL)
576 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000577
Damien Miller95def091999-11-25 00:26:21 +1100578 if (options.hostname != NULL)
579 host = options.hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000580
Damien Miller95def091999-11-25 00:26:21 +1100581 /* Disable rhosts authentication if not running as root. */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100582#ifdef HAVE_CYGWIN
583 /* Ignore uid if running under Windows */
584 if (!options.use_privileged_port) {
585#else
Damien Miller95def091999-11-25 00:26:21 +1100586 if (original_effective_uid != 0 || !options.use_privileged_port) {
Damien Millerbac2d8a2000-09-05 16:13:06 +1100587#endif
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000588 debug("Rhosts Authentication disabled, "
589 "originating port will not be trusted.");
Damien Miller95def091999-11-25 00:26:21 +1100590 options.rhosts_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100591 }
Damien Miller5428f641999-11-25 11:54:57 +1100592 /*
593 * If using rsh has been selected, exec it now (without trying
594 * anything else). Note that we must release privileges first.
595 */
Damien Miller95def091999-11-25 00:26:21 +1100596 if (options.use_rsh) {
Damien Miller5428f641999-11-25 11:54:57 +1100597 /*
598 * Restore our superuser privileges. This must be done
599 * before permanently setting the uid.
600 */
Damien Miller95def091999-11-25 00:26:21 +1100601 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000602
Damien Miller95def091999-11-25 00:26:21 +1100603 /* Switch to the original uid permanently. */
604 permanently_set_uid(original_real_uid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000605
Damien Miller95def091999-11-25 00:26:21 +1100606 /* Execute rsh. */
607 rsh_connect(host, options.user, &command);
608 fatal("rsh_connect returned");
609 }
610 /* Restore our superuser privileges. */
611 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000612
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000613 /* Open a connection to the remote host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000614
Damien Miller95def091999-11-25 00:26:21 +1100615 ok = ssh_connect(host, &hostaddr, options.port,
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000616 options.connection_attempts,
617 original_effective_uid != 0 || !options.use_privileged_port,
618 original_real_uid,
619 options.proxy_command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000620
Damien Miller5428f641999-11-25 11:54:57 +1100621 /*
622 * If we successfully made the connection, load the host private key
623 * in case we will need it later for combined rsa-rhosts
624 * authentication. This must be done before releasing extra
625 * privileges, because the file is only readable by root.
626 */
Damien Millereba71ba2000-04-29 23:57:08 +1000627 if (ok && (options.protocol & SSH_PROTO_1)) {
628 Key k;
Damien Miller95def091999-11-25 00:26:21 +1100629 host_private_key = RSA_new();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100630 k.type = KEY_RSA1;
Damien Millereba71ba2000-04-29 23:57:08 +1000631 k.rsa = host_private_key;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000632 if (load_private_key(_PATH_HOST_KEY_FILE, "", &k, NULL))
Damien Miller95def091999-11-25 00:26:21 +1100633 host_private_key_loaded = 1;
634 }
Damien Miller5428f641999-11-25 11:54:57 +1100635 /*
636 * Get rid of any extra privileges that we may have. We will no
637 * longer need them. Also, extra privileges could make it very hard
638 * to read identity files and other non-world-readable files from the
639 * user's home directory if it happens to be on a NFS volume where
640 * root is mapped to nobody.
641 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000642
Damien Miller5428f641999-11-25 11:54:57 +1100643 /*
644 * Note that some legacy systems need to postpone the following call
645 * to permanently_set_uid() until the private hostkey is destroyed
646 * with RSA_free(). Otherwise the calling user could ptrace() the
647 * process, read the private hostkey and impersonate the host.
648 * OpenBSD does not allow ptracing of setuid processes.
649 */
Damien Miller95def091999-11-25 00:26:21 +1100650 permanently_set_uid(original_real_uid);
651
Damien Miller5428f641999-11-25 11:54:57 +1100652 /*
653 * Now that we are back to our own permissions, create ~/.ssh
654 * directory if it doesn\'t already exist.
655 */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000656 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, _PATH_SSH_USER_DIR);
Damien Miller95def091999-11-25 00:26:21 +1100657 if (stat(buf, &st) < 0)
Damien Millerbe484b52000-07-15 14:14:16 +1000658 if (mkdir(buf, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100659 error("Could not create directory '%.200s'.", buf);
660
661 /* Check if the connection failed, and try "rsh" if appropriate. */
662 if (!ok) {
663 if (options.port != 0)
Damien Milleraae6c611999-12-06 11:47:28 +1100664 log("Secure connection to %.100s on port %hu refused%.100s.",
Damien Miller95def091999-11-25 00:26:21 +1100665 host, options.port,
666 options.fallback_to_rsh ? "; reverting to insecure method" : "");
667 else
668 log("Secure connection to %.100s refused%.100s.", host,
669 options.fallback_to_rsh ? "; reverting to insecure method" : "");
670
671 if (options.fallback_to_rsh) {
672 rsh_connect(host, options.user, &command);
673 fatal("rsh_connect returned");
674 }
675 exit(1);
676 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100677 /* Expand ~ in options.identity_files, known host file names. */
Damien Millereba71ba2000-04-29 23:57:08 +1000678 /* XXX mem-leaks */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100679 for (i = 0; i < options.num_identity_files; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100680 options.identity_files[i] =
Damien Miller0bc1bd82000-11-13 22:57:25 +1100681 tilde_expand_filename(options.identity_files[i], original_real_uid);
682 options.identity_files_type[i] = guess_identity_file_type(options.identity_files[i]);
683 debug("identity file %s type %d", options.identity_files[i],
684 options.identity_files_type[i]);
685 }
686 options.system_hostfile =
687 tilde_expand_filename(options.system_hostfile, original_real_uid);
688 options.user_hostfile =
689 tilde_expand_filename(options.user_hostfile, original_real_uid);
690 options.system_hostfile2 =
691 tilde_expand_filename(options.system_hostfile2, original_real_uid);
692 options.user_hostfile2 =
693 tilde_expand_filename(options.user_hostfile2, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100694
695 /* Log into the remote system. This never returns if the login fails. */
696 ssh_login(host_private_key_loaded, host_private_key,
Damien Miller34132e52000-01-14 15:45:46 +1100697 host, (struct sockaddr *)&hostaddr, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100698
699 /* We no longer need the host private key. Clear it now. */
700 if (host_private_key_loaded)
701 RSA_free(host_private_key); /* Destroys contents safely */
702
Damien Miller1383bd82000-04-06 12:32:37 +1000703 exit_status = compat20 ? ssh_session2() : ssh_session();
704 packet_close();
705 return exit_status;
706}
707
Damien Millerbd483e72000-04-30 10:00:53 +1000708void
709x11_get_proto(char *proto, int proto_len, char *data, int data_len)
710{
711 char line[512];
712 FILE *f;
713 int got_data = 0, i;
714
Damien Millerd3a18572000-06-07 19:55:44 +1000715 if (options.xauth_location) {
716 /* Try to get Xauthority information for the display. */
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000717 snprintf(line, sizeof line, "%.100s list %.200s 2>" _PATH_DEVNULL,
Damien Millerd3a18572000-06-07 19:55:44 +1000718 options.xauth_location, getenv("DISPLAY"));
719 f = popen(line, "r");
720 if (f && fgets(line, sizeof(line), f) &&
721 sscanf(line, "%*s %s %s", proto, data) == 2)
722 got_data = 1;
723 if (f)
724 pclose(f);
725 }
Damien Millerbd483e72000-04-30 10:00:53 +1000726 /*
727 * If we didn't get authentication data, just make up some
728 * data. The forwarding code will check the validity of the
729 * response anyway, and substitute this data. The X11
730 * server, however, will ignore this fake data and use
731 * whatever authentication mechanisms it was using otherwise
732 * for the local connection.
733 */
734 if (!got_data) {
735 u_int32_t rand = 0;
736
737 strlcpy(proto, "MIT-MAGIC-COOKIE-1", proto_len);
738 for (i = 0; i < 16; i++) {
739 if (i % 4 == 0)
740 rand = arc4random();
741 snprintf(data + 2 * i, data_len - 2 * i, "%02x", rand & 0xff);
742 rand >>= 8;
743 }
744 }
745}
746
Damien Miller0bc1bd82000-11-13 22:57:25 +1100747void
748ssh_init_forwarding(void)
749{
Kevin Steves12057502001-02-05 14:54:34 +0000750 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100751 int i;
Kevin Steves12057502001-02-05 14:54:34 +0000752
Damien Miller0bc1bd82000-11-13 22:57:25 +1100753 /* Initiate local TCP/IP port forwardings. */
754 for (i = 0; i < options.num_local_forwards; i++) {
755 debug("Connections to local port %d forwarded to remote address %.200s:%d",
756 options.local_forwards[i].port,
757 options.local_forwards[i].host,
758 options.local_forwards[i].host_port);
Kevin Steves12057502001-02-05 14:54:34 +0000759 success += channel_request_local_forwarding(
Damien Miller0bc1bd82000-11-13 22:57:25 +1100760 options.local_forwards[i].port,
761 options.local_forwards[i].host,
762 options.local_forwards[i].host_port,
763 options.gateway_ports);
764 }
Kevin Steves12057502001-02-05 14:54:34 +0000765 if (i > 0 && success == 0)
766 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100767
768 /* Initiate remote TCP/IP port forwardings. */
769 for (i = 0; i < options.num_remote_forwards; i++) {
770 debug("Connections to remote port %d forwarded to local address %.200s:%d",
771 options.remote_forwards[i].port,
772 options.remote_forwards[i].host,
773 options.remote_forwards[i].host_port);
774 channel_request_remote_forwarding(
775 options.remote_forwards[i].port,
776 options.remote_forwards[i].host,
777 options.remote_forwards[i].host_port);
778 }
779}
780
781void
782check_agent_present(void)
783{
784 if (options.forward_agent) {
785 /* Clear agent forwarding if we don\'t have an agent. */
786 int authfd = ssh_get_authentication_socket();
787 if (authfd < 0)
788 options.forward_agent = 0;
789 else
790 ssh_close_authentication_socket(authfd);
791 }
792}
793
Damien Miller1383bd82000-04-06 12:32:37 +1000794int
795ssh_session(void)
796{
797 int type;
Damien Miller1383bd82000-04-06 12:32:37 +1000798 int plen;
799 int interactive = 0;
800 int have_tty = 0;
801 struct winsize ws;
Damien Miller1383bd82000-04-06 12:32:37 +1000802 char *cp;
803
Damien Miller95def091999-11-25 00:26:21 +1100804 /* Enable compression if requested. */
805 if (options.compression) {
806 debug("Requesting compression at level %d.", options.compression_level);
807
808 if (options.compression_level < 1 || options.compression_level > 9)
809 fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
810
811 /* Send the request. */
812 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
813 packet_put_int(options.compression_level);
814 packet_send();
815 packet_write_wait();
816 type = packet_read(&plen);
817 if (type == SSH_SMSG_SUCCESS)
818 packet_start_compression(options.compression_level);
819 else if (type == SSH_SMSG_FAILURE)
820 log("Warning: Remote host refused compression.");
821 else
822 packet_disconnect("Protocol error waiting for compression response.");
823 }
824 /* Allocate a pseudo tty if appropriate. */
825 if (tty_flag) {
826 debug("Requesting pty.");
827
828 /* Start the packet. */
829 packet_start(SSH_CMSG_REQUEST_PTY);
830
831 /* Store TERM in the packet. There is no limit on the
832 length of the string. */
833 cp = getenv("TERM");
834 if (!cp)
835 cp = "";
836 packet_put_string(cp, strlen(cp));
837
838 /* Store window size in the packet. */
839 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
840 memset(&ws, 0, sizeof(ws));
841 packet_put_int(ws.ws_row);
842 packet_put_int(ws.ws_col);
843 packet_put_int(ws.ws_xpixel);
844 packet_put_int(ws.ws_ypixel);
845
846 /* Store tty modes in the packet. */
847 tty_make_modes(fileno(stdin));
848
849 /* Send the packet, and wait for it to leave. */
850 packet_send();
851 packet_write_wait();
852
853 /* Read response from the server. */
854 type = packet_read(&plen);
Damien Miller450a7a12000-03-26 13:04:51 +1000855 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100856 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +1000857 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000858 } else if (type == SSH_SMSG_FAILURE)
Damien Miller95def091999-11-25 00:26:21 +1100859 log("Warning: Remote host failed or refused to allocate a pseudo tty.");
860 else
861 packet_disconnect("Protocol error waiting for pty request response.");
862 }
863 /* Request X11 forwarding if enabled and DISPLAY is set. */
864 if (options.forward_x11 && getenv("DISPLAY") != NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +1000865 char proto[512], data[512];
866 /* Get reasonable local authentication information. */
867 x11_get_proto(proto, sizeof proto, data, sizeof data);
868 /* Request forwarding with authentication spoofing. */
Damien Miller95def091999-11-25 00:26:21 +1100869 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Millerbd483e72000-04-30 10:00:53 +1000870 x11_request_forwarding_with_spoofing(0, proto, data);
Damien Miller95def091999-11-25 00:26:21 +1100871
872 /* Read response from the server. */
873 type = packet_read(&plen);
874 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100875 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +1000876 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller95def091999-11-25 00:26:21 +1100877 log("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +1000878 } else {
Damien Miller95def091999-11-25 00:26:21 +1100879 packet_disconnect("Protocol error waiting for X11 forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +1000880 }
Damien Miller95def091999-11-25 00:26:21 +1100881 }
882 /* Tell the packet module whether this is an interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000883 packet_set_interactive(interactive);
Damien Miller95def091999-11-25 00:26:21 +1100884
885 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100886 check_agent_present();
887
Damien Miller95def091999-11-25 00:26:21 +1100888 if (options.forward_agent) {
889 debug("Requesting authentication agent forwarding.");
890 auth_request_forwarding();
891
892 /* Read response from the server. */
893 type = packet_read(&plen);
894 packet_integrity_check(plen, 0, type);
895 if (type != SSH_SMSG_SUCCESS)
896 log("Warning: Remote host denied authentication agent forwarding.");
897 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000898
Damien Miller0bc1bd82000-11-13 22:57:25 +1100899 /* Initiate port forwardings. */
900 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000901
Damien Miller5428f641999-11-25 11:54:57 +1100902 /* If requested, let ssh continue in the background. */
Damien Miller4af51302000-04-16 11:18:38 +1000903 if (fork_after_authentication_flag)
Damien Miller5428f641999-11-25 11:54:57 +1100904 if (daemon(1, 1) < 0)
905 fatal("daemon() failed: %.200s", strerror(errno));
906
907 /*
908 * If a command was specified on the command line, execute the
909 * command now. Otherwise request the server to start a shell.
910 */
Damien Miller95def091999-11-25 00:26:21 +1100911 if (buffer_len(&command) > 0) {
912 int len = buffer_len(&command);
913 if (len > 900)
914 len = 900;
915 debug("Sending command: %.*s", len, buffer_ptr(&command));
916 packet_start(SSH_CMSG_EXEC_CMD);
917 packet_put_string(buffer_ptr(&command), buffer_len(&command));
918 packet_send();
919 packet_write_wait();
920 } else {
921 debug("Requesting shell.");
922 packet_start(SSH_CMSG_EXEC_SHELL);
923 packet_send();
924 packet_write_wait();
925 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000926
Damien Miller95def091999-11-25 00:26:21 +1100927 /* Enter the interactive session. */
Damien Millerad833b32000-08-23 10:46:23 +1000928 return client_loop(have_tty, tty_flag ? options.escape_char : -1, 0);
Damien Miller1383bd82000-04-06 12:32:37 +1000929}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000930
Damien Miller1383bd82000-04-06 12:32:37 +1000931void
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000932client_subsystem_reply(int type, int plen, void *ctxt)
933{
934 int id, len;
935
936 id = packet_get_int();
937 len = buffer_len(&command);
938 len = MAX(len, 900);
939 packet_done();
940 if (type == SSH2_MSG_CHANNEL_FAILURE)
941 fatal("Request for subsystem '%.*s' failed on channel %d",
942 len, buffer_ptr(&command), id);
943}
944
945void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100946ssh_session2_callback(int id, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +1000947{
948 int len;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000949 int interactive = 0;
950
Kevin Stevesadf74cd2001-02-05 14:22:50 +0000951 debug("client_init id %d arg %ld", id, (long)arg);
Damien Miller1383bd82000-04-06 12:32:37 +1000952
953 if (no_shell_flag)
954 goto done;
955
956 if (tty_flag) {
957 struct winsize ws;
958 char *cp;
959 cp = getenv("TERM");
960 if (!cp)
961 cp = "";
962 /* Store window size in the packet. */
963 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
964 memset(&ws, 0, sizeof(ws));
965
966 channel_request_start(id, "pty-req", 0);
967 packet_put_cstring(cp);
968 packet_put_int(ws.ws_col);
969 packet_put_int(ws.ws_row);
970 packet_put_int(ws.ws_xpixel);
971 packet_put_int(ws.ws_ypixel);
972 packet_put_cstring(""); /* XXX: encode terminal modes */
973 packet_send();
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000974 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +1000975 /* XXX wait for reply */
976 }
Damien Millerbd483e72000-04-30 10:00:53 +1000977 if (options.forward_x11 &&
978 getenv("DISPLAY") != NULL) {
979 char proto[512], data[512];
980 /* Get reasonable local authentication information. */
981 x11_get_proto(proto, sizeof proto, data, sizeof data);
982 /* Request forwarding with authentication spoofing. */
983 debug("Requesting X11 forwarding with authentication spoofing.");
984 x11_request_forwarding_with_spoofing(id, proto, data);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000985 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +1000986 /* XXX wait for reply */
987 }
988
Damien Miller0bc1bd82000-11-13 22:57:25 +1100989 check_agent_present();
990 if (options.forward_agent) {
991 debug("Requesting authentication agent forwarding.");
992 channel_request_start(id, "auth-agent-req@openssh.com", 0);
993 packet_send();
994 }
995
Damien Miller1383bd82000-04-06 12:32:37 +1000996 len = buffer_len(&command);
997 if (len > 0) {
998 if (len > 900)
999 len = 900;
Damien Miller832562e2001-01-30 09:30:01 +11001000 if (subsystem_flag) {
1001 debug("Sending subsystem: %.*s", len, buffer_ptr(&command));
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001002 channel_request_start(id, "subsystem", /*want reply*/ 1);
1003 /* register callback for reply */
1004 /* XXX we asume that client_loop has already been called */
1005 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &client_subsystem_reply);
1006 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &client_subsystem_reply);
Damien Miller832562e2001-01-30 09:30:01 +11001007 } else {
1008 debug("Sending command: %.*s", len, buffer_ptr(&command));
1009 channel_request_start(id, "exec", 0);
1010 }
Damien Miller1383bd82000-04-06 12:32:37 +10001011 packet_put_string(buffer_ptr(&command), len);
1012 packet_send();
1013 } else {
1014 channel_request(id, "shell", 0);
1015 }
1016 /* channel_callback(id, SSH2_MSG_OPEN_CONFIGMATION, client_init, 0); */
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001017
Damien Miller1383bd82000-04-06 12:32:37 +10001018done:
1019 /* register different callback, etc. XXX */
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001020 packet_set_interactive(interactive);
Damien Miller1383bd82000-04-06 12:32:37 +10001021}
1022
1023int
1024ssh_session2(void)
1025{
1026 int window, packetmax, id;
Damien Millerad833b32000-08-23 10:46:23 +10001027 int in, out, err;
1028
Damien Millercaf6dd62000-08-29 11:33:50 +11001029 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001030 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001031 } else {
1032 in = dup(STDIN_FILENO);
1033 }
1034 out = dup(STDOUT_FILENO);
1035 err = dup(STDERR_FILENO);
1036
1037 if (in < 0 || out < 0 || err < 0)
1038 fatal("dup() in/out/err failed");
1039
Damien Miller69b69aa2000-10-28 14:19:58 +11001040 /* enable nonblocking unless tty */
1041 if (!isatty(in))
1042 set_nonblock(in);
1043 if (!isatty(out))
1044 set_nonblock(out);
1045 if (!isatty(err))
1046 set_nonblock(err);
1047
Damien Miller0bc1bd82000-11-13 22:57:25 +11001048 /* XXX should be pre-session */
1049 ssh_init_forwarding();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001050
Damien Millerad833b32000-08-23 10:46:23 +10001051 /* If requested, let ssh continue in the background. */
1052 if (fork_after_authentication_flag)
1053 if (daemon(1, 1) < 0)
1054 fatal("daemon() failed: %.200s", strerror(errno));
1055
Damien Millere4340be2000-09-16 13:29:08 +11001056 window = CHAN_SES_WINDOW_DEFAULT;
1057 packetmax = CHAN_SES_PACKET_DEFAULT;
1058 if (!tty_flag) {
Damien Miller1383bd82000-04-06 12:32:37 +10001059 window *= 2;
Damien Millere4340be2000-09-16 13:29:08 +11001060 packetmax *=2;
Damien Miller1383bd82000-04-06 12:32:37 +10001061 }
Damien Miller1383bd82000-04-06 12:32:37 +10001062 id = channel_new(
1063 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001064 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Miller69b69aa2000-10-28 14:19:58 +11001065 xstrdup("client-session"), /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001066
Damien Miller1383bd82000-04-06 12:32:37 +10001067 channel_open(id);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001068 channel_register_callback(id, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION,
1069 ssh_session2_callback, (void *)0);
Damien Miller1383bd82000-04-06 12:32:37 +10001070
Damien Millerad833b32000-08-23 10:46:23 +10001071 return client_loop(tty_flag, tty_flag ? options.escape_char : -1, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001072}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001073
1074int
1075guess_identity_file_type(const char *filename)
1076{
1077 struct stat st;
1078 Key *public;
1079 int type = KEY_RSA1; /* default */
1080
1081 if (stat(filename, &st) < 0) {
Ben Lindstrom14920292000-11-21 21:24:55 +00001082 /* ignore this key */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001083 return KEY_UNSPEC;
1084 }
1085 public = key_new(type);
1086 if (!load_public_key(filename, public, NULL)) {
1087 /* ok, so we will assume this is 'some' key */
1088 type = KEY_UNSPEC;
1089 }
1090 key_free(public);
1091 return type;
1092}