1 /* $XConsortium: AuGetBest.c /main/23 1996/12/04 11:04:55 lehors $ */
2 
3 /*
4 
5 Copyright (c) 1988  X Consortium
6 
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:
13 
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16 
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.
23 
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.
27 
28 */
29 
30 #include <X11/Xauth.h>
31 #include <X11/Xos.h>
32 #ifdef XTHREADS
33 #include <X11/Xthreads.h>
34 #endif
35 #ifdef hpux
36 #define X_INCLUDE_NETDB_H
37 #define XOS_USE_NO_LOCKING
38 #include <X11/Xos_r.h>
39 #endif
40 
41 static
binaryEqual(a,b,len)42 binaryEqual (a, b, len)
43 register char	*a, *b;
44 register int	len;
45 {
46     while (len--)
47 	if (*a++ != *b++)
48 	    return 0;
49     return 1;
50 }
51 
52 #if NeedFunctionPrototypes
53 Xauth *
XauGetBestAuthByAddr(unsigned int family,unsigned int address_length,_Xconst char * address,unsigned int number_length,_Xconst char * number,int types_length,char ** types,_Xconst int * type_lengths)54 XauGetBestAuthByAddr (
55 #if NeedWidePrototypes
56     unsigned int	family,
57     unsigned int	address_length,
58 #else
59     unsigned short	family,
60     unsigned short	address_length,
61 #endif
62     _Xconst char*	address,
63 #if NeedWidePrototypes
64     unsigned int	number_length,
65 #else
66     unsigned short	number_length,
67 #endif
68     _Xconst char*	number,
69     int			types_length,
70     char**		types,
71     _Xconst int*	type_lengths)
72 #else
73 Xauth *
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;
79     char		*address;
80     unsigned short	number_length;
81     char		*number;
82     int			types_length;
83     char		**types;
84     int			*type_lengths;
85 #endif
86 {
87     FILE    *auth_file;
88     char    *auth_name;
89     Xauth   *entry;
90     Xauth   *best;
91     int	    best_type;
92     int	    type;
93 #ifdef hpux
94     char		*fully_qual_address;
95     unsigned short	fully_qual_address_length;
96 #endif
97 
98     auth_name = XauFileName ();
99     if (!auth_name)
100 	return 0;
101     if (access (auth_name, R_OK) != 0)		/* checks REAL id */
102 	return 0;
103     auth_file = fopen (auth_name, "rb");
104     if (!auth_file)
105 	return 0;
106 
107 #ifdef hpux
108     if (family == FamilyLocal) {
109 	_Xgethostbynameparams hparams;
110 	struct hostent *hostp;
111 
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);
116 	}
117 	else
118 	{
119 	    fully_qual_address = NULL;
120 	    fully_qual_address_length = 0;
121 	}
122     }
123 #endif /* hpux */
124 
125     best = 0;
126     best_type = types_length;
127     for (;;) {
128 	entry = XauReadAuth (auth_file);
129 	if (!entry)
130 	    break;
131 	/*
132 	 * Match when:
133 	 *   either family or entry->family are FamilyWild or
134 	 *    family and entry->family are the same
135 	 *  and
136 	 *   either address or entry->address are empty or
137 	 *    address and entry->address are the same
138 	 *  and
139 	 *   either number or entry->number are empty or
140 	 *    number and entry->number are the same
141 	 *  and
142 	 *   name matches one of the specified names, or no names
143 	 *    were specified
144 	 */
145 
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))
150 #ifdef hpux
151 	     || (family == FamilyLocal &&
152 		fully_qual_address_length == entry->address_length &&
153 	     	binaryEqual (entry->address, fully_qual_address,
154 		    (int) fully_qual_address_length))
155 #endif
156 	    ))) &&
157 	    (number_length == 0 || entry->number_length == 0 ||
158 	     (number_length == entry->number_length &&
159 	      binaryEqual (entry->number, number, (int)number_length))))
160 	{
161 	    if (best_type == 0)
162 	    {
163 		best = entry;
164 		break;
165 	    }
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)))
169 		{
170 		    break;
171 		}
172 	    if (type < best_type)
173 	    {
174 		if (best)
175 		    XauDisposeAuth (best);
176 		best = entry;
177 		best_type = type;
178 		if (type == 0)
179 		    break;
180 		continue;
181 	    }
182 	}
183 	XauDisposeAuth (entry);
184     }
185     (void) fclose (auth_file);
186     return best;
187 }
188