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/main.c | 271 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 271 insertions(+) create mode 100644 src/main.c (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..1d7879b --- /dev/null +++ b/src/main.c @@ -0,0 +1,271 @@ +/* main.c - one program, four jobs. + * + * check an operator asking, or a script + * authorize git asking, once per push (also reached as 'pre-receive') + * validate the entrypoint and CI asking before anything is served + * edit changing the file (grant, revoke, protect) + * + * They are one program because they must agree, exactly, about what a rule + * means: a check that answers differently from the hook is worse than two + * programs, it is a program that lies. The exit status is the interface, and + * it never changes: 0 allowed, 1 denied, 3 no usable policy, 64 usage. + */ +#include "authz.h" + +#include +#include +#include + +#define POLICY_ENV "GIT_HOST_AUTHZ" +#define DEFAULT_POLICY "/etc/git-host/authz.conf" +#define DEFAULT_ROOT "/var/lib/git" +#define DEFAULT_HOOKS "/usr/local/share/git-host/hooks" +#define DEFAULT_GITCONFIG "/etc/gitconfig" +#define DEFAULT_SSHD "/etc/ssh/sshd_config" +#define DEFAULT_PASSWD "/etc/passwd" +#define DEFAULT_KEYS "/run/git-host/secrets/authorized_keys" +#define DEFAULT_HTPASSWD "/etc/git-host/secrets/htpasswd" + +static void usage(FILE *f) +{ + fprintf(f, +"usage: git-authz [--policy PATH] [arguments]\n" +"\n" +" check [ref]\n" +" one question: allow or deny on stdout, the reason with it.\n" +" Without a ref it asks about the repository as a whole.\n" +"\n" +" authorize read a push exactly as git's receive hook does - identity from\n" +" GIT_HOST_USER (ssh) or REMOTE_USER (http), one \"old new ref\" line per\n" +" ref on stdin - and deny the push on the first ref it may not allow.\n" +" This also runs as 'pre-receive', which is how core.hooksPath finds it.\n" +" --user U --repo R override the identity and the repository\n" +" --ff auto|yes|no whether to ask git if old is an ancestor of new\n" +" --head auto|none|REF the default branch, or how to treat deletions\n" +" --no-git ask git nothing (an unknown HEAD is not guessed)\n" +" --explain report the allows too\n" +"\n" +" validate is the policy parseable, and is the host wired to run it?\n" +" --no-host only the policy, none of the wiring below\n" +" --root DIR repositories (default %s)\n" +" --hook-dir DIR hooks (default %s)\n" +" --gitconfig FILE (default %s)\n" +" --sshd-config FILE (default %s)\n" +" --passwd FILE (default %s)\n" +" --keys FILE (default %s)\n" +" --htpasswd FILE (default %s)\n" +" --strict warnings are failures too\n" +"\n" +" grant [ref ...] add one rule, or replace that pair's\n" +" revoke take it away again\n" +" protect [--repo R] ... set which refs need an extra level\n" +" --out FILE | --stdout write elsewhere instead of in place\n" +" The deployed policy is a read-only mount on purpose; these are for the\n" +" source copy, and they say so when they are pointed at a read-only file.\n" +"\n" +"Levels are monotone: admin implies force implies write. '@all' means everyone\n" +"the policy already names. \"main\", \"heads/main\" and \"refs/heads/main\" are the\n" +"same branch; a bare name is always a branch and never a tag.\n" +"\n" +"Exit status: 0 allowed, 1 denied, 3 the policy could not be read or does not\n" +"parse - nothing is allowed on a policy that cannot be read - and 64 for usage.\n", +DEFAULT_ROOT, DEFAULT_HOOKS, DEFAULT_GITCONFIG, DEFAULT_SSHD, DEFAULT_PASSWD, +DEFAULT_KEYS, DEFAULT_HTPASSWD); +} + +static int one_option(int argc, char **argv, int *i, const char *name, const char **slot) +{ + size_t n = strlen(name); + + if (strcmp(argv[*i], name) == 0) { + if (*i + 1 >= argc) { + fprintf(stderr, "%s: %s wants a value\n", progname, name); + return -1; + } + *slot = argv[++*i]; + return 1; + } + if (strncmp(argv[*i], name, n) == 0 && argv[*i][n] == '=') { + *slot = argv[*i] + n + 1; + return 1; + } + return 0; +} + +/* Called when the policy is not usable: the only safe answer is no. */ +static int deny_policy(const char *path, int rc) +{ + fprintf(stderr, "%s: deny: the policy at %s %s\n", progname, path, + rc == -2 ? "cannot be read" : "does not parse"); + return 3; +} + +int main(int argc, char **argv) +{ + const char *path; + const char *base; + const char *cmd; + policy_t p; + hook_opts ho; + validate_opts vo; + edit_opts eo; + int i; + int rc; + + base = strrchr(argv[0], '/'); + progname = base != NULL ? base + 1 : argv[0]; + + path = getenv(POLICY_ENV); + if (path == NULL || path[0] == '\0') + path = DEFAULT_POLICY; + + memset(&ho, 0, sizeof(ho)); + ho.use_git = 1; + memset(&vo, 0, sizeof(vo)); + vo.root = DEFAULT_ROOT; + vo.hook_dir = DEFAULT_HOOKS; + vo.gitconfig = DEFAULT_GITCONFIG; + vo.sshd_config = DEFAULT_SSHD; + vo.passwd = DEFAULT_PASSWD; + vo.keys = DEFAULT_KEYS; + vo.htpasswd = DEFAULT_HTPASSWD; + memset(&eo, 0, sizeof(eo)); + + /* Reached as the hook itself: git's arguments to a hook are not ours, so + * they are ignored, and the policy comes from the environment. */ + if (strcmp(progname, "pre-receive") == 0) { + policy_init(&p, path); + rc = policy_load(&p); + if (rc != 0) + return deny_policy(path, rc); + return cmd_authorize(&p, ho, 0, NULL); + } + + i = 1; + while (i < argc && argv[i][0] == '-' && argv[i][1] != '\0') { + int got = one_option(argc, argv, &i, "--policy", &path); + + if (got < 0) + return 64; + if (got == 0 && (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)) { + usage(stdout); + return 0; + } + if (got == 0) { + fprintf(stderr, "%s: unknown option before the command: %s\n", progname, + argv[i]); + usage(stderr); + return 64; + } + i++; + } + if (i >= argc) { + usage(stderr); + return 64; + } + cmd = argv[i++]; + argc -= i; + argv += i; + + if (strcmp(cmd, "help") == 0 || strcmp(cmd, "-h") == 0 || + strcmp(cmd, "--help") == 0) { + usage(stdout); + return 0; + } + + if (strcmp(cmd, "grant") == 0 || strcmp(cmd, "revoke") == 0 || + strcmp(cmd, "protect") == 0) { + int consumed = 0; + + if (authz_parse_out(argc, argv, &eo, &consumed) != 0) + return 64; + argc -= consumed; + argv += consumed; + if (strcmp(cmd, "grant") == 0) + return cmd_grant(path, eo, argc, argv); + if (strcmp(cmd, "revoke") == 0) + return cmd_revoke(path, eo, argc, argv); + return cmd_protect(path, eo, argc, argv); + } + + /* Everything else answers from the policy, so read it once, here. */ + policy_init(&p, path); + rc = policy_load(&p); + + if (strcmp(cmd, "validate") == 0) { + int j; + + for (j = 0; j < argc; j++) { + int got; + + got = one_option(argc, argv, &j, "--root", &vo.root); + if (got == 0) + got = one_option(argc, argv, &j, "--hook-dir", &vo.hook_dir); + if (got == 0) + got = one_option(argc, argv, &j, "--gitconfig", &vo.gitconfig); + if (got == 0) + got = one_option(argc, argv, &j, "--sshd-config", &vo.sshd_config); + if (got == 0) + got = one_option(argc, argv, &j, "--passwd", &vo.passwd); + if (got == 0) + got = one_option(argc, argv, &j, "--keys", &vo.keys); + if (got == 0) + got = one_option(argc, argv, &j, "--htpasswd", &vo.htpasswd); + if (got < 0) + return 64; + if (got == 0 && strcmp(argv[j], "--strict") == 0) { + vo.strict = 1; + continue; + } + if (got == 0 && strcmp(argv[j], "--no-host") == 0) { + vo.root = NULL; + vo.hook_dir = NULL; + vo.sshd_config = NULL; + vo.passwd = NULL; + vo.keys = NULL; + vo.htpasswd = NULL; + vo.gitconfig = NULL; + continue; + } + if (got == 0) { + fprintf(stderr, "%s: validate: unknown argument %s\n", progname, + argv[j]); + return 64; + } + } + /* cmd_validate reports the parse errors itself: a policy that does not + * parse is the finding, not a reason to say nothing. */ + return cmd_validate(&p, vo); + } + + if (strcmp(cmd, "authorize") == 0) { + if (rc != 0) + return deny_policy(path, rc); + return cmd_authorize(&p, ho, argc, argv); + } + + if (strcmp(cmd, "check") == 0) { + decision_t d; + int level; + + if (rc != 0) + return deny_policy(path, rc); + if (argc < 3 || argc > 4) { + fprintf(stderr, "%s: check wants [ref]\n", progname); + return 64; + } + level = authz_level_of(argv[2]); + if (level < 0) { + fprintf(stderr, "%s: unknown access level \"%s\" (write force admin)\n", + progname, argv[2]); + return 64; + } + authz_check(&p, argv[0], argv[1], (level_t)level, argc > 3 ? argv[3] : NULL, &d); + printf("%s: %s\n", d.allowed ? "allow" : "deny", d.why); + return d.allowed ? 0 : 1; + } + + fprintf(stderr, "%s: unknown command \"%s\"\n", progname, cmd); + usage(stderr); + return 64; +} -- cgit v1.2.3