// Run from the entrypoint before anything is served, and from CI. // // An error means a push would be authorised wrongly; a warning means a human is // about to file a ticket. Missing files are skipped rather than warned about: a // validator that cries about a developer machine gets ignored, which is worse // than not checking. #include "authz.h" #include #include #include #include #include #include #include #include // One validate per process, so file scope is enough. 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); } 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'; } // git reads a section name without regard to case, so [CORE] opens core. static int ci_prefix(const char *line, const char *want) { while (*want != '\0') { if (tolower((unsigned char)*line) != tolower((unsigned char)*want)) return 0; line++; want++; } return 1; } 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; } // [section] key = value, last one winning - which is the mistake worth // reporting in a gitconfig. 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 = ci_prefix(t, here); 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", case-insensitive. sshd keeps the first value // it reads for a directive, not the last, so the first match is the answer and // every later line is one sshd never looked at - which is the difference between // a host that accepts key identities and one that silently drops them. 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; 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)); free(line); fclose(f); return 1; } free(line); fclose(f); return 0; } 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); // A rule naming a repository that is not there is a renamed repository, // and the rule still 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 != 1) verr("%s sets no ForceCommand: a key can run an interactive shell", o.sshd_config); else if (ci_equal(value, "none")) verr("ForceCommand is none in %s: that is how a Match block turns it back " "off, and 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[8]; int nf = 0; char *q = line; // The seventh field is the shell, which decides whether a key can open // a shell at all. Splitting on eight would make the shell field 6. while (*q != '\0') { char *start = q; while (*q != '\0' && *q != ':') q++; if (*q == '\0') { fields[nf++] = start; break; } *q++ = '\0'; fields[nf++] = start; if (nf == 7) break; } if (nf >= 7 && strcmp(fields[0], "git") == 0) { char *shell = strip(fields[6]); 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; // The parser already printed the line numbers. A policy with errors is not // used at all, so nothing else matters until it is fixed. if (p->unreadable) { verr("%s cannot be read: the hook fails closed on this, so every push is " "denied until it is mounted", p->path); return 3; } 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)", p->path, p->nrules); 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; }