diff options
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/main.c | 49 |
1 files changed, 34 insertions, 15 deletions
@@ -1,7 +1,8 @@ // One program, because check and the hook have to agree exactly about what a // rule means: a check that answers differently from the hook is a program that -// lies. Exit status is the interface: 0 allowed, 1 denied, 3 no usable policy, -// 64 usage. +// lies. Exit status is the interface: 0 allowed, 1 denied, 3 the host is +// broken rather than this person (no usable policy, or input that was never a +// push), 64 usage. #include "authz.h" @@ -20,18 +21,21 @@ static void usage(FILE *f) "usage: git-authz [--policy PATH] <command> [arguments]\n" "\n" " check <user> <repo> <write|force|admin> [ref]\n" -" one question: allow or deny on stdout, the reason with it.\n" +" one question: allow or deny on stdout, the reason with it. This is\n" +" where the reason for a refusal is read out; the hook does not give it.\n" " Without a ref it asks about the repository as a whole.\n" "\n" " authorize read a push exactly as git's receive hook does - identity from\n" " GIT_HOST_USER (ssh) or REMOTE_USER (http), one \"old new ref\" line per\n" " ref on stdin - and deny the push on the first ref it may not allow.\n" " This also runs as 'pre-receive', which is how core.hooksPath finds it.\n" +" A denial is one line naming the input it refused, and nothing else:\n" +" the reason is what check is for.\n" " --user U --repo R override the identity and the repository\n" " --ff auto|yes|no whether to ask git if old is an ancestor of new\n" " --head auto|none|REF the default branch, or how to treat deletions\n" " --no-git ask git nothing (an unknown HEAD is not guessed)\n" -" --explain report the allows too\n" +" --explain print the reason, and the allows too\n" "\n" " validate is the policy parseable, and is it sane?\n" " --strict warnings are failures too\n" @@ -41,17 +45,20 @@ static void usage(FILE *f) "same branch; a bare name is always a branch and never a tag. A rule says what\n" "it says: nothing else in the file can raise the price of a ref.\n" "\n" -"Exit status: 0 allowed, 1 denied, 3 the policy could not be read or does not\n" -"parse - nothing is allowed on a policy that cannot be read - and 64 for usage.\n"); +"Exit status: 0 allowed, 1 denied, 64 for usage, and 3 when the host is broken\n" +"rather than this person: nothing is allowed on a policy that cannot be read\n" +"('invalid policy file'), and a hook handed something that is not a push says\n" +"'invalid operation' - a real git never sends that.\n"); } // 3 and not 1: a push refused because the host is broken has to sound different -// from one refused because the person is not allowed. +// from one refused because the person is not allowed. Every 3 is one line, and +// which of them it is is all a monitor has to tell; what is wrong with the policy +// is `validate`'s job, and it says so with line numbers. -static int deny_policy(const char *path, int rc) +static int deny_policy(void) { - fprintf(stderr, "%s: deny: the policy at %s %s\n", progname, path, - rc == -2 ? "cannot be read" : "does not parse"); + fprintf(stderr, "%s: invalid policy file\n", progname); return 3; } @@ -91,7 +98,7 @@ static int parse_validate(int argc, char **argv, validate_opts *vo) } static int dispatch(policy_t *p, int rc, const char *cmd, int argc, char **argv, - const char *path, hook_opts ho, validate_opts vo) + hook_opts ho, validate_opts vo) { // argv[0] is the command name, which is where getopt_long expects argv[0] // to be. A command's own positional arguments therefore start at argv[1]. @@ -107,7 +114,7 @@ static int dispatch(policy_t *p, int rc, const char *cmd, int argc, char **argv, if (strcmp(cmd, "authorize") == 0) { if (rc != 0) - return deny_policy(path, rc); + return deny_policy(); return cmd_authorize(p, ho, argc, argv); } @@ -131,7 +138,7 @@ static int dispatch(policy_t *p, int rc, const char *cmd, int argc, char **argv, } level = authz_level_of(argv[2]); if (rc != 0) - return deny_policy(path, rc); + return deny_policy(); authz_check(p, argv[0], argv[1], (level_t)level, argc > 3 ? argv[3] : NULL, &d); printf("%s: %s\n", d.allowed ? "allow" : "deny", d.why); return d.allowed ? 0 : 1; @@ -178,10 +185,16 @@ int main(int argc, char **argv) char *hook_argv[] = { (char *)"pre-receive", NULL }; policy_init(&p, path); + + // Reached by the name git calls, so everything it prints is read by the + // person pushing. The policy's own complaints - line number, errno - + // are for whoever owns the file, and are not in this output. + + p.log = NULL; rc = policy_load(&p); if (rc != 0) { policy_free(&p); - return deny_policy(path, rc); + return deny_policy(); } status = cmd_authorize(&p, ho, 1, hook_argv); policy_free(&p); @@ -231,8 +244,14 @@ int main(int argc, char **argv) } policy_init(&p, path); + + // authorize mode answers a person pushing, whoever invoked it: the mode is + // the audience, so the file's line numbers stay out of its output. + + if (strcmp(cmd, "authorize") == 0) + p.log = NULL; rc = policy_load(&p); - status = dispatch(&p, rc, cmd, argc, argv, path, ho, vo); + status = dispatch(&p, rc, cmd, argc, argv, ho, vo); policy_free(&p); return status; } |
