diff options
Diffstat (limited to 'src/validate.c')
| -rw-r--r-- | src/validate.c | 426 |
1 files changed, 12 insertions, 414 deletions
diff --git a/src/validate.c b/src/validate.c index 6c2b19a..799d686 100644 --- a/src/validate.c +++ b/src/validate.c @@ -1,20 +1,14 @@ // 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. +// about to file a ticket. #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> // One validate per process, so file scope is enough. @@ -22,408 +16,25 @@ static int g_errors; static int g_warnings; // One message, three voices. The tag is five columns wide so the findings line -// up when they are read back out of a log. +// up when they are read back out of a log, and the counter is what --strict turns +// into a failure - an 'ok' counts nothing, so it passes NULL. -static void vmsg(FILE *f, const char *tag, const char *fmt, va_list ap) -{ - fputs(tag, f); - vfprintf(f, fmt, ap); - fputc('\n', f); -} - -static void verr(const char *fmt, ...) -{ - va_list ap; - - g_errors++; - va_start(ap, fmt); - vmsg(stderr, "err ", fmt, ap); - va_end(ap); -} - -static void vwarn(const char *fmt, ...) -{ - va_list ap; - - g_warnings++; - va_start(ap, fmt); - vmsg(stderr, "warn ", fmt, ap); - va_end(ap); -} - -static void vok(const char *fmt, ...) +static void vmsgv(FILE *f, const char *tag, int *counter, const char *fmt, ...) { va_list ap; + if (counter != NULL) + (*counter)++; + fputs(tag, f); va_start(ap, fmt); - vmsg(stdout, "ok ", fmt, ap); + vfprintf(f, fmt, ap); va_end(ap); + fputc('\n', f); } -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; -} - -// [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) -{ - lines_t lines; - size_t i; - char here[64]; - int in = 0; - int found = 0; - - snprintf(here, sizeof(here), "[%s]", section); - if (authz_read_lines(path, &lines) != 0) { - lines_free(&lines); - return -1; - } - for (i = 0; i < lines.n; i++) { - char *t = authz_trim(lines.v[i]); - 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(authz_trim(t), key)) - continue; - snprintf(out, outsz, "%s", authz_trim(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; - } - lines_free(&lines); - 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) -{ - lines_t lines; - size_t i; - - if (authz_read_lines(path, &lines) != 0) { - lines_free(&lines); - return -1; - } - for (i = 0; i < lines.n; i++) { - char *t = authz_trim(lines.v[i]); - 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", authz_trim(sp + 1)); - lines_free(&lines); - return 1; - } - lines_free(&lines); - 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) -{ - lines_t lines; - size_t i; - int found = 0; - - (void)p; - if (authz_read_lines(o.passwd, &lines) != 0) { - lines_free(&lines); - vok("no %s (nothing to cross-check)", o.passwd); - return; - } - for (i = 0; i < lines.n; i++) { - char *fields[8]; - int nf = 0; - char *q = lines.v[i]; - - // 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 = authz_trim(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; - } - } - lines_free(&lines); - if (!found) - vwarn("no git account in %s", o.passwd); -} - -static void check_keys(const policy_t *p, validate_opts o) -{ - lines_t lines; - size_t i; - int n = 0; - - if (authz_read_lines(o.keys, &lines) != 0) { - lines_free(&lines); - vok("no authorized_keys at %s (nothing to cross-check)", o.keys); - return; - } - for (i = 0; i < lines.n; i++) { - char *t = authz_trim(lines.v[i]); - 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); - } - } - lines_free(&lines); - if (n > 0) - vok("%d ssh key(s) in %s", n, o.keys); -} - -static void check_htpasswd(const policy_t *p, validate_opts o) -{ - lines_t lines; - size_t i; - int n = 0; - - if (authz_read_lines(o.htpasswd, &lines) != 0) { - lines_free(&lines); - vok("no htpasswd at %s (nothing to cross-check)", o.htpasswd); - return; - } - for (i = 0; i < lines.n; i++) { - char *t = authz_trim(lines.v[i]); - 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); - } - lines_free(&lines); - if (n > 0) - vok("%d http user(s) in %s", n, o.htpasswd); -} +#define verr(...) vmsgv(stderr, "err ", &g_errors, __VA_ARGS__) +#define vwarn(...) vmsgv(stderr, "warn ", &g_warnings, __VA_ARGS__) +#define vok(...) vmsgv(stdout, "ok ", NULL, __VA_ARGS__) int cmd_validate(const policy_t *p, validate_opts o) { @@ -459,19 +70,6 @@ int cmd_validate(const policy_t *p, validate_opts o) "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); |
