getauth.c revision 266e564d
1/* $Xorg: getauth.c,v 1.4 2001/02/09 02:03:26 xorgcvs Exp $ */ 2/****************************************************************************** 3 4 5Copyright 1993, 1998 The Open Group 6 7Permission to use, copy, modify, distribute, and sell this software and its 8documentation for any purpose is hereby granted without fee, provided that 9the above copyright notice appear in all copies and that both that 10copyright notice and this permission notice appear in supporting 11documentation. 12 13The above copyright notice and this permission notice shall be included in 14all copies or substantial portions of the Software. 15 16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 20AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 23Except as contained in this notice, the name of The Open Group shall not be 24used in advertising or otherwise to promote the sale, use or other dealings 25in this Software without prior written authorization from The Open Group. 26 27Author: Ralph Mor, X Consortium 28******************************************************************************/ 29/* $XFree86: xc/lib/ICE/getauth.c,v 1.2 2001/10/28 03:32:28 tsi Exp $ */ 30 31#ifdef HAVE_CONFIG_H 32#include <config.h> 33#endif 34#include <X11/ICE/ICElib.h> 35#include "ICElibint.h" 36#include <X11/ICE/ICEutil.h> 37 38static Bool auth_valid (const char *auth_name, int num_auth_names, 39 char **auth_names, int *index_ret); 40 41extern int _IcePaAuthDataEntryCount; 42extern IceAuthDataEntry _IcePaAuthDataEntries[]; 43 44 45/* 46 * The functions in this file are not a standard part of ICElib. 47 * 48 * The sample implementation uses an .ICEauthority to manipulate 49 * authentication data. 50 * 51 * For the client that initiates a Protocol Setup, we look in the 52 * .ICEauthority file to get the data. 53 * 54 * For the client accepting the Protocol Setup, we get the data 55 * from an in-memory database of authentication data (set by the 56 * application calling IceSetPaAuthData). We have to get the data 57 * from memory because getting it directly from the .ICEauthority 58 * file is not secure - someone can just modify the contents of the 59 * .ICEauthority file behind our back. 60 */ 61 62void 63_IceGetPoAuthData (protocolName, networkId, authName, 64 authDataLenRet, authDataRet) 65 66char *protocolName; 67char *networkId; 68char *authName; 69unsigned short *authDataLenRet; 70char **authDataRet; 71 72{ 73 IceAuthFileEntry *entry; 74 75 entry = IceGetAuthFileEntry (protocolName, networkId, authName); 76 77 if (entry) 78 { 79 *authDataLenRet = entry->auth_data_length; 80 81 if ((*authDataRet = (char *) malloc (entry->auth_data_length)) != NULL) 82 memcpy (*authDataRet, entry->auth_data, entry->auth_data_length); 83 } 84 else 85 { 86 *authDataLenRet = 0; 87 *authDataRet = NULL; 88 } 89 90 IceFreeAuthFileEntry (entry); 91} 92 93 94 95void 96_IceGetPaAuthData (protocolName, networkId, authName, 97 authDataLenRet, authDataRet) 98 99char *protocolName; 100char *networkId; 101char *authName; 102unsigned short *authDataLenRet; 103char **authDataRet; 104 105{ 106 IceAuthDataEntry *entry = NULL; 107 int found = 0; 108 int i; 109 110 for (i = 0; i < _IcePaAuthDataEntryCount && !found; i++) 111 { 112 entry = &_IcePaAuthDataEntries[i]; 113 114 found = 115 strcmp (protocolName, entry->protocol_name) == 0 && 116 strcmp (networkId, entry->network_id) == 0 && 117 strcmp (authName, entry->auth_name) == 0; 118 } 119 120 if (found) 121 { 122 *authDataLenRet = entry->auth_data_length; 123 124 if ((*authDataRet = (char *) malloc (entry->auth_data_length)) != NULL) 125 memcpy (*authDataRet, entry->auth_data, entry->auth_data_length); 126 } 127 else 128 { 129 *authDataLenRet = 0; 130 *authDataRet = NULL; 131 } 132} 133 134 135 136void 137_IceGetPoValidAuthIndices (protocol_name, network_id, 138 num_auth_names, auth_names, num_indices_ret, indices_ret) 139 140char *protocol_name; 141char *network_id; 142int num_auth_names; 143char **auth_names; 144int *num_indices_ret; 145int *indices_ret; /* in/out arg */ 146 147{ 148 FILE *auth_file; 149 char *filename; 150 IceAuthFileEntry *entry; 151 int index_ret, i; 152 153 *num_indices_ret = 0; 154 155 if (!(filename = IceAuthFileName ())) 156 return; 157 158 if (access (filename, R_OK) != 0) /* checks REAL id */ 159 return; 160 161 if (!(auth_file = fopen (filename, "rb"))) 162 return; 163 164 for (;;) 165 { 166 if (!(entry = IceReadAuthFileEntry (auth_file))) 167 break; 168 169 if (strcmp (protocol_name, entry->protocol_name) == 0 && 170 strcmp (network_id, entry->network_id) == 0 && 171 auth_valid (entry->auth_name, num_auth_names, 172 auth_names, &index_ret)) 173 { 174 /* 175 * Make sure we didn't store this index already. 176 */ 177 178 for (i = 0; i < *num_indices_ret; i++) 179 if (index_ret == indices_ret[i]) 180 break; 181 182 if (i >= *num_indices_ret) 183 { 184 indices_ret[*num_indices_ret] = index_ret; 185 *num_indices_ret += 1; 186 } 187 } 188 189 IceFreeAuthFileEntry (entry); 190 } 191 192 fclose (auth_file); 193} 194 195 196 197void 198_IceGetPaValidAuthIndices (protocol_name, network_id, 199 num_auth_names, auth_names, num_indices_ret, indices_ret) 200 201char *protocol_name; 202char *network_id; 203int num_auth_names; 204char **auth_names; 205int *num_indices_ret; 206int *indices_ret; /* in/out arg */ 207 208{ 209 int index_ret; 210 int i, j; 211 IceAuthDataEntry *entry; 212 213 *num_indices_ret = 0; 214 215 for (i = 0; i < _IcePaAuthDataEntryCount; i++) 216 { 217 entry = &_IcePaAuthDataEntries[i]; 218 219 if (strcmp (protocol_name, entry->protocol_name) == 0 && 220 strcmp (network_id, entry->network_id) == 0 && 221 auth_valid (entry->auth_name, num_auth_names, 222 auth_names, &index_ret)) 223 { 224 /* 225 * Make sure we didn't store this index already. 226 */ 227 228 for (j = 0; j < *num_indices_ret; j++) 229 if (index_ret == indices_ret[j]) 230 break; 231 232 if (j >= *num_indices_ret) 233 { 234 indices_ret[*num_indices_ret] = index_ret; 235 *num_indices_ret += 1; 236 } 237 } 238 } 239} 240 241 242 243/* 244 * local routines 245 */ 246 247static Bool 248auth_valid (const char *auth_name, int num_auth_names, 249 char **auth_names, int *index_ret) 250 251{ 252 /* 253 * Check if auth_name is in auth_names. Return index. 254 */ 255 256 int i; 257 258 for (i = 0; i < num_auth_names; i++) 259 if (strcmp (auth_name, auth_names[i]) == 0) 260 { 261 break; 262 } 263 264 if (i < num_auth_names) 265 { 266 *index_ret = i; 267 return (1); 268 } 269 else 270 return (0); 271} 272