file.c 26.1 KB
Newer Older
1 2 3 4
/*
 * Server-side file management
 *
 * 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
19 20
 */

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

24 25
#include <assert.h>
#include <fcntl.h>
26
#include <stdarg.h>
27
#include <stdio.h>
28
#include <string.h>
29
#include <stdlib.h>
30
#include <errno.h>
31 32 33 34 35
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
Steven Edwards's avatar
Steven Edwards committed
36
#ifdef HAVE_UTIME_H
37
#include <utime.h>
Steven Edwards's avatar
Steven Edwards committed
38
#endif
39 40 41
#ifdef HAVE_POLL_H
#include <poll.h>
#endif
42

43 44
#include "ntstatus.h"
#define WIN32_NO_STATUS
45
#include "windef.h"
46
#include "winternl.h"
47

48
#include "file.h"
49 50
#include "handle.h"
#include "thread.h"
51
#include "request.h"
52 53
#include "process.h"
#include "security.h"
54 55 56

struct file
{
57 58 59 60 61 62
    struct object       obj;            /* object header */
    struct fd          *fd;             /* file descriptor for this file */
    unsigned int        access;         /* file access (FILE_READ_DATA etc.) */
    mode_t              mode;           /* file stat.st_mode */
    uid_t               uid;            /* file stat.st_uid */
    struct list         kernel_object;  /* list of kernel object pointers */
63 64
};

65 66
static unsigned int generic_file_map_access( unsigned int access );

67
static void file_dump( struct object *obj, int verbose );
68
static struct fd *file_get_fd( struct object *obj );
69 70
static struct security_descriptor *file_get_sd( struct object *obj );
static int file_set_sd( struct object *obj, const struct security_descriptor *sd, unsigned int set_info );
71
static struct object *file_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr );
72 73
static struct object *file_open_file( struct object *obj, unsigned int access,
                                      unsigned int sharing, unsigned int options );
74
static struct list *file_get_kernel_obj_list( struct object *obj );
75
static void file_destroy( struct object *obj );
76 77

static int file_get_poll_events( struct fd *fd );
78
static int file_flush( struct fd *fd, struct async *async );
79
static enum server_fd_type file_get_fd_type( struct fd *fd );
80 81 82

static const struct object_ops file_ops =
{
83 84
    sizeof(struct file),          /* size */
    file_dump,                    /* dump */
85
    file_get_type,                /* get_type */
86 87
    add_queue,                    /* add_queue */
    remove_queue,                 /* remove_queue */
88
    default_fd_signaled,          /* signaled */
89
    no_satisfied,                 /* satisfied */
90
    no_signal,                    /* signal */
91
    file_get_fd,                  /* get_fd */
92
    default_fd_map_access,        /* map_access */
93 94
    file_get_sd,                  /* get_sd */
    file_set_sd,                  /* set_sd */
95
    file_lookup_name,             /* lookup_name */
96 97
    no_link_name,                 /* link_name */
    NULL,                         /* unlink_name */
98
    file_open_file,               /* open_file */
99
    file_get_kernel_obj_list,     /* get_kernel_obj_list */
100
    fd_close_handle,              /* close_handle */
101 102 103 104 105
    file_destroy                  /* destroy */
};

static const struct fd_ops file_fd_ops =
{
106
    file_get_poll_events,         /* get_poll_events */
107
    default_poll_event,           /* poll_event */
108
    file_get_fd_type,             /* get_fd_type */
109 110 111
    no_fd_read,                   /* read */
    no_fd_write,                  /* write */
    file_flush,                   /* flush */
112
    default_fd_get_file_info,     /* get_file_info */
113
    no_fd_get_volume_info,        /* get_volume_info */
114
    default_fd_ioctl,             /* ioctl */
115
    default_fd_queue_async,       /* queue_async */
116
    default_fd_reselect_async     /* reselect_async */
117 118
};

119 120
/* create a file from a file descriptor */
/* if the function fails the fd is closed */
121
struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing )
122 123
{
    struct file *file;
124 125 126 127 128
    struct stat st;

    if (fstat( fd, &st ) == -1)
    {
        file_set_error();
129
        close( fd );
130 131
        return NULL;
    }
132

133
    if (!(file = alloc_object( &file_ops )))
134
    {
135 136 137 138 139 140
        close( fd );
        return NULL;
    }

    file->mode = st.st_mode;
    file->access = default_fd_map_access( &file->obj, access );
141
    list_init( &file->kernel_object );
142 143 144 145 146
    if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj,
                                          FILE_SYNCHRONOUS_IO_NONALERT )))
    {
        release_object( file );
        return NULL;
147
    }
