file.c 24.4 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
    struct object       obj;        /* object header */
58
    struct fd          *fd;         /* file descriptor for this file */
59
    unsigned int        access;     /* file access (FILE_READ_DATA etc.) */
60
    mode_t              mode;       /* file stat.st_mode */
61
    uid_t               uid;        /* file stat.st_uid */
62 63
};

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

66
static void file_dump( struct object *obj, int verbose );
67
static struct fd *file_get_fd( struct object *obj );
68 69
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 );
70
static void file_destroy( struct object *obj );
71 72

static int file_get_poll_events( struct fd *fd );
73
static void file_flush( struct fd *fd, struct event **event );
74
static enum server_fd_type file_get_fd_type( struct fd *fd );
75 76 77

static const struct object_ops file_ops =
{
78 79
    sizeof(struct file),          /* size */
    file_dump,                    /* dump */
80
    no_get_type,                  /* get_type */
81 82
    add_queue,                    /* add_queue */
    remove_queue,                 /* remove_queue */
83
    default_fd_signaled,          /* signaled */
84
    no_satisfied,                 /* satisfied */
85
    no_signal,                    /* signal */
86
    file_get_fd,                  /* get_fd */
87
    default_fd_map_access,        /* map_access */
88 89
    file_get_sd,                  /* get_sd */
    file_set_sd,                  /* set_sd */
90
    no_lookup_name,               /* lookup_name */
91
    no_open_file,                 /* open_file */
92
    fd_close_handle,              /* close_handle */
93 94 95 96 97
    file_destroy                  /* destroy */
};

static const struct fd_ops file_fd_ops =
{
98
    file_get_poll_events,         /* get_poll_events */
99
    default_poll_event,           /* poll_event */
100
    file_flush,                   /* flush */
101
    file_get_fd_type,             /* get_fd_type */
102
    default_fd_ioctl,             /* ioctl */
103
    default_fd_queue_async,       /* queue_async */
104
    default_fd_reselect_async,    /* reselect_async */
105
    default_fd_cancel_async       /* cancel_async */
106 107
};

108 109
static inline int is_overlapped( const struct file *file )
{
110
    return !(get_fd_options( file->fd ) & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
111 112
}

113 114
/* create a file from a file descriptor */
/* if the function fails the fd is closed */
115
struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing )
116 117
{
    struct file *file;
118 119 120 121 122 123 124
    struct stat st;

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

126
    if ((file = alloc_object( &file_ops )))
127
    {
128
        file->mode = st.st_mode;
129
        file->access = default_fd_map_access( &file->obj, access );
130 131
        if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj,
                                              FILE_SYNCHRONOUS_IO_NONALERT )))
132 133 134 135
        {
            release_object( file );
            return NULL;
        }
136
        allow_fd_caching( file->fd );
137 138 139
    }
    return file;
}
140

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
/* 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 );
        if (!(file->fd = dup_fd_object( fd, access, sharing, FILE_SYNCHRONOUS_IO_NONALERT )))
        {
            release_object( file );
            return NULL;
        }
162
        set_fd_user( file->fd, &file_fd_ops, &file->obj );
163 164 165 166
    }
    return file;
}

167
static struct object *create_file_obj( struct fd *fd, unsigned int access, mode_t mode )
168 169 170 171 172
{
    struct file *file = alloc_object( &file_ops );

    if (!file) return NULL;
    file->access  = access;
173
    file->mode    = mode;
174
    file->uid     = ~(uid_t)0;
175 176 177 178 179
    file->fd      = fd;
    grab_object( fd );
    set_fd_user( fd, &file_fd_ops, &file->obj );
    return &file->obj;
}
180

181 182 183 184
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 )
185
{
186 187
    struct object *obj = NULL;
    struct fd *fd;
188
    int flags;
189
    char *name;
190
    mode_t mode;
191

192 193 194 195 196
    if (!len || ((nameptr[0] == '/') ^ !root))
    {
        set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
        return NULL;
    }
197 198 199
    if (!(name = mem_alloc( len + 1 ))) return NULL;
    memcpy( name, nameptr, len );
    name[len] = 0;
200

201
    switch(create)
202
    {
203 204
    case FILE_CREATE:       flags = O_CREAT | O_EXCL; break;
    case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
205
                            access |= FILE_WRITE_ATTRIBUTES;
206 207 208
    case FILE_SUPERSEDE:    flags = O_CREAT | O_TRUNC; break;
    case FILE_OPEN:         flags = 0; break;
    case FILE_OPEN_IF:      flags = O_CREAT; break;
209 210
    case FILE_OVERWRITE:    flags = O_TRUNC;
                            access |= FILE_WRITE_ATTRIBUTES; break;
211
    default:                set_error( STATUS_INVALID_PARAMETER ); goto done;
212
    }
213

214 215 216 217 218 219 220 221 222
    if (sd)
    {
        const SID *owner = sd_get_owner( sd );
        if (!owner)
            owner = token_get_user( current->process->token );
        mode = sd_to_mode( sd, owner );
    }
    else
        mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
223 224 225

    if (len >= 4 &&
        (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
226 227 228 229 230 231 232 233
    {
        if (mode & S_IRUSR)
            mode |= S_IXUSR;
        if (mode & S_IRGRP)
            mode |= S_IXGRP;
        if (mode & S_IROTH)
            mode |= S_IXOTH;
    }
234

235
    access = generic_file_map_access( access );
236

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

241
    if (S_ISDIR(mode))
242
        obj = create_dir_obj( fd, access, mode );
243
    else if (S_ISCHR(mode) && is_serial_fd( fd ))
244
        obj = create_serial( fd );
245
    else
246
        obj = create_file_obj( fd, access, mode );
247

248
    release_object( fd );
249

250
done:
251
    free( name );
252
    return obj;
253 254
}

255 256 257
/* check if two file objects point to the same file */
int is_same_file( struct file *file1, struct file *file2 )
{
258
    return is_same_file_fd( file1->fd, file2->fd );
259 260
}

