blob: 356d95db6f5505afc3d5de2f995883e5d98c4a70 [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 <stdlib.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000013#include <assert.h>
14#include <sys/stat.h>
Eric Andersenaad1a882001-03-16 22:47:14 +000015#include "libbb.h"
16
Manuel Novoa III cad53642003-03-19 09:13:01 +000017#define FILEMODEBITS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
Eric Andersenaad1a882001-03-16 22:47:14 +000018
Rob Landleydfba7412006-03-06 20:47:33 +000019int bb_parse_mode(const char *s, mode_t *current_mode)
Eric Andersenaad1a882001-03-16 22:47:14 +000020{
Eric Andersenc7bda1c2004-03-15 08:29:22 +000021 static const mode_t who_mask[] = {
Manuel Novoa III cad53642003-03-19 09:13:01 +000022 S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO, /* a */
Eric Andersenaad1a882001-03-16 22:47:14 +000023 S_ISUID | S_IRWXU, /* u */
24 S_ISGID | S_IRWXG, /* g */
Manuel Novoa III cad53642003-03-19 09:13:01 +000025 S_IRWXO /* o */
Eric Andersenaad1a882001-03-16 22:47:14 +000026 };
27
Manuel Novoa III cad53642003-03-19 09:13:01 +000028 static const mode_t perm_mask[] = {
Eric Andersenaad1a882001-03-16 22:47:14 +000029 S_IRUSR | S_IRGRP | S_IROTH, /* r */
30 S_IWUSR | S_IWGRP | S_IWOTH, /* w */
31 S_IXUSR | S_IXGRP | S_IXOTH, /* x */
Manuel Novoa III cad53642003-03-19 09:13:01 +000032 S_IXUSR | S_IXGRP | S_IXOTH, /* X -- special -- see below */
Eric Andersenaad1a882001-03-16 22:47:14 +000033 S_ISUID | S_ISGID, /* s */
34 S_ISVTX /* t */
35 };
36
Manuel Novoa III cad53642003-03-19 09:13:01 +000037 static const char who_chars[] = "augo";
38 static const char perm_chars[] = "rwxXst";
Eric Andersenaad1a882001-03-16 22:47:14 +000039
40 const char *p;
41
Manuel Novoa III cad53642003-03-19 09:13:01 +000042 mode_t wholist;
43 mode_t permlist;
44 mode_t mask;
45 mode_t new_mode;
46 char op;
Eric Andersenaad1a882001-03-16 22:47:14 +000047
Manuel Novoa III cad53642003-03-19 09:13:01 +000048 assert(s);
49
50 if (((unsigned int)(*s - '0')) < 8) {
51 unsigned long tmp;
52 char *e;
53
54 tmp = strtol(s, &e, 8);
Manuel Novoa III ea4c4342003-03-19 18:09:03 +000055 if (*e || (tmp > 07777U)) { /* Check range and trailing chars. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000056 return 0;
57 }
58 *current_mode = tmp;
59 return 1;
Eric Andersenaad1a882001-03-16 22:47:14 +000060 }
61
Manuel Novoa III cad53642003-03-19 09:13:01 +000062 mask = umask(0);
63 umask(mask);
64
65 new_mode = *current_mode;
66
67 /* Note: We allow empty clauses, and hence empty modes.
68 * We treat an empty mode as no change to perms. */
69
70 while (*s) { /* Process clauses. */
71
72 if (*s == ',') { /* We allow empty clauses. */
73 ++s;
74 continue;
Eric Andersenaad1a882001-03-16 22:47:14 +000075 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000076
77 /* Get a wholist. */
78 wholist = 0;
79
80 WHO_LIST:
81 p = who_chars;
82 do {
83 if (*p == *s) {
84 wholist |= who_mask[(int)(p-who_chars)];
85 if (!*++s) {
86 return 0;
Eric Andersenaad1a882001-03-16 22:47:14 +000087 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000088 goto WHO_LIST;
89 }
90 } while (*++p);
91
92 do { /* Process action list. */
93 if ((*s != '+') && (*s != '-')) {
94 if (*s != '=') {
95 return 0;
96 }
97 /* Since op is '=', clear all bits corresponding to the
98 * wholist, of all file bits if wholist is empty. */
99 permlist = ~FILEMODEBITS;
100 if (wholist) {
101 permlist = ~wholist;
102 }
103 new_mode &= permlist;
104 }
105 op = *s++;
106
107 /* Check for permcopy. */
108 p = who_chars + 1; /* Skip 'a' entry. */
109 do {
110 if (*p == *s) {
111 int i = 0;
112 permlist = who_mask[(int)(p-who_chars)]
113 & (S_IRWXU | S_IRWXG | S_IRWXO)
114 & new_mode;
115 do {
116 if (permlist & perm_mask[i]) {
117 permlist |= perm_mask[i];
118 }
119 } while (++i < 3);
120 ++s;
121 goto GOT_ACTION;
122 }
123 } while (*++p);
124
125 /* It was not a permcopy, so get a permlist. */
126 permlist = 0;
127
128 PERM_LIST:
129 p = perm_chars;
130 do {
131 if (*p == *s) {
132 if ((*p != 'X')
133 || (new_mode & (S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH))
134 ) {
135 permlist |= perm_mask[(int)(p-perm_chars)];
136 }
137 if (!*++s) {
138 break;
139 }
140 goto PERM_LIST;
141 }
142 } while (*++p);
143
144 GOT_ACTION:
145 if (permlist) { /* The permlist was nonempty. */
146 mode_t tmp = ~mask;
147 if (wholist) {
148 tmp = wholist;
149 }
150 permlist &= tmp;
151
152 if (op == '-') {
153 new_mode &= ~permlist;
Eric Andersenaad1a882001-03-16 22:47:14 +0000154 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000155 new_mode |= permlist;
Eric Andersenaad1a882001-03-16 22:47:14 +0000156 }
157 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000158 } while (*s && (*s != ','));
159 }
Eric Andersenaad1a882001-03-16 22:47:14 +0000160
Manuel Novoa III cad53642003-03-19 09:13:01 +0000161 *current_mode = new_mode;
Eric Andersenaad1a882001-03-16 22:47:14 +0000162
Manuel Novoa III cad53642003-03-19 09:13:01 +0000163 return 1;
Eric Andersenaad1a882001-03-16 22:47:14 +0000164}