2001-03-16 22:47:14 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
2003-03-19 09:13:01 +00:00
|
|
|
* parse_mode implementation for busybox
|
2001-03-16 22:47:14 +00:00
|
|
|
*
|
2003-03-19 09:13:01 +00:00
|
|
|
* Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
|
2001-03-16 22:47:14 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2003-03-19 09:13:01 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2001-03-16 22:47:14 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2003-03-19 09:13:01 +00:00
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
2001-03-16 22:47:14 +00:00
|
|
|
*/
|
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
/* http://www.opengroup.org/onlinepubs/007904975/utilities/chmod.html */
|
|
|
|
|
2001-03-16 22:47:14 +00:00
|
|
|
#include <stdlib.h>
|
2003-03-19 09:13:01 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <sys/stat.h>
|
2001-03-16 22:47:14 +00:00
|
|
|
#include "libbb.h"
|
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
#define FILEMODEBITS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
|
2001-03-16 22:47:14 +00:00
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
extern int bb_parse_mode(const char *s, mode_t *current_mode)
|
2001-03-16 22:47:14 +00:00
|
|
|
{
|
2004-03-15 08:29:22 +00:00
|
|
|
static const mode_t who_mask[] = {
|
2003-03-19 09:13:01 +00:00
|
|
|
S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO, /* a */
|
2001-03-16 22:47:14 +00:00
|
|
|
S_ISUID | S_IRWXU, /* u */
|
|
|
|
S_ISGID | S_IRWXG, /* g */
|
2003-03-19 09:13:01 +00:00
|
|
|
S_IRWXO /* o */
|
2001-03-16 22:47:14 +00:00
|
|
|
};
|
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
static const mode_t perm_mask[] = {
|
2001-03-16 22:47:14 +00:00
|
|
|
S_IRUSR | S_IRGRP | S_IROTH, /* r */
|
|
|
|
S_IWUSR | S_IWGRP | S_IWOTH, /* w */
|
|
|
|
S_IXUSR | S_IXGRP | S_IXOTH, /* x */
|
2003-03-19 09:13:01 +00:00
|
|
|
S_IXUSR | S_IXGRP | S_IXOTH, /* X -- special -- see below */
|
2001-03-16 22:47:14 +00:00
|
|
|
S_ISUID | S_ISGID, /* s */
|
|
|
|
S_ISVTX /* t */
|
|
|
|
};
|
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
static const char who_chars[] = "augo";
|
|
|
|
static const char perm_chars[] = "rwxXst";
|
2001-03-16 22:47:14 +00:00
|
|
|
|
|
|
|
const char *p;
|
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
mode_t wholist;
|
|
|
|
mode_t permlist;
|
|
|
|
mode_t mask;
|
|
|
|
mode_t new_mode;
|
|
|
|
char op;
|
|
|
|
|
|
|
|
assert(s);
|
|
|
|
|
|
|
|
if (((unsigned int)(*s - '0')) < 8) {
|
|
|
|
unsigned long tmp;
|
|
|
|
char *e;
|
2001-03-16 22:47:14 +00:00
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
tmp = strtol(s, &e, 8);
|
2003-03-19 18:09:03 +00:00
|
|
|
if (*e || (tmp > 07777U)) { /* Check range and trailing chars. */
|
2003-03-19 09:13:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*current_mode = tmp;
|
|
|
|
return 1;
|
2001-03-16 22:47:14 +00:00
|
|
|
}
|
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
mask = umask(0);
|
|
|
|
umask(mask);
|
|
|
|
|
|
|
|
new_mode = *current_mode;
|
|
|
|
|
|
|
|
/* Note: We allow empty clauses, and hence empty modes.
|
|
|
|
* We treat an empty mode as no change to perms. */
|
|
|
|
|
|
|
|
while (*s) { /* Process clauses. */
|
|
|
|
|
|
|
|
if (*s == ',') { /* We allow empty clauses. */
|
|
|
|
++s;
|
|
|
|
continue;
|
2001-03-16 22:47:14 +00:00
|
|
|
}
|
2003-03-19 09:13:01 +00:00
|
|
|
|
|
|
|
/* Get a wholist. */
|
|
|
|
wholist = 0;
|
|
|
|
|
|
|
|
WHO_LIST:
|
|
|
|
p = who_chars;
|
|
|
|
do {
|
|
|
|
if (*p == *s) {
|
|
|
|
wholist |= who_mask[(int)(p-who_chars)];
|
|
|
|
if (!*++s) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
goto WHO_LIST;
|
2001-03-16 22:47:14 +00:00
|
|
|
}
|
2003-03-19 09:13:01 +00:00
|
|
|
} while (*++p);
|
|
|
|
|
|
|
|
do { /* Process action list. */
|
|
|
|
if ((*s != '+') && (*s != '-')) {
|
|
|
|
if (*s != '=') {
|
|
|
|
return 0;
|
2001-03-16 22:47:14 +00:00
|
|
|
}
|
2003-03-19 09:13:01 +00:00
|
|
|
/* Since op is '=', clear all bits corresponding to the
|
|
|
|
* wholist, of all file bits if wholist is empty. */
|
|
|
|
permlist = ~FILEMODEBITS;
|
|
|
|
if (wholist) {
|
|
|
|
permlist = ~wholist;
|
2001-03-16 22:47:14 +00:00
|
|
|
}
|
2003-03-19 09:13:01 +00:00
|
|
|
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-16 22:47:14 +00:00
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
/* It was not a permcopy, so get a permlist. */
|
|
|
|
permlist = 0;
|
|
|
|
|
|
|
|
PERM_LIST:
|
|
|
|
p = perm_chars;
|
|
|
|
do {
|
|
|
|
if (*p == *s) {
|
|
|
|
if ((*p != 'X')
|
|
|
|
|| (new_mode & (S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH))
|
|
|
|
) {
|
|
|
|
permlist |= perm_mask[(int)(p-perm_chars)];
|
|
|
|
}
|
|
|
|
if (!*++s) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
goto PERM_LIST;
|
|
|
|
}
|
|
|
|
} while (*++p);
|
|
|
|
|
|
|
|
GOT_ACTION:
|
|
|
|
if (permlist) { /* The permlist was nonempty. */
|
|
|
|
mode_t tmp = ~mask;
|
|
|
|
if (wholist) {
|
|
|
|
tmp = wholist;
|
|
|
|
}
|
|
|
|
permlist &= tmp;
|
|
|
|
|
|
|
|
if (op == '-') {
|
|
|
|
new_mode &= ~permlist;
|
|
|
|
} else {
|
|
|
|
new_mode |= permlist;
|
2001-03-16 22:47:14 +00:00
|
|
|
}
|
|
|
|
}
|
2003-03-19 09:13:01 +00:00
|
|
|
} while (*s && (*s != ','));
|
|
|
|
}
|
2001-03-16 22:47:14 +00:00
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
*current_mode = new_mode;
|
2001-03-16 22:47:14 +00:00
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
return 1;
|
2001-03-16 22:47:14 +00:00
|
|
|
}
|