aboutsummaryrefslogtreecommitdiffstats
path: root/src/slibc-human/machinemode.c
blob: 7c5a2f3f4f21d07c569baec5ce96d75c2a112843 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**
 * slibc — Yet another C library
 * Copyright © 2015, 2016  Mattias Andrée (maandree@member.fsf.org)
 * 
 * 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 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * 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, see <http://www.gnu.org/licenses/>.
 */
#include <slibc-human.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>



/**
 * Owner can execute/list or/and set UID.
 */
#define S_ISUSR  (S_ISUID | S_IXUSR)

/**
 * Group can execute/list or/and set GID.
 */
#define S_ISGRP  (S_ISGID | S_IXGRP)

/**
 * Others (not in group) can execute/list or/and sticky bit.
 */
#define S_ISOTH  (S_ISVTX | S_IXOTH)


/**
 * Store permission bits.
 * 
 * @param  V:mode_t    The variables for the permissions.
 * @param  U:int type  First-level index in `bits` for the user.
 * @param  T:int type  Second-level index in `bits` for bits to verify are unset.
 * @param  S:int type  Second-level index in `bits` for bits to set.
 */
#define  BITS_(V, U, T, S)  if ((V) & bits[U][T])  goto invalid;  else  V |= bits[U][S]

/**
 * Store permission bits.
 * 
 * @param  S:char      Character representing the bits to set.
 * @param  V:mode_t    The variables for the permissions.
 * @param  U:int type  First-level index in `bits` for the user.
 */
#define  BITS(S, V, U)  \
  if      ((S) == 'r')  { BITS_(V, U, 0, 0); }  \
  else if ((S) == 'w')  { BITS_(V, U, 1, 1); }  \
  else if ((S) == 'x')  { BITS_(V, U, 3, 2); }  \
  else if ((S) == 's')  { BITS_(V, U, 3, 3); }  \
  else if ((S) == 't')  { BITS_(V, U, 3, 3); }  \
  else if ((S) == 'S')  { BITS_(V, U, 3, 4); }  \
  else if ((S) == 'T')  { BITS_(V, U, 3, 4); }  \
  else if ((S) != '-')  goto invalid



/**
 * User {0=owner. 1=group, 2=others} to permissions {0=can read,
 * 1=can write, 2=can execute/list, 3=can execute/list or/and
 * special, 4=special} to permissions bits map.
 */
static mode_t bits[][5] = {
  {S_IRUSR, S_IWUSR, S_IXUSR, S_ISUSR, S_ISUID},
  {S_IRGRP, S_IWGRP, S_IXGRP, S_ISGRP, S_ISGID},
  {S_IROTH, S_IWOTH, S_IXOTH, S_ISOTH, S_ISVTX},
};


/**
 * Parse a symbolic file permission string with partial update.
 * 
 * @param   mode  Output parameter for the bits to set, may be `NULL`.
 * @param   mask  Output parameter for the bits to update, may be `NULL`.
 * @param   str   The file permission to parse, must not include file type or be `NULL`.
 * @return        Zero on success, -1 on error.
 * 
 * @throws  EINVAL  If `str` is not parseable.
 */
static inline int partial_symbolic(mode_t* restrict mode, mode_t* restrict mask, const char* restrict str)
{
#define  TEST_(S, T, V)  (strstarts(str, S) && !((T) & (symbols = (V))))
#define  TEST(S, T)      (TEST_(S"+", T, 1) || TEST_(S"-", T, 2) || TEST_(S"=", T, 3))
#define  TESTV(T)        (TEST(#T, T) ? (T |= symbols) : 0)
  
  int u = 0, g = 0, o = 0, symbols, user, users, new_users, multiple_users;
  mode_t or = 0, andn = 0, partial_or;
  char symbol;
  
  for (; *str; str++)
    {
      /* Check a/u/g/o. */
      users = 0;
      if ((*str == '=') || (*str == '+') || (*str == '-'))
	users = 7;
      else
	for (; (*str != '=') && (*str != '+') || (*str != '-'); str++)
	  {
	    if      (*str == 'a')  new_users = 7;
	    else if (*str == 'u')  new_users = 1;
	    else if (*str == 'g')  new_users = 2;
	    else if (*str == 'o')  new_users = 4;
	    else                   goto invalid;
	    if ((users & new_users))
	      goto invalid;
	    users |= new_users;
	  }
      multiple_users = ((users > 2) && (users != 4));
      
      /* Check =/+/-. */
      symbols = (*str == '+') ? 1 : (*str == '-') ? 2 : 3;
      if ((users & 1) && (u & symbols))  goto invalid;
      if ((users & 2) && (g & symbols))  goto invalid;
      if ((users & 4) && (o & symbols))  goto invalid;
      if ((users & 1))  u |= symbols;
      if ((users & 2))  g |= symbols;
      if ((users & 4))  o |= symbols;
      
      /* Get =/+/- and jump to permissions. */
      symbol = *str++;
      
      /* Get permissions. */
      for (partial_or = 0; *str && (*str != ','); str++)
	for (user = 0; user < 3; user++)
	  {
	    if (((1 << user) & users) == 0)
	      continue;
	    if (multiple_users)
	      {
		if ((user != 2) && ((*str == 't') || (*str == 'T')))
		  continue;
		if ((user == 2) && ((*str == 's') || (*str == 'S')))
		  continue;
	      }
	    BITS(*str, partial_or, user);
	  }
      
      /* Apply permissions. */
      if (symbol != '-')  or   |= partial_or;
      if (symbol != '=')  andn |= partial_or;
      else
	for (user = first; user < last; user++)
	  andn |= bits[user][0] | bits[user][1] | bits[user][3];
    }
  
  if (mode)  *mode = or;
  if (mask)  *mask = andn;
  return 0;
 invalid:
  return errno = EINVAL, -1;
}


