xcb_auth.c revision b20a2039
1/* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp. 2 * 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: 9 * 10 * The above copyright notice and this permission notice shall be included in 11 * all copies or substantial portions of the Software. 12 * 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. 19 * 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. 24 */ 25 26/* Authorization systems for the X protocol. */ 27 28#include <assert.h> 29#include <X11/Xauth.h> 30#include <sys/socket.h> 31#include <netinet/in.h> 32#include <sys/un.h> 33#include <sys/param.h> 34#include <unistd.h> 35#include <stdlib.h> 36 37#include "xcb.h" 38#include "xcbint.h" 39 40#ifdef HASXDMAUTH 41#include <X11/Xdmcp.h> 42#endif 43 44enum auth_protos { 45#ifdef HASXDMAUTH 46 AUTH_XA1, 47#endif 48 AUTH_MC1, 49 N_AUTH_PROTOS 50}; 51 52#define AUTH_PROTO_XDM_AUTHORIZATION "XDM-AUTHORIZATION-1" 53#define AUTH_PROTO_MIT_MAGIC_COOKIE "MIT-MAGIC-COOKIE-1" 54 55static char *authnames[N_AUTH_PROTOS] = { 56#ifdef HASXDMAUTH 57 AUTH_PROTO_XDM_AUTHORIZATION, 58#endif 59 AUTH_PROTO_MIT_MAGIC_COOKIE, 60}; 61 62static int authnameslen[N_AUTH_PROTOS] = { 63#ifdef HASXDMAUTH 64 sizeof(AUTH_PROTO_XDM_AUTHORIZATION) - 1, 65#endif 66 sizeof(AUTH_PROTO_MIT_MAGIC_COOKIE) - 1, 67}; 68 69static size_t memdup(char **dst, void *src, size_t len) 70{ 71 if(len) 72 *dst = malloc(len); 73 else 74 *dst = 0; 75 if(!*dst) 76 return 0; 77 memcpy(*dst, src, len); 78 return len; 79} 80 81static int authname_match(enum auth_protos kind, char *name, size_t namelen) 82{ 83 if(authnameslen[kind] != namelen) 84 return 0; 85 if(memcmp(authnames[kind], name, namelen)) 86 return 0; 87 return 1; 88} 89 90#define SIN6_ADDR(s) (&((struct sockaddr_in6 *)s)->sin6_addr) 91 92static Xauth *get_authptr(struct sockaddr *sockname, int display) 93{ 94 char *addr = 0; 95 int addrlen = 0; 96 unsigned short family; 97 char hostnamebuf[256]; /* big enough for max hostname */ 98 char dispbuf[40]; /* big enough to hold more than 2^64 base 10 */ 99 int dispbuflen; 100 101 family = FamilyLocal; /* 256 */ 102 switch(sockname->sa_family) 103 { 104#ifdef AF_INET6 105 case AF_INET6: 106 addr = (char *) SIN6_ADDR(sockname); 107 addrlen = sizeof(*SIN6_ADDR(sockname)); 108 if(!IN6_IS_ADDR_V4MAPPED(SIN6_ADDR(sockname))) 109 { 110 if(!IN6_IS_ADDR_LOOPBACK(SIN6_ADDR(sockname))) 111 family = XCB_FAMILY_INTERNET_6; 112 break; 113 } 114 addr += 12; 115 /* if v4-mapped, fall through. */ 116#endif 117 case AF_INET: 118 if(!addr) 119 addr = (char *) &((struct sockaddr_in *)sockname)->sin_addr; 120 addrlen = sizeof(((struct sockaddr_in *)sockname)->sin_addr); 121 if(*(in_addr_t *) addr != htonl(INADDR_LOOPBACK)) 122 family = XCB_FAMILY_INTERNET; 123 break; 124 case AF_UNIX: 125 break; 126 default: 127 return 0; /* cannot authenticate this family */ 128 } 129 130 dispbuflen = snprintf(dispbuf, sizeof(dispbuf), "%d", display); 131 if(dispbuflen < 0) 132 return 0; 133 /* snprintf may have truncate our text */ 134 dispbuflen = MIN(dispbuflen, sizeof(dispbuf) - 1); 135 136 if (family == FamilyLocal) { 137 if (gethostname(hostnamebuf, sizeof(hostnamebuf)) == -1) 138 return 0; /* do not know own hostname */ 139 addr = hostnamebuf; 140 addrlen = strlen(addr); 141 } 142 143 return XauGetBestAuthByAddr (family, 144 (unsigned short) addrlen, addr, 145 (unsigned short) dispbuflen, dispbuf, 146 N_AUTH_PROTOS, authnames, authnameslen); 147} 148 149#ifdef HASXDMAUTH 150static int next_nonce(void) 151{ 152 static int nonce = 0; 153 static pthread_mutex_t nonce_mutex = PTHREAD_MUTEX_INITIALIZER; 154 int ret; 155 pthread_mutex_lock(&nonce_mutex); 156 ret = nonce++; 157 pthread_mutex_unlock(&nonce_mutex); 158 return ret; 159} 160 161static void do_append(char *buf, int *idxp, void *val, size_t valsize) { 162 memcpy(buf + *idxp, val, valsize); 163 *idxp += valsize; 164} 165#endif 166 167static int compute_auth(xcb_auth_info_t *info, Xauth *authptr, struct sockaddr *sockname) 168{ 169 if (authname_match(AUTH_MC1, authptr->name, authptr->name_length)) { 170 info->datalen = memdup(&info->data, authptr->data, authptr->data_length); 171 if(!info->datalen) 172 return 0; 173 return 1; 174 } 175#ifdef HASXDMAUTH 176#define APPEND(buf,idx,val) do_append((buf),&(idx),&(val),sizeof(val)) 177 if (authname_match(AUTH_XA1, authptr->name, authptr->name_length)) { 178 int j; 179 180 info->data = malloc(192 / 8); 181 if(!info->data) 182 return 0; 183 184 for (j = 0; j < 8; j++) 185 info->data[j] = authptr->data[j]; 186 switch(sockname->sa_family) { 187 case AF_INET: 188 /*block*/ { 189 struct sockaddr_in *si = (struct sockaddr_in *) sockname; 190 APPEND(info->data, j, si->sin_addr.s_addr); 191 APPEND(info->data, j, si->sin_port); 192 } 193 break; 194#ifdef AF_INET6 195 case AF_INET6: 196 /*block*/ { 197 struct sockaddr_in6 *si6 = (struct sockaddr_in6 *) sockname; 198 if(IN6_IS_ADDR_V4MAPPED(SIN6_ADDR(sockname))) 199 { 200 do_append(info->data, &j, &si6->sin6_addr.s6_addr[12], 4); 201 APPEND(info->data, j, si6->sin6_port); 202 } 203 else 204 { 205 /* XDM-AUTHORIZATION-1 does not handle IPv6 correctly. Do the 206 same thing Xlib does: use all zeroes for the 4-byte address 207 and 2-byte port number. */ 208 uint32_t fakeaddr = 0; 209 uint16_t fakeport = 0; 210 APPEND(info->data, j, fakeaddr); 211 APPEND(info->data, j, fakeport); 212 } 213 } 214 break; 215#endif 216 case AF_UNIX: 217 /*block*/ { 218 uint32_t fakeaddr = htonl(0xffffffff - next_nonce()); 219 uint16_t fakeport = htons(getpid()); 220 APPEND(info->data, j, fakeaddr); 221 APPEND(info->data, j, fakeport); 222 } 223 break; 224 default: 225 free(info->data); 226 return 0; /* do not know how to build this */ 227 } 228 { 229 uint32_t now = htonl(time(0)); 230 APPEND(info->data, j, now); 231 } 232 assert(j <= 192 / 8); 233 while (j < 192 / 8) 234 info->data[j++] = 0; 235 info->datalen = j; 236 XdmcpWrap ((unsigned char *) info->data, (unsigned char *) authptr->data + 8, (unsigned char *) info->data, info->datalen); 237 return 1; 238 } 239#undef APPEND 240#endif 241 242 return 0; /* Unknown authorization type */ 243} 244 245/* `sockaddr_un.sun_path' typical size usually ranges between 92 and 108 */ 246#define INITIAL_SOCKNAME_SLACK 108 247 248/* Return a dynamically allocated socket address structure according 249 to the value returned by either getpeername() or getsockname() 250 (according to POSIX, applications should not assume a particular 251 length for `sockaddr_un.sun_path') */ 252static struct sockaddr *get_peer_sock_name(int (*socket_func)(int, 253 struct sockaddr *, 254 socklen_t *), 255 int fd) 256{ 257 socklen_t socknamelen = sizeof(struct sockaddr) + INITIAL_SOCKNAME_SLACK; 258 socklen_t actual_socknamelen = socknamelen; 259 struct sockaddr *sockname = malloc(socknamelen), *new_sockname = NULL; 260 261 if (sockname == NULL) 262 return NULL; 263 264 /* Both getpeername() and getsockname() truncates sockname if 265 there is not enough space and set the required length in 266 actual_socknamelen */ 267 if (socket_func(fd, sockname, &actual_socknamelen) == -1) 268 goto sock_or_realloc_error; 269 270 if (actual_socknamelen > socknamelen) 271 { 272 socknamelen = actual_socknamelen; 273 274 if ((new_sockname = realloc(sockname, actual_socknamelen)) == NULL || 275 socket_func(fd, new_sockname, &actual_socknamelen) == -1 || 276 actual_socknamelen > socknamelen) 277 goto sock_or_realloc_error; 278 279 sockname = new_sockname; 280 } 281 282 return sockname; 283 284 sock_or_realloc_error: 285 free(sockname); 286 return NULL; 287} 288 289int _xcb_get_auth_info(int fd, xcb_auth_info_t *info, int display) 290{ 291 /* code adapted from Xlib/ConnDis.c, xtrans/Xtranssocket.c, 292 xtrans/Xtransutils.c */ 293 struct sockaddr *sockname = NULL; 294 int gotsockname = 0; 295 Xauth *authptr = 0; 296 int ret = 1; 297 298 /* Some systems like hpux or Hurd do not expose peer names 299 * for UNIX Domain Sockets, but this is irrelevant, 300 * since compute_auth() ignores the peer name in this 301 * case anyway.*/ 302 if ((sockname = get_peer_sock_name(getpeername, fd)) == NULL) 303 { 304 if ((sockname = get_peer_sock_name(getsockname, fd)) == NULL) 305 return 0; /* can only authenticate sockets */ 306 if (sockname->sa_family != AF_UNIX) 307 { 308 free(sockname); 309 return 0; /* except for AF_UNIX, sockets should have peernames */ 310 } 311 gotsockname = 1; 312 } 313 314 authptr = get_authptr(sockname, display); 315 if (authptr == 0) 316 { 317 free(sockname); 318 return 0; /* cannot find good auth data */ 319 } 320 321 info->namelen = memdup(&info->name, authptr->name, authptr->name_length); 322 if (!info->namelen) 323 goto no_auth; /* out of memory */ 324 325 if (!gotsockname && (sockname = get_peer_sock_name(getsockname, fd)) == NULL) 326 { 327 free(info->name); 328 goto no_auth; /* can only authenticate sockets */ 329 } 330 331 ret = compute_auth(info, authptr, sockname); 332 if(!ret) 333 { 334 free(info->name); 335 goto no_auth; /* cannot build auth record */ 336 } 337 338 free(sockname); 339 sockname = NULL; 340 341 XauDisposeAuth(authptr); 342 return ret; 343 344 no_auth: 345 free(sockname); 346 347 info->name = 0; 348 info->namelen = 0; 349 XauDisposeAuth(authptr); 350 return 0; 351} 352