home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / clients / xdm / genauth.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-23  |  3.4 KB  |  161 lines

  1. /*
  2.  * xdm - display manager daemon
  3.  *
  4.  * $XConsortium: genauth.c,v 1.8 91/07/24 00:07:03 keith Exp $
  5.  *
  6.  * Copyright 1988 Massachusetts Institute of Technology
  7.  *
  8.  * Permission to use, copy, modify, and distribute this software and its
  9.  * documentation for any purpose and without fee is hereby granted, provided
  10.  * that the above copyright notice appear in all copies and that both that
  11.  * copyright notice and this permission notice appear in supporting
  12.  * documentation, and that the name of M.I.T. not be used in advertising or
  13.  * publicity pertaining to distribution of the software without specific,
  14.  * written prior permission.  M.I.T. makes no representations about the
  15.  * suitability of this software for any purpose.  It is provided "as is"
  16.  * without express or implied warranty.
  17.  *
  18.  * Author:  Keith Packard, MIT X Consortium
  19.  */
  20.  
  21. # include   <X11/Xauth.h>
  22. # include   <X11/Xos.h>
  23. # include   "dm.h"
  24.  
  25. static unsigned char    key[8];
  26.  
  27. #ifdef HASXDMAUTH
  28.  
  29. typedef unsigned char auth_cblock[8];    /* block size */
  30.  
  31. typedef struct auth_ks_struct { auth_cblock _; } auth_wrapper_schedule[16];
  32.  
  33. extern void _XdmcpWrapperToOddParity();
  34.  
  35. static
  36. longtochars (l, c)
  37.     long        l;
  38.     unsigned char    *c;
  39. {
  40.     c[0] = (l >> 24) & 0xff;
  41.     c[1] = (l >> 16) & 0xff;
  42.     c[2] = (l >> 8) & 0xff;
  43.     c[3] = l & 0xff;
  44. }
  45.  
  46.  
  47. # define FILE_LIMIT    1024    /* no more than this many buffers */
  48.  
  49. static
  50. sumFile (name, sum)
  51. char    *name;
  52. long    sum[2];
  53. {
  54.     long    buf[1024*2];
  55.     int        cnt;
  56.     int        fd;
  57.     int        loops;
  58.     int        reads;
  59.     int        i;
  60.  
  61.     fd = open (name, 0);
  62.     if (fd < 0)
  63.     return 0;
  64.     reads = FILE_LIMIT;
  65.     while ((cnt = read (fd, buf, sizeof (buf))) > 0 && --reads > 0) {
  66.     loops = cnt / (2 * sizeof (long));
  67.     for (i = 0; i < loops; i+= 2) {
  68.         sum[0] += buf[i];
  69.         sum[1] += buf[i+1];
  70.     }
  71.     }
  72.     close (fd);
  73.     return 1;
  74. }
  75.  
  76. static
  77. InitXdmcpWrapper ()
  78. {
  79.     long        sum[2];
  80.     unsigned char   tmpkey[8];
  81.     
  82.     if (!sumFile (randomFile, sum)) {
  83.     sum[0] = time ((long *) 0);
  84.     sum[1] = time ((long *) 0);
  85.     }
  86.     longtochars (sum[0], tmpkey+0);
  87.     longtochars (sum[1], tmpkey+4);
  88.     tmpkey[0] = 0;
  89.     _XdmcpWrapperToOddParity (tmpkey, key);
  90. }
  91.  
  92. #endif
  93.  
  94. GenerateAuthorization (auth, len)
  95. char    *auth;
  96. int    len;
  97. {
  98.     long        ldata[2];
  99.  
  100. #ifdef ITIMER_REAL
  101.     {
  102.     struct timeval  now;
  103.     struct timezone zone;
  104.     gettimeofday (&now, &zone);
  105.     ldata[0] = now.tv_sec;
  106.     ldata[1] = now.tv_usec;
  107.     }
  108. #else
  109.     {
  110.     long    time ();
  111.  
  112.     ldata[0] = time ((long *) 0);
  113.     ldata[1] = getpid ();
  114.     }
  115. #endif
  116. #ifdef HASXDMAUTH
  117.     {
  118.         int            bit;
  119.         int            i;
  120.     auth_wrapper_schedule    schedule;
  121.     unsigned char        data[8];
  122.     static int        xdmcpAuthInited;
  123.     
  124.     longtochars (ldata[0], data+0);
  125.     longtochars (ldata[1], data+4);
  126.     if (!xdmcpAuthInited)
  127.     {
  128.         InitXdmcpWrapper ();
  129.         xdmcpAuthInited = 1;
  130.     }
  131.     _XdmcpAuthSetup (key, schedule);
  132.         for (i = 0; i < len; i++) {
  133.         auth[i] = 0;
  134.         for (bit = 1; bit < 256; bit <<= 1) {
  135.             _XdmcpAuthDoIt (data, data, schedule, 1);
  136.             if (data[0] + data[1] & 0x4)
  137.             auth[i] |= bit;
  138.         }
  139.         }
  140.     }
  141. #else
  142.     {
  143.         int        seed;
  144.         int        value;
  145.         int        i;
  146.     
  147.         seed = (ldata[0]) + (ldata[1] << 16);
  148.         srand (seed);
  149.         for (i = 0; i < len; i++)
  150.         {
  151.         value = rand ();
  152.         auth[i] = value & 0xff;
  153.         }
  154.     value = len;
  155.     if (value > sizeof (key))
  156.         value = sizeof (key);
  157.         bcopy (auth, (char *) key, value);
  158.     }
  159. #endif
  160. }
  161.