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.

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