/* policy.c - reading the policy, and answering from it. * * Grammar (see authz.conf for the prose version): * * [ ...] * * '#' starts a comment wherever it appears. Rules are additive and the * strongest match wins; there is no 'deny', so a rule is taken away by editing * the file, not by ordering it after another one. * * A line that does not parse is an error, and a policy with errors is not used * at all: it is better for every push to be refused for one evening than for a * mistyped line to quietly widen somebody's access. */ #include "authz.h" #include #include #include #include #include #define DEFAULT_PROTECTED "refs/heads/main" /* The characters a name may contain, spelled out rather than as a regular * expression: the engine has none, and the message says why. */ static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; static const char user_first[] = "_@*?["; static const char user_rest[] = "_@*?[].-"; static const char repo_chars[] = "._*?-"; static int in_set(unsigned char c, const char *extra) { return c != '\0' && (strchr(letters, (int)c) != NULL || strchr(extra, (int)c) != NULL); } /* ------------------------------------------------------------------ little */ /* Trims in place: leading by moving the pointer, trailing by writing over it. */ char *authz_trim(char *s) { char *end; while (*s != '\0' && isspace((unsigned char)*s)) s++; end = s + strlen(s); while (end > s && isspace((unsigned char)end[-1])) end--; *end = '\0'; return s; } int authz_split_ws(char *line, char **f, int max) { int n = 0; char *p = line; while (*p != '\0') { while (*p != '\0' && isspace((unsigned char)*p)) p++; if (*p == '\0') break; f[n++] = p; while (*p != '\0' && !isspace((unsigned char)*p)) p++; if (*p != '\0') *p++ = '\0'; if (n == max) return n + 1; /* "too many", the caller decides */ } return n; } static void report(policy_t *p, int line, const char *kind, const char *fmt, va_list ap) { fprintf(stderr, "%s:%d: %s: ", p->path != NULL ? p->path : "authz.conf", line, kind); vfprintf(stderr, fmt, ap); fputc('\n', stderr); } static void perr(policy_t *p, int line, const char *fmt, ...) { va_list ap; p->errors++; va_start(ap, fmt); report(p, line, "error", fmt, ap); va_end(ap); } /* ------------------------------------------------------------ name shapes */ int authz_user_pattern_ok(const char *s) { size_t i; if (s[0] == '\0' || !in_set((unsigned char)s[0], user_first)) return 0; for (i = 1; s[i] != '\0'; i++) { if (!in_set((unsigned char)s[i], user_rest)) return 0; } return 1; } int authz_repo_pattern_ok(const char *s) { size_t i; if (s[0] == '\0') return 0; for (i = 0; s[i] != '\0'; i++) { if (!in_set((unsigned char)s[i], repo_chars)) return 0; } return 1; } /* ------------------------------------------------------------------ levels */ const char *authz_level_name(level_t l) { switch (l) { case LV_WRITE: return "write"; case LV_FORCE: return "force"; case LV_ADMIN: return "admin"; default: return "none"; } } int authz_level_of(const char *name) { if (name == NULL) return -1; if (strcmp(name, "write") == 0) return LV_WRITE; if (strcmp(name, "force") == 0) return LV_FORCE; if (strcmp(name, "admin") == 0) return LV_ADMIN; return -1; } /* ----------------------------------------------------------- normalisation */ char *authz_norm_repo(const char *in) { size_t n; if (in == NULL) return xstrdup(""); if (strcmp(in, "*") == 0) return xstrdup("*"); n = strlen(in); if (n > 4 && strcmp(in + n - 4, ".git") == 0) return xsubstr(in, n - 4); return xstrdup(in); } /* One spelling for a ref, so that "main", "heads/main" and "refs/heads/main" * are the same branch - and so that a bare name can never mean a tag. */ char *authz_norm_ref(const char *in) { size_t n; char *out; if (in == NULL || *in == '\0') return xstrdup("refs/*"); if (strcmp(in, "*") == 0 || strcmp(in, "all") == 0 || strcmp(in, "refs") == 0 || strcmp(in, "refs/*") == 0) return xstrdup("refs/*"); if (strncmp(in, "refs/", 5) == 0) return xstrdup(in); /* "heads/main", "tags/v1" and "notes/x" are half a refname: the "refs/" is * missing and nothing else. Anything at all, including a path with slashes * in it, is a branch name. */ if (strncmp(in, "heads/", 6) == 0 || strncmp(in, "tags/", 5) == 0 || strncmp(in, "notes/", 6) == 0) { n = strlen(in) + 6; out = xmalloc(n); snprintf(out, n, "refs/%s", in); return out; } n = strlen(in) + 12; out = xmalloc(n); snprintf(out, n, "refs/heads/%s", in); return out; } /* A whole list of them, space separated, the way 'set' lines store them. */ static rule_t *rule_slot(policy_t *p) { rule_t *r; if (p->nrules == p->rcap) { p->rcap = p->rcap != 0 ? p->rcap * 2 : 32; p->rules = xrealloc(p->rules, p->rcap * sizeof(*p->rules)); } r = &p->rules[p->nrules++]; memset(r, 0, sizeof(*r)); return r; } static void parse_set(policy_t *p, char *line, int lineno) { char *body = authz_trim(line + 3); char *sp = body; char *key; while (*sp != '\0' && !isspace((unsigned char)*sp)) sp++; if (*sp == '\0') { perr(p, lineno, "set needs a value"); return; } key = xsubstr(body, (size_t)(sp - body)); /* protected-refs was removed, and it is named rather than left to "unknown * setting" on purpose. A policy that still carries one of these lines has * to be refused rather than read as if the line were a comment: ignoring it * would leave every branch one fast-forward away from whoever holds 'write' * on them, which is the opposite of what the line was there to prevent, and * the operator would learn that from a push instead of from the parser. */ if (strncmp(key, "protected-refs", 14) == 0) perr(p, lineno, "protected-refs no longer exists: name the refs a rule " "covers instead, for example \"dave team admin refs/heads/main\""); else perr(p, lineno, "unknown setting \"%s\"", key); free(key); } static void parse_rule(policy_t *p, char *line, int lineno) { char *fields[MAX_FIELDS]; rule_t *r; int n, i, level; n = authz_split_ws(line, fields, MAX_FIELDS); if (n > MAX_FIELDS) { perr(p, lineno, "more than %d fields on one line", MAX_FIELDS); return; } if (n < 3) { perr(p, lineno, "expected \" [ref ...]\": %s", line); return; } level = authz_level_of(fields[2]); if (level < 0) { perr(p, lineno, "unknown access level \"%s\" (write force admin)", fields[2]); return; } if (strcmp(fields[0], "@all") != 0 && !authz_user_pattern_ok(fields[0])) { perr(p, lineno, "invalid user pattern \"%s\"", fields[0]); return; } if (!authz_repo_pattern_ok(fields[1])) { perr(p, lineno, "invalid repo pattern \"%s\" (flat names only, no slashes)", fields[1]); return; } for (i = 3; i < n; i++) { if (fields[i][0] == '!' || fields[i][0] == '-') { perr(p, lineno, "ref patterns are additive, there is no deny: %s", fields[i]); return; } } r = rule_slot(p); r->user = xstrdup(fields[0]); r->repo = authz_norm_repo(fields[1]); r->level = (level_t)level; r->line = lineno; if (n > 3) { r->refs = xmalloc((size_t)(n - 3) * sizeof(*r->refs)); for (i = 3; i < n; i++) r->refs[r->nrefs++] = authz_norm_ref(fields[i]); } } void policy_init(policy_t *p, const char *path) { memset(p, 0, sizeof(*p)); p->path = xstrdup(path); } void policy_free(policy_t *p) { size_t i; size_t j; for (i = 0; i < p->nrules; i++) { free(p->rules[i].user); free(p->rules[i].repo); for (j = 0; j < p->rules[i].nrefs; j++) free(p->rules[i].refs[j]); free(p->rules[i].refs); } free(p->rules); free(p->path); memset(p, 0, sizeof(*p)); } int policy_load(policy_t *p) { FILE *f; char *line = NULL; size_t cap = 0; ssize_t len; int lineno = 0; f = fopen(p->path, "r"); if (f == NULL) { p->unreadable = 1; fprintf(stderr, "%s: cannot read the policy: ", p->path); perror(""); return -2; } while ((len = getline(&line, &cap, f)) != -1) { char *hash; char *text; lineno++; text = authz_trim(line); hash = strchr(text, '#'); if (hash != NULL) { *hash = '\0'; text = authz_trim(text); } if (*text == '\0') continue; if (strncmp(text, "set", 3) == 0 && isspace((unsigned char)text[3])) parse_set(p, text, lineno); else parse_rule(p, text, lineno); } free(line); fclose(f); return p->errors > 0 ? -1 : 0; } /* ------------------------------------------------------------- the answers */ static int user_match(const policy_t *p, const char *pat, const char *user) { if (user == NULL || user[0] == '\0') return 0; if (strcmp(pat, "@all") == 0) return authz_user_known(p, user); return glob_match(pat, user); } static int repo_match(const char *pat, const char *repo) { char *want; int hit; if (strcmp(pat, "*") == 0) return 1; want = authz_norm_repo(repo); hit = glob_match(pat, want); free(want); return hit; } int authz_user_known(const policy_t *p, const char *user) { size_t i; if (user == NULL || user[0] == '\0') return 0; for (i = 0; i < p->nrules; i++) { if (strcmp(p->rules[i].user, "@all") == 0) continue; if (glob_match(p->rules[i].user, user)) return 1; } return 0; } int authz_repo_reachable(const policy_t *p, const char *repo) { size_t i; for (i = 0; i < p->nrules; i++) { if (repo_match(p->rules[i].repo, repo)) return 1; } return 0; } static int ordinary_ref(const char *ref) { return strncmp(ref, "refs/heads/", 11) == 0 || strncmp(ref, "refs/tags/", 10) == 0; } level_t authz_need_for(const char *ref, level_t have, char *raised, size_t raisedsz) { if (raised != NULL && raisedsz > 0) raised[0] = '\0'; /* Anything that is not a branch or a tag is for maintainers, whatever a rule * appears to grant about it. Nothing else raises a price here: a rule says * what it says, and there is no second, invisible list that can make a ref * cost more behind the operator's back. */ if (!ordinary_ref(ref) && have != LV_ADMIN) { if (raised != NULL) snprintf(raised, raisedsz, "neither a branch nor a tag"); return LV_ADMIN; } return have; } /* Everything one user may do to one repository at or above 'min': whether any * of it is unscoped, whether 'ref' is covered, and the strongest level the user * holds there at all (which is what a denial names). */ typedef struct { int unscoped; /* a rule with no ref list: the whole repository */ int scoped; /* a rule with a ref list, at or above the level */ int hit; /* and one of its patterns is this ref */ level_t strongest; /* the best this user holds there at all */ level_t here; /* the best held on this ref specifically */ int any; /* any rule at all, at any level */ } scope_t; static void scope(const policy_t *p, const char *user, const char *repo, level_t min, const char *ref, scope_t *s) { size_t i; size_t j; s->unscoped = 0; s->scoped = 0; s->hit = 0; s->strongest = LV_NONE; s->here = LV_NONE; s->any = 0; for (i = 0; i < p->nrules; i++) { if (!user_match(p, p->rules[i].user, user)) continue; if (!repo_match(p->rules[i].repo, repo)) continue; s->any = 1; if (p->rules[i].level > s->strongest) s->strongest = p->rules[i].level; /* What this rule says about the ref in question, whatever its level: a * refusal has to name the level the user actually holds on this ref, * because "carol has force on team" while refusing her a force push to * main is a sentence that sends somebody to open the file and stare. */ if (ref != NULL) { level_t covers = LV_NONE; if (p->rules[i].nrefs == 0) covers = p->rules[i].level; for (j = 0; j < p->rules[i].nrefs; j++) { if (glob_match(p->rules[i].refs[j], ref)) { covers = p->rules[i].level; break; } } if (covers > s->here) s->here = covers; } if (p->rules[i].level < min) continue; if (p->rules[i].nrefs == 0) { s->unscoped = 1; continue; } s->scoped = 1; for (j = 0; ref != NULL && j < p->rules[i].nrefs; j++) { if (glob_match(p->rules[i].refs[j], ref)) { s->hit = 1; break; } } } } void authz_check(const policy_t *p, const char *user, const char *repo, level_t need, const char *ref, decision_t *out) { scope_t s; char raised[64]; char *nref = NULL; level_t want = need; out->allowed = 0; out->need = need; out->why[0] = '\0'; if (user == NULL || user[0] == '\0') { out->need = need; snprintf(out->why, sizeof(out->why), "no identity was presented"); return; } if (ref == NULL) { /* No ref named: "may this user do this somewhere in the repository?" * This is the question 'can this person push anything at all here', and * it is answered by whether any rule reaches the repository, not by * which refs a scoped rule would cover. */ char *r = authz_norm_repo(repo); scope(p, user, repo, need, NULL, &s); if (s.unscoped || s.scoped) { snprintf(out->why, sizeof(out->why), "%s has %s on %s of %s", user, authz_level_name(need), s.unscoped ? "all" : "some refs", r); out->allowed = 1; } else { snprintf(out->why, sizeof(out->why), "%s has no %s on %s", user, authz_level_name(need), r); } free(r); return; } nref = authz_norm_ref(ref); want = authz_need_for(nref, need, raised, sizeof(raised)); out->need = want; scope(p, user, repo, want, nref, &s); if (s.unscoped || s.hit) { char *r = authz_norm_repo(repo); if (raised[0] != '\0') snprintf(out->why, sizeof(out->why), "%s has %s on %s %s (this ref costs %s, not %s: %s)", user, authz_level_name(want), r, nref, authz_level_name(want), authz_level_name(need), raised); else snprintf(out->why, sizeof(out->why), "%s has %s on %s %s", user, authz_level_name(want), r, nref); free(r); out->allowed = 1; free(nref); return; } { char *r = authz_norm_repo(repo); if (!s.any) snprintf(out->why, sizeof(out->why), "%s has no rule for %s; %s needs %s", user, r, nref, authz_level_name(want)); else if (s.here == LV_NONE) snprintf(out->why, sizeof(out->why), "%s holds %s on %s, but not on %s; that ref needs %s%s%s%s", user, authz_level_name(s.strongest), r, nref, authz_level_name(want), raised[0] != '\0' ? " (" : "", raised[0] != '\0' ? raised : "", raised[0] != '\0' ? ")" : ""); else if (raised[0] != '\0') snprintf(out->why, sizeof(out->why), "%s has %s on %s %s; it needs %s (%s)", user, authz_level_name(s.here), r, nref, authz_level_name(want), raised); else snprintf(out->why, sizeof(out->why), "%s has %s on %s %s; it needs %s", user, authz_level_name(s.here), r, nref, authz_level_name(want)); free(r); } free(nref); }