~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/server/file.c

Version: ~ [ wine-1.1.33 ] ~ [ wine-1.1.32 ] ~ [ wine-1.1.31 ] ~ [ wine-1.1.30 ] ~ [ wine-1.1.29 ] ~ [ wine-1.1.28 ] ~ [ wine-1.1.27 ] ~ [ wine-1.1.26 ] ~ [ wine-1.1.25 ] ~ [ wine-1.1.24 ] ~ [ wine-1.1.23 ] ~ [ wine-1.1.22 ] ~ [ wine-1.1.21 ] ~ [ wine-1.1.20 ] ~ [ wine-1.1.19 ] ~ [ wine-1.1.18 ] ~ [ wine-1.1.17 ] ~ [ wine-1.1.16 ] ~ [ wine-1.1.15 ] ~ [ wine-1.1.14 ] ~ [ wine-1.1.13 ] ~ [ wine-1.1.12 ] ~ [ wine-1.1.11 ] ~ [ wine-1.1.10 ] ~ [ wine-1.1.9 ] ~ [ wine-1.1.8 ] ~ [ wine-1.1.7 ] ~ [ wine-1.0.1 ] ~ [ wine-1.1.6 ] ~ [ wine-1.1.5 ] ~ [ wine-1.1.4 ] ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~

  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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 #ifdef HAVE_POLL_H
 43 #include <poll.h>
 44 #endif
 45 
 46 #include "ntstatus.h"
 47 #define WIN32_NO_STATUS
 48 #include "windef.h"
 49 #include "winternl.h"
 50 
 51 #include "file.h"
 52 #include "handle.h"
 53 #include "thread.h"
 54 #include "request.h"
 55 #include "process.h"
 56 #include "security.h"
 57 
 58 struct file
 59 {
 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 */
 65 };
 66 
 67 static unsigned int generic_file_map_access( unsigned int access );
 68 
 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 );
 74 
 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 
 79 static const struct object_ops file_ops =
 80 {
 81     sizeof(struct file),          /* size */
 82     file_dump,                    /* dump */
 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 */
 97 };
 98 
 99 static const struct fd_ops file_fd_ops =
