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