Cope with a (non-standard) ENAMETOOLONG return from gethostname(); glibc will
[u/mdw/putty] / unix / ux_x11.c
CommitLineData
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 15void 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);
40 len = 128;
41 do {
42 len *= 2;
43 disp->hostname = snewn(len, char);
2726fc15 44 if ((gethostname(disp->hostname, len) < 0) &&
45 (errno != ENAMETOOLONG)) {
8def70c3 46 disp->hostname = NULL;
47 return;
48 }
49 } while (strlen(disp->hostname) >= len-1);
50 }
e0e7dff8 51
8def70c3 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 }
e0e7dff8 64
8def70c3 65 if (xauthfile) {
66 x11_get_auth_from_authfile(disp, xauthfile);
67 if (needs_free)
68 sfree(xauthfile);
e0e7dff8 69 }
e0e7dff8 70}
8def70c3 71
72const int platform_uses_x11_unix_by_default = TRUE;