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