server: Fix file_set_sd to handle NULL DACLs.
[wine] / server / file.c
1 /*
2  * Server-side file management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
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
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <fcntl.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #ifdef HAVE_SYS_ERRNO_H
32 #include <sys/errno.h>
33 #endif
34 #include <sys/stat.h>
35 #include <sys/time.h>
36 #include <sys/types.h>
37 #include <time.h>
38 #include <unistd.h>
39 #ifdef HAVE_UTIME_H
40 #include <utime.h>
41 #endif
42 #ifdef HAVE_POLL_H
43 #include <poll.h>
44 #endif
45
46 #include "ntstatus.h"
47 #define WIN32_NO_STATUS
48 #include "windef.h"
49 #include "winternl.h"
50
51 #include "file.h"
52 #include "handle.h"
53 #include "thread.h"
54 #include "request.h"
55 #include "process.h"
56 #include "security.h"
57
58 struct file
59 {
60     struct object       obj;        /* object header */
61     struct fd          *fd;         /* file descriptor for this file */
62     unsigned int        access;     /* file access (FILE_READ_DATA etc.) */
63     mode_t              mode;       /* file stat.st_mode */
64     uid_t               uid;        /* file stat.st_uid */
65 };
66
67 static unsigned int generic_file_map_access( unsigned int access );
68
69 static void file_dump( struct object *obj, int verbose );
70 static struct fd *file_get_fd( struct object *obj );
71 static struct security_descriptor *file_get_sd( struct object *obj );
72 static int file_set_sd( struct object *obj, const struct security_descriptor *sd, unsigned int set_info );
73 static void file_destroy( struct object *obj );
74
75 static int file_get_poll_events( struct fd *fd );
76 static void file_flush( struct fd *fd, struct event **event );
77 static enum server_fd_type file_get_fd_type( struct fd *fd );
78
79 static const struct object_ops file_ops =
80 {
81     sizeof(struct file),          /* size */
82     file_dump,                    /* dump */
83     add_queue,                    /* add_queue */
84     remove_queue,                 /* remove_queue */
85     default_fd_signaled,          /* signaled */
86     no_satisfied,                 /* satisfied */
87     no_signal,                    /* signal */
88     file_get_fd,                  /* get_fd */
89     default_fd_map_access,        /* map_access */
90     file_get_sd,                  /* get_sd */
91     file_set_sd,                  /* set_sd */
92     no_lookup_name,               /* lookup_name */
93     no_open_file,                 /* open_file */
94     fd_close_handle,              /* close_handle */
95     file_destroy                  /* destroy */
96 };
97
98 static const struct fd_ops file_fd_ops =
99 {
100     file_get_poll_events,         /* get_poll_events */
101     default_poll_event,           /* poll_event */
102     file_flush,                   /* flush */
103     file_get_fd_type,             /* get_fd_type */
104     default_fd_ioctl,             /* ioctl */
105     default_fd_queue_async,       /* queue_async */
106     default_fd_reselect_async,    /* reselect_async */
107     default_fd_cancel_async       /* cancel_async */
108 };
109
110 static inline int is_overlapped( const struct file *file )
111 {
112     return !(get_fd_options( file->fd ) & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
113 }
114
115 /* create a file from a file descriptor */
116 /* if the function fails the fd is closed */
117 static struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing )
118 {
119     struct file *file;
120     struct stat st;
121
122     if (fstat( fd, &st ) == -1)
123     {
124         file_set_error();
125         return NULL;
126     }
127
128     if ((file = alloc_object( &file_ops )))
129     {
130         file->mode = st.st_mode;
131         file->access = default_fd_map_access( &file->obj, access );
132         if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj,
133                                               FILE_SYNCHRONOUS_IO_NONALERT )))
134         {
135             release_object( file );
136             return NULL;
137         }
138     }
139     return file;
140 }
141
142 static struct object *create_file_obj( struct fd *fd, unsigned int access, mode_t mode )
143 {
144     struct file *file = alloc_object( &file_ops );
145
146     if (!file) return NULL;
147     file->access  = access;
148     file->mode    = mode;
149     file->fd      = fd;
150     grab_object( fd );
151     set_fd_user( fd, &file_fd_ops, &file->obj );
152     return &file->obj;
153 }
154
155 static struct object *create_file( const char *nameptr, data_size_t len, unsigned int access,
156                                    unsigned int sharing, int create, unsigned int options,
157                                    unsigned int attrs )
158 {
159     struct object *obj = NULL;
160     struct fd *fd;
161     int flags;
162     char *name;
163     mode_t mode;
164
165     if (!(name = mem_alloc( len + 1 ))) return NULL;
166     memcpy( name, nameptr, len );
167     name[len] = 0;
168
169     switch(create)
170     {
171     case FILE_CREATE:       flags = O_CREAT | O_EXCL; break;
172     case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
173     case FILE_SUPERSEDE:    flags = O_CREAT | O_TRUNC; break;
174     case FILE_OPEN:         flags = 0; break;
175     case FILE_OPEN_IF:      flags = O_CREAT; break;
176     case FILE_OVERWRITE:    flags = O_TRUNC; break;
177     default:                set_error( STATUS_INVALID_PARAMETER ); goto done;
178     }
179
180     mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
181
182     if (len >= 4 &&
183         (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
184         mode |= 0111;
185
186     access = generic_file_map_access( access );
187
188     /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
189     fd = open_fd( name, flags | O_NONBLOCK | O_LARGEFILE, &mode, access, sharing, options );
190     if (!fd) goto done;
191
192     if (S_ISDIR(mode))
193         obj = create_dir_obj( fd );
194     else if (S_ISCHR(mode) && is_serial_fd( fd ))
195         obj = create_serial( fd );
196     else
197         obj = create_file_obj( fd, access, mode );
198
199     release_object( fd );
200
201 done:
202     free( name );
203     return obj;
204 }
205
206 /* check if two file objects point to the same file */
207 int is_same_file( struct file *file1, struct file *file2 )
208 {
209     return is_same_file_fd( file1->fd, file2->fd );
210 }
211
212 /* create a temp file for anonymous mappings */
213 struct file *create_temp_file( int access )
214 {
215     char tmpfn[16];
216     int fd;
217
218     sprintf( tmpfn, "anonmap.XXXXXX" );  /* create it in the server directory */
219     fd = mkstemps( tmpfn, 0 );
220     if (fd == -1)
221     {
222         file_set_error();
223         return NULL;
224     }
225     unlink( tmpfn );
226     return create_file_for_fd( fd, access, 0 );
227 }
228
229 static void file_dump( struct object *obj, int verbose )
230 {
231     struct file *file = (struct file *)obj;
232     assert( obj->ops == &file_ops );
233     fprintf( stderr, "File fd=%p\n", file->fd );
234 }
235
236 static int file_get_poll_events( struct fd *fd )
237 {
238     struct file *file = get_fd_user( fd );
239     int events = 0;
240     assert( file->obj.ops == &file_ops );
241     if (file->access & FILE_UNIX_READ_ACCESS) events |= POLLIN;
242     if (file->access & FILE_UNIX_WRITE_ACCESS) events |= POLLOUT;
243     return events;
244 }
245
246 static void file_flush( struct fd *fd, struct event **event )
247 {
248     int unix_fd = get_unix_fd( fd );
249     if (unix_fd != -1 && fsync( unix_fd ) == -1) file_set_error();
250 }
251
252 static enum server_fd_type file_get_fd_type( struct fd *fd )
253 {
254     struct file *file = get_fd_user( fd );
255
256     if (S_ISREG(file->mode) || S_ISBLK(file->mode)) return FD_TYPE_FILE;
257     if (S_ISDIR(file->mode)) return FD_TYPE_DIR;
258     return FD_TYPE_CHAR;
259 }
260
261 static struct fd *file_get_fd( struct object *obj )
262 {
263     struct file *file = (struct file *)obj;
264     assert( obj->ops == &file_ops );
265     return (struct fd *)grab_object( file->fd );
266 }
267
268 static unsigned int generic_file_map_access( unsigned int access )
269 {
270     if (access & GENERIC_READ)    access |= FILE_GENERIC_READ;
271     if (access & GENERIC_WRITE)   access |= FILE_GENERIC_WRITE;
272     if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
273     if (access & GENERIC_ALL)     access |= FILE_ALL_ACCESS;
274     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
275 }
276
277 static struct security_descriptor *file_get_sd( struct object *obj )
278 {
279     struct file *file = (struct file *)obj;
280     struct stat st;
281     int unix_fd;
282     struct security_descriptor *sd;
283     const SID *user;
284     const SID *group;
285     size_t dacl_size;
286     ACCESS_ALLOWED_ACE *aaa;
287     ACL *dacl;
288     SID *sid;
289     char *ptr;
290     const SID *world_sid = security_world_sid;
291     const SID *local_system_sid = security_local_system_sid;
292
293     assert( obj->ops == &file_ops );
294
295     unix_fd = get_file_unix_fd( file );
296
297     if (unix_fd == -1) return obj->sd;
298
299     if (fstat( unix_fd, &st ) == -1)
300         return obj->sd;
301
302     /* mode and uid the same? if so, no need to re-generate security descriptor */
303     if (obj->sd && (st.st_mode & (S_IRWXU|S_IRWXO)) == (file->mode & (S_IRWXU|S_IRWXO)) &&
304         (st.st_uid == file->uid))
305         return obj->sd;
306
307     user = security_unix_uid_to_sid( st.st_uid );
308     group = token_get_primary_group( current->process->token );
309
310     dacl_size = sizeof(ACL) + FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
311         FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]);
312     if (st.st_mode & S_IRWXU)
313         dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
314             FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
315     if (st.st_mode & S_IRWXO)
316         dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
317             FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]);
318
319     sd = mem_alloc( sizeof(struct security_descriptor) +
320                     FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) +
321                     FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]) +
322                     dacl_size );
323     if (!sd) return obj->sd;
324
325     sd->control = SE_DACL_PRESENT;
326     sd->owner_len = FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
327     sd->group_len = FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]);
328     sd->sacl_len = 0;
329     sd->dacl_len = dacl_size;
330
331     ptr = (char *)(sd + 1);
332     memcpy( ptr, user, sd->owner_len );
333     ptr += sd->owner_len;
334     memcpy( ptr, group, sd->group_len );
335     ptr += sd->group_len;
336
337     dacl = (ACL *)ptr;
338     dacl->AclRevision = ACL_REVISION;
339     dacl->Sbz1 = 0;
340     dacl->AclSize = dacl_size;
341     dacl->AceCount = 1 + (st.st_mode & S_IRWXU ? 1 : 0) + (st.st_mode & S_IRWXO ? 1 : 0);
342     dacl->Sbz2 = 0;
343
344     /* always give FILE_ALL_ACCESS for Local System */
345     aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
346     aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
347     aaa->Header.AceFlags = 0;
348     aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
349         FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]);
350     aaa->Mask = FILE_ALL_ACCESS;
351     sid = (SID *)&aaa->SidStart;
352     memcpy( sid, local_system_sid, FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]) );
353
354     if (st.st_mode & S_IRWXU)
355     {
356         /* appropriate access rights for the user */
357         aaa = (ACCESS_ALLOWED_ACE *)ace_next( &aaa->Header );
358         aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
359         aaa->Header.AceFlags = 0;
360         aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
361                               FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
362         aaa->Mask = WRITE_DAC | WRITE_OWNER;
363         if (st.st_mode & S_IRUSR)
364             aaa->Mask |= FILE_GENERIC_READ;
365         if (st.st_mode & S_IWUSR)
366             aaa->Mask |= FILE_GENERIC_WRITE | DELETE;
367         if (st.st_mode & S_IXUSR)
368             aaa->Mask |= FILE_GENERIC_EXECUTE;
369         sid = (SID *)&aaa->SidStart;
370         memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
371     }
372     if (st.st_mode & S_IRWXO)
373     {
374         /* appropriate access rights for Everyone */
375         aaa = (ACCESS_ALLOWED_ACE *)ace_next( &aaa->Header );
376         aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
377         aaa->Header.AceFlags = 0;
378         aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
379                              FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]);
380         aaa->Mask = 0;
381         if (st.st_mode & S_IROTH)
382             aaa->Mask |= FILE_GENERIC_READ;
383         if (st.st_mode & S_IWOTH)
384             aaa->Mask |= FILE_GENERIC_WRITE | DELETE;
385         if (st.st_mode & S_IXOTH)
386             aaa->Mask |= FILE_GENERIC_EXECUTE;
387         sid = (SID *)&aaa->SidStart;
388         memcpy( sid, world_sid, FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]) );
389     }
390
391     file->mode = st.st_mode;
392     file->uid = st.st_uid;
393     free( obj->sd );
394     obj->sd = sd;
395     return sd;
396 }
397
398 static int file_set_sd( struct object *obj, const struct security_descriptor *sd,
399                         unsigned int set_info )
400 {
401     struct file *file = (struct file *)obj;
402     mode_t new_mode;
403     mode_t denied_mode = 0;
404     const SID *owner;
405     int unix_fd;
406
407     assert( obj->ops == &file_ops );
408
409     unix_fd = get_file_unix_fd( file );
410
411     if (unix_fd == -1) return 1;
412
413     if (set_info & OWNER_SECURITY_INFORMATION)
414     {
415         owner = sd_get_owner( sd );
416         if (!owner)
417         {
418             set_error( STATUS_INVALID_SECURITY_DESCR );
419             return 0;
420         }
421         if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
422         {
423             /* FIXME: get Unix uid and call fchown */
424         }
425     }
426     else if (obj->sd)
427         owner = sd_get_owner( obj->sd );
428     else
429         owner = token_get_user( current->process->token );
430
431     /* group and sacl not supported */
432
433     /* keep the bits that we don't map to access rights in the ACL */
434     new_mode = file->mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXG);
435
436     if (set_info & DACL_SECURITY_INFORMATION)
437     {
438         int present;
439         const ACL *dacl = sd_get_dacl( sd, &present );
440         if (present && dacl)
441         {
442             const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
443             ULONG i;
444             for (i = 0; i < dacl->AceCount; i++)
445             {
446                 const ACCESS_ALLOWED_ACE *aa_ace;
447                 const ACCESS_DENIED_ACE *ad_ace;
448                 const SID *sid;
449                 switch (ace->AceType)
450                 {
451                 case ACCESS_DENIED_ACE_TYPE:
452                     ad_ace = (const ACCESS_DENIED_ACE *)ace;
453                     sid = (const SID *)&ad_ace->SidStart;
454                     if (security_equal_sid( sid, security_world_sid ))
455                     {
456                         unsigned int access = generic_file_map_access( ad_ace->Mask );
457                         if (access & FILE_READ_DATA)
458                             denied_mode |= S_IROTH;
459                         if (access & FILE_WRITE_DATA)
460                             denied_mode |= S_IWOTH;
461                         if (access & FILE_EXECUTE)
462                             denied_mode |= S_IXOTH;
463                     }
464                     else if (security_equal_sid( sid, owner ))
465                     {
466                         unsigned int access = generic_file_map_access( ad_ace->Mask );
467                         if (access & FILE_READ_DATA)
468                             denied_mode |= S_IRUSR;
469                         if (access & FILE_WRITE_DATA)
470                             denied_mode |= S_IWUSR;
471                         if (access & FILE_EXECUTE)
472                             denied_mode |= S_IXUSR;
473                     }
474                     break;
475                 case ACCESS_ALLOWED_ACE_TYPE:
476                     aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
477                     sid = (const SID *)&aa_ace->SidStart;
478                     if (security_equal_sid( sid, security_world_sid ))
479                     {
480                         unsigned int access = generic_file_map_access( aa_ace->Mask );
481                         if (access & FILE_READ_DATA)
482                             new_mode |= S_IROTH;
483                         if (access & FILE_WRITE_DATA)
484                             new_mode |= S_IWOTH;
485                         if (access & FILE_EXECUTE)
486                             new_mode |= S_IXOTH;
487                     }
488                     else if (security_equal_sid( sid, owner ))
489                     {
490                         unsigned int access = generic_file_map_access( aa_ace->Mask );
491                         if (access & FILE_READ_DATA)
492                             new_mode |= S_IRUSR;
493                         if (access & FILE_WRITE_DATA)
494                             new_mode |= S_IWUSR;
495                         if (access & FILE_EXECUTE)
496                             new_mode |= S_IXUSR;
497                     }
498                     break;
499                 }
500                 ace = ace_next( ace );
501             }
502         }
503         else
504             /* no ACL means full access rights to anyone */
505             new_mode |= S_IRWXU | S_IRWXO;
506
507         if (file->mode != (new_mode & ~denied_mode))
508         {
509             if (fchmod( unix_fd, new_mode & ~denied_mode ) == -1)
510             {
511                 file_set_error();
512                 return 0;
513             }
514
515             file->mode = new_mode & ~denied_mode;
516         }
517     }
518     return 1;
519 }
520
521 static void file_destroy( struct object *obj )
522 {
523     struct file *file = (struct file *)obj;
524     assert( obj->ops == &file_ops );
525
526     if (file->fd) release_object( file->fd );
527 }
528
529 /* set the last error depending on errno */
530 void file_set_error(void)
531 {
532     switch (errno)
533     {
534     case ETXTBSY:
535     case EAGAIN:    set_error( STATUS_SHARING_VIOLATION ); break;
536     case EBADF:     set_error( STATUS_INVALID_HANDLE ); break;
537     case ENOSPC:    set_error( STATUS_DISK_FULL ); break;
538     case EACCES:
539     case ESRCH:
540     case EPERM:     set_error( STATUS_ACCESS_DENIED ); break;
541     case EROFS:     set_error( STATUS_MEDIA_WRITE_PROTECTED ); break;
542     case EBUSY:     set_error( STATUS_FILE_LOCK_CONFLICT ); break;
543     case ENOENT:    set_error( STATUS_NO_SUCH_FILE ); break;
544     case EISDIR:    set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
545     case ENFILE:
546     case EMFILE:    set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
547     case EEXIST:    set_error( STATUS_OBJECT_NAME_COLLISION ); break;
548     case EINVAL:    set_error( STATUS_INVALID_PARAMETER ); break;
549     case ESPIPE:    set_error( STATUS_ILLEGAL_FUNCTION ); break;
550     case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
551     case EIO:       set_error( STATUS_ACCESS_VIOLATION ); break;
552     case ENOTDIR:   set_error( STATUS_NOT_A_DIRECTORY ); break;
553     case EFBIG:     set_error( STATUS_SECTION_TOO_BIG ); break;
554     case ENODEV:    set_error( STATUS_NO_SUCH_DEVICE ); break;
555     case ENXIO:     set_error( STATUS_NO_SUCH_DEVICE ); break;
556 #ifdef EOVERFLOW
557     case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
558 #endif
559     default:
560         perror("wineserver: file_set_error() can't map error");
561         set_error( STATUS_UNSUCCESSFUL );
562         break;
563     }
564 }
565
566 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
567 {
568     return (struct file *)get_handle_obj( process, handle, access, &file_ops );
569 }
570
571 int get_file_unix_fd( struct file *file )
572 {
573     return get_unix_fd( file->fd );
574 }
575
576 struct file *grab_file_unless_removable( struct file *file )
577 {
578     if (is_fd_removable( file->fd )) return NULL;
579     return (struct file *)grab_object( file );
580 }
581
582 /* extend a file beyond the current end of file */
583 static int extend_file( struct file *file, file_pos_t new_size )
584 {
585     static const char zero;
586     int unix_fd = get_file_unix_fd( file );
587     off_t size = new_size;
588
589     if (unix_fd == -1) return 0;
590
591     if (sizeof(new_size) > sizeof(size) && size != new_size)
592     {
593         set_error( STATUS_INVALID_PARAMETER );
594         return 0;
595     }
596     /* extend the file one byte beyond the requested size and then truncate it */
597     /* this should work around ftruncate implementations that can't extend files */
598     if (pwrite( unix_fd, &zero, 1, size ) != -1)
599     {
600         ftruncate( unix_fd, size );
601         return 1;
602     }
603     file_set_error();
604     return 0;
605 }
606
607 /* try to grow the file to the specified size */
608 int grow_file( struct file *file, file_pos_t size )
609 {
610     struct stat st;
611     int unix_fd = get_file_unix_fd( file );
612
613     if (unix_fd == -1) return 0;
614
615     if (fstat( unix_fd, &st ) == -1)
616     {
617         file_set_error();
618         return 0;
619     }
620     if (st.st_size >= size) return 1;  /* already large enough */
621     return extend_file( file, size );
622 }
623
624 /* create a file */
625 DECL_HANDLER(create_file)
626 {
627     struct object *file;
628
629     reply->handle = 0;
630     if ((file = create_file( get_req_data(), get_req_data_size(), req->access,
631                              req->sharing, req->create, req->options, req->attrs )))
632     {
633         reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
634         release_object( file );
635     }
636 }
637
638 /* allocate a file handle for a Unix fd */
639 DECL_HANDLER(alloc_file_handle)
640 {
641     struct file *file;
642     int fd;
643
644     reply->handle = 0;
645     if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
646     {
647         set_error( STATUS_INVALID_HANDLE );
648         return;
649     }
650     if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
651     {
652         reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
653         release_object( file );
654     }
655 }
656
657 /* lock a region of a file */
658 DECL_HANDLER(lock_file)
659 {
660     struct file *file;
661
662     if ((file = get_file_obj( current->process, req->handle, 0 )))
663     {
664         reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
665         reply->overlapped = is_overlapped( file );
666         release_object( file );
667     }
668 }
669
670 /* unlock a region of a file */
671 DECL_HANDLER(unlock_file)
672 {
673     struct file *file;
674
675     if ((file = get_file_obj( current->process, req->handle, 0 )))
676     {
677         unlock_fd( file->fd, req->offset, req->count );
678         release_object( file );
679     }
680 }