Merge branch 'devel/x11' into tizen
[platform/upstream/libXau.git] / AuGetAddr.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
33 #define binaryEqual(a, b, len) (memcmp(a, b, len) == 0)
34
35 Xauth *
36 XauGetAuthByAddr (
37 #if NeedWidePrototypes
38 unsigned int    family,
39 unsigned int    address_length,
40 #else
41 unsigned short  family,
42 unsigned short  address_length,
43 #endif
44 _Xconst char*   address,
45 #if NeedWidePrototypes
46 unsigned int    number_length,
47 #else
48 unsigned short  number_length,
49 #endif
50 _Xconst char*   number,
51 #if NeedWidePrototypes
52 unsigned int    name_length,
53 #else
54 unsigned short  name_length,
55 #endif
56 _Xconst char*   name)
57 {
58     FILE    *auth_file;
59     char    *auth_name;
60     Xauth   *entry;
61
62     auth_name = XauFileName ();
63     if (!auth_name)
64         return NULL;
65     if (access (auth_name, R_OK) != 0)          /* checks REAL id */
66         return NULL;
67     auth_file = fopen (auth_name, "rb");
68     if (!auth_file)
69         return NULL;
70     for (;;) {
71         entry = XauReadAuth (auth_file);
72         if (!entry)
73             break;
74         /*
75          * Match when:
76          *   either family or entry->family are FamilyWild or
77          *    family and entry->family are the same and
78          *     address and entry->address are the same
79          *  and
80          *   either number or entry->number are empty or
81          *    number and entry->number are the same
82          *  and
83          *   either name or entry->name are empty or
84          *    name and entry->name are the same
85          */
86
87         if ((family == FamilyWild || entry->family == FamilyWild ||
88              (entry->family == family &&
89               address_length == entry->address_length &&
90               binaryEqual (entry->address, address, address_length))) &&
91             (number_length == 0 || entry->number_length == 0 ||
92              (number_length == entry->number_length &&
93               binaryEqual (entry->number, number, number_length))) &&
94             (name_length == 0 || entry->name_length == 0 ||
95              (entry->name_length == name_length &&
96               binaryEqual (entry->name, name, name_length))))
97             break;
98         XauDisposeAuth (entry);
99     }
100     (void) fclose (auth_file);
101     return entry;
102 }