Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 3 | * parse_mode implementation for busybox |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 4 | * |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 5 | * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 10 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/chmod.html */ |
| 11 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 12 | #include "libbb.h" |
| 13 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 14 | /* This function is used from NOFORK applets. It must not allocate anything */ |
| 15 | |
Denis Vlasenko | 86724af | 2007-01-26 22:54:01 +0000 | [diff] [blame] | 16 | #define FILEMODEBITS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 17 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 18 | int FAST_FUNC bb_parse_mode(const char *s, mode_t *current_mode) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 19 | { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 20 | static const mode_t who_mask[] = { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 21 | S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO, /* a */ |
Denis Vlasenko | 86724af | 2007-01-26 22:54:01 +0000 | [diff] [blame] | 22 | S_ISUID | S_IRWXU, /* u */ |
| 23 | S_ISGID | S_IRWXG, /* g */ |
| 24 | S_IRWXO /* o */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 25 | }; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 26 | static const mode_t perm_mask[] = { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 27 | 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 | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 30 | S_IXUSR | S_IXGRP | S_IXOTH, /* X -- special -- see below */ |
Denis Vlasenko | 86724af | 2007-01-26 22:54:01 +0000 | [diff] [blame] | 31 | S_ISUID | S_ISGID, /* s */ |
| 32 | S_ISVTX /* t */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 33 | }; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 34 | static const char who_chars[] ALIGN1 = "augo"; |
| 35 | static const char perm_chars[] ALIGN1 = "rwxXst"; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 36 | |
| 37 | const char *p; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 38 | mode_t wholist; |
| 39 | mode_t permlist; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 40 | mode_t new_mode; |
| 41 | char op; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 42 | |
Denys Vlasenko | 1f27ab0 | 2009-09-23 17:17:53 +0200 | [diff] [blame] | 43 | if ((unsigned char)(*s - '0') < 8) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 44 | unsigned long tmp; |
| 45 | char *e; |
| 46 | |
Denis Vlasenko | 86724af | 2007-01-26 22:54:01 +0000 | [diff] [blame] | 47 | tmp = strtoul(s, &e, 8); |
Manuel Novoa III | ea4c434 | 2003-03-19 18:09:03 +0000 | [diff] [blame] | 48 | if (*e || (tmp > 07777U)) { /* Check range and trailing chars. */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 49 | return 0; |
| 50 | } |
| 51 | *current_mode = tmp; |
| 52 | return 1; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 55 | new_mode = *current_mode; |
| 56 | |
Denis Vlasenko | 86724af | 2007-01-26 22:54:01 +0000 | [diff] [blame] | 57 | /* Note: we allow empty clauses, and hence empty modes. |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 58 | * We treat an empty mode as no change to perms. */ |
| 59 | |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 60 | while (*s) { /* Process clauses. */ |
| 61 | if (*s == ',') { /* We allow empty clauses. */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 62 | ++s; |
| 63 | continue; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 64 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 65 | |
| 66 | /* Get a wholist. */ |
| 67 | wholist = 0; |
Denis Vlasenko | 86724af | 2007-01-26 22:54:01 +0000 | [diff] [blame] | 68 | WHO_LIST: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 69 | p = who_chars; |
| 70 | do { |
| 71 | if (*p == *s) { |
| 72 | wholist |= who_mask[(int)(p-who_chars)]; |
| 73 | if (!*++s) { |
| 74 | return 0; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 75 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 76 | goto WHO_LIST; |
| 77 | } |
| 78 | } while (*++p); |
| 79 | |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 80 | do { /* Process action list. */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 81 | if ((*s != '+') && (*s != '-')) { |
| 82 | if (*s != '=') { |
| 83 | return 0; |
| 84 | } |
| 85 | /* Since op is '=', clear all bits corresponding to the |
Denis Vlasenko | 86724af | 2007-01-26 22:54:01 +0000 | [diff] [blame] | 86 | * wholist, or all file bits if wholist is empty. */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 87 | permlist = ~FILEMODEBITS; |
| 88 | if (wholist) { |
| 89 | permlist = ~wholist; |
| 90 | } |
| 91 | new_mode &= permlist; |
| 92 | } |
| 93 | op = *s++; |
| 94 | |
| 95 | /* Check for permcopy. */ |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 96 | p = who_chars + 1; /* Skip 'a' entry. */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 97 | do { |
| 98 | if (*p == *s) { |
| 99 | int i = 0; |
| 100 | permlist = who_mask[(int)(p-who_chars)] |
| 101 | & (S_IRWXU | S_IRWXG | S_IRWXO) |
| 102 | & new_mode; |
| 103 | do { |
| 104 | if (permlist & perm_mask[i]) { |
| 105 | permlist |= perm_mask[i]; |
| 106 | } |
| 107 | } while (++i < 3); |
| 108 | ++s; |
| 109 | goto GOT_ACTION; |
| 110 | } |
| 111 | } while (*++p); |
| 112 | |
| 113 | /* It was not a permcopy, so get a permlist. */ |
| 114 | permlist = 0; |
Denis Vlasenko | 86724af | 2007-01-26 22:54:01 +0000 | [diff] [blame] | 115 | PERM_LIST: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 116 | p = perm_chars; |
| 117 | do { |
| 118 | if (*p == *s) { |
| 119 | if ((*p != 'X') |
Denis Vlasenko | 86724af | 2007-01-26 22:54:01 +0000 | [diff] [blame] | 120 | || (new_mode & (S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH)) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 121 | ) { |
| 122 | permlist |= perm_mask[(int)(p-perm_chars)]; |
| 123 | } |
| 124 | if (!*++s) { |
| 125 | break; |
| 126 | } |
| 127 | goto PERM_LIST; |
| 128 | } |
| 129 | } while (*++p); |
Denis Vlasenko | 86724af | 2007-01-26 22:54:01 +0000 | [diff] [blame] | 130 | GOT_ACTION: |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 131 | if (permlist) { /* The permlist was nonempty. */ |
Denis Vlasenko | 86724af | 2007-01-26 22:54:01 +0000 | [diff] [blame] | 132 | mode_t tmp = wholist; |
| 133 | if (!wholist) { |
| 134 | mode_t u_mask = umask(0); |
| 135 | umask(u_mask); |
| 136 | tmp = ~u_mask; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 137 | } |
| 138 | permlist &= tmp; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 139 | if (op == '-') { |
| 140 | new_mode &= ~permlist; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 141 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 142 | new_mode |= permlist; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 143 | } |
| 144 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 145 | } while (*s && (*s != ',')); |
| 146 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 147 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 148 | *current_mode = new_mode; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 149 | return 1; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 150 | } |