1 /* $TOG: genauth.c /main/6 1997/03/25 12:33:13 barstow $ */
2 /* (c) Copyright 1997, The Open Group */
4 * (c) Copyright 1993, 1994 Hewlett-Packard Company *
5 * (c) Copyright 1993, 1994 International Business Machines Corp. *
6 * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
7 * (c) Copyright 1993, 1994 Novell, Inc. *
16 * Revision 1.1.2.2 1995/04/21 13:05:23 Peter_Derr
17 * dtlogin auth key fixes from deltacde
18 * [1995/04/14 18:03:41 Peter_Derr]
20 * R6 xdm code used in dtlogin.
21 * [1995/04/10 16:52:31 Peter_Derr]
23 * Revision 1.1.3.3 1995/02/20 21:03:19 Peter_Derr
25 * [1995/02/20 20:13:02 Peter_Derr]
27 * Revision 1.1.3.2 1994/07/13 19:26:25 Peter_Derr
28 * Include Wrap.h to get definitions for XDM-AUTHENTICATION-1
29 * authorization mechanism.
30 * [1994/07/13 12:15:59 Peter_Derr]
35 static char *rcsid = "@(#)$RCSfile: genauth.c $ $Revision: /main/6 $ (DEC) $Date: 1997/03/25 12:33:13 $";
39 Copyright (c) 1988 X Consortium
41 Permission is hereby granted, free of charge, to any person obtaining
42 a copy of this software and associated documentation files (the
43 "Software"), to deal in the Software without restriction, including
44 without limitation the rights to use, copy, modify, merge, publish,
45 distribute, sublicense, and/or sell copies of the Software, and to
46 permit persons to whom the Software is furnished to do so, subject to
47 the following conditions:
49 The above copyright notice and this permission notice shall be included
50 in all copies or substantial portions of the Software.
52 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
53 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
54 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
55 IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
56 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
57 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
58 OTHER DEALINGS IN THE SOFTWARE.
60 Except as contained in this notice, the name of the X Consortium shall
61 not be used in advertising or otherwise to promote the sale, use or
62 other dealings in this Software without prior written authorization
63 from the X Consortium.
68 * xdm - display manager daemon
69 * Author: Keith Packard, MIT X Consortium
72 # include <X11/Xauth.h>
80 extern Time_t time ();
103 # if (OSMAJORVERSION >= 4)
104 /* avoid strange sun crypt hackery */
105 # define crypt _crypt
111 #if !defined (DONT_USE_DES) && !defined (USE_CRYPT)
116 static unsigned char key[8];
121 static sumFile (char *name, long sum[2]);
125 typedef unsigned char auth_cblock[8]; /* block size */
127 typedef struct auth_ks_struct { auth_cblock _; } auth_wrapper_schedule[16];
129 extern void _XdmcpWrapperToOddParity();
136 c[0] = (l >> 24) & 0xff;
137 c[1] = (l >> 16) & 0xff;
138 c[2] = (l >> 8) & 0xff;
146 unsigned char tmpkey[8];
148 * randomFile is and xdm resource not defined in dtlogin.
150 * if (!sumFile (randomFile, sum)) {
152 if (!sumFile ("/dev/mem", sum)) {
153 sum[0] = time ((Time_t *) 0);
154 sum[1] = time ((Time_t *) 0);
156 longtochars (sum[0], tmpkey+0);
157 longtochars (sum[1], tmpkey+4);
159 _XdmcpWrapperToOddParity (tmpkey, key);
162 #endif /* HASXDMAUTH */
165 /* A random number generator that is more unpredictable
166 than that shipped with some systems.
167 This code is taken from the C standard. */
169 static unsigned long int next = 1;
174 next = next * 1103515245 + 12345;
175 return (unsigned int)(next/65536) % 32768;
184 #endif /* no HASXDMAUTH */
188 bitsToBytes (bits, bytes)
189 unsigned long bits[2];
196 for (byte = 0; byte < 2; byte++)
197 for (bit = 0; bit < 32; bit++)
198 bytes[i++] = ((bits[byte] & (1 << bit)) != 0);
200 #endif /* USE_ENCRYPT */
202 # define FILE_LIMIT 1024 /* no more than this many buffers */
219 LogError((unsigned char *) "Cannot open randomFile \"%s\", errno = %d\n", name, errno);
222 #ifdef FRAGILE_DEV_MEM
223 if (strcmp(name, "/dev/mem") == 0) lseek (fd, (off_t) 0x100000, SEEK_SET);
228 while ((cnt = read (fd, (char *) buf, sizeof (buf))) > 0 && --reads > 0) {
229 loops = cnt / (2 * sizeof (long));
230 for (i = 0; i < loops; i+= 2) {
237 LogError((unsigned char *) "Cannot read randomFile \"%s\", errno = %d\n", name, errno);
243 GenerateAuthData (auth, len)
253 X_GETTIMEOFDAY (&now);
254 ldata[0] = now.tv_sec;
255 ldata[1] = now.tv_usec;
261 ldata[0] = time ((long *) 0);
262 ldata[1] = getpid ();
264 #endif /* ITIMER_REAL */
270 auth_wrapper_schedule schedule;
271 unsigned char tdata[8];
272 static int xdmcpAuthInited;
274 longtochars (ldata[0], tdata+0);
275 longtochars (ldata[1], tdata+4);
276 if (!xdmcpAuthInited)
281 _XdmcpAuthSetup (key, schedule);
282 for (i = 0; i < len; i++) {
284 for (bit = 1; bit < 256; bit <<= 1) {
285 _XdmcpAuthDoIt (tdata, tdata, schedule, 1);
286 if (tdata[0] + tdata[1] & 0x4)
295 #if defined(USE_CRYPT)
298 char *result, *crypt ();
302 for (j = 0; j < 2; j++)
305 for (i = 0; i < 4; i++)
308 cdata[k] = sdata & 0xff;
315 for (i = 0; i < len; i += 4)
317 result = crypt (cdata, (const char *) key);
321 for (j = 0; j < k; j++)
322 auth[i + j] = result[2 + j];
323 for (j = 0; j < 8; j++)
324 cdata[j] = result[2 + j];
327 #elif defined(USE_ENCRYPT)
334 bitsToBytes (key, key_bits);
335 bitsToBytes (ldata, data_bits);
337 for (i = 0; i < len; i++) {
339 for (bit = 1; bit < 256; bit <<= 1) {
340 encrypt (data_bits, 0);
352 seed = (ldata[0] + key[0]) +
353 ((ldata[1] + key[1]) << 16);
355 for (i = 0; i < len; i++)
358 auth[i] = (value & 0xff00) >> 8;
361 if (value > sizeof (key))
362 value = sizeof (key);
363 memmove( (char *) key, auth, value);
371 static int cryptoInited = 0;
374 InitCryptoKey( void )
376 char *key_file = "/dev/mem";
382 * If the sumFile fails to produce a result
383 * use the time of day.
385 if (!sumFile (key_file, key)) {
390 struct timezone zone;
391 gettimeofday (&now, &zone);
393 key[1] = now.tv_usec;
399 key[0] = time ((long *) 0);
408 #endif /* HASXDMAUTH */