directory.c 21.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Server-side directory object management
 *
 * Copyright (C) 2005 Vitaliy Margolen
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 *
 */

#include "config.h"
#include "wine/port.h"

#include <assert.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>

#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "winternl.h"
#include "ddk/wdm.h"

#include "handle.h"
#include "request.h"
#include "process.h"
39
#include "file.h"
40 41 42 43
#include "unicode.h"

#define HASH_SIZE 7  /* default hash size */

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
struct object_type
{
    struct object     obj;        /* object header */
};

static void object_type_dump( struct object *obj, int verbose );
static struct object_type *object_type_get_type( struct object *obj );

static const struct object_ops object_type_ops =
{
    sizeof(struct object_type),   /* size */
    object_type_dump,             /* dump */
    object_type_get_type,         /* get_type */
    no_add_queue,                 /* add_queue */
    NULL,                         /* remove_queue */
    NULL,                         /* signaled */
    NULL,                         /* satisfied */
    no_signal,                    /* signal */
    no_get_fd,                    /* get_fd */
    no_map_access,                /* map_access */
    default_get_sd,               /* get_sd */
    default_set_sd,               /* set_sd */
    no_lookup_name,               /* lookup_name */
67 68
    directory_link_name,          /* link_name */
    default_unlink_name,          /* unlink_name */
69 70 71 72 73 74
    no_open_file,                 /* open_file */
    no_close_handle,              /* close_handle */
    no_destroy                    /* destroy */
};


75 76 77 78 79 80 81
struct directory
{
    struct object     obj;        /* object header */
    struct namespace *entries;    /* directory's name space */
};

static void directory_dump( struct object *obj, int verbose );
82
static struct object_type *directory_get_type( struct object *obj );
83 84 85 86 87 88 89 90
static struct object *directory_lookup_name( struct object *obj, struct unicode_str *name,
                                             unsigned int attr );
static void directory_destroy( struct object *obj );

static const struct object_ops directory_ops =
{
    sizeof(struct directory),     /* size */
    directory_dump,               /* dump */
91
    directory_get_type,           /* get_type */
92 93 94 95 96 97
    no_add_queue,                 /* add_queue */
    NULL,                         /* remove_queue */
    NULL,                         /* signaled */
    NULL,                         /* satisfied */
    no_signal,                    /* signal */
    no_get_fd,                    /* get_fd */
98
    default_fd_map_access,        /* map_access */
99 100
    default_get_sd,               /* get_sd */
    default_set_sd,               /* set_sd */
101
    directory_lookup_name,        /* lookup_name */
102 103
    directory_link_name,          /* link_name */
    default_unlink_name,          /* unlink_name */
104
    no_open_file,                 /* open_file */
105 106 107 108 109
    no_close_handle,              /* close_handle */
    directory_destroy             /* destroy */
};

static struct directory *root_directory;
110
static struct directory *dir_objtype;
111 112


113 114
static void object_type_dump( struct object *obj, int verbose )
{
115
    fputs( "Object type\n", stderr );
116 117 118 119 120 121 122 123 124
}

static struct object_type *object_type_get_type( struct object *obj )
{
    static const WCHAR name[] = {'O','b','j','e','c','t','T','y','p','e'};
    static const struct unicode_str str = { name, sizeof(name) };
    return get_object_type( &str );
}

125 126
static void directory_dump( struct object *obj, int verbose )
{
127
    fputs( "Directory\n", stderr );
128 129
}

130 131 132 133 134 135 136
static struct object_type *directory_get_type( struct object *obj )
{
    static const WCHAR name[] = {'D','i','r','e','c','t','o','r','y'};
    static const struct unicode_str str = { name, sizeof(name) };
    return get_object_type( &str );
}

137 138 139 140 141 142 143 144 145 146
static struct object *directory_lookup_name( struct object *obj, struct unicode_str *name,
                                             unsigned int attr )
{
    struct directory *dir = (struct directory *)obj;
    struct object *found;
    struct unicode_str tmp;
    const WCHAR *p;

    assert( obj->ops == &directory_ops );

147 148
    if (!name) return NULL;  /* open the directory itself */

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    if (!(p = memchrW( name->str, '\\', name->len / sizeof(WCHAR) )))
        /* Last element in the path name */
        tmp.len = name->len;
    else
        tmp.len = (p - name->str) * sizeof(WCHAR);

    tmp.str = name->str;
    if ((found = find_object( dir->entries, &tmp, attr )))
    {
        /* Skip trailing \\ */
        if (p)
        {
            p++;
            tmp.len += sizeof(WCHAR);
        }
        /* Move to the next element*/
        name->str = p;
        name->len -= tmp.len;
        return found;
    }

170
    if (name->str)  /* not the last element */
171 172 173 174 175 176 177 178 179
    {
        if (tmp.len == 0) /* Double backslash */
            set_error( STATUS_OBJECT_NAME_INVALID );
        else if (p)  /* Path still has backslashes */
            set_error( STATUS_OBJECT_PATH_NOT_FOUND );
    }
    return NULL;
}

