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/socket.h>
31 #include <netinet/in.h>
33 #include <sys/param.h>
41 #include <X11/Xdmcp.h>
52 static char *authnames[N_AUTH_PROTOS] = {
54 "XDM-AUTHORIZATION-1",
59 static size_t memdup(char **dst, void *src, size_t len)
67 memcpy(*dst, src, len);
71 static int authname_match(enum auth_protos kind, char *name, size_t namelen)
73 if(strlen(authnames[kind]) != namelen)
75 if(memcmp(authnames[kind], name, namelen))
80 #define SIN6_ADDR(s) (&((struct sockaddr_in6 *)s)->sin6_addr)
82 static Xauth *get_authptr(struct sockaddr *sockname, unsigned int socknamelen,
87 unsigned short family;
88 char hostnamebuf[256]; /* big enough for max hostname */
89 char dispbuf[40]; /* big enough to hold more than 2^64 base 10 */
90 int authnamelens[N_AUTH_PROTOS];
93 family = FamilyLocal; /* 256 */
94 switch(sockname->sa_family)
98 addr = (char *) SIN6_ADDR(sockname);
99 addrlen = sizeof(*SIN6_ADDR(sockname));
100 if(!IN6_IS_ADDR_V4MAPPED(SIN6_ADDR(sockname)))
102 if(!IN6_IS_ADDR_LOOPBACK(SIN6_ADDR(sockname)))
103 family = XCB_FAMILY_INTERNET_6;
107 /* if v4-mapped, fall through. */
111 addr = (char *) &((struct sockaddr_in *)sockname)->sin_addr;
112 addrlen = sizeof(((struct sockaddr_in *)sockname)->sin_addr);
113 if(*(in_addr_t *) addr != htonl(INADDR_LOOPBACK))
114 family = XCB_FAMILY_INTERNET;
119 return 0; /* cannot authenticate this family */
122 snprintf(dispbuf, sizeof(dispbuf), "%d", display);
124 if (family == FamilyLocal) {
125 if (gethostname(hostnamebuf, sizeof(hostnamebuf)) == -1)
126 return 0; /* do not know own hostname */
128 addrlen = strlen(addr);
131 for (i = 0; i < N_AUTH_PROTOS; i++)
132 authnamelens[i] = strlen(authnames[i]);
133 return XauGetBestAuthByAddr (family,
134 (unsigned short) addrlen, addr,
135 (unsigned short) strlen(dispbuf), dispbuf,
136 N_AUTH_PROTOS, authnames, authnamelens);
140 static int next_nonce(void)
142 static int nonce = 0;
143 static pthread_mutex_t nonce_mutex = PTHREAD_MUTEX_INITIALIZER;
145 pthread_mutex_lock(&nonce_mutex);
147 pthread_mutex_unlock(&nonce_mutex);
151 static void do_append(char *buf, int *idxp, void *val, size_t valsize) {
152 memcpy(buf + *idxp, val, valsize);
157 static int compute_auth(xcb_auth_info_t *info, Xauth *authptr, struct sockaddr *sockname)
159 if (authname_match(AUTH_MC1, authptr->name, authptr->name_length)) {
160 info->datalen = memdup(&info->data, authptr->data, authptr->data_length);
166 #define APPEND(buf,idx,val) do_append((buf),&(idx),&(val),sizeof(val))
167 if (authname_match(AUTH_XA1, authptr->name, authptr->name_length)) {
170 info->data = malloc(192 / 8);
174 for (j = 0; j < 8; j++)
175 info->data[j] = authptr->data[j];
176 switch(sockname->sa_family) {
179 struct sockaddr_in *si = (struct sockaddr_in *) sockname;
180 APPEND(info->data, j, si->sin_addr.s_addr);
181 APPEND(info->data, j, si->sin_port);
187 struct sockaddr_in6 *si6 = (struct sockaddr_in6 *) sockname;
188 if(IN6_IS_ADDR_V4MAPPED(SIN6_ADDR(sockname)))
190 APPEND(info->data, j, si6->sin6_addr.s6_addr[12]);
191 APPEND(info->data, j, si6->sin6_port);
195 /* XDM-AUTHORIZATION-1 does not handle IPv6 correctly. Do the
196 same thing Xlib does: use all zeroes for the 4-byte address
197 and 2-byte port number. */
198 uint32_t fakeaddr = 0;
199 uint16_t fakeport = 0;
200 APPEND(info->data, j, fakeaddr);
201 APPEND(info->data, j, fakeport);
208 uint32_t fakeaddr = htonl(0xffffffff - next_nonce());
209 uint16_t fakeport = htons(getpid());
210 APPEND(info->data, j, fakeaddr);
211 APPEND(info->data, j, fakeport);
216 return 0; /* do not know how to build this */
219 uint32_t now = htonl(time(0));
220 APPEND(info->data, j, now);
222 assert(j <= 192 / 8);
226 XdmcpWrap ((unsigned char *) info->data, (unsigned char *) authptr->data + 8, (unsigned char *) info->data, info->datalen);
232 return 0; /* Unknown authorization type */
235 int _xcb_get_auth_info(int fd, xcb_auth_info_t *info, int display)
237 /* code adapted from Xlib/ConnDis.c, xtrans/Xtranssocket.c,
238 xtrans/Xtransutils.c */
239 char sockbuf[sizeof(struct sockaddr) + MAXPATHLEN];
240 unsigned int socknamelen = sizeof(sockbuf); /* need extra space */
241 struct sockaddr *sockname = (struct sockaddr *) &sockbuf;
245 if (getpeername(fd, sockname, &socknamelen) == -1)
246 return 0; /* can only authenticate sockets */
248 authptr = get_authptr(sockname, socknamelen, display);
250 return 0; /* cannot find good auth data */
252 info->namelen = memdup(&info->name, authptr->name, authptr->name_length);
254 ret = compute_auth(info, authptr, sockname);
261 XauDisposeAuth(authptr);