object.c 14.6 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4
/*
 * Server-side objects
 *
 * Copyright (C) 1998 Alexandre Julliard
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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
Alexandre Julliard's avatar
Alexandre Julliard committed
19 20
 */

21 22 23
#include "config.h"
#include "wine/port.h"

Alexandre Julliard's avatar
Alexandre Julliard committed
24
#include <assert.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
25
#include <limits.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
26
#include <stdlib.h>
27
#include <stdio.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
28
#include <string.h>
29
#include <unistd.h>
30
#include <stdarg.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
31

32 33
#include "ntstatus.h"
#define WIN32_NO_STATUS
34 35
#include "winternl.h"

36
#include "file.h"
37
#include "process.h"
38
#include "thread.h"
39
#include "unicode.h"
40
#include "security.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
41

Alexandre Julliard's avatar
Alexandre Julliard committed
42 43 44

struct object_name
{
45 46 47
    struct list         entry;           /* entry in the hash list */
    struct object      *obj;             /* object owning this name */
    struct object      *parent;          /* parent object */
48
    data_size_t         len;             /* name length in bytes */
49
    WCHAR               name[1];
Alexandre Julliard's avatar
Alexandre Julliard committed
50 51
};

52 53 54 55 56
struct namespace
{
    unsigned int        hash_size;       /* size of hash table */
    struct list         names[1];        /* array of hash entry lists */
};
Alexandre Julliard's avatar
Alexandre Julliard committed
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 64

void dump_objects(void)
{
65 66
    struct list *p;

67 68 69 70 71 72
    LIST_FOR_EACH( p, &static_object_list )
    {
        struct object *ptr = LIST_ENTRY( p, struct object, obj_list );
        fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
        ptr->ops->dump( ptr, 1 );
    }
73
    LIST_FOR_EACH( p, &object_list )
74
    {
75
        struct object *ptr = LIST_ENTRY( p, struct object, obj_list );
76 77 78 79
        fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
        ptr->ops->dump( ptr, 1 );
    }
}
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

void close_objects(void)
{
    struct list *ptr;

    /* release the static objects */
    while ((ptr = list_head( &static_object_list )))
    {
        struct object *obj = LIST_ENTRY( ptr, struct object, obj_list );
        /* move it back to the standard list before freeing */
        list_remove( &obj->obj_list );
        list_add_head( &object_list, &obj->obj_list );
        release_object( obj );
    }

    dump_objects();  /* dump any remaining objects */
}

#endif  /* DEBUG_OBJECTS */
99

Alexandre Julliard's avatar
Alexandre Julliard committed
100 101
/*****************************************************************/

102
/* malloc replacement */
Alexandre Julliard's avatar
Alexandre Julliard committed
103 104 105 106
void *mem_alloc( size_t size )
{
    void *ptr = malloc( size );
    if (ptr) memset( ptr, 0x55, size );
107
    else set_error( STATUS_NO_MEMORY );
Alexandre Julliard's avatar
Alexandre Julliard committed
108 109 110
    return ptr;
}

