diff options
Diffstat (limited to 'src/edit.c')
| -rw-r--r-- | src/edit.c | 423 |
1 files changed, 0 insertions, 423 deletions
diff --git a/src/edit.c b/src/edit.c deleted file mode 100644 index 9e5ff33..0000000 --- a/src/edit.c +++ /dev/null @@ -1,423 +0,0 @@ -// 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> -#include <errno.h> -#include <libgen.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <sys/stat.h> -#include <unistd.h> - -#define HIDDEN_PREFIX "/.authz" -#define TEMPLATE_SUFFIX ".XXXXXX" - -// 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); - char *dir; - char *template; - size_t need; - struct stat st; - int fd; - size_t i; - int had = stat(path, &st) == 0; - - // dirname may return a pointer into its argument or a fresh one. - - dir = dirname(dirtext); - if (dir == NULL) - dir = (char *)"."; - need = strlen(dir) + strlen(HIDDEN_PREFIX) + strlen(TEMPLATE_SUFFIX) + 1; - template = xmalloc(need); - snprintf(template, need, "%s%s%s", dir, HIDDEN_PREFIX, TEMPLATE_SUFFIX); - fd = mkstemp(template); - if (fd < 0) { - fprintf(stderr, "%s: cannot write beside %s: %s\n", progname, path, - strerror(errno)); - free(template); - free(dirtext); - return -1; - } - for (i = 0; i < l->n; i++) { - size_t n = strlen(l->v[i]); - size_t off = 0; - - while (off < n) { - ssize_t w = write(fd, l->v[i] + off, n - off); - - if (w <= 0) { - fprintf(stderr, "%s: write failed: %s\n", progname, strerror(errno)); - close(fd); - unlink(template); - free(template); - free(dirtext); - return -1; - } - off += (size_t)w; - } - } - if (fchmod(fd, had ? (st.st_mode & 07777) : 0644) != 0) - fprintf(stderr, "%s: warning: chmod: %s\n", progname, strerror(errno)); - if (had && geteuid() == 0 && fchown(fd, st.st_uid, st.st_gid) != 0) - fprintf(stderr, "%s: warning: chown: %s\n", progname, strerror(errno)); - if (fsync(fd) != 0) - fprintf(stderr, "%s: warning: fsync: %s\n", progname, strerror(errno)); - if (close(fd) != 0) { - fprintf(stderr, "%s: close failed: %s\n", progname, strerror(errno)); - unlink(template); - free(template); - free(dirtext); - return -1; - } - if (rename(template, path) != 0) { - fprintf(stderr, "%s: cannot replace %s: %s\n", progname, path, strerror(errno)); - if (errno == EROFS || errno == EACCES || errno == EPERM) - fprintf(stderr, "%s: the policy is read-only by design. Edit the " - "source copy (git-host/authz.conf), roll the ConfigMap, or " - "write elsewhere with --out.\n", progname); - unlink(template); - free(template); - free(dirtext); - return -1; - } - free(template); - free(dirtext); - return 0; -} - -// ------------------------------------------------------------------ matching - -// Copied before splitting: splitting writes over the text, and these lines are -// the file. - -static int is_rule_for(const char *line, const char *user, const char *repo) -{ - char *copy = xstrdup(line); - char *fields[MAX_FIELDS]; - char *text = authz_trim(copy); - char *hash = strchr(text, '#'); - char *want; - char *have; - int same = 0; - - if (hash != NULL) - *hash = '\0'; - if (authz_split_ws(text, fields, MAX_FIELDS) >= 3) { - want = authz_norm_repo(repo); - have = authz_norm_repo(fields[1]); - same = strcmp(fields[0], user) == 0 && strcmp(have, want) == 0; - free(want); - free(have); - } - free(copy); - return same; -} - -// A ref pattern reaches the file as one field. Whitespace would split it into -// two patterns when the file is read back - granting a ref nobody asked for - and -// '#' starts a comment, so the pattern would be quietly cut short. An argument -// carrying either means something other than what was typed. - -static int ref_field_ok(const char *s) -{ - size_t i; - - if (s[0] == '\0') - return 0; - for (i = 0; s[i] != '\0'; i++) { - unsigned char c = (unsigned char)s[i]; - - if (isspace(c) || c == '#') - return 0; - } - return 1; -} - -static char *rule_text(const char *user, const char *repo, const char *level, - int nrefs, char **refs) -{ - strbuf_t b; - int i; - - sb_init(&b); - sb_addf(&b, "%s %s %s", user, repo, level); - for (i = 0; i < nrefs; i++) { - char *one = authz_norm_ref(refs[i]); - - sb_addf(&b, " %s", one); - free(one); - } - sb_add(&b, "\n"); - return b.s; -} - -// 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; - int rc; - - if (created != NULL) - *created = 0; - policy_init(&probe, path); - rc = policy_load(&probe); - policy_free(&probe); - if (rc == -1) { - fprintf(stderr, "%s: %s does not parse; fix it before editing\n", progname, path); - return 3; - } - if (rc == -2 && created != NULL) - *created = 1; - return 0; -} - -// --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; - int n = 0; - - for (i = 0; i < argc; i++) { - if (strcmp(argv[i], "--stdout") == 0) { - o->out = "-"; - continue; - } - if (strcmp(argv[i], "--out") == 0) { - if (i + 1 >= argc) { - fprintf(stderr, "%s: --out wants a file\n", progname); - return -1; - } - o->out = argv[++i]; - continue; - } - if (strncmp(argv[i], "--out=", 6) == 0) { - o->out = argv[i] + 6; - continue; - } - argv[n++] = argv[i]; - } - *consumed = argc - n; - return 0; -} - -int authz_reject_options(int argc, char **argv) -{ - int i; - - for (i = 0; i < argc; i++) { - if (argv[i][0] == '-' && argv[i][1] == '-') { - fprintf(stderr, "%s: unknown option \"%s\" - options come before the " - "names, and nothing here starts with a dash\n", progname, argv[i]); - return 1; - } - } - return 0; -} - -// 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; - - if (o.out != NULL && strcmp(o.out, "-") == 0) { - size_t i; - - for (i = 0; i < l->n; i++) - fputs(l->v[i], stdout); - fprintf(stderr, "%s: %s (to stdout; %s untouched)\n", progname, what, path); - rc = 0; - } else if (write_atomic(l, o.out != NULL ? o.out : path) != 0) { - rc = 1; - } else { - printf("%s: %s: %s\n", progname, what, o.out != NULL ? o.out : path); - rc = 0; - } - lines_free(l); - return rc; -} - -// ------------------------------------------------------------------ commands - -int cmd_grant(const char *path, edit_opts o, int argc, char **argv) -{ - lines_t lines; - char *text; - int i; - int first = -1; - int replaced = -1; - int appended = 0; - int dropped = 0; - int created = 0; - - if (argc < 3) { - fprintf(stderr, "%s: grant wants " - "<user> <repo> <write|force|admin> [ref ...]\n", progname); - return 64; - } - if (authz_level_of(argv[2]) < 0) { - fprintf(stderr, "%s: unknown access level \"%s\" (write force admin)\n", - progname, argv[2]); - return 64; - } - if (strcmp(argv[0], "@all") != 0 && !authz_user_pattern_ok(argv[0])) { - fprintf(stderr, "%s: invalid user pattern \"%s\"\n", progname, argv[0]); - return 64; - } - if (!authz_repo_pattern_ok(argv[1])) { - fprintf(stderr, "%s: invalid repo pattern \"%s\" (flat names only, no slashes)\n", - progname, argv[1]); - return 64; - } - for (i = 3; i < argc; i++) { - if (argv[i][0] == '!' || argv[i][0] == '-') { - fprintf(stderr, "%s: ref patterns are additive, there is no deny: %s\n", - progname, argv[i]); - return 64; - } - if (!ref_field_ok(argv[i])) { - fprintf(stderr, "%s: not a ref pattern: %s\n", progname, argv[i]); - return 64; - } - } - - if (authz_reject_options(argc, argv) != 0) - return 64; - if (refuse_broken(path, &created) != 0) - return 3; - if (created) { - // 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> " - "[ref pattern ...] levels stack: " - "admin > force > write\n")); - lines_push(&lines, xstrdup("# Name the refs a rule covers: with no " - "patterns it covers the whole repository.\n")); - } else if (authz_read_lines(path, &lines) != 0) { - fprintf(stderr, "%s: cannot read %s: %s\n", progname, path, - strerror(errno)); - return 3; - } - - // 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 - continue; - } - 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 - replaced = (int)lines.n; - appended = 1; - } else { - size_t w; - - free(lines.v[first]); - lines.v[first] = text; - for (i = 0, w = 0; i < (int)lines.n; i++) { - if (lines.v[i] != NULL) - lines.v[w++] = lines.v[i]; - } - lines.n = w; - replaced = first + 1; - } - - { - char what[512]; - - if (created) - snprintf(what, sizeof(what), "granted %s %s %s (new policy, line %d)", - argv[0], argv[1], argv[2], replaced); - else if (dropped > 0) - snprintf(what, sizeof(what), "granted %s %s %s (line %d, %d duplicate(s) removed)", - argv[0], argv[1], argv[2], replaced, dropped); - else if (appended) - snprintf(what, sizeof(what), "granted %s %s %s (appended as line %d)", - argv[0], argv[1], argv[2], replaced); - else - snprintf(what, sizeof(what), "granted %s %s %s (replaced line %d)", - argv[0], argv[1], argv[2], replaced); - return finish(&lines, path, o, what); - } -} - -int cmd_revoke(const char *path, edit_opts o, int argc, char **argv) -{ - lines_t lines; - size_t w; - int i; - int gone = 0; - - if (argc != 2) { - fprintf(stderr, "%s: revoke wants <user> <repo>\n", progname); - return 64; - } - if (authz_reject_options(argc, argv) != 0) - return 64; - if (refuse_broken(path, NULL) != 0) - return 3; - if (authz_read_lines(path, &lines) != 0) { - fprintf(stderr, "%s: no rule for %s on %s to remove\n", progname, argv[0], - argv[1]); - return 1; - } - - for (i = 0, w = 0; i < (int)lines.n; i++) { - if (is_rule_for(lines.v[i], argv[0], argv[1])) { - free(lines.v[i]); - gone++; - continue; - } - lines.v[w++] = lines.v[i]; - } - lines.n = w; - - if (gone == 0) { - fprintf(stderr, "%s: no rule for %s on %s to remove\n", progname, argv[0], - argv[1]); - lines_free(&lines); - return 1; - } - { - char what[512]; - - snprintf(what, sizeof(what), "revoked %s on %s (%d rule(s) removed)", argv[0], - argv[1], gone); - return finish(&lines, path, o, what); - } -} |
