chmod.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Implement the core of the --chmod option.
  3. *
  4. * Copyright (C) 2002 Scott Howard
  5. * Copyright (C) 2005-2009 Wayne Davison
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, visit the http://fsf.org website.
  19. */
  20. #include "rsync.h"
  21. extern mode_t orig_umask;
  22. #define FLAG_X_KEEP (1<<0)
  23. #define FLAG_DIRS_ONLY (1<<1)
  24. #define FLAG_FILES_ONLY (1<<2)
  25. struct chmod_mode_struct {
  26. struct chmod_mode_struct *next;
  27. int ModeAND, ModeOR;
  28. char flags;
  29. };
  30. #define CHMOD_ADD 1
  31. #define CHMOD_SUB 2
  32. #define CHMOD_EQ 3
  33. #define STATE_ERROR 0
  34. #define STATE_1ST_HALF 1
  35. #define STATE_2ND_HALF 2
  36. /* Parse a chmod-style argument, and break it down into one or more AND/OR
  37. * pairs in a linked list. We return a pointer to new items on succcess
  38. * (appending the items to the specified list), or NULL on error. */
  39. struct chmod_mode_struct *parse_chmod(const char *modestr,
  40. struct chmod_mode_struct **root_mode_ptr)
  41. {
  42. int state = STATE_1ST_HALF;
  43. int where = 0, what = 0, op = 0, topbits = 0, topoct = 0, flags = 0;
  44. struct chmod_mode_struct *first_mode = NULL, *curr_mode = NULL,
  45. *prev_mode = NULL;
  46. while (state != STATE_ERROR) {
  47. if (!*modestr || *modestr == ',') {
  48. int bits;
  49. if (!op) {
  50. state = STATE_ERROR;
  51. break;
  52. }
  53. prev_mode = curr_mode;
  54. curr_mode = new_array(struct chmod_mode_struct, 1);
  55. if (prev_mode)
  56. prev_mode->next = curr_mode;
  57. else
  58. first_mode = curr_mode;
  59. curr_mode->next = NULL;
  60. if (where)
  61. bits = where * what;
  62. else {
  63. where = 0111;
  64. bits = (where * what) & ~orig_umask;
  65. }
  66. switch (op) {
  67. case CHMOD_ADD:
  68. curr_mode->ModeAND = CHMOD_BITS;
  69. curr_mode->ModeOR = bits + topoct;
  70. break;
  71. case CHMOD_SUB:
  72. curr_mode->ModeAND = CHMOD_BITS - bits - topoct;
  73. curr_mode->ModeOR = 0;
  74. break;
  75. case CHMOD_EQ:
  76. curr_mode->ModeAND = CHMOD_BITS - (where * 7) - (topoct ? topbits : 0);
  77. curr_mode->ModeOR = bits + topoct;
  78. break;
  79. }
  80. curr_mode->flags = flags;
  81. if (!*modestr)
  82. break;
  83. modestr++;
  84. state = STATE_1ST_HALF;
  85. where = what = op = topoct = topbits = flags = 0;
  86. }
  87. if (state != STATE_2ND_HALF) {
  88. switch (*modestr) {
  89. case 'D':
  90. if (flags & FLAG_FILES_ONLY)
  91. state = STATE_ERROR;
  92. flags |= FLAG_DIRS_ONLY;
  93. break;
  94. case 'F':
  95. if (flags & FLAG_DIRS_ONLY)
  96. state = STATE_ERROR;
  97. flags |= FLAG_FILES_ONLY;
  98. break;
  99. case 'u':
  100. where |= 0100;
  101. topbits |= 04000;
  102. break;
  103. case 'g':
  104. where |= 0010;
  105. topbits |= 02000;
  106. break;
  107. case 'o':
  108. where |= 0001;
  109. break;
  110. case 'a':
  111. where |= 0111;
  112. break;
  113. case '+':
  114. op = CHMOD_ADD;
  115. state = STATE_2ND_HALF;
  116. break;
  117. case '-':
  118. op = CHMOD_SUB;
  119. state = STATE_2ND_HALF;
  120. break;
  121. case '=':
  122. op = CHMOD_EQ;
  123. state = STATE_2ND_HALF;
  124. break;
  125. default:
  126. state = STATE_ERROR;
  127. break;
  128. }
  129. } else {
  130. switch (*modestr) {
  131. case 'r':
  132. what |= 4;
  133. break;
  134. case 'w':
  135. what |= 2;
  136. break;
  137. case 'X':
  138. flags |= FLAG_X_KEEP;
  139. /* FALL THROUGH */
  140. case 'x':
  141. what |= 1;
  142. break;
  143. case 's':
  144. if (topbits)
  145. topoct |= topbits;
  146. else
  147. topoct = 04000;
  148. break;
  149. case 't':
  150. topoct |= 01000;
  151. break;
  152. default:
  153. state = STATE_ERROR;
  154. break;
  155. }
  156. }
  157. modestr++;
  158. }
  159. if (state == STATE_ERROR) {
  160. free_chmod_mode(first_mode);
  161. return NULL;
  162. }
  163. if (!(curr_mode = *root_mode_ptr))
  164. *root_mode_ptr = first_mode;
  165. else {
  166. while (curr_mode->next)
  167. curr_mode = curr_mode->next;
  168. curr_mode->next = first_mode;
  169. }
  170. return first_mode;
  171. }
  172. /* Takes an existing file permission and a list of AND/OR changes, and
  173. * create a new permissions. */
  174. int tweak_mode(int mode, struct chmod_mode_struct *chmod_modes)
  175. {
  176. int IsX = mode & 0111;
  177. int NonPerm = mode & ~CHMOD_BITS;
  178. for ( ; chmod_modes; chmod_modes = chmod_modes->next) {
  179. if ((chmod_modes->flags & FLAG_DIRS_ONLY) && !S_ISDIR(NonPerm))
  180. continue;
  181. if ((chmod_modes->flags & FLAG_FILES_ONLY) && S_ISDIR(NonPerm))
  182. continue;
  183. mode &= chmod_modes->ModeAND;
  184. if ((chmod_modes->flags & FLAG_X_KEEP) && !IsX && !S_ISDIR(NonPerm))
  185. mode |= chmod_modes->ModeOR & ~0111;
  186. else
  187. mode |= chmod_modes->ModeOR;
  188. }
  189. return mode | NonPerm;
  190. }
  191. /* Free the linked list created by parse_chmod. */
  192. int free_chmod_mode(struct chmod_mode_struct *chmod_modes)
  193. {
  194. struct chmod_mode_struct *next;
  195. while (chmod_modes) {
  196. next = chmod_modes->next;
  197. free(chmod_modes);
  198. chmod_modes = next;
  199. }
  200. return 0;
  201. }