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