mitauth.c revision 030cabe0
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 const char *data, 54 XID id) 55{ 56 struct auth *new; 57 58 new = malloc(sizeof (struct auth)); 59 if (!new) 60 return 0; 61 new->data = malloc((unsigned) data_length); 62 if (!new->data) { 63 free(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 const 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 timingsafe_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 free(auth->data); 100 free(auth); 101 } 102 mit_auth = 0; 103 return 0; 104} 105 106int 107MitFromID ( 108 XID id, 109 unsigned short *data_lenp, 110 char **datap) 111{ 112 struct auth *auth; 113 114 for (auth = mit_auth; auth; auth=auth->next) { 115 if (id == auth->id) { 116 *data_lenp = auth->len; 117 *datap = auth->data; 118 return 1; 119 } 120 } 121 return 0; 122} 123 124int 125MitRemoveCookie ( 126 unsigned short data_length, 127 const char *data) 128{ 129 struct auth *auth, *prev; 130 131 prev = 0; 132 for (auth = mit_auth; auth; prev = auth, auth=auth->next) { 133 if (data_length == auth->len && 134 memcmp (data, auth->data, data_length) == 0) 135 { 136 if (prev) 137 prev->next = auth->next; 138 else 139 mit_auth = auth->next; 140 free(auth->data); 141 free(auth); 142 return 1; 143 } 144 } 145 return 0; 146} 147 148#ifdef XCSECURITY 149 150static char cookie[16]; /* 128 bits */ 151 152XID 153MitGenerateCookie ( 154 unsigned data_length, 155 const char *data, 156 XID id, 157 unsigned *data_length_return, 158 char **data_return) 159{ 160 int i = 0; 161 int status; 162 163 while (data_length--) 164 { 165 cookie[i++] += *data++; 166 if (i >= sizeof (cookie)) i = 0; 167 } 168 GenerateRandomData(sizeof (cookie), cookie); 169 status = MitAddCookie(sizeof (cookie), cookie, id); 170 if (!status) 171 { 172 id = -1; 173 } 174 else 175 { 176 *data_return = cookie; 177 *data_length_return = sizeof (cookie); 178 } 179 return id; 180} 181 182#endif /* XCSECURITY */ 183