100 {
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 */
109 };
110 
111 static inline int is_overlapped( const struct file *file )
112 {
113     return !(get_fd_options( file->fd ) & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
114 }
115 
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 )
119 {
120     struct file *file;
121     struct stat st;
122 
123     if (fstat( fd, &st ) == -1)
124     {
125         file_set_error();
126         return NULL;
127     }
128 
129     if ((file = alloc_object( &file_ops )))
130     {
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 )))
135         {
136             release_object( file );
137             return NULL;
138         }
139     }
140     return file;
141 }
142 
143 static struct object *create_file_obj( struct fd *fd, unsigned int access, mode_t mode )
144 {
145     struct file *file = alloc_object( &file_ops );
146 
147     if (!file) return NULL;
148     file->access  = access;
149     file->mode    = mode;
150     file->uid     = ~(uid_t)0;
151     file->fd      = fd;
152     grab_object( fd );
153     set_fd_user( fd, &file_fd_ops, &file->obj );
154     return &file->obj;
155 }
156 
157 static struct object *create_file( const char *nameptr, data_size_t len, unsigned int access,
158                                    unsigned int sharing, int create, unsigned int options,
159                                    unsigned int attrs, const struct security_descriptor *sd )
160 {
161     struct object *obj = NULL;
162     struct fd *fd;
163     int flags;
164     char *name;
165     mode_t mode;
166 
167     if (!(name = mem_alloc( len + 1 ))) return NULL;
168     memcpy( name, nameptr, len );
169     name[len] = 0;
170 
171     switch(create)
172     {
173     case FILE_CREATE:       flags = O_CREAT | O_EXCL; break;
174     case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
175     case FILE_SUPERSEDE:    flags = O_CREAT | O_TRUNC; break;
176     case FILE_OPEN:         flags = 0; break;
177     case FILE_OPEN_IF:      flags = O_CREAT; break;
178     case FILE_OVERWRITE:    flags = O_TRUNC; break;
179     default:                set_error( STATUS_INVALID_PARAMETER ); goto done;
180     }
181 
182     if (sd)
183     {
184         const SID *owner = sd_get_owner( sd );
185         if (!owner)
186             owner = token_get_user( current->process->token );
187         mode = sd_to_mode( sd, owner );
188     }
189     else
190         mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
191 
192     if (len >= 4 &&
193         (!strcasecmp( name + len - 4, ".exe" ) || !strcasecmp( name + len - 4, ".com" )))
194     {
195         if (mode & S_IRUSR)
196             mode |= S_IXUSR;
197         if (mode & S_IRGRP)
198             mode |= S_IXGRP;
199         if (mode & S_IROTH)
200             mode |= S_IXOTH;
201     }
202 
203     access = generic_file_map_access( access );
204 
205     /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
206     fd = open_fd( name, flags | O_NONBLOCK | O_LARGEFILE, &mode, access, sharing, options );
207     if (!fd) goto done;
208 
209     if (S_ISDIR(mode))
210         obj = create_dir_obj( fd, access, mode );
211     else if (S_ISCHR(mode) && is_serial_fd( fd ))
212         obj = create_serial( fd );
213     else
214         obj = create_file_obj( fd, access, mode );
215 
216     release_object( fd );
217 
218 done:
219     free( name );
220     return obj;
221 }
222 
223 /* check if two file objects point to the same file */
224 int is_same_file( struct file *file1, struct file *file2 )
225 {
226     return is_same_file_fd( file1->fd, file2->fd );
227 }
228 
229 /* create a temp file for anonymous mappings */
230 struct file *create_temp_file( int access )
231 {
232     char tmpfn[16];
233     int fd;
234 
235     sprintf( tmpfn, "anonmap.XXXXXX" );  /* create it in the server directory */
236     fd = mkstemps( tmpfn, 0 );
237     if (fd == -1)
238     {
239         file_set_error();
240         return NULL;
241     }
242     unlink( tmpfn );
243     return create_file_for_fd( fd, access, 0 );
244 }
245 
246 static void file_dump( struct object *obj, int verbose )
247 {
248     struct file *file = (struct file *)obj;
249     assert( obj->ops == &file_ops );
250     fprintf( stderr, "File fd=%p\n", file->fd );
251 }
252 
253 static int file_get_poll_events( struct fd *fd )
254 {
255     struct file *file = get_fd_user( fd );
256     int events = 0;
257     assert( file->obj.ops == &file_ops );
258     if (file->access & FILE_UNIX_READ_ACCESS) events |= POLLIN;
259     if (file->access & FILE_UNIX_WRITE_ACCESS) events |= POLLOUT;
260     return events;
261 }
262 
263 static void file_flush( struct fd *fd, struct event **event )
264 {
265     int unix_fd = get_unix_fd( fd );
266     if (unix_fd != -1 && fsync( unix_fd ) == -1) file_set_error();
267 }
268 
269 static enum server_fd_type file_get_fd_type( struct fd *fd )
270 {
271     struct file *file = get_fd_user( fd );
272 
273     if (S_ISREG(file->mode) || S_ISBLK(file->mode)) return FD_TYPE_FILE;
274     if (S_ISDIR(file->mode)) return FD_TYPE_DIR;
275     return FD_TYPE_CHAR;
276 }
277 
278 static struct fd *file_get_fd( struct object *obj )
279 {
280     struct file *file = (struct file *)obj;
281     assert( obj->ops == &file_ops );
282     return (struct fd *)grab_object( file->fd );
283 }
284 
285 static unsigned int generic_file_map_access( unsigned int access )
286 {
287     if (access & GENERIC_READ)    access |= FILE_GENERIC_READ;
288     if (access & GENERIC_WRITE)   access |= FILE_GENERIC_WRITE;
289     if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
290     if (access & GENERIC_ALL)     access |= FILE_ALL_ACCESS;
291     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
292 }
293 
294 struct security_descriptor *mode_to_sd( mode_t mode, const SID *user, const SID *group )
295 {
296     struct security_descriptor *sd;
297     size_t dacl_size;
298     ACE_HEADER *current_ace;
299     ACCESS_ALLOWED_ACE *aaa;
300     ACL *dacl;
301     SID *sid;
302     char *ptr;
303     const SID *world_sid = security_world_sid;
304     const SID *local_system_sid = security_local_system_sid;
305 
306     dacl_size = sizeof(ACL) + FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
307         FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]);
308     if (mode & S_IRWXU)
309         dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
310             FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
311     if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
312         (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
313         (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
314         dacl_size += FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) +
315             FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
316     if (mode & S_IRWXO)
317         dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
318             FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]);
319 
320     sd = mem_alloc( sizeof(struct security_descriptor) +
321                     FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) +
322                     FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]) +
323                     dacl_size );
324     if (!sd) return sd;
325 
326     sd->control = SE_DACL_PRESENT;
327     sd->owner_len = FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
328     sd->group_len = FIELD_OFFSET(SID, SubAuthority[group->SubAuthorityCount]);
329     sd->sacl_len = 0;
330     sd->dacl_len = dacl_size;
331 
332     ptr = (char *)(sd + 1);
333     memcpy( ptr, user, sd->owner_len );
334     ptr += sd->owner_len;
335     memcpy( ptr, group, sd->group_len );
336     ptr += sd->group_len;
337 
338     dacl = (ACL *)ptr;
339     dacl->AclRevision = ACL_REVISION;
340     dacl->Sbz1 = 0;
341     dacl->AclSize = dacl_size;
342     dacl->AceCount = 1 + (mode & S_IRWXU ? 1 : 0) + (mode & S_IRWXO ? 1 : 0);
343     if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
344         (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
345         (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
346         dacl->AceCount++;
347     dacl->Sbz2 = 0;
348 
349     /* always give FILE_ALL_ACCESS for Local System */
350     aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
351     current_ace = &aaa->Header;
352     aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
353     aaa->Header.AceFlags = 0;
354     aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
355         FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]);
356     aaa->Mask = FILE_ALL_ACCESS;
357     sid = (SID *)&aaa->SidStart;
358     memcpy( sid, local_system_sid, FIELD_OFFSET(SID, SubAuthority[local_system_sid->SubAuthorityCount]) );
359 
360     if (mode & S_IRWXU)
361     {
362         /* appropriate access rights for the user */
363         aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
364         current_ace = &aaa->Header;
365         aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
366         aaa->Header.AceFlags = 0;
367         aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
368                               FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
369         aaa->Mask = WRITE_DAC | WRITE_OWNER;
370         if (mode & S_IRUSR)
371             aaa->Mask |= FILE_GENERIC_READ;
372         if (mode & S_IWUSR)
373             aaa->Mask |= FILE_GENERIC_WRITE | DELETE;
374         if (mode & S_IXUSR)
375             aaa->Mask |= FILE_GENERIC_EXECUTE;
376         sid = (SID *)&aaa->SidStart;
377         memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
378     }
379     if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
380         (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
381         (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
382     {
383         /* deny just in case the user is a member of the group */
384         ACCESS_DENIED_ACE *ada = (ACCESS_DENIED_ACE *)ace_next( current_ace );
385         current_ace = &ada->Header;
386         ada->Header.AceType = ACCESS_DENIED_ACE_TYPE;
387         ada->Header.AceFlags = 0;
388         ada->Header.AceSize = FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) +
389                               FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]);
390         ada->Mask = 0;
391         if (!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH)))
392             ada->Mask |= FILE_GENERIC_READ;
393         if (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IROTH)))
394             ada->Mask |= FILE_GENERIC_WRITE | DELETE;
395         if (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH)))
396             ada->Mask |= FILE_GENERIC_EXECUTE;
397         ada->Mask &= ~STANDARD_RIGHTS_ALL; /* never deny standard rights */
398         sid = (SID *)&ada->SidStart;
399         memcpy( sid, user, FIELD_OFFSET(SID, SubAuthority[user->SubAuthorityCount]) );
400     }
401     if (mode & S_IRWXO)
402     {
403         /* appropriate access rights for Everyone */
404         aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
405         current_ace = &aaa->Header;
406         aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
407         aaa->Header.AceFlags = 0;
408         aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
409                              FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]);
410         aaa->Mask = 0;
411         if (mode & S_IROTH)
412             aaa->Mask |= FILE_GENERIC_READ;
413         if (mode & S_IWOTH)
414             aaa->Mask |= FILE_GENERIC_WRITE | DELETE;
415         if (mode & S_IXOTH)
416             aaa->Mask |= FILE_GENERIC_EXECUTE;
417         sid = (SID *)&aaa->SidStart;
418         memcpy( sid, world_sid, FIELD_OFFSET(SID, SubAuthority[world_sid->SubAuthorityCount]) );
419     }
420 
421     return sd;
422 }
423 
424 static struct security_descriptor *file_get_sd( struct object *obj )
425 {
426     struct file *file = (struct file *)obj;
427     struct stat st;
428     int unix_fd;
429     struct security_descriptor *sd;
430 
431     assert( obj->ops == &file_ops );
432 
433     unix_fd = get_file_unix_fd( file );
434 
435     if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
436         return obj->sd;
437 
438     /* mode and uid the same? if so, no need to re-generate security descriptor */
439     if (obj->sd && (st.st_mode & (S_IRWXU|S_IRWXO)) == (file->mode & (S_IRWXU|S_IRWXO)) &&
440         (st.st_uid == file->uid))
441         return obj->sd;
442 
443     sd = mode_to_sd( st.st_mode,
444                      security_unix_uid_to_sid( st.st_uid ),
445                      token_get_primary_group( current->process->token ));
446     if (!sd) return obj->sd;
447 
448     file->mode = st.st_mode;
449     file->uid = st.st_uid;
450     free( obj->sd );
451     obj->sd = sd;
452     return sd;
453 }
454 
455 mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner )
456 {
457     mode_t new_mode = 0;
458     mode_t denied_mode = 0;
459     int present;
460     const ACL *dacl = sd_get_dacl( sd, &present );
461     if (present && dacl)
462     {
463         const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
464         ULONG i;
465         for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
466         {
467             const ACCESS_ALLOWED_ACE *aa_ace;
468             const ACCESS_DENIED_ACE *ad_ace;
469             const SID *sid;
470 
471             if (ace->AceFlags & INHERIT_ONLY_ACE) continue;
472 
473             switch (ace->AceType)
474             {
475                 case ACCESS_DENIED_ACE_TYPE:
476                     ad_ace = (const ACCESS_DENIED_ACE *)ace;
477                     sid = (const SID *)&ad_ace->SidStart;
478                     if (security_equal_sid( sid, security_world_sid ))
479                     {
480                         unsigned int access = generic_file_map_access( ad_ace->Mask );
481                         if (access & FILE_READ_DATA)
482                             denied_mode |= S_IRUSR|S_IRGRP|S_IROTH;
483                         if (access & FILE_WRITE_DATA)
484                             denied_mode |= S_IWUSR|S_IWGRP|S_IWOTH;
485                         if (access & FILE_EXECUTE)
486                             denied_mode |= S_IXUSR|S_IXGRP|S_IXOTH;
487                     }
488                     else if (security_equal_sid( sid, owner ))
489                     {
490                         unsigned int access = generic_file_map_access( ad_ace->Mask );
491                         if (access & FILE_READ_DATA)
492                             denied_mode |= S_IRUSR;
493                         if (access & FILE_WRITE_DATA)
494                             denied_mode |= S_IWUSR;
495                         if (access & FILE_EXECUTE)
496                             denied_mode |= S_IXUSR;
497                     }
498                     break;
499                 case ACCESS_ALLOWED_ACE_TYPE:
500                     aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
501                     sid = (const SID *)&aa_ace->SidStart;
502                     if (security_equal_sid( sid, security_world_sid ))
503                     {
504                         unsigned int access = generic_file_map_access( aa_ace->Mask );
505                         if (access & FILE_READ_DATA)
506                             new_mode |= S_IRUSR|S_IRGRP|S_IROTH;
507                         if (access & FILE_WRITE_DATA)
508                             new_mode |= S_IWUSR|S_IWGRP|S_IWOTH;
509                         if (access & FILE_EXECUTE)
510                             new_mode |= S_IXUSR|S_IXGRP|S_IXOTH;
511                     }
512                     else if (security_equal_sid( sid, owner ))
513                     {
514                         unsigned int access = generic_file_map_access( aa_ace->Mask );
515                         if (access & FILE_READ_DATA)
516                             new_mode |= S_IRUSR;
517                         if (access & FILE_WRITE_DATA)
518                             new_mode |= S_IWUSR;
519                         if (access & FILE_EXECUTE)
520                             new_mode |= S_IXUSR;
521                     }
522                     break;
523             }
524         }
525     }
526     else
527         /* no ACL means full access rights to anyone */
528         new_mode = S_IRWXU | S_IRWXO;
529 
530     return new_mode & ~denied_mode;
531 }
532 
533 static int file_set_sd( struct object *obj, const struct security_descriptor *sd,
534                         unsigned int set_info )
535 {
536     struct file *file = (struct file *)obj;
537     const SID *owner;
538     struct stat st;
539     mode_t mode;
540     int unix_fd;
541 
542     assert( obj->ops == &file_ops );
543 
544     unix_fd = get_file_unix_fd( file );
545 
546     if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
547 
548     if (set_info & OWNER_SECURITY_INFORMATION)
549     {
550         owner = sd_get_owner( sd );
551         if (!owner)
552         {
553             set_error( STATUS_INVALID_SECURITY_DESCR );
554             return 0;
555         }
556         if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
557         {
558             /* FIXME: get Unix uid and call fchown */
559         }
560     }
561     else if (obj->sd)
562         owner = sd_get_owner( obj->sd );
563     else
564         owner = token_get_user( current->process->token );
565 
566     /* group and sacl not supported */
567 
568     if (set_info & DACL_SECURITY_INFORMATION)
569     {
570         /* keep the bits that we don't map to access rights in the ACL */
571         mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXG);
572         mode |= sd_to_mode( sd, owner );
573 
574         if (st.st_mode != mode && fchmod( unix_fd, mode ) == -1)
575         {
576             file_set_error();
577             return 0;
578         }
579     }
580     return 1;
581 }
582 
583 static void file_destroy( struct object *obj )
584 {
585     struct file *file = (struct file *)obj;
586     assert( obj->ops == &file_ops );
587 
588     if (file->fd) release_object( file->fd );
589 }
590 
591 /* set the last error depending on errno */
592 void file_set_error(void)
593 {
594     switch (errno)
595     {
596     case ETXTBSY:
597     case EAGAIN:    set_error( STATUS_SHARING_VIOLATION ); break;
598     case EBADF:     set_error( STATUS_INVALID_HANDLE ); break;
599     case ENOSPC:    set_error( STATUS_DISK_FULL ); break;
600     case EACCES:
601     case ESRCH:
602     case EROFS:
603     case EPERM:     set_error( STATUS_ACCESS_DENIED ); break;
604     case EBUSY:     set_error( STATUS_FILE_LOCK_CONFLICT ); break;
605     case ENOENT:    set_error( STATUS_NO_SUCH_FILE ); break;
606     case EISDIR:    set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
607     case ENFILE:
608     case EMFILE:    set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
609     case EEXIST:    set_error( STATUS_OBJECT_NAME_COLLISION ); break;
610     case EINVAL:    set_error( STATUS_INVALID_PARAMETER ); break;
611     case ESPIPE:    set_error( STATUS_ILLEGAL_FUNCTION ); break;
612     case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
613     case EIO:       set_error( STATUS_ACCESS_VIOLATION ); break;
614     case ENOTDIR:   set_error( STATUS_NOT_A_DIRECTORY ); break;
615     case EFBIG:     set_error( STATUS_SECTION_TOO_BIG ); break;
616     case ENODEV:    set_error( STATUS_NO_SUCH_DEVICE ); break;
617     case ENXIO:     set_error( STATUS_NO_SUCH_DEVICE ); break;
618 #ifdef EOVERFLOW
619     case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
620 #endif
621     default:
622         perror("wineserver: file_set_error() can't map error");
623         set_error( STATUS_UNSUCCESSFUL );
624         break;
625     }
626 }
627 
628 struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
629 {
630     return (struct file *)get_handle_obj( process, handle, access, &file_ops );
631 }
632 
633 int get_file_unix_fd( struct file *file )
634 {
635     return get_unix_fd( file->fd );
636 }
637 
638 struct file *grab_file_unless_removable( struct file *file )
639 {
640     if (is_fd_removable( file->fd )) return NULL;
641     return (struct file *)grab_object( file );
642 }
643 
644 /* extend a file beyond the current end of file */
645 static int extend_file( struct file *file, file_pos_t new_size )
646 {
647     static const char zero;
648     int unix_fd = get_file_unix_fd( file );
649     off_t size = new_size;
650 
651     if (unix_fd == -1) return 0;
652 
653     if (sizeof(new_size) > sizeof(size) && size != new_size)
654     {
655         set_error( STATUS_INVALID_PARAMETER );
656         return 0;
657     }
658     /* extend the file one byte beyond the requested size and then truncate it */
659     /* this should work around ftruncate implementations that can't extend files */
660     if (pwrite( unix_fd, &zero, 1, size ) != -1)
661     {
662         ftruncate( unix_fd, size );
663         return 1;
664     }
665     file_set_error();
666     return 0;
667 }
668 
669 /* try to grow the file to the specified size */
670 int grow_file( struct file *file, file_pos_t size )
671 {
672     struct stat st;
673     int unix_fd = get_file_unix_fd( file );
674 
675     if (unix_fd == -1) return 0;
676 
677     if (fstat( unix_fd, &st ) == -1)
678     {
679         file_set_error();
680         return 0;
681     }
682     if (st.st_size >= size) return 1;  /* already large enough */
683     return extend_file( file, size );
684 }
685 
686 /* create a file */
687 DECL_HANDLER(create_file)
688 {
689     struct object *file;
690     const struct object_attributes *objattr = get_req_data();
691     const struct security_descriptor *sd;
692     const char *name;
693     data_size_t name_len;
694 
695     reply->handle = 0;
696 
697     if (!objattr_is_valid( objattr, get_req_data_size() ))
698         return;
699     /* name is transferred in the unix codepage outside of the objattr structure */
700     if (objattr->name_len)
701     {
702         set_error( STATUS_INVALID_PARAMETER );
703         return;
704     }
705 
706     sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
707 
708     name = (const char *)get_req_data() + sizeof(*objattr) + objattr->sd_len;
709     name_len = get_req_data_size() - sizeof(*objattr) - objattr->sd_len;
710 
711     reply->handle = 0;
712     if ((file = create_file( name, name_len, req->access,
713                              req->sharing, req->create, req->options,
714                              req->attrs, sd )))
715     {
716         reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
717         release_object( file );
718     }
719 }
720 
721 /* allocate a file handle for a Unix fd */
722 DECL_HANDLER(alloc_file_handle)
723 {
724     struct file *file;
725     int fd;
726 
727     reply->handle = 0;
728     if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
729     {
730         set_error( STATUS_INVALID_HANDLE );
731         return;
732     }
733     if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
734     {
735         reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
736         release_object( file );
737     }
738 }
739 
740 /* lock a region of a file */
741 DECL_HANDLER(lock_file)
742 {
743     struct file *file;
744 
745     if ((file = get_file_obj( current->process, req->handle, 0 )))
746     {
747         reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
748         reply->overlapped = is_overlapped( file );
749         release_object( file );
750     }
751 }
752 
753 /* unlock a region of a file */
754 DECL_HANDLER(unlock_file)
755 {
756     struct file *file;
757 
758     if ((file = get_file_obj( current->process, req->handle, 0 )))
759     {
760         unlock_fd( file->fd, req->offset, req->count );
761         release_object( file );
762     }
763 }
764 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.