blob: dc65860f6924cfe3d7e80b050f40c6f1be02cfaa [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{
Eric Andersenc7bda1c2004-03-15 08:29:22 +000019 static const mode_t who_mask[] = {
Manuel Novoa III cad53642003-03-19 09:13:01 +000020 S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO, /* a */
Denis Vlasenko86724af2007-01-26 22:54:01 +000021 S_ISUID | S_IRWXU, /* u */
22 S_ISGID | S_IRWXG, /* g */
23 S_IRWXO /* o */
Eric Andersenaad1a882001-03-16 22:47:14 +000024 };
Manuel Novoa III cad53642003-03-19 09:13:01 +000025 static const mode_t perm_mask[] = {
Eric Andersenaad1a882001-03-16 22:47:14 +000026 S_IRUSR | S_IRGRP | S_IROTH, /* r */
27 S_IWUSR | S_IWGRP | S_IWOTH, /* w */
28 S_IXUSR | S_IXGRP | S_IXOTH, /* x */
Manuel Novoa III cad53642003-03-19 09:13:01 +000029 S_IXUSR | S_IXGRP | S_IXOTH, /* X -- special -- see below */
Denis Vlasenko86724af2007-01-26 22:54:01 +000030 S_ISUID | S_ISGID, /* s */
31 S_ISVTX /* t */
Eric Andersenaad1a882001-03-16 22:47:14 +000032 };
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000033 static const char who_chars[] ALIGN1 = "augo";
34 static const char perm_chars[] ALIGN1 = "rwxXst";
Eric Andersenaad1a882001-03-16 22:47:14 +000035
36 const char *p;
Manuel Novoa III cad53642003-03-19 09:13:01 +000037 mode_t wholist;
38 mode_t permlist;
Manuel Novoa III cad53642003-03-19 09:13:01 +000039 mode_t new_mode;
40 char op;
Eric Andersenaad1a882001-03-16 22:47:14 +000041
Denys Vlasenko1f27ab02009-09-23 17:17:53 +020042 if ((unsigned char)(*s - '0') < 8) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000043 unsigned long tmp;
44 char *e;
45
Denis Vlasenko86724af2007-01-26 22:54:01 +000046 tmp = strtoul(s, &e, 8);
Manuel Novoa III ea4c4342003-03-19 18:09:03 +000047 if (*e || (tmp > 07777U)) { /* Check range and trailing chars. */
Denys Vlasenko5711a2a2015-10-07 17:55:33 +020048 return -1;
Manuel Novoa III cad53642003-03-19 09:13:01 +000049 }
Denys Vlasenko5711a2a2015-10-07 17:55:33 +020050 return tmp;
Eric Andersenaad1a882001-03-16 22:47:14 +000051 }
52
Denys Vlasenko5711a2a2015-10-07 17:55:33 +020053 new_mode = current_mode;
Manuel Novoa III cad53642003-03-19 09:13:01 +000054
Denis Vlasenko86724af2007-01-26 22:54:01 +000055 /* Note: we allow empty clauses, and hence empty modes.
Manuel Novoa III cad53642003-03-19 09:13:01 +000056 * We treat an empty mode as no change to perms. */
57
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020058 while (*s) { /* Process clauses. */
59 if (*s == ',') { /* We allow empty clauses. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000060 ++s;
61 continue;
Eric Andersenaad1a882001-03-16 22:47:14 +000062 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000063
64 /* Get a wholist. */
65 wholist = 0;
Denis Vlasenko86724af2007-01-26 22:54:01 +000066 WHO_LIST:
Manuel Novoa III cad53642003-03-19 09:13:01 +000067 p = who_chars;
68 do {
69 if (*p == *s) {
70 wholist |= who_mask[(int)(p-who_chars)];
71 if (!*++s) {
Denys Vlasenko5711a2a2015-10-07 17:55:33 +020072 return -1;
Eric Andersenaad1a882001-03-16 22:47:14 +000073 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000074 goto WHO_LIST;
75 }
76 } while (*++p);
77
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020078 do { /* Process action list. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000079 if ((*s != '+') && (*s != '-')) {
80 if (*s != '=') {
Denys Vlasenko5711a2a2015-10-07 17:55:33 +020081 return -1;
Manuel Novoa III cad53642003-03-19 09:13:01 +000082 }
83 /* Since op is '=', clear all bits corresponding to the
Denis Vlasenko86724af2007-01-26 22:54:01 +000084 * wholist, or all file bits if wholist is empty. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000085 permlist = ~FILEMODEBITS;
86 if (wholist) {
87 permlist = ~wholist;
88 }
89 new_mode &= permlist;
90 }
91 op = *s++;
92
93 /* Check for permcopy. */
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020094 p = who_chars + 1; /* Skip 'a' entry. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000095 do {
96 if (*p == *s) {
97 int i = 0;
98 permlist = who_mask[(int)(p-who_chars)]
99 & (S_IRWXU | S_IRWXG | S_IRWXO)
100 & new_mode;
101 do {
102 if (permlist & perm_mask[i]) {
103 permlist |= perm_mask[i];
104 }
105 } while (++i < 3);
106 ++s;
107 goto GOT_ACTION;
108 }
109 } while (*++p);
110
111 /* It was not a permcopy, so get a permlist. */
112 permlist = 0;
Denis Vlasenko86724af2007-01-26 22:54:01 +0000113 PERM_LIST:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000114 p = perm_chars;
115 do {
116 if (*p == *s) {
117 if ((*p != 'X')
Denis Vlasenko86724af2007-01-26 22:54:01 +0000118 || (new_mode & (S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000119 ) {
120 permlist |= perm_mask[(int)(p-perm_chars)];
121 }
122 if (!*++s) {
123 break;
124 }
125 goto PERM_LIST;
126 }
127 } while (*++p);
Denis Vlasenko86724af2007-01-26 22:54:01 +0000128 GOT_ACTION:
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200129 if (permlist) { /* The permlist was nonempty. */
Denis Vlasenko86724af2007-01-26 22:54:01 +0000130 mode_t tmp = wholist;
131 if (!wholist) {
132 mode_t u_mask = umask(0);
133 umask(u_mask);
134 tmp = ~u_mask;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000135 }
136 permlist &= tmp;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000137 if (op == '-') {
138 new_mode &= ~permlist;
Eric Andersenaad1a882001-03-16 22:47:14 +0000139 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000140 new_mode |= permlist;
Eric Andersenaad1a882001-03-16 22:47:14 +0000141 }
142 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000143 } while (*s && (*s != ','));
144 }
Eric Andersenaad1a882001-03-16 22:47:14 +0000145
Denys Vlasenko5711a2a2015-10-07 17:55:33 +0200146 return new_mode;
Eric Andersenaad1a882001-03-16 22:47:14 +0000147}