getauth.c revision 698f425b
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 37static Bool auth_valid (const char *auth_name, int num_auth_names, 38 char **auth_names, int *index_ret); 39 40 41/* 42 * The functions in this file are not a standard part of ICElib. 43 * 44 * The sample implementation uses an .ICEauthority to manipulate 45 * authentication data. 46 * 47 * For the client that initiates a Protocol Setup, we look in the 48 * .ICEauthority file to get the data. 49 * 50 * For the client accepting the Protocol Setup, we get the data 51 * from an in-memory database of authentication data (set by the 52 * application calling IceSetPaAuthData). We have to get the data 53 * from memory because getting it directly from the .ICEauthority 54 * file is not secure - someone can just modify the contents of the 55 * .ICEauthority file behind our back. 56 */ 57 58void 59_IceGetPoAuthData ( 60 char *protocolName, 61 char *networkId, 62 char *authName, 63 unsigned short *authDataLenRet, 64 char **authDataRet 65) 66{ 67 IceAuthFileEntry *entry; 68 69 entry = IceGetAuthFileEntry (protocolName, networkId, authName); 70 71 if (entry) 72 { 73 *authDataLenRet = entry->auth_data_length; 74 75 if ((*authDataRet = (char *) malloc (entry->auth_data_length)) != NULL) 76 memcpy (*authDataRet, entry->auth_data, entry->auth_data_length); 77 } 78 else 79 { 80 *authDataLenRet = 0; 81 *authDataRet = NULL; 82 } 83 84 IceFreeAuthFileEntry (entry); 85} 86 87 88 89void 90_IceGetPaAuthData ( 91 char *protocolName, 92 char *networkId, 93 char *authName, 94 unsigned short *authDataLenRet, 95 char **authDataRet 96) 97{ 98 IceAuthDataEntry *entry = NULL; 99 int found = 0; 100 int i; 101 102 for (i = 0; i < _IcePaAuthDataEntryCount && !found; i++) 103 { 104 entry = &_IcePaAuthDataEntries[i]; 105 106 found = 107 strcmp (protocolName, entry->protocol_name) == 0 && 108 strcmp (networkId, entry->network_id) == 0 && 109 strcmp (authName, entry->auth_name) == 0; 110 } 111 112 if (found) 113 { 114 *authDataLenRet = entry->auth_data_length; 115 116 if ((*authDataRet = (char *) malloc (entry->auth_data_length)) != NULL) 117 memcpy (*authDataRet, entry->auth_data, entry->auth_data_length); 118 } 119 else 120 { 121 *authDataLenRet = 0; 122 *authDataRet = NULL; 123 } 124} 125 126 127 128void 129_IceGetPoValidAuthIndices ( 130 char *protocol_name, 131 char *network_id, 132 int num_auth_names, 133 char **auth_names, 134 int *num_indices_ret, 135 int *indices_ret /* in/out arg */ 136) 137{ 138 FILE *auth_file; 139 char *filename; 140 IceAuthFileEntry *entry; 141 int index_ret, i; 142 143 *num_indices_ret = 0; 144 145 if (!(filename = IceAuthFileName ())) 146 return; 147 148 if (access (filename, R_OK) != 0) /* checks REAL id */ 149 return; 150 151 if (!(auth_file = fopen (filename, "rb"))) 152 return; 153 154 for (;;) 155 { 156 if (!(entry = IceReadAuthFileEntry (auth_file))) 157 break; 158 159 if (strcmp (protocol_name, entry->protocol_name) == 0 && 160 strcmp (network_id, entry->network_id) == 0 && 161 auth_valid (entry->auth_name, num_auth_names, 162 auth_names, &index_ret)) 163 { 164 /* 165 * Make sure we didn't store this index already. 166 */ 167 168 for (i = 0; i < *num_indices_ret; i++) 169 if (index_ret == indices_ret[i]) 170 break; 171 172 if (i >= *num_indices_ret) 173 { 174 indices_ret[*num_indices_ret] = index_ret; 175 *num_indices_ret += 1; 176 } 177 } 178 179 IceFreeAuthFileEntry (entry); 180 } 181 182 fclose (auth_file); 183} 184 185 186 187void 188_IceGetPaValidAuthIndices ( 189 char *protocol_name, 190 char *network_id, 191 int num_auth_names, 192 char **auth_names, 193 int *num_indices_ret, 194 int *indices_ret /* in/out arg */ 195) 196{ 197 int index_ret; 198 int i, j; 199 IceAuthDataEntry *entry; 200 201 *num_indices_ret = 0; 202 203 for (i = 0; i < _IcePaAuthDataEntryCount; i++) 204 { 205 entry = &_IcePaAuthDataEntries[i]; 206 207 if (strcmp (protocol_name, entry->protocol_name) == 0 && 208 strcmp (network_id, entry->network_id) == 0 && 209 auth_valid (entry->auth_name, num_auth_names, 210 auth_names, &index_ret)) 211 { 212 /* 213 * Make sure we didn't store this index already. 214 */ 215 216 for (j = 0; j < *num_indices_ret; j++) 217 if (index_ret == indices_ret[j]) 218 break; 219 220 if (j >= *num_indices_ret) 221 { 222 indices_ret[*num_indices_ret] = index_ret; 223 *num_indices_ret += 1; 224 } 225 } 226 } 227} 228 229 230 231/* 232 * local routines 233 */ 234 235static Bool 236auth_valid (const char *auth_name, int num_auth_names, 237 char **auth_names, int *index_ret) 238 239{ 240 /* 241 * Check if auth_name is in auth_names. Return index. 242 */ 243 244 int i; 245 246 for (i = 0; i < num_auth_names; i++) 247 if (strcmp (auth_name, auth_names[i]) == 0) 248 { 249 break; 250 } 251 252 if (i < num_auth_names) 253 { 254 *index_ret = i; 255 return (1); 256 } 257 else 258 return (0); 259} 260