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
|
// The deployed policy is a read-only mount, on purpose: that is what makes it
// reviewable and the rollout visible. These are for the source copy, and say so
// when the file cannot be written rather than pretending to have granted
// something.
//
// Comments survive, because the file is also the record of why an access exists.
// The rewrite is a whole file to a temporary and a rename: a policy truncated
// half way through a write is a policy that denies everything.
#include "authz.h"
#include <ctype.h>
#include <errno.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#define HIDDEN_PREFIX "/.authz"
#define TEMPLATE_SUFFIX ".XXXXXX"
typedef struct {
char **v;
size_t n;
size_t cap;
} lines_t;
static void lines_init(lines_t *l)
{
l->v = NULL;
l->n = 0;
l->cap = 0;
}
static void lines_push(lines_t *l, char *s) // takes ownership
{
if (l->n == l->cap) {
l->cap = l->cap != 0 ? l->cap * 2 : 64;
l->v = xrealloc(l->v, l->cap * sizeof(*l->v));
}
l->v[l->n++] = s;
}
static void lines_free(lines_t *l)
{
size_t i;
for (i = 0; i < l->n; i++)
free(l->v[i]);
free(l->v);
lines_init(l);
}
// Every line, newline included, exactly as it was.
static int read_lines(const char *path, lines_t *l)
{
FILE *f;
char *line = NULL;
size_t cap = 0;
ssize_t len;
lines_init(l);
f = fopen(path, "r");
if (f == NULL) {
fprintf(stderr, "%s: cannot read %s: %s\n", progname, path, strerror(errno));
return -1;
}
while ((len = getline(&line, &cap, f)) != -1) {
size_t n = strlen(line);
if (n == 0 || line[n - 1] == '\n') {
lines_push(l, xstrdup(line));
continue;
}
{ // a last line without its newline
char *own = xmalloc(n + 2);
memcpy(own, line, n);
own[n] = '\n';
own[n + 1] = '\0';
lines_push(l, own);
}
}
free(line);
fclose(f);
return 0;
}
// A temporary beside the target, then rename: a reader sees the old file or the
// new one. Mode and owner are carried over, because a policy that becomes 0600
// root-owned stops working for the git account.
static int write_atomic(const lines_t *l, const char *path)
{
char *dirtext = xstrdup(path);
char *dir;
char *template;
size_t need;
struct stat st;
int fd;
size_t i;
int had = stat(path, &st) == 0;
// dirname may return a pointer into its argument or a fresh one.
dir = dirname(dirtext);
if (dir == NULL)
dir = (char *)".";
need = strlen(dir) + strlen(HIDDEN_PREFIX) + strlen(TEMPLATE_SUFFIX) + 1;
template = xmalloc(need);
snprintf(template, need, "%s%s%s", dir, HIDDEN_PREFIX, TEMPLATE_SUFFIX);
fd = mkstemp(template);
if (fd < 0) {
fprintf(stderr, "%s: cannot write beside %s: %s\n", progname, path,
strerror(errno));
free(template);
free(dirtext);
return -1;
}
for (i = 0; i < l->n; i++) {
size_t n = strlen(l->v[i]);
size_t off = 0;
while (off < n) {
ssize_t w = write(fd, l->v[i] + off, n - off);
if (w <= 0) {
fprintf(stderr, "%s: write failed: %s\n", progname, strerror(errno));
close(fd);
unlink(template);
free(template);
free(dirtext);
return -1;
}
off += (size_t)w;
}
}
if (fchmod(fd, had ? (st.st_mode & 07777) : 0644) != 0)
fprintf(stderr, "%s: warning: chmod: %s\n", progname, strerror(errno));
if (had && geteuid() == 0 && fchown(fd, st.st_uid, st.st_gid) != 0)
fprintf(stderr, "%s: warning: chown: %s\n", progname, strerror(errno));
if (fsync(fd) != 0)
fprintf(stderr, "%s: warning: fsync: %s\n", progname, strerror(errno));
if (close(fd) != 0) {
fprintf(stderr, "%s: close failed: %s\n", progname, strerror(errno));
unlink(template);
free(template);
free(dirtext);
return -1;
}
if (rename(template, path) != 0) {
fprintf(stderr, "%s: cannot replace %s: %s\n", progname, path, strerror(errno));
if (errno == EROFS || errno == EACCES || errno == EPERM)
fprintf(stderr, "%s: the policy is read-only by design. Edit the "
"source copy (git-host/authz.conf), roll the ConfigMap, or "
"write elsewhere with --out.\n", progname);
unlink(template);
free(template);
free(dirtext);
return -1;
}
free(template);
free(dirtext);
return 0;
}
// ------------------------------------------------------------------ matching
// Copied before splitting: splitting writes over the text, and these lines are
// the file.
static int is_rule_for(const char *line, const char *user, const char *repo)
{
char *copy = xstrdup(line);
char *fields[MAX_FIELDS];
char *text = authz_trim(copy);
char *hash = strchr(text, '#');
char *want;
char *have;
int same = 0;
if (hash != NULL)
*hash = '\0';
if (authz_split_ws(text, fields, MAX_FIELDS) >= 3) {
want = authz_norm_repo(repo);
have = authz_norm_repo(fields[1]);
same = strcmp(fields[0], user) == 0 && strcmp(have, want) == 0;
free(want);
free(have);
}
free(copy);
return same;
}
// A ref pattern reaches the file as one field. Whitespace would split it into
// two patterns when the file is read back - granting a ref nobody asked for - and
// '#' starts a comment, so the pattern would be quietly cut short. An argument
// carrying either means something other than what was typed.
static int ref_field_ok(const char *s)
{
size_t i;
if (s[0] == '\0')
return 0;
for (i = 0; s[i] != '\0'; i++) {
unsigned char c = (unsigned char)s[i];
if (isspace(c) || c == '#')
return 0;
}
return 1;
}
static char *rule_text(const char *user, const char *repo, const char *level,
int nrefs, char **refs)
{
strbuf_t b;
int i;
sb_init(&b);
sb_addf(&b, "%s %s %s", user, repo, level);
for (i = 0; i < nrefs; i++) {
char *one = authz_norm_ref(refs[i]);
sb_addf(&b, " %s", one);
free(one);
}
sb_add(&b, "\n");
return b.s;
}
// A file that does not parse is refused: rewriting the parts that parse would
// keep the damage. A file that is not there yet is bootstrapped with the
// grammar
// written at the top.
static int refuse_broken(const char *path, int *created)
{
policy_t probe;
int rc;
if (created != NULL)
*created = 0;
policy_init(&probe, path);
rc = policy_load(&probe);
policy_free(&probe);
if (rc == -1) {
fprintf(stderr, "%s: %s does not parse; fix it before editing\n", progname, path);
return 3;
}
if (rc == -2 && created != NULL)
*created = 1;
return 0;
}
// --out and --stdout are allowed anywhere, because "grant bob team write --out
// /tmp/new.conf" is what people type; parsing them only at the front made "--
// out"
// a ref pattern.
int authz_parse_out(int argc, char **argv, edit_opts *o, int *consumed)
{
int i;
int n = 0;
for (i = 0; i < argc; i++) {
if (strcmp(argv[i], "--stdout") == 0) {
o->out = "-";
continue;
}
if (strcmp(argv[i], "--out") == 0) {
if (i + 1 >= argc) {
fprintf(stderr, "%s: --out wants a file\n", progname);
return -1;
}
o->out = argv[++i];
continue;
}
if (strncmp(argv[i], "--out=", 6) == 0) {
o->out = argv[i] + 6;
continue;
}
argv[n++] = argv[i];
}
*consumed = argc - n;
return 0;
}
int authz_reject_options(int argc, char **argv)
{
int i;
for (i = 0; i < argc; i++) {
if (argv[i][0] == '-' && argv[i][1] == '-') {
fprintf(stderr, "%s: unknown option \"%s\" - options come before the "
"names, and nothing here starts with a dash\n", progname, argv[i]);
return 1;
}
}
return 0;
}
// Every command ends here, so this is where the lines are released.
static int finish(lines_t *l, const char *path, edit_opts o, const char *what)
{
int rc;
if (o.out != NULL && strcmp(o.out, "-") == 0) {
size_t i;
for (i = 0; i < l->n; i++)
fputs(l->v[i], stdout);
fprintf(stderr, "%s: %s (to stdout; %s untouched)\n", progname, what, path);
rc = 0;
} else if (write_atomic(l, o.out != NULL ? o.out : path) != 0) {
rc = 1;
} else {
printf("%s: %s: %s\n", progname, what, o.out != NULL ? o.out : path);
rc = 0;
}
lines_free(l);
return rc;
}
// ------------------------------------------------------------------ commands
int cmd_grant(const char *path, edit_opts o, int argc, char **argv)
{
lines_t lines;
char *text;
int i;
int first = -1;
int replaced = -1;
int appended = 0;
int dropped = 0;
int created = 0;
if (argc < 3) {
fprintf(stderr, "%s: grant wants "
"<user> <repo> <write|force|admin> [ref ...]\n", progname);
return 64;
}
if (authz_level_of(argv[2]) < 0) {
fprintf(stderr, "%s: unknown access level \"%s\" (write force admin)\n",
progname, argv[2]);
return 64;
}
if (strcmp(argv[0], "@all") != 0 && !authz_user_pattern_ok(argv[0])) {
fprintf(stderr, "%s: invalid user pattern \"%s\"\n", progname, argv[0]);
return 64;
}
if (!authz_repo_pattern_ok(argv[1])) {
fprintf(stderr, "%s: invalid repo pattern \"%s\" (flat names only, no slashes)\n",
progname, argv[1]);
return 64;
}
for (i = 3; i < argc; i++) {
if (argv[i][0] == '!' || argv[i][0] == '-') {
fprintf(stderr, "%s: ref patterns are additive, there is no deny: %s\n",
progname, argv[i]);
return 64;
}
if (!ref_field_ok(argv[i])) {
fprintf(stderr, "%s: not a ref pattern: %s\n", progname, argv[i]);
return 64;
}
}
if (authz_reject_options(argc, argv) != 0)
return 64;
if (refuse_broken(path, &created) != 0)
return 3;
if (created) {
// xstrdup'd: lines_free frees every entry it holds, and a string
// literal is not something free() may be handed.
lines_init(&lines);
lines_push(&lines, xstrdup("# git-authz policy\n"));
lines_push(&lines, xstrdup("# <user> <repo> <write|force|admin> "
"[ref pattern ...] levels stack: "
"admin > force > write\n"));
lines_push(&lines, xstrdup("# Name the refs a rule covers: with no "
"patterns it covers the whole repository.\n"));
} else if (read_lines(path, &lines) != 0)
return 3;
// Several rules for one pair are legal and pointless - the strongest wins -
// so they collapse into one line, in the place of the first.
text = rule_text(argv[0], argv[1], argv[2], argc - 3, argv + 3);
for (i = 0; i < (int)lines.n; i++) {
if (!is_rule_for(lines.v[i], argv[0], argv[1]))
continue;
if (first < 0) {
first = i; // the line the new rule takes over
continue;
}
free(lines.v[i]); // a later rule for the same pair
lines.v[i] = NULL;
dropped++;
}
if (first < 0) {
lines_push(&lines, text); // the buffer belongs to the file now
replaced = (int)lines.n;
appended = 1;
} else {
size_t w;
free(lines.v[first]);
lines.v[first] = text;
for (i = 0, w = 0; i < (int)lines.n; i++) {
if (lines.v[i] != NULL)
lines.v[w++] = lines.v[i];
}
lines.n = w;
replaced = first + 1;
}
{
char what[512];
if (created)
snprintf(what, sizeof(what), "granted %s %s %s (new policy, line %d)",
argv[0], argv[1], argv[2], replaced);
else if (dropped > 0)
snprintf(what, sizeof(what), "granted %s %s %s (line %d, %d duplicate(s) removed)",
argv[0], argv[1], argv[2], replaced, dropped);
else if (appended)
snprintf(what, sizeof(what), "granted %s %s %s (appended as line %d)",
argv[0], argv[1], argv[2], replaced);
else
snprintf(what, sizeof(what), "granted %s %s %s (replaced line %d)",
argv[0], argv[1], argv[2], replaced);
return finish(&lines, path, o, what);
}
}
int cmd_revoke(const char *path, edit_opts o, int argc, char **argv)
{
lines_t lines;
size_t w;
int i;
int gone = 0;
if (argc != 2) {
fprintf(stderr, "%s: revoke wants <user> <repo>\n", progname);
return 64;
}
if (authz_reject_options(argc, argv) != 0)
return 64;
if (refuse_broken(path, NULL) != 0)
return 3;
if (read_lines(path, &lines) != 0) {
fprintf(stderr, "%s: no rule for %s on %s to remove\n", progname, argv[0],
argv[1]);
return 1;
}
for (i = 0, w = 0; i < (int)lines.n; i++) {
if (is_rule_for(lines.v[i], argv[0], argv[1])) {
free(lines.v[i]);
gone++;
continue;
}
lines.v[w++] = lines.v[i];
}
lines.n = w;
if (gone == 0) {
fprintf(stderr, "%s: no rule for %s on %s to remove\n", progname, argv[0],
argv[1]);
lines_free(&lines);
return 1;
}
{
char what[512];
snprintf(what, sizeof(what), "revoked %s on %s (%d rule(s) removed)", argv[0],
argv[1], gone);
return finish(&lines, path, o, what);
}
}
|