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 "libbb.h" |
| 13 | |
Denis Vlasenko | 86724af | 2007-01-26 22:54:01 +0000 | [diff] [blame^] | 14 | #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] | 15 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 16 | int bb_parse_mode(const char *s, mode_t *current_mode) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 17 | { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 18 | static const mode_t who_mask[] = { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 19 | 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^] | 20 | S_ISUID | S_IRWXU, /* u */ |
| 21 | S_ISGID | S_IRWXG, /* g */ |
| 22 | S_IRWXO /* o */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 23 | }; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 24 | static const mode_t perm_mask[] = { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 25 | S_IRUSR | S_IRGRP | S_IROTH, /* r */ |
| 26 | S_IWUSR | S_IWGRP | S_IWOTH, /* w */ |
| 27 | S_IXUSR | S_IXGRP | S_IXOTH, /* x */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 28 | S_IXUSR | S_IXGRP | S_IXOTH, /* X -- special -- see below */ |
Denis Vlasenko | 86724af | 2007-01-26 22:54:01 +0000 | [diff] [blame^] | 29 | S_ISUID | S_ISGID, /* s */ |
| 30 | S_ISVTX /* t */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 31 | }; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 32 | static const char who_chars[] = "augo"; |
| 33 | static const char perm_chars[] = "rwxXst"; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 34 | |
| 35 | const char *p; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 36 | mode_t wholist; |
| 37 | mode_t permlist; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 38 | mode_t new_mode; |
| 39 | char op; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 40 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 41 | if (((unsigned int)(*s - '0')) < 8) { |
| 42 | unsigned long tmp; |
| 43 | char *e; |
| 44 | |
Denis Vlasenko | 86724af | 2007-01-26 22:54:01 +0000 | [diff] [blame^] | 45 | tmp = strtoul(s, &e, 8); |
Manuel Novoa III | ea4c434 | 2003-03-19 18:09:03 +0000 | [diff] [blame] | 46 | if (*e || (tmp > 07777U)) { /* Check range and trailing chars. */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 47 | return 0; |
| 48 | } |
| 49 | *current_mode = tmp; |
| 50 | return 1; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 53 | new_mode = *current_mode; |
| 54 | |
Denis Vlasenko | 86724af | 2007-01-26 22:54:01 +0000 | [diff] [blame^] | 55 | /* Note: we allow empty clauses, and hence empty modes. |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 56 | * We treat an empty mode as no change to perms. */ |
| 57 | |
| 58 | while (*s) { /* Process clauses. */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 59 | if (*s == ',') { /* We allow empty clauses. */ |
| 60 | ++s; |
| 61 | continue; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 62 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 63 | |
| 64 | /* Get a wholist. */ |
| 65 | wholist = 0; |
Denis Vlasenko | 86724af | 2007-01-26 22:54:01 +0000 | [diff] [blame^] | 66 | WHO_LIST: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 67 | p = who_chars; |
| 68 | do { |
| 69 | if (*p == *s) { |
| 70 | wholist |= who_mask[(int)(p-who_chars)]; |
| 71 | if (!*++s) { |
| 72 | return 0; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 73 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 74 | goto WHO_LIST; |
| 75 | } |
| 76 | } while (*++p); |
| 77 | |
| 78 | do { /* Process action list. */ |
| 79 | if ((*s != '+') && (*s != '-')) { |
| 80 | if (*s != '=') { |
| 81 | return 0; |
| 82 | } |
| 83 | /* Since op is '=', clear all bits corresponding to the |
Denis Vlasenko | 86724af | 2007-01-26 22:54:01 +0000 | [diff] [blame^] | 84 | * wholist, or all file bits if wholist is empty. */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 85 | permlist = ~FILEMODEBITS; |
| 86 | if (wholist) { |
| 87 | permlist = ~wholist; |
| 88 | } |
| 89 | new_mode &= permlist; |
| 90 | } |
| 91 | op = *s++; |
| 92 | |
| 93 | /* Check for permcopy. */ |
| 94 | p = who_chars + 1; /* Skip 'a' entry. */ |
| 95 | 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 Vlasenko | 86724af | 2007-01-26 22:54:01 +0000 | [diff] [blame^] | 113 | PERM_LIST: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 114 | p = perm_chars; |
| 115 | do { |
| 116 | if (*p == *s) { |
| 117 | if ((*p != 'X') |
Denis Vlasenko | 86724af | 2007-01-26 22:54:01 +0000 | [diff] [blame^] | 118 | || (new_mode & (S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH)) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 119 | ) { |
| 120 | permlist |= perm_mask[(int)(p-perm_chars)]; |
| 121 | } |
| 122 | if (!*++s) { |
| 123 | break; |
| 124 | } |
| 125 | goto PERM_LIST; |
| 126 | } |
| 127 | } while (*++p); |
Denis Vlasenko | 86724af | 2007-01-26 22:54:01 +0000 | [diff] [blame^] | 128 | GOT_ACTION: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 129 | if (permlist) { /* The permlist was nonempty. */ |
Denis Vlasenko | 86724af | 2007-01-26 22:54:01 +0000 | [diff] [blame^] | 130 | 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 | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 135 | } |
| 136 | permlist &= tmp; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 137 | if (op == '-') { |
| 138 | new_mode &= ~permlist; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 139 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 140 | new_mode |= permlist; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 143 | } while (*s && (*s != ',')); |
| 144 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 145 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 146 | *current_mode = new_mode; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 147 | return 1; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 148 | } |