1 /* $Xorg: AuGetBest.c,v 1.4 2001/02/09 02:03:42 xorgcvs Exp $ */
5 Copyright 1988, 1998 The Open Group
7 Permission to use, copy, modify, distribute, and sell this software and its
8 documentation for any purpose is hereby granted without fee, provided that
9 the above copyright notice appear in all copies and that both that
10 copyright notice and this permission notice appear in supporting
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 Except as contained in this notice, the name of The Open Group shall not be
24 used in advertising or otherwise to promote the sale, use or other dealings
25 in this Software without prior written authorization from The Open Group.
28 /* $XFree86: xc/lib/Xau/AuGetBest.c,v 1.7 2001/12/14 19:54:36 dawes Exp $ */
33 #include <X11/Xauth.h>
36 #include <X11/Xthreads.h>
39 #define X_INCLUDE_NETDB_H
40 #define XOS_USE_NO_LOCKING
41 #include <X11/Xos_r.h>
45 binaryEqual (_Xconst char *a, _Xconst char *b, int len)
54 XauGetBestAuthByAddr (
55 #if NeedWidePrototypes
57 unsigned int address_length,
59 unsigned short family,
60 unsigned short address_length,
62 _Xconst char* address,
63 #if NeedWidePrototypes
64 unsigned int number_length,
66 unsigned short number_length,
71 _Xconst int* type_lengths)
80 char *fully_qual_address;
81 unsigned short fully_qual_address_length;
84 auth_name = XauFileName ();
87 if (access (auth_name, R_OK) != 0) /* checks REAL id */
89 auth_file = fopen (auth_name, "rb");
94 if (family == FamilyLocal) {
95 #ifdef XTHREADS_NEEDS_BYNAMEPARAMS
96 _Xgethostbynameparams hparams;
98 struct hostent *hostp;
100 /* make sure we try fully-qualified hostname */
101 if ((hostp = _XGethostbyname(address,hparams)) != NULL) {
102 fully_qual_address = hostp->h_name;
103 fully_qual_address_length = strlen(fully_qual_address);
107 fully_qual_address = NULL;
108 fully_qual_address_length = 0;
114 best_type = types_length;
116 entry = XauReadAuth (auth_file);
121 * either family or entry->family are FamilyWild or
122 * family and entry->family are the same and
123 * address and entry->address are the same
125 * either number or entry->number are empty or
126 * number and entry->number are the same
128 * either name or entry->name are empty or
129 * name and entry->name are the same
132 if ((family == FamilyWild || entry->family == FamilyWild ||
133 (entry->family == family &&
134 ((address_length == entry->address_length &&
135 binaryEqual (entry->address, address, (int)address_length))
137 || (family == FamilyLocal &&
138 fully_qual_address_length == entry->address_length &&
139 binaryEqual (entry->address, fully_qual_address,
140 (int) fully_qual_address_length))
143 (number_length == 0 || entry->number_length == 0 ||
144 (number_length == entry->number_length &&
145 binaryEqual (entry->number, number, (int)number_length))))
152 for (type = 0; type < best_type; type++)
153 if (type_lengths[type] == entry->name_length &&
154 !(strncmp (types[type], entry->name, entry->name_length)))
158 if (type < best_type)
161 XauDisposeAuth (best);
169 XauDisposeAuth (entry);
171 (void) fclose (auth_file);