Revamp of the local X11 connection code. We now parse X display
[u/mdw/putty] / unix / ux_x11.c
1 /*
2 * ux_x11.c: fetch local auth data for X forwarding.
3 */
4
5 #include <ctype.h>
6 #include <unistd.h>
7 #include <assert.h>
8 #include <stdlib.h>
9
10 #include "putty.h"
11 #include "ssh.h"
12 #include "network.h"
13
14 void platform_get_x11_auth(struct X11Display *disp, const Config *cfg)
15 {
16 char *xauthfile;
17 int needs_free;
18
19 /*
20 * Upgrade an IP-style localhost display to a Unix-socket
21 * display.
22 */
23 if (!disp->unixdomain && sk_address_is_local(disp->addr)) {
24 sk_addr_free(disp->addr);
25 disp->unixdomain = TRUE;
26 disp->addr = platform_get_x11_unix_address(NULL, disp->displaynum);
27 disp->realhost = dupprintf("unix:%d", disp->displaynum);
28 disp->port = 0;
29 }
30
31 /*
32 * Set the hostname for Unix-socket displays, so that we'll
33 * look it up correctly in the X authority file.
34 */
35 if (disp->unixdomain) {
36 int len;
37
38 sfree(disp->hostname);
39 len = 128;
40 do {
41 len *= 2;
42 disp->hostname = snewn(len, char);
43 if (gethostname(disp->hostname, len) < 0) {
44 disp->hostname = NULL;
45 return;
46 }
47 } while (strlen(disp->hostname) >= len-1);
48 }
49
50 /*
51 * Find the .Xauthority file.
52 */
53 needs_free = FALSE;
54 xauthfile = getenv("XAUTHORITY");
55 if (!xauthfile) {
56 xauthfile = getenv("HOME");
57 if (xauthfile) {
58 xauthfile = dupcat(xauthfile, "/.Xauthority", NULL);
59 needs_free = TRUE;
60 }
61 }
62
63 if (xauthfile) {
64 x11_get_auth_from_authfile(disp, xauthfile);
65 if (needs_free)
66 sfree(xauthfile);
67 }
68 }
69
70 const int platform_uses_x11_unix_by_default = TRUE;