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