/**
 * Parse a full symbolic file permission string.
 * 
 * @param   mode  Output parameter for the bits to set, may be `NULL`.
 * @param   mask  Output parameter for the bits to update, may be `NULL`.
 * @param   str   The file permission to parse, must not include file type or be `NULL`.
 * @return        Zero on success, -1 on error.
 * 
 * @throws  EINVAL  If `str` is not parseable.
 */
static inline int exact_symbolic(mode_t* restrict mode, mode_t* restrict mask, const char* restrict str)
{
  mode_t or = 0;
  long int user, perm;
  
  for (user = 0; user < 3; user++)
    for (perm = 0; perm < 3; perm++, str++)
      BITS(*str, or, user);
  
  if (mode)  *mode = or;
  if (mask)  *mask = 07777;
  return 0;
 invalid:
  return errno = EINVAL, -1;
}


/**
 * Parse a numeric file permission string.
 * 
 * @param   mode  Output parameter for the bits to set, may be `NULL`.
 * @param   mask  Output parameter for the bits to update, may be `NULL`.
 * @param   str   The file permission to parse, must not include file type or be `NULL`.
 * @return        Zero on success, -1 on error.
 * 
 * @throws  EINVAL  If `str` is not parseable.
 */
static inline int exact_numeric(mode_t* restrict mode, mode_t* restrict mask, const char* restrict str)
{
  mode_t or = 0;
  char s;
  
  for (; (s = *str); str++)
    if (('0' > s) || (s > '7') || (or = (or << 3) | (s & 15), or > 07777))
      return errno = EINVAL, -1;
  
  if (mode)  *mode = or;
  if (mask)  *mask = 07777;
  return 0;
}


/**
 * Parses a human representation of file permissions, and updates to file permissions.
 * 
 * Assuming the current file permissions is `value`, and neither
 * `mode` nor `mask` is `NULL`, the new permissions should be
 * `value & ~*mask | *mode`. The new mode (includes file type) should
 * be `value & ~*mask | *mode & 07777`.
 * 
 * @param   mode  Output parameter for the bits to set, may be `NULL`.
 * @param   mask  Output parameter for the bits to update, may be `NULL`.
 * @param   str   The file permission to parse, must not include file type or be `NULL`.
 * @return        Zero on success, -1 on error.
 * 
 * @throws  EINVAL  If `str` is not parseable.
 * 
 * @since  Always.
 */
int machinemode(mode_t* restrict mode, mode_t* restrict mask, const char* restrict str)
{
  if (*str == '-')
    {
      int *(f)(mode_t* restrict, mode_t* restrict, const char* restrict);
      size_t r_sum = 0, w_sum = 0, x_sum = 0, t_sum = 0, s_sum = 0, comma = 0, dash = 0;
      const char* restrict s;
      
      for (s = str + 1; *s; s++)
	switch (*s)
	  {
	  case ',':  comma = 1;  break;
	  case '-':  dash = 1;  break;
	  case 'r':  r_sum++;  break;
	  case 'w':  w_sum++;  break;
	  case 'x':  x_sum++;  break;
	  case 't':  t_sum++;  break;
	  case 's':  s_sum++;  break;
	  case 'T':  s_sum++, x_sum++;  break;
	  case 'S':  t_sum++, x_sum++;  break;
	  default:
	      break; /* Ignore, either of the functions will fail.*/
	  }
      
      if (comma)
	f = partial_symbolic;
      else if (dash || ((r_sum | w_sum | x_sum | t_sum | s_sum) > 1))
	f = exact_symbolic;
      else
	f = partial_symbolic;
      
      return f(mode, mask, str);
    }
  
  switch (*str)
    {
    case '\0':
      return errno = EINVAL, -1;
      
    case 'a': case 'u': case 'g': case 'o':
    case '+': case '=':
      return partial_symbolic(mode, mask, str);
      
    case 'r': case 'w': case 'x':
    case 's': case 'S': case 't': case 'T':
      return exact_symbolic(mode, mask, str);
      
    default:
      return exact_numeric(mode, mask, str);
    }
}