server: Add get_sd and set_sd object operations to allow the security descriptor...
[wine] / server / hook.c
1 /*
2  * Server-side window hooks support
3  *
4  * Copyright (C) 2002 Alexandre Julliard
5  * Copyright (C) 2005 Dmitry Timoshkov
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winuser.h"
34 #include "winternl.h"
35
36 #include "object.h"
37 #include "process.h"
38 #include "request.h"
39 #include "user.h"
40
41 struct hook_table;
42
43 struct hook
44 {
45     struct list         chain;    /* hook chain entry */
46     user_handle_t       handle;   /* user handle for this hook */
47     struct process     *process;  /* process the hook is set to */
48     struct thread      *thread;   /* thread the hook is set to */
49     struct thread      *owner;    /* owner of the out of context hook */
50     struct hook_table  *table;    /* hook table that contains this hook */
51     int                 index;    /* hook table index */
52     int                 event_min;
53     int                 event_max;
54     int                 flags;
55     void               *proc;     /* hook function */
56     int                 unicode;  /* is it a unicode hook? */
57     WCHAR              *module;   /* module name for global hooks */
58     data_size_t         module_size;
59 };
60
61 #define WH_WINEVENT (WH_MAXHOOK+1)
62
63 #define NB_HOOKS (WH_WINEVENT-WH_MINHOOK+1)
64 #define HOOK_ENTRY(p)  LIST_ENTRY( (p), struct hook, chain )
65
66 struct hook_table
67 {
68     struct object obj;              /* object header */
69     struct list   hooks[NB_HOOKS];  /* array of hook chains */
70     int           counts[NB_HOOKS]; /* use counts for each hook chain */
71 };
72
73 static void hook_table_dump( struct object *obj, int verbose );
74 static void hook_table_destroy( struct object *obj );
75
76 static const struct object_ops hook_table_ops =
77 {
78     sizeof(struct hook_table),    /* size */
79     hook_table_dump,              /* dump */
80     no_add_queue,                 /* add_queue */
81     NULL,                         /* remove_queue */
82     NULL,                         /* signaled */
83     NULL,                         /* satisfied */
84     no_signal,                    /* signal */
85     no_get_fd,                    /* get_fd */
86     no_map_access,                /* map_access */
87     default_get_sd,               /* get_sd */
88     default_set_sd,               /* set_sd */
89     no_lookup_name,               /* lookup_name */
90     no_open_file,                 /* open_file */
91     no_close_handle,              /* close_handle */
92     hook_table_destroy            /* destroy */
93 };
94
95
96 /* create a new hook table */
97 static struct hook_table *alloc_hook_table(void)
98 {
99     struct hook_table *table;
100     int i;
101
102     if ((table = alloc_object( &hook_table_ops )))
103     {
104         for (i = 0; i < NB_HOOKS; i++)
105         {
106             list_init( &table->hooks[i] );
107             table->counts[i] = 0;
108         }
109     }
110     return table;
111 }
112
113 static struct hook_table *get_global_hooks( struct thread *thread )
114 {
115     struct hook_table *table;
116     struct desktop *desktop = get_thread_desktop( thread, 0 );
117
118     if (!desktop) return NULL;
119     table = desktop->global_hooks;
120     release_object( desktop );
121     return table;
122 }
123
124 /* create a new hook and add it to the specified table */
125 static struct hook *add_hook( struct desktop *desktop, struct thread *thread, int index, int global )
126 {
127     struct hook *hook;
128     struct hook_table *table = global ? desktop->global_hooks : get_queue_hooks(thread);
129
130     if (!table)
131     {
132         if (!(table = alloc_hook_table())) return NULL;
133         if (global) desktop->global_hooks = table;
134         else set_queue_hooks( thread, table );
135     }
136     if (!(hook = mem_alloc( sizeof(*hook) ))) return NULL;
137
138     if (!(hook->handle = alloc_user_handle( hook, USER_HOOK )))
139     {
140         free( hook );
141         return NULL;
142     }
143     hook->thread = thread ? (struct thread *)grab_object( thread ) : NULL;
144     hook->table  = table;
145     hook->index  = index;
146     list_add_head( &table->hooks[index], &hook->chain );
147     if (thread) thread->desktop_users++;
148     return hook;
149 }
150
151 /* free a hook, removing it from its chain */
152 static void free_hook( struct hook *hook )
153 {
154     free_user_handle( hook->handle );
155     free( hook->module );
156     if (hook->thread)
157     {
158         assert( hook->thread->desktop_users > 0 );
159         hook->thread->desktop_users--;
160         release_object( hook->thread );
161     }
162     if (hook->process) release_object( hook->process );
163     release_object( hook->owner );
164     list_remove( &hook->chain );
165     free( hook );
166 }
167
168 /* find a hook from its index and proc */
169 static struct hook *find_hook( struct thread *thread, int index, void *proc )
170 {
171     struct list *p;
172     struct hook_table *table = get_queue_hooks( thread );
173
174     if (table)
175     {
176         LIST_FOR_EACH( p, &table->hooks[index] )
177         {
178             struct hook *hook = HOOK_ENTRY( p );
179             if (hook->proc == proc) return hook;
180         }
181     }
182     return NULL;
183 }
184
185 /* get the first hook in the chain */
186 static inline struct hook *get_first_hook( struct hook_table *table, int index )
187 {
188     struct list *elem = list_head( &table->hooks[index] );
189     return elem ? HOOK_ENTRY( elem ) : NULL;
190 }
191
192 /* check if a given hook should run in the current thread */
193 static inline int run_hook_in_current_thread( struct hook *hook )
194 {
195     if ((!hook->process || hook->process == current->process) &&
196         (!(hook->flags & WINEVENT_SKIPOWNPROCESS) || hook->process != current->process))
197     {
198         if ((!hook->thread || hook->thread == current) &&
199             (!(hook->flags & WINEVENT_SKIPOWNTHREAD) || hook->thread != current))
200             return 1;
201     }
202     return 0;
203 }
204
205 /* check if a given hook should run in the owner thread instead of the current thread */
206 static inline int run_hook_in_owner_thread( struct hook *hook )
207 {
208     if ((hook->index == WH_MOUSE_LL - WH_MINHOOK ||
209          hook->index == WH_KEYBOARD_LL - WH_MINHOOK))
210         return hook->owner != current;
211     return 0;
212 }
213
214 /* find the first non-deleted hook in the chain */
215 static inline struct hook *get_first_valid_hook( struct hook_table *table, int index,
216                                                  int event, user_handle_t win,
217                                                  int object_id, int child_id )
218 {
219     struct hook *hook = get_first_hook( table, index );
220
221     while (hook)
222     {
223         if (hook->proc && run_hook_in_current_thread( hook ))
224         {
225             if (event >= hook->event_min && event <= hook->event_max)
226             {
227                 if (hook->flags & WINEVENT_INCONTEXT) return hook;
228
229                 /* only winevent hooks may be out of context */
230                 assert(hook->index + WH_MINHOOK == WH_WINEVENT);
231                 post_win_event( hook->owner, event, win, object_id, child_id,
232                                 hook->proc, hook->module, hook->module_size,
233                                 hook->handle );
234             }
235         }
236         hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
237     }
238     return hook;
239 }
240
241 /* find the next hook in the chain, skipping the deleted ones */
242 static struct hook *get_next_hook( struct thread *thread, struct hook *hook, int event,
243                                    user_handle_t win, int object_id, int child_id )
244 {
245     struct hook_table *global_hooks, *table = hook->table;
246     int index = hook->index;
247
248     while ((hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) )))
249     {
250         if (hook->proc && run_hook_in_current_thread( hook ))
251         {
252             if (event >= hook->event_min && event <= hook->event_max)
253             {
254                 if (hook->flags & WINEVENT_INCONTEXT) return hook;
255
256                 /* only winevent hooks may be out of context */
257                 assert(hook->index + WH_MINHOOK == WH_WINEVENT);
258                 post_win_event( hook->owner, event, win, object_id, child_id,
259                                 hook->proc, hook->module, hook->module_size,
260                                 hook->handle );
261             }
262         }
263     }
264     global_hooks = get_global_hooks( thread );
265     if (global_hooks && table != global_hooks)  /* now search through the global table */
266     {
267         hook = get_first_valid_hook( global_hooks, index, event, win, object_id, child_id );
268     }
269     return hook;
270 }
271
272 static void hook_table_dump( struct object *obj, int verbose )
273 {
274     /* struct hook_table *table = (struct hook_table *)obj; */
275     fprintf( stderr, "Hook table\n" );
276 }
277
278 static void hook_table_destroy( struct object *obj )
279 {
280     int i;
281     struct hook *hook;
282     struct hook_table *table = (struct hook_table *)obj;
283
284     for (i = 0; i < NB_HOOKS; i++)
285     {
286         while ((hook = get_first_hook( table, i )) != NULL) free_hook( hook );
287     }
288 }
289
290 /* remove a hook, freeing it if the chain is not in use */
291 static void remove_hook( struct hook *hook )
292 {
293     if (hook->table->counts[hook->index])
294         hook->proc = NULL; /* chain is in use, just mark it and return */
295     else
296         free_hook( hook );
297 }
298
299 /* release a hook chain, removing deleted hooks if the use count drops to 0 */
300 static void release_hook_chain( struct hook_table *table, int index )
301 {
302     if (!table->counts[index])  /* use count shouldn't already be 0 */
303     {
304         set_error( STATUS_INVALID_PARAMETER );
305         return;
306     }
307     if (!--table->counts[index])
308     {
309         struct hook *hook = get_first_hook( table, index );
310         while (hook)
311         {
312             struct hook *next = HOOK_ENTRY( list_next( &table->hooks[hook->index], &hook->chain ) );
313             if (!hook->proc) free_hook( hook );
314             hook = next;
315         }
316     }
317 }
318
319 /* remove all global hooks owned by a given thread */
320 void remove_thread_hooks( struct thread *thread )
321 {
322     struct hook_table *global_hooks = get_global_hooks( thread );
323     int index;
324
325     if (!global_hooks) return;
326
327     /* only low-level keyboard/mouse global hooks can be owned by a thread */
328     for (index = WH_KEYBOARD_LL - WH_MINHOOK; index <= WH_MOUSE_LL - WH_MINHOOK; index++)
329     {
330         struct hook *hook = get_first_hook( global_hooks, index );
331         while (hook)
332         {
333             struct hook *next = HOOK_ENTRY( list_next( &global_hooks->hooks[index], &hook->chain ) );
334             if (hook->thread == thread) remove_hook( hook );
335             hook = next;
336         }
337     }
338 }
339
340 /* get a bitmap of active hooks in a hook table */
341 static int is_hook_active( struct hook_table *table, int index )
342 {
343     struct hook *hook = get_first_hook( table, index );
344
345     while (hook)
346     {
347         if (hook->proc && run_hook_in_current_thread( hook )) return 1;
348         hook = HOOK_ENTRY( list_next( &table->hooks[index], &hook->chain ) );
349     }
350     return 0;
351 }
352
353 /* get a bitmap of all active hooks for the current thread */
354 unsigned int get_active_hooks(void)
355 {
356     struct hook_table *table = get_queue_hooks( current );
357     struct hook_table *global_hooks = get_global_hooks( current );
358     unsigned int ret = 1 << 31;  /* set high bit to indicate that the bitmap is valid */
359     int id;
360
361     for (id = WH_MINHOOK; id <= WH_WINEVENT; id++)
362     {
363         if ((table && is_hook_active( table, id - WH_MINHOOK )) ||
364             (global_hooks && is_hook_active( global_hooks, id - WH_MINHOOK )))
365             ret |= 1 << (id - WH_MINHOOK);
366     }
367     return ret;
368 }
369
370 /* set a window hook */
371 DECL_HANDLER(set_hook)
372 {
373     struct process *process = NULL;
374     struct thread *thread = NULL;
375     struct desktop *desktop;
376     struct hook *hook;
377     WCHAR *module;
378     int global;
379     data_size_t module_size = get_req_data_size();
380
381     if (!req->proc || req->id < WH_MINHOOK || req->id > WH_WINEVENT)
382     {
383         set_error( STATUS_INVALID_PARAMETER );
384         return;
385     }
386
387     if (!(desktop = get_thread_desktop( current, DESKTOP_HOOKCONTROL ))) return;
388
389     if (req->pid && !(process = get_process_from_id( req->pid ))) goto done;
390
391     if (req->tid)
392     {
393         if (!(thread = get_thread_from_id( req->tid ))) goto done;
394         if (process && process != thread->process)
395         {
396             set_error( STATUS_INVALID_PARAMETER );
397             goto done;
398         }
399     }
400
401     if (req->id == WH_KEYBOARD_LL || req->id == WH_MOUSE_LL)
402     {
403         /* low-level hardware hooks are special: always global, but without a module */
404         if (thread)
405         {
406             set_error( STATUS_INVALID_PARAMETER );
407             goto done;
408         }
409         module = NULL;
410         global = 1;
411     }
412     else if (!req->tid)
413     {
414         /* out of context hooks do not need a module handle */
415         if (!module_size && (req->flags & WINEVENT_INCONTEXT))
416         {
417             set_error( STATUS_INVALID_PARAMETER );
418             goto done;
419         }
420         if (!(module = memdup( get_req_data(), module_size ))) goto done;
421         global = 1;
422     }
423     else
424     {
425         /* module is optional only if hook is in current process */
426         if (!module_size)
427         {
428             module = NULL;
429             if (thread->process != current->process)
430             {
431                 set_error( STATUS_INVALID_PARAMETER );
432                 goto done;
433             }
434         }
435         else if (!(module = memdup( get_req_data(), module_size ))) goto done;
436         global = 0;
437     }
438
439     if ((hook = add_hook( desktop, thread, req->id - WH_MINHOOK, global )))
440     {
441         hook->owner = (struct thread *)grab_object( current );
442         hook->process = process ? (struct process *)grab_object( process ) : NULL;
443         hook->event_min   = req->event_min;
444         hook->event_max   = req->event_max;
445         hook->flags       = req->flags;
446         hook->proc        = req->proc;
447         hook->unicode     = req->unicode;
448         hook->module      = module;
449         hook->module_size = module_size;
450         reply->handle = hook->handle;
451         reply->active_hooks = get_active_hooks();
452     }
453     else free( module );
454
455 done:
456     if (process) release_object( process );
457     if (thread) release_object( thread );
458     release_object( desktop );
459 }
460
461
462 /* remove a window hook */
463 DECL_HANDLER(remove_hook)
464 {
465     struct hook *hook;
466
467     if (req->handle)
468     {
469         if (!(hook = get_user_object( req->handle, USER_HOOK )))
470         {
471             set_error( STATUS_INVALID_HANDLE );
472             return;
473         }
474     }
475     else
476     {
477         if (!req->proc || req->id < WH_MINHOOK || req->id > WH_WINEVENT)
478         {
479             set_error( STATUS_INVALID_PARAMETER );
480             return;
481         }
482         if (!(hook = find_hook( current, req->id - WH_MINHOOK, req->proc )))
483         {
484             set_error( STATUS_INVALID_PARAMETER );
485             return;
486         }
487     }
488     remove_hook( hook );
489     reply->active_hooks = get_active_hooks();
490 }
491
492
493 /* start calling a hook chain */
494 DECL_HANDLER(start_hook_chain)
495 {
496     struct hook *hook;
497     struct hook_table *table = get_queue_hooks( current );
498
499     if (req->id < WH_MINHOOK || req->id > WH_WINEVENT)
500     {
501         set_error( STATUS_INVALID_PARAMETER );
502         return;
503     }
504
505     reply->active_hooks = get_active_hooks();
506
507     if (!table || !(hook = get_first_valid_hook( table, req->id - WH_MINHOOK, req->event,
508                                                  req->window, req->object_id, req->child_id )))
509     {
510         /* try global table */
511         if (!(table = get_global_hooks( current )) ||
512             !(hook = get_first_valid_hook( table, req->id - WH_MINHOOK, req->event,
513                                            req->window, req->object_id, req->child_id )))
514             return;  /* no hook set */
515     }
516
517     if (run_hook_in_owner_thread( hook ))
518     {
519         reply->pid  = get_process_id( hook->owner->process );
520         reply->tid  = get_thread_id( hook->owner );
521     }
522     else
523     {
524         reply->pid  = 0;
525         reply->tid  = 0;
526     }
527     reply->proc    = hook->proc;
528     reply->handle  = hook->handle;
529     reply->unicode = hook->unicode;
530     table->counts[hook->index]++;
531     if (hook->module) set_reply_data( hook->module, hook->module_size );
532 }
533
534
535 /* finished calling a hook chain */
536 DECL_HANDLER(finish_hook_chain)
537 {
538     struct hook_table *table = get_queue_hooks( current );
539     struct hook_table *global_hooks = get_global_hooks( current );
540     int index = req->id - WH_MINHOOK;
541
542     if (req->id < WH_MINHOOK || req->id > WH_WINEVENT)
543     {
544         set_error( STATUS_INVALID_PARAMETER );
545         return;
546     }
547     if (table) release_hook_chain( table, index );
548     if (global_hooks) release_hook_chain( global_hooks, index );
549 }
550
551
552 /* get the hook information */
553 DECL_HANDLER(get_hook_info)
554 {
555     struct hook *hook;
556
557     if (!(hook = get_user_object( req->handle, USER_HOOK ))) return;
558     if (hook->thread && (hook->thread != current))
559     {
560         set_error( STATUS_INVALID_HANDLE );
561         return;
562     }
563     if (req->get_next && !(hook = get_next_hook( current, hook, req->event, req->window,
564                                                  req->object_id, req->child_id )))
565         return;
566
567     reply->handle  = hook->handle;
568     reply->id      = hook->index + WH_MINHOOK;
569     reply->unicode = hook->unicode;
570     if (hook->module) set_reply_data( hook->module, min(hook->module_size,get_reply_max_size()) );
571     if (run_hook_in_owner_thread( hook ))
572     {
573         reply->pid  = get_process_id( hook->owner->process );
574         reply->tid  = get_thread_id( hook->owner );
575     }
576     else
577     {
578         reply->pid  = 0;
579         reply->tid  = 0;
580     }
581     reply->proc = hook->proc;
582 }