180 181 182 183 184 185 186 187 188 189 190 191 192 193
int directory_link_name( struct object *obj, struct object_name *name, struct object *parent )
{
    struct directory *dir = (struct directory *)parent;

    if (parent->ops != &directory_ops)
    {
        set_error( STATUS_OBJECT_TYPE_MISMATCH );
        return 0;
    }
    namespace_add( dir->entries, name );
    name->parent = grab_object( parent );
    return 1;
}

194 195 196 197 198 199 200
static void directory_destroy( struct object *obj )
{
    struct directory *dir = (struct directory *)obj;
    assert( obj->ops == &directory_ops );
    free( dir->entries );
}

201
static struct directory *create_directory( struct object *root, const struct unicode_str *name,
202 203
                                           unsigned int attr, unsigned int hash_size,
                                           const struct security_descriptor *sd )
204 205 206
{
    struct directory *dir;

207
    if ((dir = create_named_object( root, &directory_ops, name, attr, sd )) &&
208 209 210 211 212
        get_error() != STATUS_OBJECT_NAME_EXISTS)
    {
        if (!(dir->entries = create_namespace( hash_size )))
        {
            release_object( dir );
213
            return NULL;
214 215 216 217 218
        }
    }
    return dir;
}

219 220 221 222 223
struct object *get_root_directory(void)
{
    return grab_object( root_directory );
}

224 225
/* return a directory object for creating/opening some object; no access rights are required */
struct object *get_directory_obj( struct process *process, obj_handle_t handle )
226
{
227
    return get_handle_obj( process, handle, 0, &directory_ops );
228 229
}

230 231 232 233 234
/* retrieve an object type, creating it if needed */
struct object_type *get_object_type( const struct unicode_str *name )
{
    struct object_type *type;

235
    if ((type = create_named_object( &dir_objtype->obj, &object_type_ops, name, OBJ_OPENIF, NULL )))
236
    {
237 238 239 240 241
        if (get_error() != STATUS_OBJECT_NAME_EXISTS)
        {
            grab_object( type );
            make_object_static( &type->obj );
        }
242 243 244 245
        clear_error();
    }
    return type;
}
246 247 248

/* Global initialization */

