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