Release 1.5.29.
[wine] / server / object.c
1 /*
2  * Server-side objects
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 <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <stdarg.h>
31
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "winternl.h"
35
36 #include "file.h"
37 #include "process.h"
38 #include "thread.h"
39 #include "unicode.h"
40 #include "security.h"
41
42
43 struct object_name
44 {
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];
50 };
51
52 struct namespace
53 {
54     unsigned int        hash_size;       /* size of hash table */
55     struct list         names[1];        /* array of hash entry lists */
56 };
57
58
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);
62
63 void dump_objects(void)
64 {
65     struct list *p;
66
67     LIST_FOR_EACH( p, &static_object_list )
68     {
69         struct object *ptr = LIST_ENTRY( p, struct object, obj_list );
70         fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
71         ptr->ops->dump( ptr, 1 );
72     }
73     LIST_FOR_EACH( p, &object_list )
74     {
75         struct object *ptr = LIST_ENTRY( p, struct object, obj_list );
76         fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
77         ptr->ops->dump( ptr, 1 );
78     }
79 }
80
81 void close_objects(void)
82 {
83     struct list *ptr;
84
85     /* release the static objects */
86     while ((ptr = list_head( &static_object_list )))
87     {
88         struct object *obj = LIST_ENTRY( ptr, struct object, obj_list );
89         /* move it back to the standard list before freeing */
90         list_remove( &obj->obj_list );
91         list_add_head( &object_list, &obj->obj_list );
92         release_object( obj );
93     }
94
95     dump_objects();  /* dump any remaining objects */
96 }
97
98 #endif  /* DEBUG_OBJECTS */
99
100 /*****************************************************************/
101
102 /* malloc replacement */
103 void *mem_alloc( size_t size )
104 {
105     void *ptr = malloc( size );
106     if (ptr) memset( ptr, 0x55, size );
107     else set_error( STATUS_NO_MEMORY );
108     return ptr;
109 }
110
111 /* duplicate a block of memory */
112 void *memdup( const void *data, size_t len )
113 {
114     void *ptr = malloc( len );
115     if (ptr) memcpy( ptr, data, len );
116     else set_error( STATUS_NO_MEMORY );
117     return ptr;
118 }
119
120
121 /*****************************************************************/
122
123 static int get_name_hash( const struct namespace *namespace, const WCHAR *name, data_size_t len )
124 {
125     WCHAR hash = 0;
126     len /= sizeof(WCHAR);
127     while (len--) hash ^= tolowerW(*name++);
128     return hash % namespace->hash_size;
129 }
130
131 /* allocate a name for an object */
132 static struct object_name *alloc_name( const struct unicode_str *name )
133 {
134     struct object_name *ptr;
135
136     if ((ptr = mem_alloc( sizeof(*ptr) + name->len - sizeof(ptr->name) )))
137     {
138         ptr->len = name->len;
139         ptr->parent = NULL;
140         memcpy( ptr->name, name->str, name->len );
141     }
142     return ptr;
143 }
144
145 /* free the name of an object */
146 static void free_name( struct object *obj )
147 {
148     struct object_name *ptr = obj->name;
149     list_remove( &ptr->entry );
150     if (ptr->parent) release_object( ptr->parent );
151     free( ptr );
152 }
153
154 /* set the name of an existing object */
155 static void set_object_name( struct namespace *namespace,
156                              struct object *obj, struct object_name *ptr )
157 {
158     int hash = get_name_hash( namespace, ptr->name, ptr->len );
159
160     list_add_head( &namespace->names[hash], &ptr->entry );
161     ptr->obj = obj;
162     obj->name = ptr;
163 }
164
165 /* get the name of an existing object */
166 const WCHAR *get_object_name( struct object *obj, data_size_t *len )
167 {
168     struct object_name *ptr = obj->name;
169     if (!ptr) return NULL;
170     *len = ptr->len;
171     return ptr->name;
172 }
173
174 /* get the full path name of an existing object */
175 WCHAR *get_object_full_name( struct object *obj, data_size_t *ret_len )
176 {
177     static const WCHAR backslash = '\\';
178     struct object *ptr = obj;
179     data_size_t len = 0;
180     char *ret;
181
182     while (ptr && ptr->name)
183     {
184         struct object_name *name = ptr->name;
185         len += name->len + sizeof(WCHAR);
186         ptr = name->parent;
187     }
188     if (!len) return NULL;
189     if (!(ret = malloc( len ))) return NULL;
190
191     *ret_len = len;
192     while (obj && obj->name)
193     {
194         struct object_name *name = obj->name;
195         memcpy( ret + len - name->len, name->name, name->len );
196         len -= name->len + sizeof(WCHAR);
197         memcpy( ret + len, &backslash, sizeof(WCHAR) );
198         obj = name->parent;
199     }
200     return (WCHAR *)ret;
201 }
202
203 /* allocate and initialize an object */
204 void *alloc_object( const struct object_ops *ops )
205 {
206     struct object *obj = mem_alloc( ops->size );
207     if (obj)
208     {
209         obj->refcount = 1;
210         obj->ops      = ops;
211         obj->name     = NULL;
212         obj->sd       = NULL;
213         list_init( &obj->wait_queue );
214 #ifdef DEBUG_OBJECTS
215         list_add_head( &object_list, &obj->obj_list );
216 #endif
217         return obj;
218     }
219     return NULL;
220 }
221
222 void *create_object( struct namespace *namespace, const struct object_ops *ops,
223                      const struct unicode_str *name, struct object *parent )
224 {
225     struct object *obj;
226     struct object_name *name_ptr;
227
228     if (!(name_ptr = alloc_name( name ))) return NULL;
229     if ((obj = alloc_object( ops )))
230     {
231         set_object_name( namespace, obj, name_ptr );
232         if (parent) name_ptr->parent = grab_object( parent );
233     }
234     else
235         free( name_ptr );
236     return obj;
237 }
238
239 void *create_named_object( struct namespace *namespace, const struct object_ops *ops,
240                            const struct unicode_str *name, unsigned int attributes )
241 {
242     struct object *obj;
243
244     if (!name || !name->len) return alloc_object( ops );
245
246     if ((obj = find_object( namespace, name, attributes )))
247     {
248         if (attributes & OBJ_OPENIF && obj->ops == ops)
249             set_error( STATUS_OBJECT_NAME_EXISTS );
250         else
251         {
252             release_object( obj );
253             obj = NULL;
254             if (attributes & OBJ_OPENIF)
255                 set_error( STATUS_OBJECT_TYPE_MISMATCH );
256             else
257                 set_error( STATUS_OBJECT_NAME_COLLISION );
258         }
259         return obj;
260     }
261     if ((obj = create_object( namespace, ops, name, NULL ))) clear_error();
262     return obj;
263 }
264
265 /* dump the name of an object to stderr */
266 void dump_object_name( struct object *obj )
267 {
268     if (!obj->name) fprintf( stderr, "name=\"\"" );
269     else
270     {
271         fprintf( stderr, "name=L\"" );
272         dump_strW( obj->name->name, obj->name->len/sizeof(WCHAR), stderr, "\"\"" );
273         fputc( '\"', stderr );
274     }
275 }
276
277 /* unlink a named object from its namespace, without freeing the object itself */
278 void unlink_named_object( struct object *obj )
279 {
280     if (obj->name) free_name( obj );
281     obj->name = NULL;
282 }
283
284 /* mark an object as being stored statically, i.e. only released at shutdown */
285 void make_object_static( struct object *obj )
286 {
287 #ifdef DEBUG_OBJECTS
288     list_remove( &obj->obj_list );
289     list_add_head( &static_object_list, &obj->obj_list );
290 #endif
291 }
292
293 /* grab an object (i.e. increment its refcount) and return the object */
294 struct object *grab_object( void *ptr )
295 {
296     struct object *obj = (struct object *)ptr;
297     assert( obj->refcount < INT_MAX );
298     obj->refcount++;
299     return obj;
300 }
301
302 /* release an object (i.e. decrement its refcount) */
303 void release_object( void *ptr )
304 {
305     struct object *obj = (struct object *)ptr;
306     assert( obj->refcount );
307     if (!--obj->refcount)
308     {
309         /* if the refcount is 0, nobody can be in the wait queue */
310         assert( list_empty( &obj->wait_queue ));
311         obj->ops->destroy( obj );
312         if (obj->name) free_name( obj );
313         free( obj->sd );
314 #ifdef DEBUG_OBJECTS
315         list_remove( &obj->obj_list );
316         memset( obj, 0xaa, obj->ops->size );
317 #endif
318         free( obj );
319     }
320 }
321
322 /* find an object by its name; the refcount is incremented */
323 struct object *find_object( const struct namespace *namespace, const struct unicode_str *name,
324                             unsigned int attributes )
325 {
326     const struct list *list;
327     struct list *p;
328
329     if (!name || !name->len) return NULL;
330
331     list = &namespace->names[ get_name_hash( namespace, name->str, name->len ) ];
332     LIST_FOR_EACH( p, list )
333     {
334         const struct object_name *ptr = LIST_ENTRY( p, struct object_name, entry );
335         if (ptr->len != name->len) continue;
336         if (attributes & OBJ_CASE_INSENSITIVE)
337         {
338             if (!strncmpiW( ptr->name, name->str, name->len/sizeof(WCHAR) ))
339                 return grab_object( ptr->obj );
340         }
341         else
342         {
343             if (!memcmp( ptr->name, name->str, name->len ))
344                 return grab_object( ptr->obj );
345         }
346     }
347     return NULL;
348 }
349
350 /* find an object by its index; the refcount is incremented */
351 struct object *find_object_index( const struct namespace *namespace, unsigned int index )
352 {
353     unsigned int i;
354
355     /* FIXME: not efficient at all */
356     for (i = 0; i < namespace->hash_size; i++)
357     {
358         const struct object_name *ptr;
359         LIST_FOR_EACH_ENTRY( ptr, &namespace->names[i], const struct object_name, entry )
360         {
361             if (!index--) return grab_object( ptr->obj );
362         }
363     }
364     set_error( STATUS_NO_MORE_ENTRIES );
365     return NULL;
366 }
367
368 /* allocate a namespace */
369 struct namespace *create_namespace( unsigned int hash_size )
370 {
371     struct namespace *namespace;
372     unsigned int i;
373
374     namespace = mem_alloc( sizeof(*namespace) + (hash_size - 1) * sizeof(namespace->names[0]) );
375     if (namespace)
376     {
377         namespace->hash_size      = hash_size;
378         for (i = 0; i < hash_size; i++) list_init( &namespace->names[i] );
379     }
380     return namespace;
381 }
382
383 /* functions for unimplemented/default object operations */
384
385 struct object_type *no_get_type( struct object *obj )
386 {
387     return NULL;
388 }
389
390 int no_add_queue( struct object *obj, struct wait_queue_entry *entry )
391 {
392     set_error( STATUS_OBJECT_TYPE_MISMATCH );
393     return 0;
394 }
395
396 int no_satisfied( struct object *obj, struct thread *thread )
397 {
398     return 0;  /* not abandoned */
399 }
400
401 int no_signal( struct object *obj, unsigned int access )
402 {
403     set_error( STATUS_OBJECT_TYPE_MISMATCH );
404     return 0;
405 }
406
407 struct fd *no_get_fd( struct object *obj )
408 {
409     set_error( STATUS_OBJECT_TYPE_MISMATCH );
410     return NULL;
411 }
412
413 unsigned int no_map_access( struct object *obj, unsigned int access )
414 {
415     if (access & GENERIC_READ)    access |= STANDARD_RIGHTS_READ;
416     if (access & GENERIC_WRITE)   access |= STANDARD_RIGHTS_WRITE;
417     if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
418     if (access & GENERIC_ALL)     access |= STANDARD_RIGHTS_ALL;
419     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
420 }
421
422 struct security_descriptor *default_get_sd( struct object *obj )
423 {
424     return obj->sd;
425 }
426
427 int default_set_sd( struct object *obj, const struct security_descriptor *sd,
428                     unsigned int set_info )
429 {
430     struct security_descriptor new_sd, *new_sd_ptr;
431     int present;
432     const SID *owner, *group;
433     const ACL *sacl, *dacl;
434     char *ptr;
435
436     if (!set_info) return 1;
437
438     new_sd.control = sd->control & ~SE_SELF_RELATIVE;
439
440     owner = sd_get_owner( sd );
441     if (set_info & OWNER_SECURITY_INFORMATION && owner)
442         new_sd.owner_len = sd->owner_len;
443     else
444     {
445         owner = token_get_user( current->process->token );
446         new_sd.owner_len = security_sid_len( owner );
447         new_sd.control |= SE_OWNER_DEFAULTED;
448     }
449
450     group = sd_get_group( sd );
451     if (set_info & GROUP_SECURITY_INFORMATION && group)
452         new_sd.group_len = sd->group_len;
453     else
454     {
455         group = token_get_primary_group( current->process->token );
456         new_sd.group_len = security_sid_len( group );
457         new_sd.control |= SE_GROUP_DEFAULTED;
458     }
459
460     new_sd.control |= SE_SACL_PRESENT;
461     sacl = sd_get_sacl( sd, &present );
462     if (set_info & SACL_SECURITY_INFORMATION && present)
463         new_sd.sacl_len = sd->sacl_len;
464     else
465     {
466         if (obj->sd) sacl = sd_get_sacl( obj->sd, &present );
467
468         if (obj->sd && present)
469             new_sd.sacl_len = obj->sd->sacl_len;
470         else
471         {
472             new_sd.sacl_len = 0;
473             new_sd.control |= SE_SACL_DEFAULTED;
474         }
475     }
476
477     new_sd.control |= SE_DACL_PRESENT;
478     dacl = sd_get_dacl( sd, &present );
479     if (set_info & DACL_SECURITY_INFORMATION && present)
480         new_sd.dacl_len = sd->dacl_len;
481     else
482     {
483         if (obj->sd) dacl = sd_get_dacl( obj->sd, &present );
484
485         if (obj->sd && present)
486             new_sd.dacl_len = obj->sd->dacl_len;
487         else
488         {
489             dacl = token_get_default_dacl( current->process->token );
490             new_sd.dacl_len = dacl->AclSize;
491             new_sd.control |= SE_DACL_DEFAULTED;
492         }
493     }
494
495     ptr = mem_alloc( sizeof(new_sd) + new_sd.owner_len + new_sd.group_len +
496                      new_sd.sacl_len + new_sd.dacl_len );
497     if (!ptr) return 0;
498     new_sd_ptr = (struct security_descriptor*)ptr;
499
500     memcpy( ptr, &new_sd, sizeof(new_sd) );
501     ptr += sizeof(new_sd);
502     memcpy( ptr, owner, new_sd.owner_len );
503     ptr += new_sd.owner_len;
504     memcpy( ptr, group, new_sd.group_len );
505     ptr += new_sd.group_len;
506     memcpy( ptr, sacl, new_sd.sacl_len );
507     ptr += new_sd.sacl_len;
508     memcpy( ptr, dacl, new_sd.dacl_len );
509
510     free( obj->sd );
511     obj->sd = new_sd_ptr;
512     return 1;
513 }
514
515 struct object *no_lookup_name( struct object *obj, struct unicode_str *name,
516                                unsigned int attr )
517 {
518     return NULL;
519 }
520
521 struct object *no_open_file( struct object *obj, unsigned int access, unsigned int sharing,
522                              unsigned int options )
523 {
524     set_error( STATUS_OBJECT_TYPE_MISMATCH );
525     return NULL;
526 }
527
528 int no_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
529 {
530     return 1;  /* ok to close */
531 }
532
533 void no_destroy( struct object *obj )
534 {
535 }