blob: 1d238e1e0c5cd57077690e3553baf173045b2e6b [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenaad1a882001-03-16 22:47:14 +00008 */
Denys Vlasenkoebe6d9d2017-10-05 14:40:24 +02009#include "libbb.h"
Eric Andersenaad1a882001-03-16 22:47:14 +000010
Manuel Novoa III cad53642003-03-19 09:13:01 +000011/* http://www.opengroup.org/onlinepubs/007904975/utilities/chmod.html */
12
Denis Vlasenko99912ca2007-04-10 15:43:37 +000013/* This function is used from NOFORK applets. It must not allocate anything */
14
Denis Vlasenko86724af2007-01-26 22:54:01 +000015#define FILEMODEBITS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
Eric Andersenaad1a882001-03-16 22:47:14 +000016
Denys Vlasenko5711a2a2015-10-07 17:55:33 +020017int FAST_FUNC bb_parse_mode(const char *s, unsigned current_mode)
Eric Andersenaad1a882001-03-16 22:47:14 +000018{
Denys Vlasenko4958c182021-09-17 00:47:23 +020019/* should be mode_t really, but in all Unixes these constants fit into uint16 */
20 static const uint16_t who_mask[] ALIGN2 = {
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 };
Denys Vlasenko4958c182021-09-17 00:47:23 +020026 static const uint16_t perm_mask[] ALIGN2 = {
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. */
Denys Vlasenko5711a2a2015-10-07 17:55:33 +020049 return -1;
Manuel Novoa III cad53642003-03-19 09:13:01 +000050 }
Denys Vlasenko5711a2a2015-10-07 17:55:33 +020051 return tmp;
Eric Andersenaad1a882001-03-16 22:47:14 +000052 }
53
Denys Vlasenko5711a2a2015-10-07 17:55:33 +020054 new_mode = current_mode;
Manuel Novoa III cad53642003-03-19 09:13:01 +000055
Denis Vlasenko86724af2007-01-26 22:54:01 +000056 /* Note: we allow empty clauses, and hence empty modes.
Manuel Novoa III cad53642003-03-19 09:13:01 +000057 * We treat an empty mode as no change to perms. */
58
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020059 while (*s) { /* Process clauses. */
60 if (*s == ',') { /* We allow empty clauses. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000061 ++s;
62 continue;
Eric Andersenaad1a882001-03-16 22:47:14 +000063 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000064
65 /* Get a wholist. */
66 wholist = 0;
Denis Vlasenko86724af2007-01-26 22:54:01 +000067 WHO_LIST:
Manuel Novoa III cad53642003-03-19 09:13:01 +000068 p = who_chars;
69 do {
70 if (*p == *s) {
71 wholist |= who_mask[(int)(p-who_chars)];
72 if (!*++s) {
Denys Vlasenko5711a2a2015-10-07 17:55:33 +020073 return -1;
Eric Andersenaad1a882001-03-16 22:47:14 +000074 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000075 goto WHO_LIST;
76 }
77 } while (*++p);
78
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020079 do { /* Process action list. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000080 if ((*s != '+') && (*s != '-')) {
81 if (*s != '=') {
Denys Vlasenko5711a2a2015-10-07 17:55:33 +020082 return -1;
Manuel Novoa III cad53642003-03-19 09:13:01 +000083 }
84 /* Since op is '=', clear all bits corresponding to the
Denis Vlasenko86724af2007-01-26 22:54:01 +000085 * wholist, or all file bits if wholist is empty. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000086 permlist = ~FILEMODEBITS;
87 if (wholist) {
88 permlist = ~wholist;
89 }
90 new_mode &= permlist;
91 }
92 op = *s++;
93
94 /* Check for permcopy. */
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020095 p = who_chars + 1; /* Skip 'a' entry. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000096 do {
97 if (*p == *s) {
98 int i = 0;
99 permlist = who_mask[(int)(p-who_chars)]
100 & (S_IRWXU | S_IRWXG | S_IRWXO)
101 & new_mode;
102 do {
103 if (permlist & perm_mask[i]) {
104 permlist |= perm_mask[i];
105 }
106 } while (++i < 3);
107 ++s;
108 goto GOT_ACTION;
109 }
110 } while (*++p);
111
112 /* It was not a permcopy, so get a permlist. */
113 permlist = 0;
Denis Vlasenko86724af2007-01-26 22:54:01 +0000114 PERM_LIST:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000115 p = perm_chars;
116 do {
117 if (*p == *s) {
118 if ((*p != 'X')
Denis Vlasenko86724af2007-01-26 22:54:01 +0000119 || (new_mode & (S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000120 ) {
121 permlist |= perm_mask[(int)(p-perm_chars)];
122 }
123 if (!*++s) {
124 break;
125 }
126 goto PERM_LIST;
127 }
128 } while (*++p);
Denis Vlasenko86724af2007-01-26 22:54:01 +0000129 GOT_ACTION:
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200130 if (permlist) { /* The permlist was nonempty. */
Denis Vlasenko86724af2007-01-26 22:54:01 +0000131 mode_t tmp = wholist;
132 if (!wholist) {
133 mode_t u_mask = umask(0);
134 umask(u_mask);
135 tmp = ~u_mask;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000136 }
137 permlist &= tmp;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000138 if (op == '-') {
139 new_mode &= ~permlist;
Eric Andersenaad1a882001-03-16 22:47:14 +0000140 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000141 new_mode |= permlist;
Eric Andersenaad1a882001-03-16 22:47:14 +0000142 }
143 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000144 } while (*s && (*s != ','));
145 }
Eric Andersenaad1a882001-03-16 22:47:14 +0000146
Denys Vlasenko5711a2a2015-10-07 17:55:33 +0200147 return new_mode;
Eric Andersenaad1a882001-03-16 22:47:14 +0000148}