aboutsummaryrefslogtreecommitdiff
path: root/winsup/utils/getfacl.c
blob: 482bc0c7b5d5e0746694abb8d3827b6911fd4065 (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

#include <pwd.h>
#include <grp.h>
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/acl.h>
#include <sys/stat.h>

char *
permstr (mode_t perm)
{
  static char pbuf[4];

  pbuf[0] = (perm & S_IREAD) ? 'r' : '-';
  pbuf[1] = (perm & S_IWRITE) ? 'w' : '-';
  pbuf[2] = (perm & S_IEXEC) ? 'x' : '-';
  pbuf[3] = '\0';
  return pbuf;
}

#if 0
char *
username (uid_t uid)
{
  static char ubuf[256];
  struct passwd *pw;

  if (pw = getpwuid (uid))
    strcpy (ubuf, pw->pw_name);
  else
    strcpy (ubuf, "<unknown>");
}

char *
groupname (gid_t gid)
{
  static char gbuf[256];
  struct group *gr;

  if (gr = getgruid (gid))
    strcpy (gbuf, gr->gr_name);
  else
    strcpy (gbuf, "<unknown>");
}
#endif

int
main (int argc, char **argv)
{
  extern int optind;
  int c, i;
  int aopt = 0;
  int dopt = 0;
  int first = 1;
  struct stat st;
  aclent_t acls[MAX_ACL_ENTRIES];

  while ((c = getopt (argc, argv, "ad")) != EOF)
    switch (c)
      {
      case 'a':
	aopt = 1;
	break;
      case 'd':
	dopt = 1;
	break;
      default:
	fprintf (stderr, "usage: %s [-ad] file...\n", argv[0]);
	return 1;
      }
  while ((c = optind++) < argc)
    {
      if (stat (argv[c], &st))
	{
	  perror (argv[0]);
	  continue;
	}
      if (!first)
	putchar ('\n');
      first = 0;
      printf ("# file: %s\n", argv[c]);
      printf ("# owner: %d\n", st.st_uid);
      printf ("# group: %d\n", st.st_gid);
      if ((c = acl (argv[c], GETACL, MAX_ACL_ENTRIES, acls)) < 0)
	{
	  perror (argv[0]);
	  continue;
	}
      for (i = 0; i < c; ++i)
	{
	  if (acls[i].a_type & ACL_DEFAULT)
	    {
	      if (aopt)
		continue;
	      printf ("default:");
	    }
	  else if (dopt)
	    continue;
	  switch (acls[i].a_type & ~ACL_DEFAULT)
	    {
	    case USER_OBJ:
	      printf ("user::");
	      break;
	    case USER:
	      printf ("user:%d:", acls[i].a_id);
	      break;
	    case GROUP_OBJ:
	      printf ("group::");
	      break;
	    case GROUP:
	      printf ("group:%d:", acls[i].a_id);
	      break;
	    case CLASS_OBJ:
	      printf ("mask::");
	      break;
	    case OTHER_OBJ:
	      printf ("other::");
	      break;
	    }
	  printf ("%s\n", permstr (acls[i].a_perm));
	}
    }
  return 0;
}