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
32 #include <sys/types.h>
41 #define WIN32_NO_STATUS
52 static const WCHAR file_name
[] = {'F','i','l','e'};
54 struct type_descr file_type
=
56 { file_name
, sizeof(file_name
) }, /* name */
57 FILE_ALL_ACCESS
, /* valid_access */
68 struct object obj
; /* object header */
69 struct fd
*fd
; /* file descriptor for this file */
70 unsigned int access
; /* file access (FILE_READ_DATA etc.) */
71 mode_t mode
; /* file stat.st_mode */
72 uid_t uid
; /* file stat.st_uid */
73 struct list kernel_object
; /* list of kernel object pointers */
76 static void file_dump( struct object
*obj
, int verbose
);
77 static struct fd
*file_get_fd( struct object
*obj
);
78 static struct security_descriptor
*file_get_sd( struct object
*obj
);
79 static int file_set_sd( struct object
*obj
, const struct security_descriptor
*sd
, unsigned int set_info
);
80 static struct object
*file_lookup_name( struct object
*obj
, struct unicode_str
*name
,
81 unsigned int attr
, struct object
*root
);
82 static struct object
*file_open_file( struct object
*obj
, unsigned int access
,
83 unsigned int sharing
, unsigned int options
);
84 static struct list
*file_get_kernel_obj_list( struct object
*obj
);
85 static void file_destroy( struct object
*obj
);
87 static enum server_fd_type
file_get_fd_type( struct fd
*fd
);
89 static const struct object_ops file_ops
=
91 sizeof(struct file
), /* size */
92 &file_type
, /* type */
94 add_queue
, /* add_queue */
95 remove_queue
, /* remove_queue */
96 default_fd_signaled
, /* signaled */
97 no_satisfied
, /* satisfied */
98 no_signal
, /* signal */
99 file_get_fd
, /* get_fd */
100 default_map_access
, /* map_access */
101 file_get_sd
, /* get_sd */
102 file_set_sd
, /* set_sd */
103 no_get_full_name
, /* get_full_name */
104 file_lookup_name
, /* lookup_name */
105 no_link_name
, /* link_name */
106 NULL
, /* unlink_name */
107 file_open_file
, /* open_file */
108 file_get_kernel_obj_list
, /* get_kernel_obj_list */
109 no_close_handle
, /* close_handle */
110 file_destroy
/* destroy */
113 static const struct fd_ops file_fd_ops
=
115 default_fd_get_poll_events
, /* get_poll_events */
116 default_poll_event
, /* poll_event */
117 file_get_fd_type
, /* get_fd_type */
118 no_fd_read
, /* read */
119 no_fd_write
, /* write */
120 no_fd_flush
, /* flush */
121 default_fd_get_file_info
, /* get_file_info */
122 no_fd_get_volume_info
, /* get_volume_info */
123 default_fd_ioctl
, /* ioctl */
124 default_fd_cancel_async
, /* cancel_async */
125 default_fd_queue_async
, /* queue_async */
126 default_fd_reselect_async
/* reselect_async */
129 /* create a file from a file descriptor */
130 /* if the function fails the fd is closed */
131 struct file
*create_file_for_fd( int fd
, unsigned int access
, unsigned int sharing
)
136 if (fstat( fd
, &st
) == -1)
143 if (!(file
= alloc_object( &file_ops
)))
149 file
->mode
= st
.st_mode
;
150 file
->access
= default_map_access( &file
->obj
, access
);
151 list_init( &file
->kernel_object
);
152 if (!(file
->fd
= create_anonymous_fd( &file_fd_ops
, fd
, &file
->obj
,
153 FILE_SYNCHRONOUS_IO_NONALERT
)))
155 release_object( file
);
158 allow_fd_caching( file
->fd
);
162 /* create a file by duplicating an fd object */
163 struct file
*create_file_for_fd_obj( struct fd
*fd
, unsigned int access
, unsigned int sharing
)
168 if (fstat( get_unix_fd(fd
), &st
) == -1)
174 if ((file
= alloc_object( &file_ops
)))
176 file
->mode
= st
.st_mode
;
177 file
->access
= default_map_access( &file
->obj
, access
);
178 list_init( &file
->kernel_object
);
179 if (!(file
->fd
= dup_fd_object( fd
, access
, sharing
, FILE_SYNCHRONOUS_IO_NONALERT
)))
181 release_object( file
);
184 set_fd_user( file
->fd
, &file_fd_ops
, &file
->obj
);
189 static struct object
*create_file_obj( struct fd
*fd
, unsigned int access
, mode_t mode
)
191 struct file
*file
= alloc_object( &file_ops
);
193 if (!file
) return NULL
;
194 file
->access
= access
;
196 file
->uid
= ~(uid_t
)0;
198 list_init( &file
->kernel_object
);
200 set_fd_user( fd
, &file_fd_ops
, &file
->obj
);
204 int is_file_executable( const char *name
)
206 int len
= strlen( name
);
207 return len
>= 4 && (!strcasecmp( name
+ len
- 4, ".exe") || !strcasecmp( name
+ len
- 4, ".com" ));
210 static struct object
*create_file( struct fd
*root
, const char *nameptr
, data_size_t len
,
211 struct unicode_str nt_name
,
212 unsigned int access
, unsigned int sharing
, int create
,
213 unsigned int options
, unsigned int attrs
,
214 const struct security_descriptor
*sd
)
216 struct object
*obj
= NULL
;
222 if (!len
|| ((nameptr
[0] == '/') ^ !root
) || (nt_name
.len
&& ((nt_name
.str
[0] == '\\') ^ !root
)))
224 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD
);
227 if (!(name
= mem_alloc( len
+ 1 ))) return NULL
;
228 memcpy( name
, nameptr
, len
);
233 case FILE_CREATE
: flags
= O_CREAT
| O_EXCL
; break;
234 case FILE_OVERWRITE_IF
: /* FIXME: the difference is whether we trash existing attr or not */
235 access
|= FILE_WRITE_ATTRIBUTES
;
236 case FILE_SUPERSEDE
: flags
= O_CREAT
| O_TRUNC
; break;
237 case FILE_OPEN
: flags
= 0; break;
238 case FILE_OPEN_IF
: flags
= O_CREAT
; break;
239 case FILE_OVERWRITE
: flags
= O_TRUNC
;
240 access
|= FILE_WRITE_ATTRIBUTES
; break;
241 default: set_error( STATUS_INVALID_PARAMETER
); goto done
;
246 const struct sid
*owner
= sd_get_owner( sd
);
248 owner
= token_get_user( current
->process
->token
);
249 mode
= sd_to_mode( sd
, owner
);
251 else if (options
& FILE_DIRECTORY_FILE
)
252 mode
= (attrs
& FILE_ATTRIBUTE_READONLY
) ? 0555 : 0777;
254 mode
= (attrs
& FILE_ATTRIBUTE_READONLY
) ? 0444 : 0666;
256 if (is_file_executable( name
))
266 access
= map_access( access
, &file_type
.mapping
);
268 /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
269 fd
= open_fd( root
, name
, nt_name
, flags
| O_NONBLOCK
, &mode
, access
, sharing
, options
);
273 obj
= create_dir_obj( fd
, access
, mode
);
274 else if (S_ISCHR(mode
) && is_serial_fd( fd
))
275 obj
= create_serial( fd
);
277 obj
= create_file_obj( fd
, access
, mode
);
279 release_object( fd
);
286 static void file_dump( struct object
*obj
, int verbose
)
288 struct file
*file
= (struct file
*)obj
;
289 assert( obj
->ops
== &file_ops
);
290 fprintf( stderr
, "File fd=%p\n", file
->fd
);
293 static enum server_fd_type
file_get_fd_type( struct fd
*fd
)
295 struct file
*file
= get_fd_user( fd
);
297 if (S_ISREG(file
->mode
) || S_ISBLK(file
->mode
)) return FD_TYPE_FILE
;
298 if (S_ISDIR(file
->mode
)) return FD_TYPE_DIR
;
302 static struct fd
*file_get_fd( struct object
*obj
)
304 struct file
*file
= (struct file
*)obj
;
305 assert( obj
->ops
== &file_ops
);
306 return (struct fd
*)grab_object( file
->fd
);
309 struct security_descriptor
*mode_to_sd( mode_t mode
, const struct sid
*user
, const struct sid
*group
)
311 struct security_descriptor
*sd
;
318 dacl_size
= sizeof(*dacl
) + sizeof(*ace
) + sid_len( &local_system_sid
);
319 if (mode
& S_IRWXU
) dacl_size
+= sizeof(*ace
) + sid_len( user
);
320 if ((!(mode
& S_IRUSR
) && (mode
& (S_IRGRP
|S_IROTH
))) ||
321 (!(mode
& S_IWUSR
) && (mode
& (S_IWGRP
|S_IWOTH
))) ||
322 (!(mode
& S_IXUSR
) && (mode
& (S_IXGRP
|S_IXOTH
))))
323 dacl_size
+= sizeof(*ace
) + sid_len( user
);
324 if (mode
& S_IRWXO
) dacl_size
+= sizeof(*ace
) + sid_len( &world_sid
);
326 sd
= mem_alloc( sizeof(*sd
) + sid_len( user
) + sid_len( group
) + dacl_size
);
329 sd
->control
= SE_DACL_PRESENT
;
330 sd
->owner_len
= sid_len( user
);
331 sd
->group_len
= sid_len( group
);
333 sd
->dacl_len
= dacl_size
;
335 ptr
= (char *)(sd
+ 1);
336 memcpy( ptr
, user
, sd
->owner_len
);
337 ptr
+= sd
->owner_len
;
338 memcpy( ptr
, group
, sd
->group_len
);
339 ptr
+= sd
->group_len
;
341 dacl
= (struct acl
*)ptr
;
342 dacl
->revision
= ACL_REVISION
;
344 dacl
->size
= dacl_size
;
347 if (mode
& S_IRWXU
) dacl
->count
++;
348 if ((!(mode
& S_IRUSR
) && (mode
& (S_IRGRP
|S_IROTH
))) ||
349 (!(mode
& S_IWUSR
) && (mode
& (S_IWGRP
|S_IWOTH
))) ||
350 (!(mode
& S_IXUSR
) && (mode
& (S_IXGRP
|S_IXOTH
))))
352 if (mode
& S_IRWXO
) dacl
->count
++;
354 flags
= (mode
& S_IFDIR
) ? OBJECT_INHERIT_ACE
| CONTAINER_INHERIT_ACE
: 0;
356 /* always give FILE_ALL_ACCESS for Local System */
357 ace
= set_ace( (struct ace
*)(dacl
+ 1), &local_system_sid
,
358 ACCESS_ALLOWED_ACE_TYPE
, flags
, FILE_ALL_ACCESS
);
362 /* appropriate access rights for the user */
363 ace
= set_ace( ace_next( ace
), user
, ACCESS_ALLOWED_ACE_TYPE
, flags
, WRITE_DAC
| WRITE_OWNER
);
364 if (mode
& S_IRUSR
) ace
->mask
|= FILE_GENERIC_READ
| FILE_GENERIC_EXECUTE
;
365 if (mode
& S_IWUSR
) ace
->mask
|= FILE_GENERIC_WRITE
| DELETE
| FILE_DELETE_CHILD
;
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
))))
371 /* deny just in case the user is a member of the group */
372 ace
= set_ace( ace_next( ace
), user
, ACCESS_DENIED_ACE_TYPE
, flags
, 0 );
373 if (!(mode
& S_IRUSR
) && (mode
& (S_IRGRP
|S_IROTH
)))
374 ace
->mask
|= FILE_GENERIC_READ
| FILE_GENERIC_EXECUTE
;
375 if (!(mode
& S_IWUSR
) && (mode
& (S_IWGRP
|S_IROTH
)))
376 ace
->mask
|= FILE_GENERIC_WRITE
| DELETE
| FILE_DELETE_CHILD
;
377 ace
->mask
&= ~STANDARD_RIGHTS_ALL
; /* never deny standard rights */
381 /* appropriate access rights for Everyone */
382 ace
= set_ace( ace_next( ace
), &world_sid
, ACCESS_ALLOWED_ACE_TYPE
, flags
, 0 );
383 if (mode
& S_IROTH
) ace
->mask
|= FILE_GENERIC_READ
| FILE_GENERIC_EXECUTE
;
384 if (mode
& S_IWOTH
) ace
->mask
|= FILE_GENERIC_WRITE
| DELETE
| FILE_DELETE_CHILD
;
390 static struct security_descriptor
*file_get_sd( struct object
*obj
)
392 struct file
*file
= (struct file
*)obj
;
395 struct security_descriptor
*sd
;
397 assert( obj
->ops
== &file_ops
);
399 unix_fd
= get_file_unix_fd( file
);
401 if (unix_fd
== -1 || fstat( unix_fd
, &st
) == -1)
404 /* mode and uid the same? if so, no need to re-generate security descriptor */
405 if (obj
->sd
&& (st
.st_mode
& (S_IRWXU
|S_IRWXO
)) == (file
->mode
& (S_IRWXU
|S_IRWXO
)) &&
406 (st
.st_uid
== file
->uid
))
409 sd
= mode_to_sd( st
.st_mode
,
410 security_unix_uid_to_sid( st
.st_uid
),
411 token_get_primary_group( current
->process
->token
));
412 if (!sd
) return obj
->sd
;
414 file
->mode
= st
.st_mode
;
415 file
->uid
= st
.st_uid
;
421 static mode_t
file_access_to_mode( unsigned int access
)
425 access
= map_access( access
, &file_type
.mapping
);
426 if (access
& FILE_READ_DATA
) mode
|= 4;
427 if (access
& (FILE_WRITE_DATA
|FILE_APPEND_DATA
)) mode
|= 2;
428 if (access
& FILE_EXECUTE
) mode
|= 1;
432 mode_t
sd_to_mode( const struct security_descriptor
*sd
, const struct sid
*owner
)
435 mode_t bits_to_set
= ~0;
438 const struct acl
*dacl
= sd_get_dacl( sd
, &present
);
442 const struct ace
*ace
= (const struct ace
*)(dacl
+ 1);
445 for (i
= 0; i
< dacl
->count
; i
++, ace
= ace_next( ace
))
447 const struct sid
*sid
= (const struct sid
*)(ace
+ 1);
449 if (ace
->flags
& INHERIT_ONLY_ACE
) continue;
451 mode
= file_access_to_mode( ace
->mask
);
454 case ACCESS_DENIED_ACE_TYPE
:
455 if (equal_sid( sid
, &world_sid
))
457 bits_to_set
&= ~((mode
<< 6) | (mode
<< 3) | mode
); /* all */
459 else if (token_sid_present( current
->process
->token
, owner
, TRUE
) &&
460 token_sid_present( current
->process
->token
, sid
, TRUE
))
462 bits_to_set
&= ~((mode
<< 6) | (mode
<< 3)); /* user + group */
464 else if (equal_sid( sid
, owner
))
466 bits_to_set
&= ~(mode
<< 6); /* user only */
469 case ACCESS_ALLOWED_ACE_TYPE
:
470 if (equal_sid( sid
, &world_sid
))
472 mode
= (mode
<< 6) | (mode
<< 3) | mode
; /* all */
473 new_mode
|= mode
& bits_to_set
;
474 bits_to_set
&= ~mode
;
476 else if (token_sid_present( current
->process
->token
, owner
, FALSE
) &&
477 token_sid_present( current
->process
->token
, sid
, FALSE
))
479 mode
= (mode
<< 6) | (mode
<< 3); /* user + group */
480 new_mode
|= mode
& bits_to_set
;
481 bits_to_set
&= ~mode
;
483 else if (equal_sid( sid
, owner
))
485 mode
= (mode
<< 6); /* user only */
486 new_mode
|= mode
& bits_to_set
;
487 bits_to_set
&= ~mode
;
494 /* no ACL means full access rights to anyone */
495 new_mode
= S_IRWXU
| S_IRWXG
| S_IRWXO
;
500 static int file_set_sd( struct object
*obj
, const struct security_descriptor
*sd
,
501 unsigned int set_info
)
503 struct file
*file
= (struct file
*)obj
;
504 const struct sid
*owner
;
509 assert( obj
->ops
== &file_ops
);
511 unix_fd
= get_file_unix_fd( file
);
513 if (unix_fd
== -1 || fstat( unix_fd
, &st
) == -1) return 1;
515 if (set_info
& OWNER_SECURITY_INFORMATION
)
517 owner
= sd_get_owner( sd
);
520 set_error( STATUS_INVALID_SECURITY_DESCR
);
523 if (!obj
->sd
|| !equal_sid( owner
, sd_get_owner( obj
->sd
) ))
525 /* FIXME: get Unix uid and call fchown */
529 owner
= sd_get_owner( obj
->sd
);
531 owner
= token_get_user( current
->process
->token
);
533 /* group and sacl not supported */
535 if (set_info
& DACL_SECURITY_INFORMATION
)
537 /* keep the bits that we don't map to access rights in the ACL */
538 mode
= st
.st_mode
& (S_ISUID
|S_ISGID
|S_ISVTX
);
539 mode
|= sd_to_mode( sd
, owner
);
541 if (((st
.st_mode
^ mode
) & (S_IRWXU
|S_IRWXG
|S_IRWXO
)) && fchmod( unix_fd
, mode
) == -1)
550 static struct object
*file_lookup_name( struct object
*obj
, struct unicode_str
*name
,
551 unsigned int attr
, struct object
*root
)
553 if (!name
|| !name
->len
) return NULL
; /* open the file itself */
555 set_error( STATUS_OBJECT_PATH_NOT_FOUND
);
559 static struct object
*file_open_file( struct object
*obj
, unsigned int access
,
560 unsigned int sharing
, unsigned int options
)
562 struct file
*file
= (struct file
*)obj
;
563 struct object
*new_file
= NULL
;
564 struct unicode_str nt_name
;
567 assert( obj
->ops
== &file_ops
);
569 if ((unix_name
= dup_fd_name( file
->fd
, "" )))
571 get_nt_name( file
->fd
, &nt_name
);
572 new_file
= create_file( NULL
, unix_name
, strlen(unix_name
), nt_name
, access
,
573 sharing
, FILE_OPEN
, options
, 0, NULL
);
576 else set_error( STATUS_OBJECT_TYPE_MISMATCH
);
580 static struct list
*file_get_kernel_obj_list( struct object
*obj
)
582 struct file
*file
= (struct file
*)obj
;
583 return &file
->kernel_object
;
586 static void file_destroy( struct object
*obj
)
588 struct file
*file
= (struct file
*)obj
;
589 assert( obj
->ops
== &file_ops
);
591 if (file
->fd
) release_object( file
->fd
);
594 /* set the last error depending on errno */
595 void file_set_error(void)
600 case EAGAIN
: set_error( STATUS_SHARING_VIOLATION
); break;
601 case EBADF
: set_error( STATUS_INVALID_HANDLE
); break;
602 case ENOSPC
: set_error( STATUS_DISK_FULL
); break;
606 case EPERM
: set_error( STATUS_ACCESS_DENIED
); break;
607 case EBUSY
: set_error( STATUS_FILE_LOCK_CONFLICT
); break;
608 case ENOENT
: set_error( STATUS_NO_SUCH_FILE
); break;
609 case EISDIR
: set_error( STATUS_FILE_IS_A_DIRECTORY
); break;
611 case EMFILE
: set_error( STATUS_TOO_MANY_OPENED_FILES
); break;
612 case EEXIST
: set_error( STATUS_OBJECT_NAME_COLLISION
); break;
613 case EINVAL
: set_error( STATUS_INVALID_PARAMETER
); break;
614 case ESPIPE
: set_error( STATUS_ILLEGAL_FUNCTION
); break;
615 case ENOTEMPTY
: set_error( STATUS_DIRECTORY_NOT_EMPTY
); break;
616 case EIO
: set_error( STATUS_ACCESS_VIOLATION
); break;
617 case ENOTDIR
: set_error( STATUS_NOT_A_DIRECTORY
); break;
618 case EFBIG
: set_error( STATUS_SECTION_TOO_BIG
); break;
619 case ENODEV
: set_error( STATUS_NO_SUCH_DEVICE
); break;
620 case ENXIO
: set_error( STATUS_NO_SUCH_DEVICE
); break;
621 case EXDEV
: set_error( STATUS_NOT_SAME_DEVICE
); break;
622 case ELOOP
: set_error( STATUS_REPARSE_POINT_NOT_RESOLVED
); break;
624 case EOVERFLOW
: set_error( STATUS_INVALID_PARAMETER
); break;
627 perror("wineserver: file_set_error() can't map error");
628 set_error( STATUS_UNSUCCESSFUL
);
633 struct file
*get_file_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
635 return (struct file
*)get_handle_obj( process
, handle
, access
, &file_ops
);
638 int get_file_unix_fd( struct file
*file
)
640 return get_unix_fd( file
->fd
);
644 DECL_HANDLER(create_file
)
647 struct fd
*root_fd
= NULL
;
648 struct unicode_str nt_name
;
649 const struct security_descriptor
*sd
;
650 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &nt_name
, NULL
);
652 data_size_t name_len
;
654 if (!objattr
) return;
656 if (objattr
->rootdir
)
660 if (!(root
= get_dir_obj( current
->process
, objattr
->rootdir
, 0 ))) return;
661 root_fd
= get_obj_fd( (struct object
*)root
);
662 release_object( root
);
663 if (!root_fd
) return;
666 name
= get_req_data_after_objattr( objattr
, &name_len
);
669 if ((file
= create_file( root_fd
, name
, name_len
, nt_name
, req
->access
, req
->sharing
,
670 req
->create
, req
->options
, req
->attrs
, sd
)))
672 reply
->handle
= alloc_handle( current
->process
, file
, req
->access
, objattr
->attributes
);
673 release_object( file
);
675 if (root_fd
) release_object( root_fd
);
678 /* allocate a file handle for a Unix fd */
679 DECL_HANDLER(alloc_file_handle
)
685 if ((fd
= thread_get_inflight_fd( current
, req
->fd
)) == -1)
687 set_error( STATUS_INVALID_HANDLE
);
690 if ((file
= create_file_for_fd( fd
, req
->access
, FILE_SHARE_READ
| FILE_SHARE_WRITE
)))
692 reply
->handle
= alloc_handle( current
->process
, file
, req
->access
, req
->attributes
);
693 release_object( file
);
697 /* lock a region of a file */
698 DECL_HANDLER(lock_file
)
702 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
704 reply
->handle
= lock_fd( file
->fd
, req
->offset
, req
->count
, req
->shared
, req
->wait
);
705 reply
->overlapped
= is_fd_overlapped( file
->fd
);
706 release_object( file
);
710 /* unlock a region of a file */
711 DECL_HANDLER(unlock_file
)
715 if ((file
= get_file_obj( current
->process
, req
->handle
, 0 )))
717 unlock_fd( file
->fd
, req
->offset
, req
->count
);
718 release_object( file
);