261 262 263 264
static void file_dump( struct object *obj, int verbose )
{
    struct file *file = (struct file *)obj;
    assert( obj->ops == &file_ops );
265
    fprintf( stderr, "File fd=%p\n", file->fd );
266 267
}

268
static int file_get_poll_events( struct fd *fd )
269
{
270
    struct file *file = get_fd_user( fd );
271
    int events = 0;
272
    assert( file->obj.ops == &file_ops );
273 274
    if (file->access & FILE_UNIX_READ_ACCESS) events |= POLLIN;
    if (file->access & FILE_UNIX_WRITE_ACCESS) events |= POLLOUT;
275
    return events;
276 277
}

278
static void file_flush( struct fd *fd, struct event **event )
279
{
280 281
    int unix_fd = get_unix_fd( fd );
    if (unix_fd != -1 && fsync( unix_fd ) == -1) file_set_error();
282 283
}

284
static enum server_fd_type file_get_fd_type( struct fd *fd )
285
{
286 287 288 289 290
    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;
291 292
}

293 294 295 296 297
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 );
298 299
}

300
static unsigned int generic_file_map_access( unsigned int access )
301 302 303 304 305 306 307 308
{
    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);
}

309
struct security_descriptor *mode_to_sd( mode_t mode, const SID *user, const SID *group )
310 311 312
{
    struct security_descriptor *sd;
    size_t dacl_size;
313
    ACE_HEADER *current_ace;
314 315 316 317 318 319 320 321 322
    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) +
        FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]);
323
    if (mode & S_IRWXU)
324 325
        dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
            FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
326 327 328
    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))))
329 330
        dacl_size += FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) +
            FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
331
    if (mode & S_IRWXO)
332 333 334 335 336 337 338
        dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
            FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]);

    sd = mem_alloc( sizeof(struct security_descriptor) +
                    FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) +
                    FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]) +
                    dacl_size );
339
    if (!sd) return sd;
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356

    sd->control = SE_DACL_PRESENT;
    sd->owner_len = FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
    sd->group_len = FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]);
    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;
357 358 359 360
    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))))
361
        dacl->AceCount++;
362 363 364 365
    dacl->Sbz2 = 0;

    /* always give FILE_ALL_ACCESS for Local System */
    aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
366
    current_ace = &aaa->Header;
367 368 369 370 371 372 373 374
    aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
    aaa->Header.AceFlags = 0;
    aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
        FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]);
    aaa->Mask = FILE_ALL_ACCESS;
    sid = (SID *)&aaa->SidStart;
    memcpy( sid, local_system_sid, FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]) );

375
    if (mode & S_IRWXU)
