ae2b748dcdab87336cb5b915cbc55a9f5e534acb
[framework/uifw/xorg/lib/libxau.git] / AuGetBest.c
1 /* $Xorg: AuGetBest.c,v 1.4 2001/02/09 02:03:42 xorgcvs Exp $ */
2
3 /*
4
5 Copyright 1988, 1998  The Open Group
6
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
11 documentation.
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
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.
22
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.
26
27 */
28 /* $XFree86: xc/lib/Xau/AuGetBest.c,v 1.7 2001/12/14 19:54:36 dawes Exp $ */
29
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33 #include <X11/Xauth.h>
34 #include <X11/Xos.h>
35 #ifdef XTHREADS
36 #include <X11/Xthreads.h>
37 #endif
38 #ifdef hpux
39 #define X_INCLUDE_NETDB_H
40 #define XOS_USE_NO_LOCKING
41 #include <X11/Xos_r.h>
42 #endif
43
44 static int
45 binaryEqual (_Xconst char *a, _Xconst char *b, int len)
46 {
47     while (len--)
48         if (*a++ != *b++)
49             return 0;
50     return 1;
51 }
52
53 Xauth *
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 {
73     FILE    *auth_file;
74     char    *auth_name;
75     Xauth   *entry;
76     Xauth   *best;
77     int     best_type;
78     int     type;
79 #ifdef hpux
80     char                *fully_qual_address;
81     unsigned short      fully_qual_address_length;
82 #endif
83
84     auth_name = XauFileName ();
85     if (!auth_name)
86         return NULL;
87     if (access (auth_name, R_OK) != 0)          /* checks REAL id */
88         return NULL;
89     auth_file = fopen (auth_name, "rb");
90     if (!auth_file)
91         return NULL;
92
93 #ifdef hpux
94     if (family == FamilyLocal) {
95 #ifdef XTHREADS_NEEDS_BYNAMEPARAMS
96         _Xgethostbynameparams hparams;
97 #endif
98         struct hostent *hostp;
99
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);
104         }
105         else
106         {
107             fully_qual_address = NULL;
108             fully_qual_address_length = 0;
109         }
110     }
111 #endif /* hpux */
112
113     best = NULL;
114     best_type = types_length;
115     for (;;) {
116         entry = XauReadAuth (auth_file);
117         if (!entry)
118             break;
119         /*
120          * Match when:
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
124          *  and
125          *   either number or entry->number are empty or
126          *    number and entry->number are the same
127          *  and
128          *   either name or entry->name are empty or
129          *    name and entry->name are the same
130          */
131
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))
136 #ifdef hpux
137              || (family == FamilyLocal &&
138                 fully_qual_address_length == entry->address_length &&
139                 binaryEqual (entry->address, fully_qual_address,
140                     (int) fully_qual_address_length))
141 #endif
142             ))) &&
143             (number_length == 0 || entry->number_length == 0 ||
144              (number_length == entry->number_length &&
145               binaryEqual (entry->number, number, (int)number_length))))
146         {
147             if (best_type == 0)
148             {
149                 best = entry;
150                 break;
151             }
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)))
155                 {
156                     break;
157                 }
158             if (type < best_type)
159             {
160                 if (best)
161                     XauDisposeAuth (best);
162                 best = entry;
163                 best_type = type;
164                 if (type == 0)
165                     break;
166                 continue;
167             }
168         }
169         XauDisposeAuth (entry);
170     }
171     (void) fclose (auth_file);
172     return best;
173 }