summaryrefslogtreecommitdiffstats
path: root/src/hook.c
diff options
context:
space:
mode:
authordev <dev@example>2026-11-13 19:04:50 +0000
committerdev <dev@example>2026-11-13 19:04:50 +0000
commitf87a792743227ea06cfa1580f8d30a618b4d1a04 (patch)
tree7af494393b4a316f620bab7de79b15230df2cf36 /src/hook.c
downloadaccess-control-f87a792743227ea06cfa1580f8d30a618b4d1a04.tar.gz
access-control-f87a792743227ea06cfa1580f8d30a618b4d1a04.zip
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.
Diffstat (limited to 'src/hook.c')
-rw-r--r--src/hook.c381
1 files changed, 381 insertions, 0 deletions
diff --git a/src/hook.c b/src/hook.c
new file mode 100644
index 0000000..d4d801b
--- /dev/null
+++ b/src/hook.c
@@ -0,0 +1,381 @@
+/* hook.c - the mode git calls, and the only one a push depends on.
+ *
+ * This is what `core.hooksPath` points at. The shape is git's: an identity in
+ * the environment, one "old new ref" line per ref on stdin, and a non-zero exit
+ * to stop the whole push. Nothing is asked of the client and nothing is
+ * inferred from the transport, because ref names travel inside a compressed
+ * request body - the hook is the first place they exist as data.
+ *
+ * Two facts are not in the hook's input, and only the object store has them:
+ * whether "old -> new" is a fast-forward, and which branch HEAD points at.
+ * Those are asked of git, and an answer that cannot be had is taken the
+ * expensive way: a rewrite until proven a fast-forward, and any deletion of a
+ * branch treated as the default one while HEAD itself is unknown. Guessing the
+ * cheap way would be the hole.
+ */
+#include "authz.h"
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#define DEV_NULL "/dev/null"
+
+/* Run a program, optionally keeping the first line of its stdout.
+ *
+ * stdin is shut off from /dev/null: a child left holding the hook's stdin eats
+ * the ref lines that have not been read yet, and a push that loses refs from
+ * its own list is a push that skips checks. stderr goes to /dev/null too, so
+ * that a failed lookup does not shout "fatal:" at the person pushing about
+ * machinery they cannot touch. */
+static int spawn(const char *const argv[], int capture, char **out)
+{
+ int fds[2] = { -1, -1 };
+ int status = -1;
+ pid_t pid;
+
+ if (out != NULL)
+ *out = NULL;
+ if (capture && pipe(fds) != 0)
+ return -1;
+
+ pid = fork();
+ if (pid < 0) {
+ if (capture) {
+ close(fds[0]);
+ close(fds[1]);
+ }
+ return -1;
+ }
+ if (pid == 0) {
+ int devnull = open(DEV_NULL, O_RDWR);
+
+ if (devnull >= 0) {
+ dup2(devnull, STDIN_FILENO);
+ if (!capture)
+ dup2(devnull, STDOUT_FILENO);
+ dup2(devnull, STDERR_FILENO);
+ if (devnull > 2)
+ close(devnull);
+ }
+ if (capture) {
+ dup2(fds[1], STDOUT_FILENO);
+ close(fds[0]);
+ close(fds[1]);
+ }
+ execvp(argv[0], (char *const *)argv);
+ _exit(127);
+ }
+
+ if (capture) {
+ FILE *f;
+
+ close(fds[1]);
+ f = fdopen(fds[0], "r");
+ if (f != NULL) {
+ char *line = NULL;
+ size_t cap = 0;
+
+ if (getline(&line, &cap, f) != -1) {
+ size_t n = strlen(line);
+
+ while (n > 0 && (line[n - 1] == '\n' || line[n - 1] == '\r'))
+ line[--n] = '\0';
+ *out = line;
+ } else {
+ free(line);
+ }
+ fclose(f);
+ } else {
+ close(fds[0]);
+ }
+ }
+
+ while (waitpid(pid, &status, 0) == -1)
+ ;
+ if (WIFEXITED(status))
+ return WEXITSTATUS(status);
+ return -1;
+}
+
+static int is_zero(const char *s)
+{
+ int i;
+
+ if (s == NULL || s[0] == '\0')
+ return 0;
+ for (i = 0; s[i] != '\0'; i++) {
+ if (s[i] != '0')
+ return 0;
+ }
+ return 1;
+}
+
+/* Object ids are 40 or 64 hex digits, or all zeroes for "does not exist".
+ * Anything else is a protocol oddity, and an oddity gets denied rather than
+ * interpreted. */
+static int object_id_ok(const char *s)
+{
+ size_t i;
+ int hex = 0;
+
+ for (i = 0; s[i] != '\0'; i++) {
+ char c = s[i];
+
+ if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') ||
+ (c >= 'A' && c <= 'F')))
+ return 0;
+ hex++;
+ }
+ return hex == 40 || hex == 64;
+}
+
+static char *head_ref(hook_opts o, int *unknown)
+{
+ const char *argv[4];
+ char *out = NULL;
+ int rc;
+
+ *unknown = 0;
+ if (strcmp(o.head, "none") == 0)
+ return NULL;
+ if (strcmp(o.head, "auto") != 0)
+ return xstrdup(o.head);
+ if (!o.use_git)
+ return NULL; /* told not to ask, so do not guess either way */
+
+ argv[0] = "git";
+ argv[1] = "symbolic-ref";
+ argv[2] = "-q";
+ argv[3] = "HEAD";
+ rc = spawn(argv, 1, &out);
+ if (rc == 0 && out != NULL && out[0] != '\0')
+ return out;
+ free(out);
+ *unknown = 1; /* cannot tell: charge deletions the most */
+ return NULL;
+}
+
+/* Is old an ancestor of new? */
+static int fast_forward(hook_opts o, const char *old, const char *new)
+{
+ const char *argv[5];
+ int rc;
+
+ if (strcmp(o.ff, "yes") == 0)
+ return 1;
+ if (strcmp(o.ff, "no") == 0)
+ return 0;
+ if (!o.use_git)
+ return 0;
+
+ argv[0] = "git";
+ argv[1] = "merge-base";
+ argv[2] = "--is-ancestor";
+ argv[3] = old;
+ argv[4] = new;
+ rc = spawn(argv, 0, NULL);
+ return rc == 0;
+}
+
+static char *derive_repo(hook_opts o)
+{
+ const char *e;
+ const char *root;
+ size_t rootlen;
+ char *cwd;
+ char *name;
+
+ if (o.repo != NULL)
+ return authz_norm_repo(o.repo);
+ e = getenv("GIT_HOST_REPO");
+ if (e != NULL && e[0] != '\0')
+ return authz_norm_repo(e);
+
+ cwd = getcwd(NULL, 0);
+ if (cwd == NULL)
+ return xstrdup("");
+ root = getenv("GIT_REPO_ROOT");
+ if (root == NULL || root[0] == '\0')
+ root = "/var/lib/git";
+ rootlen = strlen(root);
+ while (rootlen > 1 && root[rootlen - 1] == '/')
+ rootlen--; /* "/var/lib/git/" is "/var/lib/git" */
+
+ if (strncmp(cwd, root, rootlen) == 0 && cwd[rootlen] == '/')
+ name = cwd + rootlen + 1;
+ else {
+ char *slash = strrchr(cwd, '/');
+
+ name = (slash != NULL && slash[1] != '\0') ? slash + 1 : cwd;
+ }
+ {
+ char *out = authz_norm_repo(name);
+
+ free(cwd);
+ return out;
+ }
+}
+
+static const char *identity(hook_opts o)
+{
+ const char *e;
+
+ if (o.user != NULL)
+ return o.user;
+ e = getenv("GIT_HOST_USER");
+ if (e != NULL && e[0] != '\0')
+ return e;
+ e = getenv("REMOTE_USER");
+ if (e != NULL && e[0] != '\0')
+ return e;
+ return NULL;
+}
+
+static int flag(const char *arg, const char *name, const char **slot)
+{
+ size_t n = strlen(name);
+
+ if (strncmp(arg, name, n) != 0 || arg[n] != '=')
+ return 0;
+ *slot = arg + n + 1;
+ return 1;
+}
+
+int cmd_authorize(const policy_t *p, hook_opts o, int argc, char **argv)
+{
+ char *line = NULL;
+ size_t cap = 0;
+ ssize_t len;
+ char *repo;
+ const char *user;
+ const char *head;
+ int head_unknown;
+ int seen = 0;
+ int i;
+
+ if (o.ff == NULL)
+ o.ff = "auto";
+ if (o.head == NULL)
+ o.head = "auto";
+
+ for (i = 0; i < argc; i++) {
+ if (flag(argv[i], "--user", &o.user))
+ continue;
+ if (flag(argv[i], "--repo", &o.repo))
+ continue;
+ if (flag(argv[i], "--ff", &o.ff))
+ continue;
+ if (flag(argv[i], "--head", &o.head))
+ continue;
+ if (strcmp(argv[i], "--no-git") == 0) {
+ o.use_git = 0;
+ continue;
+ }
+ if (strcmp(argv[i], "--explain") == 0) {
+ o.explain = 1;
+ continue;
+ }
+ fprintf(stderr, "%s: authorize: unknown argument %s\n", progname, argv[i]);
+ return 64;
+ }
+ if (strcmp(o.ff, "auto") != 0 && strcmp(o.ff, "yes") != 0 &&
+ strcmp(o.ff, "no") != 0) {
+ fprintf(stderr, "%s: --ff wants auto, yes or no\n", progname);
+ return 64;
+ }
+
+ user = identity(o);
+ repo = derive_repo(o);
+ head = head_ref(o, &head_unknown);
+
+ while ((len = getline(&line, &cap, stdin)) != -1) {
+ char *fields[4];
+ int nf = 0;
+ char *ref;
+ char *old;
+ char *new;
+ level_t need;
+ decision_t d;
+
+ /* Split on whitespace by hand: the object ids and refnames in this line
+ * never contain it, and it keeps the reader's index the same as the
+ * writer's. */
+ {
+ char *q = line;
+
+ while (*q != '\0') {
+ while (*q == ' ' || *q == '\t' || *q == '\n' || *q == '\r')
+ q++;
+ if (*q == '\0')
+ break;
+ fields[nf++] = q;
+ while (*q != '\0' && *q != ' ' && *q != '\t' && *q != '\n' && *q != '\r')
+ q++;
+ if (*q != '\0')
+ *q++ = '\0';
+ if (nf == 4)
+ break;
+ }
+ }
+ if (nf == 0)
+ continue;
+ if (nf != 3) {
+ fprintf(stderr, "%s: deny: expected \"old new ref\", got %d field(s)\n",
+ progname, nf);
+ free(line);
+ free(repo);
+ free((char *)head);
+ return 1;
+ }
+
+ old = fields[0];
+ new = fields[1];
+ ref = fields[2];
+ seen++;
+
+ if (!object_id_ok(old) || !object_id_ok(new) ||
+ strncmp(ref, "refs/", 5) != 0) {
+ fprintf(stderr, "%s: deny: \"%s %s %s\" is not an update git would send\n",
+ progname, old, new, ref);
+ free(line);
+ free(repo);
+ free((char *)head);
+ return 1;
+ }
+
+ if (is_zero(new)) {
+ /* A deletion. Branches cost force; the one HEAD points at costs
+ * an admin, because a repository without a default branch is a
+ * repository whose clones check out nothing. */
+ need = LV_FORCE;
+ if (head_unknown || (head != NULL && strcmp(ref, head) == 0))
+ need = LV_ADMIN;
+ } else if (is_zero(old)) {
+ need = LV_WRITE; /* a creation */
+ } else {
+ need = fast_forward(o, old, new) ? LV_WRITE : LV_FORCE;
+ }
+
+ authz_check(p, user, repo, need, ref, &d);
+ if (!d.allowed) {
+ fprintf(stderr, "%s: deny: %s\n", progname, d.why);
+ free(line);
+ free(repo);
+ free((char *)head);
+ return 1;
+ }
+ if (o.explain)
+ printf("%s: allow: %s\n", progname, d.why);
+ }
+ free(line);
+ free(repo);
+ free((char *)head);
+
+ if (seen == 0 && o.explain)
+ printf("%s: nothing to check\n", progname);
+ return 0;
+}