]> git.sesse.net Git - rdpsrv/blob - Xserver/programs/Xserver/os/auth.c
Support RDP5 logon packets.
[rdpsrv] / Xserver / programs / Xserver / os / auth.c
1 /* $XConsortium: auth.c /main/27 1996/12/02 10:22:41 lehors $ */
2 /*
3
4 Copyright (c) 1988  X Consortium
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 The above copyright notice and this permission notice shall be included
15 in all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 OTHER DEALINGS IN THE SOFTWARE.
24
25 Except as contained in this notice, the name of the X Consortium shall
26 not be used in advertising or otherwise to promote the sale, use or
27 other dealings in this Software without prior written authorization
28 from the X Consortium.
29
30 */
31
32 /*
33  * authorization hooks for the server
34  * Author:  Keith Packard, MIT X Consortium
35  */
36
37 #ifdef K5AUTH
38 # include   <krb5/krb5.h>
39 #endif
40 # include   "X.h"
41 # include   "Xauth.h"
42 # include   "misc.h"
43 # include   "dixstruct.h"
44 # include   <sys/types.h>
45 # include   <sys/stat.h>
46 #ifdef XCSECURITY
47 #define _SECURITY_SERVER
48 # include   "extensions/security.h"
49 #endif
50 #ifdef WIN32
51 #include "Xw32defs.h"
52 #endif
53
54 struct protocol {
55     unsigned short   name_length;
56     char    *name;
57     int     (*Add)();       /* new authorization data */
58     XID     (*Check)();     /* verify client authorization data */
59     int     (*Reset)();     /* delete all authorization data entries */
60     XID     (*ToID)();      /* convert cookie to ID */
61     int     (*FromID)();    /* convert ID to cookie */
62     int     (*Remove)();    /* remove a specific cookie */
63 #ifdef XCSECURITY
64     XID     (*Generate)();
65 #endif
66 };
67
68 extern int  MitAddCookie ();
69 extern XID  MitCheckCookie ();
70 extern int  MitResetCookie ();
71 extern XID  MitToID ();
72 extern int  MitFromID (), MitRemoveCookie ();
73 extern XID  MitGenerateCookie();
74
75 #ifdef HASXDMAUTH
76 extern int  XdmAddCookie ();
77 extern XID  XdmCheckCookie ();
78 extern int  XdmResetCookie ();
79 extern XID  XdmToID ();
80 extern int  XdmFromID (), XdmRemoveCookie ();
81 #endif
82
83 #ifdef SECURE_RPC
84 extern int  SecureRPCAdd();
85 extern XID  SecureRPCCheck();
86 extern int  SecureRPCReset();
87 extern XID  SecureRPCToID();
88 extern int  SecureRPCFromID(), SecureRPCRemove();
89 #endif
90
91 #ifdef K5AUTH
92 extern int K5Add();
93 extern XID K5Check();
94 extern int K5Reset();
95 extern XID K5ToID();
96 extern int K5FromID(), K5Remove();
97 #endif
98
99 extern XID AuthSecurityCheck();
100
101 static struct protocol   protocols[] = {
102 {   (unsigned short) 18,    "MIT-MAGIC-COOKIE-1",
103                 MitAddCookie,   MitCheckCookie, MitResetCookie,
104                 MitToID,        MitFromID,      MitRemoveCookie,
105 #ifdef XCSECURITY
106                 MitGenerateCookie
107 #endif
108 },
109 #ifdef HASXDMAUTH
110 {   (unsigned short) 19,    "XDM-AUTHORIZATION-1",
111                 XdmAddCookie,   XdmCheckCookie, XdmResetCookie,
112                 XdmToID,        XdmFromID,      XdmRemoveCookie,
113 #ifdef XCSECURITY
114                 NULL
115 #endif
116 },
117 #endif
118 #ifdef SECURE_RPC
119 {   (unsigned short) 9,    "SUN-DES-1",
120                 SecureRPCAdd,   SecureRPCCheck, SecureRPCReset,
121                 SecureRPCToID,  SecureRPCFromID,SecureRPCRemove,
122 #ifdef XCSECURITY
123                 NULL
124 #endif
125 },
126 #endif
127 #ifdef K5AUTH
128 {   (unsigned short) 14, "MIT-KERBEROS-5",
129                 K5Add, K5Check, K5Reset,
130                 K5ToID, K5FromID, K5Remove,
131 #ifdef XCSECURITY
132                 NULL
133 #endif
134 },
135 #endif
136 #ifdef XCSECURITY
137 {   (unsigned short) XSecurityAuthorizationNameLen,
138         XSecurityAuthorizationName,
139                 NULL, AuthSecurityCheck, NULL,
140                 NULL, NULL, NULL,
141                 NULL
142 },
143 #endif
144 };
145
146 # define NUM_AUTHORIZATION  (sizeof (protocols) /\
147                              sizeof (struct protocol))
148
149 /*
150  * Initialize all classes of authorization by reading the
151  * specified authorization file
152  */
153
154 static char *authorization_file = (char *)NULL;
155
156 static Bool ShouldLoadAuth = TRUE;
157
158 void
159 InitAuthorization (file_name)
160 char    *file_name;
161 {
162     authorization_file = file_name;
163 }
164
165 int
166 LoadAuthorization ()
167 {
168     FILE    *f;
169     Xauth   *auth;
170     int     i;
171     int     count = 0;
172
173     ShouldLoadAuth = FALSE;
174     if (!authorization_file)
175         return 0;
176     f = fopen (authorization_file, "r");
177     if (!f)
178         return 0;
179     while (auth = XauReadAuth (f)) {
180         for (i = 0; i < NUM_AUTHORIZATION; i++) {
181             if (protocols[i].name_length == auth->name_length &&
182                 memcmp (protocols[i].name, auth->name, (int) auth->name_length) == 0 &&
183                 protocols[i].Add)
184             {
185                 ++count;
186                 (*protocols[i].Add) (auth->data_length, auth->data,
187                                          FakeClientID(0));
188             }
189         }
190         XauDisposeAuth (auth);
191     }
192     fclose (f);
193     return count;
194 }
195
196 #ifdef XDMCP
197 /*
198  * XdmcpInit calls this function to discover all authorization
199  * schemes supported by the display
200  */
201 void
202 RegisterAuthorizations ()
203 {
204     int     i;
205
206     for (i = 0; i < NUM_AUTHORIZATION; i++)
207         XdmcpRegisterAuthorization (protocols[i].name,
208                                     (int)protocols[i].name_length);
209 }
210 #endif
211
212 XID
213 CheckAuthorization (name_length, name, data_length, data, client, reason)
214     unsigned int name_length;
215     char        *name;
216     unsigned int data_length;
217     char        *data;
218     ClientPtr client;
219     char        **reason;       /* failure message.  NULL for default msg */
220 {
221     int i;
222     struct stat buf;
223     static time_t lastmod = 0;
224
225     if (!authorization_file || stat(authorization_file, &buf))
226     {
227         lastmod = 0;
228         ShouldLoadAuth = TRUE;  /* stat lost, so force reload */
229     }
230     else if (buf.st_mtime > lastmod)
231     {
232         lastmod = buf.st_mtime;
233         ShouldLoadAuth = TRUE;
234     }
235     if (ShouldLoadAuth)
236     {
237         if (LoadAuthorization())
238             DisableLocalHost(); /* got at least one */
239         else
240             EnableLocalHost ();
241     }
242     if (name_length)
243         for (i = 0; i < NUM_AUTHORIZATION; i++) {
244             if (protocols[i].name_length == name_length &&
245                 memcmp (protocols[i].name, name, (int) name_length) == 0)
246             {
247                 return (*protocols[i].Check) (data_length, data, client, reason);
248             }
249         }
250     return (XID) ~0L;
251 }
252
253 void
254 ResetAuthorization ()
255 {
256     int i;
257
258     for (i = 0; i < NUM_AUTHORIZATION; i++)
259         if (protocols[i].Reset)
260             (*protocols[i].Reset)();
261     ShouldLoadAuth = TRUE;
262 }
263
264 XID
265 AuthorizationToID (name_length, name, data_length, data)
266 unsigned short  name_length;
267 char    *name;
268 unsigned short  data_length;
269 char    *data;
270 {
271     int i;
272
273     for (i = 0; i < NUM_AUTHORIZATION; i++) {
274         if (protocols[i].name_length == name_length &&
275             memcmp (protocols[i].name, name, (int) name_length) == 0 &&
276             protocols[i].ToID)
277         {
278             return (*protocols[i].ToID) (data_length, data);
279         }
280     }
281     return (XID) ~0L;
282 }
283
284 int
285 AuthorizationFromID (id, name_lenp, namep, data_lenp, datap)
286 XID id;
287 unsigned short  *name_lenp;
288 char    **namep;
289 unsigned short  *data_lenp;
290 char    **datap;
291 {
292     int i;
293
294     for (i = 0; i < NUM_AUTHORIZATION; i++) {
295         if (protocols[i].FromID &&
296             (*protocols[i].FromID) (id, data_lenp, datap)) {
297             *name_lenp = protocols[i].name_length;
298             *namep = protocols[i].name;
299             return 1;
300         }
301     }
302     return 0;
303 }
304
305 int
306 RemoveAuthorization (name_length, name, data_length, data)
307 unsigned short  name_length;
308 char    *name;
309 unsigned short  data_length;
310 char    *data;
311 {
312     int i;
313
314     for (i = 0; i < NUM_AUTHORIZATION; i++) {
315         if (protocols[i].name_length == name_length &&
316             memcmp (protocols[i].name, name, (int) name_length) == 0 &&
317             protocols[i].Remove)
318         {
319             return (*protocols[i].Remove) (data_length, data);
320         }
321     }
322     return 0;
323 }
324
325 int
326 AddAuthorization (name_length, name, data_length, data)
327 unsigned int name_length;
328 char    *name;
329 unsigned int data_length;
330 char    *data;
331 {
332     int i;
333
334     for (i = 0; i < NUM_AUTHORIZATION; i++) {
335         if (protocols[i].name_length == name_length &&
336             memcmp (protocols[i].name, name, (int) name_length) == 0 &&
337             protocols[i].Add)
338         {
339             return (*protocols[i].Add) (data_length, data, FakeClientID(0));
340         }
341     }
342     return 0;
343 }
344
345 #ifdef XCSECURITY
346
347 XID
348 GenerateAuthorization(name_length, name, data_length, data,
349                       data_length_return, data_return)
350 unsigned int name_length;
351 char    *name;
352 unsigned int data_length;
353 char    *data;
354 unsigned int *data_length_return;
355 char    **data_return;
356 {
357     int i;
358
359     for (i = 0; i < NUM_AUTHORIZATION; i++) {
360         if (protocols[i].name_length == name_length &&
361             memcmp (protocols[i].name, name, (int) name_length) == 0 &&
362             protocols[i].Generate)
363         {
364             return (*protocols[i].Generate) (data_length, data,
365                         FakeClientID(0), data_length_return, data_return);
366         }
367     }
368     return -1;
369 }
370
371 /* A random number generator that is more unpredictable
372    than that shipped with some systems.
373    This code is taken from the C standard. */
374
375 static unsigned long int next = 1;
376
377 static int
378 xdm_rand()
379 {
380     next = next * 1103515245 + 12345;
381     return (unsigned int)(next/65536) % 32768;
382 }
383
384 static void
385 xdm_srand(seed)
386     unsigned int seed;
387 {
388     next = seed;
389 }
390
391 void
392 GenerateRandomData (len, buf)
393 int     len;
394 char    *buf;
395 {
396     static int seed;
397     int value;
398     int i;
399
400     seed += GetTimeInMillis();
401     xdm_srand (seed);
402     for (i = 0; i < len; i++)
403     {
404         value = xdm_rand ();
405         buf[i] ^= (value & 0xff00) >> 8;
406     }
407
408     /* XXX add getrusage, popen("ps -ale") */
409 }
410
411 #endif /* XCSECURITY */