2001-03-17 04:17:14 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
2003-03-19 14:43:01 +05:30
|
|
|
* parse_mode implementation for busybox
|
2001-03-17 04:17:14 +05:30
|
|
|
*
|
2003-03-19 14:43:01 +05:30
|
|
|
* Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
|
2001-03-17 04:17:14 +05:30
|
|
|
*
|
2006-07-10 17:11:19 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2001-03-17 04:17:14 +05:30
|
|
|
*/
|
|
|
|
|
2003-03-19 14:43:01 +05:30
|
|
|
/* http://www.opengroup.org/onlinepubs/007904975/utilities/chmod.html */
|
|
|
|
|
2001-03-17 04:17:14 +05:30
|
|
|
#include "libbb.h"
|
|
|
|
|
2007-04-10 21:13:37 +05:30
|
|
|
/* This function is used from NOFORK applets. It must not allocate anything */
|
|
|
|
|
2007-01-27 04:24:01 +05:30
|
|
|
#define FILEMODEBITS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
|
2001-03-17 04:17:14 +05:30
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
int FAST_FUNC bb_parse_mode(const char *s, mode_t *current_mode)
|
2001-03-17 04:17:14 +05:30
|
|
|
{
|
2004-03-15 13:59:22 +05:30
|
|
|
static const mode_t who_mask[] = {
|
2003-03-19 14:43:01 +05:30
|
|
|
S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO, /* a */
|
2007-01-27 04:24:01 +05:30
|
|
|
S_ISUID | S_IRWXU, /* u */
|
|
|
|
S_ISGID | S_IRWXG, /* g */
|
|
|
|
S_IRWXO /* o */
|
2001-03-17 04:17:14 +05:30
|
|
|
};
|
2003-03-19 14:43:01 +05:30
|
|
|
static const mode_t perm_mask[] = {
|
2001-03-17 04:17:14 +05:30
|
|
|
S_IRUSR | S_IRGRP | S_IROTH, /* r */
|
|
|
|
S_IWUSR | S_IWGRP | S_IWOTH, /* w */
|
|
|
|
S_IXUSR | S_IXGRP | S_IXOTH, /* x */
|
2003-03-19 14:43:01 +05:30
|
|
|
S_IXUSR | S_IXGRP | S_IXOTH, /* X -- special -- see below */
|
2007-01-27 04:24:01 +05:30
|
|
|
S_ISUID | S_ISGID, /* s */
|
|
|
|
S_ISVTX /* t */
|
2001-03-17 04:17:14 +05:30
|
|
|
};
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char who_chars[] ALIGN1 = "augo";
|
|
|
|
static const char perm_chars[] ALIGN1 = "rwxXst";
|
2001-03-17 04:17:14 +05:30
|
|
|
|
|
|
|
const char *p;
|
2003-03-19 14:43:01 +05:30
|
|
|
mode_t wholist;
|
|
|
|
mode_t permlist;
|
|
|
|
mode_t new_mode;
|
|
|
|
char op;
|
|
|
|
|
2009-09-23 20:47:53 +05:30
|
|
|
if ((unsigned char)(*s - '0') < 8) {
|
2003-03-19 14:43:01 +05:30
|
|
|
unsigned long tmp;
|
|
|
|
char *e;
|
2001-03-17 04:17:14 +05:30
|
|
|
|
2007-01-27 04:24:01 +05:30
|
|
|
tmp = strtoul(s, &e, 8);
|
2003-03-19 23:39:03 +05:30
|
|
|
if (*e || (tmp > 07777U)) { /* Check range and trailing chars. */
|
2003-03-19 14:43:01 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*current_mode = tmp;
|
|
|
|
return 1;
|
2001-03-17 04:17:14 +05:30
|
|
|
}
|
|
|
|
|
2003-03-19 14:43:01 +05:30
|
|
|
new_mode = *current_mode;
|
|
|
|
|
2007-01-27 04:24:01 +05:30
|
|
|
/* Note: we allow empty clauses, and hence empty modes.
|
2003-03-19 14:43:01 +05:30
|
|
|
* We treat an empty mode as no change to perms. */
|
|
|
|
|
|
|
|
while (*s) { /* Process clauses. */
|
|
|
|
if (*s == ',') { /* We allow empty clauses. */
|
|
|
|
++s;
|
|
|
|
continue;
|
2001-03-17 04:17:14 +05:30
|
|
|
}
|
2003-03-19 14:43:01 +05:30
|
|
|
|
|
|
|
/* Get a wholist. */
|
|
|
|
wholist = 0;
|
2007-01-27 04:24:01 +05:30
|
|
|
WHO_LIST:
|
2003-03-19 14:43:01 +05:30
|
|
|
p = who_chars;
|
|
|
|
do {
|
|
|
|
if (*p == *s) {
|
|
|
|
wholist |= who_mask[(int)(p-who_chars)];
|
|
|
|
if (!*++s) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
goto WHO_LIST;
|
2001-03-17 04:17:14 +05:30
|
|
|
}
|
2003-03-19 14:43:01 +05:30
|
|
|
} while (*++p);
|
|
|
|
|
|
|
|
do { /* Process action list. */
|
|
|
|
if ((*s != '+') && (*s != '-')) {
|
|
|
|
if (*s != '=') {
|
|
|
|
return 0;
|
2001-03-17 04:17:14 +05:30
|
|
|
}
|
2003-03-19 14:43:01 +05:30
|
|
|
/* Since op is '=', clear all bits corresponding to the
|
2007-01-27 04:24:01 +05:30
|
|
|
* wholist, or all file bits if wholist is empty. */
|
2003-03-19 14:43:01 +05:30
|
|
|
permlist = ~FILEMODEBITS;
|
|
|
|
if (wholist) {
|
|
|
|
permlist = ~wholist;
|
2001-03-17 04:17:14 +05:30
|
|
|
}
|
2003-03-19 14:43:01 +05:30
|
|
|
new_mode &= permlist;
|
|
|
|
}
|
|
|
|
op = *s++;
|
|
|
|
|
|
|
|
/* Check for permcopy. */
|
|
|
|
p = who_chars + 1; /* Skip 'a' entry. */
|
|
|
|
do {
|
|
|
|
if (*p == *s) {
|
|
|
|
int i = 0;
|
|
|
|
permlist = who_mask[(int)(p-who_chars)]
|
|
|
|
& (S_IRWXU | S_IRWXG | S_IRWXO)
|
|
|
|
& new_mode;
|
|
|
|
do {
|
|
|
|
if (permlist & perm_mask[i]) {
|
|
|
|
permlist |= perm_mask[i];
|
|
|
|
}
|
|
|
|
} while (++i < 3);
|
|
|
|
++s;
|
|
|
|
goto GOT_ACTION;
|
|
|
|
}
|
|
|
|
} while (*++p);
|
2001-03-17 04:17:14 +05:30
|
|
|
|
2003-03-19 14:43:01 +05:30
|
|
|
/* It was not a permcopy, so get a permlist. */
|
|
|
|
permlist = 0;
|
2007-01-27 04:24:01 +05:30
|
|
|
PERM_LIST:
|
2003-03-19 14:43:01 +05:30
|
|
|
p = perm_chars;
|
|
|
|
do {
|
|
|
|
if (*p == *s) {
|
|
|
|
if ((*p != 'X')
|
2007-01-27 04:24:01 +05:30
|
|
|
|| (new_mode & (S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH))
|
2003-03-19 14:43:01 +05:30
|
|
|
) {
|
|
|
|
permlist |= perm_mask[(int)(p-perm_chars)];
|
|
|
|
}
|
|
|
|
if (!*++s) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
goto PERM_LIST;
|
|
|
|
}
|
|
|
|
} while (*++p);
|
2007-01-27 04:24:01 +05:30
|
|
|
GOT_ACTION:
|
2003-03-19 14:43:01 +05:30
|
|
|
if (permlist) { /* The permlist was nonempty. */
|
2007-01-27 04:24:01 +05:30
|
|
|
mode_t tmp = wholist;
|
|
|
|
if (!wholist) {
|
|
|
|
mode_t u_mask = umask(0);
|
|
|
|
umask(u_mask);
|
|
|
|
tmp = ~u_mask;
|
2003-03-19 14:43:01 +05:30
|
|
|
}
|
|
|
|
permlist &= tmp;
|
|
|
|
if (op == '-') {
|
|
|
|
new_mode &= ~permlist;
|
|
|
|
} else {
|
|
|
|
new_mode |= permlist;
|
2001-03-17 04:17:14 +05:30
|
|
|
}
|
|
|
|
}
|
2003-03-19 14:43:01 +05:30
|
|
|
} while (*s && (*s != ','));
|
|
|
|
}
|
2001-03-17 04:17:14 +05:30
|
|
|
|
2003-03-19 14:43:01 +05:30
|
|
|
*current_mode = new_mode;
|
|
|
|
return 1;
|
2001-03-17 04:17:14 +05:30
|
|
|
}
|