]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - unix/ux_x11.c
...and fix an unlikely memory leak.
[PuTTY.git] / 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         disp->hostname = NULL;
41         len = 128;
42         do {
43             len *= 2;
44             disp->hostname = sresize(disp->hostname, len, char);
45             if ((gethostname(disp->hostname, len) < 0) &&
46                 (errno != ENAMETOOLONG)) {
47                 sfree(disp->hostname);
48                 disp->hostname = NULL;
49                 return;
50             }
51         } while (strlen(disp->hostname) >= len-1);
52     }
53
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     }
66
67     if (xauthfile) {
68         x11_get_auth_from_authfile(disp, xauthfile);
69         if (needs_free)
70             sfree(xauthfile);
71     }
72 }
73
74 const int platform_uses_x11_unix_by_default = TRUE;