4
0

modechange.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /* modechange.c -- file mode manipulation
  2. Copyright (C) 1989, 1990, 1997, 1998, 1999 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. /* Written by David MacKenzie <[email protected]> */
  15. /* The ASCII mode string is compiled into a linked list of `struct
  16. modechange', which can then be applied to each file to be changed.
  17. We do this instead of re-parsing the ASCII string for each file
  18. because the compiled form requires less computation to use; when
  19. changing the mode of many files, this probably results in a
  20. performance gain. */
  21. #if HAVE_CONFIG_H
  22. # include <config.h>
  23. #endif
  24. #include "modechange.h"
  25. #include <sys/stat.h>
  26. #include "xstrtol.h"
  27. #if STDC_HEADERS
  28. # include <stdlib.h>
  29. #else
  30. char *malloc ();
  31. #endif
  32. #ifndef NULL
  33. # define NULL 0
  34. #endif
  35. #if STAT_MACROS_BROKEN
  36. # undef S_ISDIR
  37. #endif
  38. #if !defined(S_ISDIR) && defined(S_IFDIR)
  39. # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  40. #endif
  41. #ifndef S_ISUID
  42. # define S_ISUID 04000
  43. #endif
  44. #ifndef S_ISGID
  45. # define S_ISGID 04000
  46. #endif
  47. #ifndef S_ISVTX
  48. # define S_ISVTX 01000
  49. #endif
  50. #ifndef S_IRUSR
  51. # define S_IRUSR 0400
  52. #endif
  53. #ifndef S_IWUSR
  54. # define S_IWUSR 0200
  55. #endif
  56. #ifndef S_IXUSR
  57. # define S_IXUSR 0100
  58. #endif
  59. #ifndef S_IRGRP
  60. # define S_IRGRP 0040
  61. #endif
  62. #ifndef S_IWGRP
  63. # define S_IWGRP 0020
  64. #endif
  65. #ifndef S_IXGRP
  66. # define S_IXGRP 0010
  67. #endif
  68. #ifndef S_IROTH
  69. # define S_IROTH 0004
  70. #endif
  71. #ifndef S_IWOTH
  72. # define S_IWOTH 0002
  73. #endif
  74. #ifndef S_IXOTH
  75. # define S_IXOTH 0001
  76. #endif
  77. #ifndef S_IRWXU
  78. # define S_IRWXU 0700
  79. #endif
  80. #ifndef S_IRWXG
  81. # define S_IRWXG 0070
  82. #endif
  83. #ifndef S_IRWXO
  84. # define S_IRWXO 0007
  85. #endif
  86. /* All the mode bits that can be affected by chmod. */
  87. #define CHMOD_MODE_BITS \
  88. (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
  89. /* Return newly allocated memory to hold one element of type TYPE. */
  90. #define talloc(type) ((type *) malloc (sizeof (type)))
  91. /* Create a mode_change entry with the specified `=ddd'-style
  92. mode change operation, where NEW_MODE is `ddd'. Return the
  93. new entry, or NULL upon failure. */
  94. static struct mode_change *
  95. make_node_op_equals (mode_t new_mode)
  96. {
  97. struct mode_change *p;
  98. p = talloc (struct mode_change);
  99. if (p == NULL)
  100. return p;
  101. p->next = NULL;
  102. p->op = '=';
  103. p->flags = 0;
  104. p->value = new_mode;
  105. p->affected = CHMOD_MODE_BITS; /* Affect all permissions. */
  106. return p;
  107. }
  108. /* Append entry E to the end of the link list with the specified
  109. HEAD and TAIL. */
  110. static void
  111. mode_append_entry (struct mode_change **head,
  112. struct mode_change **tail,
  113. struct mode_change *e)
  114. {
  115. if (*head == NULL)
  116. *head = *tail = e;
  117. else
  118. {
  119. (*tail)->next = e;
  120. *tail = e;
  121. }
  122. }
  123. /* Return a linked list of file mode change operations created from
  124. MODE_STRING, an ASCII string that contains either an octal number
  125. specifying an absolute mode, or symbolic mode change operations with
  126. the form:
  127. [ugoa...][[+-=][rwxXstugo...]...][,...]
  128. MASKED_OPS is a bitmask indicating which symbolic mode operators (=+-)
  129. should not affect bits set in the umask when no users are given.
  130. Operators not selected in MASKED_OPS ignore the umask.
  131. Return MODE_INVALID if `mode_string' does not contain a valid
  132. representation of file mode change operations;
  133. return MODE_MEMORY_EXHAUSTED if there is insufficient memory. */
  134. struct mode_change *
  135. mode_compile (const char *mode_string, unsigned int masked_ops)
  136. {
  137. struct mode_change *head; /* First element of the linked list. */
  138. struct mode_change *tail; /* An element of the linked list. */
  139. uintmax_t mode_value; /* The mode value, if octal. */
  140. char *string_end; /* Pointer to end of parsed value. */
  141. mode_t umask_value; /* The umask value (surprise). */
  142. head = NULL;
  143. #ifdef lint
  144. tail = NULL;
  145. #endif
  146. if (xstrtoumax (mode_string, &string_end, 8, &mode_value, "") == LONGINT_OK)
  147. {
  148. struct mode_change *p;
  149. if (mode_value != (mode_value & CHMOD_MODE_BITS))
  150. return MODE_INVALID;
  151. p = make_node_op_equals ((mode_t) mode_value);
  152. if (p == NULL)
  153. return MODE_MEMORY_EXHAUSTED;
  154. mode_append_entry (&head, &tail, p);
  155. return head;
  156. }
  157. umask_value = umask (0);
  158. umask (umask_value); /* Restore the old value. */
  159. --mode_string;
  160. /* One loop iteration for each "ugoa...=+-rwxXstugo...[=+-rwxXstugo...]". */
  161. do
  162. {
  163. /* Which bits in the mode are operated on. */
  164. mode_t affected_bits = 0;
  165. /* `affected_bits' modified by umask. */
  166. mode_t affected_masked;
  167. /* Operators to actually use umask on. */
  168. unsigned ops_to_mask = 0;
  169. int who_specified_p;
  170. affected_bits = 0;
  171. ops_to_mask = 0;
  172. /* Turn on all the bits in `affected_bits' for each group given. */
  173. for (++mode_string;; ++mode_string)
  174. switch (*mode_string)
  175. {
  176. case 'u':
  177. affected_bits |= S_ISUID | S_IRWXU;
  178. break;
  179. case 'g':
  180. affected_bits |= S_ISGID | S_IRWXG;
  181. break;
  182. case 'o':
  183. affected_bits |= S_ISVTX | S_IRWXO;
  184. break;
  185. case 'a':
  186. affected_bits |= CHMOD_MODE_BITS;
  187. break;
  188. default:
  189. goto no_more_affected;
  190. }
  191. no_more_affected:
  192. /* If none specified, affect all bits, except perhaps those
  193. set in the umask. */
  194. if (affected_bits)
  195. who_specified_p = 1;
  196. else
  197. {
  198. who_specified_p = 0;
  199. affected_bits = CHMOD_MODE_BITS;
  200. ops_to_mask = masked_ops;
  201. }
  202. while (*mode_string == '=' || *mode_string == '+' || *mode_string == '-')
  203. {
  204. struct mode_change *change = talloc (struct mode_change);
  205. if (change == NULL)
  206. {
  207. mode_free (head);
  208. return MODE_MEMORY_EXHAUSTED;
  209. }
  210. change->next = NULL;
  211. change->op = *mode_string; /* One of "=+-". */
  212. affected_masked = affected_bits;
  213. /* Per the Single Unix Spec, if `who' is not specified and the
  214. `=' operator is used, then clear all the bits first. */
  215. if (!who_specified_p &&
  216. ops_to_mask & (*mode_string == '=' ? MODE_MASK_EQUALS : 0))
  217. {
  218. struct mode_change *p = make_node_op_equals (0);
  219. if (p == NULL)
  220. return MODE_MEMORY_EXHAUSTED;
  221. mode_append_entry (&head, &tail, p);
  222. }
  223. if (ops_to_mask & (*mode_string == '=' ? MODE_MASK_EQUALS
  224. : *mode_string == '+' ? MODE_MASK_PLUS
  225. : MODE_MASK_MINUS))
  226. affected_masked &= ~umask_value;
  227. change->affected = affected_masked;
  228. change->value = 0;
  229. change->flags = 0;
  230. /* Add the element to the tail of the list, so the operations
  231. are performed in the correct order. */
  232. mode_append_entry (&head, &tail, change);
  233. /* Set `value' according to the bits set in `affected_masked'. */
  234. for (++mode_string;; ++mode_string)
  235. switch (*mode_string)
  236. {
  237. case 'r':
  238. change->value |= ((S_IRUSR | S_IRGRP | S_IROTH)
  239. & affected_masked);
  240. break;
  241. case 'w':
  242. change->value |= ((S_IWUSR | S_IWGRP | S_IWOTH)
  243. & affected_masked);
  244. break;
  245. case 'X':
  246. change->flags |= MODE_X_IF_ANY_X;
  247. /* Fall through. */
  248. case 'x':
  249. change->value |= ((S_IXUSR | S_IXGRP | S_IXOTH)
  250. & affected_masked);
  251. break;
  252. case 's':
  253. /* Set the setuid/gid bits if `u' or `g' is selected. */
  254. change->value |= (S_ISUID | S_ISGID) & affected_masked;
  255. break;
  256. case 't':
  257. /* Set the "save text image" bit if `o' is selected. */
  258. change->value |= S_ISVTX & affected_masked;
  259. break;
  260. case 'u':
  261. /* Set the affected bits to the value of the `u' bits
  262. on the same file. */
  263. if (change->value)
  264. goto invalid;
  265. change->value = S_IRWXU;
  266. change->flags |= MODE_COPY_EXISTING;
  267. break;
  268. case 'g':
  269. /* Set the affected bits to the value of the `g' bits
  270. on the same file. */
  271. if (change->value)
  272. goto invalid;
  273. change->value = S_IRWXG;
  274. change->flags |= MODE_COPY_EXISTING;
  275. break;
  276. case 'o':
  277. /* Set the affected bits to the value of the `o' bits
  278. on the same file. */
  279. if (change->value)
  280. goto invalid;
  281. change->value = S_IRWXO;
  282. change->flags |= MODE_COPY_EXISTING;
  283. break;
  284. default:
  285. goto no_more_values;
  286. }
  287. no_more_values:;
  288. }
  289. } while (*mode_string == ',');
  290. if (*mode_string == 0)
  291. return head;
  292. invalid:
  293. mode_free (head);
  294. return MODE_INVALID;
  295. }
  296. /* Return a file mode change operation that sets permissions to match those
  297. of REF_FILE. Return MODE_BAD_REFERENCE if REF_FILE can't be accessed. */
  298. struct mode_change *
  299. mode_create_from_ref (const char *ref_file)
  300. {
  301. struct mode_change *change; /* the only change element */
  302. struct stat ref_stats;
  303. if (stat (ref_file, &ref_stats))
  304. return MODE_BAD_REFERENCE;
  305. change = talloc (struct mode_change);
  306. if (change == NULL)
  307. return MODE_MEMORY_EXHAUSTED;
  308. change->op = '=';
  309. change->flags = 0;
  310. change->affected = CHMOD_MODE_BITS;
  311. change->value = ref_stats.st_mode;
  312. change->next = NULL;
  313. return change;
  314. }
  315. /* Return file mode OLDMODE, adjusted as indicated by the list of change
  316. operations CHANGES. If OLDMODE is a directory, the type `X'
  317. change affects it even if no execute bits were set in OLDMODE.
  318. The returned value has the S_IFMT bits cleared. */
  319. mode_t
  320. mode_adjust (mode_t oldmode, const struct mode_change *changes)
  321. {
  322. mode_t newmode; /* The adjusted mode and one operand. */
  323. mode_t value; /* The other operand. */
  324. newmode = oldmode & CHMOD_MODE_BITS;
  325. for (; changes; changes = changes->next)
  326. {
  327. if (changes->flags & MODE_COPY_EXISTING)
  328. {
  329. /* Isolate in `value' the bits in `newmode' to copy, given in
  330. the mask `changes->value'. */
  331. value = newmode & changes->value;
  332. if (changes->value & S_IRWXU)
  333. /* Copy `u' permissions onto `g' and `o'. */
  334. value |= ((value & S_IRUSR ? S_IRGRP | S_IROTH : 0)
  335. | (value & S_IWUSR ? S_IWGRP | S_IROTH : 0)
  336. | (value & S_IXUSR ? S_IXGRP | S_IXOTH : 0));
  337. else if (changes->value & S_IRWXG)
  338. /* Copy `g' permissions onto `u' and `o'. */
  339. value |= ((value & S_IRGRP ? S_IRUSR | S_IROTH : 0)
  340. | (value & S_IWGRP ? S_IWUSR | S_IROTH : 0)
  341. | (value & S_IXGRP ? S_IXUSR | S_IXOTH : 0));
  342. else
  343. /* Copy `o' permissions onto `u' and `g'. */
  344. value |= ((value & S_IROTH ? S_IRUSR | S_IRGRP : 0)
  345. | (value & S_IWOTH ? S_IWUSR | S_IRGRP : 0)
  346. | (value & S_IXOTH ? S_IXUSR | S_IXGRP : 0));
  347. /* In order to change only `u', `g', or `o' permissions,
  348. or some combination thereof, clear unselected bits.
  349. This can not be done in mode_compile because the value
  350. to which the `changes->affected' mask is applied depends
  351. on the old mode of each file. */
  352. value &= changes->affected;
  353. }
  354. else
  355. {
  356. value = changes->value;
  357. /* If `X', do not affect the execute bits if the file is not a
  358. directory and no execute bits are already set. */
  359. if ((changes->flags & MODE_X_IF_ANY_X)
  360. && !S_ISDIR (oldmode)
  361. && (newmode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0)
  362. /* Clear the execute bits. */
  363. value &= ~ (S_IXUSR | S_IXGRP | S_IXOTH);
  364. }
  365. switch (changes->op)
  366. {
  367. case '=':
  368. /* Preserve the previous values in `newmode' of bits that are
  369. not affected by this change operation. */
  370. newmode = (newmode & ~changes->affected) | value;
  371. break;
  372. case '+':
  373. newmode |= value;
  374. break;
  375. case '-':
  376. newmode &= ~value;
  377. break;
  378. }
  379. }
  380. return newmode;
  381. }
  382. /* Free the memory used by the list of file mode change operations
  383. CHANGES. */
  384. void
  385. mode_free (register struct mode_change *changes)
  386. {
  387. register struct mode_change *next;
  388. while (changes)
  389. {
  390. next = changes->next;
  391. free (changes);
  392. changes = next;
  393. }
  394. }