home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / os / auth.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-24  |  5.9 KB  |  250 lines

  1. /*
  2.  * authorization hooks for the server
  3.  *
  4.  * $XConsortium: auth.c,v 1.12 91/07/24 18:36:16 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   "X.h"
  22. # include   "Xauth.h"
  23. # include   "misc.h"
  24.  
  25. struct protocol {
  26.     unsigned short   name_length;
  27.     char    *name;
  28.     int     (*Add)();        /* new authorization data */
  29.     XID        (*Check)();        /* verify client authorization data */
  30.     int     (*Reset)();        /* delete all authorization data entries */
  31.     XID        (*ToID)();        /* convert cookie to ID */
  32.     int        (*FromID)();    /* convert ID to cookie */
  33.     int        (*Remove)();    /* remove a specific cookie */
  34. };
  35.  
  36. extern int  MitAddCookie ();
  37. extern XID  MitCheckCookie ();
  38. extern int  MitResetCookie ();
  39. extern XID  MitToID ();
  40. extern int  MitFromID (), MitRemoveCookie ();
  41.  
  42. #ifdef HASXDMAUTH
  43. extern int  XdmAddCookie ();
  44. extern XID  XdmCheckCookie ();
  45. extern int  XdmResetCookie ();
  46. extern XID  XdmToID ();
  47. extern int  XdmFromID (), XdmRemoveCookie ();
  48. #endif
  49.  
  50. #ifdef SECURE_RPC
  51. extern int  SecureRPCAdd();
  52. extern XID  SecureRPCCheck();
  53. extern int  SecureRPCReset();
  54. extern XID  SecureRPCToID();
  55. extern int  SecureRPCFromID(), SecureRPCRemove();
  56. #endif
  57.  
  58. static struct protocol   protocols[] = {
  59. {   (unsigned short) 18,    "MIT-MAGIC-COOKIE-1",
  60.         MitAddCookie,    MitCheckCookie,    MitResetCookie,
  61.         MitToID,    MitFromID,    MitRemoveCookie,
  62. },
  63. #ifdef HASXDMAUTH
  64. {   (unsigned short) 19,    "XDM-AUTHORIZATION-1",
  65.         XdmAddCookie,    XdmCheckCookie,    XdmResetCookie,
  66.         XdmToID,    XdmFromID,    XdmRemoveCookie,
  67. },
  68. #endif
  69. #ifdef SECURE_RPC
  70. {   (unsigned short) 9,    "SUN-DES-1",
  71.         SecureRPCAdd,    SecureRPCCheck,    SecureRPCReset,
  72.         SecureRPCToID,    SecureRPCFromID,SecureRPCRemove,
  73. },
  74. #endif
  75. };
  76.  
  77. # define NUM_AUTHORIZATION  (sizeof (protocols) /\
  78.                  sizeof (struct protocol))
  79.  
  80. /*
  81.  * Initialize all classes of authorization by reading the
  82.  * specified authorization file
  83.  */
  84.  
  85. static char *authorization_file = (char *)NULL;
  86.  
  87. static int  AuthorizationIndex = 0;
  88. static Bool ShouldLoadAuth = TRUE;
  89.  
  90. InitAuthorization (file_name)
  91. char    *file_name;
  92. {
  93.     authorization_file = file_name;
  94. }
  95.  
  96. int
  97. LoadAuthorization ()
  98. {
  99.     FILE    *f;
  100.     Xauth   *auth;
  101.     int        i;
  102.     int        count = 0;
  103.  
  104.     ShouldLoadAuth = FALSE;
  105.     if (!authorization_file)
  106.     return 0;
  107.     f = fopen (authorization_file, "r");
  108.     if (!f)
  109.     return 0;
  110.     AuthorizationIndex = 0;
  111.     while (auth = XauReadAuth (f)) {
  112.     for (i = 0; i < NUM_AUTHORIZATION; i++) {
  113.         if (protocols[i].name_length == auth->name_length &&
  114.         bcmp (protocols[i].name, auth->name, (int) auth->name_length) == 0)
  115.         {
  116.         ++count;
  117.         (*protocols[i].Add) (auth->data_length, auth->data,
  118.                      ++AuthorizationIndex);
  119.         }
  120.     }
  121.     XauDisposeAuth (auth);
  122.     }
  123.     fclose (f);
  124.     return count;
  125. }
  126.  
  127. #ifdef XDMCP
  128. /*
  129.  * XdmcpInit calls this function to discover all authorization
  130.  * schemes supported by the display
  131.  */
  132. RegisterAuthorizations ()
  133. {
  134.     int        i;
  135.  
  136.     for (i = 0; i < NUM_AUTHORIZATION; i++)
  137.     XdmcpRegisterAuthorization (protocols[i].name,
  138.                     (int)protocols[i].name_length);
  139. }
  140. #endif
  141.  
  142. XID
  143. CheckAuthorization (name_length, name, data_length, data)
  144. unsigned short    name_length;
  145. char    *name;
  146. unsigned short    data_length;
  147. char    *data;
  148. {
  149.     int    i;
  150.  
  151.     if (ShouldLoadAuth)
  152.     {
  153.     if (!LoadAuthorization())
  154.         EnableLocalHost ();
  155.     }
  156.     if (name_length)
  157.     for (i = 0; i < NUM_AUTHORIZATION; i++) {
  158.         if (protocols[i].name_length == name_length &&
  159.         bcmp (protocols[i].name, name, (int) name_length) == 0)
  160.         {
  161.         return (*protocols[i].Check) (data_length, data);
  162.         }
  163.     }
  164.     return (XID) ~0L;
  165. }
  166.  
  167. ResetAuthorization ()
  168. {
  169.     int    i;
  170.  
  171.     for (i = 0; i < NUM_AUTHORIZATION; i++)
  172.     (*protocols[i].Reset)();
  173.     ShouldLoadAuth = TRUE;
  174. }
  175.  
  176. XID
  177. AuthorizationToID (name_length, name, data_length, data)
  178. unsigned short    name_length;
  179. char    *name;
  180. unsigned short    data_length;
  181. char    *data;
  182. {
  183.     int    i;
  184.  
  185.     for (i = 0; i < NUM_AUTHORIZATION; i++) {
  186.         if (protocols[i].name_length == name_length &&
  187.         bcmp (protocols[i].name, name, (int) name_length) == 0)
  188.         {
  189.         return (*protocols[i].ToID) (data_length, data);
  190.         }
  191.     }
  192.     return (XID) ~0L;
  193. }
  194.  
  195. AuthorizationFromID (id, name_lenp, namep, data_lenp, datap)
  196. XID id;
  197. unsigned short    *name_lenp;
  198. char    **namep;
  199. unsigned short    *data_lenp;
  200. char    **datap;
  201. {
  202.     int    i;
  203.  
  204.     for (i = 0; i < NUM_AUTHORIZATION; i++) {
  205.     if ((*protocols[i].FromID) (id, data_lenp, datap)) {
  206.         *name_lenp = protocols[i].name_length;
  207.         *namep = protocols[i].name;
  208.         return 1;
  209.     }
  210.     }
  211.     return 0;
  212. }
  213.  
  214. RemoveAuthorization (name_length, name, data_length, data)
  215. unsigned short    name_length;
  216. char    *name;
  217. unsigned short    data_length;
  218. char    *data;
  219. {
  220.     int    i;
  221.  
  222.     for (i = 0; i < NUM_AUTHORIZATION; i++) {
  223.         if (protocols[i].name_length == name_length &&
  224.         bcmp (protocols[i].name, name, (int) name_length) == 0)
  225.         {
  226.         return (*protocols[i].Remove) (data_length, data);
  227.         }
  228.     }
  229.     return 0;
  230. }
  231.  
  232. AddAuthorization (name_length, name, data_length, data)
  233. unsigned short    name_length;
  234. char    *name;
  235. unsigned short    data_length;
  236. char    *data;
  237. {
  238.     int    i;
  239.  
  240.     for (i = 0; i < NUM_AUTHORIZATION; i++) {
  241.         if (protocols[i].name_length == name_length &&
  242.         bcmp (protocols[i].name, name, (int) name_length) == 0)
  243.         {
  244.         return (*protocols[i].Add) (data_length, data,
  245.                     ++AuthorizationIndex);
  246.         }
  247.     }
  248.     return 0;
  249. }
  250.