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.

228 lines
5.8 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_findallfiles.hpp"
  17. #include "fs_lgetxattr.hpp"
  18. #include "fs_path.hpp"
  19. #include "fs_statvfs_cache.hpp"
  20. #include "str.hpp"
  21. #include "ugid.hpp"
  22. #include "version.hpp"
  23. #include "fuse.h"
  24. #include <algorithm>
  25. #include <sstream>
  26. #include <string>
  27. #include <stdio.h>
  28. #include <string.h>
  29. static const char SECURITY_CAPABILITY[] = "security.capability";
  30. using std::string;
  31. namespace l
  32. {
  33. static
  34. bool
  35. is_attrname_security_capability(const char *attrname_)
  36. {
  37. return (strcmp(attrname_,SECURITY_CAPABILITY) == 0);
  38. }
  39. static
  40. int
  41. lgetxattr(const string &path_,
  42. const char *attrname_,
  43. void *value_,
  44. const size_t size_)
  45. {
  46. int rv;
  47. rv = fs::lgetxattr(path_,attrname_,value_,size_);
  48. return ((rv == -1) ? -errno : rv);
  49. }
  50. static
  51. int
  52. getxattr_controlfile(Config::Read &cfg_,
  53. const char *attrname_,
  54. char *buf_,
  55. const size_t count_)
  56. {
  57. int rv;
  58. size_t len;
  59. string key;
  60. string val;
  61. StrVec attr;
  62. if(!str::startswith(attrname_,"user.mergerfs."))
  63. return -ENOATTR;
  64. key = &attrname_[14];
  65. rv = cfg_->get(key,&val);
  66. if(rv < 0)
  67. return rv;
  68. len = val.size();
  69. if(count_ == 0)
  70. return len;
  71. if(count_ < len)
  72. return -ERANGE;
  73. memcpy(buf_,val.c_str(),len);
  74. return (int)len;
  75. }
  76. static
  77. int
  78. getxattr_from_string(char *destbuf_,
  79. const size_t destbufsize_,
  80. const string &src_)
  81. {
  82. const size_t srcbufsize = src_.size();
  83. if(destbufsize_ == 0)
  84. return srcbufsize;
  85. if(srcbufsize > destbufsize_)
  86. return -ERANGE;
  87. memcpy(destbuf_,src_.data(),srcbufsize);
  88. return srcbufsize;
  89. }
  90. static
  91. int
  92. getxattr_user_mergerfs_allpaths(const Branches::CPtr &branches_,
  93. const char *fusepath_,
  94. char *buf_,
  95. const size_t count_)
  96. {
  97. string concated;
  98. StrVec paths;
  99. StrVec branches;
  100. branches_->to_paths(branches);
  101. fs::findallfiles(branches,fusepath_,&paths);
  102. concated = str::join(paths,'\0');
  103. return l::getxattr_from_string(buf_,count_,concated);
  104. }
  105. static
  106. int
  107. getxattr_user_mergerfs(const string &basepath_,
  108. const char *fusepath_,
  109. const string &fullpath_,
  110. const Branches &branches_,
  111. const char *attrname_,
  112. char *buf_,
  113. const size_t count_)
  114. {
  115. StrVec attr;
  116. str::split(attrname_,'.',&attr);
  117. if(attr[2] == "basepath")
  118. return l::getxattr_from_string(buf_,count_,basepath_);
  119. else if(attr[2] == "relpath")
  120. return l::getxattr_from_string(buf_,count_,fusepath_);
  121. else if(attr[2] == "fullpath")
  122. return l::getxattr_from_string(buf_,count_,fullpath_);
  123. else if(attr[2] == "allpaths")
  124. return l::getxattr_user_mergerfs_allpaths(branches_,fusepath_,buf_,count_);
  125. return -ENOATTR;
  126. }
  127. static
  128. int
  129. getxattr(const Policy::Search &searchFunc_,
  130. const Branches &branches_,
  131. const char *fusepath_,
  132. const char *attrname_,
  133. char *buf_,
  134. const size_t count_)
  135. {
  136. int rv;
  137. string fullpath;
  138. StrVec basepaths;
  139. rv = searchFunc_(branches_,fusepath_,&basepaths);
  140. if(rv == -1)
  141. return -errno;
  142. fullpath = fs::path::make(basepaths[0],fusepath_);
  143. if(str::startswith(attrname_,"user.mergerfs."))
  144. return l::getxattr_user_mergerfs(basepaths[0],
  145. fusepath_,
  146. fullpath,
  147. branches_,
  148. attrname_,
  149. buf_,
  150. count_);
  151. return l::lgetxattr(fullpath,attrname_,buf_,count_);
  152. }
  153. }
  154. namespace FUSE
  155. {
  156. int
  157. getxattr(const char *fusepath_,
  158. const char *attrname_,
  159. char *buf_,
  160. size_t count_)
  161. {
  162. Config::Read cfg;
  163. if(fusepath_ == CONTROLFILE)
  164. return l::getxattr_controlfile(cfg,
  165. attrname_,
  166. buf_,
  167. count_);
  168. if((cfg->security_capability == false) &&
  169. l::is_attrname_security_capability(attrname_))
  170. return -ENOATTR;
  171. if(cfg->xattr.to_int())
  172. return -cfg->xattr.to_int();
  173. const fuse_context *fc = fuse_get_context();
  174. const ugid::Set ugid(fc->uid,fc->gid);
  175. return l::getxattr(cfg->func.getxattr.policy,
  176. cfg->branches,
  177. fusepath_,
  178. attrname_,
  179. buf_,
  180. count_);
  181. }
  182. }