249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
static void create_session( unsigned int id )
{
    /* directories */
    static const WCHAR dir_sessionsW[] = {'S','e','s','s','i','o','n','s'};
    static const WCHAR dir_bnolinksW[] = {'B','N','O','L','I','N','K','S'};
    static const WCHAR dir_bnoW[] = {'B','a','s','e','N','a','m','e','d','O','b','j','e','c','t','s'};
    static const WCHAR dir_dosdevicesW[] = {'D','o','s','D','e','v','i','c','e','s'};
    static const WCHAR dir_windowsW[] = {'W','i','n','d','o','w','s'};
    static const WCHAR dir_winstationsW[] = {'W','i','n','d','o','w','S','t','a','t','i','o','n','s'};
    static const struct unicode_str dir_sessions_str = {dir_sessionsW, sizeof(dir_sessionsW)};
    static const struct unicode_str dir_bnolinks_str = {dir_bnolinksW, sizeof(dir_bnolinksW)};
    static const struct unicode_str dir_bno_str = {dir_bnoW, sizeof(dir_bnoW)};
    static const struct unicode_str dir_dosdevices_str = {dir_dosdevicesW, sizeof(dir_dosdevicesW)};
    static const struct unicode_str dir_windows_str = {dir_windowsW, sizeof(dir_windowsW)};
    static const struct unicode_str dir_winstations_str = {dir_winstationsW, sizeof(dir_winstationsW)};

    /* symlinks */
    static const WCHAR link_globalW[] = {'G','l','o','b','a','l'};
    static const WCHAR link_localW[]  = {'L','o','c','a','l'};
    static const WCHAR link_sessionW[] = {'S','e','s','s','i','o','n'};
    static const struct unicode_str link_global_str = {link_globalW, sizeof(link_globalW)};
    static const struct unicode_str link_local_str = {link_localW, sizeof(link_localW)};
    static const struct unicode_str link_session_str = {link_sessionW, sizeof(link_sessionW)};

    static const WCHAR fmt_u[] = {'%','u',0};
    static struct directory *dir_bno_global, *dir_sessions, *dir_bnolinks;
    struct directory *dir_id, *dir_bno, *dir_dosdevices, *dir_windows, *dir_winstation;
    struct object *link_global, *link_local, *link_session, *link_bno, *link_windows;
    struct unicode_str id_str;
    WCHAR id_strW[10];

    if (!id)
    {
        dir_bno_global = create_directory( &root_directory->obj, &dir_bno_str, 0, HASH_SIZE, NULL );
        dir_sessions   = create_directory( &root_directory->obj, &dir_sessions_str, 0, HASH_SIZE, NULL );
        dir_bnolinks   = create_directory( &dir_sessions->obj, &dir_bnolinks_str, 0, HASH_SIZE, NULL );
        make_object_static( (struct object *)dir_bno_global );
        make_object_static( (struct object *)dir_bnolinks );
        make_object_static( (struct object *)dir_sessions );
    }

    sprintfW( id_strW, fmt_u, id );
    id_str.str = id_strW;
    id_str.len = strlenW( id_strW ) * sizeof(WCHAR);
    dir_id = create_directory( &dir_sessions->obj, &id_str, 0, HASH_SIZE, NULL );
    dir_dosdevices = create_directory( &dir_id->obj, &dir_dosdevices_str, 0, HASH_SIZE, NULL );

    /* for session 0, directories are created under the root */
    if (!id)
    {
        dir_bno      = (struct directory *)grab_object( dir_bno_global );
        dir_windows  = create_directory( &root_directory->obj, &dir_windows_str, 0, HASH_SIZE, NULL );
        link_bno     = create_obj_symlink( &dir_id->obj, &dir_bno_str, 0, &dir_bno->obj, NULL );
        link_windows = create_obj_symlink( &dir_id->obj, &dir_windows_str, 0, &dir_windows->obj, NULL );
        make_object_static( link_bno );
        make_object_static( link_windows );
    }
    else
    {
        /* use a larger hash table for this one since it can contain a lot of objects */
        dir_bno     = create_directory( &dir_id->obj, &dir_bno_str, 0, 37, NULL );
        dir_windows = create_directory( &dir_id->obj, &dir_windows_str, 0, HASH_SIZE, NULL );
    }
    dir_winstation = create_directory( &dir_windows->obj, &dir_winstations_str, 0, HASH_SIZE, NULL );

    link_global  = create_obj_symlink( &dir_bno->obj, &link_global_str, 0, &dir_bno_global->obj, NULL );
    link_local   = create_obj_symlink( &dir_bno->obj, &link_local_str, 0, &dir_bno->obj, NULL );
    link_session = create_obj_symlink( &dir_bno->obj, &link_session_str, 0, &dir_bnolinks->obj, NULL );
    link_bno     = create_obj_symlink( &dir_bnolinks->obj, &id_str, 0, &dir_bno->obj, NULL );
    make_object_static( link_global );
    make_object_static( link_local );
    make_object_static( link_session );
    make_object_static( link_bno );

    make_object_static( &dir_dosdevices->obj );
    make_object_static( &dir_winstation->obj );
    release_object( dir_windows );
    release_object( dir_bno );
    release_object( dir_id );
}

