8fcfafdef04ff2c2a9b1043435a91ba1dd78b92a
[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/param.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33
34 #ifdef _WIN32
35 #include "xcb_windefs.h"
36 #else
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <sys/un.h>
40 #endif /* _WIN32 */
41
42 #include "xcb.h"
43 #include "xcbint.h"
44
45 #ifdef HASXDMAUTH
46 #include <X11/Xdmcp.h>
47 #endif
48
49 enum auth_protos {
50 #ifdef HASXDMAUTH
51     AUTH_XA1,
52 #endif
53     AUTH_MC1,
54     N_AUTH_PROTOS
55 };
56
57 #define AUTH_PROTO_XDM_AUTHORIZATION "XDM-AUTHORIZATION-1"
58 #define AUTH_PROTO_MIT_MAGIC_COOKIE "MIT-MAGIC-COOKIE-1"
59
60 static char *authnames[N_AUTH_PROTOS] = {
61 #ifdef HASXDMAUTH
62     AUTH_PROTO_XDM_AUTHORIZATION,
63 #endif
64     AUTH_PROTO_MIT_MAGIC_COOKIE,
65 };
66
67 static int authnameslen[N_AUTH_PROTOS] = {
68 #ifdef HASXDMAUTH
69     sizeof(AUTH_PROTO_XDM_AUTHORIZATION) - 1,
70 #endif
71     sizeof(AUTH_PROTO_MIT_MAGIC_COOKIE) - 1,
72 };
73
74 static size_t memdup(char **dst, void *src, size_t len)
75 {
76     if(len)
77         *dst = malloc(len);
78     else
79         *dst = 0;
80     if(!*dst)
81         return 0;
82     memcpy(*dst, src, len);
83     return len;
84 }
85
86 static int authname_match(enum auth_protos kind, char *name, size_t namelen)
87 {
88     if(authnameslen[kind] != namelen)
89         return 0;
90     if(memcmp(authnames[kind], name, namelen))
91         return 0;
92     return 1;
93 }
94
95 #define SIN6_ADDR(s) (&((struct sockaddr_in6 *)s)->sin6_addr)
96
97 static Xauth *get_authptr(struct sockaddr *sockname, unsigned int socknamelen,
98                           int display)
99 {
100     char *addr = 0;
101     int addrlen = 0;
102     unsigned short family;
103     char hostnamebuf[256];   /* big enough for max hostname */
104     char dispbuf[40];   /* big enough to hold more than 2^64 base 10 */
105     int dispbuflen;
106
107     family = FamilyLocal; /* 256 */
108     switch(sockname->sa_family)
109     {
110 #ifdef AF_INET6
111     case AF_INET6:
112         addr = (char *) SIN6_ADDR(sockname);
113         addrlen = sizeof(*SIN6_ADDR(sockname));
114         if(!IN6_IS_ADDR_V4MAPPED(SIN6_ADDR(sockname)))
115         {
116             if(!IN6_IS_ADDR_LOOPBACK(SIN6_ADDR(sockname)))
117                 family = XCB_FAMILY_INTERNET_6;
118             break;
119         }
120         addr += 12;
121         /* if v4-mapped, fall through. */
122 #endif
123     case AF_INET:
124         if(!addr)
125             addr = (char *) &((struct sockaddr_in *)sockname)->sin_addr;
126         addrlen = sizeof(((struct sockaddr_in *)sockname)->sin_addr);
127         if(*(in_addr_t *) addr != htonl(INADDR_LOOPBACK))
128             family = XCB_FAMILY_INTERNET;
129         break;
130     case AF_UNIX:
131         break;
132     default:
133         return 0;   /* cannot authenticate this family */
134     }
135
136     dispbuflen = snprintf(dispbuf, sizeof(dispbuf), "%d", display);
137     if(dispbuflen < 0)
138         return 0;
139     /* snprintf may have truncate our text */
140     dispbuflen = MIN(dispbuflen, sizeof(dispbuf) - 1);
141
142     if (family == FamilyLocal) {
143         if (gethostname(hostnamebuf, sizeof(hostnamebuf)) == -1)
144             return 0;   /* do not know own hostname */
145         addr = hostnamebuf;
146         addrlen = strlen(addr);
147     }
148
149     return XauGetBestAuthByAddr (family,
150                                  (unsigned short) addrlen, addr,
151                                  (unsigned short) dispbuflen, dispbuf,
152                                  N_AUTH_PROTOS, authnames, authnameslen);
153 }
154
155 #ifdef HASXDMAUTH
156 static int next_nonce(void)
157 {
158     static int nonce = 0;
159     static pthread_mutex_t nonce_mutex = PTHREAD_MUTEX_INITIALIZER;
160     int ret;
161     pthread_mutex_lock(&nonce_mutex);
162     ret = nonce++;
163     pthread_mutex_unlock(&nonce_mutex);
164     return ret;
165 }
166
167 static void do_append(char *buf, int *idxp, void *val, size_t valsize) {
168     memcpy(buf + *idxp, val, valsize);
169     *idxp += valsize;
170 }
171 #endif
172      
173 static int compute_auth(xcb_auth_info_t *info, Xauth *authptr, struct sockaddr *sockname)
174 {
175     if (authname_match(AUTH_MC1, authptr->name, authptr->name_length)) {
176         info->datalen = memdup(&info->data, authptr->data, authptr->data_length);
177         if(!info->datalen)
178             return 0;
179         return 1;
180     }
181 #ifdef HASXDMAUTH
182 #define APPEND(buf,idx,val) do_append((buf),&(idx),&(val),sizeof(val))
183     if (authname_match(AUTH_XA1, authptr->name, authptr->name_length)) {
184         int j;
185
186         info->data = malloc(192 / 8);
187         if(!info->data)
188             return 0;
189
190         for (j = 0; j < 8; j++)
191             info->data[j] = authptr->data[j];
192         switch(sockname->sa_family) {
193         case AF_INET:
194             /*block*/ {
195             struct sockaddr_in *si = (struct sockaddr_in *) sockname;
196             APPEND(info->data, j, si->sin_addr.s_addr);
197             APPEND(info->data, j, si->sin_port);
198         }
199         break;
200 #ifdef AF_INET6
201         case AF_INET6:
202             /*block*/ {
203             struct sockaddr_in6 *si6 = (struct sockaddr_in6 *) sockname;
204             if(IN6_IS_ADDR_V4MAPPED(SIN6_ADDR(sockname)))
205             {
206                 do_append(info->data, &j, &si6->sin6_addr.s6_addr[12], 4);
207                 APPEND(info->data, j, si6->sin6_port);
208             }
209             else
210             {
211                 /* XDM-AUTHORIZATION-1 does not handle IPv6 correctly.  Do the
212                    same thing Xlib does: use all zeroes for the 4-byte address
213                    and 2-byte port number. */
214                 uint32_t fakeaddr = 0;
215                 uint16_t fakeport = 0;
216                 APPEND(info->data, j, fakeaddr);
217                 APPEND(info->data, j, fakeport);
218             }
219         }
220         break;
221 #endif
222         case AF_UNIX:
223             /*block*/ {
224             uint32_t fakeaddr = htonl(0xffffffff - next_nonce());
225             uint16_t fakeport = htons(getpid());
226             APPEND(info->data, j, fakeaddr);
227             APPEND(info->data, j, fakeport);
228         }
229         break;
230         default:
231             free(info->data);
232             return 0;   /* do not know how to build this */
233         }
234         {
235             uint32_t now = htonl(time(0));
236             APPEND(info->data, j, now);
237         }
238         assert(j <= 192 / 8);
239         while (j < 192 / 8)
240             info->data[j++] = 0;
241         info->datalen = j;
242         XdmcpWrap ((unsigned char *) info->data, (unsigned char *) authptr->data + 8, (unsigned char *) info->data, info->datalen);
243         return 1;
244     }
245 #undef APPEND
246 #endif
247
248     return 0;   /* Unknown authorization type */
249 }
250
251 int _xcb_get_auth_info(int fd, xcb_auth_info_t *info, int display)
252 {
253     /* code adapted from Xlib/ConnDis.c, xtrans/Xtranssocket.c,
254        xtrans/Xtransutils.c */
255     char sockbuf[sizeof(struct sockaddr) + MAXPATHLEN];
256     unsigned int socknamelen = sizeof(sockbuf);   /* need extra space */
257     struct sockaddr *sockname = (struct sockaddr *) &sockbuf;
258     int gotsockname = 0;
259     Xauth *authptr = 0;
260     int ret = 1;
261
262     /* Some systems like hpux or Hurd do not expose peer names
263      * for UNIX Domain Sockets, but this is irrelevant,
264      * since compute_auth() ignores the peer name in this
265      * case anyway.*/
266     if (getpeername(fd, sockname, &socknamelen) == -1)
267     {
268         if (sockname->sa_family != AF_UNIX)
269             return 0;   /* except for AF_UNIX, sockets should have peernames */
270         if (getsockname(fd, sockname, &socknamelen) == -1)
271             return 0;   /* can only authenticate sockets */
272         gotsockname = 1;
273     }
274
275     authptr = get_authptr(sockname, socknamelen, display);
276     if (authptr == 0)
277         return 0;   /* cannot find good auth data */
278
279     info->namelen = memdup(&info->name, authptr->name, authptr->name_length);
280     if (!info->namelen)
281         goto no_auth;   /* out of memory */
282
283     if (!gotsockname && getsockname(fd, sockname, &socknamelen) == -1)
284     {
285         free(info->name);
286         goto no_auth;   /* can only authenticate sockets */
287     }
288
289     ret = compute_auth(info, authptr, sockname);
290     if(!ret)
291     {
292         free(info->name);
293         goto no_auth;   /* cannot build auth record */
294     }
295
296     XauDisposeAuth(authptr);
297     return ret;
298
299  no_auth:
300     info->name = 0;
301     info->namelen = 0;
302     XauDisposeAuth(authptr);
303     return 0;
304 }