Welcome to mirror list, hosted at ThFree Co, Russian Federation.

setfacl.c « utils « winsup - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 996b5ec7f7848eb1a355863e85a432c84d86439c (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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/* setfacl.c

   Copyright 2000, 2001, 2002, 2003 Red Hat Inc.

   Written by Corinna Vinschen <vinschen@redhat.com>

This file is part of Cygwin.

This software is a copyrighted work licensed under the terms of the
Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
details. */

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <pwd.h>
#include <grp.h>
#include <sys/types.h>
#include <sys/acl.h>

#ifndef BOOL
#define BOOL int
#endif

#ifndef TRUE
#define TRUE (1)
#endif

#ifndef FALSE
#define FALSE (0)
#endif

#ifndef ILLEGAL_MODE
#define ILLEGAL_MODE ((mode_t)0xffffffff)
#endif

static const char version[] = "$Revision$";
static char *prog_name;

typedef enum {
  NoAction,
  Set,
  Modify,
  Delete,
  ModNDel,
  SetFromFile
} action_t;

mode_t getperm (char *in)
{
  if (isdigit (*in) && !in[1])
    {
      int i = atoi (in);
      if (i < 0 || i > 7)
        return ILLEGAL_MODE;
      return i << 6 | i << 3 | i;
    }
  if (strlen (in) > 3 && strchr (" \t\n\r#", in[3]))
    in[3] = '\0';
  if (strlen (in) != 3)
    return ILLEGAL_MODE;
  if (!strchr ("r-", in[0])
      || !strchr ("w-", in[1])
      || !strchr ("x-", in[2]))
    return ILLEGAL_MODE;
  return (in[0] == 'r' ? S_IROTH : 0)
         | (in[1] == 'w' ? S_IWOTH : 0)
         | (in[2] == 'x' ? S_IXOTH : 0);
}

BOOL
getaclentry (action_t action, char *c, aclent_t *ace)
{
  char *c2;

  ace->a_type = 0;
  ace->a_id = -1;
  ace->a_perm = 0;

  if (!strncmp (c, "default:", 8)
      || !strncmp (c, "d:", 2))
    {
      ace->a_type = ACL_DEFAULT;
      c = strchr (c, ':') + 1;
    }
  if (!strncmp (c, "user:", 5)
      || !strncmp (c, "u:", 2))
    {
      ace->a_type |= USER_OBJ;
      c = strchr (c, ':') + 1;
    }
  else if (!strncmp (c, "group:", 6)
           || !strncmp (c, "g:", 2))
    {
      ace->a_type |= GROUP_OBJ;
      c = strchr (c, ':') + 1;
    }
  else if (!strncmp (c, "mask:", 5)
           || !strncmp (c, "m:", 2))
    {
      ace->a_type |= CLASS_OBJ;
      c = strchr (c, ':') + 1;
    }
  else if (!strncmp (c, "other:", 6)
           || !strncmp (c, "o:", 2))
    {
      ace->a_type |= OTHER_OBJ;
      c = strchr (c, ':') + 1;
    }
  else
    return FALSE;
  if (ace->a_type & (USER_OBJ | GROUP_OBJ))
    {
      if ((c2 = strchr (c, ':')))
        {
          if (action == Delete)
            return FALSE;
          *c2 = '\0';
        }
      else if (action != Delete)
        return FALSE;
      if (c2 == c)
        {
          if (action == Delete)
            return FALSE;
        }
      else if (isdigit (*c))
        {
          char *c3;

          ace->a_id = strtol (c, &c3, 10);
          if (*c3)
            return FALSE;
        }
      else if (ace->a_type & USER_OBJ)
        {
          struct passwd *pw = getpwnam (c);
          if (!pw)
            return FALSE;
          ace->a_id = pw->pw_uid;
        }
      else
        {
          struct group *gr = getgrnam (c);
          if (!gr)
            return FALSE;
          ace->a_id = gr->gr_gid;
        }
      if (c2 != c)
        {
	  if (ace->a_type & USER_OBJ)
	    {
	      ace->a_type &= ~USER_OBJ;
	      ace->a_type |= USER;
	    }
	  else
	    {
	      ace->a_type &= ~GROUP_OBJ;
	      ace->a_type |= GROUP;
	    }
	}
      if (c2)
        c = c2 + 1;
    }
  /* FIXME: currently allow both :: and : */
  else if (*c == ':')
    c++;
  if (action == Delete)
    {
      if ((ace->a_type & (CLASS_OBJ | OTHER_OBJ))
          && *c)
        return FALSE;
      ace->a_perm = ILLEGAL_MODE;
      return TRUE;
    }
  if ((ace->a_perm = getperm (c)) == ILLEGAL_MODE)
    return FALSE;
  return TRUE;
}

BOOL
getaclentries (action_t action, char *buf, aclent_t *acls, int *idx)
{
  char *c;

  if (action == SetFromFile)
    {
      FILE *fp;
      char fbuf[256], *fb;

      if (!strcmp (buf, "-"))
        fp = stdin;
      else if (! (fp = fopen (buf, "r")))
        return FALSE;
      while ((fb = fgets (fbuf, 256, fp)))
        {
	  while (strchr (" \t", *fb))
	    ++fb;
	  if (strchr ("\n\r#", *fb))
	    continue;
          if (!getaclentry (action, fb, acls + (*idx)++))
            {
              fclose (fp);
              return FALSE;
            }
        }
      if (fp != stdin)
	fclose (fp);
    }
  else
    for (c = strtok (buf, ","); c; c = strtok (NULL, ","))
      if (!getaclentry (action, c, acls + (*idx)++))
        return FALSE;
  return TRUE;
}

int
searchace (aclent_t *aclp, int nentries, int type, int id)
{
  int i;

  for (i = 0; i < nentries; ++i)
    if ((aclp[i].a_type == type && (id < 0 || aclp[i].a_id == id))
        || !aclp[i].a_type)
      return i;
  return -1;
}

int
modacl (aclent_t *tgt, int tcnt, aclent_t *src, int scnt)
{
  int t, s, i;

  for (s = 0; s < scnt; ++s)
    {
      t = searchace (tgt, MAX_ACL_ENTRIES, src[s].a_type,
                     (src[s].a_type & (USER | GROUP)) ? src[s].a_id : -1);
      if (t < 0)
        return -1;
      if (src[s].a_perm == ILLEGAL_MODE)
        {
          if (t < tcnt)
            {
              for (i = t + 1; i < tcnt; ++i)
                tgt[i - 1] = tgt[i];
              --tcnt;
            }
        }
      else
        {
          tgt[t] = src[s];
          if (t >= tcnt)
            ++tcnt;
        }
    }
  return tcnt;
}

void
setfacl (action_t action, char *path, aclent_t *acls, int cnt)
{
  aclent_t lacl[MAX_ACL_ENTRIES];
  int lcnt;

  memset (lacl, 0, sizeof lacl);
  if (action == Set)
    {
      if (acl (path, SETACL, cnt, acls))
        perror (prog_name);
      return;
    }
  if ((lcnt = acl (path, GETACL, MAX_ACL_ENTRIES, lacl)) < 0
      || (lcnt = modacl (lacl, lcnt, acls, cnt)) < 0
      || (lcnt = acl (path, SETACL, lcnt, lacl)) < 0)
    perror (prog_name);
}

static void
usage (FILE * stream)
{
  fprintf (stream, ""
            "Usage: %s [-r] (-f ACL_FILE | -s acl_entries) FILE...\n"
            "       %s [-r] ([-d acl_entries] [-m acl_entries]) FILE...\n"
            "Modify file and directory access control lists (ACLs)\n"
            "\n"
            "  -d, --delete     delete one or more specified ACL entries\n"
            "  -f, --file       set ACL entries for FILE to ACL entries read\n"
            "                   from a ACL_FILE\n"
            "  -m, --modify     modify one or more specified ACL entries\n"
            "  -r, --replace    replace mask entry with maximum permissions\n"
            "                   needed for the file group class\n"
            "  -s, --substitute substitute specified ACL entries for the\n"
            "                   ACL of FILE\n"
            "  -h, --help       output usage information and exit\n"
            "  -v, --version    output version information and exit\n"
            "\n"
            "At least one of (-d, -f, -m, -s) must be specified\n"
            "\n"
            "", prog_name, prog_name);
  if (stream == stdout) 
  {
    printf(""
            "     Acl_entries are one or more comma-separated ACL entries \n"
            "     from the following list:\n"
            "\n"
            "         u[ser]::perm\n"
            "         u[ser]:uid:perm\n"
            "         g[roup]::perm\n"
            "         g[roup]:gid:perm\n"
            "         m[ask]:perm\n"
            "         o[ther]:perm\n"
            "\n"
            "     Default entries are like the above with the additional\n"
            "     default identifier. For example: \n"
            "\n"
            "         d[efault]:u[ser]:uid:perm\n"
            "\n"
            "     `perm' is either a 3-char permissions string in the form\n"
            "     \"rwx\" with the character - for no permission\n"
            "     or it is the octal representation of the permissions, a\n"
            "     value from 0 (equivalent to \"---\") to 7 (\"rwx\").\n"
            "     `uid' is a user name or a numerical uid.\n"
            "     `gid' is a group name or a numerical gid.\n"
            "\n"
            "\n"
            "For each file given as parameter, %s will either replace its\n"
            "complete ACL (-s, -f), or it will add, modify, or delete ACL\n"
            "entries.\n"
            "\n"
            "The following options are supported:\n"
            "\n"
            "-d   Delete one or more specified entries from the file's ACL.\n"
            "     The owner, group and others entries must not be deleted.\n"
            "     Acl_entries to be deleted should be specified without\n"
            "     permissions, as in the following list:\n"
            "\n"
            "         u[ser]:uid\n"
            "         g[roup]:gid\n"
            "         d[efault]:u[ser]:uid\n"
            "         d[efault]:g[roup]:gid\n"
            "         d[efault]:m[ask]:\n"
            "         d[efault]:o[ther]:\n"
            "\n"
            "-f   Take the Acl_entries from ACL_FILE one per line. Whitespace\n"
            "     characters are ignored, and the character \"#\" may be used\n"
            "     to start a comment.  The special filename \"-\" indicates\n"
            "     reading from stdin.\n"
            "     Required entries are\n"
            "     - One user entry for the owner of the file.\n"
            "     - One group entry for the group of the file.\n"
            "     - One other entry.\n"
            "     If additional user and group entries are given:\n"
            "     - A mask entry for the file group class of the file.\n"
            "     - No duplicate user or group entries with the same uid/gid.\n"
            "     If it is a directory:\n"
            "     - One default user entry for the owner of the file.\n"
            "     - One default group entry for the group of the file.\n"
            "     - One default mask entry for the file group class.\n"
            "     - One default other entry.\n"
            "\n"
            "-m   Add or modify one or more specified ACL entries.\n"
            "     Acl_entries is a comma-separated list of entries from the \n"
            "     same list as above.\n"
            "\n"
            "-r   Causes the permissions specified in the mask entry to be\n"
            "     ignored and replaced by the maximum permissions needed for\n"
            "     the file group class.\n"
            "\n"
            "-s   Like -f, but substitute the file's ACL with Acl_entries\n" 
            "     specified in a comma-separated list on the command line.\n"
            "\n"
            "While the -d and -m options may be used in the same command, the\n"
            "-f and -s options may be used only exclusively.\n"
            "\n"
            "Directories may contain default ACL entries.  Files created\n"
            "in a directory that contains default ACL entries will have\n"
            "permissions according to the combination of the current umask,\n"
            "the explicit permissions requested and the default ACL entries\n"
            "Note: Under Cygwin, the default ACL entries are not taken into\n"
            "account currently.\n", prog_name);
  }
  else
    fprintf(stream, "Try `%s --help' for more information.\n", prog_name);
}

struct option longopts[] = {
  {"delete", required_argument, NULL, 'd'},
  {"file", required_argument, NULL, 'f'},
  {"modify", required_argument, NULL, 'm'},
  {"replace", no_argument, NULL, 'r'},
  {"substitute", required_argument, NULL, 's'},
  {"help", no_argument, NULL, 'h'},
  {"version", no_argument, NULL, 'v'},
  {0, no_argument, NULL, 0}
};

static void
print_version ()
{
  const char *v = strchr (version, ':');
  int len;
  if (!v)
    {
      v = "?";
      len = 1;
    }
  else
    {
      v += 2;
      len = strchr (v, ' ') - v;
    }
  printf ("\
setfacl (cygwin) %.*s\n\
ACL Modification Utility\n\
Copyright 2000, 2001, 2002 Red Hat, Inc.\n\
Compiled on %s\n\
", len, v, __DATE__);
}

int
main (int argc, char **argv)
{
  int c;
  action_t action = NoAction;
  int ropt = 0;
  aclent_t acls[MAX_ACL_ENTRIES];
  int aclidx = 0;

  prog_name = strrchr (argv[0], '/');
  if (prog_name == NULL)
    prog_name = strrchr (argv[0], '\\');
  if (prog_name == NULL)
    prog_name = argv[0];
  else
    prog_name++;

  memset (acls, 0, sizeof acls);
  while ((c = getopt_long (argc, argv, "d:f:hm:rs:v", longopts, NULL)) != EOF)
    switch (c)
      {
      case 'd':
        if (action == NoAction)
          action = Delete;
        else if (action == Modify)
          action = ModNDel;
        else
          {
            usage (stderr);
            return 1;
	  }
        if (! getaclentries (Delete, optarg, acls, &aclidx))
          {
            fprintf (stderr, "%s: illegal acl entries\n", prog_name);
            return 2;
          }
        break;
      case 'f':
        if (action == NoAction)
          action = Set;
        else
          {
            usage (stderr);
            return 1;
	  }
        if (! getaclentries (SetFromFile, optarg, acls, &aclidx))
          {
            fprintf (stderr, "%s: illegal acl entries\n", prog_name);
            return 2;
          }
        break;
      case 'h':
        usage (stdout);
        return 0;
      case 'm':
        if (action == NoAction)
          action = Modify;
        else if (action == Delete)
          action = ModNDel;
        else
          {
            usage (stderr);
            return 1;
	  }
        if (! getaclentries (Modify, optarg, acls, &aclidx))
          {
            fprintf (stderr, "%s: illegal acl entries\n", prog_name);
            return 2;
          }
        break;
      case 'r':
        if (!ropt)
          ropt = 1;
        else
          {
            usage (stderr);
            return 1;
	  }
        break;
      case 's':
        if (action == NoAction)
          action = Set;
        else
          {
            usage (stderr);
            return 1;
	  }
        if (! getaclentries (Set, optarg, acls, &aclidx))
          {
            fprintf (stderr, "%s: illegal acl entries\n", prog_name);
            return 2;
          }
        break;
      case 'v':
        print_version ();
        return 0;
      default:
        usage (stderr);
        return 1;
      }
  if (action == NoAction)
    {
      usage (stderr);
      return 1;
    }
  if (optind > argc - 1)
    {
      usage (stderr);
      return 1;
    }
  if (action == Set)
    switch (aclcheck (acls, aclidx, NULL))
      {
      case GRP_ERROR:
        fprintf (stderr, "%s: more than one group entry.\n", prog_name);
        return 2;
      case USER_ERROR:
        fprintf (stderr, "%s: more than one user entry.\n", prog_name);
        return 2;
      case CLASS_ERROR:
        fprintf (stderr, "%s: more than one mask entry.\n", prog_name);
        return 2;
      case OTHER_ERROR:
        fprintf (stderr, "%s: more than one other entry.\n", prog_name);
        return 2;
      case DUPLICATE_ERROR:
        fprintf (stderr, "%s: duplicate additional user or group.\n", prog_name);
        return 2;
      case ENTRY_ERROR:
        fprintf (stderr, "%s: invalid entry type.\n", prog_name);
        return 2;
      case MISS_ERROR:
        fprintf (stderr, "%s: missing entries.\n", prog_name);
        return 2;
      case MEM_ERROR:
        fprintf (stderr, "%s: out of memory.\n", prog_name);
        return 2;
      default:
        break;
      }
  for (c = optind; c < argc; ++c)
    setfacl (action, argv[c], acls, aclidx);
  return 0;
}