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>
37 /* FIXME: for the moment Xdmcp.h must be included before xcb.h because
38 * it includes Xmd.h, but config.h (normally included from xcbint.h
39 * which must be included after xcb.h) must be included before Xdmcp.h.
46 #include <X11/Xdmcp.h>
60 static char *authnames[N_AUTH_PROTOS] = {
62 "XDM-AUTHORIZATION-1",
67 static size_t memdup(char **dst, void *src, size_t len)
75 memcpy(*dst, src, len);
79 static int authname_match(enum auth_protos kind, char *name, int namelen)
81 if(strlen(authnames[kind]) != namelen)
83 if(memcmp(authnames[kind], name, namelen))
88 static Xauth *get_authptr(struct sockaddr *sockname, unsigned int socknamelen)
92 unsigned short family;
93 char hostnamebuf[256]; /* big enough for max hostname */
94 char dispbuf[40]; /* big enough to hold more than 2^64 base 10 */
96 int authnamelens[N_AUTH_PROTOS];
99 family = FamilyLocal; /* 256 */
100 switch (sockname->sa_family) {
103 struct sockaddr_in *si = (struct sockaddr_in *) sockname;
104 assert(sizeof(*si) == socknamelen);
105 addr = (char *) &si->sin_addr;
107 if (ntohl(si->sin_addr.s_addr) != 0x7f000001)
108 family = 0; /* X.h: FamilyInternet */
109 snprintf(dispbuf, sizeof(dispbuf), "%d", ntohs(si->sin_port) - X_TCP_PORT);
115 struct sockaddr_un *su = (struct sockaddr_un *) sockname;
116 char *sockbuf = (char *) sockname;
117 assert(sizeof(*su) >= socknamelen);
118 sockbuf[socknamelen] = 0; /* null-terminate path */
119 display = strrchr(su->sun_path, 'X');
121 return 0; /* sockname is mangled somehow */
126 return 0; /* cannot authenticate this family */
128 if (family == FamilyLocal) {
129 if (gethostname(hostnamebuf, sizeof(hostnamebuf)) == -1)
130 return 0; /* do not know own hostname */
132 addrlen = strlen(addr);
135 for (i = 0; i < N_AUTH_PROTOS; i++)
136 authnamelens[i] = strlen(authnames[i]);
137 return XauGetBestAuthByAddr (family,
138 (unsigned short) addrlen, addr,
139 (unsigned short) strlen(display), display,
140 N_AUTH_PROTOS, authnames, authnamelens);
144 static int next_nonce(void)
146 static int nonce = 0;
147 static pthread_mutex_t nonce_mutex = PTHREAD_MUTEX_INITIALIZER;
149 pthread_mutex_lock(&nonce_mutex);
151 pthread_mutex_unlock(&nonce_mutex);
155 static void do_append(char *buf, int *idxp, void *val, size_t valsize) {
156 memcpy(buf + *idxp, val, valsize);
161 static int compute_auth(XCBAuthInfo *info, Xauth *authptr, struct sockaddr *sockname)
163 if (authname_match(AUTH_MC1, authptr->name, authptr->name_length)) {
164 info->datalen = memdup(&info->data, authptr->data, authptr->data_length);
170 #define APPEND(buf,idx,val) do_append((buf),&(idx),&(val),sizeof(val))
171 if (authname_match(AUTH_XA1, authptr->name, authptr->name_length)) {
174 info->data = malloc(192 / 8);
178 for (j = 0; j < 8; j++)
179 info->data[j] = authptr->data[j];
180 switch(sockname->sa_family) {
183 struct sockaddr_in *si = (struct sockaddr_in *) sockname;
184 APPEND(info->data, j, si->sin_addr.s_addr);
185 APPEND(info->data, j, si->sin_port);
190 long fakeaddr = htonl(0xffffffff - next_nonce());
191 short fakeport = htons(getpid());
192 APPEND(info->data, j, fakeaddr);
193 APPEND(info->data, j, fakeport);
198 return 0; /* do not know how to build this */
204 APPEND(info->data, j, now);
206 assert(j <= 192 / 8);
210 XdmcpWrap ((unsigned char *) info->data, (unsigned char *) authptr->data + 8, (unsigned char *) info->data, info->datalen);
216 return 0; /* Unknown authorization type */
219 int _xcb_get_auth_info(int fd, XCBAuthInfo *info)
221 /* code adapted from Xlib/ConnDis.c, xtrans/Xtranssocket.c,
222 xtrans/Xtransutils.c */
223 char sockbuf[sizeof(struct sockaddr) + MAXPATHLEN];
224 unsigned int socknamelen = sizeof(sockbuf); /* need extra space */
225 struct sockaddr *sockname = (struct sockaddr *) &sockbuf;
229 /* ensure info has reasonable contents */
230 /* XXX This should be removed, but Jamey depends on it
231 somehow but can't remember how. Principle: don't touch
232 someone else's data if you're borken. */
233 info->namelen = info->datalen = 0;
234 info->name = info->data = 0;
236 if (getpeername(fd, sockname, &socknamelen) == -1)
237 return 0; /* can only authenticate sockets */
239 authptr = get_authptr(sockname, socknamelen);
241 return 0; /* cannot find good auth data */
243 info->namelen = memdup(&info->name, authptr->name, authptr->name_length);
245 ret = compute_auth(info, authptr, sockname);
252 XauDisposeAuth(authptr);