111 112 113
/* duplicate a block of memory */
void *memdup( const void *data, size_t len )
{
Alexandre Julliard's avatar
Alexandre Julliard committed
114
    void *ptr = malloc( len );
115
    if (ptr) memcpy( ptr, data, len );
116
    else set_error( STATUS_NO_MEMORY );
117 118 119 120
    return ptr;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
121 122
/*****************************************************************/

123
static int get_name_hash( const struct namespace *namespace, const WCHAR *name, data_size_t len )
Alexandre Julliard's avatar
Alexandre Julliard committed
124
{
125
    WCHAR hash = 0;
126
    len /= sizeof(WCHAR);
127
    while (len--) hash ^= tolowerW(*name++);
128
    return hash % namespace->hash_size;
Alexandre Julliard's avatar
Alexandre Julliard committed
129 130
}

131
/* allocate a name for an object */
132
static struct object_name *alloc_name( const struct unicode_str *name )
Alexandre Julliard's avatar
Alexandre Julliard committed
133 134 135
{
    struct object_name *ptr;

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
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
142 143 144
    return ptr;
}

145
/* free the name of an object */
Alexandre Julliard's avatar
Alexandre Julliard committed
146 147
static void free_name( struct object *obj )
{
148
    struct object_name *ptr = obj->name;
149
    list_remove( &ptr->entry );
150
    if (ptr->parent) release_object( ptr->parent );
151 152 153 154
    free( ptr );
}

/* set the name of an existing object */
155 156
static void set_object_name( struct namespace *namespace,
                             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 162
    ptr->obj = obj;
    obj->name = ptr;
Alexandre Julliard's avatar
Alexandre Julliard committed
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 169 170 171 172 173
{
    struct object_name *ptr = obj->name;
    if (!ptr) return NULL;
    *len = ptr->len;
    return ptr->name;
}

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
/* get the full path name of an existing object */
WCHAR *get_object_full_name( struct object *obj, data_size_t *ret_len )
{
    static const WCHAR backslash = '\\';
    struct object *ptr = obj;
    data_size_t len = 0;
    char *ret;

    while (ptr && ptr->name)
    {
        struct object_name *name = ptr->name;
        len += name->len + sizeof(WCHAR);
        ptr = name->parent;
    }
    if (!len) return NULL;
    if (!(ret = malloc( len ))) return NULL;

    *ret_len = len;
    while (obj && obj->name)
    {
        struct object_name *name = obj->name;
        memcpy( ret + len - name->len, name->name, name->len );
        len -= name->len + sizeof(WCHAR);
        memcpy( ret + len, &backslash, sizeof(WCHAR) );
        obj = name->parent;
    }
    return (WCHAR *)ret;
}

203
/* allocate and initialize an object */
204
void *alloc_object( const struct object_ops *ops )
205
{
206 207 208 209 210 211
    struct object *obj = mem_alloc( ops->size );
    if (obj)
    {
        obj->refcount = 1;
        obj->ops      = ops;
        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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
void *create_object( struct namespace *namespace, const struct object_ops *ops,
                     const struct unicode_str *name, struct object *parent )
{
    struct object *obj;
    struct object_name *name_ptr;

    if (!(name_ptr = alloc_name( name ))) return NULL;
    if ((obj = alloc_object( ops )))
    {
        set_object_name( namespace, obj, name_ptr );
        if (parent) name_ptr->parent = grab_object( parent );
    }
    else
        free( name_ptr );
    return obj;
}

239
void *create_named_object( struct namespace *namespace, const struct object_ops *ops,
240
                           const struct unicode_str *name, unsigned int attributes )
Alexandre Julliard's avatar
Alexandre Julliard committed
241 242
{
    struct object *obj;
243

244
    if (!name || !name->len) return alloc_object( ops );
245

246
    if ((obj = find_object( namespace, name, attributes )))
Alexandre Julliard's avatar
Alexandre Julliard committed
247
    {
248 249 250
        if (attributes & OBJ_OPENIF && obj->ops == ops)
            set_error( STATUS_OBJECT_NAME_EXISTS );
        else
Alexandre Julliard's avatar
Alexandre Julliard committed
251
        {
252 253
            release_object( obj );
            obj = NULL;
254 255 256 257
            if (attributes & OBJ_OPENIF)
                set_error( STATUS_OBJECT_TYPE_MISMATCH );
            else
                set_error( STATUS_OBJECT_NAME_COLLISION );
Alexandre Julliard's avatar
Alexandre Julliard committed
258
        }
259
        return obj;
Alexandre Julliard's avatar
Alexandre Julliard committed
260
    }
261
    if ((obj = create_object( namespace, ops, name, NULL ))) clear_error();
Alexandre Julliard's avatar
Alexandre Julliard committed
262
    return obj;
Alexandre Julliard's avatar
Alexandre Julliard committed
263 264
}

265 266
/* dump the name of an object to stderr */
void dump_object_name( struct object *obj )
267
{
268 269 270 271
    if (!obj->name) fprintf( stderr, "name=\"\"" );
    else
    {
        fprintf( stderr, "name=L\"" );
272
        dump_strW( obj->name->name, obj->name->len/sizeof(WCHAR), stderr, "\"\"" );
273 274
        fputc( '\"', stderr );
    }
275 276
}

277 278 279 280 281 282 283
/* unlink a named object from its namespace, without freeing the object itself */
void unlink_named_object( struct object *obj )
{
    if (obj->name) free_name( obj );
    obj->name = NULL;
}

284 285 286 287 288 289 290 291 292
/* mark an object as being stored statically, i.e. only released at shutdown */
void make_object_static( struct object *obj )
{
#ifdef DEBUG_OBJECTS
    list_remove( &obj->obj_list );
    list_add_head( &static_object_list, &obj->obj_list );
#endif
}

Alexandre Julliard's avatar
Alexandre Julliard committed
293 294 295 296 297 298 299 300 301 302
/* grab an object (i.e. increment its refcount) and return the object */
struct object *grab_object( void *ptr )
{
    struct object *obj = (struct object *)ptr;
    assert( obj->refcount < INT_MAX );
    obj->refcount++;
    return obj;
}

/* release an object (i.e. decrement its refcount) */
Alexandre Julliard's avatar
Alexandre Julliard committed
303 304 305 306 307 308
void release_object( void *ptr )
{
    struct object *obj = (struct object *)ptr;
    assert( obj->refcount );
    if (!--obj->refcount)
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
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 );
Alexandre Julliard's avatar
Alexandre Julliard committed
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 );
Alexandre Julliard's avatar
Alexandre Julliard committed
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 )
Alexandre Julliard's avatar
Alexandre Julliard committed
325
{
326 327
    const struct list *list;
    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 339
            if (!strncmpiW( ptr->name, name->str, name->len/sizeof(WCHAR) ))
                return grab_object( ptr->obj );
340
        }
341
        else
342
        {
343 344
            if (!memcmp( ptr->name, name->str, name->len ))
                return grab_object( ptr->obj );
345
        }
346 347
    }
    return NULL;
Alexandre Julliard's avatar
Alexandre Julliard committed
348
}
349

