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 );
78 static mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner );
80 static const struct object_ops file_ops =
82 sizeof(struct file), /* size */
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 static 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 );
143 static struct object *create_file_obj( struct fd *fd, unsigned int access, mode_t mode )
145 struct file *file = alloc_object( &file_ops );
147 if (!file) return NULL;
148 file->access = access;
152 set_fd_user( fd, &file_fd_ops, &file->obj );
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 )
160 struct object *obj = NULL;
166 if (!(name = mem_alloc( len + 1 ))) return NULL;
167 memcpy( name, nameptr, len );
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;
183 const SID *owner = sd_get_owner( sd );
185 owner = token_get_user( current->process->token );
186 mode = sd_to_mode( sd, owner );
189 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
192 (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
202 access = generic_file_map_access( access );
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 );
209 obj = create_dir_obj( fd );
210 else if (S_ISCHR(mode) && is_serial_fd( fd ))
211 obj = create_serial( fd );
213 obj = create_file_obj( fd, access, mode );
215 release_object( fd );
222 /* check if two file objects point to the same file */
223 int is_same_file( struct file *file1, struct file *file2 )
225 return is_same_file_fd( file1->fd, file2->fd );
228 /* create a temp file for anonymous mappings */
229 struct file *create_temp_file( int access )
234 sprintf( tmpfn, "anonmap.XXXXXX" ); /* create it in the server directory */
235 fd = mkstemps( tmpfn, 0 );
242 return create_file_for_fd( fd, access, 0 );
245 static void file_dump( struct object *obj, int verbose )
247 struct file *file = (struct file *)obj;
248 assert( obj->ops == &file_ops );
249 fprintf( stderr, "File fd=%p\n", file->fd );
252 static int file_get_poll_events( struct fd *fd )
254 struct file *file = get_fd_user( fd );
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;
262 static void file_flush( struct fd *fd, struct event **event )
264 int unix_fd = get_unix_fd( fd );
265 if (unix_fd != -1 && fsync( unix_fd ) == -1) file_set_error();
268 static enum server_fd_type file_get_fd_type( struct fd *fd )
270 struct file *file = get_fd_user( fd );
272 if (S_ISREG(file->mode) || S_ISBLK(file->mode)) return FD_TYPE_FILE;
273 if (S_ISDIR(file->mode)) return FD_TYPE_DIR;
277 static struct fd *file_get_fd( struct object *obj )
279 struct file *file = (struct file *)obj;
280 assert( obj->ops == &file_ops );
281 return (struct fd *)grab_object( file->fd );
284 static unsigned int generic_file_map_access( unsigned int access )
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);
293 static struct security_descriptor *file_get_sd( struct object *obj )
295 struct file *file = (struct file *)obj;
298 struct security_descriptor *sd;
302 ACCESS_ALLOWED_ACE *aaa;
306 const SID *world_sid = security_world_sid;
307 const SID *local_system_sid = security_local_system_sid;
309 assert( obj->ops == &file_ops );
311 unix_fd = get_file_unix_fd( file );
313 if (unix_fd == -1) return obj->sd;
315 if (fstat( unix_fd, &st ) == -1)
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))
323 user = security_unix_uid_to_sid( st.st_uid );
324 group = token_get_primary_group( current->process->token );
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]);
335 sd = mem_alloc( sizeof(struct security_descriptor) +
336 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) +
337 FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]) +
339 if (!sd) return obj->sd;
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]);
345 sd->dacl_len = dacl_size;
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;
354 dacl->AclRevision = ACL_REVISION;
356 dacl->AclSize = dacl_size;
357 dacl->AceCount = 1 + (st.st_mode & S_IRWXU ? 1 : 0) + (st.st_mode & S_IRWXO ? 1 : 0);
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]) );
370 if (st.st_mode & S_IRWXU)
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]) );
388 if (st.st_mode & S_IRWXO)
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]);
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]) );
407 file->mode = st.st_mode;
408 file->uid = st.st_uid;
414 static mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner )
417 mode_t denied_mode = 0;
419 const ACL *dacl = sd_get_dacl( sd, &present );
422 const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
424 for (i = 0; i < dacl->AceCount; i++, ace_next( ace ))
426 const ACCESS_ALLOWED_ACE *aa_ace;
427 const ACCESS_DENIED_ACE *ad_ace;
430 if (ace->AceFlags & INHERIT_ONLY_ACE) continue;
432 switch (ace->AceType)
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 ))
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;
447 else if (security_equal_sid( sid, owner ))
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;
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 ))
463 unsigned int access = generic_file_map_access( aa_ace->Mask );
464 if (access & FILE_READ_DATA)
466 if (access & FILE_WRITE_DATA)
468 if (access & FILE_EXECUTE)
471 else if (security_equal_sid( sid, owner ))
473 unsigned int access = generic_file_map_access( aa_ace->Mask );
474 if (access & FILE_READ_DATA)
476 if (access & FILE_WRITE_DATA)
478 if (access & FILE_EXECUTE)
486 /* no ACL means full access rights to anyone */
487 new_mode = S_IRWXU | S_IRWXO;
489 return new_mode & ~denied_mode;
492 static int file_set_sd( struct object *obj, const struct security_descriptor *sd,
493 unsigned int set_info )
495 struct file *file = (struct file *)obj;
500 assert( obj->ops == &file_ops );
502 unix_fd = get_file_unix_fd( file );
504 if (unix_fd == -1) return 1;
506 if (set_info & OWNER_SECURITY_INFORMATION)
508 owner = sd_get_owner( sd );
511 set_error( STATUS_INVALID_SECURITY_DESCR );
514 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
516 /* FIXME: get Unix uid and call fchown */
520 owner = sd_get_owner( obj->sd );
522 owner = token_get_user( current->process->token );
524 /* group and sacl not supported */
526 if (set_info & DACL_SECURITY_INFORMATION)
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 );
532 if (file->mode != mode)
534 if (fchmod( unix_fd, mode ) == -1)
546 static void file_destroy( struct object *obj )
548 struct file *file = (struct file *)obj;
549 assert( obj->ops == &file_ops );
551 if (file->fd) release_object( file->fd );
554 /* set the last error depending on errno */
555 void file_set_error(void)
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;
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;
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;
582 case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
585 perror("wineserver: file_set_error() can't map error");
586 set_error( STATUS_UNSUCCESSFUL );
591 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
593 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
596 int get_file_unix_fd( struct file *file )
598 return get_unix_fd( file->fd );
601 struct file *grab_file_unless_removable( struct file *file )
603 if (is_fd_removable( file->fd )) return NULL;
604 return (struct file *)grab_object( file );
607 /* extend a file beyond the current end of file */
608 static int extend_file( struct file *file, file_pos_t new_size )
610 static const char zero;
611 int unix_fd = get_file_unix_fd( file );
612 off_t size = new_size;
614 if (unix_fd == -1) return 0;
616 if (sizeof(new_size) > sizeof(size) && size != new_size)
618 set_error( STATUS_INVALID_PARAMETER );
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)
625 ftruncate( unix_fd, size );
632 /* try to grow the file to the specified size */
633 int grow_file( struct file *file, file_pos_t size )
636 int unix_fd = get_file_unix_fd( file );
638 if (unix_fd == -1) return 0;
640 if (fstat( unix_fd, &st ) == -1)
645 if (st.st_size >= size) return 1; /* already large enough */
646 return extend_file( file, size );
650 DECL_HANDLER(create_file)
653 const struct object_attributes *objattr = get_req_data();
654 const struct security_descriptor *sd;
656 data_size_t name_len;
660 if (!objattr_is_valid( objattr, get_req_data_size() ))
662 /* name is transferred in the unix codepage outside of the objattr structure */
663 if (objattr->name_len)
665 set_error( STATUS_INVALID_PARAMETER );
669 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
671 name = (const char *)get_req_data() + sizeof(*objattr) + objattr->sd_len;
672 name_len = get_req_data_size() - sizeof(*objattr) - objattr->sd_len;
675 if ((file = create_file( name, name_len, req->access,
676 req->sharing, req->create, req->options,
679 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
680 release_object( file );
684 /* allocate a file handle for a Unix fd */
685 DECL_HANDLER(alloc_file_handle)
691 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
693 set_error( STATUS_INVALID_HANDLE );
696 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
698 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
699 release_object( file );
703 /* lock a region of a file */
704 DECL_HANDLER(lock_file)
708 if ((file = get_file_obj( current->process, req->handle, 0 )))
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 );
716 /* unlock a region of a file */
717 DECL_HANDLER(unlock_file)
721 if ((file = get_file_obj( current->process, req->handle, 0 )))
723 unlock_fd( file->fd, req->offset, req->count );
724 release_object( file );