Release 1.5.29.
[wine] / server / directory.c
1 /*
2  * Server-side directory object management
3  *
4  * Copyright (C) 2005 Vitaliy Margolen
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
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <sys/types.h>
30
31 #include "ntstatus.h"
32 #define WIN32_NO_STATUS
33 #include "winternl.h"
34 #include "ddk/wdm.h"
35
36 #include "handle.h"
37 #include "request.h"
38 #include "process.h"
39 #include "file.h"
40 #include "unicode.h"
41
42 #define HASH_SIZE 7  /* default hash size */
43
44 struct object_type
45 {
46     struct object     obj;        /* object header */
47 };
48
49 static void object_type_dump( struct object *obj, int verbose );
50 static struct object_type *object_type_get_type( struct object *obj );
51
52 static const struct object_ops object_type_ops =
53 {
54     sizeof(struct object_type),   /* size */
55     object_type_dump,             /* dump */
56     object_type_get_type,         /* get_type */
57     no_add_queue,                 /* add_queue */
58     NULL,                         /* remove_queue */
59     NULL,                         /* signaled */
60     NULL,                         /* satisfied */
61     no_signal,                    /* signal */
62     no_get_fd,                    /* get_fd */
63     no_map_access,                /* map_access */
64     default_get_sd,               /* get_sd */
65     default_set_sd,               /* set_sd */
66     no_lookup_name,               /* lookup_name */
67     no_open_file,                 /* open_file */
68     no_close_handle,              /* close_handle */
69     no_destroy                    /* destroy */
70 };
71
72
73 struct directory
74 {
75     struct object     obj;        /* object header */
76     struct namespace *entries;    /* directory's name space */
77 };
78
79 static void directory_dump( struct object *obj, int verbose );
80 static struct object_type *directory_get_type( struct object *obj );
81 static struct object *directory_lookup_name( struct object *obj, struct unicode_str *name,
82                                              unsigned int attr );
83 static void directory_destroy( struct object *obj );
84
85 static const struct object_ops directory_ops =
86 {
87     sizeof(struct directory),     /* size */
88     directory_dump,               /* dump */
89     directory_get_type,           /* get_type */
90     no_add_queue,                 /* add_queue */
91     NULL,                         /* remove_queue */
92     NULL,                         /* signaled */
93     NULL,                         /* satisfied */
94     no_signal,                    /* signal */
95     no_get_fd,                    /* get_fd */
96     default_fd_map_access,        /* map_access */
97     default_get_sd,               /* get_sd */
98     default_set_sd,               /* set_sd */
99     directory_lookup_name,        /* lookup_name */
100     no_open_file,                 /* open_file */
101     no_close_handle,              /* close_handle */
102     directory_destroy             /* destroy */
103 };
104
105 static struct directory *root_directory;
106 static struct directory *dir_objtype;
107
108
109 static void object_type_dump( struct object *obj, int verbose )
110 {
111     assert( obj->ops == &object_type_ops );
112
113     fputs( "Object type ", stderr );
114     dump_object_name( obj );
115     fputc( '\n', stderr );
116 }
117
118 static struct object_type *object_type_get_type( struct object *obj )
119 {
120     static const WCHAR name[] = {'O','b','j','e','c','t','T','y','p','e'};
121     static const struct unicode_str str = { name, sizeof(name) };
122     return get_object_type( &str );
123 }
124
125 static void directory_dump( struct object *obj, int verbose )
126 {
127     assert( obj->ops == &directory_ops );
128
129     fputs( "Directory ", stderr );
130     dump_object_name( obj );
131     fputc( '\n', stderr );
132 }
133
134 static struct object_type *directory_get_type( struct object *obj )
135 {
136     static const WCHAR name[] = {'D','i','r','e','c','t','o','r','y'};
137     static const struct unicode_str str = { name, sizeof(name) };
138     return get_object_type( &str );
139 }
140
141 static struct object *directory_lookup_name( struct object *obj, struct unicode_str *name,
142                                              unsigned int attr )
143 {
144     struct directory *dir = (struct directory *)obj;
145     struct object *found;
146     struct unicode_str tmp;
147     const WCHAR *p;
148
149     assert( obj->ops == &directory_ops );
150
151     if (!(p = memchrW( name->str, '\\', name->len / sizeof(WCHAR) )))
152         /* Last element in the path name */
153         tmp.len = name->len;
154     else
155         tmp.len = (p - name->str) * sizeof(WCHAR);
156
157     tmp.str = name->str;
158     if ((found = find_object( dir->entries, &tmp, attr )))
159     {
160         /* Skip trailing \\ */
161         if (p)
162         {
163             p++;
164             tmp.len += sizeof(WCHAR);
165         }
166         /* Move to the next element*/
167         name->str = p;
168         name->len -= tmp.len;
169         return found;
170     }
171
172     if (name->str)
173     {
174         if (tmp.len == 0) /* Double backslash */
175             set_error( STATUS_OBJECT_NAME_INVALID );
176         else if (p)  /* Path still has backslashes */
177             set_error( STATUS_OBJECT_PATH_NOT_FOUND );
178         else
179             clear_error();
180     }
181     return NULL;
182 }
183
184 static void directory_destroy( struct object *obj )
185 {
186     struct directory *dir = (struct directory *)obj;
187     assert( obj->ops == &directory_ops );
188     free( dir->entries );
189 }
190
191 static struct directory *create_directory( struct directory *root, const struct unicode_str *name,
192                                            unsigned int attr, unsigned int hash_size )
193 {
194     struct directory *dir;
195
196     if ((dir = create_named_object_dir( root, name, attr, &directory_ops )) &&
197         get_error() != STATUS_OBJECT_NAME_EXISTS)
198     {
199         if (!(dir->entries = create_namespace( hash_size )))
200         {
201             release_object( dir );
202             dir = NULL;
203         }
204     }
205     return dir;
206 }
207
208 struct directory *get_directory_obj( struct process *process, obj_handle_t handle, unsigned int access )
209 {
210     return (struct directory *)get_handle_obj( process, handle, access, &directory_ops );
211 }
212
213 /******************************************************************************
214  * Find an object by its name in a given root object
215  *
216  * PARAMS
217  *  root      [I] directory to start search from or NULL to start from \\
218  *  name      [I] object name to search for
219  *  attr      [I] OBJECT_ATTRIBUTES.Attributes
220  *  name_left [O] [optional] leftover name if object is not found
221  *
222  * RETURNS
223  *  NULL:      If params are invalid
224  *  Found:     If object with exact name is found returns that object
225  *             (name_left->len == 0). Object's refcount is incremented
226  *  Not found: The last matched parent. (name_left->len > 0)
227  *             Parent's refcount is incremented.
228  */
229 struct object *find_object_dir( struct directory *root, const struct unicode_str *name,
230                                 unsigned int attr, struct unicode_str *name_left )
231 {
232     struct object *obj, *parent;
233     struct unicode_str name_tmp;
234
235     if (name) name_tmp = *name;
236     else name_tmp.len = 0;
237
238     /* Arguments check:
239      * - Either rootdir or name have to be specified
240      * - If root is specified path shouldn't start with backslash */
241     if (root)
242     {
243         if (name_tmp.len && name_tmp.str[0] == '\\')
244         {
245             set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
246             return NULL;
247         }
248         parent = grab_object( root );
249     }
250     else
251     {
252         if (!name_tmp.len || name_tmp.str[0] != '\\')
253         {
254             set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
255             return NULL;
256         }
257         parent = grab_object( &root_directory->obj );
258         /* skip leading backslash */
259         name_tmp.str++;
260         name_tmp.len -= sizeof(WCHAR);
261     }
262
263     /* Special case for opening RootDirectory */
264     if (!name_tmp.len) goto done;
265
266     while ((obj = parent->ops->lookup_name( parent, &name_tmp, attr )))
267     {
268         /* move to the next element */
269         release_object ( parent );
270         parent = obj;
271     }
272     if (get_error())
273     {
274         release_object( parent );
275         return NULL;
276     }
277
278     done:
279     if (name_left) *name_left = name_tmp;
280     return parent;
281 }
282
283 /* create a named (if name is present) or unnamed object. */
284 void *create_named_object_dir( struct directory *root, const struct unicode_str *name,
285                                unsigned int attributes, const struct object_ops *ops )
286 {
287     struct object *obj, *new_obj = NULL;
288     struct unicode_str new_name;
289
290     if (!name || !name->len) return alloc_object( ops );
291
292     if (!(obj = find_object_dir( root, name, attributes, &new_name ))) return NULL;
293     if (!new_name.len)
294     {
295         if (attributes & OBJ_OPENIF && obj->ops == ops)
296             set_error( STATUS_OBJECT_NAME_EXISTS );
297         else
298         {
299             release_object( obj );
300             obj = NULL;
301             if (attributes & OBJ_OPENIF)
302                 set_error( STATUS_OBJECT_TYPE_MISMATCH );
303             else
304                 set_error( STATUS_OBJECT_NAME_COLLISION );
305         }
306         return obj;
307     }
308
309     /* ATM we can't insert objects into anything else but directories */
310     if (obj->ops != &directory_ops)
311         set_error( STATUS_OBJECT_TYPE_MISMATCH );
312     else
313     {
314         struct directory *dir = (struct directory *)obj;
315         if ((new_obj = create_object( dir->entries, ops, &new_name, &dir->obj )))
316             clear_error();
317     }
318
319     release_object( obj );
320     return new_obj;
321 }
322
323 /* open a new handle to an existing object */
324 void *open_object_dir( struct directory *root, const struct unicode_str *name,
325                        unsigned int attr, const struct object_ops *ops )
326 {
327     struct unicode_str name_left;
328     struct object *obj;
329
330     if ((obj = find_object_dir( root, name, attr, &name_left )))
331     {
332         if (name_left.len) /* not fully parsed */
333             set_error( STATUS_OBJECT_NAME_NOT_FOUND );
334         else if (ops && obj->ops != ops)
335             set_error( STATUS_OBJECT_TYPE_MISMATCH );
336         else
337             return obj;
338
339         release_object( obj );
340     }
341     return NULL;
342 }
343
344 /* retrieve an object type, creating it if needed */
345 struct object_type *get_object_type( const struct unicode_str *name )
346 {
347     struct object_type *type;
348
349     if ((type = open_object_dir( dir_objtype, name, 0, &object_type_ops )))
350         return type;
351
352     if ((type = create_named_object_dir( dir_objtype, name, 0, &object_type_ops )))
353     {
354         grab_object( type );
355         make_object_static( &type->obj );
356         clear_error();
357     }
358     return type;
359 }
360
361 /* Global initialization */
362
363 void init_directories(void)
364 {
365     /* Directories */
366     static const WCHAR dir_globalW[] = {'\\','?','?'};
367     static const WCHAR dir_driverW[] = {'D','r','i','v','e','r'};
368     static const WCHAR dir_deviceW[] = {'D','e','v','i','c','e'};
369     static const WCHAR dir_basenamedW[] = {'\\','B','a','s','e','N','a','m','e','d','O','b','j','e','c','t','s'};
370     static const WCHAR dir_named_pipeW[] = {'\\','D','e','v','i','c','e','\\','N','a','m','e','d','P','i','p','e'};
371     static const WCHAR dir_mailslotW[] = {'\\','D','e','v','i','c','e','\\','M','a','i','l','S','l','o','t'};
372     static const WCHAR dir_objtypeW[] = {'O','b','j','e','c','t','T','y','p','e','s',};
373     static const WCHAR dir_sessionsW[] = {'S','e','s','s','i','o','n','s'};
374     static const WCHAR dir_kernelW[] = {'K','e','r','n','e','l','O','b','j','e','c','t','s'};
375     static const struct unicode_str dir_global_str = {dir_globalW, sizeof(dir_globalW)};
376     static const struct unicode_str dir_driver_str = {dir_driverW, sizeof(dir_driverW)};
377     static const struct unicode_str dir_device_str = {dir_deviceW, sizeof(dir_deviceW)};
378     static const struct unicode_str dir_basenamed_str = {dir_basenamedW, sizeof(dir_basenamedW)};
379     static const struct unicode_str dir_named_pipe_str = {dir_named_pipeW, sizeof(dir_named_pipeW)};
380     static const struct unicode_str dir_mailslot_str = {dir_mailslotW, sizeof(dir_mailslotW)};
381     static const struct unicode_str dir_objtype_str = {dir_objtypeW, sizeof(dir_objtypeW)};
382     static const struct unicode_str dir_sessions_str = {dir_sessionsW, sizeof(dir_sessionsW)};
383     static const struct unicode_str dir_kernel_str = {dir_kernelW, sizeof(dir_kernelW)};
384
385     /* symlinks */
386     static const WCHAR link_dosdevW[] = {'D','o','s','D','e','v','i','c','e','s'};
387     static const WCHAR link_globalW[] = {'G','l','o','b','a','l'};
388     static const WCHAR link_localW[]  = {'L','o','c','a','l'};
389     static const WCHAR link_pipeW[]   = {'P','I','P','E'};
390     static const WCHAR link_mailslotW[] = {'M','A','I','L','S','L','O','T'};
391     static const WCHAR link_0W[]      = {'0'};
392     static const WCHAR link_sessionW[] = {'S','e','s','s','i','o','n'};
393     static const WCHAR link_sessionsW[] = {'\\','S','e','s','s','i','o','n','s'};
394     static const struct unicode_str link_dosdev_str = {link_dosdevW, sizeof(link_dosdevW)};
395     static const struct unicode_str link_global_str = {link_globalW, sizeof(link_globalW)};
396     static const struct unicode_str link_local_str  = {link_localW, sizeof(link_localW)};
397     static const struct unicode_str link_pipe_str   = {link_pipeW, sizeof(link_pipeW)};
398     static const struct unicode_str link_mailslot_str = {link_mailslotW, sizeof(link_mailslotW)};
399     static const struct unicode_str link_0_str      = {link_0W, sizeof(link_0W)};
400     static const struct unicode_str link_session_str = {link_sessionW, sizeof(link_sessionW)};
401     static const struct unicode_str link_sessions_str = {link_sessionsW, sizeof(link_sessionsW)};
402
403     /* devices */
404     static const WCHAR named_pipeW[] = {'N','a','m','e','d','P','i','p','e'};
405     static const WCHAR mailslotW[] = {'M','a','i','l','S','l','o','t'};
406     static const struct unicode_str named_pipe_str = {named_pipeW, sizeof(named_pipeW)};
407     static const struct unicode_str mailslot_str = {mailslotW, sizeof(mailslotW)};
408
409     /* events */
410     static const WCHAR event_low_memW[] = {'L','o','w','M','e','m','o','r','y','C','o','n','d','i','t','i','o','n'};
411     static const WCHAR event_low_pagedW[] = {'L','o','w','P','a','g','e','d','P','o','o','l','C','o','n','d','i','t','i','o','n'};
412     static const WCHAR event_low_nonpgW[] = {'L','o','w','N','o','n','P','a','g','e','d','P','o','o','l','C','o','n','d','i','t','i','o','n'};
413     static const WCHAR event_high_memW[] = {'H','i','g','h','M','e','m','o','r','y','C','o','n','d','i','t','i','o','n'};
414     static const WCHAR event_high_pagedW[] = {'H','i','g','h','P','a','g','e','d','P','o','o','l','C','o','n','d','i','t','i','o','n'};
415     static const WCHAR event_high_nonpgW[] = {'H','i','g','h','N','o','n','P','a','g','e','d','P','o','o','l','C','o','n','d','i','t','i','o','n'};
416     static const struct unicode_str kernel_events[] =
417     {
418         { event_low_memW, sizeof(event_low_memW) },
419         { event_low_pagedW, sizeof(event_low_pagedW) },
420         { event_low_nonpgW, sizeof(event_low_nonpgW) },
421         { event_high_memW, sizeof(event_high_memW) },
422         { event_high_pagedW, sizeof(event_high_pagedW) },
423         { event_high_nonpgW, sizeof(event_high_nonpgW) }
424     };
425
426     struct directory *dir_driver, *dir_device, *dir_global, *dir_basenamed, *dir_sessions, *dir_kernel;
427     struct symlink *link_dosdev, *link_global1, *link_global2, *link_local, *link_pipe, *link_mailslot, *link_0, *link_session;
428     unsigned int i;
429
430     root_directory = create_directory( NULL, NULL, 0, HASH_SIZE );
431     dir_driver     = create_directory( root_directory, &dir_driver_str, 0, HASH_SIZE );
432     dir_device     = create_directory( root_directory, &dir_device_str, 0, HASH_SIZE );
433     dir_objtype    = create_directory( root_directory, &dir_objtype_str, 0, HASH_SIZE );
434     dir_sessions   = create_directory( root_directory, &dir_sessions_str, 0, HASH_SIZE );
435     dir_kernel     = create_directory( root_directory, &dir_kernel_str, 0, HASH_SIZE );
436     make_object_static( &root_directory->obj );
437     make_object_static( &dir_driver->obj );
438     make_object_static( &dir_objtype->obj );
439
440     dir_global     = create_directory( NULL, &dir_global_str, 0, HASH_SIZE );
441     /* use a larger hash table for this one since it can contain a lot of objects */
442     dir_basenamed  = create_directory( NULL, &dir_basenamed_str, 0, 37 );
443
444     /* devices */
445     create_named_pipe_device( dir_device, &named_pipe_str );
446     create_mailslot_device( dir_device, &mailslot_str );
447
448     /* symlinks */
449     link_dosdev    = create_symlink( root_directory, &link_dosdev_str, 0, &dir_global_str );
450     link_global1   = create_symlink( dir_global, &link_global_str, 0, &dir_global_str );
451     link_global2   = create_symlink( dir_basenamed, &link_global_str, 0, &dir_basenamed_str );
452     link_local     = create_symlink( dir_basenamed, &link_local_str, 0, &dir_basenamed_str );
453     link_pipe      = create_symlink( dir_global, &link_pipe_str, 0, &dir_named_pipe_str );
454     link_mailslot  = create_symlink( dir_global, &link_mailslot_str, 0, &dir_mailslot_str );
455     link_0         = create_symlink( dir_sessions, &link_0_str, 0, &dir_basenamed_str );
456     link_session   = create_symlink( dir_basenamed, &link_session_str, 0, &link_sessions_str );
457     make_object_static( (struct object *)link_dosdev );
458     make_object_static( (struct object *)link_global1 );
459     make_object_static( (struct object *)link_global2 );
460     make_object_static( (struct object *)link_local );
461     make_object_static( (struct object *)link_pipe );
462     make_object_static( (struct object *)link_mailslot );
463     make_object_static( (struct object *)link_0 );
464     make_object_static( (struct object *)link_session );
465
466     /* events */
467     for (i = 0; i < sizeof(kernel_events)/sizeof(kernel_events[0]); i++)
468     {
469         struct event *event = create_event( dir_kernel, &kernel_events[i], 0, 1, 0, NULL );
470         make_object_static( (struct object *)event );
471     }
472
473     /* the objects hold references so we can release these directories */
474     release_object( dir_global );
475     release_object( dir_device );
476     release_object( dir_basenamed );
477     release_object( dir_sessions );
478     release_object( dir_kernel );
479 }
480
481 /* create a directory object */
482 DECL_HANDLER(create_directory)
483 {
484     struct unicode_str name;
485     struct directory *dir, *root = NULL;
486
487     reply->handle = 0;
488     get_req_unicode_str( &name );
489     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
490         return;
491
492     if ((dir = create_directory( root, &name, req->attributes, HASH_SIZE )))
493     {
494         reply->handle = alloc_handle( current->process, dir, req->access, req->attributes );
495         release_object( dir );
496     }
497
498     if (root) release_object( root );
499 }
500
501 /* open a directory object */
502 DECL_HANDLER(open_directory)
503 {
504     struct unicode_str name;
505     struct directory *dir, *root = NULL;
506
507     get_req_unicode_str( &name );
508     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
509         return;
510
511     if ((dir = open_object_dir( root, &name, req->attributes, &directory_ops )))
512     {
513         reply->handle = alloc_handle( current->process, &dir->obj, req->access, req->attributes );
514         release_object( dir );
515     }
516
517     if (root) release_object( root );
518 }
519
520 /* get a directory entry by index */
521 DECL_HANDLER(get_directory_entry)
522 {
523     struct directory *dir = get_directory_obj( current->process, req->handle, DIRECTORY_QUERY );
524     if (dir)
525     {
526         struct object *obj = find_object_index( dir->entries, req->index );
527         if (obj)
528         {
529             data_size_t name_len, type_len = 0;
530             const WCHAR *type_name = NULL;
531             const WCHAR *name = get_object_name( obj, &name_len );
532             struct object_type *type = obj->ops->get_type( obj );
533
534             if (type) type_name = get_object_name( &type->obj, &type_len );
535
536             if (name_len + type_len <= get_reply_max_size())
537             {
538                 void *ptr = set_reply_data_size( name_len + type_len );
539                 if (ptr)
540                 {
541                     reply->name_len = name_len;
542                     memcpy( ptr, name, name_len );
543                     memcpy( (char *)ptr + name_len, type_name, type_len );
544                 }
545             }
546             else set_error( STATUS_BUFFER_OVERFLOW );
547
548             if (type) release_object( type );
549             release_object( obj );
550         }
551         release_object( dir );
552     }
553 }
554
555 /* unlink a named object */
556 DECL_HANDLER(unlink_object)
557 {
558     struct object *obj = get_handle_obj( current->process, req->handle, 0, NULL );
559
560     if (obj)
561     {
562         unlink_named_object( obj );
563         release_object( obj );
564     }
565 }