file.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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. #include "config.h"
  21. #include "wine/port.h"
  22. #include <assert.h>
  23. #include <fcntl.h>
  24. #include <stdarg.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <errno.h>
  29. #include <sys/stat.h>
  30. #include <sys/time.h>
  31. #include <sys/types.h>
  32. #include <time.h>
  33. #include <limits.h>
  34. #include <unistd.h>
  35. #ifdef HAVE_UTIME_H
  36. #include <utime.h>
  37. #endif
  38. #ifdef HAVE_POLL_H
  39. #include <poll.h>
  40. #endif
  41. #include "ntstatus.h"
  42. #define WIN32_NO_STATUS
  43. #include "windef.h"
  44. #include "winternl.h"
  45. #include "file.h"
  46. #include "handle.h"
  47. #include "thread.h"
  48. #include "request.h"
  49. #include "process.h"
  50. #include "security.h"
  51. /* We intentionally do not match the Samba 4 extended attribute for NT security descriptors (SDs):
  52. * 1) Samba stores this information using an internal data structure (we use a flat NT SD).
  53. * 2) Samba uses the attribute "security.NTACL". This attribute is within a namespace that only
  54. * the administrator has write access to, which prohibits the user from copying the attributes
  55. * when copying a file and would require Wine to run with adminstrative privileges.
  56. */
  57. #define WINE_XATTR_SD XATTR_USER_PREFIX "wine.sd"
  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. struct list kernel_object; /* list of kernel object pointers */
  66. };
  67. static unsigned int generic_file_map_access( unsigned int access );
  68. static struct security_descriptor *get_xattr_sd( int fd );
  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 struct object *file_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr );
  74. static struct object *file_open_file( struct object *obj, unsigned int access,
  75. unsigned int sharing, unsigned int options );
  76. static struct list *file_get_kernel_obj_list( struct object *obj );
  77. static void file_destroy( struct object *obj );
  78. static int file_get_poll_events( struct fd *fd );
  79. static int file_flush( struct fd *fd, struct async *async );
  80. static enum server_fd_type file_get_fd_type( struct fd *fd );
  81. static const struct object_ops file_ops =
  82. {
  83. sizeof(struct file), /* size */
  84. file_dump, /* dump */
  85. file_get_type, /* get_type */
  86. add_queue, /* add_queue */
  87. remove_queue, /* remove_queue */
  88. default_fd_signaled, /* signaled */
  89. NULL, /* get_esync_fd */
  90. no_satisfied, /* satisfied */
  91. no_signal, /* signal */
  92. file_get_fd, /* get_fd */
  93. default_fd_map_access, /* map_access */
  94. file_get_sd, /* get_sd */
  95. file_set_sd, /* set_sd */
  96. file_lookup_name, /* lookup_name */
  97. no_link_name, /* link_name */
  98. NULL, /* unlink_name */
  99. file_open_file, /* open_file */
  100. file_get_kernel_obj_list, /* get_kernel_obj_list */
  101. no_alloc_handle, /* alloc_handle */
  102. fd_close_handle, /* close_handle */
  103. file_destroy /* destroy */
  104. };
  105. static const struct fd_ops file_fd_ops =
  106. {
  107. file_get_poll_events, /* get_poll_events */
  108. default_poll_event, /* poll_event */
  109. file_get_fd_type, /* get_fd_type */
  110. no_fd_read, /* read */
  111. no_fd_write, /* write */
  112. file_flush, /* flush */
  113. default_fd_get_file_info, /* get_file_info */
  114. no_fd_get_volume_info, /* get_volume_info */
  115. default_fd_ioctl, /* ioctl */
  116. default_fd_queue_async, /* queue_async */
  117. default_fd_reselect_async /* reselect_async */
  118. };
  119. /* create a file from a file descriptor */
  120. /* if the function fails the fd is closed */
  121. struct file *create_file_for_fd( int fd, unsigned int access, unsigned int sharing )
  122. {
  123. struct file *file;
  124. struct stat st;
  125. if (fstat( fd, &st ) == -1)
  126. {
  127. file_set_error();
  128. close( fd );
  129. return NULL;
  130. }
  131. if (!(file = alloc_object( &file_ops )))
  132. {
  133. close( fd );
  134. return NULL;
  135. }
  136. file->mode = st.st_mode;
  137. file->access = default_fd_map_access( &file->obj, access );
  138. list_init( &file->kernel_object );
  139. if (!(file->fd = create_anonymous_fd( &file_fd_ops, fd, &file->obj,
  140. FILE_SYNCHRONOUS_IO_NONALERT )))
  141. {
  142. release_object( file );
  143. return NULL;
  144. }
  145. allow_fd_caching( file->fd );
  146. return file;
  147. }
  148. /* create a file by duplicating an fd object */
  149. struct file *create_file_for_fd_obj( struct fd *fd, unsigned int access, unsigned int sharing )
  150. {
  151. struct file *file;
  152. struct stat st;
  153. if (fstat( get_unix_fd(fd), &st ) == -1)
  154. {
  155. file_set_error();
  156. return NULL;
  157. }
  158. if ((file = alloc_object( &file_ops )))
  159. {
  160. file->mode = st.st_mode;
  161. file->access = default_fd_map_access( &file->obj, access );
  162. list_init( &file->kernel_object );
  163. if (!(file->fd = dup_fd_object( fd, access, sharing, FILE_SYNCHRONOUS_IO_NONALERT )))
  164. {
  165. release_object( file );
  166. return NULL;
  167. }
  168. set_fd_user( file->fd, &file_fd_ops, &file->obj );
  169. }
  170. return file;
  171. }
  172. static struct object *create_file_obj( struct fd *fd, unsigned int access, mode_t mode,
  173. const struct security_descriptor *sd )
  174. {
  175. struct file *file = alloc_object( &file_ops );
  176. if (!file) return NULL;
  177. file->access = access;
  178. file->mode = mode;
  179. file->uid = ~(uid_t)0;
  180. file->fd = fd;
  181. list_init( &file->kernel_object );
  182. grab_object( fd );
  183. set_fd_user( fd, &file_fd_ops, &file->obj );
  184. if (sd) file_set_sd( &file->obj, sd, OWNER_SECURITY_INFORMATION |
  185. GROUP_SECURITY_INFORMATION |
  186. DACL_SECURITY_INFORMATION |
  187. SACL_SECURITY_INFORMATION );
  188. return &file->obj;
  189. }
  190. int is_file_executable( const char *name )
  191. {
  192. int len = strlen( name );
  193. return len >= 4 && (!strcasecmp( name + len - 4, ".exe") || !strcasecmp( name + len - 4, ".com" ));
  194. }
  195. static void set_xattr_sd( int fd, const struct security_descriptor *sd )
  196. {
  197. char buffer[XATTR_SIZE_MAX];
  198. int present, len;
  199. const ACL *dacl;
  200. /* there's no point in storing the security descriptor if there's no DACL */
  201. if (!sd) return;
  202. dacl = sd_get_dacl( sd, &present );
  203. if (!present || !dacl) return;
  204. len = 2 + sizeof(struct security_descriptor) + sd->owner_len +
  205. sd->group_len + sd->sacl_len + sd->dacl_len;
  206. if (len > XATTR_SIZE_MAX) return;
  207. /* include the descriptor revision and resource manager control bits */
  208. buffer[0] = SECURITY_DESCRIPTOR_REVISION;
  209. buffer[1] = 0;
  210. memcpy( &buffer[2], sd, len - 2 );
  211. xattr_fset( fd, WINE_XATTR_SD, buffer, len );
  212. }
  213. static struct security_descriptor *inherit_sd( const struct security_descriptor *parent_sd, int is_dir )
  214. {
  215. const DWORD inheritance_mask = INHERIT_ONLY_ACE | OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE;
  216. struct security_descriptor *sd = NULL;
  217. const ACL *parent_dacl;
  218. int present;
  219. ACL *dacl;
  220. parent_dacl = sd_get_dacl( parent_sd, &present );
  221. if (present && parent_dacl)
  222. {
  223. size_t dacl_size = sizeof(ACL), ace_count = 0;
  224. const ACE_HEADER *parent_ace;
  225. const SID *user, *group;
  226. ACE_HEADER *ace;
  227. char *ptr;
  228. ULONG i;
  229. /* Calculate the size of the DACL */
  230. parent_ace = (const ACE_HEADER *)(parent_dacl + 1);
  231. for (i = 0; i < parent_dacl->AceCount; i++, parent_ace = ace_next( parent_ace ))
  232. {
  233. if (!(parent_ace->AceFlags & inheritance_mask)) continue;
  234. ace_count++;
  235. dacl_size += parent_ace->AceSize;
  236. }
  237. if (!ace_count) return sd; /* No inheritance */
  238. /* Fill in the security descriptor so that it is compatible with our DACL */
  239. user = (const SID *)(parent_sd + 1);
  240. group = (const SID *)((char *)(parent_sd + 1) + parent_sd->owner_len);
  241. sd = mem_alloc( sizeof(struct security_descriptor) + parent_sd->owner_len
  242. + parent_sd->group_len + dacl_size );
  243. if (!sd) return sd;
  244. sd->control = SE_DACL_PRESENT;
  245. sd->owner_len = parent_sd->owner_len;
  246. sd->group_len = parent_sd->group_len;
  247. sd->sacl_len = 0;
  248. sd->dacl_len = dacl_size;
  249. ptr = (char *)(sd + 1);
  250. memcpy( ptr, user, sd->owner_len );
  251. ptr += sd->owner_len;
  252. memcpy( ptr, group, sd->group_len );
  253. ptr += sd->group_len;
  254. dacl = (ACL *)ptr;
  255. dacl->AclRevision = ACL_REVISION;
  256. dacl->Sbz1 = 0;
  257. dacl->AclSize = dacl_size;
  258. dacl->AceCount = ace_count;
  259. dacl->Sbz2 = 0;
  260. ace = (ACE_HEADER *)(dacl + 1);
  261. /* Build the new DACL, inheriting from the parent's information */
  262. parent_ace = (const ACE_HEADER *)(parent_dacl + 1);
  263. for (i = 0; i < parent_dacl->AceCount; i++, parent_ace = ace_next( parent_ace ))
  264. {
  265. DWORD flags = parent_ace->AceFlags;
  266. if (!(flags & inheritance_mask)) continue;
  267. ace->AceType = parent_ace->AceType;
  268. if (is_dir && (flags & CONTAINER_INHERIT_ACE))
  269. flags &= ~INHERIT_ONLY_ACE;
  270. else if (!is_dir && (flags & OBJECT_INHERIT_ACE))
  271. flags &= ~INHERIT_ONLY_ACE;
  272. else if (is_dir && (flags & OBJECT_INHERIT_ACE))
  273. flags |= INHERIT_ONLY_ACE;
  274. if (is_dir)
  275. ace->AceFlags = flags | INHERITED_ACE;
  276. else
  277. ace->AceFlags = (parent_ace->AceFlags & ~inheritance_mask) | INHERITED_ACE;
  278. ace->AceSize = parent_ace->AceSize;
  279. memcpy( ace + 1, parent_ace + 1, parent_ace->AceSize - sizeof(ACE_HEADER));
  280. ace = (ACE_HEADER *)ace_next( ace );
  281. }
  282. }
  283. return sd;
  284. }
  285. static struct security_descriptor *file_get_parent_sd( struct fd *root, const char *child_name,
  286. int child_len, int is_dir )
  287. {
  288. struct security_descriptor *parent_sd, *sd = NULL;
  289. mode_t parent_mode = 0555;
  290. char *p, *parent_name;
  291. struct fd *parent_fd;
  292. int unix_fd;
  293. if (!(parent_name = mem_alloc( child_len + 1 ))) return NULL;
  294. memcpy( parent_name, child_name, child_len );
  295. parent_name[child_len] = 0;
  296. /* skip trailing slashes */
  297. p = parent_name + strlen( parent_name ) - 1;
  298. while (p > parent_name && *p == '/') p--;
  299. /* remove last path component */
  300. if (p == parent_name && *p == '/')
  301. {
  302. free(parent_name);
  303. return NULL;
  304. }
  305. while (p > parent_name && *p != '/') p--;
  306. while (p > parent_name && *p == '/') p--;
  307. p[1] = 0;
  308. parent_fd = open_fd( root, parent_name, O_NONBLOCK | O_LARGEFILE, &parent_mode,
  309. READ_CONTROL|ACCESS_SYSTEM_SECURITY,
  310. FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
  311. FILE_OPEN_FOR_BACKUP_INTENT );
  312. free( parent_name );
  313. if (parent_fd)
  314. {
  315. unix_fd = get_unix_fd( parent_fd );
  316. if (unix_fd != -1)
  317. {
  318. parent_sd = get_xattr_sd( unix_fd );
  319. if (parent_sd)
  320. {
  321. sd = inherit_sd( parent_sd, is_dir );
  322. free( parent_sd );
  323. }
  324. }
  325. release_object( parent_fd );
  326. }
  327. return sd;
  328. }
  329. static struct object *create_file( struct fd *root, const char *nameptr, data_size_t len,
  330. unsigned int access, unsigned int sharing, int create,
  331. unsigned int options, unsigned int attrs,
  332. const struct security_descriptor *sd )
  333. {
  334. struct security_descriptor *temp_sd = NULL;
  335. struct object *obj = NULL;
  336. struct fd *fd;
  337. int flags;
  338. char *name;
  339. mode_t mode;
  340. if (!len || ((nameptr[0] == '/') ^ !root))
  341. {
  342. set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
  343. return NULL;
  344. }
  345. if (!(name = mem_alloc( len + 1 ))) return NULL;
  346. memcpy( name, nameptr, len );
  347. name[len] = 0;
  348. switch(create)
  349. {
  350. case FILE_CREATE: flags = O_CREAT | O_EXCL; break;
  351. case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
  352. access |= FILE_WRITE_ATTRIBUTES;
  353. case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break;
  354. case FILE_OPEN: flags = 0; break;
  355. case FILE_OPEN_IF: flags = O_CREAT; break;
  356. case FILE_OVERWRITE: flags = O_TRUNC;
  357. access |= FILE_WRITE_ATTRIBUTES; break;
  358. default: set_error( STATUS_INVALID_PARAMETER ); goto done;
  359. }
  360. /* Note: inheritance of security descriptors only occurs on creation when sd is NULL */
  361. if (!sd && (create == FILE_CREATE || create == FILE_OVERWRITE_IF))
  362. sd = temp_sd = file_get_parent_sd( root, nameptr, len, options & FILE_DIRECTORY_FILE );
  363. if (sd)
  364. {
  365. const SID *owner = sd_get_owner( sd );
  366. if (!owner)
  367. owner = token_get_user( current->process->token );
  368. mode = sd_to_mode( sd, owner );
  369. }
  370. else if (options & FILE_DIRECTORY_FILE)
  371. mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0555 : 0777;
  372. else
  373. mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
  374. if (is_file_executable( name ))
  375. {
  376. if (mode & S_IRUSR)
  377. mode |= S_IXUSR;
  378. if (mode & S_IRGRP)
  379. mode |= S_IXGRP;
  380. if (mode & S_IROTH)
  381. mode |= S_IXOTH;
  382. }
  383. access = generic_file_map_access( access );
  384. /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
  385. fd = open_fd( root, name, flags | O_NONBLOCK | O_LARGEFILE, &mode, access, sharing, options );
  386. if (!fd) goto done;
  387. if (S_ISDIR(mode))
  388. obj = create_dir_obj( fd, access, mode, sd );
  389. else if (S_ISCHR(mode) && is_serial_fd( fd ))
  390. obj = create_serial( fd );
  391. else
  392. obj = create_file_obj( fd, access, mode, sd );
  393. release_object( fd );
  394. done:
  395. free( temp_sd );
  396. free( name );
  397. return obj;
  398. }
  399. static void file_dump( struct object *obj, int verbose )
  400. {
  401. struct file *file = (struct file *)obj;
  402. assert( obj->ops == &file_ops );
  403. fprintf( stderr, "File fd=%p\n", file->fd );
  404. }
  405. struct object_type *file_get_type( struct object *obj )
  406. {
  407. static const struct unicode_str str = { type_File, sizeof(type_File) };
  408. return get_object_type( &str );
  409. }
  410. static int file_get_poll_events( struct fd *fd )
  411. {
  412. struct file *file = get_fd_user( fd );
  413. int events = 0;
  414. assert( file->obj.ops == &file_ops );
  415. if (file->access & FILE_UNIX_READ_ACCESS) events |= POLLIN;
  416. if (file->access & FILE_UNIX_WRITE_ACCESS) events |= POLLOUT;
  417. return events;
  418. }
  419. static int file_flush( struct fd *fd, struct async *async )
  420. {
  421. int unix_fd = get_unix_fd( fd );
  422. if (unix_fd != -1 && fsync( unix_fd ) == -1)
  423. {
  424. file_set_error();
  425. return 0;
  426. }
  427. return 1;
  428. }
  429. static enum server_fd_type file_get_fd_type( struct fd *fd )
  430. {
  431. struct file *file = get_fd_user( fd );
  432. if (S_ISREG(file->mode) || S_ISBLK(file->mode)) return FD_TYPE_FILE;
  433. if (S_ISDIR(file->mode)) return FD_TYPE_DIR;
  434. return FD_TYPE_CHAR;
  435. }
  436. static struct fd *file_get_fd( struct object *obj )
  437. {
  438. struct file *file = (struct file *)obj;
  439. assert( obj->ops == &file_ops );
  440. return (struct fd *)grab_object( file->fd );
  441. }
  442. static unsigned int generic_file_map_access( unsigned int access )
  443. {
  444. if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
  445. if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
  446. if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
  447. if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
  448. return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
  449. }
  450. struct security_descriptor *mode_to_sd( mode_t mode, const SID *user, const SID *group )
  451. {
  452. struct security_descriptor *sd;
  453. size_t dacl_size;
  454. ACE_HEADER *current_ace;
  455. ACCESS_ALLOWED_ACE *aaa;
  456. ACL *dacl;
  457. SID *sid;
  458. char *ptr;
  459. const SID *world_sid = security_world_sid;
  460. const SID *local_system_sid = security_local_system_sid;
  461. dacl_size = sizeof(ACL) + FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
  462. security_sid_len( local_system_sid );
  463. if (mode & S_IRWXU)
  464. dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
  465. if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
  466. (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
  467. (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
  468. dacl_size += FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
  469. if (mode & S_IRWXO)
  470. dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
  471. sd = mem_alloc( sizeof(struct security_descriptor) +
  472. security_sid_len( user ) + security_sid_len( group ) +
  473. dacl_size );
  474. if (!sd) return sd;
  475. sd->control = SE_DACL_PRESENT;
  476. sd->owner_len = security_sid_len( user );
  477. sd->group_len = security_sid_len( group );
  478. sd->sacl_len = 0;
  479. sd->dacl_len = dacl_size;
  480. ptr = (char *)(sd + 1);
  481. memcpy( ptr, user, sd->owner_len );
  482. ptr += sd->owner_len;
  483. memcpy( ptr, group, sd->group_len );
  484. ptr += sd->group_len;
  485. dacl = (ACL *)ptr;
  486. dacl->AclRevision = ACL_REVISION;
  487. dacl->Sbz1 = 0;
  488. dacl->AclSize = dacl_size;
  489. dacl->AceCount = 1 + (mode & S_IRWXU ? 1 : 0) + (mode & S_IRWXO ? 1 : 0);
  490. if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
  491. (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
  492. (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
  493. dacl->AceCount++;
  494. dacl->Sbz2 = 0;
  495. /* always give FILE_ALL_ACCESS for Local System */
  496. aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
  497. current_ace = &aaa->Header;
  498. aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
  499. aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
  500. aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( local_system_sid );
  501. aaa->Mask = FILE_ALL_ACCESS;
  502. sid = (SID *)&aaa->SidStart;
  503. memcpy( sid, local_system_sid, security_sid_len( local_system_sid ));
  504. if (mode & S_IRWXU)
  505. {
  506. /* appropriate access rights for the user */
  507. aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
  508. current_ace = &aaa->Header;
  509. aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
  510. aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
  511. aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
  512. aaa->Mask = WRITE_DAC | WRITE_OWNER;
  513. if (mode & S_IRUSR)
  514. aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
  515. if (mode & S_IWUSR)
  516. aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
  517. sid = (SID *)&aaa->SidStart;
  518. memcpy( sid, user, security_sid_len( user ));
  519. }
  520. if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
  521. (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
  522. (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
  523. {
  524. /* deny just in case the user is a member of the group */
  525. ACCESS_DENIED_ACE *ada = (ACCESS_DENIED_ACE *)ace_next( current_ace );
  526. current_ace = &ada->Header;
  527. ada->Header.AceType = ACCESS_DENIED_ACE_TYPE;
  528. ada->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
  529. ada->Header.AceSize = FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
  530. ada->Mask = 0;
  531. if (!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH)))
  532. ada->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
  533. if (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IROTH)))
  534. ada->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
  535. ada->Mask &= ~STANDARD_RIGHTS_ALL; /* never deny standard rights */
  536. sid = (SID *)&ada->SidStart;
  537. memcpy( sid, user, security_sid_len( user ));
  538. }
  539. if (mode & S_IRWXO)
  540. {
  541. /* appropriate access rights for Everyone */
  542. aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
  543. current_ace = &aaa->Header;
  544. aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
  545. aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
  546. aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
  547. aaa->Mask = 0;
  548. if (mode & S_IROTH)
  549. aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
  550. if (mode & S_IWOTH)
  551. aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
  552. sid = (SID *)&aaa->SidStart;
  553. memcpy( sid, world_sid, security_sid_len( world_sid ));
  554. }
  555. return sd;
  556. }
  557. /* Convert generic rights into standard access rights */
  558. static void convert_generic_sd( struct security_descriptor *sd )
  559. {
  560. const ACL *dacl;
  561. int present;
  562. dacl = sd_get_dacl( sd, &present );
  563. if (present && dacl)
  564. {
  565. const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
  566. ULONG i;
  567. for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
  568. {
  569. DWORD *mask = (DWORD *)(ace + 1);
  570. *mask = generic_file_map_access( *mask );
  571. }
  572. }
  573. }
  574. static struct security_descriptor *get_xattr_sd( int fd )
  575. {
  576. struct security_descriptor *sd;
  577. char buffer[XATTR_SIZE_MAX];
  578. int n;
  579. n = xattr_fget( fd, WINE_XATTR_SD, buffer, sizeof(buffer) );
  580. if (n == -1 || n < 2 + sizeof(struct security_descriptor)) return NULL;
  581. /* validate that we can handle the descriptor */
  582. if (buffer[0] != SECURITY_DESCRIPTOR_REVISION || buffer[1] != 0 ||
  583. !sd_is_valid( (struct security_descriptor *)&buffer[2], n - 2 ))
  584. return NULL;
  585. sd = mem_alloc( n - 2 );
  586. if (sd)
  587. {
  588. memcpy( sd, &buffer[2], n - 2 );
  589. convert_generic_sd( sd ); /* for backwards compatibility */
  590. }
  591. return sd;
  592. }
  593. struct security_descriptor *get_file_sd( struct object *obj, struct fd *fd, mode_t *mode,
  594. uid_t *uid )
  595. {
  596. int unix_fd = get_unix_fd( fd );
  597. struct stat st;
  598. struct security_descriptor *sd;
  599. if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
  600. return obj->sd;
  601. /* mode and uid the same? if so, no need to re-generate security descriptor */
  602. if (obj->sd && (st.st_mode & (S_IRWXU|S_IRWXO)) == (*mode & (S_IRWXU|S_IRWXO)) &&
  603. (st.st_uid == *uid))
  604. return obj->sd;
  605. sd = get_xattr_sd( unix_fd );
  606. if (!sd) sd = mode_to_sd( st.st_mode,
  607. security_unix_uid_to_sid( st.st_uid ),
  608. token_get_primary_group( current->process->token ));
  609. if (!sd) return obj->sd;
  610. *mode = st.st_mode;
  611. *uid = st.st_uid;
  612. free( obj->sd );
  613. obj->sd = sd;
  614. return sd;
  615. }
  616. static struct security_descriptor *file_get_sd( struct object *obj )
  617. {
  618. struct file *file = (struct file *)obj;
  619. struct security_descriptor *sd;
  620. struct fd *fd;
  621. assert( obj->ops == &file_ops );
  622. fd = file_get_fd( obj );
  623. sd = get_file_sd( obj, fd, &file->mode, &file->uid );
  624. release_object( fd );
  625. return sd;
  626. }
  627. static mode_t file_access_to_mode( unsigned int access )
  628. {
  629. mode_t mode = 0;
  630. access = generic_file_map_access( access );
  631. if (access & FILE_READ_DATA) mode |= 4;
  632. if (access & (FILE_WRITE_DATA|FILE_APPEND_DATA)) mode |= 2;
  633. if (access & FILE_EXECUTE) mode |= 1;
  634. return mode;
  635. }
  636. mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner )
  637. {
  638. mode_t new_mode = 0;
  639. mode_t bits_to_set = ~0;
  640. mode_t mode;
  641. int present;
  642. const ACL *dacl = sd_get_dacl( sd, &present );
  643. if (present && dacl)
  644. {
  645. const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
  646. ULONG i;
  647. for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
  648. {
  649. const ACCESS_ALLOWED_ACE *aa_ace;
  650. const ACCESS_DENIED_ACE *ad_ace;
  651. const SID *sid;
  652. if (ace->AceFlags & INHERIT_ONLY_ACE) continue;
  653. switch (ace->AceType)
  654. {
  655. case ACCESS_DENIED_ACE_TYPE:
  656. ad_ace = (const ACCESS_DENIED_ACE *)ace;
  657. sid = (const SID *)&ad_ace->SidStart;
  658. mode = file_access_to_mode( ad_ace->Mask );
  659. if (security_equal_sid( sid, security_world_sid ))
  660. {
  661. bits_to_set &= ~(mode << 0); /* all */
  662. }
  663. if (token_sid_present( current->process->token, sid, TRUE ))
  664. {
  665. bits_to_set &= ~(mode << 3); /* group */
  666. }
  667. if (security_equal_sid( sid, owner ))
  668. {
  669. bits_to_set &= ~(mode << 6); /* user */
  670. }
  671. break;
  672. case ACCESS_ALLOWED_ACE_TYPE:
  673. aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
  674. sid = (const SID *)&aa_ace->SidStart;
  675. mode = file_access_to_mode( aa_ace->Mask );
  676. if (security_equal_sid( sid, security_world_sid ))
  677. {
  678. new_mode |= (mode << 0) & bits_to_set; /* all */
  679. bits_to_set &= ~(mode << 0);
  680. }
  681. if (token_sid_present( current->process->token, sid, FALSE ))
  682. {
  683. new_mode |= (mode << 3) & bits_to_set; /* group */
  684. bits_to_set &= ~(mode << 3);
  685. }
  686. if (security_equal_sid( sid, owner ))
  687. {
  688. new_mode |= (mode << 6) & bits_to_set; /* user */
  689. bits_to_set &= ~(mode << 6);
  690. }
  691. break;
  692. }
  693. }
  694. new_mode |= (new_mode & S_IRWXO) << 3;
  695. new_mode |= (new_mode & S_IRWXG) << 3;
  696. }
  697. else
  698. /* no ACL means full access rights to anyone */
  699. new_mode = S_IRWXU | S_IRWXG | S_IRWXO;
  700. return new_mode;
  701. }
  702. int set_file_sd( struct object *obj, struct fd *fd, mode_t *mode, uid_t *uid,
  703. const struct security_descriptor *sd, unsigned int set_info )
  704. {
  705. struct security_descriptor *new_sd;
  706. int unix_fd = get_unix_fd( fd );
  707. const SID *owner, *group;
  708. struct stat st;
  709. mode_t new_mode;
  710. if (!set_info || unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
  711. if (!obj->sd) get_file_sd( obj, fd, mode, uid );
  712. /* calculate the new sd, save to a temporary variable before assigning */
  713. new_sd = set_sd_from_token_internal( sd, obj->sd, set_info, current->process->token );
  714. if (new_sd)
  715. {
  716. /* convert generic rights into standard access rights */
  717. convert_generic_sd( new_sd );
  718. if (set_info & OWNER_SECURITY_INFORMATION)
  719. {
  720. owner = sd_get_owner( new_sd );
  721. assert( owner );
  722. if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
  723. {
  724. /* FIXME: get Unix uid and call fchown */
  725. }
  726. }
  727. if (set_info & GROUP_SECURITY_INFORMATION)
  728. {
  729. group = sd_get_group( new_sd );
  730. assert( group );
  731. if (!obj->sd || !security_equal_sid( group, sd_get_group( obj->sd ) ))
  732. {
  733. /* FIXME: get Unix uid and call fchown */
  734. }
  735. }
  736. if (set_info & DACL_SECURITY_INFORMATION)
  737. {
  738. owner = sd_get_owner( new_sd );
  739. assert( owner );
  740. /* keep the bits that we don't map to access rights in the ACL */
  741. new_mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
  742. new_mode |= sd_to_mode( new_sd, owner );
  743. if (((st.st_mode ^ new_mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, new_mode ) == -1)
  744. {
  745. free( new_sd );
  746. file_set_error();
  747. return 0;
  748. }
  749. *mode = new_mode;
  750. }
  751. /* extended attributes are set after the file mode, to ensure it stays in sync */
  752. set_xattr_sd( unix_fd, new_sd );
  753. free( obj->sd );
  754. obj->sd = new_sd;
  755. return 1;
  756. }
  757. return 0;
  758. }
  759. static struct object *file_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr )
  760. {
  761. if (!name || !name->len) return NULL; /* open the file itself */
  762. set_error( STATUS_OBJECT_PATH_NOT_FOUND );
  763. return NULL;
  764. }
  765. static struct object *file_open_file( struct object *obj, unsigned int access,
  766. unsigned int sharing, unsigned int options )
  767. {
  768. struct file *file = (struct file *)obj;
  769. struct object *new_file = NULL;
  770. char *unix_name;
  771. assert( obj->ops == &file_ops );
  772. if ((unix_name = dup_fd_name( file->fd, "" )))
  773. {
  774. new_file = create_file( NULL, unix_name, strlen(unix_name), access,
  775. sharing, FILE_OPEN, options, 0, NULL );
  776. free( unix_name );
  777. }
  778. else set_error( STATUS_OBJECT_TYPE_MISMATCH );
  779. return new_file;
  780. }
  781. static struct list *file_get_kernel_obj_list( struct object *obj )
  782. {
  783. struct file *file = (struct file *)obj;
  784. return &file->kernel_object;
  785. }
  786. static int file_set_sd( struct object *obj, const struct security_descriptor *sd,
  787. unsigned int set_info )
  788. {
  789. struct file *file = (struct file *)obj;
  790. struct fd *fd;
  791. int ret;
  792. assert( obj->ops == &file_ops );
  793. fd = file_get_fd( obj );
  794. ret = set_file_sd( obj, fd, &file->mode, &file->uid, sd, set_info );
  795. release_object( fd );
  796. return ret;
  797. }
  798. static void file_destroy( struct object *obj )
  799. {
  800. struct file *file = (struct file *)obj;
  801. assert( obj->ops == &file_ops );
  802. if (file->fd) release_object( file->fd );
  803. }
  804. /* set the last error depending on errno */
  805. void file_set_error(void)
  806. {
  807. switch (errno)
  808. {
  809. case ETXTBSY:
  810. case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
  811. case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
  812. case ENOSPC: set_error( STATUS_DISK_FULL ); break;
  813. case EACCES:
  814. case ESRCH:
  815. case EROFS:
  816. case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
  817. case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
  818. case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
  819. case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
  820. case ENFILE:
  821. case EMFILE: set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
  822. case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
  823. case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
  824. case ESPIPE: set_error( STATUS_ILLEGAL_FUNCTION ); break;
  825. case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
  826. case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
  827. case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
  828. case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
  829. case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break;
  830. case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break;
  831. case EXDEV: set_error( STATUS_NOT_SAME_DEVICE ); break;
  832. case ELOOP: set_error( STATUS_REPARSE_POINT_NOT_RESOLVED ); break;
  833. #ifdef EOVERFLOW
  834. case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
  835. #endif
  836. default:
  837. perror("wineserver: file_set_error() can't map error");
  838. set_error( STATUS_UNSUCCESSFUL );
  839. break;
  840. }
  841. }
  842. struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
  843. {
  844. return (struct file *)get_handle_obj( process, handle, access, &file_ops );
  845. }
  846. int get_file_unix_fd( struct file *file )
  847. {
  848. return get_unix_fd( file->fd );
  849. }
  850. /* create a file */
  851. DECL_HANDLER(create_file)
  852. {
  853. struct object *file;
  854. struct fd *root_fd = NULL;
  855. struct unicode_str unicode_name;
  856. const struct security_descriptor *sd;
  857. const struct object_attributes *objattr = get_req_object_attributes( &sd, &unicode_name, NULL );
  858. const char *name;
  859. data_size_t name_len;
  860. if (!objattr) return;
  861. /* name is transferred in the unix codepage outside of the objattr structure */
  862. if (unicode_name.len)
  863. {
  864. set_error( STATUS_INVALID_PARAMETER );
  865. return;
  866. }
  867. if (objattr->rootdir)
  868. {
  869. struct dir *root;
  870. if (!(root = get_dir_obj( current->process, objattr->rootdir, 0 ))) return;
  871. root_fd = get_obj_fd( (struct object *)root );
  872. release_object( root );
  873. if (!root_fd) return;
  874. }
  875. name = get_req_data_after_objattr( objattr, &name_len );
  876. reply->handle = 0;
  877. if ((file = create_file( root_fd, name, name_len, req->access, req->sharing,
  878. req->create, req->options, req->attrs, sd )))
  879. {
  880. if (get_error() == STATUS_OBJECT_NAME_EXISTS)
  881. reply->handle = alloc_handle( current->process, file, req->access, objattr->attributes );
  882. else
  883. reply->handle = alloc_handle_no_access_check( current->process, file, req->access, objattr->attributes );
  884. release_object( file );
  885. }
  886. if (root_fd) release_object( root_fd );
  887. }
  888. /* allocate a file handle for a Unix fd */
  889. DECL_HANDLER(alloc_file_handle)
  890. {
  891. struct file *file;
  892. int fd;
  893. reply->handle = 0;
  894. if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
  895. {
  896. set_error( STATUS_INVALID_HANDLE );
  897. return;
  898. }
  899. if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
  900. {
  901. reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
  902. release_object( file );
  903. }
  904. }
  905. /* lock a region of a file */
  906. DECL_HANDLER(lock_file)
  907. {
  908. struct file *file;
  909. if ((file = get_file_obj( current->process, req->handle, 0 )))
  910. {
  911. reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
  912. reply->overlapped = is_fd_overlapped( file->fd );
  913. release_object( file );
  914. }
  915. }
  916. /* unlock a region of a file */
  917. DECL_HANDLER(unlock_file)
  918. {
  919. struct file *file;
  920. if ((file = get_file_obj( current->process, req->handle, 0 )))
  921. {
  922. unlock_fd( file->fd, req->offset, req->count );
  923. release_object( file );
  924. }
  925. }