server: Add a default access mapping function for files, and use it for devices too.
[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 directory
45 {
46     struct object     obj;        /* object header */
47     struct namespace *entries;    /* directory's name space */
48 };
49
50 static void directory_dump( struct object *obj, int verbose );
51 static struct object *directory_lookup_name( struct object *obj, struct unicode_str *name,
52                                              unsigned int attr );
53 static void directory_destroy( struct object *obj );
54
55 static const struct object_ops directory_ops =
56 {
57     sizeof(struct directory),     /* size */
58     directory_dump,               /* dump */
59     no_add_queue,                 /* add_queue */
60     NULL,                         /* remove_queue */
61     NULL,                         /* signaled */
62     NULL,                         /* satisfied */
63     no_signal,                    /* signal */
64     no_get_fd,                    /* get_fd */
65     default_fd_map_access,        /* map_access */
66     directory_lookup_name,        /* lookup_name */
67     no_open_file,                 /* open_file */
68     no_close_handle,              /* close_handle */
69     directory_destroy             /* destroy */
70 };
71
72 static struct directory *root_directory;
73
74
75 static void directory_dump( struct object *obj, int verbose )
76 {
77     assert( obj->ops == &directory_ops );
78
79     fputs( "Directory ", stderr );
80     dump_object_name( obj );
81     fputc( '\n', stderr );
82 }
83
84 static struct object *directory_lookup_name( struct object *obj, struct unicode_str *name,
85                                              unsigned int attr )
86 {
87     struct directory *dir = (struct directory *)obj;
88     struct object *found;
89     struct unicode_str tmp;
90     const WCHAR *p;
91
92     assert( obj->ops == &directory_ops );
93
94     if (!(p = memchrW( name->str, '\\', name->len / sizeof(WCHAR) )))
95         /* Last element in the path name */
96         tmp.len = name->len;
97     else
98         tmp.len = (p - name->str) * sizeof(WCHAR);
99
100     tmp.str = name->str;
101     if ((found = find_object( dir->entries, &tmp, attr )))
102     {
103         /* Skip trailing \\ */
104         if (p)
105         {
106             p++;
107             tmp.len += sizeof(WCHAR);
108         }
109         /* Move to the next element*/
110         name->str = p;
111         name->len -= tmp.len;
112         return found;
113     }
114
115     if (name->str)
116     {
117         if (tmp.len == 0) /* Double backslash */
118             set_error( STATUS_OBJECT_NAME_INVALID );
119         else if (p)  /* Path still has backslashes */
120             set_error( STATUS_OBJECT_PATH_NOT_FOUND );
121     }
122     return NULL;
123 }
124
125 static void directory_destroy( struct object *obj )
126 {
127     struct directory *dir = (struct directory *)obj;
128     assert( obj->ops == &directory_ops );
129     free( dir->entries );
130 }
131
132 static struct directory *create_directory( struct directory *root, const struct unicode_str *name,
133                                            unsigned int attr, unsigned int hash_size )
134 {
135     struct directory *dir;
136
137     if ((dir = create_named_object_dir( root, name, attr, &directory_ops )) &&
138         get_error() != STATUS_OBJECT_NAME_EXISTS)
139     {
140         if (!(dir->entries = create_namespace( hash_size )))
141         {
142             release_object( dir );
143             dir = NULL;
144         }
145     }
146     return dir;
147 }
148
149 struct directory *get_directory_obj( struct process *process, obj_handle_t handle, unsigned int access )
150 {
151     return (struct directory *)get_handle_obj( process, handle, access, &directory_ops );
152 }
153
154 /******************************************************************************
155  * Find an object by its name in a given root object
156  *
157  * PARAMS
158  *  root      [I] directory to start search from or NULL to start from \\
159  *  name      [I] object name to search for
160  *  attr      [I] OBJECT_ATTRIBUTES.Attributes
161  *  name_left [O] [optional] leftover name if object is not found
162  *
163  * RETURNS
164  *  NULL:      If params are invalid
165  *  Found:     If object with exact name is found returns that object
166  *             (name_left->len == 0). Object's refcount is incremented
167  *  Not found: The last matched parent. (name_left->len > 0)
168  *             Parent's refcount is incremented.
169  */
170 struct object *find_object_dir( struct directory *root, const struct unicode_str *name,
171                                 unsigned int attr, struct unicode_str *name_left )
172 {
173     struct object *obj, *parent;
174     struct unicode_str name_tmp;
175
176     if (name) name_tmp = *name;
177     else name_tmp.len = 0;
178
179     /* Arguments check:
180      * - Either rootdir or name have to be specified
181      * - If root is specified path shouldn't start with backslash */
182     if (root)
183     {
184         if (name_tmp.len && name_tmp.str[0] == '\\')
185         {
186             set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
187             return NULL;
188         }
189         parent = grab_object( root );
190     }
191     else
192     {
193         if (!name_tmp.len || name_tmp.str[0] != '\\')
194         {
195             set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
196             return NULL;
197         }
198         parent = grab_object( &root_directory->obj );
199         /* skip leading backslash */
200         name_tmp.str++;
201         name_tmp.len -= sizeof(WCHAR);
202     }
203
204     /* Special case for opening RootDirectory */
205     if (!name_tmp.len) goto done;
206
207     while ((obj = parent->ops->lookup_name( parent, &name_tmp, attr )))
208     {
209         /* move to the next element */
210         release_object ( parent );
211         parent = obj;
212     }
213     if (get_error())
214     {
215         release_object( parent );
216         return NULL;
217     }
218
219     done:
220     if (name_left) *name_left = name_tmp;
221     return parent;
222 }
223
224 /* create a named (if name is present) or unnamed object. */
225 void *create_named_object_dir( struct directory *root, const struct unicode_str *name,
226                                unsigned int attributes, const struct object_ops *ops )
227 {
228     struct object *obj, *new_obj = NULL;
229     struct unicode_str new_name;
230
231     if (!name || !name->len) return alloc_object( ops );
232
233     if (!(obj = find_object_dir( root, name, attributes, &new_name ))) return NULL;
234     if (!new_name.len)
235     {
236         if (attributes & OBJ_OPENIF && obj->ops == ops)
237             set_error( STATUS_OBJECT_NAME_EXISTS );
238         else
239         {
240             release_object( obj );
241             obj = NULL;
242             if (attributes & OBJ_OPENIF)
243                 set_error( STATUS_OBJECT_TYPE_MISMATCH );
244             else
245                 set_error( STATUS_OBJECT_NAME_COLLISION );
246         }
247         return obj;
248     }
249
250     /* ATM we can't insert objects into anything else but directories */
251     if (obj->ops != &directory_ops)
252         set_error( STATUS_OBJECT_TYPE_MISMATCH );
253     else
254     {
255         struct directory *dir = (struct directory *)obj;
256         if ((new_obj = create_object( dir->entries, ops, &new_name, &dir->obj )))
257             clear_error();
258     }
259
260     release_object( obj );
261     return new_obj;
262 }
263
264 /* open a new handle to an existing object */
265 void *open_object_dir( struct directory *root, const struct unicode_str *name,
266                        unsigned int attr, const struct object_ops *ops )
267 {
268     struct unicode_str name_left;
269     struct object *obj;
270
271     if ((obj = find_object_dir( root, name, attr, &name_left )))
272     {
273         if (name_left.len) /* not fully parsed */
274             set_error( STATUS_OBJECT_NAME_NOT_FOUND );
275         else if (ops && obj->ops != ops)
276             set_error( STATUS_OBJECT_TYPE_MISMATCH );
277         else
278             return obj;
279
280         release_object( obj );
281     }
282     return NULL;
283 }
284
285
286 /* Global initialization */
287
288 void init_directories(void)
289 {
290     /* Directories */
291     static const WCHAR dir_globalW[] = {'\\','?','?'};
292     static const WCHAR dir_driverW[] = {'D','r','i','v','e','r'};
293     static const WCHAR dir_deviceW[] = {'D','e','v','i','c','e'};
294     static const WCHAR dir_basenamedW[] = {'\\','B','a','s','e','N','a','m','e','d','O','b','j','e','c','t','s'};
295     static const WCHAR dir_named_pipeW[] = {'\\','D','e','v','i','c','e','\\','N','a','m','e','d','P','i','p','e'};
296     static const WCHAR dir_mailslotW[] = {'\\','D','e','v','i','c','e','\\','M','a','i','l','S','l','o','t'};
297     static const struct unicode_str dir_global_str = {dir_globalW, sizeof(dir_globalW)};
298     static const struct unicode_str dir_driver_str = {dir_driverW, sizeof(dir_driverW)};
299     static const struct unicode_str dir_device_str = {dir_deviceW, sizeof(dir_deviceW)};
300     static const struct unicode_str dir_basenamed_str = {dir_basenamedW, sizeof(dir_basenamedW)};
301     static const struct unicode_str dir_named_pipe_str = {dir_named_pipeW, sizeof(dir_named_pipeW)};
302     static const struct unicode_str dir_mailslot_str = {dir_mailslotW, sizeof(dir_mailslotW)};
303
304     /* symlinks */
305     static const WCHAR link_dosdevW[] = {'D','o','s','D','e','v','i','c','e','s'};
306     static const WCHAR link_globalW[] = {'G','l','o','b','a','l'};
307     static const WCHAR link_localW[]  = {'L','o','c','a','l'};
308     static const WCHAR link_pipeW[]   = {'P','I','P','E'};
309     static const WCHAR link_mailslotW[] = {'M','A','I','L','S','L','O','T'};
310     static const struct unicode_str link_dosdev_str = {link_dosdevW, sizeof(link_dosdevW)};
311     static const struct unicode_str link_global_str = {link_globalW, sizeof(link_globalW)};
312     static const struct unicode_str link_local_str  = {link_localW, sizeof(link_localW)};
313     static const struct unicode_str link_pipe_str   = {link_pipeW, sizeof(link_pipeW)};
314     static const struct unicode_str link_mailslot_str = {link_mailslotW, sizeof(link_mailslotW)};
315
316     /* devices */
317     static const WCHAR named_pipeW[] = {'N','a','m','e','d','P','i','p','e'};
318     static const WCHAR mailslotW[] = {'M','a','i','l','S','l','o','t'};
319     static const struct unicode_str named_pipe_str = {named_pipeW, sizeof(named_pipeW)};
320     static const struct unicode_str mailslot_str = {mailslotW, sizeof(mailslotW)};
321
322     struct directory *dir_driver, *dir_device, *dir_global, *dir_basenamed;
323     struct symlink *link_dosdev, *link_global1, *link_global2, *link_local, *link_pipe, *link_mailslot;
324
325     root_directory = create_directory( NULL, NULL, 0, HASH_SIZE );
326     dir_driver     = create_directory( root_directory, &dir_driver_str, 0, HASH_SIZE );
327     dir_device     = create_directory( root_directory, &dir_device_str, 0, HASH_SIZE );
328     make_object_static( &root_directory->obj );
329     make_object_static( &dir_driver->obj );
330
331     dir_global     = create_directory( NULL, &dir_global_str, 0, HASH_SIZE );
332     /* use a larger hash table for this one since it can contain a lot of objects */
333     dir_basenamed  = create_directory( NULL, &dir_basenamed_str, 0, 37 );
334
335     /* devices */
336     create_named_pipe_device( dir_device, &named_pipe_str );
337     create_mailslot_device( dir_device, &mailslot_str );
338
339     /* symlinks */
340     link_dosdev    = create_symlink( root_directory, &link_dosdev_str, 0, &dir_global_str );
341     link_global1   = create_symlink( dir_global, &link_global_str, 0, &dir_global_str );
342     link_global2   = create_symlink( dir_basenamed, &link_global_str, 0, &dir_basenamed_str );
343     link_local     = create_symlink( dir_basenamed, &link_local_str, 0, &dir_basenamed_str );
344     link_pipe      = create_symlink( dir_global, &link_pipe_str, 0, &dir_named_pipe_str );
345     link_mailslot  = create_symlink( dir_global, &link_mailslot_str, 0, &dir_mailslot_str );
346     make_object_static( (struct object *)link_dosdev );
347     make_object_static( (struct object *)link_global1 );
348     make_object_static( (struct object *)link_global2 );
349     make_object_static( (struct object *)link_local );
350     make_object_static( (struct object *)link_pipe );
351     make_object_static( (struct object *)link_mailslot );
352
353     /* the symlinks or devices hold references so we can release these */
354     release_object( dir_global );
355     release_object( dir_device );
356     release_object( dir_basenamed );
357 }
358
359 /* create a directory object */
360 DECL_HANDLER(create_directory)
361 {
362     struct unicode_str name;
363     struct directory *dir, *root = NULL;
364
365     reply->handle = 0;
366     get_req_unicode_str( &name );
367     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
368         return;
369
370     if ((dir = create_directory( root, &name, req->attributes, HASH_SIZE )))
371     {
372         reply->handle = alloc_handle( current->process, dir, req->access, req->attributes );
373         release_object( dir );
374     }
375
376     if (root) release_object( root );
377 }
378
379 /* open a directory object */
380 DECL_HANDLER(open_directory)
381 {
382     struct unicode_str name;
383     struct directory *dir, *root = NULL;
384
385     get_req_unicode_str( &name );
386     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
387         return;
388
389     if ((dir = open_object_dir( root, &name, req->attributes, &directory_ops )))
390     {
391         reply->handle = alloc_handle( current->process, &dir->obj, req->access, req->attributes );
392         release_object( dir );
393     }
394
395     if (root) release_object( root );
396 }