summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/authz.h52
-rw-r--r--src/edit.c423
-rw-r--r--src/hook.c11
-rw-r--r--src/policy.c211
-rw-r--r--src/util.c13
-rw-r--r--src/validate.c426
6 files changed, 151 insertions, 985 deletions
diff --git a/src/authz.h b/src/authz.h
index d107fd8..ad39b35 100644
--- a/src/authz.h
+++ b/src/authz.h
@@ -10,7 +10,10 @@
#include <getopt.h>
#include <stddef.h>
-#define MAX_FIELDS 64 // one rule line, split on whitespace // one rule line, split on whitespace
+#define MAX_FIELDS 64 // one rule line, split on whitespace
+#define AUTHZ_MSGSZ 512 // one complaint, in the words the caller prints
+#define DEFAULT_ROOT "/var/lib/git" // where the repositories live; the hook
+ // derives a repo name from this too
typedef enum {
LV_NONE = 0,
@@ -67,6 +70,9 @@ char *authz_norm_ref(const char *in);
char *authz_trim(char *s);
+// A policy line's body: trimmed, comment and all after it gone. Edits the line.
+char *authz_line_body(char *line);
+
// More than 'max' fields returns max+1, so the caller complains rather than
// reading past the array.
int authz_split_ws(char *line, char **fields, int max);
@@ -83,6 +89,14 @@ int authz_level_of(const char *name); // -1 for anything that is not one
// '@all' means everybody this policy already names; it never invents a name.
int authz_user_known(const policy_t *p, const char *user);
+// The grammar, in one place: check and the file parser must refuse the same
+// lines for the same reason, because a rule the parser rejects denies every push
+// in the repository. Fills msg with the complaint in the words the caller
+// prints; returns 0 when the fields parse. A NULL user or repo is a field the
+// caller is not asking about.
+int authz_rule_validate(const char *user, const char *repo, const char *level,
+ char *const *refs, int nrefs, char *msg, size_t msgs);
+
// What a ref costs beyond what the rule said. Neither a branch nor a tag is
// admin whatever a rule grants - refs/notes, refs/merge-requests, whatever a tool
// invents next. The reason goes in raised (>= 64 bytes).
@@ -95,10 +109,6 @@ level_t authz_need_for(const char *ref, level_t have, char *raised,
void authz_check(const policy_t *p, const char *user, const char *repo,
level_t need, const char *ref, decision_t *out);
-// False means no rule reaches it at all - usually a name that stopped matching.
-
-int authz_repo_reachable(const policy_t *p, const char *repo);
-
// hook.c --------------------------------------------------------------------
// authorize mode: identity from the environment, "old new ref" on stdin, the
@@ -121,35 +131,11 @@ int cmd_authorize(const policy_t *p, hook_opts o, int argc, char **argv);
// validate.c ----------------------------------------------------------------
typedef struct {
- const char *root; // where the repositories live
- const char *hook_dir;
- const char *gitconfig;
- const char *sshd_config;
- const char *passwd;
- const char *keys;
- const char *htpasswd;
int strict; // warnings are errors
} validate_opts;
int cmd_validate(const policy_t *p, validate_opts o);
-// edit.c --------------------------------------------------------------------
-
-typedef struct {
- const char *out; // NULL edits in place; "-" is stdout
-} edit_opts;
-
-// Pulls --out and --stdout out of argv wherever they sit and compacts what is
-// left, returning the count in *consumed. -1 on a missing value.
-int authz_parse_out(int argc, char **argv, edit_opts *o, int *consumed);
-
-// A name starting with "--" is a mistyped option, not something to grant.
-// Prints and returns 1 when it finds one.
-int authz_reject_options(int argc, char **argv);
-
-int cmd_grant(const char *path, edit_opts o, int argc, char **argv);
-int cmd_revoke(const char *path, edit_opts o, int argc, char **argv);
-
// util.c --------------------------------------------------------------------
// A growing string. strncat with hand-computed room is how buffer bugs are
@@ -174,8 +160,8 @@ void lines_free(lines_t *l);
// 0, -1 when the file cannot be opened, or -2 when reading stopped before the
// end - a directory, an I/O error halfway, a mount pulled out. errno says which.
-// The caller decides what a missing file means: an emergency for the policy, a
-// plain fact for a validator cross-checking a host.
+// The caller decides what a missing file means: an emergency for the policy,
+// nothing at all for a file that is optional here.
int authz_read_lines(const char *path, lines_t *l);
void sb_init(strbuf_t *b);
@@ -202,4 +188,8 @@ const char *authz_optname(int val, const struct option *lo);
void authz_opt_reset(void);
+// The two complaints every getopt_long loop would otherwise write by hand.
+void authz_opt_need_value(const struct option *lo);
+void authz_opt_unknown(const char *cmd, const char *arg);
+
#endif /* AUTHZ_H */
diff --git a/src/edit.c b/src/edit.c
deleted file mode 100644
index 9e5ff33..0000000
--- a/src/edit.c
+++ /dev/null
@@ -1,423 +0,0 @@
-// The deployed policy is a read-only mount, on purpose: that is what makes it
-// reviewable and the rollout visible. These are for the source copy, and say so
-// when the file cannot be written rather than pretending to have granted
-// something.
-//
-// Comments survive, because the file is also the record of why an access exists.
-// The rewrite is a whole file to a temporary and a rename: a policy truncated
-// half way through a write is a policy that denies everything.
-
-#include "authz.h"
-
-#include <ctype.h>
-#include <errno.h>
-#include <libgen.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/stat.h>
-#include <unistd.h>
-
-#define HIDDEN_PREFIX "/.authz"
-#define TEMPLATE_SUFFIX ".XXXXXX"
-
-// A temporary beside the target, then rename: a reader sees the old file or the
-// new one. Mode and owner are carried over, because a policy that becomes 0600
-// root-owned stops working for the git account.
-
-static int write_atomic(const lines_t *l, const char *path)
-{
- char *dirtext = xstrdup(path);
- char *dir;
- char *template;
- size_t need;
- struct stat st;
- int fd;
- size_t i;
- int had = stat(path, &st) == 0;
-
- // dirname may return a pointer into its argument or a fresh one.
-
- dir = dirname(dirtext);
- if (dir == NULL)
- dir = (char *)".";
- need = strlen(dir) + strlen(HIDDEN_PREFIX) + strlen(TEMPLATE_SUFFIX) + 1;
- template = xmalloc(need);
- snprintf(template, need, "%s%s%s", dir, HIDDEN_PREFIX, TEMPLATE_SUFFIX);
- fd = mkstemp(template);
- if (fd < 0) {
- fprintf(stderr, "%s: cannot write beside %s: %s\n", progname, path,
- strerror(errno));
- free(template);
- free(dirtext);
- return -1;
- }
- for (i = 0; i < l->n; i++) {
- size_t n = strlen(l->v[i]);
- size_t off = 0;
-
- while (off < n) {
- ssize_t w = write(fd, l->v[i] + off, n - off);
-
- if (w <= 0) {
- fprintf(stderr, "%s: write failed: %s\n", progname, strerror(errno));
- close(fd);
- unlink(template);
- free(template);
- free(dirtext);
- return -1;
- }
- off += (size_t)w;
- }
- }
- if (fchmod(fd, had ? (st.st_mode & 07777) : 0644) != 0)
- fprintf(stderr, "%s: warning: chmod: %s\n", progname, strerror(errno));
- if (had && geteuid() == 0 && fchown(fd, st.st_uid, st.st_gid) != 0)
- fprintf(stderr, "%s: warning: chown: %s\n", progname, strerror(errno));
- if (fsync(fd) != 0)
- fprintf(stderr, "%s: warning: fsync: %s\n", progname, strerror(errno));
- if (close(fd) != 0) {
- fprintf(stderr, "%s: close failed: %s\n", progname, strerror(errno));
- unlink(template);
- free(template);
- free(dirtext);
- return -1;
- }
- if (rename(template, path) != 0) {
- fprintf(stderr, "%s: cannot replace %s: %s\n", progname, path, strerror(errno));
- if (errno == EROFS || errno == EACCES || errno == EPERM)
- fprintf(stderr, "%s: the policy is read-only by design. Edit the "
- "source copy (git-host/authz.conf), roll the ConfigMap, or "
- "write elsewhere with --out.\n", progname);
- unlink(template);
- free(template);
- free(dirtext);
- return -1;
- }
- free(template);
- free(dirtext);
- return 0;
-}
-
-// ------------------------------------------------------------------ matching
-
-// Copied before splitting: splitting writes over the text, and these lines are
-// the file.
-
-static int is_rule_for(const char *line, const char *user, const char *repo)
-{
- char *copy = xstrdup(line);
- char *fields[MAX_FIELDS];
- char *text = authz_trim(copy);
- char *hash = strchr(text, '#');
- char *want;
- char *have;
- int same = 0;
-
- if (hash != NULL)
- *hash = '\0';
- if (authz_split_ws(text, fields, MAX_FIELDS) >= 3) {
- want = authz_norm_repo(repo);
- have = authz_norm_repo(fields[1]);
- same = strcmp(fields[0], user) == 0 && strcmp(have, want) == 0;
- free(want);
- free(have);
- }
- free(copy);
- return same;
-}
-
-// A ref pattern reaches the file as one field. Whitespace would split it into
-// two patterns when the file is read back - granting a ref nobody asked for - and
-// '#' starts a comment, so the pattern would be quietly cut short. An argument
-// carrying either means something other than what was typed.
-
-static int ref_field_ok(const char *s)
-{
- size_t i;
-
- if (s[0] == '\0')
- return 0;
- for (i = 0; s[i] != '\0'; i++) {
- unsigned char c = (unsigned char)s[i];
-
- if (isspace(c) || c == '#')
- return 0;
- }
- return 1;
-}
-
-static char *rule_text(const char *user, const char *repo, const char *level,
- int nrefs, char **refs)
-{
- strbuf_t b;
- int i;
-
- sb_init(&b);
- sb_addf(&b, "%s %s %s", user, repo, level);
- for (i = 0; i < nrefs; i++) {
- char *one = authz_norm_ref(refs[i]);
-
- sb_addf(&b, " %s", one);
- free(one);
- }
- sb_add(&b, "\n");
- return b.s;
-}
-
-// A file that does not parse is refused: rewriting the parts that parse would
-// keep the damage. A file that is not there yet is bootstrapped with the
-// grammar
-// written at the top.
-
-static int refuse_broken(const char *path, int *created)
-{
- policy_t probe;
- int rc;
-
- if (created != NULL)
- *created = 0;
- policy_init(&probe, path);
- rc = policy_load(&probe);
- policy_free(&probe);
- if (rc == -1) {
- fprintf(stderr, "%s: %s does not parse; fix it before editing\n", progname, path);
- return 3;
- }
- if (rc == -2 && created != NULL)
- *created = 1;
- return 0;
-}
-
-// --out and --stdout are allowed anywhere, because "grant bob team write --out
-// /tmp/new.conf" is what people type; parsing them only at the front made "--
-// out"
-// a ref pattern.
-
-int authz_parse_out(int argc, char **argv, edit_opts *o, int *consumed)
-{
- int i;
- int n = 0;
-
- for (i = 0; i < argc; i++) {
- if (strcmp(argv[i], "--stdout") == 0) {
- o->out = "-";
- continue;
- }
- if (strcmp(argv[i], "--out") == 0) {
- if (i + 1 >= argc) {
- fprintf(stderr, "%s: --out wants a file\n", progname);
- return -1;
- }
- o->out = argv[++i];
- continue;
- }
- if (strncmp(argv[i], "--out=", 6) == 0) {
- o->out = argv[i] + 6;
- continue;
- }
- argv[n++] = argv[i];
- }
- *consumed = argc - n;
- return 0;
-}
-
-int authz_reject_options(int argc, char **argv)
-{
- int i;
-
- for (i = 0; i < argc; i++) {
- if (argv[i][0] == '-' && argv[i][1] == '-') {
- fprintf(stderr, "%s: unknown option \"%s\" - options come before the "
- "names, and nothing here starts with a dash\n", progname, argv[i]);
- return 1;
- }
- }
- return 0;
-}
-
-// Every command ends here, so this is where the lines are released.
-
-static int finish(lines_t *l, const char *path, edit_opts o, const char *what)
-{
- int rc;
-
- if (o.out != NULL && strcmp(o.out, "-") == 0) {
- size_t i;
-
- for (i = 0; i < l->n; i++)
- fputs(l->v[i], stdout);
- fprintf(stderr, "%s: %s (to stdout; %s untouched)\n", progname, what, path);
- rc = 0;
- } else if (write_atomic(l, o.out != NULL ? o.out : path) != 0) {
- rc = 1;
- } else {
- printf("%s: %s: %s\n", progname, what, o.out != NULL ? o.out : path);
- rc = 0;
- }
- lines_free(l);
- return rc;
-}
-
-// ------------------------------------------------------------------ commands
-
-int cmd_grant(const char *path, edit_opts o, int argc, char **argv)
-{
- lines_t lines;
- char *text;
- int i;
- int first = -1;
- int replaced = -1;
- int appended = 0;
- int dropped = 0;
- int created = 0;
-
- if (argc < 3) {
- fprintf(stderr, "%s: grant wants "
- "<user> <repo> <write|force|admin> [ref ...]\n", progname);
- return 64;
- }
- if (authz_level_of(argv[2]) < 0) {
- fprintf(stderr, "%s: unknown access level \"%s\" (write force admin)\n",
- progname, argv[2]);
- return 64;
- }
- if (strcmp(argv[0], "@all") != 0 && !authz_user_pattern_ok(argv[0])) {
- fprintf(stderr, "%s: invalid user pattern \"%s\"\n", progname, argv[0]);
- return 64;
- }
- if (!authz_repo_pattern_ok(argv[1])) {
- fprintf(stderr, "%s: invalid repo pattern \"%s\" (flat names only, no slashes)\n",
- progname, argv[1]);
- return 64;
- }
- for (i = 3; i < argc; i++) {
- if (argv[i][0] == '!' || argv[i][0] == '-') {
- fprintf(stderr, "%s: ref patterns are additive, there is no deny: %s\n",
- progname, argv[i]);
- return 64;
- }
- if (!ref_field_ok(argv[i])) {
- fprintf(stderr, "%s: not a ref pattern: %s\n", progname, argv[i]);
- return 64;
- }
- }
-
- if (authz_reject_options(argc, argv) != 0)
- return 64;
- if (refuse_broken(path, &created) != 0)
- return 3;
- if (created) {
- // xstrdup'd: lines_free frees every entry it holds, and a string
- // literal is not something free() may be handed.
-
- lines_init(&lines);
- lines_push(&lines, xstrdup("# git-authz policy\n"));
- lines_push(&lines, xstrdup("# <user> <repo> <write|force|admin> "
- "[ref pattern ...] levels stack: "
- "admin > force > write\n"));
- lines_push(&lines, xstrdup("# Name the refs a rule covers: with no "
- "patterns it covers the whole repository.\n"));
- } else if (authz_read_lines(path, &lines) != 0) {
- fprintf(stderr, "%s: cannot read %s: %s\n", progname, path,
- strerror(errno));
- return 3;
- }
-
- // Several rules for one pair are legal and pointless - the strongest wins -
- // so they collapse into one line, in the place of the first.
-
- text = rule_text(argv[0], argv[1], argv[2], argc - 3, argv + 3);
- for (i = 0; i < (int)lines.n; i++) {
- if (!is_rule_for(lines.v[i], argv[0], argv[1]))
- continue;
- if (first < 0) {
- first = i; // the line the new rule takes over
- continue;
- }
- free(lines.v[i]); // a later rule for the same pair
- lines.v[i] = NULL;
- dropped++;
- }
-
- if (first < 0) {
- lines_push(&lines, text); // the buffer belongs to the file now
- replaced = (int)lines.n;
- appended = 1;
- } else {
- size_t w;
-
- free(lines.v[first]);
- lines.v[first] = text;
- for (i = 0, w = 0; i < (int)lines.n; i++) {
- if (lines.v[i] != NULL)
- lines.v[w++] = lines.v[i];
- }
- lines.n = w;
- replaced = first + 1;
- }
-
- {
- char what[512];
-
- if (created)
- snprintf(what, sizeof(what), "granted %s %s %s (new policy, line %d)",
- argv[0], argv[1], argv[2], replaced);
- else if (dropped > 0)
- snprintf(what, sizeof(what), "granted %s %s %s (line %d, %d duplicate(s) removed)",
- argv[0], argv[1], argv[2], replaced, dropped);
- else if (appended)
- snprintf(what, sizeof(what), "granted %s %s %s (appended as line %d)",
- argv[0], argv[1], argv[2], replaced);
- else
- snprintf(what, sizeof(what), "granted %s %s %s (replaced line %d)",
- argv[0], argv[1], argv[2], replaced);
- return finish(&lines, path, o, what);
- }
-}
-
-int cmd_revoke(const char *path, edit_opts o, int argc, char **argv)
-{
- lines_t lines;
- size_t w;
- int i;
- int gone = 0;
-
- if (argc != 2) {
- fprintf(stderr, "%s: revoke wants <user> <repo>\n", progname);
- return 64;
- }
- if (authz_reject_options(argc, argv) != 0)
- return 64;
- if (refuse_broken(path, NULL) != 0)
- return 3;
- if (authz_read_lines(path, &lines) != 0) {
- fprintf(stderr, "%s: no rule for %s on %s to remove\n", progname, argv[0],
- argv[1]);
- return 1;
- }
-
- for (i = 0, w = 0; i < (int)lines.n; i++) {
- if (is_rule_for(lines.v[i], argv[0], argv[1])) {
- free(lines.v[i]);
- gone++;
- continue;
- }
- lines.v[w++] = lines.v[i];
- }
- lines.n = w;
-
- if (gone == 0) {
- fprintf(stderr, "%s: no rule for %s on %s to remove\n", progname, argv[0],
- argv[1]);
- lines_free(&lines);
- return 1;
- }
- {
- char what[512];
-
- snprintf(what, sizeof(what), "revoked %s on %s (%d rule(s) removed)", argv[0],
- argv[1], gone);
- return finish(&lines, path, o, what);
- }
-}
diff --git a/src/hook.c b/src/hook.c
index ffad0bf..1dc4445 100644
--- a/src/hook.c
+++ b/src/hook.c
@@ -190,7 +190,7 @@ static char *derive_repo(hook_opts o)
return xstrdup("");
root = getenv("GIT_REPO_ROOT");
if (root == NULL || root[0] == '\0')
- root = "/var/lib/git";
+ root = DEFAULT_ROOT; // the documented home of the repositories
rootlen = strlen(root);
while (rootlen > 1 && root[rootlen - 1] == '/')
rootlen--; // "/var/lib/git/" is "/var/lib/git"
@@ -262,20 +262,17 @@ static int parse_options(int argc, char **argv, hook_opts *o)
case OPT_NOGIT: o->use_git = 0; break;
case OPT_EXPLAIN: o->explain = 1; break;
case ':':
- fprintf(stderr, "%s: --%s wants a value\n", progname,
- authz_optname(optopt, longopts));
+ authz_opt_need_value(longopts);
return -1;
default:
// getopt_long gives back no name for an option it does not know,
// but optind has already stepped past it.
- fprintf(stderr, "%s: authorize: unknown argument %s\n", progname,
- argv[optind - 1]);
+ authz_opt_unknown("authorize", argv[optind - 1]);
return -1;
}
}
if (optind < argc) {
- fprintf(stderr, "%s: authorize: unknown argument %s\n", progname,
- argv[optind]);
+ authz_opt_unknown("authorize", argv[optind]);
return -1;
}
return 0;
diff --git a/src/policy.c b/src/policy.c
index 657e0c9..b2d4348 100644
--- a/src/policy.c
+++ b/src/policy.c
@@ -44,6 +44,23 @@ char *authz_trim(char *s)
return s;
}
+// The body of a policy line: trimmed, with its comment and everything after it
+// gone. Both the parser and the editor split what is left on whitespace, and the
+// second trim is what keeps "rule # note" from leaving a trailing space where a
+// field would be.
+char *authz_line_body(char *line)
+{
+ char *hash;
+ char *text = authz_trim(line);
+
+ hash = strchr(text, '#');
+ if (hash != NULL) {
+ *hash = '\0';
+ text = authz_trim(text);
+ }
+ return text;
+}
+
int authz_split_ws(char *line, char **f, int max)
{
int n = 0;
@@ -65,22 +82,17 @@ int authz_split_ws(char *line, char **f, int max)
return n;
}
-static void report(policy_t *p, int line, const char *kind,
- const char *fmt, va_list ap)
-{
- fprintf(stderr, "%s:%d: %s: ", p->path != NULL ? p->path : "authz.conf", line, kind);
- vfprintf(stderr, fmt, ap);
- fputc('\n', stderr);
-}
-
static void perr(policy_t *p, int line, const char *fmt, ...)
{
va_list ap;
p->errors++;
+ fprintf(stderr, "%s:%d: error: ",
+ p->path != NULL ? p->path : "authz.conf", line);
va_start(ap, fmt);
- report(p, line, "error", fmt, ap);
+ vfprintf(stderr, fmt, ap);
va_end(ap);
+ fputc('\n', stderr);
}
int authz_user_pattern_ok(const char *s)
@@ -218,9 +230,31 @@ static void parse_set(policy_t *p, char *line, int lineno)
free(key);
}
+int authz_rule_validate(const char *user, const char *repo, const char *level,
+ char *const *refs, int nrefs, char *msg, size_t msgs)
+{
+ int i;
+
+ if (authz_level_of(level) < 0)
+ return snprintf(msg, msgs,
+ "unknown access level \"%s\" (write force admin)", level);
+ if (user != NULL && strcmp(user, "@all") != 0 && !authz_user_pattern_ok(user))
+ return snprintf(msg, msgs, "invalid user pattern \"%s\"", user);
+ if (repo != NULL && !authz_repo_pattern_ok(repo))
+ return snprintf(msg, msgs,
+ "invalid repo pattern \"%s\" (flat names only, no slashes)", repo);
+ for (i = 0; i < nrefs; i++) {
+ if (refs[i][0] == '!' || refs[i][0] == '-')
+ return snprintf(msg, msgs,
+ "ref patterns are additive, there is no deny: %s", refs[i]);
+ }
+ return 0;
+}
+
static void parse_rule(policy_t *p, char *line, int lineno)
{
char *fields[MAX_FIELDS];
+ char msg[AUTHZ_MSGSZ];
rule_t *r;
int n, i, level;
@@ -233,26 +267,12 @@ static void parse_rule(policy_t *p, char *line, int lineno)
perr(p, lineno, "expected \"<user> <repo> <level> [ref ...]\": %s", line);
return;
}
- level = authz_level_of(fields[2]);
- if (level < 0) {
- perr(p, lineno, "unknown access level \"%s\" (write force admin)", fields[2]);
- return;
- }
- if (strcmp(fields[0], "@all") != 0 && !authz_user_pattern_ok(fields[0])) {
- perr(p, lineno, "invalid user pattern \"%s\"", fields[0]);
- return;
- }
- if (!authz_repo_pattern_ok(fields[1])) {
- perr(p, lineno, "invalid repo pattern \"%s\" (flat names only, no slashes)",
- fields[1]);
+ if (authz_rule_validate(fields[0], fields[1], fields[2],
+ fields + 3, n - 3, msg, sizeof(msg)) != 0) {
+ perr(p, lineno, "%s", msg);
return;
}
- for (i = 3; i < n; i++) {
- if (fields[i][0] == '!' || fields[i][0] == '-') {
- perr(p, lineno, "ref patterns are additive, there is no deny: %s", fields[i]);
- return;
- }
- }
+ level = authz_level_of(fields[2]);
r = rule_slot(p);
r->user = xstrdup(fields[0]);
@@ -310,16 +330,10 @@ int policy_load(policy_t *p)
return -2;
}
for (i = 0; i < lines.n; i++) {
- char *hash;
char *text;
lineno++;
- text = authz_trim(lines.v[i]);
- hash = strchr(text, '#');
- if (hash != NULL) {
- *hash = '\0';
- text = authz_trim(text);
- }
+ text = authz_line_body(lines.v[i]);
if (*text == '\0')
continue;
@@ -369,17 +383,6 @@ int authz_user_known(const policy_t *p, const char *user)
return 0;
}
-int authz_repo_reachable(const policy_t *p, const char *repo)
-{
- size_t i;
-
- for (i = 0; i < p->nrules; i++) {
- if (repo_match(p->rules[i].repo, repo))
- return 1;
- }
- return 0;
-}
-
static int ordinary_ref(const char *ref)
{
return strncmp(ref, "refs/heads/", 11) == 0 || strncmp(ref, "refs/tags/", 10) == 0;
@@ -412,11 +415,27 @@ typedef struct {
} scope_t;
+// What this rule says about this ref, whatever its level: an unscoped rule
+// covers the whole repository, a scoped one only what its patterns name.
+static level_t rule_covers(const rule_t *r, const char *ref)
+{
+ size_t j;
+
+ if (ref == NULL)
+ return LV_NONE;
+ if (r->nrefs == 0)
+ return r->level;
+ for (j = 0; j < r->nrefs; j++)
+ if (glob_match(r->refs[j], ref))
+ return r->level;
+ return LV_NONE;
+}
+
static void scope(const policy_t *p, const char *user, const char *repo,
level_t min, const char *ref, scope_t *s)
{
size_t i;
- size_t j;
+ level_t covers;
s->unscoped = 0;
s->scoped = 0;
@@ -434,24 +453,13 @@ 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 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;
-
- if (p->rules[i].nrefs == 0)
- covers = p->rules[i].level;
- for (j = 0; j < p->rules[i].nrefs; j++) {
- if (glob_match(p->rules[i].refs[j], ref)) {
- covers = p->rules[i].level;
- break;
- }
- }
- if (covers > s->here)
- s->here = covers;
- }
+ // A refusal that names a level held elsewhere sends somebody to open
+ // the file and stare at it, so what the rule says about this ref is
+ // kept whatever the rule's level is.
+
+ covers = rule_covers(&p->rules[i], ref);
+ if (covers > s->here)
+ s->here = covers;
if (p->rules[i].level < min)
continue;
@@ -460,12 +468,8 @@ static void scope(const policy_t *p, const char *user, const char *repo,
continue;
}
s->scoped = 1;
- for (j = 0; ref != NULL && j < p->rules[i].nrefs; j++) {
- if (glob_match(p->rules[i].refs[j], ref)) {
- s->hit = 1;
- break;
- }
- }
+ if (covers != LV_NONE)
+ s->hit = 1;
}
}
@@ -474,32 +478,34 @@ void authz_check(const policy_t *p, const char *user, const char *repo,
{
scope_t s;
char raised[64];
- char *nref = NULL;
- level_t want = need;
+ char tail[96];
+ char *nref;
+ char *r;
+ level_t want;
out->allowed = 0;
out->need = need;
out->why[0] = '\0';
+ tail[0] = '\0';
if (user == NULL || user[0] == '\0') {
- out->need = need;
snprintf(out->why, sizeof(out->why), "no identity was presented");
return;
}
- if (ref == NULL) {
- // 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.
+ r = authz_norm_repo(repo);
- char *r = authz_norm_repo(repo);
+ // 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.
+ if (ref == NULL) {
scope(p, user, repo, need, NULL, &s);
if (s.unscoped || s.scoped) {
+ out->allowed = 1;
snprintf(out->why, sizeof(out->why), "%s has %s on %s of %s",
user, authz_level_name(need),
s.unscoped ? "all" : "some refs", r);
- out->allowed = 1;
} else {
snprintf(out->why, sizeof(out->why), "%s has no %s on %s",
user, authz_level_name(need), r);
@@ -511,12 +517,13 @@ void authz_check(const policy_t *p, const char *user, const char *repo,
nref = authz_norm_ref(ref);
want = authz_need_for(nref, need, raised, sizeof(raised));
out->need = want;
+ if (raised[0] != '\0')
+ snprintf(tail, sizeof(tail), " (%s)", raised);
scope(p, user, repo, want, nref, &s);
if (s.unscoped || s.hit) {
- char *r = authz_norm_repo(repo);
-
+ out->allowed = 1;
if (raised[0] != '\0')
snprintf(out->why, sizeof(out->why),
"%s has %s on %s %s (this ref costs %s, not %s: %s)",
@@ -525,35 +532,19 @@ void authz_check(const policy_t *p, const char *user, const char *repo,
else
snprintf(out->why, sizeof(out->why), "%s has %s on %s %s",
user, authz_level_name(want), r, nref);
- free(r);
- out->allowed = 1;
- free(nref);
- return;
- }
-
- {
- char *r = authz_norm_repo(repo);
-
- if (!s.any)
- snprintf(out->why, sizeof(out->why), "%s has no rule for %s; %s needs %s",
- user, r, nref, authz_level_name(want));
- else if (s.here == LV_NONE)
- snprintf(out->why, sizeof(out->why),
- "%s holds %s on %s, but not on %s; that ref needs %s%s%s%s",
- user, authz_level_name(s.strongest), r, nref,
- authz_level_name(want),
- raised[0] != '\0' ? " (" : "",
- raised[0] != '\0' ? raised : "",
- raised[0] != '\0' ? ")" : "");
- else if (raised[0] != '\0')
- snprintf(out->why, sizeof(out->why),
- "%s has %s on %s %s; it needs %s (%s)",
- user, authz_level_name(s.here), r, nref,
- authz_level_name(want), raised);
- else
- snprintf(out->why, sizeof(out->why), "%s has %s on %s %s; it needs %s",
- user, authz_level_name(s.here), r, nref, authz_level_name(want));
- free(r);
+ } else if (!s.any) {
+ snprintf(out->why, sizeof(out->why), "%s has no rule for %s; %s needs %s",
+ user, r, nref, authz_level_name(want));
+ } else if (s.here == LV_NONE) {
+ snprintf(out->why, sizeof(out->why),
+ "%s holds %s on %s, but not on %s; that ref needs %s%s",
+ user, authz_level_name(s.strongest), r, nref,
+ authz_level_name(want), tail);
+ } else {
+ snprintf(out->why, sizeof(out->why), "%s has %s on %s %s; it needs %s%s",
+ user, authz_level_name(s.here), r, nref,
+ authz_level_name(want), tail);
}
free(nref);
+ free(r);
}
diff --git a/src/util.c b/src/util.c
index f0991fe..1cebd39 100644
--- a/src/util.c
+++ b/src/util.c
@@ -136,6 +136,19 @@ void authz_opt_reset(void)
opterr = 0;
}
+// The two complaints every getopt_long loop otherwise writes by hand. Neither
+// returns anything: the caller knows whether its own failure is 64 or -1.
+
+void authz_opt_need_value(const struct option *lo)
+{
+ fprintf(stderr, "%s: --%s wants a value\n", progname, authz_optname(optopt, lo));
+}
+
+void authz_opt_unknown(const char *cmd, const char *arg)
+{
+ fprintf(stderr, "%s: %s: unknown argument %s\n", progname, cmd, arg);
+}
+
// ----------------------------------------------------------------- lines
void lines_init(lines_t *l)
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);