getauth.c revision c5629e66
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 ( 64 char *protocolName, 65 char *networkId, 66 char *authName, 67 unsigned short *authDataLenRet, 68 char **authDataRet 69) 70{ 71 IceAuthFileEntry *entry; 72 73 entry = IceGetAuthFileEntry (protocolName, networkId, authName); 74 75 if (entry) 76 { 77 *authDataLenRet = entry->auth_data_length; 78 79 if ((*authDataRet = (char *) malloc (entry->auth_data_length)) != NULL) 80 memcpy (*authDataRet, entry->auth_data, entry->auth_data_length); 81 } 82 else 83 { 84 *authDataLenRet = 0; 85 *authDataRet = NULL; 86 } 87 88 IceFreeAuthFileEntry (entry); 89} 90 91 92 93void 94_IceGetPaAuthData ( 95 char *protocolName, 96 char *networkId, 97 char *authName, 98 unsigned short *authDataLenRet, 99 char **authDataRet 100) 101{ 102 IceAuthDataEntry *entry = NULL; 103 int found = 0; 104 int i; 105 106 for (i = 0; i < _IcePaAuthDataEntryCount && !found; i++) 107 { 108 entry = &_IcePaAuthDataEntries[i]; 109 110 found = 111 strcmp (protocolName, entry->protocol_name) == 0 && 112 strcmp (networkId, entry->network_id) == 0 && 113 strcmp (authName, entry->auth_name) == 0; 114 } 115 116 if (found) 117 { 118 *authDataLenRet = entry->auth_data_length; 119 120 if ((*authDataRet = (char *) malloc (entry->auth_data_length)) != NULL) 121 memcpy (*authDataRet, entry->auth_data, entry->auth_data_length); 122 } 123 else 124 { 125 *authDataLenRet = 0; 126 *authDataRet = NULL; 127 } 128} 129 130 131 132void 133_IceGetPoValidAuthIndices ( 134 char *protocol_name, 135 char *network_id, 136 int num_auth_names, 137 char **auth_names, 138 int *num_indices_ret, 139 int *indices_ret /* in/out arg */ 140) 141{ 142 FILE *auth_file; 143 char *filename; 144 IceAuthFileEntry *entry; 145 int index_ret, i; 146 147 *num_indices_ret = 0; 148 149 if (!(filename = IceAuthFileName ())) 150 return; 151 152 if (access (filename, R_OK) != 0) /* checks REAL id */ 153 return; 154 155 if (!(auth_file = fopen (filename, "rb"))) 156 return; 157 158 for (;;) 159 { 160 if (!(entry = IceReadAuthFileEntry (auth_file))) 161 break; 162 163 if (strcmp (protocol_name, entry->protocol_name) == 0 && 164 strcmp (network_id, entry->network_id) == 0 && 165 auth_valid (entry->auth_name, num_auth_names, 166 auth_names, &index_ret)) 167 { 168 /* 169 * Make sure we didn't store this index already. 170 */ 171 172 for (i = 0; i < *num_indices_ret; i++) 173 if (index_ret == indices_ret[i]) 174 break; 175 176 if (i >= *num_indices_ret) 177 { 178 indices_ret[*num_indices_ret] = index_ret; 179 *num_indices_ret += 1; 180 } 181 } 182 183 IceFreeAuthFileEntry (entry); 184 } 185 186 fclose (auth_file); 187} 188 189 190 191void 192_IceGetPaValidAuthIndices ( 193 char *protocol_name, 194 char *network_id, 195 int num_auth_names, 196 char **auth_names, 197 int *num_indices_ret, 198 int *indices_ret /* in/out arg */ 199) 200{ 201 int index_ret; 202 int i, j; 203 IceAuthDataEntry *entry; 204 205 *num_indices_ret = 0; 206 207 for (i = 0; i < _IcePaAuthDataEntryCount; i++) 208 { 209 entry = &_IcePaAuthDataEntries[i]; 210 211 if (strcmp (protocol_name, entry->protocol_name) == 0 && 212 strcmp (network_id, entry->network_id) == 0 && 213 auth_valid (entry->auth_name, num_auth_names, 214 auth_names, &index_ret)) 215 { 216 /* 217 * Make sure we didn't store this index already. 218 */ 219 220 for (j = 0; j < *num_indices_ret; j++) 221 if (index_ret == indices_ret[j]) 222 break; 223 224 if (j >= *num_indices_ret) 225 { 226 indices_ret[*num_indices_ret] = index_ret; 227 *num_indices_ret += 1; 228 } 229 } 230 } 231} 232 233 234 235/* 236 * local routines 237 */ 238 239static Bool 240auth_valid (const char *auth_name, int num_auth_names, 241 char **auth_names, int *index_ret) 242 243{ 244 /* 245 * Check if auth_name is in auth_names. Return index. 246 */ 247 248 int i; 249 250 for (i = 0; i < num_auth_names; i++) 251 if (strcmp (auth_name, auth_names[i]) == 0) 252 { 253 break; 254 } 255 256 if (i < num_auth_names) 257 { 258 *index_ret = i; 259 return (1); 260 } 261 else 262 return (0); 263} 264