auth.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. Copyright 1988, 1998 The Open Group
  3. Permission to use, copy, modify, distribute, and sell this software and its
  4. documentation for any purpose is hereby granted without fee, provided that
  5. the above copyright notice appear in all copies and that both that
  6. copyright notice and this permission notice appear in supporting
  7. documentation.
  8. The above copyright notice and this permission notice shall be included
  9. in all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  11. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  12. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  13. IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
  14. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  15. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  16. OTHER DEALINGS IN THE SOFTWARE.
  17. Except as contained in this notice, the name of The Open Group shall
  18. not be used in advertising or otherwise to promote the sale, use or
  19. other dealings in this Software without prior written authorization
  20. from The Open Group.
  21. */
  22. /*
  23. * authorization hooks for the server
  24. * Author: Keith Packard, MIT X Consortium
  25. */
  26. #ifdef HAVE_DIX_CONFIG_H
  27. #include <dix-config.h>
  28. #endif
  29. # include <X11/X.h>
  30. # include <X11/Xauth.h>
  31. # include "misc.h"
  32. # include "osdep.h"
  33. # include "dixstruct.h"
  34. # include <sys/types.h>
  35. # include <sys/stat.h>
  36. struct protocol {
  37. unsigned short name_length;
  38. char *name;
  39. AuthAddCFunc Add; /* new authorization data */
  40. AuthCheckFunc Check; /* verify client authorization data */
  41. AuthRstCFunc Reset; /* delete all authorization data entries */
  42. AuthToIDFunc ToID; /* convert cookie to ID */
  43. AuthFromIDFunc FromID; /* convert ID to cookie */
  44. AuthRemCFunc Remove; /* remove a specific cookie */
  45. };
  46. static const struct protocol protocols[] = {
  47. { (unsigned short) 18, "MIT-MAGIC-COOKIE-1",
  48. MitAddCookie, MitCheckCookie, MitResetCookie,
  49. MitToID, MitFromID, MitRemoveCookie,
  50. },
  51. #ifdef HASXDMAUTH
  52. { (unsigned short) 19, "XDM-AUTHORIZATION-1",
  53. XdmAddCookie, XdmCheckCookie, XdmResetCookie,
  54. XdmToID, XdmFromID, XdmRemoveCookie,
  55. },
  56. #endif
  57. #ifdef SECURE_RPC
  58. { (unsigned short) 9, "SUN-DES-1",
  59. SecureRPCAdd, SecureRPCCheck, SecureRPCReset,
  60. SecureRPCToID, SecureRPCFromID,SecureRPCRemove,
  61. },
  62. #endif
  63. };
  64. # define NUM_AUTHORIZATION (sizeof (protocols) /\
  65. sizeof (struct protocol))
  66. /*
  67. * Initialize all classes of authorization by reading the
  68. * specified authorization file
  69. */
  70. static char *authorization_file = (char *)NULL;
  71. static Bool ShouldLoadAuth = TRUE;
  72. void
  73. InitAuthorization (char *file_name)
  74. {
  75. authorization_file = file_name;
  76. }
  77. static int
  78. LoadAuthorization (void)
  79. {
  80. FILE *f;
  81. Xauth *auth;
  82. int i;
  83. int count = 0;
  84. ShouldLoadAuth = FALSE;
  85. if (!authorization_file)
  86. return 0;
  87. f = Fopen (authorization_file, "r");
  88. if (!f)
  89. return -1;
  90. while ((auth = XauReadAuth (f)) != 0) {
  91. for (i = 0; i < NUM_AUTHORIZATION; i++) {
  92. if (protocols[i].name_length == auth->name_length &&
  93. memcmp (protocols[i].name, auth->name, (int) auth->name_length) == 0 &&
  94. protocols[i].Add)
  95. {
  96. ++count;
  97. (*protocols[i].Add) (auth->data_length, auth->data,
  98. FakeClientID(0));
  99. }
  100. }
  101. XauDisposeAuth (auth);
  102. }
  103. Fclose (f);
  104. return count;
  105. }
  106. #ifdef XDMCP
  107. /*
  108. * XdmcpInit calls this function to discover all authorization
  109. * schemes supported by the display
  110. */
  111. void
  112. RegisterAuthorizations (void)
  113. {
  114. int i;
  115. for (i = 0; i < NUM_AUTHORIZATION; i++)
  116. XdmcpRegisterAuthorization (protocols[i].name,
  117. (int)protocols[i].name_length);
  118. }
  119. #endif
  120. XID
  121. CheckAuthorization (
  122. unsigned int name_length,
  123. const char *name,
  124. unsigned int data_length,
  125. const char *data,
  126. ClientPtr client,
  127. const char **reason) /* failure message. NULL for default msg */
  128. {
  129. int i;
  130. struct stat buf;
  131. static time_t lastmod = 0;
  132. static Bool loaded = FALSE;
  133. if (!authorization_file || stat(authorization_file, &buf))
  134. {
  135. if (lastmod != 0) {
  136. lastmod = 0;
  137. ShouldLoadAuth = TRUE; /* stat lost, so force reload */
  138. }
  139. }
  140. else if (buf.st_mtime > lastmod)
  141. {
  142. lastmod = buf.st_mtime;
  143. ShouldLoadAuth = TRUE;
  144. }
  145. if (ShouldLoadAuth)
  146. {
  147. int loadauth = LoadAuthorization();
  148. /*
  149. * If the authorization file has at least one entry for this server,
  150. * disable local host access. (loadauth > 0)
  151. *
  152. * If there are zero entries (either initially or when the
  153. * authorization file is later reloaded), or if a valid
  154. * authorization file was never loaded, enable local host access.
  155. * (loadauth == 0 || !loaded)
  156. *
  157. * If the authorization file was loaded initially (with valid
  158. * entries for this server), and reloading it later fails, don't
  159. * change anything. (loadauth == -1 && loaded)
  160. */
  161. if (loadauth > 0)
  162. {
  163. DisableLocalHost(); /* got at least one */
  164. loaded = TRUE;
  165. }
  166. else if (loadauth == 0 || !loaded)
  167. EnableLocalHost ();
  168. }
  169. if (name_length) {
  170. for (i = 0; i < NUM_AUTHORIZATION; i++) {
  171. if (protocols[i].name_length == name_length &&
  172. memcmp (protocols[i].name, name, (int) name_length) == 0)
  173. {
  174. return (*protocols[i].Check) (data_length, data, client, reason);
  175. }
  176. *reason = "Protocol not supported by server\n";
  177. }
  178. } else *reason = "No protocol specified\n";
  179. return (XID) ~0L;
  180. }
  181. void
  182. ResetAuthorization (void)
  183. {
  184. int i;
  185. for (i = 0; i < NUM_AUTHORIZATION; i++)
  186. if (protocols[i].Reset)
  187. (*protocols[i].Reset)();
  188. ShouldLoadAuth = TRUE;
  189. }
  190. int
  191. AddAuthorization (unsigned name_length, const char *name,
  192. unsigned data_length, char *data)
  193. {
  194. int i;
  195. for (i = 0; i < NUM_AUTHORIZATION; i++) {
  196. if (protocols[i].name_length == name_length &&
  197. memcmp (protocols[i].name, name, (int) name_length) == 0 &&
  198. protocols[i].Add)
  199. {
  200. return (*protocols[i].Add) (data_length, data, FakeClientID(0));
  201. }
  202. }
  203. return 0;
  204. }