Support authentication for IPv6 connections
[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 static char *authnames[N_AUTH_PROTOS] = {
53 #ifdef HASXDMAUTH
54     "XDM-AUTHORIZATION-1",
55 #endif
56     "MIT-MAGIC-COOKIE-1",
57 };
58
59 static size_t memdup(char **dst, void *src, size_t len)
60 {
61     if(len)
62         *dst = malloc(len);
63     else
64         *dst = 0;
65     if(!*dst)
66         return 0;
67     memcpy(*dst, src, len);
68     return len;
69 }
70
71 static int authname_match(enum auth_protos kind, char *name, int namelen)
72 {
73     if(strlen(authnames[kind]) != namelen)
74         return 0;
75     if(memcmp(authnames[kind], name, namelen))
76         return 0;
77     return 1;
78 }
79
80 static void *_xcb_memrchr(const void *s, int c, size_t n)
81 {
82     for(s = (char *) s + n - 1; n--; s = (char *) s - 1)
83         if(*(char *)s == (char)c)
84             return (void *) s;
85     return 0;
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, port = 0;
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     {
102     case AF_INET6:
103         addr = (char *) &((struct sockaddr_in6 *)sockname)->sin6_addr;
104         addrlen = sizeof(((struct sockaddr_in6 *)sockname)->sin6_addr);
105         port = ((struct sockaddr_in6 *)sockname)->sin6_port;
106         if(!IN6_IS_ADDR_V4MAPPED(addr))
107         {
108             if(!IN6_IS_ADDR_LOOPBACK(addr))
109                 family = XCB_FAMILY_INTERNET_6;
110             break;
111         }
112         addr += 12;
113         /* if v4-mapped, fall through. */
114     case AF_INET:
115         if(!addr)
116         {
117             addr = (char *) &((struct sockaddr_in *)sockname)->sin_addr;
118             port = ((struct sockaddr_in *)sockname)->sin_port;
119         }
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         display = _xcb_memrchr(((struct sockaddr_un *) sockname)->sun_path, 'X',
126                                socknamelen);
127         if(!display)
128             return 0;   /* sockname is mangled somehow */
129         display++;
130         break;
131     default:
132         return 0;   /* cannot authenticate this family */
133     }
134
135     if(port)
136     {
137         snprintf(dispbuf, sizeof(dispbuf), "%hu", ntohs(port) - X_TCP_PORT);
138         display = dispbuf;
139     }
140
141     if (family == FamilyLocal) {
142         if (gethostname(hostnamebuf, sizeof(hostnamebuf)) == -1)
143             return 0;   /* do not know own hostname */
144         addr = hostnamebuf;
145         addrlen = strlen(addr);
146     }
147
148     for (i = 0; i < N_AUTH_PROTOS; i++)
149         authnamelens[i] = strlen(authnames[i]);
150     return XauGetBestAuthByAddr (family,
151                                  (unsigned short) addrlen, addr,
152                                  (unsigned short) strlen(display), display,
153                                  N_AUTH_PROTOS, authnames, authnamelens);
154 }
155
156 #ifdef HASXDMAUTH
157 static int next_nonce(void)
158 {
159     static int nonce = 0;
160     static pthread_mutex_t nonce_mutex = PTHREAD_MUTEX_INITIALIZER;
161     int ret;
162     pthread_mutex_lock(&nonce_mutex);
163     ret = nonce++;
164     pthread_mutex_unlock(&nonce_mutex);
165     return ret;
166 }
167
168 static void do_append(char *buf, int *idxp, void *val, size_t valsize) {
169     memcpy(buf + *idxp, val, valsize);
170     *idxp += valsize;
171 }
172 #endif
173      
174 static int compute_auth(xcb_auth_info_t *info, Xauth *authptr, struct sockaddr *sockname)
175 {
176     if (authname_match(AUTH_MC1, authptr->name, authptr->name_length)) {
177         info->datalen = memdup(&info->data, authptr->data, authptr->data_length);
178         if(!info->datalen)
179             return 0;
180         return 1;
181     }
182 #ifdef HASXDMAUTH
183 #define APPEND(buf,idx,val) do_append((buf),&(idx),&(val),sizeof(val))
184     if (authname_match(AUTH_XA1, authptr->name, authptr->name_length)) {
185         int j;
186
187         info->data = malloc(192 / 8);
188         if(!info->data)
189             return 0;
190
191         for (j = 0; j < 8; j++)
192             info->data[j] = authptr->data[j];
193         switch(sockname->sa_family) {
194         case AF_INET:
195             /*block*/ {
196             struct sockaddr_in *si = (struct sockaddr_in *) sockname;
197             APPEND(info->data, j, si->sin_addr.s_addr);
198             APPEND(info->data, j, si->sin_port);
199         }
200         break;
201         case AF_UNIX:
202             /*block*/ {
203             long fakeaddr = htonl(0xffffffff - next_nonce());
204             short fakeport = htons(getpid());
205             APPEND(info->data, j, fakeaddr);
206             APPEND(info->data, j, fakeport);
207         }
208         break;
209         default:
210             free(info->data);
211             return 0;   /* do not know how to build this */
212         }
213         {
214             long now;
215             time(&now);
216             now = htonl(now);
217             APPEND(info->data, j, now);
218         }
219         assert(j <= 192 / 8);
220         while (j < 192 / 8)
221             info->data[j++] = 0;
222         info->datalen = j;
223         XdmcpWrap ((unsigned char *) info->data, (unsigned char *) authptr->data + 8, (unsigned char *) info->data, info->datalen);
224         return 1;
225     }
226 #undef APPEND
227 #endif
228
229     return 0;   /* Unknown authorization type */
230 }
231
232 int _xcb_get_auth_info(int fd, xcb_auth_info_t *info)
233 {
234     /* code adapted from Xlib/ConnDis.c, xtrans/Xtranssocket.c,
235        xtrans/Xtransutils.c */
236     char sockbuf[sizeof(struct sockaddr) + MAXPATHLEN];
237     unsigned int socknamelen = sizeof(sockbuf);   /* need extra space */
238     struct sockaddr *sockname = (struct sockaddr *) &sockbuf;
239     Xauth *authptr = 0;
240     int ret = 1;
241
242     /* ensure info has reasonable contents */
243     /* XXX This should be removed, but Jamey depends on it
244        somehow but can't remember how.  Principle: don't touch
245        someone else's data if you're borken. */
246     info->namelen = info->datalen = 0;
247     info->name = info->data = 0;
248
249     if (getpeername(fd, sockname, &socknamelen) == -1)
250         return 0;  /* can only authenticate sockets */
251
252     authptr = get_authptr(sockname, socknamelen);
253     if (authptr == 0)
254         return 0;   /* cannot find good auth data */
255
256     info->namelen = memdup(&info->name, authptr->name, authptr->name_length);
257     if(info->namelen)
258         ret = compute_auth(info, authptr, sockname);
259     if(!ret)
260     {
261         free(info->name);
262         info->name = 0;
263         info->namelen = 0;
264     }
265     XauDisposeAuth(authptr);
266     return ret;
267 }