You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

217 lines
5.5 KiB

  1. /*
  2. Copyright (c) 2018, Antonio SJ Musumeci <trapexit@spawn.link>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. */
  14. #include "config.hpp"
  15. #include "errno.hpp"
  16. #include "fs_glob.hpp"
  17. #include "fs_lsetxattr.hpp"
  18. #include "fs_path.hpp"
  19. #include "fs_statvfs_cache.hpp"
  20. #include "num.hpp"
  21. #include "policy_rv.hpp"
  22. #include "str.hpp"
  23. #include "ugid.hpp"
  24. #include "fuse.h"
  25. #include <string>
  26. #include <vector>
  27. #include <string.h>
  28. static const char SECURITY_CAPABILITY[] = "security.capability";
  29. using std::string;
  30. using std::vector;
  31. namespace l
  32. {
  33. static
  34. int
  35. get_error(const PolicyRV &prv_,
  36. const string &basepath_)
  37. {
  38. for(int i = 0, ei = prv_.success.size(); i < ei; i++)
  39. {
  40. if(prv_.success[i].basepath == basepath_)
  41. return prv_.success[i].rv;
  42. }
  43. for(int i = 0, ei = prv_.error.size(); i < ei; i++)
  44. {
  45. if(prv_.error[i].basepath == basepath_)
  46. return prv_.error[i].rv;
  47. }
  48. return 0;
  49. }
  50. static
  51. bool
  52. is_attrname_security_capability(const char *attrname_)
  53. {
  54. return (strcmp(attrname_,SECURITY_CAPABILITY) == 0);
  55. }
  56. static
  57. int
  58. setxattr_controlfile(const string &attrname_,
  59. const string &attrval_,
  60. const int flags_)
  61. {
  62. int rv;
  63. string key;
  64. Config::Write cfg;
  65. if(!str::startswith(attrname_,"user.mergerfs."))
  66. return -ENOATTR;
  67. key = &attrname_[14];
  68. if(cfg->has_key(key) == false)
  69. return -ENOATTR;
  70. if((flags_ & XATTR_CREATE) == XATTR_CREATE)
  71. return -EEXIST;
  72. rv = cfg->set(key,attrval_);
  73. if(rv < 0)
  74. return rv;
  75. fs::statvfs_cache_timeout(cfg->cache_statfs);
  76. return rv;
  77. }
  78. static
  79. void
  80. setxattr_loop_core(const string &basepath_,
  81. const char *fusepath_,
  82. const char *attrname_,
  83. const char *attrval_,
  84. const size_t attrvalsize_,
  85. const int flags_,
  86. PolicyRV *prv_)
  87. {
  88. string fullpath;
  89. fullpath = fs::path::make(basepath_,fusepath_);
  90. errno = 0;
  91. fs::lsetxattr(fullpath,attrname_,attrval_,attrvalsize_,flags_);
  92. prv_->insert(errno,basepath_);
  93. }
  94. static
  95. void
  96. setxattr_loop(const StrVec &basepaths_,
  97. const char *fusepath_,
  98. const char *attrname_,
  99. const char *attrval_,
  100. const size_t attrvalsize_,
  101. const int flags_,
  102. PolicyRV *prv_)
  103. {
  104. for(size_t i = 0, ei = basepaths_.size(); i != ei; i++)
  105. {
  106. l::setxattr_loop_core(basepaths_[i],fusepath_,
  107. attrname_,attrval_,attrvalsize_,
  108. flags_,prv_);
  109. }
  110. }
  111. static
  112. int
  113. setxattr(const Policy::Action &setxattrPolicy_,
  114. const Policy::Search &getxattrPolicy_,
  115. const Branches &branches_,
  116. const char *fusepath_,
  117. const char *attrname_,
  118. const char *attrval_,
  119. const size_t attrvalsize_,
  120. const int flags_)
  121. {
  122. int rv;
  123. PolicyRV prv;
  124. StrVec basepaths;
  125. rv = setxattrPolicy_(branches_,fusepath_,&basepaths);
  126. if(rv == -1)
  127. return -errno;
  128. l::setxattr_loop(basepaths,fusepath_,attrname_,attrval_,attrvalsize_,flags_,&prv);
  129. if(prv.error.empty())
  130. return 0;
  131. if(prv.success.empty())
  132. return prv.error[0].rv;
  133. basepaths.clear();
  134. rv = getxattrPolicy_(branches_,fusepath_,&basepaths);
  135. if(rv == -1)
  136. return -errno;
  137. return l::get_error(prv,basepaths[0]);
  138. }
  139. int
  140. setxattr(const char *fusepath_,
  141. const char *attrname_,
  142. const char *attrval_,
  143. size_t attrvalsize_,
  144. int flags_)
  145. {
  146. Config::Read cfg;
  147. if((cfg->security_capability == false) &&
  148. l::is_attrname_security_capability(attrname_))
  149. return -ENOATTR;
  150. if(cfg->xattr.to_int())
  151. return -cfg->xattr.to_int();
  152. const fuse_context *fc = fuse_get_context();
  153. const ugid::Set ugid(fc->uid,fc->gid);
  154. return l::setxattr(cfg->func.setxattr.policy,
  155. cfg->func.getxattr.policy,
  156. cfg->branches,
  157. fusepath_,
  158. attrname_,
  159. attrval_,
  160. attrvalsize_,
  161. flags_);
  162. }
  163. }
  164. namespace FUSE
  165. {
  166. int
  167. setxattr(const char *fusepath_,
  168. const char *attrname_,
  169. const char *attrval_,
  170. size_t attrvalsize_,
  171. int flags_)
  172. {
  173. if(fusepath_ == CONTROLFILE)
  174. return l::setxattr_controlfile(attrname_,
  175. string(attrval_,attrvalsize_),
  176. flags_);
  177. return l::setxattr(fusepath_,attrname_,attrval_,attrvalsize_,flags_);
  178. }
  179. }