Cope with a (non-standard) ENAMETOOLONG return from gethostname(); glibc will
[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 #include <errno.h>
10
11 #include "putty.h"
12 #include "ssh.h"
13 #include "network.h"
14
15 void platform_get_x11_auth(struct X11Display *disp, const Config *cfg)
16 {
17 char *xauthfile;
18 int needs_free;
19
20 /*
21 * Upgrade an IP-style localhost display to a Unix-socket
22 * display.
23 */
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 }
31
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;
38
39 sfree(disp->hostname);
40 len = 128;
41 do {
42 len *= 2;
43 disp->hostname = snewn(len, char);
44 if ((gethostname(disp->hostname, len) < 0) &&
45 (errno != ENAMETOOLONG)) {
46 disp->hostname = NULL;
47 return;
48 }
49 } while (strlen(disp->hostname) >= len-1);
50 }
51
52 /*
53 * Find the .Xauthority file.
54 */
55 needs_free = FALSE;
56 xauthfile = getenv("XAUTHORITY");
57 if (!xauthfile) {
58 xauthfile = getenv("HOME");
59 if (xauthfile) {
60 xauthfile = dupcat(xauthfile, "/.Xauthority", NULL);
61 needs_free = TRUE;
62 }
63 }
64
65 if (xauthfile) {
66 x11_get_auth_from_authfile(disp, xauthfile);
67 if (needs_free)
68 sfree(xauthfile);
69 }
70 }
71
72 const int platform_uses_x11_unix_by_default = TRUE;