summaryrefslogtreecommitdiffstats
path: root/src/glob.c
diff options
context:
space:
mode:
authordev <dev@example>2026-11-13 19:04:50 +0000
committerdev <dev@example>2026-11-13 19:04:50 +0000
commitf87a792743227ea06cfa1580f8d30a618b4d1a04 (patch)
tree7af494393b4a316f620bab7de79b15230df2cf36 /src/glob.c
downloadaccess-control-f87a792743227ea06cfa1580f8d30a618b4d1a04.tar.gz
access-control-f87a792743227ea06cfa1580f8d30a618b4d1a04.zip
git-authz: one C program for who may move which refs
A git host that anonymous-clones and shared-pushes has one question left to answer: which human may move which ref. This answers it from one plain file, in one binary, with no runtime beyond libc. Four modes, because they must agree exactly about what a rule means - a check that answers differently from the hook is a program that lies: check one question, for an operator or a script authorize a whole push, the way core.hooksPath hands it over: identity from GIT_HOST_USER (ssh key option) or REMOTE_USER (http auth), and one "old new ref" line per ref on stdin. Also runs as "pre-receive", which is how git finds it. validate does the policy parse, and is the host wired to run it edit grant, revoke, protect (the deployed policy is a read-only mount on purpose, so these are for the source copy and say so when told) The verdict is the exit status, since the caller is a hook: 0 allowed, 1 denied, 3 no usable policy, 64 usage. A policy that cannot be read or does not parse denies everything rather than guessing, and one bad line poisons the whole file on purpose: a mistyped rule that quietly widens access is worse than an evening of refused pushes. Semantics kept from the shell engine this replaces: levels are monotone (admin implies force implies write), rules add up rather than order, "@" is only everyone the policy already names, protected refs cost one step more than the action asked for, and refs that are neither branches nor tags are for admins. "main", "heads/main" and "refs/heads/main" are one branch; a bare name is never a tag. Two facts a push does not carry - is old an ancestor of new, and which branch HEAD points at - are asked of git, and an answer that cannot be had is taken the expensive way. tests/unit.c covers the matcher directly (173 checks); the behavioural suite that drives this the way git does follows next.
Diffstat (limited to 'src/glob.c')
-rw-r--r--src/glob.c105
1 files changed, 105 insertions, 0 deletions
diff --git a/src/glob.c b/src/glob.c
new file mode 100644
index 0000000..7c6b969
--- /dev/null
+++ b/src/glob.c
@@ -0,0 +1,105 @@
+/* glob.c - the only pattern matching in this program.
+ *
+ * Three metacharacters, no library. See the note in authz.h for why this is
+ * not fnmatch(3) or regex(3). */
+#include "authz.h"
+
+/* Length of the class starting at p ('[' ... ']'), or 0 when it is not closed
+ * and therefore means a literal '['. A ']' first - or first after a negation -
+ * is a member, like everywhere else. */
+static int class_len(const char *p)
+{
+ const char *c = p + 1;
+
+ if (*c == '!' || *c == '^')
+ c++;
+ if (*c == ']')
+ c++;
+ while (*c != '\0' && *c != ']')
+ c++;
+ return (*c == ']') ? (int)(c - p) + 1 : 0;
+}
+
+/* text is a complete class of len bytes, including the brackets. */
+static int class_match(const char *text, int len, char c)
+{
+ const char *p = text + 1;
+ const char *end = text + len - 1; /* the ']' */
+ int negate = 0;
+ int hit = 0;
+
+ if (p < end && (*p == '!' || *p == '^')) {
+ negate = 1;
+ p++;
+ }
+ while (p < end) {
+ char lo = *p++;
+ if (p + 1 < end && *p == '-') { /* 'a-z'; a trailing '-' is literal */
+ char hi = p[1];
+ p += 2;
+ if (c >= lo && c <= hi)
+ hit = 1;
+ } else if (lo == c) {
+ hit = 1;
+ }
+ }
+ return negate ? !hit : hit;
+}
+
+int glob_match(const char *pattern, const char *s)
+{
+ const char *p = pattern;
+ const char *t = s;
+ const char *star = NULL; /* in the pattern, just after the last '*' */
+ const char *back = NULL; /* in the text, where that '*' may yet start */
+ int clen;
+
+ if (pattern == NULL || s == NULL)
+ return 0;
+
+ while (*t != '\0') {
+ if (*p == '*') {
+ star = p + 1;
+ back = t;
+ p++;
+ continue;
+ }
+
+ clen = (*p == '[') ? class_len(p) : 0;
+ if (clen > 0) {
+ if (!class_match(p, clen, *t)) {
+ /* The class was well formed and said no. Backtrack to the
+ * last '*' rather than comparing '[' against the text: a
+ * negated class must be able to fail. */
+ if (star == NULL)
+ return 0;
+ p = star;
+ t = ++back;
+ continue;
+ }
+ p += clen;
+ t++;
+ continue;
+ }
+
+ if (*p == '?') {
+ p++;
+ t++;
+ continue;
+ }
+ if (*p == *t) {
+ p++;
+ t++;
+ continue;
+ }
+
+ if (star == NULL)
+ return 0;
+ p = star;
+ t = ++back;
+ }
+
+ while (*p == '*')
+ p++;
+ return *p == '\0';
+}