1 /* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
3 * Permission is hereby granted, free of charge, to any person obtaining a
4 * copy of this software and associated documentation files (the "Software"),
5 * to deal in the Software without restriction, including without limitation
6 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 * and/or sell copies of the Software, and to permit persons to whom the
8 * Software is furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
17 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 * Except as contained in this notice, the names of the authors or their
21 * institutions shall not be used in advertising or otherwise to promote the
22 * sale, use or other dealings in this Software without prior written
23 * authorization from the authors.
26 /* Authorization systems for the X protocol. */
29 #include <X11/Xauth.h>
30 #include <sys/param.h>
35 #include "xcb_windefs.h"
37 #include <sys/socket.h>
38 #include <netinet/in.h>
46 #include <X11/Xdmcp.h>
57 #define AUTH_PROTO_XDM_AUTHORIZATION "XDM-AUTHORIZATION-1"
58 #define AUTH_PROTO_MIT_MAGIC_COOKIE "MIT-MAGIC-COOKIE-1"
60 static char *authnames[N_AUTH_PROTOS] = {
62 AUTH_PROTO_XDM_AUTHORIZATION,
64 AUTH_PROTO_MIT_MAGIC_COOKIE,
67 static int authnameslen[N_AUTH_PROTOS] = {
69 sizeof(AUTH_PROTO_XDM_AUTHORIZATION) - 1,
71 sizeof(AUTH_PROTO_MIT_MAGIC_COOKIE) - 1,
74 static size_t memdup(char **dst, void *src, size_t len)
82 memcpy(*dst, src, len);
86 static int authname_match(enum auth_protos kind, char *name, size_t namelen)
88 if(authnameslen[kind] != namelen)
90 if(memcmp(authnames[kind], name, namelen))
95 #define SIN6_ADDR(s) (&((struct sockaddr_in6 *)s)->sin6_addr)
97 static Xauth *get_authptr(struct sockaddr *sockname, int display)
101 unsigned short family;
102 char hostnamebuf[256]; /* big enough for max hostname */
103 char dispbuf[40]; /* big enough to hold more than 2^64 base 10 */
106 family = FamilyLocal; /* 256 */
107 switch(sockname->sa_family)
111 addr = (char *) SIN6_ADDR(sockname);
112 addrlen = sizeof(*SIN6_ADDR(sockname));
113 if(!IN6_IS_ADDR_V4MAPPED(SIN6_ADDR(sockname)))
115 if(!IN6_IS_ADDR_LOOPBACK(SIN6_ADDR(sockname)))
116 family = XCB_FAMILY_INTERNET_6;
120 /* if v4-mapped, fall through. */
124 addr = (char *) &((struct sockaddr_in *)sockname)->sin_addr;
125 addrlen = sizeof(((struct sockaddr_in *)sockname)->sin_addr);
126 if(*(in_addr_t *) addr != htonl(INADDR_LOOPBACK))
127 family = XCB_FAMILY_INTERNET;
132 return 0; /* cannot authenticate this family */
135 dispbuflen = snprintf(dispbuf, sizeof(dispbuf), "%d", display);
138 /* snprintf may have truncate our text */
139 dispbuflen = MIN(dispbuflen, sizeof(dispbuf) - 1);
141 if (family == FamilyLocal) {
142 if (gethostname(hostnamebuf, sizeof(hostnamebuf)) == -1)
143 return 0; /* do not know own hostname */
145 addrlen = strlen(addr);
148 return XauGetBestAuthByAddr (family,
149 (unsigned short) addrlen, addr,
150 (unsigned short) dispbuflen, dispbuf,
151 N_AUTH_PROTOS, authnames, authnameslen);
155 static int next_nonce(void)
157 static int nonce = 0;
158 static pthread_mutex_t nonce_mutex = PTHREAD_MUTEX_INITIALIZER;
160 pthread_mutex_lock(&nonce_mutex);
162 pthread_mutex_unlock(&nonce_mutex);
166 static void do_append(char *buf, int *idxp, void *val, size_t valsize) {
167 memcpy(buf + *idxp, val, valsize);
172 static int compute_auth(xcb_auth_info_t *info, Xauth *authptr, struct sockaddr *sockname)
174 if (authname_match(AUTH_MC1, authptr->name, authptr->name_length)) {
175 info->datalen = memdup(&info->data, authptr->data, authptr->data_length);
181 #define APPEND(buf,idx,val) do_append((buf),&(idx),&(val),sizeof(val))
182 if (authname_match(AUTH_XA1, authptr->name, authptr->name_length)) {
185 info->data = malloc(192 / 8);
189 for (j = 0; j < 8; j++)
190 info->data[j] = authptr->data[j];
191 switch(sockname->sa_family) {
194 struct sockaddr_in *si = (struct sockaddr_in *) sockname;
195 APPEND(info->data, j, si->sin_addr.s_addr);
196 APPEND(info->data, j, si->sin_port);
202 struct sockaddr_in6 *si6 = (struct sockaddr_in6 *) sockname;
203 if(IN6_IS_ADDR_V4MAPPED(SIN6_ADDR(sockname)))
205 do_append(info->data, &j, &si6->sin6_addr.s6_addr[12], 4);
206 APPEND(info->data, j, si6->sin6_port);
210 /* XDM-AUTHORIZATION-1 does not handle IPv6 correctly. Do the
211 same thing Xlib does: use all zeroes for the 4-byte address
212 and 2-byte port number. */
213 uint32_t fakeaddr = 0;
214 uint16_t fakeport = 0;
215 APPEND(info->data, j, fakeaddr);
216 APPEND(info->data, j, fakeport);
223 uint32_t fakeaddr = htonl(0xffffffff - next_nonce());
224 uint16_t fakeport = htons(getpid());
225 APPEND(info->data, j, fakeaddr);
226 APPEND(info->data, j, fakeport);
231 return 0; /* do not know how to build this */
234 uint32_t now = htonl(time(0));
235 APPEND(info->data, j, now);
237 assert(j <= 192 / 8);
241 XdmcpWrap ((unsigned char *) info->data, (unsigned char *) authptr->data + 8, (unsigned char *) info->data, info->datalen);
247 return 0; /* Unknown authorization type */
250 /* `sockaddr_un.sun_path' typical size usually ranges between 92 and 108 */
251 #define INITIAL_SOCKNAME_SLACK 108
253 /* Return a dynamically allocated socket address structure according
254 to the value returned by either getpeername() or getsockname()
255 (according to POSIX, applications should not assume a particular
256 length for `sockaddr_un.sun_path') */
257 static struct sockaddr *get_peer_sock_name(int (*socket_func)(int,
262 socklen_t socknamelen = sizeof(struct sockaddr) + INITIAL_SOCKNAME_SLACK;
263 socklen_t actual_socknamelen = socknamelen;
264 struct sockaddr *sockname = malloc(socknamelen), *new_sockname = NULL;
266 if (sockname == NULL)
269 /* Both getpeername() and getsockname() truncates sockname if
270 there is not enough space and set the required length in
271 actual_socknamelen */
272 if (socket_func(fd, sockname, &actual_socknamelen) == -1)
273 goto sock_or_realloc_error;
275 if (actual_socknamelen > socknamelen)
277 socknamelen = actual_socknamelen;
279 if ((new_sockname = realloc(sockname, actual_socknamelen)) == NULL ||
280 socket_func(fd, new_sockname, &actual_socknamelen) == -1 ||
281 actual_socknamelen > socknamelen)
282 goto sock_or_realloc_error;
284 sockname = new_sockname;
289 sock_or_realloc_error:
294 int _xcb_get_auth_info(int fd, xcb_auth_info_t *info, int display)
296 /* code adapted from Xlib/ConnDis.c, xtrans/Xtranssocket.c,
297 xtrans/Xtransutils.c */
298 struct sockaddr *sockname = NULL;
303 /* Some systems like hpux or Hurd do not expose peer names
304 * for UNIX Domain Sockets, but this is irrelevant,
305 * since compute_auth() ignores the peer name in this
307 if ((sockname = get_peer_sock_name(getpeername, fd)) == NULL)
309 if ((sockname = get_peer_sock_name(getsockname, fd)) == NULL)
310 return 0; /* can only authenticate sockets */
311 if (sockname->sa_family != AF_UNIX)
314 return 0; /* except for AF_UNIX, sockets should have peernames */
319 authptr = get_authptr(sockname, display);
323 return 0; /* cannot find good auth data */
326 info->namelen = memdup(&info->name, authptr->name, authptr->name_length);
328 goto no_auth; /* out of memory */
334 if ((sockname = get_peer_sock_name(getsockname, fd)) == NULL)
337 goto no_auth; /* can only authenticate sockets */
341 ret = compute_auth(info, authptr, sockname);
345 goto no_auth; /* cannot build auth record */
351 XauDisposeAuth(authptr);
359 XauDisposeAuth(authptr);