blob: 70f9d6a68903747632640721a29757840432f40b [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
5 * Created: Sat Mar 18 16:36:11 1995 ylo
6 * Ssh client program. This program can be used to log into a remote machine.
7 * The software supports strong authentication, encryption, and forwarding
8 * of X11, TCP/IP, and authentication connections.
9 *
10 * Modified to work with SSL by Niels Provos <provos@citi.umich.edu> in Canada.
11 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100012
13#include "includes.h"
Damien Millerb38eff82000-04-01 11:09:21 +100014RCSID("$Id: ssh.c,v 1.23 2000/04/01 01:09:26 damien Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
16#include "xmalloc.h"
17#include "ssh.h"
18#include "packet.h"
19#include "buffer.h"
20#include "authfd.h"
21#include "readconf.h"
22#include "uidswap.h"
Damien Millerb38eff82000-04-01 11:09:21 +100023#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100024
Damien Miller3f905871999-11-15 17:10:57 +110025#ifdef HAVE___PROGNAME
26extern char *__progname;
27#else /* HAVE___PROGNAME */
28const char *__progname = "ssh";
29#endif /* HAVE___PROGNAME */
30
Damien Miller34132e52000-01-14 15:45:46 +110031/* Flag indicating whether IPv4 or IPv6. This can be set on the command line.
32 Default value is AF_UNSPEC means both IPv4 and IPv6. */
Damien Miller7d80e342000-01-19 14:36:49 +110033#ifdef IPV4_DEFAULT
34int IPv4or6 = AF_INET;
35#else
Damien Miller34132e52000-01-14 15:45:46 +110036int IPv4or6 = AF_UNSPEC;
Damien Miller7d80e342000-01-19 14:36:49 +110037#endif
Damien Miller34132e52000-01-14 15:45:46 +110038
Damien Miller95def091999-11-25 00:26:21 +110039/* Flag indicating whether debug mode is on. This can be set on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040int debug_flag = 0;
41
Damien Millerd4a8b7e1999-10-27 13:42:43 +100042int tty_flag = 0;
43
Damien Miller5428f641999-11-25 11:54:57 +110044/*
45 * Flag indicating that nothing should be read from stdin. This can be set
46 * on the command line.
47 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048int stdin_null_flag = 0;
49
Damien Miller5428f641999-11-25 11:54:57 +110050/*
51 * Flag indicating that ssh should fork after authentication. This is useful
52 * so that the pasphrase can be entered manually, and then ssh goes to the
53 * background.
54 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100055int fork_after_authentication_flag = 0;
56
Damien Miller5428f641999-11-25 11:54:57 +110057/*
58 * General data structure for command line options and options configurable
59 * in configuration files. See readconf.h.
60 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061Options options;
62
Damien Miller5428f641999-11-25 11:54:57 +110063/*
64 * Name of the host we are connecting to. This is the name given on the
65 * command line, or the HostName specified for the user-supplied name in a
66 * configuration file.
67 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068char *host;
69
70/* socket address the host resolves to */
Damien Miller34132e52000-01-14 15:45:46 +110071struct sockaddr_storage hostaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072
Damien Miller5428f641999-11-25 11:54:57 +110073/*
74 * Flag to indicate that we have received a window change signal which has
75 * not yet been processed. This will cause a message indicating the new
76 * window size to be sent to the server a little later. This is volatile
77 * because this is updated in a signal handler.
78 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079volatile int received_window_change_signal = 0;
80
81/* Value of argv[0] (set in the main program). */
82char *av0;
83
84/* Flag indicating whether we have a valid host private key loaded. */
85int host_private_key_loaded = 0;
86
87/* Host private key. */
88RSA *host_private_key = NULL;
89
90/* Original real UID. */
91uid_t original_real_uid;
92
93/* Prints a help message to the user. This function never returns. */
94
95void
96usage()
97{
Damien Miller95def091999-11-25 00:26:21 +110098 fprintf(stderr, "Usage: %s [options] host [command]\n", av0);
99 fprintf(stderr, "Options:\n");
100 fprintf(stderr, " -l user Log in using this user name.\n");
101 fprintf(stderr, " -n Redirect input from /dev/null.\n");
102 fprintf(stderr, " -a Disable authentication agent forwarding.\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103#ifdef AFS
Damien Miller95def091999-11-25 00:26:21 +1100104 fprintf(stderr, " -k Disable Kerberos ticket and AFS token forwarding.\n");
105#endif /* AFS */
106 fprintf(stderr, " -x Disable X11 connection forwarding.\n");
Damien Miller98c7ad62000-03-09 21:27:49 +1100107 fprintf(stderr, " -X Enable X11 connection forwarding.\n");
Damien Miller95def091999-11-25 00:26:21 +1100108 fprintf(stderr, " -i file Identity for RSA authentication (default: ~/.ssh/identity).\n");
109 fprintf(stderr, " -t Tty; allocate a tty even if command is given.\n");
110 fprintf(stderr, " -v Verbose; display verbose debugging messages.\n");
111 fprintf(stderr, " -V Display version number only.\n");
112 fprintf(stderr, " -P Don't allocate a privileged port.\n");
113 fprintf(stderr, " -q Quiet; don't display any warning messages.\n");
114 fprintf(stderr, " -f Fork into background after authentication.\n");
115 fprintf(stderr, " -e char Set escape character; ``none'' = disable (default: ~).\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116
Damien Miller95def091999-11-25 00:26:21 +1100117 fprintf(stderr, " -c cipher Select encryption algorithm: "
118 "``3des'', "
119 "``blowfish''\n");
120 fprintf(stderr, " -p port Connect to this port. Server must be on the same port.\n");
121 fprintf(stderr, " -L listen-port:host:port Forward local port to remote address\n");
122 fprintf(stderr, " -R listen-port:host:port Forward remote port to local address\n");
123 fprintf(stderr, " These cause %s to listen for connections on a port, and\n", av0);
124 fprintf(stderr, " forward them to the other side by connecting to host:port.\n");
125 fprintf(stderr, " -C Enable compression.\n");
126 fprintf(stderr, " -g Allow remote hosts to connect to forwarded ports.\n");
Damien Miller34132e52000-01-14 15:45:46 +1100127 fprintf(stderr, " -4 Use IPv4 only.\n");
128 fprintf(stderr, " -6 Use IPv6 only.\n");
Damien Miller95def091999-11-25 00:26:21 +1100129 fprintf(stderr, " -o 'option' Process the option as if it was read from a configuration file.\n");
130 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131}
132
Damien Miller95def091999-11-25 00:26:21 +1100133/*
134 * Connects to the given host using rsh (or prints an error message and exits
135 * if rsh is not available). This function never returns.
136 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137void
Damien Miller95def091999-11-25 00:26:21 +1100138rsh_connect(char *host, char *user, Buffer * command)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139{
Damien Miller95def091999-11-25 00:26:21 +1100140 char *args[10];
141 int i;
142
143 log("Using rsh. WARNING: Connection will not be encrypted.");
144 /* Build argument list for rsh. */
145 i = 0;
146 args[i++] = _PATH_RSH;
147 /* host may have to come after user on some systems */
148 args[i++] = host;
149 if (user) {
150 args[i++] = "-l";
151 args[i++] = user;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152 }
Damien Miller95def091999-11-25 00:26:21 +1100153 if (buffer_len(command) > 0) {
154 buffer_append(command, "\0", 1);
155 args[i++] = buffer_ptr(command);
156 }
157 args[i++] = NULL;
158 if (debug_flag) {
159 for (i = 0; args[i]; i++) {
160 if (i != 0)
161 fprintf(stderr, " ");
162 fprintf(stderr, "%s", args[i]);
163 }
164 fprintf(stderr, "\n");
165 }
166 execv(_PATH_RSH, args);
167 perror(_PATH_RSH);
168 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000169}
170
Damien Miller95def091999-11-25 00:26:21 +1100171/*
172 * Main program for the ssh client.
173 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000174int
175main(int ac, char **av)
176{
Damien Milleraae6c611999-12-06 11:47:28 +1100177 int i, opt, optind, type, exit_status, ok, authfd;
178 u_short fwd_port, fwd_host_port;
Damien Miller95def091999-11-25 00:26:21 +1100179 char *optarg, *cp, buf[256];
180 Buffer command;
181 struct winsize ws;
182 struct stat st;
183 struct passwd *pw, pwcopy;
184 int interactive = 0, dummy;
Damien Miller450a7a12000-03-26 13:04:51 +1000185 int have_pty = 0;
Damien Miller95def091999-11-25 00:26:21 +1100186 uid_t original_effective_uid;
187 int plen;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000188
Damien Miller5428f641999-11-25 11:54:57 +1100189 /*
190 * Save the original real uid. It will be needed later (uid-swapping
191 * may clobber the real uid).
192 */
Damien Miller95def091999-11-25 00:26:21 +1100193 original_real_uid = getuid();
194 original_effective_uid = geteuid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195
Damien Miller95def091999-11-25 00:26:21 +1100196 /* If we are installed setuid root be careful to not drop core. */
197 if (original_real_uid != original_effective_uid) {
198 struct rlimit rlim;
199 rlim.rlim_cur = rlim.rlim_max = 0;
200 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
201 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202 }
Damien Miller5428f641999-11-25 11:54:57 +1100203 /*
204 * Use uid-swapping to give up root privileges for the duration of
205 * option processing. We will re-instantiate the rights when we are
206 * ready to create the privileged port, and will permanently drop
207 * them when the port has been created (actually, when the connection
208 * has been made, as we may need to create the port several times).
209 */
Damien Miller95def091999-11-25 00:26:21 +1100210 temporarily_use_uid(original_real_uid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000211
Damien Miller5428f641999-11-25 11:54:57 +1100212 /*
213 * Set our umask to something reasonable, as some files are created
214 * with the default umask. This will make them world-readable but
215 * writable only by the owner, which is ok for all files for which we
216 * don't set the modes explicitly.
217 */
Damien Miller95def091999-11-25 00:26:21 +1100218 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000219
Damien Miller95def091999-11-25 00:26:21 +1100220 /* Save our own name. */
221 av0 = av[0];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000222
Damien Miller95def091999-11-25 00:26:21 +1100223 /* Initialize option structure to indicate that no values have been set. */
224 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000225
Damien Miller95def091999-11-25 00:26:21 +1100226 /* Parse command-line arguments. */
227 host = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000228
Damien Miller95def091999-11-25 00:26:21 +1100229 /* If program name is not one of the standard names, use it as host name. */
230 if (strchr(av0, '/'))
231 cp = strrchr(av0, '/') + 1;
232 else
233 cp = av0;
234 if (strcmp(cp, "rsh") != 0 && strcmp(cp, "ssh") != 0 &&
235 strcmp(cp, "rlogin") != 0 && strcmp(cp, "slogin") != 0)
236 host = cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000237
Damien Miller95def091999-11-25 00:26:21 +1100238 for (optind = 1; optind < ac; optind++) {
239 if (av[optind][0] != '-') {
240 if (host)
241 break;
242 if ((cp = strchr(av[optind], '@'))) {
Damien Miller34132e52000-01-14 15:45:46 +1100243 if(cp == av[optind])
244 usage();
Damien Miller95def091999-11-25 00:26:21 +1100245 options.user = av[optind];
246 *cp = '\0';
247 host = ++cp;
248 } else
249 host = av[optind];
250 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000251 }
Damien Miller95def091999-11-25 00:26:21 +1100252 opt = av[optind][1];
253 if (!opt)
254 usage();
255 if (strchr("eilcpLRo", opt)) { /* options with arguments */
256 optarg = av[optind] + 2;
257 if (strcmp(optarg, "") == 0) {
258 if (optind >= ac - 1)
259 usage();
260 optarg = av[++optind];
261 }
262 } else {
263 if (av[optind][2])
264 usage();
265 optarg = NULL;
266 }
267 switch (opt) {
Damien Miller34132e52000-01-14 15:45:46 +1100268 case '4':
269 IPv4or6 = AF_INET;
270 break;
271
272 case '6':
273 IPv4or6 = AF_INET6;
274 break;
275
Damien Miller95def091999-11-25 00:26:21 +1100276 case 'n':
277 stdin_null_flag = 1;
278 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000279
Damien Miller95def091999-11-25 00:26:21 +1100280 case 'f':
281 fork_after_authentication_flag = 1;
282 stdin_null_flag = 1;
283 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000284
Damien Miller95def091999-11-25 00:26:21 +1100285 case 'x':
286 options.forward_x11 = 0;
287 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000288
Damien Miller95def091999-11-25 00:26:21 +1100289 case 'X':
290 options.forward_x11 = 1;
291 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000292
Damien Miller95def091999-11-25 00:26:21 +1100293 case 'g':
294 options.gateway_ports = 1;
295 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000296
Damien Miller95def091999-11-25 00:26:21 +1100297 case 'P':
298 options.use_privileged_port = 0;
299 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000300
Damien Miller95def091999-11-25 00:26:21 +1100301 case 'a':
302 options.forward_agent = 0;
303 break;
304#ifdef AFS
305 case 'k':
306 options.kerberos_tgt_passing = 0;
307 options.afs_token_passing = 0;
308 break;
309#endif
310 case 'i':
311 if (stat(optarg, &st) < 0) {
312 fprintf(stderr, "Warning: Identity file %s does not exist.\n",
313 optarg);
314 break;
315 }
316 if (options.num_identity_files >= SSH_MAX_IDENTITY_FILES)
317 fatal("Too many identity files specified (max %d)",
318 SSH_MAX_IDENTITY_FILES);
319 options.identity_files[options.num_identity_files++] =
320 xstrdup(optarg);
321 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322
Damien Miller95def091999-11-25 00:26:21 +1100323 case 't':
324 tty_flag = 1;
325 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000326
Damien Miller95def091999-11-25 00:26:21 +1100327 case 'v':
328 case 'V':
329 fprintf(stderr, "SSH Version %s, protocol version %d.%d.\n",
330 SSH_VERSION, PROTOCOL_MAJOR, PROTOCOL_MINOR);
331 fprintf(stderr, "Compiled with SSL.\n");
332 if (opt == 'V')
333 exit(0);
334 debug_flag = 1;
335 options.log_level = SYSLOG_LEVEL_DEBUG;
336 break;
337
338 case 'q':
339 options.log_level = SYSLOG_LEVEL_QUIET;
340 break;
341
342 case 'e':
343 if (optarg[0] == '^' && optarg[2] == 0 &&
344 (unsigned char) optarg[1] >= 64 && (unsigned char) optarg[1] < 128)
345 options.escape_char = (unsigned char) optarg[1] & 31;
346 else if (strlen(optarg) == 1)
347 options.escape_char = (unsigned char) optarg[0];
348 else if (strcmp(optarg, "none") == 0)
349 options.escape_char = -2;
350 else {
351 fprintf(stderr, "Bad escape character '%s'.\n", optarg);
352 exit(1);
353 }
354 break;
355
356 case 'c':
357 options.cipher = cipher_number(optarg);
358 if (options.cipher == -1) {
359 fprintf(stderr, "Unknown cipher type '%s'\n", optarg);
360 exit(1);
361 }
362 break;
363
364 case 'p':
365 options.port = atoi(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100366 break;
367
368 case 'l':
369 options.user = optarg;
370 break;
371
372 case 'R':
Damien Miller34132e52000-01-14 15:45:46 +1100373 if (sscanf(optarg, "%hu/%255[^/]/%hu", &fwd_port, buf,
374 &fwd_host_port) != 3 &&
375 sscanf(optarg, "%hu:%255[^:]:%hu", &fwd_port, buf,
376 &fwd_host_port) != 3) {
Damien Miller95def091999-11-25 00:26:21 +1100377 fprintf(stderr, "Bad forwarding specification '%s'.\n", optarg);
378 usage();
379 /* NOTREACHED */
380 }
381 add_remote_forward(&options, fwd_port, buf, fwd_host_port);
382 break;
383
384 case 'L':
Damien Miller34132e52000-01-14 15:45:46 +1100385 if (sscanf(optarg, "%hu/%255[^/]/%hu", &fwd_port, buf,
386 &fwd_host_port) != 3 &&
387 sscanf(optarg, "%hu:%255[^:]:%hu", &fwd_port, buf,
388 &fwd_host_port) != 3) {
Damien Miller95def091999-11-25 00:26:21 +1100389 fprintf(stderr, "Bad forwarding specification '%s'.\n", optarg);
390 usage();
391 /* NOTREACHED */
392 }
393 add_local_forward(&options, fwd_port, buf, fwd_host_port);
394 break;
395
396 case 'C':
397 options.compression = 1;
398 break;
399
400 case 'o':
401 dummy = 1;
402 if (process_config_line(&options, host ? host : "", optarg,
403 "command-line", 0, &dummy) != 0)
404 exit(1);
405 break;
406
407 default:
408 usage();
409 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000410 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000411
Damien Miller95def091999-11-25 00:26:21 +1100412 /* Check that we got a host name. */
413 if (!host)
414 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000415
Damien Miller95def091999-11-25 00:26:21 +1100416 /* check if RSA support exists */
417 if (rsa_alive() == 0) {
418 fprintf(stderr,
419 "%s: no RSA support in libssl and libcrypto. See ssl(8).\n",
420 __progname);
421 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000422 }
Damien Miller95def091999-11-25 00:26:21 +1100423 /* Initialize the command to execute on remote host. */
424 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000425
Damien Miller5428f641999-11-25 11:54:57 +1100426 /*
427 * Save the command to execute on the remote host in a buffer. There
428 * is no limit on the length of the command, except by the maximum
429 * packet size. Also sets the tty flag if there is no command.
430 */
Damien Miller95def091999-11-25 00:26:21 +1100431 if (optind == ac) {
432 /* No command specified - execute shell on a tty. */
433 tty_flag = 1;
434 } else {
435 /* A command has been specified. Store it into the
436 buffer. */
437 for (i = optind; i < ac; i++) {
438 if (i > optind)
439 buffer_append(&command, " ", 1);
440 buffer_append(&command, av[i], strlen(av[i]));
441 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000442 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000443
Damien Miller95def091999-11-25 00:26:21 +1100444 /* Cannot fork to background if no command. */
445 if (fork_after_authentication_flag && buffer_len(&command) == 0)
446 fatal("Cannot fork into background without a command to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000447
Damien Miller95def091999-11-25 00:26:21 +1100448 /* Allocate a tty by default if no command specified. */
449 if (buffer_len(&command) == 0)
450 tty_flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000451
Damien Miller95def091999-11-25 00:26:21 +1100452 /* Do not allocate a tty if stdin is not a tty. */
453 if (!isatty(fileno(stdin))) {
454 if (tty_flag)
455 fprintf(stderr, "Pseudo-terminal will not be allocated because stdin is not a terminal.\n");
456 tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000457 }
Damien Miller95def091999-11-25 00:26:21 +1100458 /* Get user data. */
459 pw = getpwuid(original_real_uid);
460 if (!pw) {
461 fprintf(stderr, "You don't exist, go away!\n");
462 exit(1);
463 }
464 /* Take a copy of the returned structure. */
465 memset(&pwcopy, 0, sizeof(pwcopy));
466 pwcopy.pw_name = xstrdup(pw->pw_name);
467 pwcopy.pw_passwd = xstrdup(pw->pw_passwd);
468 pwcopy.pw_uid = pw->pw_uid;
469 pwcopy.pw_gid = pw->pw_gid;
470 pwcopy.pw_dir = xstrdup(pw->pw_dir);
471 pwcopy.pw_shell = xstrdup(pw->pw_shell);
472 pw = &pwcopy;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000473
Damien Miller95def091999-11-25 00:26:21 +1100474 /* Initialize "log" output. Since we are the client all output
475 actually goes to the terminal. */
476 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000477
Damien Miller95def091999-11-25 00:26:21 +1100478 /* Read per-user configuration file. */
479 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, SSH_USER_CONFFILE);
480 read_config_file(buf, host, &options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000481
Damien Miller95def091999-11-25 00:26:21 +1100482 /* Read systemwide configuration file. */
483 read_config_file(HOST_CONFIG_FILE, host, &options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000484
Damien Miller95def091999-11-25 00:26:21 +1100485 /* Fill configuration defaults. */
486 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000487
Damien Miller95def091999-11-25 00:26:21 +1100488 /* reinit */
489 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490
Damien Miller95def091999-11-25 00:26:21 +1100491 if (options.user == NULL)
492 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000493
Damien Miller95def091999-11-25 00:26:21 +1100494 if (options.hostname != NULL)
495 host = options.hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000496
Damien Miller95def091999-11-25 00:26:21 +1100497 /* Find canonic host name. */
498 if (strchr(host, '.') == 0) {
Damien Miller34132e52000-01-14 15:45:46 +1100499 struct addrinfo hints;
500 struct addrinfo *ai = NULL;
501 int errgai;
502 memset(&hints, 0, sizeof(hints));
Damien Miller98c7ad62000-03-09 21:27:49 +1100503 hints.ai_family = IPv4or6;
Damien Miller34132e52000-01-14 15:45:46 +1100504 hints.ai_flags = AI_CANONNAME;
505 hints.ai_socktype = SOCK_STREAM;
506 errgai = getaddrinfo(host, NULL, &hints, &ai);
507 if (errgai == 0) {
508 if (ai->ai_canonname != NULL)
509 host = xstrdup(ai->ai_canonname);
510 freeaddrinfo(ai);
Damien Miller95def091999-11-25 00:26:21 +1100511 }
512 }
513 /* Disable rhosts authentication if not running as root. */
514 if (original_effective_uid != 0 || !options.use_privileged_port) {
515 options.rhosts_authentication = 0;
516 options.rhosts_rsa_authentication = 0;
517 }
Damien Miller5428f641999-11-25 11:54:57 +1100518 /*
519 * If using rsh has been selected, exec it now (without trying
520 * anything else). Note that we must release privileges first.
521 */
Damien Miller95def091999-11-25 00:26:21 +1100522 if (options.use_rsh) {
Damien Miller5428f641999-11-25 11:54:57 +1100523 /*
524 * Restore our superuser privileges. This must be done
525 * before permanently setting the uid.
526 */
Damien Miller95def091999-11-25 00:26:21 +1100527 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000528
Damien Miller95def091999-11-25 00:26:21 +1100529 /* Switch to the original uid permanently. */
530 permanently_set_uid(original_real_uid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000531
Damien Miller95def091999-11-25 00:26:21 +1100532 /* Execute rsh. */
533 rsh_connect(host, options.user, &command);
534 fatal("rsh_connect returned");
535 }
536 /* Restore our superuser privileges. */
537 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000538
Damien Miller5428f641999-11-25 11:54:57 +1100539 /*
540 * Open a connection to the remote host. This needs root privileges
541 * if rhosts_{rsa_}authentication is enabled.
542 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000543
Damien Miller95def091999-11-25 00:26:21 +1100544 ok = ssh_connect(host, &hostaddr, options.port,
545 options.connection_attempts,
546 !options.rhosts_authentication &&
547 !options.rhosts_rsa_authentication,
548 original_real_uid,
549 options.proxy_command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000550
Damien Miller5428f641999-11-25 11:54:57 +1100551 /*
552 * If we successfully made the connection, load the host private key
553 * in case we will need it later for combined rsa-rhosts
554 * authentication. This must be done before releasing extra
555 * privileges, because the file is only readable by root.
556 */
Damien Miller95def091999-11-25 00:26:21 +1100557 if (ok) {
558 host_private_key = RSA_new();
559 if (load_private_key(HOST_KEY_FILE, "", host_private_key, NULL))
560 host_private_key_loaded = 1;
561 }
Damien Miller5428f641999-11-25 11:54:57 +1100562 /*
563 * Get rid of any extra privileges that we may have. We will no
564 * longer need them. Also, extra privileges could make it very hard
565 * to read identity files and other non-world-readable files from the
566 * user's home directory if it happens to be on a NFS volume where
567 * root is mapped to nobody.
568 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000569
Damien Miller5428f641999-11-25 11:54:57 +1100570 /*
571 * Note that some legacy systems need to postpone the following call
572 * to permanently_set_uid() until the private hostkey is destroyed
573 * with RSA_free(). Otherwise the calling user could ptrace() the
574 * process, read the private hostkey and impersonate the host.
575 * OpenBSD does not allow ptracing of setuid processes.
576 */
Damien Miller95def091999-11-25 00:26:21 +1100577 permanently_set_uid(original_real_uid);
578
Damien Miller5428f641999-11-25 11:54:57 +1100579 /*
580 * Now that we are back to our own permissions, create ~/.ssh
581 * directory if it doesn\'t already exist.
582 */
Damien Miller95def091999-11-25 00:26:21 +1100583 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, SSH_USER_DIR);
584 if (stat(buf, &st) < 0)
585 if (mkdir(buf, 0755) < 0)
586 error("Could not create directory '%.200s'.", buf);
587
588 /* Check if the connection failed, and try "rsh" if appropriate. */
589 if (!ok) {
590 if (options.port != 0)
Damien Milleraae6c611999-12-06 11:47:28 +1100591 log("Secure connection to %.100s on port %hu refused%.100s.",
Damien Miller95def091999-11-25 00:26:21 +1100592 host, options.port,
593 options.fallback_to_rsh ? "; reverting to insecure method" : "");
594 else
595 log("Secure connection to %.100s refused%.100s.", host,
596 options.fallback_to_rsh ? "; reverting to insecure method" : "");
597
598 if (options.fallback_to_rsh) {
599 rsh_connect(host, options.user, &command);
600 fatal("rsh_connect returned");
601 }
602 exit(1);
603 }
604 /* Expand ~ in options.identity_files. */
605 for (i = 0; i < options.num_identity_files; i++)
606 options.identity_files[i] =
607 tilde_expand_filename(options.identity_files[i], original_real_uid);
608
609 /* Expand ~ in known host file names. */
610 options.system_hostfile = tilde_expand_filename(options.system_hostfile,
611 original_real_uid);
612 options.user_hostfile = tilde_expand_filename(options.user_hostfile,
613 original_real_uid);
614
615 /* Log into the remote system. This never returns if the login fails. */
616 ssh_login(host_private_key_loaded, host_private_key,
Damien Miller34132e52000-01-14 15:45:46 +1100617 host, (struct sockaddr *)&hostaddr, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100618
619 /* We no longer need the host private key. Clear it now. */
620 if (host_private_key_loaded)
621 RSA_free(host_private_key); /* Destroys contents safely */
622
Damien Miller95def091999-11-25 00:26:21 +1100623 /* Enable compression if requested. */
624 if (options.compression) {
625 debug("Requesting compression at level %d.", options.compression_level);
626
627 if (options.compression_level < 1 || options.compression_level > 9)
628 fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
629
630 /* Send the request. */
631 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
632 packet_put_int(options.compression_level);
633 packet_send();
634 packet_write_wait();
635 type = packet_read(&plen);
636 if (type == SSH_SMSG_SUCCESS)
637 packet_start_compression(options.compression_level);
638 else if (type == SSH_SMSG_FAILURE)
639 log("Warning: Remote host refused compression.");
640 else
641 packet_disconnect("Protocol error waiting for compression response.");
642 }
643 /* Allocate a pseudo tty if appropriate. */
644 if (tty_flag) {
645 debug("Requesting pty.");
646
647 /* Start the packet. */
648 packet_start(SSH_CMSG_REQUEST_PTY);
649
650 /* Store TERM in the packet. There is no limit on the
651 length of the string. */
652 cp = getenv("TERM");
653 if (!cp)
654 cp = "";
655 packet_put_string(cp, strlen(cp));
656
657 /* Store window size in the packet. */
658 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
659 memset(&ws, 0, sizeof(ws));
660 packet_put_int(ws.ws_row);
661 packet_put_int(ws.ws_col);
662 packet_put_int(ws.ws_xpixel);
663 packet_put_int(ws.ws_ypixel);
664
665 /* Store tty modes in the packet. */
666 tty_make_modes(fileno(stdin));
667
668 /* Send the packet, and wait for it to leave. */
669 packet_send();
670 packet_write_wait();
671
672 /* Read response from the server. */
673 type = packet_read(&plen);
Damien Miller450a7a12000-03-26 13:04:51 +1000674 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100675 interactive = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000676 have_pty = 1;
677 } else if (type == SSH_SMSG_FAILURE)
Damien Miller95def091999-11-25 00:26:21 +1100678 log("Warning: Remote host failed or refused to allocate a pseudo tty.");
679 else
680 packet_disconnect("Protocol error waiting for pty request response.");
681 }
682 /* Request X11 forwarding if enabled and DISPLAY is set. */
683 if (options.forward_x11 && getenv("DISPLAY") != NULL) {
684 char line[512], proto[512], data[512];
685 FILE *f;
686 int forwarded = 0, got_data = 0, i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000687
688#ifdef XAUTH_PATH
Damien Miller95def091999-11-25 00:26:21 +1100689 /* Try to get Xauthority information for the display. */
690 snprintf(line, sizeof line, "%.100s list %.200s 2>/dev/null",
691 XAUTH_PATH, getenv("DISPLAY"));
692 f = popen(line, "r");
693 if (f && fgets(line, sizeof(line), f) &&
694 sscanf(line, "%*s %s %s", proto, data) == 2)
695 got_data = 1;
696 if (f)
697 pclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000698#endif /* XAUTH_PATH */
Damien Miller5428f641999-11-25 11:54:57 +1100699 /*
700 * If we didn't get authentication data, just make up some
701 * data. The forwarding code will check the validity of the
702 * response anyway, and substitute this data. The X11
703 * server, however, will ignore this fake data and use
704 * whatever authentication mechanisms it was using otherwise
705 * for the local connection.
706 */
Damien Miller95def091999-11-25 00:26:21 +1100707 if (!got_data) {
708 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000709
Damien Miller95def091999-11-25 00:26:21 +1100710 strlcpy(proto, "MIT-MAGIC-COOKIE-1", sizeof proto);
711 for (i = 0; i < 16; i++) {
712 if (i % 4 == 0)
713 rand = arc4random();
714 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x", rand & 0xff);
715 rand >>= 8;
716 }
717 }
Damien Miller5428f641999-11-25 11:54:57 +1100718 /*
719 * Got local authentication reasonable information. Request
720 * forwarding with authentication spoofing.
721 */
Damien Miller95def091999-11-25 00:26:21 +1100722 debug("Requesting X11 forwarding with authentication spoofing.");
723 x11_request_forwarding_with_spoofing(proto, data);
724
725 /* Read response from the server. */
726 type = packet_read(&plen);
727 if (type == SSH_SMSG_SUCCESS) {
728 forwarded = 1;
729 interactive = 1;
730 } else if (type == SSH_SMSG_FAILURE)
731 log("Warning: Remote host denied X11 forwarding.");
732 else
733 packet_disconnect("Protocol error waiting for X11 forwarding");
734 }
735 /* Tell the packet module whether this is an interactive session. */
736 packet_set_interactive(interactive, options.keepalives);
737
738 /* Clear agent forwarding if we don\'t have an agent. */
739 authfd = ssh_get_authentication_socket();
740 if (authfd < 0)
741 options.forward_agent = 0;
742 else
743 ssh_close_authentication_socket(authfd);
744
745 /* Request authentication agent forwarding if appropriate. */
746 if (options.forward_agent) {
747 debug("Requesting authentication agent forwarding.");
748 auth_request_forwarding();
749
750 /* Read response from the server. */
751 type = packet_read(&plen);
752 packet_integrity_check(plen, 0, type);
753 if (type != SSH_SMSG_SUCCESS)
754 log("Warning: Remote host denied authentication agent forwarding.");
755 }
756 /* Initiate local TCP/IP port forwardings. */
757 for (i = 0; i < options.num_local_forwards; i++) {
758 debug("Connections to local port %d forwarded to remote address %.200s:%d",
759 options.local_forwards[i].port,
760 options.local_forwards[i].host,
761 options.local_forwards[i].host_port);
762 channel_request_local_forwarding(options.local_forwards[i].port,
763 options.local_forwards[i].host,
Damien Millera34a28b1999-12-14 10:47:15 +1100764 options.local_forwards[i].host_port,
765 options.gateway_ports);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000766 }
767
Damien Miller95def091999-11-25 00:26:21 +1100768 /* Initiate remote TCP/IP port forwardings. */
769 for (i = 0; i < options.num_remote_forwards; i++) {
770 debug("Connections to remote port %d forwarded to local address %.200s:%d",
771 options.remote_forwards[i].port,
772 options.remote_forwards[i].host,
773 options.remote_forwards[i].host_port);
774 channel_request_remote_forwarding(options.remote_forwards[i].port,
775 options.remote_forwards[i].host,
776 options.remote_forwards[i].host_port);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000777 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000778
Damien Miller5428f641999-11-25 11:54:57 +1100779 /* If requested, let ssh continue in the background. */
780 if (fork_after_authentication_flag)
781 if (daemon(1, 1) < 0)
782 fatal("daemon() failed: %.200s", strerror(errno));
783
784 /*
785 * If a command was specified on the command line, execute the
786 * command now. Otherwise request the server to start a shell.
787 */
Damien Miller95def091999-11-25 00:26:21 +1100788 if (buffer_len(&command) > 0) {
789 int len = buffer_len(&command);
790 if (len > 900)
791 len = 900;
792 debug("Sending command: %.*s", len, buffer_ptr(&command));
793 packet_start(SSH_CMSG_EXEC_CMD);
794 packet_put_string(buffer_ptr(&command), buffer_len(&command));
795 packet_send();
796 packet_write_wait();
797 } else {
798 debug("Requesting shell.");
799 packet_start(SSH_CMSG_EXEC_SHELL);
800 packet_send();
801 packet_write_wait();
802 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000803
Damien Miller95def091999-11-25 00:26:21 +1100804 /* Enter the interactive session. */
Damien Miller450a7a12000-03-26 13:04:51 +1000805 exit_status = client_loop(have_pty, tty_flag ? options.escape_char : -1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000806
Damien Miller95def091999-11-25 00:26:21 +1100807 /* Close the connection to the remote host. */
808 packet_close();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000809
Damien Miller95def091999-11-25 00:26:21 +1100810 /* Exit with the status returned by the program on the remote side. */
811 exit(exit_status);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000812}