summaryrefslogtreecommitdiff
path: root/builtins/umask.def
blob: d3488bf903ce4acdc8d2523dd19012543a50095e (plain)
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
This file is umask.def, from which is created umask.c.
It implements the builtin "umask" in Bash.

Copyright (C) 1987-2022 Free Software Foundation, Inc.

This file is part of GNU Bash, the Bourne Again SHell.

Bash 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.

Bash 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 Bash.  If not, see <http://www.gnu.org/licenses/>.

$PRODUCES umask.c

$BUILTIN umask
$FUNCTION umask_builtin
$SHORT_DOC umask [-p] [-S] [mode]
Display or set file mode mask.

Sets the user file-creation mask to MODE.  If MODE is omitted, prints
the current value of the mask.

If MODE begins with a digit, it is interpreted as an octal number;
otherwise it is a symbolic mode string like that accepted by chmod(1).

Options:
  -p	if MODE is omitted, output in a form that may be reused as input
  -S	makes the output symbolic; otherwise an octal number is output

Exit Status:
Returns success unless MODE is invalid or an invalid option is given.
$END

#include <config.h>

#include "../bashtypes.h"
#include "filecntl.h"
#if ! defined(_MINIX) && defined (HAVE_SYS_FILE_H)
#  include <sys/file.h>
#endif

#if defined (HAVE_UNISTD_H)
#include <unistd.h>
#endif

#include <stdio.h>
#include <chartypes.h>

#include "../bashintl.h"

#include "../shell.h"
#include "posixstat.h"
#include "common.h"
#include "bashgetopt.h"

/* **************************************************************** */
/*                                                                  */
/*                     UMASK Builtin and Helpers                    */
/*                                                                  */
/* **************************************************************** */

static void print_symbolic_umask (mode_t);
static int symbolic_umask (WORD_LIST *);

/* Set or display the mask used by the system when creating files.  Flag
   of -S means display the umask in a symbolic mode. */
int
umask_builtin (WORD_LIST *list)
{
  int print_symbolically, opt, umask_value, pflag;
  mode_t umask_arg;

  print_symbolically = pflag = 0;
  reset_internal_getopt ();
  while ((opt = internal_getopt (list, "Sp")) != -1)
    {
      switch (opt)
	{
	case 'S':
	  print_symbolically++;
	  break;
	case 'p':
	  pflag++;
	  break;
	CASE_HELPOPT;
	default:
	  builtin_usage ();
	  return (EX_USAGE);
	}
    }

  list = loptend;

  if (list)
    {
      if (DIGIT (*list->word->word))
	{
	  umask_value = read_octal (list->word->word);

	  /* Note that other shells just let you set the umask to zero
	     by specifying a number out of range.  This is a problem
	     with those shells.  We don't change the umask if the input
	     is lousy. */
	  if (umask_value == -1)
	    {
	      sh_erange (list->word->word, _("octal number"));
	      return (EXECUTION_FAILURE);
	    }
	}
      else
	{
	  umask_value = symbolic_umask (list);
	  if (umask_value == -1)
	    return (EXECUTION_FAILURE);
	}
      umask_arg = (mode_t)umask_value;
      umask (umask_arg);
      if (print_symbolically)
	print_symbolic_umask (umask_arg);
    }
  else				/* Display the UMASK for this user. */
    {
      umask_arg = umask (022);
      umask (umask_arg);

      if (pflag)
	printf ("umask%s ", (print_symbolically ? " -S" : ""));
      if (print_symbolically)
	print_symbolic_umask (umask_arg);
      else
	printf ("%04lo\n", (unsigned long)umask_arg);
    }

  return (sh_chkwrite (EXECUTION_SUCCESS));
}

/* Print the umask in a symbolic form.  In the output, a letter is
   printed if the corresponding bit is clear in the umask. */
static void
print_symbolic_umask (mode_t um)
{
  char ubits[4], gbits[4], obits[4];		/* u=rwx,g=rwx,o=rwx */
  int i;

  i = 0;
  if ((um & S_IRUSR) == 0)
    ubits[i++] = 'r';
  if ((um & S_IWUSR) == 0)
    ubits[i++] = 'w';
  if ((um & S_IXUSR) == 0)
    ubits[i++] = 'x';
  ubits[i] = '\0';

  i = 0;
  if ((um & S_IRGRP) == 0)
    gbits[i++] = 'r';
  if ((um & S_IWGRP) == 0)
    gbits[i++] = 'w';
  if ((um & S_IXGRP) == 0)
    gbits[i++] = 'x';
  gbits[i] = '\0';

  i = 0;
  if ((um & S_IROTH) == 0)
    obits[i++] = 'r';
  if ((um & S_IWOTH) == 0)
    obits[i++] = 'w';
  if ((um & S_IXOTH) == 0)
    obits[i++] = 'x';
  obits[i] = '\0';

  printf ("u=%s,g=%s,o=%s\n", ubits, gbits, obits);
}

