127702724Smrg/* 227702724Smrg 327702724SmrgCopyright 1988, 1998 The Open Group 427702724Smrg 527702724SmrgPermission to use, copy, modify, distribute, and sell this software and its 627702724Smrgdocumentation for any purpose is hereby granted without fee, provided that 727702724Smrgthe above copyright notice appear in all copies and that both that 827702724Smrgcopyright notice and this permission notice appear in supporting 927702724Smrgdocumentation. 1027702724Smrg 1127702724SmrgThe above copyright notice and this permission notice shall be included in 1227702724Smrgall copies or substantial portions of the Software. 1327702724Smrg 1427702724SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1527702724SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1627702724SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 1727702724SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 1827702724SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 1927702724SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2027702724Smrg 2127702724SmrgExcept as contained in this notice, the name of The Open Group shall not be 2227702724Smrgused in advertising or otherwise to promote the sale, use or other dealings 2327702724Smrgin this Software without prior written authorization from The Open Group. 2427702724Smrg 2527702724Smrg*/ 2627702724Smrg 2727702724Smrg#ifdef HAVE_CONFIG_H 2827702724Smrg#include <config.h> 2927702724Smrg#endif 3027702724Smrg#include <X11/Xauth.h> 3127702724Smrg#include <X11/Xos.h> 3227702724Smrg#ifdef XTHREADS 3327702724Smrg#include <X11/Xthreads.h> 3427702724Smrg#endif 35ae545d91Smrg 36ae545d91Smrg#ifdef O_CLOEXEC 37ae545d91Smrg#define FOPEN_CLOEXEC "e" 38ae545d91Smrg#else 39ae545d91Smrg#define FOPEN_CLOEXEC "" 4027702724Smrg#endif 4127702724Smrg 42313a12fdSmrg#define binaryEqual(a, b, len) (memcmp(a, b, len) == 0) 4327702724Smrg 4427702724SmrgXauth * 4527702724SmrgXauGetBestAuthByAddr ( 4627702724Smrg#if NeedWidePrototypes 4727702724Smrg unsigned int family, 4827702724Smrg unsigned int address_length, 4927702724Smrg#else 5027702724Smrg unsigned short family, 5127702724Smrg unsigned short address_length, 5227702724Smrg#endif 5327702724Smrg _Xconst char* address, 5427702724Smrg#if NeedWidePrototypes 5527702724Smrg unsigned int number_length, 5627702724Smrg#else 5727702724Smrg unsigned short number_length, 5827702724Smrg#endif 5927702724Smrg _Xconst char* number, 6027702724Smrg int types_length, 6127702724Smrg char** types, 6227702724Smrg _Xconst int* type_lengths) 6327702724Smrg{ 6427702724Smrg FILE *auth_file; 6527702724Smrg char *auth_name; 6627702724Smrg Xauth *entry; 6727702724Smrg Xauth *best; 6827702724Smrg int best_type; 6927702724Smrg int type; 7027702724Smrg 7127702724Smrg auth_name = XauFileName (); 7227702724Smrg if (!auth_name) 7327702724Smrg return NULL; 7427702724Smrg if (access (auth_name, R_OK) != 0) /* checks REAL id */ 7527702724Smrg return NULL; 76ae545d91Smrg auth_file = fopen (auth_name, "rb" FOPEN_CLOEXEC); 7727702724Smrg if (!auth_file) 7827702724Smrg return NULL; 7927702724Smrg 8027702724Smrg best = NULL; 8127702724Smrg best_type = types_length; 8227702724Smrg for (;;) { 8327702724Smrg entry = XauReadAuth (auth_file); 8427702724Smrg if (!entry) 8527702724Smrg break; 8627702724Smrg /* 8727702724Smrg * Match when: 8827702724Smrg * either family or entry->family are FamilyWild or 8927702724Smrg * family and entry->family are the same and 9027702724Smrg * address and entry->address are the same 9127702724Smrg * and 9227702724Smrg * either number or entry->number are empty or 9327702724Smrg * number and entry->number are the same 9427702724Smrg * and 9527702724Smrg * either name or entry->name are empty or 9627702724Smrg * name and entry->name are the same 9727702724Smrg */ 9827702724Smrg 9927702724Smrg if ((family == FamilyWild || entry->family == FamilyWild || 10027702724Smrg (entry->family == family && 10127702724Smrg ((address_length == entry->address_length && 102313a12fdSmrg binaryEqual (entry->address, address, address_length)) 10327702724Smrg ))) && 10427702724Smrg (number_length == 0 || entry->number_length == 0 || 10527702724Smrg (number_length == entry->number_length && 106313a12fdSmrg binaryEqual (entry->number, number, number_length)))) 10727702724Smrg { 10827702724Smrg if (best_type == 0) 10927702724Smrg { 11027702724Smrg best = entry; 11127702724Smrg break; 11227702724Smrg } 11327702724Smrg for (type = 0; type < best_type; type++) 11427702724Smrg if (type_lengths[type] == entry->name_length && 11527702724Smrg !(strncmp (types[type], entry->name, entry->name_length))) 11627702724Smrg { 11727702724Smrg break; 11827702724Smrg } 11927702724Smrg if (type < best_type) 12027702724Smrg { 12127702724Smrg if (best) 12227702724Smrg XauDisposeAuth (best); 12327702724Smrg best = entry; 12427702724Smrg best_type = type; 12527702724Smrg if (type == 0) 12627702724Smrg break; 12727702724Smrg continue; 12827702724Smrg } 12927702724Smrg } 13027702724Smrg XauDisposeAuth (entry); 13127702724Smrg } 13227702724Smrg (void) fclose (auth_file); 13327702724Smrg return best; 13427702724Smrg} 135