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
;
19 * Normally we should run `xauth list DISPLAYNAME'. However,
20 * there's an oddity when the display is local: the display
21 * `localhost:0' (or `:0') should become just `:0'.
23 if (!strncmp(display
, "localhost:", 10))
24 command
= dupprintf("xauth list %s 2>/dev/null", display
+9);
26 command
= dupprintf("xauth list %s 2>/dev/null", display
);
27 fp
= popen(command
, "r");
31 return; /* assume no auth */
33 localbuf
= snewn(maxsize
, char);
37 * Read a line from stdin, and attempt to parse it into a
38 * display name (ignored), auth protocol, and auth string.
43 /* Skip the display name. */
44 while (c
= getc(fp
), c
!= EOF
&& c
!= '\n' && !isspace(c
));
46 if (c
== '\n') continue;
48 /* Skip white space. */
49 while (c
!= EOF
&& c
!= '\n' && isspace(c
))
52 if (c
== '\n') continue;
54 /* Read the auth protocol name, and see if it matches any we
57 while (c
!= EOF
&& c
!= '\n' && !isspace(c
)) {
58 if (i
< lenof(protoname
)-1) protoname
[i
++] = c
;
63 for (i
= X11_NO_AUTH
; ++i
< X11_NAUTHS
;) {
64 if (!strcmp(protoname
, x11_authnames
[i
]))
67 if (i
>= X11_NAUTHS
|| i
<= proto
) {
68 /* Unrecognised protocol name, or a worse one than we already have.
70 while (c
!= EOF
&& c
!= '\n')
76 /* Skip white space. */
77 while (c
!= EOF
&& c
!= '\n' && isspace(c
))
80 if (c
== '\n') continue;
83 * Now grab pairs of hex digits and shove them into `data'.
87 while (c
!= EOF
&& c
!= '\n') {
89 if (c
>= 'A' && c
<= 'F')
90 hexval
= c
+ 10 - 'A';
91 if (c
>= 'a' && c
<= 'f')
92 hexval
= c
+ 10 - 'a';
93 if (c
>= '0' && c
<= '9')
97 hexdigit
= (hexdigit
<< 4) + hexval
;
99 localbuf
[i
++] = hexdigit
;
109 memcpy(data
, localbuf
, i
);
111 /* Nonetheless, continue looping round; we might find a better one. */