file.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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 struct 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 struct sid *user, const struct sid *group )
  274. {
  275. struct security_descriptor *sd;
  276. unsigned char flags;
  277. size_t dacl_size;
  278. struct ace *ace;
  279. struct acl *dacl;
  280. char *ptr;
  281. dacl_size = sizeof(*dacl) + sizeof(*ace) + sid_len( &local_system_sid );
  282. if (mode & S_IRWXU) dacl_size += sizeof(*ace) + sid_len( user );
  283. if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
  284. (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
  285. (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
  286. dacl_size += sizeof(*ace) + sid_len( user );
  287. if (mode & S_IRWXO) dacl_size += sizeof(*ace) + sid_len( &world_sid );
  288. sd = mem_alloc( sizeof(*sd) + sid_len( user ) + sid_len( group ) + dacl_size );
  289. if (!sd) return sd;
  290. sd->control = SE_DACL_PRESENT;
  291. sd->owner_len = sid_len( user );
  292. sd->group_len = sid_len( group );
  293. sd->sacl_len = 0;
  294. sd->dacl_len = dacl_size;
  295. ptr = (char *)(sd + 1);
  296. memcpy( ptr, user, sd->owner_len );
  297. ptr += sd->owner_len;
  298. memcpy( ptr, group, sd->group_len );
  299. ptr += sd->group_len;
  300. dacl = (struct acl *)ptr;
  301. dacl->revision = ACL_REVISION;
  302. dacl->pad1 = 0;
  303. dacl->size = dacl_size;
  304. dacl->count = 1;
  305. dacl->pad2 = 0;
  306. if (mode & S_IRWXU) dacl->count++;
  307. if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
  308. (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
  309. (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
  310. dacl->count++;
  311. if (mode & S_IRWXO) dacl->count++;
  312. flags = (mode & S_IFDIR) ? OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE : 0;
  313. /* always give FILE_ALL_ACCESS for Local System */
  314. ace = set_ace( (struct ace *)(dacl + 1), &local_system_sid,
  315. ACCESS_ALLOWED_ACE_TYPE, flags, FILE_ALL_ACCESS );
  316. if (mode & S_IRWXU)
  317. {
  318. /* appropriate access rights for the user */
  319. ace = set_ace( ace_next( ace ), user, ACCESS_ALLOWED_ACE_TYPE, flags, WRITE_DAC | WRITE_OWNER );
  320. if (mode & S_IRUSR) ace->mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
  321. if (mode & S_IWUSR) ace->mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
  322. }
  323. if ((!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH))) ||
  324. (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IWOTH))) ||
  325. (!(mode & S_IXUSR) && (mode & (S_IXGRP|S_IXOTH))))
  326. {
  327. /* deny just in case the user is a member of the group */
  328. ace = set_ace( ace_next( ace ), user, ACCESS_DENIED_ACE_TYPE, flags, 0 );
  329. if (!(mode & S_IRUSR) && (mode & (S_IRGRP|S_IROTH)))
  330. ace->mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
  331. if (!(mode & S_IWUSR) && (mode & (S_IWGRP|S_IROTH)))
  332. ace->mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
  333. ace->mask &= ~STANDARD_RIGHTS_ALL; /* never deny standard rights */
  334. }
  335. if (mode & S_IRWXO)
  336. {
  337. /* appropriate access rights for Everyone */
  338. ace = set_ace( ace_next( ace ), &world_sid, ACCESS_ALLOWED_ACE_TYPE, flags, 0 );
  339. if (mode & S_IROTH) ace->mask |= FILE_GENERIC_READ | FILE_GENERIC_EXECUTE;
  340. if (mode & S_IWOTH) ace->mask |= FILE_GENERIC_WRITE | DELETE | FILE_DELETE_CHILD;
  341. }
  342. return sd;
  343. }
  344. static struct security_descriptor *file_get_sd( struct object *obj )
  345. {
  346. struct file *file = (struct file *)obj;
  347. struct stat st;
  348. int unix_fd;
  349. struct security_descriptor *sd;
  350. assert( obj->ops == &file_ops );
  351. unix_fd = get_file_unix_fd( file );
  352. if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
  353. return obj->sd;
  354. /* mode and uid the same? if so, no need to re-generate security descriptor */
  355. if (obj->sd && (st.st_mode & (S_IRWXU|S_IRWXO)) == (file->mode & (S_IRWXU|S_IRWXO)) &&
  356. (st.st_uid == file->uid))
  357. return obj->sd;
  358. sd = mode_to_sd( st.st_mode,
  359. security_unix_uid_to_sid( st.st_uid ),
  360. token_get_primary_group( current->process->token ));
  361. if (!sd) return obj->sd;
  362. file->mode = st.st_mode;
  363. file->uid = st.st_uid;
  364. free( obj->sd );
  365. obj->sd = sd;
  366. return sd;
  367. }
  368. static mode_t file_access_to_mode( unsigned int access )
  369. {
  370. mode_t mode = 0;
  371. access = map_access( access, &file_type.mapping );
  372. if (access & FILE_READ_DATA) mode |= 4;
  373. if (access & (FILE_WRITE_DATA|FILE_APPEND_DATA)) mode |= 2;
  374. if (access & FILE_EXECUTE) mode |= 1;
  375. return mode;
  376. }
  377. mode_t sd_to_mode( const struct security_descriptor *sd, const struct sid *owner )
  378. {
  379. mode_t new_mode = 0;
  380. mode_t bits_to_set = ~0;
  381. mode_t mode;
  382. int present;
  383. const struct acl *dacl = sd_get_dacl( sd, &present );
  384. if (present && dacl)
  385. {
  386. const struct ace *ace = (const struct ace *)(dacl + 1);
  387. unsigned int i;
  388. for (i = 0; i < dacl->count; i++, ace = ace_next( ace ))
  389. {
  390. const struct sid *sid = (const struct sid *)(ace + 1);
  391. if (ace->flags & INHERIT_ONLY_ACE) continue;
  392. mode = file_access_to_mode( ace->mask );
  393. switch (ace->type)
  394. {
  395. case ACCESS_DENIED_ACE_TYPE:
  396. if (equal_sid( sid, &world_sid ))
  397. {
  398. bits_to_set &= ~((mode << 6) | (mode << 3) | mode); /* all */
  399. }
  400. else if (token_sid_present( current->process->token, owner, TRUE ) &&
  401. token_sid_present( current->process->token, sid, TRUE ))
  402. {
  403. bits_to_set &= ~((mode << 6) | (mode << 3)); /* user + group */
  404. }
  405. else if (equal_sid( sid, owner ))
  406. {
  407. bits_to_set &= ~(mode << 6); /* user only */
  408. }
  409. break;
  410. case ACCESS_ALLOWED_ACE_TYPE:
  411. if (equal_sid( sid, &world_sid ))
  412. {
  413. mode = (mode << 6) | (mode << 3) | mode; /* all */
  414. new_mode |= mode & bits_to_set;
  415. bits_to_set &= ~mode;
  416. }
  417. else if (token_sid_present( current->process->token, owner, FALSE ) &&
  418. token_sid_present( current->process->token, sid, FALSE ))
  419. {
  420. mode = (mode << 6) | (mode << 3); /* user + group */
  421. new_mode |= mode & bits_to_set;
  422. bits_to_set &= ~mode;
  423. }
  424. else if (equal_sid( sid, owner ))
  425. {
  426. mode = (mode << 6); /* user only */
  427. new_mode |= mode & bits_to_set;
  428. bits_to_set &= ~mode;
  429. }
  430. break;
  431. }
  432. }
  433. }
  434. else
  435. /* no ACL means full access rights to anyone */
  436. new_mode = S_IRWXU | S_IRWXG | S_IRWXO;
  437. return new_mode;
  438. }
  439. static int file_set_sd( struct object *obj, const struct security_descriptor *sd,
  440. unsigned int set_info )
  441. {
  442. struct file *file = (struct file *)obj;
  443. const struct sid *owner;
  444. struct stat st;
  445. mode_t mode;
  446. int unix_fd;
  447. assert( obj->ops == &file_ops );
  448. unix_fd = get_file_unix_fd( file );
  449. if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
  450. if (set_info & OWNER_SECURITY_INFORMATION)
  451. {
  452. owner = sd_get_owner( sd );
  453. if (!owner)
  454. {
  455. set_error( STATUS_INVALID_SECURITY_DESCR );
  456. return 0;
  457. }
  458. if (!obj->sd || !equal_sid( owner, sd_get_owner( obj->sd ) ))
  459. {
  460. /* FIXME: get Unix uid and call fchown */
  461. }
  462. }
  463. else if (obj->sd)
  464. owner = sd_get_owner( obj->sd );
  465. else
  466. owner = token_get_user( current->process->token );
  467. /* group and sacl not supported */
  468. if (set_info & DACL_SECURITY_INFORMATION)
  469. {
  470. /* keep the bits that we don't map to access rights in the ACL */
  471. mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
  472. mode |= sd_to_mode( sd, owner );
  473. if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
  474. {
  475. file_set_error();
  476. return 0;
  477. }
  478. }
  479. return 1;
  480. }
  481. static struct object *file_lookup_name( struct object *obj, struct unicode_str *name,
  482. unsigned int attr, struct object *root )
  483. {
  484. if (!name || !name->len) return NULL; /* open the file itself */
  485. set_error( STATUS_OBJECT_PATH_NOT_FOUND );
  486. return NULL;
  487. }
  488. static struct object *file_open_file( struct object *obj, unsigned int access,
  489. unsigned int sharing, unsigned int options )
  490. {
  491. struct file *file = (struct file *)obj;
  492. struct object *new_file = NULL;
  493. struct unicode_str nt_name;
  494. char *unix_name;
  495. assert( obj->ops == &file_ops );
  496. if ((unix_name = dup_fd_name( file->fd, "" )))
  497. {
  498. get_nt_name( file->fd, &nt_name );
  499. new_file = create_file( NULL, unix_name, strlen(unix_name), nt_name, access,
  500. sharing, FILE_OPEN, options, 0, NULL );
  501. free( unix_name );
  502. }
  503. else set_error( STATUS_OBJECT_TYPE_MISMATCH );
  504. return new_file;
  505. }
  506. static struct list *file_get_kernel_obj_list( struct object *obj )
  507. {
  508. struct file *file = (struct file *)obj;
  509. return &file->kernel_object;
  510. }
  511. static void file_destroy( struct object *obj )
  512. {
  513. struct file *file = (struct file *)obj;
  514. assert( obj->ops == &file_ops );
  515. if (file->fd) release_object( file->fd );
  516. }
  517. /* set the last error depending on errno */
  518. void file_set_error(void)
  519. {
  520. switch (errno)
  521. {
  522. case ETXTBSY:
  523. case EAGAIN: set_error( STATUS_SHARING_VIOLATION ); break;
  524. case EBADF: set_error( STATUS_INVALID_HANDLE ); break;
  525. case ENOSPC: set_error( STATUS_DISK_FULL ); break;
  526. case EACCES:
  527. case ESRCH:
  528. case EROFS:
  529. case EPERM: set_error( STATUS_ACCESS_DENIED ); break;
  530. case EBUSY: set_error( STATUS_FILE_LOCK_CONFLICT ); break;
  531. case ENOENT: set_error( STATUS_NO_SUCH_FILE ); break;
  532. case EISDIR: set_error( STATUS_FILE_IS_A_DIRECTORY ); break;
  533. case ENFILE:
  534. case EMFILE: set_error( STATUS_TOO_MANY_OPENED_FILES ); break;
  535. case EEXIST: set_error( STATUS_OBJECT_NAME_COLLISION ); break;
  536. case EINVAL: set_error( STATUS_INVALID_PARAMETER ); break;
  537. case ESPIPE: set_error( STATUS_ILLEGAL_FUNCTION ); break;
  538. case ENOTEMPTY: set_error( STATUS_DIRECTORY_NOT_EMPTY ); break;
  539. case EIO: set_error( STATUS_ACCESS_VIOLATION ); break;
  540. case ENOTDIR: set_error( STATUS_NOT_A_DIRECTORY ); break;
  541. case EFBIG: set_error( STATUS_SECTION_TOO_BIG ); break;
  542. case ENODEV: set_error( STATUS_NO_SUCH_DEVICE ); break;
  543. case ENXIO: set_error( STATUS_NO_SUCH_DEVICE ); break;
  544. case EXDEV: set_error( STATUS_NOT_SAME_DEVICE ); break;
  545. case ELOOP: set_error( STATUS_REPARSE_POINT_NOT_RESOLVED ); break;
  546. #ifdef EOVERFLOW
  547. case EOVERFLOW: set_error( STATUS_INVALID_PARAMETER ); break;
  548. #endif
  549. default:
  550. perror("wineserver: file_set_error() can't map error");
  551. set_error( STATUS_UNSUCCESSFUL );
  552. break;
  553. }
  554. }
  555. struct file *get_file_obj( struct process *process, obj_handle_t handle, unsigned int access )
  556. {
  557. return (struct file *)get_handle_obj( process, handle, access, &file_ops );
  558. }
  559. int get_file_unix_fd( struct file *file )
  560. {
  561. return get_unix_fd( file->fd );
  562. }
  563. /* create a file */
  564. DECL_HANDLER(create_file)
  565. {
  566. struct object *file;
  567. struct fd *root_fd = NULL;
  568. struct unicode_str nt_name;
  569. const struct security_descriptor *sd;
  570. const struct object_attributes *objattr = get_req_object_attributes( &sd, &nt_name, NULL );
  571. const char *name;
  572. data_size_t name_len;
  573. if (!objattr) return;
  574. if (objattr->rootdir)
  575. {
  576. struct dir *root;
  577. if (!(root = get_dir_obj( current->process, objattr->rootdir, 0 ))) return;
  578. root_fd = get_obj_fd( (struct object *)root );
  579. release_object( root );
  580. if (!root_fd) return;
  581. }
  582. name = get_req_data_after_objattr( objattr, &name_len );
  583. reply->handle = 0;
  584. if ((file = create_file( root_fd, name, name_len, nt_name, req->access, req->sharing,
  585. req->create, req->options, req->attrs, sd )))
  586. {
  587. reply->handle = alloc_handle( current->process, file, req->access, objattr->attributes );
  588. release_object( file );
  589. }
  590. if (root_fd) release_object( root_fd );
  591. }
  592. /* allocate a file handle for a Unix fd */
  593. DECL_HANDLER(alloc_file_handle)
  594. {
  595. struct file *file;
  596. int fd;
  597. reply->handle = 0;
  598. if ((fd = thread_get_inflight_fd( current, req->fd )) == -1)
  599. {
  600. set_error( STATUS_INVALID_HANDLE );
  601. return;
  602. }
  603. if ((file = create_file_for_fd( fd, req->access, FILE_SHARE_READ | FILE_SHARE_WRITE )))
  604. {
  605. reply->handle = alloc_handle( current->process, file, req->access, req->attributes );
  606. release_object( file );
  607. }
  608. }
  609. /* lock a region of a file */
  610. DECL_HANDLER(lock_file)
  611. {
  612. struct file *file;
  613. if ((file = get_file_obj( current->process, req->handle, 0 )))
  614. {
  615. reply->handle = lock_fd( file->fd, req->offset, req->count, req->shared, req->wait );
  616. reply->overlapped = is_fd_overlapped( file->fd );
  617. release_object( file );
  618. }
  619. }
  620. /* unlock a region of a file */
  621. DECL_HANDLER(unlock_file)
  622. {
  623. struct file *file;
  624. if ((file = get_file_obj( current->process, req->handle, 0 )))
  625. {
  626. unlock_fd( file->fd, req->offset, req->count );
  627. release_object( file );
  628. }
  629. }