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