2 * Server-side file management
4 * Copyright (C) 1998 Alexandre Julliard
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.
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.
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
22 #include "wine/port.h"
31 #ifdef HAVE_SYS_ERRNO_H
32 #include <sys/errno.h>
36 #include <sys/types.h>
47 #define WIN32_NO_STATUS
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 */
67 static unsigned int generic_file_map_access( unsigned int access );
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 );
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 );
79 static const struct object_ops file_ops =
81 sizeof(struct file), /* size */
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 */
99 static const struct fd_ops file_fd_ops =
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 */
111 static inline int is_overlapped( const struct file *file )
113 return !(get_fd_options( file->fd ) & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
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 )
123 if (fstat( fd, &st ) == -1)
129 if ((file = alloc_object( &file_ops )))
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 )))
136 release_object( file );
139 allow_fd_caching( file->fd );
144 /* create a file by duplicating an fd object */
145 struct file *create_file_for_fd_obj( struct fd *fd, unsigned int access, unsigned int sharing )
150 if (fstat( get_unix_fd(fd), &st ) == -1)
156 if ((file = alloc_object( &file_ops )))
158 file->mode = st.st_mode;
159 file->access = default_fd_map_access( &file->obj, access );
160 if (!(file->fd = dup_fd_object( fd, access, sharing, FILE_SYNCHRONOUS_IO_NONALERT )))
162 release_object( file );
169 static struct object *create_file_obj( struct fd *fd, unsigned int access, mode_t mode )
171 struct file *file = alloc_object( &file_ops );
173 if (!file) return NULL;
174 file->access = access;
176 file->uid = ~(uid_t)0;
179 set_fd_user( fd, &file_fd_ops, &file->obj );
183 static struct object *create_file( struct fd *root, const char *nameptr, data_size_t len,
184 unsigned int access, unsigned int sharing, int create,
185 unsigned int options, unsigned int attrs,
186 const struct security_descriptor *sd )
188 struct object *obj = NULL;
194 if (!len || ((nameptr[0] == '/') ^ !root))
196 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
199 if (!(name = mem_alloc( len + 1 ))) return NULL;
200 memcpy( name, nameptr, len );
205 case FILE_CREATE: flags = O_CREAT | O_EXCL; break;
206 case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
207 access |= FILE_WRITE_ATTRIBUTES;
208 case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break;
209 case FILE_OPEN: flags = 0; break;
210 case FILE_OPEN_IF: flags = O_CREAT; break;
211 case FILE_OVERWRITE: flags = O_TRUNC;
212 access |= FILE_WRITE_ATTRIBUTES; break;
213 default: set_error( STATUS_INVALID_PARAMETER ); goto done;
218 const SID *owner = sd_get_owner( sd );
220 owner = token_get_user( current->process->token );
221 mode = sd_to_mode( sd, owner );
224 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
227 (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
237 access = generic_file_map_access( access );
239 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
240 fd = open_fd( root, name, flags | O_NONBLOCK | O_LARGEFILE, &mode, access, sharing, options );
244 obj = create_dir_obj( fd, access, mode );
245 else if (S_ISCHR(mode) && is_serial_fd( fd ))
246 obj = create_serial( fd );
248 obj = create_file_obj( fd, access, mode );
250 release_object( fd );
257 /* check if two file objects point to the same file */
258 int is_same_file( struct file *file1, struct file *file2 )
260 return is_same_file_fd( file1->fd, file2->fd );
263 static void file_dump( struct object *obj, int verbose )
265 struct file *file = (struct file *)obj;
266 assert( obj->ops == &file_ops );
267 fprintf( stderr, "File fd=%p\n", file->fd );
270 static int file_get_poll_events( struct fd *fd )
272 struct file *file = get_fd_user( fd );
274 assert( file->obj.ops == &file_ops );
275 if (file->access & FILE_UNIX_READ_ACCESS) events |= POLLIN;
276 if (file->access & FILE_UNIX_WRITE_ACCESS) events |= POLLOUT;
280 static void file_flush( struct fd *fd, struct event **event )
282 int unix_fd = get_unix_fd( fd );
283 if (unix_fd != -1 && fsync( unix_fd ) == -1) file_set_error();
286 static enum server_fd_type file_get_fd_type( struct fd *fd )
288 struct file *file = get_fd_user( fd );
290 if (S_ISREG(file->mode) || S_ISBLK(file->mode)) return FD_TYPE_FILE;
291 if (S_ISDIR(file->mode)) return FD_TYPE_DIR;
295 static struct fd *file_get_fd( struct object *obj )
297 struct file *file = (struct file *)obj;
298 assert( obj->ops == &file_ops );
299 return (struct fd *)grab_object( file->fd );
302 static unsigned int generic_file_map_access( unsigned int access )
304 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
305 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
306 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
307 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
308 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
311 struct security_descriptor *mode_to_sd( mode_t mode, const SID *user, const SID *group )
313 struct security_descriptor *sd;
315 ACE_HEADER *current_ace;
316 ACCESS_ALLOWED_ACE *aaa;
320 const SID *world_sid = security_world_sid;
321 const SID *local_system_sid = security_local_system_sid;
323 dacl_size = sizeof(ACL) + FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
324 FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]);
326 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
327 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
328 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
329 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
330 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
331 dacl_size += FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) +
332 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
334 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
335 FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]);
337 sd = mem_alloc( sizeof(struct security_descriptor) +
338 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) +
339 FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]) +
343 sd->control = SE_DACL_PRESENT;
344 sd->owner_len = FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
345 sd->group_len = FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]);
347 sd->dacl_len = dacl_size;
349 ptr = (char *)(sd + 1);
350 memcpy( ptr, user, sd->owner_len );
351 ptr += sd->owner_len;
352 memcpy( ptr, group, sd->group_len );
353 ptr += sd->group_len;
356 dacl->AclRevision = ACL_REVISION;
358 dacl->AclSize = dacl_size;
359 dacl->AceCount = 1 + (mode & S_IRWXU ? 1 : 0) + (mode & S_IRWXO ? 1 : 0);
360 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
361 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
362 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
366 /* always give FILE_ALL_ACCESS for Local System */
367 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
368 current_ace = &aaa->Header;
369 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
370 aaa->Header.AceFlags = 0;
371 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
372 FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]);
373 aaa->Mask = FILE_ALL_ACCESS;
374 sid = (SID *)&aaa->SidStart;
375 memcpy( sid, local_system_sid, FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]) );
379 /* appropriate access rights for the user */
380 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
381 current_ace = &aaa->Header;
382 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
383 aaa->Header.AceFlags = 0;
384 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
385 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
386 aaa->Mask = WRITE_DAC | WRITE_OWNER;
388 aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
390 aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
391 sid = (SID *)&aaa->SidStart;
392 memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
394 if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
395 (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
396 (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
398 /* deny just in case the user is a member of the group */
399 ACCESS_DENIED_ACE *ada = (ACCESS_DENIED_ACE *)ace_next( current_ace );
400 current_ace = &ada->Header;
401 ada->Header.AceType = ACCESS_DENIED_ACE_TYPE;
402 ada->Header.AceFlags = 0;
403 ada->Header.AceSize = FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) +
404 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
406 if (!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH)))
407 ada->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
408 if (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IROTH)))
409 ada->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
410 ada->Mask &= ~STANDARD_RIGHTS_ALL; /* never deny standard rights */
411 sid = (SID *)&ada->SidStart;
412 memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
416 /* appropriate access rights for Everyone */
417 aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
418 current_ace = &aaa->Header;
419 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
420 aaa->Header.AceFlags = 0;
421 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
422 FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]);
425 aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
427 aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
428 sid = (SID *)&aaa->SidStart;
429 memcpy( sid, world_sid, FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]) );
435 static struct security_descriptor *file_get_sd( struct object *obj )
437 struct file *file = (struct file *)obj;
440 struct security_descriptor *sd;
442 assert( obj->ops == &file_ops );
444 unix_fd = get_file_unix_fd( file );
446 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
449 /* mode and uid the same? if so, no need to re-generate security descriptor */
450 if (obj->sd && (st.st_mode & (S_IRWXU|S_IRWXO)) == (file->mode & (S_IRWXU|S_IRWXO)) &&
451 (st.st_uid == file->uid))
454 sd = mode_to_sd( st.st_mode,
455 security_unix_uid_to_sid( st.st_uid ),
456 token_get_primary_group( current->process->token ));
457 if (!sd) return obj->sd;
459 file->mode = st.st_mode;
460 file->uid = st.st_uid;
466 static mode_t file_access_to_mode( unsigned int access )
470 access = generic_file_map_access( access );
471 if (access & FILE_READ_DATA) mode |= 4;
472 if (access & FILE_WRITE_DATA) mode |= 2;
473 if (access & FILE_EXECUTE) mode |= 1;
477 mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner )
480 mode_t denied_mode = 0;
483 const ACL *dacl = sd_get_dacl( sd, &present );
484 const SID *user = token_get_user( current->process->token );
487 const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
489 for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
491 const ACCESS_ALLOWED_ACE *aa_ace;
492 const ACCESS_DENIED_ACE *ad_ace;
495 if (ace->AceFlags & INHERIT_ONLY_ACE) continue;
497 switch (ace->AceType)
499 case ACCESS_DENIED_ACE_TYPE:
500 ad_ace = (const ACCESS_DENIED_ACE *)ace;
501 sid = (const SID *)&ad_ace->SidStart;
502 mode = file_access_to_mode( ad_ace->Mask );
503 if (security_equal_sid( sid, security_world_sid ))
505 denied_mode |= (mode << 6) | (mode << 3) | mode; /* all */
507 else if (security_equal_sid( sid, owner ))
509 denied_mode |= (mode << 6); /* user only */
511 else if ((security_equal_sid( user, owner ) &&
512 token_sid_present( current->process->token, sid, TRUE )))
514 denied_mode |= (mode << 6) | (mode << 3); /* user + group */
517 case ACCESS_ALLOWED_ACE_TYPE:
518 aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
519 sid = (const SID *)&aa_ace->SidStart;
520 mode = file_access_to_mode( aa_ace->Mask );
521 if (security_equal_sid( sid, security_world_sid ))
523 new_mode |= (mode << 6) | (mode << 3) | mode; /* all */
525 else if (security_equal_sid( sid, owner ))
527 new_mode |= (mode << 6); /* user only */
529 else if ((security_equal_sid( user, owner ) &&
530 token_sid_present( current->process->token, sid, FALSE )))
532 new_mode |= (mode << 6) | (mode << 3); /* user + group */
539 /* no ACL means full access rights to anyone */
540 new_mode = S_IRWXU | S_IRWXG | S_IRWXO;
542 return new_mode & ~denied_mode;
545 static int file_set_sd( struct object *obj, const struct security_descriptor *sd,
546 unsigned int set_info )
548 struct file *file = (struct file *)obj;
554 assert( obj->ops == &file_ops );
556 unix_fd = get_file_unix_fd( file );
558 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
560 if (set_info & OWNER_SECURITY_INFORMATION)
562 owner = sd_get_owner( sd );
565 set_error( STATUS_INVALID_SECURITY_DESCR );
568 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
570 /* FIXME: get Unix uid and call fchown */
574 owner = sd_get_owner( obj->sd );
576 owner = token_get_user( current->process->token );
578 /* group and sacl not supported */
580 if (set_info & DACL_SECURITY_INFORMATION)
582 /* keep the bits that we don't map to access rights in the ACL */
583 mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
584 mode |= sd_to_mode( sd, owner );
586 if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
595 static void file_destroy( struct object *obj )
597 struct file *file = (struct file *)obj;
598 assert( obj->ops == &file_ops );
600 if (file->fd) release_object( file->fd );
603 /* set the last error depending on errno */
604 void file_set_error(void)
609 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
610 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
611 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
615 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
616 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
617 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
618 case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
620 case EMFILE: set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
621 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
622 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
623 case ESPIPE: set_error( STATUS_ILLEGAL_FUNCTION ); break;
624 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
625 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
626 case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
627 case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
628 case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break;
629 case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break;
631 case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
634 perror("wineserver: file_set_error() can't map error");
635 set_error( STATUS_UNSUCCESSFUL );
640 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
642 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
645 int get_file_unix_fd( struct file *file )
647 return get_unix_fd( file->fd );
650 struct file *grab_file_unless_removable( struct file *file )
652 if (is_fd_removable( file->fd )) return NULL;
653 return (struct file *)grab_object( file );
657 DECL_HANDLER(create_file)
660 struct fd *root_fd = NULL;
661 const struct object_attributes *objattr = get_req_data();
662 const struct security_descriptor *sd;
664 data_size_t name_len;
668 if (!objattr_is_valid( objattr, get_req_data_size() ))
670 /* name is transferred in the unix codepage outside of the objattr structure */
671 if (objattr->name_len)
673 set_error( STATUS_INVALID_PARAMETER );
677 if (objattr->rootdir)
681 if (!(root = get_dir_obj( current->process, objattr->rootdir, 0 ))) return;
682 root_fd = get_obj_fd( (struct object *)root );
683 release_object( root );
684 if (!root_fd) return;
687 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
689 name = (const char *)get_req_data() + sizeof(*objattr) + objattr->sd_len;
690 name_len = get_req_data_size() - sizeof(*objattr) - objattr->sd_len;
693 if ((file = create_file( root_fd, name, name_len, req->access, req->sharing,
694 req->create, req->options, req->attrs, sd )))
696 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
697 release_object( file );
699 if (root_fd) release_object( root_fd );
702 /* allocate a file handle for a Unix fd */
703 DECL_HANDLER(alloc_file_handle)
709 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
711 set_error( STATUS_INVALID_HANDLE );
714 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
716 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
717 release_object( file );
721 /* lock a region of a file */
722 DECL_HANDLER(lock_file)
726 if ((file = get_file_obj( current->process, req->handle, 0 )))
728 reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
729 reply->overlapped = is_overlapped( file );
730 release_object( file );
734 /* unlock a region of a file */
735 DECL_HANDLER(unlock_file)
739 if ((file = get_file_obj( current->process, req->handle, 0 )))
741 unlock_fd( file->fd, req->offset, req->count );
742 release_object( file );