2 * ux_x11.c: fetch local auth data for X forwarding.
11 void platform_get_x11_auth(char *display, int *protocol,
12 unsigned char *data, int *datalen)
16 int maxsize = *datalen;
20 display = x11_display(display);
22 * Normally we should run `xauth list DISPLAYNAME'. However,
23 * there's an oddity when the display is local: the display
24 * `localhost:0' (or `:0') should become just `:0'.
26 if (!strncmp(display, "localhost:", 10)
27 || !strncmp(display, "unix:", 5))
28 command = dupprintf("xauth list %s 2>/dev/null",
29 strchr(display, ':'));
31 command = dupprintf("xauth list %s 2>/dev/null", display);
33 fp = popen(command, "r");
37 return; /* assume no auth */
39 localbuf = snewn(maxsize, char);
43 * Read a line from stdin, and attempt to parse it into a
44 * display name (ignored), auth protocol, and auth string.
49 /* Skip the display name. */
50 while (c = getc(fp), c != EOF && c != '\n' && !isspace(c));
52 if (c == '\n') continue;
54 /* Skip white space. */
55 while (c != EOF && c != '\n' && isspace(c))
58 if (c == '\n') continue;
60 /* Read the auth protocol name, and see if it matches any we
63 while (c != EOF && c != '\n' && !isspace(c)) {
64 if (i < lenof(protoname)-1) protoname[i++] = c;
69 for (i = X11_NO_AUTH; ++i < X11_NAUTHS ;) {
70 if (!strcmp(protoname, x11_authnames[i]))
73 if (i >= X11_NAUTHS || i <= proto) {
74 /* Unrecognised protocol name, or a worse one than we already have.
76 while (c != EOF && c != '\n')
82 /* Skip white space. */
83 while (c != EOF && c != '\n' && isspace(c))
86 if (c == '\n') continue;
89 * Now grab pairs of hex digits and shove them into `data'.
93 while (c != EOF && c != '\n') {
95 if (c >= 'A' && c <= 'F')
96 hexval = c + 10 - 'A';
97 if (c >= 'a' && c <= 'f')
98 hexval = c + 10 - 'a';
99 if (c >= '0' && c <= '9')
103 hexdigit = (hexdigit << 4) + hexval;
105 localbuf[i++] = hexdigit;
115 memcpy(data, localbuf, i);
117 /* Nonetheless, continue looping round; we might find a better one. */