148
    allow_fd_caching( file->fd );
149 150
    return file;
}
151

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
/* create a file by duplicating an fd object */
struct file *create_file_for_fd_obj( struct fd *fd, unsigned int access, unsigned int sharing )
{
    struct file *file;
    struct stat st;

    if (fstat( get_unix_fd(fd), &st ) == -1)
    {
        file_set_error();
        return NULL;
    }

    if ((file = alloc_object( &file_ops )))
    {
        file->mode = st.st_mode;
        file->access = default_fd_map_access( &file->obj, access );
168
        list_init( &file->kernel_object );
169 170 171 172 173
        if (!(file->fd = dup_fd_object( fd, access, sharing, FILE_SYNCHRONOUS_IO_NONALERT )))
        {
            release_object( file );
            return NULL;
        }
174
        set_fd_user( file->fd, &file_fd_ops, &file->obj );
175 176 177 178
    }
    return file;
}

179
static struct object *create_file_obj( struct fd *fd, unsigned int access, mode_t mode )
180 181 182 183 184
{
    struct file *file = alloc_object( &file_ops );

    if (!file) return NULL;
    file->access  = access;
185
    file->mode    = mode;
186
    file->uid     = ~(uid_t)0;
187
    file->fd      = fd;
188
    list_init( &file->kernel_object );
189 190 191 192
    grab_object( fd );
    set_fd_user( fd, &file_fd_ops, &file->obj );
    return &file->obj;
}
193

194 195 196 197
static struct object *create_file( struct fd *root, const char *nameptr, data_size_t len,
                                   unsigned int access, unsigned int sharing, int create,
                                   unsigned int options, unsigned int attrs,
                                   const struct security_descriptor *sd )
