// No regex, no fnmatch, no libgit2: a hook must not depend on anything that can // be missing at 3am. // // The exit status is the interface: 0 allowed, 1 denied, 3 no usable policy, // 64 usage. #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, fast-forward LV_FORCE = 2, // + rewrite history, delete LV_ADMIN = 3 // + the default branch, refs that are neither } 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; int errors; int unreadable; // the file itself could not be opened char *path; // what messages call the file } policy_t; typedef struct { int allowed; level_t need; // what it cost in the end char why[512]; // the sentence the person pushing reads } decision_t; // glob.c -------------------------------------------------------------------- // '*' is any run including '/', '?' one character, [a-z] classes with ! or ^ // negating, everything else literal. fnmatch and regex mean different things on // different systems; this does not. 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); // 0 usable, -1 errors, -2 unreadable. Errors print as they are found. int policy_load(policy_t *p); // A bare name is a branch: "main" and "refs/heads/main" are one thing, and a // branch never means a same-named tag. malloc'd. char *authz_norm_ref(const char *in); char *authz_trim(char *s); // More than 'max' fields returns max+1, so the caller complains rather than // reading past the array. int authz_split_ws(char *line, char **fields, int max); // Repository names are flat: two levels with a glob over each is the whole model. 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); int authz_level_of(const char *name); // -1 for anything that is not one // '@all' means everybody this policy already names; it never invents a name. int authz_user_known(const policy_t *p, const char *user); // What a ref costs beyond what the rule said. Neither a branch nor a tag is // admin whatever a rule grants - refs/notes, refs/merge-requests, whatever a tool // invents next. The reason goes in raised (>= 64 bytes). level_t authz_need_for(const char *ref, level_t have, char *raised, size_t raisedsz); // 'ref' may be NULL, which asks "somewhere in this 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); // False means no rule reaches it at all - usually a name that stopped matching. int authz_repo_reachable(const policy_t *p, const char *repo); // hook.c -------------------------------------------------------------------- // authorize mode: identity from the environment, "old new ref" on stdin, the // shape git hands a receive hook. Returns the exit status. // // Is old an ancestor of new, and which ref is HEAD, are asked of git. No answer // is the conservative answer: a rewrite, and any deleted ref 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; // Pulls --out and --stdout out of argv wherever they sit and compacts what is // left, returning the count in *consumed. -1 on a missing value. int authz_parse_out(int argc, char **argv, edit_opts *o, int *consumed); // A name starting with "--" is a mistyped option, not something to grant. // Prints and returns 1 when it finds one. int authz_reject_options(int argc, char **argv); 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); // util.c -------------------------------------------------------------------- // A growing string. 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); // 'pre-receive' when invoked through the hook symlink, 'git-authz' otherwise. extern const char *progname; #endif /* AUTHZ_H */