350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
/* find an object by its index; the refcount is incremented */
struct object *find_object_index( const struct namespace *namespace, unsigned int index )
{
    unsigned int i;

    /* FIXME: not efficient at all */
    for (i = 0; i < namespace->hash_size; i++)
    {
        const struct object_name *ptr;
        LIST_FOR_EACH_ENTRY( ptr, &namespace->names[i], const struct object_name, entry )
        {
            if (!index--) return grab_object( ptr->obj );
        }
    }
    set_error( STATUS_NO_MORE_ENTRIES );
    return NULL;
}

368
/* allocate a namespace */
369
struct namespace *create_namespace( unsigned int hash_size )
370 371 372 373 374 375 376 377 378 379 380 381 382
{
    struct namespace *namespace;
    unsigned int i;

    namespace = mem_alloc( sizeof(*namespace) + (hash_size - 1) * sizeof(namespace->names[0]) );
    if (namespace)
    {
        namespace->hash_size      = hash_size;
        for (i = 0; i < hash_size; i++) list_init( &namespace->names[i] );
    }
    return namespace;
}

383
/* functions for unimplemented/default object operations */
384

385 386 387 388 389
struct object_type *no_get_type( struct object *obj )
{
    return NULL;
}

390 391
int no_add_queue( struct object *obj, struct wait_queue_entry *entry )
{
392
    set_error( STATUS_OBJECT_TYPE_MISMATCH );
393 394 395
    return 0;
}

396 397 398 399 400
int no_satisfied( struct object *obj, struct thread *thread )
{
    return 0;  /* not abandoned */
}

401 402 403 404 405 406
int no_signal( struct object *obj, unsigned int access )
{
    set_error( STATUS_OBJECT_TYPE_MISMATCH );
    return 0;
}

407
struct fd *no_get_fd( struct object *obj )
408
{
409
    set_error( STATUS_OBJECT_TYPE_MISMATCH );
410
    return NULL;
411 412
}

413 414
unsigned int no_map_access( struct object *obj, unsigned int access )
{
415 416 417 418 419
    if (access & GENERIC_READ)    access |= STANDARD_RIGHTS_READ;
    if (access & GENERIC_WRITE)   access |= STANDARD_RIGHTS_WRITE;
    if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
    if (access & GENERIC_ALL)     access |= STANDARD_RIGHTS_ALL;
    return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
420 421
}

