1 /*
2
3 Copyright 1988, 1998 The Open Group
4
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.
10
11 The above copyright notice and this permission notice shall be included
12 in all copies or substantial portions of the Software.
13
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.
21
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.
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
43 static struct auth {
44 struct auth *next;
45 unsigned short len;
46 char *data;
47 XID id;
48 } *mit_auth;
49
50 int
MitAddCookie(unsigned short data_length,const char * data,XID id)51 MitAddCookie(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
71 XID
MitCheckCookie(unsigned short data_length,const char * data,ClientPtr client,const char ** reason)72 MitCheckCookie(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 timingsafe_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
86 int
MitResetCookie(void)87 MitResetCookie(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
100 int
MitFromID(XID id,unsigned short * data_lenp,char ** datap)101 MitFromID(XID id, unsigned short *data_lenp, char **datap)
102 {
103 struct auth *auth;
104
105 for (auth = mit_auth; auth; auth = auth->next) {
106 if (id == auth->id) {
107 *data_lenp = auth->len;
108 *datap = auth->data;
109 return 1;
110 }
111 }
112 return 0;
113 }
114
115 int
MitRemoveCookie(unsigned short data_length,const char * data)116 MitRemoveCookie(unsigned short data_length, const char *data)
117 {
118 struct auth *auth, *prev;
119
120 prev = 0;
121 for (auth = mit_auth; auth; prev = auth, auth = auth->next) {
122 if (data_length == auth->len &&
123 memcmp(data, auth->data, data_length) == 0) {
124 if (prev)
125 prev->next = auth->next;
126 else
127 mit_auth = auth->next;
128 free(auth->data);
129 free(auth);
130 return 1;
131 }
132 }
133 return 0;
134 }
135
136 static char cookie[16]; /* 128 bits */
137
138 XID
MitGenerateCookie(unsigned data_length,const char * data,XID id,unsigned * data_length_return,char ** data_return)139 MitGenerateCookie(unsigned data_length,
140 const char *data,
141 XID id, unsigned *data_length_return, char **data_return)
142 {
143 int i = 0;
144 int status;
145
146 while (data_length--) {
147 cookie[i++] += *data++;
148 if (i >= sizeof(cookie))
149 i = 0;
150 }
151 GenerateRandomData(sizeof(cookie), cookie);
152 status = MitAddCookie(sizeof(cookie), cookie, id);
153 if (!status) {
154 id = -1;
155 }
156 else {
157 *data_return = cookie;
158 *data_length_return = sizeof(cookie);
159 }
160 return id;
161 }
162