home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / os / mitauth.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-14  |  2.9 KB  |  137 lines

  1. /*
  2.  * MIT-MAGIC-COOKIE-1 authorization scheme
  3.  *
  4.  * $XConsortium: mitauth.c,v 1.3 89/03/14 15:53:36 rws 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 "os.h"
  23.  
  24. static struct auth {
  25.     struct auth    *next;
  26.     unsigned short    len;
  27.     char    *data;
  28.     XID        id;
  29. } *mit_auth;
  30.  
  31. int
  32. MitAddCookie (data_length, data, id)
  33. unsigned short    data_length;
  34. char    *data;
  35. XID    id;
  36. {
  37.     struct auth    *new;
  38.  
  39.     new = (struct auth *) xalloc (sizeof (struct auth));
  40.     if (!new)
  41.     return 0;
  42.     new->data = (char *) xalloc ((unsigned) data_length);
  43.     if (!new->data) {
  44.     xfree(new);
  45.     return 0;
  46.     }
  47.     new->next = mit_auth;
  48.     mit_auth = new;
  49.     bcopy (data, new->data, (int) data_length);
  50.     new->len = data_length;
  51.     new->id = id;
  52.     return 1;
  53. }
  54.  
  55. XID
  56. MitCheckCookie (data_length, data)
  57. unsigned short    data_length;
  58. char    *data;
  59. {
  60.     struct auth    *auth;
  61.  
  62.     for (auth = mit_auth; auth; auth=auth->next) {
  63.         if (data_length == auth->len &&
  64.        bcmp (data, auth->data, (int) data_length) == 0)
  65.         return auth->id;
  66.     }
  67.     return (XID) -1;
  68. }
  69.  
  70. int
  71. MitResetCookie ()
  72. {
  73.     struct auth    *auth, *next;
  74.  
  75.     for (auth = mit_auth; auth; auth=next) {
  76.     next = auth->next;
  77.     xfree (auth->data);
  78.     xfree (auth);
  79.     }
  80.     mit_auth = 0;
  81. }
  82.  
  83. XID
  84. MitToID (data_length, data)
  85. unsigned short    data_length;
  86. char    *data;
  87. {
  88.     struct auth    *auth;
  89.  
  90.     for (auth = mit_auth; auth; auth=auth->next) {
  91.     if (data_length == auth->len &&
  92.         bcmp (data, auth->data, data_length) == 0)
  93.         return auth->id;
  94.     }
  95.     return (XID) -1;
  96. }
  97.  
  98. MitFromID (id, data_lenp, datap)
  99. XID id;
  100. unsigned short    *data_lenp;
  101. char    **datap;
  102. {
  103.     struct auth    *auth;
  104.  
  105.     for (auth = mit_auth; auth; auth=auth->next) {
  106.     if (id == auth->id) {
  107.         *data_lenp = auth->len;
  108.         *datap = auth->data;
  109.         return 1;
  110.     }
  111.     }
  112.     return 0;
  113. }
  114.  
  115. MitRemoveCookie (data_length, data)
  116. unsigned short    data_length;
  117. char    *data;
  118. {
  119.     struct auth    *auth, *prev;
  120.  
  121.     prev = 0;
  122.     for (auth = mit_auth; auth; auth=auth->next) {
  123.     if (data_length == auth->len &&
  124.         bcmp (data, auth->data, data_length) == 0)
  125.      {
  126.         if (prev)
  127.         prev->next = auth->next;
  128.         else
  129.         mit_auth = auth->next;
  130.         xfree (auth->data);
  131.         xfree (auth);
  132.         return 1;
  133.     }
  134.     }
  135.     return 0;
  136. }
  137.