blob: ddf28c538a16f803bec1d63e15cf44c0df14070c [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 Lindstrom31ca54a2001-02-09 02:11:24 +000042RCSID("$OpenBSD: ssh.c,v 1.93 2001/02/08 19:30:52 itojun 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 /* If program name is not one of the standard names, use it as host name. */
Kevin Stevesec84dc12000-12-13 17:45:15 +0000292 cp = __progname;
Damien Millerbac2d8a2000-09-05 16:13:06 +1100293#ifdef HAVE_CYGWIN
294 if (strcasecmp(cp, "rsh") && strcasecmp(cp, "ssh") &&
295 strcasecmp(cp, "rlogin") && strcasecmp(cp, "slogin") &&
296 strcasecmp(cp, "remsh") &&
297 strcasecmp(cp, "rsh.exe") && strcasecmp(cp, "ssh.exe") &&
298 strcasecmp(cp, "rlogin.exe") && strcasecmp(cp, "slogin.exe") &&
299 strcasecmp(cp, "remsh.exe"))
300#else
Damien Millerad833b32000-08-23 10:46:23 +1000301 if (strcmp(cp, "rsh") && strcmp(cp, "ssh") && strcmp(cp, "rlogin") &&
302 strcmp(cp, "slogin") && strcmp(cp, "remsh"))
Damien Millerbac2d8a2000-09-05 16:13:06 +1100303#endif
Damien Miller95def091999-11-25 00:26:21 +1100304 host = cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000305
Damien Miller95def091999-11-25 00:26:21 +1100306 for (optind = 1; optind < ac; optind++) {
307 if (av[optind][0] != '-') {
308 if (host)
309 break;
310 if ((cp = strchr(av[optind], '@'))) {
Damien Miller4af51302000-04-16 11:18:38 +1000311 if(cp == av[optind])
312 usage();
Damien Miller95def091999-11-25 00:26:21 +1100313 options.user = av[optind];
314 *cp = '\0';
315 host = ++cp;
316 } else
317 host = av[optind];
318 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000319 }
Damien Miller95def091999-11-25 00:26:21 +1100320 opt = av[optind][1];
321 if (!opt)
322 usage();
323 if (strchr("eilcpLRo", opt)) { /* options with arguments */
324 optarg = av[optind] + 2;
325 if (strcmp(optarg, "") == 0) {
326 if (optind >= ac - 1)
327 usage();
328 optarg = av[++optind];
329 }
330 } else {
331 if (av[optind][2])
332 usage();
333 optarg = NULL;
334 }
335 switch (opt) {
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000336 case '1':
337 options.protocol = SSH_PROTO_1;
338 break;
Damien Miller4af51302000-04-16 11:18:38 +1000339 case '2':
340 options.protocol = SSH_PROTO_2;
341 break;
Damien Miller34132e52000-01-14 15:45:46 +1100342 case '4':
343 IPv4or6 = AF_INET;
344 break;
Damien Miller34132e52000-01-14 15:45:46 +1100345 case '6':
346 IPv4or6 = AF_INET6;
347 break;
Damien Miller95def091999-11-25 00:26:21 +1100348 case 'n':
349 stdin_null_flag = 1;
350 break;
Damien Miller95def091999-11-25 00:26:21 +1100351 case 'f':
352 fork_after_authentication_flag = 1;
353 stdin_null_flag = 1;
354 break;
Damien Miller95def091999-11-25 00:26:21 +1100355 case 'x':
356 options.forward_x11 = 0;
357 break;
Damien Miller95def091999-11-25 00:26:21 +1100358 case 'X':
359 options.forward_x11 = 1;
360 break;
Damien Miller95def091999-11-25 00:26:21 +1100361 case 'g':
362 options.gateway_ports = 1;
363 break;
Damien Miller95def091999-11-25 00:26:21 +1100364 case 'P':
365 options.use_privileged_port = 0;
366 break;
Damien Miller95def091999-11-25 00:26:21 +1100367 case 'a':
368 options.forward_agent = 0;
369 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000370 case 'A':
371 options.forward_agent = 1;
372 break;
Damien Miller95def091999-11-25 00:26:21 +1100373#ifdef AFS
374 case 'k':
375 options.kerberos_tgt_passing = 0;
376 options.afs_token_passing = 0;
377 break;
378#endif
379 case 'i':
380 if (stat(optarg, &st) < 0) {
381 fprintf(stderr, "Warning: Identity file %s does not exist.\n",
Damien Miller0bc1bd82000-11-13 22:57:25 +1100382 optarg);
Damien Miller95def091999-11-25 00:26:21 +1100383 break;
384 }
385 if (options.num_identity_files >= SSH_MAX_IDENTITY_FILES)
386 fatal("Too many identity files specified (max %d)",
Damien Miller0bc1bd82000-11-13 22:57:25 +1100387 SSH_MAX_IDENTITY_FILES);
388 options.identity_files[options.num_identity_files++] = xstrdup(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100389 break;
Damien Miller95def091999-11-25 00:26:21 +1100390 case 't':
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000391 if (tty_flag)
392 force_tty_flag = 1;
Damien Miller95def091999-11-25 00:26:21 +1100393 tty_flag = 1;
394 break;
Damien Miller95def091999-11-25 00:26:21 +1100395 case 'v':
Damien Millere4340be2000-09-16 13:29:08 +1100396 if (0 == debug_flag) {
397 debug_flag = 1;
398 options.log_level = SYSLOG_LEVEL_DEBUG1;
399 } else if (options.log_level < SYSLOG_LEVEL_DEBUG3) {
400 options.log_level++;
401 break;
402 } else {
403 fatal("Too high debugging level.\n");
404 }
405 /* fallthrough */
Damien Miller95def091999-11-25 00:26:21 +1100406 case 'V':
Damien Miller78928792000-04-12 20:17:38 +1000407 fprintf(stderr, "SSH Version %s, protocol versions %d.%d/%d.%d.\n",
408 SSH_VERSION,
409 PROTOCOL_MAJOR_1, PROTOCOL_MINOR_1,
410 PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2);
Damien Miller1383bd82000-04-06 12:32:37 +1000411 fprintf(stderr, "Compiled with SSL (0x%8.8lx).\n", SSLeay());
Damien Miller95def091999-11-25 00:26:21 +1100412 if (opt == 'V')
413 exit(0);
Damien Miller95def091999-11-25 00:26:21 +1100414 break;
Damien Miller95def091999-11-25 00:26:21 +1100415 case 'q':
416 options.log_level = SYSLOG_LEVEL_QUIET;
417 break;
Damien Miller95def091999-11-25 00:26:21 +1100418 case 'e':
419 if (optarg[0] == '^' && optarg[2] == 0 &&
Ben Lindstrom46c16222000-12-22 01:43:59 +0000420 (u_char) optarg[1] >= 64 && (u_char) optarg[1] < 128)
421 options.escape_char = (u_char) optarg[1] & 31;
Damien Miller95def091999-11-25 00:26:21 +1100422 else if (strlen(optarg) == 1)
Ben Lindstrom46c16222000-12-22 01:43:59 +0000423 options.escape_char = (u_char) optarg[0];
Damien Miller95def091999-11-25 00:26:21 +1100424 else if (strcmp(optarg, "none") == 0)
425 options.escape_char = -2;
426 else {
427 fprintf(stderr, "Bad escape character '%s'.\n", optarg);
428 exit(1);
429 }
430 break;
Damien Miller95def091999-11-25 00:26:21 +1100431 case 'c':
Damien Millereba71ba2000-04-29 23:57:08 +1000432 if (ciphers_valid(optarg)) {
433 /* SSH2 only */
434 options.ciphers = xstrdup(optarg);
Damien Miller30c3d422000-05-09 11:02:59 +1000435 options.cipher = SSH_CIPHER_ILLEGAL;
Damien Millereba71ba2000-04-29 23:57:08 +1000436 } else {
437 /* SSH1 only */
Damien Millere39cacc2000-11-29 12:18:44 +1100438 options.cipher = cipher_number(optarg);
439 if (options.cipher == -1) {
Damien Millereba71ba2000-04-29 23:57:08 +1000440 fprintf(stderr, "Unknown cipher type '%s'\n", optarg);
441 exit(1);
442 }
Damien Millere39cacc2000-11-29 12:18:44 +1100443 if (options.cipher == SSH_CIPHER_3DES) {
444 options.ciphers = "3des-cbc";
445 } else if (options.cipher == SSH_CIPHER_BLOWFISH) {
446 options.ciphers = "blowfish-cbc";
447 } else {
448 options.ciphers = (char *)-1;
449 }
Damien Miller95def091999-11-25 00:26:21 +1100450 }
451 break;
Damien Miller95def091999-11-25 00:26:21 +1100452 case 'p':
453 options.port = atoi(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100454 break;
Damien Miller95def091999-11-25 00:26:21 +1100455 case 'l':
456 options.user = optarg;
457 break;
Damien Miller95def091999-11-25 00:26:21 +1100458 case 'R':
Damien Miller34132e52000-01-14 15:45:46 +1100459 if (sscanf(optarg, "%hu/%255[^/]/%hu", &fwd_port, buf,
460 &fwd_host_port) != 3 &&
461 sscanf(optarg, "%hu:%255[^:]:%hu", &fwd_port, buf,
462 &fwd_host_port) != 3) {
Damien Miller95def091999-11-25 00:26:21 +1100463 fprintf(stderr, "Bad forwarding specification '%s'.\n", optarg);
464 usage();
465 /* NOTREACHED */
466 }
467 add_remote_forward(&options, fwd_port, buf, fwd_host_port);
468 break;
Damien Miller95def091999-11-25 00:26:21 +1100469 case 'L':
Damien Miller34132e52000-01-14 15:45:46 +1100470 if (sscanf(optarg, "%hu/%255[^/]/%hu", &fwd_port, buf,
471 &fwd_host_port) != 3 &&
472 sscanf(optarg, "%hu:%255[^:]:%hu", &fwd_port, buf,
473 &fwd_host_port) != 3) {
Damien Miller95def091999-11-25 00:26:21 +1100474 fprintf(stderr, "Bad forwarding specification '%s'.\n", optarg);
475 usage();
476 /* NOTREACHED */
477 }
478 add_local_forward(&options, fwd_port, buf, fwd_host_port);
479 break;
Damien Miller95def091999-11-25 00:26:21 +1100480 case 'C':
481 options.compression = 1;
482 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000483 case 'N':
484 no_shell_flag = 1;
485 no_tty_flag = 1;
486 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000487 case 'T':
488 no_tty_flag = 1;
489 break;
Damien Miller95def091999-11-25 00:26:21 +1100490 case 'o':
491 dummy = 1;
492 if (process_config_line(&options, host ? host : "", optarg,
493 "command-line", 0, &dummy) != 0)
494 exit(1);
495 break;
Damien Miller832562e2001-01-30 09:30:01 +1100496 case 's':
497 subsystem_flag = 1;
498 break;
Damien Miller95def091999-11-25 00:26:21 +1100499 default:
500 usage();
501 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000502 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000503
Damien Miller95def091999-11-25 00:26:21 +1100504 /* Check that we got a host name. */
505 if (!host)
506 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000507
Damien Millercb5e44a2000-09-29 12:12:36 +1100508 SSLeay_add_all_algorithms();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100509 ERR_load_crypto_strings();
Damien Millercb5e44a2000-09-29 12:12:36 +1100510
Damien Miller95def091999-11-25 00:26:21 +1100511 /* Initialize the command to execute on remote host. */
512 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000513
Damien Miller5428f641999-11-25 11:54:57 +1100514 /*
515 * Save the command to execute on the remote host in a buffer. There
516 * is no limit on the length of the command, except by the maximum
517 * packet size. Also sets the tty flag if there is no command.
518 */
Damien Miller95def091999-11-25 00:26:21 +1100519 if (optind == ac) {
520 /* No command specified - execute shell on a tty. */
521 tty_flag = 1;
Damien Miller832562e2001-01-30 09:30:01 +1100522 if (subsystem_flag) {
523 fprintf(stderr, "You must specify a subsystem to invoke.");
524 usage();
525 }
Damien Miller95def091999-11-25 00:26:21 +1100526 } else {
527 /* A command has been specified. Store it into the
528 buffer. */
529 for (i = optind; i < ac; i++) {
530 if (i > optind)
531 buffer_append(&command, " ", 1);
532 buffer_append(&command, av[i], strlen(av[i]));
533 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000534 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000535
Damien Miller95def091999-11-25 00:26:21 +1100536 /* Cannot fork to background if no command. */
Damien Millercaf6dd62000-08-29 11:33:50 +1100537 if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
Damien Miller95def091999-11-25 00:26:21 +1100538 fatal("Cannot fork into background without a command to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000539
Damien Miller95def091999-11-25 00:26:21 +1100540 /* Allocate a tty by default if no command specified. */
541 if (buffer_len(&command) == 0)
542 tty_flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000543
Ben Lindstromc72745a2000-12-02 19:03:54 +0000544 /* Force no tty*/
545 if (no_tty_flag)
546 tty_flag = 0;
Damien Miller95def091999-11-25 00:26:21 +1100547 /* Do not allocate a tty if stdin is not a tty. */
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000548 if (!isatty(fileno(stdin)) && !force_tty_flag) {
Damien Miller95def091999-11-25 00:26:21 +1100549 if (tty_flag)
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000550 log("Pseudo-terminal will not be allocated because stdin is not a terminal.\n");
Damien Miller95def091999-11-25 00:26:21 +1100551 tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000552 }
Damien Miller1383bd82000-04-06 12:32:37 +1000553
Damien Miller95def091999-11-25 00:26:21 +1100554 /* Get user data. */
555 pw = getpwuid(original_real_uid);
556 if (!pw) {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000557 log("You don't exist, go away!\n");
Damien Miller95def091999-11-25 00:26:21 +1100558 exit(1);
559 }
560 /* Take a copy of the returned structure. */
561 memset(&pwcopy, 0, sizeof(pwcopy));
562 pwcopy.pw_name = xstrdup(pw->pw_name);
563 pwcopy.pw_passwd = xstrdup(pw->pw_passwd);
564 pwcopy.pw_uid = pw->pw_uid;
565 pwcopy.pw_gid = pw->pw_gid;
Damien Millerad833b32000-08-23 10:46:23 +1000566#ifdef HAVE_PW_CLASS_IN_PASSWD
567 pwcopy.pw_class = xstrdup(pw->pw_class);
568#endif
Damien Miller95def091999-11-25 00:26:21 +1100569 pwcopy.pw_dir = xstrdup(pw->pw_dir);
570 pwcopy.pw_shell = xstrdup(pw->pw_shell);
571 pw = &pwcopy;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000572
Damien Miller95def091999-11-25 00:26:21 +1100573 /* Initialize "log" output. Since we are the client all output
574 actually goes to the terminal. */
575 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000576
Damien Miller95def091999-11-25 00:26:21 +1100577 /* Read per-user configuration file. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000578 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, _PATH_SSH_USER_CONFFILE);
Damien Miller95def091999-11-25 00:26:21 +1100579 read_config_file(buf, host, &options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000580
Damien Miller95def091999-11-25 00:26:21 +1100581 /* Read systemwide configuration file. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000582 read_config_file(_PATH_HOST_CONFIG_FILE, host, &options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000583
Damien Miller95def091999-11-25 00:26:21 +1100584 /* Fill configuration defaults. */
585 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000586
Damien Miller95def091999-11-25 00:26:21 +1100587 /* reinit */
588 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000589
Damien Miller95def091999-11-25 00:26:21 +1100590 if (options.user == NULL)
591 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000592
Damien Miller95def091999-11-25 00:26:21 +1100593 if (options.hostname != NULL)
594 host = options.hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000595
Damien Miller95def091999-11-25 00:26:21 +1100596 /* Disable rhosts authentication if not running as root. */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100597#ifdef HAVE_CYGWIN
598 /* Ignore uid if running under Windows */
599 if (!options.use_privileged_port) {
600#else
Damien Miller95def091999-11-25 00:26:21 +1100601 if (original_effective_uid != 0 || !options.use_privileged_port) {
Damien Millerbac2d8a2000-09-05 16:13:06 +1100602#endif
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000603 debug("Rhosts Authentication disabled, "
604 "originating port will not be trusted.");
Damien Miller95def091999-11-25 00:26:21 +1100605 options.rhosts_authentication = 0;
Damien Miller95def091999-11-25 00:26:21 +1100606 }
Damien Miller5428f641999-11-25 11:54:57 +1100607 /*
608 * If using rsh has been selected, exec it now (without trying
609 * anything else). Note that we must release privileges first.
610 */
Damien Miller95def091999-11-25 00:26:21 +1100611 if (options.use_rsh) {
Damien Miller5428f641999-11-25 11:54:57 +1100612 /*
613 * Restore our superuser privileges. This must be done
614 * before permanently setting the uid.
615 */
Damien Miller95def091999-11-25 00:26:21 +1100616 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000617
Damien Miller95def091999-11-25 00:26:21 +1100618 /* Switch to the original uid permanently. */
619 permanently_set_uid(original_real_uid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000620
Damien Miller95def091999-11-25 00:26:21 +1100621 /* Execute rsh. */
622 rsh_connect(host, options.user, &command);
623 fatal("rsh_connect returned");
624 }
625 /* Restore our superuser privileges. */
626 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000627
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000628 /* Open a connection to the remote host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000629
Damien Miller95def091999-11-25 00:26:21 +1100630 ok = ssh_connect(host, &hostaddr, options.port,
Kevin Stevesfcec7f82000-12-15 19:55:48 +0000631 options.connection_attempts,
632 original_effective_uid != 0 || !options.use_privileged_port,
633 original_real_uid,
634 options.proxy_command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000635
Damien Miller5428f641999-11-25 11:54:57 +1100636 /*
637 * If we successfully made the connection, load the host private key
638 * in case we will need it later for combined rsa-rhosts
639 * authentication. This must be done before releasing extra
640 * privileges, because the file is only readable by root.
641 */
Damien Millereba71ba2000-04-29 23:57:08 +1000642 if (ok && (options.protocol & SSH_PROTO_1)) {
643 Key k;
Damien Miller95def091999-11-25 00:26:21 +1100644 host_private_key = RSA_new();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100645 k.type = KEY_RSA1;
Damien Millereba71ba2000-04-29 23:57:08 +1000646 k.rsa = host_private_key;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000647 if (load_private_key(_PATH_HOST_KEY_FILE, "", &k, NULL))
Damien Miller95def091999-11-25 00:26:21 +1100648 host_private_key_loaded = 1;
649 }
Damien Miller5428f641999-11-25 11:54:57 +1100650 /*
651 * Get rid of any extra privileges that we may have. We will no
652 * longer need them. Also, extra privileges could make it very hard
653 * to read identity files and other non-world-readable files from the
654 * user's home directory if it happens to be on a NFS volume where
655 * root is mapped to nobody.
656 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000657
Damien Miller5428f641999-11-25 11:54:57 +1100658 /*
659 * Note that some legacy systems need to postpone the following call
660 * to permanently_set_uid() until the private hostkey is destroyed
661 * with RSA_free(). Otherwise the calling user could ptrace() the
662 * process, read the private hostkey and impersonate the host.
663 * OpenBSD does not allow ptracing of setuid processes.
664 */
Damien Miller95def091999-11-25 00:26:21 +1100665 permanently_set_uid(original_real_uid);
666
Damien Miller5428f641999-11-25 11:54:57 +1100667 /*
668 * Now that we are back to our own permissions, create ~/.ssh
669 * directory if it doesn\'t already exist.
670 */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000671 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, _PATH_SSH_USER_DIR);
Damien Miller95def091999-11-25 00:26:21 +1100672 if (stat(buf, &st) < 0)
Damien Millerbe484b52000-07-15 14:14:16 +1000673 if (mkdir(buf, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100674 error("Could not create directory '%.200s'.", buf);
675
676 /* Check if the connection failed, and try "rsh" if appropriate. */
677 if (!ok) {
678 if (options.port != 0)
Damien Milleraae6c611999-12-06 11:47:28 +1100679 log("Secure connection to %.100s on port %hu refused%.100s.",
Damien Miller95def091999-11-25 00:26:21 +1100680 host, options.port,
681 options.fallback_to_rsh ? "; reverting to insecure method" : "");
682 else
683 log("Secure connection to %.100s refused%.100s.", host,
684 options.fallback_to_rsh ? "; reverting to insecure method" : "");
685
686 if (options.fallback_to_rsh) {
687 rsh_connect(host, options.user, &command);
688 fatal("rsh_connect returned");
689 }
690 exit(1);
691 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100692 /* Expand ~ in options.identity_files, known host file names. */
Damien Millereba71ba2000-04-29 23:57:08 +1000693 /* XXX mem-leaks */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100694 for (i = 0; i < options.num_identity_files; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100695 options.identity_files[i] =
Damien Miller0bc1bd82000-11-13 22:57:25 +1100696 tilde_expand_filename(options.identity_files[i], original_real_uid);
697 options.identity_files_type[i] = guess_identity_file_type(options.identity_files[i]);
698 debug("identity file %s type %d", options.identity_files[i],
699 options.identity_files_type[i]);
700 }
701 options.system_hostfile =
702 tilde_expand_filename(options.system_hostfile, original_real_uid);
703 options.user_hostfile =
704 tilde_expand_filename(options.user_hostfile, original_real_uid);
705 options.system_hostfile2 =
706 tilde_expand_filename(options.system_hostfile2, original_real_uid);
707 options.user_hostfile2 =
708 tilde_expand_filename(options.user_hostfile2, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100709
710 /* Log into the remote system. This never returns if the login fails. */
711 ssh_login(host_private_key_loaded, host_private_key,
Damien Miller34132e52000-01-14 15:45:46 +1100712 host, (struct sockaddr *)&hostaddr, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100713
714 /* We no longer need the host private key. Clear it now. */
715 if (host_private_key_loaded)
716 RSA_free(host_private_key); /* Destroys contents safely */
717
Damien Miller1383bd82000-04-06 12:32:37 +1000718 exit_status = compat20 ? ssh_session2() : ssh_session();
719 packet_close();
720 return exit_status;
721}
722
Damien Millerbd483e72000-04-30 10:00:53 +1000723void
724x11_get_proto(char *proto, int proto_len, char *data, int data_len)
725{
726 char line[512];
727 FILE *f;
728 int got_data = 0, i;
729
Damien Millerd3a18572000-06-07 19:55:44 +1000730 if (options.xauth_location) {
731 /* Try to get Xauthority information for the display. */
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000732 snprintf(line, sizeof line, "%.100s list %.200s 2>" _PATH_DEVNULL,
Damien Millerd3a18572000-06-07 19:55:44 +1000733 options.xauth_location, getenv("DISPLAY"));
734 f = popen(line, "r");
735 if (f && fgets(line, sizeof(line), f) &&
736 sscanf(line, "%*s %s %s", proto, data) == 2)
737 got_data = 1;
738 if (f)
739 pclose(f);
740 }
Damien Millerbd483e72000-04-30 10:00:53 +1000741 /*
742 * If we didn't get authentication data, just make up some
743 * data. The forwarding code will check the validity of the
744 * response anyway, and substitute this data. The X11
745 * server, however, will ignore this fake data and use
746 * whatever authentication mechanisms it was using otherwise
747 * for the local connection.
748 */
749 if (!got_data) {
750 u_int32_t rand = 0;
751
752 strlcpy(proto, "MIT-MAGIC-COOKIE-1", proto_len);
753 for (i = 0; i < 16; i++) {
754 if (i % 4 == 0)
755 rand = arc4random();
756 snprintf(data + 2 * i, data_len - 2 * i, "%02x", rand & 0xff);
757 rand >>= 8;
758 }
759 }
760}
761
Damien Miller0bc1bd82000-11-13 22:57:25 +1100762void
763ssh_init_forwarding(void)
764{
Kevin Steves12057502001-02-05 14:54:34 +0000765 int success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100766 int i;
Kevin Steves12057502001-02-05 14:54:34 +0000767
Damien Miller0bc1bd82000-11-13 22:57:25 +1100768 /* Initiate local TCP/IP port forwardings. */
769 for (i = 0; i < options.num_local_forwards; i++) {
770 debug("Connections to local port %d forwarded to remote address %.200s:%d",
771 options.local_forwards[i].port,
772 options.local_forwards[i].host,
773 options.local_forwards[i].host_port);
Kevin Steves12057502001-02-05 14:54:34 +0000774 success += channel_request_local_forwarding(
Damien Miller0bc1bd82000-11-13 22:57:25 +1100775 options.local_forwards[i].port,
776 options.local_forwards[i].host,
777 options.local_forwards[i].host_port,
778 options.gateway_ports);
779 }
Kevin Steves12057502001-02-05 14:54:34 +0000780 if (i > 0 && success == 0)
781 error("Could not request local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100782
783 /* Initiate remote TCP/IP port forwardings. */
784 for (i = 0; i < options.num_remote_forwards; i++) {
785 debug("Connections to remote port %d forwarded to local address %.200s:%d",
786 options.remote_forwards[i].port,
787 options.remote_forwards[i].host,
788 options.remote_forwards[i].host_port);
789 channel_request_remote_forwarding(
790 options.remote_forwards[i].port,
791 options.remote_forwards[i].host,
792 options.remote_forwards[i].host_port);
793 }
794}
795
796void
797check_agent_present(void)
798{
799 if (options.forward_agent) {
800 /* Clear agent forwarding if we don\'t have an agent. */
801 int authfd = ssh_get_authentication_socket();
802 if (authfd < 0)
803 options.forward_agent = 0;
804 else
805 ssh_close_authentication_socket(authfd);
806 }
807}
808
Damien Miller1383bd82000-04-06 12:32:37 +1000809int
810ssh_session(void)
811{
812 int type;
Damien Miller1383bd82000-04-06 12:32:37 +1000813 int plen;
814 int interactive = 0;
815 int have_tty = 0;
816 struct winsize ws;
Damien Miller1383bd82000-04-06 12:32:37 +1000817 char *cp;
818
Damien Miller95def091999-11-25 00:26:21 +1100819 /* Enable compression if requested. */
820 if (options.compression) {
821 debug("Requesting compression at level %d.", options.compression_level);
822
823 if (options.compression_level < 1 || options.compression_level > 9)
824 fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
825
826 /* Send the request. */
827 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
828 packet_put_int(options.compression_level);
829 packet_send();
830 packet_write_wait();
831 type = packet_read(&plen);
832 if (type == SSH_SMSG_SUCCESS)
833 packet_start_compression(options.compression_level);
834 else if (type == SSH_SMSG_FAILURE)
835 log("Warning: Remote host refused compression.");
836 else
837 packet_disconnect("Protocol error waiting for compression response.");
838 }
839 /* Allocate a pseudo tty if appropriate. */
840 if (tty_flag) {
841 debug("Requesting pty.");
842
843 /* Start the packet. */
844 packet_start(SSH_CMSG_REQUEST_PTY);
845
846 /* Store TERM in the packet. There is no limit on the
847 length of the string. */
848 cp = getenv("TERM");
849 if (!cp)
850 cp = "";
851 packet_put_string(cp, strlen(cp));
852
853 /* Store window size in the packet. */
854 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
855 memset(&ws, 0, sizeof(ws));
856 packet_put_int(ws.ws_row);
857 packet_put_int(ws.ws_col);
858 packet_put_int(ws.ws_xpixel);
859 packet_put_int(ws.ws_ypixel);
860
861 /* Store tty modes in the packet. */
862 tty_make_modes(fileno(stdin));
863
864 /* Send the packet, and wait for it to leave. */
865 packet_send();
866 packet_write_wait();
867
868 /* Read response from the server. */
869 type = packet_read(&plen);
Damien Miller450a7a12000-03-26 13:04:51 +1000870 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100871 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +1000872 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000873 } else if (type == SSH_SMSG_FAILURE)
Damien Miller95def091999-11-25 00:26:21 +1100874 log("Warning: Remote host failed or refused to allocate a pseudo tty.");
875 else
876 packet_disconnect("Protocol error waiting for pty request response.");
877 }
878 /* Request X11 forwarding if enabled and DISPLAY is set. */
879 if (options.forward_x11 && getenv("DISPLAY") != NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +1000880 char proto[512], data[512];
881 /* Get reasonable local authentication information. */
882 x11_get_proto(proto, sizeof proto, data, sizeof data);
883 /* Request forwarding with authentication spoofing. */
Damien Miller95def091999-11-25 00:26:21 +1100884 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Millerbd483e72000-04-30 10:00:53 +1000885 x11_request_forwarding_with_spoofing(0, proto, data);
Damien Miller95def091999-11-25 00:26:21 +1100886
887 /* Read response from the server. */
888 type = packet_read(&plen);
889 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100890 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +1000891 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller95def091999-11-25 00:26:21 +1100892 log("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +1000893 } else {
Damien Miller95def091999-11-25 00:26:21 +1100894 packet_disconnect("Protocol error waiting for X11 forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +1000895 }
Damien Miller95def091999-11-25 00:26:21 +1100896 }
897 /* Tell the packet module whether this is an interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000898 packet_set_interactive(interactive);
Damien Miller95def091999-11-25 00:26:21 +1100899
900 /* Request authentication agent forwarding if appropriate. */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100901 check_agent_present();
902
Damien Miller95def091999-11-25 00:26:21 +1100903 if (options.forward_agent) {
904 debug("Requesting authentication agent forwarding.");
905 auth_request_forwarding();
906
907 /* Read response from the server. */
908 type = packet_read(&plen);
909 packet_integrity_check(plen, 0, type);
910 if (type != SSH_SMSG_SUCCESS)
911 log("Warning: Remote host denied authentication agent forwarding.");
912 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000913
Damien Miller0bc1bd82000-11-13 22:57:25 +1100914 /* Initiate port forwardings. */
915 ssh_init_forwarding();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000916
Damien Miller5428f641999-11-25 11:54:57 +1100917 /* If requested, let ssh continue in the background. */
Damien Miller4af51302000-04-16 11:18:38 +1000918 if (fork_after_authentication_flag)
Damien Miller5428f641999-11-25 11:54:57 +1100919 if (daemon(1, 1) < 0)
920 fatal("daemon() failed: %.200s", strerror(errno));
921
922 /*
923 * If a command was specified on the command line, execute the
924 * command now. Otherwise request the server to start a shell.
925 */
Damien Miller95def091999-11-25 00:26:21 +1100926 if (buffer_len(&command) > 0) {
927 int len = buffer_len(&command);
928 if (len > 900)
929 len = 900;
930 debug("Sending command: %.*s", len, buffer_ptr(&command));
931 packet_start(SSH_CMSG_EXEC_CMD);
932 packet_put_string(buffer_ptr(&command), buffer_len(&command));
933 packet_send();
934 packet_write_wait();
935 } else {
936 debug("Requesting shell.");
937 packet_start(SSH_CMSG_EXEC_SHELL);
938 packet_send();
939 packet_write_wait();
940 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000941
Damien Miller95def091999-11-25 00:26:21 +1100942 /* Enter the interactive session. */
Damien Millerad833b32000-08-23 10:46:23 +1000943 return client_loop(have_tty, tty_flag ? options.escape_char : -1, 0);
Damien Miller1383bd82000-04-06 12:32:37 +1000944}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000945
Damien Miller1383bd82000-04-06 12:32:37 +1000946void
Ben Lindstrom1e7d3062001-02-09 02:36:43 +0000947client_subsystem_reply(int type, int plen, void *ctxt)
948{
949 int id, len;
950
951 id = packet_get_int();
952 len = buffer_len(&command);
953 len = MAX(len, 900);
954 packet_done();
955 if (type == SSH2_MSG_CHANNEL_FAILURE)
956 fatal("Request for subsystem '%.*s' failed on channel %d",
957 len, buffer_ptr(&command), id);
958}
959
960void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100961ssh_session2_callback(int id, void *arg)
Damien Miller1383bd82000-04-06 12:32:37 +1000962{
963 int len;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000964 int interactive = 0;
965
Kevin Stevesadf74cd2001-02-05 14:22:50 +0000966 debug("client_init id %d arg %ld", id, (long)arg);
Damien Miller1383bd82000-04-06 12:32:37 +1000967
968 if (no_shell_flag)
969 goto done;
970
971 if (tty_flag) {
972 struct winsize ws;
973 char *cp;
974 cp = getenv("TERM");
975 if (!cp)
976 cp = "";
977 /* Store window size in the packet. */
978 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
979 memset(&ws, 0, sizeof(ws));
980
981 channel_request_start(id, "pty-req", 0);
982 packet_put_cstring(cp);
983 packet_put_int(ws.ws_col);
984 packet_put_int(ws.ws_row);
985 packet_put_int(ws.ws_xpixel);
986 packet_put_int(ws.ws_ypixel);
987 packet_put_cstring(""); /* XXX: encode terminal modes */
988 packet_send();
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000989 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +1000990 /* XXX wait for reply */
991 }
Damien Millerbd483e72000-04-30 10:00:53 +1000992 if (options.forward_x11 &&
993 getenv("DISPLAY") != NULL) {
994 char proto[512], data[512];
995 /* Get reasonable local authentication information. */
996 x11_get_proto(proto, sizeof proto, data, sizeof data);
997 /* Request forwarding with authentication spoofing. */
998 debug("Requesting X11 forwarding with authentication spoofing.");
999 x11_request_forwarding_with_spoofing(id, proto, data);
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001000 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001001 /* XXX wait for reply */
1002 }
1003
Damien Miller0bc1bd82000-11-13 22:57:25 +11001004 check_agent_present();
1005 if (options.forward_agent) {
1006 debug("Requesting authentication agent forwarding.");
1007 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1008 packet_send();
1009 }
1010
Damien Miller1383bd82000-04-06 12:32:37 +10001011 len = buffer_len(&command);
1012 if (len > 0) {
1013 if (len > 900)
1014 len = 900;
Damien Miller832562e2001-01-30 09:30:01 +11001015 if (subsystem_flag) {
1016 debug("Sending subsystem: %.*s", len, buffer_ptr(&command));
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001017 channel_request_start(id, "subsystem", /*want reply*/ 1);
1018 /* register callback for reply */
1019 /* XXX we asume that client_loop has already been called */
1020 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &client_subsystem_reply);
1021 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &client_subsystem_reply);
Damien Miller832562e2001-01-30 09:30:01 +11001022 } else {
1023 debug("Sending command: %.*s", len, buffer_ptr(&command));
1024 channel_request_start(id, "exec", 0);
1025 }
Damien Miller1383bd82000-04-06 12:32:37 +10001026 packet_put_string(buffer_ptr(&command), len);
1027 packet_send();
1028 } else {
1029 channel_request(id, "shell", 0);
1030 }
1031 /* channel_callback(id, SSH2_MSG_OPEN_CONFIGMATION, client_init, 0); */
Ben Lindstrom1e7d3062001-02-09 02:36:43 +00001032
Damien Miller1383bd82000-04-06 12:32:37 +10001033done:
1034 /* register different callback, etc. XXX */
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001035 packet_set_interactive(interactive);
Damien Miller1383bd82000-04-06 12:32:37 +10001036}
1037
1038int
1039ssh_session2(void)
1040{
1041 int window, packetmax, id;
Damien Millerad833b32000-08-23 10:46:23 +10001042 int in, out, err;
1043
Damien Millercaf6dd62000-08-29 11:33:50 +11001044 if (stdin_null_flag) {
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001045 in = open(_PATH_DEVNULL, O_RDONLY);
Damien Millercaf6dd62000-08-29 11:33:50 +11001046 } else {
1047 in = dup(STDIN_FILENO);
1048 }
1049 out = dup(STDOUT_FILENO);
1050 err = dup(STDERR_FILENO);
1051
1052 if (in < 0 || out < 0 || err < 0)
1053 fatal("dup() in/out/err failed");
1054
Damien Miller69b69aa2000-10-28 14:19:58 +11001055 /* enable nonblocking unless tty */
1056 if (!isatty(in))
1057 set_nonblock(in);
1058 if (!isatty(out))
1059 set_nonblock(out);
1060 if (!isatty(err))
1061 set_nonblock(err);
1062
Damien Miller0bc1bd82000-11-13 22:57:25 +11001063 /* XXX should be pre-session */
1064 ssh_init_forwarding();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001065
Damien Millerad833b32000-08-23 10:46:23 +10001066 /* If requested, let ssh continue in the background. */
1067 if (fork_after_authentication_flag)
1068 if (daemon(1, 1) < 0)
1069 fatal("daemon() failed: %.200s", strerror(errno));
1070
Damien Millere4340be2000-09-16 13:29:08 +11001071 window = CHAN_SES_WINDOW_DEFAULT;
1072 packetmax = CHAN_SES_PACKET_DEFAULT;
1073 if (!tty_flag) {
Damien Miller1383bd82000-04-06 12:32:37 +10001074 window *= 2;
Damien Millere4340be2000-09-16 13:29:08 +11001075 packetmax *=2;
Damien Miller1383bd82000-04-06 12:32:37 +10001076 }
Damien Miller1383bd82000-04-06 12:32:37 +10001077 id = channel_new(
1078 "session", SSH_CHANNEL_OPENING, in, out, err,
Damien Millere4340be2000-09-16 13:29:08 +11001079 window, packetmax, CHAN_EXTENDED_WRITE,
Damien Miller69b69aa2000-10-28 14:19:58 +11001080 xstrdup("client-session"), /*nonblock*/0);
Damien Miller1383bd82000-04-06 12:32:37 +10001081
Damien Miller1383bd82000-04-06 12:32:37 +10001082 channel_open(id);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001083 channel_register_callback(id, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION,
1084 ssh_session2_callback, (void *)0);
Damien Miller1383bd82000-04-06 12:32:37 +10001085
Damien Millerad833b32000-08-23 10:46:23 +10001086 return client_loop(tty_flag, tty_flag ? options.escape_char : -1, id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001087}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001088
1089int
1090guess_identity_file_type(const char *filename)
1091{
1092 struct stat st;
1093 Key *public;
1094 int type = KEY_RSA1; /* default */
1095
1096 if (stat(filename, &st) < 0) {
Ben Lindstrom14920292000-11-21 21:24:55 +00001097 /* ignore this key */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001098 return KEY_UNSPEC;
1099 }
1100 public = key_new(type);
1101 if (!load_public_key(filename, public, NULL)) {
1102 /* ok, so we will assume this is 'some' key */
1103 type = KEY_UNSPEC;
1104 }
1105 key_free(public);
1106 return type;
1107}