Add patch that contain Mali fixes.
[deb_xorg-server.git] / os / mitauth.c
CommitLineData
a09e091a
JB
1/*
2
3Copyright 1988, 1998 The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included
12in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20OTHER DEALINGS IN THE SOFTWARE.
21
22Except as contained in this notice, the name of The Open Group shall
23not be used in advertising or otherwise to promote the sale, use or
24other dealings in this Software without prior written authorization
25from The Open Group.
26
27*/
28
29/*
30 * MIT-MAGIC-COOKIE-1 authorization scheme
31 * Author: Keith Packard, MIT X Consortium
32 */
33
34#ifdef HAVE_DIX_CONFIG_H
35#include <dix-config.h>
36#endif
37
38#include <X11/X.h>
39#include "os.h"
40#include "osdep.h"
41#include "dixstruct.h"
42
43static struct auth {
44 struct auth *next;
45 unsigned short len;
46 char *data;
47 XID id;
48} *mit_auth;
49
50int
51MitAddCookie(unsigned short data_length, const char *data, XID id)
52{
53 struct auth *new;
54
55 new = malloc(sizeof(struct auth));
56 if (!new)
57 return 0;
58 new->data = malloc((unsigned) data_length);
59 if (!new->data) {
60 free(new);
61 return 0;
62 }
63 new->next = mit_auth;
64 mit_auth = new;
65 memmove(new->data, data, (int) data_length);
66 new->len = data_length;
67 new->id = id;
68 return 1;
69}
70
71XID
72MitCheckCookie(unsigned short data_length,
73 const char *data, ClientPtr client, const char **reason)
74{
75 struct auth *auth;
76
77 for (auth = mit_auth; auth; auth = auth->next) {
78 if (data_length == auth->len &&
79 memcmp(data, auth->data, (int) data_length) == 0)
80 return auth->id;
81 }
82 *reason = "Invalid MIT-MAGIC-COOKIE-1 key";
83 return (XID) -1;
84}
85
86int
87MitResetCookie(void)
88{
89 struct auth *auth, *next;
90
91 for (auth = mit_auth; auth; auth = next) {
92 next = auth->next;
93 free(auth->data);
94 free(auth);
95 }
96 mit_auth = 0;
97 return 0;
98}
99
100XID
101MitToID(unsigned short data_length, char *data)
102{
103 struct auth *auth;
104
105 for (auth = mit_auth; auth; auth = auth->next) {
106 if (data_length == auth->len &&
107 memcmp(data, auth->data, data_length) == 0)
108 return auth->id;
109 }
110 return (XID) -1;
111}
112
113int
114MitFromID(XID id, unsigned short *data_lenp, char **datap)
115{
116 struct auth *auth;
117
118 for (auth = mit_auth; auth; auth = auth->next) {
119 if (id == auth->id) {
120 *data_lenp = auth->len;
121 *datap = auth->data;
122 return 1;
123 }
124 }
125 return 0;
126}
127
128int
129MitRemoveCookie(unsigned short data_length, const char *data)
130{
131 struct auth *auth, *prev;
132
133 prev = 0;
134 for (auth = mit_auth; auth; prev = auth, auth = auth->next) {
135 if (data_length == auth->len &&
136 memcmp(data, auth->data, data_length) == 0) {
137 if (prev)
138 prev->next = auth->next;
139 else
140 mit_auth = auth->next;
141 free(auth->data);
142 free(auth);
143 return 1;
144 }
145 }
146 return 0;
147}
148
149#ifdef XCSECURITY
150
151static char cookie[16]; /* 128 bits */
152
153XID
154MitGenerateCookie(unsigned data_length,
155 const char *data,
156 XID id, unsigned *data_length_return, char **data_return)
157{
158 int i = 0;
159 int status;
160
161 while (data_length--) {
162 cookie[i++] += *data++;
163 if (i >= sizeof(cookie))
164 i = 0;
165 }
166 GenerateRandomData(sizeof(cookie), cookie);
167 status = MitAddCookie(sizeof(cookie), cookie, id);
168 if (!status) {
169 id = -1;
170 }
171 else {
172 *data_return = cookie;
173 *data_length_return = sizeof(cookie);
174 }
175 return id;
176}
177
178#endif /* XCSECURITY */