Annotation of src/usr.bin/ssh/ssh.c, Revision 1.10
1.1 deraadt 1: /*
2:
3: ssh.c
4:
5: Author: Tatu Ylonen <ylo@cs.hut.fi>
6:
7: Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8: All rights reserved
9:
10: Created: Sat Mar 18 16:36:11 1995 ylo
11:
12: Ssh client program. This program can be used to log into a remote machine.
13: The software supports strong authentication, encryption, and forwarding
14: of X11, TCP/IP, and authentication connections.
15:
1.2 provos 16: Modified to work with SSL by Niels Provos <provos@citi.umich.edu> in Canada.
17:
1.1 deraadt 18: */
19:
20: #include "includes.h"
1.10 ! dugsong 21: RCSID("$Id: ssh.c,v 1.9 1999/09/29 18:16:20 dugsong Exp $");
1.1 deraadt 22:
23: #include "xmalloc.h"
24: #include "ssh.h"
25: #include "packet.h"
26: #include "buffer.h"
27: #include "authfd.h"
28: #include "readconf.h"
29: #include "uidswap.h"
30:
31: /* Flag indicating whether debug mode is on. This can be set on the
32: command line. */
33: int debug_flag = 0;
34:
35: /* Flag indicating whether quiet mode is on. */
36: int quiet_flag = 0;
37:
38: /* Flag indicating whether to allocate a pseudo tty. This can be set on the
39: command line, and is automatically set if no command is given on the command
40: line. */
41: int tty_flag = 0;
42:
43: /* Flag indicating that nothing should be read from stdin. This can be set
44: on the command line. */
45: int stdin_null_flag = 0;
46:
47: /* Flag indicating that ssh should fork after authentication. This is useful
48: so that the pasphrase can be entered manually, and then ssh goes to the
49: background. */
50: int fork_after_authentication_flag = 0;
51:
52: /* General data structure for command line options and options configurable
53: in configuration files. See readconf.h. */
54: Options options;
55:
56: /* Name of the host we are connecting to. This is the name given on the
57: command line, or the HostName specified for the user-supplied name
58: in a configuration file. */
59: char *host;
60:
61: #ifdef SIGWINCH
62: /* Flag to indicate that we have received a window change signal which has
63: not yet been processed. This will cause a message indicating the new
64: window size to be sent to the server a little later. This is volatile
65: because this is updated in a signal handler. */
66: volatile int received_window_change_signal = 0;
67: #endif /* SIGWINCH */
68:
69: /* Value of argv[0] (set in the main program). */
70: char *av0;
71:
72: /* Flag indicating whether we have a valid host private key loaded. */
73: int host_private_key_loaded = 0;
74:
75: /* Host private key. */
1.2 provos 76: RSA *host_private_key = NULL;
1.1 deraadt 77:
1.10 ! dugsong 78: /* Original real UID. */
! 79: uid_t original_real_uid;
1.1 deraadt 80:
81: /* Prints a help message to the user. This function never returns. */
82:
1.2 provos 83: void
84: usage()
1.1 deraadt 85: {
86: int i;
87:
88: fprintf(stderr, "Usage: %s [options] host [command]\n", av0);
89: fprintf(stderr, "Options:\n");
90: fprintf(stderr, " -l user Log in using this user name.\n");
91: fprintf(stderr, " -n Redirect input from /dev/null.\n");
1.7 provos 92: fprintf(stderr, " -a Disable authentication agent forwarding.\n");
1.9 dugsong 93: #ifdef AFS
94: fprintf(stderr, " -k Disable Kerberos ticket and AFS token forwarding.\n");
95: #endif /* AFS */
1.1 deraadt 96: fprintf(stderr, " -x Disable X11 connection forwarding.\n");
97: fprintf(stderr, " -i file Identity for RSA authentication (default: ~/.ssh/identity).\n");
98: fprintf(stderr, " -t Tty; allocate a tty even if command is given.\n");
99: fprintf(stderr, " -v Verbose; display verbose debugging messages.\n");
1.8 provos 100: fprintf(stderr, " -V Display version number only.\n");
1.1 deraadt 101: fprintf(stderr, " -q Quiet; don't display any warning messages.\n");
102: fprintf(stderr, " -f Fork into background after authentication.\n");
103: fprintf(stderr, " -e char Set escape character; ``none'' = disable (default: ~).\n");
104:
105: fprintf(stderr, " -c cipher Select encryption algorithm: ");
106: for (i = 1; i <= 6; i++)
107: {
108: const char *t = cipher_name(i);
109: if (t[0] == 'n' && t[1] == 'o' && t[2] == ' ')
110: continue;
111: fprintf(stderr, "%s, ", t);
112: }
113: fprintf(stderr, "or none.\n");
114:
115: fprintf(stderr, " -p port Connect to this port. Server must be on the same port.\n");
1.4 deraadt 116: fprintf(stderr, " -g Allow remote hosts to connect to forwarded ports.\n");
1.1 deraadt 117: fprintf(stderr, " -L listen-port:host:port Forward local port to remote address\n");
118: fprintf(stderr, " -R listen-port:host:port Forward remote port to local address\n");
119: fprintf(stderr, " These cause %s to listen for connections on a port, and\n", av0);
120: fprintf(stderr, " forward them to the other side by connecting to host:port.\n");
121: #ifdef WITH_ZLIB
122: fprintf(stderr, " -C Enable compression.\n");
123: #endif /* WITH_ZLIB */
124: fprintf(stderr, " -o 'option' Process the option as if it was read from a configuration file.\n");
125: exit(1);
126: }
127:
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:
1.2 provos 131: void
132: rsh_connect(char *host, char *user, Buffer *command)
1.1 deraadt 133: {
134: #ifdef RSH_PATH
135: char *args[10];
136: int i;
137:
138: log("Using rsh. WARNING: Connection will not be encrypted.");
139: /* Build argument list for rsh. */
140: i = 0;
141: args[i++] = RSH_PATH;
142: args[i++] = host; /* may have to come after user on some systems */
143: if (user)
144: {
145: args[i++] = "-l";
146: args[i++] = user;
147: }
148: if (buffer_len(command) > 0)
149: {
150: buffer_append(command, "\0", 1);
151: args[i++] = buffer_ptr(command);
152: }
153: args[i++] = NULL;
154: if (debug_flag)
155: {
156: for (i = 0; args[i]; i++)
157: {
158: if (i != 0)
159: fprintf(stderr, " ");
160: fprintf(stderr, "%s", args[i]);
161: }
162: fprintf(stderr, "\n");
163: }
164: execv(RSH_PATH, args);
165: perror(RSH_PATH);
166: exit(1);
167: #else /* RSH_PATH */
168: fatal("Rsh not available.");
169: #endif /* RSH_PATH */
170: }
171:
172: /* Main program for the ssh client. */
173:
1.2 provos 174: int
175: main(int ac, char **av)
1.1 deraadt 176: {
177: int i, opt, optind, type, exit_status, ok, fwd_port, fwd_host_port, authfd;
178: char *optarg, *cp, buf[256];
179: Buffer command;
180: struct winsize ws;
181: struct stat st;
182: struct passwd *pw, pwcopy;
183: int interactive = 0, dummy;
184: uid_t original_effective_uid;
185: int plen;
186:
187: /* Save the original real uid. It will be needed later (uid-swapping may
188: clobber the real uid). */
189: original_real_uid = getuid();
190: original_effective_uid = geteuid();
191:
192: /* If we are installed setuid root be careful to not drop core. */
193: if (original_real_uid != original_effective_uid)
194: {
195: #ifdef HAVE_SETRLIMIT
196: struct rlimit rlim;
197: rlim.rlim_cur = rlim.rlim_max = 0;
198: if (setrlimit(RLIMIT_CORE, &rlim) < 0)
199: fatal("setrlimit failed: %.100s", strerror(errno));
200: #else
201: fatal("ssh is installed setuid root.\n");
202: #endif
203: }
204:
205: /* Use uid-swapping to give up root privileges for the duration of option
206: processing. We will re-instantiate the rights when we are ready to
207: create the privileged port, and will permanently drop them when the
208: port has been created (actually, when the connection has been made, as
209: we may need to create the port several times). */
210: temporarily_use_uid(original_real_uid);
211:
212: #ifdef HAVE_UMASK
213: /* Set our umask to something reasonable, as some files are created with
214: the default umask. This will make them world-readable but writable
215: only by the owner, which is ok for all files for which we don't set
216: the modes explicitly. */
217: umask(022);
218: #endif /* HAVE_UMASK */
219:
220: /* Save our own name. */
221: av0 = av[0];
222:
223: #ifdef SOCKS
224: /* Initialize SOCKS (the firewall traversal library). */
225: SOCKSinit(av0);
226: #endif /* SOCKS */
227:
228: /* Initialize option structure to indicate that no values have been set. */
229: initialize_options(&options);
230:
231: /* Parse command-line arguments. */
232: host = NULL;
233:
234: /* If program name is not one of the standard names, use it as host name. */
235: if (strchr(av0, '/'))
236: cp = strrchr(av0, '/') + 1;
237: else
238: cp = av0;
239: if (strcmp(cp, "rsh") != 0 && strcmp(cp, "ssh") != 0 &&
240: strcmp(cp, "rlogin") != 0 && strcmp(cp, "slogin") != 0)
241: host = cp;
242:
243: for (optind = 1; optind < ac; optind++)
244: {
245: if (av[optind][0] != '-')
246: {
247: if (host)
248: break;
1.3 deraadt 249: if ((cp = strchr(av[optind], '@'))) {
250: options.user = av[optind];
251: *cp = '\0';
252: host = ++cp;
253: }
254: else
255: host = av[optind];
1.1 deraadt 256: continue;
257: }
258: opt = av[optind][1];
259: if (!opt)
260: usage();
261: if (strchr("eilcpLRo", opt)) /* options with arguments */
262: {
263: optarg = av[optind] + 2;
264: if (strcmp(optarg, "") == 0)
265: {
266: if (optind >= ac - 1)
267: usage();
268: optarg = av[++optind];
269: }
270: }
271: else
272: {
273: if (av[optind][2])
274: usage();
275: optarg = NULL;
276: }
277: switch (opt)
278: {
279: case 'n':
280: stdin_null_flag = 1;
281: break;
282:
283: case 'f':
284: fork_after_authentication_flag = 1;
285: stdin_null_flag = 1;
286: break;
287:
288: case 'x':
289: options.forward_x11 = 0;
290: break;
291:
292: case 'X':
293: options.forward_x11 = 1;
1.4 deraadt 294: break;
295:
296: case 'g':
297: options.gateway_ports = 1;
1.1 deraadt 298: break;
299:
300: case 'a':
301: options.forward_agent = 0;
1.9 dugsong 302: break;
303: #ifdef AFS
304: case 'k':
1.1 deraadt 305: options.kerberos_tgt_passing = 0;
306: options.afs_token_passing = 0;
1.9 dugsong 307: break;
1.1 deraadt 308: #endif
309: case 'i':
310: if (stat(optarg, &st) < 0)
311: {
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;
322:
323: case 't':
324: tty_flag = 1;
325: break;
326:
327: case 'v':
1.6 deraadt 328: case 'V':
1.1 deraadt 329: debug_flag = 1;
330: fprintf(stderr, "SSH Version %s, protocol version %d.%d.\n",
331: SSH_VERSION, PROTOCOL_MAJOR, PROTOCOL_MINOR);
332: fprintf(stderr, "Compiled with SSL.\n");
1.6 deraadt 333: if (opt == 'V')
334: exit(0);
1.1 deraadt 335: break;
336:
337: case 'q':
338: quiet_flag = 1;
339: break;
340:
341: case 'e':
342: if (optarg[0] == '^' && optarg[2] == 0 &&
343: (unsigned char)optarg[1] >= 64 && (unsigned char)optarg[1] < 128)
344: options.escape_char = (unsigned char)optarg[1] & 31;
345: else
346: if (strlen(optarg) == 1)
347: options.escape_char = (unsigned char)optarg[0];
348: else
349: if (strcmp(optarg, "none") == 0)
350: options.escape_char = -2;
351: else
352: {
353: fprintf(stderr, "Bad escape character '%s'.\n", optarg);
354: exit(1);
355: }
356: break;
357:
358: case 'c':
359: options.cipher = cipher_number(optarg);
360: if (options.cipher == -1)
361: {
362: fprintf(stderr, "Unknown cipher type '%s'\n", optarg);
363: exit(1);
364: }
365: break;
366:
367: case 'p':
368: options.port = atoi(optarg);
369: if (options.port < 1 || options.port > 65535)
370: {
371: fprintf(stderr, "Bad port %s.\n", optarg);
372: exit(1);
373: }
374: break;
375:
376: case 'l':
377: options.user = optarg;
378: break;
379:
380: case 'R':
381: if (sscanf(optarg, "%d:%255[^:]:%d", &fwd_port, buf,
382: &fwd_host_port) != 3)
383: {
384: fprintf(stderr, "Bad forwarding specification '%s'.\n", optarg);
385: usage();
386: /*NOTREACHED*/
387: }
388: add_remote_forward(&options, fwd_port, buf, fwd_host_port);
389: break;
390:
391: case 'L':
392: if (sscanf(optarg, "%d:%255[^:]:%d", &fwd_port, buf,
393: &fwd_host_port) != 3)
394: {
395: fprintf(stderr, "Bad forwarding specification '%s'.\n", optarg);
396: usage();
397: /*NOTREACHED*/
398: }
399: if (fwd_port < 1024 && original_real_uid != 0)
400: {
401: fprintf(stderr,
402: "Privileged ports can only be forwarded by root.\n");
403: exit(1);
404: }
405: add_local_forward(&options, fwd_port, buf, fwd_host_port);
406: break;
407:
408: #ifdef WITH_ZLIB
409: case 'C':
410: options.compression = 1;
411: break;
412: #endif /* WITH_ZLIB */
413:
414: case 'o':
415: dummy = 1;
416: process_config_line(&options, host ? host : "", optarg,
417: "command-line", 0, &dummy);
418: break;
419:
420: default:
421: usage();
422: }
423: }
424:
425: /* Check that we got a host name. */
426: if (!host)
427: usage();
1.5 deraadt 428:
429: /* check if RSA support exists */
430: if (rsa_alive() == 0) {
431: extern char *__progname;
432:
433: fprintf(stderr,
434: "%s: no RSA support in libssl and libcrypto. See ssl(8).\n",
435: __progname);
436: exit(1);
437: }
1.1 deraadt 438:
439: /* Initialize the command to execute on remote host. */
440: buffer_init(&command);
441:
442: /* Save the command to execute on the remote host in a buffer. There is
443: no limit on the length of the command, except by the maximum packet
444: size. Also sets the tty flag if there is no command. */
445: if (optind == ac)
446: {
447: /* No command specified - execute shell on a tty. */
448: tty_flag = 1;
449: }
450: else
451: {
452: /* A command has been specified. Store it into the buffer. */
453: for (i = optind; i < ac; i++)
454: {
455: if (i > optind)
456: buffer_append(&command, " ", 1);
457: buffer_append(&command, av[i], strlen(av[i]));
458: }
459: }
460:
461: /* Cannot fork to background if no command. */
462: if (fork_after_authentication_flag && buffer_len(&command) == 0)
463: fatal("Cannot fork into background without a command to execute.");
464:
465: /* Allocate a tty by default if no command specified. */
466: if (buffer_len(&command) == 0)
467: tty_flag = 1;
468:
469: /* Do not allocate a tty if stdin is not a tty. */
470: if (!isatty(fileno(stdin)))
471: {
472: if (tty_flag)
473: fprintf(stderr, "Pseudo-terminal will not be allocated because stdin is not a terminal.\n");
474: tty_flag = 0;
475: }
476:
477: /* Get user data. */
478: pw = getpwuid(original_real_uid);
479: if (!pw)
480: {
481: fprintf(stderr, "You don't exist, go away!\n");
482: exit(1);
483: }
484:
485: /* Take a copy of the returned structure. */
486: memset(&pwcopy, 0, sizeof(pwcopy));
487: pwcopy.pw_name = xstrdup(pw->pw_name);
488: pwcopy.pw_passwd = xstrdup(pw->pw_passwd);
489: pwcopy.pw_uid = pw->pw_uid;
490: pwcopy.pw_gid = pw->pw_gid;
491: pwcopy.pw_dir = xstrdup(pw->pw_dir);
492: pwcopy.pw_shell = xstrdup(pw->pw_shell);
493: pw = &pwcopy;
494:
495: /* Initialize "log" output. Since we are the client all output actually
496: goes to the terminal. */
497: log_init(av[0], 1, debug_flag, quiet_flag, SYSLOG_FACILITY_USER);
498:
499: /* Read per-user configuration file. */
500: sprintf(buf, "%.100s/%.100s", pw->pw_dir, SSH_USER_CONFFILE);
501: read_config_file(buf, host, &options);
502:
503: /* Read systemwide configuration file. */
504: read_config_file(HOST_CONFIG_FILE, host, &options);
505:
506: /* Fill configuration defaults. */
507: fill_default_options(&options);
508: if (options.user == NULL)
509: options.user = xstrdup(pw->pw_name);
510:
511: if (options.hostname != NULL)
512: host = options.hostname;
513:
514: /* Find canonic host name. */
515: if (strchr(host, '.') == 0)
516: {
517: struct hostent *hp = gethostbyname(host);
518: if (hp != 0)
519: {
520: if (strchr(hp->h_name, '.') != 0)
521: host = xstrdup(hp->h_name);
522: else if (hp->h_aliases != 0
523: && hp->h_aliases[0] != 0
524: && strchr(hp->h_aliases[0], '.') != 0)
525: host = xstrdup(hp->h_aliases[0]);
526: }
527: }
528:
529: /* Disable rhosts authentication if not running as root. */
530: if (original_effective_uid != 0)
531: {
532: options.rhosts_authentication = 0;
533: options.rhosts_rsa_authentication = 0;
534: }
535:
536: /* If using rsh has been selected, exec it now (without trying anything
537: else). Note that we must release privileges first. */
538: if (options.use_rsh)
539: {
540: /* Restore our superuser privileges. This must be done before
541: permanently setting the uid. */
542: restore_uid();
543:
544: /* Switch to the original uid permanently. */
545: permanently_set_uid(original_real_uid);
546:
547: /* Execute rsh. */
548: rsh_connect(host, options.user, &command);
549: fatal("rsh_connect returned");
550: }
551:
552: /* Restore our superuser privileges. */
553: restore_uid();
554:
555: /* Open a connection to the remote host. This needs root privileges if
1.2 provos 556: rhosts_authentication is true. */
1.1 deraadt 557: ok = ssh_connect(host, options.port, options.connection_attempts,
558: !options.rhosts_authentication &&
559: !options.rhosts_rsa_authentication,
1.2 provos 560: original_real_uid, options.proxy_command);
1.1 deraadt 561:
562: /* If we successfully made the connection, load the host private key in
563: case we will need it later for combined rsa-rhosts authentication.
564: This must be done before releasing extra privileges, because the file
565: is only readable by root. */
566: if (ok)
567: {
1.2 provos 568: host_private_key = RSA_new();
569: if (load_private_key(HOST_KEY_FILE, "", host_private_key, NULL))
1.1 deraadt 570: host_private_key_loaded = 1;
571: }
572:
573: /* Get rid of any extra privileges that we may have. We will no longer need
574: them. Also, extra privileges could make it very hard to read identity
575: files and other non-world-readable files from the user's home directory
576: if it happens to be on a NFS volume where root is mapped to nobody. */
577: permanently_set_uid(original_real_uid);
578:
579: /* Now that we are back to our own permissions, create ~/.ssh directory
580: if it doesn\'t already exist. */
581: sprintf(buf, "%.100s/%.100s", pw->pw_dir, SSH_USER_DIR);
582: if (stat(buf, &st) < 0)
583: if (mkdir(buf, 0755) < 0)
584: error("Could not create directory '%.200s'.", buf);
585:
586: /* Check if the connection failed, and try "rsh" if appropriate. */
587: if (!ok)
588: {
589: if (options.port != 0)
590: log("Secure connection to %.100s on port %d refused%.100s.",
591: host, options.port,
592: options.fallback_to_rsh ? "; reverting to insecure method" : "");
593: else
594: log("Secure connection to %.100s refused%.100s.", host,
595: options.fallback_to_rsh ? "; reverting to insecure method" : "");
596:
597: if (options.fallback_to_rsh)
598: {
599: rsh_connect(host, options.user, &command);
600: fatal("rsh_connect returned");
601: }
602: exit(1);
603: }
604:
605: /* Expand ~ in options.identity_files. */
606: for (i = 0; i < options.num_identity_files; i++)
607: options.identity_files[i] =
608: tilde_expand_filename(options.identity_files[i], original_real_uid);
609:
610: /* Expand ~ in known host file names. */
611: options.system_hostfile = tilde_expand_filename(options.system_hostfile,
612: original_real_uid);
613: options.user_hostfile = tilde_expand_filename(options.user_hostfile,
614: original_real_uid);
615:
1.2 provos 616: /* Log into the remote system. This never returns if the login fails. */
617: ssh_login(host_private_key_loaded, host_private_key,
1.1 deraadt 618: host, &options, original_real_uid);
619:
620: /* We no longer need the host private key. Clear it now. */
621: if (host_private_key_loaded)
1.2 provos 622: RSA_free(host_private_key); /* Destroys contents safely */
1.1 deraadt 623:
624: /* Close connection cleanly after attack. */
625: cipher_attack_detected = packet_disconnect;
626:
627: /* If requested, fork and let ssh continue in the background. */
628: if (fork_after_authentication_flag)
629: {
630: int ret = fork();
631: if (ret == -1)
632: fatal("fork failed: %.100s", strerror(errno));
633: if (ret != 0)
634: exit(0);
635: #ifdef HAVE_SETSID
636: setsid();
637: #endif /* HAVE_SETSID */
638: }
639:
640: #ifdef WITH_ZLIB
641: /* Enable compression if requested. */
642: if (options.compression)
643: {
644: debug("Requesting compression at level %d.", options.compression_level);
645:
646: if (options.compression_level < 1 || options.compression_level > 9)
647: fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
648:
649: /* Send the request. */
650: packet_start(SSH_CMSG_REQUEST_COMPRESSION);
651: packet_put_int(options.compression_level);
652: packet_send();
653: packet_write_wait();
654: type = packet_read(&plen);
655: if (type == SSH_SMSG_SUCCESS)
656: packet_start_compression(options.compression_level);
657: else if (type == SSH_SMSG_FAILURE)
658: log("Warning: Remote host refused compression.");
659: else
660: packet_disconnect("Protocol error waiting for compression response.");
661: }
662: #endif /* WITH_ZLIB */
663:
664: /* Allocate a pseudo tty if appropriate. */
665: if (tty_flag)
666: {
667: debug("Requesting pty.");
668:
669: /* Start the packet. */
670: packet_start(SSH_CMSG_REQUEST_PTY);
671:
672: /* Store TERM in the packet. There is no limit on the length of the
673: string. */
674: cp = getenv("TERM");
675: if (!cp)
676: cp = "";
677: packet_put_string(cp, strlen(cp));
678:
679: /* Store window size in the packet. */
680: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
681: memset(&ws, 0, sizeof(ws));
682: packet_put_int(ws.ws_row);
683: packet_put_int(ws.ws_col);
684: packet_put_int(ws.ws_xpixel);
685: packet_put_int(ws.ws_ypixel);
686:
687: /* Store tty modes in the packet. */
688: tty_make_modes(fileno(stdin));
689:
690: /* Send the packet, and wait for it to leave. */
691: packet_send();
692: packet_write_wait();
693:
694: /* Read response from the server. */
695: type = packet_read(&plen);
696: if (type == SSH_SMSG_SUCCESS)
697: interactive = 1;
698: else if (type == SSH_SMSG_FAILURE)
699: log("Warning: Remote host failed or refused to allocate a pseudo tty.");
700: else
701: packet_disconnect("Protocol error waiting for pty request response.");
702: }
703:
704: /* Request X11 forwarding if enabled and DISPLAY is set. */
705: if (options.forward_x11 && getenv("DISPLAY") != NULL)
706: {
707: char line[512], proto[512], data[512];
708: FILE *f;
709: int forwarded = 0, got_data = 0, i;
710:
711: #ifdef XAUTH_PATH
712: /* Try to get Xauthority information for the display. */
713: sprintf(line, "%.100s list %.200s 2>/dev/null",
714: XAUTH_PATH, getenv("DISPLAY"));
715: f = popen(line, "r");
716: if (f && fgets(line, sizeof(line), f) &&
717: sscanf(line, "%*s %s %s", proto, data) == 2)
718: got_data = 1;
719: if (f)
720: pclose(f);
721: #endif /* XAUTH_PATH */
722: /* If we didn't get authentication data, just make up some data. The
723: forwarding code will check the validity of the response anyway, and
724: substitute this data. The X11 server, however, will ignore this
725: fake data and use whatever authentication mechanisms it was using
726: otherwise for the local connection. */
727: if (!got_data)
728: {
1.9 dugsong 729: u_int32_t rand = 0;
1.2 provos 730:
1.1 deraadt 731: strcpy(proto, "MIT-MAGIC-COOKIE-1");
1.2 provos 732: for (i = 0; i < 16; i++) {
733: if (i % 4 == 0)
734: rand = arc4random();
735: sprintf(data + 2 * i, "%02x", rand & 0xff);
736: rand >>= 8;
737: }
1.1 deraadt 738: }
739:
740: /* Got local authentication reasonable information. Request forwarding
741: with authentication spoofing. */
742: debug("Requesting X11 forwarding with authentication spoofing.");
1.2 provos 743: x11_request_forwarding_with_spoofing(proto, data);
1.1 deraadt 744:
745: /* Read response from the server. */
746: type = packet_read(&plen);
747: if (type == SSH_SMSG_SUCCESS)
748: {
749: forwarded = 1;
750: interactive = 1;
751: }
752: else if (type == SSH_SMSG_FAILURE)
753: log("Warning: Remote host denied X11 forwarding.");
754: else
755: packet_disconnect("Protocol error waiting for X11 forwarding");
756: }
757:
758: /* Tell the packet module whether this is an interactive session. */
759: packet_set_interactive(interactive, options.keepalives);
760:
761: /* Clear agent forwarding if we don\'t have an agent. */
762: authfd = ssh_get_authentication_fd();
763: if (authfd < 0)
764: options.forward_agent = 0;
765: else
766: ssh_close_authentication_socket(authfd);
767:
768: /* Request authentication agent forwarding if appropriate. */
769: if (options.forward_agent)
770: {
771: debug("Requesting authentication agent forwarding.");
772: auth_request_forwarding();
773:
774: /* Read response from the server. */
775: type = packet_read(&plen);
776: packet_integrity_check(plen, 0, type);
777: if (type != SSH_SMSG_SUCCESS)
778: log("Warning: Remote host denied authentication agent forwarding.");
779: }
780:
781: /* Initiate local TCP/IP port forwardings. */
782: for (i = 0; i < options.num_local_forwards; i++)
783: {
784: debug("Connections to local port %d forwarded to remote address %.200s:%d",
785: options.local_forwards[i].port, options.local_forwards[i].host,
786: options.local_forwards[i].host_port);
787: channel_request_local_forwarding(options.local_forwards[i].port,
788: options.local_forwards[i].host,
789: options.local_forwards[i].host_port);
790: }
791:
792: /* Initiate remote TCP/IP port forwardings. */
793: for (i = 0; i < options.num_remote_forwards; i++)
794: {
795: debug("Connections to remote port %d forwarded to local address %.200s:%d",
796: options.remote_forwards[i].port, options.remote_forwards[i].host,
797: options.remote_forwards[i].host_port);
798: channel_request_remote_forwarding(options.remote_forwards[i].port,
799: options.remote_forwards[i].host,
800: options.remote_forwards[i].host_port);
801: }
802:
803: /* If a command was specified on the command line, execute the command now.
804: Otherwise request the server to start a shell. */
805: if (buffer_len(&command) > 0)
806: {
807: int len = buffer_len(&command);
808: if (len > 900)
809: len = 900;
810: debug("Sending command: %.*s", len, buffer_ptr(&command));
811: packet_start(SSH_CMSG_EXEC_CMD);
812: packet_put_string(buffer_ptr(&command), buffer_len(&command));
813: packet_send();
814: packet_write_wait();
815: }
816: else
817: {
818: debug("Requesting shell.");
819: packet_start(SSH_CMSG_EXEC_SHELL);
820: packet_send();
821: packet_write_wait();
822: }
823:
824: /* Enter the interactive session. */
825: exit_status = client_loop(tty_flag, tty_flag ? options.escape_char : -1);
826:
827: /* Close the connection to the remote host. */
828: packet_close();
829:
830: /* Exit with the status returned by the program on the remote side. */
831: exit(exit_status);
832: }