forked from andreas-gruenbacher/richacl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetrichacl.c
298 lines (258 loc) · 6.56 KB
/
getrichacl.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
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
/*
Copyright (C) 2006, 2008, 2009, 2010 Novell, Inc.
Copyright (C) 2015 Red Hat, Inc.
Written by Andreas Gruenbacher <[email protected]>
The getrichacl 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 2, or (at
your option) any later version.
The getrichacl 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/>.
*/
/*
* FIXME:
* Add a way to show only expicitly set acls and hide inherited ones?
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <getopt.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/xattr.h>
#include <ctype.h>
#include <pwd.h>
#include <grp.h>
#include "sys/richacl.h"
#include "string_buffer.h"
#include "common.h"
static const char *progname;
int format_for_mode(mode_t mode)
{
if (S_ISDIR(mode))
return RICHACL_TEXT_DIRECTORY_CONTEXT;
else
return RICHACL_TEXT_FILE_CONTEXT;
}
static int print_richacl(const char *file, struct richacl **acl,
struct stat *st, int fmt)
{
char *text;
if (!(fmt & RICHACL_TEXT_SHOW_MASKS)) {
if (richacl_apply_masks(acl, st->st_uid))
goto fail;
}
text = richacl_to_text(*acl, fmt | format_for_mode(st->st_mode));
if (!text)
goto fail;
printf("%s:\n", file);
puts(text);
free(text);
return 0;
fail:
return -1;
}
static struct option long_options[] = {
{"access", 2, 0, 'a'},
{"long", 0, 0, 'l'},
{"raw", 0, 0, 2 },
{"full", 0, 0, 3 },
{"unaligned", 0, 0, 4 },
{"numeric-ids", 0, 0, 5 },
{"version", 0, 0, 'v'},
{"help", 0, 0, 'h'},
{ NULL, 0, 0, 0 }
};
static void synopsis(int help)
{
FILE *file = help ? stdout : stderr;
fprintf(file, "SYNOPSIS: %s [options] file ...\n",
basename(progname));
if (!help) {
fprintf(file, "Try '%s --help' for more information.\n",
basename(progname));
exit(1);
}
fprintf(file,
"\n"
"Options:\n"
" --long, -l Display access masks and flags in their long form.\n"
" --full Also show permissions which are always implicitly allowed.\n"
" --raw Show acls as stored on the file system including the file masks.\n"
" Implies --full.\n"
" --unaligned\n"
" Do not align acl entries or pad missing permissions with '-'.\n"
" --numeric-ids\n"
" Display numeric user and group IDs instead of names.\n"
" --access[=user[:group:...]}, -a[user[:group:...]}\n"
" Instead of the acl, show which permissions the caller or a\n"
" specified user has for file(s). When a list of groups is\n"
" given, this overrides the groups the user is in.\n"
" --version, -v\n"
" Display the version of %s and exit.\n"
" --help, -h This help text.\n"
"\n"
COMMON_HELP,
basename(progname));
exit(0);
}
int main(int argc, char *argv[])
{
int opt_access = 0;
char *opt_user = NULL;
int format = RICHACL_TEXT_SIMPLIFY | RICHACL_TEXT_ALIGN;
uid_t user = -1;
gid_t *groups = NULL;
int n_groups = -1;
int status = 0;
int c;
progname = argv[0];
while ((c = getopt_long(argc, argv, "a::lvh",
long_options, NULL)) != -1) {
switch(c) {
case 'a': /* --access */
opt_access = 1;
opt_user = optarg;
break;
case 'l': /* --long */
format |= RICHACL_TEXT_LONG;
break;
case 2: /* --raw */
format |= RICHACL_TEXT_SHOW_MASKS;
format &= ~RICHACL_TEXT_SIMPLIFY;
break;
case 3: /* --full */
format &= ~RICHACL_TEXT_SIMPLIFY;
break;
case 4: /* --unaligned */
format &= ~RICHACL_TEXT_ALIGN;
break;
case 5: /* --numeric-ids */
format |= RICHACL_TEXT_NUMERIC_IDS;
break;
case 'v':
printf("%s %s\n", basename(progname), VERSION);
exit(0);
case 'h':
synopsis(1);
break;
default:
synopsis(0);
break;
}
}
if (optind == argc)
synopsis(0);
if (opt_user) {
int n_groups_alloc;
char *opt_groups;
struct passwd *passwd = NULL;
char *endp;
opt_groups = strchr(opt_user, ':');
if (opt_groups)
*opt_groups++ = 0;
user = strtoul(opt_user, &endp, 10);
if (*endp) {
passwd = getpwnam(opt_user);
if (passwd == NULL) {
fprintf(stderr, "%s: No such user\n", opt_user);
exit(1);
}
user = passwd->pw_uid;
}
n_groups_alloc = 32;
groups = malloc(sizeof(gid_t) * n_groups_alloc);
if (!groups)
goto fail;
if (opt_groups) {
char *tok;
n_groups = 0;
tok = strtok(opt_groups, ":");
while (tok) {
struct group *group;
if (n_groups == n_groups_alloc) {
gid_t *new_groups;
n_groups_alloc *= 2;
new_groups = realloc(groups, sizeof(gid_t) * n_groups_alloc);
if (!new_groups)
goto fail;
}
groups[n_groups] = strtoul(tok, &endp, 10);
if (*endp) {
group = getgrnam(tok);
if (!group) {
fprintf(stderr, "%s: No such group\n", tok);
exit(1);
}
groups[n_groups] = group->gr_gid;
}
n_groups++;
tok = strtok(NULL, ":");
}
} else {
if (!passwd)
passwd = getpwuid(user);
if (passwd) {
n_groups = n_groups_alloc;
if (getgrouplist(passwd->pw_name, passwd->pw_gid,
groups, &n_groups) < 0) {
free(groups);
groups = malloc(sizeof(gid_t) * n_groups);
if (getgrouplist(passwd->pw_name, passwd->pw_gid,
groups, &n_groups) < 0)
goto fail;
}
} else
n_groups = 0;
}
} else
user = geteuid();
for (; optind < argc; optind++) {
const char *file = argv[optind];
struct richacl *acl = NULL;
struct stat st;
if (stat(file, &st))
goto fail2;
if (opt_access) {
unsigned int mask;
char *mask_text;
mask = richacl_access(file, &st, user, groups, n_groups);
if (mask < 0)
goto fail2;
mask_text = richacl_mask_to_text(mask,
format | format_for_mode(st.st_mode));
printf("%s %s\n", *mask_text ? mask_text : "-", file);
free(mask_text);
} else {
acl = get_richacl(file, st.st_mode);
if (!acl) {
if (!errno)
goto fail3;
goto fail2;
}
if (print_richacl(file, &acl, &st, format))
goto fail2;
}
richacl_free(acl);
continue;
fail2:
richacl_free(acl);
perror(file);
fail3:
status = 1;
}
return status;
fail:
perror(basename(progname));
return 1;
}