diff options
Diffstat (limited to 'src/policy.c')
| -rw-r--r-- | src/policy.c | 56 |
1 files changed, 46 insertions, 10 deletions
diff --git a/src/policy.c b/src/policy.c index ffce34d..1866bb2 100644 --- a/src/policy.c +++ b/src/policy.c @@ -385,6 +385,7 @@ int policy_load(policy_t *p) f = fopen(p->path, "r"); if (f == NULL) { + p->unreadable = 1; fprintf(stderr, "%s: cannot read the policy: ", p->path); perror(""); return -2; @@ -535,6 +536,7 @@ typedef struct { 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; @@ -548,6 +550,7 @@ static void scope(const policy_t *p, const char *user, const char *repo, s->scoped = 0; s->hit = 0; s->strongest = LV_NONE; + s->here = LV_NONE; s->any = 0; for (i = 0; i < p->nrules; i++) { @@ -558,6 +561,26 @@ static void scope(const policy_t *p, const char *user, const char *repo, 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) { @@ -593,17 +616,23 @@ void authz_check(const policy_t *p, const char *user, const char *repo, } if (ref == NULL) { - /* No ref named: "may this user do this somewhere in the repository?" */ + /* 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", authz_norm_repo(repo)); + 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), authz_norm_repo(repo)); + user, authz_level_name(need), r); } + free(r); return; } @@ -635,16 +664,23 @@ void authz_check(const policy_t *p, const char *user, const char *repo, if (!s.any) snprintf(out->why, sizeof(out->why), "%s has no rule for %s; %s needs %s", - user, authz_norm_repo(repo), nref, authz_level_name(want)); - else if (raised[0] != '\0') + user, r, nref, authz_level_name(want)); + else if (s.here == LV_NONE) snprintf(out->why, sizeof(out->why), - "%s has %s on %s; %s needs %s (raised from %s: %s is %s)", + "%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), authz_level_name(need), nref, raised); + 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 needs %s", - user, authz_level_name(s.strongest), r, nref, - authz_level_name(want)); + 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); |
