Merge branch 'master' of git+ssh://iano@git.freedesktop.org/git/xcb
[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 Xauth *get_authptr(struct sockaddr *sockname, unsigned int socknamelen)
81 {
82     char *addr = 0;
83     int addrlen = 0;
84     unsigned short family;
85     char hostnamebuf[256];   /* big enough for max hostname */
86     char dispbuf[40];   /* big enough to hold more than 2^64 base 10 */
87     char *display;
88     int authnamelens[N_AUTH_PROTOS];
89     int i;
90
91     family = FamilyLocal; /* 256 */
92     switch (sockname->sa_family) {
93     case AF_INET:
94         /*block*/ {
95              struct sockaddr_in *si = (struct sockaddr_in *) sockname;
96              assert(sizeof(*si) == socknamelen);
97              addr = (char *) &si->sin_addr;
98              addrlen = 4;
99              if (ntohl(si->sin_addr.s_addr) != 0x7f000001)
100                  family = FamilyInternet; /* 0 */
101              snprintf(dispbuf, sizeof(dispbuf), "%d", ntohs(si->sin_port) - X_TCP_PORT);
102              display = dispbuf;
103         }
104         break;
105     case AF_UNIX:
106         /*block*/ { 
107             struct sockaddr_un *su = (struct sockaddr_un *) sockname;
108             char *sockbuf = (char *) sockname;
109             assert(sizeof(*su) >= socknamelen);
110             sockbuf[socknamelen] = 0;   /* null-terminate path */
111             display = strrchr(su->sun_path, 'X');
112             if (display == 0)
113                 return 0;   /* sockname is mangled somehow */
114             display++;
115         }
116         break;
117     default:
118         return 0;   /* cannot authenticate this family */
119     }
120     if (family == FamilyLocal) {
121         if (gethostname(hostnamebuf, sizeof(hostnamebuf)) == -1)
122             return 0;   /* do not know own hostname */
123         addr = hostnamebuf;
124         addrlen = strlen(addr);
125     }
126
127     for (i = 0; i < N_AUTH_PROTOS; i++)
128         authnamelens[i] = strlen(authnames[i]);
129     return XauGetBestAuthByAddr (family,
130                                  (unsigned short) addrlen, addr,
131                                  (unsigned short) strlen(display), display,
132                                  N_AUTH_PROTOS, authnames, authnamelens);
133 }
134
135 #ifdef HASXDMAUTH
136 static int next_nonce(void)
137 {
138     static int nonce = 0;
139     static pthread_mutex_t nonce_mutex = PTHREAD_MUTEX_INITIALIZER;
140     int ret;
141     pthread_mutex_lock(&nonce_mutex);
142     ret = nonce++;
143     pthread_mutex_unlock(&nonce_mutex);
144     return ret;
145 }
146
147 static void do_append(char *buf, int *idxp, void *val, size_t valsize) {
148     memcpy(buf + *idxp, val, valsize);
149     *idxp += valsize;
150 }
151 #endif
152      
153 static int compute_auth(XCBAuthInfo *info, Xauth *authptr, struct sockaddr *sockname)
154 {
155     if (authname_match(AUTH_MC1, authptr->name, authptr->name_length)) {
156         info->datalen = memdup(&info->data, authptr->data, authptr->data_length);
157         if(!info->datalen)
158             return 0;
159         return 1;
160     }
161 #ifdef HASXDMAUTH
162 #define APPEND(buf,idx,val) do_append((buf),&(idx),&(val),sizeof(val))
163     if (authname_match(AUTH_XA1, authptr->name, authptr->name_length)) {
164         int j;
165
166         info->data = malloc(192 / 8);
167         if(!info->data)
168             return 0;
169
170         for (j = 0; j < 8; j++)
171             info->data[j] = authptr->data[j];
172         switch(sockname->sa_family) {
173         case AF_INET:
174             /*block*/ {
175             struct sockaddr_in *si = (struct sockaddr_in *) sockname;
176             APPEND(info->data, j, si->sin_addr.s_addr);
177             APPEND(info->data, j, si->sin_port);
178         }
179         break;
180         case AF_UNIX:
181             /*block*/ {
182             long fakeaddr = htonl(0xffffffff - next_nonce());
183             short fakeport = htons(getpid());
184             APPEND(info->data, j, fakeaddr);
185             APPEND(info->data, j, fakeport);
186         }
187         break;
188         default:
189             free(info->data);
190             return 0;   /* do not know how to build this */
191         }
192         {
193             long now;
194             time(&now);
195             now = htonl(now);
196             APPEND(info->data, j, now);
197         }
198         assert(j <= 192 / 8);
199         while (j < 192 / 8)
200             info->data[j++] = 0;
201         info->datalen = j;
202         XdmcpWrap ((unsigned char *) info->data, (unsigned char *) authptr->data + 8, (unsigned char *) info->data, info->datalen);
203         return 1;
204     }
205 #undef APPEND
206 #endif
207
208     return 0;   /* Unknown authorization type */
209 }
210
211 int XCBGetAuthInfo(int fd, XCBAuthInfo *info)
212 {
213     /* code adapted from Xlib/ConnDis.c, xtrans/Xtranssocket.c,
214        xtrans/Xtransutils.c */
215     char sockbuf[sizeof(struct sockaddr) + MAXPATHLEN];
216     unsigned int socknamelen = sizeof(sockbuf);   /* need extra space */
217     struct sockaddr *sockname = (struct sockaddr *) &sockbuf;
218     Xauth *authptr = 0;
219     int ret = 1;
220
221     /* ensure info has reasonable contents */
222     /* XXX This should be removed, but Jamey depends on it
223        somehow but can't remember how.  Principle: don't touch
224        someone else's data if you're borken. */
225     info->namelen = info->datalen = 0;
226     info->name = info->data = 0;
227
228     if (getpeername(fd, sockname, &socknamelen) == -1)
229         return 0;  /* can only authenticate sockets */
230
231     authptr = get_authptr(sockname, socknamelen);
232     if (authptr == 0)
233         return 0;   /* cannot find good auth data */
234
235     info->namelen = memdup(&info->name, authptr->name, authptr->name_length);
236     if(info->namelen)
237         ret = compute_auth(info, authptr, sockname);
238     if(!ret)
239     {
240         free(info->name);
241         info->name = 0;
242         info->namelen = 0;
243     }
244     XauDisposeAuth(authptr);
245     return ret;
246 }