blob: 447878f298023c4755613294f584a5ddeeb26cd4 [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 Millera34a28b1999-12-14 10:47:15 +110014RCSID("$Id: ssh.c,v 1.14 1999/12/13 23:47:16 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 Miller95def091999-11-25 00:26:21 +110030/* Flag indicating whether debug mode is on. This can be set on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100031int debug_flag = 0;
32
Damien Millerd4a8b7e1999-10-27 13:42:43 +100033int tty_flag = 0;
34
Damien Miller5428f641999-11-25 11:54:57 +110035/*
36 * Flag indicating that nothing should be read from stdin. This can be set
37 * on the command line.
38 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039int stdin_null_flag = 0;
40
Damien Miller5428f641999-11-25 11:54:57 +110041/*
42 * Flag indicating that ssh should fork after authentication. This is useful
43 * so that the pasphrase can be entered manually, and then ssh goes to the
44 * background.
45 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100046int fork_after_authentication_flag = 0;
47
Damien Miller5428f641999-11-25 11:54:57 +110048/*
49 * General data structure for command line options and options configurable
50 * in configuration files. See readconf.h.
51 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052Options options;
53
Damien Miller5428f641999-11-25 11:54:57 +110054/*
55 * Name of the host we are connecting to. This is the name given on the
56 * command line, or the HostName specified for the user-supplied name in a
57 * configuration file.
58 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059char *host;
60
61/* socket address the host resolves to */
62struct sockaddr_in hostaddr;
63
Damien Miller5428f641999-11-25 11:54:57 +110064/*
65 * Flag to indicate that we have received a window change signal which has
66 * not yet been processed. This will cause a message indicating the new
67 * window size to be sent to the server a little later. This is volatile
68 * because this is updated in a signal handler.
69 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070volatile int received_window_change_signal = 0;
71
72/* Value of argv[0] (set in the main program). */
73char *av0;
74
75/* Flag indicating whether we have a valid host private key loaded. */
76int host_private_key_loaded = 0;
77
78/* Host private key. */
79RSA *host_private_key = NULL;
80
81/* Original real UID. */
82uid_t original_real_uid;
83
84/* Prints a help message to the user. This function never returns. */
85
86void
87usage()
88{
Damien Miller95def091999-11-25 00:26:21 +110089 fprintf(stderr, "Usage: %s [options] host [command]\n", av0);
90 fprintf(stderr, "Options:\n");
91 fprintf(stderr, " -l user Log in using this user name.\n");
92 fprintf(stderr, " -n Redirect input from /dev/null.\n");
93 fprintf(stderr, " -a Disable authentication agent forwarding.\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094#ifdef AFS
Damien Miller95def091999-11-25 00:26:21 +110095 fprintf(stderr, " -k Disable Kerberos ticket and AFS token forwarding.\n");
96#endif /* AFS */
97 fprintf(stderr, " -x Disable X11 connection forwarding.\n");
98 fprintf(stderr, " -i file Identity for RSA authentication (default: ~/.ssh/identity).\n");
99 fprintf(stderr, " -t Tty; allocate a tty even if command is given.\n");
100 fprintf(stderr, " -v Verbose; display verbose debugging messages.\n");
101 fprintf(stderr, " -V Display version number only.\n");
102 fprintf(stderr, " -P Don't allocate a privileged port.\n");
103 fprintf(stderr, " -q Quiet; don't display any warning messages.\n");
104 fprintf(stderr, " -f Fork into background after authentication.\n");
105 fprintf(stderr, " -e char Set escape character; ``none'' = disable (default: ~).\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106
Damien Miller95def091999-11-25 00:26:21 +1100107 fprintf(stderr, " -c cipher Select encryption algorithm: "
108 "``3des'', "
109 "``blowfish''\n");
110 fprintf(stderr, " -p port Connect to this port. Server must be on the same port.\n");
111 fprintf(stderr, " -L listen-port:host:port Forward local port to remote address\n");
112 fprintf(stderr, " -R listen-port:host:port Forward remote port to local address\n");
113 fprintf(stderr, " These cause %s to listen for connections on a port, and\n", av0);
114 fprintf(stderr, " forward them to the other side by connecting to host:port.\n");
115 fprintf(stderr, " -C Enable compression.\n");
116 fprintf(stderr, " -g Allow remote hosts to connect to forwarded ports.\n");
117 fprintf(stderr, " -o 'option' Process the option as if it was read from a configuration file.\n");
118 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119}
120
Damien Miller95def091999-11-25 00:26:21 +1100121/*
122 * Connects to the given host using rsh (or prints an error message and exits
123 * if rsh is not available). This function never returns.
124 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125void
Damien Miller95def091999-11-25 00:26:21 +1100126rsh_connect(char *host, char *user, Buffer * command)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127{
Damien Miller95def091999-11-25 00:26:21 +1100128 char *args[10];
129 int i;
130
131 log("Using rsh. WARNING: Connection will not be encrypted.");
132 /* Build argument list for rsh. */
133 i = 0;
134 args[i++] = _PATH_RSH;
135 /* host may have to come after user on some systems */
136 args[i++] = host;
137 if (user) {
138 args[i++] = "-l";
139 args[i++] = user;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140 }
Damien Miller95def091999-11-25 00:26:21 +1100141 if (buffer_len(command) > 0) {
142 buffer_append(command, "\0", 1);
143 args[i++] = buffer_ptr(command);
144 }
145 args[i++] = NULL;
146 if (debug_flag) {
147 for (i = 0; args[i]; i++) {
148 if (i != 0)
149 fprintf(stderr, " ");
150 fprintf(stderr, "%s", args[i]);
151 }
152 fprintf(stderr, "\n");
153 }
154 execv(_PATH_RSH, args);
155 perror(_PATH_RSH);
156 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000157}
158
Damien Miller95def091999-11-25 00:26:21 +1100159/*
160 * Main program for the ssh client.
161 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000162int
163main(int ac, char **av)
164{
Damien Milleraae6c611999-12-06 11:47:28 +1100165 int i, opt, optind, type, exit_status, ok, authfd;
166 u_short fwd_port, fwd_host_port;
Damien Miller95def091999-11-25 00:26:21 +1100167 char *optarg, *cp, buf[256];
168 Buffer command;
169 struct winsize ws;
170 struct stat st;
171 struct passwd *pw, pwcopy;
172 int interactive = 0, dummy;
173 uid_t original_effective_uid;
174 int plen;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000175
Damien Miller5428f641999-11-25 11:54:57 +1100176 /*
177 * Save the original real uid. It will be needed later (uid-swapping
178 * may clobber the real uid).
179 */
Damien Miller95def091999-11-25 00:26:21 +1100180 original_real_uid = getuid();
181 original_effective_uid = geteuid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000182
Damien Miller95def091999-11-25 00:26:21 +1100183 /* If we are installed setuid root be careful to not drop core. */
184 if (original_real_uid != original_effective_uid) {
185 struct rlimit rlim;
186 rlim.rlim_cur = rlim.rlim_max = 0;
187 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
188 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000189 }
Damien Miller5428f641999-11-25 11:54:57 +1100190 /*
191 * Use uid-swapping to give up root privileges for the duration of
192 * option processing. We will re-instantiate the rights when we are
193 * ready to create the privileged port, and will permanently drop
194 * them when the port has been created (actually, when the connection
195 * has been made, as we may need to create the port several times).
196 */
Damien Miller95def091999-11-25 00:26:21 +1100197 temporarily_use_uid(original_real_uid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000198
Damien Miller5428f641999-11-25 11:54:57 +1100199 /*
200 * Set our umask to something reasonable, as some files are created
201 * with the default umask. This will make them world-readable but
202 * writable only by the owner, which is ok for all files for which we
203 * don't set the modes explicitly.
204 */
Damien Miller95def091999-11-25 00:26:21 +1100205 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000206
Damien Miller95def091999-11-25 00:26:21 +1100207 /* Save our own name. */
208 av0 = av[0];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209
Damien Miller95def091999-11-25 00:26:21 +1100210 /* Initialize option structure to indicate that no values have been set. */
211 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000212
Damien Miller95def091999-11-25 00:26:21 +1100213 /* Parse command-line arguments. */
214 host = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215
Damien Miller95def091999-11-25 00:26:21 +1100216 /* If program name is not one of the standard names, use it as host name. */
217 if (strchr(av0, '/'))
218 cp = strrchr(av0, '/') + 1;
219 else
220 cp = av0;
221 if (strcmp(cp, "rsh") != 0 && strcmp(cp, "ssh") != 0 &&
222 strcmp(cp, "rlogin") != 0 && strcmp(cp, "slogin") != 0)
223 host = cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000224
Damien Miller95def091999-11-25 00:26:21 +1100225 for (optind = 1; optind < ac; optind++) {
226 if (av[optind][0] != '-') {
227 if (host)
228 break;
229 if ((cp = strchr(av[optind], '@'))) {
230 options.user = av[optind];
231 *cp = '\0';
232 host = ++cp;
233 } else
234 host = av[optind];
235 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000236 }
Damien Miller95def091999-11-25 00:26:21 +1100237 opt = av[optind][1];
238 if (!opt)
239 usage();
240 if (strchr("eilcpLRo", opt)) { /* options with arguments */
241 optarg = av[optind] + 2;
242 if (strcmp(optarg, "") == 0) {
243 if (optind >= ac - 1)
244 usage();
245 optarg = av[++optind];
246 }
247 } else {
248 if (av[optind][2])
249 usage();
250 optarg = NULL;
251 }
252 switch (opt) {
253 case 'n':
254 stdin_null_flag = 1;
255 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000256
Damien Miller95def091999-11-25 00:26:21 +1100257 case 'f':
258 fork_after_authentication_flag = 1;
259 stdin_null_flag = 1;
260 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000261
Damien Miller95def091999-11-25 00:26:21 +1100262 case 'x':
263 options.forward_x11 = 0;
264 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000265
Damien Miller95def091999-11-25 00:26:21 +1100266 case 'X':
267 options.forward_x11 = 1;
268 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000269
Damien Miller95def091999-11-25 00:26:21 +1100270 case 'g':
271 options.gateway_ports = 1;
272 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000273
Damien Miller95def091999-11-25 00:26:21 +1100274 case 'P':
275 options.use_privileged_port = 0;
276 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000277
Damien Miller95def091999-11-25 00:26:21 +1100278 case 'a':
279 options.forward_agent = 0;
280 break;
281#ifdef AFS
282 case 'k':
283 options.kerberos_tgt_passing = 0;
284 options.afs_token_passing = 0;
285 break;
286#endif
287 case 'i':
288 if (stat(optarg, &st) < 0) {
289 fprintf(stderr, "Warning: Identity file %s does not exist.\n",
290 optarg);
291 break;
292 }
293 if (options.num_identity_files >= SSH_MAX_IDENTITY_FILES)
294 fatal("Too many identity files specified (max %d)",
295 SSH_MAX_IDENTITY_FILES);
296 options.identity_files[options.num_identity_files++] =
297 xstrdup(optarg);
298 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000299
Damien Miller95def091999-11-25 00:26:21 +1100300 case 't':
301 tty_flag = 1;
302 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303
Damien Miller95def091999-11-25 00:26:21 +1100304 case 'v':
305 case 'V':
306 fprintf(stderr, "SSH Version %s, protocol version %d.%d.\n",
307 SSH_VERSION, PROTOCOL_MAJOR, PROTOCOL_MINOR);
308 fprintf(stderr, "Compiled with SSL.\n");
309 if (opt == 'V')
310 exit(0);
311 debug_flag = 1;
312 options.log_level = SYSLOG_LEVEL_DEBUG;
313 break;
314
315 case 'q':
316 options.log_level = SYSLOG_LEVEL_QUIET;
317 break;
318
319 case 'e':
320 if (optarg[0] == '^' && optarg[2] == 0 &&
321 (unsigned char) optarg[1] >= 64 && (unsigned char) optarg[1] < 128)
322 options.escape_char = (unsigned char) optarg[1] & 31;
323 else if (strlen(optarg) == 1)
324 options.escape_char = (unsigned char) optarg[0];
325 else if (strcmp(optarg, "none") == 0)
326 options.escape_char = -2;
327 else {
328 fprintf(stderr, "Bad escape character '%s'.\n", optarg);
329 exit(1);
330 }
331 break;
332
333 case 'c':
334 options.cipher = cipher_number(optarg);
335 if (options.cipher == -1) {
336 fprintf(stderr, "Unknown cipher type '%s'\n", optarg);
337 exit(1);
338 }
339 break;
340
341 case 'p':
342 options.port = atoi(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100343 break;
344
345 case 'l':
346 options.user = optarg;
347 break;
348
349 case 'R':
Damien Milleraae6c611999-12-06 11:47:28 +1100350 if (sscanf(optarg, "%hu:%255[^:]:%hu", &fwd_port, buf,
Damien Miller95def091999-11-25 00:26:21 +1100351 &fwd_host_port) != 3) {
352 fprintf(stderr, "Bad forwarding specification '%s'.\n", optarg);
353 usage();
354 /* NOTREACHED */
355 }
356 add_remote_forward(&options, fwd_port, buf, fwd_host_port);
357 break;
358
359 case 'L':
Damien Milleraae6c611999-12-06 11:47:28 +1100360 if (sscanf(optarg, "%hu:%255[^:]:%hu", &fwd_port, buf,
Damien Miller95def091999-11-25 00:26:21 +1100361 &fwd_host_port) != 3) {
362 fprintf(stderr, "Bad forwarding specification '%s'.\n", optarg);
363 usage();
364 /* NOTREACHED */
365 }
366 add_local_forward(&options, fwd_port, buf, fwd_host_port);
367 break;
368
369 case 'C':
370 options.compression = 1;
371 break;
372
373 case 'o':
374 dummy = 1;
375 if (process_config_line(&options, host ? host : "", optarg,
376 "command-line", 0, &dummy) != 0)
377 exit(1);
378 break;
379
380 default:
381 usage();
382 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000383 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000384
Damien Miller95def091999-11-25 00:26:21 +1100385 /* Check that we got a host name. */
386 if (!host)
387 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000388
Damien Miller95def091999-11-25 00:26:21 +1100389 /* check if RSA support exists */
390 if (rsa_alive() == 0) {
391 fprintf(stderr,
392 "%s: no RSA support in libssl and libcrypto. See ssl(8).\n",
393 __progname);
394 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000395 }
Damien Miller95def091999-11-25 00:26:21 +1100396 /* Initialize the command to execute on remote host. */
397 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000398
Damien Miller5428f641999-11-25 11:54:57 +1100399 /*
400 * Save the command to execute on the remote host in a buffer. There
401 * is no limit on the length of the command, except by the maximum
402 * packet size. Also sets the tty flag if there is no command.
403 */
Damien Miller95def091999-11-25 00:26:21 +1100404 if (optind == ac) {
405 /* No command specified - execute shell on a tty. */
406 tty_flag = 1;
407 } else {
408 /* A command has been specified. Store it into the
409 buffer. */
410 for (i = optind; i < ac; i++) {
411 if (i > optind)
412 buffer_append(&command, " ", 1);
413 buffer_append(&command, av[i], strlen(av[i]));
414 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000415 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000416
Damien Miller95def091999-11-25 00:26:21 +1100417 /* Cannot fork to background if no command. */
418 if (fork_after_authentication_flag && buffer_len(&command) == 0)
419 fatal("Cannot fork into background without a command to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000420
Damien Miller95def091999-11-25 00:26:21 +1100421 /* Allocate a tty by default if no command specified. */
422 if (buffer_len(&command) == 0)
423 tty_flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000424
Damien Miller95def091999-11-25 00:26:21 +1100425 /* Do not allocate a tty if stdin is not a tty. */
426 if (!isatty(fileno(stdin))) {
427 if (tty_flag)
428 fprintf(stderr, "Pseudo-terminal will not be allocated because stdin is not a terminal.\n");
429 tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000430 }
Damien Miller95def091999-11-25 00:26:21 +1100431 /* Get user data. */
432 pw = getpwuid(original_real_uid);
433 if (!pw) {
434 fprintf(stderr, "You don't exist, go away!\n");
435 exit(1);
436 }
437 /* Take a copy of the returned structure. */
438 memset(&pwcopy, 0, sizeof(pwcopy));
439 pwcopy.pw_name = xstrdup(pw->pw_name);
440 pwcopy.pw_passwd = xstrdup(pw->pw_passwd);
441 pwcopy.pw_uid = pw->pw_uid;
442 pwcopy.pw_gid = pw->pw_gid;
443 pwcopy.pw_dir = xstrdup(pw->pw_dir);
444 pwcopy.pw_shell = xstrdup(pw->pw_shell);
445 pw = &pwcopy;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000446
Damien Miller95def091999-11-25 00:26:21 +1100447 /* Initialize "log" output. Since we are the client all output
448 actually goes to the terminal. */
449 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000450
Damien Miller95def091999-11-25 00:26:21 +1100451 /* Read per-user configuration file. */
452 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, SSH_USER_CONFFILE);
453 read_config_file(buf, host, &options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000454
Damien Miller95def091999-11-25 00:26:21 +1100455 /* Read systemwide configuration file. */
456 read_config_file(HOST_CONFIG_FILE, host, &options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000457
Damien Miller95def091999-11-25 00:26:21 +1100458 /* Fill configuration defaults. */
459 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000460
Damien Miller95def091999-11-25 00:26:21 +1100461 /* reinit */
462 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000463
Damien Miller95def091999-11-25 00:26:21 +1100464 if (options.user == NULL)
465 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000466
Damien Miller95def091999-11-25 00:26:21 +1100467 if (options.hostname != NULL)
468 host = options.hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000469
Damien Miller95def091999-11-25 00:26:21 +1100470 /* Find canonic host name. */
471 if (strchr(host, '.') == 0) {
472 struct hostent *hp = gethostbyname(host);
473 if (hp != 0) {
474 if (strchr(hp->h_name, '.') != 0)
475 host = xstrdup(hp->h_name);
476 else if (hp->h_aliases != 0
477 && hp->h_aliases[0] != 0
478 && strchr(hp->h_aliases[0], '.') != 0)
479 host = xstrdup(hp->h_aliases[0]);
480 }
481 }
482 /* Disable rhosts authentication if not running as root. */
483 if (original_effective_uid != 0 || !options.use_privileged_port) {
484 options.rhosts_authentication = 0;
485 options.rhosts_rsa_authentication = 0;
486 }
Damien Miller5428f641999-11-25 11:54:57 +1100487 /*
488 * If using rsh has been selected, exec it now (without trying
489 * anything else). Note that we must release privileges first.
490 */
Damien Miller95def091999-11-25 00:26:21 +1100491 if (options.use_rsh) {
Damien Miller5428f641999-11-25 11:54:57 +1100492 /*
493 * Restore our superuser privileges. This must be done
494 * before permanently setting the uid.
495 */
Damien Miller95def091999-11-25 00:26:21 +1100496 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000497
Damien Miller95def091999-11-25 00:26:21 +1100498 /* Switch to the original uid permanently. */
499 permanently_set_uid(original_real_uid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000500
Damien Miller95def091999-11-25 00:26:21 +1100501 /* Execute rsh. */
502 rsh_connect(host, options.user, &command);
503 fatal("rsh_connect returned");
504 }
505 /* Restore our superuser privileges. */
506 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000507
Damien Miller5428f641999-11-25 11:54:57 +1100508 /*
509 * Open a connection to the remote host. This needs root privileges
510 * if rhosts_{rsa_}authentication is enabled.
511 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000512
Damien Miller95def091999-11-25 00:26:21 +1100513 ok = ssh_connect(host, &hostaddr, options.port,
514 options.connection_attempts,
515 !options.rhosts_authentication &&
516 !options.rhosts_rsa_authentication,
517 original_real_uid,
518 options.proxy_command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000519
Damien Miller5428f641999-11-25 11:54:57 +1100520 /*
521 * If we successfully made the connection, load the host private key
522 * in case we will need it later for combined rsa-rhosts
523 * authentication. This must be done before releasing extra
524 * privileges, because the file is only readable by root.
525 */
Damien Miller95def091999-11-25 00:26:21 +1100526 if (ok) {
527 host_private_key = RSA_new();
528 if (load_private_key(HOST_KEY_FILE, "", host_private_key, NULL))
529 host_private_key_loaded = 1;
530 }
Damien Miller5428f641999-11-25 11:54:57 +1100531 /*
532 * Get rid of any extra privileges that we may have. We will no
533 * longer need them. Also, extra privileges could make it very hard
534 * to read identity files and other non-world-readable files from the
535 * user's home directory if it happens to be on a NFS volume where
536 * root is mapped to nobody.
537 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000538
Damien Miller5428f641999-11-25 11:54:57 +1100539 /*
540 * Note that some legacy systems need to postpone the following call
541 * to permanently_set_uid() until the private hostkey is destroyed
542 * with RSA_free(). Otherwise the calling user could ptrace() the
543 * process, read the private hostkey and impersonate the host.
544 * OpenBSD does not allow ptracing of setuid processes.
545 */
Damien Miller95def091999-11-25 00:26:21 +1100546 permanently_set_uid(original_real_uid);
547
Damien Miller5428f641999-11-25 11:54:57 +1100548 /*
549 * Now that we are back to our own permissions, create ~/.ssh
550 * directory if it doesn\'t already exist.
551 */
Damien Miller95def091999-11-25 00:26:21 +1100552 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, SSH_USER_DIR);
553 if (stat(buf, &st) < 0)
554 if (mkdir(buf, 0755) < 0)
555 error("Could not create directory '%.200s'.", buf);
556
557 /* Check if the connection failed, and try "rsh" if appropriate. */
558 if (!ok) {
559 if (options.port != 0)
Damien Milleraae6c611999-12-06 11:47:28 +1100560 log("Secure connection to %.100s on port %hu refused%.100s.",
Damien Miller95def091999-11-25 00:26:21 +1100561 host, options.port,
562 options.fallback_to_rsh ? "; reverting to insecure method" : "");
563 else
564 log("Secure connection to %.100s refused%.100s.", host,
565 options.fallback_to_rsh ? "; reverting to insecure method" : "");
566
567 if (options.fallback_to_rsh) {
568 rsh_connect(host, options.user, &command);
569 fatal("rsh_connect returned");
570 }
571 exit(1);
572 }
573 /* Expand ~ in options.identity_files. */
574 for (i = 0; i < options.num_identity_files; i++)
575 options.identity_files[i] =
576 tilde_expand_filename(options.identity_files[i], original_real_uid);
577
578 /* Expand ~ in known host file names. */
579 options.system_hostfile = tilde_expand_filename(options.system_hostfile,
580 original_real_uid);
581 options.user_hostfile = tilde_expand_filename(options.user_hostfile,
582 original_real_uid);
583
584 /* Log into the remote system. This never returns if the login fails. */
585 ssh_login(host_private_key_loaded, host_private_key,
586 host, &hostaddr, original_real_uid);
587
588 /* We no longer need the host private key. Clear it now. */
589 if (host_private_key_loaded)
590 RSA_free(host_private_key); /* Destroys contents safely */
591
592 /* Close connection cleanly after attack. */
593 cipher_attack_detected = packet_disconnect;
594
Damien Miller95def091999-11-25 00:26:21 +1100595 /* Enable compression if requested. */
596 if (options.compression) {
597 debug("Requesting compression at level %d.", options.compression_level);
598
599 if (options.compression_level < 1 || options.compression_level > 9)
600 fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
601
602 /* Send the request. */
603 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
604 packet_put_int(options.compression_level);
605 packet_send();
606 packet_write_wait();
607 type = packet_read(&plen);
608 if (type == SSH_SMSG_SUCCESS)
609 packet_start_compression(options.compression_level);
610 else if (type == SSH_SMSG_FAILURE)
611 log("Warning: Remote host refused compression.");
612 else
613 packet_disconnect("Protocol error waiting for compression response.");
614 }
615 /* Allocate a pseudo tty if appropriate. */
616 if (tty_flag) {
617 debug("Requesting pty.");
618
619 /* Start the packet. */
620 packet_start(SSH_CMSG_REQUEST_PTY);
621
622 /* Store TERM in the packet. There is no limit on the
623 length of the string. */
624 cp = getenv("TERM");
625 if (!cp)
626 cp = "";
627 packet_put_string(cp, strlen(cp));
628
629 /* Store window size in the packet. */
630 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
631 memset(&ws, 0, sizeof(ws));
632 packet_put_int(ws.ws_row);
633 packet_put_int(ws.ws_col);
634 packet_put_int(ws.ws_xpixel);
635 packet_put_int(ws.ws_ypixel);
636
637 /* Store tty modes in the packet. */
638 tty_make_modes(fileno(stdin));
639
640 /* Send the packet, and wait for it to leave. */
641 packet_send();
642 packet_write_wait();
643
644 /* Read response from the server. */
645 type = packet_read(&plen);
646 if (type == SSH_SMSG_SUCCESS)
647 interactive = 1;
648 else if (type == SSH_SMSG_FAILURE)
649 log("Warning: Remote host failed or refused to allocate a pseudo tty.");
650 else
651 packet_disconnect("Protocol error waiting for pty request response.");
652 }
653 /* Request X11 forwarding if enabled and DISPLAY is set. */
654 if (options.forward_x11 && getenv("DISPLAY") != NULL) {
655 char line[512], proto[512], data[512];
656 FILE *f;
657 int forwarded = 0, got_data = 0, i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000658
659#ifdef XAUTH_PATH
Damien Miller95def091999-11-25 00:26:21 +1100660 /* Try to get Xauthority information for the display. */
661 snprintf(line, sizeof line, "%.100s list %.200s 2>/dev/null",
662 XAUTH_PATH, getenv("DISPLAY"));
663 f = popen(line, "r");
664 if (f && fgets(line, sizeof(line), f) &&
665 sscanf(line, "%*s %s %s", proto, data) == 2)
666 got_data = 1;
667 if (f)
668 pclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000669#endif /* XAUTH_PATH */
Damien Miller5428f641999-11-25 11:54:57 +1100670 /*
671 * If we didn't get authentication data, just make up some
672 * data. The forwarding code will check the validity of the
673 * response anyway, and substitute this data. The X11
674 * server, however, will ignore this fake data and use
675 * whatever authentication mechanisms it was using otherwise
676 * for the local connection.
677 */
Damien Miller95def091999-11-25 00:26:21 +1100678 if (!got_data) {
679 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000680
Damien Miller95def091999-11-25 00:26:21 +1100681 strlcpy(proto, "MIT-MAGIC-COOKIE-1", sizeof proto);
682 for (i = 0; i < 16; i++) {
683 if (i % 4 == 0)
684 rand = arc4random();
685 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x", rand & 0xff);
686 rand >>= 8;
687 }
688 }
Damien Miller5428f641999-11-25 11:54:57 +1100689 /*
690 * Got local authentication reasonable information. Request
691 * forwarding with authentication spoofing.
692 */
Damien Miller95def091999-11-25 00:26:21 +1100693 debug("Requesting X11 forwarding with authentication spoofing.");
694 x11_request_forwarding_with_spoofing(proto, data);
695
696 /* Read response from the server. */
697 type = packet_read(&plen);
698 if (type == SSH_SMSG_SUCCESS) {
699 forwarded = 1;
700 interactive = 1;
701 } else if (type == SSH_SMSG_FAILURE)
702 log("Warning: Remote host denied X11 forwarding.");
703 else
704 packet_disconnect("Protocol error waiting for X11 forwarding");
705 }
706 /* Tell the packet module whether this is an interactive session. */
707 packet_set_interactive(interactive, options.keepalives);
708
709 /* Clear agent forwarding if we don\'t have an agent. */
710 authfd = ssh_get_authentication_socket();
711 if (authfd < 0)
712 options.forward_agent = 0;
713 else
714 ssh_close_authentication_socket(authfd);
715
716 /* Request authentication agent forwarding if appropriate. */
717 if (options.forward_agent) {
718 debug("Requesting authentication agent forwarding.");
719 auth_request_forwarding();
720
721 /* Read response from the server. */
722 type = packet_read(&plen);
723 packet_integrity_check(plen, 0, type);
724 if (type != SSH_SMSG_SUCCESS)
725 log("Warning: Remote host denied authentication agent forwarding.");
726 }
727 /* Initiate local TCP/IP port forwardings. */
728 for (i = 0; i < options.num_local_forwards; i++) {
729 debug("Connections to local port %d forwarded to remote address %.200s:%d",
730 options.local_forwards[i].port,
731 options.local_forwards[i].host,
732 options.local_forwards[i].host_port);
733 channel_request_local_forwarding(options.local_forwards[i].port,
734 options.local_forwards[i].host,
Damien Millera34a28b1999-12-14 10:47:15 +1100735 options.local_forwards[i].host_port,
736 options.gateway_ports);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000737 }
738
Damien Miller95def091999-11-25 00:26:21 +1100739 /* Initiate remote TCP/IP port forwardings. */
740 for (i = 0; i < options.num_remote_forwards; i++) {
741 debug("Connections to remote port %d forwarded to local address %.200s:%d",
742 options.remote_forwards[i].port,
743 options.remote_forwards[i].host,
744 options.remote_forwards[i].host_port);
745 channel_request_remote_forwarding(options.remote_forwards[i].port,
746 options.remote_forwards[i].host,
747 options.remote_forwards[i].host_port);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000748 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000749
Damien Miller5428f641999-11-25 11:54:57 +1100750 /* If requested, let ssh continue in the background. */
751 if (fork_after_authentication_flag)
752 if (daemon(1, 1) < 0)
753 fatal("daemon() failed: %.200s", strerror(errno));
754
755 /*
756 * If a command was specified on the command line, execute the
757 * command now. Otherwise request the server to start a shell.
758 */
Damien Miller95def091999-11-25 00:26:21 +1100759 if (buffer_len(&command) > 0) {
760 int len = buffer_len(&command);
761 if (len > 900)
762 len = 900;
763 debug("Sending command: %.*s", len, buffer_ptr(&command));
764 packet_start(SSH_CMSG_EXEC_CMD);
765 packet_put_string(buffer_ptr(&command), buffer_len(&command));
766 packet_send();
767 packet_write_wait();
768 } else {
769 debug("Requesting shell.");
770 packet_start(SSH_CMSG_EXEC_SHELL);
771 packet_send();
772 packet_write_wait();
773 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000774
Damien Miller95def091999-11-25 00:26:21 +1100775 /* Enter the interactive session. */
776 exit_status = client_loop(tty_flag, tty_flag ? options.escape_char : -1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000777
Damien Miller95def091999-11-25 00:26:21 +1100778 /* Close the connection to the remote host. */
779 packet_close();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000780
Damien Miller95def091999-11-25 00:26:21 +1100781 /* Exit with the status returned by the program on the remote side. */
782 exit(exit_status);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000783}