]> git.sesse.net Git - rdpsrv/blob - Xserver/programs/Xserver/os/mitauth.c
Support RDP5 logon packets.
[rdpsrv] / Xserver / programs / Xserver / os / mitauth.c
1 /* $XConsortium: mitauth.c /main/11 1996/10/28 22:56:36 dpw $ */
2 /*
3
4 Copyright (c) 1988  X Consortium
5
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:
13
14 The above copyright notice and this permission notice shall be included
15 in all copies or substantial portions of the Software.
16
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.
24
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.
29
30 */
31
32 /*
33  * MIT-MAGIC-COOKIE-1 authorization scheme
34  * Author:  Keith Packard, MIT X Consortium
35  */
36
37 #include "X.h"
38 #include "os.h"
39 #include "dixstruct.h"
40
41 static struct auth {
42     struct auth *next;
43     unsigned short      len;
44     char        *data;
45     XID         id;
46 } *mit_auth;
47
48 int
49 MitAddCookie (data_length, data, id)
50 unsigned short  data_length;
51 char    *data;
52 XID     id;
53 {
54     struct auth *new;
55
56     new = (struct auth *) xalloc (sizeof (struct auth));
57     if (!new)
58         return 0;
59     new->data = (char *) xalloc ((unsigned) data_length);
60     if (!new->data) {
61         xfree(new);
62         return 0;
63     }
64     new->next = mit_auth;
65     mit_auth = new;
66     memmove(new->data, data, (int) data_length);
67     new->len = data_length;
68     new->id = id;
69     return 1;
70 }
71
72 XID
73 MitCheckCookie (data_length, data, client, reason)
74     unsigned short      data_length;
75     char        *data;
76     ClientPtr client;
77     char        **reason;
78 {
79     struct auth *auth;
80
81     for (auth = mit_auth; auth; auth=auth->next) {
82         if (data_length == auth->len &&
83            memcmp (data, auth->data, (int) data_length) == 0)
84             return auth->id;
85     }
86     *reason = "Invalid MIT-MAGIC-COOKIE-1 key";
87     return (XID) -1;
88 }
89
90 int
91 MitResetCookie ()
92 {
93     struct auth *auth, *next;
94
95     for (auth = mit_auth; auth; auth=next) {
96         next = auth->next;
97         xfree (auth->data);
98         xfree (auth);
99     }
100     mit_auth = 0;
101     return 0;
102 }
103
104 XID
105 MitToID (data_length, data)
106 unsigned short  data_length;
107 char    *data;
108 {
109     struct auth *auth;
110
111     for (auth = mit_auth; auth; auth=auth->next) {
112         if (data_length == auth->len &&
113             memcmp (data, auth->data, data_length) == 0)
114             return auth->id;
115     }
116     return (XID) -1;
117 }
118
119 int
120 MitFromID (id, data_lenp, datap)
121 XID id;
122 unsigned short  *data_lenp;
123 char    **datap;
124 {
125     struct auth *auth;
126
127     for (auth = mit_auth; auth; auth=auth->next) {
128         if (id == auth->id) {
129             *data_lenp = auth->len;
130             *datap = auth->data;
131             return 1;
132         }
133     }
134     return 0;
135 }
136
137 int
138 MitRemoveCookie (data_length, data)
139 unsigned short  data_length;
140 char    *data;
141 {
142     struct auth *auth, *prev;
143
144     prev = 0;
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)
148         {
149             if (prev)
150                 prev->next = auth->next;
151             else
152                 mit_auth = auth->next;
153             xfree (auth->data);
154             xfree (auth);
155             return 1;
156         }
157     }
158     return 0;
159 }
160
161 #ifdef XCSECURITY
162
163 static char cookie[16]; /* 128 bits */
164
165 XID
166 MitGenerateCookie (data_length, data, id, data_length_return, data_return)
167     unsigned int data_length;
168     char *data;
169     XID id;
170     unsigned int *data_length_return;
171     char        **data_return;
172 {
173     int i = 0;
174     int status;
175
176     while (data_length--)
177     {
178         cookie[i++] += *data++;
179         if (i >= sizeof (cookie)) i = 0;
180     }
181     GenerateRandomData(sizeof (cookie), cookie);
182     status = MitAddCookie(sizeof (cookie), cookie, id);
183     if (!status)
184     {
185         id = -1;
186     }
187     else
188     {
189         *data_return = cookie;
190         *data_length_return = sizeof (cookie);
191     }
192     return id;
193 }
194
195 #endif /* XCSECURITY */