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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
|
/* edit.c - change the policy file, without losing the file.
*
* The deployed policy is a read-only mount, usually a ConfigMap, and that is
* not a bug to work around: it is what makes the policy reviewable and the
* rollout visible. So these commands are for the source copy - a checkout, a
* CI job, a notebook - and they say so plainly when the file they were given
* cannot be written, rather than pretending to have granted something.
*
* What they preserve is the thing a generator cannot: the comments. A policy
* file is also the record of why an access exists, so lines are replaced in
* place and everything else is left exactly as it was. When a line has to be
* written, it is written whole; the file is never truncated and rebuilt in
* place, because a policy that disappears half way through a write is a policy
* that denies everything (or, if the order were different, nothing).
*
* A file that does not parse is refused: editing a broken policy would hide the
* breakage.
*/
#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:
* keep the file well formed */
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;
}
/* Write to a temporary file beside the target, then rename. The reader either
* sees the old file or the new one; and the mode and owner survive, 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, and may
* fail; the copy is ours either way and dirtext is freed at the end. */
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 */
/* Does this line grant to exactly this user and repository?
*
* The line is copied first: splitting writes over the text, and these lines are
* the file - a matcher that flattened the comments out of the ones it did not
* change would be a vandal. */
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;
}
static int is_set_for(const char *line, const char *key)
{
char *copy = xstrdup(line);
char *text = authz_trim(copy);
char *hash = strchr(text, '#');
size_t klen = strlen(key);
int hit = 0;
if (hash != NULL)
*hash = '\0';
if (strncmp(text, "set", 3) == 0 && isspace((unsigned char)text[3])) {
text = authz_trim(text + 3);
hit = strncmp(text, key, klen) == 0 &&
(text[klen] == '\0' || isspace((unsigned char)text[klen]));
}
free(copy);
return hit;
}
/* --------------------------------------------------------------- emitting */
/* "user repo level ref ref" - one line, no alignment tricks, because the next
* tool to read this is diff. */
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;
}
/* Fail before touching anything if the file cannot be parsed. */
/* Load a policy for editing. A policy that does not parse is refused: editing
* a file we cannot read would rewrite the parts we can read and keep the
* damage. A policy that is not there yet is a bootstrap, not an error - the
* source copy has to start somewhere - so the file begins with the grammar
* written down, which is also the least surprising thing to find in it. */
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;
}
/* Pull the shared options out of the argument list. They are allowed anywhere
* rather than only at the front, because "grant bob team write --out
* /tmp/new.conf" is what people type - and treating "--out" as a ref pattern
* would grant something nobody asked for. */
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;
}
/* Write the result and release the lines: every command ends here, so this is
* where the file it read stops being held. */
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 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 (authz_reject_options(argc, argv) != 0)
return 64;
if (refuse_broken(path, &created) != 0)
return 3;
if (created) {
/* Bootstrapped lines are xstrdup'd for the same reason as the rest:
* 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("set protected-refs refs/heads/main\n"));
} else if (read_lines(path, &lines) != 0)
return 3;
/* More than one rule for the same pair is legal but pointless - the
* strongest wins - so the pair is collapsed into one line, in the place of
* the first. Everything else, comments included, is byte for byte what it
* was. */
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;
} 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 ((int)lines.n == replaced)
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);
}
}
int cmd_protect(const char *path, edit_opts o, int argc, char **argv)
{
lines_t lines;
const char *repo = NULL;
char key[256];
char *line;
strbuf_t text;
int i;
int replaced = -1;
int created = 0;
for (i = 0; i < argc; i++) { /* --repo anywhere, like --out */
if (strcmp(argv[i], "--repo") != 0)
continue;
if (i + 1 >= argc) {
fprintf(stderr, "%s: --repo wants a repository name\n", progname);
return 64;
}
repo = argv[i + 1];
for (; i + 2 < argc; i++)
argv[i] = argv[i + 2];
argc -= 2;
break;
}
if (argc < 1) {
fprintf(stderr, "%s: protect wants [--repo <name>] <ref pattern> ...\n",
progname);
return 64;
}
if (repo != NULL && !authz_repo_pattern_ok(repo)) {
fprintf(stderr, "%s: invalid repo pattern \"%s\"\n", progname, repo);
return 64;
}
for (i = 0; i < argc; i++) {
if (argv[i][0] == '-' && argv[i][1] == '-') {
fprintf(stderr, "%s: unknown option \"%s\" (the only one here is "
"--repo, which comes first)\n", progname, argv[i]);
return 64;
}
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 (refuse_broken(path, &created) != 0)
return 3;
if (created) {
lines_init(&lines);
lines_push(&lines, xstrdup("# git-authz policy\n"));
} else if (read_lines(path, &lines) != 0)
return 3;
if (repo != NULL)
snprintf(key, sizeof(key), "protected-refs.%s", repo);
else
snprintf(key, sizeof(key), "protected-refs");
sb_init(&text);
sb_addf(&text, "set %s", key);
for (i = 0; i < argc; i++) {
char *one = authz_norm_ref(argv[i]);
sb_addf(&text, " %s", one);
free(one);
}
sb_add(&text, "\n");
line = xstrdup(text.s); /* the array owns its own copy; text is
* released by sb_free below */
for (i = 0; i < (int)lines.n; i++) {
if (!is_set_for(lines.v[i], key))
continue;
free(lines.v[i]);
lines.v[i] = line;
replaced = i + 1;
break;
}
if (replaced < 0) {
lines_push(&lines, line);
replaced = (int)lines.n;
}
sb_free(&text);
{
char what[512];
if (created)
snprintf(what, sizeof(what), "protected refs for %s (%s, new policy)",
repo != NULL ? repo : "every repository", key);
else
snprintf(what, sizeof(what), "protected refs for %s (%s, line %d)",
repo != NULL ? repo : "every repository", key, replaced);
return finish(&lines, path, o, what);
}
}
|