1/* 2 3Copyright 1988, 1998 The Open Group 4 5Permission to use, copy, modify, distribute, and sell this software and its 6documentation for any purpose is hereby granted without fee, provided that 7the above copyright notice appear in all copies and that both that 8copyright notice and this permission notice appear in supporting 9documentation. 10 11The above copyright notice and this permission notice shall be included in 12all copies or substantial portions of the Software. 13 14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 21Except as contained in this notice, the name of The Open Group shall not be 22used in advertising or otherwise to promote the sale, use or other dealings 23in this Software without prior written authorization from The Open Group. 24 25*/ 26 27#ifdef HAVE_CONFIG_H 28#include <config.h> 29#endif 30#include <X11/Xauth.h> 31#include <X11/Xos.h> 32 33#ifdef O_CLOEXEC 34#define FOPEN_CLOEXEC "e" 35#else 36#define FOPEN_CLOEXEC "" 37#endif 38 39#define binaryEqual(a, b, len) (memcmp(a, b, len) == 0) 40 41Xauth * 42XauGetAuthByAddr ( 43#if NeedWidePrototypes 44unsigned int family, 45unsigned int address_length, 46#else 47unsigned short family, 48unsigned short address_length, 49#endif 50_Xconst char* address, 51#if NeedWidePrototypes 52unsigned int number_length, 53#else 54unsigned short number_length, 55#endif 56_Xconst char* number, 57#if NeedWidePrototypes 58unsigned int name_length, 59#else 60unsigned short name_length, 61#endif 62_Xconst char* name) 63{ 64 FILE *auth_file; 65 char *auth_name; 66 Xauth *entry; 67 68 auth_name = XauFileName (); 69 if (!auth_name) 70 return NULL; 71 if (access (auth_name, R_OK) != 0) /* checks REAL id */ 72 return NULL; 73 auth_file = fopen (auth_name, "rb" FOPEN_CLOEXEC); 74 if (!auth_file) 75 return NULL; 76 for (;;) { 77 entry = XauReadAuth (auth_file); 78 if (!entry) 79 break; 80 /* 81 * Match when: 82 * either family or entry->family are FamilyWild or 83 * family and entry->family are the same and 84 * address and entry->address are the same 85 * and 86 * either number or entry->number are empty or 87 * number and entry->number are the same 88 * and 89 * either name or entry->name are empty or 90 * name and entry->name are the same 91 */ 92 93 if ((family == FamilyWild || entry->family == FamilyWild || 94 (entry->family == family && 95 address_length == entry->address_length && 96 binaryEqual (entry->address, address, address_length))) && 97 (number_length == 0 || entry->number_length == 0 || 98 (number_length == entry->number_length && 99 binaryEqual (entry->number, number, number_length))) && 100 (name_length == 0 || entry->name_length == 0 || 101 (entry->name_length == name_length && 102 binaryEqual (entry->name, name, name_length)))) 103 break; 104 XauDisposeAuth (entry); 105 } 106 (void) fclose (auth_file); 107 return entry; 108} 109