chmod.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Optional.h>
  27. #include <AK/StdLibExtras.h>
  28. #include <AK/kmalloc.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <sys/stat.h>
  32. #include <unistd.h>
  33. /* the new mode will be computed using the boolean function(for each bit):
  34. |current mode|removal mask|applying mask|result |
  35. | 0 | 0 | 0 | 0 |
  36. | 0 | 0 | 1 | 1 |
  37. | 0 | 1 | 0 | 0 |
  38. | 0 | 1 | 1 | 1 | ---> find the CNF --> find the minimal CNF
  39. | 1 | 0 | 0 | 1 |
  40. | 1 | 0 | 1 | 1 |
  41. | 1 | 1 | 0 | 0 |
  42. | 1 | 1 | 1 | 1 |
  43. */
  44. class Mask {
  45. private:
  46. mode_t removal_mask; // the bits that will be removed
  47. mode_t applying_mask; // the bits that will be set
  48. public:
  49. Mask()
  50. : removal_mask(0)
  51. , applying_mask(0)
  52. {
  53. }
  54. Mask& operator|=(const Mask& other)
  55. {
  56. removal_mask |= other.removal_mask;
  57. applying_mask |= other.applying_mask;
  58. return *this;
  59. }
  60. mode_t& get_removal_mask() { return removal_mask; }
  61. mode_t& get_applying_mask() { return applying_mask; }
  62. };
  63. Optional<Mask> string_to_mode(char access_scope, const char*& access_string);
  64. Optional<Mask> apply_permission(char access_scope, char permission, char operation);
  65. int main(int argc, char** argv)
  66. {
  67. if (pledge("stdio rpath fattr", nullptr) < 0) {
  68. perror("pledge");
  69. return 1;
  70. }
  71. if (argc < 3) {
  72. printf("usage: chmod <octal-mode> <path...>\n"
  73. " chmod [[ugoa][+-=][rwx...],...] <path...>\n");
  74. return 1;
  75. }
  76. Mask mask;
  77. /* compute a mask */
  78. if (argv[1][0] >= '0' && argv[1][0] <= '7') {
  79. if (sscanf(argv[1], "%ho", &mask.get_applying_mask()) != 1) {
  80. perror("sscanf");
  81. return 1;
  82. }
  83. mask.get_removal_mask() = ~mask.get_applying_mask();
  84. } else {
  85. const char* access_string = argv[1];
  86. while (*access_string != '\0') {
  87. Optional<Mask> tmp_mask;
  88. switch (*access_string) {
  89. case 'u':
  90. tmp_mask = string_to_mode('u', ++access_string);
  91. break;
  92. case 'g':
  93. tmp_mask = string_to_mode('g', ++access_string);
  94. break;
  95. case 'o':
  96. tmp_mask = string_to_mode('o', ++access_string);
  97. break;
  98. case 'a':
  99. tmp_mask = string_to_mode('a', ++access_string);
  100. break;
  101. case '=':
  102. case '+':
  103. case '-':
  104. tmp_mask = string_to_mode('a', access_string);
  105. break;
  106. case ',':
  107. ++access_string;
  108. continue;
  109. }
  110. if (!tmp_mask.has_value()) {
  111. fprintf(stderr, "chmod: invalid mode: %s\n", argv[1]);
  112. return 1;
  113. }
  114. mask |= tmp_mask.value();
  115. }
  116. }
  117. /* set the mask for each file's permissions */
  118. struct stat current_access;
  119. int i = 2;
  120. while (i < argc) {
  121. if (stat(argv[i], &current_access) != 0) {
  122. perror("stat");
  123. return 1;
  124. }
  125. /* found the minimal CNF by The Quine–McCluskey algorithm and use it */
  126. mode_t mode = mask.get_applying_mask()
  127. | (current_access.st_mode & ~mask.get_removal_mask());
  128. if (chmod(argv[i++], mode) != 0) {
  129. perror("chmod");
  130. }
  131. }
  132. return 0;
  133. }
  134. Optional<Mask> string_to_mode(char access_scope, const char*& access_string)
  135. {
  136. char operation = *access_string;
  137. if (operation != '+' && operation != '-' && operation != '=') {
  138. return {};
  139. }
  140. Mask mask;
  141. if (operation == '=') {
  142. switch (access_scope) {
  143. case 'u':
  144. mask.get_removal_mask() = (S_IRUSR | S_IWUSR | S_IXUSR);
  145. break;
  146. case 'g':
  147. mask.get_removal_mask() = (S_IRGRP | S_IWGRP | S_IXGRP);
  148. break;
  149. case 'o':
  150. mask.get_removal_mask() = (S_IROTH | S_IWOTH | S_IXOTH);
  151. break;
  152. case 'a':
  153. mask.get_removal_mask() = (S_IRUSR | S_IWUSR | S_IXUSR
  154. | S_IRGRP | S_IWGRP | S_IXGRP
  155. | S_IROTH | S_IWOTH | S_IXOTH);
  156. break;
  157. }
  158. operation = '+';
  159. }
  160. access_string++;
  161. while (*access_string != '\0' && *access_string != ',') {
  162. Optional<Mask> tmp_mask;
  163. tmp_mask = apply_permission(access_scope, *access_string, operation);
  164. if (!tmp_mask.has_value()) {
  165. return {};
  166. }
  167. mask |= tmp_mask.value();
  168. access_string++;
  169. }
  170. return mask;
  171. }
  172. Optional<Mask> apply_permission(char access_scope, char permission, char operation)
  173. {
  174. if (permission != 'r' && permission != 'w' && permission != 'x') {
  175. return {};
  176. }
  177. Mask mask;
  178. mode_t tmp_mask = 0;
  179. switch (access_scope) {
  180. case 'u':
  181. switch (permission) {
  182. case 'r':
  183. tmp_mask = S_IRUSR;
  184. break;
  185. case 'w':
  186. tmp_mask = S_IWUSR;
  187. break;
  188. case 'x':
  189. tmp_mask = S_IXUSR;
  190. break;
  191. }
  192. break;
  193. case 'g':
  194. switch (permission) {
  195. case 'r':
  196. tmp_mask = S_IRGRP;
  197. break;
  198. case 'w':
  199. tmp_mask = S_IWGRP;
  200. break;
  201. case 'x':
  202. tmp_mask = S_IXGRP;
  203. break;
  204. }
  205. break;
  206. case 'o':
  207. switch (permission) {
  208. case 'r':
  209. tmp_mask = S_IROTH;
  210. break;
  211. case 'w':
  212. tmp_mask = S_IWOTH;
  213. break;
  214. case 'x':
  215. tmp_mask = S_IXOTH;
  216. break;
  217. }
  218. break;
  219. case 'a':
  220. mask |= apply_permission('u', permission, operation).value();
  221. mask |= apply_permission('g', permission, operation).value();
  222. mask |= apply_permission('o', permission, operation).value();
  223. break;
  224. }
  225. if (operation == '+') {
  226. mask.get_applying_mask() |= tmp_mask;
  227. } else {
  228. mask.get_removal_mask() |= tmp_mask;
  229. }
  230. return mask;
  231. }