upload tizen2.0 source
[framework/uifw/xorg/lib/libxau.git] / AuGetBest.c
1 /*
2
3 Copyright 1988, 1998  The Open Group
4
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
9 documentation.
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from The Open Group.
24
25 */
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
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 int
42 binaryEqual (_Xconst char *a, _Xconst char *b, int len)
43 {
44     while (len--)
45         if (*a++ != *b++)
46             return 0;
47     return 1;
48 }
49
50 Xauth *
51 XauGetBestAuthByAddr (
52 #if NeedWidePrototypes
53     unsigned int        family,
54     unsigned int        address_length,
55 #else
56     unsigned short      family,
57     unsigned short      address_length,
58 #endif
59     _Xconst char*       address,
60 #if NeedWidePrototypes
61     unsigned int        number_length,
62 #else
63     unsigned short      number_length,
64 #endif
65     _Xconst char*       number,
66     int                 types_length,
67     char**              types,
68     _Xconst int*        type_lengths)
69 {
70     FILE    *auth_file;
71     char    *auth_name;
72     Xauth   *entry;
73     Xauth   *best;
74     int     best_type;
75     int     type;
76 #ifdef hpux
77     char                *fully_qual_address;
78     unsigned short      fully_qual_address_length;
79 #endif
80
81     auth_name = XauFileName ();
82     if (!auth_name)
83         return NULL;
84     if (access (auth_name, R_OK) != 0)          /* checks REAL id */
85         return NULL;
86     auth_file = fopen (auth_name, "rb");
87     if (!auth_file)
88         return NULL;
89
90 #ifdef hpux
91     if (family == FamilyLocal) {
92 #ifdef XTHREADS_NEEDS_BYNAMEPARAMS
93         _Xgethostbynameparams hparams;
94 #endif
95         struct hostent *hostp;
96
97         /* make sure we try fully-qualified hostname */
98         if ((hostp = _XGethostbyname(address,hparams)) != NULL) {
99             fully_qual_address = hostp->h_name;
100             fully_qual_address_length = strlen(fully_qual_address);
101         }
102         else
103         {
104             fully_qual_address = NULL;
105             fully_qual_address_length = 0;
106         }
107     }
108 #endif /* hpux */
109
110     best = NULL;
111     best_type = types_length;
112     for (;;) {
113         entry = XauReadAuth (auth_file);
114         if (!entry)
115             break;
116         /*
117          * Match when:
118          *   either family or entry->family are FamilyWild or
119          *    family and entry->family are the same and
120          *     address and entry->address are the same
121          *  and
122          *   either number or entry->number are empty or
123          *    number and entry->number are the same
124          *  and
125          *   either name or entry->name are empty or
126          *    name and entry->name are the same
127          */
128
129         if ((family == FamilyWild || entry->family == FamilyWild ||
130              (entry->family == family &&
131              ((address_length == entry->address_length &&
132               binaryEqual (entry->address, address, (int)address_length))
133 #ifdef hpux
134              || (family == FamilyLocal &&
135                 fully_qual_address_length == entry->address_length &&
136                 binaryEqual (entry->address, fully_qual_address,
137                     (int) fully_qual_address_length))
138 #endif
139             ))) &&
140             (number_length == 0 || entry->number_length == 0 ||
141              (number_length == entry->number_length &&
142               binaryEqual (entry->number, number, (int)number_length))))
143         {
144             if (best_type == 0)
145             {
146                 best = entry;
147                 break;
148             }
149             for (type = 0; type < best_type; type++)
150                 if (type_lengths[type] == entry->name_length &&
151                     !(strncmp (types[type], entry->name, entry->name_length)))
152                 {
153                     break;
154                 }
155             if (type < best_type)
156             {
157                 if (best)
158                     XauDisposeAuth (best);
159                 best = entry;
160                 best_type = type;
161                 if (type == 0)
162                     break;
163                 continue;
164             }
165         }
166         XauDisposeAuth (entry);
167     }
168     (void) fclose (auth_file);
169     return best;
170 }