Welcome to mirror list, hosted at ThFree Co, Russian Federation.

x11disp.c - github.com/mRemoteNG/PuTTYNG.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1dbc9c07768b0813ea3d47c208d148c389aa424f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * Functions to manage an X11Display structure, by creating one from
 * an ordinary display name string, and freeing one.
 */

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>

#include "putty.h"
#include "ssh.h"
#include "ssh/channel.h"
#include "tree234.h"

struct X11Display *x11_setup_display(const char *display, Conf *conf,
                                     char **error_msg)
{
    struct X11Display *disp = snew(struct X11Display);
    char *localcopy;

    *error_msg = NULL;

    if (!display || !*display) {
        localcopy = platform_get_x_display();
        if (!localcopy || !*localcopy) {
            sfree(localcopy);
            localcopy = dupstr(":0");  /* plausible default for any platform */
        }
    } else
        localcopy = dupstr(display);

    /*
     * Parse the display name.
     *
     * We expect this to have one of the following forms:
     *
     *  - the standard X format which looks like
     *    [ [ protocol '/' ] host ] ':' displaynumber [ '.' screennumber ]
     *    (X11 also permits a double colon to indicate DECnet, but
     *    that's not our problem, thankfully!)
     *
     *  - only seen in the wild on MacOS (so far): a pathname to a
     *    Unix-domain socket, which will typically and confusingly
     *    end in ":0", and which I'm currently distinguishing from
     *    the standard scheme by noting that it starts with '/'.
     */
    if (localcopy[0] == '/') {
        disp->unixsocketpath = localcopy;
        disp->unixdomain = true;
        disp->hostname = NULL;
        disp->displaynum = -1;
        disp->screennum = 0;
        disp->addr = NULL;
    } else {
        char *colon, *dot, *slash;
        char *protocol, *hostname;

        colon = host_strrchr(localcopy, ':');
        if (!colon) {
            *error_msg = dupprintf("display name '%s' has no ':number'"
                                   " suffix", localcopy);

            sfree(disp);
            sfree(localcopy);
            return NULL;
        }

        *colon++ = '\0';
        dot = strchr(colon, '.');
        if (dot)
            *dot++ = '\0';

        disp->displaynum = atoi(colon);
        if (dot)
            disp->screennum = atoi(dot);
        else
            disp->screennum = 0;

        protocol = NULL;
        hostname = localcopy;
        if (colon > localcopy) {
            slash = strchr(localcopy, '/');
            if (slash) {
                *slash++ = '\0';
                protocol = localcopy;
                hostname = slash;
            }
        }

        disp->hostname = *hostname ? dupstr(hostname) : NULL;

        if (protocol)
            disp->unixdomain = (!strcmp(protocol, "local") ||
                                !strcmp(protocol, "unix"));
        else if (!*hostname || !strcmp(hostname, "unix"))
            disp->unixdomain = platform_uses_x11_unix_by_default;
        else
            disp->unixdomain = false;

        if (!disp->hostname && !disp->unixdomain)
            disp->hostname = dupstr("localhost");

        disp->unixsocketpath = NULL;
        disp->addr = NULL;

        sfree(localcopy);
    }

    /*
     * Look up the display hostname, if we need to.
     */
    if (!disp->unixdomain) {
        const char *err;

        disp->port = 6000 + disp->displaynum;
        disp->addr = name_lookup(disp->hostname, disp->port,
                                 &disp->realhost, conf, ADDRTYPE_UNSPEC,
                                 NULL, NULL);

        if ((err = sk_addr_error(disp->addr)) != NULL) {
            *error_msg = dupprintf("unable to resolve host name '%s' in "
                                   "display name", disp->hostname);

            sk_addr_free(disp->addr);
            sfree(disp->hostname);
            sfree(disp->unixsocketpath);
            sfree(disp);
            return NULL;
        }
    }

    /*
     * Try upgrading an IP-style localhost display to a Unix-socket
     * display (as the standard X connection libraries do).
     */
    if (!disp->unixdomain && sk_address_is_local(disp->addr)) {
        SockAddr *ux = platform_get_x11_unix_address(NULL, disp->displaynum);
        const char *err = sk_addr_error(ux);
        if (!err) {
            /* Create trial connection to see if there is a useful Unix-domain
             * socket */
            Socket *s = sk_new(sk_addr_dup(ux), 0, false, false,
                               false, false, nullplug);
            err = sk_socket_error(s);
            sk_close(s);
        }
        if (err) {
            sk_addr_free(ux);
        } else {
            sk_addr_free(disp->addr);
            disp->unixdomain = true;
            disp->addr = ux;
            /* Fill in the rest in a moment */
        }
    }

    if (disp->unixdomain) {
        if (!disp->addr)
            disp->addr = platform_get_x11_unix_address(disp->unixsocketpath,
                                                       disp->displaynum);
        if (disp->unixsocketpath)
            disp->realhost = dupstr(disp->unixsocketpath);
        else
            disp->realhost = dupprintf("unix:%d", disp->displaynum);
        disp->port = 0;
    }

    /*
     * Fetch the local authorisation details.
     */
    disp->localauthproto = X11_NO_AUTH;
    disp->localauthdata = NULL;
    disp->localauthdatalen = 0;
    platform_get_x11_auth(disp, conf);

    return disp;
}

void x11_free_display(struct X11Display *disp)
{
    sfree(disp->hostname);
    sfree(disp->unixsocketpath);
    if (disp->localauthdata)
        smemclr(disp->localauthdata, disp->localauthdatalen);
    sfree(disp->localauthdata);
    sk_addr_free(disp->addr);
    sfree(disp);
}