mitauth.c revision 05b261ec
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 ( 52 unsigned short data_length, 53 char *data, 54 XID id) 55{ 56 struct auth *new; 57 58 new = (struct auth *) xalloc (sizeof (struct auth)); 59 if (!new) 60 return 0; 61 new->data = (char *) xalloc ((unsigned) data_length); 62 if (!new->data) { 63 xfree(new); 64 return 0; 65 } 66 new->next = mit_auth; 67 mit_auth = new; 68 memmove(new->data, data, (int) data_length); 69 new->len = data_length; 70 new->id = id; 71 return 1; 72} 73 74XID 75MitCheckCookie ( 76 unsigned short data_length, 77 char *data, 78 ClientPtr client, 79 char **reason) 80{ 81 struct auth *auth; 82 83 for (auth = mit_auth; auth; auth=auth->next) { 84 if (data_length == auth->len && 85 memcmp (data, auth->data, (int) data_length) == 0) 86 return auth->id; 87 } 88 *reason = "Invalid MIT-MAGIC-COOKIE-1 key"; 89 return (XID) -1; 90} 91 92int 93MitResetCookie (void) 94{ 95 struct auth *auth, *next; 96 97 for (auth = mit_auth; auth; auth=next) { 98 next = auth->next; 99 xfree (auth->data); 100 xfree (auth); 101 } 102 mit_auth = 0; 103 return 0; 104} 105 106XID 107MitToID ( 108 unsigned short data_length, 109 char *data) 110{ 111 struct auth *auth; 112 113 for (auth = mit_auth; auth; auth=auth->next) { 114 if (data_length == auth->len && 115 memcmp (data, auth->data, data_length) == 0) 116 return auth->id; 117 } 118 return (XID) -1; 119} 120 121int 122MitFromID ( 123 XID id, 124 unsigned short *data_lenp, 125 char **datap) 126{ 127 struct auth *auth; 128 129 for (auth = mit_auth; auth; auth=auth->next) { 130 if (id == auth->id) { 131 *data_lenp = auth->len; 132 *datap = auth->data; 133 return 1; 134 } 135 } 136 return 0; 137} 138 139int 140MitRemoveCookie ( 141 unsigned short data_length, 142 char *data) 143{ 144 struct auth *auth, *prev; 145 146 prev = 0; 147 for (auth = mit_auth; auth; prev = auth, auth=auth->next) { 148 if (data_length == auth->len && 149 memcmp (data, auth->data, data_length) == 0) 150 { 151 if (prev) 152 prev->next = auth->next; 153 else 154 mit_auth = auth->next; 155 xfree (auth->data); 156 xfree (auth); 157 return 1; 158 } 159 } 160 return 0; 161} 162 163#ifdef XCSECURITY 164 165static char cookie[16]; /* 128 bits */ 166 167XID 168MitGenerateCookie ( 169 unsigned data_length, 170 char *data, 171 XID id, 172 unsigned *data_length_return, 173 char **data_return) 174{ 175 int i = 0; 176 int status; 177 178 while (data_length--) 179 { 180 cookie[i++] += *data++; 181 if (i >= sizeof (cookie)) i = 0; 182 } 183 GenerateRandomData(sizeof (cookie), cookie); 184 status = MitAddCookie(sizeof (cookie), cookie, id); 185 if (!status) 186 { 187 id = -1; 188 } 189 else 190 { 191 *data_return = cookie; 192 *data_length_return = sizeof (cookie); 193 } 194 return id; 195} 196 197#endif /* XCSECURITY */ 198