diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/authz.h | 22 | ||||
| -rw-r--r-- | src/hook.c | 42 | ||||
| -rw-r--r-- | src/policy.c | 17 |
3 files changed, 64 insertions, 17 deletions
diff --git a/src/authz.h b/src/authz.h index f9fbfa3..6665e03 100644 --- a/src/authz.h +++ b/src/authz.h @@ -1,14 +1,16 @@ // 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. +// The exit status is the interface: 0 allowed, 1 denied (this person may not), +// 3 the host is broken and not this person (no usable policy, or input that was +// never a push), 64 usage. #ifndef AUTHZ_H #define AUTHZ_H #include <getopt.h> #include <stddef.h> +#include <stdio.h> #define MAX_FIELDS 64 // one rule line, split on whitespace #define AUTHZ_MSGSZ 512 // one complaint, in the words the caller prints @@ -38,12 +40,20 @@ typedef struct { int errors; int unreadable; // the file itself could not be opened char *path; // what messages call the file + FILE *log; // where the reading of the file complains: line + // numbers and errno, which an operator needs and a + // person pushing does not. NULL says nothing; the + // hook sets that, because its stderr is the + // pusher's terminal and `validate` has the detail. } policy_t; typedef struct { int allowed; level_t need; // what it cost in the end - char why[512]; // the sentence the person pushing reads + char why[512]; // the sentence about the policy: what was held, the + // ref, what it cost and why. `check` prints it, + // and so does the hook under --explain; the person + // pushing gets one terse line instead. } decision_t; // glob.c -------------------------------------------------------------------- @@ -59,7 +69,8 @@ int glob_match(const char *pattern, const char *s); 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. +// 0 usable, -1 errors, -2 unreadable. Errors print as they are found, to +// p->log, which is stderr until a caller says otherwise. int policy_load(policy_t *p); @@ -123,7 +134,8 @@ typedef struct { 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 + int explain; // the sentence, and the allows too, not only the + // terse denial a pusher is given } hook_opts; int cmd_authorize(const policy_t *p, hook_opts o, int argc, char **argv); @@ -278,6 +278,33 @@ static int parse_options(int argc, char **argv, hook_opts *o) return 0; } +// The one line a person pushing is given: the input they presented, and the +// verdict. Nothing else. The reason is a sentence about a policy file they +// cannot read, and quoting it here sends them to somebody else's configuration +// while telling a monitor nothing the exit status did not. `check` is where +// whoever owns the policy reads the reason out, and --explain below prints it in +// this seat for the operator who is already standing here. + +static void deny_ref(const char *user, const char *ref) +{ + if (user != NULL && user[0] != '\0') + fprintf(stderr, "%s: permission denied [user=%s, obj=%s]\n", + progname, user, ref); + else + fprintf(stderr, "%s: permission denied [obj=%s]\n", progname, ref); +} + +// Input that is not a push at all: not three fields, not object ids, not a ref +// under refs/. None of it is echoed - the person on the other end can do +// nothing with a line they did not write, and exit 3 already says whose problem +// it is. A real git never writes these lines, so what to look at is the pipe, +// the wrapper, or whoever was debugging the hook by hand. + +static void invalid_operation(void) +{ + fprintf(stderr, "%s: invalid operation\n", progname); +} + int cmd_authorize(const policy_t *p, hook_opts o, int argc, char **argv) { char *line = NULL; @@ -328,9 +355,8 @@ int cmd_authorize(const policy_t *p, hook_opts o, int argc, char **argv) if (nf == 0) continue; if (nf != 3) { - fprintf(stderr, "%s: deny: expected \"old new ref\", got %d field(s)\n", - progname, nf); - status = 1; + invalid_operation(); + status = 3; // the host's fault, not the pusher's break; } @@ -341,9 +367,8 @@ int cmd_authorize(const policy_t *p, hook_opts o, int argc, char **argv) if (!object_id_ok(old) || !object_id_ok(new) || strncmp(ref, "refs/", 5) != 0) { - fprintf(stderr, "%s: deny: \"%s %s %s\" is not an update git would send\n", - progname, old, new, ref); - status = 1; + invalid_operation(); + status = 3; break; } @@ -362,7 +387,10 @@ int cmd_authorize(const policy_t *p, hook_opts o, int argc, char **argv) authz_check(p, user, repo, need, ref, &d); if (!d.allowed) { - fprintf(stderr, "%s: deny: %s\n", progname, d.why); + if (o.explain) + fprintf(stderr, "%s: deny: %s\n", progname, d.why); + else + deny_ref(user, ref); status = 1; break; } diff --git a/src/policy.c b/src/policy.c index b2d4348..db00c78 100644 --- a/src/policy.c +++ b/src/policy.c @@ -86,13 +86,18 @@ static void perr(policy_t *p, int line, const char *fmt, ...) { va_list ap; + // A person pushing has no use for a line number in a file they cannot read, + // so the hook loads its policy with the log switched off. + p->errors++; - fprintf(stderr, "%s:%d: error: ", + if (p->log == NULL) + return; + fprintf(p->log, "%s:%d: error: ", p->path != NULL ? p->path : "authz.conf", line); va_start(ap, fmt); - vfprintf(stderr, fmt, ap); + vfprintf(p->log, fmt, ap); va_end(ap); - fputc('\n', stderr); + fputc('\n', p->log); } int authz_user_pattern_ok(const char *s) @@ -290,6 +295,7 @@ void policy_init(policy_t *p, const char *path) { memset(p, 0, sizeof(*p)); p->path = xstrdup(path); + p->log = stderr; // whoever reads the file hears what is wrong } void policy_free(policy_t *p) @@ -324,8 +330,9 @@ int policy_load(policy_t *p) rc = authz_read_lines(p->path, &lines); if (rc != 0) { p->unreadable = 1; - fprintf(stderr, "%s: cannot read the policy: %s\n", p->path, - strerror(errno)); + if (p->log != NULL) + fprintf(p->log, "%s: cannot read the policy: %s\n", p->path, + strerror(errno)); lines_free(&lines); return -2; } |
