file.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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 <unistd.h>
  34. #ifdef HAVE_UTIME_H
  35. #include <utime.h>
  36. #endif
  37. #ifdef HAVE_POLL_H
  38. #include <poll.h>
  39. #endif
  40. #include "ntstatus.h"
  41. #define WIN32_NO_STATUS
  42. #include "windef.h"
  43. #include "winternl.h"
  44. #include "file.h"
  45. #include "handle.h"
  46. #include "thread.h"
  47. #include "request.h"
  48. #include "process.h"
  49. #include "security.h"
  50. static const WCHAR file_name[] = {'F','i','l','e'};
  51. struct type_descr file_type =
  52. {
  53. { file_name, sizeof(file_name) }, /* name */
  54. FILE_ALL_ACCESS, /* valid_access */
  55. { /* mapping */
  56. FILE_GENERIC_READ,
  57. FILE_GENERIC_WRITE,
  58. FILE_GENERIC_EXECUTE,
  59. FILE_ALL_ACCESS
  60. },
  61. };
  62. struct file
  63. {
  64. struct object obj; /* object header */
  65. struct fd *fd; /* file descriptor for this file */
  66. unsigned int access; /* file access (FILE_READ_DATA etc.) */
  67. mode_t mode; /* file stat.st_mode */
  68. uid_t uid; /* file stat.st_uid */
  69. struct list kernel_object; /* list of kernel object pointers */
  70. };
  71. static void file_dump( struct object *obj, int verbose );
  72. static struct fd *file_get_fd( struct object *obj );
  73. static struct security_descriptor *file_get_sd( struct object *obj );
  74. static int file_set_sd( struct object *obj, const struct security_descriptor *sd, unsigned int set_info );
  75. static struct object *file_lookup_name( struct object *obj, struct unicode_str *name,
  76. unsigned int attr, struct object *root );
  77. static struct object *file_open_file( struct object *obj, unsigned int access,
  78. unsigned int sharing, unsigned int options );
  79. static struct list *file_get_kernel_obj_list( struct object *obj );
  80. static void file_destroy( struct object *obj );
  81. static enum server_fd_type file_get_fd_type( struct fd *fd );
  82. static const struct object_ops file_ops =
  83. {
  84. sizeof(struct file), /* size */
  85. &file_type, /* type */
  86. file_dump, /* dump */
  87. add_queue, /* add_queue */
  88. remove_queue, /* remove_queue */
  89. default_fd_signaled, /* signaled */
  90. no_satisfied, /* satisfied */
  91. no_signal, /* signal */
  92. file_get_fd, /* get_fd */
  93. default_map_access, /* map_access */
  94. file_get_sd, /* get_sd */
  95. file_set_sd, /* set_sd */
  96. no_get_full_name, /* get_full_name */
  97. file_lookup_name, /* lookup_name */
  98. no_link_name, /* link_name */
  99. NULL, /* unlink_name */
  100. file_open_file, /* open_file */
  101. file_get_kernel_obj_list, /* get_kernel_obj_list */
  102. no_close_handle, /* close_handle */
  103. file_destroy /* destroy */
  104. };
  105. static const struct fd_ops file_fd_ops =
  106. {
  107. default_fd_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. no_fd_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_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_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. {
  174. struct file *file = alloc_object( &file_ops );
  175. if (!file) return NULL;
  176. file->access = access;
  177. file->mode = mode;
  178. file->uid = ~(uid_t)0;
  179. file->fd = fd;
  180. list_init( &file->kernel_object );
  181. grab_object( fd );
  182. set_fd_user( fd, &file_fd_ops, &file->obj );
  183. return &file->obj;
  184. }
  185. int is_file_executable( const char *name )
  186. {
  187. int len = strlen( name );
  188. return len >= 4 && (!strcasecmp( name + len - 4, ".exe") || !strcasecmp( name + len - 4, ".com" ));
  189. }
  190. static struct object *create_file( struct fd *root, const char *nameptr, data_size_t len,
  191. struct unicode_str nt_name,
  192. unsigned int access, unsigned int sharing, int create,
  193. unsigned int options, unsigned int attrs,
  194. const struct security_descriptor *sd )
  195. {
  196. struct object *obj = NULL;
  197. struct fd *fd;
  198. int flags;
  199. char *name;
  200. mode_t mode;
  201. if (!len || ((nameptr[0] == '/') ^ !root) || (nt_name.len && ((nt_name.str[0] == '\\') ^ !root)))
  202. {
  203. set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
  204. return NULL;
  205. }
  206. if (!(name = mem_alloc( len + 1 ))) return NULL;
  207. memcpy( name, nameptr, len );
  208. name[len] = 0;
  209. switch(create)
  210. {
  211. case FILE_CREATE: flags = O_CREAT | O_EXCL; break;
  212. case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
  213. access |= FILE_WRITE_ATTRIBUTES;
  214. case FILE_SUPERSEDE: flags = O_CREAT | O_TRUNC; break;
  215. case FILE_OPEN: flags = 0; break;
  216. case FILE_OPEN_IF: flags = O_CREAT; break;
  217. case FILE_OVERWRITE: flags = O_TRUNC;
  218. access |= FILE_WRITE_ATTRIBUTES; break;
  219. default: set_error( STATUS_INVALID_PARAMETER ); goto done;
  220. }
  221. if (sd)
  222. {
  223. const SID *owner = sd_get_owner( sd );
  224. if (!owner)
  225. owner = token_get_user( current->process->token );
  226. mode = sd_to_mode( sd, owner );
  227. }
  228. else if (options & FILE_DIRECTORY_FILE)
  229. mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0555 : 0777;
  230. else
  231. mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
  232. if (is_file_executable( name ))
  233. {
  234. if (mode & S_IRUSR)
  235. mode |= S_IXUSR;
  236. if (mode & S_IRGRP)
  237. mode |= S_IXGRP;
  238. if (mode & S_IROTH)
  239. mode |= S_IXOTH;
  240. }
  241. access = map_access( access, &file_type.mapping );
  242. /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
  243. fd = open_fd( root, name, nt_name, flags | O_NONBLOCK | O_LARGEFILE, &mode, access, sharing, options );
  244. if (!fd) goto done;
  245. if (S_ISDIR(mode))
  246. obj = create_dir_obj( fd, access, mode );
  247. else if (S_ISCHR(mode) && is_serial_fd( fd ))
  248. obj = create_serial( fd );
  249. else
  250. obj = create_file_obj( fd, access, mode );
  251. release_object( fd );
  252. done:
  253. free( name );
  254. return obj;
  255. }
  256. static void file_dump( struct object *obj, int verbose )
  257. {
  258. struct file *file = (struct file *)obj;
  259. assert( obj->ops == &file_ops );
  260. fprintf( stderr, "File fd=%p\n", file->fd );
  261. }
  262. static enum server_fd_type file_get_fd_type( struct fd *fd )
  263. {
  264. struct file *file = get_fd_user( fd );
  265. if (S_ISREG(file->mode) || S_ISBLK(file->mode)) return FD_TYPE_FILE;
  266. if (S_ISDIR(file->mode)) return FD_TYPE_DIR;
  267. return FD_TYPE_CHAR;
  268. }
  269. static struct fd *file_get_fd( struct object *obj )
  270. {
  271. struct file *file = (struct file *)obj;
  272. assert( obj->ops == &file_ops );
  273. return (struct fd *)grab_object( file->fd );
  274. }
  275. struct security_descriptor *mode_to_sd( mode_t mode, const SID *user, const SID *group )
  276. {
  277. struct security_descriptor *sd;
  278. size_t dacl_size;
  279. ACE_HEADER *current_ace;
  280. ACCESS_ALLOWED_ACE *aaa;
  281. ACL *dacl;
  282. SID *sid;
  283. char *ptr;
  284. const SID *world_sid = security_world_sid;
  285. const SID *local_system_sid = security_local_system_sid;
  286. dacl_size = sizeof(ACL) + FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) +
  287. security_sid_len( local_system_sid );
  288. if (mode & S_IRWXU)
  289. dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
  290. if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
  291. (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
  292. (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
  293. dacl_size += FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
  294. if (mode & S_IRWXO)
  295. dacl_size += FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
  296. sd = mem_alloc( sizeof(struct security_descriptor) +
  297. security_sid_len( user ) + security_sid_len( group ) +
  298. dacl_size );
  299. if (!sd) return sd;
  300. sd->control = SE_DACL_PRESENT;
  301. sd->owner_len = security_sid_len( user );
  302. sd->group_len = security_sid_len( group );
  303. sd->sacl_len = 0;
  304. sd->dacl_len = dacl_size;
  305. ptr = (char *)(sd + 1);
  306. memcpy( ptr, user, sd->owner_len );
  307. ptr += sd->owner_len;
  308. memcpy( ptr, group, sd->group_len );
  309. ptr += sd->group_len;
  310. dacl = (ACL *)ptr;
  311. dacl->AclRevision = ACL_REVISION;
  312. dacl->Sbz1 = 0;
  313. dacl->AclSize = dacl_size;
  314. dacl->AceCount = 1 + (mode & S_IRWXU ? 1 : 0) + (mode & S_IRWXO ? 1 : 0);
  315. if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
  316. (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
  317. (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
  318. dacl->AceCount++;
  319. dacl->Sbz2 = 0;
  320. /* always give FILE_ALL_ACCESS for Local System */
  321. aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
  322. current_ace = &aaa->Header;
  323. aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
  324. aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
  325. aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( local_system_sid );
  326. aaa->Mask = FILE_ALL_ACCESS;
  327. sid = (SID *)&aaa->SidStart;
  328. memcpy( sid, local_system_sid, security_sid_len( local_system_sid ));
  329. if (mode & S_IRWXU)
  330. {
  331. /* appropriate access rights for the user */
  332. aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
  333. current_ace = &aaa->Header;
  334. aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
  335. aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
  336. aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( user );
  337. aaa->Mask = WRITE_DAC | WRITE_OWNER;
  338. if (mode & S_IRUSR)
  339. aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
  340. if (mode & S_IWUSR)
  341. aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
  342. sid = (SID *)&aaa->SidStart;
  343. memcpy( sid, user, security_sid_len( user ));
  344. }
  345. if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
  346. (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
  347. (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
  348. {
  349. /* deny just in case the user is a member of the group */
  350. ACCESS_DENIED_ACE *ada = (ACCESS_DENIED_ACE *)ace_next( current_ace );
  351. current_ace = &ada->Header;
  352. ada->Header.AceType = ACCESS_DENIED_ACE_TYPE;
  353. ada->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
  354. ada->Header.AceSize = FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart) + security_sid_len( user );
  355. ada->Mask = 0;
  356. if (!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH)))
  357. ada->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
  358. if (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IROTH)))
  359. ada->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
  360. ada->Mask &= ~STANDARD_RIGHTS_ALL; /* never deny standard rights */
  361. sid = (SID *)&ada->SidStart;
  362. memcpy( sid, user, security_sid_len( user ));
  363. }
  364. if (mode & S_IRWXO)
  365. {
  366. /* appropriate access rights for Everyone */
  367. aaa = (ACCESS_ALLOWED_ACE *)ace_next( current_ace );
  368. current_ace = &aaa->Header;
  369. aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
  370. aaa->Header.AceFlags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
  371. aaa->Header.AceSize = FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart) + security_sid_len( world_sid );
  372. aaa->Mask = 0;
  373. if (mode & S_IROTH)
  374. aaa->Mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
  375. if (mode & S_IWOTH)
  376. aaa->Mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
  377. sid = (SID *)&aaa->SidStart;
  378. memcpy( sid, world_sid, security_sid_len( world_sid ));
  379. }
  380. return sd;
  381. }
  382. static struct security_descriptor *file_get_sd( struct object *obj )
  383. {
  384. struct file *file = (struct file *)obj;
  385. struct stat st;
  386. int unix_fd;
  387. struct security_descriptor *sd;
  388. assert( obj->ops == &file_ops );
  389. unix_fd = get_file_unix_fd( file );
  390. if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
  391. return obj->sd;
  392. /* mode and uid the same? if so, no need to re-generate security descriptor */
  393. if (obj->sd && (st.st_mode & (S_IRWXU|S_IRWXO)) == (file->mode & (S_IRWXU|S_IRWXO)) &&
  394. (st.st_uid == file->uid))
  395. return obj->sd;
  396. sd = mode_to_sd( st.st_mode,
  397. security_unix_uid_to_sid( st.st_uid ),
  398. token_get_primary_group( current->process->token ));
  399. if (!sd) return obj->sd;
  400. file->mode = st.st_mode;
  401. file->uid = st.st_uid;
  402. free( obj->sd );
  403. obj->sd = sd;
  404. return sd;
  405. }
  406. static mode_t file_access_to_mode( unsigned int access )
  407. {
  408. mode_t mode = 0;
  409. access = map_access( access, &file_type.mapping );
  410. if (access & FILE_READ_DATA) mode |= 4;
  411. if (access & (FILE_WRITE_DATA|FILE_APPEND_DATA)) mode |= 2;
  412. if (access & FILE_EXECUTE) mode |= 1;
  413. return mode;
  414. }
  415. mode_t sd_to_mode( const struct security_descriptor *sd, const SID *owner )
  416. {
  417. mode_t new_mode = 0;
  418. mode_t bits_to_set = ~0;
  419. mode_t mode;
  420. int present;
  421. const ACL *dacl = sd_get_dacl( sd, &present );
  422. if (present && dacl)
  423. {
  424. const ACE_HEADER *ace = (const ACE_HEADER *)(dacl + 1);
  425. ULONG i;
  426. for (i = 0; i < dacl->AceCount; i++, ace = ace_next( ace ))
  427. {
  428. const ACCESS_ALLOWED_ACE *aa_ace;
  429. const ACCESS_DENIED_ACE *ad_ace;
  430. const SID *sid;
  431. if (ace->AceFlags & INHERIT_ONLY_ACE) continue;
  432. switch (ace->AceType)
  433. {
  434. case ACCESS_DENIED_ACE_TYPE:
  435. ad_ace = (const ACCESS_DENIED_ACE *)ace;
  436. sid = (const SID *)&ad_ace->SidStart;
  437. mode = file_access_to_mode( ad_ace->Mask );
  438. if (security_equal_sid( sid, security_world_sid ))
  439. {
  440. bits_to_set &= ~((mode << 6) | (mode << 3) | mode); /* all */
  441. }
  442. else if (token_sid_present( current->process->token, owner, TRUE ) &&
  443. token_sid_present( current->process->token, sid, TRUE ))
  444. {
  445. bits_to_set &= ~((mode << 6) | (mode << 3)); /* user + group */
  446. }
  447. else if (security_equal_sid( sid, owner ))
  448. {
  449. bits_to_set &= ~(mode << 6); /* user only */
  450. }
  451. break;
  452. case ACCESS_ALLOWED_ACE_TYPE:
  453. aa_ace = (const ACCESS_ALLOWED_ACE *)ace;
  454. sid = (const SID *)&aa_ace->SidStart;
  455. mode = file_access_to_mode( aa_ace->Mask );
  456. if (security_equal_sid( sid, security_world_sid ))
  457. {
  458. mode = (mode << 6) | (mode << 3) | mode; /* all */
  459. new_mode |= mode & bits_to_set;
  460. bits_to_set &= ~mode;
  461. }
  462. else if (token_sid_present( current->process->token, owner, FALSE ) &&
  463. token_sid_present( current->process->token, sid, FALSE ))
  464. {
  465. mode = (mode << 6) | (mode << 3); /* user + group */
  466. new_mode |= mode & bits_to_set;
  467. bits_to_set &= ~mode;
  468. }
  469. else if (security_equal_sid( sid, owner ))
  470. {
  471. mode = (mode << 6); /* user only */
  472. new_mode |= mode & bits_to_set;
  473. bits_to_set &= ~mode;
  474. }
  475. break;
  476. }
  477. }
  478. }
  479. else
  480. /* no ACL means full access rights to anyone */
  481. new_mode = S_IRWXU | S_IRWXG | S_IRWXO;
  482. return new_mode;
  483. }
  484. static int file_set_sd( struct object *obj, const struct security_descriptor *sd,
  485. unsigned int set_info )
  486. {
  487. struct file *file = (struct file *)obj;
  488. const SID *owner;
  489. struct stat st;
  490. mode_t mode;
  491. int unix_fd;
  492. assert( obj->ops == &file_ops );
  493. unix_fd = get_file_unix_fd( file );
  494. if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
  495. if (set_info & OWNER_SECURITY_INFORMATION)
  496. {
  497. owner = sd_get_owner( sd );
  498. if (!owner)
  499. {
  500. set_error( STATUS_INVALID_SECURITY_DESCR );
  501. return 0;
  502. }
  503. if (!obj->sd || !security_equal_sid( owner, sd_get_owner( obj->sd ) ))
  504. {
  505. /* FIXME: get Unix uid and call fchown */
  506. }
  507. }
  508. else if (obj->sd)
  509. owner = sd_get_owner( obj->sd );
  510. else
  511. owner = token_get_user( current->process->token );
  512. /* group and sacl not supported */
  513. if (set_info & DACL_SECURITY_INFORMATION)
  514. {
  515. /* keep the bits that we don't map to access rights in the ACL */
  516. mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
  517. mode |= sd_to_mode( sd, owner );
  518. if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
  519. {
  520. file_set_error();
  521. return 0;
  522. }
  523. }
  524. return 1;
  525. }
  526. static struct object *file_lookup_name( struct object *obj, struct unicode_str *name,
  527. unsigned int attr, struct object *root )
  528. {
  529. if (!name || !name->len) return NULL; /* open the file itself */
  530. set_error( STATUS_OBJECT_PATH_NOT_FOUND );
  531. return NULL;
  532. }
  533. static struct object *file_open_file( struct object *obj, unsigned int access,
  534. unsigned int sharing, unsigned int options )
  535. {
  536. struct file *file = (struct file *)obj;
  537. struct object *new_file = NULL;
  538. struct unicode_str nt_name;
  539. char *unix_name;
  540. assert( obj->ops == &file_ops );
  541. if ((unix_name = dup_fd_name( file->fd, "" )))
  542. {
  543. get_nt_name( file->fd, &nt_name );
  544. new_file = create_file( NULL, unix_name, strlen(unix_name), nt_name, access,
  545. sharing, FILE_OPEN, options, 0, NULL );
  546. free( unix_name );
  547. }
  548. else set_error( STATUS_OBJECT_TYPE_MISMATCH );
  549. return new_file;
  550. }
  551. static struct list *file_get_kernel_obj_list( struct object *obj )
  552. {
  553. struct file *file = (struct file *)obj;
  554. return &file->kernel_object;
  555. }
  556. static void file_destroy( struct object *obj )
  557. {
  558. struct file *file = (struct file *)obj;
  559. assert( obj->ops == &file_ops );
  560. if (file->fd) release_object( file->fd );
  561. }
  562. /* set the last error depending on errno */
  563. void file_set_error(void)
  564. {
  565. switch (errno)
  566. {
  567. case ETXTBSY:
  568. case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
  569. case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
  570. case ENOSPC: set_error( STATUS_DISK_FULL ); break;
  571. case EACCES:
  572. case ESRCH:
  573. case EROFS:
  574. case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
  575. case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
  576. case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
  577. case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
  578. case ENFILE:
  579. case EMFILE: set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
  580. case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
  581. case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
  582. case ESPIPE: set_error( STATUS_ILLEGAL_FUNCTION ); break;
  583. case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
  584. case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
  585. case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
  586. case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
  587. case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break;
  588. case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break;
  589. case EXDEV: set_error( STATUS_NOT_SAME_DEVICE ); break;
  590. case ELOOP: set_error( STATUS_REPARSE_POINT_NOT_RESOLVED ); break;
  591. #ifdef EOVERFLOW
  592. case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
  593. #endif
  594. default:
  595. perror("wineserver: file_set_error() can't map error");
  596. set_error( STATUS_UNSUCCESSFUL );
  597. break;
  598. }
  599. }
  600. struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
  601. {
  602. return (struct file *)get_handle_obj( process, handle, access, &file_ops );
  603. }
  604. int get_file_unix_fd( struct file *file )
  605. {
  606. return get_unix_fd( file->fd );
  607. }
  608. /* create a file */
  609. DECL_HANDLER(create_file)
  610. {
  611. struct object *file;
  612. struct fd *root_fd = NULL;
  613. struct unicode_str nt_name;
  614. const struct security_descriptor *sd;
  615. const struct object_attributes *objattr = get_req_object_attributes( &sd, &nt_name, NULL );
  616. const char *name;
  617. data_size_t name_len;
  618. if (!objattr) return;
  619. if (objattr->rootdir)
  620. {
  621. struct dir *root;
  622. if (!(root = get_dir_obj( current->process, objattr->rootdir, 0 ))) return;
  623. root_fd = get_obj_fd( (struct object *)root );
  624. release_object( root );
  625. if (!root_fd) return;
  626. }
  627. name = get_req_data_after_objattr( objattr, &name_len );
  628. reply->handle = 0;
  629. if ((file = create_file( root_fd, name, name_len, nt_name, req->access, req->sharing,
  630. req->create, req->options, req->attrs, sd )))
  631. {
  632. reply->handle = alloc_handle( current->process, file, req->access, objattr->attributes );
  633. release_object( file );
  634. }
  635. if (root_fd) release_object( root_fd );
  636. }
  637. /* allocate a file handle for a Unix fd */
  638. DECL_HANDLER(alloc_file_handle)
  639. {
  640. struct file *file;
  641. int fd;
  642. reply->handle = 0;
  643. if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
  644. {
  645. set_error( STATUS_INVALID_HANDLE );
  646. return;
  647. }
  648. if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
  649. {
  650. reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
  651. release_object( file );
  652. }
  653. }
  654. /* lock a region of a file */
  655. DECL_HANDLER(lock_file)
  656. {
  657. struct file *file;
  658. if ((file = get_file_obj( current->process, req->handle, 0 )))
  659. {
  660. reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
  661. reply->overlapped = is_fd_overlapped( file->fd );
  662. release_object( file );
  663. }
  664. }
  665. /* unlock a region of a file */
  666. DECL_HANDLER(unlock_file)
  667. {
  668. struct file *file;
  669. if ((file = get_file_obj( current->process, req->handle, 0 )))
  670. {
  671. unlock_fd( file->fd, req->offset, req->count );
  672. release_object( file );
  673. }
  674. }