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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
|
/* policy.c - reading the policy, and answering from it.
*
* Grammar (see authz.conf for the prose version):
*
* set protected-refs <pattern> ... globally, default refs/heads/main
* set protected-refs.<repo> <pattern> ... for one repository, replacing it
* <user> <repo> <write|force|admin> [<ref pattern> ...]
*
* '#' starts a comment wherever it appears. Rules are additive and the
* strongest match wins; there is no 'deny', so a rule is taken away by editing
* the file, not by ordering it after another one.
*
* A line that does not parse is an error, and a policy with errors is not used
* at all: it is better for every push to be refused for one evening than for a
* mistyped line to quietly widen somebody's access. */
#include "authz.h"
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DEFAULT_PROTECTED "refs/heads/main"
/* The characters a name may contain, spelled out rather than as a regular
* expression: the engine has none, and the message says why. */
static const char letters[] =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
static const char user_first[] = "_@*?[";
static const char user_rest[] = "_@*?[].-";
static const char repo_chars[] = "._*?-";
static int in_set(unsigned char c, const char *extra)
{
return c != '\0' && (strchr(letters, (int)c) != NULL ||
strchr(extra, (int)c) != NULL);
}
/* ------------------------------------------------------------------ little */
/* Trims in place: leading by moving the pointer, trailing by writing over it. */
char *authz_trim(char *s)
{
char *end;
while (*s != '\0' && isspace((unsigned char)*s))
s++;
end = s + strlen(s);
while (end > s && isspace((unsigned char)end[-1]))
end--;
*end = '\0';
return s;
}
int authz_split_ws(char *line, char **f, int max)
{
int n = 0;
char *p = line;
while (*p != '\0') {
while (*p != '\0' && isspace((unsigned char)*p))
p++;
if (*p == '\0')
break;
f[n++] = p;
while (*p != '\0' && !isspace((unsigned char)*p))
p++;
if (*p != '\0')
*p++ = '\0';
if (n == max)
return n + 1; /* "too many", the caller decides */
}
return n;
}
static void report(policy_t *p, int line, const char *kind,
const char *fmt, va_list ap)
{
fprintf(stderr, "%s:%d: %s: ", p->path != NULL ? p->path : "authz.conf", line, kind);
vfprintf(stderr, fmt, ap);
fputc('\n', stderr);
}
static void perr(policy_t *p, int line, const char *fmt, ...)
{
va_list ap;
p->errors++;
va_start(ap, fmt);
report(p, line, "error", fmt, ap);
va_end(ap);
}
static void pwarn(policy_t *p, int line, const char *fmt, ...)
{
va_list ap;
p->warnings++;
va_start(ap, fmt);
report(p, line, "warning", fmt, ap);
va_end(ap);
}
/* ------------------------------------------------------------ name shapes */
int authz_user_pattern_ok(const char *s)
{
size_t i;
if (s[0] == '\0' || !in_set((unsigned char)s[0], user_first))
return 0;
for (i = 1; s[i] != '\0'; i++) {
if (!in_set((unsigned char)s[i], user_rest))
return 0;
}
return 1;
}
int authz_repo_pattern_ok(const char *s)
{
size_t i;
if (s[0] == '\0')
return 0;
for (i = 0; s[i] != '\0'; i++) {
if (!in_set((unsigned char)s[i], repo_chars))
return 0;
}
return 1;
}
/* ------------------------------------------------------------------ levels */
const char *authz_level_name(level_t l)
{
switch (l) {
case LV_WRITE: return "write";
case LV_FORCE: return "force";
case LV_ADMIN: return "admin";
default: return "none";
}
}
int authz_level_of(const char *name)
{
if (name == NULL)
return -1;
if (strcmp(name, "write") == 0)
return LV_WRITE;
if (strcmp(name, "force") == 0)
return LV_FORCE;
if (strcmp(name, "admin") == 0)
return LV_ADMIN;
return -1;
}
/* ----------------------------------------------------------- normalisation */
char *authz_norm_repo(const char *in)
{
size_t n;
if (in == NULL)
return xstrdup("");
if (strcmp(in, "*") == 0)
return xstrdup("*");
n = strlen(in);
if (n > 4 && strcmp(in + n - 4, ".git") == 0)
return xsubstr(in, n - 4);
return xstrdup(in);
}
/* One spelling for a ref, so that "main", "heads/main" and "refs/heads/main"
* are the same branch - and so that a bare name can never mean a tag. */
char *authz_norm_ref(const char *in)
{
size_t n;
char *out;
if (in == NULL || *in == '\0')
return xstrdup("refs/*");
if (strcmp(in, "*") == 0 || strcmp(in, "all") == 0 ||
strcmp(in, "refs") == 0 || strcmp(in, "refs/*") == 0)
return xstrdup("refs/*");
if (strncmp(in, "refs/", 5) == 0)
return xstrdup(in);
/* "heads/main", "tags/v1" and "notes/x" are half a refname: the "refs/" is
* missing and nothing else. Anything at all, including a path with slashes
* in it, is a branch name. */
if (strncmp(in, "heads/", 6) == 0 || strncmp(in, "tags/", 5) == 0 ||
strncmp(in, "notes/", 6) == 0) {
n = strlen(in) + 6;
out = xmalloc(n);
snprintf(out, n, "refs/%s", in);
return out;
}
n = strlen(in) + 12;
out = xmalloc(n);
snprintf(out, n, "refs/heads/%s", in);
return out;
}
/* A whole list of them, space separated, the way 'set' lines store them. */
static char *norm_ref_list(const char *value)
{
char *copy = xstrdup(value);
char *fields[MAX_FIELDS];
strbuf_t out;
int i, n;
sb_init(&out);
n = authz_split_ws(copy, fields, MAX_FIELDS);
for (i = 0; i < n; i++) {
char *one = authz_norm_ref(fields[i]);
if (out.len != 0)
sb_add(&out, " ");
sb_add(&out, one);
free(one);
}
free(copy);
return out.s;
}
/* ------------------------------------------------------------------ tables */
static rule_t *rule_slot(policy_t *p)
{
rule_t *r;
if (p->nrules == p->rcap) {
p->rcap = p->rcap != 0 ? p->rcap * 2 : 32;
p->rules = xrealloc(p->rules, p->rcap * sizeof(*p->rules));
}
r = &p->rules[p->nrules++];
memset(r, 0, sizeof(*r));
return r;
}
static void set_protected_for(policy_t *p, const char *repo, const char *refs, int line)
{
size_t i;
for (i = 0; i < p->nprot; i++) {
if (strcmp(p->prot_repo[i], repo) == 0) {
pwarn(p, line, "protected-refs for %s is set twice; the last one wins", repo);
free(p->prot_refs[i]);
p->prot_refs[i] = xstrdup(refs);
return;
}
}
if (p->nprot == p->pcap) {
p->pcap = p->pcap != 0 ? p->pcap * 2 : 8;
p->prot_repo = xrealloc(p->prot_repo, p->pcap * sizeof(*p->prot_repo));
p->prot_refs = xrealloc(p->prot_refs, p->pcap * sizeof(*p->prot_refs));
}
p->prot_repo[p->nprot] = xstrdup(repo);
p->prot_refs[p->nprot] = xstrdup(refs);
p->nprot++;
}
/* ---------------------------------------------------------------- the file */
static void parse_set(policy_t *p, char *line, int lineno)
{
char *body = authz_trim(line + 3);
char *sp = body;
char *key, *val, *repo;
while (*sp != '\0' && !isspace((unsigned char)*sp))
sp++;
if (*sp == '\0') {
perr(p, lineno, "set needs a value");
return;
}
key = xsubstr(body, (size_t)(sp - body));
val = authz_trim(sp);
if (strcmp(key, "protected-refs") == 0) {
if (p->protected_global != NULL)
pwarn(p, lineno, "set protected-refs appears twice; the last one wins");
free(p->protected_global);
p->protected_global = norm_ref_list(val);
} else if (strncmp(key, "protected-refs.", 15) == 0) {
repo = authz_norm_repo(key + 15);
if (repo[0] == '\0' || strcmp(repo, "*") == 0) {
perr(p, lineno, "protected-refs. needs one repository, not \"%s\"", key + 15);
} else {
set_protected_for(p, repo, val, lineno);
}
free(repo);
} else {
perr(p, lineno, "unknown setting \"%s\"", key);
}
free(key);
}
static void parse_rule(policy_t *p, char *line, int lineno)
{
char *fields[MAX_FIELDS];
rule_t *r;
int n, i, level;
n = authz_split_ws(line, fields, MAX_FIELDS);
if (n > MAX_FIELDS) {
perr(p, lineno, "more than %d fields on one line", MAX_FIELDS);
return;
}
if (n < 3) {
perr(p, lineno, "expected \"<user> <repo> <level> [ref ...]\": %s", line);
return;
}
level = authz_level_of(fields[2]);
if (level < 0) {
perr(p, lineno, "unknown access level \"%s\" (write force admin)", fields[2]);
return;
}
if (strcmp(fields[0], "@all") != 0 && !authz_user_pattern_ok(fields[0])) {
perr(p, lineno, "invalid user pattern \"%s\"", fields[0]);
return;
}
if (!authz_repo_pattern_ok(fields[1])) {
perr(p, lineno, "invalid repo pattern \"%s\" (flat names only, no slashes)",
fields[1]);
return;
}
for (i = 3; i < n; i++) {
if (fields[i][0] == '!' || fields[i][0] == '-') {
perr(p, lineno, "ref patterns are additive, there is no deny: %s", fields[i]);
return;
}
}
r = rule_slot(p);
r->user = xstrdup(fields[0]);
r->repo = authz_norm_repo(fields[1]);
r->level = (level_t)level;
r->line = lineno;
if (n > 3) {
r->refs = xmalloc((size_t)(n - 3) * sizeof(*r->refs));
for (i = 3; i < n; i++)
r->refs[r->nrefs++] = authz_norm_ref(fields[i]);
}
}
void policy_init(policy_t *p, const char *path)
{
memset(p, 0, sizeof(*p));
p->path = xstrdup(path);
}
void policy_free(policy_t *p)
{
size_t i;
size_t j;
for (i = 0; i < p->nrules; i++) {
free(p->rules[i].user);
free(p->rules[i].repo);
for (j = 0; j < p->rules[i].nrefs; j++)
free(p->rules[i].refs[j]);
free(p->rules[i].refs);
}
free(p->rules);
free(p->protected_global);
for (i = 0; i < p->nprot; i++) {
free(p->prot_repo[i]);
free(p->prot_refs[i]);
}
free(p->prot_repo);
free(p->prot_refs);
free(p->path);
memset(p, 0, sizeof(*p));
}
int policy_load(policy_t *p)
{
FILE *f;
char *line = NULL;
size_t cap = 0;
ssize_t len;
int lineno = 0;
f = fopen(p->path, "r");
if (f == NULL) {
p->unreadable = 1;
fprintf(stderr, "%s: cannot read the policy: ", p->path);
perror("");
return -2;
}
while ((len = getline(&line, &cap, f)) != -1) {
char *hash;
char *text;
lineno++;
text = authz_trim(line);
hash = strchr(text, '#');
if (hash != NULL) {
*hash = '\0';
text = authz_trim(text);
}
if (*text == '\0')
continue;
if (strncmp(text, "set", 3) == 0 && isspace((unsigned char)text[3]))
parse_set(p, text, lineno);
else
parse_rule(p, text, lineno);
}
free(line);
fclose(f);
return p->errors > 0 ? -1 : 0;
}
/* ------------------------------------------------------------- the answers */
static int user_match(const policy_t *p, const char *pat, const char *user)
{
if (user == NULL || user[0] == '\0')
return 0;
if (strcmp(pat, "@all") == 0)
return authz_user_known(p, user);
return glob_match(pat, user);
}
static int repo_match(const char *pat, const char *repo)
{
char *want;
int hit;
if (strcmp(pat, "*") == 0)
return 1;
want = authz_norm_repo(repo);
hit = glob_match(pat, want);
free(want);
return hit;
}
int authz_user_known(const policy_t *p, const char *user)
{
size_t i;
if (user == NULL || user[0] == '\0')
return 0;
for (i = 0; i < p->nrules; i++) {
if (strcmp(p->rules[i].user, "@all") == 0)
continue;
if (glob_match(p->rules[i].user, user))
return 1;
}
return 0;
}
int authz_repo_reachable(const policy_t *p, const char *repo)
{
size_t i;
for (i = 0; i < p->nrules; i++) {
if (repo_match(p->rules[i].repo, repo))
return 1;
}
return 0;
}
const char *authz_protected_for(const policy_t *p, const char *repo)
{
char *want;
size_t i;
want = authz_norm_repo(repo);
for (i = 0; i < p->nprot; i++) {
if (strcmp(p->prot_repo[i], want) == 0) {
free(want);
return p->prot_refs[i];
}
}
free(want);
if (p->protected_global != NULL && p->protected_global[0] != '\0')
return p->protected_global;
return DEFAULT_PROTECTED;
}
static int is_protected(const policy_t *p, const char *repo, const char *ref)
{
const char *list = authz_protected_for(p, repo);
char *copy = xstrdup(list);
char *fields[MAX_FIELDS];
int n, i, hit = 0;
n = authz_split_ws(copy, fields, MAX_FIELDS);
for (i = 0; i < n; i++) {
if (glob_match(fields[i], ref)) {
hit = 1;
break;
}
}
free(copy);
return hit;
}
static int ordinary_ref(const char *ref)
{
return strncmp(ref, "refs/heads/", 11) == 0 || strncmp(ref, "refs/tags/", 10) == 0;
}
level_t authz_need_for(const policy_t *p, const char *repo, const char *ref,
level_t have, char *raised, size_t raisedsz)
{
if (raised != NULL && raisedsz > 0)
raised[0] = '\0';
/* Anything that is not a branch or a tag is for maintainers: refs/notes,
* refs/merge-requests, whatever a tool invents next. */
if (!ordinary_ref(ref) && have != LV_ADMIN) {
if (raised != NULL)
snprintf(raised, raisedsz, "neither a branch nor a tag");
return LV_ADMIN;
}
if (!is_protected(p, repo, ref))
return have;
if (have == LV_WRITE || have == LV_FORCE) {
if (raised != NULL)
snprintf(raised, raisedsz, "protected");
return have == LV_WRITE ? LV_FORCE : LV_ADMIN;
}
return have;
}
/* Everything one user may do to one repository at or above 'min': whether any
* of it is unscoped, whether 'ref' is covered, and the strongest level the user
* holds there at all (which is what a denial names). */
typedef struct {
int unscoped; /* a rule with no ref list: the whole repository */
int scoped; /* a rule with a ref list, at or above the level */
int hit; /* and one of its patterns is this ref */
level_t strongest; /* the best this user holds there at all */
level_t here; /* the best held on this ref specifically */
int any; /* any rule at all, at any level */
} scope_t;
static void scope(const policy_t *p, const char *user, const char *repo,
level_t min, const char *ref, scope_t *s)
{
size_t i;
size_t j;
s->unscoped = 0;
s->scoped = 0;
s->hit = 0;
s->strongest = LV_NONE;
s->here = LV_NONE;
s->any = 0;
for (i = 0; i < p->nrules; i++) {
if (!user_match(p, p->rules[i].user, user))
continue;
if (!repo_match(p->rules[i].repo, repo))
continue;
s->any = 1;
if (p->rules[i].level > s->strongest)
s->strongest = p->rules[i].level;
/* What this rule says about the ref in question, whatever its level: a
* refusal has to name the level the user actually holds on this ref,
* because "carol has force on team" while refusing her a force push to
* main is a sentence that sends somebody to open the file and stare. */
if (ref != NULL) {
level_t covers = LV_NONE;
if (p->rules[i].nrefs == 0)
covers = p->rules[i].level;
for (j = 0; j < p->rules[i].nrefs; j++) {
if (glob_match(p->rules[i].refs[j], ref)) {
covers = p->rules[i].level;
break;
}
}
if (covers > s->here)
s->here = covers;
}
if (p->rules[i].level < min)
continue;
if (p->rules[i].nrefs == 0) {
s->unscoped = 1;
continue;
}
s->scoped = 1;
for (j = 0; ref != NULL && j < p->rules[i].nrefs; j++) {
if (glob_match(p->rules[i].refs[j], ref)) {
s->hit = 1;
break;
}
}
}
}
void authz_check(const policy_t *p, const char *user, const char *repo,
level_t need, const char *ref, decision_t *out)
{
scope_t s;
char raised[64];
char *nref = NULL;
level_t want = need;
out->allowed = 0;
out->need = need;
out->why[0] = '\0';
if (user == NULL || user[0] == '\0') {
out->need = need;
snprintf(out->why, sizeof(out->why), "no identity was presented");
return;
}
if (ref == NULL) {
/* No ref named: "may this user do this somewhere in the repository?"
* This is the question 'can this person push anything at all here', and
* it is answered by whether any rule reaches the repository, not by
* which refs a scoped rule would cover. */
char *r = authz_norm_repo(repo);
scope(p, user, repo, need, NULL, &s);
if (s.unscoped || s.scoped) {
snprintf(out->why, sizeof(out->why), "%s has %s on %s of %s",
user, authz_level_name(need),
s.unscoped ? "all" : "some refs", r);
out->allowed = 1;
} else {
snprintf(out->why, sizeof(out->why), "%s has no %s on %s",
user, authz_level_name(need), r);
}
free(r);
return;
}
nref = authz_norm_ref(ref);
want = authz_need_for(p, repo, nref, need, raised, sizeof(raised));
out->need = want;
scope(p, user, repo, want, nref, &s);
if (s.unscoped || s.hit) {
char *r = authz_norm_repo(repo);
if (raised[0] != '\0')
snprintf(out->why, sizeof(out->why),
"%s has %s on %s %s (raised from %s: %s is %s)",
user, authz_level_name(want), r, nref,
authz_level_name(need), nref, raised);
else
snprintf(out->why, sizeof(out->why), "%s has %s on %s %s",
user, authz_level_name(want), r, nref);
free(r);
out->allowed = 1;
free(nref);
return;
}
{
char *r = authz_norm_repo(repo);
if (!s.any)
snprintf(out->why, sizeof(out->why), "%s has no rule for %s; %s needs %s",
user, r, nref, authz_level_name(want));
else if (s.here == LV_NONE)
snprintf(out->why, sizeof(out->why),
"%s has %s on %s, and nothing at all on %s; it needs %s%s%s%s",
user, authz_level_name(s.strongest), r, nref,
authz_level_name(want),
raised[0] != '\0' ? " (raised: " : "",
raised[0] != '\0' ? raised : "",
raised[0] != '\0' ? ")" : "");
else if (raised[0] != '\0')
snprintf(out->why, sizeof(out->why),
"%s has %s on %s %s; it needs %s (raised from %s: %s)",
user, authz_level_name(s.here), r, nref,
authz_level_name(want), authz_level_name(need), raised);
else
snprintf(out->why, sizeof(out->why), "%s has %s on %s %s; it needs %s",
user, authz_level_name(s.here), r, nref, authz_level_name(want));
free(r);
}
free(nref);
}
|