Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Server-side file management |
| 3 | * |
| 4 | * Copyright (C) 1998 Alexandre Julliard |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
Jonathan Ernst | 360a3f9 | 2006-05-18 14:49:52 +0200 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
Patrik Stridvall | 9633632 | 1999-10-24 22:13:47 +0000 | [diff] [blame] | 21 | #include "config.h" |
Marcus Meissner | 3f1ed52 | 2001-05-14 20:09:37 +0000 | [diff] [blame] | 22 | #include "wine/port.h" |
Patrik Stridvall | 9633632 | 1999-10-24 22:13:47 +0000 | [diff] [blame] | 23 | |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 24 | #include <assert.h> |
| 25 | #include <fcntl.h> |
Alexandre Julliard | e37c6e1 | 2003-09-05 23:08:26 +0000 | [diff] [blame] | 26 | #include <stdarg.h> |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 27 | #include <stdio.h> |
David Luyer | ee517e8 | 1999-02-28 12:27:56 +0000 | [diff] [blame] | 28 | #include <string.h> |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 29 | #include <stdlib.h> |
Marcus Meissner | 8ba68fb | 1999-01-01 18:42:17 +0000 | [diff] [blame] | 30 | #include <errno.h> |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 31 | #include <sys/stat.h> |
| 32 | #include <sys/time.h> |
| 33 | #include <sys/types.h> |
| 34 | #include <time.h> |
| 35 | #include <unistd.h> |
Steven Edwards | 037c8a1 | 2003-02-11 22:27:13 +0000 | [diff] [blame] | 36 | #ifdef HAVE_UTIME_H |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 37 | #include <utime.h> |
Steven Edwards | 037c8a1 | 2003-02-11 22:27:13 +0000 | [diff] [blame] | 38 | #endif |
Steven Edwards | 5727918 | 2005-03-04 12:38:36 +0000 | [diff] [blame] | 39 | #ifdef HAVE_POLL_H |
| 40 | #include <poll.h> |
| 41 | #endif |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 42 | |
Ge van Geldorp | 1a1583a | 2005-11-28 17:32:54 +0100 | [diff] [blame] | 43 | #include "ntstatus.h" |
| 44 | #define WIN32_NO_STATUS |
Alexandre Julliard | e37c6e1 | 2003-09-05 23:08:26 +0000 | [diff] [blame] | 45 | #include "windef.h" |
Alexandre Julliard | 014099c | 2004-03-12 01:56:49 +0000 | [diff] [blame] | 46 | #include "winternl.h" |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 47 | |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 48 | #include "file.h" |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 49 | #include "handle.h" |
| 50 | #include "thread.h" |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 51 | #include "request.h" |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 52 | #include "process.h" |
| 53 | #include "security.h" |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 54 | |
| 55 | struct file |
| 56 | { |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 57 | struct object obj; /* object header */ |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 58 | struct fd *fd; /* file descriptor for this file */ |
Alexandre Julliard | a510a7e | 2005-12-12 16:46:17 +0100 | [diff] [blame] | 59 | unsigned int access; /* file access (FILE_READ_DATA etc.) */ |
Alexandre Julliard | 133b8bc | 2007-04-23 15:13:22 +0200 | [diff] [blame] | 60 | mode_t mode; /* file stat.st_mode */ |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 61 | uid_t uid; /* file stat.st_uid */ |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
Mike McCormack | 9a7124e | 2006-01-24 13:30:55 +0100 | [diff] [blame] | 64 | static unsigned int generic_file_map_access( unsigned int access ); |
| 65 | |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 66 | static void file_dump( struct object *obj, int verbose ); |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 67 | static struct fd *file_get_fd( struct object *obj ); |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 68 | static struct security_descriptor *file_get_sd( struct object *obj ); |
| 69 | static int file_set_sd( struct object *obj, const struct security_descriptor *sd, unsigned int set_info ); |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 70 | static void file_destroy( struct object *obj ); |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 71 | |
| 72 | static int file_get_poll_events( struct fd *fd ); |
Alexandre Julliard | df65187 | 2007-03-27 16:51:44 +0200 | [diff] [blame] | 73 | static void file_flush( struct fd *fd, struct event **event ); |
Alexandre Julliard | 7a9363a | 2007-04-10 22:26:23 +0200 | [diff] [blame] | 74 | static enum server_fd_type file_get_fd_type( struct fd *fd ); |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 75 | |
| 76 | static const struct object_ops file_ops = |
| 77 | { |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 78 | sizeof(struct file), /* size */ |
| 79 | file_dump, /* dump */ |
Alexandre Julliard | 8382eb0 | 2007-12-05 18:16:42 +0100 | [diff] [blame] | 80 | no_get_type, /* get_type */ |
Alexandre Julliard | ba896e7 | 2007-04-04 19:39:29 +0200 | [diff] [blame] | 81 | add_queue, /* add_queue */ |
| 82 | remove_queue, /* remove_queue */ |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 83 | default_fd_signaled, /* signaled */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 84 | no_satisfied, /* satisfied */ |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 85 | no_signal, /* signal */ |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 86 | file_get_fd, /* get_fd */ |
Alexandre Julliard | 24001e8 | 2007-10-02 14:20:15 +0200 | [diff] [blame] | 87 | default_fd_map_access, /* map_access */ |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 88 | file_get_sd, /* get_sd */ |
| 89 | file_set_sd, /* set_sd */ |
Vitaliy Margolen | baffcb9 | 2005-11-22 14:55:42 +0000 | [diff] [blame] | 90 | no_lookup_name, /* lookup_name */ |
Alexandre Julliard | 7e71c1d | 2007-03-22 11:44:29 +0100 | [diff] [blame] | 91 | no_open_file, /* open_file */ |
Alexandre Julliard | 715d78e | 2006-11-02 20:52:22 +0100 | [diff] [blame] | 92 | fd_close_handle, /* close_handle */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 93 | file_destroy /* destroy */ |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 96 | static const struct fd_ops file_fd_ops = |
| 97 | { |
| 98 | file_get_poll_events, /* get_poll_events */ |
Robert Shearman | d6a4e34 | 2005-06-07 20:09:01 +0000 | [diff] [blame] | 99 | default_poll_event, /* poll_event */ |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 100 | file_flush, /* flush */ |
Alexandre Julliard | 7a9363a | 2007-04-10 22:26:23 +0200 | [diff] [blame] | 101 | file_get_fd_type, /* get_fd_type */ |
Alexandre Julliard | 6357143 | 2007-04-16 14:45:03 +0200 | [diff] [blame] | 102 | default_fd_ioctl, /* ioctl */ |
Robert Shearman | d6a4e34 | 2005-06-07 20:09:01 +0000 | [diff] [blame] | 103 | default_fd_queue_async, /* queue_async */ |
Alexandre Julliard | 72bff2e | 2007-04-10 17:07:27 +0200 | [diff] [blame] | 104 | default_fd_reselect_async, /* reselect_async */ |
Robert Shearman | d6a4e34 | 2005-06-07 20:09:01 +0000 | [diff] [blame] | 105 | default_fd_cancel_async /* cancel_async */ |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 106 | }; |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 107 | |
Alexandre Julliard | 014099c | 2004-03-12 01:56:49 +0000 | [diff] [blame] | 108 | static inline int is_overlapped( const struct file *file ) |
| 109 | { |
Alexandre Julliard | f85437c | 2007-04-10 22:25:07 +0200 | [diff] [blame] | 110 | return !(get_fd_options( file->fd ) & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)); |
Alexandre Julliard | 014099c | 2004-03-12 01:56:49 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 113 | /* create a file from a file descriptor */ |
| 114 | /* if the function fails the fd is closed */ |
Alexandre Julliard | 62eda39 | 2009-11-23 16:20:15 +0100 | [diff] [blame] | 115 | struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing ) |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 116 | { |
| 117 | struct file *file; |
Alexandre Julliard | 133b8bc | 2007-04-23 15:13:22 +0200 | [diff] [blame] | 118 | struct stat st; |
| 119 | |
| 120 | if (fstat( fd, &st ) == -1) |
| 121 | { |
| 122 | file_set_error(); |
| 123 | return NULL; |
| 124 | } |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 125 | |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 126 | if ((file = alloc_object( &file_ops ))) |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 127 | { |
Alexandre Julliard | 133b8bc | 2007-04-23 15:13:22 +0200 | [diff] [blame] | 128 | file->mode = st.st_mode; |
Alexandre Julliard | 24001e8 | 2007-10-02 14:20:15 +0200 | [diff] [blame] | 129 | file->access = default_fd_map_access( &file->obj, access ); |
Alexandre Julliard | f85437c | 2007-04-10 22:25:07 +0200 | [diff] [blame] | 130 | if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj, |
| 131 | FILE_SYNCHRONOUS_IO_NONALERT ))) |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 132 | { |
| 133 | release_object( file ); |
| 134 | return NULL; |
| 135 | } |
Alexandre Julliard | a2ca2eb | 2010-08-23 17:09:45 +0200 | [diff] [blame] | 136 | allow_fd_caching( file->fd ); |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 137 | } |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 138 | return file; |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Alexandre Julliard | eb2fe39 | 2011-04-18 13:41:54 +0200 | [diff] [blame] | 141 | /* create a file by duplicating an fd object */ |
| 142 | struct file *create_file_for_fd_obj( struct fd *fd, unsigned int access, unsigned int sharing ) |
| 143 | { |
| 144 | struct file *file; |
| 145 | struct stat st; |
| 146 | |
| 147 | if (fstat( get_unix_fd(fd), &st ) == -1) |
| 148 | { |
| 149 | file_set_error(); |
| 150 | return NULL; |
| 151 | } |
| 152 | |
| 153 | if ((file = alloc_object( &file_ops ))) |
| 154 | { |
| 155 | file->mode = st.st_mode; |
| 156 | file->access = default_fd_map_access( &file->obj, access ); |
| 157 | if (!(file->fd = dup_fd_object( fd, access, sharing, FILE_SYNCHRONOUS_IO_NONALERT ))) |
| 158 | { |
| 159 | release_object( file ); |
| 160 | return NULL; |
| 161 | } |
Alexandre Julliard | f2b34dd | 2011-04-22 17:07:45 +0200 | [diff] [blame] | 162 | set_fd_user( file->fd, &file_fd_ops, &file->obj ); |
Alexandre Julliard | eb2fe39 | 2011-04-18 13:41:54 +0200 | [diff] [blame] | 163 | } |
| 164 | return file; |
| 165 | } |
| 166 | |
Alexandre Julliard | 133b8bc | 2007-04-23 15:13:22 +0200 | [diff] [blame] | 167 | static struct object *create_file_obj( struct fd *fd, unsigned int access, mode_t mode ) |
Mike McCormack | 9a7124e | 2006-01-24 13:30:55 +0100 | [diff] [blame] | 168 | { |
| 169 | struct file *file = alloc_object( &file_ops ); |
| 170 | |
| 171 | if (!file) return NULL; |
| 172 | file->access = access; |
Alexandre Julliard | 133b8bc | 2007-04-23 15:13:22 +0200 | [diff] [blame] | 173 | file->mode = mode; |
Alexandre Julliard | 74a6302 | 2009-08-17 17:18:54 +0200 | [diff] [blame] | 174 | file->uid = ~(uid_t)0; |
Mike McCormack | 9a7124e | 2006-01-24 13:30:55 +0100 | [diff] [blame] | 175 | file->fd = fd; |
| 176 | grab_object( fd ); |
| 177 | set_fd_user( fd, &file_fd_ops, &file->obj ); |
| 178 | return &file->obj; |
| 179 | } |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 180 | |
Alexandre Julliard | 42806f3 | 2009-12-01 17:38:24 +0100 | [diff] [blame] | 181 | static struct object *create_file( struct fd *root, const char *nameptr, data_size_t len, |
| 182 | unsigned int access, unsigned int sharing, int create, |
| 183 | unsigned int options, unsigned int attrs, |
| 184 | const struct security_descriptor *sd ) |
Alexandre Julliard | cb28bdc | 1999-02-28 10:13:59 +0000 | [diff] [blame] | 185 | { |
Mike McCormack | 9a7124e | 2006-01-24 13:30:55 +0100 | [diff] [blame] | 186 | struct object *obj = NULL; |
| 187 | struct fd *fd; |
Alexandre Julliard | 471782a | 2006-01-25 15:06:48 +0100 | [diff] [blame] | 188 | int flags; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 189 | char *name; |
Bernhard Rosenkraenzer | 5dda1f7 | 2001-07-23 18:09:41 +0000 | [diff] [blame] | 190 | mode_t mode; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 191 | |
Alexandre Julliard | 42806f3 | 2009-12-01 17:38:24 +0100 | [diff] [blame] | 192 | if (!len || ((nameptr[0] == '/') ^ !root)) |
| 193 | { |
| 194 | set_error( STATUS_OBJECT_PATH_SYNTAX_BAD ); |
| 195 | return NULL; |
| 196 | } |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 197 | if (!(name = mem_alloc( len + 1 ))) return NULL; |
| 198 | memcpy( name, nameptr, len ); |
| 199 | name[len] = 0; |
| 200 | |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 201 | switch(create) |
| 202 | { |
Alexandre Julliard | 014099c | 2004-03-12 01:56:49 +0000 | [diff] [blame] | 203 | case FILE_CREATE: flags = O_CREAT | O_EXCL; break; |
| 204 | case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */ |
Dmitry Timoshkov | f834d23 | 2011-03-30 18:08:49 +0900 | [diff] [blame] | 205 | access |= FILE_WRITE_ATTRIBUTES; |
Alexandre Julliard | 014099c | 2004-03-12 01:56:49 +0000 | [diff] [blame] | 206 | case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break; |
| 207 | case FILE_OPEN: flags = 0; break; |
| 208 | case FILE_OPEN_IF: flags = O_CREAT; break; |
Dmitry Timoshkov | f834d23 | 2011-03-30 18:08:49 +0900 | [diff] [blame] | 209 | case FILE_OVERWRITE: flags = O_TRUNC; |
| 210 | access |= FILE_WRITE_ATTRIBUTES; break; |
Mike McCormack | 9a7124e | 2006-01-24 13:30:55 +0100 | [diff] [blame] | 211 | default: set_error( STATUS_INVALID_PARAMETER ); goto done; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 212 | } |
Eric Pouech | 4634447 | 2005-01-14 19:54:38 +0000 | [diff] [blame] | 213 | |
Rob Shearman | 5f5df83 | 2007-10-26 17:02:16 +0100 | [diff] [blame] | 214 | if (sd) |
| 215 | { |
| 216 | const SID *owner = sd_get_owner( sd ); |
| 217 | if (!owner) |
| 218 | owner = token_get_user( current->process->token ); |
| 219 | mode = sd_to_mode( sd, owner ); |
| 220 | } |
| 221 | else |
| 222 | mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666; |
Bernhard Rosenkraenzer | 5dda1f7 | 2001-07-23 18:09:41 +0000 | [diff] [blame] | 223 | |
| 224 | if (len >= 4 && |
| 225 | (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" ))) |
Rob Shearman | 5f5df83 | 2007-10-26 17:02:16 +0100 | [diff] [blame] | 226 | { |
| 227 | if (mode & S_IRUSR) |
| 228 | mode |= S_IXUSR; |
| 229 | if (mode & S_IRGRP) |
| 230 | mode |= S_IXGRP; |
| 231 | if (mode & S_IROTH) |
| 232 | mode |= S_IXOTH; |
| 233 | } |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 234 | |
Mike McCormack | 9a7124e | 2006-01-24 13:30:55 +0100 | [diff] [blame] | 235 | access = generic_file_map_access( access ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 236 | |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 237 | /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */ |
Alexandre Julliard | 42806f3 | 2009-12-01 17:38:24 +0100 | [diff] [blame] | 238 | fd = open_fd( root, name, flags | O_NONBLOCK | O_LARGEFILE, &mode, access, sharing, options ); |
Mike McCormack | 9a7124e | 2006-01-24 13:30:55 +0100 | [diff] [blame] | 239 | if (!fd) goto done; |
| 240 | |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 241 | if (S_ISDIR(mode)) |
Alexandre Julliard | 74a6302 | 2009-08-17 17:18:54 +0200 | [diff] [blame] | 242 | obj = create_dir_obj( fd, access, mode ); |
Mike McCormack | 0835107 | 2006-01-27 12:13:56 +0100 | [diff] [blame] | 243 | else if (S_ISCHR(mode) && is_serial_fd( fd )) |
Alexandre Julliard | f85437c | 2007-04-10 22:25:07 +0200 | [diff] [blame] | 244 | obj = create_serial( fd ); |
Mike McCormack | 9a7124e | 2006-01-24 13:30:55 +0100 | [diff] [blame] | 245 | else |
Alexandre Julliard | 133b8bc | 2007-04-23 15:13:22 +0200 | [diff] [blame] | 246 | obj = create_file_obj( fd, access, mode ); |
Mike McCormack | 9a7124e | 2006-01-24 13:30:55 +0100 | [diff] [blame] | 247 | |
| 248 | release_object( fd ); |
| 249 | |
| 250 | done: |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 251 | free( name ); |
Mike McCormack | 9a7124e | 2006-01-24 13:30:55 +0100 | [diff] [blame] | 252 | return obj; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Alexandre Julliard | 84fdfd0 | 2001-04-13 22:38:39 +0000 | [diff] [blame] | 255 | /* check if two file objects point to the same file */ |
| 256 | int is_same_file( struct file *file1, struct file *file2 ) |
| 257 | { |
Alexandre Julliard | 74bd1e4 | 2004-03-27 20:48:42 +0000 | [diff] [blame] | 258 | return is_same_file_fd( file1->fd, file2->fd ); |
Alexandre Julliard | 84fdfd0 | 2001-04-13 22:38:39 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 261 | static void file_dump( struct object *obj, int verbose ) |
| 262 | { |
| 263 | struct file *file = (struct file *)obj; |
| 264 | assert( obj->ops == &file_ops ); |
Alexandre Julliard | f85437c | 2007-04-10 22:25:07 +0200 | [diff] [blame] | 265 | fprintf( stderr, "File fd=%p\n", file->fd ); |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 268 | static int file_get_poll_events( struct fd *fd ) |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 269 | { |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 270 | struct file *file = get_fd_user( fd ); |
Alexandre Julliard | 57e1131 | 1999-05-16 16:59:38 +0000 | [diff] [blame] | 271 | int events = 0; |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 272 | assert( file->obj.ops == &file_ops ); |
Alexandre Julliard | a510a7e | 2005-12-12 16:46:17 +0100 | [diff] [blame] | 273 | if (file->access & FILE_UNIX_READ_ACCESS) events |= POLLIN; |
| 274 | if (file->access & FILE_UNIX_WRITE_ACCESS) events |= POLLOUT; |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 275 | return events; |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Alexandre Julliard | df65187 | 2007-03-27 16:51:44 +0200 | [diff] [blame] | 278 | static void file_flush( struct fd *fd, struct event **event ) |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 279 | { |
Alexandre Julliard | df65187 | 2007-03-27 16:51:44 +0200 | [diff] [blame] | 280 | int unix_fd = get_unix_fd( fd ); |
| 281 | if (unix_fd != -1 && fsync( unix_fd ) == -1) file_set_error(); |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Alexandre Julliard | 7a9363a | 2007-04-10 22:26:23 +0200 | [diff] [blame] | 284 | static enum server_fd_type file_get_fd_type( struct fd *fd ) |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 285 | { |
Alexandre Julliard | 133b8bc | 2007-04-23 15:13:22 +0200 | [diff] [blame] | 286 | struct file *file = get_fd_user( fd ); |
| 287 | |
| 288 | if (S_ISREG(file->mode) || S_ISBLK(file->mode)) return FD_TYPE_FILE; |
| 289 | if (S_ISDIR(file->mode)) return FD_TYPE_DIR; |
| 290 | return FD_TYPE_CHAR; |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 293 | static struct fd *file_get_fd( struct object *obj ) |
| 294 | { |
| 295 | struct file *file = (struct file *)obj; |
| 296 | assert( obj->ops == &file_ops ); |
| 297 | return (struct fd *)grab_object( file->fd ); |
Martin Wilck | 718b1b7 | 2002-01-07 21:02:15 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Mike McCormack | 9a7124e | 2006-01-24 13:30:55 +0100 | [diff] [blame] | 300 | static unsigned int generic_file_map_access( unsigned int access ) |
Alexandre Julliard | a510a7e | 2005-12-12 16:46:17 +0100 | [diff] [blame] | 301 | { |
| 302 | if (access & GENERIC_READ) access |= FILE_GENERIC_READ; |
| 303 | if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE; |
| 304 | if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE; |
| 305 | if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS; |
| 306 | return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL); |
| 307 | } |
| 308 | |
Paul Bryan Roberts | d87af3d | 2008-11-03 22:25:37 +0000 | [diff] [blame] | 309 | struct security_descriptor *mode_to_sd( mode_t mode, const SID *user, const SID *group ) |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 310 | { |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 311 | struct security_descriptor *sd; |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 312 | size_t dacl_size; |
Rob Shearman | ec8a27f | 2007-11-20 17:49:07 +0000 | [diff] [blame] | 313 | ACE_HEADER *current_ace; |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 314 | ACCESS_ALLOWED_ACE *aaa; |
| 315 | ACL *dacl; |
| 316 | SID *sid; |
| 317 | char *ptr; |
| 318 | const SID *world_sid = security_world_sid; |
| 319 | const SID *local_system_sid = security_local_system_sid; |
| 320 | |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 321 | dacl_size = sizeof(ACL) + FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + |
| 322 | FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]); |
Paul Bryan Roberts | d87af3d | 2008-11-03 22:25:37 +0000 | [diff] [blame] | 323 | if (mode & S_IRWXU) |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 324 | dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + |
| 325 | FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]); |
Paul Bryan Roberts | d87af3d | 2008-11-03 22:25:37 +0000 | [diff] [blame] | 326 | if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) || |
| 327 | (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) || |
| 328 | (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH)))) |
Rob Shearman | ec8a27f | 2007-11-20 17:49:07 +0000 | [diff] [blame] | 329 | dacl_size += FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + |
| 330 | FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]); |
Paul Bryan Roberts | d87af3d | 2008-11-03 22:25:37 +0000 | [diff] [blame] | 331 | if (mode & S_IRWXO) |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 332 | dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + |
| 333 | FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]); |
| 334 | |
| 335 | sd = mem_alloc( sizeof(struct security_descriptor) + |
| 336 | FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) + |
| 337 | FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]) + |
| 338 | dacl_size ); |
Paul Bryan Roberts | d87af3d | 2008-11-03 22:25:37 +0000 | [diff] [blame] | 339 | if (!sd) return sd; |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 340 | |
| 341 | sd->control = SE_DACL_PRESENT; |
| 342 | sd->owner_len = FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]); |
| 343 | sd->group_len = FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]); |
| 344 | sd->sacl_len = 0; |
| 345 | sd->dacl_len = dacl_size; |
| 346 | |
| 347 | ptr = (char *)(sd + 1); |
| 348 | memcpy( ptr, user, sd->owner_len ); |
| 349 | ptr += sd->owner_len; |
| 350 | memcpy( ptr, group, sd->group_len ); |
| 351 | ptr += sd->group_len; |
| 352 | |
| 353 | dacl = (ACL *)ptr; |
| 354 | dacl->AclRevision = ACL_REVISION; |
| 355 | dacl->Sbz1 = 0; |
| 356 | dacl->AclSize = dacl_size; |
Paul Bryan Roberts | d87af3d | 2008-11-03 22:25:37 +0000 | [diff] [blame] | 357 | dacl->AceCount = 1 + (mode & S_IRWXU ? 1 : 0) + (mode & S_IRWXO ? 1 : 0); |
| 358 | if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) || |
| 359 | (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) || |
| 360 | (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH)))) |
Rob Shearman | ec8a27f | 2007-11-20 17:49:07 +0000 | [diff] [blame] | 361 | dacl->AceCount++; |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 362 | dacl->Sbz2 = 0; |
| 363 | |
| 364 | /* always give FILE_ALL_ACCESS for Local System */ |
| 365 | aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1); |
Rob Shearman | ec8a27f | 2007-11-20 17:49:07 +0000 | [diff] [blame] | 366 | current_ace = &aaa->Header; |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 367 | aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE; |
| 368 | aaa->Header.AceFlags = 0; |
| 369 | aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + |
| 370 | FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]); |
| 371 | aaa->Mask = FILE_ALL_ACCESS; |
| 372 | sid = (SID *)&aaa->SidStart; |
| 373 | memcpy( sid, local_system_sid, FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]) ); |
| 374 | |
Paul Bryan Roberts | d87af3d | 2008-11-03 22:25:37 +0000 | [diff] [blame] | 375 | if (mode & S_IRWXU) |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 376 | { |
| 377 | /* appropriate access rights for the user */ |
Rob Shearman | ec8a27f | 2007-11-20 17:49:07 +0000 | [diff] [blame] | 378 | aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace ); |
| 379 | current_ace = &aaa->Header; |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 380 | aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE; |
| 381 | aaa->Header.AceFlags = 0; |
| 382 | aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + |
| 383 | FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]); |
| 384 | aaa->Mask = WRITE_DAC | WRITE_OWNER; |
Paul Bryan Roberts | d87af3d | 2008-11-03 22:25:37 +0000 | [diff] [blame] | 385 | if (mode & S_IRUSR) |
Dmitry Timoshkov | 1b5602e | 2011-03-24 16:57:21 +0800 | [diff] [blame] | 386 | aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE; |
Paul Bryan Roberts | d87af3d | 2008-11-03 22:25:37 +0000 | [diff] [blame] | 387 | if (mode & S_IWUSR) |
Dmitry Timoshkov | da07795 | 2011-03-16 18:46:00 +0800 | [diff] [blame] | 388 | aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD; |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 389 | sid = (SID *)&aaa->SidStart; |
| 390 | memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) ); |
| 391 | } |
Paul Bryan Roberts | d87af3d | 2008-11-03 22:25:37 +0000 | [diff] [blame] | 392 | if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) || |
| 393 | (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) || |
| 394 | (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH)))) |
Rob Shearman | ec8a27f | 2007-11-20 17:49:07 +0000 | [diff] [blame] | 395 | { |
| 396 | /* deny just in case the user is a member of the group */ |
| 397 | ACCESS_DENIED_ACE *ada = (ACCESS_DENIED_ACE *)ace_next( current_ace ); |
| 398 | current_ace = &ada->Header; |
| 399 | ada->Header.AceType = ACCESS_DENIED_ACE_TYPE; |
| 400 | ada->Header.AceFlags = 0; |
| 401 | ada->Header.AceSize = FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + |
| 402 | FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]); |
| 403 | ada->Mask = 0; |
Paul Bryan Roberts | d87af3d | 2008-11-03 22:25:37 +0000 | [diff] [blame] | 404 | if (!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) |
Dmitry Timoshkov | 1b5602e | 2011-03-24 16:57:21 +0800 | [diff] [blame] | 405 | ada->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE; |
Paul Bryan Roberts | d87af3d | 2008-11-03 22:25:37 +0000 | [diff] [blame] | 406 | if (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IROTH))) |
Dmitry Timoshkov | da07795 | 2011-03-16 18:46:00 +0800 | [diff] [blame] | 407 | ada->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD; |
Rob Shearman | ec8a27f | 2007-11-20 17:49:07 +0000 | [diff] [blame] | 408 | ada->Mask &= ~STANDARD_RIGHTS_ALL; /* never deny standard rights */ |
| 409 | sid = (SID *)&ada->SidStart; |
| 410 | memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) ); |
| 411 | } |
Paul Bryan Roberts | d87af3d | 2008-11-03 22:25:37 +0000 | [diff] [blame] | 412 | if (mode & S_IRWXO) |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 413 | { |
| 414 | /* appropriate access rights for Everyone */ |
Rob Shearman | ec8a27f | 2007-11-20 17:49:07 +0000 | [diff] [blame] | 415 | aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace ); |
| 416 | current_ace = &aaa->Header; |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 417 | aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE; |
| 418 | aaa->Header.AceFlags = 0; |
| 419 | aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + |
| 420 | FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]); |
| 421 | aaa->Mask = 0; |
Paul Bryan Roberts | d87af3d | 2008-11-03 22:25:37 +0000 | [diff] [blame] | 422 | if (mode & S_IROTH) |
Dmitry Timoshkov | 1b5602e | 2011-03-24 16:57:21 +0800 | [diff] [blame] | 423 | aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE; |
Paul Bryan Roberts | d87af3d | 2008-11-03 22:25:37 +0000 | [diff] [blame] | 424 | if (mode & S_IWOTH) |
Dmitry Timoshkov | da07795 | 2011-03-16 18:46:00 +0800 | [diff] [blame] | 425 | aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD; |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 426 | sid = (SID *)&aaa->SidStart; |
| 427 | memcpy( sid, world_sid, FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]) ); |
| 428 | } |
| 429 | |
Paul Bryan Roberts | d87af3d | 2008-11-03 22:25:37 +0000 | [diff] [blame] | 430 | return sd; |
| 431 | } |
| 432 | |
| 433 | static struct security_descriptor *file_get_sd( struct object *obj ) |
| 434 | { |
| 435 | struct file *file = (struct file *)obj; |
| 436 | struct stat st; |
| 437 | int unix_fd; |
| 438 | struct security_descriptor *sd; |
| 439 | |
| 440 | assert( obj->ops == &file_ops ); |
| 441 | |
| 442 | unix_fd = get_file_unix_fd( file ); |
| 443 | |
| 444 | if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) |
| 445 | return obj->sd; |
| 446 | |
| 447 | /* mode and uid the same? if so, no need to re-generate security descriptor */ |
| 448 | if (obj->sd && (st.st_mode & (S_IRWXU|S_IRWXO)) == (file->mode & (S_IRWXU|S_IRWXO)) && |
| 449 | (st.st_uid == file->uid)) |
| 450 | return obj->sd; |
| 451 | |
| 452 | sd = mode_to_sd( st.st_mode, |
| 453 | security_unix_uid_to_sid( st.st_uid ), |
| 454 | token_get_primary_group( current->process->token )); |
| 455 | if (!sd) return obj->sd; |
| 456 | |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 457 | file->mode = st.st_mode; |
| 458 | file->uid = st.st_uid; |
| 459 | free( obj->sd ); |
| 460 | obj->sd = sd; |
| 461 | return sd; |
| 462 | } |
| 463 | |
Alexandre Julliard | 80e844f | 2009-12-11 17:32:38 +0100 | [diff] [blame] | 464 | static mode_t file_access_to_mode( unsigned int access ) |
| 465 | { |
| 466 | mode_t mode = 0; |
| 467 | |
| 468 | access = generic_file_map_access( access ); |
| 469 | if (access & FILE_READ_DATA) mode |= 4; |
| 470 | if (access & FILE_WRITE_DATA) mode |= 2; |
| 471 | if (access & FILE_EXECUTE) mode |= 1; |
| 472 | return mode; |
| 473 | } |
| 474 | |
Paul Bryan Roberts | d87af3d | 2008-11-03 22:25:37 +0000 | [diff] [blame] | 475 | mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner ) |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 476 | { |
Rob Shearman | 5f5df83 | 2007-10-26 17:02:16 +0100 | [diff] [blame] | 477 | mode_t new_mode = 0; |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 478 | mode_t denied_mode = 0; |
Alexandre Julliard | 80e844f | 2009-12-11 17:32:38 +0100 | [diff] [blame] | 479 | mode_t mode; |
Rob Shearman | 5f5df83 | 2007-10-26 17:02:16 +0100 | [diff] [blame] | 480 | int present; |
| 481 | const ACL *dacl = sd_get_dacl( sd, &present ); |
Ben Peddell | b419df1 | 2009-12-10 01:31:53 +1000 | [diff] [blame] | 482 | const SID *user = token_get_user( current->process->token ); |
Rob Shearman | 5f5df83 | 2007-10-26 17:02:16 +0100 | [diff] [blame] | 483 | if (present && dacl) |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 484 | { |
Rob Shearman | 5f5df83 | 2007-10-26 17:02:16 +0100 | [diff] [blame] | 485 | const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1); |
| 486 | ULONG i; |
Rob Shearman | 6753fdc | 2007-11-20 10:47:11 +0000 | [diff] [blame] | 487 | for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace )) |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 488 | { |
Rob Shearman | 5f5df83 | 2007-10-26 17:02:16 +0100 | [diff] [blame] | 489 | const ACCESS_ALLOWED_ACE *aa_ace; |
| 490 | const ACCESS_DENIED_ACE *ad_ace; |
| 491 | const SID *sid; |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 492 | |
Rob Shearman | 5f5df83 | 2007-10-26 17:02:16 +0100 | [diff] [blame] | 493 | if (ace->AceFlags & INHERIT_ONLY_ACE) continue; |
Rob Shearman | f43d8b6 | 2007-10-06 14:31:25 +0200 | [diff] [blame] | 494 | |
Rob Shearman | 5f5df83 | 2007-10-26 17:02:16 +0100 | [diff] [blame] | 495 | switch (ace->AceType) |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 496 | { |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 497 | case ACCESS_DENIED_ACE_TYPE: |
| 498 | ad_ace = (const ACCESS_DENIED_ACE *)ace; |
| 499 | sid = (const SID *)&ad_ace->SidStart; |
Alexandre Julliard | 80e844f | 2009-12-11 17:32:38 +0100 | [diff] [blame] | 500 | mode = file_access_to_mode( ad_ace->Mask ); |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 501 | if (security_equal_sid( sid, security_world_sid )) |
| 502 | { |
Alexandre Julliard | 80e844f | 2009-12-11 17:32:38 +0100 | [diff] [blame] | 503 | denied_mode |= (mode << 6) | (mode << 3) | mode; /* all */ |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 504 | } |
Ben Peddell | b419df1 | 2009-12-10 01:31:53 +1000 | [diff] [blame] | 505 | else if ((security_equal_sid( user, owner ) && |
| 506 | token_sid_present( current->process->token, sid, TRUE ))) |
| 507 | { |
Alexandre Julliard | 80e844f | 2009-12-11 17:32:38 +0100 | [diff] [blame] | 508 | denied_mode |= (mode << 6) | (mode << 3); /* user + group */ |
Ben Peddell | b419df1 | 2009-12-10 01:31:53 +1000 | [diff] [blame] | 509 | } |
Hans Leidekker | 3321c45 | 2011-07-19 09:52:54 +0200 | [diff] [blame] | 510 | else if (security_equal_sid( sid, owner )) |
| 511 | { |
| 512 | denied_mode |= (mode << 6); /* user only */ |
| 513 | } |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 514 | break; |
| 515 | case ACCESS_ALLOWED_ACE_TYPE: |
| 516 | aa_ace = (const ACCESS_ALLOWED_ACE *)ace; |
| 517 | sid = (const SID *)&aa_ace->SidStart; |
Alexandre Julliard | 80e844f | 2009-12-11 17:32:38 +0100 | [diff] [blame] | 518 | mode = file_access_to_mode( aa_ace->Mask ); |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 519 | if (security_equal_sid( sid, security_world_sid )) |
| 520 | { |
Alexandre Julliard | 80e844f | 2009-12-11 17:32:38 +0100 | [diff] [blame] | 521 | new_mode |= (mode << 6) | (mode << 3) | mode; /* all */ |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 522 | } |
Ben Peddell | b419df1 | 2009-12-10 01:31:53 +1000 | [diff] [blame] | 523 | else if ((security_equal_sid( user, owner ) && |
| 524 | token_sid_present( current->process->token, sid, FALSE ))) |
| 525 | { |
Alexandre Julliard | 80e844f | 2009-12-11 17:32:38 +0100 | [diff] [blame] | 526 | new_mode |= (mode << 6) | (mode << 3); /* user + group */ |
Ben Peddell | b419df1 | 2009-12-10 01:31:53 +1000 | [diff] [blame] | 527 | } |
Hans Leidekker | 3321c45 | 2011-07-19 09:52:54 +0200 | [diff] [blame] | 528 | else if (security_equal_sid( sid, owner )) |
| 529 | { |
| 530 | new_mode |= (mode << 6); /* user only */ |
| 531 | } |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 532 | break; |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 533 | } |
| 534 | } |
Rob Shearman | 5f5df83 | 2007-10-26 17:02:16 +0100 | [diff] [blame] | 535 | } |
| 536 | else |
| 537 | /* no ACL means full access rights to anyone */ |
Ben Peddell | b419df1 | 2009-12-10 01:31:53 +1000 | [diff] [blame] | 538 | new_mode = S_IRWXU | S_IRWXG | S_IRWXO; |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 539 | |
Rob Shearman | 5f5df83 | 2007-10-26 17:02:16 +0100 | [diff] [blame] | 540 | return new_mode & ~denied_mode; |
| 541 | } |
| 542 | |
| 543 | static int file_set_sd( struct object *obj, const struct security_descriptor *sd, |
| 544 | unsigned int set_info ) |
| 545 | { |
| 546 | struct file *file = (struct file *)obj; |
| 547 | const SID *owner; |
Alexandre Julliard | da1784b | 2009-08-17 17:26:51 +0200 | [diff] [blame] | 548 | struct stat st; |
Rob Shearman | 5f5df83 | 2007-10-26 17:02:16 +0100 | [diff] [blame] | 549 | mode_t mode; |
| 550 | int unix_fd; |
| 551 | |
| 552 | assert( obj->ops == &file_ops ); |
| 553 | |
| 554 | unix_fd = get_file_unix_fd( file ); |
| 555 | |
Alexandre Julliard | da1784b | 2009-08-17 17:26:51 +0200 | [diff] [blame] | 556 | if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1; |
Rob Shearman | 5f5df83 | 2007-10-26 17:02:16 +0100 | [diff] [blame] | 557 | |
| 558 | if (set_info & OWNER_SECURITY_INFORMATION) |
| 559 | { |
| 560 | owner = sd_get_owner( sd ); |
| 561 | if (!owner) |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 562 | { |
Rob Shearman | 5f5df83 | 2007-10-26 17:02:16 +0100 | [diff] [blame] | 563 | set_error( STATUS_INVALID_SECURITY_DESCR ); |
| 564 | return 0; |
| 565 | } |
| 566 | if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) )) |
| 567 | { |
| 568 | /* FIXME: get Unix uid and call fchown */ |
| 569 | } |
| 570 | } |
| 571 | else if (obj->sd) |
| 572 | owner = sd_get_owner( obj->sd ); |
| 573 | else |
| 574 | owner = token_get_user( current->process->token ); |
| 575 | |
| 576 | /* group and sacl not supported */ |
| 577 | |
| 578 | if (set_info & DACL_SECURITY_INFORMATION) |
| 579 | { |
| 580 | /* keep the bits that we don't map to access rights in the ACL */ |
Ben Peddell | b419df1 | 2009-12-10 01:31:53 +1000 | [diff] [blame] | 581 | mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX); |
Rob Shearman | 5f5df83 | 2007-10-26 17:02:16 +0100 | [diff] [blame] | 582 | mode |= sd_to_mode( sd, owner ); |
| 583 | |
Paul Chitescu | 9235249 | 2009-12-08 14:10:01 +0200 | [diff] [blame] | 584 | if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1) |
Rob Shearman | 5f5df83 | 2007-10-26 17:02:16 +0100 | [diff] [blame] | 585 | { |
Alexandre Julliard | da1784b | 2009-08-17 17:26:51 +0200 | [diff] [blame] | 586 | file_set_error(); |
| 587 | return 0; |
Rob Shearman | f5070b0 | 2007-10-06 14:31:34 +0200 | [diff] [blame] | 588 | } |
Rob Shearman | cb124c9 | 2007-10-03 20:19:05 +0100 | [diff] [blame] | 589 | } |
| 590 | return 1; |
| 591 | } |
| 592 | |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 593 | static void file_destroy( struct object *obj ) |
| 594 | { |
Alexandre Julliard | 0562539 | 1999-01-03 11:55:56 +0000 | [diff] [blame] | 595 | struct file *file = (struct file *)obj; |
| 596 | assert( obj->ops == &file_ops ); |
| 597 | |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 598 | if (file->fd) release_object( file->fd ); |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 601 | /* set the last error depending on errno */ |
| 602 | void file_set_error(void) |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 603 | { |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 604 | switch (errno) |
| 605 | { |
Dmitry Timoshkov | c75edd0 | 2007-09-11 13:07:29 +0900 | [diff] [blame] | 606 | case ETXTBSY: |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 607 | case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break; |
| 608 | case EBADF: set_error( STATUS_INVALID_HANDLE ); break; |
| 609 | case ENOSPC: set_error( STATUS_DISK_FULL ); break; |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 610 | case EACCES: |
Alexandre Julliard | 5f258c6 | 2001-07-14 00:50:30 +0000 | [diff] [blame] | 611 | case ESRCH: |
Andrew Nguyen | 270179a | 2009-07-12 23:55:43 -0500 | [diff] [blame] | 612 | case EROFS: |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 613 | case EPERM: set_error( STATUS_ACCESS_DENIED ); break; |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 614 | case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break; |
| 615 | case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break; |
Alexandre Julliard | 716878c | 2004-04-16 23:32:40 +0000 | [diff] [blame] | 616 | case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break; |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 617 | case ENFILE: |
Robert Shearman | 4d7b230 | 2006-08-29 21:11:28 +0100 | [diff] [blame] | 618 | case EMFILE: set_error( STATUS_TOO_MANY_OPENED_FILES ); break; |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 619 | case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break; |
| 620 | case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break; |
Dmitry Timoshkov | ced2e56 | 2007-01-08 11:16:59 +0800 | [diff] [blame] | 621 | case ESPIPE: set_error( STATUS_ILLEGAL_FUNCTION ); break; |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 622 | case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break; |
| 623 | case EIO: set_error( STATUS_ACCESS_VIOLATION ); break; |
Alexandre Julliard | 014099c | 2004-03-12 01:56:49 +0000 | [diff] [blame] | 624 | case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break; |
Alexandre Julliard | 72f87b8 | 2004-05-01 02:50:06 +0000 | [diff] [blame] | 625 | case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break; |
Mike McCormack | ea782b6 | 2004-07-06 19:42:09 +0000 | [diff] [blame] | 626 | case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break; |
| 627 | case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break; |
Wim Lewis | 99a01c0 | 2004-01-02 20:11:35 +0000 | [diff] [blame] | 628 | #ifdef EOVERFLOW |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 629 | case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break; |
Wim Lewis | 99a01c0 | 2004-01-02 20:11:35 +0000 | [diff] [blame] | 630 | #endif |
Dmitry Timoshkov | 69025ba | 2007-09-04 22:51:39 +0900 | [diff] [blame] | 631 | default: |
| 632 | perror("wineserver: file_set_error() can't map error"); |
| 633 | set_error( STATUS_UNSUCCESSFUL ); |
| 634 | break; |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame] | 635 | } |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Alexandre Julliard | 5188574 | 2002-05-30 20:12:58 +0000 | [diff] [blame] | 638 | struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access ) |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 639 | { |
Alexandre Julliard | 61ec6c1 | 1999-11-29 02:17:08 +0000 | [diff] [blame] | 640 | return (struct file *)get_handle_obj( process, handle, access, &file_ops ); |
Alexandre Julliard | a8b8d9c | 1999-01-01 16:59:27 +0000 | [diff] [blame] | 641 | } |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 642 | |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 643 | int get_file_unix_fd( struct file *file ) |
| 644 | { |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 645 | return get_unix_fd( file->fd ); |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 648 | /* create a file */ |
| 649 | DECL_HANDLER(create_file) |
| 650 | { |
Alexandre Julliard | adc0610 | 2004-03-18 04:08:48 +0000 | [diff] [blame] | 651 | struct object *file; |
Alexandre Julliard | 42806f3 | 2009-12-01 17:38:24 +0100 | [diff] [blame] | 652 | struct fd *root_fd = NULL; |
Rob Shearman | 5f5df83 | 2007-10-26 17:02:16 +0100 | [diff] [blame] | 653 | const struct object_attributes *objattr = get_req_data(); |
| 654 | const struct security_descriptor *sd; |
| 655 | const char *name; |
| 656 | data_size_t name_len; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 657 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 658 | reply->handle = 0; |
Rob Shearman | 5f5df83 | 2007-10-26 17:02:16 +0100 | [diff] [blame] | 659 | |
| 660 | if (!objattr_is_valid( objattr, get_req_data_size() )) |
| 661 | return; |
| 662 | /* name is transferred in the unix codepage outside of the objattr structure */ |
| 663 | if (objattr->name_len) |
| 664 | { |
| 665 | set_error( STATUS_INVALID_PARAMETER ); |
| 666 | return; |
| 667 | } |
| 668 | |
Alexandre Julliard | 42806f3 | 2009-12-01 17:38:24 +0100 | [diff] [blame] | 669 | if (objattr->rootdir) |
| 670 | { |
| 671 | struct dir *root; |
| 672 | |
| 673 | if (!(root = get_dir_obj( current->process, objattr->rootdir, 0 ))) return; |
| 674 | root_fd = get_obj_fd( (struct object *)root ); |
| 675 | release_object( root ); |
| 676 | if (!root_fd) return; |
| 677 | } |
| 678 | |
Rob Shearman | 5f5df83 | 2007-10-26 17:02:16 +0100 | [diff] [blame] | 679 | sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL; |
| 680 | |
| 681 | name = (const char *)get_req_data() + sizeof(*objattr) + objattr->sd_len; |
| 682 | name_len = get_req_data_size() - sizeof(*objattr) - objattr->sd_len; |
| 683 | |
| 684 | reply->handle = 0; |
Alexandre Julliard | 42806f3 | 2009-12-01 17:38:24 +0100 | [diff] [blame] | 685 | if ((file = create_file( root_fd, name, name_len, req->access, req->sharing, |
| 686 | req->create, req->options, req->attrs, sd ))) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 687 | { |
Alexandre Julliard | 24560e7 | 2005-12-09 13:58:25 +0100 | [diff] [blame] | 688 | reply->handle = alloc_handle( current->process, file, req->access, req->attributes ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 689 | release_object( file ); |
| 690 | } |
Alexandre Julliard | 42806f3 | 2009-12-01 17:38:24 +0100 | [diff] [blame] | 691 | if (root_fd) release_object( root_fd ); |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | /* allocate a file handle for a Unix fd */ |
| 695 | DECL_HANDLER(alloc_file_handle) |
| 696 | { |
| 697 | struct file *file; |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 698 | int fd; |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 699 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 700 | reply->handle = 0; |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 701 | if ((fd = thread_get_inflight_fd( current, req->fd )) == -1) |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 702 | { |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 703 | set_error( STATUS_INVALID_HANDLE ); |
| 704 | return; |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 705 | } |
Alexandre Julliard | 014099c | 2004-03-12 01:56:49 +0000 | [diff] [blame] | 706 | if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE ))) |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 707 | { |
Alexandre Julliard | 24560e7 | 2005-12-09 13:58:25 +0100 | [diff] [blame] | 708 | reply->handle = alloc_handle( current->process, file, req->access, req->attributes ); |
Alexandre Julliard | f524240 | 2001-02-28 21:45:23 +0000 | [diff] [blame] | 709 | release_object( file ); |
| 710 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 713 | /* lock a region of a file */ |
| 714 | DECL_HANDLER(lock_file) |
| 715 | { |
| 716 | struct file *file; |
| 717 | |
| 718 | if ((file = get_file_obj( current->process, req->handle, 0 ))) |
| 719 | { |
Alexandre Julliard | 235532c | 2007-10-10 14:06:25 +0200 | [diff] [blame] | 720 | reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait ); |
Alexandre Julliard | 014099c | 2004-03-12 01:56:49 +0000 | [diff] [blame] | 721 | reply->overlapped = is_overlapped( file ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 722 | release_object( file ); |
| 723 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | /* unlock a region of a file */ |
| 727 | DECL_HANDLER(unlock_file) |
| 728 | { |
| 729 | struct file *file; |
| 730 | |
| 731 | if ((file = get_file_obj( current->process, req->handle, 0 ))) |
| 732 | { |
Alexandre Julliard | 235532c | 2007-10-10 14:06:25 +0200 | [diff] [blame] | 733 | unlock_fd( file->fd, req->offset, req->count ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 734 | release_object( file ); |
| 735 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 736 | } |