push 583922ece91f6a3e6a32c772f3574cd16e6257fa
[wine/hacks.git] / server / file.c
blob7a62b645bbfc2784265dbdacd66c73667c53644a
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
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
46 #include "ntstatus.h"
47 #define WIN32_NO_STATUS
48 #include "windef.h"
49 #include "winternl.h"
51 #include "file.h"
52 #include "handle.h"
53 #include "thread.h"
54 #include "request.h"
55 #include "process.h"
56 #include "security.h"
58 struct file
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 */
82 file_dump, /* dump */
83 add_queue, /* add_queue */
84 remove_queue, /* remove_queue */
85 default_fd_signaled, /* signaled */
86 no_satisfied, /* satisfied */
87 no_signal, /* signal */
88 file_get_fd, /* get_fd */
89 default_fd_map_access, /* map_access */
90 file_get_sd, /* get_sd */
91 file_set_sd, /* set_sd */
92 no_lookup_name, /* lookup_name */
93 no_open_file, /* open_file */
94 fd_close_handle, /* close_handle */
95 file_destroy /* destroy */
98 static const struct fd_ops file_fd_ops =
100 file_get_poll_events, /* get_poll_events */
101 default_poll_event, /* poll_event */
102 file_flush, /* flush */
103 file_get_fd_type, /* get_fd_type */
104 default_fd_ioctl, /* ioctl */
105 default_fd_queue_async, /* queue_async */
106 default_fd_reselect_async, /* reselect_async */
107 default_fd_cancel_async /* cancel_async */
110 static inline int is_overlapped( const struct file *file )
112 return !(get_fd_options( file->fd ) & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
115 /* create a file from a file descriptor */
116 /* if the function fails the fd is closed */
117 static struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing )
119 struct file *file;
120 struct stat st;
122 if (fstat( fd, &st ) == -1)
124 file_set_error();
125 return NULL;
128 if ((file = alloc_object( &file_ops )))
130 file->mode = st.st_mode;
131 file->access = default_fd_map_access( &file->obj, access );
132 if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj,
133 FILE_SYNCHRONOUS_IO_NONALERT )))
135 release_object( file );
136 return NULL;
139 return file;
142 static struct object *create_file_obj( struct fd *fd, unsigned int access, mode_t mode )
144 struct file *file = alloc_object( &file_ops );
146 if (!file) return NULL;
147 file->access = access;
148 file->mode = mode;
149 file->fd = fd;
150 grab_object( fd );
151 set_fd_user( fd, &file_fd_ops, &file->obj );
152 return &file->obj;
155 static struct object *create_file( const char *nameptr, data_size_t len, unsigned int access,
156 unsigned int sharing, int create, unsigned int options,
157 unsigned int attrs )
159 struct object *obj = NULL;
160 struct fd *fd;
161 int flags;
162 char *name;
163 mode_t mode;
165 if (!(name = mem_alloc( len + 1 ))) return NULL;
166 memcpy( name, nameptr, len );
167 name[len] = 0;
169 switch(create)
171 case FILE_CREATE: flags = O_CREAT | O_EXCL; break;
172 case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
173 case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break;
174 case FILE_OPEN: flags = 0; break;
175 case FILE_OPEN_IF: flags = O_CREAT; break;
176 case FILE_OVERWRITE: flags = O_TRUNC; break;
177 default: set_error( STATUS_INVALID_PARAMETER ); goto done;
180 mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
182 if (len >= 4 &&
183 (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
184 mode |= 0111;
186 access = generic_file_map_access( access );
188 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
189 fd = open_fd( name, flags | O_NONBLOCK | O_LARGEFILE, &mode, access, sharing, options );
190 if (!fd) goto done;
192 if (S_ISDIR(mode))
193 obj = create_dir_obj( fd );
194 else if (S_ISCHR(mode) && is_serial_fd( fd ))
195 obj = create_serial( fd );
196 else
197 obj = create_file_obj( fd, access, mode );
199 release_object( fd );
201 done:
202 free( name );
203 return obj;
206 /* check if two file objects point to the same file */
207 int is_same_file( struct file *file1, struct file *file2 )
209 return is_same_file_fd( file1->fd, file2->fd );
212 /* create a temp file for anonymous mappings */
213 struct file *create_temp_file( int access )
215 char tmpfn[16];
216 int fd;
218 sprintf( tmpfn, "anonmap.XXXXXX" ); /* create it in the server directory */
219 fd = mkstemps( tmpfn, 0 );
220 if (fd == -1)
222 file_set_error();
223 return NULL;
225 unlink( tmpfn );
226 return create_file_for_fd( fd, access, 0 );
229 static void file_dump( struct object *obj, int verbose )
231 struct file *file = (struct file *)obj;
232 assert( obj->ops == &file_ops );
233 fprintf( stderr, "File fd=%p\n", file->fd );
236 static int file_get_poll_events( struct fd *fd )
238 struct file *file = get_fd_user( fd );
239 int events = 0;
240 assert( file->obj.ops == &file_ops );
241 if (file->access & FILE_UNIX_READ_ACCESS) events |= POLLIN;
242 if (file->access & FILE_UNIX_WRITE_ACCESS) events |= POLLOUT;
243 return events;
246 static void file_flush( struct fd *fd, struct event **event )
248 int unix_fd = get_unix_fd( fd );
249 if (unix_fd != -1 && fsync( unix_fd ) == -1) file_set_error();
252 static enum server_fd_type file_get_fd_type( struct fd *fd )
254 struct file *file = get_fd_user( fd );
256 if (S_ISREG(file->mode) || S_ISBLK(file->mode)) return FD_TYPE_FILE;
257 if (S_ISDIR(file->mode)) return FD_TYPE_DIR;
258 return FD_TYPE_CHAR;
261 static struct fd *file_get_fd( struct object *obj )
263 struct file *file = (struct file *)obj;
264 assert( obj->ops == &file_ops );
265 return (struct fd *)grab_object( file->fd );
268 static unsigned int generic_file_map_access( unsigned int access )
270 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
271 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
272 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
273 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
274 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
277 static struct security_descriptor *file_get_sd( struct object *obj )
279 struct file *file = (struct file *)obj;
280 struct stat st;
281 int unix_fd;
282 struct security_descriptor *sd;
283 const SID *user;
284 const SID *group;
285 size_t dacl_size;
286 ACCESS_ALLOWED_ACE *aaa;
287 ACL *dacl;
288 SID *sid;
289 char *ptr;
290 const SID *world_sid = security_world_sid;
291 const SID *local_system_sid = security_local_system_sid;
293 assert( obj->ops == &file_ops );
295 unix_fd = get_file_unix_fd( file );
297 if (unix_fd == -1) return obj->sd;
299 if (fstat( unix_fd, &st ) == -1)
300 return obj->sd;
302 /* mode and uid the same? if so, no need to re-generate security descriptor */
303 if (obj->sd && (st.st_mode & (S_IRWXU|S_IRWXO)) == (file->mode & (S_IRWXU|S_IRWXO)) &&
304 (st.st_uid == file->uid))
305 return obj->sd;
307 user = security_unix_uid_to_sid( st.st_uid );
308 group = token_get_primary_group( current->process->token );
310 dacl_size = sizeof(ACL) + FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
311 FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]);
312 if (st.st_mode & S_IRWXU)
313 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
314 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
315 if (st.st_mode & S_IRWXO)
316 dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
317 FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]);
319 sd = mem_alloc( sizeof(struct security_descriptor) +
320 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) +
321 FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]) +
322 dacl_size );
323 if (!sd) return obj->sd;
325 sd->control = SE_DACL_PRESENT;
326 sd->owner_len = FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
327 sd->group_len = FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]);
328 sd->sacl_len = 0;
329 sd->dacl_len = dacl_size;
331 ptr = (char *)(sd + 1);
332 memcpy( ptr, user, sd->owner_len );
333 ptr += sd->owner_len;
334 memcpy( ptr, group, sd->group_len );
335 ptr += sd->group_len;
337 dacl = (ACL *)ptr;
338 dacl->AclRevision = ACL_REVISION;
339 dacl->Sbz1 = 0;
340 dacl->AclSize = dacl_size;
341 dacl->AceCount = 1 + (st.st_mode & S_IRWXU ? 1 : 0) + (st.st_mode & S_IRWXO ? 1 : 0);
342 dacl->Sbz2 = 0;
344 /* always give FILE_ALL_ACCESS for Local System */
345 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
346 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
347 aaa->Header.AceFlags = 0;
348 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
349 FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]);
350 aaa->Mask = FILE_ALL_ACCESS;
351 sid = (SID *)&aaa->SidStart;
352 memcpy( sid, local_system_sid, FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]) );
354 if (st.st_mode & S_IRWXU)
356 /* appropriate access rights for the user */
357 aaa = (ACCESS_ALLOWED_ACE *)ace_next( &aaa->Header );
358 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
359 aaa->Header.AceFlags = 0;
360 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
361 FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
362 aaa->Mask = WRITE_DAC | WRITE_OWNER;
363 if (st.st_mode & S_IRUSR)
364 aaa->Mask |= FILE_GENERIC_READ;
365 if (st.st_mode & S_IWUSR)
366 aaa->Mask |= FILE_GENERIC_WRITE | DELETE;
367 if (st.st_mode & S_IXUSR)
368 aaa->Mask |= FILE_GENERIC_EXECUTE;
369 sid = (SID *)&aaa->SidStart;
370 memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
372 if (st.st_mode & S_IRWXO)
374 /* appropriate access rights for Everyone */
375 aaa = (ACCESS_ALLOWED_ACE *)ace_next( &aaa->Header );
376 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
377 aaa->Header.AceFlags = 0;
378 aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
379 FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]);
380 aaa->Mask = 0;
381 if (st.st_mode & S_IROTH)
382 aaa->Mask |= FILE_GENERIC_READ;
383 if (st.st_mode & S_IWOTH)
384 aaa->Mask |= FILE_GENERIC_WRITE | DELETE;
385 if (st.st_mode & S_IXOTH)
386 aaa->Mask |= FILE_GENERIC_EXECUTE;
387 sid = (SID *)&aaa->SidStart;
388 memcpy( sid, world_sid, FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]) );
391 file->mode = st.st_mode;
392 file->uid = st.st_uid;
393 free( obj->sd );
394 obj->sd = sd;
395 return sd;
398 static int file_set_sd( struct object *obj, const struct security_descriptor *sd,
399 unsigned int set_info )
401 struct file *file = (struct file *)obj;
402 mode_t new_mode;
403 mode_t denied_mode = 0;
404 const SID *owner;
405 int unix_fd;
407 assert( obj->ops == &file_ops );
409 unix_fd = get_file_unix_fd( file );
411 if (unix_fd == -1) return 1;
413 if (set_info & OWNER_SECURITY_INFORMATION)
415 owner = sd_get_owner( sd );
416 if (!owner)
418 set_error( STATUS_INVALID_SECURITY_DESCR );
419 return 0;
421 if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
423 /* FIXME: get Unix uid and call fchown */
426 else if (obj->sd)
427 owner = sd_get_owner( obj->sd );
428 else
429 owner = token_get_user( current->process->token );
431 /* group and sacl not supported */
433 /* keep the bits that we don't map to access rights in the ACL */
434 new_mode = file->mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXG);
436 if (set_info & DACL_SECURITY_INFORMATION)
438 if (sd->control & SE_DACL_PRESENT)
440 const ACL *dacl = (const ACL *)((char *)sd + sd->owner_len + sd->group_len + sd->sacl_len);
441 const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
442 ULONG i;
443 for (i = 0; i < dacl->AceCount; i++)
445 const ACCESS_ALLOWED_ACE *aa_ace;
446 const ACCESS_DENIED_ACE *ad_ace;
447 const SID *sid;
448 switch (ace->AceType)
450 case ACCESS_DENIED_ACE_TYPE:
451 ad_ace = (const ACCESS_DENIED_ACE *)ace;
452 sid = (const SID *)&ad_ace->SidStart;
453 if (security_equal_sid( sid, security_world_sid ))
455 unsigned int access = generic_file_map_access( ad_ace->Mask );
456 if (access & FILE_READ_DATA)
457 denied_mode |= S_IROTH;
458 if (access & FILE_WRITE_DATA)
459 denied_mode |= S_IWOTH;
460 if (access & FILE_EXECUTE)
461 denied_mode |= S_IXOTH;
463 else if (security_equal_sid( sid, owner ))
465 unsigned int access = generic_file_map_access( ad_ace->Mask );
466 if (access & FILE_READ_DATA)
467 denied_mode |= S_IRUSR;
468 if (access & FILE_WRITE_DATA)
469 denied_mode |= S_IWUSR;
470 if (access & FILE_EXECUTE)
471 denied_mode |= S_IXUSR;
473 break;
474 case ACCESS_ALLOWED_ACE_TYPE:
475 aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
476 sid = (const SID *)&aa_ace->SidStart;
477 if (security_equal_sid( sid, security_world_sid ))
479 unsigned int access = generic_file_map_access( aa_ace->Mask );
480 if (access & FILE_READ_DATA)
481 new_mode |= S_IROTH;
482 if (access & FILE_WRITE_DATA)
483 new_mode |= S_IWOTH;
484 if (access & FILE_EXECUTE)
485 new_mode |= S_IXOTH;
487 else if (security_equal_sid( sid, owner ))
489 unsigned int access = generic_file_map_access( aa_ace->Mask );
490 if (access & FILE_READ_DATA)
491 new_mode |= S_IRUSR;
492 if (access & FILE_WRITE_DATA)
493 new_mode |= S_IWUSR;
494 if (access & FILE_EXECUTE)
495 new_mode |= S_IXUSR;
497 break;
499 ace = ace_next( ace );
502 else
503 /* no ACL means full access rights to anyone */
504 new_mode |= S_IRWXU | S_IRWXO;
506 if (file->mode != (new_mode & ~denied_mode))
508 if (fchmod( unix_fd, new_mode & ~denied_mode ) == -1)
510 file_set_error();
511 return 0;
514 file->mode = new_mode & ~denied_mode;
517 return 1;
520 static void file_destroy( struct object *obj )
522 struct file *file = (struct file *)obj;
523 assert( obj->ops == &file_ops );
525 if (file->fd) release_object( file->fd );
528 /* set the last error depending on errno */
529 void file_set_error(void)
531 switch (errno)
533 case ETXTBSY:
534 case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
535 case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
536 case ENOSPC: set_error( STATUS_DISK_FULL ); break;
537 case EACCES:
538 case ESRCH:
539 case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
540 case EROFS: set_error( STATUS_MEDIA_WRITE_PROTECTED ); break;
541 case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
542 case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
543 case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
544 case ENFILE:
545 case EMFILE: set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
546 case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
547 case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
548 case ESPIPE: set_error( STATUS_ILLEGAL_FUNCTION ); break;
549 case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
550 case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
551 case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
552 case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
553 case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break;
554 case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break;
555 #ifdef EOVERFLOW
556 case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
557 #endif
558 default:
559 perror("wineserver: file_set_error() can't map error");
560 set_error( STATUS_UNSUCCESSFUL );
561 break;
565 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
567 return (struct file *)get_handle_obj( process, handle, access, &file_ops );
570 int get_file_unix_fd( struct file *file )
572 return get_unix_fd( file->fd );
575 struct file *grab_file_unless_removable( struct file *file )
577 if (is_fd_removable( file->fd )) return NULL;
578 return (struct file *)grab_object( file );
581 /* extend a file beyond the current end of file */
582 static int extend_file( struct file *file, file_pos_t new_size )
584 static const char zero;
585 int unix_fd = get_file_unix_fd( file );
586 off_t size = new_size;
588 if (unix_fd == -1) return 0;
590 if (sizeof(new_size) > sizeof(size) && size != new_size)
592 set_error( STATUS_INVALID_PARAMETER );
593 return 0;
595 /* extend the file one byte beyond the requested size and then truncate it */
596 /* this should work around ftruncate implementations that can't extend files */
597 if (pwrite( unix_fd, &zero, 1, size ) != -1)
599 ftruncate( unix_fd, size );
600 return 1;
602 file_set_error();
603 return 0;
606 /* try to grow the file to the specified size */
607 int grow_file( struct file *file, file_pos_t size )
609 struct stat st;
610 int unix_fd = get_file_unix_fd( file );
612 if (unix_fd == -1) return 0;
614 if (fstat( unix_fd, &st ) == -1)
616 file_set_error();
617 return 0;
619 if (st.st_size >= size) return 1; /* already large enough */
620 return extend_file( file, size );
623 /* create a file */
624 DECL_HANDLER(create_file)
626 struct object *file;
628 reply->handle = 0;
629 if ((file = create_file( get_req_data(), get_req_data_size(), req->access,
630 req->sharing, req->create, req->options, req->attrs )))
632 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
633 release_object( file );
637 /* allocate a file handle for a Unix fd */
638 DECL_HANDLER(alloc_file_handle)
640 struct file *file;
641 int fd;
643 reply->handle = 0;
644 if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
646 set_error( STATUS_INVALID_HANDLE );
647 return;
649 if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
651 reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
652 release_object( file );
656 /* lock a region of a file */
657 DECL_HANDLER(lock_file)
659 struct file *file;
661 if ((file = get_file_obj( current->process, req->handle, 0 )))
663 reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
664 reply->overlapped = is_overlapped( file );
665 release_object( file );
669 /* unlock a region of a file */
670 DECL_HANDLER(unlock_file)
672 struct file *file;
674 if ((file = get_file_obj( current->process, req->handle, 0 )))
676 unlock_fd( file->fd, req->offset, req->count );
677 release_object( file );