server: Don't set the idle event when a queue is destroyed, instead return success...
[wine] / server / handle.c
1 /*
2  * Server-side handle management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <limits.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include "ntstatus.h"
32 #define WIN32_NO_STATUS
33 #include "windef.h"
34 #include "winternl.h"
35
36 #include "handle.h"
37 #include "process.h"
38 #include "thread.h"
39 #include "security.h"
40 #include "request.h"
41
42 struct handle_entry
43 {
44     struct object *ptr;       /* object */
45     unsigned int   access;    /* access rights */
46 };
47
48 struct handle_table
49 {
50     struct object        obj;         /* object header */
51     struct process      *process;     /* process owning this table */
52     int                  count;       /* number of allocated entries */
53     int                  last;        /* last used entry */
54     int                  free;        /* first entry that may be free */
55     struct handle_entry *entries;     /* handle entries */
56 };
57
58 static struct handle_table *global_table;
59
60 /* reserved handle access rights */
61 #define RESERVED_SHIFT         26
62 #define RESERVED_INHERIT       (HANDLE_FLAG_INHERIT << RESERVED_SHIFT)
63 #define RESERVED_CLOSE_PROTECT (HANDLE_FLAG_PROTECT_FROM_CLOSE << RESERVED_SHIFT)
64 #define RESERVED_ALL           (RESERVED_INHERIT | RESERVED_CLOSE_PROTECT)
65
66 #define MIN_HANDLE_ENTRIES  32
67 #define MAX_HANDLE_ENTRIES  0x00ffffff
68
69
70 /* handle to table index conversion */
71
72 /* handles are a multiple of 4 under NT; handle 0 is not used */
73 static inline obj_handle_t index_to_handle( int index )
74 {
75     return (obj_handle_t)((index + 1) << 2);
76 }
77 static inline int handle_to_index( obj_handle_t handle )
78 {
79     return (handle >> 2) - 1;
80 }
81
82 /* global handle conversion */
83
84 #define HANDLE_OBFUSCATOR 0x544a4def
85
86 static inline int handle_is_global( obj_handle_t handle)
87 {
88     return (handle ^ HANDLE_OBFUSCATOR) <= (MAX_HANDLE_ENTRIES << 2);
89 }
90 static inline obj_handle_t handle_local_to_global( obj_handle_t handle )
91 {
92     if (!handle) return 0;
93     return handle ^ HANDLE_OBFUSCATOR;
94 }
95 static inline obj_handle_t handle_global_to_local( obj_handle_t handle )
96 {
97     return handle ^ HANDLE_OBFUSCATOR;
98 }
99
100
101 static void handle_table_dump( struct object *obj, int verbose );
102 static void handle_table_destroy( struct object *obj );
103
104 static const struct object_ops handle_table_ops =
105 {
106     sizeof(struct handle_table),     /* size */
107     handle_table_dump,               /* dump */
108     no_get_type,                     /* get_type */
109     no_add_queue,                    /* add_queue */
110     NULL,                            /* remove_queue */
111     NULL,                            /* signaled */
112     NULL,                            /* satisfied */
113     no_signal,                       /* signal */
114     no_get_fd,                       /* get_fd */
115     no_map_access,                   /* map_access */
116     default_get_sd,                  /* get_sd */
117     default_set_sd,                  /* set_sd */
118     no_lookup_name,                  /* lookup_name */
119     no_open_file,                    /* open_file */
120     no_close_handle,                 /* close_handle */
121     handle_table_destroy             /* destroy */
122 };
123
124 /* dump a handle table */
125 static void handle_table_dump( struct object *obj, int verbose )
126 {
127     int i;
128     struct handle_table *table = (struct handle_table *)obj;
129     struct handle_entry *entry = table->entries;
130
131     assert( obj->ops == &handle_table_ops );
132
133     fprintf( stderr, "Handle table last=%d count=%d process=%p\n",
134              table->last, table->count, table->process );
135     if (!verbose) return;
136     entry = table->entries;
137     for (i = 0; i <= table->last; i++, entry++)
138     {
139         if (!entry->ptr) continue;
140         fprintf( stderr, "    %04x: %p %08x ",
141                  index_to_handle(i), entry->ptr, entry->access );
142         entry->ptr->ops->dump( entry->ptr, 0 );
143     }
144 }
145
146 /* destroy a handle table */
147 static void handle_table_destroy( struct object *obj )
148 {
149     int i;
150     struct handle_table *table = (struct handle_table *)obj;
151     struct handle_entry *entry;
152
153     assert( obj->ops == &handle_table_ops );
154
155     /* first notify all objects that handles are being closed */
156     if (table->process)
157     {
158         for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
159         {
160             struct object *obj = entry->ptr;
161             if (obj) obj->ops->close_handle( obj, table->process, index_to_handle(i) );
162         }
163     }
164
165     for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
166     {
167         struct object *obj = entry->ptr;
168         entry->ptr = NULL;
169         if (obj) release_object( obj );
170     }
171     free( table->entries );
172 }
173
174 /* allocate a new handle table */
175 struct handle_table *alloc_handle_table( struct process *process, int count )
176 {
177     struct handle_table *table;
178
179     if (count < MIN_HANDLE_ENTRIES) count = MIN_HANDLE_ENTRIES;
180     if (!(table = alloc_object( &handle_table_ops )))
181         return NULL;
182     table->process = process;
183     table->count   = count;
184     table->last    = -1;
185     table->free    = 0;
186     if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return table;
187     release_object( table );
188     return NULL;
189 }
190
191 /* grow a handle table */
192 static int grow_handle_table( struct handle_table *table )
193 {
194     struct handle_entry *new_entries;
195     int count = min( table->count * 2, MAX_HANDLE_ENTRIES );
196
197     if (count == table->count ||
198         !(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) )))
199     {
200         set_error( STATUS_INSUFFICIENT_RESOURCES );
201         return 0;
202     }
203     table->entries = new_entries;
204     table->count   = count;
205     return 1;
206 }
207
208 /* allocate the first free entry in the handle table */
209 static obj_handle_t alloc_entry( struct handle_table *table, void *obj, unsigned int access )
210 {
211     struct handle_entry *entry = table->entries + table->free;
212     int i;
213
214     for (i = table->free; i <= table->last; i++, entry++) if (!entry->ptr) goto found;
215     if (i >= table->count)
216     {
217         if (!grow_handle_table( table )) return 0;
218         entry = table->entries + i;  /* the entries may have moved */
219     }
220     table->last = i;
221  found:
222     table->free = i + 1;
223     entry->ptr    = grab_object( obj );
224     entry->access = access;
225     return index_to_handle(i);
226 }
227
228 /* allocate a handle for an object, incrementing its refcount */
229 /* return the handle, or 0 on error */
230 obj_handle_t alloc_handle_no_access_check( struct process *process, void *ptr, unsigned int access, unsigned int attr )
231 {
232     struct object *obj = ptr;
233
234     access &= ~RESERVED_ALL;
235     if (attr & OBJ_INHERIT) access |= RESERVED_INHERIT;
236     if (!process->handles)
237     {
238         set_error( STATUS_NO_MEMORY );
239         return 0;
240     }
241     return alloc_entry( process->handles, obj, access );
242 }
243
244 /* allocate a handle for an object, checking the dacl allows the process to */
245 /* access it and incrementing its refcount */
246 /* return the handle, or 0 on error */
247 obj_handle_t alloc_handle( struct process *process, void *ptr, unsigned int access, unsigned int attr )
248 {
249     struct object *obj = ptr;
250     access = obj->ops->map_access( obj, access );
251     if (access && !check_object_access( obj, &access )) return 0;
252     return alloc_handle_no_access_check( process, ptr, access, attr );
253 }
254
255 /* allocate a global handle for an object, incrementing its refcount */
256 /* return the handle, or 0 on error */
257 static obj_handle_t alloc_global_handle_no_access_check( void *obj, unsigned int access )
258 {
259     if (!global_table)
260     {
261         if (!(global_table = (struct handle_table *)alloc_handle_table( NULL, 0 )))
262             return 0;
263         make_object_static( &global_table->obj );
264     }
265     return handle_local_to_global( alloc_entry( global_table, obj, access ));
266 }
267
268 /* allocate a global handle for an object, checking the dacl allows the */
269 /* process to access it and incrementing its refcount and incrementing its refcount */
270 /* return the handle, or 0 on error */
271 static obj_handle_t alloc_global_handle( void *obj, unsigned int access )
272 {
273     if (access && !check_object_access( obj, &access )) return 0;
274     return alloc_global_handle_no_access_check( obj, access );
275 }
276
277 /* return a handle entry, or NULL if the handle is invalid */
278 static struct handle_entry *get_handle( struct process *process, obj_handle_t handle )
279 {
280     struct handle_table *table = process->handles;
281     struct handle_entry *entry;
282     int index;
283
284     if (handle_is_global(handle))
285     {
286         handle = handle_global_to_local(handle);
287         table = global_table;
288     }
289     if (!table) return NULL;
290     index = handle_to_index( handle );
291     if (index < 0) return NULL;
292     if (index > table->last) return NULL;
293     entry = table->entries + index;
294     if (!entry->ptr) return NULL;
295     return entry;
296 }
297
298 /* attempt to shrink a table */
299 static void shrink_handle_table( struct handle_table *table )
300 {
301     struct handle_entry *entry = table->entries + table->last;
302     struct handle_entry *new_entries;
303     int count = table->count;
304
305     while (table->last >= 0)
306     {
307         if (entry->ptr) break;
308         table->last--;
309         entry--;
310     }
311     if (table->last >= count / 4) return;  /* no need to shrink */
312     if (count < MIN_HANDLE_ENTRIES * 2) return;  /* too small to shrink */
313     count /= 2;
314     if (!(new_entries = realloc( table->entries, count * sizeof(*new_entries) ))) return;
315     table->count   = count;
316     table->entries = new_entries;
317 }
318
319 /* copy the handle table of the parent process */
320 /* return 1 if OK, 0 on error */
321 struct handle_table *copy_handle_table( struct process *process, struct process *parent )
322 {
323     struct handle_table *parent_table = parent->handles;
324     struct handle_table *table;
325     int i;
326
327     assert( parent_table );
328     assert( parent_table->obj.ops == &handle_table_ops );
329
330     if (!(table = (struct handle_table *)alloc_handle_table( process, parent_table->count )))
331         return NULL;
332
333     if ((table->last = parent_table->last) >= 0)
334     {
335         struct handle_entry *ptr = table->entries;
336         memcpy( ptr, parent_table->entries, (table->last + 1) * sizeof(struct handle_entry) );
337         for (i = 0; i <= table->last; i++, ptr++)
338         {
339             if (!ptr->ptr) continue;
340             if (ptr->access & RESERVED_INHERIT) grab_object( ptr->ptr );
341             else ptr->ptr = NULL; /* don't inherit this entry */
342         }
343     }
344     /* attempt to shrink the table */
345     shrink_handle_table( table );
346     return table;
347 }
348
349 /* close a handle and decrement the refcount of the associated object */
350 unsigned int close_handle( struct process *process, obj_handle_t handle )
351 {
352     struct handle_table *table;
353     struct handle_entry *entry;
354     struct object *obj;
355
356     if (!(entry = get_handle( process, handle ))) return STATUS_INVALID_HANDLE;
357     if (entry->access & RESERVED_CLOSE_PROTECT) return STATUS_HANDLE_NOT_CLOSABLE;
358     obj = entry->ptr;
359     if (!obj->ops->close_handle( obj, process, handle )) return STATUS_HANDLE_NOT_CLOSABLE;
360     entry->ptr = NULL;
361     table = handle_is_global(handle) ? global_table : process->handles;
362     if (entry < table->entries + table->free) table->free = entry - table->entries;
363     if (entry == table->entries + table->last) shrink_handle_table( table );
364     release_object( obj );
365     return STATUS_SUCCESS;
366 }
367
368 /* retrieve the object corresponding to one of the magic pseudo-handles */
369 static inline struct object *get_magic_handle( obj_handle_t handle )
370 {
371     switch(handle)
372     {
373         case 0xfffffffe:  /* current thread pseudo-handle */
374             return &current->obj;
375         case 0x7fffffff:  /* current process pseudo-handle */
376         case 0xffffffff:  /* current process pseudo-handle */
377             return (struct object *)current->process;
378         default:
379             return NULL;
380     }
381 }
382
383 /* retrieve the object corresponding to a handle, incrementing its refcount */
384 struct object *get_handle_obj( struct process *process, obj_handle_t handle,
385                                unsigned int access, const struct object_ops *ops )
386 {
387     struct handle_entry *entry;
388     struct object *obj;
389
390     if (!(obj = get_magic_handle( handle )))
391     {
392         if (!(entry = get_handle( process, handle )))
393         {
394             set_error( STATUS_INVALID_HANDLE );
395             return NULL;
396         }
397         if ((entry->access & access) != access)
398         {
399             set_error( STATUS_ACCESS_DENIED );
400             return NULL;
401         }
402         obj = entry->ptr;
403     }
404     if (ops && (obj->ops != ops))
405     {
406         set_error( STATUS_OBJECT_TYPE_MISMATCH );  /* not the right type */
407         return NULL;
408     }
409     return grab_object( obj );
410 }
411
412 /* retrieve the access rights of a given handle */
413 unsigned int get_handle_access( struct process *process, obj_handle_t handle )
414 {
415     struct handle_entry *entry;
416
417     if (get_magic_handle( handle )) return ~RESERVED_ALL;  /* magic handles have all access rights */
418     if (!(entry = get_handle( process, handle ))) return 0;
419     return entry->access & ~RESERVED_ALL;
420 }
421
422 /* find the first inherited handle of the given type */
423 /* this is needed for window stations and desktops (don't ask...) */
424 obj_handle_t find_inherited_handle( struct process *process, const struct object_ops *ops )
425 {
426     struct handle_table *table = process->handles;
427     struct handle_entry *ptr;
428     int i;
429
430     if (!table) return 0;
431
432     for (i = 0, ptr = table->entries; i <= table->last; i++, ptr++)
433     {
434         if (!ptr->ptr) continue;
435         if (ptr->ptr->ops != ops) continue;
436         if (ptr->access & RESERVED_INHERIT) return index_to_handle(i);
437     }
438     return 0;
439 }
440
441 /* enumerate handles of a given type */
442 /* this is needed for window stations and desktops */
443 obj_handle_t enumerate_handles( struct process *process, const struct object_ops *ops,
444                                 unsigned int *index )
445 {
446     struct handle_table *table = process->handles;
447     unsigned int i;
448     struct handle_entry *entry;
449
450     if (!table) return 0;
451
452     for (i = *index, entry = &table->entries[i]; i <= table->last; i++, entry++)
453     {
454         if (!entry->ptr) continue;
455         if (entry->ptr->ops != ops) continue;
456         *index = i + 1;
457         return index_to_handle(i);
458     }
459     return 0;
460 }
461
462 /* get/set the handle reserved flags */
463 /* return the old flags (or -1 on error) */
464 static int set_handle_flags( struct process *process, obj_handle_t handle, int mask, int flags )
465 {
466     struct handle_entry *entry;
467     unsigned int old_access;
468
469     if (get_magic_handle( handle ))
470     {
471         /* we can retrieve but not set info for magic handles */
472         if (mask) set_error( STATUS_ACCESS_DENIED );
473         return 0;
474     }
475     if (!(entry = get_handle( process, handle )))
476     {
477         set_error( STATUS_INVALID_HANDLE );
478         return -1;
479     }
480     old_access = entry->access;
481     mask  = (mask << RESERVED_SHIFT) & RESERVED_ALL;
482     flags = (flags << RESERVED_SHIFT) & mask;
483     entry->access = (entry->access & ~mask) | flags;
484     return (old_access & RESERVED_ALL) >> RESERVED_SHIFT;
485 }
486
487 /* duplicate a handle */
488 obj_handle_t duplicate_handle( struct process *src, obj_handle_t src_handle, struct process *dst,
489                                unsigned int access, unsigned int attr, unsigned int options )
490 {
491     obj_handle_t res;
492     struct handle_entry *entry;
493     unsigned int src_access;
494     struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
495
496     if (!obj) return 0;
497     if ((entry = get_handle( src, src_handle )))
498         src_access = entry->access;
499     else  /* pseudo-handle, give it full access */
500         src_access = obj->ops->map_access( obj, GENERIC_ALL );
501     src_access &= ~RESERVED_ALL;
502
503     if (options & DUP_HANDLE_SAME_ACCESS)
504         access = src_access;
505     else
506         access = obj->ops->map_access( obj, access ) & ~RESERVED_ALL;
507
508     /* asking for the more access rights than src_access? */
509     if (access & ~src_access)
510     {
511         if (options & DUP_HANDLE_MAKE_GLOBAL)
512             res = alloc_global_handle( obj, access );
513         else
514             res = alloc_handle( dst, obj, access, attr );
515     }
516     else
517     {
518         if (options & DUP_HANDLE_MAKE_GLOBAL)
519             res = alloc_global_handle_no_access_check( obj, access );
520         else
521             res = alloc_handle_no_access_check( dst, obj, access, attr );
522     }
523
524     release_object( obj );
525     return res;
526 }
527
528 /* open a new handle to an existing object */
529 obj_handle_t open_object( const struct namespace *namespace, const struct unicode_str *name,
530                           const struct object_ops *ops, unsigned int access, unsigned int attr )
531 {
532     obj_handle_t handle = 0;
533     struct object *obj = find_object( namespace, name, attr );
534     if (obj)
535     {
536         if (ops && obj->ops != ops)
537             set_error( STATUS_OBJECT_TYPE_MISMATCH );
538         else
539             handle = alloc_handle( current->process, obj, access, attr );
540         release_object( obj );
541     }
542     else
543         set_error( STATUS_OBJECT_NAME_NOT_FOUND );
544     return handle;
545 }
546
547 /* return the size of the handle table of a given process */
548 unsigned int get_handle_table_count( struct process *process )
549 {
550     if (!process->handles) return 0;
551     return process->handles->count;
552 }
553
554 /* close a handle */
555 DECL_HANDLER(close_handle)
556 {
557     unsigned int err = close_handle( current->process, req->handle );
558     set_error( err );
559 }
560
561 /* set a handle information */
562 DECL_HANDLER(set_handle_info)
563 {
564     reply->old_flags = set_handle_flags( current->process, req->handle, req->mask, req->flags );
565 }
566
567 /* duplicate a handle */
568 DECL_HANDLER(dup_handle)
569 {
570     struct process *src, *dst;
571
572     reply->handle = 0;
573     if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
574     {
575         if (req->options & DUP_HANDLE_MAKE_GLOBAL)
576         {
577             reply->handle = duplicate_handle( src, req->src_handle, NULL,
578                                               req->access, req->attributes, req->options );
579         }
580         else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
581         {
582             reply->handle = duplicate_handle( src, req->src_handle, dst,
583                                               req->access, req->attributes, req->options );
584             release_object( dst );
585         }
586         /* close the handle no matter what happened */
587         if (req->options & DUP_HANDLE_CLOSE_SOURCE) reply->closed = !close_handle( src, req->src_handle );
588         reply->self = (src == current->process);
589         release_object( src );
590     }
591 }
592
593 DECL_HANDLER(get_object_info)
594 {
595     struct object *obj;
596
597     if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
598
599     reply->access = get_handle_access( current->process, req->handle );
600     reply->ref_count = obj->refcount;
601     release_object( obj );
602 }
603
604 DECL_HANDLER(set_security_object)
605 {
606     data_size_t sd_size = get_req_data_size();
607     const struct security_descriptor *sd = get_req_data();
608     struct object *obj;
609     unsigned int access = 0;
610
611     if (!sd_is_valid( sd, sd_size ))
612     {
613         set_error( STATUS_ACCESS_VIOLATION );
614         return;
615     }
616
617     if (req->security_info & OWNER_SECURITY_INFORMATION ||
618         req->security_info & GROUP_SECURITY_INFORMATION)
619         access |= WRITE_OWNER;
620     if (req->security_info & SACL_SECURITY_INFORMATION)
621         access |= ACCESS_SYSTEM_SECURITY;
622     if (req->security_info & DACL_SECURITY_INFORMATION)
623         access |= WRITE_DAC;
624
625     if (!(obj = get_handle_obj( current->process, req->handle, access, NULL ))) return;
626
627     obj->ops->set_sd( obj, sd, req->security_info );
628     release_object( obj );
629 }
630
631 DECL_HANDLER(get_security_object)
632 {
633     const struct security_descriptor *sd;
634     struct object *obj;
635     unsigned int access = READ_CONTROL;
636     struct security_descriptor req_sd;
637     int present;
638     const SID *owner, *group;
639     const ACL *sacl, *dacl;
640
641     if (req->security_info & SACL_SECURITY_INFORMATION)
642         access |= ACCESS_SYSTEM_SECURITY;
643
644     if (!(obj = get_handle_obj( current->process, req->handle, access, NULL ))) return;
645
646     sd = obj->ops->get_sd( obj );
647     if (sd)
648     {
649         req_sd.control = sd->control & ~SE_SELF_RELATIVE;
650
651         owner = sd_get_owner( sd );
652         if (req->security_info & OWNER_SECURITY_INFORMATION)
653             req_sd.owner_len = sd->owner_len;
654         else
655             req_sd.owner_len = 0;
656
657         group = sd_get_group( sd );
658         if (req->security_info & GROUP_SECURITY_INFORMATION)
659             req_sd.group_len = sd->group_len;
660         else
661             req_sd.group_len = 0;
662
663         req_sd.control |= SE_SACL_PRESENT;
664         sacl = sd_get_sacl( sd, &present );
665         if (req->security_info & SACL_SECURITY_INFORMATION && present)
666             req_sd.sacl_len = sd->sacl_len;
667         else
668             req_sd.sacl_len = 0;
669
670         req_sd.control |= SE_DACL_PRESENT;
671         dacl = sd_get_dacl( sd, &present );
672         if (req->security_info & DACL_SECURITY_INFORMATION && present)
673             req_sd.dacl_len = sd->dacl_len;
674         else
675             req_sd.dacl_len = 0;
676
677         reply->sd_len = sizeof(req_sd) + req_sd.owner_len + req_sd.group_len +
678             req_sd.sacl_len + req_sd.dacl_len;
679         if (reply->sd_len <= get_reply_max_size())
680         {
681             char *ptr = set_reply_data_size(reply->sd_len);
682
683             memcpy( ptr, &req_sd, sizeof(req_sd) );
684             ptr += sizeof(req_sd);
685             memcpy( ptr, owner, req_sd.owner_len );
686             ptr += req_sd.owner_len;
687             memcpy( ptr, group, req_sd.group_len );
688             ptr += req_sd.group_len;
689             memcpy( ptr, sacl, req_sd.sacl_len );
690             ptr += req_sd.sacl_len;
691             memcpy( ptr, dacl, req_sd.dacl_len );
692         }
693         else
694             set_error(STATUS_BUFFER_TOO_SMALL);
695     }
696
697     release_object( obj );
698 }