acl.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (C) 2007 Red Hat. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/fs.h>
  19. #include <linux/string.h>
  20. #include <linux/xattr.h>
  21. #include <linux/posix_acl_xattr.h>
  22. #include "ctree.h"
  23. #include "xattr.h"
  24. #ifndef is_owner_or_cap
  25. #define is_owner_or_cap(inode) \
  26. ((current->fsuid == (inode)->i_uid) || capable(CAP_FOWNER))
  27. #endif
  28. static int btrfs_xattr_set_acl(struct inode *inode, int type,
  29. const void *value, size_t size)
  30. {
  31. int ret = 0;
  32. struct posix_acl *acl;
  33. if (!is_owner_or_cap(inode))
  34. return -EPERM;
  35. if (value) {
  36. acl = posix_acl_from_xattr(value, size);
  37. if (acl == NULL) {
  38. value = NULL;
  39. size = 0;
  40. } else if (IS_ERR(acl)) {
  41. ret = PTR_ERR(acl);
  42. } else {
  43. ret = posix_acl_valid(acl);
  44. posix_acl_release(acl);
  45. }
  46. if (ret)
  47. return ret;
  48. }
  49. return btrfs_xattr_set(inode, type, "", value, size, 0);
  50. }
  51. static int btrfs_xattr_get_acl(struct inode *inode, int type,
  52. void *value, size_t size)
  53. {
  54. return btrfs_xattr_get(inode, type, "", value, size);
  55. }
  56. static int btrfs_xattr_acl_access_get(struct inode *inode, const char *name,
  57. void *value, size_t size)
  58. {
  59. if (*name != '\0')
  60. return -EINVAL;
  61. return btrfs_xattr_get_acl(inode, BTRFS_XATTR_INDEX_POSIX_ACL_ACCESS,
  62. value, size);
  63. }
  64. static int btrfs_xattr_acl_access_set(struct inode *inode, const char *name,
  65. const void *value, size_t size, int flags)
  66. {
  67. if (*name != '\0')
  68. return -EINVAL;
  69. return btrfs_xattr_set_acl(inode, BTRFS_XATTR_INDEX_POSIX_ACL_ACCESS,
  70. value, size);
  71. }
  72. static int btrfs_xattr_acl_default_get(struct inode *inode, const char *name,
  73. void *value, size_t size)
  74. {
  75. if (*name != '\0')
  76. return -EINVAL;
  77. return btrfs_xattr_get_acl(inode, BTRFS_XATTR_INDEX_POSIX_ACL_DEFAULT,
  78. value, size);
  79. }
  80. static int btrfs_xattr_acl_default_set(struct inode *inode, const char *name,
  81. const void *value, size_t size, int flags)
  82. {
  83. if (*name != '\0')
  84. return -EINVAL;
  85. return btrfs_xattr_set_acl(inode, BTRFS_XATTR_INDEX_POSIX_ACL_DEFAULT,
  86. value, size);
  87. }
  88. struct xattr_handler btrfs_xattr_acl_default_handler = {
  89. .prefix = POSIX_ACL_XATTR_DEFAULT,
  90. .list = btrfs_xattr_generic_list,
  91. .get = btrfs_xattr_acl_default_get,
  92. .set = btrfs_xattr_acl_default_set,
  93. };
  94. struct xattr_handler btrfs_xattr_acl_access_handler = {
  95. .prefix = POSIX_ACL_XATTR_ACCESS,
  96. .list = btrfs_xattr_generic_list,
  97. .get = btrfs_xattr_acl_access_get,
  98. .set = btrfs_xattr_acl_access_set,
  99. };