2 * ux_x11.c: fetch local auth data for X forwarding.
9 void platform_get_x11_auth(char *display, int *protocol,
10 unsigned char *data, int *datalen)
14 int maxsize = *datalen;
18 * Normally we should run `xauth list DISPLAYNAME'. However,
19 * there's an oddity when the display is local: the display
20 * `localhost:0' (or `:0') should become just `:0'.
22 if (!strncmp(display, "localhost:", 10))
23 command = dupprintf("xauth list %s 2>/dev/null", display+9);
25 command = dupprintf("xauth list %s 2>/dev/null", display);
26 fp = popen(command, "r");
30 return; /* assume no auth */
32 localbuf = smalloc(maxsize);
36 * Read a line from stdin, and attempt to parse it into a
37 * display name (ignored), auth protocol, and auth string.
39 int c, i, hexdigit, proto;
42 /* Skip the display name. */
43 while (c = getc(fp), c != EOF && c != '\n' && !isspace(c));
45 if (c == '\n') continue;
47 /* Skip white space. */
48 while (c != EOF && c != '\n' && isspace(c))
51 if (c == '\n') continue;
53 /* Read the auth protocol name, and see if it matches any we
56 while (c != EOF && c != '\n' && !isspace(c)) {
57 if (i < lenof(protoname)-1) protoname[i++] = c;
62 for (i = X11_NO_AUTH; ++i < X11_NAUTHS ;) {
63 if (!strcmp(protoname, x11_authnames[i]))
66 if (i >= X11_NAUTHS || i <= proto) {
67 /* Unrecognised protocol name, or a worse one than we already have.
69 while (c != EOF && c != '\n')
75 /* Skip white space. */
76 while (c != EOF && c != '\n' && isspace(c))
79 if (c == '\n') continue;
82 * Now grab pairs of hex digits and shove them into `data'.
86 while (c != EOF && c != '\n') {
88 if (c >= 'A' && c <= 'F')
89 hexval = c + 10 - 'A';
90 if (c >= 'a' && c <= 'f')
91 hexval = c + 10 - 'a';
92 if (c >= '0' && c <= '9')
96 hexdigit = (hexdigit << 4) + hexval;
98 localbuf[i++] = hexdigit;
108 memcpy(data, localbuf, i);
110 /* Nonetheless, continue looping round; we might find a better one. */