summaryrefslogtreecommitdiffstats
path: root/src/authz.h
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/authz.h
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/authz.h')
-rw-r--r--src/authz.h214
1 files changed, 214 insertions, 0 deletions
diff --git a/src/authz.h b/src/authz.h
new file mode 100644
index 0000000..9ca0eb6
--- /dev/null
+++ b/src/authz.h
@@ -0,0 +1,214 @@
+/* authz.h - who may move which refs.
+ *
+ * One policy file, one program, three modes:
+ *
+ * check answer one question (an operator, or a script)
+ * authorize answer the questions a push asks, the way git's receive hook
+ * asks them: identity in the environment, one "old new ref" line
+ * per ref on stdin
+ * validate say whether the policy, and the host around it, make sense
+ * grant/revoke/protect edit the policy file (it is a file; that is the
+ * whole point - but see the read-only rules in edit.c)
+ *
+ * The verdict travels in the exit status, because the caller is a hook:
+ *
+ * 0 allowed
+ * 1 denied
+ * 3 the policy could not be read, or does not parse - fail closed
+ * 64 usage
+ *
+ * Everything here is C99 plus libc. There is deliberately no regex library,
+ * no fnmatch, no YAML, no SQLite: the policy grammar is words and three glob
+ * metacharacters, and a hook must not depend on anything that can be missing
+ * at 3am when the push happens.
+ */
+#ifndef AUTHZ_H
+#define AUTHZ_H
+
+#include <stddef.h>
+
+#define MAX_FIELDS 64 /* one rule line, split on whitespace */
+
+typedef enum {
+ LV_NONE = 0,
+ LV_WRITE = 1, /* create and fast-forward */
+ LV_FORCE = 2, /* + rewrite history and delete */
+ LV_ADMIN = 3 /* + protected refs, odd refs, the default branch */
+} level_t;
+
+typedef struct {
+ char *user; /* pattern, as written */
+ char *repo; /* pattern, normalised (no .git) */
+ level_t level;
+ char **refs; /* normalised patterns; NULL/0 = whole repository */
+ size_t nrefs;
+ int line; /* 1-based, for messages */
+} rule_t;
+
+typedef struct {
+ rule_t *rules;
+ size_t nrules, rcap;
+
+ char *protected_global; /* space separated normalised patterns, or NULL */
+ char **prot_repo; /* repositories with their own list */
+ char **prot_refs; /* and the list that goes with each */
+ size_t nprot, pcap;
+
+ int errors;
+ int warnings;
+ char *path; /* what messages call the file */
+} policy_t;
+
+typedef struct {
+ int allowed;
+ level_t need; /* what it cost in the end, after any raise */
+ char why[512]; /* the sentence the person pushing reads */
+} decision_t;
+
+/* ---------------------------------------------------------------- glob.c */
+
+/* Glob matching: '*' is any run of characters (including '/'), '?' is one
+ * character, '[abc]' and '[a-z]' are classes, '!' or '^' after the bracket
+ * negates. Everything else is literal, including '.'.
+ *
+ * Written out rather than reached for with fnmatch(3) or regex(3): both exist
+ * under different names and with different escapes on the systems this runs
+ * on, and neither is worth a dependency for three metacharacters. */
+int glob_match(const char *pattern, const char *s);
+
+/* -------------------------------------------------------------- policy.c */
+
+void policy_init(policy_t *p, const char *path);
+void policy_free(policy_t *p);
+
+/* Read and parse. Returns 0 when the file is usable, -1 when it produced
+ * errors, -2 when it could not be read at all. Errors and warnings are
+ * reported as they are found, on stderr, with the line number. */
+int policy_load(policy_t *p);
+
+/* Refs and repositories have one spelling each internally. A bare name is a
+ * branch, which is what makes "main" and "refs/heads/main" the same thing and
+ * keeps a branch from ever meaning a tag of the same name. Both return
+ * malloc'd strings. */
+char *authz_norm_ref(const char *in);
+
+/* Whitespace helpers, shared by the parser and the editor. authz_trim edits in
+ * place and returns the first non-blank byte. */
+char *authz_trim(char *s);
+/* Splits in place, returning the field count; more than 'max' returns max+1 so
+ * the caller can complain rather than read past the array. */
+int authz_split_ws(char *line, char **fields, int max);
+
+/* The shapes a user and a repository name may take. Repository names are flat:
+ * two levels of namespace with a glob over each is all this model has, and the
+ * check that enforces it is the documentation for it. */
+int authz_user_pattern_ok(const char *s);
+int authz_repo_pattern_ok(const char *s);
+char *authz_norm_repo(const char *in);
+
+const char *authz_level_name(level_t l);
+/* -1 for anything that is not a level. */
+int authz_level_of(const char *name);
+
+/* True if some rule other than an '@all' one names this user: '@all' means
+ * "everybody this policy already knows about", and never invents a name. */
+int authz_user_known(const policy_t *p, const char *user);
+
+/* What a ref really costs. Anything that is not a branch or a tag is for
+ * maintainers; on a protected ref 'write' is not enough and 'force' is not
+ * enough either. When the answer is higher than 'have', the reason
+ * ("protected", "neither a branch nor a tag") is written to raised, which must
+ * hold at least 64 bytes. */
+level_t authz_need_for(const policy_t *p, const char *repo, const char *ref,
+ level_t have, char *raised, size_t raisedsz);
+
+/* The decision. 'ref' may be NULL, which asks "may this user do this
+ * somewhere in the repository" rather than about one ref. */
+void authz_check(const policy_t *p, const char *user, const char *repo,
+ level_t need, const char *ref, decision_t *out);
+
+/* True if any rule reaches this repository at all: an unreadable-by-nobody
+ * repository is readable and unpushable, which is usually a name that stopped
+ * matching rather than a decision. */
+int authz_repo_reachable(const policy_t *p, const char *repo);
+
+/* The protected list in force for one repository, as stored (normalised
+ * patterns, space separated). Never NULL. */
+const char *authz_protected_for(const policy_t *p, const char *repo);
+
+/* --------------------------------------------------------------- hook.c */
+
+/* authorize mode: identity from the environment, ref lines from stdin, the
+ * shape git hands a receive hook. Returns the exit status.
+ *
+ * Whether an update is a fast-forward, and which ref HEAD points at, are facts
+ * about the object store that only git knows. They are asked of git unless
+ * asked not to be, and an answer that cannot be had is the conservative one:
+ * treat the update as a rewrite, and treat any deleted ref as the default
+ * branch. */
+typedef struct {
+ const char *user; /* overrides the environment (tests, debugging) */
+ const char *repo; /* ditto */
+ const char *ff; /* "auto" (ask git), "yes", "no" */
+ const char *head; /* "auto" (ask git), "none", or a refname */
+ int use_git; /* 0 never runs git, whatever 'ff'/'head' say */
+ int explain; /* print every verdict, not only the denials */
+} hook_opts;
+
+int cmd_authorize(const policy_t *p, hook_opts o, int argc, char **argv);
+
+/* ------------------------------------------------------------ validate.c */
+
+typedef struct {
+ const char *root; /* where the repositories live */
+ const char *hook_dir;
+ const char *gitconfig;
+ const char *sshd_config;
+ const char *passwd;
+ const char *keys;
+ const char *htpasswd;
+ int strict; /* warnings are errors */
+} validate_opts;
+
+int cmd_validate(const policy_t *p, validate_opts o);
+
+/* ---------------------------------------------------------------- edit.c */
+
+typedef struct {
+ const char *out; /* NULL edits in place; "-" is stdout */
+} edit_opts;
+
+/* Shared by the three editing commands: leading --out/--stdout flags, and how
+ * many arguments they consumed. Returns -1 on an unknown option. */
+int authz_parse_out(int argc, char **argv, edit_opts *o, int *consumed);
+
+int cmd_grant(const char *path, edit_opts o, int argc, char **argv);
+int cmd_revoke(const char *path, edit_opts o, int argc, char **argv);
+int cmd_protect(const char *path, edit_opts o, int argc, char **argv);
+
+/* ---------------------------------------------------------------- main.c
+ * Shared little helpers, so that no file invents its own way to fail. */
+
+/* A growing string. The policy is assembled and explained in pieces, and
+ * strncat with hand-computed room is how buffer bugs are born. */
+typedef struct {
+ char *s; /* never NULL once sb_init has run */
+ size_t len;
+ size_t cap;
+} strbuf_t;
+
+void sb_init(strbuf_t *b);
+void sb_add(strbuf_t *b, const char *s);
+void sb_addf(strbuf_t *b, const char *fmt, ...);
+void sb_free(strbuf_t *b);
+
+void *xmalloc(size_t n);
+void *xrealloc(void *p, size_t n);
+char *xstrdup(const char *s);
+char *xsubstr(const char *s, size_t len);
+
+/* The name of this program as it should appear in messages: 'pre-receive' when
+ * it was invoked through the hook symlink, 'git-authz' otherwise. */
+extern const char *progname;
+
+#endif /* AUTHZ_H */