1 /* $XConsortium: mitauth.c /main/11 1996/10/28 22:56:36 dpw $ */
4 Copyright (c) 1988 X Consortium
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
14 The above copyright notice and this permission notice shall be included
15 in all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 OTHER DEALINGS IN THE SOFTWARE.
25 Except as contained in this notice, the name of the X Consortium shall
26 not be used in advertising or otherwise to promote the sale, use or
27 other dealings in this Software without prior written authorization
28 from the X Consortium.
33 * MIT-MAGIC-COOKIE-1 authorization scheme
34 * Author: Keith Packard, MIT X Consortium
39 #include "dixstruct.h"
49 MitAddCookie (data_length, data, id)
50 unsigned short data_length;
56 new = (struct auth *) xalloc (sizeof (struct auth));
59 new->data = (char *) xalloc ((unsigned) data_length);
66 memmove(new->data, data, (int) data_length);
67 new->len = data_length;
73 MitCheckCookie (data_length, data, client, reason)
74 unsigned short data_length;
81 for (auth = mit_auth; auth; auth=auth->next) {
82 if (data_length == auth->len &&
83 memcmp (data, auth->data, (int) data_length) == 0)
86 *reason = "Invalid MIT-MAGIC-COOKIE-1 key";
93 struct auth *auth, *next;
95 for (auth = mit_auth; auth; auth=next) {
105 MitToID (data_length, data)
106 unsigned short data_length;
111 for (auth = mit_auth; auth; auth=auth->next) {
112 if (data_length == auth->len &&
113 memcmp (data, auth->data, data_length) == 0)
120 MitFromID (id, data_lenp, datap)
122 unsigned short *data_lenp;
127 for (auth = mit_auth; auth; auth=auth->next) {
128 if (id == auth->id) {
129 *data_lenp = auth->len;
138 MitRemoveCookie (data_length, data)
139 unsigned short data_length;
142 struct auth *auth, *prev;
145 for (auth = mit_auth; auth; prev = auth, auth=auth->next) {
146 if (data_length == auth->len &&
147 memcmp (data, auth->data, data_length) == 0)
150 prev->next = auth->next;
152 mit_auth = auth->next;
163 static char cookie[16]; /* 128 bits */
166 MitGenerateCookie (data_length, data, id, data_length_return, data_return)
167 unsigned int data_length;
170 unsigned int *data_length_return;
176 while (data_length--)
178 cookie[i++] += *data++;
179 if (i >= sizeof (cookie)) i = 0;
181 GenerateRandomData(sizeof (cookie), cookie);
182 status = MitAddCookie(sizeof (cookie), cookie, id);
189 *data_return = cookie;
190 *data_length_return = sizeof (cookie);
195 #endif /* XCSECURITY */