376 377
    {
        /* appropriate access rights for the user */
378 379
        aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
        current_ace = &aaa->Header;
380 381 382 383 384
        aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
        aaa->Header.AceFlags = 0;
        aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
                              FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
        aaa->Mask = WRITE_DAC | WRITE_OWNER;
385
        if (mode & S_IRUSR)
386
            aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
387
        if (mode & S_IWUSR)
388
            aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
389 390 391
        sid = (SID *)&aaa->SidStart;
        memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
    }
392 393 394
    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))))
395 396 397 398 399 400 401 402 403
    {
        /* 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;
        ada->Header.AceFlags = 0;
        ada->Header.AceSize = FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) +
                              FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
        ada->Mask = 0;
404
        if (!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH)))
405
            ada->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
406
        if (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IROTH)))
407
            ada->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
408 409 410 411
        ada->Mask &= ~STANDARD_RIGHTS_ALL; /* never deny standard rights */
        sid = (SID *)&ada->SidStart;
        memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
    }
412
    if (mode & S_IRWXO)
413 414
    {
        /* appropriate access rights for Everyone */
415 416
        aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
        current_ace = &aaa->Header;
417 418 419 420 421
        aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
        aaa->Header.AceFlags = 0;
        aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
                             FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]);
        aaa->Mask = 0;
422
        if (mode & S_IROTH)
423
            aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
424
        if (mode & S_IWOTH)
425
            aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
426 427 428 429
        sid = (SID *)&aaa->SidStart;
        memcpy( sid, world_sid, FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]) );
    }

430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
    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;

457 458 459 460 461 462 463
    file->mode = st.st_mode;
    file->uid = st.st_uid;
    free( obj->sd );
    obj->sd = sd;
    return sd;
}

464 465 466 467 468 469 470 471 472 473 474
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;
    if (access & FILE_WRITE_DATA) mode |= 2;
    if (access & FILE_EXECUTE)    mode |= 1;
    return mode;
}

475
mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner )
476
{
477
    mode_t new_mode = 0;
478
    mode_t denied_mode = 0;
479
    mode_t mode;
480 481
    int present;
    const ACL *dacl = sd_get_dacl( sd, &present );
482
    const SID *user = token_get_user( current->process->token );
483
    if (present && dacl)
484
    {
485 486
        const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
        ULONG i;
487
        for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
488
        {
489 490 491
            const ACCESS_ALLOWED_ACE *aa_ace;
            const ACCESS_DENIED_ACE *ad_ace;
            const SID *sid;
492

493
            if (ace->AceFlags & INHERIT_ONLY_ACE) continue;
494

495
            switch (ace->AceType)
496 497 498 499
            {
                case ACCESS_DENIED_ACE_TYPE:
                    ad_ace = (const ACCESS_DENIED_ACE *)ace;
                    sid = (const SID *)&ad_ace->SidStart;
500
                    mode = file_access_to_mode( ad_ace->Mask );
501 502
                    if (security_equal_sid( sid, security_world_sid ))
                    {
503
                        denied_mode |= (mode << 6) | (mode << 3) | mode; /* all */
504
                    }
505 506 507
                    else if ((security_equal_sid( user, owner ) &&
                              token_sid_present( current->process->token, sid, TRUE )))
                    {
508
                        denied_mode |= (mode << 6) | (mode << 3);  /* user + group */
509
                    }
510 511 512 513
                    else if (security_equal_sid( sid, owner ))
                    {
                        denied_mode |= (mode << 6);  /* user only */
                    }
514 515 516 517
                    break;
                case ACCESS_ALLOWED_ACE_TYPE:
                    aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
                    sid = (const SID *)&aa_ace->SidStart;
518
                    mode = file_access_to_mode( aa_ace->Mask );
519 520
                    if (security_equal_sid( sid, security_world_sid ))
                    {
521
                        new_mode |= (mode << 6) | (mode << 3) | mode;  /* all */
522
                    }
523 524 525
                    else if ((security_equal_sid( user, owner ) &&
                              token_sid_present( current->process->token, sid, FALSE )))
                    {
526
                        new_mode |= (mode << 6) | (mode << 3);  /* user + group */
527
                    }
528 529 530 531
                    else if (security_equal_sid( sid, owner ))
                    {
                        new_mode |= (mode << 6);  /* user only */
                    }
532 533 534
                    break;
            }
        }
535 536 537
    }
    else
        /* no ACL means full access rights to anyone */
538
        new_mode = S_IRWXU | S_IRWXG | S_IRWXO;
539 540 541

    return new_mode & ~denied_mode;
}
542

543 544 545 546 547
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;
548
    struct stat st;
