server: FILE_OVERWRITE and FILE_OVERWRITE_IF need the FILE_WRITE_ATTRIBUTES permission.
[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     no_get_type,                  /* get_type */
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 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         allow_fd_caching( file->fd );
140     }
141     return file;
142 }
143
144 static struct object *create_file_obj( struct fd *fd, unsigned int access, mode_t mode )
145 {
146     struct file *file = alloc_object( &file_ops );
147
148     if (!file) return NULL;
149     file->access  = access;
150     file->mode    = mode;
151     file->uid     = ~(uid_t)0;
152     file->fd      = fd;
153     grab_object( fd );
154     set_fd_user( fd, &file_fd_ops, &file->obj );
155     return &file->obj;
156 }
157
158 static struct object *create_file( struct fd *root, const char *nameptr, data_size_t len,
159                                    unsigned int access, unsigned int sharing, int create,
160                                    unsigned int options, unsigned int attrs,
161                                    const struct security_descriptor *sd )
162 {
163     struct object *obj = NULL;
164     struct fd *fd;
165     int flags;
166     char *name;
167     mode_t mode;
168
169     if (!len || ((nameptr[0] == '/') ^ !root))
170     {
171         set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
172         return NULL;
173     }
174     if (!(name = mem_alloc( len + 1 ))) return NULL;
175     memcpy( name, nameptr, len );
176     name[len] = 0;
177
178     switch(create)
179     {
180     case FILE_CREATE:       flags = O_CREAT | O_EXCL; break;
181     case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
182                             access |= FILE_WRITE_ATTRIBUTES;
183     case FILE_SUPERSEDE:    flags = O_CREAT | O_TRUNC; break;
184     case FILE_OPEN:         flags = 0; break;
185     case FILE_OPEN_IF:      flags = O_CREAT; break;
186     case FILE_OVERWRITE:    flags = O_TRUNC;
187                             access |= FILE_WRITE_ATTRIBUTES; break;
188     default:                set_error( STATUS_INVALID_PARAMETER ); goto done;
189     }
190
191     if (sd)
192     {
193         const SID *owner = sd_get_owner( sd );
194         if (!owner)
195             owner = token_get_user( current->process->token );
196         mode = sd_to_mode( sd, owner );
197     }
198     else
199         mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
200
201     if (len >= 4 &&
202         (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
203     {
204         if (mode & S_IRUSR)
205             mode |= S_IXUSR;
206         if (mode & S_IRGRP)
207             mode |= S_IXGRP;
208         if (mode & S_IROTH)
209             mode |= S_IXOTH;
210     }
211
212     access = generic_file_map_access( access );
213
214     /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
215     fd = open_fd( root, name, flags | O_NONBLOCK | O_LARGEFILE, &mode, access, sharing, options );
216     if (!fd) goto done;
217
218     if (S_ISDIR(mode))
219         obj = create_dir_obj( fd, access, mode );
220     else if (S_ISCHR(mode) && is_serial_fd( fd ))
221         obj = create_serial( fd );
222     else
223         obj = create_file_obj( fd, access, mode );
224
225     release_object( fd );
226
227 done:
228     free( name );
229     return obj;
230 }
231
232 /* check if two file objects point to the same file */
233 int is_same_file( struct file *file1, struct file *file2 )
234 {
235     return is_same_file_fd( file1->fd, file2->fd );
236 }
237
238 static void file_dump( struct object *obj, int verbose )
239 {
240     struct file *file = (struct file *)obj;
241     assert( obj->ops == &file_ops );
242     fprintf( stderr, "File fd=%p\n", file->fd );
243 }
244
245 static int file_get_poll_events( struct fd *fd )
246 {
247     struct file *file = get_fd_user( fd );
248     int events = 0;
249     assert( file->obj.ops == &file_ops );
250     if (file->access & FILE_UNIX_READ_ACCESS) events |= POLLIN;
251     if (file->access & FILE_UNIX_WRITE_ACCESS) events |= POLLOUT;
252     return events;
253 }
254
255 static void file_flush( struct fd *fd, struct event **event )
256 {
257     int unix_fd = get_unix_fd( fd );
258     if (unix_fd != -1 && fsync( unix_fd ) == -1) file_set_error();
259 }
260
261 static enum server_fd_type file_get_fd_type( struct fd *fd )
262 {
263     struct file *file = get_fd_user( fd );
264
265     if (S_ISREG(file->mode) || S_ISBLK(file->mode)) return FD_TYPE_FILE;
266     if (S_ISDIR(file->mode)) return FD_TYPE_DIR;
267     return FD_TYPE_CHAR;
268 }
269
270 static struct fd *file_get_fd( struct object *obj )
271 {
272     struct file *file = (struct file *)obj;
273     assert( obj->ops == &file_ops );
274     return (struct fd *)grab_object( file->fd );
275 }
276
277 static unsigned int generic_file_map_access( unsigned int access )
278 {
279     if (access & GENERIC_READ)    access |= FILE_GENERIC_READ;
280     if (access & GENERIC_WRITE)   access |= FILE_GENERIC_WRITE;
281     if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
282     if (access & GENERIC_ALL)     access |= FILE_ALL_ACCESS;
283     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
284 }
285
286 struct security_descriptor *mode_to_sd( mode_t mode, const SID *user, const SID *group )
287 {
288     struct security_descriptor *sd;
289     size_t dacl_size;
290     ACE_HEADER *current_ace;
291     ACCESS_ALLOWED_ACE *aaa;
292     ACL *dacl;
293     SID *sid;
294     char *ptr;
295     const SID *world_sid = security_world_sid;
296     const SID *local_system_sid = security_local_system_sid;
297
298     dacl_size = sizeof(ACL) + FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
299         FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]);
300     if (mode & S_IRWXU)
301         dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
302             FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
303     if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
304         (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
305         (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
306         dacl_size += FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) +
307             FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
308     if (mode & S_IRWXO)
309         dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
310             FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]);
311
312     sd = mem_alloc( sizeof(struct security_descriptor) +
313                     FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) +
314                     FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]) +
315                     dacl_size );
316     if (!sd) return sd;
317
318     sd->control = SE_DACL_PRESENT;
319     sd->owner_len = FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
320     sd->group_len = FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]);
321     sd->sacl_len = 0;
322     sd->dacl_len = dacl_size;
323
324     ptr = (char *)(sd + 1);
325     memcpy( ptr, user, sd->owner_len );
326     ptr += sd->owner_len;
327     memcpy( ptr, group, sd->group_len );
328     ptr += sd->group_len;
329
330     dacl = (ACL *)ptr;
331     dacl->AclRevision = ACL_REVISION;
332     dacl->Sbz1 = 0;
333     dacl->AclSize = dacl_size;
334     dacl->AceCount = 1 + (mode & S_IRWXU ? 1 : 0) + (mode & S_IRWXO ? 1 : 0);
335     if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
336         (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
337         (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
338         dacl->AceCount++;
339     dacl->Sbz2 = 0;
340
341     /* always give FILE_ALL_ACCESS for Local System */
342     aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
343     current_ace = &aaa->Header;
344     aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
345     aaa->Header.AceFlags = 0;
346     aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
347         FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]);
348     aaa->Mask = FILE_ALL_ACCESS;
349     sid = (SID *)&aaa->SidStart;
350     memcpy( sid, local_system_sid, FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]) );
351
352     if (mode & S_IRWXU)
353     {
354         /* appropriate access rights for the user */
355         aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
356         current_ace = &aaa->Header;
357         aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
358         aaa->Header.AceFlags = 0;
359         aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
360                               FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
361         aaa->Mask = WRITE_DAC | WRITE_OWNER;
362         if (mode & S_IRUSR)
363             aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
364         if (mode & S_IWUSR)
365             aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
366         sid = (SID *)&aaa->SidStart;
367         memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
368     }
369     if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
370         (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
371         (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
372     {
373         /* deny just in case the user is a member of the group */
374         ACCESS_DENIED_ACE *ada = (ACCESS_DENIED_ACE *)ace_next( current_ace );
375         current_ace = &ada->Header;
376         ada->Header.AceType = ACCESS_DENIED_ACE_TYPE;
377         ada->Header.AceFlags = 0;
378         ada->Header.AceSize = FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) +
379                               FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
380         ada->Mask = 0;
381         if (!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH)))
382             ada->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
383         if (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IROTH)))
384             ada->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
385         ada->Mask &= ~STANDARD_RIGHTS_ALL; /* never deny standard rights */
386         sid = (SID *)&ada->SidStart;
387         memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
388     }
389     if (mode & S_IRWXO)
390     {
391         /* appropriate access rights for Everyone */
392         aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
393         current_ace = &aaa->Header;
394         aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
395         aaa->Header.AceFlags = 0;
396         aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
397                              FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]);
398         aaa->Mask = 0;
399         if (mode & S_IROTH)
400             aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
401         if (mode & S_IWOTH)
402             aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
403         sid = (SID *)&aaa->SidStart;
404         memcpy( sid, world_sid, FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]) );
405     }
406
407     return sd;
408 }
409
410 static struct security_descriptor *file_get_sd( struct object *obj )
411 {
412     struct file *file = (struct file *)obj;
413     struct stat st;
414     int unix_fd;
415     struct security_descriptor *sd;
416
417     assert( obj->ops == &file_ops );
418
419     unix_fd = get_file_unix_fd( file );
420
421     if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
422         return obj->sd;
423
424     /* mode and uid the same? if so, no need to re-generate security descriptor */
425     if (obj->sd && (st.st_mode & (S_IRWXU|S_IRWXO)) == (file->mode & (S_IRWXU|S_IRWXO)) &&
426         (st.st_uid == file->uid))
427         return obj->sd;
428
429     sd = mode_to_sd( st.st_mode,
430                      security_unix_uid_to_sid( st.st_uid ),
431                      token_get_primary_group( current->process->token ));
432     if (!sd) return obj->sd;
433
434     file->mode = st.st_mode;
435     file->uid = st.st_uid;
436     free( obj->sd );
437     obj->sd = sd;
438     return sd;
439 }
440
441 static mode_t file_access_to_mode( unsigned int access )
442 {
443     mode_t mode = 0;
444
445     access = generic_file_map_access( access );
446     if (access & FILE_READ_DATA)  mode |= 4;
447     if (access & FILE_WRITE_DATA) mode |= 2;
448     if (access & FILE_EXECUTE)    mode |= 1;
449     return mode;
450 }
451
452 mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner )
453 {
454     mode_t new_mode = 0;
455     mode_t denied_mode = 0;
456     mode_t mode;
457     int present;
458     const ACL *dacl = sd_get_dacl( sd, &present );
459     const SID *user = token_get_user( current->process->token );
460     if (present && dacl)
461     {
462         const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
463         ULONG i;
464         for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
465         {
466             const ACCESS_ALLOWED_ACE *aa_ace;
467             const ACCESS_DENIED_ACE *ad_ace;
468             const SID *sid;
469
470             if (ace->AceFlags & INHERIT_ONLY_ACE) continue;
471
472             switch (ace->AceType)
473             {
474                 case ACCESS_DENIED_ACE_TYPE:
475                     ad_ace = (const ACCESS_DENIED_ACE *)ace;
476                     sid = (const SID *)&ad_ace->SidStart;
477                     mode = file_access_to_mode( ad_ace->Mask );
478                     if (security_equal_sid( sid, security_world_sid ))
479                     {
480                         denied_mode |= (mode << 6) | (mode << 3) | mode; /* all */
481                     }
482                     else if (security_equal_sid( sid, owner ))
483                     {
484                         denied_mode |= (mode << 6);  /* user only */
485                     }
486                     else if ((security_equal_sid( user, owner ) &&
487                               token_sid_present( current->process->token, sid, TRUE )))
488                     {
489                         denied_mode |= (mode << 6) | (mode << 3);  /* user + group */
490                     }
491                     break;
492                 case ACCESS_ALLOWED_ACE_TYPE:
493                     aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
494                     sid = (const SID *)&aa_ace->SidStart;
495                     mode = file_access_to_mode( aa_ace->Mask );
496                     if (security_equal_sid( sid, security_world_sid ))
497                     {
498                         new_mode |= (mode << 6) | (mode << 3) | mode;  /* all */
499                     }
500                     else if (security_equal_sid( sid, owner ))
501                     {
502                         new_mode |= (mode << 6);  /* user only */
503                     }
504                     else if ((security_equal_sid( user, owner ) &&
505                               token_sid_present( current->process->token, sid, FALSE )))
506                     {
507                         new_mode |= (mode << 6) | (mode << 3);  /* user + group */
508                     }
509                     break;
510             }
511         }
512     }
513     else
514         /* no ACL means full access rights to anyone */
515         new_mode = S_IRWXU | S_IRWXG | S_IRWXO;
516
517     return new_mode & ~denied_mode;
518 }
519
520 static int file_set_sd( struct object *obj, const struct security_descriptor *sd,
521                         unsigned int set_info )
522 {
523     struct file *file = (struct file *)obj;
524     const SID *owner;
525     struct stat st;
526     mode_t mode;
527     int unix_fd;
528
529     assert( obj->ops == &file_ops );
530
531     unix_fd = get_file_unix_fd( file );
532
533     if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
534
535     if (set_info & OWNER_SECURITY_INFORMATION)
536     {
537         owner = sd_get_owner( sd );
538         if (!owner)
539         {
540             set_error( STATUS_INVALID_SECURITY_DESCR );
541             return 0;
542         }
543         if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
544         {
545             /* FIXME: get Unix uid and call fchown */
546         }
547     }
548     else if (obj->sd)
549         owner = sd_get_owner( obj->sd );
550     else
551         owner = token_get_user( current->process->token );
552
553     /* group and sacl not supported */
554
555     if (set_info & DACL_SECURITY_INFORMATION)
556     {
557         /* keep the bits that we don't map to access rights in the ACL */
558         mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
559         mode |= sd_to_mode( sd, owner );
560
561         if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
562         {
563             file_set_error();
564             return 0;
565         }
566     }
567     return 1;
568 }
569
570 static void file_destroy( struct object *obj )
571 {
572     struct file *file = (struct file *)obj;
573     assert( obj->ops == &file_ops );
574
575     if (file->fd) release_object( file->fd );
576 }
577
578 /* set the last error depending on errno */
579 void file_set_error(void)
580 {
581     switch (errno)
582     {
583     case ETXTBSY:
584     case EAGAIN:    set_error( STATUS_SHARING_VIOLATION ); break;
585     case EBADF:     set_error( STATUS_INVALID_HANDLE ); break;
586     case ENOSPC:    set_error( STATUS_DISK_FULL ); break;
587     case EACCES:
588     case ESRCH:
589     case EROFS:
590     case EPERM:     set_error( STATUS_ACCESS_DENIED ); break;
591     case EBUSY:     set_error( STATUS_FILE_LOCK_CONFLICT ); break;
592     case ENOENT:    set_error( STATUS_NO_SUCH_FILE ); break;
593     case EISDIR:    set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
594     case ENFILE:
595     case EMFILE:    set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
596     case EEXIST:    set_error( STATUS_OBJECT_NAME_COLLISION ); break;
597     case EINVAL:    set_error( STATUS_INVALID_PARAMETER ); break;
598     case ESPIPE:    set_error( STATUS_ILLEGAL_FUNCTION ); break;
599     case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
600     case EIO:       set_error( STATUS_ACCESS_VIOLATION ); break;
601     case ENOTDIR:   set_error( STATUS_NOT_A_DIRECTORY ); break;
602     case EFBIG:     set_error( STATUS_SECTION_TOO_BIG ); break;
603     case ENODEV:    set_error( STATUS_NO_SUCH_DEVICE ); break;
604     case ENXIO:     set_error( STATUS_NO_SUCH_DEVICE ); break;
605 #ifdef EOVERFLOW
606     case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
607 #endif
608     default:
609         perror("wineserver: file_set_error() can't map error");
610         set_error( STATUS_UNSUCCESSFUL );
611         break;
612     }
613 }
614
615 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
616 {
617     return (struct file *)get_handle_obj( process, handle, access, &file_ops );
618 }
619
620 int get_file_unix_fd( struct file *file )
621 {
622     return get_unix_fd( file->fd );
623 }
624
625 struct file *grab_file_unless_removable( struct file *file )
626 {
627     if (is_fd_removable( file->fd )) return NULL;
628     return (struct file *)grab_object( file );
629 }
630
631 /* create a file */
632 DECL_HANDLER(create_file)
633 {
634     struct object *file;
635     struct fd *root_fd = NULL;
636     const struct object_attributes *objattr = get_req_data();
637     const struct security_descriptor *sd;
638     const char *name;
639     data_size_t name_len;
640
641     reply->handle = 0;
642
643     if (!objattr_is_valid( objattr, get_req_data_size() ))
644         return;
645     /* name is transferred in the unix codepage outside of the objattr structure */
646     if (objattr->name_len)
647     {
648         set_error( STATUS_INVALID_PARAMETER );
649         return;
650     }
651
652     if (objattr->rootdir)
653     {
654         struct dir *root;
655
656         if (!(root = get_dir_obj( current->process, objattr->rootdir, 0 ))) return;
657         root_fd = get_obj_fd( (struct object *)root );
658         release_object( root );
659         if (!root_fd) return;
660     }
661
662     sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
663
664     name = (const char *)get_req_data() + sizeof(*objattr) + objattr->sd_len;
665     name_len = get_req_data_size() - sizeof(*objattr) - objattr->sd_len;
666
667     reply->handle = 0;
668     if ((file = create_file( root_fd, name, name_len, req->access, req->sharing,
669                              req->create, req->options, req->attrs, sd )))
670     {
671         reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
672         release_object( file );
673     }
674     if (root_fd) release_object( root_fd );
675 }
676
677 /* allocate a file handle for a Unix fd */
678 DECL_HANDLER(alloc_file_handle)
679 {
680     struct file *file;
681     int fd;
682
683     reply->handle = 0;
684     if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
685     {
686         set_error( STATUS_INVALID_HANDLE );
687         return;
688     }
689     if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
690     {
691         reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
692         release_object( file );
693     }
694 }
695
696 /* lock a region of a file */
697 DECL_HANDLER(lock_file)
698 {
699     struct file *file;
700
701     if ((file = get_file_obj( current->process, req->handle, 0 )))
702     {
703         reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
704         reply->overlapped = is_overlapped( file );
705         release_object( file );
706     }
707 }
708
709 /* unlock a region of a file */
710 DECL_HANDLER(unlock_file)
711 {
712     struct file *file;
713
714     if ((file = get_file_obj( current->process, req->handle, 0 )))
715     {
716         unlock_fd( file->fd, req->offset, req->count );
717         release_object( file );
718     }
719 }