diff options
Diffstat (limited to 'src/policy.c')
| -rw-r--r-- | src/policy.c | 164 |
1 files changed, 26 insertions, 138 deletions
diff --git a/src/policy.c b/src/policy.c index 1866bb2..ef429e0 100644 --- a/src/policy.c +++ b/src/policy.c @@ -2,8 +2,6 @@ * * 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 @@ -92,16 +90,6 @@ static void perr(policy_t *p, int line, const char *fmt, ...) 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) @@ -203,29 +191,6 @@ char *authz_norm_ref(const char *in) } /* 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; @@ -239,35 +204,11 @@ static rule_t *rule_slot(policy_t *p) 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; + char *key; while (*sp != '\0' && !isspace((unsigned char)*sp)) sp++; @@ -276,24 +217,19 @@ static void parse_set(policy_t *p, char *line, int lineno) 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 { + + /* protected-refs was removed, and it is named rather than left to "unknown + * setting" on purpose. A policy that still carries one of these lines has + * to be refused rather than read as if the line were a comment: ignoring it + * would leave every branch one fast-forward away from whoever holds 'write' + * on them, which is the opposite of what the line was there to prevent, and + * the operator would learn that from a push instead of from the parser. */ + 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); } @@ -364,13 +300,6 @@ void policy_free(policy_t *p) 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)); } @@ -464,67 +393,26 @@ int authz_repo_reachable(const policy_t *p, const char *repo) 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) +level_t authz_need_for(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. */ + /* Anything that is not a branch or a tag is for maintainers, whatever a rule + * appears to grant about it. Nothing else raises a price here: a rule says + * what it says, and there is no second, invisible list that can make a ref + * cost more behind the operator's back. */ 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; } @@ -637,7 +525,7 @@ void authz_check(const policy_t *p, const char *user, const char *repo, } nref = authz_norm_ref(ref); - want = authz_need_for(p, repo, nref, need, raised, sizeof(raised)); + want = authz_need_for(nref, need, raised, sizeof(raised)); out->need = want; scope(p, user, repo, want, nref, &s); @@ -647,9 +535,9 @@ void authz_check(const policy_t *p, const char *user, const char *repo, if (raised[0] != '\0') snprintf(out->why, sizeof(out->why), - "%s has %s on %s %s (raised from %s: %s is %s)", + "%s has %s on %s %s (this ref costs %s, not %s: %s)", user, authz_level_name(want), r, nref, - authz_level_name(need), nref, raised); + 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); @@ -667,17 +555,17 @@ void authz_check(const policy_t *p, const char *user, const char *repo, 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", + "%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: " : "", + 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 (raised from %s: %s)", + "%s has %s on %s %s; it needs %s (%s)", user, authz_level_name(s.here), r, nref, - authz_level_name(want), authz_level_name(need), raised); + 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)); |
