blob: 5224e85feaac7d37eed57a74395668e261ce3c2c [file] [log] [blame]
Alexandre Julliard338e7571998-12-27 15:28:54 +00001/*
2 * Server-side file management
3 *
4 * Copyright (C) 1998 Alexandre Julliard
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00005 *
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 Ernst360a3f92006-05-18 14:49:52 +020018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard338e7571998-12-27 15:28:54 +000019 */
20
Patrik Stridvall96336321999-10-24 22:13:47 +000021#include "config.h"
Marcus Meissner3f1ed522001-05-14 20:09:37 +000022#include "wine/port.h"
Patrik Stridvall96336321999-10-24 22:13:47 +000023
Alexandre Julliard338e7571998-12-27 15:28:54 +000024#include <assert.h>
25#include <fcntl.h>
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000026#include <stdarg.h>
Alexandre Julliard338e7571998-12-27 15:28:54 +000027#include <stdio.h>
David Luyeree517e81999-02-28 12:27:56 +000028#include <string.h>
Alexandre Julliard338e7571998-12-27 15:28:54 +000029#include <stdlib.h>
Marcus Meissner8ba68fb1999-01-01 18:42:17 +000030#include <errno.h>
Howard Abrams13277481999-07-10 13:16:29 +000031#ifdef HAVE_SYS_ERRNO_H
Alexandre Julliard338e7571998-12-27 15:28:54 +000032#include <sys/errno.h>
Howard Abrams13277481999-07-10 13:16:29 +000033#endif
Alexandre Julliard338e7571998-12-27 15:28:54 +000034#include <sys/stat.h>
35#include <sys/time.h>
36#include <sys/types.h>
37#include <time.h>
38#include <unistd.h>
Steven Edwards037c8a12003-02-11 22:27:13 +000039#ifdef HAVE_UTIME_H
Alexandre Julliard05625391999-01-03 11:55:56 +000040#include <utime.h>
Steven Edwards037c8a12003-02-11 22:27:13 +000041#endif
Steven Edwards57279182005-03-04 12:38:36 +000042#ifdef HAVE_POLL_H
43#include <poll.h>
44#endif
Alexandre Julliard338e7571998-12-27 15:28:54 +000045
Ge van Geldorp1a1583a2005-11-28 17:32:54 +010046#include "ntstatus.h"
47#define WIN32_NO_STATUS
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000048#include "windef.h"
Alexandre Julliard014099c2004-03-12 01:56:49 +000049#include "winternl.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000050
Alexandre Julliard863637b2003-01-30 00:26:44 +000051#include "file.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000052#include "handle.h"
53#include "thread.h"
Alexandre Julliard5bc78081999-06-22 17:26:53 +000054#include "request.h"
Rob Shearmancb124c92007-10-03 20:19:05 +010055#include "process.h"
56#include "security.h"
Alexandre Julliard338e7571998-12-27 15:28:54 +000057
58struct file
59{
Alexandre Julliard57e11311999-05-16 16:59:38 +000060 struct object obj; /* object header */
Alexandre Julliarde66207e2003-02-19 00:33:32 +000061 struct fd *fd; /* file descriptor for this file */
Alexandre Julliarda510a7e2005-12-12 16:46:17 +010062 unsigned int access; /* file access (FILE_READ_DATA etc.) */
Alexandre Julliard133b8bc2007-04-23 15:13:22 +020063 mode_t mode; /* file stat.st_mode */
Rob Shearmancb124c92007-10-03 20:19:05 +010064 uid_t uid; /* file stat.st_uid */
Alexandre Julliard338e7571998-12-27 15:28:54 +000065};
66
Mike McCormack9a7124e2006-01-24 13:30:55 +010067static unsigned int generic_file_map_access( unsigned int access );
68
Alexandre Julliard338e7571998-12-27 15:28:54 +000069static void file_dump( struct object *obj, int verbose );
Alexandre Julliarde66207e2003-02-19 00:33:32 +000070static struct fd *file_get_fd( struct object *obj );
Rob Shearmancb124c92007-10-03 20:19:05 +010071static struct security_descriptor *file_get_sd( struct object *obj );
72static int file_set_sd( struct object *obj, const struct security_descriptor *sd, unsigned int set_info );
Alexandre Julliard338e7571998-12-27 15:28:54 +000073static void file_destroy( struct object *obj );
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +000074
75static int file_get_poll_events( struct fd *fd );
Alexandre Julliarddf651872007-03-27 16:51:44 +020076static void file_flush( struct fd *fd, struct event **event );
Alexandre Julliard7a9363a2007-04-10 22:26:23 +020077static enum server_fd_type file_get_fd_type( struct fd *fd );
Rob Shearman5f5df832007-10-26 17:02:16 +010078static mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner );
Alexandre Julliard338e7571998-12-27 15:28:54 +000079
80static const struct object_ops file_ops =
81{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000082 sizeof(struct file), /* size */
83 file_dump, /* dump */
Alexandre Julliard8382eb02007-12-05 18:16:42 +010084 no_get_type, /* get_type */
Alexandre Julliardba896e72007-04-04 19:39:29 +020085 add_queue, /* add_queue */
86 remove_queue, /* remove_queue */
Alexandre Julliard863637b2003-01-30 00:26:44 +000087 default_fd_signaled, /* signaled */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000088 no_satisfied, /* satisfied */
Mike McCormackf92fff62005-04-24 17:35:52 +000089 no_signal, /* signal */
Alexandre Julliarde66207e2003-02-19 00:33:32 +000090 file_get_fd, /* get_fd */
Alexandre Julliard24001e82007-10-02 14:20:15 +020091 default_fd_map_access, /* map_access */
Rob Shearmancb124c92007-10-03 20:19:05 +010092 file_get_sd, /* get_sd */
93 file_set_sd, /* set_sd */
Vitaliy Margolenbaffcb92005-11-22 14:55:42 +000094 no_lookup_name, /* lookup_name */
Alexandre Julliard7e71c1d2007-03-22 11:44:29 +010095 no_open_file, /* open_file */
Alexandre Julliard715d78e2006-11-02 20:52:22 +010096 fd_close_handle, /* close_handle */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000097 file_destroy /* destroy */
Alexandre Julliard338e7571998-12-27 15:28:54 +000098};
99
Alexandre Julliard863637b2003-01-30 00:26:44 +0000100static const struct fd_ops file_fd_ops =
101{
102 file_get_poll_events, /* get_poll_events */
Robert Shearmand6a4e342005-06-07 20:09:01 +0000103 default_poll_event, /* poll_event */
Alexandre Julliard863637b2003-01-30 00:26:44 +0000104 file_flush, /* flush */
Alexandre Julliard7a9363a2007-04-10 22:26:23 +0200105 file_get_fd_type, /* get_fd_type */
Alexandre Julliard63571432007-04-16 14:45:03 +0200106 default_fd_ioctl, /* ioctl */
Robert Shearmand6a4e342005-06-07 20:09:01 +0000107 default_fd_queue_async, /* queue_async */
Alexandre Julliard72bff2e2007-04-10 17:07:27 +0200108 default_fd_reselect_async, /* reselect_async */
Robert Shearmand6a4e342005-06-07 20:09:01 +0000109 default_fd_cancel_async /* cancel_async */
Alexandre Julliard863637b2003-01-30 00:26:44 +0000110};
Alexandre Julliard05625391999-01-03 11:55:56 +0000111
Alexandre Julliard014099c2004-03-12 01:56:49 +0000112static inline int is_overlapped( const struct file *file )
113{
Alexandre Julliardf85437c2007-04-10 22:25:07 +0200114 return !(get_fd_options( file->fd ) & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
Alexandre Julliard014099c2004-03-12 01:56:49 +0000115}
116
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000117/* create a file from a file descriptor */
118/* if the function fails the fd is closed */
Alexandre Julliard014099c2004-03-12 01:56:49 +0000119static struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing )
Alexandre Julliard05625391999-01-03 11:55:56 +0000120{
121 struct file *file;
Alexandre Julliard133b8bc2007-04-23 15:13:22 +0200122 struct stat st;
123
124 if (fstat( fd, &st ) == -1)
125 {
126 file_set_error();
127 return NULL;
128 }
Alexandre Julliard863637b2003-01-30 00:26:44 +0000129
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000130 if ((file = alloc_object( &file_ops )))
Alexandre Julliard05625391999-01-03 11:55:56 +0000131 {
Alexandre Julliard133b8bc2007-04-23 15:13:22 +0200132 file->mode = st.st_mode;
Alexandre Julliard24001e82007-10-02 14:20:15 +0200133 file->access = default_fd_map_access( &file->obj, access );
Alexandre Julliardf85437c2007-04-10 22:25:07 +0200134 if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj,
135 FILE_SYNCHRONOUS_IO_NONALERT )))
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000136 {
137 release_object( file );
138 return NULL;
139 }
Alexandre Julliard05625391999-01-03 11:55:56 +0000140 }
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000141 return file;
Alexandre Julliard338e7571998-12-27 15:28:54 +0000142}
143
Alexandre Julliard133b8bc2007-04-23 15:13:22 +0200144static struct object *create_file_obj( struct fd *fd, unsigned int access, mode_t mode )
Mike McCormack9a7124e2006-01-24 13:30:55 +0100145{
146 struct file *file = alloc_object( &file_ops );
147
148 if (!file) return NULL;
149 file->access = access;
Alexandre Julliard133b8bc2007-04-23 15:13:22 +0200150 file->mode = mode;
Mike McCormack9a7124e2006-01-24 13:30:55 +0100151 file->fd = fd;
152 grab_object( fd );
153 set_fd_user( fd, &file_fd_ops, &file->obj );
154 return &file->obj;
155}
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000156
Alexandre Julliard0f273c12006-07-26 10:43:25 +0200157static struct object *create_file( const char *nameptr, data_size_t len, unsigned int access,
Alexandre Julliardadc06102004-03-18 04:08:48 +0000158 unsigned int sharing, int create, unsigned int options,
Rob Shearman5f5df832007-10-26 17:02:16 +0100159 unsigned int attrs, const struct security_descriptor *sd )
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000160{
Mike McCormack9a7124e2006-01-24 13:30:55 +0100161 struct object *obj = NULL;
162 struct fd *fd;
Alexandre Julliard471782a2006-01-25 15:06:48 +0100163 int flags;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000164 char *name;
Bernhard Rosenkraenzer5dda1f72001-07-23 18:09:41 +0000165 mode_t mode;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000166
167 if (!(name = mem_alloc( len + 1 ))) return NULL;
168 memcpy( name, nameptr, len );
169 name[len] = 0;
170
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000171 switch(create)
172 {
Alexandre Julliard014099c2004-03-12 01:56:49 +0000173 case FILE_CREATE: flags = O_CREAT | O_EXCL; break;
174 case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
175 case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break;
176 case FILE_OPEN: flags = 0; break;
177 case FILE_OPEN_IF: flags = O_CREAT; break;
178 case FILE_OVERWRITE: flags = O_TRUNC; break;
Mike McCormack9a7124e2006-01-24 13:30:55 +0100179 default: set_error( STATUS_INVALID_PARAMETER ); goto done;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000180 }
Eric Pouech46344472005-01-14 19:54:38 +0000181
Rob Shearman5f5df832007-10-26 17:02:16 +0100182 if (sd)
183 {
184 const SID *owner = sd_get_owner( sd );
185 if (!owner)
186 owner = token_get_user( current->process->token );
187 mode = sd_to_mode( sd, owner );
188 }
189 else
190 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
Bernhard Rosenkraenzer5dda1f72001-07-23 18:09:41 +0000191
192 if (len >= 4 &&
193 (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
Rob Shearman5f5df832007-10-26 17:02:16 +0100194 {
195 if (mode & S_IRUSR)
196 mode |= S_IXUSR;
197 if (mode & S_IRGRP)
198 mode |= S_IXGRP;
199 if (mode & S_IROTH)
200 mode |= S_IXOTH;
201 }
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000202
Mike McCormack9a7124e2006-01-24 13:30:55 +0100203 access = generic_file_map_access( access );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000204
Alexandre Julliard580da242003-03-12 22:38:14 +0000205 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
Mike McCormack9a7124e2006-01-24 13:30:55 +0100206 fd = open_fd( name, flags | O_NONBLOCK | O_LARGEFILE, &mode, access, sharing, options );
207 if (!fd) goto done;
208
Mike McCormack08351072006-01-27 12:13:56 +0100209 if (S_ISDIR(mode))
210 obj = create_dir_obj( fd );
211 else if (S_ISCHR(mode) && is_serial_fd( fd ))
Alexandre Julliardf85437c2007-04-10 22:25:07 +0200212 obj = create_serial( fd );
Mike McCormack9a7124e2006-01-24 13:30:55 +0100213 else
Alexandre Julliard133b8bc2007-04-23 15:13:22 +0200214 obj = create_file_obj( fd, access, mode );
Mike McCormack9a7124e2006-01-24 13:30:55 +0100215
216 release_object( fd );
217
218done:
Alexandre Julliard9ff78722004-04-02 19:50:49 +0000219 free( name );
Mike McCormack9a7124e2006-01-24 13:30:55 +0100220 return obj;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000221}
222
Alexandre Julliard84fdfd02001-04-13 22:38:39 +0000223/* check if two file objects point to the same file */
224int is_same_file( struct file *file1, struct file *file2 )
225{
Alexandre Julliard74bd1e42004-03-27 20:48:42 +0000226 return is_same_file_fd( file1->fd, file2->fd );
Alexandre Julliard84fdfd02001-04-13 22:38:39 +0000227}
228
Alexandre Julliard580da242003-03-12 22:38:14 +0000229/* create a temp file for anonymous mappings */
230struct file *create_temp_file( int access )
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000231{
Alexandre Julliard580da242003-03-12 22:38:14 +0000232 char tmpfn[16];
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000233 int fd;
234
Alexandre Julliard580da242003-03-12 22:38:14 +0000235 sprintf( tmpfn, "anonmap.XXXXXX" ); /* create it in the server directory */
Alexandre Julliard2ee8b5b2003-03-20 21:07:49 +0000236 fd = mkstemps( tmpfn, 0 );
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000237 if (fd == -1)
238 {
239 file_set_error();
Alexandre Julliard580da242003-03-12 22:38:14 +0000240 return NULL;
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000241 }
Marcus Meissner786d2492002-07-29 23:55:39 +0000242 unlink( tmpfn );
Alexandre Julliard014099c2004-03-12 01:56:49 +0000243 return create_file_for_fd( fd, access, 0 );
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000244}
245
Alexandre Julliard338e7571998-12-27 15:28:54 +0000246static void file_dump( struct object *obj, int verbose )
247{
248 struct file *file = (struct file *)obj;
249 assert( obj->ops == &file_ops );
Alexandre Julliardf85437c2007-04-10 22:25:07 +0200250 fprintf( stderr, "File fd=%p\n", file->fd );
Alexandre Julliard338e7571998-12-27 15:28:54 +0000251}
252
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000253static int file_get_poll_events( struct fd *fd )
Alexandre Julliard338e7571998-12-27 15:28:54 +0000254{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000255 struct file *file = get_fd_user( fd );
Alexandre Julliard57e11311999-05-16 16:59:38 +0000256 int events = 0;
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000257 assert( file->obj.ops == &file_ops );
Alexandre Julliarda510a7e2005-12-12 16:46:17 +0100258 if (file->access & FILE_UNIX_READ_ACCESS) events |= POLLIN;
259 if (file->access & FILE_UNIX_WRITE_ACCESS) events |= POLLOUT;
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000260 return events;
Alexandre Julliard338e7571998-12-27 15:28:54 +0000261}
262
Alexandre Julliarddf651872007-03-27 16:51:44 +0200263static void file_flush( struct fd *fd, struct event **event )
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000264{
Alexandre Julliarddf651872007-03-27 16:51:44 +0200265 int unix_fd = get_unix_fd( fd );
266 if (unix_fd != -1 && fsync( unix_fd ) == -1) file_set_error();
Alexandre Julliard338e7571998-12-27 15:28:54 +0000267}
268
Alexandre Julliard7a9363a2007-04-10 22:26:23 +0200269static enum server_fd_type file_get_fd_type( struct fd *fd )
Alexandre Julliard338e7571998-12-27 15:28:54 +0000270{
Alexandre Julliard133b8bc2007-04-23 15:13:22 +0200271 struct file *file = get_fd_user( fd );
272
273 if (S_ISREG(file->mode) || S_ISBLK(file->mode)) return FD_TYPE_FILE;
274 if (S_ISDIR(file->mode)) return FD_TYPE_DIR;
275 return FD_TYPE_CHAR;
Alexandre Julliard05625391999-01-03 11:55:56 +0000276}
277
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000278static struct fd *file_get_fd( struct object *obj )
279{
280 struct file *file = (struct file *)obj;
281 assert( obj->ops == &file_ops );
282 return (struct fd *)grab_object( file->fd );
Martin Wilck718b1b72002-01-07 21:02:15 +0000283}
284
Mike McCormack9a7124e2006-01-24 13:30:55 +0100285static unsigned int generic_file_map_access( unsigned int access )
Alexandre Julliarda510a7e2005-12-12 16:46:17 +0100286{
287 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
288 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
289 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
290 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
291 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
292}
293
Rob Shearmancb124c92007-10-03 20:19:05 +0100294static struct security_descriptor *file_get_sd( struct object *obj )
295{
296 struct file *file = (struct file *)obj;
297 struct stat st;
298 int unix_fd;
299 struct security_descriptor *sd;
300 const SID *user;
301 const SID *group;
302 size_t dacl_size;
Rob Shearmanec8a27f2007-11-20 17:49:07 +0000303 ACE_HEADER *current_ace;
Rob Shearmancb124c92007-10-03 20:19:05 +0100304 ACCESS_ALLOWED_ACE *aaa;
305 ACL *dacl;
306 SID *sid;
307 char *ptr;
308 const SID *world_sid = security_world_sid;
309 const SID *local_system_sid = security_local_system_sid;
310
311 assert( obj->ops == &file_ops );
312
313 unix_fd = get_file_unix_fd( file );
314
315 if (unix_fd == -1) return obj->sd;
316
317 if (fstat( unix_fd, &st ) == -1)
318 return obj->sd;
319
320 /* mode and uid the same? if so, no need to re-generate security descriptor */
321 if (obj->sd && (st.st_mode & (S_IRWXU|S_IRWXO)) == (file->mode & (S_IRWXU|S_IRWXO)) &&
322 (st.st_uid == file->uid))
323 return obj->sd;
324
325 user = security_unix_uid_to_sid( st.st_uid );
326 group = token_get_primary_group( current->process->token );
327
328 dacl_size = sizeof(ACL) + FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
329 FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]);
330 if (st.st_mode & S_IRWXU)
331 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
332 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
Rob Shearmanec8a27f2007-11-20 17:49:07 +0000333 if ((!(st.st_mode & S_IRUSR) && (st.st_mode & (S_IRGRP|S_IROTH))) ||
334 (!(st.st_mode & S_IWUSR) && (st.st_mode & (S_IWGRP|S_IWOTH))) ||
335 (!(st.st_mode & S_IXUSR) && (st.st_mode & (S_IXGRP|S_IXOTH))))
336 dacl_size += FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) +
337 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
Rob Shearmancb124c92007-10-03 20:19:05 +0100338 if (st.st_mode & S_IRWXO)
339 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
340 FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]);
341
342 sd = mem_alloc( sizeof(struct security_descriptor) +
343 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) +
344 FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]) +
345 dacl_size );
346 if (!sd) return obj->sd;
347
348 sd->control = SE_DACL_PRESENT;
349 sd->owner_len = FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
350 sd->group_len = FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]);
351 sd->sacl_len = 0;
352 sd->dacl_len = dacl_size;
353
354 ptr = (char *)(sd + 1);
355 memcpy( ptr, user, sd->owner_len );
356 ptr += sd->owner_len;
357 memcpy( ptr, group, sd->group_len );
358 ptr += sd->group_len;
359
360 dacl = (ACL *)ptr;
361 dacl->AclRevision = ACL_REVISION;
362 dacl->Sbz1 = 0;
363 dacl->AclSize = dacl_size;
364 dacl->AceCount = 1 + (st.st_mode & S_IRWXU ? 1 : 0) + (st.st_mode & S_IRWXO ? 1 : 0);
Rob Shearmanec8a27f2007-11-20 17:49:07 +0000365 if ((!(st.st_mode & S_IRUSR) && (st.st_mode & (S_IRGRP|S_IROTH))) ||
366 (!(st.st_mode & S_IWUSR) && (st.st_mode & (S_IWGRP|S_IWOTH))) ||
367 (!(st.st_mode & S_IXUSR) && (st.st_mode & (S_IXGRP|S_IXOTH))))
368 dacl->AceCount++;
Rob Shearmancb124c92007-10-03 20:19:05 +0100369 dacl->Sbz2 = 0;
370
371 /* always give FILE_ALL_ACCESS for Local System */
372 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
Rob Shearmanec8a27f2007-11-20 17:49:07 +0000373 current_ace = &aaa->Header;
Rob Shearmancb124c92007-10-03 20:19:05 +0100374 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
375 aaa->Header.AceFlags = 0;
376 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
377 FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]);
378 aaa->Mask = FILE_ALL_ACCESS;
379 sid = (SID *)&aaa->SidStart;
380 memcpy( sid, local_system_sid, FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]) );
381
382 if (st.st_mode & S_IRWXU)
383 {
384 /* appropriate access rights for the user */
Rob Shearmanec8a27f2007-11-20 17:49:07 +0000385 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
386 current_ace = &aaa->Header;
Rob Shearmancb124c92007-10-03 20:19:05 +0100387 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
388 aaa->Header.AceFlags = 0;
389 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
390 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
391 aaa->Mask = WRITE_DAC | WRITE_OWNER;
392 if (st.st_mode & S_IRUSR)
393 aaa->Mask |= FILE_GENERIC_READ;
394 if (st.st_mode & S_IWUSR)
395 aaa->Mask |= FILE_GENERIC_WRITE | DELETE;
396 if (st.st_mode & S_IXUSR)
397 aaa->Mask |= FILE_GENERIC_EXECUTE;
398 sid = (SID *)&aaa->SidStart;
399 memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
400 }
Rob Shearmanec8a27f2007-11-20 17:49:07 +0000401 if ((!(st.st_mode & S_IRUSR) && (st.st_mode & (S_IRGRP|S_IROTH))) ||
402 (!(st.st_mode & S_IWUSR) && (st.st_mode & (S_IWGRP|S_IWOTH))) ||
403 (!(st.st_mode & S_IXUSR) && (st.st_mode & (S_IXGRP|S_IXOTH))))
404 {
405 /* deny just in case the user is a member of the group */
406 ACCESS_DENIED_ACE *ada = (ACCESS_DENIED_ACE *)ace_next( current_ace );
407 current_ace = &ada->Header;
408 ada->Header.AceType = ACCESS_DENIED_ACE_TYPE;
409 ada->Header.AceFlags = 0;
410 ada->Header.AceSize = FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) +
411 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
412 ada->Mask = 0;
413 if (!(st.st_mode & S_IRUSR) && (st.st_mode & (S_IRGRP|S_IROTH)))
414 ada->Mask |= FILE_GENERIC_READ;
415 if (!(st.st_mode & S_IWUSR) && (st.st_mode & (S_IWGRP|S_IROTH)))
416 ada->Mask |= FILE_GENERIC_WRITE | DELETE;
417 if (!(st.st_mode & S_IXUSR) && (st.st_mode & (S_IXGRP|S_IXOTH)))
418 ada->Mask |= FILE_GENERIC_EXECUTE;
419 ada->Mask &= ~STANDARD_RIGHTS_ALL; /* never deny standard rights */
420 sid = (SID *)&ada->SidStart;
421 memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
422 }
Rob Shearmancb124c92007-10-03 20:19:05 +0100423 if (st.st_mode & S_IRWXO)
424 {
425 /* appropriate access rights for Everyone */
Rob Shearmanec8a27f2007-11-20 17:49:07 +0000426 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
427 current_ace = &aaa->Header;
Rob Shearmancb124c92007-10-03 20:19:05 +0100428 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
429 aaa->Header.AceFlags = 0;
430 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
431 FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]);
432 aaa->Mask = 0;
433 if (st.st_mode & S_IROTH)
434 aaa->Mask |= FILE_GENERIC_READ;
435 if (st.st_mode & S_IWOTH)
436 aaa->Mask |= FILE_GENERIC_WRITE | DELETE;
437 if (st.st_mode & S_IXOTH)
438 aaa->Mask |= FILE_GENERIC_EXECUTE;
439 sid = (SID *)&aaa->SidStart;
440 memcpy( sid, world_sid, FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]) );
441 }
442
443 file->mode = st.st_mode;
444 file->uid = st.st_uid;
445 free( obj->sd );
446 obj->sd = sd;
447 return sd;
448}
449
Rob Shearman5f5df832007-10-26 17:02:16 +0100450static mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner )
Rob Shearmancb124c92007-10-03 20:19:05 +0100451{
Rob Shearman5f5df832007-10-26 17:02:16 +0100452 mode_t new_mode = 0;
Rob Shearmancb124c92007-10-03 20:19:05 +0100453 mode_t denied_mode = 0;
Rob Shearman5f5df832007-10-26 17:02:16 +0100454 int present;
455 const ACL *dacl = sd_get_dacl( sd, &present );
456 if (present && dacl)
Rob Shearmancb124c92007-10-03 20:19:05 +0100457 {
Rob Shearman5f5df832007-10-26 17:02:16 +0100458 const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
459 ULONG i;
Rob Shearman6753fdc2007-11-20 10:47:11 +0000460 for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
Rob Shearmancb124c92007-10-03 20:19:05 +0100461 {
Rob Shearman5f5df832007-10-26 17:02:16 +0100462 const ACCESS_ALLOWED_ACE *aa_ace;
463 const ACCESS_DENIED_ACE *ad_ace;
464 const SID *sid;
Rob Shearmancb124c92007-10-03 20:19:05 +0100465
Rob Shearman5f5df832007-10-26 17:02:16 +0100466 if (ace->AceFlags & INHERIT_ONLY_ACE) continue;
Rob Shearmanf43d8b62007-10-06 14:31:25 +0200467
Rob Shearman5f5df832007-10-26 17:02:16 +0100468 switch (ace->AceType)
Rob Shearmancb124c92007-10-03 20:19:05 +0100469 {
Rob Shearmancb124c92007-10-03 20:19:05 +0100470 case ACCESS_DENIED_ACE_TYPE:
471 ad_ace = (const ACCESS_DENIED_ACE *)ace;
472 sid = (const SID *)&ad_ace->SidStart;
473 if (security_equal_sid( sid, security_world_sid ))
474 {
475 unsigned int access = generic_file_map_access( ad_ace->Mask );
476 if (access & FILE_READ_DATA)
Rob Shearmanec8a27f2007-11-20 17:49:07 +0000477 denied_mode |= S_IRUSR|S_IRGRP|S_IROTH;
Rob Shearmancb124c92007-10-03 20:19:05 +0100478 if (access & FILE_WRITE_DATA)
Rob Shearmanec8a27f2007-11-20 17:49:07 +0000479 denied_mode |= S_IWUSR|S_IWGRP|S_IWOTH;
Rob Shearmancb124c92007-10-03 20:19:05 +0100480 if (access & FILE_EXECUTE)
Rob Shearmanec8a27f2007-11-20 17:49:07 +0000481 denied_mode |= S_IXUSR|S_IXGRP|S_IXOTH;
Rob Shearmancb124c92007-10-03 20:19:05 +0100482 }
483 else if (security_equal_sid( sid, owner ))
484 {
485 unsigned int access = generic_file_map_access( ad_ace->Mask );
486 if (access & FILE_READ_DATA)
487 denied_mode |= S_IRUSR;
488 if (access & FILE_WRITE_DATA)
489 denied_mode |= S_IWUSR;
490 if (access & FILE_EXECUTE)
491 denied_mode |= S_IXUSR;
492 }
493 break;
494 case ACCESS_ALLOWED_ACE_TYPE:
495 aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
496 sid = (const SID *)&aa_ace->SidStart;
497 if (security_equal_sid( sid, security_world_sid ))
498 {
499 unsigned int access = generic_file_map_access( aa_ace->Mask );
500 if (access & FILE_READ_DATA)
Rob Shearmanec8a27f2007-11-20 17:49:07 +0000501 new_mode |= S_IRUSR|S_IRGRP|S_IROTH;
Rob Shearmancb124c92007-10-03 20:19:05 +0100502 if (access & FILE_WRITE_DATA)
Rob Shearmanec8a27f2007-11-20 17:49:07 +0000503 new_mode |= S_IWUSR|S_IWGRP|S_IWOTH;
Rob Shearmancb124c92007-10-03 20:19:05 +0100504 if (access & FILE_EXECUTE)
Rob Shearmanec8a27f2007-11-20 17:49:07 +0000505 new_mode |= S_IXUSR|S_IXGRP|S_IXOTH;
Rob Shearmancb124c92007-10-03 20:19:05 +0100506 }
507 else if (security_equal_sid( sid, owner ))
508 {
509 unsigned int access = generic_file_map_access( aa_ace->Mask );
510 if (access & FILE_READ_DATA)
511 new_mode |= S_IRUSR;
512 if (access & FILE_WRITE_DATA)
513 new_mode |= S_IWUSR;
514 if (access & FILE_EXECUTE)
515 new_mode |= S_IXUSR;
516 }
517 break;
Rob Shearmancb124c92007-10-03 20:19:05 +0100518 }
519 }
Rob Shearman5f5df832007-10-26 17:02:16 +0100520 }
521 else
522 /* no ACL means full access rights to anyone */
523 new_mode = S_IRWXU | S_IRWXO;
Rob Shearmancb124c92007-10-03 20:19:05 +0100524
Rob Shearman5f5df832007-10-26 17:02:16 +0100525 return new_mode & ~denied_mode;
526}
527
528static int file_set_sd( struct object *obj, const struct security_descriptor *sd,
529 unsigned int set_info )
530{
531 struct file *file = (struct file *)obj;
532 const SID *owner;
533 mode_t mode;
534 int unix_fd;
535
536 assert( obj->ops == &file_ops );
537
538 unix_fd = get_file_unix_fd( file );
539
540 if (unix_fd == -1) return 1;
541
542 if (set_info & OWNER_SECURITY_INFORMATION)
543 {
544 owner = sd_get_owner( sd );
545 if (!owner)
Rob Shearmancb124c92007-10-03 20:19:05 +0100546 {
Rob Shearman5f5df832007-10-26 17:02:16 +0100547 set_error( STATUS_INVALID_SECURITY_DESCR );
548 return 0;
549 }
550 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
551 {
552 /* FIXME: get Unix uid and call fchown */
553 }
554 }
555 else if (obj->sd)
556 owner = sd_get_owner( obj->sd );
557 else
558 owner = token_get_user( current->process->token );
559
560 /* group and sacl not supported */
561
562 if (set_info & DACL_SECURITY_INFORMATION)
563 {
564 /* keep the bits that we don't map to access rights in the ACL */
565 mode = file->mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXG);
566 mode |= sd_to_mode( sd, owner );
567
568 if (file->mode != mode)
569 {
570 if (fchmod( unix_fd, mode ) == -1)
Rob Shearmanf5070b02007-10-06 14:31:34 +0200571 {
572 file_set_error();
573 return 0;
574 }
Rob Shearmancb124c92007-10-03 20:19:05 +0100575
Rob Shearman5f5df832007-10-26 17:02:16 +0100576 file->mode = mode;
Rob Shearmanf5070b02007-10-06 14:31:34 +0200577 }
Rob Shearmancb124c92007-10-03 20:19:05 +0100578 }
579 return 1;
580}
581
Alexandre Julliard05625391999-01-03 11:55:56 +0000582static void file_destroy( struct object *obj )
583{
Alexandre Julliard05625391999-01-03 11:55:56 +0000584 struct file *file = (struct file *)obj;
585 assert( obj->ops == &file_ops );
586
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000587 if (file->fd) release_object( file->fd );
Alexandre Julliard338e7571998-12-27 15:28:54 +0000588}
589
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000590/* set the last error depending on errno */
591void file_set_error(void)
Alexandre Julliard338e7571998-12-27 15:28:54 +0000592{
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000593 switch (errno)
594 {
Dmitry Timoshkovc75edd02007-09-11 13:07:29 +0900595 case ETXTBSY:
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000596 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
597 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
598 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000599 case EACCES:
Alexandre Julliard5f258c62001-07-14 00:50:30 +0000600 case ESRCH:
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000601 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
602 case EROFS: set_error( STATUS_MEDIA_WRITE_PROTECTED ); break;
603 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
604 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
Alexandre Julliard716878c2004-04-16 23:32:40 +0000605 case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000606 case ENFILE:
Robert Shearman4d7b2302006-08-29 21:11:28 +0100607 case EMFILE: set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000608 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
609 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
Dmitry Timoshkovced2e562007-01-08 11:16:59 +0800610 case ESPIPE: set_error( STATUS_ILLEGAL_FUNCTION ); break;
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000611 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
612 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
Alexandre Julliard014099c2004-03-12 01:56:49 +0000613 case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
Alexandre Julliard72f87b82004-05-01 02:50:06 +0000614 case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
Mike McCormackea782b62004-07-06 19:42:09 +0000615 case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break;
616 case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break;
Wim Lewis99a01c02004-01-02 20:11:35 +0000617#ifdef EOVERFLOW
Alexandre Julliardce613492003-03-18 05:04:33 +0000618 case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
Wim Lewis99a01c02004-01-02 20:11:35 +0000619#endif
Dmitry Timoshkov69025ba2007-09-04 22:51:39 +0900620 default:
621 perror("wineserver: file_set_error() can't map error");
622 set_error( STATUS_UNSUCCESSFUL );
623 break;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000624 }
Alexandre Julliard338e7571998-12-27 15:28:54 +0000625}
626
Alexandre Julliard51885742002-05-30 20:12:58 +0000627struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
Alexandre Julliard338e7571998-12-27 15:28:54 +0000628{
Alexandre Julliard61ec6c11999-11-29 02:17:08 +0000629 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000630}
Alexandre Julliard338e7571998-12-27 15:28:54 +0000631
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000632int get_file_unix_fd( struct file *file )
633{
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000634 return get_unix_fd( file->fd );
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000635}
636
Alexandre Julliard5bd51362007-01-12 14:42:43 +0100637struct file *grab_file_unless_removable( struct file *file )
638{
639 if (is_fd_removable( file->fd )) return NULL;
640 return (struct file *)grab_object( file );
641}
642
Alexandre Julliard86e5efb2002-01-29 02:51:12 +0000643/* extend a file beyond the current end of file */
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000644static int extend_file( struct file *file, file_pos_t new_size )
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000645{
Alexandre Julliard86e5efb2002-01-29 02:51:12 +0000646 static const char zero;
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000647 int unix_fd = get_file_unix_fd( file );
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000648 off_t size = new_size;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000649
Alexandre Julliard964815b2005-08-08 15:11:03 +0000650 if (unix_fd == -1) return 0;
651
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000652 if (sizeof(new_size) > sizeof(size) && size != new_size)
653 {
654 set_error( STATUS_INVALID_PARAMETER );
655 return 0;
656 }
Alexandre Julliard86e5efb2002-01-29 02:51:12 +0000657 /* extend the file one byte beyond the requested size and then truncate it */
658 /* this should work around ftruncate implementations that can't extend files */
Alexandre Julliard72f87b82004-05-01 02:50:06 +0000659 if (pwrite( unix_fd, &zero, 1, size ) != -1)
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000660 {
Alexandre Julliard863637b2003-01-30 00:26:44 +0000661 ftruncate( unix_fd, size );
Alexandre Julliard86e5efb2002-01-29 02:51:12 +0000662 return 1;
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000663 }
Alexandre Julliard86e5efb2002-01-29 02:51:12 +0000664 file_set_error();
665 return 0;
666}
667
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000668/* try to grow the file to the specified size */
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000669int grow_file( struct file *file, file_pos_t size )
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000670{
Marcus Meissner6bb990f2001-05-29 20:55:21 +0000671 struct stat st;
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000672 int unix_fd = get_file_unix_fd( file );
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000673
Alexandre Julliard964815b2005-08-08 15:11:03 +0000674 if (unix_fd == -1) return 0;
675
Alexandre Julliard863637b2003-01-30 00:26:44 +0000676 if (fstat( unix_fd, &st ) == -1)
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000677 {
678 file_set_error();
679 return 0;
680 }
Marcus Meissner3f1ed522001-05-14 20:09:37 +0000681 if (st.st_size >= size) return 1; /* already large enough */
Alexandre Julliard72f87b82004-05-01 02:50:06 +0000682 return extend_file( file, size );
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000683}
684
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000685/* create a file */
686DECL_HANDLER(create_file)
687{
Alexandre Julliardadc06102004-03-18 04:08:48 +0000688 struct object *file;
Rob Shearman5f5df832007-10-26 17:02:16 +0100689 const struct object_attributes *objattr = get_req_data();
690 const struct security_descriptor *sd;
691 const char *name;
692 data_size_t name_len;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000693
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000694 reply->handle = 0;
Rob Shearman5f5df832007-10-26 17:02:16 +0100695
696 if (!objattr_is_valid( objattr, get_req_data_size() ))
697 return;
698 /* name is transferred in the unix codepage outside of the objattr structure */
699 if (objattr->name_len)
700 {
701 set_error( STATUS_INVALID_PARAMETER );
702 return;
703 }
704
705 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
706
707 name = (const char *)get_req_data() + sizeof(*objattr) + objattr->sd_len;
708 name_len = get_req_data_size() - sizeof(*objattr) - objattr->sd_len;
709
710 reply->handle = 0;
711 if ((file = create_file( name, name_len, req->access,
712 req->sharing, req->create, req->options,
713 req->attrs, sd )))
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000714 {
Alexandre Julliard24560e72005-12-09 13:58:25 +0100715 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000716 release_object( file );
717 }
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000718}
719
720/* allocate a file handle for a Unix fd */
721DECL_HANDLER(alloc_file_handle)
722{
723 struct file *file;
Alexandre Julliardf5242402001-02-28 21:45:23 +0000724 int fd;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000725
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000726 reply->handle = 0;
Alexandre Julliardf5242402001-02-28 21:45:23 +0000727 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000728 {
Alexandre Julliardf5242402001-02-28 21:45:23 +0000729 set_error( STATUS_INVALID_HANDLE );
730 return;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000731 }
Alexandre Julliard014099c2004-03-12 01:56:49 +0000732 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
Alexandre Julliardf5242402001-02-28 21:45:23 +0000733 {
Alexandre Julliard24560e72005-12-09 13:58:25 +0100734 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
Alexandre Julliardf5242402001-02-28 21:45:23 +0000735 release_object( file );
736 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000737}
738
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000739/* lock a region of a file */
740DECL_HANDLER(lock_file)
741{
742 struct file *file;
743
744 if ((file = get_file_obj( current->process, req->handle, 0 )))
745 {
Alexandre Julliard235532c2007-10-10 14:06:25 +0200746 reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
Alexandre Julliard014099c2004-03-12 01:56:49 +0000747 reply->overlapped = is_overlapped( file );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000748 release_object( file );
749 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000750}
751
752/* unlock a region of a file */
753DECL_HANDLER(unlock_file)
754{
755 struct file *file;
756
757 if ((file = get_file_obj( current->process, req->handle, 0 )))
758 {
Alexandre Julliard235532c2007-10-10 14:06:25 +0200759 unlock_fd( file->fd, req->offset, req->count );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000760 release_object( file );
761 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000762}