The typedefs replacing Xmd.h conflict with Xmd.h. Here is a hacky workaround: FIXME!
[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 /* 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.
40  * Ow. */
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44
45 #ifdef HASXDMAUTH
46 #include <X11/Xdmcp.h>
47 #endif
48
49 #include "xcb.h"
50 #include "xcbint.h"
51
52 enum auth_protos {
53 #ifdef HASXDMAUTH
54     AUTH_XA1,
55 #endif
56     AUTH_MC1,
57     N_AUTH_PROTOS
58 };
59
60 static char *authnames[N_AUTH_PROTOS] = {
61 #ifdef HASXDMAUTH
62     "XDM-AUTHORIZATION-1",
63 #endif
64     "MIT-MAGIC-COOKIE-1",
65 };
66
67 static size_t memdup(char **dst, void *src, size_t len)
68 {
69     if(len)
70         *dst = malloc(len);
71     else
72         *dst = 0;
73     if(!*dst)
74         return 0;
75     memcpy(*dst, src, len);
76     return len;
77 }
78
79 static int authname_match(enum auth_protos kind, char *name, int namelen)
80 {
81     if(strlen(authnames[kind]) != namelen)
82         return 0;
83     if(memcmp(authnames[kind], name, namelen))
84         return 0;
85     return 1;
86 }
87
88 static Xauth *get_authptr(struct sockaddr *sockname, unsigned int socknamelen)
89 {
90     char *addr = 0;
91     int addrlen = 0;
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 */
95     char *display;
96     int authnamelens[N_AUTH_PROTOS];
97     int i;
98
99     family = FamilyLocal; /* 256 */
100     switch (sockname->sa_family) {
101     case AF_INET:
102         /*block*/ {
103              struct sockaddr_in *si = (struct sockaddr_in *) sockname;
104              assert(sizeof(*si) == socknamelen);
105              addr = (char *) &si->sin_addr;
106              addrlen = 4;
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);
110              display = dispbuf;
111         }
112         break;
113     case AF_UNIX:
114         /*block*/ { 
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');
120             if (display == 0)
121                 return 0;   /* sockname is mangled somehow */
122             display++;
123         }
124         break;
125     default:
126         return 0;   /* cannot authenticate this family */
127     }
128     if (family == FamilyLocal) {
129         if (gethostname(hostnamebuf, sizeof(hostnamebuf)) == -1)
130             return 0;   /* do not know own hostname */
131         addr = hostnamebuf;
132         addrlen = strlen(addr);
133     }
134
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);
141 }
142
143 #ifdef HASXDMAUTH
144 static int next_nonce(void)
145 {
146     static int nonce = 0;
147     static pthread_mutex_t nonce_mutex = PTHREAD_MUTEX_INITIALIZER;
148     int ret;
149     pthread_mutex_lock(&nonce_mutex);
150     ret = nonce++;
151     pthread_mutex_unlock(&nonce_mutex);
152     return ret;
153 }
154
155 static void do_append(char *buf, int *idxp, void *val, size_t valsize) {
156     memcpy(buf + *idxp, val, valsize);
157     *idxp += valsize;
158 }
159 #endif
160      
161 static int compute_auth(XCBAuthInfo *info, Xauth *authptr, struct sockaddr *sockname)
162 {
163     if (authname_match(AUTH_MC1, authptr->name, authptr->name_length)) {
164         info->datalen = memdup(&info->data, authptr->data, authptr->data_length);
165         if(!info->datalen)
166             return 0;
167         return 1;
168     }
169 #ifdef HASXDMAUTH
170 #define APPEND(buf,idx,val) do_append((buf),&(idx),&(val),sizeof(val))
171     if (authname_match(AUTH_XA1, authptr->name, authptr->name_length)) {
172         int j;
173
174         info->data = malloc(192 / 8);
175         if(!info->data)
176             return 0;
177
178         for (j = 0; j < 8; j++)
179             info->data[j] = authptr->data[j];
180         switch(sockname->sa_family) {
181         case AF_INET:
182             /*block*/ {
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);
186         }
187         break;
188         case AF_UNIX:
189             /*block*/ {
190             long fakeaddr = htonl(0xffffffff - next_nonce());
191             short fakeport = htons(getpid());
192             APPEND(info->data, j, fakeaddr);
193             APPEND(info->data, j, fakeport);
194         }
195         break;
196         default:
197             free(info->data);
198             return 0;   /* do not know how to build this */
199         }
200         {
201             long now;
202             time(&now);
203             now = htonl(now);
204             APPEND(info->data, j, now);
205         }
206         assert(j <= 192 / 8);
207         while (j < 192 / 8)
208             info->data[j++] = 0;
209         info->datalen = j;
210         XdmcpWrap ((unsigned char *) info->data, (unsigned char *) authptr->data + 8, (unsigned char *) info->data, info->datalen);
211         return 1;
212     }
213 #undef APPEND
214 #endif
215
216     return 0;   /* Unknown authorization type */
217 }
218
219 int _xcb_get_auth_info(int fd, XCBAuthInfo *info)
220 {
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;
226     Xauth *authptr = 0;
227     int ret = 1;
228
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;
235
236     if (getpeername(fd, sockname, &socknamelen) == -1)
237         return 0;  /* can only authenticate sockets */
238
239     authptr = get_authptr(sockname, socknamelen);
240     if (authptr == 0)
241         return 0;   /* cannot find good auth data */
242
243     info->namelen = memdup(&info->name, authptr->name, authptr->name_length);
244     if(info->namelen)
245         ret = compute_auth(info, authptr, sockname);
246     if(!ret)
247     {
248         free(info->name);
249         info->name = 0;
250         info->namelen = 0;
251     }
252     XauDisposeAuth(authptr);
253     return ret;
254 }