Source code of Windows XP (NT5)
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.

461 lines
11 KiB

  1. /*
  2. * Copyright (c) 1989 The Regents of the University of California.
  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
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. All advertising materials mentioning features or use of this software
  14. * must display the following acknowledgement:
  15. * This product includes software developed by the University of
  16. * California, Berkeley and its contributors.
  17. * 4. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #if defined(LIBC_SCCS) && !defined(lint)
  34. static char sccsid[] = "@(#)setmode.c 5.6 (Berkeley) 5/27/91";
  35. #endif /* LIBC_SCCS and not lint */
  36. #ifdef _POSIX_SOURCE
  37. #include <misc.h>
  38. #endif
  39. #ifdef _POSIX_SOURCE //DF_DSC POSIX does not need this
  40. #else // only MAXPATHLEN was found there
  41. #include <sys/param.h> // and it wants machine directory stuff
  42. #endif
  43. #include <sys/stat.h>
  44. #include <sys/errno.h>
  45. #ifdef SETMODE_DEBUG
  46. #include <stdio.h>
  47. #endif
  48. #include <stdlib.h>
  49. #include <ctype.h>
  50. #define SET_LEN 6 /* initial # of bitcmd struct to malloc */
  51. #define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */
  52. struct bitcmd {
  53. char cmd;
  54. char cmd2;
  55. mode_t bits;
  56. };
  57. #define CMD2_CLR 0x01
  58. #define CMD2_SET 0x02
  59. #define CMD2_GBITS 0x04
  60. #define CMD2_OBITS 0x08
  61. #define CMD2_UBITS 0x10
  62. /*
  63. * Given the old mode and an array of bitcmd structures, apply the operations
  64. * described in the bitcmd structures to the old mode, and return the new mode.
  65. * Note that there is no '=' command; a strict assignment is just a '-' (clear
  66. * bits) followed by a '+' (set bits).
  67. */
  68. mode_t
  69. getmode(bbox, omode)
  70. void *bbox;
  71. mode_t omode;
  72. {
  73. register struct bitcmd *set;
  74. register mode_t newmode, value;
  75. set = (struct bitcmd *)bbox;
  76. newmode = omode;
  77. for (value = 0;; set++)
  78. switch(set->cmd) {
  79. /*
  80. * When copying the user, group or other bits around, we "know"
  81. * where the bit are in the mode so that we can do shifts to
  82. * copy them around. If we don't use shifts, it gets real
  83. * grundgy with lots of single bit checks and bit sets.
  84. */
  85. case 'u':
  86. value = (newmode & S_IRWXU) >> 6;
  87. goto common;
  88. case 'g':
  89. value = (newmode & S_IRWXG) >> 3;
  90. goto common;
  91. case 'o':
  92. value = newmode & S_IRWXO;
  93. common:
  94. if (set->cmd2 & CMD2_CLR) {
  95. if (set->cmd2 & CMD2_UBITS)
  96. newmode &= ~(S_IRWXU & set->bits);
  97. if (set->cmd2 & CMD2_GBITS)
  98. newmode &= ~(S_IRWXG & set->bits);
  99. if (set->cmd2 & CMD2_OBITS)
  100. newmode &= ~(S_IRWXO & set->bits);
  101. }
  102. if (set->cmd2 & CMD2_SET) {
  103. if (set->cmd2 & CMD2_UBITS)
  104. newmode |= (value<<6) & set->bits;
  105. if (set->cmd2 & CMD2_GBITS)
  106. newmode |= (value<<3) & set->bits;
  107. if (set->cmd2 & CMD2_OBITS)
  108. newmode |= value & set->bits;
  109. }
  110. break;
  111. case '+':
  112. newmode |= set->bits;
  113. break;
  114. case '-':
  115. newmode &= ~set->bits;
  116. break;
  117. case 'X':
  118. if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH))
  119. newmode |= set->bits;
  120. break;
  121. case '\0':
  122. default:
  123. #ifdef SETMODE_DEBUG
  124. (void)printf("getmode(, %04o) -> %04o\n",
  125. omode, newmode);
  126. #endif
  127. return(newmode);
  128. }
  129. }
  130. #define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
  131. static struct bitcmd *
  132. addcmd(set, op, who, oparg, mask)
  133. struct bitcmd *set;
  134. register int oparg, who;
  135. register int op;
  136. mode_t mask;
  137. {
  138. switch (op) {
  139. case '+':
  140. case 'X':
  141. set->cmd = (char)op;
  142. set->bits = (who ? who : mask) & oparg;
  143. break;
  144. case '-':
  145. set->cmd = '-';
  146. set->bits = (who ? who : (S_IRWXU|S_IRWXG|S_IRWXO)) & oparg;
  147. break;
  148. case '=':
  149. set->cmd = '-';
  150. if (!who) {
  151. set->bits = STANDARD_BITS;
  152. who = mask;
  153. } else
  154. set->bits = who;
  155. set++;
  156. set->cmd = '+';
  157. set->bits = who & oparg;
  158. break;
  159. case 'u':
  160. case 'g':
  161. case 'o':
  162. set->cmd = (char)op;
  163. if (who) {
  164. set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
  165. ((who & S_IRGRP) ? CMD2_GBITS : 0) |
  166. ((who & S_IROTH) ? CMD2_OBITS : 0);
  167. set->bits = (unsigned long)~0;
  168. } else {
  169. set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
  170. set->bits = mask;
  171. }
  172. if (oparg == '+')
  173. set->cmd2 |= CMD2_SET;
  174. else if (oparg == '-')
  175. set->cmd2 |= CMD2_CLR;
  176. else if (oparg == '=')
  177. set->cmd2 |= CMD2_SET|CMD2_CLR;
  178. break;
  179. }
  180. return(set+1);
  181. }
  182. #define ADDCMD(a, b, c, d) \
  183. if (set >= endset) { \
  184. register struct bitcmd *newset; \
  185. setlen += SET_LEN_INCR; \
  186. newset = realloc(saveset, sizeof(struct bitcmd) * setlen); \
  187. if (!saveset) \
  188. return(NULL); \
  189. set = newset + (set - saveset); \
  190. saveset = newset; \
  191. endset = newset + (setlen - 2); \
  192. } \
  193. set = addcmd(set, (a), (b), (c), (d))
  194. void *
  195. setmode(p)
  196. register char *p;
  197. {
  198. register int perm, who;
  199. register char op;
  200. mode_t mask;
  201. struct bitcmd *set, *saveset, *endset;
  202. int permXbits, setlen;
  203. static void compress_mode();
  204. /*
  205. * Get a copy of the mask for the permissions that are mask relative.
  206. * Flip the bits, we want what's not set.
  207. */
  208. (void)umask(mask = umask(0));
  209. mask = ~mask;
  210. setlen = SET_LEN + 2;
  211. set = (struct bitcmd *)malloc((u_int)(sizeof(struct bitcmd) * setlen));
  212. if (!set)
  213. return(NULL);
  214. saveset = set;
  215. endset = set + (setlen - 2);
  216. /*
  217. * If an absolute number, get it and return; disallow non-octal digits
  218. * or illegal bits.
  219. */
  220. if (isdigit(*p)) {
  221. perm = (mode_t)strtol(p, (char **)0, 8);
  222. #ifdef _POSIX_SOURCE
  223. if (perm & ~(STANDARD_BITS)) {
  224. #else
  225. if (perm & ~(STANDARD_BITS|S_ISTXT)) {
  226. #endif
  227. free(saveset);
  228. return(NULL);
  229. }
  230. while (*++p)
  231. if (*p < '0' || *p > '7') {
  232. free(saveset);
  233. return(NULL);
  234. }
  235. #ifdef _POSIX_SOURCE
  236. ADDCMD('=', (STANDARD_BITS), perm, mask);
  237. #else
  238. ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask);
  239. #endif
  240. return((void *)saveset);
  241. }
  242. if (!*p) {
  243. free(saveset);
  244. return(NULL);
  245. }
  246. /*
  247. * Build list of structures to set/clear/copy bits as described by
  248. * each clause of the symbolic mode.
  249. */
  250. for (;;) {
  251. /* First, find out which bits might be modified. */
  252. for (who = 0;; ++p) {
  253. switch (*p) {
  254. case 'a':
  255. who |= STANDARD_BITS;
  256. break;
  257. case 'u':
  258. who |= S_ISUID|S_IRWXU;
  259. break;
  260. case 'g':
  261. who |= S_ISGID|S_IRWXG;
  262. break;
  263. case 'o':
  264. who |= S_IRWXO;
  265. break;
  266. default:
  267. goto getop;
  268. }
  269. }
  270. getop:
  271. if ((op = *p++) != '+' && op != '-' && op != '=') {
  272. free(saveset);
  273. return(NULL);
  274. }
  275. #ifndef _POSIX_SOURCE
  276. who &= ~S_ISTXT;
  277. #endif
  278. for (perm = 0, permXbits = 0;; ++p) {
  279. switch (*p) {
  280. case 'r':
  281. perm |= S_IRUSR|S_IRGRP|S_IROTH;
  282. break;
  283. case 's':
  284. /* If only "other" bits ignore set-id. */
  285. if (who & ~S_IRWXO)
  286. perm |= S_ISUID|S_ISGID;
  287. break;
  288. case 't':
  289. /* If only "other" bits ignore sticky. */
  290. if (who & ~S_IRWXO) {
  291. #ifndef _POSIX_SOURCE
  292. who |= S_ISTXT;
  293. perm |= S_ISTXT;
  294. #endif
  295. }
  296. break;
  297. case 'w':
  298. perm |= S_IWUSR|S_IWGRP|S_IWOTH;
  299. break;
  300. case 'X':
  301. permXbits = S_IXUSR|S_IXGRP|S_IXOTH;
  302. break;
  303. case 'x':
  304. perm |= S_IXUSR|S_IXGRP|S_IXOTH;
  305. break;
  306. case 'u':
  307. case 'g':
  308. case 'o':
  309. /*
  310. * When ever we hit 'u', 'g', or 'o', we have
  311. * to flush out any partial mode that we have,
  312. * and then do the copying of the mode bits.
  313. */
  314. if (perm) {
  315. ADDCMD(op, who, perm, mask);
  316. perm = 0;
  317. }
  318. if (op == '+' && permXbits) {
  319. ADDCMD('X', who, permXbits, mask);
  320. permXbits = 0;
  321. }
  322. ADDCMD(*p, who, op, mask);
  323. break;
  324. default:
  325. /*
  326. * Add any permissions that we haven't already
  327. * done.
  328. */
  329. if (perm) {
  330. ADDCMD(op, who, perm, mask);
  331. perm = 0;
  332. }
  333. if (permXbits) {
  334. ADDCMD('X', who, permXbits, mask);
  335. permXbits = 0;
  336. }
  337. goto apply;
  338. }
  339. }
  340. apply: if (!*p)
  341. break;
  342. if (*p != ',')
  343. goto getop;
  344. ++p;
  345. }
  346. set->cmd = 0;
  347. #ifdef SETMODE_DEBUG
  348. (void)printf("Before compress_mode()\n");
  349. dumpmode(saveset);
  350. #endif
  351. compress_mode(saveset);
  352. #ifdef SETMODE_DEBUG
  353. (void)printf("After compress_mode()\n");
  354. dumpmode(saveset);
  355. #endif
  356. return((void *)saveset);
  357. }
  358. #ifdef SETMODE_DEBUG
  359. dumpmode(set)
  360. register struct bitcmd *set;
  361. {
  362. for (; set->cmd; ++set)
  363. (void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
  364. set->cmd, set->bits, set->cmd2 ? " cmd2:" : "",
  365. set->cmd2 & CMD2_CLR ? " CLR" : "",
  366. set->cmd2 & CMD2_SET ? " SET" : "",
  367. set->cmd2 & CMD2_UBITS ? " UBITS" : "",
  368. set->cmd2 & CMD2_GBITS ? " GBITS" : "",
  369. set->cmd2 & CMD2_OBITS ? " OBITS" : "");
  370. }
  371. #endif
  372. /*
  373. * Given an array of bitcmd structures, compress by compacting consecutive
  374. * '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u',
  375. * 'g' and 'o' commands continue to be separate. They could probably be
  376. * compacted, but it's not worth the effort.
  377. */
  378. static
  379. void
  380. compress_mode(set)
  381. register struct bitcmd *set;
  382. {
  383. register struct bitcmd *nset;
  384. register int setbits, clrbits, Xbits, op;
  385. for (nset = set;;) {
  386. /* Copy over any 'u', 'g' and 'o' commands. */
  387. while ((op = nset->cmd) != '+' && op != '-' && op != 'X') {
  388. *set++ = *nset++;
  389. if (!op)
  390. return;
  391. }
  392. for (setbits = clrbits = Xbits = 0;; nset++) {
  393. if ((op = nset->cmd) == '-') {
  394. clrbits |= nset->bits;
  395. setbits &= ~nset->bits;
  396. Xbits &= ~nset->bits;
  397. } else if (op == '+') {
  398. setbits |= nset->bits;
  399. clrbits &= ~nset->bits;
  400. Xbits &= ~nset->bits;
  401. } else if (op == 'X')
  402. Xbits |= nset->bits & ~setbits;
  403. else
  404. break;
  405. }
  406. if (clrbits) {
  407. set->cmd = '-';
  408. set->cmd2 = 0;
  409. set->bits = clrbits;
  410. set++;
  411. }
  412. if (setbits) {
  413. set->cmd = '+';
  414. set->cmd2 = 0;
  415. set->bits = setbits;
  416. set++;
  417. }
  418. if (Xbits) {
  419. set->cmd = 'X';
  420. set->cmd2 = 0;
  421. set->bits = Xbits;
  422. set++;
  423. }
  424. }
  425. }