Remove XCB_CEIL and use a simpler definition for XCB_PAD.
[free-sw/xcb/libxcb] / src / xcb_auth.c
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 HAS_AUTH_XA1
41 #include "xcb_des.h"
42 #endif
43
44 enum auth_protos {
45 #ifdef HAS_AUTH_XA1
46     AUTH_XA1,
47 #endif
48     AUTH_MC1,
49     N_AUTH_PROTOS
50 };
51
52 static char *authnames[N_AUTH_PROTOS] = {
53 #ifdef HAS_AUTH_XA1
54     "XDM-AUTHORIZATION-1",
55 #endif
56     "MIT-MAGIC-COOKIE-1",
57 };
58
59 #ifdef HAS_AUTH_XA1
60
61 static int next_nonce(void)
62 {
63     static int nonce = 0;
64     static pthread_mutex_t nonce_mutex = PTHREAD_MUTEX_INITIALIZER;
65     int ret;
66     pthread_mutex_lock(&nonce_mutex);
67     ret = nonce++;
68     pthread_mutex_unlock(&nonce_mutex);
69     return ret;
70 }
71
72 /*
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
79  */
80
81 static void
82 Wrap (
83     des_cblock          input,
84     des_cblock          key,
85     des_cblock          output,
86     int                 bytes)
87 {
88     int                 i, j;
89     int                 len;
90     des_cblock          tmp;
91     des_cblock          expand_key;
92     des_key_schedule    schedule;
93
94     XCBDESKeyToOddParity (key, expand_key);
95     XCBDESKeySchedule (expand_key, schedule);
96     for (j = 0; j < bytes; j += 8)
97     {
98         len = 8;
99         if (bytes - j < len)
100             len = bytes - j;
101         /* block chaining */
102         for (i = 0; i < len; i++)
103         {
104             if (j == 0)
105                 tmp[i] = input[i];
106             else
107                 tmp[i] = input[j + i] ^ output[j - 8 + i];
108         }
109         for (; i < 8; i++)
110         {
111             if (j == 0)
112                 tmp[i] = 0;
113             else
114                 tmp[i] = 0 ^ output[j - 8 + i];
115         }
116         XCBDESEncrypt (tmp, (output + j), schedule, 1);
117     }
118 }
119
120 #endif
121
122 static size_t memdup(char **dst, void *src, size_t len)
123 {
124     if(len)
125         *dst = malloc(len);
126     else
127         *dst = 0;
128     if(!*dst)
129         return 0;
130     memcpy(*dst, src, len);
131     return len;
132 }
133
134 static int authname_match(enum auth_protos kind, char *name, int namelen)
135 {
136     if(strlen(authnames[kind]) != namelen)
137         return 0;
138     if(memcmp(authnames[kind], name, namelen))
139         return 0;
140     return 1;
141 }
142
143 static Xauth *get_authptr(struct sockaddr *sockname, unsigned int socknamelen)
144 {
145     char *addr = 0;
146     int addrlen = 0;
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 */
150     char *display;
151     int authnamelens[N_AUTH_PROTOS];
152     int i;
153
154     family = FamilyLocal; /* 256 */
155     switch (sockname->sa_family) {
156     case AF_INET:
157         /*block*/ {
158              struct sockaddr_in *si = (struct sockaddr_in *) sockname;
159              assert(sizeof(*si) == socknamelen);
160              addr = (char *) &si->sin_addr;
161              addrlen = 4;
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);
165              display = dispbuf;
166         }
167         break;
168     case AF_UNIX:
169         /*block*/ { 
170             struct sockaddr_un *su = (struct sockaddr_un *) sockname;
171             assert(sizeof(*su) >= socknamelen);
172             display = strrchr(su->sun_path, 'X');
173             if (display == 0)
174                 return 0;   /* sockname is mangled somehow */
175             display++;
176         }
177         break;
178     default:
179         return 0;   /* cannot authenticate this family */
180     }
181     if (family == FamilyLocal) {
182         if (gethostname(hostnamebuf, sizeof(hostnamebuf)) == -1)
183             return 0;   /* do not know own hostname */
184         addr = hostnamebuf;
185         addrlen = strlen(addr);
186     }
187
188     for (i = 0; i < N_AUTH_PROTOS; i++)
189         authnamelens[i] = strlen(authnames[i]);
190     return XauGetBestAuthByAddr (family,
191                                  (unsigned short) addrlen, addr,
192                                  (unsigned short) strlen(display), display,
193                                  N_AUTH_PROTOS, authnames, authnamelens);
194 }
195
196 #ifdef HAS_AUTH_XA1
197 static void do_append(char *buf, int *idxp, void *val, size_t valsize) {
198     memcpy(buf + *idxp, val, valsize);
199     *idxp += valsize;
200 }
201 #endif
202      
203 static int compute_auth(XCBAuthInfo *info, Xauth *authptr, struct sockaddr *sockname)
204 {
205     if (authname_match(AUTH_MC1, authptr->name, authptr->name_length)) {
206         info->datalen = memdup(&info->data, authptr->data, authptr->data_length);
207         if(!info->datalen)
208             return 0;
209         return 1;
210     }
211 #ifdef HAS_AUTH_XA1
212 #define APPEND(buf,idx,val) do_append((buf),&(idx),(val),sizeof(val))
213     if (authname_match(AUTH_XA1, authptr->name, authptr->name_length)) {
214         int j;
215
216         info->data = malloc(192 / 8);
217         if(!info->data)
218             return 0;
219
220         for (j = 0; j < 8; j++)
221             info->data[j] = authptr->data[j];
222         switch(sockname->sa_family) {
223         case AF_INET:
224             /*block*/ {
225             struct sockaddr_in *si = (struct sockaddr_in *) sockname;
226             APPEND(info->data, j, si->sin_addr.s_addr);
227             APPEND(info->data, j, si->sin_port);
228         }
229         break;
230         case AF_UNIX:
231             /*block*/ {
232             long fakeaddr = htonl(0xffffffff - next_nonce());
233             short fakeport = htons(getpid());
234             APPEND(info->data, j, fakeaddr);
235             APPEND(info->data, j, fakeport);
236         }
237         break;
238         default:
239             free(info->data);
240             return 0;   /* do not know how to build this */
241         }
242         {
243             long now;
244             time(&now);
245             now = htonl(now);
246             APPEND(info->data, j, now);
247         }
248         assert(j <= 192 / 8);
249         while (j < 192 / 8)
250             info->data[j++] = 0;
251         info->datalen = j;
252         Wrap (info->data, authptr->data + 8, info->data, info->datalen);
253         return 1;
254     }
255 #undef APPEND
256 #endif
257
258     return 0;   /* Unknown authorization type */
259 }
260
261 int XCBGetAuthInfo(int fd, XCBAuthInfo *info)
262 {
263     /* code adapted from Xlib/ConnDis.c, xtrans/Xtranssocket.c,
264        xtrans/Xtransutils.c */
265     char sockbuf[sizeof(struct sockaddr) + MAXPATHLEN];
266     unsigned int socknamelen = sizeof(sockbuf);   /* need extra space */
267     struct sockaddr *sockname = (struct sockaddr *) &sockbuf;
268     Xauth *authptr = 0;
269     int ret = 1;
270
271     /* ensure info has reasonable contents */
272     /* XXX This should be removed, but Jamey depends on it
273        somehow but can't remember how.  Principle: don't touch
274        someone else's data if you're borken. */
275     info->namelen = info->datalen = 0;
276     info->name = info->data = 0;
277
278     if (getpeername(fd, sockname, &socknamelen) == -1)
279         return 0;  /* can only authenticate sockets */
280
281     authptr = get_authptr(sockname, socknamelen);
282     if (authptr == 0)
283         return 0;   /* cannot find good auth data */
284
285     info->namelen = memdup(&info->name, authptr->name, authptr->name_length);
286     if(info->namelen)
287         ret = compute_auth(info, authptr, sockname);
288     if(!ret)
289     {
290         free(info->name);
291         info->name = 0;
292         info->namelen = 0;
293     }
294     XauDisposeAuth(authptr);
295     return ret;
296 }