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 | * |
"Robert P. J. Day" | 5d8843e | 2006-07-10 11:41:19 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
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 <stdlib.h> |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 13 | #include <assert.h> |
| 14 | #include <sys/stat.h> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 15 | #include "libbb.h" |
| 16 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 17 | #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] | 18 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 19 | int bb_parse_mode(const char *s, mode_t *current_mode) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 20 | { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 21 | static const mode_t who_mask[] = { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 22 | S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO, /* a */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 23 | S_ISUID | S_IRWXU, /* u */ |
| 24 | S_ISGID | S_IRWXG, /* g */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 25 | S_IRWXO /* o */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 26 | }; |
| 27 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 28 | static const mode_t perm_mask[] = { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 29 | S_IRUSR | S_IRGRP | S_IROTH, /* r */ |
| 30 | S_IWUSR | S_IWGRP | S_IWOTH, /* w */ |
| 31 | S_IXUSR | S_IXGRP | S_IXOTH, /* x */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 32 | S_IXUSR | S_IXGRP | S_IXOTH, /* X -- special -- see below */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 33 | S_ISUID | S_ISGID, /* s */ |
| 34 | S_ISVTX /* t */ |
| 35 | }; |
| 36 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 37 | static const char who_chars[] = "augo"; |
| 38 | static const char perm_chars[] = "rwxXst"; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 39 | |
| 40 | const char *p; |
| 41 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 42 | mode_t wholist; |
| 43 | mode_t permlist; |
| 44 | mode_t mask; |
| 45 | mode_t new_mode; |
| 46 | char op; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 47 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 48 | assert(s); |
| 49 | |
| 50 | if (((unsigned int)(*s - '0')) < 8) { |
| 51 | unsigned long tmp; |
| 52 | char *e; |
| 53 | |
| 54 | tmp = strtol(s, &e, 8); |
Manuel Novoa III | ea4c434 | 2003-03-19 18:09:03 +0000 | [diff] [blame] | 55 | if (*e || (tmp > 07777U)) { /* Check range and trailing chars. */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 56 | return 0; |
| 57 | } |
| 58 | *current_mode = tmp; |
| 59 | return 1; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 62 | mask = umask(0); |
| 63 | umask(mask); |
| 64 | |
| 65 | new_mode = *current_mode; |
| 66 | |
| 67 | /* Note: We allow empty clauses, and hence empty modes. |
| 68 | * We treat an empty mode as no change to perms. */ |
| 69 | |
| 70 | while (*s) { /* Process clauses. */ |
| 71 | |
| 72 | if (*s == ',') { /* We allow empty clauses. */ |
| 73 | ++s; |
| 74 | continue; |
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 | |
| 77 | /* Get a wholist. */ |
| 78 | wholist = 0; |
| 79 | |
| 80 | WHO_LIST: |
| 81 | p = who_chars; |
| 82 | do { |
| 83 | if (*p == *s) { |
| 84 | wholist |= who_mask[(int)(p-who_chars)]; |
| 85 | if (!*++s) { |
| 86 | return 0; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 87 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 88 | goto WHO_LIST; |
| 89 | } |
| 90 | } while (*++p); |
| 91 | |
| 92 | do { /* Process action list. */ |
| 93 | if ((*s != '+') && (*s != '-')) { |
| 94 | if (*s != '=') { |
| 95 | return 0; |
| 96 | } |
| 97 | /* Since op is '=', clear all bits corresponding to the |
| 98 | * wholist, of all file bits if wholist is empty. */ |
| 99 | permlist = ~FILEMODEBITS; |
| 100 | if (wholist) { |
| 101 | permlist = ~wholist; |
| 102 | } |
| 103 | new_mode &= permlist; |
| 104 | } |
| 105 | op = *s++; |
| 106 | |
| 107 | /* Check for permcopy. */ |
| 108 | p = who_chars + 1; /* Skip 'a' entry. */ |
| 109 | do { |
| 110 | if (*p == *s) { |
| 111 | int i = 0; |
| 112 | permlist = who_mask[(int)(p-who_chars)] |
| 113 | & (S_IRWXU | S_IRWXG | S_IRWXO) |
| 114 | & new_mode; |
| 115 | do { |
| 116 | if (permlist & perm_mask[i]) { |
| 117 | permlist |= perm_mask[i]; |
| 118 | } |
| 119 | } while (++i < 3); |
| 120 | ++s; |
| 121 | goto GOT_ACTION; |
| 122 | } |
| 123 | } while (*++p); |
| 124 | |
| 125 | /* It was not a permcopy, so get a permlist. */ |
| 126 | permlist = 0; |
| 127 | |
| 128 | PERM_LIST: |
| 129 | p = perm_chars; |
| 130 | do { |
| 131 | if (*p == *s) { |
| 132 | if ((*p != 'X') |
| 133 | || (new_mode & (S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH)) |
| 134 | ) { |
| 135 | permlist |= perm_mask[(int)(p-perm_chars)]; |
| 136 | } |
| 137 | if (!*++s) { |
| 138 | break; |
| 139 | } |
| 140 | goto PERM_LIST; |
| 141 | } |
| 142 | } while (*++p); |
| 143 | |
| 144 | GOT_ACTION: |
| 145 | if (permlist) { /* The permlist was nonempty. */ |
| 146 | mode_t tmp = ~mask; |
| 147 | if (wholist) { |
| 148 | tmp = wholist; |
| 149 | } |
| 150 | permlist &= tmp; |
| 151 | |
| 152 | if (op == '-') { |
| 153 | new_mode &= ~permlist; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 154 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 155 | new_mode |= permlist; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 158 | } while (*s && (*s != ',')); |
| 159 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 160 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 161 | *current_mode = new_mode; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 162 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 163 | return 1; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 164 | } |