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