mitauth.c revision 05b261ec
105b261ecSmrg/* 205b261ecSmrg 305b261ecSmrgCopyright 1988, 1998 The Open Group 405b261ecSmrg 505b261ecSmrgPermission to use, copy, modify, distribute, and sell this software and its 605b261ecSmrgdocumentation for any purpose is hereby granted without fee, provided that 705b261ecSmrgthe above copyright notice appear in all copies and that both that 805b261ecSmrgcopyright notice and this permission notice appear in supporting 905b261ecSmrgdocumentation. 1005b261ecSmrg 1105b261ecSmrgThe above copyright notice and this permission notice shall be included 1205b261ecSmrgin all copies or substantial portions of the Software. 1305b261ecSmrg 1405b261ecSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 1505b261ecSmrgOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 1605b261ecSmrgMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 1705b261ecSmrgIN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR 1805b261ecSmrgOTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 1905b261ecSmrgARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 2005b261ecSmrgOTHER DEALINGS IN THE SOFTWARE. 2105b261ecSmrg 2205b261ecSmrgExcept as contained in this notice, the name of The Open Group shall 2305b261ecSmrgnot be used in advertising or otherwise to promote the sale, use or 2405b261ecSmrgother dealings in this Software without prior written authorization 2505b261ecSmrgfrom The Open Group. 2605b261ecSmrg 2705b261ecSmrg*/ 2805b261ecSmrg 2905b261ecSmrg/* 3005b261ecSmrg * MIT-MAGIC-COOKIE-1 authorization scheme 3105b261ecSmrg * Author: Keith Packard, MIT X Consortium 3205b261ecSmrg */ 3305b261ecSmrg 3405b261ecSmrg#ifdef HAVE_DIX_CONFIG_H 3505b261ecSmrg#include <dix-config.h> 3605b261ecSmrg#endif 3705b261ecSmrg 3805b261ecSmrg#include <X11/X.h> 3905b261ecSmrg#include "os.h" 4005b261ecSmrg#include "osdep.h" 4105b261ecSmrg#include "dixstruct.h" 4205b261ecSmrg 4305b261ecSmrgstatic struct auth { 4405b261ecSmrg struct auth *next; 4505b261ecSmrg unsigned short len; 4605b261ecSmrg char *data; 4705b261ecSmrg XID id; 4805b261ecSmrg} *mit_auth; 4905b261ecSmrg 5005b261ecSmrgint 5105b261ecSmrgMitAddCookie ( 5205b261ecSmrg unsigned short data_length, 5305b261ecSmrg char *data, 5405b261ecSmrg XID id) 5505b261ecSmrg{ 5605b261ecSmrg struct auth *new; 5705b261ecSmrg 5805b261ecSmrg new = (struct auth *) xalloc (sizeof (struct auth)); 5905b261ecSmrg if (!new) 6005b261ecSmrg return 0; 6105b261ecSmrg new->data = (char *) xalloc ((unsigned) data_length); 6205b261ecSmrg if (!new->data) { 6305b261ecSmrg xfree(new); 6405b261ecSmrg return 0; 6505b261ecSmrg } 6605b261ecSmrg new->next = mit_auth; 6705b261ecSmrg mit_auth = new; 6805b261ecSmrg memmove(new->data, data, (int) data_length); 6905b261ecSmrg new->len = data_length; 7005b261ecSmrg new->id = id; 7105b261ecSmrg return 1; 7205b261ecSmrg} 7305b261ecSmrg 7405b261ecSmrgXID 7505b261ecSmrgMitCheckCookie ( 7605b261ecSmrg unsigned short data_length, 7705b261ecSmrg char *data, 7805b261ecSmrg ClientPtr client, 7905b261ecSmrg char **reason) 8005b261ecSmrg{ 8105b261ecSmrg struct auth *auth; 8205b261ecSmrg 8305b261ecSmrg for (auth = mit_auth; auth; auth=auth->next) { 8405b261ecSmrg if (data_length == auth->len && 8505b261ecSmrg memcmp (data, auth->data, (int) data_length) == 0) 8605b261ecSmrg return auth->id; 8705b261ecSmrg } 8805b261ecSmrg *reason = "Invalid MIT-MAGIC-COOKIE-1 key"; 8905b261ecSmrg return (XID) -1; 9005b261ecSmrg} 9105b261ecSmrg 9205b261ecSmrgint 9305b261ecSmrgMitResetCookie (void) 9405b261ecSmrg{ 9505b261ecSmrg struct auth *auth, *next; 9605b261ecSmrg 9705b261ecSmrg for (auth = mit_auth; auth; auth=next) { 9805b261ecSmrg next = auth->next; 9905b261ecSmrg xfree (auth->data); 10005b261ecSmrg xfree (auth); 10105b261ecSmrg } 10205b261ecSmrg mit_auth = 0; 10305b261ecSmrg return 0; 10405b261ecSmrg} 10505b261ecSmrg 10605b261ecSmrgXID 10705b261ecSmrgMitToID ( 10805b261ecSmrg unsigned short data_length, 10905b261ecSmrg char *data) 11005b261ecSmrg{ 11105b261ecSmrg struct auth *auth; 11205b261ecSmrg 11305b261ecSmrg for (auth = mit_auth; auth; auth=auth->next) { 11405b261ecSmrg if (data_length == auth->len && 11505b261ecSmrg memcmp (data, auth->data, data_length) == 0) 11605b261ecSmrg return auth->id; 11705b261ecSmrg } 11805b261ecSmrg return (XID) -1; 11905b261ecSmrg} 12005b261ecSmrg 12105b261ecSmrgint 12205b261ecSmrgMitFromID ( 12305b261ecSmrg XID id, 12405b261ecSmrg unsigned short *data_lenp, 12505b261ecSmrg char **datap) 12605b261ecSmrg{ 12705b261ecSmrg struct auth *auth; 12805b261ecSmrg 12905b261ecSmrg for (auth = mit_auth; auth; auth=auth->next) { 13005b261ecSmrg if (id == auth->id) { 13105b261ecSmrg *data_lenp = auth->len; 13205b261ecSmrg *datap = auth->data; 13305b261ecSmrg return 1; 13405b261ecSmrg } 13505b261ecSmrg } 13605b261ecSmrg return 0; 13705b261ecSmrg} 13805b261ecSmrg 13905b261ecSmrgint 14005b261ecSmrgMitRemoveCookie ( 14105b261ecSmrg unsigned short data_length, 14205b261ecSmrg char *data) 14305b261ecSmrg{ 14405b261ecSmrg struct auth *auth, *prev; 14505b261ecSmrg 14605b261ecSmrg prev = 0; 14705b261ecSmrg for (auth = mit_auth; auth; prev = auth, auth=auth->next) { 14805b261ecSmrg if (data_length == auth->len && 14905b261ecSmrg memcmp (data, auth->data, data_length) == 0) 15005b261ecSmrg { 15105b261ecSmrg if (prev) 15205b261ecSmrg prev->next = auth->next; 15305b261ecSmrg else 15405b261ecSmrg mit_auth = auth->next; 15505b261ecSmrg xfree (auth->data); 15605b261ecSmrg xfree (auth); 15705b261ecSmrg return 1; 15805b261ecSmrg } 15905b261ecSmrg } 16005b261ecSmrg return 0; 16105b261ecSmrg} 16205b261ecSmrg 16305b261ecSmrg#ifdef XCSECURITY 16405b261ecSmrg 16505b261ecSmrgstatic char cookie[16]; /* 128 bits */ 16605b261ecSmrg 16705b261ecSmrgXID 16805b261ecSmrgMitGenerateCookie ( 16905b261ecSmrg unsigned data_length, 17005b261ecSmrg char *data, 17105b261ecSmrg XID id, 17205b261ecSmrg unsigned *data_length_return, 17305b261ecSmrg char **data_return) 17405b261ecSmrg{ 17505b261ecSmrg int i = 0; 17605b261ecSmrg int status; 17705b261ecSmrg 17805b261ecSmrg while (data_length--) 17905b261ecSmrg { 18005b261ecSmrg cookie[i++] += *data++; 18105b261ecSmrg if (i >= sizeof (cookie)) i = 0; 18205b261ecSmrg } 18305b261ecSmrg GenerateRandomData(sizeof (cookie), cookie); 18405b261ecSmrg status = MitAddCookie(sizeof (cookie), cookie, id); 18505b261ecSmrg if (!status) 18605b261ecSmrg { 18705b261ecSmrg id = -1; 18805b261ecSmrg } 18905b261ecSmrg else 19005b261ecSmrg { 19105b261ecSmrg *data_return = cookie; 19205b261ecSmrg *data_length_return = sizeof (cookie); 19305b261ecSmrg } 19405b261ecSmrg return id; 19505b261ecSmrg} 19605b261ecSmrg 19705b261ecSmrg#endif /* XCSECURITY */ 198