static inline mode_t
copyuser (mode_t mask)
{
  return ((mask & S_IRUSR) ? S_IRUGO : 0) |
	 ((mask & S_IWUSR) ? S_IWUGO : 0) |
	 ((mask & S_IXUSR) ? S_IXUGO : 0);
}

static inline mode_t
copygroup (mode_t mask)
{
  return ((mask & S_IRGRP) ? S_IRUGO : 0) |
	 ((mask & S_IWGRP) ? S_IWUGO : 0) |
	 ((mask & S_IXGRP) ? S_IXUGO : 0);
}

static inline mode_t
copyother (mode_t mask)
{
  return ((mask & S_IROTH) ? S_IRUGO : 0) |
	 ((mask & S_IWOTH) ? S_IWUGO : 0) |
	 ((mask & S_IXOTH) ? S_IXUGO : 0);
}

int
parse_symbolic_mode (char *mode, mode_t initial_bits)
{
  char op, c;
  mode_t who, perm, bits;
  char *s;

  for (s = mode, bits = initial_bits;;)
    {
      who = 0;
      op = 0;

      /* Parse the `who' portion of the symbolic mode clause. */
      while (member (*s, "agou"))
	{
	  switch (c = *s++)
	    {
	    case 'u':
	      who |= S_IRWXU;
	      continue;
	    case 'g':
	      who |= S_IRWXG;
	      continue;
	    case 'o':
	      who |= S_IRWXO;
	      continue;
	    case 'a':
	      who |= S_IRWXU | S_IRWXG | S_IRWXO;
	      continue;
	    default:
	      break;
	    }
	}

      /* default `who' is `a' */
      if (who == 0)
	who = S_IRWXU | S_IRWXG | S_IRWXO;

      /* The operation is now sitting in *s. */
start_op:
      perm = 0;
      op = *s++;
      switch (op)
	{
	case '+':
	case '-':
	case '=':
	  break;
	default:
	  builtin_error (_("`%c': invalid symbolic mode operator"), op);
	  return (-1);
	}

      /* Parse out the `action' section of the symbolic mode clause. An
	 action can be a set of permissions (rwxXst), a copy specification
	 (ugo), or another op (+-=). */
      while (member (*s, "rwxXstugo"))
	{
	  switch (*s)
	    {
	    /* First the copy specification */
	    case 'u':
	      perm = copyuser (initial_bits);
	      break;
	    case 'g':
	      perm = copygroup (initial_bits);
	      break;
	    case 'o':
	      perm = copyother (initial_bits);
	      break;

	    /* Then the permissions. */
	    case 'r':
	      perm |= S_IRUGO;
	      break;
	    case 'w':
	      perm |= S_IWUGO;
	      break;
	    case 'X':
	      /* for chmod, this includes S_ISDIR but that doesn't make sense here */
	      if ((initial_bits & S_IXUGO) == 0)
		break;		/* no-op if original mask doesn't include execute bits */
	      /* FALLTHROUGH */
	    case 'x':
	      perm |= S_IXUGO;
	      break;
	    case 's':
#ifdef S_ISUID
	      perm |= S_ISUID | S_ISGID;
	      break;
#else
	      goto spec_error;
#endif
	    case 't':
#ifdef S_ISVTX
	      perm |= S_ISVTX;
	      break;
#else
	      goto spec_error;
#endif
	    }
	  s++;
	}

      /* Now perform the operation or return an error for a
	 bad permission string. */
      perm &= who;

      switch (op)
	{
	case '+':
	  bits |= perm;
	  break;
	case '-':
	  bits &= ~perm;
	  break;
	case '=':
	  bits &= ~who;
	  bits |= perm;
	  break;
	}

      /* Break if the end of the action string, loop if we're going to parse
	 another `who', go back to parsing another op if we have an op spec
	 (+-=). Return an invalid mode character error for everything else. */
      if (*s == '\0')
	break;
      else if (*s == ',')
	s++;	/* skip past ',' */
      else if (*s == '+' || *s == '-' || *s == '=')
	goto start_op;
      else
	{
spec_error:
	  builtin_error (_("`%c': invalid symbolic mode character"), *s);
	  return (-1);
	}
    }

  return (bits);
}

/* Set the umask from a symbolic mode string similar to that accepted
   by chmod.  If the -S argument is given, then print the umask in a
   symbolic form. */
static int
symbolic_umask (WORD_LIST *list)
{
  mode_t um;
  int bits;

  /* Get the initial umask.  Don't change it yet. */
  um = umask (022);
  umask (um);

  /* All work is done with the complement of the umask -- it's
     more intuitive and easier to deal with.  It is complemented
     again before being returned. */
  bits = parse_symbolic_mode (list->word->word, ~um & 0777);
  if (bits == -1)
    return (-1);

  um = ~bits & 0777;
  return (um);
}