diff options
Diffstat (limited to 'src/edit.c')
| -rw-r--r-- | src/edit.c | 100 |
1 files changed, 44 insertions, 56 deletions
@@ -1,21 +1,12 @@ -/* edit.c - change the policy file, without losing the file. - * - * The deployed policy is a read-only mount, usually a ConfigMap, and that is - * not a bug to work around: it is what makes the policy reviewable and the - * rollout visible. So these commands are for the source copy - a checkout, a - * CI job, a notebook - and they say so plainly when the file they were given - * cannot be written, rather than pretending to have granted something. - * - * What they preserve is the thing a generator cannot: the comments. A policy - * file is also the record of why an access exists, so lines are replaced in - * place and everything else is left exactly as it was. When a line has to be - * written, it is written whole; the file is never truncated and rebuilt in - * place, because a policy that disappears half way through a write is a policy - * that denies everything (or, if the order were different, nothing). - * - * A file that does not parse is refused: editing a broken policy would hide the - * breakage. - */ +// The deployed policy is a read-only mount, on purpose: that is what makes it +// reviewable and the rollout visible. These are for the source copy, and say so +// when the file cannot be written rather than pretending to have granted +// something. +// +// Comments survive, because the file is also the record of why an access exists. +// The rewrite is a whole file to a temporary and a rename: a policy truncated +// half way through a write is a policy that denies everything. + #include "authz.h" #include <ctype.h> @@ -43,7 +34,7 @@ static void lines_init(lines_t *l) l->cap = 0; } -static void lines_push(lines_t *l, char *s) /* takes ownership */ +static void lines_push(lines_t *l, char *s) // takes ownership { if (l->n == l->cap) { l->cap = l->cap != 0 ? l->cap * 2 : 64; @@ -62,7 +53,7 @@ static void lines_free(lines_t *l) lines_init(l); } -/* Every line, newline included, exactly as it was. */ +// Every line, newline included, exactly as it was. static int read_lines(const char *path, lines_t *l) { FILE *f; @@ -83,8 +74,7 @@ static int read_lines(const char *path, lines_t *l) lines_push(l, xstrdup(line)); continue; } - { /* a last line without its newline: - * keep the file well formed */ + { // a last line without its newline char *own = xmalloc(n + 2); memcpy(own, line, n); @@ -98,9 +88,10 @@ static int read_lines(const char *path, lines_t *l) return 0; } -/* Write to a temporary file beside the target, then rename. The reader either - * sees the old file or the new one; and the mode and owner survive, because a - * policy that becomes 0600 root-owned stops working for the git account. */ +// A temporary beside the target, then rename: a reader sees the old file or the +// new one. Mode and owner are carried over, because a policy that becomes 0600 +// root-owned stops working for the git account. + static int write_atomic(const lines_t *l, const char *path) { char *dirtext = xstrdup(path); @@ -112,8 +103,8 @@ static int write_atomic(const lines_t *l, const char *path) size_t i; int had = stat(path, &st) == 0; - /* dirname may return a pointer into its argument or a fresh one, and may - * fail; the copy is ours either way and dirtext is freed at the end. */ + // dirname may return a pointer into its argument or a fresh one. + dir = dirname(dirtext); if (dir == NULL) dir = (char *)"."; @@ -175,13 +166,11 @@ static int write_atomic(const lines_t *l, const char *path) return 0; } -/* ------------------------------------------------------------------ matching */ +// ------------------------------------------------------------------ matching + +// Copied before splitting: splitting writes over the text, and these lines are +// the file. -/* Does this line grant to exactly this user and repository? - * - * The line is copied first: splitting writes over the text, and these lines are - * the file - a matcher that flattened the comments out of the ones it did not - * change would be a vandal. */ static int is_rule_for(const char *line, const char *user, const char *repo) { char *copy = xstrdup(line); @@ -223,12 +212,11 @@ static char *rule_text(const char *user, const char *repo, const char *level, return b.s; } -/* Fail before touching anything if the file cannot be parsed. */ -/* Load a policy for editing. A policy that does not parse is refused: editing - * a file we cannot read would rewrite the parts we can read and keep the - * damage. A policy that is not there yet is a bootstrap, not an error - the - * source copy has to start somewhere - so the file begins with the grammar - * written down, which is also the least surprising thing to find in it. */ +// A file that does not parse is refused: rewriting the parts that parse would +// keep the damage. A file that is not there yet is bootstrapped with the +// grammar +// written at the top. + static int refuse_broken(const char *path, int *created) { policy_t probe; @@ -248,10 +236,11 @@ static int refuse_broken(const char *path, int *created) return 0; } -/* Pull the shared options out of the argument list. They are allowed anywhere - * rather than only at the front, because "grant bob team write --out - * /tmp/new.conf" is what people type - and treating "--out" as a ref pattern - * would grant something nobody asked for. */ +// --out and --stdout are allowed anywhere, because "grant bob team write --out +// /tmp/new.conf" is what people type; parsing them only at the front made "-- +// out" +// a ref pattern. + int authz_parse_out(int argc, char **argv, edit_opts *o, int *consumed) { int i; @@ -294,8 +283,8 @@ int authz_reject_options(int argc, char **argv) return 0; } -/* Write the result and release the lines: every command ends here, so this is - * where the file it read stops being held. */ +// Every command ends here, so this is where the lines are released. + static int finish(lines_t *l, const char *path, edit_opts o, const char *what) { int rc; @@ -317,7 +306,7 @@ static int finish(lines_t *l, const char *path, edit_opts o, const char *what) return rc; } -/* ------------------------------------------------------------------ commands */ +// ------------------------------------------------------------------ commands int cmd_grant(const char *path, edit_opts o, int argc, char **argv) { @@ -361,9 +350,9 @@ int cmd_grant(const char *path, edit_opts o, int argc, char **argv) if (refuse_broken(path, &created) != 0) return 3; if (created) { - /* Bootstrapped lines are xstrdup'd for the same reason as the rest: - * lines_free frees every entry it holds, and a string literal is not - * something free() may be handed. */ + // xstrdup'd: lines_free frees every entry it holds, and a string + // literal is not something free() may be handed. + lines_init(&lines); lines_push(&lines, xstrdup("# git-authz policy\n")); lines_push(&lines, xstrdup("# <user> <repo> <write|force|admin> " @@ -374,25 +363,24 @@ int cmd_grant(const char *path, edit_opts o, int argc, char **argv) } else if (read_lines(path, &lines) != 0) return 3; - /* More than one rule for the same pair is legal but pointless - the - * strongest wins - so the pair is collapsed into one line, in the place of - * the first. Everything else, comments included, is byte for byte what it - * was. */ + // Several rules for one pair are legal and pointless - the strongest wins - + // so they collapse into one line, in the place of the first. + text = rule_text(argv[0], argv[1], argv[2], argc - 3, argv + 3); for (i = 0; i < (int)lines.n; i++) { if (!is_rule_for(lines.v[i], argv[0], argv[1])) continue; if (first < 0) { - first = i; /* the line the new rule takes over */ + first = i; // the line the new rule takes over continue; } - free(lines.v[i]); /* a later rule for the same pair */ + free(lines.v[i]); // a later rule for the same pair lines.v[i] = NULL; dropped++; } if (first < 0) { - lines_push(&lines, text); /* the buffer belongs to the file now */ + lines_push(&lines, text); // the buffer belongs to the file now replaced = (int)lines.n; } else { size_t w; |
