iceauth.c revision c5629e66
1266e564dSmrg/* $Xorg: iceauth.c,v 1.4 2001/02/09 02:03:26 xorgcvs Exp $ */ 2266e564dSmrg/****************************************************************************** 3266e564dSmrg 4266e564dSmrg 5266e564dSmrgCopyright 1993, 1998 The Open Group 6266e564dSmrg 7266e564dSmrgPermission to use, copy, modify, distribute, and sell this software and its 8266e564dSmrgdocumentation for any purpose is hereby granted without fee, provided that 9266e564dSmrgthe above copyright notice appear in all copies and that both that 10266e564dSmrgcopyright notice and this permission notice appear in supporting 11266e564dSmrgdocumentation. 12266e564dSmrg 13266e564dSmrgThe above copyright notice and this permission notice shall be included in 14266e564dSmrgall copies or substantial portions of the Software. 15266e564dSmrg 16266e564dSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17266e564dSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18266e564dSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19266e564dSmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 20266e564dSmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21266e564dSmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22266e564dSmrg 23266e564dSmrgExcept as contained in this notice, the name of The Open Group shall not be 24266e564dSmrgused in advertising or otherwise to promote the sale, use or other dealings 25266e564dSmrgin this Software without prior written authorization from The Open Group. 26266e564dSmrg 27266e564dSmrgAuthor: Ralph Mor, X Consortium 28266e564dSmrg******************************************************************************/ 29266e564dSmrg/* $XFree86: xc/lib/ICE/iceauth.c,v 3.5 2001/12/14 19:53:36 dawes Exp $ */ 30266e564dSmrg 31266e564dSmrg#ifdef HAVE_CONFIG_H 32266e564dSmrg#include <config.h> 33266e564dSmrg#endif 34266e564dSmrg#include <X11/ICE/ICElib.h> 35266e564dSmrg#include "ICElibint.h" 36266e564dSmrg#include <X11/ICE/ICEutil.h> 37266e564dSmrg 38266e564dSmrg#include <time.h> 39266e564dSmrg#define Time_t time_t 40266e564dSmrg 41266e564dSmrgstatic int was_called_state; 42266e564dSmrg 43266e564dSmrg/* 44266e564dSmrg * MIT-MAGIC-COOKIE-1 is a sample authentication method implemented by 45266e564dSmrg * the SI. It is not part of standard ICElib. 46266e564dSmrg */ 47266e564dSmrg 48266e564dSmrg 49266e564dSmrgchar * 50c5629e66SmrgIceGenerateMagicCookie ( 51c5629e66Smrg int len 52c5629e66Smrg) 53266e564dSmrg{ 54266e564dSmrg char *auth; 55266e564dSmrg long ldata[2]; 56266e564dSmrg int seed; 57266e564dSmrg int value; 58266e564dSmrg int i; 59266e564dSmrg 60266e564dSmrg if ((auth = (char *) malloc (len + 1)) == NULL) 61266e564dSmrg return (NULL); 62266e564dSmrg 63266e564dSmrg#ifdef ITIMER_REAL 64266e564dSmrg { 65266e564dSmrg struct timeval now; 66266e564dSmrg X_GETTIMEOFDAY (&now); 67266e564dSmrg ldata[0] = now.tv_sec; 68266e564dSmrg ldata[1] = now.tv_usec; 69266e564dSmrg } 70266e564dSmrg#else 71266e564dSmrg { 72266e564dSmrg#ifndef __UNIXOS2__ 73266e564dSmrg long time (); 74266e564dSmrg#endif 75266e564dSmrg ldata[0] = time ((long *) 0); 76266e564dSmrg ldata[1] = getpid (); 77266e564dSmrg } 78266e564dSmrg#endif 79266e564dSmrg seed = (ldata[0]) + (ldata[1] << 16); 80266e564dSmrg srand (seed); 81266e564dSmrg for (i = 0; i < len; i++) 82266e564dSmrg { 83266e564dSmrg value = rand (); 84266e564dSmrg auth[i] = value & 0xff; 85266e564dSmrg } 86266e564dSmrg auth[len] = '\0'; 87266e564dSmrg 88266e564dSmrg return (auth); 89266e564dSmrg} 90266e564dSmrg 91266e564dSmrg 92266e564dSmrg 93266e564dSmrgIcePoAuthStatus 94c5629e66Smrg_IcePoMagicCookie1Proc ( 95c5629e66Smrg IceConn iceConn, 96c5629e66Smrg IcePointer *authStatePtr, 97c5629e66Smrg Bool cleanUp, 98c5629e66Smrg Bool swap, 99c5629e66Smrg int authDataLen, 100c5629e66Smrg IcePointer authData, 101c5629e66Smrg int *replyDataLenRet, 102c5629e66Smrg IcePointer *replyDataRet, 103c5629e66Smrg char **errorStringRet 104c5629e66Smrg) 105266e564dSmrg{ 106266e564dSmrg if (cleanUp) 107266e564dSmrg { 108266e564dSmrg /* 109266e564dSmrg * We didn't allocate any state. We're done. 110266e564dSmrg */ 111266e564dSmrg 112266e564dSmrg return (IcePoAuthDoneCleanup); 113266e564dSmrg } 114266e564dSmrg 115266e564dSmrg *errorStringRet = NULL; 116266e564dSmrg 117266e564dSmrg if (*authStatePtr == NULL) 118266e564dSmrg { 119266e564dSmrg /* 120266e564dSmrg * This is the first time we're being called. Search the 121266e564dSmrg * authentication data for the first occurence of 122266e564dSmrg * MIT-MAGIC-COOKIE-1 that matches iceConn->connection_string. 123266e564dSmrg */ 124266e564dSmrg 125266e564dSmrg unsigned short length; 126266e564dSmrg char *data; 127266e564dSmrg 128266e564dSmrg _IceGetPoAuthData ("ICE", iceConn->connection_string, 129266e564dSmrg "MIT-MAGIC-COOKIE-1", &length, &data); 130266e564dSmrg 131266e564dSmrg if (!data) 132266e564dSmrg { 133266e564dSmrg const char *tempstr = 134266e564dSmrg "Could not find correct MIT-MAGIC-COOKIE-1 authentication"; 135266e564dSmrg 136266e564dSmrg *errorStringRet = strdup(tempstr); 137266e564dSmrg 138266e564dSmrg return (IcePoAuthFailed); 139266e564dSmrg } 140266e564dSmrg else 141266e564dSmrg { 142266e564dSmrg *authStatePtr = (IcePointer) &was_called_state; 143266e564dSmrg 144266e564dSmrg *replyDataLenRet = length; 145266e564dSmrg *replyDataRet = data; 146266e564dSmrg 147266e564dSmrg return (IcePoAuthHaveReply); 148266e564dSmrg } 149266e564dSmrg } 150266e564dSmrg else 151266e564dSmrg { 152266e564dSmrg /* 153266e564dSmrg * We should never get here for MIT-MAGIC-COOKIE-1 since it is 154266e564dSmrg * a single pass authentication method. 155266e564dSmrg */ 156266e564dSmrg 157266e564dSmrg const char *tempstr = 158266e564dSmrg "MIT-MAGIC-COOKIE-1 authentication internal error"; 159266e564dSmrg 160266e564dSmrg *errorStringRet = strdup(tempstr); 161266e564dSmrg 162266e564dSmrg return (IcePoAuthFailed); 163266e564dSmrg } 164266e564dSmrg} 165266e564dSmrg 166c5629e66SmrgIcePoAuthProc _IcePoAuthProcs[] = {_IcePoMagicCookie1Proc}; 167266e564dSmrg 168266e564dSmrg 169266e564dSmrgIcePaAuthStatus 170c5629e66Smrg_IcePaMagicCookie1Proc ( 171c5629e66Smrg IceConn iceConn, 172c5629e66Smrg IcePointer *authStatePtr, 173c5629e66Smrg Bool swap, 174c5629e66Smrg int authDataLen, 175c5629e66Smrg IcePointer authData, 176c5629e66Smrg int *replyDataLenRet, 177c5629e66Smrg IcePointer *replyDataRet, 178c5629e66Smrg char **errorStringRet 179c5629e66Smrg) 180266e564dSmrg{ 181266e564dSmrg *errorStringRet = NULL; 182266e564dSmrg *replyDataLenRet = 0; 183266e564dSmrg *replyDataRet = NULL; 184266e564dSmrg 185266e564dSmrg if (*authStatePtr == NULL) 186266e564dSmrg { 187266e564dSmrg /* 188266e564dSmrg * This is the first time we're being called. We don't have 189266e564dSmrg * any data to pass to the other client. 190266e564dSmrg */ 191266e564dSmrg 192266e564dSmrg *authStatePtr = (IcePointer) &was_called_state; 193266e564dSmrg 194266e564dSmrg return (IcePaAuthContinue); 195266e564dSmrg } 196266e564dSmrg else 197266e564dSmrg { 198266e564dSmrg /* 199266e564dSmrg * Search the authentication data for the first occurence of 200266e564dSmrg * MIT-MAGIC-COOKIE-1 that matches iceConn->connection_string. 201266e564dSmrg */ 202266e564dSmrg 203266e564dSmrg unsigned short length; 204266e564dSmrg char *data; 205266e564dSmrg 206266e564dSmrg _IceGetPaAuthData ("ICE", iceConn->connection_string, 207266e564dSmrg "MIT-MAGIC-COOKIE-1", &length, &data); 208266e564dSmrg 209266e564dSmrg if (data) 210266e564dSmrg { 211266e564dSmrg IcePaAuthStatus stat; 212266e564dSmrg 213266e564dSmrg if (authDataLen == length && 214c5629e66Smrg memcmp (authData, data, authDataLen) == 0) 215266e564dSmrg { 216266e564dSmrg stat = IcePaAuthAccepted; 217266e564dSmrg } 218266e564dSmrg else 219266e564dSmrg { 220266e564dSmrg const char *tempstr 221266e564dSmrg = "MIT-MAGIC-COOKIE-1 authentication rejected"; 222266e564dSmrg 223266e564dSmrg *errorStringRet = strdup(tempstr); 224266e564dSmrg 225266e564dSmrg stat = IcePaAuthRejected; 226266e564dSmrg } 227266e564dSmrg 228266e564dSmrg free (data); 229266e564dSmrg return (stat); 230266e564dSmrg } 231266e564dSmrg else 232266e564dSmrg { 233266e564dSmrg /* 234266e564dSmrg * We should never get here because in the ConnectionReply 235266e564dSmrg * we should have passed all the valid methods. So we should 236266e564dSmrg * always find a valid entry. 237266e564dSmrg */ 238266e564dSmrg 239266e564dSmrg const char *tempstr = 240266e564dSmrg "MIT-MAGIC-COOKIE-1 authentication internal error"; 241266e564dSmrg 242266e564dSmrg *errorStringRet = strdup(tempstr); 243266e564dSmrg 244266e564dSmrg return (IcePaAuthFailed); 245266e564dSmrg } 246266e564dSmrg } 247266e564dSmrg} 248266e564dSmrg 249c5629e66SmrgIcePaAuthProc _IcePaAuthProcs[] = {_IcePaMagicCookie1Proc}; 250