file.c 25 KB

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