]> andersk Git - openssh.git/blame - ssh.c
Small cleanup of PAM code
[openssh.git] / ssh.c
CommitLineData
8efc0c15 1/*
5260325f 2 * 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 */
8efc0c15 12
13#include "includes.h"
14RCSID("$Id$");
15
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
f601d847 24#ifdef HAVE___PROGNAME
25extern char *__progname;
26#else /* HAVE___PROGNAME */
27const char *__progname = "ssh";
28#endif /* HAVE___PROGNAME */
29
5260325f 30/* Flag indicating whether debug mode is on. This can be set on the command line. */
8efc0c15 31int debug_flag = 0;
32
8efc0c15 33int tty_flag = 0;
34
aa3378df 35/*
36 * Flag indicating that nothing should be read from stdin. This can be set
37 * on the command line.
38 */
8efc0c15 39int stdin_null_flag = 0;
40
aa3378df 41/*
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 */
8efc0c15 46int fork_after_authentication_flag = 0;
47
aa3378df 48/*
49 * General data structure for command line options and options configurable
50 * in configuration files. See readconf.h.
51 */
8efc0c15 52Options options;
53
aa3378df 54/*
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 */
8efc0c15 59char *host;
60
61/* socket address the host resolves to */
62struct sockaddr_in hostaddr;
63
aa3378df 64/*
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 */
8efc0c15 70volatile 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{
5260325f 89 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");
8efc0c15 94#ifdef AFS
5260325f 95 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");
106
107 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);
8efc0c15 119}
120
5260325f 121/*
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 */
8efc0c15 125void
5260325f 126rsh_connect(char *host, char *user, Buffer * command)
8efc0c15 127{
5260325f 128 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;
140 }
141 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");
8efc0c15 153 }
5260325f 154 execv(_PATH_RSH, args);
155 perror(_PATH_RSH);
156 exit(1);
8efc0c15 157}
158
5260325f 159/*
160 * Main program for the ssh client.
161 */
8efc0c15 162int
163main(int ac, char **av)
164{
5260325f 165 int i, opt, optind, type, exit_status, ok, fwd_port, fwd_host_port,
166 authfd;
167 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;
175
aa3378df 176 /*
177 * Save the original real uid. It will be needed later (uid-swapping
178 * may clobber the real uid).
179 */
5260325f 180 original_real_uid = getuid();
181 original_effective_uid = geteuid();
182
183 /* 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));
8efc0c15 189 }
aa3378df 190 /*
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 */
5260325f 197 temporarily_use_uid(original_real_uid);
198
aa3378df 199 /*
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 */
5260325f 205 umask(022);
206
207 /* Save our own name. */
208 av0 = av[0];
209
210 /* Initialize option structure to indicate that no values have been set. */
211 initialize_options(&options);
212
213 /* Parse command-line arguments. */
214 host = NULL;
215
216 /* 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;
224
225 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;
236 }
237 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;
256
257 case 'f':
258 fork_after_authentication_flag = 1;
259 stdin_null_flag = 1;
260 break;
261
262 case 'x':
263 options.forward_x11 = 0;
264 break;
265
266 case 'X':
267 options.forward_x11 = 1;
268 break;
269
270 case 'g':
271 options.gateway_ports = 1;
272 break;
273
274 case 'P':
275 options.use_privileged_port = 0;
276 break;
277
278 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;
299
300 case 't':
301 tty_flag = 1;
302 break;
303
304 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);
343 if (options.port < 1 || options.port > 65535) {
344 fprintf(stderr, "Bad port %s.\n", optarg);
345 exit(1);
346 }
347 break;
348
349 case 'l':
350 options.user = optarg;
351 break;
352
353 case 'R':
354 if (sscanf(optarg, "%d:%255[^:]:%d", &fwd_port, buf,
355 &fwd_host_port) != 3) {
356 fprintf(stderr, "Bad forwarding specification '%s'.\n", optarg);
357 usage();
358 /* NOTREACHED */
359 }
360 add_remote_forward(&options, fwd_port, buf, fwd_host_port);
361 break;
362
363 case 'L':
364 if (sscanf(optarg, "%d:%255[^:]:%d", &fwd_port, buf,
365 &fwd_host_port) != 3) {
366 fprintf(stderr, "Bad forwarding specification '%s'.\n", optarg);
367 usage();
368 /* NOTREACHED */
369 }
370 add_local_forward(&options, fwd_port, buf, fwd_host_port);
371 break;
372
373 case 'C':
374 options.compression = 1;
375 break;
376
377 case 'o':
378 dummy = 1;
379 if (process_config_line(&options, host ? host : "", optarg,
380 "command-line", 0, &dummy) != 0)
381 exit(1);
382 break;
383
384 default:
385 usage();
386 }
387 }
388
389 /* Check that we got a host name. */
390 if (!host)
8efc0c15 391 usage();
5260325f 392
393 /* check if RSA support exists */
394 if (rsa_alive() == 0) {
395 fprintf(stderr,
396 "%s: no RSA support in libssl and libcrypto. See ssl(8).\n",
397 __progname);
398 exit(1);
8efc0c15 399 }
5260325f 400 /* Initialize the command to execute on remote host. */
401 buffer_init(&command);
402
aa3378df 403 /*
404 * Save the command to execute on the remote host in a buffer. There
405 * is no limit on the length of the command, except by the maximum
406 * packet size. Also sets the tty flag if there is no command.
407 */
5260325f 408 if (optind == ac) {
409 /* No command specified - execute shell on a tty. */
410 tty_flag = 1;
411 } else {
412 /* A command has been specified. Store it into the
413 buffer. */
414 for (i = optind; i < ac; i++) {
415 if (i > optind)
416 buffer_append(&command, " ", 1);
417 buffer_append(&command, av[i], strlen(av[i]));
418 }
8efc0c15 419 }
5260325f 420
421 /* Cannot fork to background if no command. */
422 if (fork_after_authentication_flag && buffer_len(&command) == 0)
423 fatal("Cannot fork into background without a command to execute.");
424
425 /* Allocate a tty by default if no command specified. */
426 if (buffer_len(&command) == 0)
427 tty_flag = 1;
428
429 /* Do not allocate a tty if stdin is not a tty. */
430 if (!isatty(fileno(stdin))) {
431 if (tty_flag)
432 fprintf(stderr, "Pseudo-terminal will not be allocated because stdin is not a terminal.\n");
433 tty_flag = 0;
434 }
435 /* Get user data. */
436 pw = getpwuid(original_real_uid);
437 if (!pw) {
438 fprintf(stderr, "You don't exist, go away!\n");
439 exit(1);
440 }
441 /* Take a copy of the returned structure. */
442 memset(&pwcopy, 0, sizeof(pwcopy));
443 pwcopy.pw_name = xstrdup(pw->pw_name);
444 pwcopy.pw_passwd = xstrdup(pw->pw_passwd);
445 pwcopy.pw_uid = pw->pw_uid;
446 pwcopy.pw_gid = pw->pw_gid;
447 pwcopy.pw_dir = xstrdup(pw->pw_dir);
448 pwcopy.pw_shell = xstrdup(pw->pw_shell);
449 pw = &pwcopy;
450
451 /* Initialize "log" output. Since we are the client all output
452 actually goes to the terminal. */
453 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 0);
454
455 /* Read per-user configuration file. */
456 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, SSH_USER_CONFFILE);
457 read_config_file(buf, host, &options);
458
459 /* Read systemwide configuration file. */
460 read_config_file(HOST_CONFIG_FILE, host, &options);
461
462 /* Fill configuration defaults. */
463 fill_default_options(&options);
464
465 /* reinit */
466 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 0);
467
468 if (options.user == NULL)
469 options.user = xstrdup(pw->pw_name);
470
471 if (options.hostname != NULL)
472 host = options.hostname;
473
474 /* Find canonic host name. */
475 if (strchr(host, '.') == 0) {
476 struct hostent *hp = gethostbyname(host);
477 if (hp != 0) {
478 if (strchr(hp->h_name, '.') != 0)
479 host = xstrdup(hp->h_name);
480 else if (hp->h_aliases != 0
481 && hp->h_aliases[0] != 0
482 && strchr(hp->h_aliases[0], '.') != 0)
483 host = xstrdup(hp->h_aliases[0]);
8efc0c15 484 }
8efc0c15 485 }
5260325f 486 /* Disable rhosts authentication if not running as root. */
487 if (original_effective_uid != 0 || !options.use_privileged_port) {
488 options.rhosts_authentication = 0;
489 options.rhosts_rsa_authentication = 0;
490 }
aa3378df 491 /*
492 * If using rsh has been selected, exec it now (without trying
493 * anything else). Note that we must release privileges first.
494 */
5260325f 495 if (options.use_rsh) {
aa3378df 496 /*
497 * Restore our superuser privileges. This must be done
498 * before permanently setting the uid.
499 */
5260325f 500 restore_uid();
501
502 /* Switch to the original uid permanently. */
503 permanently_set_uid(original_real_uid);
504
505 /* Execute rsh. */
506 rsh_connect(host, options.user, &command);
507 fatal("rsh_connect returned");
8efc0c15 508 }
5260325f 509 /* Restore our superuser privileges. */
510 restore_uid();
511
aa3378df 512 /*
513 * Open a connection to the remote host. This needs root privileges
514 * if rhosts_{rsa_}authentication is enabled.
515 */
5260325f 516
517 ok = ssh_connect(host, &hostaddr, options.port,
518 options.connection_attempts,
519 !options.rhosts_authentication &&
520 !options.rhosts_rsa_authentication,
521 original_real_uid,
522 options.proxy_command);
523
aa3378df 524 /*
525 * If we successfully made the connection, load the host private key
526 * in case we will need it later for combined rsa-rhosts
527 * authentication. This must be done before releasing extra
528 * privileges, because the file is only readable by root.
529 */
5260325f 530 if (ok) {
531 host_private_key = RSA_new();
532 if (load_private_key(HOST_KEY_FILE, "", host_private_key, NULL))
533 host_private_key_loaded = 1;
534 }
aa3378df 535 /*
536 * Get rid of any extra privileges that we may have. We will no
537 * longer need them. Also, extra privileges could make it very hard
538 * to read identity files and other non-world-readable files from the
539 * user's home directory if it happens to be on a NFS volume where
540 * root is mapped to nobody.
541 */
542
543 /*
544 * Note that some legacy systems need to postpone the following call
545 * to permanently_set_uid() until the private hostkey is destroyed
546 * with RSA_free(). Otherwise the calling user could ptrace() the
547 * process, read the private hostkey and impersonate the host.
548 * OpenBSD does not allow ptracing of setuid processes.
549 */
5260325f 550 permanently_set_uid(original_real_uid);
551
aa3378df 552 /*
553 * Now that we are back to our own permissions, create ~/.ssh
554 * directory if it doesn\'t already exist.
555 */
5260325f 556 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, SSH_USER_DIR);
557 if (stat(buf, &st) < 0)
558 if (mkdir(buf, 0755) < 0)
559 error("Could not create directory '%.200s'.", buf);
560
561 /* Check if the connection failed, and try "rsh" if appropriate. */
562 if (!ok) {
563 if (options.port != 0)
564 log("Secure connection to %.100s on port %d refused%.100s.",
565 host, options.port,
566 options.fallback_to_rsh ? "; reverting to insecure method" : "");
567 else
568 log("Secure connection to %.100s refused%.100s.", host,
569 options.fallback_to_rsh ? "; reverting to insecure method" : "");
570
571 if (options.fallback_to_rsh) {
572 rsh_connect(host, options.user, &command);
573 fatal("rsh_connect returned");
574 }
575 exit(1);
8efc0c15 576 }
5260325f 577 /* Expand ~ in options.identity_files. */
578 for (i = 0; i < options.num_identity_files; i++)
579 options.identity_files[i] =
580 tilde_expand_filename(options.identity_files[i], original_real_uid);
581
582 /* Expand ~ in known host file names. */
583 options.system_hostfile = tilde_expand_filename(options.system_hostfile,
584 original_real_uid);
585 options.user_hostfile = tilde_expand_filename(options.user_hostfile,
586 original_real_uid);
587
588 /* Log into the remote system. This never returns if the login fails. */
589 ssh_login(host_private_key_loaded, host_private_key,
590 host, &hostaddr, original_real_uid);
591
592 /* We no longer need the host private key. Clear it now. */
593 if (host_private_key_loaded)
594 RSA_free(host_private_key); /* Destroys contents safely */
595
596 /* Close connection cleanly after attack. */
597 cipher_attack_detected = packet_disconnect;
598
5260325f 599 /* Enable compression if requested. */
600 if (options.compression) {
601 debug("Requesting compression at level %d.", options.compression_level);
602
603 if (options.compression_level < 1 || options.compression_level > 9)
604 fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
605
606 /* Send the request. */
607 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
608 packet_put_int(options.compression_level);
609 packet_send();
610 packet_write_wait();
611 type = packet_read(&plen);
612 if (type == SSH_SMSG_SUCCESS)
613 packet_start_compression(options.compression_level);
614 else if (type == SSH_SMSG_FAILURE)
615 log("Warning: Remote host refused compression.");
616 else
617 packet_disconnect("Protocol error waiting for compression response.");
618 }
619 /* Allocate a pseudo tty if appropriate. */
620 if (tty_flag) {
621 debug("Requesting pty.");
622
623 /* Start the packet. */
624 packet_start(SSH_CMSG_REQUEST_PTY);
625
626 /* Store TERM in the packet. There is no limit on the
627 length of the string. */
628 cp = getenv("TERM");
629 if (!cp)
630 cp = "";
631 packet_put_string(cp, strlen(cp));
632
633 /* Store window size in the packet. */
634 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
635 memset(&ws, 0, sizeof(ws));
636 packet_put_int(ws.ws_row);
637 packet_put_int(ws.ws_col);
638 packet_put_int(ws.ws_xpixel);
639 packet_put_int(ws.ws_ypixel);
640
641 /* Store tty modes in the packet. */
642 tty_make_modes(fileno(stdin));
643
644 /* Send the packet, and wait for it to leave. */
645 packet_send();
646 packet_write_wait();
647
648 /* Read response from the server. */
649 type = packet_read(&plen);
650 if (type == SSH_SMSG_SUCCESS)
651 interactive = 1;
652 else if (type == SSH_SMSG_FAILURE)
653 log("Warning: Remote host failed or refused to allocate a pseudo tty.");
654 else
655 packet_disconnect("Protocol error waiting for pty request response.");
656 }
657 /* Request X11 forwarding if enabled and DISPLAY is set. */
658 if (options.forward_x11 && getenv("DISPLAY") != NULL) {
659 char line[512], proto[512], data[512];
660 FILE *f;
661 int forwarded = 0, got_data = 0, i;
8efc0c15 662
663#ifdef XAUTH_PATH
5260325f 664 /* Try to get Xauthority information for the display. */
665 snprintf(line, sizeof line, "%.100s list %.200s 2>/dev/null",
666 XAUTH_PATH, getenv("DISPLAY"));
667 f = popen(line, "r");
668 if (f && fgets(line, sizeof(line), f) &&
669 sscanf(line, "%*s %s %s", proto, data) == 2)
670 got_data = 1;
671 if (f)
672 pclose(f);
8efc0c15 673#endif /* XAUTH_PATH */
aa3378df 674 /*
675 * If we didn't get authentication data, just make up some
676 * data. The forwarding code will check the validity of the
677 * response anyway, and substitute this data. The X11
678 * server, however, will ignore this fake data and use
679 * whatever authentication mechanisms it was using otherwise
680 * for the local connection.
681 */
5260325f 682 if (!got_data) {
683 u_int32_t rand = 0;
684
685 strlcpy(proto, "MIT-MAGIC-COOKIE-1", sizeof proto);
686 for (i = 0; i < 16; i++) {
687 if (i % 4 == 0)
688 rand = arc4random();
689 snprintf(data + 2 * i, sizeof data - 2 * i, "%02x", rand & 0xff);
690 rand >>= 8;
691 }
692 }
aa3378df 693 /*
694 * Got local authentication reasonable information. Request
695 * forwarding with authentication spoofing.
696 */
5260325f 697 debug("Requesting X11 forwarding with authentication spoofing.");
698 x11_request_forwarding_with_spoofing(proto, data);
699
700 /* Read response from the server. */
701 type = packet_read(&plen);
702 if (type == SSH_SMSG_SUCCESS) {
703 forwarded = 1;
704 interactive = 1;
705 } else if (type == SSH_SMSG_FAILURE)
706 log("Warning: Remote host denied X11 forwarding.");
707 else
708 packet_disconnect("Protocol error waiting for X11 forwarding");
709 }
710 /* Tell the packet module whether this is an interactive session. */
711 packet_set_interactive(interactive, options.keepalives);
712
713 /* Clear agent forwarding if we don\'t have an agent. */
714 authfd = ssh_get_authentication_socket();
715 if (authfd < 0)
716 options.forward_agent = 0;
717 else
718 ssh_close_authentication_socket(authfd);
719
720 /* Request authentication agent forwarding if appropriate. */
721 if (options.forward_agent) {
722 debug("Requesting authentication agent forwarding.");
723 auth_request_forwarding();
724
725 /* Read response from the server. */
726 type = packet_read(&plen);
727 packet_integrity_check(plen, 0, type);
728 if (type != SSH_SMSG_SUCCESS)
729 log("Warning: Remote host denied authentication agent forwarding.");
730 }
731 /* Initiate local TCP/IP port forwardings. */
732 for (i = 0; i < options.num_local_forwards; i++) {
733 debug("Connections to local port %d forwarded to remote address %.200s:%d",
734 options.local_forwards[i].port,
735 options.local_forwards[i].host,
736 options.local_forwards[i].host_port);
737 channel_request_local_forwarding(options.local_forwards[i].port,
738 options.local_forwards[i].host,
739 options.local_forwards[i].host_port);
8efc0c15 740 }
741
5260325f 742 /* Initiate remote TCP/IP port forwardings. */
743 for (i = 0; i < options.num_remote_forwards; i++) {
744 debug("Connections to remote port %d forwarded to local address %.200s:%d",
745 options.remote_forwards[i].port,
746 options.remote_forwards[i].host,
747 options.remote_forwards[i].host_port);
748 channel_request_remote_forwarding(options.remote_forwards[i].port,
749 options.remote_forwards[i].host,
750 options.remote_forwards[i].host_port);
8efc0c15 751 }
5260325f 752
aa3378df 753 /* If requested, let ssh continue in the background. */
754 if (fork_after_authentication_flag)
755 if (daemon(1, 1) < 0)
756 fatal("daemon() failed: %.200s", strerror(errno));
757
758 /*
759 * If a command was specified on the command line, execute the
760 * command now. Otherwise request the server to start a shell.
761 */
5260325f 762 if (buffer_len(&command) > 0) {
763 int len = buffer_len(&command);
764 if (len > 900)
765 len = 900;
766 debug("Sending command: %.*s", len, buffer_ptr(&command));
767 packet_start(SSH_CMSG_EXEC_CMD);
768 packet_put_string(buffer_ptr(&command), buffer_len(&command));
769 packet_send();
770 packet_write_wait();
771 } else {
772 debug("Requesting shell.");
773 packet_start(SSH_CMSG_EXEC_SHELL);
774 packet_send();
775 packet_write_wait();
776 }
777
778 /* Enter the interactive session. */
779 exit_status = client_loop(tty_flag, tty_flag ? options.escape_char : -1);
780
781 /* Close the connection to the remote host. */
782 packet_close();
783
784 /* Exit with the status returned by the program on the remote side. */
785 exit(exit_status);
8efc0c15 786}
This page took 0.163016 seconds and 5 git commands to generate.