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