server: Add primitive support for setting and getting the security descriptor of...
[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     /* only DACL translation is currently supported */
410     if (!(set_info & DACL_SECURITY_INFORMATION))
411         return 1;
412
413     unix_fd = get_file_unix_fd( file );
414
415     if (unix_fd == -1) return 1;
416
417     if (set_info & OWNER_SECURITY_INFORMATION)
418     {
419         owner = sd_get_owner( sd );
420         if (!owner)
421         {
422             set_error( STATUS_INVALID_SECURITY_DESCR );
423             return 0;
424         }
425         if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
426         {
427             /* FIXME: get Unix uid and call fchown */
428         }
429     }
430     else if (obj->sd)
431         owner = sd_get_owner( obj->sd );
432     else
433         owner = token_get_user( current->process->token );
434
435     /* keep the bits that we don't map to access rights in the ACL */
436     new_mode = file->mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXG);
437
438     if (set_info & DACL_SECURITY_INFORMATION)
439     {
440         if (sd->control & SE_DACL_PRESENT)
441         {
442             const ACL *dacl = (const ACL *)((char *)sd + sd->owner_len + sd->group_len + sd->sacl_len);
443             const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
444             ULONG i;
445             for (i = 0; i < dacl->AceCount; i++)
446             {
447                 const ACCESS_ALLOWED_ACE *aa_ace;
448                 const ACCESS_DENIED_ACE *ad_ace;
449                 const SID *sid;
450                 switch (ace->AceType)
451                 {
452                 case ACCESS_DENIED_ACE_TYPE:
453                     ad_ace = (const ACCESS_DENIED_ACE *)ace;
454                     sid = (const SID *)&ad_ace->SidStart;
455                     if (security_equal_sid( sid, security_world_sid ))
456                     {
457                         unsigned int access = generic_file_map_access( ad_ace->Mask );
458                         if (access & FILE_READ_DATA)
459                             denied_mode |= S_IROTH;
460                         if (access & FILE_WRITE_DATA)
461                             denied_mode |= S_IWOTH;
462                         if (access & FILE_EXECUTE)
463                             denied_mode |= S_IXOTH;
464                     }
465                     else if (security_equal_sid( sid, owner ))
466                     {
467                         unsigned int access = generic_file_map_access( ad_ace->Mask );
468                         if (access & FILE_READ_DATA)
469                             denied_mode |= S_IRUSR;
470                         if (access & FILE_WRITE_DATA)
471                             denied_mode |= S_IWUSR;
472                         if (access & FILE_EXECUTE)
473                             denied_mode |= S_IXUSR;
474                     }
475                     break;
476                 case ACCESS_ALLOWED_ACE_TYPE:
477                     aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
478                     sid = (const SID *)&aa_ace->SidStart;
479                     if (security_equal_sid( sid, security_world_sid ))
480                     {
481                         unsigned int access = generic_file_map_access( aa_ace->Mask );
482                         if (access & FILE_READ_DATA)
483                             new_mode |= S_IROTH;
484                         if (access & FILE_WRITE_DATA)
485                             new_mode |= S_IWOTH;
486                         if (access & FILE_EXECUTE)
487                             new_mode |= S_IXOTH;
488                     }
489                     else if (security_equal_sid( sid, owner ))
490                     {
491                         unsigned int access = generic_file_map_access( aa_ace->Mask );
492                         if (access & FILE_READ_DATA)
493                             new_mode |= S_IRUSR;
494                         if (access & FILE_WRITE_DATA)
495                             new_mode |= S_IWUSR;
496                         if (access & FILE_EXECUTE)
497                             new_mode |= S_IXUSR;
498                     }
499                     break;
500                 }
501                 ace = ace_next( ace );
502             }
503         }
504         else
505             /* no ACL means full access rights to anyone */
506             new_mode |= S_IRWXU | S_IRWXO;
507
508         if (fchmod( unix_fd, new_mode & ~denied_mode ) == -1)
509         {
510             file_set_error();
511             return 0;
512         }
513
514         file->mode = new_mode & ~denied_mode;
515     }
516     return 1;
517 }
518
519 static void file_destroy( struct object *obj )
520 {
521     struct file *file = (struct file *)obj;
522     assert( obj->ops == &file_ops );
523
524     if (file->fd) release_object( file->fd );
525 }
526
527 /* set the last error depending on errno */
528 void file_set_error(void)
529 {
530     switch (errno)
531     {
532     case ETXTBSY:
533     case EAGAIN:    set_error( STATUS_SHARING_VIOLATION ); break;
534     case EBADF:     set_error( STATUS_INVALID_HANDLE ); break;
535     case ENOSPC:    set_error( STATUS_DISK_FULL ); break;
536     case EACCES:
537     case ESRCH:
538     case EPERM:     set_error( STATUS_ACCESS_DENIED ); break;
539     case EROFS:     set_error( STATUS_MEDIA_WRITE_PROTECTED ); break;
540     case EBUSY:     set_error( STATUS_FILE_LOCK_CONFLICT ); break;
541     case ENOENT:    set_error( STATUS_NO_SUCH_FILE ); break;
542     case EISDIR:    set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
543     case ENFILE:
544     case EMFILE:    set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
545     case EEXIST:    set_error( STATUS_OBJECT_NAME_COLLISION ); break;
546     case EINVAL:    set_error( STATUS_INVALID_PARAMETER ); break;
547     case ESPIPE:    set_error( STATUS_ILLEGAL_FUNCTION ); break;
548     case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
549     case EIO:       set_error( STATUS_ACCESS_VIOLATION ); break;
550     case ENOTDIR:   set_error( STATUS_NOT_A_DIRECTORY ); break;
551     case EFBIG:     set_error( STATUS_SECTION_TOO_BIG ); break;
552     case ENODEV:    set_error( STATUS_NO_SUCH_DEVICE ); break;
553     case ENXIO:     set_error( STATUS_NO_SUCH_DEVICE ); break;
554 #ifdef EOVERFLOW
555     case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
556 #endif
557     default:
558         perror("wineserver: file_set_error() can't map error");
559         set_error( STATUS_UNSUCCESSFUL );
560         break;
561     }
562 }
563
564 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
565 {
566     return (struct file *)get_handle_obj( process, handle, access, &file_ops );
567 }
568
569 int get_file_unix_fd( struct file *file )
570 {
571     return get_unix_fd( file->fd );
572 }
573
574 struct file *grab_file_unless_removable( struct file *file )
575 {
576     if (is_fd_removable( file->fd )) return NULL;
577     return (struct file *)grab_object( file );
578 }
579
580 /* extend a file beyond the current end of file */
581 static int extend_file( struct file *file, file_pos_t new_size )
582 {
583     static const char zero;
584     int unix_fd = get_file_unix_fd( file );
585     off_t size = new_size;
586
587     if (unix_fd == -1) return 0;
588
589     if (sizeof(new_size) > sizeof(size) && size != new_size)
590     {
591         set_error( STATUS_INVALID_PARAMETER );
592         return 0;
593     }
594     /* extend the file one byte beyond the requested size and then truncate it */
595     /* this should work around ftruncate implementations that can't extend files */
596     if (pwrite( unix_fd, &zero, 1, size ) != -1)
597     {
598         ftruncate( unix_fd, size );
599         return 1;
600     }
601     file_set_error();
602     return 0;
603 }
604
605 /* try to grow the file to the specified size */
606 int grow_file( struct file *file, file_pos_t size )
607 {
608     struct stat st;
609     int unix_fd = get_file_unix_fd( file );
610
611     if (unix_fd == -1) return 0;
612
613     if (fstat( unix_fd, &st ) == -1)
614     {
615         file_set_error();
616         return 0;
617     }
618     if (st.st_size >= size) return 1;  /* already large enough */
619     return extend_file( file, size );
620 }
621
622 /* create a file */
623 DECL_HANDLER(create_file)
624 {
625     struct object *file;
626
627     reply->handle = 0;
628     if ((file = create_file( get_req_data(), get_req_data_size(), req->access,
629                              req->sharing, req->create, req->options, req->attrs )))
630     {
631         reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
632         release_object( file );
633     }
634 }
635
636 /* allocate a file handle for a Unix fd */
637 DECL_HANDLER(alloc_file_handle)
638 {
639     struct file *file;
640     int fd;
641
642     reply->handle = 0;
643     if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
644     {
645         set_error( STATUS_INVALID_HANDLE );
646         return;
647     }
648     if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
649     {
650         reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
651         release_object( file );
652     }
653 }
654
655 /* lock a region of a file */
656 DECL_HANDLER(lock_file)
657 {
658     struct file *file;
659     file_pos_t offset = ((file_pos_t)req->offset_high << 32) | req->offset_low;
660     file_pos_t count = ((file_pos_t)req->count_high << 32) | req->count_low;
661
662     if ((file = get_file_obj( current->process, req->handle, 0 )))
663     {
664         reply->handle = lock_fd( file->fd, offset, 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     file_pos_t offset = ((file_pos_t)req->offset_high << 32) | req->offset_low;
675     file_pos_t count = ((file_pos_t)req->count_high << 32) | req->count_low;
676
677     if ((file = get_file_obj( current->process, req->handle, 0 )))
678     {
679         unlock_fd( file->fd, offset, count );
680         release_object( file );
681     }
682 }