summaryrefslogtreecommitdiff
path: root/dix/access.c
blob: 970d7c49ae0326c80ca97b7d741538037c46229c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*

Copyright 2007 Peter Hutterer <peter@cs.unisa.edu.au>

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of the author shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization
from the author.

*/

/* This file controls the access control lists for each window. 
 * Each device can be explicitely allowed or denied access to a given window.
 */

#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

#include <X11/Xlib.h>
#include <X11/extensions/XI.h>

#include "input.h"
#include "inputstr.h"
#include "windowstr.h"


/* Only one single client can be responsible for window access control. */
static ClientPtr ACClient = NULL;


/* Forward declarations */
static void acReplaceList(DeviceIntPtr** list, 
                          int* count, 
                          DeviceIntPtr* devices, 
                          int ndevices);

/* Register global window access control client
 * Return True on success or False otherwise.
 */ 

Bool
ACRegisterClient(ClientPtr client)
{
    if (ACClient && ACClient != client)
        return False;

    ACClient = client;
    return True;
}


/* Unregister global client. If client is not the registered client, nothing
 * happens and False is returned. If no client is registered, return True.
 * Returns True if client was registred and is now unregistered.
 */ 

Bool
ACUnregisterClient(ClientPtr client)
{
    if (ACClient && ACClient != client)
        return False;

    ACClient = NULL;
    return True;
}

/* Clears all access control for the window and remove the default rule,
 * depending on what is set. */
int ACClearWindowAccess(ClientPtr client,
                        WindowPtr win,
                        int what)
{
    if (client != ACClient && client != wClient(win))
        return BadAccess;

    if (!win->optional)
    {
        /* we shouldn't get here if programmers know what they're doing. 
         * A client should not request to clear a window's access controls 
         * if they've never been set before anyway. If they do, do nothing and
         * let the client figure out what to do next.
         */
        return Success;
    }

    if (what & WindowAccessClearPerm)
    {
        xfree(win->optional->access.perm);
        win->optional->access.perm = NULL;
        win->optional->access.nperm = 0;
    }

    if (what & WindowAccessClearDeny)
    {
        xfree(win->optional->access.deny);
        win->optional->access.deny = NULL;
        win->optional->access.ndeny = 0;
    }

    if (what & WindowAccessClearRule)
        win->optional->access.defaultRule = WindowAccessNoRule;

    return Success;
}

/*
 * Changes window access control.
 *
 * Returns Success or BadAccess if the client is not allowed to change
 * anything.
 */

int
ACChangeWindowAccess(ClientPtr client, 
                     WindowPtr win, 
                     int defaultRule,
                     DeviceIntPtr* perm_devices,
                     int nperm,
                     DeviceIntPtr* deny_devices,
                     int ndeny)
{
    if (client != ACClient && client != wClient(win))
        return BadAccess;

    if (!win->optional && !MakeWindowOptional(win))
    {
        ErrorF("ACChangeWindowAcccess: Failed to make window optional.\n");
        return BadImplementation;
    }

    if (defaultRule != WindowAccessKeepRule)
        win->optional->access.defaultRule = defaultRule;

    if (nperm)
    {
        acReplaceList(&win->optional->access.perm, 
                      &win->optional->access.nperm,
                      perm_devices, nperm);
    }

    if (ndeny)
    {
        acReplaceList(&win->optional->access.deny, 
                &win->optional->access.ndeny,
                deny_devices, ndeny);
    }

    return Success;
}

static void
acReplaceList(DeviceIntPtr** list, 
              int* count, 
              DeviceIntPtr* devices, 
              int ndevices)
{
    xfree(*list);
    *list = NULL;
    *count = 0;

    if (ndevices)
    {
        *list = 
            xalloc(ndevices * sizeof(DeviceIntPtr*));
        if (!*list)
        {
            ErrorF("ACChangeWindowAccess: out of memory\n");
            return;
        }
        memcpy(*list,
                devices, 
                ndevices * sizeof(DeviceIntPtr));
        *count = ndevices;
    }
    return;
}

/*
 * Query the given window for the devices allowed to access a window.
 * The caller is responsible for freeing perm and deny.
 */
void
ACQueryWindowAccess(WindowPtr win, 
                    int* defaultRule,
                    DeviceIntPtr** perm,
                    int* nperm,
                    DeviceIntPtr** deny,
                    int* ndeny)
{
    *defaultRule = WindowAccessNoRule;
    *perm = NULL;
    *nperm = 0;
    *deny = NULL;
    *ndeny = 0;

    if (!win->optional)
        return;

    *defaultRule = win->optional->access.defaultRule;

    if (win->optional->access.nperm)
    {
        *nperm = win->optional->access.nperm;
        *perm = (DeviceIntPtr*)xalloc(*nperm * sizeof(DeviceIntPtr));
        if (!*perm)
        {
            ErrorF("ACQuerywinAccess: xalloc failure\n");
            return;
        }
        memcpy(*perm, 
               win->optional->access.perm, 
               *nperm * sizeof(DeviceIntPtr));
    }

    if (win->optional->access.ndeny)
    {
        *ndeny = win->optional->access.ndeny;
        *deny = (DeviceIntPtr*)xalloc(*ndeny * sizeof(DeviceIntPtr));
        if (!*deny)
        {
            ErrorF("ACQuerywinAccess: xalloc failure\n");
            return;
        }
        memcpy(*deny, 
               win->optional->access.deny, 
               *ndeny * sizeof(DeviceIntPtr));
    }
}

/*
 * Check if the given device is allowed to send events to the window. Returns
 * true if device is allowed or false otherwise.
 *
 * Checks are done in the following order until a result is found:
 * If the device is explicitely permitted, allow.
 * If the window has a default of DenyAll, do not allow.
 * If the device is explicitely denied, do not allow.
 * Check parent window. Rinse, wash, repeat.
 * If no rule could be found, allow.
 */
Bool
ACDeviceAllowed(WindowPtr win, DeviceIntPtr dev)
{
    int i;

    if (!win) /* happens for parent of RootWindow */
        return True;

    if (!win->optional) /* no list, check parent */
        return ACDeviceAllowed(win->parent, dev);

    for (i = 0; i < win->optional->access.nperm; i++)
    {
        if (win->optional->access.perm[i]->id == dev->id)
            return True;
    }

    if (win->optional->access.defaultRule == WindowAccessDenyAll)
        return False;

    for (i = 0; i < win->optional->access.ndeny; i++)
    {
        if (win->optional->access.deny[i]->id == dev->id)
            return False;
    }

    return ACDeviceAllowed(win->parent, dev);
}