diff options
| author | dev <dev@example> | 2026-11-15 11:28:47 +0000 |
|---|---|---|
| committer | dev <dev@example> | 2026-11-15 11:28:47 +0000 |
| commit | c2b95afc6aefbab69133a202cab95a50a2df82fc (patch) | |
| tree | 4788b1b9302ef77b244c4107de2f3da15982f303 /src/policy.c | |
| parent | 5a9afae082f36812a2e7c9e61d8662628b798f41 (diff) | |
| download | access-control-c2b95afc6aefbab69133a202cab95a50a2df82fc.tar.gz access-control-c2b95afc6aefbab69133a202cab95a50a2df82fc.zip | |
Drop grant/revoke and validate's host-wiring checks
Two features out, to answer a size target: src/ and cmd/ go from 2,306
non-blank lines to 1,495.
`grant` and `revoke` were a convenience wrapper over a file that is a
read-only mount in production by design, so they sat outside the
enforcement path. src/edit.c is gone; both names now report an unknown
command with exit 64.
`validate` keeps the policy checks - parse errors with their line
numbers, an unreadable policy, the @all-whole-repo and not-readable-by-
the-git-account warnings - and stops cross-checking the machine around
it. The repository cross-check, hook/hooksPath, sshd, the git account's
shell and the key/htpasswd identity checks are deleted, along with
--root, --hook-dir, --gitconfig, --sshd-config, --passwd, --keys,
--htpasswd and --no-host; plain `validate` is what `validate --no-host`
used to be. The container's build gate and the test suite are updated to
match, since both called the removed flag.
Orphaned by the removals and deleted with them: authz_repo_reachable,
and authz_rule_validate's ref_ok parameter, which existed only so grant
could interleave its own ref-pattern test.
Surviving behaviour is unchanged. A 1,370-invocation differential run
against the pre-removal binary - the check matrix, every authorize path,
the hook reached through its own name, fake-git ancestry and HEAD,
policy-only validate, the failure modes - differs only in the usage text
that documented the removed surface. make strict, make test and
make sanitizer are all clean, with ASan and UBSan silent.
Also folded in from the same effort, since it touched the same files:
authz_rule_validate as the single grammar, rule_covers() as the single
answer for rule/ref coverage, vmsgv() behind the validate message tags,
one kv reader for gitconfig and sshd_config while both were still there,
and `sanitizer:` in the Makefile appending to CFLAGS rather than
replacing it.
Diffstat (limited to 'src/policy.c')
| -rw-r--r-- | src/policy.c | 211 |
1 files changed, 101 insertions, 110 deletions
diff --git a/src/policy.c b/src/policy.c index 657e0c9..b2d4348 100644 --- a/src/policy.c +++ b/src/policy.c @@ -44,6 +44,23 @@ char *authz_trim(char *s) return s; } +// The body of a policy line: trimmed, with its comment and everything after it +// gone. Both the parser and the editor split what is left on whitespace, and the +// second trim is what keeps "rule # note" from leaving a trailing space where a +// field would be. +char *authz_line_body(char *line) +{ + char *hash; + char *text = authz_trim(line); + + hash = strchr(text, '#'); + if (hash != NULL) { + *hash = '\0'; + text = authz_trim(text); + } + return text; +} + int authz_split_ws(char *line, char **f, int max) { int n = 0; @@ -65,22 +82,17 @@ int authz_split_ws(char *line, char **f, int max) 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++; + fprintf(stderr, "%s:%d: error: ", + p->path != NULL ? p->path : "authz.conf", line); va_start(ap, fmt); - report(p, line, "error", fmt, ap); + vfprintf(stderr, fmt, ap); va_end(ap); + fputc('\n', stderr); } int authz_user_pattern_ok(const char *s) @@ -218,9 +230,31 @@ static void parse_set(policy_t *p, char *line, int lineno) free(key); } +int authz_rule_validate(const char *user, const char *repo, const char *level, + char *const *refs, int nrefs, char *msg, size_t msgs) +{ + int i; + + if (authz_level_of(level) < 0) + return snprintf(msg, msgs, + "unknown access level \"%s\" (write force admin)", level); + if (user != NULL && strcmp(user, "@all") != 0 && !authz_user_pattern_ok(user)) + return snprintf(msg, msgs, "invalid user pattern \"%s\"", user); + if (repo != NULL && !authz_repo_pattern_ok(repo)) + return snprintf(msg, msgs, + "invalid repo pattern \"%s\" (flat names only, no slashes)", repo); + for (i = 0; i < nrefs; i++) { + if (refs[i][0] == '!' || refs[i][0] == '-') + return snprintf(msg, msgs, + "ref patterns are additive, there is no deny: %s", refs[i]); + } + return 0; +} + static void parse_rule(policy_t *p, char *line, int lineno) { char *fields[MAX_FIELDS]; + char msg[AUTHZ_MSGSZ]; rule_t *r; int n, i, level; @@ -233,26 +267,12 @@ static void parse_rule(policy_t *p, char *line, int lineno) 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]); + if (authz_rule_validate(fields[0], fields[1], fields[2], + fields + 3, n - 3, msg, sizeof(msg)) != 0) { + perr(p, lineno, "%s", msg); 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; - } - } + level = authz_level_of(fields[2]); r = rule_slot(p); r->user = xstrdup(fields[0]); @@ -310,16 +330,10 @@ int policy_load(policy_t *p) return -2; } for (i = 0; i < lines.n; i++) { - char *hash; char *text; lineno++; - text = authz_trim(lines.v[i]); - hash = strchr(text, '#'); - if (hash != NULL) { - *hash = '\0'; - text = authz_trim(text); - } + text = authz_line_body(lines.v[i]); if (*text == '\0') continue; @@ -369,17 +383,6 @@ int authz_user_known(const policy_t *p, const char *user) 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; @@ -412,11 +415,27 @@ typedef struct { } scope_t; +// What this rule says about this ref, whatever its level: an unscoped rule +// covers the whole repository, a scoped one only what its patterns name. +static level_t rule_covers(const rule_t *r, const char *ref) +{ + size_t j; + + if (ref == NULL) + return LV_NONE; + if (r->nrefs == 0) + return r->level; + for (j = 0; j < r->nrefs; j++) + if (glob_match(r->refs[j], ref)) + return r->level; + return LV_NONE; +} + 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; + level_t covers; s->unscoped = 0; s->scoped = 0; @@ -434,24 +453,13 @@ static void scope(const policy_t *p, const char *user, const char *repo, 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; - } + // A refusal that names a level held elsewhere sends somebody to open + // the file and stare at it, so what the rule says about this ref is + // kept whatever the rule's level is. + + covers = rule_covers(&p->rules[i], ref); + if (covers > s->here) + s->here = covers; if (p->rules[i].level < min) continue; @@ -460,12 +468,8 @@ static void scope(const policy_t *p, const char *user, const char *repo, 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; - } - } + if (covers != LV_NONE) + s->hit = 1; } } @@ -474,32 +478,34 @@ void authz_check(const policy_t *p, const char *user, const char *repo, { scope_t s; char raised[64]; - char *nref = NULL; - level_t want = need; + char tail[96]; + char *nref; + char *r; + level_t want; out->allowed = 0; out->need = need; out->why[0] = '\0'; + tail[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. + r = authz_norm_repo(repo); - char *r = authz_norm_repo(repo); + // 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. + if (ref == NULL) { scope(p, user, repo, need, NULL, &s); if (s.unscoped || s.scoped) { + out->allowed = 1; 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); @@ -511,12 +517,13 @@ void authz_check(const policy_t *p, const char *user, const char *repo, nref = authz_norm_ref(ref); want = authz_need_for(nref, need, raised, sizeof(raised)); out->need = want; + if (raised[0] != '\0') + snprintf(tail, sizeof(tail), " (%s)", raised); scope(p, user, repo, want, nref, &s); if (s.unscoped || s.hit) { - char *r = authz_norm_repo(repo); - + out->allowed = 1; if (raised[0] != '\0') snprintf(out->why, sizeof(out->why), "%s has %s on %s %s (this ref costs %s, not %s: %s)", @@ -525,35 +532,19 @@ void authz_check(const policy_t *p, const char *user, const char *repo, 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); + } else 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", + user, authz_level_name(s.strongest), r, nref, + authz_level_name(want), tail); + } else { + 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), tail); } free(nref); + free(r); } |
