summaryrefslogtreecommitdiffstats
path: root/src/validate.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/validate.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/validate.c')
-rw-r--r--src/validate.c483
1 files changed, 483 insertions, 0 deletions
diff --git a/src/validate.c b/src/validate.c
new file mode 100644
index 0000000..fd8abb9
--- /dev/null
+++ b/src/validate.c
@@ -0,0 +1,483 @@
+/* validate.c - does this policy, and the host around it, add up?
+ *
+ * Run from the entrypoint before anything is served, and from CI. A container
+ * that starts with a policy nobody can read, or a hooks path that points
+ * nowhere, is a container that serves repositories nobody can push to - or
+ * anybody can. Both are found here, in the second, rather than in the first
+ * push of the morning.
+ *
+ * Errors mean "a push would be authorised wrongly". Warnings mean "a human is
+ * going to file a ticket". Missing files are skipped, not warned about: on a
+ * developer machine there is no sshd_config here, and a validator that cries
+ * about that gets ignored, which is worse than not checking.
+ */
+#include "authz.h"
+
+#include <ctype.h>
+#include <dirent.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+/* Tally of what this one run found. One validate per process, so file scope
+ * is enough and the helpers stay readable. */
+static int g_errors;
+static int g_warnings;
+
+static void verr(const char *fmt, ...)
+{
+ va_list ap;
+
+ g_errors++;
+ fputs("err ", stderr);
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+ fputc('\n', stderr);
+}
+
+static void vwarn(const char *fmt, ...)
+{
+ va_list ap;
+
+ g_warnings++;
+ fputs("warn ", stderr);
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+ fputc('\n', stderr);
+}
+
+static void vok(const char *fmt, ...)
+{
+ va_list ap;
+
+ fputs("ok ", stdout);
+ va_start(ap, fmt);
+ vfprintf(stdout, fmt, ap);
+ va_end(ap);
+ fputc('\n', stdout);
+}
+
+/* ------------------------------------------------------------- file lookups */
+
+static int ci_equal(const char *a, const char *b)
+{
+ for (; *a != '\0' && *b != '\0'; a++, b++) {
+ if (tolower((unsigned char)*a) != tolower((unsigned char)*b))
+ return 0;
+ }
+ return *a == '\0' && *b == '\0';
+}
+
+static char *strip(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;
+}
+
+/* An ini file's value for [section] key = value, last one winning. Used for
+ * gitconfig, where a value set twice is exactly the mistake worth reporting. */
+static int ini_value(const char *path, const char *section, const char *key,
+ char *out, size_t outsz)
+{
+ FILE *f;
+ char *line = NULL;
+ size_t cap = 0;
+ ssize_t len;
+ char here[64];
+ int in = 0;
+ int found = 0;
+
+ snprintf(here, sizeof(here), "[%s]", section);
+ f = fopen(path, "r");
+ if (f == NULL)
+ return -1;
+ while ((len = getline(&line, &cap, f)) != -1) {
+ char *t = strip(line);
+ char *eq;
+
+ if (*t == '\0' || *t == '#' || *t == ';')
+ continue;
+ if (*t == '[') {
+ in = strncmp(t, here, strlen(here)) == 0;
+ continue;
+ }
+ if (!in)
+ continue;
+ eq = strchr(t, '=');
+ if (eq == NULL)
+ continue;
+ *eq = '\0';
+ if (!ci_equal(strip(t), key))
+ continue;
+ snprintf(out, outsz, "%s", strip(eq + 1));
+ { /* gitconfig allows quoting */
+ size_t n = strlen(out);
+
+ if (n >= 2 && out[0] == '"' && out[n - 1] == '"') {
+ memmove(out, out + 1, n - 2);
+ out[n - 2] = '\0';
+ }
+ }
+ found = 1;
+ }
+ free(line);
+ fclose(f);
+ return found;
+}
+
+/* sshd_config's "Directive value", last one winning, case-insensitive. */
+static int directive_value(const char *path, const char *name, char *out, size_t outsz)
+{
+ FILE *f;
+ char *line = NULL;
+ size_t cap = 0;
+ ssize_t len;
+ int found = 0;
+
+ f = fopen(path, "r");
+ if (f == NULL)
+ return -1;
+ while ((len = getline(&line, &cap, f)) != -1) {
+ char *t = strip(line);
+ char *sp;
+
+ if (*t == '\0' || *t == '#')
+ continue;
+ sp = t;
+ while (*sp != '\0' && !isspace((unsigned char)*sp))
+ sp++;
+ if (*sp == '\0')
+ continue;
+ *sp = '\0';
+ if (!ci_equal(t, name))
+ continue;
+ snprintf(out, outsz, "%s", strip(sp + 1));
+ found = 1;
+ }
+ free(line);
+ fclose(f);
+ return found;
+}
+
+/* -------------------------------------------------------------- the checks */
+
+static void check_repos(const policy_t *p, const char *root)
+{
+ DIR *d;
+ struct dirent *ent;
+ int ondisk = 0;
+ size_t i;
+
+ d = opendir(root);
+ if (d == NULL) {
+ vok("no repository root at %s (nothing to cross-check)", root);
+ return;
+ }
+ while ((ent = readdir(d)) != NULL) {
+ size_t n = strlen(ent->d_name);
+ char path[4096];
+ struct stat st;
+
+ if (n <= 4 || strcmp(ent->d_name + n - 4, ".git") != 0)
+ continue;
+ snprintf(path, sizeof(path), "%s/%s", root, ent->d_name);
+ if (stat(path, &st) != 0 || !S_ISDIR(st.st_mode))
+ continue;
+ ondisk++;
+ {
+ char *name = authz_norm_repo(ent->d_name);
+
+ if (!authz_repo_reachable(p, name))
+ vwarn("%s is readable and nobody can push to it", name);
+ free(name);
+ }
+ }
+ closedir(d);
+
+ /* The other direction: a rule naming a repository that is not there is
+ * almost always a repository that was renamed, and the rule looks fine. */
+ for (i = 0; i < p->nrules; i++) {
+ const char *r = p->rules[i].repo;
+
+ if (r == NULL || strpbrk(r, "*?[") != NULL || strcmp(r, "*") == 0)
+ continue;
+ {
+ char path[4096];
+ struct stat st;
+
+ snprintf(path, sizeof(path), "%s/%s.git", root, r);
+ if (stat(path, &st) != 0)
+ vwarn("line %d gives %s access to %s, which is not in %s",
+ p->rules[i].line, p->rules[i].user, r, root);
+ }
+ }
+ if (ondisk > 0)
+ vok("%d repository/repositories under %s", ondisk, root);
+}
+
+static void check_hook(const policy_t *p, validate_opts o)
+{
+ char path[4096];
+ struct stat st;
+ char hooks[4096];
+ int rc;
+
+ (void)p;
+ snprintf(path, sizeof(path), "%s/pre-receive", o.hook_dir);
+ if (stat(path, &st) != 0) {
+ verr("no hook at %s: every push would be accepted unchecked", path);
+ return;
+ }
+ if (access(path, X_OK) != 0)
+ verr("%s is not executable: git would skip it", path);
+ else
+ vok("hook %s", path);
+
+ rc = ini_value(o.gitconfig, "core", "hooksPath", hooks, sizeof(hooks));
+ if (rc == -1) {
+ vok("no gitconfig at %s (nothing to cross-check)", o.gitconfig);
+ return;
+ }
+ if (rc == 0)
+ verr("%s does not set core.hooksPath: %s is never run", o.gitconfig, path);
+ else if (strcmp(hooks, o.hook_dir) != 0)
+ verr("core.hooksPath is %s, not %s: pushes are checked by something else",
+ hooks, o.hook_dir);
+ else
+ vok("core.hooksPath -> %s", hooks);
+}
+
+static void check_sshd(const policy_t *p, validate_opts o)
+{
+ char value[4096];
+ int rc;
+
+ rc = directive_value(o.sshd_config, "PermitUserEnvironment", value, sizeof(value));
+ if (rc == -1) {
+ vok("no sshd config at %s (nothing to cross-check)", o.sshd_config);
+ return;
+ }
+ if (rc == 0 || !ci_equal(value, "yes"))
+ verr("PermitUserEnvironment is not yes in %s: key identities are dropped, "
+ "and every push arrives anonymous", o.sshd_config);
+ else
+ vok("PermitUserEnvironment yes");
+
+ rc = directive_value(o.sshd_config, "ForceCommand", value, sizeof(value));
+ if (rc == 0)
+ verr("%s sets no ForceCommand: a key can run an interactive shell",
+ o.sshd_config);
+ else
+ vok("ForceCommand %s", value);
+
+ rc = directive_value(o.sshd_config, "AuthorizedKeysFile", value, sizeof(value));
+ if (rc == 1 && o.keys != NULL && strstr(value, o.keys) == NULL)
+ vwarn("AuthorizedKeysFile is \"%s\", which does not include %s", value, o.keys);
+ (void)p;
+}
+
+static void check_account(const policy_t *p, validate_opts o)
+{
+ FILE *f;
+ char *line = NULL;
+ size_t cap = 0;
+ ssize_t len;
+ int found = 0;
+
+ (void)p;
+ f = fopen(o.passwd, "r");
+ if (f == NULL) {
+ vok("no %s (nothing to cross-check)", o.passwd);
+ return;
+ }
+ while ((len = getline(&line, &cap, f)) != -1) {
+ char *fields[9];
+ int nf = 0;
+ char *q = line;
+
+ while (*q != '\0') {
+ while (*q != '\0' && *q != ':')
+ q++;
+ fields[nf++] = q;
+ if (*q == '\0')
+ break;
+ *q++ = '\0';
+ if (nf == 8)
+ break;
+ }
+ if (nf == 8 && strcmp(fields[0], "git") == 0) {
+ char *shell = strip(fields[7]);
+
+ found = 1;
+ if (strstr(shell, "git-shell") == NULL)
+ verr("the git account's shell is %s, not git-shell: ssh keys "
+ "would land in a real shell", shell);
+ else
+ vok("git account shell %s", shell);
+ break;
+ }
+ }
+ free(line);
+ fclose(f);
+ if (!found)
+ vwarn("no git account in %s", o.passwd);
+}
+
+static void check_keys(const policy_t *p, validate_opts o)
+{
+ FILE *f;
+ char *line = NULL;
+ size_t cap = 0;
+ ssize_t len;
+ int n = 0;
+
+ f = fopen(o.keys, "r");
+ if (f == NULL) {
+ vok("no authorized_keys at %s (nothing to cross-check)", o.keys);
+ return;
+ }
+ while ((len = getline(&line, &cap, f)) != -1) {
+ char *t = strip(line);
+ char *at;
+
+ if (*t == '\0' || *t == '#')
+ continue;
+ n++;
+ at = strstr(t, "GIT_HOST_USER=");
+ if (at == NULL) {
+ char *nl = strchr(t, '\n');
+ char *sp;
+
+ if (nl != NULL)
+ *nl = '\0';
+ sp = strchr(t, ' ');
+ if (sp != NULL)
+ *sp = '\0'; /* leave the key type, e.g. ssh-ed25519 */
+ vwarn("a %s key carries no GIT_HOST_USER: pushes made with it are "
+ "anonymous and will be denied", t);
+ continue;
+ }
+ at += strlen("GIT_HOST_USER=");
+ {
+ char name[256];
+ size_t i = 0;
+
+ while (at[i] != '\0' && at[i] != '"' && at[i] != ',' && at[i] != ' ' &&
+ i + 1 < sizeof(name)) {
+ name[i] = at[i];
+ i++;
+ }
+ name[i] = '\0';
+ if (i == 0)
+ vwarn("an empty GIT_HOST_USER: pushes made with that key are denied");
+ else if (!authz_user_known(p, name))
+ vwarn("key identity \"%s\" has no rule in %s: the key works and "
+ "every push is denied", name, p->path);
+ }
+ }
+ free(line);
+ fclose(f);
+ if (n > 0)
+ vok("%d ssh key(s) in %s", n, o.keys);
+}
+
+static void check_htpasswd(const policy_t *p, validate_opts o)
+{
+ FILE *f;
+ char *line = NULL;
+ size_t cap = 0;
+ ssize_t len;
+ int n = 0;
+
+ f = fopen(o.htpasswd, "r");
+ if (f == NULL) {
+ vok("no htpasswd at %s (nothing to cross-check)", o.htpasswd);
+ return;
+ }
+ while ((len = getline(&line, &cap, f)) != -1) {
+ char *t = strip(line);
+ char *colon;
+
+ if (*t == '\0' || *t == '#')
+ continue;
+ n++;
+ colon = strchr(t, ':');
+ if (colon == NULL) {
+ vwarn("%s: a line with no \":\" is not an htpasswd entry", o.htpasswd);
+ continue;
+ }
+ *colon = '\0';
+ if (!authz_user_known(p, t))
+ vwarn("htpasswd user \"%s\" has no rule in %s: they can authenticate "
+ "and change nothing", t, p->path);
+ }
+ free(line);
+ fclose(f);
+ if (n > 0)
+ vok("%d http user(s) in %s", n, o.htpasswd);
+}
+
+int cmd_validate(const policy_t *p, validate_opts o)
+{
+ size_t i;
+ struct stat st;
+
+ g_errors = 0;
+ g_warnings = 0;
+
+ /* Anything the parser already complained about went to stderr with its line
+ * number. A policy with errors is not used at all, so nothing else here
+ * matters until it is fixed. */
+ if (p->errors > 0) {
+ verr("%s: %d error(s): the policy is not used, and every push is denied",
+ p->path, p->errors);
+ return 3;
+ }
+ vok("%s: %zu rule(s), protected refs: %s", p->path, p->nrules,
+ authz_protected_for(p, "*"));
+
+ if (stat(p->path, &st) == 0 && (st.st_mode & 0004) == 0)
+ vwarn("%s is not readable by anyone outside its owner and group: the hook "
+ "runs as the git account and fails closed when it cannot read this",
+ p->path);
+
+ for (i = 0; i < p->nrules; i++) {
+ if (strcmp(p->rules[i].user, "@all") == 0 && p->rules[i].nrefs == 0)
+ vwarn("line %zu gives @all the whole of %s, which is not a default "
+ "anybody chose", p->rules[i].line, p->rules[i].repo);
+ }
+
+ if (o.root != NULL)
+ check_repos(p, o.root);
+ if (o.hook_dir != NULL)
+ check_hook(p, o);
+ if (o.sshd_config != NULL)
+ check_sshd(p, o);
+ if (o.passwd != NULL)
+ check_account(p, o);
+ if (o.keys != NULL)
+ check_keys(p, o);
+ if (o.htpasswd != NULL)
+ check_htpasswd(p, o);
+
+ if (g_errors > 0 || (o.strict && g_warnings > 0)) {
+ fprintf(stderr, "%s: validate: %d error(s), %d warning(s)\n",
+ progname, g_errors, g_warnings);
+ return 1;
+ }
+ printf("validate: %s is in force, %d rule(s), %d warning(s)\n",
+ p->path, (int)p->nrules, g_warnings);
+ return 0;
+}