422 423 424 425 426 427
struct security_descriptor *default_get_sd( struct object *obj )
{
    return obj->sd;
}

int default_set_sd( struct object *obj, const struct security_descriptor *sd,
428 429 430 431 432 433 434 435
                    unsigned int set_info )
{
    struct security_descriptor new_sd, *new_sd_ptr;
    int present;
    const SID *owner, *group;
    const ACL *sacl, *dacl;
    char *ptr;

436
    if (!set_info) return 1;
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496

    new_sd.control = sd->control & ~SE_SELF_RELATIVE;

    owner = sd_get_owner( sd );
    if (set_info & OWNER_SECURITY_INFORMATION && owner)
        new_sd.owner_len = sd->owner_len;
    else
    {
        owner = token_get_user( current->process->token );
        new_sd.owner_len = FIELD_OFFSET(SID, SubAuthority[owner->SubAuthorityCount]);
        new_sd.control |= SE_OWNER_DEFAULTED;
    }

    group = sd_get_group( sd );
    if (set_info & GROUP_SECURITY_INFORMATION && group)
        new_sd.group_len = sd->group_len;
    else
    {
        group = token_get_primary_group( current->process->token );
        new_sd.group_len = FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]);
        new_sd.control |= SE_GROUP_DEFAULTED;
    }

    new_sd.control |= SE_SACL_PRESENT;
    sacl = sd_get_sacl( sd, &present );
    if (set_info & SACL_SECURITY_INFORMATION && present)
        new_sd.sacl_len = sd->sacl_len;
    else
    {
        if (obj->sd) sacl = sd_get_sacl( obj->sd, &present );

        if (obj->sd && present)
            new_sd.sacl_len = obj->sd->sacl_len;
        else
        {
            new_sd.sacl_len = 0;
            new_sd.control |= SE_SACL_DEFAULTED;
        }
    }

    new_sd.control |= SE_DACL_PRESENT;
    dacl = sd_get_dacl( sd, &present );
    if (set_info & DACL_SECURITY_INFORMATION && present)
        new_sd.dacl_len = sd->dacl_len;
    else
    {
        if (obj->sd) dacl = sd_get_dacl( obj->sd, &present );

        if (obj->sd && present)
            new_sd.dacl_len = obj->sd->dacl_len;
        else
        {
            dacl = token_get_default_dacl( current->process->token );
            new_sd.dacl_len = dacl->AclSize;
            new_sd.control |= SE_DACL_DEFAULTED;
        }
    }

    ptr = mem_alloc( sizeof(new_sd) + new_sd.owner_len + new_sd.group_len +
                     new_sd.sacl_len + new_sd.dacl_len );
497
    if (!ptr) return 0;
498 499 500 501 502 503 504 505 506 507 508 509 510 511
    new_sd_ptr = (struct security_descriptor*)ptr;

    memcpy( ptr, &new_sd, sizeof(new_sd) );
    ptr += sizeof(new_sd);
    memcpy( ptr, owner, new_sd.owner_len );
    ptr += new_sd.owner_len;
    memcpy( ptr, group, new_sd.group_len );
    ptr += new_sd.group_len;
    memcpy( ptr, sacl, new_sd.sacl_len );
    ptr += new_sd.sacl_len;
    memcpy( ptr, dacl, new_sd.dacl_len );

    free( obj->sd );
    obj->sd = new_sd_ptr;
512
    return 1;
513 514
}

515 516 517 518 519 520
struct object *no_lookup_name( struct object *obj, struct unicode_str *name,
                               unsigned int attr )
{
    return NULL;
}

521 522 523 524 525 526 527
struct object *no_open_file( struct object *obj, unsigned int access, unsigned int sharing,
                             unsigned int options )
{
    set_error( STATUS_OBJECT_TYPE_MISMATCH );
    return NULL;
}

528 529 530 531 532
int no_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
{
    return 1;  /* ok to close */
}

533 534 535
void no_destroy( struct object *obj )
{
}