-
Notifications
You must be signed in to change notification settings - Fork 0
/
acl_view.c
130 lines (103 loc) · 3.61 KB
/
acl_view.c
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
/* acl_view.c
Display the access control list (ACL) on a file.
Usage: acl_view [-d] file
If the '-d' option is specified, then the default ACL is displayed (and
'file' must be a directory), otherwise the access ACL is displayed.
This program is Linux-specific. ACLs are supported since Linux 2.6.
To build this program, you must have the ACL library (libacl) installed
on your system.
*/
#include <acl/libacl.h>
#include <sys/acl.h>
#include "ugid_functions.h"
#include "tlpi_hdr.h"
static void
usageError(char *progName)
{
fprintf(stderr, "Usage: %s [-d] filename\n", progName);
exit(EXIT_FAILURE);
}
int
main(int argc, char *argv[])
{
acl_t acl;
acl_type_t type;
acl_entry_t entry;
acl_tag_t tag;
uid_t *uidp;
gid_t *gidp;
acl_permset_t permset;
char *name;
int entryId, permVal, opt;
type = ACL_TYPE_ACCESS;
while ((opt = getopt(argc, argv, "d")) != -1) {
switch (opt) {
case 'd': type = ACL_TYPE_DEFAULT; break;
case '?': usageError(argv[0]);
}
}
if (optind + 1 != argc)
usageError(argv[0]);
acl = acl_get_file(argv[optind], type);
if (acl == NULL)
errExit("acl_get_file");
/* Walk through each entry in this ACL */
for (entryId = ACL_FIRST_ENTRY; ; entryId = ACL_NEXT_ENTRY) {
if (acl_get_entry(acl, entryId, &entry) != 1)
break; /* Exit on error or no more entries */
/* Retrieve and display tag type */
if (acl_get_tag_type(entry, &tag) == -1)
errExit("acl_get_tag_type");
printf("%-12s", (tag == ACL_USER_OBJ) ? "user_obj" :
(tag == ACL_USER) ? "user" :
(tag == ACL_GROUP_OBJ) ? "group_obj" :
(tag == ACL_GROUP) ? "group" :
(tag == ACL_MASK) ? "mask" :
(tag == ACL_OTHER) ? "other" : "???");
/* Retrieve and display optional tag qualifier */
if (tag == ACL_USER) {
uidp = acl_get_qualifier(entry);
if (uidp == NULL)
errExit("acl_get_qualifier");
name = userNameFromId(*uidp);
if (name == NULL)
printf("%-8d ", *uidp);
else
printf("%-8s ", name);
if (acl_free(uidp) == -1)
errExit("acl_free");
} else if (tag == ACL_GROUP) {
gidp = acl_get_qualifier(entry);
if (gidp == NULL)
errExit("acl_get_qualifier");
name = groupNameFromId(*gidp);
if (name == NULL)
printf("%-8d ", *gidp);
else
printf("%-8s ", name);
if (acl_free(gidp) == -1)
errExit("acl_free");
} else {
printf(" ");
}
/* Retrieve and display permissions */
if (acl_get_permset(entry, &permset) == -1)
errExit("acl_get_permset");
permVal = acl_get_perm(permset, ACL_READ);
if (permVal == -1)
errExit("acl_get_perm - ACL_READ");
printf("%c", (permVal == 1) ? 'r' : '-');
permVal = acl_get_perm(permset, ACL_WRITE);
if (permVal == -1)
errExit("acl_get_perm - ACL_WRITE");
printf("%c", (permVal == 1) ? 'w' : '-');
permVal = acl_get_perm(permset, ACL_EXECUTE);
if (permVal == -1)
errExit("acl_get_perm - ACL_EXECUTE");
printf("%c", (permVal == 1) ? 'x' : '-');
printf("\n");
}
if (acl_free(acl) == -1)
errExit("acl_free");
exit(EXIT_SUCCESS);
}