mshtml: Implement url prop for StorageEvent.
[wine.git] / server / object.c
blob89e541ffb6b9448383c4791911f1e14801aaeedb
1 /*
2 * Server-side objects
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"
23 #include <assert.h>
24 #include <limits.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <stdarg.h>
30 #include <sys/types.h>
31 #ifdef HAVE_VALGRIND_MEMCHECK_H
32 #include <valgrind/memcheck.h>
33 #endif
35 #include "ntstatus.h"
36 #define WIN32_NO_STATUS
37 #include "winternl.h"
39 #include "file.h"
40 #include "process.h"
41 #include "thread.h"
42 #include "unicode.h"
43 #include "security.h"
46 struct namespace
48 unsigned int hash_size; /* size of hash table */
49 struct list names[1]; /* array of hash entry lists */
53 struct type_descr no_type =
55 { NULL, 0 }, /* name */
56 STANDARD_RIGHTS_REQUIRED, /* valid_access */
57 { /* mapping */
58 STANDARD_RIGHTS_READ,
59 STANDARD_RIGHTS_WRITE,
60 STANDARD_RIGHTS_EXECUTE,
61 STANDARD_RIGHTS_REQUIRED
65 #ifdef DEBUG_OBJECTS
66 static struct list object_list = LIST_INIT(object_list);
68 void dump_objects(void)
70 struct object *ptr;
72 LIST_FOR_EACH_ENTRY( ptr, &object_list, struct object, obj_list )
74 fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
75 dump_object_name( ptr );
76 ptr->ops->dump( ptr, 1 );
80 void close_objects(void)
82 /* release the permanent objects */
83 for (;;)
85 struct object *obj;
86 int found = 0;
88 LIST_FOR_EACH_ENTRY( obj, &object_list, struct object, obj_list )
90 if (!(found = obj->is_permanent)) continue;
91 obj->is_permanent = 0;
92 release_object( obj );
93 break;
95 if (!found) break;
98 dump_objects(); /* dump any remaining objects */
101 #endif /* DEBUG_OBJECTS */
103 /*****************************************************************/
105 /* mark a block of memory as uninitialized for debugging purposes */
106 static inline void mark_block_uninitialized( void *ptr, size_t size )
108 memset( ptr, 0x55, size );
109 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
110 VALGRIND_DISCARD( VALGRIND_MAKE_MEM_UNDEFINED( ptr, size ));
111 #elif defined(VALGRIND_MAKE_WRITABLE)
112 VALGRIND_DISCARD( VALGRIND_MAKE_WRITABLE( ptr, size ));
113 #endif
116 /* malloc replacement */
117 void *mem_alloc( size_t size )
119 void *ptr = malloc( size );
120 if (ptr) mark_block_uninitialized( ptr, size );
121 else set_error( STATUS_NO_MEMORY );
122 return ptr;
125 /* duplicate a block of memory */
126 void *memdup( const void *data, size_t len )
128 void *ptr = malloc( len );
129 if (ptr) memcpy( ptr, data, len );
130 else set_error( STATUS_NO_MEMORY );
131 return ptr;
135 /*****************************************************************/
137 void namespace_add( struct namespace *namespace, struct object_name *ptr )
139 unsigned int hash = hash_strW( ptr->name, ptr->len, namespace->hash_size );
141 list_add_head( &namespace->names[hash], &ptr->entry );
144 /* allocate a name for an object */
145 static struct object_name *alloc_name( const struct unicode_str *name )
147 struct object_name *ptr;
149 if ((ptr = mem_alloc( sizeof(*ptr) + name->len - sizeof(ptr->name) )))
151 ptr->len = name->len;
152 ptr->parent = NULL;
153 memcpy( ptr->name, name->str, name->len );
155 return ptr;
158 /* get the name of an existing object */
159 const WCHAR *get_object_name( struct object *obj, data_size_t *len )
161 struct object_name *ptr = obj->name;
162 if (!ptr) return NULL;
163 *len = ptr->len;
164 return ptr->name;
167 /* get the full path name of an existing object */
168 WCHAR *default_get_full_name( struct object *obj, data_size_t *ret_len )
170 static const WCHAR backslash = '\\';
171 struct object *ptr = obj;
172 data_size_t len = 0;
173 char *ret;
175 while (ptr && ptr->name)
177 struct object_name *name = ptr->name;
178 len += name->len + sizeof(WCHAR);
179 ptr = name->parent;
181 if (!len) return NULL;
182 if (!(ret = malloc( len ))) return NULL;
184 *ret_len = len;
185 while (obj && obj->name)
187 struct object_name *name = obj->name;
188 memcpy( ret + len - name->len, name->name, name->len );
189 len -= name->len + sizeof(WCHAR);
190 memcpy( ret + len, &backslash, sizeof(WCHAR) );
191 obj = name->parent;
193 return (WCHAR *)ret;
196 /* allocate and initialize an object */
197 void *alloc_object( const struct object_ops *ops )
199 struct object *obj = mem_alloc( ops->size );
200 if (obj)
202 obj->refcount = 1;
203 obj->handle_count = 0;
204 obj->is_permanent = 0;
205 obj->ops = ops;
206 obj->name = NULL;
207 obj->sd = NULL;
208 list_init( &obj->wait_queue );
209 #ifdef DEBUG_OBJECTS
210 list_add_head( &object_list, &obj->obj_list );
211 #endif
212 obj->ops->type->obj_count++;
213 obj->ops->type->obj_max = max( obj->ops->type->obj_max, obj->ops->type->obj_count );
214 return obj;
216 return NULL;
219 /* free an object once it has been destroyed */
220 static void free_object( struct object *obj )
222 free( obj->sd );
223 obj->ops->type->obj_count--;
224 #ifdef DEBUG_OBJECTS
225 list_remove( &obj->obj_list );
226 memset( obj, 0xaa, obj->ops->size );
227 #endif
228 free( obj );
231 /* find an object by name starting from the specified root */
232 /* if it doesn't exist, its parent is returned, and name_left contains the remaining name */
233 struct object *lookup_named_object( struct object *root, const struct unicode_str *name,
234 unsigned int attr, struct unicode_str *name_left )
236 static int recursion_count;
237 struct object *obj, *parent;
238 struct unicode_str name_tmp = *name, *ptr = &name_tmp;
240 if (root)
242 /* if root is specified path shouldn't start with backslash */
243 if (name_tmp.len && name_tmp.str[0] == '\\')
245 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
246 return NULL;
248 parent = grab_object( root );
250 else
252 if (!name_tmp.len || name_tmp.str[0] != '\\')
254 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
255 return NULL;
257 /* skip leading backslash */
258 name_tmp.str++;
259 name_tmp.len -= sizeof(WCHAR);
260 parent = root = get_root_directory();
263 if (!name_tmp.len) ptr = NULL; /* special case for empty path */
265 if (recursion_count > 32)
267 set_error( STATUS_INVALID_PARAMETER );
268 release_object( parent );
269 return NULL;
271 recursion_count++;
272 clear_error();
274 while ((obj = parent->ops->lookup_name( parent, ptr, attr, root )))
276 /* move to the next element */
277 release_object ( parent );
278 parent = obj;
281 recursion_count--;
282 if (get_error())
284 release_object( parent );
285 return NULL;
288 if (name_left) *name_left = name_tmp;
289 return parent;
292 /* return length of first path element in name */
293 data_size_t get_path_element( const WCHAR *name, data_size_t len )
295 data_size_t i;
297 for (i = 0; i < len / sizeof(WCHAR); i++) if (name[i] == '\\') break;
298 return i * sizeof(WCHAR);
301 static struct object *create_object( struct object *parent, const struct object_ops *ops,
302 const struct unicode_str *name, unsigned int attributes,
303 const struct security_descriptor *sd )
305 struct object *obj;
306 struct object_name *name_ptr;
308 if (!(name_ptr = alloc_name( name ))) return NULL;
309 if (!(obj = alloc_object( ops ))) goto failed;
310 if (sd && !default_set_sd( obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
311 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION ))
312 goto failed;
313 if (!obj->ops->link_name( obj, name_ptr, parent )) goto failed;
315 name_ptr->obj = obj;
316 obj->name = name_ptr;
317 return obj;
319 failed:
320 if (obj) free_object( obj );
321 free( name_ptr );
322 return NULL;
325 /* create an object as named child under the specified parent */
326 void *create_named_object( struct object *parent, const struct object_ops *ops,
327 const struct unicode_str *name, unsigned int attributes,
328 const struct security_descriptor *sd )
330 struct object *obj, *new_obj;
331 struct unicode_str new_name;
333 clear_error();
335 if (!name || !name->len)
337 if (!(new_obj = alloc_object( ops ))) return NULL;
338 if (sd && !default_set_sd( new_obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
339 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION ))
341 free_object( new_obj );
342 return NULL;
345 else
347 if (!(obj = lookup_named_object( parent, name, attributes, &new_name ))) return NULL;
349 if (!new_name.len)
351 if (attributes & OBJ_OPENIF && obj->ops == ops)
353 set_error( STATUS_OBJECT_NAME_EXISTS );
354 return obj;
356 release_object( obj );
357 if (attributes & OBJ_OPENIF)
358 set_error( STATUS_OBJECT_TYPE_MISMATCH );
359 else
360 set_error( STATUS_OBJECT_NAME_COLLISION );
361 return NULL;
364 new_obj = create_object( obj, ops, &new_name, attributes, sd );
365 release_object( obj );
366 if (!new_obj) return NULL;
369 if (attributes & OBJ_PERMANENT)
371 make_object_permanent( new_obj );
372 grab_object( new_obj );
374 return new_obj;
377 /* open a object by name under the specified parent */
378 void *open_named_object( struct object *parent, const struct object_ops *ops,
379 const struct unicode_str *name, unsigned int attributes )
381 struct unicode_str name_left;
382 struct object *obj;
384 if ((obj = lookup_named_object( parent, name, attributes, &name_left )))
386 if (name_left.len) /* not fully parsed */
387 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
388 else if (ops && obj->ops != ops)
389 set_error( STATUS_OBJECT_TYPE_MISMATCH );
390 else
391 return obj;
393 release_object( obj );
395 return NULL;
398 /* recursive helper for dump_object_name */
399 static void dump_name( struct object *obj )
401 struct object_name *name = obj->name;
403 if (!name) return;
404 if (name->parent) dump_name( name->parent );
405 fputs( "\\\\", stderr );
406 dump_strW( name->name, name->len, stderr, "[]" );
409 /* dump the name of an object to stderr */
410 void dump_object_name( struct object *obj )
412 if (!obj->name) return;
413 fputc( '[', stderr );
414 dump_name( obj );
415 fputs( "] ", stderr );
418 /* unlink a named object from its namespace, without freeing the object itself */
419 void unlink_named_object( struct object *obj )
421 struct object_name *name_ptr = obj->name;
423 if (!name_ptr) return;
424 obj->name = NULL;
425 obj->ops->unlink_name( obj, name_ptr );
426 if (name_ptr->parent) release_object( name_ptr->parent );
427 free( name_ptr );
430 /* grab an object (i.e. increment its refcount) and return the object */
431 struct object *grab_object( void *ptr )
433 struct object *obj = (struct object *)ptr;
434 assert( obj->refcount < INT_MAX );
435 obj->refcount++;
436 return obj;
439 /* release an object (i.e. decrement its refcount) */
440 void release_object( void *ptr )
442 struct object *obj = (struct object *)ptr;
443 assert( obj->refcount );
444 if (!--obj->refcount)
446 assert( !obj->handle_count );
447 /* if the refcount is 0, nobody can be in the wait queue */
448 assert( list_empty( &obj->wait_queue ));
449 free_kernel_objects( obj );
450 unlink_named_object( obj );
451 obj->ops->destroy( obj );
452 free_object( obj );
456 /* find an object by its name; the refcount is incremented */
457 struct object *find_object( const struct namespace *namespace, const struct unicode_str *name,
458 unsigned int attributes )
460 const struct list *list;
461 struct list *p;
463 if (!name || !name->len) return NULL;
465 list = &namespace->names[ hash_strW( name->str, name->len, namespace->hash_size ) ];
466 LIST_FOR_EACH( p, list )
468 const struct object_name *ptr = LIST_ENTRY( p, struct object_name, entry );
469 if (ptr->len != name->len) continue;
470 if (attributes & OBJ_CASE_INSENSITIVE)
472 if (!memicmp_strW( ptr->name, name->str, name->len ))
473 return grab_object( ptr->obj );
475 else
477 if (!memcmp( ptr->name, name->str, name->len ))
478 return grab_object( ptr->obj );
481 return NULL;
484 /* find an object by its index; the refcount is incremented */
485 struct object *find_object_index( const struct namespace *namespace, unsigned int index )
487 unsigned int i;
489 /* FIXME: not efficient at all */
490 for (i = 0; i < namespace->hash_size; i++)
492 const struct object_name *ptr;
493 LIST_FOR_EACH_ENTRY( ptr, &namespace->names[i], const struct object_name, entry )
495 if (!index--) return grab_object( ptr->obj );
498 set_error( STATUS_NO_MORE_ENTRIES );
499 return NULL;
502 /* allocate a namespace */
503 struct namespace *create_namespace( unsigned int hash_size )
505 struct namespace *namespace;
506 unsigned int i;
508 namespace = mem_alloc( sizeof(*namespace) + (hash_size - 1) * sizeof(namespace->names[0]) );
509 if (namespace)
511 namespace->hash_size = hash_size;
512 for (i = 0; i < hash_size; i++) list_init( &namespace->names[i] );
514 return namespace;
517 /* functions for unimplemented/default object operations */
519 int no_add_queue( struct object *obj, struct wait_queue_entry *entry )
521 set_error( STATUS_OBJECT_TYPE_MISMATCH );
522 return 0;
525 void no_satisfied( struct object *obj, struct wait_queue_entry *entry )
529 int no_signal( struct object *obj, unsigned int access )
531 set_error( STATUS_OBJECT_TYPE_MISMATCH );
532 return 0;
535 struct fd *no_get_fd( struct object *obj )
537 set_error( STATUS_OBJECT_TYPE_MISMATCH );
538 return NULL;
541 unsigned int default_map_access( struct object *obj, unsigned int access )
543 return map_access( access, &obj->ops->type->mapping );
546 struct security_descriptor *default_get_sd( struct object *obj )
548 return obj->sd;
551 int set_sd_defaults_from_token( struct object *obj, const struct security_descriptor *sd,
552 unsigned int set_info, struct token *token )
554 struct security_descriptor new_sd, *new_sd_ptr;
555 int present;
556 const struct sid *owner = NULL, *group = NULL;
557 const struct acl *sacl, *dacl;
558 struct acl *replaced_sacl = NULL;
559 char *ptr;
561 if (!set_info) return 1;
563 new_sd.control = sd->control & ~SE_SELF_RELATIVE;
565 if (set_info & OWNER_SECURITY_INFORMATION && sd->owner_len)
567 owner = sd_get_owner( sd );
568 new_sd.owner_len = sd->owner_len;
570 else if (obj->sd && obj->sd->owner_len)
572 owner = sd_get_owner( obj->sd );
573 new_sd.owner_len = obj->sd->owner_len;
575 else if (token)
577 owner = token_get_owner( token );
578 new_sd.owner_len = sid_len( owner );
580 else new_sd.owner_len = 0;
582 if (set_info & GROUP_SECURITY_INFORMATION && sd->group_len)
584 group = sd_get_group( sd );
585 new_sd.group_len = sd->group_len;
587 else if (obj->sd && obj->sd->group_len)
589 group = sd_get_group( obj->sd );
590 new_sd.group_len = obj->sd->group_len;
592 else if (token)
594 group = token_get_primary_group( token );
595 new_sd.group_len = sid_len( group );
597 else new_sd.group_len = 0;
599 sacl = sd_get_sacl( sd, &present );
600 if (set_info & SACL_SECURITY_INFORMATION && present)
602 new_sd.control |= SE_SACL_PRESENT;
603 new_sd.sacl_len = sd->sacl_len;
605 else if (set_info & LABEL_SECURITY_INFORMATION && present)
607 const struct acl *old_sacl = NULL;
608 if (obj->sd && obj->sd->control & SE_SACL_PRESENT) old_sacl = sd_get_sacl( obj->sd, &present );
609 if (!(replaced_sacl = replace_security_labels( old_sacl, sacl ))) return 0;
610 new_sd.control |= SE_SACL_PRESENT;
611 new_sd.sacl_len = replaced_sacl->size;
612 sacl = replaced_sacl;
614 else
616 if (obj->sd) sacl = sd_get_sacl( obj->sd, &present );
618 if (obj->sd && present)
620 new_sd.control |= SE_SACL_PRESENT;
621 new_sd.sacl_len = obj->sd->sacl_len;
623 else
624 new_sd.sacl_len = 0;
627 dacl = sd_get_dacl( sd, &present );
628 if (set_info & DACL_SECURITY_INFORMATION && present)
630 new_sd.control |= SE_DACL_PRESENT;
631 new_sd.dacl_len = sd->dacl_len;
633 else
635 if (obj->sd) dacl = sd_get_dacl( obj->sd, &present );
637 if (obj->sd && present)
639 new_sd.control |= SE_DACL_PRESENT;
640 new_sd.dacl_len = obj->sd->dacl_len;
642 else if (token)
644 dacl = token_get_default_dacl( token );
645 new_sd.control |= SE_DACL_PRESENT;
646 new_sd.dacl_len = dacl->size;
648 else new_sd.dacl_len = 0;
651 ptr = mem_alloc( sizeof(new_sd) + new_sd.owner_len + new_sd.group_len +
652 new_sd.sacl_len + new_sd.dacl_len );
653 if (!ptr)
655 free( replaced_sacl );
656 return 0;
658 new_sd_ptr = (struct security_descriptor*)ptr;
660 memcpy( ptr, &new_sd, sizeof(new_sd) );
661 ptr += sizeof(new_sd);
662 memcpy( ptr, owner, new_sd.owner_len );
663 ptr += new_sd.owner_len;
664 memcpy( ptr, group, new_sd.group_len );
665 ptr += new_sd.group_len;
666 memcpy( ptr, sacl, new_sd.sacl_len );
667 ptr += new_sd.sacl_len;
668 memcpy( ptr, dacl, new_sd.dacl_len );
670 free( replaced_sacl );
671 free( obj->sd );
672 obj->sd = new_sd_ptr;
673 return 1;
676 /** Set the security descriptor using the current primary token for defaults. */
677 int default_set_sd( struct object *obj, const struct security_descriptor *sd,
678 unsigned int set_info )
680 return set_sd_defaults_from_token( obj, sd, set_info, current->process->token );
683 WCHAR *no_get_full_name( struct object *obj, data_size_t *ret_len )
685 return NULL;
688 struct object *no_lookup_name( struct object *obj, struct unicode_str *name,
689 unsigned int attr, struct object *root )
691 if (!name) set_error( STATUS_OBJECT_TYPE_MISMATCH );
692 return NULL;
695 int no_link_name( struct object *obj, struct object_name *name, struct object *parent )
697 set_error( STATUS_OBJECT_TYPE_MISMATCH );
698 return 0;
701 void default_unlink_name( struct object *obj, struct object_name *name )
703 list_remove( &name->entry );
706 struct object *no_open_file( struct object *obj, unsigned int access, unsigned int sharing,
707 unsigned int options )
709 set_error( STATUS_OBJECT_TYPE_MISMATCH );
710 return NULL;
713 int no_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
715 return 1; /* ok to close */
718 void no_destroy( struct object *obj )