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.

181 lines
4.4 KiB

  1. /*
  2. Copyright (c) 2016, 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_acl.hpp"
  17. #include "fs_mknod.hpp"
  18. #include "fs_clonepath.hpp"
  19. #include "fs_path.hpp"
  20. #include "ugid.hpp"
  21. #include "fuse.h"
  22. #include <string>
  23. #include <vector>
  24. using std::string;
  25. using std::vector;
  26. namespace error
  27. {
  28. static
  29. inline
  30. int
  31. calc(const int rv_,
  32. const int prev_,
  33. const int cur_)
  34. {
  35. if(rv_ == -1)
  36. {
  37. if(prev_ == 0)
  38. return 0;
  39. return cur_;
  40. }
  41. return 0;
  42. }
  43. }
  44. namespace l
  45. {
  46. static
  47. inline
  48. int
  49. mknod_core(const string &fullpath_,
  50. mode_t mode_,
  51. const mode_t umask_,
  52. const dev_t dev_)
  53. {
  54. if(!fs::acl::dir_has_defaults(fullpath_))
  55. mode_ &= ~umask_;
  56. return fs::mknod(fullpath_,mode_,dev_);
  57. }
  58. static
  59. int
  60. mknod_loop_core(const string &createpath_,
  61. const char *fusepath_,
  62. const mode_t mode_,
  63. const mode_t umask_,
  64. const dev_t dev_,
  65. const int error_)
  66. {
  67. int rv;
  68. string fullpath;
  69. fullpath = fs::path::make(createpath_,fusepath_);
  70. rv = l::mknod_core(fullpath,mode_,umask_,dev_);
  71. return error::calc(rv,error_,errno);
  72. }
  73. static
  74. int
  75. mknod_loop(const string &existingpath_,
  76. const vector<string> &createpaths_,
  77. const char *fusepath_,
  78. const string &fusedirpath_,
  79. const mode_t mode_,
  80. const mode_t umask_,
  81. const dev_t dev_)
  82. {
  83. int rv;
  84. int error;
  85. error = -1;
  86. for(size_t i = 0, ei = createpaths_.size(); i != ei; i++)
  87. {
  88. rv = fs::clonepath_as_root(existingpath_,createpaths_[i],fusedirpath_);
  89. if(rv == -1)
  90. error = error::calc(rv,error,errno);
  91. else
  92. error = l::mknod_loop_core(createpaths_[i],
  93. fusepath_,
  94. mode_,umask_,dev_,error);
  95. }
  96. return -error;
  97. }
  98. static
  99. int
  100. mknod(const Policy::Search &searchFunc_,
  101. const Policy::Create &createFunc_,
  102. const Branches &branches_,
  103. const char *fusepath_,
  104. const mode_t mode_,
  105. const mode_t umask_,
  106. const dev_t dev_)
  107. {
  108. int rv;
  109. string fusedirpath;
  110. vector<string> createpaths;
  111. vector<string> existingpaths;
  112. fusedirpath = fs::path::dirname(fusepath_);
  113. rv = searchFunc_(branches_,fusedirpath,&existingpaths);
  114. if(rv == -1)
  115. return -errno;
  116. rv = createFunc_(branches_,fusedirpath,&createpaths);
  117. if(rv == -1)
  118. return -errno;
  119. return l::mknod_loop(existingpaths[0],createpaths,
  120. fusepath_,fusedirpath,
  121. mode_,umask_,dev_);
  122. }
  123. }
  124. namespace FUSE
  125. {
  126. int
  127. mknod(const char *fusepath_,
  128. mode_t mode_,
  129. dev_t rdev_)
  130. {
  131. int rv;
  132. Config::Read cfg;
  133. const fuse_context *fc = fuse_get_context();
  134. const ugid::Set ugid(fc->uid,fc->gid);
  135. rv = l::mknod(cfg->func.getattr.policy,
  136. cfg->func.mknod.policy,
  137. cfg->branches,
  138. fusepath_,
  139. mode_,
  140. fc->umask,
  141. rdev_);
  142. if(rv == -EROFS)
  143. {
  144. Config::Write()->branches.find_and_set_mode_ro();
  145. rv = l::mknod(cfg->func.getattr.policy,
  146. cfg->func.mknod.policy,
  147. cfg->branches,
  148. fusepath_,
  149. mode_,
  150. fc->umask,
  151. rdev_);
  152. }
  153. return rv;
  154. }
  155. }