parse_mode.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * parse_mode implementation for busybox
  4. *
  5. * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. #include "libbb.h"
  10. /* http://www.opengroup.org/onlinepubs/007904975/utilities/chmod.html */
  11. /* This function is used from NOFORK applets. It must not allocate anything */
  12. #define FILEMODEBITS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
  13. int FAST_FUNC bb_parse_mode(const char *s, unsigned current_mode)
  14. {
  15. /* should be mode_t really, but in all Unixes these constants fit into uint16 */
  16. static const uint16_t who_mask[] ALIGN2 = {
  17. S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO, /* a */
  18. S_ISUID | S_IRWXU, /* u */
  19. S_ISGID | S_IRWXG, /* g */
  20. S_IRWXO /* o */
  21. };
  22. static const uint16_t perm_mask[] ALIGN2 = {
  23. S_IRUSR | S_IRGRP | S_IROTH, /* r */
  24. S_IWUSR | S_IWGRP | S_IWOTH, /* w */
  25. S_IXUSR | S_IXGRP | S_IXOTH, /* x */
  26. S_IXUSR | S_IXGRP | S_IXOTH, /* X -- special -- see below */
  27. S_ISUID | S_ISGID, /* s */
  28. S_ISVTX /* t */
  29. };
  30. static const char who_chars[] ALIGN1 = "augo";
  31. static const char perm_chars[] ALIGN1 = "rwxXst";
  32. const char *p;
  33. mode_t wholist;
  34. mode_t permlist;
  35. mode_t new_mode;
  36. char op;
  37. if ((unsigned char)(*s - '0') < 8) {
  38. unsigned long tmp;
  39. char *e;
  40. tmp = strtoul(s, &e, 8);
  41. if (*e || (tmp > 07777U)) { /* Check range and trailing chars. */
  42. return -1;
  43. }
  44. return tmp;
  45. }
  46. new_mode = current_mode;
  47. /* Note: we allow empty clauses, and hence empty modes.
  48. * We treat an empty mode as no change to perms. */
  49. while (*s) { /* Process clauses. */
  50. if (*s == ',') { /* We allow empty clauses. */
  51. ++s;
  52. continue;
  53. }
  54. /* Get a wholist. */
  55. wholist = 0;
  56. WHO_LIST:
  57. p = who_chars;
  58. do {
  59. if (*p == *s) {
  60. wholist |= who_mask[(int)(p-who_chars)];
  61. if (!*++s) {
  62. return -1;
  63. }
  64. goto WHO_LIST;
  65. }
  66. } while (*++p);
  67. do { /* Process action list. */
  68. if ((*s != '+') && (*s != '-')) {
  69. if (*s != '=') {
  70. return -1;
  71. }
  72. /* Since op is '=', clear all bits corresponding to the
  73. * wholist, or all file bits if wholist is empty. */
  74. permlist = ~FILEMODEBITS;
  75. if (wholist) {
  76. permlist = ~wholist;
  77. }
  78. new_mode &= permlist;
  79. }
  80. op = *s++;
  81. /* Check for permcopy. */
  82. p = who_chars + 1; /* Skip 'a' entry. */
  83. do {
  84. if (*p == *s) {
  85. int i = 0;
  86. permlist = who_mask[(int)(p-who_chars)]
  87. & (S_IRWXU | S_IRWXG | S_IRWXO)
  88. & new_mode;
  89. do {
  90. if (permlist & perm_mask[i]) {
  91. permlist |= perm_mask[i];
  92. }
  93. } while (++i < 3);
  94. ++s;
  95. goto GOT_ACTION;
  96. }
  97. } while (*++p);
  98. /* It was not a permcopy, so get a permlist. */
  99. permlist = 0;
  100. PERM_LIST:
  101. p = perm_chars;
  102. do {
  103. if (*p == *s) {
  104. if ((*p != 'X')
  105. || (new_mode & (S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH))
  106. ) {
  107. permlist |= perm_mask[(int)(p-perm_chars)];
  108. }
  109. if (!*++s) {
  110. break;
  111. }
  112. goto PERM_LIST;
  113. }
  114. } while (*++p);
  115. GOT_ACTION:
  116. if (permlist) { /* The permlist was nonempty. */
  117. mode_t tmp = wholist;
  118. if (!wholist) {
  119. mode_t u_mask = umask(0);
  120. umask(u_mask);
  121. tmp = ~u_mask;
  122. }
  123. permlist &= tmp;
  124. if (op == '-') {
  125. new_mode &= ~permlist;
  126. } else {
  127. new_mode |= permlist;
  128. }
  129. }
  130. } while (*s && (*s != ','));
  131. }
  132. return new_mode;
  133. }