198
{
199 200
    struct object *obj = NULL;
    struct fd *fd;
201
    int flags;
202
    char *name;
203
    mode_t mode;
204

205 206 207 208 209
    if (!len || ((nameptr[0] == '/') ^ !root))
    {
        set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
        return NULL;
    }
210 211 212
    if (!(name = mem_alloc( len + 1 ))) return NULL;
    memcpy( name, nameptr, len );
    name[len] = 0;
213

214
    switch(create)
215
    {
216 217
    case FILE_CREATE:       flags = O_CREAT | O_EXCL; break;
    case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
218
                            access |= FILE_WRITE_ATTRIBUTES;
219 220 221
    case FILE_SUPERSEDE:    flags = O_CREAT | O_TRUNC; break;
    case FILE_OPEN:         flags = 0; break;
    case FILE_OPEN_IF:      flags = O_CREAT; break;
222 223
    case FILE_OVERWRITE:    flags = O_TRUNC;
                            access |= FILE_WRITE_ATTRIBUTES; break;
224
    default:                set_error( STATUS_INVALID_PARAMETER ); goto done;
225
    }
226

227 228 229 230 231 232 233
    if (sd)
    {
        const SID *owner = sd_get_owner( sd );
        if (!owner)
            owner = token_get_user( current->process->token );
        mode = sd_to_mode( sd, owner );
    }
234 235
    else if (options & FILE_DIRECTORY_FILE)
        mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0555 : 0777;
236 237
    else
        mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
238 239 240

    if (len >= 4 &&
        (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
241 242 243 244 245 246 247 248
    {
        if (mode & S_IRUSR)
            mode |= S_IXUSR;
        if (mode & S_IRGRP)
            mode |= S_IXGRP;
        if (mode & S_IROTH)
            mode |= S_IXOTH;
    }
249

250
    access = generic_file_map_access( access );
251

252
    /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
253
    fd = open_fd( root, name, flags | O_NONBLOCK | O_LARGEFILE, &mode, access, sharing, options );
254
    if (!fd) goto done;
255

256
    if (S_ISDIR(mode))
257
        obj = create_dir_obj( fd, access, mode );
258
    else if (S_ISCHR(mode) && is_serial_fd( fd ))
259
        obj = create_serial( fd );
260
    else
261
        obj = create_file_obj( fd, access, mode );
262

263
    release_object( fd );
264

265
done:
266
    free( name );
267
    return obj;
268 269 270 271 272 273
}

static void file_dump( struct object *obj, int verbose )
{
    struct file *file = (struct file *)obj;
    assert( obj->ops == &file_ops );
274
    fprintf( stderr, "File fd=%p\n", file->fd );
275 276
}

277
struct object_type *file_get_type( struct object *obj )
278 279 280 281 282 283
{
    static const WCHAR name[] = {'F','i','l','e'};
    static const struct unicode_str str = { name, sizeof(name) };
    return get_object_type( &str );
}

284
static int file_get_poll_events( struct fd *fd )
285
{
286
    struct file *file = get_fd_user( fd );
287
    int events = 0;
288
    assert( file->obj.ops == &file_ops );
289 290
    if (file->access & FILE_UNIX_READ_ACCESS) events |= POLLIN;
    if (file->access & FILE_UNIX_WRITE_ACCESS) events |= POLLOUT;
291
    return events;
292 293
}

294
static int file_flush( struct fd *fd, struct async *async )
295
{
296
    int unix_fd = get_unix_fd( fd );
297 298 299 300 301 302
    if (unix_fd != -1 && fsync( unix_fd ) == -1)
    {
        file_set_error();
        return 0;
    }
    return 1;
303 304
}

305
static enum server_fd_type file_get_fd_type( struct fd *fd )
306
{
307 308 309 310 311
    struct file *file = get_fd_user( fd );

    if (S_ISREG(file->mode) || S_ISBLK(file->mode)) return FD_TYPE_FILE;
    if (S_ISDIR(file->mode)) return FD_TYPE_DIR;
    return FD_TYPE_CHAR;
312 313
}

314 315 316 317 318
static struct fd *file_get_fd( struct object *obj )
{
    struct file *file = (struct file *)obj;
    assert( obj->ops == &file_ops );
    return (struct fd *)grab_object( file->fd );
319 320
}

321
static unsigned int generic_file_map_access( unsigned int access )
322 323 324 325 326 327 328 329
{
    if (access & GENERIC_READ)    access |= FILE_GENERIC_READ;
    if (access & GENERIC_WRITE)   access |= FILE_GENERIC_WRITE;
    if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
    if (access & GENERIC_ALL)     access |= FILE_ALL_ACCESS;
    return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
}

330
struct security_descriptor *mode_to_sd( mode_t mode, const SID *user, const SID *group )
331 332 333
{
    struct security_descriptor *sd;
    size_t dacl_size;
334
    ACE_HEADER *current_ace;
335 336 337 338 339 340 341 342
    ACCESS_ALLOWED_ACE *aaa;
    ACL *dacl;
    SID *sid;
    char *ptr;
    const SID *world_sid = security_world_sid;
    const SID *local_system_sid = security_local_system_sid;

    dacl_size = sizeof(ACL) + FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
343
        security_sid_len( local_system_sid );
344
    if (mode & S_IRWXU)
345
        dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
346 347 348
    if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
        (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
        (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
349
        dacl_size += FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
350
    if (mode & S_IRWXO)
351
        dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
352 353

    sd = mem_alloc( sizeof(struct security_descriptor) +
354
                    security_sid_len( user ) + security_sid_len( group ) +
355
                    dacl_size );
356
    if (!sd) return sd;
357 358

    sd->control = SE_DACL_PRESENT;
359 360
    sd->owner_len = security_sid_len( user );
    sd->group_len = security_sid_len( group );
361 362 363 364 365 366 367 368 369 370 371 372 373
    sd->sacl_len = 0;
    sd->dacl_len = dacl_size;

    ptr = (char *)(sd + 1);
    memcpy( ptr, user, sd->owner_len );
    ptr += sd->owner_len;
    memcpy( ptr, group, sd->group_len );
    ptr += sd->group_len;

    dacl = (ACL *)ptr;
    dacl->AclRevision = ACL_REVISION;
    dacl->Sbz1 = 0;
    dacl->AclSize = dacl_size;
374 375 376 377
    dacl->AceCount = 1 + (mode & S_IRWXU ? 1 : 0) + (mode & S_IRWXO ? 1 : 0);
    if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
        (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
        (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
378
        dacl->AceCount++;
379 380 381 382
    dacl->Sbz2 = 0;

    /* always give FILE_ALL_ACCESS for Local System */
    aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
383
    current_ace = &aaa->Header;
384
    aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
385
    aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
386
    aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( local_system_sid );
387 388
    aaa->Mask = FILE_ALL_ACCESS;
    sid = (SID *)&aaa->SidStart;
389
    memcpy( sid, local_system_sid, security_sid_len( local_system_sid ));
390

391
    if (mode & S_IRWXU)
392 393
    {
        /* appropriate access rights for the user */
394 395
        aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
        current_ace = &aaa->Header;
396
        aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
397
        aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
398
        aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
399
        aaa->Mask = WRITE_DAC | WRITE_OWNER;
400
        if (mode & S_IRUSR)
401
            aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
402
        if (mode & S_IWUSR)
403
            aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
404
        sid = (SID *)&aaa->SidStart;
405
        memcpy( sid, user, security_sid_len( user ));
406
    }
407 408 409
    if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
        (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
        (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
410 411 412 413 414
    {
        /* deny just in case the user is a member of the group */
        ACCESS_DENIED_ACE *ada = (ACCESS_DENIED_ACE *)ace_next( current_ace );
        current_ace = &ada->Header;
        ada->Header.AceType = ACCESS_DENIED_ACE_TYPE;
415
        ada->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
416
        ada->Header.AceSize = FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
417
        ada->Mask = 0;
418
        if (!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH)))
419
            ada->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
420
        if (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IROTH)))
421
            ada->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
422 423
        ada->Mask &= ~STANDARD_RIGHTS_ALL; /* never deny standard rights */
        sid = (SID *)&ada->SidStart;
424
        memcpy( sid, user, security_sid_len( user ));
425
    }
426
    if (mode & S_IRWXO)
427 428
    {
        /* appropriate access rights for Everyone */
429 430
        aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
        current_ace = &aaa->Header;
431
        aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
432
        aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
433
        aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
434
        aaa->Mask = 0;
435
        if (mode & S_IROTH)
436
            aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
437
        if (mode & S_IWOTH)
438
            aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
439
        sid = (SID *)&aaa->SidStart;
440
        memcpy( sid, world_sid, security_sid_len( world_sid ));
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
    return sd;
}

static struct security_descriptor *file_get_sd( struct object *obj )
{
    struct file *file = (struct file *)obj;
    struct stat st;
    int unix_fd;
    struct security_descriptor *sd;

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

    unix_fd = get_file_unix_fd( file );

    if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
        return obj->sd;

    /* mode and uid the same? if so, no need to re-generate security descriptor */
    if (obj->sd && (st.st_mode & (S_IRWXU|S_IRWXO)) == (file->mode & (S_IRWXU|S_IRWXO)) &&
        (st.st_uid == file->uid))
        return obj->sd;

    sd = mode_to_sd( st.st_mode,
                     security_unix_uid_to_sid( st.st_uid ),
                     token_get_primary_group( current->process->token ));
    if (!sd) return obj->sd;

470 471 472 473 474 475 476
    file->mode = st.st_mode;
    file->uid = st.st_uid;
    free( obj->sd );
    obj->sd = sd;
    return sd;
}

477 478 479 480 481 482
static mode_t file_access_to_mode( unsigned int access )
{
    mode_t mode = 0;

    access = generic_file_map_access( access );
    if (access & FILE_READ_DATA)  mode |= 4;
483
    if (access & (FILE_WRITE_DATA|FILE_APPEND_DATA)) mode |= 2;
484 485 486 487
    if (access & FILE_EXECUTE)    mode |= 1;
    return mode;
}

488
mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner )
489
{
490
    mode_t new_mode = 0;
491
    mode_t bits_to_set = ~0;
492
    mode_t mode;
493 494
    int present;
    const ACL *dacl = sd_get_dacl( sd, &present );
495
    const SID *user = token_get_user( current->process->token );
496
    if (present && dacl)
497
    {
498 499
        const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
        ULONG i;
500
        for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
501
        {
502 503 504
            const ACCESS_ALLOWED_ACE *aa_ace;
            const ACCESS_DENIED_ACE *ad_ace;
            const SID *sid;
505

506
            if (ace->AceFlags & INHERIT_ONLY_ACE) continue;
507

508
            switch (ace->AceType)
509 510 511 512
            {
                case ACCESS_DENIED_ACE_TYPE:
                    ad_ace = (const ACCESS_DENIED_ACE *)ace;
                    sid = (const SID *)&ad_ace->SidStart;
513
                    mode = file_access_to_mode( ad_ace->Mask );
514 515
                    if (security_equal_sid( sid, security_world_sid ))
                    {
516
                        bits_to_set &= ~((mode << 6) | (mode << 3) | mode); /* all */
517
                    }
518 519 520
                    else if ((security_equal_sid( user, owner ) &&
                              token_sid_present( current->process->token, sid, TRUE )))
                    {
521
                        bits_to_set &= ~((mode << 6) | (mode << 3));  /* user + group */
522
                    }
523 524
                    else if (security_equal_sid( sid, owner ))
                    {
525
                        bits_to_set &= ~(mode << 6);  /* user only */
526
                    }
527 528 529 530
                    break;
                case ACCESS_ALLOWED_ACE_TYPE:
                    aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
                    sid = (const SID *)&aa_ace->SidStart;
531
                    mode = file_access_to_mode( aa_ace->Mask );
532 533
                    if (security_equal_sid( sid, security_world_sid ))
                    {
534 535 536
                        mode = (mode << 6) | (mode << 3) | mode;  /* all */
                        new_mode |= mode & bits_to_set;
                        bits_to_set &= ~mode;
537
                    }
538 539 540
                    else if ((security_equal_sid( user, owner ) &&
                              token_sid_present( current->process->token, sid, FALSE )))
                    {
541 542 543
                        mode = (mode << 6) | (mode << 3);  /* user + group */
                        new_mode |= mode & bits_to_set;
                        bits_to_set &= ~mode;
544
                    }
545 546
                    else if (security_equal_sid( sid, owner ))
                    {
547 548 549
                        mode = (mode << 6);  /* user only */
                        new_mode |= mode & bits_to_set;
                        bits_to_set &= ~mode;
550
                    }
551 552 553
                    break;
            }
        }
554 555 556
    }
    else
        /* no ACL means full access rights to anyone */
557
        new_mode = S_IRWXU | S_IRWXG | S_IRWXO;
558

559
    return new_mode;
560
}
561

562 563 564 565 566
static int file_set_sd( struct object *obj, const struct security_descriptor *sd,
                        unsigned int set_info )
{
    struct file *file = (struct file *)obj;
    const SID *owner;
567
    struct stat st;
568 569 570 571 572 573 574
    mode_t mode;
    int unix_fd;

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

    unix_fd = get_file_unix_fd( file );

575
    if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
576 577 578 579 580

    if (set_info & OWNER_SECURITY_INFORMATION)
    {
        owner = sd_get_owner( sd );
        if (!owner)
581
        {
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
            set_error( STATUS_INVALID_SECURITY_DESCR );
            return 0;
        }
        if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
        {
            /* FIXME: get Unix uid and call fchown */
        }
    }
    else if (obj->sd)
        owner = sd_get_owner( obj->sd );
    else
        owner = token_get_user( current->process->token );

    /* group and sacl not supported */

    if (set_info & DACL_SECURITY_INFORMATION)
    {
        /* keep the bits that we don't map to access rights in the ACL */
600
        mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
601 602
        mode |= sd_to_mode( sd, owner );

603
        if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
604
        {
605 606
            file_set_error();
            return 0;
607
        }
608 609 610 611
    }
    return 1;
}

612 613 614 615 616 617 618 619
static struct object *file_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr )
{
    if (!name || !name->len) return NULL;  /* open the file itself */

    set_error( STATUS_OBJECT_PATH_NOT_FOUND );
    return NULL;
}

620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
static struct object *file_open_file( struct object *obj, unsigned int access,
                                      unsigned int sharing, unsigned int options )
{
    struct file *file = (struct file *)obj;
    struct object *new_file = NULL;
    char *unix_name;

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

    if ((unix_name = dup_fd_name( file->fd, "" )))
    {
        new_file = create_file( NULL, unix_name, strlen(unix_name), access,
                                sharing, FILE_OPEN, options, 0, NULL );
        free( unix_name );
    }
    else set_error( STATUS_OBJECT_TYPE_MISMATCH );
    return new_file;
}

639 640 641 642 643 644
static struct list *file_get_kernel_obj_list( struct object *obj )
{
    struct file *file = (struct file *)obj;
    return &file->kernel_object;
}

645
static void file_destroy( struct object *obj )
646
{
647 648
    struct file *file = (struct file *)obj;
    assert( obj->ops == &file_ops );
649

650
    if (file->fd) release_object( file->fd );
651 652 653 654 655 656 657
}

/* set the last error depending on errno */
void file_set_error(void)
{
    switch (errno)
    {
658
    case ETXTBSY:
659 660 661
    case EAGAIN:    set_error( STATUS_SHARING_VIOLATION ); break;
    case EBADF:     set_error( STATUS_INVALID_HANDLE ); break;
    case ENOSPC:    set_error( STATUS_DISK_FULL ); break;
662
    case EACCES:
663
    case ESRCH:
664
    case EROFS:
665 666 667
    case EPERM:     set_error( STATUS_ACCESS_DENIED ); break;
    case EBUSY:     set_error( STATUS_FILE_LOCK_CONFLICT ); break;
    case ENOENT:    set_error( STATUS_NO_SUCH_FILE ); break;
668
    case EISDIR:    set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
669
    case ENFILE:
670
    case EMFILE:    set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
671 672
    case EEXIST:    set_error( STATUS_OBJECT_NAME_COLLISION ); break;
    case EINVAL:    set_error( STATUS_INVALID_PARAMETER ); break;
673
    case ESPIPE:    set_error( STATUS_ILLEGAL_FUNCTION ); break;
674 675
    case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
    case EIO:       set_error( STATUS_ACCESS_VIOLATION ); break;
676
    case ENOTDIR:   set_error( STATUS_NOT_A_DIRECTORY ); break;
677
    case EFBIG:     set_error( STATUS_SECTION_TOO_BIG ); break;
678 679
    case ENODEV:    set_error( STATUS_NO_SUCH_DEVICE ); break;
    case ENXIO:     set_error( STATUS_NO_SUCH_DEVICE ); break;
680
    case EXDEV:     set_error( STATUS_NOT_SAME_DEVICE ); break;
681
    case ELOOP:     set_error( STATUS_REPARSE_POINT_NOT_RESOLVED ); break;
682
#ifdef EOVERFLOW
683
    case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
684
#endif
685 686 687 688
    default:
        perror("wineserver: file_set_error() can't map error");
        set_error( STATUS_UNSUCCESSFUL );
        break;
689
    }
690 691
}

692
struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
693
{
694
    return (struct file *)get_handle_obj( process, handle, access, &file_ops );
695
}
696

697 698
int get_file_unix_fd( struct file *file )
{
699
    return get_unix_fd( file->fd );
700 701
}

702 703 704
/* create a file */
DECL_HANDLER(create_file)
{
705
    struct object *file;
706
    struct fd *root_fd = NULL;
707
    struct unicode_str unicode_name;
708
    const struct security_descriptor *sd;
709
    const struct object_attributes *objattr = get_req_object_attributes( &sd, &unicode_name, NULL );
710 711 712
    const char *name;
    data_size_t name_len;

713
    if (!objattr) return;
714 715

    /* name is transferred in the unix codepage outside of the objattr structure */
716
    if (unicode_name.len)
717 718 719 720 721
    {
        set_error( STATUS_INVALID_PARAMETER );
        return;
    }

722 723 724 725 726 727 728 729 730 731
    if (objattr->rootdir)
    {
        struct dir *root;

        if (!(root = get_dir_obj( current->process, objattr->rootdir, 0 ))) return;
        root_fd = get_obj_fd( (struct object *)root );
        release_object( root );
        if (!root_fd) return;
    }

732
    name = get_req_data_after_objattr( objattr, &name_len );
733

734
    reply->handle = 0;
735 736
    if ((file = create_file( root_fd, name, name_len, req->access, req->sharing,
                             req->create, req->options, req->attrs, sd )))
737
    {
738
        reply->handle = alloc_handle( current->process, file, req->access, objattr->attributes );
739
        release_object( file );
740
    }
741
    if (root_fd) release_object( root_fd );
742 743 744 745 746 747
}

/* allocate a file handle for a Unix fd */
DECL_HANDLER(alloc_file_handle)
{
    struct file *file;
748
    int fd;
749

750
    reply->handle = 0;
751
    if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
752
    {
753 754 755
        set_error( STATUS_INVALID_HANDLE );
        return;
    }
756
    if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
757
    {
758
        reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
759
        release_object( file );
760 761 762 763 764 765 766 767 768 769
    }
}

/* lock a region of a file */
DECL_HANDLER(lock_file)
{
    struct file *file;

    if ((file = get_file_obj( current->process, req->handle, 0 )))
    {
770
        reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
771
        reply->overlapped = is_fd_overlapped( file->fd );
772 773 774 775 776 777 778 779 780 781 782
        release_object( file );
    }
}

/* unlock a region of a file */
DECL_HANDLER(unlock_file)
{
    struct file *file;

    if ((file = get_file_obj( current->process, req->handle, 0 )))
    {
783
        unlock_fd( file->fd, req->offset, req->count );
784 785 786
        release_object( file );
    }
}