blob: f1beb8c533d8153dca6e37e0045c38c37d66ae1e [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 Lindstrombf555ba2001-01-18 02:04:35 +000042RCSID("$OpenBSD: ssh.c,v 1.82 2001/01/15 21:40:10 markus Exp $");
Damien Millereba71ba2000-04-29 23:57:08 +100043
44#include <openssl/evp.h>
45#include <openssl/dsa.h>
46#include <openssl/rsa.h>
Damien Miller0bc1bd82000-11-13 22:57:25 +110047#include <openssl/err.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048
49#include "xmalloc.h"
50#include "ssh.h"
51#include "packet.h"
52#include "buffer.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053#include "readconf.h"
54#include "uidswap.h"
Damien Miller1383bd82000-04-06 12:32:37 +100055
56#include "ssh2.h"
57#include "compat.h"
Damien Millerb38eff82000-04-01 11:09:21 +100058#include "channels.h"
Damien Millereba71ba2000-04-29 23:57:08 +100059#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100060#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100061#include "authfile.h"
Ben Lindstrombf555ba2001-01-18 02:04:35 +000062#include "clientloop.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063
Damien Miller3f905871999-11-15 17:10:57 +110064#ifdef HAVE___PROGNAME
65extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000066#else
67char *__progname;
68#endif
Damien Miller3f905871999-11-15 17:10:57 +110069
Damien Miller34132e52000-01-14 15:45:46 +110070/* Flag indicating whether IPv4 or IPv6. This can be set on the command line.
71 Default value is AF_UNSPEC means both IPv4 and IPv6. */
Damien Miller7d80e342000-01-19 14:36:49 +110072#ifdef IPV4_DEFAULT
73int IPv4or6 = AF_INET;
74#else
Damien Miller34132e52000-01-14 15:45:46 +110075int IPv4or6 = AF_UNSPEC;
Damien Miller7d80e342000-01-19 14:36:49 +110076#endif
Damien Miller34132e52000-01-14 15:45:46 +110077
Damien Miller95def091999-11-25 00:26:21 +110078/* Flag indicating whether debug mode is on. This can be set on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079int debug_flag = 0;
80
Damien Miller78928792000-04-12 20:17:38 +100081/* Flag indicating whether a tty should be allocated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082int tty_flag = 0;
Ben Lindstrom4dccfa52000-12-28 16:40:05 +000083int no_tty_flag = 0;
84int force_tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
Damien Miller1383bd82000-04-06 12:32:37 +100086/* don't exec a shell */
87int no_shell_flag = 0;
Damien Miller1383bd82000-04-06 12:32:37 +100088
Damien Miller5428f641999-11-25 11:54:57 +110089/*
90 * Flag indicating that nothing should be read from stdin. This can be set
91 * on the command line.
92 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093int stdin_null_flag = 0;
94
Damien Miller5428f641999-11-25 11:54:57 +110095/*
96 * Flag indicating that ssh should fork after authentication. This is useful
97 * so that the pasphrase can be entered manually, and then ssh goes to the
98 * background.
99 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100int fork_after_authentication_flag = 0;
101
Damien Miller5428f641999-11-25 11:54:57 +1100102/*
103 * General data structure for command line options and options configurable
104 * in configuration files. See readconf.h.
105 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106Options options;
107
Damien Miller5428f641999-11-25 11:54:57 +1100108/*
109 * Name of the host we are connecting to. This is the name given on the
110 * command line, or the HostName specified for the user-supplied name in a
111 * configuration file.
112 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113char *host;
114
115/* socket address the host resolves to */
Damien Miller34132e52000-01-14 15:45:46 +1100116struct sockaddr_storage hostaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117
Damien Miller5428f641999-11-25 11:54:57 +1100118/*
119 * Flag to indicate that we have received a window change signal which has
120 * not yet been processed. This will cause a message indicating the new
121 * window size to be sent to the server a little later. This is volatile
122 * because this is updated in a signal handler.
123 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124volatile int received_window_change_signal = 0;
125
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126/* Flag indicating whether we have a valid host private key loaded. */
127int host_private_key_loaded = 0;
128
129/* Host private key. */
130RSA *host_private_key = NULL;
131
132/* Original real UID. */
133uid_t original_real_uid;
134
Damien Miller1383bd82000-04-06 12:32:37 +1000135/* command to be executed */
136Buffer command;
137
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138/* Prints a help message to the user. This function never returns. */
139
140void
141usage()
142{
Kevin Stevesec84dc12000-12-13 17:45:15 +0000143 fprintf(stderr, "Usage: %s [options] host [command]\n", __progname);
Damien Miller95def091999-11-25 00:26:21 +1100144 fprintf(stderr, "Options:\n");
145 fprintf(stderr, " -l user Log in using this user name.\n");
146 fprintf(stderr, " -n Redirect input from /dev/null.\n");
Damien Millerb1715dc2000-05-30 13:44:51 +1000147 fprintf(stderr, " -A Enable authentication agent forwarding.\n");
Damien Miller95def091999-11-25 00:26:21 +1100148 fprintf(stderr, " -a Disable authentication agent forwarding.\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000149#ifdef AFS
Damien Miller95def091999-11-25 00:26:21 +1100150 fprintf(stderr, " -k Disable Kerberos ticket and AFS token forwarding.\n");
151#endif /* AFS */
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000152 fprintf(stderr, " -X Enable X11 connection forwarding.\n");
Damien Miller95def091999-11-25 00:26:21 +1100153 fprintf(stderr, " -x Disable X11 connection forwarding.\n");
154 fprintf(stderr, " -i file Identity for RSA authentication (default: ~/.ssh/identity).\n");
155 fprintf(stderr, " -t Tty; allocate a tty even if command is given.\n");
Damien Miller1383bd82000-04-06 12:32:37 +1000156 fprintf(stderr, " -T Do not allocate a tty.\n");
Damien Miller95def091999-11-25 00:26:21 +1100157 fprintf(stderr, " -v Verbose; display verbose debugging messages.\n");
Damien Millere4340be2000-09-16 13:29:08 +1100158 fprintf(stderr, " Multiple -v increases verbosity.\n");
Damien Miller95def091999-11-25 00:26:21 +1100159 fprintf(stderr, " -V Display version number only.\n");
160 fprintf(stderr, " -P Don't allocate a privileged port.\n");
161 fprintf(stderr, " -q Quiet; don't display any warning messages.\n");
162 fprintf(stderr, " -f Fork into background after authentication.\n");
163 fprintf(stderr, " -e char Set escape character; ``none'' = disable (default: ~).\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164
Damien Miller95def091999-11-25 00:26:21 +1100165 fprintf(stderr, " -c cipher Select encryption algorithm: "
166 "``3des'', "
167 "``blowfish''\n");
168 fprintf(stderr, " -p port Connect to this port. Server must be on the same port.\n");
169 fprintf(stderr, " -L listen-port:host:port Forward local port to remote address\n");
170 fprintf(stderr, " -R listen-port:host:port Forward remote port to local address\n");
Kevin Stevesec84dc12000-12-13 17:45:15 +0000171 fprintf(stderr, " These cause %s to listen for connections on a port, and\n", __progname);
Damien Miller95def091999-11-25 00:26:21 +1100172 fprintf(stderr, " forward them to the other side by connecting to host:port.\n");
173 fprintf(stderr, " -C Enable compression.\n");
Damien Miller1383bd82000-04-06 12:32:37 +1000174 fprintf(stderr, " -N Do not execute a shell or command.\n");
Damien Miller95def091999-11-25 00:26:21 +1100175 fprintf(stderr, " -g Allow remote hosts to connect to forwarded ports.\n");
Damien Miller34132e52000-01-14 15:45:46 +1100176 fprintf(stderr, " -4 Use IPv4 only.\n");
177 fprintf(stderr, " -6 Use IPv6 only.\n");
Damien Miller4af51302000-04-16 11:18:38 +1000178 fprintf(stderr, " -2 Force protocol version 2.\n");
Damien Miller95def091999-11-25 00:26:21 +1100179 fprintf(stderr, " -o 'option' Process the option as if it was read from a configuration file.\n");
180 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000181}
182
Damien Miller95def091999-11-25 00:26:21 +1100183/*
184 * Connects to the given host using rsh (or prints an error message and exits
185 * if rsh is not available). This function never returns.
186 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000187void
Damien Miller95def091999-11-25 00:26:21 +1100188rsh_connect(char *host, char *user, Buffer * command)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000189{
Damien Miller95def091999-11-25 00:26:21 +1100190 char *args[10];
191 int i;
192
193 log("Using rsh. WARNING: Connection will not be encrypted.");
194 /* Build argument list for rsh. */
195 i = 0;
196 args[i++] = _PATH_RSH;
197 /* host may have to come after user on some systems */
198 args[i++] = host;
199 if (user) {
200 args[i++] = "-l";
201 args[i++] = user;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202 }
Damien Miller95def091999-11-25 00:26:21 +1100203 if (buffer_len(command) > 0) {
204 buffer_append(command, "\0", 1);
205 args[i++] = buffer_ptr(command);
206 }
207 args[i++] = NULL;
208 if (debug_flag) {
209 for (i = 0; args[i]; i++) {
210 if (i != 0)
211 fprintf(stderr, " ");
212 fprintf(stderr, "%s", args[i]);
213 }
214 fprintf(stderr, "\n");
215 }
216 execv(_PATH_RSH, args);
217 perror(_PATH_RSH);
218 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000219}
220
Damien Miller0bc1bd82000-11-13 22:57:25 +1100221int ssh_session(void);
222int ssh_session2(void);
223int guess_identity_file_type(const char *filename);
Damien Miller1383bd82000-04-06 12:32:37 +1000224
Damien Miller95def091999-11-25 00:26:21 +1100225/*
226 * Main program for the ssh client.
227 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000228int
229main(int ac, char **av)
230{
Damien Miller1383bd82000-04-06 12:32:37 +1000231 int i, opt, optind, exit_status, ok;
Damien Milleraae6c611999-12-06 11:47:28 +1100232 u_short fwd_port, fwd_host_port;
Damien Miller95def091999-11-25 00:26:21 +1100233 char *optarg, *cp, buf[256];
Damien Miller95def091999-11-25 00:26:21 +1100234 struct stat st;
235 struct passwd *pw, pwcopy;
Damien Miller1383bd82000-04-06 12:32:37 +1000236 int dummy;
Damien Miller95def091999-11-25 00:26:21 +1100237 uid_t original_effective_uid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000238
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000239 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000240 init_rng();
241
Damien Miller5428f641999-11-25 11:54:57 +1100242 /*
243 * Save the original real uid. It will be needed later (uid-swapping
244 * may clobber the real uid).
245 */
Damien Miller95def091999-11-25 00:26:21 +1100246 original_real_uid = getuid();
247 original_effective_uid = geteuid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248
Damien Miller05dd7952000-10-01 00:42:48 +1100249#ifdef HAVE_SETRLIMIT
Damien Miller95def091999-11-25 00:26:21 +1100250 /* If we are installed setuid root be careful to not drop core. */
251 if (original_real_uid != original_effective_uid) {
252 struct rlimit rlim;
253 rlim.rlim_cur = rlim.rlim_max = 0;
254 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
255 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000256 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100257#endif
Damien Miller5428f641999-11-25 11:54:57 +1100258 /*
259 * Use uid-swapping to give up root privileges for the duration of
260 * option processing. We will re-instantiate the rights when we are
261 * ready to create the privileged port, and will permanently drop
262 * them when the port has been created (actually, when the connection
263 * has been made, as we may need to create the port several times).
264 */
Damien Miller95def091999-11-25 00:26:21 +1100265 temporarily_use_uid(original_real_uid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000266
Damien Miller5428f641999-11-25 11:54:57 +1100267 /*
268 * Set our umask to something reasonable, as some files are created
269 * with the default umask. This will make them world-readable but
270 * writable only by the owner, which is ok for all files for which we
271 * don't set the modes explicitly.
272 */
Damien Miller95def091999-11-25 00:26:21 +1100273 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000274
Damien Miller95def091999-11-25 00:26:21 +1100275 /* Initialize option structure to indicate that no values have been set. */
276 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000277
Damien Miller95def091999-11-25 00:26:21 +1100278 /* Parse command-line arguments. */
279 host = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000280
Damien Miller95def091999-11-25 00:26:21 +1100281 /* If program name is not one of the standard names, use it as host name. */
Kevin Stevesec84dc12000-12-13 17:45:15 +0000282 cp = __progname;
Damien Millerbac2d8a2000-09-05 16:13:06 +1100283#ifdef HAVE_CYGWIN
284 if (strcasecmp(cp, "rsh") && strcasecmp(cp, "ssh") &&
285 strcasecmp(cp, "rlogin") && strcasecmp(cp, "slogin") &&
286 strcasecmp(cp, "remsh") &&
287 strcasecmp(cp, "rsh.exe") && strcasecmp(cp, "ssh.exe") &&
288 strcasecmp(cp, "rlogin.exe") && strcasecmp(cp, "slogin.exe") &&
289 strcasecmp(cp, "remsh.exe"))
290#else
Damien Millerad833b32000-08-23 10:46:23 +1000291 if (strcmp(cp, "rsh") && strcmp(cp, "ssh") && strcmp(cp, "rlogin") &&
292 strcmp(cp, "slogin") && strcmp(cp, "remsh"))
Damien Millerbac2d8a2000-09-05 16:13:06 +1100293#endif
Damien Miller95def091999-11-25 00:26:21 +1100294 host = cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000295
Damien Miller95def091999-11-25 00:26:21 +1100296 for (optind = 1; optind < ac; optind++) {
297 if (av[optind][0] != '-') {
298 if (host)
299 break;
300 if ((cp = strchr(av[optind], '@'))) {
Damien Miller4af51302000-04-16 11:18:38 +1000301 if(cp == av[optind])
302 usage();
Damien Miller95def091999-11-25 00:26:21 +1100303 options.user = av[optind];
304 *cp = '\0';
305 host = ++cp;
306 } else
307 host = av[optind];
308 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000309 }
Damien Miller95def091999-11-25 00:26:21 +1100310 opt = av[optind][1];
311 if (!opt)
312 usage();
313 if (strchr("eilcpLRo", opt)) { /* options with arguments */
314 optarg = av[optind] + 2;
315 if (strcmp(optarg, "") == 0) {
316 if (optind >= ac - 1)
317 usage();
318 optarg = av[++optind];
319 }
320 } else {
321 if (av[optind][2])
322 usage();
323 optarg = NULL;
324 }
325 switch (opt) {
Damien Miller4af51302000-04-16 11:18:38 +1000326 case '2':
327 options.protocol = SSH_PROTO_2;
328 break;
Damien Miller34132e52000-01-14 15:45:46 +1100329 case '4':
330 IPv4or6 = AF_INET;
331 break;
Damien Miller34132e52000-01-14 15:45:46 +1100332 case '6':
333 IPv4or6 = AF_INET6;
334 break;
Damien Miller95def091999-11-25 00:26:21 +1100335 case 'n':
336 stdin_null_flag = 1;
337 break;
Damien Miller95def091999-11-25 00:26:21 +1100338 case 'f':
339 fork_after_authentication_flag = 1;
340 stdin_null_flag = 1;
341 break;
Damien Miller95def091999-11-25 00:26:21 +1100342 case 'x':
343 options.forward_x11 = 0;
344 break;
Damien Miller95def091999-11-25 00:26:21 +1100345 case 'X':
346 options.forward_x11 = 1;
347 break;
Damien Miller95def091999-11-25 00:26:21 +1100348 case 'g':
349 options.gateway_ports = 1;
350 break;
Damien Miller95def091999-11-25 00:26:21 +1100351 case 'P':
352 options.use_privileged_port = 0;
353 break;
Damien Miller95def091999-11-25 00:26:21 +1100354 case 'a':
355 options.forward_agent = 0;
356 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000357 case 'A':
358 options.forward_agent = 1;
359 break;
Damien Miller95def091999-11-25 00:26:21 +1100360#ifdef AFS
361 case 'k':
362 options.kerberos_tgt_passing = 0;
363 options.afs_token_passing = 0;
364 break;
365#endif
366 case 'i':
367 if (stat(optarg, &st) < 0) {
368 fprintf(stderr, "Warning: Identity file %s does not exist.\n",
Damien Miller0bc1bd82000-11-13 22:57:25 +1100369 optarg);
Damien Miller95def091999-11-25 00:26:21 +1100370 break;
371 }
372 if (options.num_identity_files >= SSH_MAX_IDENTITY_FILES)
373 fatal("Too many identity files specified (max %d)",
Damien Miller0bc1bd82000-11-13 22:57:25 +1100374 SSH_MAX_IDENTITY_FILES);
375 options.identity_files[options.num_identity_files++] = xstrdup(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100376 break;
Damien Miller95def091999-11-25 00:26:21 +1100377 case 't':
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000378 if (tty_flag)
379 force_tty_flag = 1;
Damien Miller95def091999-11-25 00:26:21 +1100380 tty_flag = 1;
381 break;
Damien Miller95def091999-11-25 00:26:21 +1100382 case 'v':
Damien Millere4340be2000-09-16 13:29:08 +1100383 if (0 == debug_flag) {
384 debug_flag = 1;
385 options.log_level = SYSLOG_LEVEL_DEBUG1;
386 } else if (options.log_level < SYSLOG_LEVEL_DEBUG3) {
387 options.log_level++;
388 break;
389 } else {
390 fatal("Too high debugging level.\n");
391 }
392 /* fallthrough */
Damien Miller95def091999-11-25 00:26:21 +1100393 case 'V':
Damien Miller78928792000-04-12 20:17:38 +1000394 fprintf(stderr, "SSH Version %s, protocol versions %d.%d/%d.%d.\n",
395 SSH_VERSION,
396 PROTOCOL_MAJOR_1, PROTOCOL_MINOR_1,
397 PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2);
Damien Miller1383bd82000-04-06 12:32:37 +1000398 fprintf(stderr, "Compiled with SSL (0x%8.8lx).\n", SSLeay());
Damien Miller95def091999-11-25 00:26:21 +1100399 if (opt == 'V')
400 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100401 break;
Damien Miller95def091999-11-25 00:26:21 +1100402 case 'q':
403 options.log_level = SYSLOG_LEVEL_QUIET;
404 break;
Damien Miller95def091999-11-25 00:26:21 +1100405 case 'e':
406 if (optarg[0] == '^' && optarg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000407 (u_char) optarg[1] >= 64 && (u_char) optarg[1] < 128)
408 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100409 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000410 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100411 else if (strcmp(optarg, "none") == 0)
412 options.escape_char = -2;
413 else {
414 fprintf(stderr, "Bad escape character '%s'.\n", optarg);
415 exit(1);
416 }
417 break;
Damien Miller95def091999-11-25 00:26:21 +1100418 case 'c':
Damien Millereba71ba2000-04-29 23:57:08 +1000419 if (ciphers_valid(optarg)) {
420 /* SSH2 only */
421 options.ciphers = xstrdup(optarg);
Damien Miller30c3d422000-05-09 11:02:59 +1000422 options.cipher = SSH_CIPHER_ILLEGAL;
Damien Millereba71ba2000-04-29 23:57:08 +1000423 } else {
424 /* SSH1 only */
Damien Millere39cacc2000-11-29 12:18:44 +1100425 options.cipher = cipher_number(optarg);
426 if (options.cipher == -1) {
Damien Millereba71ba2000-04-29 23:57:08 +1000427 fprintf(stderr, "Unknown cipher type '%s'\n", optarg);
428 exit(1);
429 }
Damien Millere39cacc2000-11-29 12:18:44 +1100430 if (options.cipher == SSH_CIPHER_3DES) {
431 options.ciphers = "3des-cbc";
432 } else if (options.cipher == SSH_CIPHER_BLOWFISH) {
433 options.ciphers = "blowfish-cbc";
434 } else {
435 options.ciphers = (char *)-1;
436 }
Damien Miller95def091999-11-25 00:26:21 +1100437 }
438 break;
Damien Miller95def091999-11-25 00:26:21 +1100439 case 'p':
440 options.port = atoi(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100441 break;
Damien Miller95def091999-11-25 00:26:21 +1100442 case 'l':
443 options.user = optarg;
444 break;
Damien Miller95def091999-11-25 00:26:21 +1100445 case 'R':
Damien Miller34132e52000-01-14 15:45:46 +1100446 if (sscanf(optarg, "%hu/%255[^/]/%hu", &fwd_port, buf,
447 &fwd_host_port) != 3 &&
448 sscanf(optarg, "%hu:%255[^:]:%hu", &fwd_port, buf,
449 &fwd_host_port) != 3) {
Damien Miller95def091999-11-25 00:26:21 +1100450 fprintf(stderr, "Bad forwarding specification '%s'.\n", optarg);
451 usage();
452 /* NOTREACHED */
453 }
454 add_remote_forward(&options, fwd_port, buf, fwd_host_port);
455 break;
Damien Miller95def091999-11-25 00:26:21 +1100456 case 'L':
Damien Miller34132e52000-01-14 15:45:46 +1100457 if (sscanf(optarg, "%hu/%255[^/]/%hu", &fwd_port, buf,
458 &fwd_host_port) != 3 &&
459 sscanf(optarg, "%hu:%255[^:]:%hu", &fwd_port, buf,
460 &fwd_host_port) != 3) {
Damien Miller95def091999-11-25 00:26:21 +1100461 fprintf(stderr, "Bad forwarding specification '%s'.\n", optarg);
462 usage();
463 /* NOTREACHED */
464 }
465 add_local_forward(&options, fwd_port, buf, fwd_host_port);
466 break;
Damien Miller95def091999-11-25 00:26:21 +1100467 case 'C':
468 options.compression = 1;
469 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000470 case 'N':
471 no_shell_flag = 1;
472 no_tty_flag = 1;
473 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000474 case 'T':
475 no_tty_flag = 1;
476 break;
Damien Miller95def091999-11-25 00:26:21 +1100477 case 'o':
478 dummy = 1;
479 if (process_config_line(&options, host ? host : "", optarg,
480 "command-line", 0, &dummy) != 0)
481 exit(1);
482 break;
Damien Miller95def091999-11-25 00:26:21 +1100483 default:
484 usage();
485 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000486 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000487
Damien Miller95def091999-11-25 00:26:21 +1100488 /* Check that we got a host name. */
489 if (!host)
490 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000491
Damien Millercb5e44a2000-09-29 12:12:36 +1100492 SSLeay_add_all_algorithms();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100493 ERR_load_crypto_strings();
Damien Millercb5e44a2000-09-29 12:12:36 +1100494
Damien Miller95def091999-11-25 00:26:21 +1100495 /* Initialize the command to execute on remote host. */
496 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000497
Damien Miller5428f641999-11-25 11:54:57 +1100498 /*
499 * Save the command to execute on the remote host in a buffer. There
500 * is no limit on the length of the command, except by the maximum
501 * packet size. Also sets the tty flag if there is no command.
502 */
Damien Miller95def091999-11-25 00:26:21 +1100503 if (optind == ac) {
504 /* No command specified - execute shell on a tty. */
505 tty_flag = 1;
506 } else {
507 /* A command has been specified. Store it into the
508 buffer. */
509 for (i = optind; i < ac; i++) {
510 if (i > optind)
511 buffer_append(&command, " ", 1);
512 buffer_append(&command, av[i], strlen(av[i]));
513 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000514 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000515
Damien Miller95def091999-11-25 00:26:21 +1100516 /* Cannot fork to background if no command. */
Damien Millercaf6dd62000-08-29 11:33:50 +1100517 if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
Damien Miller95def091999-11-25 00:26:21 +1100518 fatal("Cannot fork into background without a command to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000519
Damien Miller95def091999-11-25 00:26:21 +1100520 /* Allocate a tty by default if no command specified. */
521 if (buffer_len(&command) == 0)
522 tty_flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000523
Ben Lindstromc72745a2000-12-02 19:03:54 +0000524 /* Force no tty*/
525 if (no_tty_flag)
526 tty_flag = 0;
Damien Miller95def091999-11-25 00:26:21 +1100527 /* Do not allocate a tty if stdin is not a tty. */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000528 if (!isatty(fileno(stdin)) && !force_tty_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100529 if (tty_flag)
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000530 log("Pseudo-terminal will not be allocated because stdin is not a terminal.\n");
Damien Miller95def091999-11-25 00:26:21 +1100531 tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000532 }
Damien Miller1383bd82000-04-06 12:32:37 +1000533
Damien Miller95def091999-11-25 00:26:21 +1100534 /* Get user data. */
535 pw = getpwuid(original_real_uid);
536 if (!pw) {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000537 log("You don't exist, go away!\n");
Damien Miller95def091999-11-25 00:26:21 +1100538 exit(1);
539 }
540 /* Take a copy of the returned structure. */
541 memset(&pwcopy, 0, sizeof(pwcopy));
542 pwcopy.pw_name = xstrdup(pw->pw_name);
543 pwcopy.pw_passwd = xstrdup(pw->pw_passwd);
544 pwcopy.pw_uid = pw->pw_uid;
545 pwcopy.pw_gid = pw->pw_gid;
Damien Millerad833b32000-08-23 10:46:23 +1000546#ifdef HAVE_PW_CLASS_IN_PASSWD
547 pwcopy.pw_class = xstrdup(pw->pw_class);
548#endif
Damien Miller95def091999-11-25 00:26:21 +1100549 pwcopy.pw_dir = xstrdup(pw->pw_dir);
550 pwcopy.pw_shell = xstrdup(pw->pw_shell);
551 pw = &pwcopy;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000552
Damien Miller95def091999-11-25 00:26:21 +1100553 /* Initialize "log" output. Since we are the client all output
554 actually goes to the terminal. */
555 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000556
Damien Miller95def091999-11-25 00:26:21 +1100557 /* Read per-user configuration file. */
558 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, SSH_USER_CONFFILE);
559 read_config_file(buf, host, &options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000560
Damien Miller95def091999-11-25 00:26:21 +1100561 /* Read systemwide configuration file. */
562 read_config_file(HOST_CONFIG_FILE, host, &options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000563
Damien Miller95def091999-11-25 00:26:21 +1100564 /* Fill configuration defaults. */
565 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000566
Damien Miller95def091999-11-25 00:26:21 +1100567 /* reinit */
568 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000569
Damien Miller95def091999-11-25 00:26:21 +1100570 if (options.user == NULL)
571 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000572
Damien Miller95def091999-11-25 00:26:21 +1100573 if (options.hostname != NULL)
574 host = options.hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000575
Damien Miller95def091999-11-25 00:26:21 +1100576 /* Disable rhosts authentication if not running as root. */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100577#ifdef HAVE_CYGWIN
578 /* Ignore uid if running under Windows */
579 if (!options.use_privileged_port) {
580#else
Damien Miller95def091999-11-25 00:26:21 +1100581 if (original_effective_uid != 0 || !options.use_privileged_port) {
Damien Millerbac2d8a2000-09-05 16:13:06 +1100582#endif
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000583 debug("Rhosts Authentication disabled, "
584 "originating port will not be trusted.");
Damien Miller95def091999-11-25 00:26:21 +1100585 options.rhosts_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100586 }
Damien Miller5428f641999-11-25 11:54:57 +1100587 /*
588 * If using rsh has been selected, exec it now (without trying
589 * anything else). Note that we must release privileges first.
590 */
Damien Miller95def091999-11-25 00:26:21 +1100591 if (options.use_rsh) {
Damien Miller5428f641999-11-25 11:54:57 +1100592 /*
593 * Restore our superuser privileges. This must be done
594 * before permanently setting the uid.
595 */
Damien Miller95def091999-11-25 00:26:21 +1100596 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000597
Damien Miller95def091999-11-25 00:26:21 +1100598 /* Switch to the original uid permanently. */
599 permanently_set_uid(original_real_uid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000600
Damien Miller95def091999-11-25 00:26:21 +1100601 /* Execute rsh. */
602 rsh_connect(host, options.user, &command);
603 fatal("rsh_connect returned");
604 }
605 /* Restore our superuser privileges. */
606 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000607
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000608 /* Open a connection to the remote host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000609
Damien Miller95def091999-11-25 00:26:21 +1100610 ok = ssh_connect(host, &hostaddr, options.port,
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000611 options.connection_attempts,
612 original_effective_uid != 0 || !options.use_privileged_port,
613 original_real_uid,
614 options.proxy_command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000615
Damien Miller5428f641999-11-25 11:54:57 +1100616 /*
617 * If we successfully made the connection, load the host private key
618 * in case we will need it later for combined rsa-rhosts
619 * authentication. This must be done before releasing extra
620 * privileges, because the file is only readable by root.
621 */
Damien Millereba71ba2000-04-29 23:57:08 +1000622 if (ok && (options.protocol & SSH_PROTO_1)) {
623 Key k;
Damien Miller95def091999-11-25 00:26:21 +1100624 host_private_key = RSA_new();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100625 k.type = KEY_RSA1;
Damien Millereba71ba2000-04-29 23:57:08 +1000626 k.rsa = host_private_key;
627 if (load_private_key(HOST_KEY_FILE, "", &k, NULL))
Damien Miller95def091999-11-25 00:26:21 +1100628 host_private_key_loaded = 1;
629 }
Damien Miller5428f641999-11-25 11:54:57 +1100630 /*
631 * Get rid of any extra privileges that we may have. We will no
632 * longer need them. Also, extra privileges could make it very hard
633 * to read identity files and other non-world-readable files from the
634 * user's home directory if it happens to be on a NFS volume where
635 * root is mapped to nobody.
636 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000637
Damien Miller5428f641999-11-25 11:54:57 +1100638 /*
639 * Note that some legacy systems need to postpone the following call
640 * to permanently_set_uid() until the private hostkey is destroyed
641 * with RSA_free(). Otherwise the calling user could ptrace() the
642 * process, read the private hostkey and impersonate the host.
643 * OpenBSD does not allow ptracing of setuid processes.
644 */
Damien Miller95def091999-11-25 00:26:21 +1100645 permanently_set_uid(original_real_uid);
646
Damien Miller5428f641999-11-25 11:54:57 +1100647 /*
648 * Now that we are back to our own permissions, create ~/.ssh
649 * directory if it doesn\'t already exist.
650 */
Damien Miller95def091999-11-25 00:26:21 +1100651 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, SSH_USER_DIR);
652 if (stat(buf, &st) < 0)
Damien Millerbe484b52000-07-15 14:14:16 +1000653 if (mkdir(buf, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100654 error("Could not create directory '%.200s'.", buf);
655
656 /* Check if the connection failed, and try "rsh" if appropriate. */
657 if (!ok) {
658 if (options.port != 0)
Damien Milleraae6c611999-12-06 11:47:28 +1100659 log("Secure connection to %.100s on port %hu refused%.100s.",
Damien Miller95def091999-11-25 00:26:21 +1100660 host, options.port,
661 options.fallback_to_rsh ? "; reverting to insecure method" : "");
662 else
663 log("Secure connection to %.100s refused%.100s.", host,
664 options.fallback_to_rsh ? "; reverting to insecure method" : "");
665
666 if (options.fallback_to_rsh) {
667 rsh_connect(host, options.user, &command);
668 fatal("rsh_connect returned");
669 }
670 exit(1);
671 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100672 /* Expand ~ in options.identity_files, known host file names. */
Damien Millereba71ba2000-04-29 23:57:08 +1000673 /* XXX mem-leaks */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100674 for (i = 0; i < options.num_identity_files; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100675 options.identity_files[i] =
Damien Miller0bc1bd82000-11-13 22:57:25 +1100676 tilde_expand_filename(options.identity_files[i], original_real_uid);
677 options.identity_files_type[i] = guess_identity_file_type(options.identity_files[i]);
678 debug("identity file %s type %d", options.identity_files[i],
679 options.identity_files_type[i]);
680 }
681 options.system_hostfile =
682 tilde_expand_filename(options.system_hostfile, original_real_uid);
683 options.user_hostfile =
684 tilde_expand_filename(options.user_hostfile, original_real_uid);
685 options.system_hostfile2 =
686 tilde_expand_filename(options.system_hostfile2, original_real_uid);
687 options.user_hostfile2 =
688 tilde_expand_filename(options.user_hostfile2, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100689
690 /* Log into the remote system. This never returns if the login fails. */
691 ssh_login(host_private_key_loaded, host_private_key,
Damien Miller34132e52000-01-14 15:45:46 +1100692 host, (struct sockaddr *)&hostaddr, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100693
694 /* We no longer need the host private key. Clear it now. */
695 if (host_private_key_loaded)
696 RSA_free(host_private_key); /* Destroys contents safely */
697
Damien Miller1383bd82000-04-06 12:32:37 +1000698 exit_status = compat20 ? ssh_session2() : ssh_session();
699 packet_close();
700 return exit_status;
701}
702
Damien Millerbd483e72000-04-30 10:00:53 +1000703void
704x11_get_proto(char *proto, int proto_len, char *data, int data_len)
705{
706 char line[512];
707 FILE *f;
708 int got_data = 0, i;
709
Damien Millerd3a18572000-06-07 19:55:44 +1000710 if (options.xauth_location) {
711 /* Try to get Xauthority information for the display. */
712 snprintf(line, sizeof line, "%.100s list %.200s 2>/dev/null",
713 options.xauth_location, getenv("DISPLAY"));
714 f = popen(line, "r");
715 if (f && fgets(line, sizeof(line), f) &&
716 sscanf(line, "%*s %s %s", proto, data) == 2)
717 got_data = 1;
718 if (f)
719 pclose(f);
720 }
Damien Millerbd483e72000-04-30 10:00:53 +1000721 /*
722 * If we didn't get authentication data, just make up some
723 * data. The forwarding code will check the validity of the
724 * response anyway, and substitute this data. The X11
725 * server, however, will ignore this fake data and use
726 * whatever authentication mechanisms it was using otherwise
727 * for the local connection.
728 */
729 if (!got_data) {
730 u_int32_t rand = 0;
731
732 strlcpy(proto, "MIT-MAGIC-COOKIE-1", proto_len);
733 for (i = 0; i < 16; i++) {
734 if (i % 4 == 0)
735 rand = arc4random();
736 snprintf(data + 2 * i, data_len - 2 * i, "%02x", rand & 0xff);
737 rand >>= 8;
738 }
739 }
740}
741
Damien Miller0bc1bd82000-11-13 22:57:25 +1100742void
743ssh_init_forwarding(void)
744{
745 int i;
746 /* Initiate local TCP/IP port forwardings. */
747 for (i = 0; i < options.num_local_forwards; i++) {
748 debug("Connections to local port %d forwarded to remote address %.200s:%d",
749 options.local_forwards[i].port,
750 options.local_forwards[i].host,
751 options.local_forwards[i].host_port);
752 channel_request_local_forwarding(
753 options.local_forwards[i].port,
754 options.local_forwards[i].host,
755 options.local_forwards[i].host_port,
756 options.gateway_ports);
757 }
758
759 /* Initiate remote TCP/IP port forwardings. */
760 for (i = 0; i < options.num_remote_forwards; i++) {
761 debug("Connections to remote port %d forwarded to local address %.200s:%d",
762 options.remote_forwards[i].port,
763 options.remote_forwards[i].host,
764 options.remote_forwards[i].host_port);
765 channel_request_remote_forwarding(
766 options.remote_forwards[i].port,
767 options.remote_forwards[i].host,
768 options.remote_forwards[i].host_port);
769 }
770}
771
772void
773check_agent_present(void)
774{
775 if (options.forward_agent) {
776 /* Clear agent forwarding if we don\'t have an agent. */
777 int authfd = ssh_get_authentication_socket();
778 if (authfd < 0)
779 options.forward_agent = 0;
780 else
781 ssh_close_authentication_socket(authfd);
782 }
783}
784
Damien Miller1383bd82000-04-06 12:32:37 +1000785int
786ssh_session(void)
787{
788 int type;
Damien Miller1383bd82000-04-06 12:32:37 +1000789 int plen;
790 int interactive = 0;
791 int have_tty = 0;
792 struct winsize ws;
Damien Miller1383bd82000-04-06 12:32:37 +1000793 char *cp;
794
Damien Miller95def091999-11-25 00:26:21 +1100795 /* Enable compression if requested. */
796 if (options.compression) {
797 debug("Requesting compression at level %d.", options.compression_level);
798
799 if (options.compression_level < 1 || options.compression_level > 9)
800 fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
801
802 /* Send the request. */
803 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
804 packet_put_int(options.compression_level);
805 packet_send();
806 packet_write_wait();
807 type = packet_read(&plen);
808 if (type == SSH_SMSG_SUCCESS)
809 packet_start_compression(options.compression_level);
810 else if (type == SSH_SMSG_FAILURE)
811 log("Warning: Remote host refused compression.");
812 else
813 packet_disconnect("Protocol error waiting for compression response.");
814 }
815 /* Allocate a pseudo tty if appropriate. */
816 if (tty_flag) {
817 debug("Requesting pty.");
818
819 /* Start the packet. */
820 packet_start(SSH_CMSG_REQUEST_PTY);
821
822 /* Store TERM in the packet. There is no limit on the
823 length of the string. */
824 cp = getenv("TERM");
825 if (!cp)
826 cp = "";
827 packet_put_string(cp, strlen(cp));
828
829 /* Store window size in the packet. */
830 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
831 memset(&ws, 0, sizeof(ws));
832 packet_put_int(ws.ws_row);
833 packet_put_int(ws.ws_col);
834 packet_put_int(ws.ws_xpixel);
835 packet_put_int(ws.ws_ypixel);
836
837 /* Store tty modes in the packet. */
838 tty_make_modes(fileno(stdin));
839
840 /* Send the packet, and wait for it to leave. */
841 packet_send();
842 packet_write_wait();
843
844 /* Read response from the server. */
845 type = packet_read(&plen);
Damien Miller450a7a12000-03-26 13:04:51 +1000846 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100847 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +1000848 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000849 } else if (type == SSH_SMSG_FAILURE)
Damien Miller95def091999-11-25 00:26:21 +1100850 log("Warning: Remote host failed or refused to allocate a pseudo tty.");
851 else
852 packet_disconnect("Protocol error waiting for pty request response.");
853 }
854 /* Request X11 forwarding if enabled and DISPLAY is set. */
855 if (options.forward_x11 && getenv("DISPLAY") != NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +1000856 char proto[512], data[512];
857 /* Get reasonable local authentication information. */
858 x11_get_proto(proto, sizeof proto, data, sizeof data);
859 /* Request forwarding with authentication spoofing. */
Damien Miller95def091999-11-25 00:26:21 +1100860 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Millerbd483e72000-04-30 10:00:53 +1000861 x11_request_forwarding_with_spoofing(0, proto, data);
Damien Miller95def091999-11-25 00:26:21 +1100862
863 /* Read response from the server. */
864 type = packet_read(&plen);
865 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100866 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +1000867 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller95def091999-11-25 00:26:21 +1100868 log("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +1000869 } else {
Damien Miller95def091999-11-25 00:26:21 +1100870 packet_disconnect("Protocol error waiting for X11 forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +1000871 }
Damien Miller95def091999-11-25 00:26:21 +1100872 }
873 /* Tell the packet module whether this is an interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000874 packet_set_interactive(interactive);
Damien Miller95def091999-11-25 00:26:21 +1100875
876 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100877 check_agent_present();
878
Damien Miller95def091999-11-25 00:26:21 +1100879 if (options.forward_agent) {
880 debug("Requesting authentication agent forwarding.");
881 auth_request_forwarding();
882
883 /* Read response from the server. */
884 type = packet_read(&plen);
885 packet_integrity_check(plen, 0, type);
886 if (type != SSH_SMSG_SUCCESS)
887 log("Warning: Remote host denied authentication agent forwarding.");
888 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000889
Damien Miller0bc1bd82000-11-13 22:57:25 +1100890 /* Initiate port forwardings. */
891 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000892
Damien Miller5428f641999-11-25 11:54:57 +1100893 /* If requested, let ssh continue in the background. */
Damien Miller4af51302000-04-16 11:18:38 +1000894 if (fork_after_authentication_flag)
Damien Miller5428f641999-11-25 11:54:57 +1100895 if (daemon(1, 1) < 0)
896 fatal("daemon() failed: %.200s", strerror(errno));
897
898 /*
899 * If a command was specified on the command line, execute the
900 * command now. Otherwise request the server to start a shell.
901 */
Damien Miller95def091999-11-25 00:26:21 +1100902 if (buffer_len(&command) > 0) {
903 int len = buffer_len(&command);
904 if (len > 900)
905 len = 900;
906 debug("Sending command: %.*s", len, buffer_ptr(&command));
907 packet_start(SSH_CMSG_EXEC_CMD);
908 packet_put_string(buffer_ptr(&command), buffer_len(&command));
909 packet_send();
910 packet_write_wait();
911 } else {
912 debug("Requesting shell.");
913 packet_start(SSH_CMSG_EXEC_SHELL);
914 packet_send();
915 packet_write_wait();
916 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000917
Damien Miller95def091999-11-25 00:26:21 +1100918 /* Enter the interactive session. */
Damien Millerad833b32000-08-23 10:46:23 +1000919 return client_loop(have_tty, tty_flag ? options.escape_char : -1, 0);
Damien Miller1383bd82000-04-06 12:32:37 +1000920}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000921
Damien Miller1383bd82000-04-06 12:32:37 +1000922void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100923ssh_session2_callback(int id, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +1000924{
925 int len;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000926 int interactive = 0;
927
Damien Miller1383bd82000-04-06 12:32:37 +1000928 debug("client_init id %d arg %d", id, (int)arg);
929
930 if (no_shell_flag)
931 goto done;
932
933 if (tty_flag) {
934 struct winsize ws;
935 char *cp;
936 cp = getenv("TERM");
937 if (!cp)
938 cp = "";
939 /* Store window size in the packet. */
940 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
941 memset(&ws, 0, sizeof(ws));
942
943 channel_request_start(id, "pty-req", 0);
944 packet_put_cstring(cp);
945 packet_put_int(ws.ws_col);
946 packet_put_int(ws.ws_row);
947 packet_put_int(ws.ws_xpixel);
948 packet_put_int(ws.ws_ypixel);
949 packet_put_cstring(""); /* XXX: encode terminal modes */
950 packet_send();
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000951 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +1000952 /* XXX wait for reply */
953 }
Damien Millerbd483e72000-04-30 10:00:53 +1000954 if (options.forward_x11 &&
955 getenv("DISPLAY") != NULL) {
956 char proto[512], data[512];
957 /* Get reasonable local authentication information. */
958 x11_get_proto(proto, sizeof proto, data, sizeof data);
959 /* Request forwarding with authentication spoofing. */
960 debug("Requesting X11 forwarding with authentication spoofing.");
961 x11_request_forwarding_with_spoofing(id, proto, data);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000962 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +1000963 /* XXX wait for reply */
964 }
965
Damien Miller0bc1bd82000-11-13 22:57:25 +1100966 check_agent_present();
967 if (options.forward_agent) {
968 debug("Requesting authentication agent forwarding.");
969 channel_request_start(id, "auth-agent-req@openssh.com", 0);
970 packet_send();
971 }
972
Damien Miller1383bd82000-04-06 12:32:37 +1000973 len = buffer_len(&command);
974 if (len > 0) {
975 if (len > 900)
976 len = 900;
977 debug("Sending command: %.*s", len, buffer_ptr(&command));
978 channel_request_start(id, "exec", 0);
979 packet_put_string(buffer_ptr(&command), len);
980 packet_send();
981 } else {
982 channel_request(id, "shell", 0);
983 }
984 /* channel_callback(id, SSH2_MSG_OPEN_CONFIGMATION, client_init, 0); */
985done:
986 /* register different callback, etc. XXX */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000987 packet_set_interactive(interactive);
988 clientloop_set_session_ident(id);
Damien Miller1383bd82000-04-06 12:32:37 +1000989}
990
991int
992ssh_session2(void)
993{
994 int window, packetmax, id;
Damien Millerad833b32000-08-23 10:46:23 +1000995 int in, out, err;
996
Damien Millercaf6dd62000-08-29 11:33:50 +1100997 if (stdin_null_flag) {
998 in = open("/dev/null", O_RDONLY);
999 } else {
1000 in = dup(STDIN_FILENO);
1001 }
1002 out = dup(STDOUT_FILENO);
1003 err = dup(STDERR_FILENO);
1004
1005 if (in < 0 || out < 0 || err < 0)
1006 fatal("dup() in/out/err failed");
1007
Damien Miller69b69aa2000-10-28 14:19:58 +11001008 /* enable nonblocking unless tty */
1009 if (!isatty(in))
1010 set_nonblock(in);
1011 if (!isatty(out))
1012 set_nonblock(out);
1013 if (!isatty(err))
1014 set_nonblock(err);
1015
Damien Miller0bc1bd82000-11-13 22:57:25 +11001016 /* XXX should be pre-session */
1017 ssh_init_forwarding();
Damien Millercaf6dd62000-08-29 11:33:50 +11001018
Damien Millerad833b32000-08-23 10:46:23 +10001019 /* If requested, let ssh continue in the background. */
1020 if (fork_after_authentication_flag)
1021 if (daemon(1, 1) < 0)
1022 fatal("daemon() failed: %.200s", strerror(errno));
1023
Damien Millere4340be2000-09-16 13:29:08 +11001024 window = CHAN_SES_WINDOW_DEFAULT;
1025 packetmax = CHAN_SES_PACKET_DEFAULT;
1026 if (!tty_flag) {
Damien Miller1383bd82000-04-06 12:32:37 +10001027 window *= 2;
Damien Millere4340be2000-09-16 13:29:08 +11001028 packetmax *=2;
Damien Miller1383bd82000-04-06 12:32:37 +10001029 }
Damien Miller1383bd82000-04-06 12:32:37 +10001030 id = channel_new(
1031 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001032 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Miller69b69aa2000-10-28 14:19:58 +11001033 xstrdup("client-session"), /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001034
Damien Miller1383bd82000-04-06 12:32:37 +10001035 channel_open(id);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001036 channel_register_callback(id, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION,
1037 ssh_session2_callback, (void *)0);
Damien Miller1383bd82000-04-06 12:32:37 +10001038
Damien Millerad833b32000-08-23 10:46:23 +10001039 return client_loop(tty_flag, tty_flag ? options.escape_char : -1, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001040}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001041
1042int
1043guess_identity_file_type(const char *filename)
1044{
1045 struct stat st;
1046 Key *public;
1047 int type = KEY_RSA1; /* default */
1048
1049 if (stat(filename, &st) < 0) {
Ben Lindstrom14920292000-11-21 21:24:55 +00001050 /* ignore this key */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001051 return KEY_UNSPEC;
1052 }
1053 public = key_new(type);
1054 if (!load_public_key(filename, public, NULL)) {
1055 /* ok, so we will assume this is 'some' key */
1056 type = KEY_UNSPEC;
1057 }
1058 key_free(public);
1059 return type;
1060}