webservices: Implement WsReadEndAttribute.
[wine.git] / server / object.c
blob1ec547a58b3809b33d4c52c6fb9090c6bfba6541
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"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <limits.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <stdarg.h>
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "winternl.h"
36 #include "file.h"
37 #include "process.h"
38 #include "thread.h"
39 #include "unicode.h"
40 #include "security.h"
43 struct object_name
45 struct list entry; /* entry in the hash list */
46 struct object *obj; /* object owning this name */
47 struct object *parent; /* parent object */
48 data_size_t len; /* name length in bytes */
49 WCHAR name[1];
52 struct namespace
54 unsigned int hash_size; /* size of hash table */
55 struct list names[1]; /* array of hash entry lists */
59 #ifdef DEBUG_OBJECTS
60 static struct list object_list = LIST_INIT(object_list);
61 static struct list static_object_list = LIST_INIT(static_object_list);
63 void dump_objects(void)
65 struct list *p;
67 LIST_FOR_EACH( p, &static_object_list )
69 struct object *ptr = LIST_ENTRY( p, struct object, obj_list );
70 fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
71 dump_object_name( ptr );
72 ptr->ops->dump( ptr, 1 );
74 LIST_FOR_EACH( p, &object_list )
76 struct object *ptr = LIST_ENTRY( p, struct object, obj_list );
77 fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
78 dump_object_name( ptr );
79 ptr->ops->dump( ptr, 1 );
83 void close_objects(void)
85 struct list *ptr;
87 /* release the static objects */
88 while ((ptr = list_head( &static_object_list )))
90 struct object *obj = LIST_ENTRY( ptr, struct object, obj_list );
91 /* move it back to the standard list before freeing */
92 list_remove( &obj->obj_list );
93 list_add_head( &object_list, &obj->obj_list );
94 release_object( obj );
97 dump_objects(); /* dump any remaining objects */
100 #endif /* DEBUG_OBJECTS */
102 /*****************************************************************/
104 /* malloc replacement */
105 void *mem_alloc( size_t size )
107 void *ptr = malloc( size );
108 if (ptr) memset( ptr, 0x55, size );
109 else set_error( STATUS_NO_MEMORY );
110 return ptr;
113 /* duplicate a block of memory */
114 void *memdup( const void *data, size_t len )
116 void *ptr = malloc( len );
117 if (ptr) memcpy( ptr, data, len );
118 else set_error( STATUS_NO_MEMORY );
119 return ptr;
123 /*****************************************************************/
125 static int get_name_hash( const struct namespace *namespace, const WCHAR *name, data_size_t len )
127 WCHAR hash = 0;
128 len /= sizeof(WCHAR);
129 while (len--) hash ^= tolowerW(*name++);
130 return hash % namespace->hash_size;
133 /* allocate a name for an object */
134 static struct object_name *alloc_name( const struct unicode_str *name )
136 struct object_name *ptr;
138 if ((ptr = mem_alloc( sizeof(*ptr) + name->len - sizeof(ptr->name) )))
140 ptr->len = name->len;
141 ptr->parent = NULL;
142 memcpy( ptr->name, name->str, name->len );
144 return ptr;
147 /* free the name of an object */
148 static void free_name( struct object *obj )
150 struct object_name *ptr = obj->name;
151 list_remove( &ptr->entry );
152 if (ptr->parent) release_object( ptr->parent );
153 free( ptr );
156 /* set the name of an existing object */
157 static void set_object_name( struct namespace *namespace,
158 struct object *obj, struct object_name *ptr )
160 int hash = get_name_hash( namespace, ptr->name, ptr->len );
162 list_add_head( &namespace->names[hash], &ptr->entry );
163 ptr->obj = obj;
164 obj->name = ptr;
167 /* get the name of an existing object */
168 const WCHAR *get_object_name( struct object *obj, data_size_t *len )
170 struct object_name *ptr = obj->name;
171 if (!ptr) return NULL;
172 *len = ptr->len;
173 return ptr->name;
176 /* get the full path name of an existing object */
177 WCHAR *get_object_full_name( struct object *obj, data_size_t *ret_len )
179 static const WCHAR backslash = '\\';
180 struct object *ptr = obj;
181 data_size_t len = 0;
182 char *ret;
184 while (ptr && ptr->name)
186 struct object_name *name = ptr->name;
187 len += name->len + sizeof(WCHAR);
188 ptr = name->parent;
190 if (!len) return NULL;
191 if (!(ret = malloc( len ))) return NULL;
193 *ret_len = len;
194 while (obj && obj->name)
196 struct object_name *name = obj->name;
197 memcpy( ret + len - name->len, name->name, name->len );
198 len -= name->len + sizeof(WCHAR);
199 memcpy( ret + len, &backslash, sizeof(WCHAR) );
200 obj = name->parent;
202 return (WCHAR *)ret;
205 /* allocate and initialize an object */
206 void *alloc_object( const struct object_ops *ops )
208 struct object *obj = mem_alloc( ops->size );
209 if (obj)
211 obj->refcount = 1;
212 obj->handle_count = 0;
213 obj->ops = ops;
214 obj->name = NULL;
215 obj->sd = NULL;
216 list_init( &obj->wait_queue );
217 #ifdef DEBUG_OBJECTS
218 list_add_head( &object_list, &obj->obj_list );
219 #endif
220 return obj;
222 return NULL;
225 void *create_object( struct namespace *namespace, const struct object_ops *ops,
226 const struct unicode_str *name, struct object *parent )
228 struct object *obj;
229 struct object_name *name_ptr;
231 if (!(name_ptr = alloc_name( name ))) return NULL;
232 if ((obj = alloc_object( ops )))
234 set_object_name( namespace, obj, name_ptr );
235 if (parent) name_ptr->parent = grab_object( parent );
237 else
238 free( name_ptr );
239 return obj;
242 void *create_named_object( struct namespace *namespace, const struct object_ops *ops,
243 const struct unicode_str *name, unsigned int attributes )
245 struct object *obj;
247 if (!name || !name->len)
249 if ((obj = alloc_object( ops ))) clear_error();
250 return obj;
253 if ((obj = find_object( namespace, name, attributes )))
255 if (attributes & OBJ_OPENIF && obj->ops == ops)
256 set_error( STATUS_OBJECT_NAME_EXISTS );
257 else
259 release_object( obj );
260 obj = NULL;
261 if (attributes & OBJ_OPENIF)
262 set_error( STATUS_OBJECT_TYPE_MISMATCH );
263 else
264 set_error( STATUS_OBJECT_NAME_COLLISION );
266 return obj;
268 if ((obj = create_object( namespace, ops, name, NULL ))) clear_error();
269 return obj;
272 /* recursive helper for dump_object_name */
273 static void dump_name( struct object *obj )
275 struct object_name *name = obj->name;
277 if (!name) return;
278 if (name->parent) dump_name( name->parent );
279 fputs( "\\\\", stderr );
280 dump_strW( name->name, name->len / sizeof(WCHAR), stderr, "[]" );
283 /* dump the name of an object to stderr */
284 void dump_object_name( struct object *obj )
286 if (!obj->name) return;
287 fputc( '[', stderr );
288 dump_name( obj );
289 fputs( "] ", stderr );
292 /* unlink a named object from its namespace, without freeing the object itself */
293 void unlink_named_object( struct object *obj )
295 if (obj->name) free_name( obj );
296 obj->name = NULL;
299 /* mark an object as being stored statically, i.e. only released at shutdown */
300 void make_object_static( struct object *obj )
302 #ifdef DEBUG_OBJECTS
303 list_remove( &obj->obj_list );
304 list_add_head( &static_object_list, &obj->obj_list );
305 #endif
308 /* grab an object (i.e. increment its refcount) and return the object */
309 struct object *grab_object( void *ptr )
311 struct object *obj = (struct object *)ptr;
312 assert( obj->refcount < INT_MAX );
313 obj->refcount++;
314 return obj;
317 /* release an object (i.e. decrement its refcount) */
318 void release_object( void *ptr )
320 struct object *obj = (struct object *)ptr;
321 assert( obj->refcount );
322 if (!--obj->refcount)
324 assert( !obj->handle_count );
325 /* if the refcount is 0, nobody can be in the wait queue */
326 assert( list_empty( &obj->wait_queue ));
327 obj->ops->destroy( obj );
328 if (obj->name) free_name( obj );
329 free( obj->sd );
330 #ifdef DEBUG_OBJECTS
331 list_remove( &obj->obj_list );
332 memset( obj, 0xaa, obj->ops->size );
333 #endif
334 free( obj );
338 /* find an object by its name; the refcount is incremented */
339 struct object *find_object( const struct namespace *namespace, const struct unicode_str *name,
340 unsigned int attributes )
342 const struct list *list;
343 struct list *p;
345 if (!name || !name->len) return NULL;
347 list = &namespace->names[ get_name_hash( namespace, name->str, name->len ) ];
348 LIST_FOR_EACH( p, list )
350 const struct object_name *ptr = LIST_ENTRY( p, struct object_name, entry );
351 if (ptr->len != name->len) continue;
352 if (attributes & OBJ_CASE_INSENSITIVE)
354 if (!strncmpiW( ptr->name, name->str, name->len/sizeof(WCHAR) ))
355 return grab_object( ptr->obj );
357 else
359 if (!memcmp( ptr->name, name->str, name->len ))
360 return grab_object( ptr->obj );
363 return NULL;
366 /* find an object by its index; the refcount is incremented */
367 struct object *find_object_index( const struct namespace *namespace, unsigned int index )
369 unsigned int i;
371 /* FIXME: not efficient at all */
372 for (i = 0; i < namespace->hash_size; i++)
374 const struct object_name *ptr;
375 LIST_FOR_EACH_ENTRY( ptr, &namespace->names[i], const struct object_name, entry )
377 if (!index--) return grab_object( ptr->obj );
380 set_error( STATUS_NO_MORE_ENTRIES );
381 return NULL;
384 /* allocate a namespace */
385 struct namespace *create_namespace( unsigned int hash_size )
387 struct namespace *namespace;
388 unsigned int i;
390 namespace = mem_alloc( sizeof(*namespace) + (hash_size - 1) * sizeof(namespace->names[0]) );
391 if (namespace)
393 namespace->hash_size = hash_size;
394 for (i = 0; i < hash_size; i++) list_init( &namespace->names[i] );
396 return namespace;
399 /* functions for unimplemented/default object operations */
401 struct object_type *no_get_type( struct object *obj )
403 return NULL;
406 int no_add_queue( struct object *obj, struct wait_queue_entry *entry )
408 set_error( STATUS_OBJECT_TYPE_MISMATCH );
409 return 0;
412 void no_satisfied( struct object *obj, struct wait_queue_entry *entry )
416 int no_signal( struct object *obj, unsigned int access )
418 set_error( STATUS_OBJECT_TYPE_MISMATCH );
419 return 0;
422 struct fd *no_get_fd( struct object *obj )
424 set_error( STATUS_OBJECT_TYPE_MISMATCH );
425 return NULL;
428 unsigned int no_map_access( struct object *obj, unsigned int access )
430 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
431 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE;
432 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
433 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL;
434 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
437 struct security_descriptor *default_get_sd( struct object *obj )
439 return obj->sd;
442 int set_sd_defaults_from_token( struct object *obj, const struct security_descriptor *sd,
443 unsigned int set_info, struct token *token )
445 struct security_descriptor new_sd, *new_sd_ptr;
446 int present;
447 const SID *owner = NULL, *group = NULL;
448 const ACL *sacl, *dacl;
449 char *ptr;
451 if (!set_info) return 1;
453 new_sd.control = sd->control & ~SE_SELF_RELATIVE;
455 if (set_info & OWNER_SECURITY_INFORMATION && sd->owner_len)
457 owner = sd_get_owner( sd );
458 new_sd.owner_len = sd->owner_len;
460 else if (obj->sd && obj->sd->owner_len)
462 owner = sd_get_owner( obj->sd );
463 new_sd.owner_len = obj->sd->owner_len;
465 else if (token)
467 owner = token_get_user( token );
468 new_sd.owner_len = security_sid_len( owner );
470 else new_sd.owner_len = 0;
472 if (set_info & GROUP_SECURITY_INFORMATION && sd->group_len)
474 group = sd_get_group( sd );
475 new_sd.group_len = sd->group_len;
477 else if (obj->sd && obj->sd->group_len)
479 group = sd_get_group( obj->sd );
480 new_sd.group_len = obj->sd->group_len;
482 else if (token)
484 group = token_get_primary_group( token );
485 new_sd.group_len = security_sid_len( group );
487 else new_sd.group_len = 0;
489 new_sd.control |= SE_SACL_PRESENT;
490 sacl = sd_get_sacl( sd, &present );
491 if (set_info & SACL_SECURITY_INFORMATION && present)
492 new_sd.sacl_len = sd->sacl_len;
493 else
495 if (obj->sd) sacl = sd_get_sacl( obj->sd, &present );
497 if (obj->sd && present)
498 new_sd.sacl_len = obj->sd->sacl_len;
499 else
500 new_sd.sacl_len = 0;
503 new_sd.control |= SE_DACL_PRESENT;
504 dacl = sd_get_dacl( sd, &present );
505 if (set_info & DACL_SECURITY_INFORMATION && present)
506 new_sd.dacl_len = sd->dacl_len;
507 else
509 if (obj->sd) dacl = sd_get_dacl( obj->sd, &present );
511 if (obj->sd && present)
512 new_sd.dacl_len = obj->sd->dacl_len;
513 else if (token)
515 dacl = token_get_default_dacl( token );
516 new_sd.dacl_len = dacl->AclSize;
518 else new_sd.dacl_len = 0;
521 ptr = mem_alloc( sizeof(new_sd) + new_sd.owner_len + new_sd.group_len +
522 new_sd.sacl_len + new_sd.dacl_len );
523 if (!ptr) return 0;
524 new_sd_ptr = (struct security_descriptor*)ptr;
526 memcpy( ptr, &new_sd, sizeof(new_sd) );
527 ptr += sizeof(new_sd);
528 memcpy( ptr, owner, new_sd.owner_len );
529 ptr += new_sd.owner_len;
530 memcpy( ptr, group, new_sd.group_len );
531 ptr += new_sd.group_len;
532 memcpy( ptr, sacl, new_sd.sacl_len );
533 ptr += new_sd.sacl_len;
534 memcpy( ptr, dacl, new_sd.dacl_len );
536 free( obj->sd );
537 obj->sd = new_sd_ptr;
538 return 1;
541 /** Set the security descriptor using the current primary token for defaults. */
542 int default_set_sd( struct object *obj, const struct security_descriptor *sd,
543 unsigned int set_info )
545 return set_sd_defaults_from_token( obj, sd, set_info, current->process->token );
548 struct object *no_lookup_name( struct object *obj, struct unicode_str *name,
549 unsigned int attr )
551 return NULL;
554 struct object *no_open_file( struct object *obj, unsigned int access, unsigned int sharing,
555 unsigned int options )
557 set_error( STATUS_OBJECT_TYPE_MISMATCH );
558 return NULL;
561 int no_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
563 return 1; /* ok to close */
566 void no_destroy( struct object *obj )