server: Don't set last error in get_handle(), move that into the callers.
[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 /* return 1 if OK, 0 on error */
351 int close_handle( struct process *process, obj_handle_t handle )
352 {
353     struct handle_table *table;
354     struct handle_entry *entry;
355     struct object *obj;
356
357     if (!(entry = get_handle( process, handle )))
358     {
359         set_error( STATUS_INVALID_HANDLE );
360         return 0;
361     }
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 )))
406         {
407             set_error( STATUS_INVALID_HANDLE );
408             return NULL;
409         }
410         if ((entry->access & access) != access)
411         {
412             set_error( STATUS_ACCESS_DENIED );
413             return NULL;
414         }
415         obj = entry->ptr;
416     }
417     if (ops && (obj->ops != ops))
418     {
419         set_error( STATUS_OBJECT_TYPE_MISMATCH );  /* not the right type */
420         return NULL;
421     }
422     return grab_object( obj );
423 }
424
425 /* retrieve the access rights of a given handle */
426 unsigned int get_handle_access( struct process *process, obj_handle_t handle )
427 {
428     struct handle_entry *entry;
429
430     if (get_magic_handle( handle )) return ~RESERVED_ALL;  /* magic handles have all access rights */
431     if (!(entry = get_handle( process, handle ))) return 0;
432     return entry->access & ~RESERVED_ALL;
433 }
434
435 /* find the first inherited handle of the given type */
436 /* this is needed for window stations and desktops (don't ask...) */
437 obj_handle_t find_inherited_handle( struct process *process, const struct object_ops *ops )
438 {
439     struct handle_table *table = process->handles;
440     struct handle_entry *ptr;
441     int i;
442
443     if (!table) return 0;
444
445     for (i = 0, ptr = table->entries; i <= table->last; i++, ptr++)
446     {
447         if (!ptr->ptr) continue;
448         if (ptr->ptr->ops != ops) continue;
449         if (ptr->access & RESERVED_INHERIT) return index_to_handle(i);
450     }
451     return 0;
452 }
453
454 /* enumerate handles of a given type */
455 /* this is needed for window stations and desktops */
456 obj_handle_t enumerate_handles( struct process *process, const struct object_ops *ops,
457                                 unsigned int *index )
458 {
459     struct handle_table *table = process->handles;
460     unsigned int i;
461     struct handle_entry *entry;
462
463     if (!table) return 0;
464
465     for (i = *index, entry = &table->entries[i]; i <= table->last; i++, entry++)
466     {
467         if (!entry->ptr) continue;
468         if (entry->ptr->ops != ops) continue;
469         *index = i + 1;
470         return index_to_handle(i);
471     }
472     return 0;
473 }
474
475 /* get/set the handle reserved flags */
476 /* return the old flags (or -1 on error) */
477 static int set_handle_flags( struct process *process, obj_handle_t handle, int mask, int flags )
478 {
479     struct handle_entry *entry;
480     unsigned int old_access;
481
482     if (get_magic_handle( handle ))
483     {
484         /* we can retrieve but not set info for magic handles */
485         if (mask) set_error( STATUS_ACCESS_DENIED );
486         return 0;
487     }
488     if (!(entry = get_handle( process, handle )))
489     {
490         set_error( STATUS_INVALID_HANDLE );
491         return -1;
492     }
493     old_access = entry->access;
494     mask  = (mask << RESERVED_SHIFT) & RESERVED_ALL;
495     flags = (flags << RESERVED_SHIFT) & mask;
496     entry->access = (entry->access & ~mask) | flags;
497     return (old_access & RESERVED_ALL) >> RESERVED_SHIFT;
498 }
499
500 /* duplicate a handle */
501 obj_handle_t duplicate_handle( struct process *src, obj_handle_t src_handle, struct process *dst,
502                                unsigned int access, unsigned int attr, unsigned int options )
503 {
504     obj_handle_t res;
505     struct handle_entry *entry;
506     unsigned int src_access;
507     struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
508
509     if (!obj) return 0;
510     if ((entry = get_handle( src, src_handle )))
511         src_access = entry->access;
512     else  /* pseudo-handle, give it full access */
513         src_access = obj->ops->map_access( obj, GENERIC_ALL );
514     src_access &= ~RESERVED_ALL;
515
516     if (options & DUP_HANDLE_SAME_ACCESS)
517         access = src_access;
518     else
519         access = obj->ops->map_access( obj, access ) & ~RESERVED_ALL;
520
521     /* asking for the more access rights than src_access? */
522     if (access & ~src_access)
523     {
524         if (options & DUP_HANDLE_MAKE_GLOBAL)
525             res = alloc_global_handle( obj, access );
526         else
527             res = alloc_handle( dst, obj, access, attr );
528     }
529     else
530     {
531         if (options & DUP_HANDLE_MAKE_GLOBAL)
532             res = alloc_global_handle_no_access_check( obj, access );
533         else
534             res = alloc_handle_no_access_check( dst, obj, access, attr );
535     }
536
537     release_object( obj );
538     return res;
539 }
540
541 /* open a new handle to an existing object */
542 obj_handle_t open_object( const struct namespace *namespace, const struct unicode_str *name,
543                           const struct object_ops *ops, unsigned int access, unsigned int attr )
544 {
545     obj_handle_t handle = 0;
546     struct object *obj = find_object( namespace, name, attr );
547     if (obj)
548     {
549         if (ops && obj->ops != ops)
550             set_error( STATUS_OBJECT_TYPE_MISMATCH );
551         else
552             handle = alloc_handle( current->process, obj, access, attr );
553         release_object( obj );
554     }
555     else
556         set_error( STATUS_OBJECT_NAME_NOT_FOUND );
557     return handle;
558 }
559
560 /* return the size of the handle table of a given process */
561 unsigned int get_handle_table_count( struct process *process )
562 {
563     if (!process->handles) return 0;
564     return process->handles->count;
565 }
566
567 /* close a handle */
568 DECL_HANDLER(close_handle)
569 {
570     close_handle( current->process, req->handle );
571 }
572
573 /* set a handle information */
574 DECL_HANDLER(set_handle_info)
575 {
576     reply->old_flags = set_handle_flags( current->process, req->handle, req->mask, req->flags );
577 }
578
579 /* duplicate a handle */
580 DECL_HANDLER(dup_handle)
581 {
582     struct process *src, *dst;
583
584     reply->handle = 0;
585     if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
586     {
587         if (req->options & DUP_HANDLE_MAKE_GLOBAL)
588         {
589             reply->handle = duplicate_handle( src, req->src_handle, NULL,
590                                               req->access, req->attributes, req->options );
591         }
592         else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
593         {
594             reply->handle = duplicate_handle( src, req->src_handle, dst,
595                                               req->access, req->attributes, req->options );
596             release_object( dst );
597         }
598         /* close the handle no matter what happened */
599         if (req->options & DUP_HANDLE_CLOSE_SOURCE)
600         {
601             unsigned int err = get_error();  /* don't overwrite error from the above calls */
602             reply->closed = close_handle( src, req->src_handle );
603             set_error( err );
604         }
605         reply->self = (src == current->process);
606         release_object( src );
607     }
608 }
609
610 DECL_HANDLER(get_object_info)
611 {
612     struct object *obj;
613
614     if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
615
616     reply->access = get_handle_access( current->process, req->handle );
617     reply->ref_count = obj->refcount;
618     release_object( obj );
619 }
620
621 DECL_HANDLER(set_security_object)
622 {
623     data_size_t sd_size = get_req_data_size();
624     const struct security_descriptor *sd = get_req_data();
625     struct object *obj;
626     unsigned int access = 0;
627
628     if (!sd_is_valid( sd, sd_size ))
629     {
630         set_error( STATUS_ACCESS_VIOLATION );
631         return;
632     }
633
634     if (req->security_info & OWNER_SECURITY_INFORMATION ||
635         req->security_info & GROUP_SECURITY_INFORMATION)
636         access |= WRITE_OWNER;
637     if (req->security_info & SACL_SECURITY_INFORMATION)
638         access |= ACCESS_SYSTEM_SECURITY;
639     if (req->security_info & DACL_SECURITY_INFORMATION)
640         access |= WRITE_DAC;
641
642     if (!(obj = get_handle_obj( current->process, req->handle, access, NULL ))) return;
643
644     obj->ops->set_sd( obj, sd, req->security_info );
645     release_object( obj );
646 }
647
648 DECL_HANDLER(get_security_object)
649 {
650     const struct security_descriptor *sd;
651     struct object *obj;
652     unsigned int access = READ_CONTROL;
653     struct security_descriptor req_sd;
654     int present;
655     const SID *owner, *group;
656     const ACL *sacl, *dacl;
657
658     if (req->security_info & SACL_SECURITY_INFORMATION)
659         access |= ACCESS_SYSTEM_SECURITY;
660
661     if (!(obj = get_handle_obj( current->process, req->handle, access, NULL ))) return;
662
663     sd = obj->ops->get_sd( obj );
664     if (sd)
665     {
666         req_sd.control = sd->control & ~SE_SELF_RELATIVE;
667
668         owner = sd_get_owner( sd );
669         if (req->security_info & OWNER_SECURITY_INFORMATION)
670             req_sd.owner_len = sd->owner_len;
671         else
672             req_sd.owner_len = 0;
673
674         group = sd_get_group( sd );
675         if (req->security_info & GROUP_SECURITY_INFORMATION)
676             req_sd.group_len = sd->group_len;
677         else
678             req_sd.group_len = 0;
679
680         req_sd.control |= SE_SACL_PRESENT;
681         sacl = sd_get_sacl( sd, &present );
682         if (req->security_info & SACL_SECURITY_INFORMATION && present)
683             req_sd.sacl_len = sd->sacl_len;
684         else
685             req_sd.sacl_len = 0;
686
687         req_sd.control |= SE_DACL_PRESENT;
688         dacl = sd_get_dacl( sd, &present );
689         if (req->security_info & DACL_SECURITY_INFORMATION && present)
690             req_sd.dacl_len = sd->dacl_len;
691         else
692             req_sd.dacl_len = 0;
693
694         reply->sd_len = sizeof(req_sd) + req_sd.owner_len + req_sd.group_len +
695             req_sd.sacl_len + req_sd.dacl_len;
696         if (reply->sd_len <= get_reply_max_size())
697         {
698             char *ptr = set_reply_data_size(reply->sd_len);
699
700             memcpy( ptr, &req_sd, sizeof(req_sd) );
701             ptr += sizeof(req_sd);
702             memcpy( ptr, owner, req_sd.owner_len );
703             ptr += req_sd.owner_len;
704             memcpy( ptr, group, req_sd.group_len );
705             ptr += req_sd.group_len;
706             memcpy( ptr, sacl, req_sd.sacl_len );
707             ptr += req_sd.sacl_len;
708             memcpy( ptr, dacl, req_sd.dacl_len );
709         }
710         else
711             set_error(STATUS_BUFFER_TOO_SMALL);
712     }
713
714     release_object( obj );
715 }