diff options
Diffstat (limited to 'src/authz.h')
| -rw-r--r-- | src/authz.h | 178 |
1 files changed, 71 insertions, 107 deletions
diff --git a/src/authz.h b/src/authz.h index 794c12c..671b45f 100644 --- a/src/authz.h +++ b/src/authz.h @@ -1,48 +1,30 @@ -/* 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. - */ +// 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 <stddef.h> -#define MAX_FIELDS 64 /* one rule line, split on whitespace */ +#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 */ + 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) */ + char *user; // pattern, as written + char *repo; // pattern, normalised (no .git) level_t level; - char **refs; /* normalised patterns; NULL/0 = whole repository */ + char **refs; // normalised patterns; NULL/0 = whole repository size_t nrefs; - int line; /* 1-based, for messages */ + int line; // 1-based, for messages } rule_t; typedef struct { @@ -50,146 +32,129 @@ typedef struct { size_t nrules, rcap; int errors; - int unreadable; /* the file itself could not be opened */ - char *path; /* what messages call the file */ + 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 */ + level_t need; // what it cost in the end + char why[512]; // the sentence the person pushing reads } decision_t; -/* ---------------------------------------------------------------- glob.c */ +// 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. -/* 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 */ +// 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 are reported as they are - * found, on stderr, with the line number. */ +// 0 usable, -1 errors, -2 unreadable. Errors print as they are found. + 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. */ +// 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); -/* 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. */ + +// 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); -/* 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. */ +// 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); -/* -1 for anything that is not a level. */ -int authz_level_of(const char *name); +int authz_level_of(const char *name); // -1 for anything that is not one -/* 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. */ +// '@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 really costs beyond what the rule said. Anything that is not a - * branch or a tag is for maintainers, whatever a rule about it appears to - * grant: refs/notes, refs/merge-requests, whatever a tool invents next, are not - * part of the namespace a policy was written with in mind, and the safe reading - * of "not mentioned" is "not granted". When the answer is higher than 'have', - * the reason is written to raised, which must hold at least 64 bytes. */ +// 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); -/* The decision. 'ref' may be NULL, which asks "may this user do this - * somewhere in the repository" rather than about one ref. */ +// '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); -/* 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. */ +// 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 */ +// 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. -/* 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 */ + 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 */ +// validate.c ---------------------------------------------------------------- typedef struct { - const char *root; /* where the repositories live */ + 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 */ + int strict; // warnings are errors } validate_opts; int cmd_validate(const policy_t *p, validate_opts o); -/* ---------------------------------------------------------------- edit.c */ +// edit.c -------------------------------------------------------------------- typedef struct { - const char *out; /* NULL edits in place; "-" is stdout */ + const char *out; // NULL edits in place; "-" is stdout } edit_opts; -/* Shared by the two editing commands: pulls --out and --stdout out of the - * argument list wherever they sit (an option at the end reads better in a - * shell history) and compacts what is left into argv, returning the count in - * *consumed. Returns -1 on a missing value. */ +// 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 ref pattern or name that starts with "--" is a mistyped option, not - * something to grant. Prints and returns 1 when it finds one. */ +// 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); -/* ---------------------------------------------------------------- main.c - * Shared little helpers, so that no file invents its own way to fail. */ +// util.c -------------------------------------------------------------------- -/* A growing string. The policy is assembled and explained in pieces, and - * strncat with hand-computed room is how buffer bugs are born. */ +// 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 */ + char *s; // never NULL once sb_init has run size_t len; size_t cap; } strbuf_t; @@ -204,8 +169,7 @@ 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. */ +// 'pre-receive' when invoked through the hook symlink, 'git-authz' otherwise. extern const char *progname; #endif /* AUTHZ_H */ |
