Debian Codegraph @

nx-X11/lib/src/AuGetBest.c

raw
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 <nx-X11/Xauth.h>
31 #include <nx-X11/Xos.h>
32 #ifdef XTHREADS
33 #include <nx-X11/Xthreads.h>
34 #endif
35
36 #define binaryEqual(a, b, len) (memcmp(a, b, len) == 0)
37
38 Xauth *
39 XauGetBestAuthByAddr (
40 #if NeedWidePrototypes
41 unsigned int family,
42 unsigned int address_length,
43 #else
44 unsigned short family,
45 unsigned short address_length,
46 #endif
47 _Xconst char* address,
48 #if NeedWidePrototypes
49 unsigned int number_length,
50 #else
51 unsigned short number_length,
52 #endif
53 _Xconst char* number,
54 int types_length,
55 char** types,
56 _Xconst int* type_lengths)
57 {
58 FILE *auth_file;
59 char *auth_name;
60 Xauth *entry;
61 Xauth *best;
62 int best_type;
63 int type;
64
65 auth_name = XauFileName ();
66 if (!auth_name)
67 return NULL;
68 if (access (auth_name, R_OK) != 0) /* checks REAL id */
69 return NULL;
70 auth_file = fopen (auth_name, "rb");
71 if (!auth_file)
72 return NULL;
73
74 best = NULL;
75 best_type = types_length;
76 for (;;) {
77 entry = XauReadAuth (auth_file);
78 if (!entry)
79 break;
80 /*
81 * Match when:
82 * either family or entry->family are FamilyWild or
83 * family and entry->family are the same and
84 * address and entry->address are the same
85 * and
86 * either number or entry->number are empty or
87 * number and entry->number are the same
88 * and
89 * either name or entry->name are empty or
90 * name and entry->name are the same
91 */
92
93 if ((family == FamilyWild || entry->family == FamilyWild ||
94 (entry->family == family &&
95 ((address_length == entry->address_length &&
96 binaryEqual (entry->address, address, address_length))
97 ))) &&
98 (number_length == 0 || entry->number_length == 0 ||
99 (number_length == entry->number_length &&
100 binaryEqual (entry->number, number, number_length))))
101 {
102 if (best_type == 0)
103 {
104 best = entry;
105 break;
106 }
107 for (type = 0; type < best_type; type++)
108 if (type_lengths[type] == entry->name_length &&
109 !(strncmp (types[type], entry->name, entry->name_length)))
110 {
111 break;
112 }
113 if (type < best_type)
114 {
115 if (best)
116 XauDisposeAuth (best);
117 best = entry;
118 best_type = type;
119 if (type == 0)
120 break;
121 continue;
122 }
123 }
124 XauDisposeAuth (entry);
125 }
126 (void) fclose (auth_file);
127 return best;
128 }