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.

137 lines
3.3 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_lchown.hpp"
  17. #include "fs_path.hpp"
  18. #include "policy_rv.hpp"
  19. #include "ugid.hpp"
  20. #include "fuse.h"
  21. #include <string>
  22. #include <vector>
  23. using std::string;
  24. using std::vector;
  25. namespace l
  26. {
  27. static
  28. int
  29. get_error(const PolicyRV &prv_,
  30. const string &basepath_)
  31. {
  32. for(int i = 0, ei = prv_.success.size(); i < ei; i++)
  33. {
  34. if(prv_.success[i].basepath == basepath_)
  35. return prv_.success[i].rv;
  36. }
  37. for(int i = 0, ei = prv_.error.size(); i < ei; i++)
  38. {
  39. if(prv_.error[i].basepath == basepath_)
  40. return prv_.error[i].rv;
  41. }
  42. return 0;
  43. }
  44. static
  45. void
  46. chown_loop_core(const string &basepath_,
  47. const char *fusepath_,
  48. const uid_t uid_,
  49. const gid_t gid_,
  50. PolicyRV *prv_)
  51. {
  52. string fullpath;
  53. fullpath = fs::path::make(basepath_,fusepath_);
  54. errno = 0;
  55. fs::lchown(fullpath,uid_,gid_);
  56. prv_->insert(errno,basepath_);
  57. }
  58. static
  59. void
  60. chown_loop(const vector<string> &basepaths_,
  61. const char *fusepath_,
  62. const uid_t uid_,
  63. const gid_t gid_,
  64. PolicyRV *prv_)
  65. {
  66. for(size_t i = 0, ei = basepaths_.size(); i != ei; i++)
  67. {
  68. l::chown_loop_core(basepaths_[i],fusepath_,uid_,gid_,prv_);
  69. }
  70. }
  71. static
  72. int
  73. chown(const Policy::Action &actionFunc_,
  74. const Policy::Search &searchFunc_,
  75. const Branches &branches_,
  76. const char *fusepath_,
  77. const uid_t uid_,
  78. const gid_t gid_)
  79. {
  80. int rv;
  81. PolicyRV prv;
  82. vector<string> basepaths;
  83. rv = actionFunc_(branches_,fusepath_,&basepaths);
  84. if(rv == -1)
  85. return -errno;
  86. l::chown_loop(basepaths,fusepath_,uid_,gid_,&prv);
  87. if(prv.error.empty())
  88. return 0;
  89. if(prv.success.empty())
  90. return prv.error[0].rv;
  91. basepaths.clear();
  92. rv = searchFunc_(branches_,fusepath_,&basepaths);
  93. if(rv == -1)
  94. return -errno;
  95. return l::get_error(prv,basepaths[0]);
  96. }
  97. }
  98. namespace FUSE
  99. {
  100. int
  101. chown(const char *fusepath_,
  102. uid_t uid_,
  103. gid_t gid_)
  104. {
  105. Config::Read cfg;
  106. const fuse_context *fc = fuse_get_context();
  107. const ugid::Set ugid(fc->uid,fc->gid);
  108. return l::chown(cfg->func.chown.policy,
  109. cfg->func.getattr.policy,
  110. cfg->branches,
  111. fusepath_,
  112. uid_,
  113. gid_);
  114. }
  115. }