Check file sharing permissions based on the file inode instead of the
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
43 #include "winerror.h"
44 #include "windef.h"
45 #include "winbase.h"
46 #include "winreg.h"
47 #include "winternl.h"
48
49 #include "file.h"
50 #include "handle.h"
51 #include "thread.h"
52 #include "request.h"
53 #include "async.h"
54
55 struct file
56 {
57     struct object       obj;        /* object header */
58     struct fd          *fd;         /* file descriptor for this file */
59     char               *name;       /* file name */
60     unsigned int        access;     /* file access (GENERIC_READ/WRITE) */
61     unsigned int        options;    /* file options (FILE_DELETE_ON_CLOSE, FILE_SYNCHRONOUS...) */
62     int                 removable;  /* is file on removable media? */
63     struct async_queue  read_q;
64     struct async_queue  write_q;
65 };
66
67 static void file_dump( struct object *obj, int verbose );
68 static struct fd *file_get_fd( struct object *obj );
69 static void file_destroy( struct object *obj );
70
71 static int file_get_poll_events( struct fd *fd );
72 static void file_poll_event( struct fd *fd, int event );
73 static int file_flush( struct fd *fd, struct event **event );
74 static int file_get_info( struct fd *fd, struct get_file_info_reply *reply, int *flags );
75 static void file_queue_async( struct fd *fd, void *ptr, unsigned int status, int type, int count );
76
77 static const struct object_ops file_ops =
78 {
79     sizeof(struct file),          /* size */
80     file_dump,                    /* dump */
81     default_fd_add_queue,         /* add_queue */
82     default_fd_remove_queue,      /* remove_queue */
83     default_fd_signaled,          /* signaled */
84     no_satisfied,                 /* satisfied */
85     file_get_fd,                  /* get_fd */
86     file_destroy                  /* destroy */
87 };
88
89 static const struct fd_ops file_fd_ops =
90 {
91     file_get_poll_events,         /* get_poll_events */
92     file_poll_event,              /* poll_event */
93     file_flush,                   /* flush */
94     file_get_info,                /* get_file_info */
95     file_queue_async              /* queue_async */
96 };
97
98 static inline int is_overlapped( const struct file *file )
99 {
100     return !(file->options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
101 }
102
103 /* create a file from a file descriptor */
104 /* if the function fails the fd is closed */
105 static struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing )
106 {
107     struct file *file;
108
109     if ((file = alloc_object( &file_ops )))
110     {
111         file->name       = NULL;
112         file->access     = access;
113         file->options    = FILE_SYNCHRONOUS_IO_NONALERT;
114         file->removable  = 0;
115         if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj )))
116         {
117             release_object( file );
118             return NULL;
119         }
120     }
121     return file;
122 }
123
124
125 static struct object *create_file( const char *nameptr, size_t len, unsigned int access,
126                                    unsigned int sharing, int create, unsigned int options,
127                                    unsigned int attrs, int removable )
128 {
129     struct file *file;
130     int flags;
131     char *name;
132     mode_t mode;
133
134     if (!(name = mem_alloc( len + 1 ))) return NULL;
135     memcpy( name, nameptr, len );
136     name[len] = 0;
137
138     switch(create)
139     {
140     case FILE_CREATE:       flags = O_CREAT | O_EXCL; break;
141     case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
142     case FILE_SUPERSEDE:    flags = O_CREAT | O_TRUNC; break;
143     case FILE_OPEN:         flags = 0; break;
144     case FILE_OPEN_IF:      flags = O_CREAT; break;
145     case FILE_OVERWRITE:    flags = O_TRUNC; break;
146     default:                set_error( STATUS_INVALID_PARAMETER ); goto error;
147     }
148     switch(access & (GENERIC_READ | GENERIC_WRITE))
149     {
150     case 0: break;
151     case GENERIC_READ:  flags |= O_RDONLY; break;
152     case GENERIC_WRITE: flags |= O_WRONLY; break;
153     case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
154     }
155     mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
156
157     if (len >= 4 &&
158         (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
159         mode |= 0111;
160
161     if (!(file = alloc_object( &file_ops ))) goto error;
162
163     file->access     = access;
164     file->options    = options;
165     file->removable  = removable;
166     file->name       = name;
167     if (is_overlapped( file ))
168     {
169         init_async_queue (&file->read_q);
170         init_async_queue (&file->write_q);
171     }
172
173     /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
174     if (!(file->fd = alloc_fd( &file_fd_ops, &file->obj )) ||
175         !(file->fd = open_fd( file->fd, name, flags | O_NONBLOCK | O_LARGEFILE,
176                               &mode, access, sharing)))
177     {
178         release_object( file );
179         return NULL;
180     }
181     /* refuse to open a directory */
182     if (S_ISDIR(mode) && !(options & FILE_OPEN_FOR_BACKUP_INTENT))
183     {
184         set_error( STATUS_ACCESS_DENIED );
185         release_object( file );
186         return NULL;
187     }
188     /* check for serial port */
189     if (S_ISCHR(mode) && is_serial_fd( file->fd ))
190     {
191         struct object *obj = create_serial( file->fd, file->options );
192         release_object( file );
193         return obj;
194     }
195
196     return &file->obj;
197
198  error:
199     free( name );
200     return NULL;
201 }
202
203 /* check if two file objects point to the same file */
204 int is_same_file( struct file *file1, struct file *file2 )
205 {
206     return is_same_file_fd( file1->fd, file2->fd );
207 }
208
209 /* check if the file is on removable media */
210 int is_file_removable( struct file *file )
211 {
212     return file->removable;
213 }
214
215 /* create a temp file for anonymous mappings */
216 struct file *create_temp_file( int access )
217 {
218     char tmpfn[16];
219     int fd;
220
221     sprintf( tmpfn, "anonmap.XXXXXX" );  /* create it in the server directory */
222     fd = mkstemps( tmpfn, 0 );
223     if (fd == -1)
224     {
225         file_set_error();
226         return NULL;
227     }
228     unlink( tmpfn );
229     return create_file_for_fd( fd, access, 0 );
230 }
231
232 static void file_dump( struct object *obj, int verbose )
233 {
234     struct file *file = (struct file *)obj;
235     assert( obj->ops == &file_ops );
236     fprintf( stderr, "File fd=%p options=%08x name='%s'\n", file->fd, file->options, file->name );
237 }
238
239 static int file_get_poll_events( struct fd *fd )
240 {
241     struct file *file = get_fd_user( fd );
242     int events = 0;
243     assert( file->obj.ops == &file_ops );
244     if (file->access & GENERIC_READ) events |= POLLIN;
245     if (file->access & GENERIC_WRITE) events |= POLLOUT;
246     return events;
247 }
248
249 static void file_poll_event( struct fd *fd, int event )
250 {
251     struct file *file = get_fd_user( fd );
252     assert( file->obj.ops == &file_ops );
253     if (is_overlapped( file ))
254     {
255         if( IS_READY(file->read_q) && (POLLIN & event) )
256         {
257             async_notify(file->read_q.head, STATUS_ALERTED);
258             return;
259         }
260         if( IS_READY(file->write_q) && (POLLOUT & event) )
261         {
262             async_notify(file->write_q.head, STATUS_ALERTED);
263             return;
264         }
265     }
266     default_poll_event( fd, event );
267 }
268
269
270 static int file_flush( struct fd *fd, struct event **event )
271 {
272     int ret = (fsync( get_unix_fd(fd) ) != -1);
273     if (!ret) file_set_error();
274     return ret;
275 }
276
277 static int file_get_info( struct fd *fd, struct get_file_info_reply *reply, int *flags )
278 {
279     struct stat st;
280     struct file *file = get_fd_user( fd );
281     int unix_fd = get_unix_fd( fd );
282
283     if (reply)
284     {
285         if (fstat( unix_fd, &st ) == -1)
286         {
287             file_set_error();
288             return FD_TYPE_INVALID;
289         }
290         if (S_ISCHR(st.st_mode) || S_ISFIFO(st.st_mode) ||
291             S_ISSOCK(st.st_mode) || isatty(unix_fd)) reply->type = FILE_TYPE_CHAR;
292         else reply->type = FILE_TYPE_DISK;
293         if (S_ISDIR(st.st_mode)) reply->attr = FILE_ATTRIBUTE_DIRECTORY;
294         else reply->attr = FILE_ATTRIBUTE_ARCHIVE;
295         if (!(st.st_mode & S_IWUSR)) reply->attr |= FILE_ATTRIBUTE_READONLY;
296         reply->access_time = st.st_atime;
297         reply->write_time  = st.st_mtime;
298         reply->change_time = st.st_ctime;
299         if (S_ISDIR(st.st_mode))
300         {
301             reply->size_high  = 0;
302             reply->size_low   = 0;
303             reply->alloc_high = 0;
304             reply->alloc_low  = 0;
305         }
306         else
307         {
308             file_pos_t  alloc;
309             reply->size_high  = st.st_size >> 32;
310             reply->size_low   = st.st_size & 0xffffffff;
311             alloc = (file_pos_t)st.st_blksize * st.st_blocks;
312             reply->alloc_high = alloc >> 32;
313             reply->alloc_low  = alloc & 0xffffffff;
314         }
315         reply->links       = st.st_nlink;
316         reply->index_high  = st.st_dev;
317         reply->index_low   = st.st_ino;
318         reply->serial      = 0; /* FIXME */
319     }
320     *flags = 0;
321     if (is_overlapped( file )) *flags |= FD_FLAG_OVERLAPPED;
322     return FD_TYPE_DEFAULT;
323 }
324
325 static void file_queue_async(struct fd *fd, void *ptr, unsigned int status, int type, int count)
326 {
327     struct file *file = get_fd_user( fd );
328     struct async *async;
329     struct async_queue *q;
330
331     assert( file->obj.ops == &file_ops );
332
333     if (!is_overlapped( file ))
334     {
335         set_error ( STATUS_INVALID_HANDLE );
336         return;
337     }
338
339     switch(type)
340     {
341     case ASYNC_TYPE_READ:
342         q = &file->read_q;
343         break;
344     case ASYNC_TYPE_WRITE:
345         q = &file->write_q;
346         break;
347     default:
348         set_error( STATUS_INVALID_PARAMETER );
349         return;
350     }
351
352     async = find_async ( q, current, ptr );
353
354     if ( status == STATUS_PENDING )
355     {
356         int events;
357
358         if ( !async )
359             async = create_async ( &file->obj, current, ptr );
360         if ( !async )
361             return;
362
363         async->status = STATUS_PENDING;
364         if ( !async->q )
365             async_insert( q, async );
366
367         /* Check if the new pending request can be served immediately */
368         events = check_fd_events( fd, file_get_poll_events( fd ) );
369         if (events) file_poll_event ( fd, events );
370     }
371     else if ( async ) destroy_async ( async );
372     else set_error ( STATUS_INVALID_PARAMETER );
373
374     set_fd_events( fd, file_get_poll_events( fd ));
375 }
376
377 static struct fd *file_get_fd( struct object *obj )
378 {
379     struct file *file = (struct file *)obj;
380     assert( obj->ops == &file_ops );
381     return (struct fd *)grab_object( file->fd );
382 }
383
384 static void file_destroy( struct object *obj )
385 {
386     struct file *file = (struct file *)obj;
387     assert( obj->ops == &file_ops );
388
389     if (file->name)
390     {
391         if (file->options & FILE_DELETE_ON_CLOSE) unlink( file->name );
392         free( file->name );
393     }
394     if (is_overlapped( file ))
395     {
396         destroy_async_queue (&file->read_q);
397         destroy_async_queue (&file->write_q);
398     }
399     if (file->fd) release_object( file->fd );
400 }
401
402 /* set the last error depending on errno */
403 void file_set_error(void)
404 {
405     switch (errno)
406     {
407     case EAGAIN:    set_error( STATUS_SHARING_VIOLATION ); break;
408     case EBADF:     set_error( STATUS_INVALID_HANDLE ); break;
409     case ENOSPC:    set_error( STATUS_DISK_FULL ); break;
410     case EACCES:
411     case ESRCH:
412     case EPERM:     set_error( STATUS_ACCESS_DENIED ); break;
413     case EROFS:     set_error( STATUS_MEDIA_WRITE_PROTECTED ); break;
414     case EBUSY:     set_error( STATUS_FILE_LOCK_CONFLICT ); break;
415     case ENOENT:    set_error( STATUS_NO_SUCH_FILE ); break;
416     case EISDIR:    set_win32_error( ERROR_CANNOT_MAKE ); break;
417     case ENFILE:
418     case EMFILE:    set_error( STATUS_NO_MORE_FILES ); break;
419     case EEXIST:    set_error( STATUS_OBJECT_NAME_COLLISION ); break;
420     case EINVAL:    set_error( STATUS_INVALID_PARAMETER ); break;
421     case ESPIPE:    set_win32_error( ERROR_SEEK ); break;
422     case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
423     case EIO:       set_error( STATUS_ACCESS_VIOLATION ); break;
424     case ENOTDIR:   set_error( STATUS_NOT_A_DIRECTORY ); break;
425 #ifdef EOVERFLOW
426     case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
427 #endif
428     default:        perror("file_set_error"); set_win32_error( ERROR_UNKNOWN ); break;
429     }
430 }
431
432 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
433 {
434     return (struct file *)get_handle_obj( process, handle, access, &file_ops );
435 }
436
437 int get_file_unix_fd( struct file *file )
438 {
439     return get_unix_fd( file->fd );
440 }
441
442 static int set_file_pointer( obj_handle_t handle, unsigned int *low, int *high, int whence )
443 {
444     struct file *file;
445     off_t result,xto;
446
447     xto = *low+((off_t)*high<<32);
448     if (!(file = get_file_obj( current->process, handle, 0 )))
449         return 0;
450     if ((result = lseek( get_file_unix_fd(file), xto, whence))==-1)
451     {
452         /* Check for seek before start of file */
453
454         /* also check EPERM due to SuSE7 2.2.16 lseek() EPERM kernel bug */
455         if (((errno == EINVAL) || (errno == EPERM))
456             && (whence != SEEK_SET) && (*high < 0))
457             set_win32_error( ERROR_NEGATIVE_SEEK );
458         else
459             file_set_error();
460         release_object( file );
461         return 0;
462     }
463     *low  = result & 0xffffffff;
464     *high = result >> 32;
465     release_object( file );
466     return 1;
467 }
468
469 /* extend a file beyond the current end of file */
470 static int extend_file( struct file *file, off_t size )
471 {
472     static const char zero;
473     int unix_fd = get_file_unix_fd( file );
474
475     /* extend the file one byte beyond the requested size and then truncate it */
476     /* this should work around ftruncate implementations that can't extend files */
477     if ((lseek( unix_fd, size, SEEK_SET ) != -1) &&
478         (write( unix_fd, &zero, 1 ) != -1))
479     {
480         ftruncate( unix_fd, size );
481         return 1;
482     }
483     file_set_error();
484     return 0;
485 }
486
487 /* truncate file at current position */
488 static int truncate_file( struct file *file )
489 {
490     int ret = 0;
491     int unix_fd = get_file_unix_fd( file );
492     off_t pos = lseek( unix_fd, 0, SEEK_CUR );
493     off_t eof = lseek( unix_fd, 0, SEEK_END );
494
495     if (eof < pos) ret = extend_file( file, pos );
496     else
497     {
498         if (ftruncate( unix_fd, pos ) != -1) ret = 1;
499         else file_set_error();
500     }
501     lseek( unix_fd, pos, SEEK_SET );  /* restore file pos */
502     return ret;
503 }
504
505 /* try to grow the file to the specified size */
506 int grow_file( struct file *file, int size_high, int size_low )
507 {
508     int ret = 0;
509     struct stat st;
510     int unix_fd = get_file_unix_fd( file );
511     off_t old_pos, size = size_low + (((off_t)size_high)<<32);
512
513     if (fstat( unix_fd, &st ) == -1)
514     {
515         file_set_error();
516         return 0;
517     }
518     if (st.st_size >= size) return 1;  /* already large enough */
519     old_pos = lseek( unix_fd, 0, SEEK_CUR );  /* save old pos */
520     ret = extend_file( file, size );
521     lseek( unix_fd, old_pos, SEEK_SET );  /* restore file pos */
522     return ret;
523 }
524
525 /* create a file */
526 DECL_HANDLER(create_file)
527 {
528     struct object *file;
529
530     reply->handle = 0;
531     if ((file = create_file( get_req_data(), get_req_data_size(), req->access,
532                              req->sharing, req->create, req->options, req->attrs, req->removable )))
533     {
534         reply->handle = alloc_handle( current->process, file, req->access, req->inherit );
535         release_object( file );
536     }
537 }
538
539 /* allocate a file handle for a Unix fd */
540 DECL_HANDLER(alloc_file_handle)
541 {
542     struct file *file;
543     int fd;
544
545     reply->handle = 0;
546     if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
547     {
548         set_error( STATUS_INVALID_HANDLE );
549         return;
550     }
551     if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
552     {
553         reply->handle = alloc_handle( current->process, file, req->access, req->inherit );
554         release_object( file );
555     }
556 }
557
558 /* set a file current position */
559 DECL_HANDLER(set_file_pointer)
560 {
561     int high = req->high;
562     int low  = req->low;
563     set_file_pointer( req->handle, &low, &high, req->whence );
564     reply->new_low  = low;
565     reply->new_high = high;
566 }
567
568 /* truncate (or extend) a file */
569 DECL_HANDLER(truncate_file)
570 {
571     struct file *file;
572
573     if ((file = get_file_obj( current->process, req->handle, GENERIC_WRITE )))
574     {
575         truncate_file( file );
576         release_object( file );
577     }
578 }
579
580 /* lock a region of a file */
581 DECL_HANDLER(lock_file)
582 {
583     struct file *file;
584     file_pos_t offset = ((file_pos_t)req->offset_high << 32) | req->offset_low;
585     file_pos_t count = ((file_pos_t)req->count_high << 32) | req->count_low;
586
587     if ((file = get_file_obj( current->process, req->handle, 0 )))
588     {
589         reply->handle = lock_fd( file->fd, offset, count, req->shared, req->wait );
590         reply->overlapped = is_overlapped( file );
591         release_object( file );
592     }
593 }
594
595 /* unlock a region of a file */
596 DECL_HANDLER(unlock_file)
597 {
598     struct file *file;
599     file_pos_t offset = ((file_pos_t)req->offset_high << 32) | req->offset_low;
600     file_pos_t count = ((file_pos_t)req->count_high << 32) | req->count_low;
601
602     if ((file = get_file_obj( current->process, req->handle, 0 )))
603     {
604         unlock_fd( file->fd, offset, count );
605         release_object( file );
606     }
607 }