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>
52 static char *authnames[N_AUTH_PROTOS] = {
54 "XDM-AUTHORIZATION-1",
61 static int next_nonce(void)
64 static pthread_mutex_t nonce_mutex = PTHREAD_MUTEX_INITIALIZER;
66 pthread_mutex_lock(&nonce_mutex);
68 pthread_mutex_unlock(&nonce_mutex);
73 * This code and the code it calls is taken from libXdmcp,
74 * specifically from Wrap.c, Wrap.h, and Wraphelp.c. The US
75 * has changed, thank goodness, and it should be OK to bury
76 * DES code in an open source product without a maze of
77 * twisty wrapper functions stored offshore. Or maybe
78 * not. --Bart Massey 2003/11/5
91 des_cblock expand_key;
92 des_key_schedule schedule;
94 XCBDESKeyToOddParity (key, expand_key);
95 XCBDESKeySchedule (expand_key, schedule);
96 for (j = 0; j < bytes; j += 8)
102 for (i = 0; i < len; i++)
107 tmp[i] = input[j + i] ^ output[j - 8 + i];
114 tmp[i] = 0 ^ output[j - 8 + i];
116 XCBDESEncrypt (tmp, (output + j), schedule, 1);
122 static size_t memdup(char **dst, void *src, size_t len)
130 memcpy(*dst, src, len);
134 static int authname_match(enum auth_protos kind, char *name, int namelen)
136 if(strlen(authnames[kind]) != namelen)
138 if(memcmp(authnames[kind], name, namelen))
143 static Xauth *get_authptr(struct sockaddr *sockname, unsigned int socknamelen)
147 unsigned short family;
148 char hostnamebuf[256]; /* big enough for max hostname */
149 char dispbuf[40]; /* big enough to hold more than 2^64 base 10 */
151 int authnamelens[N_AUTH_PROTOS];
154 family = FamilyLocal; /* 256 */
155 switch (sockname->sa_family) {
158 struct sockaddr_in *si = (struct sockaddr_in *) sockname;
159 assert(sizeof(*si) == socknamelen);
160 addr = (char *) &si->sin_addr;
162 if (ntohl(si->sin_addr.s_addr) != 0x7f000001)
163 family = FamilyInternet; /* 0 */
164 snprintf(dispbuf, sizeof(dispbuf), "%d", ntohs(si->sin_port) - X_TCP_PORT);
170 struct sockaddr_un *su = (struct sockaddr_un *) sockname;
171 char *sockbuf = (char *) sockname;
172 assert(sizeof(*su) >= socknamelen);
173 sockbuf[socknamelen] = 0; /* null-terminate path */
174 display = strrchr(su->sun_path, 'X');
176 return 0; /* sockname is mangled somehow */
181 return 0; /* cannot authenticate this family */
183 if (family == FamilyLocal) {
184 if (gethostname(hostnamebuf, sizeof(hostnamebuf)) == -1)
185 return 0; /* do not know own hostname */
187 addrlen = strlen(addr);
190 for (i = 0; i < N_AUTH_PROTOS; i++)
191 authnamelens[i] = strlen(authnames[i]);
192 return XauGetBestAuthByAddr (family,
193 (unsigned short) addrlen, addr,
194 (unsigned short) strlen(display), display,
195 N_AUTH_PROTOS, authnames, authnamelens);
199 static void do_append(char *buf, int *idxp, void *val, size_t valsize) {
200 memcpy(buf + *idxp, val, valsize);
205 static int compute_auth(XCBAuthInfo *info, Xauth *authptr, struct sockaddr *sockname)
207 if (authname_match(AUTH_MC1, authptr->name, authptr->name_length)) {
208 info->datalen = memdup(&info->data, authptr->data, authptr->data_length);
214 #define APPEND(buf,idx,val) do_append((buf),&(idx),(val),sizeof(val))
215 if (authname_match(AUTH_XA1, authptr->name, authptr->name_length)) {
218 info->data = malloc(192 / 8);
222 for (j = 0; j < 8; j++)
223 info->data[j] = authptr->data[j];
224 switch(sockname->sa_family) {
227 struct sockaddr_in *si = (struct sockaddr_in *) sockname;
228 APPEND(info->data, j, si->sin_addr.s_addr);
229 APPEND(info->data, j, si->sin_port);
234 long fakeaddr = htonl(0xffffffff - next_nonce());
235 short fakeport = htons(getpid());
236 APPEND(info->data, j, fakeaddr);
237 APPEND(info->data, j, fakeport);
242 return 0; /* do not know how to build this */
248 APPEND(info->data, j, now);
250 assert(j <= 192 / 8);
254 Wrap (info->data, authptr->data + 8, info->data, info->datalen);
260 return 0; /* Unknown authorization type */
263 int XCBGetAuthInfo(int fd, XCBAuthInfo *info)
265 /* code adapted from Xlib/ConnDis.c, xtrans/Xtranssocket.c,
266 xtrans/Xtransutils.c */
267 char sockbuf[sizeof(struct sockaddr) + MAXPATHLEN];
268 unsigned int socknamelen = sizeof(sockbuf); /* need extra space */
269 struct sockaddr *sockname = (struct sockaddr *) &sockbuf;
273 /* ensure info has reasonable contents */
274 /* XXX This should be removed, but Jamey depends on it
275 somehow but can't remember how. Principle: don't touch
276 someone else's data if you're borken. */
277 info->namelen = info->datalen = 0;
278 info->name = info->data = 0;
280 if (getpeername(fd, sockname, &socknamelen) == -1)
281 return 0; /* can only authenticate sockets */
283 authptr = get_authptr(sockname, socknamelen);
285 return 0; /* cannot find good auth data */
287 info->namelen = memdup(&info->name, authptr->name, authptr->name_length);
289 ret = compute_auth(info, authptr, sockname);
296 XauDisposeAuth(authptr);