1998-12-27 16:28:54 +01:00
|
|
|
/*
|
|
|
|
* Server-side file management
|
|
|
|
*
|
|
|
|
* Copyright (C) 1998 Alexandre Julliard
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* 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
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
1998-12-27 16:28:54 +01:00
|
|
|
*/
|
|
|
|
|
1999-10-25 00:13:47 +02:00
|
|
|
#include "config.h"
|
2001-05-14 22:09:37 +02:00
|
|
|
#include "wine/port.h"
|
1999-10-25 00:13:47 +02:00
|
|
|
|
1998-12-27 16:28:54 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <fcntl.h>
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
1998-12-27 16:28:54 +01:00
|
|
|
#include <stdio.h>
|
1999-02-28 13:27:56 +01:00
|
|
|
#include <string.h>
|
1998-12-27 16:28:54 +01:00
|
|
|
#include <stdlib.h>
|
1999-01-01 19:42:17 +01:00
|
|
|
#include <errno.h>
|
1999-07-10 15:16:29 +02:00
|
|
|
#ifdef HAVE_SYS_ERRNO_H
|
1998-12-27 16:28:54 +01:00
|
|
|
#include <sys/errno.h>
|
1999-07-10 15:16:29 +02:00
|
|
|
#endif
|
1998-12-27 16:28:54 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2003-02-11 23:27:13 +01:00
|
|
|
#ifdef HAVE_UTIME_H
|
1999-01-03 12:55:56 +01:00
|
|
|
#include <utime.h>
|
2003-02-11 23:27:13 +01:00
|
|
|
#endif
|
2005-03-04 13:38:36 +01:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
#include <poll.h>
|
|
|
|
#endif
|
1998-12-27 16:28:54 +01:00
|
|
|
|
2005-11-28 17:32:54 +01:00
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "windef.h"
|
2004-03-12 02:56:49 +01:00
|
|
|
#include "winternl.h"
|
1999-05-15 12:48:19 +02:00
|
|
|
|
2003-01-30 01:26:44 +01:00
|
|
|
#include "file.h"
|
1999-05-15 12:48:19 +02:00
|
|
|
#include "handle.h"
|
|
|
|
#include "thread.h"
|
1999-06-22 19:26:53 +02:00
|
|
|
#include "request.h"
|
2007-10-03 21:19:05 +02:00
|
|
|
#include "process.h"
|
|
|
|
#include "security.h"
|
1998-12-27 16:28:54 +01:00
|
|
|
|
|
|
|
struct file
|
|
|
|
{
|
1999-05-16 18:59:38 +02:00
|
|
|
struct object obj; /* object header */
|
2003-02-19 01:33:32 +01:00
|
|
|
struct fd *fd; /* file descriptor for this file */
|
2005-12-12 16:46:17 +01:00
|
|
|
unsigned int access; /* file access (FILE_READ_DATA etc.) */
|
2007-04-23 15:13:22 +02:00
|
|
|
mode_t mode; /* file stat.st_mode */
|
2007-10-03 21:19:05 +02:00
|
|
|
uid_t uid; /* file stat.st_uid */
|
1998-12-27 16:28:54 +01:00
|
|
|
};
|
|
|
|
|
2006-01-24 13:30:55 +01:00
|
|
|
static unsigned int generic_file_map_access( unsigned int access );
|
|
|
|
|
1998-12-27 16:28:54 +01:00
|
|
|
static void file_dump( struct object *obj, int verbose );
|
2003-02-19 01:33:32 +01:00
|
|
|
static struct fd *file_get_fd( struct object *obj );
|
2007-10-03 21:19:05 +02:00
|
|
|
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 );
|
1998-12-27 16:28:54 +01:00
|
|
|
static void file_destroy( struct object *obj );
|
2003-02-14 21:27:09 +01:00
|
|
|
|
|
|
|
static int file_get_poll_events( struct fd *fd );
|
2007-03-27 16:51:44 +02:00
|
|
|
static void file_flush( struct fd *fd, struct event **event );
|
2007-04-10 22:26:23 +02:00
|
|
|
static enum server_fd_type file_get_fd_type( struct fd *fd );
|
1998-12-27 16:28:54 +01:00
|
|
|
|
|
|
|
static const struct object_ops file_ops =
|
|
|
|
{
|
2000-01-01 01:56:27 +01:00
|
|
|
sizeof(struct file), /* size */
|
|
|
|
file_dump, /* dump */
|
2007-04-04 19:39:29 +02:00
|
|
|
add_queue, /* add_queue */
|
|
|
|
remove_queue, /* remove_queue */
|
2003-01-30 01:26:44 +01:00
|
|
|
default_fd_signaled, /* signaled */
|
2000-01-01 01:56:27 +01:00
|
|
|
no_satisfied, /* satisfied */
|
2005-04-24 19:35:52 +02:00
|
|
|
no_signal, /* signal */
|
2003-02-19 01:33:32 +01:00
|
|
|
file_get_fd, /* get_fd */
|
2007-10-02 14:20:15 +02:00
|
|
|
default_fd_map_access, /* map_access */
|
2007-10-03 21:19:05 +02:00
|
|
|
file_get_sd, /* get_sd */
|
|
|
|
file_set_sd, /* set_sd */
|
2005-11-22 15:55:42 +01:00
|
|
|
no_lookup_name, /* lookup_name */
|
2007-03-22 11:44:29 +01:00
|
|
|
no_open_file, /* open_file */
|
2006-11-02 20:52:22 +01:00
|
|
|
fd_close_handle, /* close_handle */
|
2003-01-30 01:26:44 +01:00
|
|
|
file_destroy /* destroy */
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct fd_ops file_fd_ops =
|
|
|
|
{
|
2000-01-01 01:56:27 +01:00
|
|
|
file_get_poll_events, /* get_poll_events */
|
2005-06-07 22:09:01 +02:00
|
|
|
default_poll_event, /* poll_event */
|
2000-01-01 01:56:27 +01:00
|
|
|
file_flush, /* flush */
|
2007-04-10 22:26:23 +02:00
|
|
|
file_get_fd_type, /* get_fd_type */
|
2007-04-16 14:45:03 +02:00
|
|
|
default_fd_ioctl, /* ioctl */
|
2005-06-07 22:09:01 +02:00
|
|
|
default_fd_queue_async, /* queue_async */
|
2007-04-10 17:07:27 +02:00
|
|
|
default_fd_reselect_async, /* reselect_async */
|
2005-06-07 22:09:01 +02:00
|
|
|
default_fd_cancel_async /* cancel_async */
|
1998-12-27 16:28:54 +01:00
|
|
|
};
|
|
|
|
|
2004-03-12 02:56:49 +01:00
|
|
|
static inline int is_overlapped( const struct file *file )
|
|
|
|
{
|
2007-04-10 22:25:07 +02:00
|
|
|
return !(get_fd_options( file->fd ) & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
|
2004-03-12 02:56:49 +01:00
|
|
|
}
|
|
|
|
|
2000-01-01 01:56:27 +01:00
|
|
|
/* create a file from a file descriptor */
|
|
|
|
/* if the function fails the fd is closed */
|
2004-03-12 02:56:49 +01:00
|
|
|
static struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing )
|
1998-12-27 16:28:54 +01:00
|
|
|
{
|
|
|
|
struct file *file;
|
2007-04-23 15:13:22 +02:00
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (fstat( fd, &st ) == -1)
|
|
|
|
{
|
|
|
|
file_set_error();
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-01-30 01:26:44 +01:00
|
|
|
|
2003-02-19 01:33:32 +01:00
|
|
|
if ((file = alloc_object( &file_ops )))
|
1999-01-03 12:55:56 +01:00
|
|
|
{
|
2007-04-23 15:13:22 +02:00
|
|
|
file->mode = st.st_mode;
|
2007-10-02 14:20:15 +02:00
|
|
|
file->access = default_fd_map_access( &file->obj, access );
|
2007-04-10 22:25:07 +02:00
|
|
|
if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj,
|
|
|
|
FILE_SYNCHRONOUS_IO_NONALERT )))
|
2003-02-19 01:33:32 +01:00
|
|
|
{
|
|
|
|
release_object( file );
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-06-22 19:26:53 +02:00
|
|
|
}
|
|
|
|
return file;
|
|
|
|
}
|
1999-01-03 12:55:56 +01:00
|
|
|
|
2007-04-23 15:13:22 +02:00
|
|
|
static struct object *create_file_obj( struct fd *fd, unsigned int access, mode_t mode )
|
2006-01-24 13:30:55 +01:00
|
|
|
{
|
|
|
|
struct file *file = alloc_object( &file_ops );
|
|
|
|
|
|
|
|
if (!file) return NULL;
|
|
|
|
file->access = access;
|
2007-04-23 15:13:22 +02:00
|
|
|
file->mode = mode;
|
2006-01-24 13:30:55 +01:00
|
|
|
file->fd = fd;
|
|
|
|
grab_object( fd );
|
|
|
|
set_fd_user( fd, &file_fd_ops, &file->obj );
|
|
|
|
return &file->obj;
|
|
|
|
}
|
1999-01-03 12:55:56 +01:00
|
|
|
|
2006-07-26 10:43:25 +02:00
|
|
|
static struct object *create_file( const char *nameptr, data_size_t len, unsigned int access,
|
2004-03-18 05:08:48 +01:00
|
|
|
unsigned int sharing, int create, unsigned int options,
|
2004-04-07 01:41:01 +02:00
|
|
|
unsigned int attrs )
|
1999-06-22 19:26:53 +02:00
|
|
|
{
|
2006-01-24 13:30:55 +01:00
|
|
|
struct object *obj = NULL;
|
|
|
|
struct fd *fd;
|
2006-01-25 15:06:48 +01:00
|
|
|
int flags;
|
1999-06-22 19:26:53 +02:00
|
|
|
char *name;
|
2001-07-23 20:09:41 +02:00
|
|
|
mode_t mode;
|
1999-01-03 12:55:56 +01:00
|
|
|
|
1999-06-22 19:26:53 +02:00
|
|
|
if (!(name = mem_alloc( len + 1 ))) return NULL;
|
|
|
|
memcpy( name, nameptr, len );
|
|
|
|
name[len] = 0;
|
1999-01-03 12:55:56 +01:00
|
|
|
|
1999-06-22 19:26:53 +02:00
|
|
|
switch(create)
|
1998-12-27 16:28:54 +01:00
|
|
|
{
|
2004-03-12 02:56:49 +01:00
|
|
|
case FILE_CREATE: flags = O_CREAT | O_EXCL; break;
|
|
|
|
case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
|
|
|
|
case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break;
|
|
|
|
case FILE_OPEN: flags = 0; break;
|
|
|
|
case FILE_OPEN_IF: flags = O_CREAT; break;
|
|
|
|
case FILE_OVERWRITE: flags = O_TRUNC; break;
|
2006-01-24 13:30:55 +01:00
|
|
|
default: set_error( STATUS_INVALID_PARAMETER ); goto done;
|
1998-12-27 16:28:54 +01:00
|
|
|
}
|
2005-01-14 20:54:38 +01:00
|
|
|
|
2001-07-23 20:09:41 +02:00
|
|
|
mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
|
|
|
|
|
|
|
|
if (len >= 4 &&
|
|
|
|
(!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
|
|
|
|
mode |= 0111;
|
1999-06-22 19:26:53 +02:00
|
|
|
|
2006-01-24 13:30:55 +01:00
|
|
|
access = generic_file_map_access( access );
|
1999-06-22 19:26:53 +02:00
|
|
|
|
2003-03-12 23:38:14 +01:00
|
|
|
/* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
|
2006-01-24 13:30:55 +01:00
|
|
|
fd = open_fd( name, flags | O_NONBLOCK | O_LARGEFILE, &mode, access, sharing, options );
|
|
|
|
if (!fd) goto done;
|
2004-04-02 21:50:49 +02:00
|
|
|
|
2006-01-27 12:13:56 +01:00
|
|
|
if (S_ISDIR(mode))
|
|
|
|
obj = create_dir_obj( fd );
|
|
|
|
else if (S_ISCHR(mode) && is_serial_fd( fd ))
|
2007-04-10 22:25:07 +02:00
|
|
|
obj = create_serial( fd );
|
2006-01-24 13:30:55 +01:00
|
|
|
else
|
2007-04-23 15:13:22 +02:00
|
|
|
obj = create_file_obj( fd, access, mode );
|
2004-03-18 05:08:48 +01:00
|
|
|
|
2006-01-24 13:30:55 +01:00
|
|
|
release_object( fd );
|
1999-06-22 19:26:53 +02:00
|
|
|
|
2006-01-24 13:30:55 +01:00
|
|
|
done:
|
1999-06-22 19:26:53 +02:00
|
|
|
free( name );
|
2006-01-24 13:30:55 +01:00
|
|
|
return obj;
|
1998-12-27 16:28:54 +01:00
|
|
|
}
|
|
|
|
|
2001-04-14 00:38:39 +02:00
|
|
|
/* check if two file objects point to the same file */
|
|
|
|
int is_same_file( struct file *file1, struct file *file2 )
|
|
|
|
{
|
2004-03-27 21:48:42 +01:00
|
|
|
return is_same_file_fd( file1->fd, file2->fd );
|
2001-04-14 00:38:39 +02:00
|
|
|
}
|
|
|
|
|
2003-03-12 23:38:14 +01:00
|
|
|
/* create a temp file for anonymous mappings */
|
|
|
|
struct file *create_temp_file( int access )
|
1999-02-28 11:13:59 +01:00
|
|
|
{
|
2003-03-12 23:38:14 +01:00
|
|
|
char tmpfn[16];
|
1999-02-28 11:13:59 +01:00
|
|
|
int fd;
|
|
|
|
|
2003-03-12 23:38:14 +01:00
|
|
|
sprintf( tmpfn, "anonmap.XXXXXX" ); /* create it in the server directory */
|
2003-03-20 22:07:49 +01:00
|
|
|
fd = mkstemps( tmpfn, 0 );
|
1999-02-28 11:13:59 +01:00
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
file_set_error();
|
2003-03-12 23:38:14 +01:00
|
|
|
return NULL;
|
1999-02-28 11:13:59 +01:00
|
|
|
}
|
2002-07-30 01:55:39 +02:00
|
|
|
unlink( tmpfn );
|
2004-03-12 02:56:49 +01:00
|
|
|
return create_file_for_fd( fd, access, 0 );
|
1999-02-28 11:13:59 +01:00
|
|
|
}
|
|
|
|
|
1998-12-27 16:28:54 +01:00
|
|
|
static void file_dump( struct object *obj, int verbose )
|
|
|
|
{
|
|
|
|
struct file *file = (struct file *)obj;
|
|
|
|
assert( obj->ops == &file_ops );
|
2007-04-10 22:25:07 +02:00
|
|
|
fprintf( stderr, "File fd=%p\n", file->fd );
|
1998-12-27 16:28:54 +01:00
|
|
|
}
|
|
|
|
|
2003-02-14 21:27:09 +01:00
|
|
|
static int file_get_poll_events( struct fd *fd )
|
1998-12-27 16:28:54 +01:00
|
|
|
{
|
2003-02-14 21:27:09 +01:00
|
|
|
struct file *file = get_fd_user( fd );
|
1999-05-16 18:59:38 +02:00
|
|
|
int events = 0;
|
2003-02-14 21:27:09 +01:00
|
|
|
assert( file->obj.ops == &file_ops );
|
2005-12-12 16:46:17 +01:00
|
|
|
if (file->access & FILE_UNIX_READ_ACCESS) events |= POLLIN;
|
|
|
|
if (file->access & FILE_UNIX_WRITE_ACCESS) events |= POLLOUT;
|
2000-01-01 01:56:27 +01:00
|
|
|
return events;
|
1998-12-27 16:28:54 +01:00
|
|
|
}
|
|
|
|
|
2007-03-27 16:51:44 +02:00
|
|
|
static void file_flush( struct fd *fd, struct event **event )
|
1998-12-27 16:28:54 +01:00
|
|
|
{
|
2007-03-27 16:51:44 +02:00
|
|
|
int unix_fd = get_unix_fd( fd );
|
|
|
|
if (unix_fd != -1 && fsync( unix_fd ) == -1) file_set_error();
|
1998-12-27 16:28:54 +01:00
|
|
|
}
|
|
|
|
|
2007-04-10 22:26:23 +02:00
|
|
|
static enum server_fd_type file_get_fd_type( struct fd *fd )
|
1999-01-03 12:55:56 +01:00
|
|
|
{
|
2007-04-23 15:13:22 +02:00
|
|
|
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;
|
1999-01-03 12:55:56 +01:00
|
|
|
}
|
|
|
|
|
2003-02-19 01:33:32 +01:00
|
|
|
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 );
|
2002-01-07 22:02:15 +01:00
|
|
|
}
|
|
|
|
|
2006-01-24 13:30:55 +01:00
|
|
|
static unsigned int generic_file_map_access( unsigned int access )
|
2005-12-12 16:46:17 +01:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2007-10-03 21:19:05 +02:00
|
|
|
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;
|
|
|
|
const SID *user;
|
|
|
|
const SID *group;
|
|
|
|
size_t dacl_size;
|
|
|
|
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;
|
|
|
|
|
|
|
|
assert( obj->ops == &file_ops );
|
|
|
|
|
|
|
|
unix_fd = get_file_unix_fd( file );
|
|
|
|
|
|
|
|
if (unix_fd == -1) return obj->sd;
|
|
|
|
|
|
|
|
if (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;
|
|
|
|
|
|
|
|
user = security_unix_uid_to_sid( st.st_uid );
|
|
|
|
group = token_get_primary_group( current->process->token );
|
|
|
|
|
|
|
|
dacl_size = sizeof(ACL) + FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
|
|
|
|
FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]);
|
|
|
|
if (st.st_mode & S_IRWXU)
|
|
|
|
dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
|
|
|
|
FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
|
|
|
|
if (st.st_mode & S_IRWXO)
|
|
|
|
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 );
|
|
|
|
if (!sd) return obj->sd;
|
|
|
|
|
|
|
|
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;
|
|
|
|
dacl->AceCount = 1 + (st.st_mode & S_IRWXU ? 1 : 0) + (st.st_mode & S_IRWXO ? 1 : 0);
|
|
|
|
dacl->Sbz2 = 0;
|
|
|
|
|
|
|
|
/* always give FILE_ALL_ACCESS for Local System */
|
|
|
|
aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
|
|
|
|
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]) );
|
|
|
|
|
|
|
|
if (st.st_mode & S_IRWXU)
|
|
|
|
{
|
|
|
|
/* appropriate access rights for the user */
|
|
|
|
aaa = (ACCESS_ALLOWED_ACE *)ace_next( &aaa->Header );
|
|
|
|
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;
|
|
|
|
if (st.st_mode & S_IRUSR)
|
|
|
|
aaa->Mask |= FILE_GENERIC_READ;
|
|
|
|
if (st.st_mode & S_IWUSR)
|
|
|
|
aaa->Mask |= FILE_GENERIC_WRITE | DELETE;
|
|
|
|
if (st.st_mode & S_IXUSR)
|
|
|
|
aaa->Mask |= FILE_GENERIC_EXECUTE;
|
|
|
|
sid = (SID *)&aaa->SidStart;
|
|
|
|
memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
|
|
|
|
}
|
|
|
|
if (st.st_mode & S_IRWXO)
|
|
|
|
{
|
|
|
|
/* appropriate access rights for Everyone */
|
|
|
|
aaa = (ACCESS_ALLOWED_ACE *)ace_next( &aaa->Header );
|
|
|
|
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;
|
|
|
|
if (st.st_mode & S_IROTH)
|
|
|
|
aaa->Mask |= FILE_GENERIC_READ;
|
|
|
|
if (st.st_mode & S_IWOTH)
|
|
|
|
aaa->Mask |= FILE_GENERIC_WRITE | DELETE;
|
|
|
|
if (st.st_mode & S_IXOTH)
|
|
|
|
aaa->Mask |= FILE_GENERIC_EXECUTE;
|
|
|
|
sid = (SID *)&aaa->SidStart;
|
|
|
|
memcpy( sid, world_sid, FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]) );
|
|
|
|
}
|
|
|
|
|
|
|
|
file->mode = st.st_mode;
|
|
|
|
file->uid = st.st_uid;
|
|
|
|
free( obj->sd );
|
|
|
|
obj->sd = sd;
|
|
|
|
return sd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int file_set_sd( struct object *obj, const struct security_descriptor *sd,
|
|
|
|
unsigned int set_info )
|
|
|
|
{
|
|
|
|
struct file *file = (struct file *)obj;
|
|
|
|
mode_t new_mode;
|
|
|
|
mode_t denied_mode = 0;
|
|
|
|
const SID *owner;
|
|
|
|
int unix_fd;
|
|
|
|
|
|
|
|
assert( obj->ops == &file_ops );
|
|
|
|
|
|
|
|
unix_fd = get_file_unix_fd( file );
|
|
|
|
|
|
|
|
if (unix_fd == -1) return 1;
|
|
|
|
|
|
|
|
if (set_info & OWNER_SECURITY_INFORMATION)
|
|
|
|
{
|
|
|
|
owner = sd_get_owner( sd );
|
|
|
|
if (!owner)
|
|
|
|
{
|
|
|
|
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 );
|
|
|
|
|
2007-10-06 14:31:25 +02:00
|
|
|
/* group and sacl not supported */
|
|
|
|
|
2007-10-03 21:19:05 +02:00
|
|
|
/* keep the bits that we don't map to access rights in the ACL */
|
|
|
|
new_mode = file->mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXG);
|
|
|
|
|
|
|
|
if (set_info & DACL_SECURITY_INFORMATION)
|
|
|
|
{
|
|
|
|
if (sd->control & SE_DACL_PRESENT)
|
|
|
|
{
|
|
|
|
const ACL *dacl = (const ACL *)((char *)sd + sd->owner_len + sd->group_len + sd->sacl_len);
|
|
|
|
const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
|
|
|
|
ULONG i;
|
|
|
|
for (i = 0; i < dacl->AceCount; i++)
|
|
|
|
{
|
|
|
|
const ACCESS_ALLOWED_ACE *aa_ace;
|
|
|
|
const ACCESS_DENIED_ACE *ad_ace;
|
|
|
|
const SID *sid;
|
|
|
|
switch (ace->AceType)
|
|
|
|
{
|
|
|
|
case ACCESS_DENIED_ACE_TYPE:
|
|
|
|
ad_ace = (const ACCESS_DENIED_ACE *)ace;
|
|
|
|
sid = (const SID *)&ad_ace->SidStart;
|
|
|
|
if (security_equal_sid( sid, security_world_sid ))
|
|
|
|
{
|
|
|
|
unsigned int access = generic_file_map_access( ad_ace->Mask );
|
|
|
|
if (access & FILE_READ_DATA)
|
|
|
|
denied_mode |= S_IROTH;
|
|
|
|
if (access & FILE_WRITE_DATA)
|
|
|
|
denied_mode |= S_IWOTH;
|
|
|
|
if (access & FILE_EXECUTE)
|
|
|
|
denied_mode |= S_IXOTH;
|
|
|
|
}
|
|
|
|
else if (security_equal_sid( sid, owner ))
|
|
|
|
{
|
|
|
|
unsigned int access = generic_file_map_access( ad_ace->Mask );
|
|
|
|
if (access & FILE_READ_DATA)
|
|
|
|
denied_mode |= S_IRUSR;
|
|
|
|
if (access & FILE_WRITE_DATA)
|
|
|
|
denied_mode |= S_IWUSR;
|
|
|
|
if (access & FILE_EXECUTE)
|
|
|
|
denied_mode |= S_IXUSR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ACCESS_ALLOWED_ACE_TYPE:
|
|
|
|
aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
|
|
|
|
sid = (const SID *)&aa_ace->SidStart;
|
|
|
|
if (security_equal_sid( sid, security_world_sid ))
|
|
|
|
{
|
|
|
|
unsigned int access = generic_file_map_access( aa_ace->Mask );
|
|
|
|
if (access & FILE_READ_DATA)
|
|
|
|
new_mode |= S_IROTH;
|
|
|
|
if (access & FILE_WRITE_DATA)
|
|
|
|
new_mode |= S_IWOTH;
|
|
|
|
if (access & FILE_EXECUTE)
|
|
|
|
new_mode |= S_IXOTH;
|
|
|
|
}
|
|
|
|
else if (security_equal_sid( sid, owner ))
|
|
|
|
{
|
|
|
|
unsigned int access = generic_file_map_access( aa_ace->Mask );
|
|
|
|
if (access & FILE_READ_DATA)
|
|
|
|
new_mode |= S_IRUSR;
|
|
|
|
if (access & FILE_WRITE_DATA)
|
|
|
|
new_mode |= S_IWUSR;
|
|
|
|
if (access & FILE_EXECUTE)
|
|
|
|
new_mode |= S_IXUSR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ace = ace_next( ace );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* no ACL means full access rights to anyone */
|
|
|
|
new_mode |= S_IRWXU | S_IRWXO;
|
|
|
|
|
2007-10-06 14:31:34 +02:00
|
|
|
if (file->mode != (new_mode & ~denied_mode))
|
2007-10-03 21:19:05 +02:00
|
|
|
{
|
2007-10-06 14:31:34 +02:00
|
|
|
if (fchmod( unix_fd, new_mode & ~denied_mode ) == -1)
|
|
|
|
{
|
|
|
|
file_set_error();
|
|
|
|
return 0;
|
|
|
|
}
|
2007-10-03 21:19:05 +02:00
|
|
|
|
2007-10-06 14:31:34 +02:00
|
|
|
file->mode = new_mode & ~denied_mode;
|
|
|
|
}
|
2007-10-03 21:19:05 +02:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1998-12-30 13:06:45 +01:00
|
|
|
static void file_destroy( struct object *obj )
|
1998-12-27 16:28:54 +01:00
|
|
|
{
|
1998-12-30 13:06:45 +01:00
|
|
|
struct file *file = (struct file *)obj;
|
|
|
|
assert( obj->ops == &file_ops );
|
1999-01-03 12:55:56 +01:00
|
|
|
|
2003-02-19 01:33:32 +01:00
|
|
|
if (file->fd) release_object( file->fd );
|
1998-12-30 13:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set the last error depending on errno */
|
|
|
|
void file_set_error(void)
|
|
|
|
{
|
|
|
|
switch (errno)
|
|
|
|
{
|
2007-09-11 06:07:29 +02:00
|
|
|
case ETXTBSY:
|
2000-01-24 22:58:06 +01:00
|
|
|
case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
|
|
|
|
case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
|
|
|
|
case ENOSPC: set_error( STATUS_DISK_FULL ); break;
|
1998-12-30 13:06:45 +01:00
|
|
|
case EACCES:
|
2001-07-14 02:50:30 +02:00
|
|
|
case ESRCH:
|
2000-01-24 22:58:06 +01:00
|
|
|
case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
|
|
|
|
case EROFS: set_error( STATUS_MEDIA_WRITE_PROTECTED ); break;
|
|
|
|
case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
|
|
|
|
case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
|
2004-04-17 01:32:40 +02:00
|
|
|
case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
|
1998-12-30 13:06:45 +01:00
|
|
|
case ENFILE:
|
2006-08-29 22:11:28 +02:00
|
|
|
case EMFILE: set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
|
2000-01-24 22:58:06 +01:00
|
|
|
case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
|
|
|
|
case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
|
2007-01-08 04:16:59 +01:00
|
|
|
case ESPIPE: set_error( STATUS_ILLEGAL_FUNCTION ); break;
|
2000-01-24 22:58:06 +01:00
|
|
|
case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
|
|
|
|
case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
|
2004-03-12 02:56:49 +01:00
|
|
|
case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
|
2004-05-01 04:50:06 +02:00
|
|
|
case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
|
2004-07-06 21:42:09 +02:00
|
|
|
case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break;
|
|
|
|
case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break;
|
2004-01-02 21:11:35 +01:00
|
|
|
#ifdef EOVERFLOW
|
2003-03-18 06:04:33 +01:00
|
|
|
case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
|
2004-01-02 21:11:35 +01:00
|
|
|
#endif
|
2007-09-04 15:51:39 +02:00
|
|
|
default:
|
|
|
|
perror("wineserver: file_set_error() can't map error");
|
|
|
|
set_error( STATUS_UNSUCCESSFUL );
|
|
|
|
break;
|
1998-12-30 13:06:45 +01:00
|
|
|
}
|
1998-12-27 16:28:54 +01:00
|
|
|
}
|
|
|
|
|
2002-05-30 22:12:58 +02:00
|
|
|
struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
|
1998-12-27 16:28:54 +01:00
|
|
|
{
|
1999-11-29 03:17:08 +01:00
|
|
|
return (struct file *)get_handle_obj( process, handle, access, &file_ops );
|
1999-01-01 17:59:27 +01:00
|
|
|
}
|
1998-12-27 16:28:54 +01:00
|
|
|
|
2003-02-14 21:27:09 +01:00
|
|
|
int get_file_unix_fd( struct file *file )
|
|
|
|
{
|
2003-02-19 01:33:32 +01:00
|
|
|
return get_unix_fd( file->fd );
|
2003-02-14 21:27:09 +01:00
|
|
|
}
|
|
|
|
|
2007-01-12 14:42:43 +01:00
|
|
|
struct file *grab_file_unless_removable( struct file *file )
|
|
|
|
{
|
|
|
|
if (is_fd_removable( file->fd )) return NULL;
|
|
|
|
return (struct file *)grab_object( file );
|
|
|
|
}
|
|
|
|
|
2002-01-29 03:51:12 +01:00
|
|
|
/* extend a file beyond the current end of file */
|
2005-04-19 13:59:13 +02:00
|
|
|
static int extend_file( struct file *file, file_pos_t new_size )
|
1998-12-30 13:06:45 +01:00
|
|
|
{
|
2002-01-29 03:51:12 +01:00
|
|
|
static const char zero;
|
2003-02-14 21:27:09 +01:00
|
|
|
int unix_fd = get_file_unix_fd( file );
|
2005-04-19 13:59:13 +02:00
|
|
|
off_t size = new_size;
|
1998-12-30 13:06:45 +01:00
|
|
|
|
2005-08-08 17:11:03 +02:00
|
|
|
if (unix_fd == -1) return 0;
|
|
|
|
|
2005-04-19 13:59:13 +02:00
|
|
|
if (sizeof(new_size) > sizeof(size) && size != new_size)
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return 0;
|
|
|
|
}
|
2002-01-29 03:51:12 +01:00
|
|
|
/* extend the file one byte beyond the requested size and then truncate it */
|
|
|
|
/* this should work around ftruncate implementations that can't extend files */
|
2004-05-01 04:50:06 +02:00
|
|
|
if (pwrite( unix_fd, &zero, 1, size ) != -1)
|
1998-12-30 13:06:45 +01:00
|
|
|
{
|
2003-01-30 01:26:44 +01:00
|
|
|
ftruncate( unix_fd, size );
|
2002-01-29 03:51:12 +01:00
|
|
|
return 1;
|
1998-12-30 13:06:45 +01:00
|
|
|
}
|
2002-01-29 03:51:12 +01:00
|
|
|
file_set_error();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-02-28 11:13:59 +01:00
|
|
|
/* try to grow the file to the specified size */
|
2005-04-19 13:59:13 +02:00
|
|
|
int grow_file( struct file *file, file_pos_t size )
|
1999-02-28 11:13:59 +01:00
|
|
|
{
|
2001-05-29 22:55:21 +02:00
|
|
|
struct stat st;
|
2003-02-14 21:27:09 +01:00
|
|
|
int unix_fd = get_file_unix_fd( file );
|
1999-02-28 11:13:59 +01:00
|
|
|
|
2005-08-08 17:11:03 +02:00
|
|
|
if (unix_fd == -1) return 0;
|
|
|
|
|
2003-01-30 01:26:44 +01:00
|
|
|
if (fstat( unix_fd, &st ) == -1)
|
1999-02-28 11:13:59 +01:00
|
|
|
{
|
|
|
|
file_set_error();
|
|
|
|
return 0;
|
|
|
|
}
|
2001-05-14 22:09:37 +02:00
|
|
|
if (st.st_size >= size) return 1; /* already large enough */
|
2004-05-01 04:50:06 +02:00
|
|
|
return extend_file( file, size );
|
1999-02-28 11:13:59 +01:00
|
|
|
}
|
|
|
|
|
1999-05-15 12:48:19 +02:00
|
|
|
/* create a file */
|
|
|
|
DECL_HANDLER(create_file)
|
|
|
|
{
|
2004-03-18 05:08:48 +01:00
|
|
|
struct object *file;
|
1999-05-15 12:48:19 +02:00
|
|
|
|
2001-11-30 19:46:42 +01:00
|
|
|
reply->handle = 0;
|
|
|
|
if ((file = create_file( get_req_data(), get_req_data_size(), req->access,
|
2004-04-07 01:41:01 +02:00
|
|
|
req->sharing, req->create, req->options, req->attrs )))
|
1999-05-15 12:48:19 +02:00
|
|
|
{
|
2005-12-09 13:58:25 +01:00
|
|
|
reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
|
1999-06-26 10:43:26 +02:00
|
|
|
release_object( file );
|
1999-06-22 19:26:53 +02:00
|
|
|
}
|
1999-06-26 10:43:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate a file handle for a Unix fd */
|
|
|
|
DECL_HANDLER(alloc_file_handle)
|
|
|
|
{
|
|
|
|
struct file *file;
|
2001-02-28 22:45:23 +01:00
|
|
|
int fd;
|
1999-06-26 10:43:26 +02:00
|
|
|
|
2001-11-30 19:46:42 +01:00
|
|
|
reply->handle = 0;
|
2001-02-28 22:45:23 +01:00
|
|
|
if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
|
1999-06-22 19:26:53 +02:00
|
|
|
{
|
2001-02-28 22:45:23 +01:00
|
|
|
set_error( STATUS_INVALID_HANDLE );
|
|
|
|
return;
|
|
|
|
}
|
2004-03-12 02:56:49 +01:00
|
|
|
if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
|
2001-02-28 22:45:23 +01:00
|
|
|
{
|
2005-12-09 13:58:25 +01:00
|
|
|
reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
|
2001-02-28 22:45:23 +01:00
|
|
|
release_object( file );
|
1999-05-15 12:48:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* lock a region of a file */
|
|
|
|
DECL_HANDLER(lock_file)
|
|
|
|
{
|
|
|
|
struct file *file;
|
|
|
|
|
|
|
|
if ((file = get_file_obj( current->process, req->handle, 0 )))
|
|
|
|
{
|
2007-10-10 14:06:25 +02:00
|
|
|
reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
|
2004-03-12 02:56:49 +01:00
|
|
|
reply->overlapped = is_overlapped( file );
|
1999-05-15 12:48:19 +02:00
|
|
|
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 )))
|
|
|
|
{
|
2007-10-10 14:06:25 +02:00
|
|
|
unlock_fd( file->fd, req->offset, req->count );
|
1999-05-15 12:48:19 +02:00
|
|
|
release_object( file );
|
|
|
|
}
|
|
|
|
}
|