Replace uses of "long" with uint32_t, and similar; fixes 64-bit bugs
[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 #define SIN6_ADDR(s) (&((struct sockaddr_in6 *)s)->sin6_addr)
81
82 static Xauth *get_authptr(struct sockaddr *sockname, unsigned int socknamelen,
83                           int display)
84 {
85     char *addr = 0;
86     int addrlen = 0;
87     unsigned short family;
88     char hostnamebuf[256];   /* big enough for max hostname */
89     char dispbuf[40];   /* big enough to hold more than 2^64 base 10 */
90     int authnamelens[N_AUTH_PROTOS];
91     int i;
92
93     family = FamilyLocal; /* 256 */
94     switch(sockname->sa_family)
95     {
96     case AF_INET6:
97         addr = (char *) SIN6_ADDR(sockname);
98         addrlen = sizeof(*SIN6_ADDR(sockname));
99         if(!IN6_IS_ADDR_V4MAPPED(SIN6_ADDR(sockname)))
100         {
101             if(!IN6_IS_ADDR_LOOPBACK(SIN6_ADDR(sockname)))
102                 family = XCB_FAMILY_INTERNET_6;
103             break;
104         }
105         addr += 12;
106         /* if v4-mapped, fall through. */
107     case AF_INET:
108         if(!addr)
109             addr = (char *) &((struct sockaddr_in *)sockname)->sin_addr;
110         addrlen = sizeof(((struct sockaddr_in *)sockname)->sin_addr);
111         if(*(in_addr_t *) addr != htonl(INADDR_LOOPBACK))
112             family = XCB_FAMILY_INTERNET;
113         break;
114     case AF_UNIX:
115         break;
116     default:
117         return 0;   /* cannot authenticate this family */
118     }
119
120     snprintf(dispbuf, sizeof(dispbuf), "%d", display);
121
122     if (family == FamilyLocal) {
123         if (gethostname(hostnamebuf, sizeof(hostnamebuf)) == -1)
124             return 0;   /* do not know own hostname */
125         addr = hostnamebuf;
126         addrlen = strlen(addr);
127     }
128
129     for (i = 0; i < N_AUTH_PROTOS; i++)
130         authnamelens[i] = strlen(authnames[i]);
131     return XauGetBestAuthByAddr (family,
132                                  (unsigned short) addrlen, addr,
133                                  (unsigned short) strlen(dispbuf), dispbuf,
134                                  N_AUTH_PROTOS, authnames, authnamelens);
135 }
136
137 #ifdef HASXDMAUTH
138 static int next_nonce(void)
139 {
140     static int nonce = 0;
141     static pthread_mutex_t nonce_mutex = PTHREAD_MUTEX_INITIALIZER;
142     int ret;
143     pthread_mutex_lock(&nonce_mutex);
144     ret = nonce++;
145     pthread_mutex_unlock(&nonce_mutex);
146     return ret;
147 }
148
149 static void do_append(char *buf, int *idxp, void *val, size_t valsize) {
150     memcpy(buf + *idxp, val, valsize);
151     *idxp += valsize;
152 }
153 #endif
154      
155 static int compute_auth(xcb_auth_info_t *info, Xauth *authptr, struct sockaddr *sockname)
156 {
157     if (authname_match(AUTH_MC1, authptr->name, authptr->name_length)) {
158         info->datalen = memdup(&info->data, authptr->data, authptr->data_length);
159         if(!info->datalen)
160             return 0;
161         return 1;
162     }
163 #ifdef HASXDMAUTH
164 #define APPEND(buf,idx,val) do_append((buf),&(idx),&(val),sizeof(val))
165     if (authname_match(AUTH_XA1, authptr->name, authptr->name_length)) {
166         int j;
167
168         info->data = malloc(192 / 8);
169         if(!info->data)
170             return 0;
171
172         for (j = 0; j < 8; j++)
173             info->data[j] = authptr->data[j];
174         switch(sockname->sa_family) {
175         case AF_INET:
176             /*block*/ {
177             struct sockaddr_in *si = (struct sockaddr_in *) sockname;
178             APPEND(info->data, j, si->sin_addr.s_addr);
179             APPEND(info->data, j, si->sin_port);
180         }
181         break;
182         case AF_INET6:
183             /*block*/ {
184             struct sockaddr_in6 *si6 = (struct sockaddr_in6 *) sockname;
185             if(IN6_IS_ADDR_V4MAPPED(SIN6_ADDR(sockname)))
186             {
187                 APPEND(info->data, j, si6->sin6_addr.s6_addr[12]);
188                 APPEND(info->data, j, si6->sin6_port);
189             }
190             else
191             {
192                 /* XDM-AUTHORIZATION-1 does not handle IPv6 correctly.  Do the
193                    same thing Xlib does: use all zeroes for the 4-byte address
194                    and 2-byte port number. */
195                 uint32_t fakeaddr = 0;
196                 uint16_t fakeport = 0;
197                 APPEND(info->data, j, fakeaddr);
198                 APPEND(info->data, j, fakeport);
199             }
200         }
201         break;
202         case AF_UNIX:
203             /*block*/ {
204             uint32_t fakeaddr = htonl(0xffffffff - next_nonce());
205             uint16_t fakeport = htons(getpid());
206             APPEND(info->data, j, fakeaddr);
207             APPEND(info->data, j, fakeport);
208         }
209         break;
210         default:
211             free(info->data);
212             return 0;   /* do not know how to build this */
213         }
214         {
215             uint32_t now = htonl(time(0));
216             APPEND(info->data, j, now);
217         }
218         assert(j <= 192 / 8);
219         while (j < 192 / 8)
220             info->data[j++] = 0;
221         info->datalen = j;
222         XdmcpWrap ((unsigned char *) info->data, (unsigned char *) authptr->data + 8, (unsigned char *) info->data, info->datalen);
223         return 1;
224     }
225 #undef APPEND
226 #endif
227
228     return 0;   /* Unknown authorization type */
229 }
230
231 int _xcb_get_auth_info(int fd, xcb_auth_info_t *info, int display)
232 {
233     /* code adapted from Xlib/ConnDis.c, xtrans/Xtranssocket.c,
234        xtrans/Xtransutils.c */
235     char sockbuf[sizeof(struct sockaddr) + MAXPATHLEN];
236     unsigned int socknamelen = sizeof(sockbuf);   /* need extra space */
237     struct sockaddr *sockname = (struct sockaddr *) &sockbuf;
238     Xauth *authptr = 0;
239     int ret = 1;
240
241     if (getpeername(fd, sockname, &socknamelen) == -1)
242         return 0;  /* can only authenticate sockets */
243
244     authptr = get_authptr(sockname, socknamelen, display);
245     if (authptr == 0)
246         return 0;   /* cannot find good auth data */
247
248     info->namelen = memdup(&info->name, authptr->name, authptr->name_length);
249     if(info->namelen)
250         ret = compute_auth(info, authptr, sockname);
251     if(!ret)
252     {
253         free(info->name);
254         info->name = 0;
255         info->namelen = 0;
256     }
257     XauDisposeAuth(authptr);
258     return ret;
259 }