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. */
33 #include <X11/Xauth.h>
34 #include <sys/param.h>
39 /* _don't_ ask. interix has INADDR_LOOPBACK in here. */
40 #include <rpc/types.h>
45 /* We must include the wrapped windows.h before any system header which includes
46 it unwrapped, to avoid conflicts with types defined in X headers */
47 #include <X11/Xwindows.h>
49 #include "xcb_windefs.h"
51 #include <arpa/inet.h>
52 #include <sys/socket.h>
53 #include <netinet/in.h>
61 #include <X11/Xdmcp.h>
72 #define AUTH_PROTO_XDM_AUTHORIZATION "XDM-AUTHORIZATION-1"
73 #define AUTH_PROTO_MIT_MAGIC_COOKIE "MIT-MAGIC-COOKIE-1"
75 static char *authnames[N_AUTH_PROTOS] = {
77 AUTH_PROTO_XDM_AUTHORIZATION,
79 AUTH_PROTO_MIT_MAGIC_COOKIE,
82 static int authnameslen[N_AUTH_PROTOS] = {
84 sizeof(AUTH_PROTO_XDM_AUTHORIZATION) - 1,
86 sizeof(AUTH_PROTO_MIT_MAGIC_COOKIE) - 1,
89 static size_t memdup(char **dst, void *src, size_t len)
97 memcpy(*dst, src, len);
101 static int authname_match(enum auth_protos kind, char *name, size_t namelen)
103 if(authnameslen[kind] != namelen)
105 if(memcmp(authnames[kind], name, namelen))
110 #define SIN6_ADDR(s) (&((struct sockaddr_in6 *)s)->sin6_addr)
112 static Xauth *get_authptr(struct sockaddr *sockname, int display)
116 unsigned short family;
117 char hostnamebuf[256]; /* big enough for max hostname */
118 char dispbuf[40]; /* big enough to hold more than 2^64 base 10 */
121 family = FamilyLocal; /* 256 */
122 switch(sockname->sa_family)
126 addr = (char *) SIN6_ADDR(sockname);
127 addrlen = sizeof(*SIN6_ADDR(sockname));
128 if(!IN6_IS_ADDR_V4MAPPED(SIN6_ADDR(sockname)))
130 if(!IN6_IS_ADDR_LOOPBACK(SIN6_ADDR(sockname)))
131 family = XCB_FAMILY_INTERNET_6;
135 /* if v4-mapped, fall through. */
139 addr = (char *) &((struct sockaddr_in *)sockname)->sin_addr;
140 addrlen = sizeof(((struct sockaddr_in *)sockname)->sin_addr);
141 if(*(in_addr_t *) addr != htonl(INADDR_LOOPBACK))
142 family = XCB_FAMILY_INTERNET;
147 return 0; /* cannot authenticate this family */
150 dispbuflen = snprintf(dispbuf, sizeof(dispbuf), "%d", display);
153 /* snprintf may have truncate our text */
154 dispbuflen = MIN(dispbuflen, sizeof(dispbuf) - 1);
156 if (family == FamilyLocal) {
157 if (gethostname(hostnamebuf, sizeof(hostnamebuf)) == -1)
158 return 0; /* do not know own hostname */
160 addrlen = strlen(addr);
163 return XauGetBestAuthByAddr (family,
164 (unsigned short) addrlen, addr,
165 (unsigned short) dispbuflen, dispbuf,
166 N_AUTH_PROTOS, authnames, authnameslen);
170 static int next_nonce(void)
172 static int nonce = 0;
173 static pthread_mutex_t nonce_mutex = PTHREAD_MUTEX_INITIALIZER;
175 pthread_mutex_lock(&nonce_mutex);
177 pthread_mutex_unlock(&nonce_mutex);
181 static void do_append(char *buf, int *idxp, void *val, size_t valsize) {
182 memcpy(buf + *idxp, val, valsize);
187 static int compute_auth(xcb_auth_info_t *info, Xauth *authptr, struct sockaddr *sockname)
189 if (authname_match(AUTH_MC1, authptr->name, authptr->name_length)) {
190 info->datalen = memdup(&info->data, authptr->data, authptr->data_length);
196 #define APPEND(buf,idx,val) do_append((buf),&(idx),&(val),sizeof(val))
197 if (authname_match(AUTH_XA1, authptr->name, authptr->name_length)) {
200 info->data = malloc(192 / 8);
204 for (j = 0; j < 8; j++)
205 info->data[j] = authptr->data[j];
206 switch(sockname->sa_family) {
209 struct sockaddr_in *si = (struct sockaddr_in *) sockname;
210 APPEND(info->data, j, si->sin_addr.s_addr);
211 APPEND(info->data, j, si->sin_port);
217 struct sockaddr_in6 *si6 = (struct sockaddr_in6 *) sockname;
218 if(IN6_IS_ADDR_V4MAPPED(SIN6_ADDR(sockname)))
220 do_append(info->data, &j, &si6->sin6_addr.s6_addr[12], 4);
221 APPEND(info->data, j, si6->sin6_port);
225 /* XDM-AUTHORIZATION-1 does not handle IPv6 correctly. Do the
226 same thing Xlib does: use all zeroes for the 4-byte address
227 and 2-byte port number. */
228 uint32_t fakeaddr = 0;
229 uint16_t fakeport = 0;
230 APPEND(info->data, j, fakeaddr);
231 APPEND(info->data, j, fakeport);
238 uint32_t fakeaddr = htonl(0xffffffff - next_nonce());
239 uint16_t fakeport = htons(getpid());
240 APPEND(info->data, j, fakeaddr);
241 APPEND(info->data, j, fakeport);
246 return 0; /* do not know how to build this */
249 uint32_t now = htonl(time(0));
250 APPEND(info->data, j, now);
252 assert(j <= 192 / 8);
256 XdmcpWrap ((unsigned char *) info->data, (unsigned char *) authptr->data + 8, (unsigned char *) info->data, info->datalen);
262 return 0; /* Unknown authorization type */
265 /* `sockaddr_un.sun_path' typical size usually ranges between 92 and 108 */
266 #define INITIAL_SOCKNAME_SLACK 108
268 /* Return a dynamically allocated socket address structure according
269 to the value returned by either getpeername() or getsockname()
270 (according to POSIX, applications should not assume a particular
271 length for `sockaddr_un.sun_path') */
272 static struct sockaddr *get_peer_sock_name(int (*socket_func)(int,
277 socklen_t socknamelen = sizeof(struct sockaddr) + INITIAL_SOCKNAME_SLACK;
278 socklen_t actual_socknamelen = socknamelen;
279 struct sockaddr *sockname = malloc(socknamelen);
281 if (sockname == NULL)
284 /* Both getpeername() and getsockname() truncates sockname if
285 there is not enough space and set the required length in
286 actual_socknamelen */
287 if (socket_func(fd, sockname, &actual_socknamelen) == -1)
288 goto sock_or_realloc_error;
290 if (actual_socknamelen > socknamelen)
292 struct sockaddr *new_sockname = NULL;
293 socknamelen = actual_socknamelen;
295 if ((new_sockname = realloc(sockname, actual_socknamelen)) == NULL)
296 goto sock_or_realloc_error;
298 sockname = new_sockname;
300 if (socket_func(fd, sockname, &actual_socknamelen) == -1 ||
301 actual_socknamelen > socknamelen)
302 goto sock_or_realloc_error;
307 sock_or_realloc_error:
312 int _xcb_get_auth_info(int fd, xcb_auth_info_t *info, int display)
314 /* code adapted from Xlib/ConnDis.c, xtrans/Xtranssocket.c,
315 xtrans/Xtransutils.c */
316 struct sockaddr *sockname = NULL;
321 /* Some systems like hpux or Hurd do not expose peer names
322 * for UNIX Domain Sockets, but this is irrelevant,
323 * since compute_auth() ignores the peer name in this
325 if ((sockname = get_peer_sock_name(getpeername, fd)) == NULL)
327 if ((sockname = get_peer_sock_name(getsockname, fd)) == NULL)
328 return 0; /* can only authenticate sockets */
329 if (sockname->sa_family != AF_UNIX)
332 return 0; /* except for AF_UNIX, sockets should have peernames */
337 authptr = get_authptr(sockname, display);
341 return 0; /* cannot find good auth data */
344 info->namelen = memdup(&info->name, authptr->name, authptr->name_length);
346 goto no_auth; /* out of memory */
352 if ((sockname = get_peer_sock_name(getsockname, fd)) == NULL)
355 goto no_auth; /* can only authenticate sockets */
359 ret = compute_auth(info, authptr, sockname);
363 goto no_auth; /* cannot build auth record */
369 XauDisposeAuth(authptr);
377 XauDisposeAuth(authptr);