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
|
// <user> <repo> <write|force|admin> [<ref pattern> ...]
//
// '#' starts a comment anywhere on a line. Rules are additive and the strongest
// wins. There is no 'deny': a rule is taken away by editing the file, not by
// ordering it after another one.
//
// A line that does not parse makes the whole file unusable. Every push refused
// for an evening is better than a mistyped line quietly widening access.
#include "authz.h"
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Spelled out rather than as a regular expression: the engine has none.
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);
}
// 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;
if (n == max)
return n + 1; // "too many"; the caller decides
f[n++] = p;
while (*p != '\0' && !isspace((unsigned char)*p))
p++;
if (*p != '\0')
*p++ = '\0';
}
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);
}
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;
}
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;
}
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: "main", "heads/main" and "refs/heads/main" are one
// branch, and 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" and friends are half a refname. Anything else, slashes
// included, 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;
}
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 parse_set(policy_t *p, char *line, int lineno)
{
char *body = authz_trim(line + 3);
char *sp = body;
char *key;
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));
// Refused by name, not left to "unknown setting": a policy that still
// carries this line must not be read as if the line were a comment, which
// would hand every branch to whoever holds write on it.
if (strncmp(key, "protected-refs", 14) == 0)
perr(p, lineno, "protected-refs no longer exists: name the refs a rule "
"covers instead, for example \"dave team admin refs/heads/main\"");
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->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);
// A read that stopped because the file would not give an answer is not an
// empty policy: a directory, an I/O error halfway, a mount pulled out. The
// lines read so far are not the file, so this is the broken-host answer (3),
// not the empty-policy answer (0).
if (ferror(f)) {
fclose(f);
p->unreadable = 1;
fprintf(stderr, "%s: cannot read the policy: ", p->path);
perror("");
return -2;
}
fclose(f);
return p->errors > 0 ? -1 : 0;
}
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;
}
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 char *ref, level_t have, char *raised,
size_t raisedsz)
{
if (raised != NULL && raisedsz > 0)
raised[0] = '\0';
// Neither a branch nor a tag is admin whatever a rule appears to grant.
// Nothing else raises a price: a rule says what it says.
if (!ordinary_ref(ref) && have != LV_ADMIN) {
if (raised != NULL)
snprintf(raised, raisedsz, "neither a branch nor a tag");
return LV_ADMIN;
}
return have;
}
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 in the repository 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 this ref, whatever its level: a refusal
// that names a level held elsewhere sends somebody to open the file
// and stare at it.
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: can this person push anything here at all? 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(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 (this ref costs %s, not %s: %s)",
user, authz_level_name(want), r, nref,
authz_level_name(want), authz_level_name(need), 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 holds %s on %s, but not on %s; that ref needs %s%s%s%s",
user, authz_level_name(s.strongest), r, nref,
authz_level_name(want),
raised[0] != '\0' ? " (" : "",
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 (%s)",
user, authz_level_name(s.here), r, nref,
authz_level_name(want), 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);
}
|