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.

372 lines
9.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 <fuse.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <algorithm>
  18. #include <set>
  19. #include <sstream>
  20. #include <string>
  21. #include <vector>
  22. #include "config.hpp"
  23. #include "errno.hpp"
  24. #include "fs_base_getxattr.hpp"
  25. #include "fs_path.hpp"
  26. #include "rwlock.hpp"
  27. #include "str.hpp"
  28. #include "ugid.hpp"
  29. #include "version.hpp"
  30. static const char SECURITY_CAPABILITY[] = "security.capability";
  31. using std::string;
  32. using std::vector;
  33. using std::set;
  34. using namespace mergerfs;
  35. static
  36. bool
  37. _is_attrname_security_capability(const char *attrname_)
  38. {
  39. return (strcmp(attrname_,SECURITY_CAPABILITY) == 0);
  40. }
  41. static
  42. int
  43. _lgetxattr(const string &path,
  44. const char *attrname,
  45. void *value,
  46. const size_t size)
  47. {
  48. int rv;
  49. rv = fs::lgetxattr(path,attrname,value,size);
  50. return ((rv == -1) ? -errno : rv);
  51. }
  52. static
  53. void
  54. _getxattr_controlfile_fusefunc_policy(const Config &config,
  55. const string &attr,
  56. string &attrvalue)
  57. {
  58. FuseFunc fusefunc;
  59. fusefunc = FuseFunc::find(attr);
  60. if(fusefunc != FuseFunc::invalid)
  61. attrvalue = (std::string)*config.policies[(FuseFunc::Enum::Type)*fusefunc];
  62. }
  63. static
  64. void
  65. _getxattr_controlfile_category_policy(const Config &config,
  66. const string &attr,
  67. string &attrvalue)
  68. {
  69. Category cat;
  70. cat = Category::find(attr);
  71. if(cat != Category::invalid)
  72. {
  73. vector<string> policies;
  74. for(int i = FuseFunc::Enum::BEGIN; i < FuseFunc::Enum::END; i++)
  75. {
  76. if(cat == (Category::Enum::Type)*FuseFunc::fusefuncs[i])
  77. policies.push_back(*config.policies[i]);
  78. }
  79. std::sort(policies.begin(),policies.end());
  80. policies.erase(std::unique(policies.begin(),policies.end()),
  81. policies.end());
  82. attrvalue = str::join(policies,',');
  83. }
  84. }
  85. static
  86. void
  87. _getxattr_controlfile_srcmounts(const Config &config,
  88. string &attrvalue)
  89. {
  90. attrvalue = str::join(config.srcmounts,':');
  91. }
  92. static
  93. void
  94. _getxattr_controlfile_uint64_t(const uint64_t uint,
  95. string &attrvalue)
  96. {
  97. std::ostringstream os;
  98. os << uint;
  99. attrvalue = os.str();
  100. }
  101. static
  102. void
  103. _getxattr_controlfile_time_t(const time_t time,
  104. string &attrvalue)
  105. {
  106. std::ostringstream os;
  107. os << time;
  108. attrvalue = os.str();
  109. }
  110. static
  111. void
  112. _getxattr_controlfile_bool(const bool boolvalue,
  113. string &attrvalue)
  114. {
  115. attrvalue = (boolvalue ? "true" : "false");
  116. }
  117. static
  118. void
  119. _getxattr_controlfile_policies(const Config &config,
  120. string &attrvalue)
  121. {
  122. size_t i = Policy::Enum::begin();
  123. attrvalue = (string)Policy::policies[i];
  124. for(i++; i < Policy::Enum::end(); i++)
  125. attrvalue += ',' + (string)Policy::policies[i];
  126. }
  127. static
  128. void
  129. _getxattr_controlfile_version(string &attrvalue)
  130. {
  131. attrvalue = MERGERFS_VERSION;
  132. if(attrvalue.empty())
  133. attrvalue = "unknown_possible_problem_with_build";
  134. }
  135. static
  136. void
  137. _getxattr_pid(string &attrvalue)
  138. {
  139. int pid;
  140. char buf[32];
  141. pid = getpid();
  142. snprintf(buf,sizeof(buf),"%d",pid);
  143. attrvalue = buf;
  144. }
  145. static
  146. int
  147. _getxattr_controlfile(const Config &config,
  148. const char *attrname,
  149. char *buf,
  150. const size_t count)
  151. {
  152. size_t len;
  153. string attrvalue;
  154. vector<string> attr;
  155. str::split(attr,attrname,'.');
  156. if((attr[0] != "user") || (attr[1] != "mergerfs"))
  157. return -ENOATTR;
  158. switch(attr.size())
  159. {
  160. case 3:
  161. if(attr[2] == "srcmounts")
  162. _getxattr_controlfile_srcmounts(config,attrvalue);
  163. else if(attr[2] == "minfreespace")
  164. _getxattr_controlfile_uint64_t(config.minfreespace,attrvalue);
  165. else if(attr[2] == "moveonenospc")
  166. _getxattr_controlfile_bool(config.moveonenospc,attrvalue);
  167. else if(attr[2] == "dropcacheonclose")
  168. _getxattr_controlfile_bool(config.dropcacheonclose,attrvalue);
  169. else if(attr[2] == "symlinkify")
  170. _getxattr_controlfile_bool(config.symlinkify,attrvalue);
  171. else if(attr[2] == "symlinkify_timeout")
  172. _getxattr_controlfile_time_t(config.symlinkify_timeout,attrvalue);
  173. else if(attr[2] == "nullrw")
  174. _getxattr_controlfile_bool(config.nullrw,attrvalue);
  175. else if(attr[2] == "ignorepponrename")
  176. _getxattr_controlfile_bool(config.ignorepponrename,attrvalue);
  177. else if(attr[2] == "security_capability")
  178. _getxattr_controlfile_bool(config.security_capability,attrvalue);
  179. else if(attr[2] == "link_cow")
  180. _getxattr_controlfile_bool(config.link_cow,attrvalue);
  181. else if(attr[2] == "policies")
  182. _getxattr_controlfile_policies(config,attrvalue);
  183. else if(attr[2] == "version")
  184. _getxattr_controlfile_version(attrvalue);
  185. else if(attr[2] == "pid")
  186. _getxattr_pid(attrvalue);
  187. break;
  188. case 4:
  189. if(attr[2] == "category")
  190. _getxattr_controlfile_category_policy(config,attr[3],attrvalue);
  191. else if(attr[2] == "func")
  192. _getxattr_controlfile_fusefunc_policy(config,attr[3],attrvalue);
  193. break;
  194. }
  195. if(attrvalue.empty())
  196. return -ENOATTR;
  197. len = attrvalue.size();
  198. if(count == 0)
  199. return len;
  200. if(count < len)
  201. return -ERANGE;
  202. memcpy(buf,attrvalue.c_str(),len);
  203. return (int)len;
  204. }
  205. static
  206. int
  207. _getxattr_from_string(char *destbuf,
  208. const size_t destbufsize,
  209. const string &src)
  210. {
  211. const size_t srcbufsize = src.size();
  212. if(destbufsize == 0)
  213. return srcbufsize;
  214. if(srcbufsize > destbufsize)
  215. return -ERANGE;
  216. memcpy(destbuf,src.data(),srcbufsize);
  217. return srcbufsize;
  218. }
  219. static
  220. int
  221. _getxattr_user_mergerfs_allpaths(const vector<string> &srcmounts,
  222. const char *fusepath,
  223. char *buf,
  224. const size_t count)
  225. {
  226. string concated;
  227. vector<string> paths;
  228. fs::findallfiles(srcmounts,fusepath,paths);
  229. concated = str::join(paths,'\0');
  230. return _getxattr_from_string(buf,count,concated);
  231. }
  232. static
  233. int
  234. _getxattr_user_mergerfs(const string &basepath,
  235. const char *fusepath,
  236. const string &fullpath,
  237. const vector<string> &srcmounts,
  238. const char *attrname,
  239. char *buf,
  240. const size_t count)
  241. {
  242. vector<string> attr;
  243. str::split(attr,attrname,'.');
  244. if(attr[2] == "basepath")
  245. return _getxattr_from_string(buf,count,basepath);
  246. else if(attr[2] == "relpath")
  247. return _getxattr_from_string(buf,count,fusepath);
  248. else if(attr[2] == "fullpath")
  249. return _getxattr_from_string(buf,count,fullpath);
  250. else if(attr[2] == "allpaths")
  251. return _getxattr_user_mergerfs_allpaths(srcmounts,fusepath,buf,count);
  252. return -ENOATTR;
  253. }
  254. static
  255. int
  256. _getxattr(Policy::Func::Search searchFunc,
  257. const vector<string> &srcmounts,
  258. const size_t minfreespace,
  259. const char *fusepath,
  260. const char *attrname,
  261. char *buf,
  262. const size_t count)
  263. {
  264. int rv;
  265. string fullpath;
  266. vector<const string*> basepaths;
  267. rv = searchFunc(srcmounts,fusepath,minfreespace,basepaths);
  268. if(rv == -1)
  269. return -errno;
  270. fs::path::make(basepaths[0],fusepath,fullpath);
  271. if(str::isprefix(attrname,"user.mergerfs."))
  272. rv = _getxattr_user_mergerfs(*basepaths[0],fusepath,fullpath,srcmounts,attrname,buf,count);
  273. else
  274. rv = _lgetxattr(fullpath,attrname,buf,count);
  275. return rv;
  276. }
  277. namespace mergerfs
  278. {
  279. namespace fuse
  280. {
  281. int
  282. getxattr(const char *fusepath,
  283. const char *attrname,
  284. char *buf,
  285. size_t count)
  286. {
  287. const fuse_context *fc = fuse_get_context();
  288. const Config &config = Config::get(fc);
  289. if((config.security_capability == false) &&
  290. _is_attrname_security_capability(attrname))
  291. return -ENOATTR;
  292. if(fusepath == config.controlfile)
  293. return _getxattr_controlfile(config,
  294. attrname,
  295. buf,
  296. count);
  297. const ugid::Set ugid(fc->uid,fc->gid);
  298. const rwlock::ReadGuard readlock(&config.srcmountslock);
  299. return _getxattr(config.getxattr,
  300. config.srcmounts,
  301. config.minfreespace,
  302. fusepath,
  303. attrname,
  304. buf,
  305. count);
  306. }
  307. }
  308. }