wined3d: Pack ARB srgb constants better.
[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) goto error;
290     index = handle_to_index( handle );
291     if (index < 0) goto error;
292     if (index > table->last) goto error;
293     entry = table->entries + index;
294     if (!entry->ptr) goto error;
295     return entry;
296
297  error:
298     set_error( STATUS_INVALID_HANDLE );
299     return NULL;
300 }
301
302 /* attempt to shrink a table */
303 static void shrink_handle_table( struct handle_table *table )
304 {
305     struct handle_entry *entry = table->entries + table->last;
306     struct handle_entry *new_entries;
307     int count = table->count;
308
309     while (table->last >= 0)
310     {
311         if (entry->ptr) break;
312         table->last--;
313         entry--;
314     }
315     if (table->last >= count / 4) return;  /* no need to shrink */
316     if (count < MIN_HANDLE_ENTRIES * 2) return;  /* too small to shrink */
317     count /= 2;
318     if (!(new_entries = realloc( table->entries, count * sizeof(*new_entries) ))) return;
319     table->count   = count;
320     table->entries = new_entries;
321 }
322
323 /* copy the handle table of the parent process */
324 /* return 1 if OK, 0 on error */
325 struct handle_table *copy_handle_table( struct process *process, struct process *parent )
326 {
327     struct handle_table *parent_table = parent->handles;
328     struct handle_table *table;
329     int i;
330
331     assert( parent_table );
332     assert( parent_table->obj.ops == &handle_table_ops );
333
334     if (!(table = (struct handle_table *)alloc_handle_table( process, parent_table->count )))
335         return NULL;
336
337     if ((table->last = parent_table->last) >= 0)
338     {
339         struct handle_entry *ptr = table->entries;
340         memcpy( ptr, parent_table->entries, (table->last + 1) * sizeof(struct handle_entry) );
341         for (i = 0; i <= table->last; i++, ptr++)
342         {
343             if (!ptr->ptr) continue;
344             if (ptr->access & RESERVED_INHERIT) grab_object( ptr->ptr );
345             else ptr->ptr = NULL; /* don't inherit this entry */
346         }
347     }
348     /* attempt to shrink the table */
349     shrink_handle_table( table );
350     return table;
351 }
352
353 /* close a handle and decrement the refcount of the associated object */
354 /* return 1 if OK, 0 on error */
355 int close_handle( struct process *process, obj_handle_t handle )
356 {
357     struct handle_table *table;
358     struct handle_entry *entry;
359     struct object *obj;
360
361     if (!(entry = get_handle( process, handle ))) return 0;
362     if (entry->access & RESERVED_CLOSE_PROTECT)
363     {
364         set_error( STATUS_HANDLE_NOT_CLOSABLE );
365         return 0;
366     }
367     obj = entry->ptr;
368     if (!obj->ops->close_handle( obj, process, handle ))
369     {
370         set_error( STATUS_HANDLE_NOT_CLOSABLE );
371         return 0;
372     }
373     entry->ptr = NULL;
374     table = handle_is_global(handle) ? global_table : process->handles;
375     if (entry < table->entries + table->free) table->free = entry - table->entries;
376     if (entry == table->entries + table->last) shrink_handle_table( table );
377     release_object( obj );
378     return 1;
379 }
380
381 /* retrieve the object corresponding to one of the magic pseudo-handles */
382 static inline struct object *get_magic_handle( obj_handle_t handle )
383 {
384     switch(handle)
385     {
386         case 0xfffffffe:  /* current thread pseudo-handle */
387             return &current->obj;
388         case 0x7fffffff:  /* current process pseudo-handle */
389         case 0xffffffff:  /* current process pseudo-handle */
390             return (struct object *)current->process;
391         default:
392             return NULL;
393     }
394 }
395
396 /* retrieve the object corresponding to a handle, incrementing its refcount */
397 struct object *get_handle_obj( struct process *process, obj_handle_t handle,
398                                unsigned int access, const struct object_ops *ops )
399 {
400     struct handle_entry *entry;
401     struct object *obj;
402
403     if (!(obj = get_magic_handle( handle )))
404     {
405         if (!(entry = get_handle( process, handle ))) return NULL;
406         if ((entry->access & access) != access)
407         {
408             set_error( STATUS_ACCESS_DENIED );
409             return NULL;
410         }
411         obj = entry->ptr;
412     }
413     if (ops && (obj->ops != ops))
414     {
415         set_error( STATUS_OBJECT_TYPE_MISMATCH );  /* not the right type */
416         return NULL;
417     }
418     return grab_object( obj );
419 }
420
421 /* retrieve the access rights of a given handle */
422 unsigned int get_handle_access( struct process *process, obj_handle_t handle )
423 {
424     struct handle_entry *entry;
425
426     if (get_magic_handle( handle )) return ~RESERVED_ALL;  /* magic handles have all access rights */
427     if (!(entry = get_handle( process, handle ))) return 0;
428     return entry->access & ~RESERVED_ALL;
429 }
430
431 /* find the first inherited handle of the given type */
432 /* this is needed for window stations and desktops (don't ask...) */
433 obj_handle_t find_inherited_handle( struct process *process, const struct object_ops *ops )
434 {
435     struct handle_table *table = process->handles;
436     struct handle_entry *ptr;
437     int i;
438
439     if (!table) return 0;
440
441     for (i = 0, ptr = table->entries; i <= table->last; i++, ptr++)
442     {
443         if (!ptr->ptr) continue;
444         if (ptr->ptr->ops != ops) continue;
445         if (ptr->access & RESERVED_INHERIT) return index_to_handle(i);
446     }
447     return 0;
448 }
449
450 /* enumerate handles of a given type */
451 /* this is needed for window stations and desktops */
452 obj_handle_t enumerate_handles( struct process *process, const struct object_ops *ops,
453                                 unsigned int *index )
454 {
455     struct handle_table *table = process->handles;
456     unsigned int i;
457     struct handle_entry *entry;
458
459     if (!table) return 0;
460
461     for (i = *index, entry = &table->entries[i]; i <= table->last; i++, entry++)
462     {
463         if (!entry->ptr) continue;
464         if (entry->ptr->ops != ops) continue;
465         *index = i + 1;
466         return index_to_handle(i);
467     }
468     return 0;
469 }
470
471 /* get/set the handle reserved flags */
472 /* return the old flags (or -1 on error) */
473 static int set_handle_flags( struct process *process, obj_handle_t handle, int mask, int flags )
474 {
475     struct handle_entry *entry;
476     unsigned int old_access;
477
478     if (get_magic_handle( handle ))
479     {
480         /* we can retrieve but not set info for magic handles */
481         if (mask) set_error( STATUS_ACCESS_DENIED );
482         return 0;
483     }
484     if (!(entry = get_handle( process, handle ))) return -1;
485     old_access = entry->access;
486     mask  = (mask << RESERVED_SHIFT) & RESERVED_ALL;
487     flags = (flags << RESERVED_SHIFT) & mask;
488     entry->access = (entry->access & ~mask) | flags;
489     return (old_access & RESERVED_ALL) >> RESERVED_SHIFT;
490 }
491
492 /* duplicate a handle */
493 obj_handle_t duplicate_handle( struct process *src, obj_handle_t src_handle, struct process *dst,
494                                unsigned int access, unsigned int attr, unsigned int options )
495 {
496     obj_handle_t res;
497     struct handle_entry *entry;
498     unsigned int src_access;
499     struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
500
501     if (!obj) return 0;
502     if ((entry = get_handle( src, src_handle )))
503         src_access = entry->access;
504     else  /* pseudo-handle, give it full access */
505     {
506         src_access = obj->ops->map_access( obj, GENERIC_ALL );
507         clear_error();
508     }
509     src_access &= ~RESERVED_ALL;
510
511     if (options & DUP_HANDLE_SAME_ACCESS)
512         access = src_access;
513     else
514         access = obj->ops->map_access( obj, access ) & ~RESERVED_ALL;
515
516     /* asking for the more access rights than src_access? */
517     if (access & ~src_access)
518     {
519         if (options & DUP_HANDLE_MAKE_GLOBAL)
520             res = alloc_global_handle( obj, access );
521         else
522             res = alloc_handle( dst, obj, access, attr );
523     }
524     else
525     {
526         if (options & DUP_HANDLE_MAKE_GLOBAL)
527             res = alloc_global_handle_no_access_check( obj, access );
528         else
529             res = alloc_handle_no_access_check( dst, obj, access, attr );
530     }
531
532     release_object( obj );
533     return res;
534 }
535
536 /* open a new handle to an existing object */
537 obj_handle_t open_object( const struct namespace *namespace, const struct unicode_str *name,
538                           const struct object_ops *ops, unsigned int access, unsigned int attr )
539 {
540     obj_handle_t handle = 0;
541     struct object *obj = find_object( namespace, name, attr );
542     if (obj)
543     {
544         if (ops && obj->ops != ops)
545             set_error( STATUS_OBJECT_TYPE_MISMATCH );
546         else
547             handle = alloc_handle( current->process, obj, access, attr );
548         release_object( obj );
549     }
550     else
551         set_error( STATUS_OBJECT_NAME_NOT_FOUND );
552     return handle;
553 }
554
555 /* return the size of the handle table of a given process */
556 unsigned int get_handle_table_count( struct process *process )
557 {
558     if (!process->handles) return 0;
559     return process->handles->count;
560 }
561
562 /* close a handle */
563 DECL_HANDLER(close_handle)
564 {
565     close_handle( current->process, req->handle );
566 }
567
568 /* set a handle information */
569 DECL_HANDLER(set_handle_info)
570 {
571     reply->old_flags = set_handle_flags( current->process, req->handle, req->mask, req->flags );
572 }
573
574 /* duplicate a handle */
575 DECL_HANDLER(dup_handle)
576 {
577     struct process *src, *dst;
578
579     reply->handle = 0;
580     if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
581     {
582         if (req->options & DUP_HANDLE_MAKE_GLOBAL)
583         {
584             reply->handle = duplicate_handle( src, req->src_handle, NULL,
585                                               req->access, req->attributes, req->options );
586         }
587         else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
588         {
589             reply->handle = duplicate_handle( src, req->src_handle, dst,
590                                               req->access, req->attributes, req->options );
591             release_object( dst );
592         }
593         /* close the handle no matter what happened */
594         if (req->options & DUP_HANDLE_CLOSE_SOURCE)
595         {
596             unsigned int err = get_error();  /* don't overwrite error from the above calls */
597             reply->closed = close_handle( src, req->src_handle );
598             set_error( err );
599         }
600         reply->self = (src == current->process);
601         release_object( src );
602     }
603 }
604
605 DECL_HANDLER(get_object_info)
606 {
607     struct object *obj;
608
609     if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
610
611     reply->access = get_handle_access( current->process, req->handle );
612     reply->ref_count = obj->refcount;
613     release_object( obj );
614 }
615
616 DECL_HANDLER(set_security_object)
617 {
618     data_size_t sd_size = get_req_data_size();
619     const struct security_descriptor *sd = get_req_data();
620     struct object *obj;
621     unsigned int access = 0;
622
623     if (!sd_is_valid( sd, sd_size ))
624     {
625         set_error( STATUS_ACCESS_VIOLATION );
626         return;
627     }
628
629     if (req->security_info & OWNER_SECURITY_INFORMATION ||
630         req->security_info & GROUP_SECURITY_INFORMATION)
631         access |= WRITE_OWNER;
632     if (req->security_info & SACL_SECURITY_INFORMATION)
633         access |= ACCESS_SYSTEM_SECURITY;
634     if (req->security_info & DACL_SECURITY_INFORMATION)
635         access |= WRITE_DAC;
636
637     if (!(obj = get_handle_obj( current->process, req->handle, access, NULL ))) return;
638
639     obj->ops->set_sd( obj, sd, req->security_info );
640     release_object( obj );
641 }
642
643 DECL_HANDLER(get_security_object)
644 {
645     const struct security_descriptor *sd;
646     struct object *obj;
647     unsigned int access = READ_CONTROL;
648     struct security_descriptor req_sd;
649     int present;
650     const SID *owner, *group;
651     const ACL *sacl, *dacl;
652
653     if (req->security_info & SACL_SECURITY_INFORMATION)
654         access |= ACCESS_SYSTEM_SECURITY;
655
656     if (!(obj = get_handle_obj( current->process, req->handle, access, NULL ))) return;
657
658     sd = obj->ops->get_sd( obj );
659     if (sd)
660     {
661         req_sd.control = sd->control & ~SE_SELF_RELATIVE;
662
663         owner = sd_get_owner( sd );
664         if (req->security_info & OWNER_SECURITY_INFORMATION)
665             req_sd.owner_len = sd->owner_len;
666         else
667             req_sd.owner_len = 0;
668
669         group = sd_get_group( sd );
670         if (req->security_info & GROUP_SECURITY_INFORMATION)
671             req_sd.group_len = sd->group_len;
672         else
673             req_sd.group_len = 0;
674
675         req_sd.control |= SE_SACL_PRESENT;
676         sacl = sd_get_sacl( sd, &present );
677         if (req->security_info & SACL_SECURITY_INFORMATION && present)
678             req_sd.sacl_len = sd->sacl_len;
679         else
680             req_sd.sacl_len = 0;
681
682         req_sd.control |= SE_DACL_PRESENT;
683         dacl = sd_get_dacl( sd, &present );
684         if (req->security_info & DACL_SECURITY_INFORMATION && present)
685             req_sd.dacl_len = sd->dacl_len;
686         else
687             req_sd.dacl_len = 0;
688
689         reply->sd_len = sizeof(req_sd) + req_sd.owner_len + req_sd.group_len +
690             req_sd.sacl_len + req_sd.dacl_len;
691         if (reply->sd_len <= get_reply_max_size())
692         {
693             char *ptr = set_reply_data_size(reply->sd_len);
694
695             memcpy( ptr, &req_sd, sizeof(req_sd) );
696             ptr += sizeof(req_sd);
697             memcpy( ptr, owner, req_sd.owner_len );
698             ptr += req_sd.owner_len;
699             memcpy( ptr, group, req_sd.group_len );
700             ptr += req_sd.group_len;
701             memcpy( ptr, sacl, req_sd.sacl_len );
702             ptr += req_sd.sacl_len;
703             memcpy( ptr, dacl, req_sd.dacl_len );
704         }
705         else
706             set_error(STATUS_BUFFER_TOO_SMALL);
707     }
708
709     release_object( obj );
710 }