330 331 332
void init_directories(void)
{
    /* Directories */
333
    static const WCHAR dir_globalW[] = {'?','?'};
334 335
    static const WCHAR dir_driverW[] = {'D','r','i','v','e','r'};
    static const WCHAR dir_deviceW[] = {'D','e','v','i','c','e'};
336
    static const WCHAR dir_objtypeW[] = {'O','b','j','e','c','t','T','y','p','e','s'};
337
    static const WCHAR dir_kernelW[] = {'K','e','r','n','e','l','O','b','j','e','c','t','s'};
338 339 340
    static const struct unicode_str dir_global_str = {dir_globalW, sizeof(dir_globalW)};
    static const struct unicode_str dir_driver_str = {dir_driverW, sizeof(dir_driverW)};
    static const struct unicode_str dir_device_str = {dir_deviceW, sizeof(dir_deviceW)};
341
    static const struct unicode_str dir_objtype_str = {dir_objtypeW, sizeof(dir_objtypeW)};
342
    static const struct unicode_str dir_kernel_str = {dir_kernelW, sizeof(dir_kernelW)};
343

344 345 346
    /* symlinks */
    static const WCHAR link_dosdevW[] = {'D','o','s','D','e','v','i','c','e','s'};
    static const WCHAR link_globalW[] = {'G','l','o','b','a','l'};
347
    static const WCHAR link_nulW[]    = {'N','U','L'};
348 349
    static const WCHAR link_pipeW[]   = {'P','I','P','E'};
    static const WCHAR link_mailslotW[] = {'M','A','I','L','S','L','O','T'};
350 351
    static const struct unicode_str link_dosdev_str = {link_dosdevW, sizeof(link_dosdevW)};
    static const struct unicode_str link_global_str = {link_globalW, sizeof(link_globalW)};
352
    static const struct unicode_str link_nul_str    = {link_nulW, sizeof(link_nulW)};
353 354
    static const struct unicode_str link_pipe_str   = {link_pipeW, sizeof(link_pipeW)};
    static const struct unicode_str link_mailslot_str = {link_mailslotW, sizeof(link_mailslotW)};
355

356
    /* devices */
357 358
    static const WCHAR named_pipeW[] = {'N','a','m','e','d','P','i','p','e'};
    static const WCHAR mailslotW[] = {'M','a','i','l','S','l','o','t'};
359
    static const WCHAR nullW[] = {'N','u','l','l'};
360
    static const struct unicode_str named_pipe_str = {named_pipeW, sizeof(named_pipeW)};
361
    static const struct unicode_str mailslot_str = {mailslotW, sizeof(mailslotW)};
362
    static const struct unicode_str null_str = {nullW, sizeof(nullW)};
363

364 365 366 367 368 369 370
    /* events */
    static const WCHAR event_low_memW[] = {'L','o','w','M','e','m','o','r','y','C','o','n','d','i','t','i','o','n'};
    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'};
    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'};
    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'};
    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'};
    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'};
371
    static const WCHAR keyed_event_crit_sectW[] = {'C','r','i','t','S','e','c','O','u','t','O','f','M','e','m','o','r','y','E','v','e','n','t'};
372 373 374 375 376 377 378 379 380
    static const struct unicode_str kernel_events[] =
    {
        { event_low_memW, sizeof(event_low_memW) },
        { event_low_pagedW, sizeof(event_low_pagedW) },
        { event_low_nonpgW, sizeof(event_low_nonpgW) },
        { event_high_memW, sizeof(event_high_memW) },
        { event_high_pagedW, sizeof(event_high_pagedW) },
        { event_high_nonpgW, sizeof(event_high_nonpgW) }
    };
381
    static const struct unicode_str keyed_event_crit_sect_str = {keyed_event_crit_sectW, sizeof(keyed_event_crit_sectW)};
382

383 384
    struct directory *dir_driver, *dir_device, *dir_global, *dir_kernel;
    struct object *link_dosdev, *link_global, *link_nul, *link_pipe, *link_mailslot;
385
    struct object *named_pipe_device, *mailslot_device, *null_device;
386
    struct keyed_event *keyed_event;
387
    unsigned int i;
388

389
    root_directory = create_directory( NULL, NULL, 0, HASH_SIZE, NULL );
390 391 392 393
    dir_driver     = create_directory( &root_directory->obj, &dir_driver_str, 0, HASH_SIZE, NULL );
    dir_device     = create_directory( &root_directory->obj, &dir_device_str, 0, HASH_SIZE, NULL );
    dir_objtype    = create_directory( &root_directory->obj, &dir_objtype_str, 0, HASH_SIZE, NULL );
    dir_kernel     = create_directory( &root_directory->obj, &dir_kernel_str, 0, HASH_SIZE, NULL );
394
    dir_global     = create_directory( &root_directory->obj, &dir_global_str, 0, HASH_SIZE, NULL );
395 396
    make_object_static( &root_directory->obj );
    make_object_static( &dir_driver->obj );
397
    make_object_static( &dir_objtype->obj );
398

399
    /* devices */
400 401 402 403 404 405
    named_pipe_device = create_named_pipe_device( &dir_device->obj, &named_pipe_str );
    mailslot_device   = create_mailslot_device( &dir_device->obj, &mailslot_str );
    null_device       = create_unix_device( &dir_device->obj, &null_str, "/dev/null" );
    make_object_static( named_pipe_device );
    make_object_static( mailslot_device );
    make_object_static( null_device );
406

407 408 409 410
    /* sessions */
    create_session( 0 );
    create_session( 1 );

411
    /* symlinks */
412
    link_dosdev    = create_obj_symlink( &root_directory->obj, &link_dosdev_str, 0, &dir_global->obj, NULL );
413
    link_global    = create_obj_symlink( &dir_global->obj, &link_global_str, 0, &dir_global->obj, NULL );
414 415 416 417
    link_nul       = create_obj_symlink( &dir_global->obj, &link_nul_str, 0, null_device, NULL );
    link_pipe      = create_obj_symlink( &dir_global->obj, &link_pipe_str, 0, named_pipe_device, NULL );
    link_mailslot  = create_obj_symlink( &dir_global->obj, &link_mailslot_str, 0, mailslot_device, NULL );
    make_object_static( link_dosdev );
418
    make_object_static( link_global );
419 420 421
    make_object_static( link_nul );
    make_object_static( link_pipe );
    make_object_static( link_mailslot );
422

423 424 425
    /* events */
    for (i = 0; i < sizeof(kernel_events)/sizeof(kernel_events[0]); i++)
    {
426
        struct event *event = create_event( &dir_kernel->obj, &kernel_events[i], 0, 1, 0, NULL );
427 428
        make_object_static( (struct object *)event );
    }
429
    keyed_event = create_keyed_event( &dir_kernel->obj, &keyed_event_crit_sect_str, 0, NULL );
430
    make_object_static( (struct object *)keyed_event );
431 432

    /* the objects hold references so we can release these directories */
433
    release_object( dir_global );
434
    release_object( dir_device );
435
    release_object( dir_kernel );
436 437 438 439 440 441
}

