mitauth.c revision 7e31ba66
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            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
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
100int
101MitFromID(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
115int
116MitRemoveCookie(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
136static char cookie[16];         /* 128 bits */
137
138XID
139MitGenerateCookie(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