sc: Don't silently fail when something doesn't work as expected in a few more places.
[wine.git] / server / object.c
blob907bc0874443287c50f2e7e1a29df4835340efed
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 struct object *obj, *parent;
237 struct unicode_str name_tmp = *name, *ptr = &name_tmp;
239 if (root)
241 /* if root is specified path shouldn't start with backslash */
242 if (name_tmp.len && name_tmp.str[0] == '\\')
244 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
245 return NULL;
247 parent = grab_object( root );
249 else
251 if (!name_tmp.len || name_tmp.str[0] != '\\')
253 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
254 return NULL;
256 /* skip leading backslash */
257 name_tmp.str++;
258 name_tmp.len -= sizeof(WCHAR);
259 parent = root = get_root_directory();
262 if (!name_tmp.len) ptr = NULL; /* special case for empty path */
264 clear_error();
266 while ((obj = parent->ops->lookup_name( parent, ptr, attr, root )))
268 /* move to the next element */
269 release_object ( parent );
270 parent = obj;
272 if (get_error())
274 release_object( parent );
275 return NULL;
278 if (name_left) *name_left = name_tmp;
279 return parent;
282 /* return length of first path element in name */
283 data_size_t get_path_element( const WCHAR *name, data_size_t len )
285 data_size_t i;
287 for (i = 0; i < len / sizeof(WCHAR); i++) if (name[i] == '\\') break;
288 return i * sizeof(WCHAR);
291 static struct object *create_object( struct object *parent, const struct object_ops *ops,
292 const struct unicode_str *name, unsigned int attributes,
293 const struct security_descriptor *sd )
295 struct object *obj;
296 struct object_name *name_ptr;
298 if (!(name_ptr = alloc_name( name ))) return NULL;
299 if (!(obj = alloc_object( ops ))) goto failed;
300 if (sd && !default_set_sd( obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
301 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION ))
302 goto failed;
303 if (!obj->ops->link_name( obj, name_ptr, parent )) goto failed;
305 name_ptr->obj = obj;
306 obj->name = name_ptr;
307 return obj;
309 failed:
310 if (obj) free_object( obj );
311 free( name_ptr );
312 return NULL;
315 /* create an object as named child under the specified parent */
316 void *create_named_object( struct object *parent, const struct object_ops *ops,
317 const struct unicode_str *name, unsigned int attributes,
318 const struct security_descriptor *sd )
320 struct object *obj, *new_obj;
321 struct unicode_str new_name;
323 clear_error();
325 if (!name || !name->len)
327 if (!(new_obj = alloc_object( ops ))) return NULL;
328 if (sd && !default_set_sd( new_obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
329 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION ))
331 free_object( new_obj );
332 return NULL;
334 goto done;
337 if (!(obj = lookup_named_object( parent, name, attributes, &new_name ))) return NULL;
339 if (!new_name.len)
341 if (attributes & OBJ_OPENIF && obj->ops == ops)
342 set_error( STATUS_OBJECT_NAME_EXISTS );
343 else
345 release_object( obj );
346 obj = NULL;
347 if (attributes & OBJ_OPENIF)
348 set_error( STATUS_OBJECT_TYPE_MISMATCH );
349 else
350 set_error( STATUS_OBJECT_NAME_COLLISION );
352 return obj;
355 new_obj = create_object( obj, ops, &new_name, attributes, sd );
356 release_object( obj );
358 done:
359 if (attributes & OBJ_PERMANENT)
361 make_object_permanent( new_obj );
362 grab_object( new_obj );
364 return new_obj;
367 /* open a object by name under the specified parent */
368 void *open_named_object( struct object *parent, const struct object_ops *ops,
369 const struct unicode_str *name, unsigned int attributes )
371 struct unicode_str name_left;
372 struct object *obj;
374 if ((obj = lookup_named_object( parent, name, attributes, &name_left )))
376 if (name_left.len) /* not fully parsed */
377 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
378 else if (ops && obj->ops != ops)
379 set_error( STATUS_OBJECT_TYPE_MISMATCH );
380 else
381 return obj;
383 release_object( obj );
385 return NULL;
388 /* recursive helper for dump_object_name */
389 static void dump_name( struct object *obj )
391 struct object_name *name = obj->name;
393 if (!name) return;
394 if (name->parent) dump_name( name->parent );
395 fputs( "\\\\", stderr );
396 dump_strW( name->name, name->len, stderr, "[]" );
399 /* dump the name of an object to stderr */
400 void dump_object_name( struct object *obj )
402 if (!obj->name) return;
403 fputc( '[', stderr );
404 dump_name( obj );
405 fputs( "] ", stderr );
408 /* unlink a named object from its namespace, without freeing the object itself */
409 void unlink_named_object( struct object *obj )
411 struct object_name *name_ptr = obj->name;
413 if (!name_ptr) return;
414 obj->name = NULL;
415 obj->ops->unlink_name( obj, name_ptr );
416 if (name_ptr->parent) release_object( name_ptr->parent );
417 free( name_ptr );
420 /* grab an object (i.e. increment its refcount) and return the object */
421 struct object *grab_object( void *ptr )
423 struct object *obj = (struct object *)ptr;
424 assert( obj->refcount < INT_MAX );
425 obj->refcount++;
426 return obj;
429 /* release an object (i.e. decrement its refcount) */
430 void release_object( void *ptr )
432 struct object *obj = (struct object *)ptr;
433 assert( obj->refcount );
434 if (!--obj->refcount)
436 assert( !obj->handle_count );
437 /* if the refcount is 0, nobody can be in the wait queue */
438 assert( list_empty( &obj->wait_queue ));
439 free_kernel_objects( obj );
440 unlink_named_object( obj );
441 obj->ops->destroy( obj );
442 free_object( obj );
446 /* find an object by its name; the refcount is incremented */
447 struct object *find_object( const struct namespace *namespace, const struct unicode_str *name,
448 unsigned int attributes )
450 const struct list *list;
451 struct list *p;
453 if (!name || !name->len) return NULL;
455 list = &namespace->names[ hash_strW( name->str, name->len, namespace->hash_size ) ];
456 LIST_FOR_EACH( p, list )
458 const struct object_name *ptr = LIST_ENTRY( p, struct object_name, entry );
459 if (ptr->len != name->len) continue;
460 if (attributes & OBJ_CASE_INSENSITIVE)
462 if (!memicmp_strW( ptr->name, name->str, name->len ))
463 return grab_object( ptr->obj );
465 else
467 if (!memcmp( ptr->name, name->str, name->len ))
468 return grab_object( ptr->obj );
471 return NULL;
474 /* find an object by its index; the refcount is incremented */
475 struct object *find_object_index( const struct namespace *namespace, unsigned int index )
477 unsigned int i;
479 /* FIXME: not efficient at all */
480 for (i = 0; i < namespace->hash_size; i++)
482 const struct object_name *ptr;
483 LIST_FOR_EACH_ENTRY( ptr, &namespace->names[i], const struct object_name, entry )
485 if (!index--) return grab_object( ptr->obj );
488 set_error( STATUS_NO_MORE_ENTRIES );
489 return NULL;
492 /* allocate a namespace */
493 struct namespace *create_namespace( unsigned int hash_size )
495 struct namespace *namespace;
496 unsigned int i;
498 namespace = mem_alloc( sizeof(*namespace) + (hash_size - 1) * sizeof(namespace->names[0]) );
499 if (namespace)
501 namespace->hash_size = hash_size;
502 for (i = 0; i < hash_size; i++) list_init( &namespace->names[i] );
504 return namespace;
507 /* functions for unimplemented/default object operations */
509 int no_add_queue( struct object *obj, struct wait_queue_entry *entry )
511 set_error( STATUS_OBJECT_TYPE_MISMATCH );
512 return 0;
515 void no_satisfied( struct object *obj, struct wait_queue_entry *entry )
519 int no_signal( struct object *obj, unsigned int access )
521 set_error( STATUS_OBJECT_TYPE_MISMATCH );
522 return 0;
525 struct fd *no_get_fd( struct object *obj )
527 set_error( STATUS_OBJECT_TYPE_MISMATCH );
528 return NULL;
531 unsigned int default_map_access( struct object *obj, unsigned int access )
533 return map_access( access, &obj->ops->type->mapping );
536 struct security_descriptor *default_get_sd( struct object *obj )
538 return obj->sd;
541 int set_sd_defaults_from_token( struct object *obj, const struct security_descriptor *sd,
542 unsigned int set_info, struct token *token )
544 struct security_descriptor new_sd, *new_sd_ptr;
545 int present;
546 const struct sid *owner = NULL, *group = NULL;
547 const struct acl *sacl, *dacl;
548 struct acl *replaced_sacl = NULL;
549 char *ptr;
551 if (!set_info) return 1;
553 new_sd.control = sd->control & ~SE_SELF_RELATIVE;
555 if (set_info & OWNER_SECURITY_INFORMATION && sd->owner_len)
557 owner = sd_get_owner( sd );
558 new_sd.owner_len = sd->owner_len;
560 else if (obj->sd && obj->sd->owner_len)
562 owner = sd_get_owner( obj->sd );
563 new_sd.owner_len = obj->sd->owner_len;
565 else if (token)
567 owner = token_get_user( token );
568 new_sd.owner_len = sid_len( owner );
570 else new_sd.owner_len = 0;
572 if (set_info & GROUP_SECURITY_INFORMATION && sd->group_len)
574 group = sd_get_group( sd );
575 new_sd.group_len = sd->group_len;
577 else if (obj->sd && obj->sd->group_len)
579 group = sd_get_group( obj->sd );
580 new_sd.group_len = obj->sd->group_len;
582 else if (token)
584 group = token_get_primary_group( token );
585 new_sd.group_len = sid_len( group );
587 else new_sd.group_len = 0;
589 sacl = sd_get_sacl( sd, &present );
590 if (set_info & SACL_SECURITY_INFORMATION && present)
592 new_sd.control |= SE_SACL_PRESENT;
593 new_sd.sacl_len = sd->sacl_len;
595 else if (set_info & LABEL_SECURITY_INFORMATION && present)
597 const struct acl *old_sacl = NULL;
598 if (obj->sd && obj->sd->control & SE_SACL_PRESENT) old_sacl = sd_get_sacl( obj->sd, &present );
599 if (!(replaced_sacl = replace_security_labels( old_sacl, sacl ))) return 0;
600 new_sd.control |= SE_SACL_PRESENT;
601 new_sd.sacl_len = replaced_sacl->size;
602 sacl = replaced_sacl;
604 else
606 if (obj->sd) sacl = sd_get_sacl( obj->sd, &present );
608 if (obj->sd && present)
610 new_sd.control |= SE_SACL_PRESENT;
611 new_sd.sacl_len = obj->sd->sacl_len;
613 else
614 new_sd.sacl_len = 0;
617 dacl = sd_get_dacl( sd, &present );
618 if (set_info & DACL_SECURITY_INFORMATION && present)
620 new_sd.control |= SE_DACL_PRESENT;
621 new_sd.dacl_len = sd->dacl_len;
623 else
625 if (obj->sd) dacl = sd_get_dacl( obj->sd, &present );
627 if (obj->sd && present)
629 new_sd.control |= SE_DACL_PRESENT;
630 new_sd.dacl_len = obj->sd->dacl_len;
632 else if (token)
634 dacl = token_get_default_dacl( token );
635 new_sd.control |= SE_DACL_PRESENT;
636 new_sd.dacl_len = dacl->size;
638 else new_sd.dacl_len = 0;
641 ptr = mem_alloc( sizeof(new_sd) + new_sd.owner_len + new_sd.group_len +
642 new_sd.sacl_len + new_sd.dacl_len );
643 if (!ptr)
645 free( replaced_sacl );
646 return 0;
648 new_sd_ptr = (struct security_descriptor*)ptr;
650 memcpy( ptr, &new_sd, sizeof(new_sd) );
651 ptr += sizeof(new_sd);
652 memcpy( ptr, owner, new_sd.owner_len );
653 ptr += new_sd.owner_len;
654 memcpy( ptr, group, new_sd.group_len );
655 ptr += new_sd.group_len;
656 memcpy( ptr, sacl, new_sd.sacl_len );
657 ptr += new_sd.sacl_len;
658 memcpy( ptr, dacl, new_sd.dacl_len );
660 free( replaced_sacl );
661 free( obj->sd );
662 obj->sd = new_sd_ptr;
663 return 1;
666 /** Set the security descriptor using the current primary token for defaults. */
667 int default_set_sd( struct object *obj, const struct security_descriptor *sd,
668 unsigned int set_info )
670 return set_sd_defaults_from_token( obj, sd, set_info, current->process->token );
673 WCHAR *no_get_full_name( struct object *obj, data_size_t *ret_len )
675 return NULL;
678 struct object *no_lookup_name( struct object *obj, struct unicode_str *name,
679 unsigned int attr, struct object *root )
681 if (!name) set_error( STATUS_OBJECT_TYPE_MISMATCH );
682 return NULL;
685 int no_link_name( struct object *obj, struct object_name *name, struct object *parent )
687 set_error( STATUS_OBJECT_TYPE_MISMATCH );
688 return 0;
691 void default_unlink_name( struct object *obj, struct object_name *name )
693 list_remove( &name->entry );
696 struct object *no_open_file( struct object *obj, unsigned int access, unsigned int sharing,
697 unsigned int options )
699 set_error( STATUS_OBJECT_TYPE_MISMATCH );
700 return NULL;
703 int no_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
705 return 1; /* ok to close */
708 void no_destroy( struct object *obj )