blob: 6eca00ab11e4eb9df3483984e754d929491191c5 [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
Manuel Novoa III cad53642003-03-19 09:13:01 +00003 * parse_mode implementation for busybox
Eric Andersenaad1a882001-03-16 22:47:14 +00004 *
Manuel Novoa III cad53642003-03-19 09:13:01 +00005 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
Eric Andersenaad1a882001-03-16 22:47:14 +00006 *
"Robert P. J. Day"5d8843e2006-07-10 11:41:19 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenaad1a882001-03-16 22:47:14 +00008 */
9
Manuel Novoa III cad53642003-03-19 09:13:01 +000010/* http://www.opengroup.org/onlinepubs/007904975/utilities/chmod.html */
11
Eric Andersenaad1a882001-03-16 22:47:14 +000012#include "libbb.h"
13
Denis Vlasenko99912ca2007-04-10 15:43:37 +000014/* This function is used from NOFORK applets. It must not allocate anything */
15
Denis Vlasenko86724af2007-01-26 22:54:01 +000016#define FILEMODEBITS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
Eric Andersenaad1a882001-03-16 22:47:14 +000017
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000018int FAST_FUNC bb_parse_mode(const char *s, mode_t *current_mode)
Eric Andersenaad1a882001-03-16 22:47:14 +000019{
Eric Andersenc7bda1c2004-03-15 08:29:22 +000020 static const mode_t who_mask[] = {
Manuel Novoa III cad53642003-03-19 09:13:01 +000021 S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO, /* a */
Denis Vlasenko86724af2007-01-26 22:54:01 +000022 S_ISUID | S_IRWXU, /* u */
23 S_ISGID | S_IRWXG, /* g */
24 S_IRWXO /* o */
Eric Andersenaad1a882001-03-16 22:47:14 +000025 };
Manuel Novoa III cad53642003-03-19 09:13:01 +000026 static const mode_t perm_mask[] = {
Eric Andersenaad1a882001-03-16 22:47:14 +000027 S_IRUSR | S_IRGRP | S_IROTH, /* r */
28 S_IWUSR | S_IWGRP | S_IWOTH, /* w */
29 S_IXUSR | S_IXGRP | S_IXOTH, /* x */
Manuel Novoa III cad53642003-03-19 09:13:01 +000030 S_IXUSR | S_IXGRP | S_IXOTH, /* X -- special -- see below */
Denis Vlasenko86724af2007-01-26 22:54:01 +000031 S_ISUID | S_ISGID, /* s */
32 S_ISVTX /* t */
Eric Andersenaad1a882001-03-16 22:47:14 +000033 };
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000034 static const char who_chars[] ALIGN1 = "augo";
35 static const char perm_chars[] ALIGN1 = "rwxXst";
Eric Andersenaad1a882001-03-16 22:47:14 +000036
37 const char *p;
Manuel Novoa III cad53642003-03-19 09:13:01 +000038 mode_t wholist;
39 mode_t permlist;
Manuel Novoa III cad53642003-03-19 09:13:01 +000040 mode_t new_mode;
41 char op;
Eric Andersenaad1a882001-03-16 22:47:14 +000042
Denys Vlasenko1f27ab02009-09-23 17:17:53 +020043 if ((unsigned char)(*s - '0') < 8) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000044 unsigned long tmp;
45 char *e;
46
Denis Vlasenko86724af2007-01-26 22:54:01 +000047 tmp = strtoul(s, &e, 8);
Manuel Novoa III ea4c4342003-03-19 18:09:03 +000048 if (*e || (tmp > 07777U)) { /* Check range and trailing chars. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000049 return 0;
50 }
51 *current_mode = tmp;
52 return 1;
Eric Andersenaad1a882001-03-16 22:47:14 +000053 }
54
Manuel Novoa III cad53642003-03-19 09:13:01 +000055 new_mode = *current_mode;
56
Denis Vlasenko86724af2007-01-26 22:54:01 +000057 /* Note: we allow empty clauses, and hence empty modes.
Manuel Novoa III cad53642003-03-19 09:13:01 +000058 * We treat an empty mode as no change to perms. */
59
60 while (*s) { /* Process clauses. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000061 if (*s == ',') { /* We allow empty clauses. */
62 ++s;
63 continue;
Eric Andersenaad1a882001-03-16 22:47:14 +000064 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000065
66 /* Get a wholist. */
67 wholist = 0;
Denis Vlasenko86724af2007-01-26 22:54:01 +000068 WHO_LIST:
Manuel Novoa III cad53642003-03-19 09:13:01 +000069 p = who_chars;
70 do {
71 if (*p == *s) {
72 wholist |= who_mask[(int)(p-who_chars)];
73 if (!*++s) {
74 return 0;
Eric Andersenaad1a882001-03-16 22:47:14 +000075 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000076 goto WHO_LIST;
77 }
78 } while (*++p);
79
80 do { /* Process action list. */
81 if ((*s != '+') && (*s != '-')) {
82 if (*s != '=') {
83 return 0;
84 }
85 /* Since op is '=', clear all bits corresponding to the
Denis Vlasenko86724af2007-01-26 22:54:01 +000086 * wholist, or all file bits if wholist is empty. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000087 permlist = ~FILEMODEBITS;
88 if (wholist) {
89 permlist = ~wholist;
90 }
91 new_mode &= permlist;
92 }
93 op = *s++;
94
95 /* Check for permcopy. */
96 p = who_chars + 1; /* Skip 'a' entry. */
97 do {
98 if (*p == *s) {
99 int i = 0;
100 permlist = who_mask[(int)(p-who_chars)]
101 & (S_IRWXU | S_IRWXG | S_IRWXO)
102 & new_mode;
103 do {
104 if (permlist & perm_mask[i]) {
105 permlist |= perm_mask[i];
106 }
107 } while (++i < 3);
108 ++s;
109 goto GOT_ACTION;
110 }
111 } while (*++p);
112
113 /* It was not a permcopy, so get a permlist. */
114 permlist = 0;
Denis Vlasenko86724af2007-01-26 22:54:01 +0000115 PERM_LIST:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000116 p = perm_chars;
117 do {
118 if (*p == *s) {
119 if ((*p != 'X')
Denis Vlasenko86724af2007-01-26 22:54:01 +0000120 || (new_mode & (S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000121 ) {
122 permlist |= perm_mask[(int)(p-perm_chars)];
123 }
124 if (!*++s) {
125 break;
126 }
127 goto PERM_LIST;
128 }
129 } while (*++p);
Denis Vlasenko86724af2007-01-26 22:54:01 +0000130 GOT_ACTION:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000131 if (permlist) { /* The permlist was nonempty. */
Denis Vlasenko86724af2007-01-26 22:54:01 +0000132 mode_t tmp = wholist;
133 if (!wholist) {
134 mode_t u_mask = umask(0);
135 umask(u_mask);
136 tmp = ~u_mask;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000137 }
138 permlist &= tmp;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000139 if (op == '-') {
140 new_mode &= ~permlist;
Eric Andersenaad1a882001-03-16 22:47:14 +0000141 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000142 new_mode |= permlist;
Eric Andersenaad1a882001-03-16 22:47:14 +0000143 }
144 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000145 } while (*s && (*s != ','));
146 }
Eric Andersenaad1a882001-03-16 22:47:14 +0000147
Manuel Novoa III cad53642003-03-19 09:13:01 +0000148 *current_mode = new_mode;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000149 return 1;
Eric Andersenaad1a882001-03-16 22:47:14 +0000150}