blob: 962aa2d43eeb68f078f8652686700f8555308fd8 [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 Miller34132e52000-01-14 15:45:46 +110014RCSID("$Id: ssh.c,v 1.16 2000/01/14 04:45:51 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"
23
Damien Miller3f905871999-11-15 17:10:57 +110024#ifdef HAVE___PROGNAME
25extern char *__progname;
26#else /* HAVE___PROGNAME */
27const char *__progname = "ssh";
28#endif /* HAVE___PROGNAME */
29
Damien Miller34132e52000-01-14 15:45:46 +110030/* Flag indicating whether IPv4 or IPv6. This can be set on the command line.
31 Default value is AF_UNSPEC means both IPv4 and IPv6. */
32int IPv4or6 = AF_UNSPEC;
33
Damien Miller95def091999-11-25 00:26:21 +110034/* Flag indicating whether debug mode is on. This can be set on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100035int debug_flag = 0;
36
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037int tty_flag = 0;
38
Damien Miller5428f641999-11-25 11:54:57 +110039/*
40 * Flag indicating that nothing should be read from stdin. This can be set
41 * on the command line.
42 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043int stdin_null_flag = 0;
44
Damien Miller5428f641999-11-25 11:54:57 +110045/*
46 * Flag indicating that ssh should fork after authentication. This is useful
47 * so that the pasphrase can be entered manually, and then ssh goes to the
48 * background.
49 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050int fork_after_authentication_flag = 0;
51
Damien Miller5428f641999-11-25 11:54:57 +110052/*
53 * General data structure for command line options and options configurable
54 * in configuration files. See readconf.h.
55 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056Options options;
57
Damien Miller5428f641999-11-25 11:54:57 +110058/*
59 * Name of the host we are connecting to. This is the name given on the
60 * command line, or the HostName specified for the user-supplied name in a
61 * configuration file.
62 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063char *host;
64
65/* socket address the host resolves to */
Damien Miller34132e52000-01-14 15:45:46 +110066struct sockaddr_storage hostaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067
Damien Miller5428f641999-11-25 11:54:57 +110068/*
69 * Flag to indicate that we have received a window change signal which has
70 * not yet been processed. This will cause a message indicating the new
71 * window size to be sent to the server a little later. This is volatile
72 * because this is updated in a signal handler.
73 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074volatile int received_window_change_signal = 0;
75
76/* Value of argv[0] (set in the main program). */
77char *av0;
78
79/* Flag indicating whether we have a valid host private key loaded. */
80int host_private_key_loaded = 0;
81
82/* Host private key. */
83RSA *host_private_key = NULL;
84
85/* Original real UID. */
86uid_t original_real_uid;
87
88/* Prints a help message to the user. This function never returns. */
89
90void
91usage()
92{
Damien Miller95def091999-11-25 00:26:21 +110093 fprintf(stderr, "Usage: %s [options] host [command]\n", av0);
94 fprintf(stderr, "Options:\n");
95 fprintf(stderr, " -l user Log in using this user name.\n");
96 fprintf(stderr, " -n Redirect input from /dev/null.\n");
97 fprintf(stderr, " -a Disable authentication agent forwarding.\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098#ifdef AFS
Damien Miller95def091999-11-25 00:26:21 +110099 fprintf(stderr, " -k Disable Kerberos ticket and AFS token forwarding.\n");
100#endif /* AFS */
101 fprintf(stderr, " -x Disable X11 connection forwarding.\n");
102 fprintf(stderr, " -i file Identity for RSA authentication (default: ~/.ssh/identity).\n");
103 fprintf(stderr, " -t Tty; allocate a tty even if command is given.\n");
104 fprintf(stderr, " -v Verbose; display verbose debugging messages.\n");
105 fprintf(stderr, " -V Display version number only.\n");
106 fprintf(stderr, " -P Don't allocate a privileged port.\n");
107 fprintf(stderr, " -q Quiet; don't display any warning messages.\n");
108 fprintf(stderr, " -f Fork into background after authentication.\n");
109 fprintf(stderr, " -e char Set escape character; ``none'' = disable (default: ~).\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110
Damien Miller95def091999-11-25 00:26:21 +1100111 fprintf(stderr, " -c cipher Select encryption algorithm: "
112 "``3des'', "
113 "``blowfish''\n");
114 fprintf(stderr, " -p port Connect to this port. Server must be on the same port.\n");
115 fprintf(stderr, " -L listen-port:host:port Forward local port to remote address\n");
116 fprintf(stderr, " -R listen-port:host:port Forward remote port to local address\n");
117 fprintf(stderr, " These cause %s to listen for connections on a port, and\n", av0);
118 fprintf(stderr, " forward them to the other side by connecting to host:port.\n");
119 fprintf(stderr, " -C Enable compression.\n");
120 fprintf(stderr, " -g Allow remote hosts to connect to forwarded ports.\n");
Damien Miller34132e52000-01-14 15:45:46 +1100121 fprintf(stderr, " -4 Use IPv4 only.\n");
122 fprintf(stderr, " -6 Use IPv6 only.\n");
Damien Miller95def091999-11-25 00:26:21 +1100123 fprintf(stderr, " -o 'option' Process the option as if it was read from a configuration file.\n");
124 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125}
126
Damien Miller95def091999-11-25 00:26:21 +1100127/*
128 * Connects to the given host using rsh (or prints an error message and exits
129 * if rsh is not available). This function never returns.
130 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131void
Damien Miller95def091999-11-25 00:26:21 +1100132rsh_connect(char *host, char *user, Buffer * command)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000133{
Damien Miller95def091999-11-25 00:26:21 +1100134 char *args[10];
135 int i;
136
137 log("Using rsh. WARNING: Connection will not be encrypted.");
138 /* Build argument list for rsh. */
139 i = 0;
140 args[i++] = _PATH_RSH;
141 /* host may have to come after user on some systems */
142 args[i++] = host;
143 if (user) {
144 args[i++] = "-l";
145 args[i++] = user;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146 }
Damien Miller95def091999-11-25 00:26:21 +1100147 if (buffer_len(command) > 0) {
148 buffer_append(command, "\0", 1);
149 args[i++] = buffer_ptr(command);
150 }
151 args[i++] = NULL;
152 if (debug_flag) {
153 for (i = 0; args[i]; i++) {
154 if (i != 0)
155 fprintf(stderr, " ");
156 fprintf(stderr, "%s", args[i]);
157 }
158 fprintf(stderr, "\n");
159 }
160 execv(_PATH_RSH, args);
161 perror(_PATH_RSH);
162 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000163}
164
Damien Miller95def091999-11-25 00:26:21 +1100165/*
166 * Main program for the ssh client.
167 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168int
169main(int ac, char **av)
170{
Damien Milleraae6c611999-12-06 11:47:28 +1100171 int i, opt, optind, type, exit_status, ok, authfd;
172 u_short fwd_port, fwd_host_port;
Damien Miller95def091999-11-25 00:26:21 +1100173 char *optarg, *cp, buf[256];
174 Buffer command;
175 struct winsize ws;
176 struct stat st;
177 struct passwd *pw, pwcopy;
178 int interactive = 0, dummy;
179 uid_t original_effective_uid;
180 int plen;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000181
Damien Miller5428f641999-11-25 11:54:57 +1100182 /*
183 * Save the original real uid. It will be needed later (uid-swapping
184 * may clobber the real uid).
185 */
Damien Miller95def091999-11-25 00:26:21 +1100186 original_real_uid = getuid();
187 original_effective_uid = geteuid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000188
Damien Miller95def091999-11-25 00:26:21 +1100189 /* If we are installed setuid root be careful to not drop core. */
190 if (original_real_uid != original_effective_uid) {
191 struct rlimit rlim;
192 rlim.rlim_cur = rlim.rlim_max = 0;
193 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
194 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195 }
Damien Miller5428f641999-11-25 11:54:57 +1100196 /*
197 * Use uid-swapping to give up root privileges for the duration of
198 * option processing. We will re-instantiate the rights when we are
199 * ready to create the privileged port, and will permanently drop
200 * them when the port has been created (actually, when the connection
201 * has been made, as we may need to create the port several times).
202 */
Damien Miller95def091999-11-25 00:26:21 +1100203 temporarily_use_uid(original_real_uid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000204
Damien Miller5428f641999-11-25 11:54:57 +1100205 /*
206 * Set our umask to something reasonable, as some files are created
207 * with the default umask. This will make them world-readable but
208 * writable only by the owner, which is ok for all files for which we
209 * don't set the modes explicitly.
210 */
Damien Miller95def091999-11-25 00:26:21 +1100211 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000212
Damien Miller95def091999-11-25 00:26:21 +1100213 /* Save our own name. */
214 av0 = av[0];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215
Damien Miller95def091999-11-25 00:26:21 +1100216 /* Initialize option structure to indicate that no values have been set. */
217 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000218
Damien Miller95def091999-11-25 00:26:21 +1100219 /* Parse command-line arguments. */
220 host = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000221
Damien Miller95def091999-11-25 00:26:21 +1100222 /* If program name is not one of the standard names, use it as host name. */
223 if (strchr(av0, '/'))
224 cp = strrchr(av0, '/') + 1;
225 else
226 cp = av0;
227 if (strcmp(cp, "rsh") != 0 && strcmp(cp, "ssh") != 0 &&
228 strcmp(cp, "rlogin") != 0 && strcmp(cp, "slogin") != 0)
229 host = cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000230
Damien Miller95def091999-11-25 00:26:21 +1100231 for (optind = 1; optind < ac; optind++) {
232 if (av[optind][0] != '-') {
233 if (host)
234 break;
235 if ((cp = strchr(av[optind], '@'))) {
Damien Miller34132e52000-01-14 15:45:46 +1100236 if(cp == av[optind])
237 usage();
Damien Miller95def091999-11-25 00:26:21 +1100238 options.user = av[optind];
239 *cp = '\0';
240 host = ++cp;
241 } else
242 host = av[optind];
243 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000244 }
Damien Miller95def091999-11-25 00:26:21 +1100245 opt = av[optind][1];
246 if (!opt)
247 usage();
248 if (strchr("eilcpLRo", opt)) { /* options with arguments */
249 optarg = av[optind] + 2;
250 if (strcmp(optarg, "") == 0) {
251 if (optind >= ac - 1)
252 usage();
253 optarg = av[++optind];
254 }
255 } else {
256 if (av[optind][2])
257 usage();
258 optarg = NULL;
259 }
260 switch (opt) {
Damien Miller34132e52000-01-14 15:45:46 +1100261 case '4':
262 IPv4or6 = AF_INET;
263 break;
264
265 case '6':
266 IPv4or6 = AF_INET6;
267 break;
268
Damien Miller95def091999-11-25 00:26:21 +1100269 case 'n':
270 stdin_null_flag = 1;
271 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000272
Damien Miller95def091999-11-25 00:26:21 +1100273 case 'f':
274 fork_after_authentication_flag = 1;
275 stdin_null_flag = 1;
276 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000277
Damien Miller95def091999-11-25 00:26:21 +1100278 case 'x':
279 options.forward_x11 = 0;
280 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000281
Damien Miller95def091999-11-25 00:26:21 +1100282 case 'X':
283 options.forward_x11 = 1;
284 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000285
Damien Miller95def091999-11-25 00:26:21 +1100286 case 'g':
287 options.gateway_ports = 1;
288 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000289
Damien Miller95def091999-11-25 00:26:21 +1100290 case 'P':
291 options.use_privileged_port = 0;
292 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000293
Damien Miller95def091999-11-25 00:26:21 +1100294 case 'a':
295 options.forward_agent = 0;
296 break;
297#ifdef AFS
298 case 'k':
299 options.kerberos_tgt_passing = 0;
300 options.afs_token_passing = 0;
301 break;
302#endif
303 case 'i':
304 if (stat(optarg, &st) < 0) {
305 fprintf(stderr, "Warning: Identity file %s does not exist.\n",
306 optarg);
307 break;
308 }
309 if (options.num_identity_files >= SSH_MAX_IDENTITY_FILES)
310 fatal("Too many identity files specified (max %d)",
311 SSH_MAX_IDENTITY_FILES);
312 options.identity_files[options.num_identity_files++] =
313 xstrdup(optarg);
314 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000315
Damien Miller95def091999-11-25 00:26:21 +1100316 case 't':
317 tty_flag = 1;
318 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000319
Damien Miller95def091999-11-25 00:26:21 +1100320 case 'v':
321 case 'V':
322 fprintf(stderr, "SSH Version %s, protocol version %d.%d.\n",
323 SSH_VERSION, PROTOCOL_MAJOR, PROTOCOL_MINOR);
Damien Millerd00d1611999-12-29 10:17:09 +1100324#ifndef RSAREF
Damien Miller95def091999-11-25 00:26:21 +1100325 fprintf(stderr, "Compiled with SSL.\n");
Damien Millerd00d1611999-12-29 10:17:09 +1100326#else
327 fprintf(stderr, "Compiled with SSL (RSAref version).\n");
328#endif
Damien Miller95def091999-11-25 00:26:21 +1100329 if (opt == 'V')
330 exit(0);
331 debug_flag = 1;
332 options.log_level = SYSLOG_LEVEL_DEBUG;
333 break;
334
335 case 'q':
336 options.log_level = SYSLOG_LEVEL_QUIET;
337 break;
338
339 case 'e':
340 if (optarg[0] == '^' && optarg[2] == 0 &&
341 (unsigned char) optarg[1] >= 64 && (unsigned char) optarg[1] < 128)
342 options.escape_char = (unsigned char) optarg[1] & 31;
343 else if (strlen(optarg) == 1)
344 options.escape_char = (unsigned char) optarg[0];
345 else if (strcmp(optarg, "none") == 0)
346 options.escape_char = -2;
347 else {
348 fprintf(stderr, "Bad escape character '%s'.\n", optarg);
349 exit(1);
350 }
351 break;
352
353 case 'c':
354 options.cipher = cipher_number(optarg);
355 if (options.cipher == -1) {
356 fprintf(stderr, "Unknown cipher type '%s'\n", optarg);
357 exit(1);
358 }
359 break;
360
361 case 'p':
362 options.port = atoi(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100363 break;
364
365 case 'l':
366 options.user = optarg;
367 break;
368
369 case 'R':
Damien Miller34132e52000-01-14 15:45:46 +1100370 if (sscanf(optarg, "%hu/%255[^/]/%hu", &fwd_port, buf,
371 &fwd_host_port) != 3 &&
372 sscanf(optarg, "%hu:%255[^:]:%hu", &fwd_port, buf,
373 &fwd_host_port) != 3) {
Damien Miller95def091999-11-25 00:26:21 +1100374 fprintf(stderr, "Bad forwarding specification '%s'.\n", optarg);
375 usage();
376 /* NOTREACHED */
377 }
378 add_remote_forward(&options, fwd_port, buf, fwd_host_port);
379 break;
380
381 case 'L':
Damien Miller34132e52000-01-14 15:45:46 +1100382 if (sscanf(optarg, "%hu/%255[^/]/%hu", &fwd_port, buf,
383 &fwd_host_port) != 3 &&
384 sscanf(optarg, "%hu:%255[^:]:%hu", &fwd_port, buf,
385 &fwd_host_port) != 3) {
Damien Miller95def091999-11-25 00:26:21 +1100386 fprintf(stderr, "Bad forwarding specification '%s'.\n", optarg);
387 usage();
388 /* NOTREACHED */
389 }
390 add_local_forward(&options, fwd_port, buf, fwd_host_port);
391 break;
392
393 case 'C':
394 options.compression = 1;
395 break;
396
397 case 'o':
398 dummy = 1;
399 if (process_config_line(&options, host ? host : "", optarg,
400 "command-line", 0, &dummy) != 0)
401 exit(1);
402 break;
403
404 default:
405 usage();
406 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000407 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000408
Damien Miller95def091999-11-25 00:26:21 +1100409 /* Check that we got a host name. */
410 if (!host)
411 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000412
Damien Miller95def091999-11-25 00:26:21 +1100413 /* check if RSA support exists */
414 if (rsa_alive() == 0) {
415 fprintf(stderr,
416 "%s: no RSA support in libssl and libcrypto. See ssl(8).\n",
417 __progname);
418 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000419 }
Damien Miller95def091999-11-25 00:26:21 +1100420 /* Initialize the command to execute on remote host. */
421 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000422
Damien Miller5428f641999-11-25 11:54:57 +1100423 /*
424 * Save the command to execute on the remote host in a buffer. There
425 * is no limit on the length of the command, except by the maximum
426 * packet size. Also sets the tty flag if there is no command.
427 */
Damien Miller95def091999-11-25 00:26:21 +1100428 if (optind == ac) {
429 /* No command specified - execute shell on a tty. */
430 tty_flag = 1;
431 } else {
432 /* A command has been specified. Store it into the
433 buffer. */
434 for (i = optind; i < ac; i++) {
435 if (i > optind)
436 buffer_append(&command, " ", 1);
437 buffer_append(&command, av[i], strlen(av[i]));
438 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000439 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000440
Damien Miller95def091999-11-25 00:26:21 +1100441 /* Cannot fork to background if no command. */
442 if (fork_after_authentication_flag && buffer_len(&command) == 0)
443 fatal("Cannot fork into background without a command to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000444
Damien Miller95def091999-11-25 00:26:21 +1100445 /* Allocate a tty by default if no command specified. */
446 if (buffer_len(&command) == 0)
447 tty_flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000448
Damien Miller95def091999-11-25 00:26:21 +1100449 /* Do not allocate a tty if stdin is not a tty. */
450 if (!isatty(fileno(stdin))) {
451 if (tty_flag)
452 fprintf(stderr, "Pseudo-terminal will not be allocated because stdin is not a terminal.\n");
453 tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000454 }
Damien Miller95def091999-11-25 00:26:21 +1100455 /* Get user data. */
456 pw = getpwuid(original_real_uid);
457 if (!pw) {
458 fprintf(stderr, "You don't exist, go away!\n");
459 exit(1);
460 }
461 /* Take a copy of the returned structure. */
462 memset(&pwcopy, 0, sizeof(pwcopy));
463 pwcopy.pw_name = xstrdup(pw->pw_name);
464 pwcopy.pw_passwd = xstrdup(pw->pw_passwd);
465 pwcopy.pw_uid = pw->pw_uid;
466 pwcopy.pw_gid = pw->pw_gid;
467 pwcopy.pw_dir = xstrdup(pw->pw_dir);
468 pwcopy.pw_shell = xstrdup(pw->pw_shell);
469 pw = &pwcopy;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000470
Damien Miller95def091999-11-25 00:26:21 +1100471 /* Initialize "log" output. Since we are the client all output
472 actually goes to the terminal. */
473 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000474
Damien Miller95def091999-11-25 00:26:21 +1100475 /* Read per-user configuration file. */
476 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, SSH_USER_CONFFILE);
477 read_config_file(buf, host, &options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000478
Damien Miller95def091999-11-25 00:26:21 +1100479 /* Read systemwide configuration file. */
480 read_config_file(HOST_CONFIG_FILE, host, &options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000481
Damien Miller95def091999-11-25 00:26:21 +1100482 /* Fill configuration defaults. */
483 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000484
Damien Miller95def091999-11-25 00:26:21 +1100485 /* reinit */
486 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000487
Damien Miller95def091999-11-25 00:26:21 +1100488 if (options.user == NULL)
489 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490
Damien Miller95def091999-11-25 00:26:21 +1100491 if (options.hostname != NULL)
492 host = options.hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000493
Damien Miller95def091999-11-25 00:26:21 +1100494 /* Find canonic host name. */
495 if (strchr(host, '.') == 0) {
Damien Miller34132e52000-01-14 15:45:46 +1100496 struct addrinfo hints;
497 struct addrinfo *ai = NULL;
498 int errgai;
499 memset(&hints, 0, sizeof(hints));
500 hints.ai_family = AF_UNSPEC;
501 hints.ai_flags = AI_CANONNAME;
502 hints.ai_socktype = SOCK_STREAM;
503 errgai = getaddrinfo(host, NULL, &hints, &ai);
504 if (errgai == 0) {
505 if (ai->ai_canonname != NULL)
506 host = xstrdup(ai->ai_canonname);
507 freeaddrinfo(ai);
Damien Miller95def091999-11-25 00:26:21 +1100508 }
509 }
510 /* Disable rhosts authentication if not running as root. */
511 if (original_effective_uid != 0 || !options.use_privileged_port) {
512 options.rhosts_authentication = 0;
513 options.rhosts_rsa_authentication = 0;
514 }
Damien Miller5428f641999-11-25 11:54:57 +1100515 /*
516 * If using rsh has been selected, exec it now (without trying
517 * anything else). Note that we must release privileges first.
518 */
Damien Miller95def091999-11-25 00:26:21 +1100519 if (options.use_rsh) {
Damien Miller5428f641999-11-25 11:54:57 +1100520 /*
521 * Restore our superuser privileges. This must be done
522 * before permanently setting the uid.
523 */
Damien Miller95def091999-11-25 00:26:21 +1100524 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000525
Damien Miller95def091999-11-25 00:26:21 +1100526 /* Switch to the original uid permanently. */
527 permanently_set_uid(original_real_uid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000528
Damien Miller95def091999-11-25 00:26:21 +1100529 /* Execute rsh. */
530 rsh_connect(host, options.user, &command);
531 fatal("rsh_connect returned");
532 }
533 /* Restore our superuser privileges. */
534 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000535
Damien Miller5428f641999-11-25 11:54:57 +1100536 /*
537 * Open a connection to the remote host. This needs root privileges
538 * if rhosts_{rsa_}authentication is enabled.
539 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000540
Damien Miller95def091999-11-25 00:26:21 +1100541 ok = ssh_connect(host, &hostaddr, options.port,
542 options.connection_attempts,
543 !options.rhosts_authentication &&
544 !options.rhosts_rsa_authentication,
545 original_real_uid,
546 options.proxy_command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000547
Damien Miller5428f641999-11-25 11:54:57 +1100548 /*
549 * If we successfully made the connection, load the host private key
550 * in case we will need it later for combined rsa-rhosts
551 * authentication. This must be done before releasing extra
552 * privileges, because the file is only readable by root.
553 */
Damien Miller95def091999-11-25 00:26:21 +1100554 if (ok) {
555 host_private_key = RSA_new();
556 if (load_private_key(HOST_KEY_FILE, "", host_private_key, NULL))
557 host_private_key_loaded = 1;
558 }
Damien Miller5428f641999-11-25 11:54:57 +1100559 /*
560 * Get rid of any extra privileges that we may have. We will no
561 * longer need them. Also, extra privileges could make it very hard
562 * to read identity files and other non-world-readable files from the
563 * user's home directory if it happens to be on a NFS volume where
564 * root is mapped to nobody.
565 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000566
Damien Miller5428f641999-11-25 11:54:57 +1100567 /*
568 * Note that some legacy systems need to postpone the following call
569 * to permanently_set_uid() until the private hostkey is destroyed
570 * with RSA_free(). Otherwise the calling user could ptrace() the
571 * process, read the private hostkey and impersonate the host.
572 * OpenBSD does not allow ptracing of setuid processes.
573 */
Damien Miller95def091999-11-25 00:26:21 +1100574 permanently_set_uid(original_real_uid);
575
Damien Miller5428f641999-11-25 11:54:57 +1100576 /*
577 * Now that we are back to our own permissions, create ~/.ssh
578 * directory if it doesn\'t already exist.
579 */
Damien Miller95def091999-11-25 00:26:21 +1100580 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, SSH_USER_DIR);
581 if (stat(buf, &st) < 0)
582 if (mkdir(buf, 0755) < 0)
583 error("Could not create directory '%.200s'.", buf);
584
585 /* Check if the connection failed, and try "rsh" if appropriate. */
586 if (!ok) {
587 if (options.port != 0)
Damien Milleraae6c611999-12-06 11:47:28 +1100588 log("Secure connection to %.100s on port %hu refused%.100s.",
Damien Miller95def091999-11-25 00:26:21 +1100589 host, options.port,
590 options.fallback_to_rsh ? "; reverting to insecure method" : "");
591 else
592 log("Secure connection to %.100s refused%.100s.", host,
593 options.fallback_to_rsh ? "; reverting to insecure method" : "");
594
595 if (options.fallback_to_rsh) {
596 rsh_connect(host, options.user, &command);
597 fatal("rsh_connect returned");
598 }
599 exit(1);
600 }
601 /* Expand ~ in options.identity_files. */
602 for (i = 0; i < options.num_identity_files; i++)
603 options.identity_files[i] =
604 tilde_expand_filename(options.identity_files[i], original_real_uid);
605
606 /* Expand ~ in known host file names. */
607 options.system_hostfile = tilde_expand_filename(options.system_hostfile,
608 original_real_uid);
609 options.user_hostfile = tilde_expand_filename(options.user_hostfile,
610 original_real_uid);
611
612 /* Log into the remote system. This never returns if the login fails. */
613 ssh_login(host_private_key_loaded, host_private_key,
Damien Miller34132e52000-01-14 15:45:46 +1100614 host, (struct sockaddr *)&hostaddr, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100615
616 /* We no longer need the host private key. Clear it now. */
617 if (host_private_key_loaded)
618 RSA_free(host_private_key); /* Destroys contents safely */
619
620 /* Close connection cleanly after attack. */
621 cipher_attack_detected = packet_disconnect;
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);
674 if (type == SSH_SMSG_SUCCESS)
675 interactive = 1;
676 else if (type == SSH_SMSG_FAILURE)
677 log("Warning: Remote host failed or refused to allocate a pseudo tty.");
678 else
679 packet_disconnect("Protocol error waiting for pty request response.");
680 }
681 /* Request X11 forwarding if enabled and DISPLAY is set. */
682 if (options.forward_x11 && getenv("DISPLAY") != NULL) {
683 char line[512], proto[512], data[512];
684 FILE *f;
685 int forwarded = 0, got_data = 0, i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000686
687#ifdef XAUTH_PATH
Damien Miller95def091999-11-25 00:26:21 +1100688 /* Try to get Xauthority information for the display. */
689 snprintf(line, sizeof line, "%.100s list %.200s 2>/dev/null",
690 XAUTH_PATH, getenv("DISPLAY"));
691 f = popen(line, "r");
692 if (f && fgets(line, sizeof(line), f) &&
693 sscanf(line, "%*s %s %s", proto, data) == 2)
694 got_data = 1;
695 if (f)
696 pclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000697#endif /* XAUTH_PATH */
Damien Miller5428f641999-11-25 11:54:57 +1100698 /*
699 * If we didn't get authentication data, just make up some
700 * data. The forwarding code will check the validity of the
701 * response anyway, and substitute this data. The X11
702 * server, however, will ignore this fake data and use
703 * whatever authentication mechanisms it was using otherwise
704 * for the local connection.
705 */
Damien Miller95def091999-11-25 00:26:21 +1100706 if (!got_data) {
707 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000708
Damien Miller95def091999-11-25 00:26:21 +1100709 strlcpy(proto, "MIT-MAGIC-COOKIE-1", sizeof proto);
710 for (i = 0; i < 16; i++) {
711 if (i % 4 == 0)
712 rand = arc4random();
713 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x", rand & 0xff);
714 rand >>= 8;
715 }
716 }
Damien Miller5428f641999-11-25 11:54:57 +1100717 /*
718 * Got local authentication reasonable information. Request
719 * forwarding with authentication spoofing.
720 */
Damien Miller95def091999-11-25 00:26:21 +1100721 debug("Requesting X11 forwarding with authentication spoofing.");
722 x11_request_forwarding_with_spoofing(proto, data);
723
724 /* Read response from the server. */
725 type = packet_read(&plen);
726 if (type == SSH_SMSG_SUCCESS) {
727 forwarded = 1;
728 interactive = 1;
729 } else if (type == SSH_SMSG_FAILURE)
730 log("Warning: Remote host denied X11 forwarding.");
731 else
732 packet_disconnect("Protocol error waiting for X11 forwarding");
733 }
734 /* Tell the packet module whether this is an interactive session. */
735 packet_set_interactive(interactive, options.keepalives);
736
737 /* Clear agent forwarding if we don\'t have an agent. */
738 authfd = ssh_get_authentication_socket();
739 if (authfd < 0)
740 options.forward_agent = 0;
741 else
742 ssh_close_authentication_socket(authfd);
743
744 /* Request authentication agent forwarding if appropriate. */
745 if (options.forward_agent) {
746 debug("Requesting authentication agent forwarding.");
747 auth_request_forwarding();
748
749 /* Read response from the server. */
750 type = packet_read(&plen);
751 packet_integrity_check(plen, 0, type);
752 if (type != SSH_SMSG_SUCCESS)
753 log("Warning: Remote host denied authentication agent forwarding.");
754 }
755 /* Initiate local TCP/IP port forwardings. */
756 for (i = 0; i < options.num_local_forwards; i++) {
757 debug("Connections to local port %d forwarded to remote address %.200s:%d",
758 options.local_forwards[i].port,
759 options.local_forwards[i].host,
760 options.local_forwards[i].host_port);
761 channel_request_local_forwarding(options.local_forwards[i].port,
762 options.local_forwards[i].host,
Damien Millera34a28b1999-12-14 10:47:15 +1100763 options.local_forwards[i].host_port,
764 options.gateway_ports);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000765 }
766
Damien Miller95def091999-11-25 00:26:21 +1100767 /* Initiate remote TCP/IP port forwardings. */
768 for (i = 0; i < options.num_remote_forwards; i++) {
769 debug("Connections to remote port %d forwarded to local address %.200s:%d",
770 options.remote_forwards[i].port,
771 options.remote_forwards[i].host,
772 options.remote_forwards[i].host_port);
773 channel_request_remote_forwarding(options.remote_forwards[i].port,
774 options.remote_forwards[i].host,
775 options.remote_forwards[i].host_port);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000776 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000777
Damien Miller5428f641999-11-25 11:54:57 +1100778 /* If requested, let ssh continue in the background. */
779 if (fork_after_authentication_flag)
780 if (daemon(1, 1) < 0)
781 fatal("daemon() failed: %.200s", strerror(errno));
782
783 /*
784 * If a command was specified on the command line, execute the
785 * command now. Otherwise request the server to start a shell.
786 */
Damien Miller95def091999-11-25 00:26:21 +1100787 if (buffer_len(&command) > 0) {
788 int len = buffer_len(&command);
789 if (len > 900)
790 len = 900;
791 debug("Sending command: %.*s", len, buffer_ptr(&command));
792 packet_start(SSH_CMSG_EXEC_CMD);
793 packet_put_string(buffer_ptr(&command), buffer_len(&command));
794 packet_send();
795 packet_write_wait();
796 } else {
797 debug("Requesting shell.");
798 packet_start(SSH_CMSG_EXEC_SHELL);
799 packet_send();
800 packet_write_wait();
801 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000802
Damien Miller95def091999-11-25 00:26:21 +1100803 /* Enter the interactive session. */
804 exit_status = client_loop(tty_flag, tty_flag ? options.escape_char : -1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000805
Damien Miller95def091999-11-25 00:26:21 +1100806 /* Close the connection to the remote host. */
807 packet_close();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000808
Damien Miller95def091999-11-25 00:26:21 +1100809 /* Exit with the status returned by the program on the remote side. */
810 exit(exit_status);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000811}