1998-12-27 15:28:54 +00:00
|
|
|
/*
|
|
|
|
* Server-side file management
|
|
|
|
*
|
|
|
|
* Copyright (C) 1998 Alexandre Julliard
|
2002-03-09 23:29:33 +00: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 12:49:52 +00:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
1998-12-27 15:28:54 +00:00
|
|
|
*/
|
|
|
|
|
1999-10-24 22:13:47 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
1998-12-27 15:28:54 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <fcntl.h>
|
2003-09-05 23:08:26 +00:00
|
|
|
#include <stdarg.h>
|
1998-12-27 15:28:54 +00:00
|
|
|
#include <stdio.h>
|
1999-02-28 12:27:56 +00:00
|
|
|
#include <string.h>
|
1998-12-27 15:28:54 +00:00
|
|
|
#include <stdlib.h>
|
1999-01-01 18:42:17 +00:00
|
|
|
#include <errno.h>
|
1998-12-27 15:28:54 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2003-02-11 22:27:13 +00:00
|
|
|
#ifdef HAVE_UTIME_H
|
1999-01-03 11:55:56 +00:00
|
|
|
#include <utime.h>
|
2003-02-11 22:27:13 +00:00
|
|
|
#endif
|
2005-03-04 12:38:36 +00:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
#include <poll.h>
|
|
|
|
#endif
|
1998-12-27 15:28:54 +00:00
|
|
|
|
2005-11-28 16:32:54 +00:00
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
2003-09-05 23:08:26 +00:00
|
|
|
#include "windef.h"
|
2004-03-12 01:56:49 +00:00
|
|
|
#include "winternl.h"
|
1999-05-15 10:48:19 +00:00
|
|
|
|
2003-01-30 00:26:44 +00:00
|
|
|
#include "file.h"
|
1999-05-15 10:48:19 +00:00
|
|
|
#include "handle.h"
|
|
|
|
#include "thread.h"
|
1999-06-22 17:26:53 +00:00
|
|
|
#include "request.h"
|
2007-10-03 19:19:05 +00:00
|
|
|
#include "process.h"
|
|
|
|
#include "security.h"
|
1998-12-27 15:28:54 +00:00
|
|
|
|
2021-02-04 12:44:18 +00:00
|
|
|
static const WCHAR file_name[] = {'F','i','l','e'};
|
|
|
|
|
|
|
|
struct type_descr file_type =
|
|
|
|
{
|
|
|
|
{ file_name, sizeof(file_name) }, /* name */
|
2021-02-05 11:08:42 +00:00
|
|
|
FILE_ALL_ACCESS, /* valid_access */
|
|
|
|
{ /* mapping */
|
|
|
|
FILE_GENERIC_READ,
|
|
|
|
FILE_GENERIC_WRITE,
|
|
|
|
FILE_GENERIC_EXECUTE,
|
|
|
|
FILE_ALL_ACCESS
|
|
|
|
},
|
2021-02-04 12:44:18 +00:00
|
|
|
};
|
|
|
|
|
1998-12-27 15:28:54 +00:00
|
|
|
struct file
|
|
|
|
{
|
2019-04-23 14:23:33 +00:00
|
|
|
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 */
|
1998-12-27 15:28:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void file_dump( struct object *obj, int verbose );
|
2003-02-19 00:33:32 +00:00
|
|
|
static struct fd *file_get_fd( struct object *obj );
|
2007-10-03 19:19:05 +00: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 );
|
2020-11-25 18:42:42 +00:00
|
|
|
static struct object *file_lookup_name( struct object *obj, struct unicode_str *name,
|
|
|
|
unsigned int attr, struct object *root );
|
2015-08-21 05:26:44 +00:00
|
|
|
static struct object *file_open_file( struct object *obj, unsigned int access,
|
|
|
|
unsigned int sharing, unsigned int options );
|
2019-04-23 14:23:33 +00:00
|
|
|
static struct list *file_get_kernel_obj_list( struct object *obj );
|
1998-12-27 15:28:54 +00:00
|
|
|
static void file_destroy( struct object *obj );
|
2003-02-14 20:27:09 +00:00
|
|
|
|
2007-04-10 20:26:23 +00:00
|
|
|
static enum server_fd_type file_get_fd_type( struct fd *fd );
|
1998-12-27 15:28:54 +00:00
|
|
|
|
|
|
|
static const struct object_ops file_ops =
|
|
|
|
{
|
2000-01-01 00:56:27 +00:00
|
|
|
sizeof(struct file), /* size */
|
2021-02-04 12:44:18 +00:00
|
|
|
&file_type, /* type */
|
2000-01-01 00:56:27 +00:00
|
|
|
file_dump, /* dump */
|
2007-04-04 17:39:29 +00:00
|
|
|
add_queue, /* add_queue */
|
|
|
|
remove_queue, /* remove_queue */
|
2003-01-30 00:26:44 +00:00
|
|
|
default_fd_signaled, /* signaled */
|
2000-01-01 00:56:27 +00:00
|
|
|
no_satisfied, /* satisfied */
|
2005-04-24 17:35:52 +00:00
|
|
|
no_signal, /* signal */
|
2003-02-19 00:33:32 +00:00
|
|
|
file_get_fd, /* get_fd */
|
2021-02-05 11:10:44 +00:00
|
|
|
default_map_access, /* map_access */
|
2007-10-03 19:19:05 +00:00
|
|
|
file_get_sd, /* get_sd */
|
|
|
|
file_set_sd, /* set_sd */
|
2020-09-22 14:52:31 +00:00
|
|
|
no_get_full_name, /* get_full_name */
|
2016-02-09 11:19:34 +00:00
|
|
|
file_lookup_name, /* lookup_name */
|
2016-02-04 12:07:19 +00:00
|
|
|
no_link_name, /* link_name */
|
|
|
|
NULL, /* unlink_name */
|
2015-08-21 05:26:44 +00:00
|
|
|
file_open_file, /* open_file */
|
2019-04-23 14:23:33 +00:00
|
|
|
file_get_kernel_obj_list, /* get_kernel_obj_list */
|
2021-03-23 04:04:30 +00:00
|
|
|
no_close_handle, /* close_handle */
|
2003-01-30 00:26:44 +00:00
|
|
|
file_destroy /* destroy */
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct fd_ops file_fd_ops =
|
|
|
|
{
|
2021-01-22 00:16:41 +00:00
|
|
|
default_fd_get_poll_events, /* get_poll_events */
|
2005-06-07 20:09:01 +00:00
|
|
|
default_poll_event, /* poll_event */
|
2007-04-10 20:26:23 +00:00
|
|
|
file_get_fd_type, /* get_fd_type */
|
2015-05-05 03:17:45 +00:00
|
|
|
no_fd_read, /* read */
|
|
|
|
no_fd_write, /* write */
|
2020-06-09 17:00:07 +00:00
|
|
|
no_fd_flush, /* flush */
|
2018-10-23 15:45:06 +00:00
|
|
|
default_fd_get_file_info, /* get_file_info */
|
2017-10-02 14:42:16 +00:00
|
|
|
no_fd_get_volume_info, /* get_volume_info */
|
2007-04-16 12:45:03 +00:00
|
|
|
default_fd_ioctl, /* ioctl */
|
2021-09-04 04:11:38 +00:00
|
|
|
default_fd_cancel_async, /* cancel_async */
|
2005-06-07 20:09:01 +00:00
|
|
|
default_fd_queue_async, /* queue_async */
|
2016-12-01 11:10:34 +00:00
|
|
|
default_fd_reselect_async /* reselect_async */
|
1998-12-27 15:28:54 +00:00
|
|
|
};
|
|
|
|
|
2000-01-01 00:56:27 +00:00
|
|
|
/* create a file from a file descriptor */
|
|
|
|
/* if the function fails the fd is closed */
|
2009-11-23 15:20:15 +00:00
|
|
|
struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing )
|
1998-12-27 15:28:54 +00:00
|
|
|
{
|
|
|
|
struct file *file;
|
2007-04-23 13:13:22 +00:00
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (fstat( fd, &st ) == -1)
|
|
|
|
{
|
|
|
|
file_set_error();
|
2014-11-17 18:19:08 +00:00
|
|
|
close( fd );
|
2007-04-23 13:13:22 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2003-01-30 00:26:44 +00:00
|
|
|
|
2014-11-17 18:19:08 +00:00
|
|
|
if (!(file = alloc_object( &file_ops )))
|
1999-01-03 11:55:56 +00:00
|
|
|
{
|
2014-11-17 18:19:08 +00:00
|
|
|
close( fd );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
file->mode = st.st_mode;
|
2021-02-05 11:10:44 +00:00
|
|
|
file->access = default_map_access( &file->obj, access );
|
2019-04-23 14:23:33 +00:00
|
|
|
list_init( &file->kernel_object );
|
2014-11-17 18:19:08 +00:00
|
|
|
if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj,
|
|
|
|
FILE_SYNCHRONOUS_IO_NONALERT )))
|
|
|
|
{
|
|
|
|
release_object( file );
|
|
|
|
return NULL;
|
1999-06-22 17:26:53 +00:00
|
|
|
}
|
2014-11-17 18:19:08 +00:00
|
|
|
allow_fd_caching( file->fd );
|
1999-06-22 17:26:53 +00:00
|
|
|
return file;
|
|
|
|
}
|
1999-01-03 11:55:56 +00:00
|
|
|
|
2011-04-18 11:41:54 +00:00
|
|
|
/* 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;
|
2021-02-05 11:10:44 +00:00
|
|
|
file->access = default_map_access( &file->obj, access );
|
2019-04-23 14:23:33 +00:00
|
|
|
list_init( &file->kernel_object );
|
2011-04-18 11:41:54 +00:00
|
|
|
if (!(file->fd = dup_fd_object( fd, access, sharing, FILE_SYNCHRONOUS_IO_NONALERT )))
|
|
|
|
{
|
|
|
|
release_object( file );
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-04-22 15:07:45 +00:00
|
|
|
set_fd_user( file->fd, &file_fd_ops, &file->obj );
|
2011-04-18 11:41:54 +00:00
|
|
|
}
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2007-04-23 13:13:22 +00:00
|
|
|
static struct object *create_file_obj( struct fd *fd, unsigned int access, mode_t mode )
|
2006-01-24 12:30:55 +00:00
|
|
|
{
|
|
|
|
struct file *file = alloc_object( &file_ops );
|
|
|
|
|
|
|
|
if (!file) return NULL;
|
|
|
|
file->access = access;
|
2007-04-23 13:13:22 +00:00
|
|
|
file->mode = mode;
|
2009-08-17 15:18:54 +00:00
|
|
|
file->uid = ~(uid_t)0;
|
2006-01-24 12:30:55 +00:00
|
|
|
file->fd = fd;
|
2019-04-23 14:23:33 +00:00
|
|
|
list_init( &file->kernel_object );
|
2006-01-24 12:30:55 +00:00
|
|
|
grab_object( fd );
|
|
|
|
set_fd_user( fd, &file_fd_ops, &file->obj );
|
|
|
|
return &file->obj;
|
|
|
|
}
|
1999-01-03 11:55:56 +00:00
|
|
|
|
2020-03-11 03:38:25 +00:00
|
|
|
int is_file_executable( const char *name )
|
|
|
|
{
|
|
|
|
int len = strlen( name );
|
|
|
|
return len >= 4 && (!strcasecmp( name + len - 4, ".exe") || !strcasecmp( name + len - 4, ".com" ));
|
|
|
|
}
|
|
|
|
|
2009-12-01 16:38:24 +00:00
|
|
|
static struct object *create_file( struct fd *root, const char *nameptr, data_size_t len,
|
2021-02-09 13:11:07 +00:00
|
|
|
struct unicode_str nt_name,
|
2009-12-01 16:38:24 +00:00
|
|
|
unsigned int access, unsigned int sharing, int create,
|
|
|
|
unsigned int options, unsigned int attrs,
|
|
|
|
const struct security_descriptor *sd )
|
1999-06-22 17:26:53 +00:00
|
|
|
{
|
2006-01-24 12:30:55 +00:00
|
|
|
struct object *obj = NULL;
|
|
|
|
struct fd *fd;
|
2006-01-25 14:06:48 +00:00
|
|
|
int flags;
|
1999-06-22 17:26:53 +00:00
|
|
|
char *name;
|
2001-07-23 18:09:41 +00:00
|
|
|
mode_t mode;
|
1999-01-03 11:55:56 +00:00
|
|
|
|
2021-02-09 13:11:07 +00:00
|
|
|
if (!len || ((nameptr[0] == '/') ^ !root) || (nt_name.len && ((nt_name.str[0] == '\\') ^ !root)))
|
2009-12-01 16:38:24 +00:00
|
|
|
{
|
|
|
|
set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-06-22 17:26:53 +00:00
|
|
|
if (!(name = mem_alloc( len + 1 ))) return NULL;
|
|
|
|
memcpy( name, nameptr, len );
|
|
|
|
name[len] = 0;
|
1999-01-03 11:55:56 +00:00
|
|
|
|
1999-06-22 17:26:53 +00:00
|
|
|
switch(create)
|
1998-12-27 15:28:54 +00:00
|
|
|
{
|
2004-03-12 01:56:49 +00: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 */
|
2011-03-30 09:08:49 +00:00
|
|
|
access |= FILE_WRITE_ATTRIBUTES;
|
2004-03-12 01:56:49 +00:00
|
|
|
case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break;
|
|
|
|
case FILE_OPEN: flags = 0; break;
|
|
|
|
case FILE_OPEN_IF: flags = O_CREAT; break;
|
2011-03-30 09:08:49 +00:00
|
|
|
case FILE_OVERWRITE: flags = O_TRUNC;
|
|
|
|
access |= FILE_WRITE_ATTRIBUTES; break;
|
2006-01-24 12:30:55 +00:00
|
|
|
default: set_error( STATUS_INVALID_PARAMETER ); goto done;
|
1998-12-27 15:28:54 +00:00
|
|
|
}
|
2005-01-14 19:54:38 +00:00
|
|
|
|
2007-10-26 16:02:16 +00:00
|
|
|
if (sd)
|
|
|
|
{
|
|
|
|
const SID *owner = sd_get_owner( sd );
|
|
|
|
if (!owner)
|
|
|
|
owner = token_get_user( current->process->token );
|
|
|
|
mode = sd_to_mode( sd, owner );
|
|
|
|
}
|
2013-11-08 19:04:56 +00:00
|
|
|
else if (options & FILE_DIRECTORY_FILE)
|
|
|
|
mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0555 : 0777;
|
2007-10-26 16:02:16 +00:00
|
|
|
else
|
|
|
|
mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
|
2001-07-23 18:09:41 +00:00
|
|
|
|
2020-03-11 03:38:25 +00:00
|
|
|
if (is_file_executable( name ))
|
2007-10-26 16:02:16 +00:00
|
|
|
{
|
|
|
|
if (mode & S_IRUSR)
|
|
|
|
mode |= S_IXUSR;
|
|
|
|
if (mode & S_IRGRP)
|
|
|
|
mode |= S_IXGRP;
|
|
|
|
if (mode & S_IROTH)
|
|
|
|
mode |= S_IXOTH;
|
|
|
|
}
|
1999-06-22 17:26:53 +00:00
|
|
|
|
2021-02-05 11:10:44 +00:00
|
|
|
access = map_access( access, &file_type.mapping );
|
1999-06-22 17:26:53 +00:00
|
|
|
|
2003-03-12 22:38:14 +00:00
|
|
|
/* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
|
2021-10-06 08:17:12 +00:00
|
|
|
fd = open_fd( root, name, nt_name, flags | O_NONBLOCK, &mode, access, sharing, options );
|
2006-01-24 12:30:55 +00:00
|
|
|
if (!fd) goto done;
|
2004-04-02 19:50:49 +00:00
|
|
|
|
2006-01-27 11:13:56 +00:00
|
|
|
if (S_ISDIR(mode))
|
2009-08-17 15:18:54 +00:00
|
|
|
obj = create_dir_obj( fd, access, mode );
|
2006-01-27 11:13:56 +00:00
|
|
|
else if (S_ISCHR(mode) && is_serial_fd( fd ))
|
2007-04-10 20:25:07 +00:00
|
|
|
obj = create_serial( fd );
|
2006-01-24 12:30:55 +00:00
|
|
|
else
|
2007-04-23 13:13:22 +00:00
|
|
|
obj = create_file_obj( fd, access, mode );
|
2004-03-18 04:08:48 +00:00
|
|
|
|
2006-01-24 12:30:55 +00:00
|
|
|
release_object( fd );
|
1999-06-22 17:26:53 +00:00
|
|
|
|
2006-01-24 12:30:55 +00:00
|
|
|
done:
|
1999-06-22 17:26:53 +00:00
|
|
|
free( name );
|
2006-01-24 12:30:55 +00:00
|
|
|
return obj;
|
1998-12-27 15:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void file_dump( struct object *obj, int verbose )
|
|
|
|
{
|
|
|
|
struct file *file = (struct file *)obj;
|
|
|
|
assert( obj->ops == &file_ops );
|
2007-04-10 20:25:07 +00:00
|
|
|
fprintf( stderr, "File fd=%p\n", file->fd );
|
1998-12-27 15:28:54 +00:00
|
|
|
}
|
|
|
|
|
2007-04-10 20:26:23 +00:00
|
|
|
static enum server_fd_type file_get_fd_type( struct fd *fd )
|
1999-01-03 11:55:56 +00:00
|
|
|
{
|
2007-04-23 13:13:22 +00: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 11:55:56 +00:00
|
|
|
}
|
|
|
|
|
2003-02-19 00:33:32 +00: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 21:02:15 +00:00
|
|
|
}
|
|
|
|
|
2008-11-03 22:25:37 +00:00
|
|
|
struct security_descriptor *mode_to_sd( mode_t mode, const SID *user, const SID *group )
|
2007-10-03 19:19:05 +00:00
|
|
|
{
|
|
|
|
struct security_descriptor *sd;
|
|
|
|
size_t dacl_size;
|
2007-11-20 17:49:07 +00:00
|
|
|
ACE_HEADER *current_ace;
|
2007-10-03 19:19:05 +00:00
|
|
|
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) +
|
2013-04-11 10:52:08 +00:00
|
|
|
security_sid_len( local_system_sid );
|
2008-11-03 22:25:37 +00:00
|
|
|
if (mode & S_IRWXU)
|
2013-04-11 10:52:08 +00:00
|
|
|
dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
|
2008-11-03 22:25:37 +00:00
|
|
|
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))))
|
2013-04-11 10:52:08 +00:00
|
|
|
dacl_size += FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
|
2008-11-03 22:25:37 +00:00
|
|
|
if (mode & S_IRWXO)
|
2013-04-11 10:52:08 +00:00
|
|
|
dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
|
2007-10-03 19:19:05 +00:00
|
|
|
|
|
|
|
sd = mem_alloc( sizeof(struct security_descriptor) +
|
2013-04-11 10:52:08 +00:00
|
|
|
security_sid_len( user ) + security_sid_len( group ) +
|
2007-10-03 19:19:05 +00:00
|
|
|
dacl_size );
|
2008-11-03 22:25:37 +00:00
|
|
|
if (!sd) return sd;
|
2007-10-03 19:19:05 +00:00
|
|
|
|
|
|
|
sd->control = SE_DACL_PRESENT;
|
2013-04-11 10:52:08 +00:00
|
|
|
sd->owner_len = security_sid_len( user );
|
|
|
|
sd->group_len = security_sid_len( group );
|
2007-10-03 19:19:05 +00:00
|
|
|
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;
|
2008-11-03 22:25:37 +00:00
|
|
|
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))))
|
2007-11-20 17:49:07 +00:00
|
|
|
dacl->AceCount++;
|
2007-10-03 19:19:05 +00:00
|
|
|
dacl->Sbz2 = 0;
|
|
|
|
|
|
|
|
/* always give FILE_ALL_ACCESS for Local System */
|
|
|
|
aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
|
2007-11-20 17:49:07 +00:00
|
|
|
current_ace = &aaa->Header;
|
2007-10-03 19:19:05 +00:00
|
|
|
aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
|
2015-03-27 10:11:40 +00:00
|
|
|
aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
|
2013-04-11 10:52:08 +00:00
|
|
|
aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( local_system_sid );
|
2007-10-03 19:19:05 +00:00
|
|
|
aaa->Mask = FILE_ALL_ACCESS;
|
|
|
|
sid = (SID *)&aaa->SidStart;
|
2013-04-11 10:52:08 +00:00
|
|
|
memcpy( sid, local_system_sid, security_sid_len( local_system_sid ));
|
2007-10-03 19:19:05 +00:00
|
|
|
|
2008-11-03 22:25:37 +00:00
|
|
|
if (mode & S_IRWXU)
|
2007-10-03 19:19:05 +00:00
|
|
|
{
|
|
|
|
/* appropriate access rights for the user */
|
2007-11-20 17:49:07 +00:00
|
|
|
aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
|
|
|
|
current_ace = &aaa->Header;
|
2007-10-03 19:19:05 +00:00
|
|
|
aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
|
2015-03-27 10:11:40 +00:00
|
|
|
aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
|
2013-04-11 10:52:08 +00:00
|
|
|
aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
|
2007-10-03 19:19:05 +00:00
|
|
|
aaa->Mask = WRITE_DAC | WRITE_OWNER;
|
2008-11-03 22:25:37 +00:00
|
|
|
if (mode & S_IRUSR)
|
2011-03-24 08:57:21 +00:00
|
|
|
aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
|
2008-11-03 22:25:37 +00:00
|
|
|
if (mode & S_IWUSR)
|
2011-03-16 10:46:00 +00:00
|
|
|
aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
|
2007-10-03 19:19:05 +00:00
|
|
|
sid = (SID *)&aaa->SidStart;
|
2013-04-11 10:52:08 +00:00
|
|
|
memcpy( sid, user, security_sid_len( user ));
|
2007-10-03 19:19:05 +00:00
|
|
|
}
|
2008-11-03 22:25:37 +00:00
|
|
|
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))))
|
2007-11-20 17:49:07 +00:00
|
|
|
{
|
|
|
|
/* 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;
|
2015-03-27 10:11:40 +00:00
|
|
|
ada->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
|
2013-04-11 10:52:08 +00:00
|
|
|
ada->Header.AceSize = FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
|
2007-11-20 17:49:07 +00:00
|
|
|
ada->Mask = 0;
|
2008-11-03 22:25:37 +00:00
|
|
|
if (!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH)))
|
2011-03-24 08:57:21 +00:00
|
|
|
ada->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
|
2008-11-03 22:25:37 +00:00
|
|
|
if (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IROTH)))
|
2011-03-16 10:46:00 +00:00
|
|
|
ada->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
|
2007-11-20 17:49:07 +00:00
|
|
|
ada->Mask &= ~STANDARD_RIGHTS_ALL; /* never deny standard rights */
|
|
|
|
sid = (SID *)&ada->SidStart;
|
2013-04-11 10:52:08 +00:00
|
|
|
memcpy( sid, user, security_sid_len( user ));
|
2007-11-20 17:49:07 +00:00
|
|
|
}
|
2008-11-03 22:25:37 +00:00
|
|
|
if (mode & S_IRWXO)
|
2007-10-03 19:19:05 +00:00
|
|
|
{
|
|
|
|
/* appropriate access rights for Everyone */
|
2007-11-20 17:49:07 +00:00
|
|
|
aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
|
|
|
|
current_ace = &aaa->Header;
|
2007-10-03 19:19:05 +00:00
|
|
|
aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
|
2015-03-27 10:11:40 +00:00
|
|
|
aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
|
2013-04-11 10:52:08 +00:00
|
|
|
aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
|
2007-10-03 19:19:05 +00:00
|
|
|
aaa->Mask = 0;
|
2008-11-03 22:25:37 +00:00
|
|
|
if (mode & S_IROTH)
|
2011-03-24 08:57:21 +00:00
|
|
|
aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
|
2008-11-03 22:25:37 +00:00
|
|
|
if (mode & S_IWOTH)
|
2011-03-16 10:46:00 +00:00
|
|
|
aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
|
2007-10-03 19:19:05 +00:00
|
|
|
sid = (SID *)&aaa->SidStart;
|
2013-04-11 10:52:08 +00:00
|
|
|
memcpy( sid, world_sid, security_sid_len( world_sid ));
|
2007-10-03 19:19:05 +00:00
|
|
|
}
|
|
|
|
|
2008-11-03 22:25:37 +00:00
|
|
|
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;
|
|
|
|
|
2007-10-03 19:19:05 +00:00
|
|
|
file->mode = st.st_mode;
|
|
|
|
file->uid = st.st_uid;
|
|
|
|
free( obj->sd );
|
|
|
|
obj->sd = sd;
|
|
|
|
return sd;
|
|
|
|
}
|
|
|
|
|
2009-12-11 16:32:38 +00:00
|
|
|
static mode_t file_access_to_mode( unsigned int access )
|
|
|
|
{
|
|
|
|
mode_t mode = 0;
|
|
|
|
|
2021-02-05 11:10:44 +00:00
|
|
|
access = map_access( access, &file_type.mapping );
|
2009-12-11 16:32:38 +00:00
|
|
|
if (access & FILE_READ_DATA) mode |= 4;
|
2013-10-02 03:19:08 +00:00
|
|
|
if (access & (FILE_WRITE_DATA|FILE_APPEND_DATA)) mode |= 2;
|
2009-12-11 16:32:38 +00:00
|
|
|
if (access & FILE_EXECUTE) mode |= 1;
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
2008-11-03 22:25:37 +00:00
|
|
|
mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner )
|
2007-10-03 19:19:05 +00:00
|
|
|
{
|
2007-10-26 16:02:16 +00:00
|
|
|
mode_t new_mode = 0;
|
2015-03-27 14:17:38 +00:00
|
|
|
mode_t bits_to_set = ~0;
|
2009-12-11 16:32:38 +00:00
|
|
|
mode_t mode;
|
2007-10-26 16:02:16 +00:00
|
|
|
int present;
|
|
|
|
const ACL *dacl = sd_get_dacl( sd, &present );
|
|
|
|
if (present && dacl)
|
2007-10-03 19:19:05 +00:00
|
|
|
{
|
2007-10-26 16:02:16 +00:00
|
|
|
const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
|
|
|
|
ULONG i;
|
2007-11-20 10:47:11 +00:00
|
|
|
for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
|
2007-10-03 19:19:05 +00:00
|
|
|
{
|
2007-10-26 16:02:16 +00:00
|
|
|
const ACCESS_ALLOWED_ACE *aa_ace;
|
|
|
|
const ACCESS_DENIED_ACE *ad_ace;
|
|
|
|
const SID *sid;
|
2007-10-03 19:19:05 +00:00
|
|
|
|
2007-10-26 16:02:16 +00:00
|
|
|
if (ace->AceFlags & INHERIT_ONLY_ACE) continue;
|
2007-10-03 19:19:05 +00:00
|
|
|
|
2007-10-26 16:02:16 +00:00
|
|
|
switch (ace->AceType)
|
2007-10-03 19:19:05 +00:00
|
|
|
{
|
|
|
|
case ACCESS_DENIED_ACE_TYPE:
|
|
|
|
ad_ace = (const ACCESS_DENIED_ACE *)ace;
|
|
|
|
sid = (const SID *)&ad_ace->SidStart;
|
2009-12-11 16:32:38 +00:00
|
|
|
mode = file_access_to_mode( ad_ace->Mask );
|
2007-10-03 19:19:05 +00:00
|
|
|
if (security_equal_sid( sid, security_world_sid ))
|
|
|
|
{
|
2015-03-27 14:17:38 +00:00
|
|
|
bits_to_set &= ~((mode << 6) | (mode << 3) | mode); /* all */
|
2007-10-03 19:19:05 +00:00
|
|
|
}
|
2021-04-20 19:35:00 +00:00
|
|
|
else if (token_sid_present( current->process->token, owner, TRUE ) &&
|
|
|
|
token_sid_present( current->process->token, sid, TRUE ))
|
2009-12-09 15:31:53 +00:00
|
|
|
{
|
2015-03-27 14:17:38 +00:00
|
|
|
bits_to_set &= ~((mode << 6) | (mode << 3)); /* user + group */
|
2009-12-09 15:31:53 +00:00
|
|
|
}
|
2011-07-19 07:52:54 +00:00
|
|
|
else if (security_equal_sid( sid, owner ))
|
|
|
|
{
|
2015-03-27 14:17:38 +00:00
|
|
|
bits_to_set &= ~(mode << 6); /* user only */
|
2011-07-19 07:52:54 +00:00
|
|
|
}
|
2007-10-03 19:19:05 +00:00
|
|
|
break;
|
|
|
|
case ACCESS_ALLOWED_ACE_TYPE:
|
|
|
|
aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
|
|
|
|
sid = (const SID *)&aa_ace->SidStart;
|
2009-12-11 16:32:38 +00:00
|
|
|
mode = file_access_to_mode( aa_ace->Mask );
|
2007-10-03 19:19:05 +00:00
|
|
|
if (security_equal_sid( sid, security_world_sid ))
|
|
|
|
{
|
2015-03-27 14:17:38 +00:00
|
|
|
mode = (mode << 6) | (mode << 3) | mode; /* all */
|
|
|
|
new_mode |= mode & bits_to_set;
|
|
|
|
bits_to_set &= ~mode;
|
2007-10-03 19:19:05 +00:00
|
|
|
}
|
2021-04-20 19:35:00 +00:00
|
|
|
else if (token_sid_present( current->process->token, owner, FALSE ) &&
|
|
|
|
token_sid_present( current->process->token, sid, FALSE ))
|
2009-12-09 15:31:53 +00:00
|
|
|
{
|
2015-03-27 14:17:38 +00:00
|
|
|
mode = (mode << 6) | (mode << 3); /* user + group */
|
|
|
|
new_mode |= mode & bits_to_set;
|
|
|
|
bits_to_set &= ~mode;
|
2009-12-09 15:31:53 +00:00
|
|
|
}
|
2011-07-19 07:52:54 +00:00
|
|
|
else if (security_equal_sid( sid, owner ))
|
|
|
|
{
|
2015-03-27 14:17:38 +00:00
|
|
|
mode = (mode << 6); /* user only */
|
|
|
|
new_mode |= mode & bits_to_set;
|
|
|
|
bits_to_set &= ~mode;
|
2011-07-19 07:52:54 +00:00
|
|
|
}
|
2007-10-03 19:19:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-10-26 16:02:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
/* no ACL means full access rights to anyone */
|
2009-12-09 15:31:53 +00:00
|
|
|
new_mode = S_IRWXU | S_IRWXG | S_IRWXO;
|
2007-10-26 16:02:16 +00:00
|
|
|
|
2015-03-27 14:17:38 +00:00
|
|
|
return new_mode;
|
2007-10-26 16:02:16 +00:00
|
|
|
}
|
2007-10-03 19:19:05 +00:00
|
|
|
|
2007-10-26 16:02:16 +00:00
|
|
|
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;
|
2009-08-17 15:26:51 +00:00
|
|
|
struct stat st;
|
2007-10-26 16:02:16 +00:00
|
|
|
mode_t mode;
|
|
|
|
int unix_fd;
|
|
|
|
|
|
|
|
assert( obj->ops == &file_ops );
|
|
|
|
|
|
|
|
unix_fd = get_file_unix_fd( file );
|
|
|
|
|
2009-08-17 15:26:51 +00:00
|
|
|
if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
|
2007-10-26 16:02:16 +00:00
|
|
|
|
|
|
|
if (set_info & OWNER_SECURITY_INFORMATION)
|
|
|
|
{
|
|
|
|
owner = sd_get_owner( sd );
|
|
|
|
if (!owner)
|
2007-10-03 19:19:05 +00:00
|
|
|
{
|
2007-10-26 16:02:16 +00:00
|
|
|
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 */
|
2009-12-09 15:31:53 +00:00
|
|
|
mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
|
2007-10-26 16:02:16 +00:00
|
|
|
mode |= sd_to_mode( sd, owner );
|
|
|
|
|
2009-12-08 12:10:01 +00:00
|
|
|
if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
|
2007-10-26 16:02:16 +00:00
|
|
|
{
|
2009-08-17 15:26:51 +00:00
|
|
|
file_set_error();
|
|
|
|
return 0;
|
2007-10-06 12:31:34 +00:00
|
|
|
}
|
2007-10-03 19:19:05 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-11-25 18:42:42 +00:00
|
|
|
static struct object *file_lookup_name( struct object *obj, struct unicode_str *name,
|
|
|
|
unsigned int attr, struct object *root )
|
2016-02-09 11:19:34 +00:00
|
|
|
{
|
|
|
|
if (!name || !name->len) return NULL; /* open the file itself */
|
|
|
|
|
|
|
|
set_error( STATUS_OBJECT_PATH_NOT_FOUND );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-21 05:26:44 +00:00
|
|
|
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;
|
2021-02-09 13:11:07 +00:00
|
|
|
struct unicode_str nt_name;
|
2015-08-21 05:26:44 +00:00
|
|
|
char *unix_name;
|
|
|
|
|
|
|
|
assert( obj->ops == &file_ops );
|
|
|
|
|
|
|
|
if ((unix_name = dup_fd_name( file->fd, "" )))
|
|
|
|
{
|
2021-02-09 13:11:07 +00:00
|
|
|
get_nt_name( file->fd, &nt_name );
|
|
|
|
new_file = create_file( NULL, unix_name, strlen(unix_name), nt_name, access,
|
2015-08-21 05:26:44 +00:00
|
|
|
sharing, FILE_OPEN, options, 0, NULL );
|
|
|
|
free( unix_name );
|
|
|
|
}
|
|
|
|
else set_error( STATUS_OBJECT_TYPE_MISMATCH );
|
|
|
|
return new_file;
|
|
|
|
}
|
|
|
|
|
2019-04-23 14:23:33 +00:00
|
|
|
static struct list *file_get_kernel_obj_list( struct object *obj )
|
|
|
|
{
|
|
|
|
struct file *file = (struct file *)obj;
|
|
|
|
return &file->kernel_object;
|
|
|
|
}
|
|
|
|
|
1998-12-30 12:06:45 +00:00
|
|
|
static void file_destroy( struct object *obj )
|
1998-12-27 15:28:54 +00:00
|
|
|
{
|
1998-12-30 12:06:45 +00:00
|
|
|
struct file *file = (struct file *)obj;
|
|
|
|
assert( obj->ops == &file_ops );
|
1999-01-03 11:55:56 +00:00
|
|
|
|
2003-02-19 00:33:32 +00:00
|
|
|
if (file->fd) release_object( file->fd );
|
1998-12-30 12:06:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set the last error depending on errno */
|
|
|
|
void file_set_error(void)
|
|
|
|
{
|
|
|
|
switch (errno)
|
|
|
|
{
|
2007-09-11 04:07:29 +00:00
|
|
|
case ETXTBSY:
|
2000-01-24 21:58:06 +00: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 12:06:45 +00:00
|
|
|
case EACCES:
|
2001-07-14 00:50:30 +00:00
|
|
|
case ESRCH:
|
2009-07-13 04:55:43 +00:00
|
|
|
case EROFS:
|
2000-01-24 21:58:06 +00:00
|
|
|
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;
|
2004-04-16 23:32:40 +00:00
|
|
|
case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
|
1998-12-30 12:06:45 +00:00
|
|
|
case ENFILE:
|
2006-08-29 20:11:28 +00:00
|
|
|
case EMFILE: set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
|
2000-01-24 21:58:06 +00:00
|
|
|
case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
|
|
|
|
case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
|
2007-01-08 03:16:59 +00:00
|
|
|
case ESPIPE: set_error( STATUS_ILLEGAL_FUNCTION ); break;
|
2000-01-24 21:58:06 +00:00
|
|
|
case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
|
|
|
|
case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
|
2004-03-12 01:56:49 +00:00
|
|
|
case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
|
2004-05-01 02:50:06 +00:00
|
|
|
case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
|
2004-07-06 19:42:09 +00:00
|
|
|
case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break;
|
|
|
|
case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break;
|
2018-05-11 04:42:48 +00:00
|
|
|
case EXDEV: set_error( STATUS_NOT_SAME_DEVICE ); break;
|
2019-12-11 09:28:49 +00:00
|
|
|
case ELOOP: set_error( STATUS_REPARSE_POINT_NOT_RESOLVED ); break;
|
2004-01-02 20:11:35 +00:00
|
|
|
#ifdef EOVERFLOW
|
2003-03-18 05:04:33 +00:00
|
|
|
case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
|
2004-01-02 20:11:35 +00:00
|
|
|
#endif
|
2007-09-04 13:51:39 +00:00
|
|
|
default:
|
|
|
|
perror("wineserver: file_set_error() can't map error");
|
|
|
|
set_error( STATUS_UNSUCCESSFUL );
|
|
|
|
break;
|
1998-12-30 12:06:45 +00:00
|
|
|
}
|
1998-12-27 15:28:54 +00:00
|
|
|
}
|
|
|
|
|
2002-05-30 20:12:58 +00:00
|
|
|
struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
|
1998-12-27 15:28:54 +00:00
|
|
|
{
|
1999-11-29 02:17:08 +00:00
|
|
|
return (struct file *)get_handle_obj( process, handle, access, &file_ops );
|
1999-01-01 16:59:27 +00:00
|
|
|
}
|
1998-12-27 15:28:54 +00:00
|
|
|
|
2003-02-14 20:27:09 +00:00
|
|
|
int get_file_unix_fd( struct file *file )
|
|
|
|
{
|
2003-02-19 00:33:32 +00:00
|
|
|
return get_unix_fd( file->fd );
|
2003-02-14 20:27:09 +00:00
|
|
|
}
|
|
|
|
|
1999-05-15 10:48:19 +00:00
|
|
|
/* create a file */
|
|
|
|
DECL_HANDLER(create_file)
|
|
|
|
{
|
2004-03-18 04:08:48 +00:00
|
|
|
struct object *file;
|
2009-12-01 16:38:24 +00:00
|
|
|
struct fd *root_fd = NULL;
|
2021-02-09 12:29:22 +00:00
|
|
|
struct unicode_str nt_name;
|
2007-10-26 16:02:16 +00:00
|
|
|
const struct security_descriptor *sd;
|
2021-02-09 12:29:22 +00:00
|
|
|
const struct object_attributes *objattr = get_req_object_attributes( &sd, &nt_name, NULL );
|
2007-10-26 16:02:16 +00:00
|
|
|
const char *name;
|
|
|
|
data_size_t name_len;
|
|
|
|
|
2016-01-15 09:15:31 +00:00
|
|
|
if (!objattr) return;
|
2007-10-26 16:02:16 +00:00
|
|
|
|
2009-12-01 16:38:24 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-01-19 06:00:21 +00:00
|
|
|
name = get_req_data_after_objattr( objattr, &name_len );
|
1999-05-15 10:48:19 +00:00
|
|
|
|
2001-11-30 18:46:42 +00:00
|
|
|
reply->handle = 0;
|
2021-02-09 13:11:07 +00:00
|
|
|
if ((file = create_file( root_fd, name, name_len, nt_name, req->access, req->sharing,
|
2009-12-01 16:38:24 +00:00
|
|
|
req->create, req->options, req->attrs, sd )))
|
1999-05-15 10:48:19 +00:00
|
|
|
{
|
2016-01-15 08:53:34 +00:00
|
|
|
reply->handle = alloc_handle( current->process, file, req->access, objattr->attributes );
|
1999-06-26 08:43:26 +00:00
|
|
|
release_object( file );
|
1999-06-22 17:26:53 +00:00
|
|
|
}
|
2009-12-01 16:38:24 +00:00
|
|
|
if (root_fd) release_object( root_fd );
|
1999-06-26 08:43:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate a file handle for a Unix fd */
|
|
|
|
DECL_HANDLER(alloc_file_handle)
|
|
|
|
{
|
|
|
|
struct file *file;
|
2001-02-28 21:45:23 +00:00
|
|
|
int fd;
|
1999-06-26 08:43:26 +00:00
|
|
|
|
2001-11-30 18:46:42 +00:00
|
|
|
reply->handle = 0;
|
2001-02-28 21:45:23 +00:00
|
|
|
if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
|
1999-06-22 17:26:53 +00:00
|
|
|
{
|
2001-02-28 21:45:23 +00:00
|
|
|
set_error( STATUS_INVALID_HANDLE );
|
|
|
|
return;
|
|
|
|
}
|
2004-03-12 01:56:49 +00:00
|
|
|
if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
|
2001-02-28 21:45:23 +00:00
|
|
|
{
|
2005-12-09 12:58:25 +00:00
|
|
|
reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
|
2001-02-28 21:45:23 +00:00
|
|
|
release_object( file );
|
1999-05-15 10:48:19 +00: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 12:06:25 +00:00
|
|
|
reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
|
2018-10-31 15:44:56 +00:00
|
|
|
reply->overlapped = is_fd_overlapped( file->fd );
|
1999-05-15 10:48:19 +00: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 12:06:25 +00:00
|
|
|
unlock_fd( file->fd, req->offset, req->count );
|
1999-05-15 10:48:19 +00:00
|
|
|
release_object( file );
|
|
|
|
}
|
|
|
|
}
|