secur32: Don't fail on SECURITY_NETWORK_DREP, ntlm_auth handles this just fine.
[wine/multimedia.git] / server / directory.c
blob7779c31e573b470dea0b28f022523cdff3d0f8a0
1 /*
2 * Server-side directory object management
4 * Copyright (C) 2005 Vitaliy Margolen
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <sys/types.h>
31 #include "ntstatus.h"
32 #define WIN32_NO_STATUS
33 #include "winternl.h"
34 #include "ddk/wdm.h"
36 #include "handle.h"
37 #include "request.h"
38 #include "process.h"
39 #include "object.h"
40 #include "unicode.h"
42 #define HASH_SIZE 7 /* default hash size */
44 struct directory
46 struct object obj; /* object header */
47 struct namespace *entries; /* directory's name space */
50 static void directory_dump( struct object *obj, int verbose );
51 static unsigned int directory_map_access( struct object *obj, unsigned int access );
52 static struct object *directory_lookup_name( struct object *obj, struct unicode_str *name,
53 unsigned int attr );
54 static void directory_destroy( struct object *obj );
56 static const struct object_ops directory_ops =
58 sizeof(struct directory), /* size */
59 directory_dump, /* dump */
60 no_add_queue, /* add_queue */
61 NULL, /* remove_queue */
62 NULL, /* signaled */
63 NULL, /* satisfied */
64 no_signal, /* signal */
65 no_get_fd, /* get_fd */
66 directory_map_access, /* map_access */
67 directory_lookup_name, /* lookup_name */
68 no_close_handle, /* close_handle */
69 directory_destroy /* destroy */
72 static struct directory *root_directory;
75 static void directory_dump( struct object *obj, int verbose )
77 assert( obj->ops == &directory_ops );
79 fputs( "Directory ", stderr );
80 dump_object_name( obj );
81 fputc( '\n', stderr );
84 static struct object *directory_lookup_name( struct object *obj, struct unicode_str *name,
85 unsigned int attr )
87 struct directory *dir = (struct directory *)obj;
88 struct object *found;
89 struct unicode_str tmp;
90 const WCHAR *p;
92 assert( obj->ops == &directory_ops );
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);
100 tmp.str = name->str;
101 if ((found = find_object( dir->entries, &tmp, attr )))
103 /* Skip trailing \\ */
104 if (p)
106 p++;
107 tmp.len += sizeof(WCHAR);
109 /* Move to the next element*/
110 name->str = p;
111 name->len -= tmp.len;
112 return found;
115 if (name->str)
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 );
122 return NULL;
125 static unsigned int directory_map_access( struct object *obj, unsigned int access )
127 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
128 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
129 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
130 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
131 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
134 static void directory_destroy( struct object *obj )
136 struct directory *dir = (struct directory *)obj;
137 assert( obj->ops == &directory_ops );
138 free( dir->entries );
141 static struct directory *create_directory( struct directory *root, const struct unicode_str *name,
142 unsigned int attr, unsigned int hash_size )
144 struct directory *dir;
146 if ((dir = create_named_object_dir( root, name, attr, &directory_ops )) &&
147 get_error() != STATUS_OBJECT_NAME_EXISTS)
149 if (!(dir->entries = create_namespace( hash_size )))
151 release_object( dir );
152 dir = NULL;
155 return dir;
158 struct directory *get_directory_obj( struct process *process, obj_handle_t handle, unsigned int access )
160 return (struct directory *)get_handle_obj( process, handle, access, &directory_ops );
163 /******************************************************************************
164 * Find an object by its name in a given root object
166 * PARAMS
167 * root [I] directory to start search from or NULL to start from \\
168 * name [I] object name to search for
169 * attr [I] OBJECT_ATTRIBUTES.Attributes
170 * name_left [O] [optional] leftover name if object is not found
172 * RETURNS
173 * NULL: If params are invalid
174 * Found: If object with exact name is found returns that object
175 * (name_left->len == 0). Object's refcount is incremented
176 * Not found: The last matched parent. (name_left->len > 0)
177 * Parent's refcount is incremented.
179 struct object *find_object_dir( struct directory *root, const struct unicode_str *name,
180 unsigned int attr, struct unicode_str *name_left )
182 struct object *obj, *parent;
183 struct unicode_str name_tmp;
185 if (name) name_tmp = *name;
186 else name_tmp.len = 0;
188 /* Arguments check:
189 * - Either rootdir or name have to be specified
190 * - If root is specified path shouldn't start with backslash */
191 if (root)
193 if (name_tmp.len && name_tmp.str[0] == '\\')
195 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
196 return NULL;
198 parent = grab_object( root );
200 else
202 if (!name_tmp.len || name_tmp.str[0] != '\\')
204 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
205 return NULL;
207 parent = grab_object( &root_directory->obj );
208 /* skip leading backslash */
209 name_tmp.str++;
210 name_tmp.len -= sizeof(WCHAR);
213 /* Special case for opening RootDirectory */
214 if (!name_tmp.len) goto done;
216 while ((obj = parent->ops->lookup_name( parent, &name_tmp, attr )))
218 /* move to the next element */
219 release_object ( parent );
220 parent = obj;
222 if (get_error())
224 release_object( parent );
225 return NULL;
228 done:
229 if (name_left) *name_left = name_tmp;
230 return parent;
233 /* create a named (if name is present) or unnamed object. */
234 void *create_named_object_dir( struct directory *root, const struct unicode_str *name,
235 unsigned int attributes, const struct object_ops *ops )
237 struct object *obj, *new_obj = NULL;
238 struct unicode_str new_name;
240 if (!name || !name->len) return alloc_object( ops );
242 if (!(obj = find_object_dir( root, name, attributes, &new_name ))) return NULL;
243 if (!new_name.len)
245 if (attributes & OBJ_OPENIF && obj->ops == ops)
246 set_error( STATUS_OBJECT_NAME_EXISTS );
247 else
249 release_object( obj );
250 obj = NULL;
251 if (attributes & OBJ_OPENIF)
252 set_error( STATUS_OBJECT_TYPE_MISMATCH );
253 else
254 set_error( STATUS_OBJECT_NAME_COLLISION );
256 return obj;
259 /* ATM we can't insert objects into anything else but directories */
260 if (obj->ops != &directory_ops)
261 set_error( STATUS_OBJECT_TYPE_MISMATCH );
262 else
264 struct directory *dir = (struct directory *)obj;
265 if ((new_obj = create_object( dir->entries, ops, &new_name, &dir->obj )))
266 clear_error();
269 release_object( obj );
270 return new_obj;
273 /* open a new handle to an existing object */
274 void *open_object_dir( struct directory *root, const struct unicode_str *name,
275 unsigned int attr, const struct object_ops *ops )
277 struct unicode_str name_left;
278 struct object *obj;
280 if ((obj = find_object_dir( root, name, attr, &name_left )))
282 if (name_left.len) /* not fully parsed */
283 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
284 else if (ops && obj->ops != ops)
285 set_error( STATUS_OBJECT_TYPE_MISMATCH );
286 else
287 return obj;
289 release_object( obj );
291 return NULL;
295 /* Global initialization */
297 void init_directories(void)
299 /* Directories */
300 static const WCHAR dir_globalW[] = {'\\','?','?'};
301 static const WCHAR dir_driverW[] = {'D','r','i','v','e','r'};
302 static const WCHAR dir_deviceW[] = {'D','e','v','i','c','e'};
303 static const WCHAR dir_basenamedW[] = {'\\','B','a','s','e','N','a','m','e','d','O','b','j','e','c','t','s'};
304 static const struct unicode_str dir_global_str = {dir_globalW, sizeof(dir_globalW)};
305 static const struct unicode_str dir_driver_str = {dir_driverW, sizeof(dir_driverW)};
306 static const struct unicode_str dir_device_str = {dir_deviceW, sizeof(dir_deviceW)};
307 static const struct unicode_str dir_basenamed_str = {dir_basenamedW, sizeof(dir_basenamedW)};
309 /* symlinks */
310 static const WCHAR link_dosdevW[] = {'D','o','s','D','e','v','i','c','e','s'};
311 static const WCHAR link_globalW[] = {'G','l','o','b','a','l'};
312 static const WCHAR link_localW[] = {'L','o','c','a','l'};
313 static const struct unicode_str link_dosdev_str = {link_dosdevW, sizeof(link_dosdevW)};
314 static const struct unicode_str link_global_str = {link_globalW, sizeof(link_globalW)};
315 static const struct unicode_str link_local_str = {link_localW, sizeof(link_localW)};
317 /* devices */
318 static const WCHAR pipeW[] = {'P','I','P','E'};
319 static const WCHAR mailslotW[] = {'M','A','I','L','S','L','O','T'};
320 static const struct unicode_str pipe_str = {pipeW, sizeof(pipeW)};
321 static const struct unicode_str mailslot_str = {mailslotW, sizeof(mailslotW)};
323 struct directory *dir_driver, *dir_device, *dir_global, *dir_basenamed;
324 struct symlink *link_dosdev, *link_global1, *link_global2, *link_local;
326 root_directory = create_directory( NULL, NULL, 0, HASH_SIZE );
327 dir_driver = create_directory( root_directory, &dir_driver_str, 0, HASH_SIZE );
328 dir_device = create_directory( root_directory, &dir_device_str, 0, HASH_SIZE );
329 make_object_static( &root_directory->obj );
330 make_object_static( &dir_driver->obj );
331 make_object_static( &dir_device->obj );
333 dir_global = create_directory( NULL, &dir_global_str, 0, HASH_SIZE );
334 /* use a larger hash table for this one since it can contain a lot of objects */
335 dir_basenamed = create_directory( NULL, &dir_basenamed_str, 0, 37 );
337 /* symlinks */
338 link_dosdev = create_symlink( root_directory, &link_dosdev_str, 0, &dir_global_str );
339 link_global1 = create_symlink( dir_global, &link_global_str, 0, &dir_global_str );
340 link_global2 = create_symlink( dir_basenamed, &link_global_str, 0, &dir_basenamed_str );
341 link_local = create_symlink( dir_basenamed, &link_local_str, 0, &dir_basenamed_str );
342 make_object_static( (struct object *)link_dosdev );
343 make_object_static( (struct object *)link_global1 );
344 make_object_static( (struct object *)link_global2 );
345 make_object_static( (struct object *)link_local );
347 /* devices */
348 create_named_pipe_device( dir_global, &pipe_str );
349 create_mailslot_device( dir_global, &mailslot_str );
351 /* the symlinks or devices hold references so we can release these */
352 release_object( dir_global );
353 release_object( dir_basenamed );
356 /* create a directory object */
357 DECL_HANDLER(create_directory)
359 struct unicode_str name;
360 struct directory *dir, *root = NULL;
362 reply->handle = 0;
363 get_req_unicode_str( &name );
364 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
365 return;
367 if ((dir = create_directory( root, &name, req->attributes, HASH_SIZE )))
369 reply->handle = alloc_handle( current->process, dir, req->access, req->attributes );
370 release_object( dir );
373 if (root) release_object( root );
376 /* open a directory object */
377 DECL_HANDLER(open_directory)
379 struct unicode_str name;
380 struct directory *dir, *root = NULL;
382 get_req_unicode_str( &name );
383 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
384 return;
386 if ((dir = open_object_dir( root, &name, req->attributes, &directory_ops )))
388 reply->handle = alloc_handle( current->process, &dir->obj, req->access, req->attributes );
389 release_object( dir );
392 if (root) release_object( root );