winhttp: Make WinHttpQueryHeaders behave the same way for all query types.
[wine/hramrach.git] / server / handle.c
blob4e52f0ed1cc6e1bde1f21f636eb157f5bd45bf8b
1 /*
2 * Server-side handle management
4 * Copyright (C) 1998 Alexandre Julliard
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <limits.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
31 #include "ntstatus.h"
32 #define WIN32_NO_STATUS
33 #include "windef.h"
34 #include "winternl.h"
36 #include "handle.h"
37 #include "process.h"
38 #include "thread.h"
39 #include "security.h"
40 #include "request.h"
42 struct handle_entry
44 struct object *ptr; /* object */
45 unsigned int access; /* access rights */
48 struct handle_table
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 */
58 static struct handle_table *global_table;
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)
66 #define MIN_HANDLE_ENTRIES 32
67 #define MAX_HANDLE_ENTRIES 0x00ffffff
70 /* handle to table index conversion */
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 )
75 return (obj_handle_t)((index + 1) << 2);
77 static inline int handle_to_index( obj_handle_t handle )
79 return (handle >> 2) - 1;
82 /* global handle conversion */
84 #define HANDLE_OBFUSCATOR 0x544a4def
86 static inline int handle_is_global( obj_handle_t handle)
88 return (handle ^ HANDLE_OBFUSCATOR) <= (MAX_HANDLE_ENTRIES << 2);
90 static inline obj_handle_t handle_local_to_global( obj_handle_t handle )
92 if (!handle) return 0;
93 return handle ^ HANDLE_OBFUSCATOR;
95 static inline obj_handle_t handle_global_to_local( obj_handle_t handle )
97 return handle ^ HANDLE_OBFUSCATOR;
101 static void handle_table_dump( struct object *obj, int verbose );
102 static void handle_table_destroy( struct object *obj );
104 static const struct object_ops handle_table_ops =
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 */
124 /* dump a handle table */
125 static void handle_table_dump( struct object *obj, int verbose )
127 int i;
128 struct handle_table *table = (struct handle_table *)obj;
129 struct handle_entry *entry = table->entries;
131 assert( obj->ops == &handle_table_ops );
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++)
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 );
146 /* destroy a handle table */
147 static void handle_table_destroy( struct object *obj )
149 int i;
150 struct handle_table *table = (struct handle_table *)obj;
151 struct handle_entry *entry;
153 assert( obj->ops == &handle_table_ops );
155 /* first notify all objects that handles are being closed */
156 if (table->process)
158 for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
160 struct object *obj = entry->ptr;
161 if (obj) obj->ops->close_handle( obj, table->process, index_to_handle(i) );
165 for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
167 struct object *obj = entry->ptr;
168 entry->ptr = NULL;
169 if (obj) release_object( obj );
171 free( table->entries );
174 /* allocate a new handle table */
175 struct handle_table *alloc_handle_table( struct process *process, int count )
177 struct handle_table *table;
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;
191 /* grow a handle table */
192 static int grow_handle_table( struct handle_table *table )
194 struct handle_entry *new_entries;
195 int count = min( table->count * 2, MAX_HANDLE_ENTRIES );
197 if (count == table->count ||
198 !(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) )))
200 set_error( STATUS_INSUFFICIENT_RESOURCES );
201 return 0;
203 table->entries = new_entries;
204 table->count = count;
205 return 1;
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 )
211 struct handle_entry *entry = table->entries + table->free;
212 int i;
214 for (i = table->free; i <= table->last; i++, entry++) if (!entry->ptr) goto found;
215 if (i >= table->count)
217 if (!grow_handle_table( table )) return 0;
218 entry = table->entries + i; /* the entries may have moved */
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);
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 )
232 struct object *obj = ptr;
234 access &= ~RESERVED_ALL;
235 if (attr & OBJ_INHERIT) access |= RESERVED_INHERIT;
236 if (!process->handles)
238 set_error( STATUS_NO_MEMORY );
239 return 0;
241 return alloc_entry( process->handles, obj, access );
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 )
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 );
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 )
259 if (!global_table)
261 if (!(global_table = (struct handle_table *)alloc_handle_table( NULL, 0 )))
262 return 0;
263 make_object_static( &global_table->obj );
265 return handle_local_to_global( alloc_entry( global_table, obj, access ));
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 )
273 if (access && !check_object_access( obj, &access )) return 0;
274 return alloc_global_handle_no_access_check( obj, access );
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 )
280 struct handle_table *table = process->handles;
281 struct handle_entry *entry;
282 int index;
284 if (handle_is_global(handle))
286 handle = handle_global_to_local(handle);
287 table = global_table;
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;
298 /* attempt to shrink a table */
299 static void shrink_handle_table( struct handle_table *table )
301 struct handle_entry *entry = table->entries + table->last;
302 struct handle_entry *new_entries;
303 int count = table->count;
305 while (table->last >= 0)
307 if (entry->ptr) break;
308 table->last--;
309 entry--;
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;
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 )
323 struct handle_table *parent_table = parent->handles;
324 struct handle_table *table;
325 int i;
327 assert( parent_table );
328 assert( parent_table->obj.ops == &handle_table_ops );
330 if (!(table = (struct handle_table *)alloc_handle_table( process, parent_table->count )))
331 return NULL;
333 if ((table->last = parent_table->last) >= 0)
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++)
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 */
344 /* attempt to shrink the table */
345 shrink_handle_table( table );
346 return table;
349 /* close a handle and decrement the refcount of the associated object */
350 unsigned int close_handle( struct process *process, obj_handle_t handle )
352 struct handle_table *table;
353 struct handle_entry *entry;
354 struct object *obj;
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;
368 /* retrieve the object corresponding to one of the magic pseudo-handles */
369 static inline struct object *get_magic_handle( obj_handle_t handle )
371 switch(handle)
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;
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 )
387 struct handle_entry *entry;
388 struct object *obj;
390 if (!(obj = get_magic_handle( handle )))
392 if (!(entry = get_handle( process, handle )))
394 set_error( STATUS_INVALID_HANDLE );
395 return NULL;
397 if ((entry->access & access) != access)
399 set_error( STATUS_ACCESS_DENIED );
400 return NULL;
402 obj = entry->ptr;
404 if (ops && (obj->ops != ops))
406 set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */
407 return NULL;
409 return grab_object( obj );
412 /* retrieve the access rights of a given handle */
413 unsigned int get_handle_access( struct process *process, obj_handle_t handle )
415 struct handle_entry *entry;
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;
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 )
426 struct handle_table *table = process->handles;
427 struct handle_entry *ptr;
428 int i;
430 if (!table) return 0;
432 for (i = 0, ptr = table->entries; i <= table->last; i++, ptr++)
434 if (!ptr->ptr) continue;
435 if (ptr->ptr->ops != ops) continue;
436 if (ptr->access & RESERVED_INHERIT) return index_to_handle(i);
438 return 0;
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 )
446 struct handle_table *table = process->handles;
447 unsigned int i;
448 struct handle_entry *entry;
450 if (!table) return 0;
452 for (i = *index, entry = &table->entries[i]; i <= table->last; i++, entry++)
454 if (!entry->ptr) continue;
455 if (entry->ptr->ops != ops) continue;
456 *index = i + 1;
457 return index_to_handle(i);
459 return 0;
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 )
466 struct handle_entry *entry;
467 unsigned int old_access;
469 if (get_magic_handle( handle ))
471 /* we can retrieve but not set info for magic handles */
472 if (mask) set_error( STATUS_ACCESS_DENIED );
473 return 0;
475 if (!(entry = get_handle( process, handle )))
477 set_error( STATUS_INVALID_HANDLE );
478 return -1;
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;
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 )
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 );
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;
503 if (options & DUP_HANDLE_SAME_ACCESS)
504 access = src_access;
505 else
506 access = obj->ops->map_access( obj, access ) & ~RESERVED_ALL;
508 /* asking for the more access rights than src_access? */
509 if (access & ~src_access)
511 if (options & DUP_HANDLE_MAKE_GLOBAL)
512 res = alloc_global_handle( obj, access );
513 else
514 res = alloc_handle( dst, obj, access, attr );
516 else
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 );
524 release_object( obj );
525 return res;
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 )
532 obj_handle_t handle = 0;
533 struct object *obj = find_object( namespace, name, attr );
534 if (obj)
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 );
542 else
543 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
544 return handle;
547 /* return the size of the handle table of a given process */
548 unsigned int get_handle_table_count( struct process *process )
550 if (!process->handles) return 0;
551 return process->handles->count;
554 /* close a handle */
555 DECL_HANDLER(close_handle)
557 unsigned int err = close_handle( current->process, req->handle );
558 set_error( err );
561 /* set a handle information */
562 DECL_HANDLER(set_handle_info)
564 reply->old_flags = set_handle_flags( current->process, req->handle, req->mask, req->flags );
567 /* duplicate a handle */
568 DECL_HANDLER(dup_handle)
570 struct process *src, *dst;
572 reply->handle = 0;
573 if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
575 if (req->options & DUP_HANDLE_MAKE_GLOBAL)
577 reply->handle = duplicate_handle( src, req->src_handle, NULL,
578 req->access, req->attributes, req->options );
580 else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
582 reply->handle = duplicate_handle( src, req->src_handle, dst,
583 req->access, req->attributes, req->options );
584 release_object( dst );
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 );
593 DECL_HANDLER(get_object_info)
595 struct object *obj;
596 WCHAR *name;
598 if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
600 reply->access = get_handle_access( current->process, req->handle );
601 reply->ref_count = obj->refcount;
602 if ((name = get_object_full_name( obj, &reply->total )))
603 set_reply_data_ptr( name, min( reply->total, get_reply_max_size() ));
604 release_object( obj );
607 DECL_HANDLER(set_security_object)
609 data_size_t sd_size = get_req_data_size();
610 const struct security_descriptor *sd = get_req_data();
611 struct object *obj;
612 unsigned int access = 0;
614 if (!sd_is_valid( sd, sd_size ))
616 set_error( STATUS_ACCESS_VIOLATION );
617 return;
620 if (req->security_info & OWNER_SECURITY_INFORMATION ||
621 req->security_info & GROUP_SECURITY_INFORMATION)
622 access |= WRITE_OWNER;
623 if (req->security_info & SACL_SECURITY_INFORMATION)
624 access |= ACCESS_SYSTEM_SECURITY;
625 if (req->security_info & DACL_SECURITY_INFORMATION)
626 access |= WRITE_DAC;
628 if (!(obj = get_handle_obj( current->process, req->handle, access, NULL ))) return;
630 obj->ops->set_sd( obj, sd, req->security_info );
631 release_object( obj );
634 DECL_HANDLER(get_security_object)
636 const struct security_descriptor *sd;
637 struct object *obj;
638 unsigned int access = READ_CONTROL;
639 struct security_descriptor req_sd;
640 int present;
641 const SID *owner, *group;
642 const ACL *sacl, *dacl;
644 if (req->security_info & SACL_SECURITY_INFORMATION)
645 access |= ACCESS_SYSTEM_SECURITY;
647 if (!(obj = get_handle_obj( current->process, req->handle, access, NULL ))) return;
649 sd = obj->ops->get_sd( obj );
650 if (sd)
652 req_sd.control = sd->control & ~SE_SELF_RELATIVE;
654 owner = sd_get_owner( sd );
655 if (req->security_info & OWNER_SECURITY_INFORMATION)
656 req_sd.owner_len = sd->owner_len;
657 else
658 req_sd.owner_len = 0;
660 group = sd_get_group( sd );
661 if (req->security_info & GROUP_SECURITY_INFORMATION)
662 req_sd.group_len = sd->group_len;
663 else
664 req_sd.group_len = 0;
666 req_sd.control |= SE_SACL_PRESENT;
667 sacl = sd_get_sacl( sd, &present );
668 if (req->security_info & SACL_SECURITY_INFORMATION && present)
669 req_sd.sacl_len = sd->sacl_len;
670 else
671 req_sd.sacl_len = 0;
673 req_sd.control |= SE_DACL_PRESENT;
674 dacl = sd_get_dacl( sd, &present );
675 if (req->security_info & DACL_SECURITY_INFORMATION && present)
676 req_sd.dacl_len = sd->dacl_len;
677 else
678 req_sd.dacl_len = 0;
680 reply->sd_len = sizeof(req_sd) + req_sd.owner_len + req_sd.group_len +
681 req_sd.sacl_len + req_sd.dacl_len;
682 if (reply->sd_len <= get_reply_max_size())
684 char *ptr = set_reply_data_size(reply->sd_len);
686 memcpy( ptr, &req_sd, sizeof(req_sd) );
687 ptr += sizeof(req_sd);
688 memcpy( ptr, owner, req_sd.owner_len );
689 ptr += req_sd.owner_len;
690 memcpy( ptr, group, req_sd.group_len );
691 ptr += req_sd.group_len;
692 memcpy( ptr, sacl, req_sd.sacl_len );
693 ptr += req_sd.sacl_len;
694 memcpy( ptr, dacl, req_sd.dacl_len );
696 else
697 set_error(STATUS_BUFFER_TOO_SMALL);
700 release_object( obj );