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