549 550 551 552 553 554 555
    mode_t mode;
    int unix_fd;

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

    unix_fd = get_file_unix_fd( file );

556
    if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
557 558 559 560 561

    if (set_info & OWNER_SECURITY_INFORMATION)
    {
        owner = sd_get_owner( sd );
        if (!owner)
562
        {
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
            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 */
581
        mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
582 583
        mode |= sd_to_mode( sd, owner );

584
        if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
585
        {
586 587
            file_set_error();
            return 0;
588
        }
589 590 591 592
    }
    return 1;
}

593
static void file_destroy( struct object *obj )
594
{
595 596
    struct file *file = (struct file *)obj;
    assert( obj->ops == &file_ops );
597

598
    if (file->fd) release_object( file->fd );
599 600 601 602 603 604 605
}

/* set the last error depending on errno */
void file_set_error(void)
{
    switch (errno)
    {
606
    case ETXTBSY:
607 608 609
    case EAGAIN:    set_error( STATUS_SHARING_VIOLATION ); break;
    case EBADF:     set_error( STATUS_INVALID_HANDLE ); break;
    case ENOSPC:    set_error( STATUS_DISK_FULL ); break;
610
    case EACCES:
611
    case ESRCH:
612
    case EROFS:
613 614 615
    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;
616
    case EISDIR:    set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
617
    case ENFILE:
618
    case EMFILE:    set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
619 620
    case EEXIST:    set_error( STATUS_OBJECT_NAME_COLLISION ); break;
    case EINVAL:    set_error( STATUS_INVALID_PARAMETER ); break;
621
    case ESPIPE:    set_error( STATUS_ILLEGAL_FUNCTION ); break;
622 623
    case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
    case EIO:       set_error( STATUS_ACCESS_VIOLATION ); break;
624
    case ENOTDIR:   set_error( STATUS_NOT_A_DIRECTORY ); break;
625
    case EFBIG:     set_error( STATUS_SECTION_TOO_BIG ); break;
626 627
    case ENODEV:    set_error( STATUS_NO_SUCH_DEVICE ); break;
    case ENXIO:     set_error( STATUS_NO_SUCH_DEVICE ); break;
628
#ifdef EOVERFLOW
629
    case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
630
#endif
631 632 633 634
    default:
        perror("wineserver: file_set_error() can't map error");
        set_error( STATUS_UNSUCCESSFUL );
        break;
635
    }
636 637
}

638
struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
639
{
640
    return (struct file *)get_handle_obj( process, handle, access, &file_ops );
641
}
642

643 644
int get_file_unix_fd( struct file *file )
{
645
    return get_unix_fd( file->fd );
646 647
}

648 649 650
/* create a file */
DECL_HANDLER(create_file)
{
651
    struct object *file;
652
    struct fd *root_fd = NULL;
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
    const struct object_attributes *objattr = get_req_data();
    const struct security_descriptor *sd;
    const char *name;
    data_size_t name_len;

    reply->handle = 0;

    if (!objattr_is_valid( objattr, get_req_data_size() ))
        return;
    /* name is transferred in the unix codepage outside of the objattr structure */
    if (objattr->name_len)
    {
        set_error( STATUS_INVALID_PARAMETER );
        return;
    }

669 670 671 672 673 674 675 676 677 678
    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;
    }

679 680 681 682
    sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;

    name = (const char *)get_req_data() + sizeof(*objattr) + objattr->sd_len;
    name_len = get_req_data_size() - sizeof(*objattr) - objattr->sd_len;
683

684
    reply->handle = 0;
685 686
    if ((file = create_file( root_fd, name, name_len, req->access, req->sharing,
                             req->create, req->options, req->attrs, sd )))
687
    {
688
        reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
689
        release_object( file );
690
    }
691
    if (root_fd) release_object( root_fd );
692 693 694 695 696 697
}

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

700
    reply->handle = 0;
701
    if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
702
    {
703 704 705
        set_error( STATUS_INVALID_HANDLE );
        return;
    }
706
    if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
707
    {
708
        reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
709
        release_object( file );
710 711 712 713 714 715 716 717 718 719
    }
}

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

    if ((file = get_file_obj( current->process, req->handle, 0 )))
    {
720
        reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
721
        reply->overlapped = is_overlapped( file );
722 723 724 725 726 727 728 729 730 731 732
        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 )))
    {
733
        unlock_fd( file->fd, req->offset, req->count );
734 735 736
        release_object( file );
    }
}