mitauth.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. * MIT-MAGIC-COOKIE-1 authorization scheme
  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 "os.h"
  31. #include "osdep.h"
  32. #include "dixstruct.h"
  33. static struct auth {
  34. struct auth *next;
  35. unsigned short len;
  36. char *data;
  37. XID id;
  38. } *mit_auth;
  39. int
  40. MitAddCookie (
  41. unsigned short data_length,
  42. const char *data,
  43. XID id)
  44. {
  45. struct auth *new;
  46. new = malloc(sizeof (struct auth));
  47. if (!new)
  48. return 0;
  49. new->data = malloc((unsigned) data_length);
  50. if (!new->data) {
  51. free(new);
  52. return 0;
  53. }
  54. new->next = mit_auth;
  55. mit_auth = new;
  56. memmove(new->data, data, (int) data_length);
  57. new->len = data_length;
  58. new->id = id;
  59. return 1;
  60. }
  61. XID
  62. MitCheckCookie (
  63. unsigned short data_length,
  64. const char *data,
  65. ClientPtr client,
  66. const char **reason)
  67. {
  68. struct auth *auth;
  69. for (auth = mit_auth; auth; auth=auth->next) {
  70. if (data_length == auth->len &&
  71. memcmp (data, auth->data, (int) data_length) == 0)
  72. return auth->id;
  73. }
  74. *reason = "Invalid MIT-MAGIC-COOKIE-1 key";
  75. return (XID) -1;
  76. }
  77. int
  78. MitResetCookie (void)
  79. {
  80. struct auth *auth, *next;
  81. for (auth = mit_auth; auth; auth=next) {
  82. next = auth->next;
  83. free(auth->data);
  84. free(auth);
  85. }
  86. mit_auth = 0;
  87. return 0;
  88. }
  89. XID
  90. MitToID (
  91. unsigned short data_length,
  92. char *data)
  93. {
  94. struct auth *auth;
  95. for (auth = mit_auth; auth; auth=auth->next) {
  96. if (data_length == auth->len &&
  97. memcmp (data, auth->data, data_length) == 0)
  98. return auth->id;
  99. }
  100. return (XID) -1;
  101. }
  102. int
  103. MitFromID (
  104. XID id,
  105. unsigned short *data_lenp,
  106. char **datap)
  107. {
  108. struct auth *auth;
  109. for (auth = mit_auth; auth; auth=auth->next) {
  110. if (id == auth->id) {
  111. *data_lenp = auth->len;
  112. *datap = auth->data;
  113. return 1;
  114. }
  115. }
  116. return 0;
  117. }
  118. int
  119. MitRemoveCookie (
  120. unsigned short data_length,
  121. const char *data)
  122. {
  123. struct auth *auth, *prev;
  124. prev = 0;
  125. for (auth = mit_auth; auth; prev = auth, auth=auth->next) {
  126. if (data_length == auth->len &&
  127. memcmp (data, auth->data, data_length) == 0)
  128. {
  129. if (prev)
  130. prev->next = auth->next;
  131. else
  132. mit_auth = auth->next;
  133. free(auth->data);
  134. free(auth);
  135. return 1;
  136. }
  137. }
  138. return 0;
  139. }