/* policy.c - reading the policy, and answering from it. * * Grammar (see authz.conf for the prose version): * * set protected-refs ... globally, default refs/heads/main * set protected-refs. ... for one repository, replacing it * [ ...] * * '#' 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); } static void pwarn(policy_t *p, int line, const char *fmt, ...) { va_list ap; p->warnings++; va_start(ap, fmt); report(p, line, "warning", 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 char *norm_ref_list(const char *value) { char *copy = xstrdup(value); char *fields[MAX_FIELDS]; strbuf_t out; int i, n; sb_init(&out); n = authz_split_ws(copy, fields, MAX_FIELDS); for (i = 0; i < n; i++) { char *one = authz_norm_ref(fields[i]); if (out.len != 0) sb_add(&out, " "); sb_add(&out, one); free(one); } free(copy); return out.s; } /* ------------------------------------------------------------------ tables */ 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 set_protected_for(policy_t *p, const char *repo, const char *refs, int line) { size_t i; for (i = 0; i < p->nprot; i++) { if (strcmp(p->prot_repo[i], repo) == 0) { pwarn(p, line, "protected-refs for %s is set twice; the last one wins", repo); free(p->prot_refs[i]); p->prot_refs[i] = xstrdup(refs); return; } } if (p->nprot == p->pcap) { p->pcap = p->pcap != 0 ? p->pcap * 2 : 8; p->prot_repo = xrealloc(p->prot_repo, p->pcap * sizeof(*p->prot_repo)); p->prot_refs = xrealloc(p->prot_refs, p->pcap * sizeof(*p->prot_refs)); } p->prot_repo[p->nprot] = xstrdup(repo); p->prot_refs[p->nprot] = xstrdup(refs); p->nprot++; } /* ---------------------------------------------------------------- the file */ static void parse_set(policy_t *p, char *line, int lineno) { char *body = authz_trim(line + 3); char *sp = body; char *key, *val, *repo; 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)); val = authz_trim(sp); if (strcmp(key, "protected-refs") == 0) { if (p->protected_global != NULL) pwarn(p, lineno, "set protected-refs appears twice; the last one wins"); free(p->protected_global); p->protected_global = norm_ref_list(val); } else if (strncmp(key, "protected-refs.", 15) == 0) { repo = authz_norm_repo(key + 15); if (repo[0] == '\0' || strcmp(repo, "*") == 0) { perr(p, lineno, "protected-refs. needs one repository, not \"%s\"", key + 15); } else { set_protected_for(p, repo, val, lineno); } free(repo); } 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->protected_global); for (i = 0; i < p->nprot; i++) { free(p->prot_repo[i]); free(p->prot_refs[i]); } free(p->prot_repo); free(p->prot_refs); 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; } const char *authz_protected_for(const policy_t *p, const char *repo) { char *want; size_t i; want = authz_norm_repo(repo); for (i = 0; i < p->nprot; i++) { if (strcmp(p->prot_repo[i], want) == 0) { free(want); return p->prot_refs[i]; } } free(want); if (p->protected_global != NULL && p->protected_global[0] != '\0') return p->protected_global; return DEFAULT_PROTECTED; } static int is_protected(const policy_t *p, const char *repo, const char *ref) { const char *list = authz_protected_for(p, repo); char *copy = xstrdup(list); char *fields[MAX_FIELDS]; int n, i, hit = 0; n = authz_split_ws(copy, fields, MAX_FIELDS); for (i = 0; i < n; i++) { if (glob_match(fields[i], ref)) { hit = 1; break; } } free(copy); return hit; } 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 policy_t *p, const char *repo, 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: refs/notes, * refs/merge-requests, whatever a tool invents next. */ if (!ordinary_ref(ref) && have != LV_ADMIN) { if (raised != NULL) snprintf(raised, raisedsz, "neither a branch nor a tag"); return LV_ADMIN; } if (!is_protected(p, repo, ref)) return have; if (have == LV_WRITE || have == LV_FORCE) { if (raised != NULL) snprintf(raised, raisedsz, "protected"); return have == LV_WRITE ? LV_FORCE : 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(p, repo, 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 (raised from %s: %s is %s)", user, authz_level_name(want), r, nref, authz_level_name(need), nref, 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 has %s on %s, and nothing at all on %s; it needs %s%s%s%s", user, authz_level_name(s.strongest), r, nref, authz_level_name(want), raised[0] != '\0' ? " (raised: " : "", 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 (raised from %s: %s)", user, authz_level_name(s.here), r, nref, authz_level_name(want), authz_level_name(need), 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); }