summaryrefslogtreecommitdiffstats
path: root/src/policy.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/policy.c')
-rw-r--r--src/policy.c101
1 files changed, 40 insertions, 61 deletions
diff --git a/src/policy.c b/src/policy.c
index ef429e0..aae99a1 100644
--- a/src/policy.c
+++ b/src/policy.c
@@ -1,16 +1,12 @@
-/* policy.c - reading the policy, and answering from it.
- *
- * Grammar (see authz.conf for the prose version):
- *
- * <user> <repo> <write|force|admin> [<ref pattern> ...]
- *
- * '#' 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. */
+// <user> <repo> <write|force|admin> [<ref pattern> ...]
+//
+// '#' starts a comment anywhere on a line. Rules are additive and the strongest
+// wins. There is no 'deny': a rule is taken away by editing the file, not by
+// ordering it after another one.
+//
+// A line that does not parse makes the whole file unusable. Every push refused
+// for an evening is better than a mistyped line quietly widening access.
+
#include "authz.h"
#include <ctype.h>
@@ -19,10 +15,8 @@
#include <stdlib.h>
#include <string.h>
-#define DEFAULT_PROTECTED "refs/heads/main"
+// Spelled out rather than as a regular expression: the engine has none.
-/* 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[] = "_@*?[";
@@ -35,9 +29,7 @@ static int in_set(unsigned char c, const char *extra)
strchr(extra, (int)c) != NULL);
}
-/* ------------------------------------------------------------------ little */
-
-/* Trims in place: leading by moving the pointer, trailing by writing over it. */
+// Trims in place: leading by moving the pointer, trailing by writing over it.
char *authz_trim(char *s)
{
char *end;
@@ -67,7 +59,7 @@ int authz_split_ws(char *line, char **f, int max)
if (*p != '\0')
*p++ = '\0';
if (n == max)
- return n + 1; /* "too many", the caller decides */
+ return n + 1; // "too many"; the caller decides
}
return n;
}
@@ -90,8 +82,6 @@ static void perr(policy_t *p, int line, const char *fmt, ...)
va_end(ap);
}
-/* ------------------------------------------------------------ name shapes */
-
int authz_user_pattern_ok(const char *s)
{
size_t i;
@@ -118,8 +108,6 @@ int authz_repo_pattern_ok(const char *s)
return 1;
}
-/* ------------------------------------------------------------------ levels */
-
const char *authz_level_name(level_t l)
{
switch (l) {
@@ -143,8 +131,6 @@ int authz_level_of(const char *name)
return -1;
}
-/* ----------------------------------------------------------- normalisation */
-
char *authz_norm_repo(const char *in)
{
size_t n;
@@ -159,8 +145,9 @@ char *authz_norm_repo(const char *in)
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. */
+// One spelling for a ref: "main", "heads/main" and "refs/heads/main" are one
+// branch, and a bare name can never mean a tag.
+
char *authz_norm_ref(const char *in)
{
size_t n;
@@ -174,9 +161,9 @@ char *authz_norm_ref(const char *in)
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. */
+ // "heads/main" and friends are half a refname. Anything else, slashes
+ // included, is a branch name.
+
if (strncmp(in, "heads/", 6) == 0 || strncmp(in, "tags/", 5) == 0 ||
strncmp(in, "notes/", 6) == 0) {
n = strlen(in) + 6;
@@ -190,7 +177,6 @@ char *authz_norm_ref(const char *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;
@@ -218,12 +204,10 @@ static void parse_set(policy_t *p, char *line, int lineno)
}
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. */
+ // Refused by name, not left to "unknown setting": a policy that still
+ // carries this line must not be read as if the line were a comment, which
+ // would hand every branch to whoever holds write on it.
+
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\"");
@@ -343,8 +327,6 @@ int policy_load(policy_t *p)
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')
@@ -404,10 +386,9 @@ level_t authz_need_for(const char *ref, level_t have, char *raised,
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. */
+ // Neither a branch nor a tag is admin whatever a rule appears to grant.
+ // Nothing else raises a price: a rule says what it says.
+
if (!ordinary_ref(ref) && have != LV_ADMIN) {
if (raised != NULL)
snprintf(raised, raisedsz, "neither a branch nor a tag");
@@ -416,18 +397,16 @@ level_t authz_need_for(const char *ref, level_t have, char *raised,
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 */
+ 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 in the repository 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)
{
@@ -450,10 +429,10 @@ static void scope(const policy_t *p, const char *user, const char *repo,
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. */
+ // What this rule says about this ref, whatever its level: a refusal
+ // that names a level held elsewhere sends somebody to open the file
+ // and stare at it.
+
if (ref != NULL) {
level_t covers = LV_NONE;
@@ -504,10 +483,10 @@ 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?"
- * 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. */
+ // No ref named: can this person push anything here at all? 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);