/* 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 #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 */