// 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 #include #define MAX_FIELDS 64 // one rule line, split on whitespace #define AUTHZ_MSGSZ 512 // one complaint, in the words the caller prints #define DEFAULT_ROOT "/var/lib/git" // where the repositories live; the hook // derives a repo name from this too 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); // A policy line's body: trimmed, comment and all after it gone. Edits the line. char *authz_line_body(char *line); // 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); // The grammar, in one place: check and the file parser must refuse the same // lines for the same reason, because a rule the parser rejects denies every push // in the repository. Fills msg with the complaint in the words the caller // prints; returns 0 when the fields parse. A NULL user or repo is a field the // caller is not asking about. int authz_rule_validate(const char *user, const char *repo, const char *level, char *const *refs, int nrefs, char *msg, size_t msgs); // 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); // 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 { int strict; // warnings are errors } validate_opts; int cmd_validate(const policy_t *p, validate_opts o); // 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; // A file, one entry per line. The newline is kept, and a last line without one // is given one, so the lines may be written straight back out again. typedef struct { char **v; size_t n; size_t cap; } lines_t; void lines_init(lines_t *l); void lines_push(lines_t *l, char *s); // takes ownership void lines_free(lines_t *l); // 0, -1 when the file cannot be opened, or -2 when reading stopped before the // end - a directory, an I/O error halfway, a mount pulled out. errno says which. // The caller decides what a missing file means: an emergency for the policy, // nothing at all for a file that is optional here. int authz_read_lines(const char *path, lines_t *l); 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; // getopt_long hands back an option's val, not the name that was typed, and for // a long-only option the val is a number nobody wrote down. The table is the // only place that still knows the pair. const char *authz_optname(int val, const struct option *lo); // A run parses options twice - the global ones, then the command's own - each on // its own argv slice, so getopt_long has to be told to start again. void authz_opt_reset(void); // The two complaints every getopt_long loop would otherwise write by hand. void authz_opt_need_value(const struct option *lo); void authz_opt_unknown(const char *cmd, const char *arg); #endif /* AUTHZ_H */