First import
[xorg_rtime.git] / xorg-server-1.4 / os / mitauth.c
blobdb66c44fbd666666f70eacf221a26624756bd683
1 /*
3 Copyright 1988, 1998 The Open Group
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
9 documentation.
11 The above copyright notice and this permission notice shall be included
12 in all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 OTHER DEALINGS IN THE SOFTWARE.
22 Except as contained in this notice, the name of The Open Group shall
23 not be used in advertising or otherwise to promote the sale, use or
24 other dealings in this Software without prior written authorization
25 from The Open Group.
30 * MIT-MAGIC-COOKIE-1 authorization scheme
31 * Author: Keith Packard, MIT X Consortium
34 #ifdef HAVE_DIX_CONFIG_H
35 #include <dix-config.h>
36 #endif
38 #include <X11/X.h>
39 #include "os.h"
40 #include "osdep.h"
41 #include "dixstruct.h"
43 static struct auth {
44 struct auth *next;
45 unsigned short len;
46 char *data;
47 XID id;
48 } *mit_auth;
50 int
51 MitAddCookie (
52 unsigned short data_length,
53 char *data,
54 XID id)
56 struct auth *new;
58 new = (struct auth *) xalloc (sizeof (struct auth));
59 if (!new)
60 return 0;
61 new->data = (char *) xalloc ((unsigned) data_length);
62 if (!new->data) {
63 xfree(new);
64 return 0;
66 new->next = mit_auth;
67 mit_auth = new;
68 memmove(new->data, data, (int) data_length);
69 new->len = data_length;
70 new->id = id;
71 return 1;
74 XID
75 MitCheckCookie (
76 unsigned short data_length,
77 char *data,
78 ClientPtr client,
79 char **reason)
81 struct auth *auth;
83 for (auth = mit_auth; auth; auth=auth->next) {
84 if (data_length == auth->len &&
85 memcmp (data, auth->data, (int) data_length) == 0)
86 return auth->id;
88 *reason = "Invalid MIT-MAGIC-COOKIE-1 key";
89 return (XID) -1;
92 int
93 MitResetCookie (void)
95 struct auth *auth, *next;
97 for (auth = mit_auth; auth; auth=next) {
98 next = auth->next;
99 xfree (auth->data);
100 xfree (auth);
102 mit_auth = 0;
103 return 0;
107 MitToID (
108 unsigned short data_length,
109 char *data)
111 struct auth *auth;
113 for (auth = mit_auth; auth; auth=auth->next) {
114 if (data_length == auth->len &&
115 memcmp (data, auth->data, data_length) == 0)
116 return auth->id;
118 return (XID) -1;
122 MitFromID (
123 XID id,
124 unsigned short *data_lenp,
125 char **datap)
127 struct auth *auth;
129 for (auth = mit_auth; auth; auth=auth->next) {
130 if (id == auth->id) {
131 *data_lenp = auth->len;
132 *datap = auth->data;
133 return 1;
136 return 0;
140 MitRemoveCookie (
141 unsigned short data_length,
142 char *data)
144 struct auth *auth, *prev;
146 prev = 0;
147 for (auth = mit_auth; auth; prev = auth, auth=auth->next) {
148 if (data_length == auth->len &&
149 memcmp (data, auth->data, data_length) == 0)
151 if (prev)
152 prev->next = auth->next;
153 else
154 mit_auth = auth->next;
155 xfree (auth->data);
156 xfree (auth);
157 return 1;
160 return 0;
163 #ifdef XCSECURITY
165 static char cookie[16]; /* 128 bits */
168 MitGenerateCookie (
169 unsigned data_length,
170 char *data,
171 XID id,
172 unsigned *data_length_return,
173 char **data_return)
175 int i = 0;
176 int status;
178 while (data_length--)
180 cookie[i++] += *data++;
181 if (i >= sizeof (cookie)) i = 0;
183 GenerateRandomData(sizeof (cookie), cookie);
184 status = MitAddCookie(sizeof (cookie), cookie, id);
185 if (!status)
187 id = -1;
189 else
191 *data_return = cookie;
192 *data_length_return = sizeof (cookie);
194 return id;
197 #endif /* XCSECURITY */