From f87a792743227ea06cfa1580f8d30a618b4d1a04 Mon Sep 17 00:00:00 2001 From: dev Date: Fri, 13 Nov 2026 19:04:50 +0000 Subject: git-authz: one C program for who may move which refs A git host that anonymous-clones and shared-pushes has one question left to answer: which human may move which ref. This answers it from one plain file, in one binary, with no runtime beyond libc. Four modes, because they must agree exactly about what a rule means - a check that answers differently from the hook is a program that lies: check one question, for an operator or a script authorize a whole push, the way core.hooksPath hands it over: identity from GIT_HOST_USER (ssh key option) or REMOTE_USER (http auth), and one "old new ref" line per ref on stdin. Also runs as "pre-receive", which is how git finds it. validate does the policy parse, and is the host wired to run it edit grant, revoke, protect (the deployed policy is a read-only mount on purpose, so these are for the source copy and say so when told) The verdict is the exit status, since the caller is a hook: 0 allowed, 1 denied, 3 no usable policy, 64 usage. A policy that cannot be read or does not parse denies everything rather than guessing, and one bad line poisons the whole file on purpose: a mistyped rule that quietly widens access is worse than an evening of refused pushes. Semantics kept from the shell engine this replaces: levels are monotone (admin implies force implies write), rules add up rather than order, "@" is only everyone the policy already names, protected refs cost one step more than the action asked for, and refs that are neither branches nor tags are for admins. "main", "heads/main" and "refs/heads/main" are one branch; a bare name is never a tag. Two facts a push does not carry - is old an ancestor of new, and which branch HEAD points at - are asked of git, and an answer that cannot be had is taken the expensive way. tests/unit.c covers the matcher directly (173 checks); the behavioural suite that drives this the way git does follows next. --- src/edit.c | 516 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 516 insertions(+) create mode 100644 src/edit.c (limited to 'src/edit.c') diff --git a/src/edit.c b/src/edit.c new file mode 100644 index 0000000..4193b44 --- /dev/null +++ b/src/edit.c @@ -0,0 +1,516 @@ +/* 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 +#include +#include +#include +#include +#include +#include +#include + +#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 = dirname(dirtext); + char *template; + struct stat st; + int fd; + size_t i; + int had = stat(path, &st) == 0; + + template = xmalloc(strlen(dir) + strlen(TEMPLATE_SUFFIX) + 2); + sprintf(template, "%s/.authz%s", dir, 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. */ +static int refuse_broken(const char *path) +{ + policy_t probe; + int rc; + + policy_init(&probe, path); + rc = policy_load(&probe); + policy_free(&probe); + if (rc == -2) + return 3; + if (rc == -1) { + fprintf(stderr, "%s: %s does not parse; fix it before editing\n", progname, path); + return 3; + } + return 0; +} + +/* Parse the shared options of the three editing commands. + * Returns -1 and prints a message on an unknown argument. */ +int authz_parse_out(int argc, char **argv, edit_opts *o, int *consumed) +{ + int n = 0; + + while (n < argc && strncmp(argv[n], "--", 2) == 0) { + if (strcmp(argv[n], "--stdout") == 0) { + o->out = "-"; + } else if (strncmp(argv[n], "--out=", 6) == 0) { + o->out = argv[n] + 6; + } else { + fprintf(stderr, "%s: unknown option %s\n", progname, argv[n]); + return -1; + } + n++; + } + *consumed = n; + return 0; +} + +static int finish(const lines_t *l, const char *path, edit_opts o, const char *what) +{ + 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); + return 0; + } + if (write_atomic(l, o.out != NULL ? o.out : path) != 0) + return 1; + printf("%s: %s: %s\n", progname, what, o.out != NULL ? o.out : path); + return 0; +} + +/* ------------------------------------------------------------------ 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; + + if (argc < 3) { + fprintf(stderr, "%s: grant wants " + " [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 (refuse_broken(path) != 0) + return 3; + 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; + else + dropped++; + } + + if (first < 0) { + lines_push(&lines, text); + replaced = (int)lines.n; + } else if (dropped == 0) { + free(lines.v[first]); + lines.v[first] = text; + replaced = first + 1; + } else { + lines_t kept; + + lines_init(&kept); + for (i = 0; i < (int)lines.n; i++) { + if (i == first) { + lines_push(&kept, text); + continue; + } + if (is_rule_for(lines.v[i], argv[0], argv[1])) { + free(lines.v[i]); + continue; + } + lines_push(&kept, lines.v[i]); + } + free(lines.v); + lines = kept; + replaced = first + 1; + } + + { + char what[128]; + + 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; + lines_t kept; + int i; + int gone = 0; + + if (argc != 2) { + fprintf(stderr, "%s: revoke wants \n", progname); + return 64; + } + if (refuse_broken(path) != 0) + return 3; + if (read_lines(path, &lines) != 0) + return 3; + + lines_init(&kept); + for (i = 0; i < (int)lines.n; i++) { + if (is_rule_for(lines.v[i], argv[0], argv[1])) { + free(lines.v[i]); + gone++; + continue; + } + lines_push(&kept, lines.v[i]); + } + if (gone == 0) { + fprintf(stderr, "%s: no rule for %s on %s to remove\n", progname, argv[0], + argv[1]); + lines_free(&lines); + lines_free(&kept); + return 1; + } + { + char what[128]; + + snprintf(what, sizeof(what), "revoked %s on %s (%d rule(s) removed)", argv[0], + argv[1], gone); + return finish(&kept, 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]; + strbuf_t text; + int i; + int replaced = -1; + + if (argc < 1) { + fprintf(stderr, "%s: protect wants [--repo ] ...\n", + progname); + return 64; + } + if (strcmp(argv[0], "--repo") == 0) { + if (argc < 2) { + fprintf(stderr, "%s: --repo wants a repository name\n", progname); + return 64; + } + repo = argv[1]; + argv += 2; + argc -= 2; + if (argc < 1) { + fprintf(stderr, "%s: protect wants at least one 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][0] == '-') { + fprintf(stderr, "%s: ref patterns are additive, there is no deny: %s\n", + progname, argv[i]); + return 64; + } + } + if (refuse_broken(path) != 0) + return 3; + 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"); + + for (i = 0; i < (int)lines.n; i++) { + if (!is_set_for(lines.v[i], key)) + continue; + free(lines.v[i]); + lines.v[i] = text.s; + replaced = i + 1; + break; + } + if (replaced < 0) { + lines_push(&lines, text.s); + replaced = (int)lines.n; + } else { + free(text.s); + } + { + char what[160]; + + snprintf(what, sizeof(what), "protected refs for %s (%s, line %d)", + repo != NULL ? repo : "every repository", key, replaced); + return finish(&lines, path, o, what); + } +} -- cgit v1.2.3