diff options
| author | dev <dev@example> | 2026-11-14 01:05:08 +0000 |
|---|---|---|
| committer | dev <dev@example> | 2026-11-14 01:05:08 +0000 |
| commit | 312ddd4936f09ae27a180ca495cacf2c06faf33e (patch) | |
| tree | 2fa46ab568ad55f7e8fb844c3447b1d296b2e340 /cmd/main.c | |
| parent | e737f3d560de2b8172e6dd83901bc1359283303c (diff) | |
| download | access-control-312ddd4936f09ae27a180ca495cacf2c06faf33e.tar.gz access-control-312ddd4936f09ae27a180ca495cacf2c06faf33e.zip | |
Comments: cut to what the code cannot say
The comments had grown into essays. A file header explaining why the file
exists, then a paragraph above each function restating what the next eight
lines do, then a paragraph above that one explaining why that matters. The
reader gets no further than the first paragraph, and the second is the one that
goes stale: it describes code that has since changed, and nothing fails when it
does.
Cut by about half. What is left is the three things a comment can do that code
cannot: say why the constraint exists (no regex because a hook must not depend
on what might be missing at 3am), say what is not obvious from the shape (a
bare ref name is a branch, never a tag; the seventh field of /etc/passwd is the
shell), and say where it hurts (the NULL in an execvp array, the mode and owner
that have to survive an atomic rewrite, the child that must not keep the hook's
stdin).
Block comments became // lines, which is also what the other C trees here use,
and the file headers went to two or three lines. Section banners stayed: they
are navigation, not prose.
Behaviour is unchanged and the suites prove it: 171 direct checks, 162
behavioural, clean under -Werror and under ASan/UBSan.
Diffstat (limited to 'cmd/main.c')
| -rw-r--r-- | cmd/main.c | 56 |
1 files changed, 23 insertions, 33 deletions
@@ -1,15 +1,8 @@ -/* main.c - one program, four jobs. - * - * check an operator asking, or a script - * authorize git asking, once per push (also reached as 'pre-receive') - * validate the entrypoint and CI asking before anything is served - * edit changing the file (grant, revoke) - * - * They are one program because they must agree, exactly, about what a rule - * means: a check that answers differently from the hook is worse than two - * programs, it is a program that lies. The exit status is the interface, and - * it never changes: 0 allowed, 1 denied, 3 no usable policy, 64 usage. - */ +// 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. + #include "authz.h" #include <stdio.h> @@ -92,9 +85,9 @@ static int one_option(int argc, char **argv, int *i, const char *name, const cha return 0; } -/* The policy is not usable, so the only safe answer is no. Three, not one: - * the push was refused because this host is broken, not because that person is - * not allowed, and whoever is debugging wants the two to sound different. */ +// 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. + static int deny_policy(const char *path, int rc) { fprintf(stderr, "%s: deny: the policy at %s %s\n", progname, path, @@ -102,10 +95,10 @@ static int deny_policy(const char *path, int rc) return 3; } -/* Everything that answers out of a loaded policy. Split out of main so the - * policy is freed on the way out whichever way that is: a process this short - * lived could leave the heap to the kernel, but then a sanitizer run reports - * forty leaks and stops showing the one that matters. */ +// Split out of main so the policy is freed on every exit path: a short-lived +// process can leave the heap to the kernel, but then the sanitizer reports +// forty leaks and stops showing the one that matters. + static int dispatch(policy_t *p, int rc, const char *cmd, int argc, char **argv, const char *path, hook_opts ho, validate_opts vo) { @@ -134,10 +127,9 @@ static int dispatch(policy_t *p, int rc, const char *cmd, int argc, char **argv, vo.strict = 1; continue; } - /* The policy is the part that decides who may push. Everything - * else is how this host is wired, and a host without a git account - * yet - a container being built, a config directory being checked - * in - still wants its policy read. */ + // A host without a git account yet - a container being built - + // still wants its policy read. + if (got == 0 && strcmp(argv[j], "--no-host") == 0) { vo.root = NULL; vo.hook_dir = NULL; @@ -154,8 +146,9 @@ static int dispatch(policy_t *p, int rc, const char *cmd, int argc, char **argv, return 64; } } - /* A policy that does not parse is itself the finding, so it is passed - * down and reported rather than turning this into a bare exit 3. */ + // A policy that does not parse is the finding, so it is passed down + // rather than exiting 3 here. + return cmd_validate(p, vo); } @@ -223,10 +216,9 @@ int main(int argc, char **argv) vo.htpasswd = DEFAULT_HTPASSWD; memset(&eo, 0, sizeof(eo)); - /* Reached through the hook symlink. Git's own arguments to a hook are not - * this program's, so they are dropped, and the policy comes from the - * environment - a hook is not in a position to be told where the policy is - * by a person who is not there. */ + // Reached through the hook symlink: git's arguments are not this program's, + // so they are dropped, and the policy comes from the environment. + if (strcmp(progname, "pre-receive") == 0) { policy_init(&p, path); rc = policy_load(&p); @@ -271,15 +263,13 @@ int main(int argc, char **argv) return 0; } - /* The three commands that change the file. They read it themselves, and - * they must not disagree with the checker about what a rule means, which is - * why they are in the same binary. */ + if (strcmp(cmd, "grant") == 0 || strcmp(cmd, "revoke") == 0) { int consumed = 0; if (authz_parse_out(argc, argv, &eo, &consumed) != 0) return 64; - argc -= consumed; /* what is left was compacted to the front */ + argc -= consumed; // what is left was compacted to the front if (strcmp(cmd, "grant") == 0) return cmd_grant(path, eo, argc, argv); return cmd_revoke(path, eo, argc, argv); |