/* create a directory object */
DECL_HANDLER(create_directory)
{
    struct unicode_str name;
442 443
    struct object *root;
    struct directory *dir;
444
    const struct security_descriptor *sd;
445
    const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
446

447
    if (!objattr) return;
448

449
    if ((dir = create_directory( root, &name, objattr->attributes, HASH_SIZE, sd )))
450
    {
451
        reply->handle = alloc_handle( current->process, dir, req->access, objattr->attributes );
452 453 454 455 456 457 458 459 460
        release_object( dir );
    }

    if (root) release_object( root );
}

/* open a directory object */
DECL_HANDLER(open_directory)
{
461
    struct unicode_str name = get_req_unicode_str();
462

463 464
    reply->handle = open_object( current->process, req->rootdir, req->access,
                                 &directory_ops, &name, req->attributes );
465
}
466 467 468 469

/* get a directory entry by index */
DECL_HANDLER(get_directory_entry)
{
470 471
    struct directory *dir = (struct directory *)get_handle_obj( current->process, req->handle,
                                                                DIRECTORY_QUERY, &directory_ops );
472 473 474 475 476
    if (dir)
    {
        struct object *obj = find_object_index( dir->entries, req->index );
        if (obj)
        {
477
            data_size_t name_len, type_len = 0;
478
            const WCHAR *type_name = NULL;
479
            const WCHAR *name = get_object_name( obj, &name_len );
480 481 482
            struct object_type *type = obj->ops->get_type( obj );

            if (type) type_name = get_object_name( &type->obj, &type_len );
483 484 485 486 487 488 489 490

            if (name_len + type_len <= get_reply_max_size())
            {
                void *ptr = set_reply_data_size( name_len + type_len );
                if (ptr)
                {
                    reply->name_len = name_len;
                    memcpy( ptr, name, name_len );
491
                    memcpy( (char *)ptr + name_len, type_name, type_len );
492 493 494 495
                }
            }
            else set_error( STATUS_BUFFER_OVERFLOW );

496
            if (type) release_object( type );
497 498 499 500 501
            release_object( obj );
        }
        release_object( dir );
    }
}
502 503 504 505 506 507 508 509 510 511 512 513

/* unlink a named object */
DECL_HANDLER(unlink_object)
{
    struct object *obj = get_handle_obj( current->process, req->handle, 0, NULL );

    if (obj)
    {
        unlink_named_object( obj );
        release_object( obj );
    }
}
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531

/* query object type name information */
DECL_HANDLER(get_object_type)
{
    struct object *obj;
    struct object_type *type;
    const WCHAR *name;

    if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;

    if ((type = obj->ops->get_type( obj )))
    {
        if ((name = get_object_name( &type->obj, &reply->total )))
            set_reply_data( name, min( reply->total, get_reply_max_size() ) );
        release_object( type );
    }
    release_object( obj );
}