1 /* $XConsortium: AuGetBest.c /main/23 1996/12/04 11:04:55 lehors $ */
5 Copyright (c) 1988 X Consortium
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 Except as contained in this notice, the name of the X Consortium shall not be
25 used in advertising or otherwise to promote the sale, use or other dealings
26 in this Software without prior written authorization from the X Consortium.
30 #include <X11/Xauth.h>
33 #include <X11/Xthreads.h>
36 #define X_INCLUDE_NETDB_H
37 #define XOS_USE_NO_LOCKING
38 #include <X11/Xos_r.h>
42 binaryEqual (a, b, len)
52 #if NeedFunctionPrototypes
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)
74 XauGetBestAuthByAddr (family, address_length, address,
75 number_length, number,
76 types_length, types, type_lengths)
77 unsigned short family;
78 unsigned short address_length;
80 unsigned short number_length;
94 char *fully_qual_address;
95 unsigned short fully_qual_address_length;
98 auth_name = XauFileName ();
101 if (access (auth_name, R_OK) != 0) /* checks REAL id */
103 auth_file = fopen (auth_name, "rb");
108 if (family == FamilyLocal) {
109 _Xgethostbynameparams hparams;
110 struct hostent *hostp;
112 /* make sure we try fully-qualified hostname */
113 if ((hostp = _XGethostbyname(address,hparams)) != NULL) {
114 fully_qual_address = hostp->h_name;
115 fully_qual_address_length = strlen(fully_qual_address);
119 fully_qual_address = NULL;
120 fully_qual_address_length = 0;
126 best_type = types_length;
128 entry = XauReadAuth (auth_file);
133 * either family or entry->family are FamilyWild or
134 * family and entry->family are the same
136 * either address or entry->address are empty or
137 * address and entry->address are the same
139 * either number or entry->number are empty or
140 * number and entry->number are the same
142 * name matches one of the specified names, or no names
146 if ((family == FamilyWild || entry->family == FamilyWild ||
147 (entry->family == family &&
148 ((address_length == entry->address_length &&
149 binaryEqual (entry->address, address, (int)address_length))
151 || (family == FamilyLocal &&
152 fully_qual_address_length == entry->address_length &&
153 binaryEqual (entry->address, fully_qual_address,
154 (int) fully_qual_address_length))
157 (number_length == 0 || entry->number_length == 0 ||
158 (number_length == entry->number_length &&
159 binaryEqual (entry->number, number, (int)number_length))))
166 for (type = 0; type < best_type; type++)
167 if (type_lengths[type] == entry->name_length &&
168 !(strncmp (types[type], entry->name, entry->name_length)))
172 if (type < best_type)
175 XauDisposeAuth (best);
183 XauDisposeAuth (entry);
185 (void) fclose (auth_file);