summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile4
-rw-r--r--README54
-rw-r--r--cmd/main.c108
-rw-r--r--container/Dockerfile2
-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
-rw-r--r--test/check.c13
-rwxr-xr-xtest/run.sh311
12 files changed, 186 insertions, 1442 deletions
diff --git a/Makefile b/Makefile
index 11746cc..8b108d8 100644
--- a/Makefile
+++ b/Makefile
@@ -55,9 +55,7 @@ test: $(PROG) $(TST_RUN)
# not need the memory back, but it does need that report to stay empty, or the
# one real leak hides among forty.
.PHONY: sanitizer
-sanitizer: CFLAGS := -std=c99 -Wall -Wextra -Wpedantic -I$(SRC_DIR) \
- -D_POSIX_C_SOURCE=200809L -O1 -g -fsanitize=address,undefined \
- -fno-omit-frame-pointer -MMD -MP
+sanitizer: CFLAGS += -O1 -fsanitize=address,undefined -fno-omit-frame-pointer
sanitizer: LDFLAGS := -fsanitize=address,undefined
sanitizer: test
diff --git a/README b/README
index b2bb0a0..1f6eb30 100644
--- a/README
+++ b/README
@@ -21,12 +21,9 @@ Commands
git-authz [--policy PATH] check <user> <repo> <write|force|admin> [ref]
git-authz [--policy PATH] authorize [options]
git-authz [--policy PATH] validate [options]
- git-authz [--policy PATH] grant <user> <repo> <level> [ref ...]
- git-authz [--policy PATH] revoke <user> <repo>
`check` answers one question and is for humans and scripts. `authorize` is what
-git invokes. `validate` asks whether the host is wired to run the policy at all.
-The last two edit the file.
+git invokes. `validate` asks whether the policy is one the hook can use.
Exit status is the interface, and it does not change:
@@ -159,41 +156,12 @@ Wiring
git-authz validate || exit 1
-It reports a policy that does not parse, a policy no user can push to any
-repository with, a repository nobody can push to, a missing or non-executable
-hook, a `core.hooksPath` pointing somewhere else, `PermitUserEnvironment no`, no
-`ForceCommand`, a `git` account with a real shell, key identities with no rule,
-and htpasswd users with no rule. Warnings do not stop it; errors do, and
-`--strict` makes warnings stop it too. `--no-host` checks the policy without
-looking at the machine, which is what you want from CI on a checkout.
-
-The point of that last set of checks is the failure mode where the policy is
-fine, the server is up, and every push is anonymous - because ssh was not told to
-accept environment variables, or the hook path moved, and nothing said so.
-
-
-Editing the file
-----------------
-
- git-authz grant bob team write refs/heads/feature/*
- git-authz revoke bob team
-
-Each writes the file atomically - a temporary in the same directory, then a
-rename, keeping the mode and owner - and preserves everything else in the file,
-comments and blank lines included, so a grant does not undo the ordering an
-operator laid out. `grant` collapses several rules for the same user and
-repository into one line, in the place of the first, because the strongest wins
-and a second rule that does nothing is a trap for the next reader.
-
-A file that does not exist yet is started, with the grammar written at the top of
-it; a file that does not parse is refused, because editing something the parser
-cannot read would rewrite the parts that parse and keep the damage.
-
-The deployed file is a read-only mount - a ConfigMap - by design: the hook should
-not be able to change who can push, and neither should whatever else runs as uid
-1000. These commands are for the copy in git. Pointed at a read-only file they
-fail with that explanation rather than writing somewhere surprising, and `--out`
-or `--stdout` writes the result elsewhere without touching the input.
+It reports a policy that does not parse, with the line numbers the parser already
+printed, and a policy that cannot be read at all. Either one denies every push,
+which is why both are errors and not warnings: nothing is allowed on a policy
+the hook cannot read. It warns when a rule gives `@all` the whole of a
+repository, and when the file is not readable by the account the hook runs as.
+Warnings do not stop it; errors do, and `--strict` makes warnings stop it too.
Reading this as an operator
@@ -231,11 +199,10 @@ Layout
ci/ the pipeline
src/glob.c the pattern matching, such as it is
- src/util.c allocation, string buffers, writing a file atomically
+ src/util.c allocation, string buffers, reading a file
src/policy.c reading the file, and answering from it
src/hook.c the mode git calls, and the only one a push depends on
- src/validate.c does this policy, and the host around it, add up
- src/edit.c grant and revoke, without losing the file
+ src/validate.c does this policy add up
Everything compiles into `obj/`, test runners included, so a build leaves the
tree as it found it apart from the binary. `git-authz` is one program rather
@@ -254,8 +221,7 @@ ladder, `@all`, scope, the sentences - and are where the semantics are pinned.
the environment, `old new ref` lines on stdin, the repository as the working
directory, the hook reached through its own name. It uses no git at all, so it
runs anywhere a compiler does, and it tests the real binary rather than a model
-of it: argument parsing, exit codes, messages, and the editing commands writing
-files.
+of it: argument parsing, exit codes, and messages.
Two facts do not come out of the line git passes, and the hook asks git for
them: which branch `HEAD` is, and whether `old` is an ancestor of `new`. Those
diff --git a/cmd/main.c b/cmd/main.c
index d667922..cb71a4f 100644
--- a/cmd/main.c
+++ b/cmd/main.c
@@ -11,13 +11,6 @@
#define POLICY_ENV "GIT_HOST_AUTHZ"
#define DEFAULT_POLICY "/etc/git-host/authz.conf"
-#define DEFAULT_ROOT "/var/lib/git"
-#define DEFAULT_HOOKS "/usr/local/share/git-host/hooks"
-#define DEFAULT_GITCONFIG "/etc/gitconfig"
-#define DEFAULT_SSHD "/etc/ssh/sshd_config"
-#define DEFAULT_PASSWD "/etc/passwd"
-#define DEFAULT_KEYS "/run/git-host/secrets/authorized_keys"
-#define DEFAULT_HTPASSWD "/etc/git-host/secrets/htpasswd"
enum { OPT_POLICY = 128, OPT_HELP };
@@ -40,32 +33,16 @@ static void usage(FILE *f)
" --no-git ask git nothing (an unknown HEAD is not guessed)\n"
" --explain report the allows too\n"
"\n"
-" validate is the policy parseable, and is the host wired to run it?\n"
-" --no-host only the policy, none of the wiring below\n"
-" --root DIR repositories (default %s)\n"
-" --hook-dir DIR hooks (default %s)\n"
-" --gitconfig FILE (default %s)\n"
-" --sshd-config FILE (default %s)\n"
-" --passwd FILE (default %s)\n"
-" --keys FILE (default %s)\n"
-" --htpasswd FILE (default %s)\n"
+" validate is the policy parseable, and is it sane?\n"
" --strict warnings are failures too\n"
"\n"
-" grant <user> <repo> <level> [ref ...] add one rule, or replace that pair's\n"
-" revoke <user> <repo> take it away again\n"
-" --out FILE | --stdout write elsewhere instead of in place\n"
-" The deployed policy is a read-only mount on purpose; these are for the\n"
-" source copy, and they say so when they are pointed at a read-only file.\n"
-"\n"
"Levels are monotone: admin implies force implies write. '@all' means everyone\n"
"the policy already names. \"main\", \"heads/main\" and \"refs/heads/main\" are the\n"
"same branch; a bare name is always a branch and never a tag. A rule says what\n"
"it says: nothing else in the file can raise the price of a ref.\n"
"\n"
"Exit status: 0 allowed, 1 denied, 3 the policy could not be read or does not\n"
-"parse - nothing is allowed on a policy that cannot be read - and 64 for usage.\n",
-DEFAULT_ROOT, DEFAULT_HOOKS, DEFAULT_GITCONFIG, DEFAULT_SSHD, DEFAULT_PASSWD,
-DEFAULT_KEYS, DEFAULT_HTPASSWD);
+"parse - nothing is allowed on a policy that cannot be read - and 64 for usage.\n");
}
// 3 and not 1: a push refused because the host is broken has to sound different
@@ -83,36 +60,13 @@ static int deny_policy(const char *path, int rc)
// forty leaks and stops showing the one that matters.
enum {
- OPT_ROOT = 128, OPT_HOOK_DIR, OPT_GITCONFIG, OPT_SSHD_CONFIG,
- OPT_PASSWD, OPT_KEYS, OPT_HTPASSWD, OPT_STRICT, OPT_NO_HOST
+ OPT_STRICT = 128
};
-// A host without a git account yet - a container being built - still wants its
-// policy read, so --no-host switches the wiring checks off wholesale.
-
-static void no_host(validate_opts *vo)
-{
- vo->root = NULL;
- vo->hook_dir = NULL;
- vo->gitconfig = NULL;
- vo->sshd_config = NULL;
- vo->passwd = NULL;
- vo->keys = NULL;
- vo->htpasswd = NULL;
-}
-
static int parse_validate(int argc, char **argv, validate_opts *vo)
{
static const struct option longopts[] = {
- { "root", required_argument, NULL, OPT_ROOT },
- { "hook-dir", required_argument, NULL, OPT_HOOK_DIR },
- { "gitconfig", required_argument, NULL, OPT_GITCONFIG },
- { "sshd-config", required_argument, NULL, OPT_SSHD_CONFIG },
- { "passwd", required_argument, NULL, OPT_PASSWD },
- { "keys", required_argument, NULL, OPT_KEYS },
- { "htpasswd", required_argument, NULL, OPT_HTPASSWD },
- { "strict", no_argument, NULL, OPT_STRICT },
- { "no-host", no_argument, NULL, OPT_NO_HOST },
+ { "strict", no_argument, NULL, OPT_STRICT },
{ NULL, 0, NULL, 0 }
};
int c;
@@ -120,28 +74,17 @@ static int parse_validate(int argc, char **argv, validate_opts *vo)
authz_opt_reset();
while ((c = getopt_long(argc, argv, "+:", longopts, NULL)) != -1) {
switch (c) {
- case OPT_ROOT: vo->root = optarg; break;
- case OPT_HOOK_DIR: vo->hook_dir = optarg; break;
- case OPT_GITCONFIG: vo->gitconfig = optarg; break;
- case OPT_SSHD_CONFIG: vo->sshd_config = optarg; break;
- case OPT_PASSWD: vo->passwd = optarg; break;
- case OPT_KEYS: vo->keys = optarg; break;
- case OPT_HTPASSWD: vo->htpasswd = optarg; break;
- case OPT_STRICT: vo->strict = 1; break;
- case OPT_NO_HOST: no_host(vo); break;
+ case OPT_STRICT: vo->strict = 1; break;
case ':':
- fprintf(stderr, "%s: --%s wants a value\n", progname,
- authz_optname(optopt, longopts));
+ authz_opt_need_value(longopts);
return -1;
default:
- fprintf(stderr, "%s: validate: unknown argument %s\n", progname,
- argv[optind - 1]);
+ authz_opt_unknown("validate", argv[optind - 1]);
return -1;
}
}
if (optind < argc) {
- fprintf(stderr, "%s: validate: unknown argument %s\n", progname,
- argv[optind]);
+ authz_opt_unknown("validate", argv[optind]);
return -1;
}
return 0;
@@ -170,6 +113,7 @@ static int dispatch(policy_t *p, int rc, const char *cmd, int argc, char **argv,
if (strcmp(cmd, "check") == 0) {
decision_t d;
+ char msg[AUTHZ_MSGSZ];
int level;
argc--;
@@ -178,12 +122,14 @@ static int dispatch(policy_t *p, int rc, const char *cmd, int argc, char **argv,
fprintf(stderr, "%s: check wants <user> <repo> <level> [ref]\n", progname);
return 64;
}
- level = authz_level_of(argv[2]);
- if (level < 0) {
- fprintf(stderr, "%s: unknown access level \"%s\" (write force admin)\n",
- progname, argv[2]);
+ // Only the level: check answers a question about names as written, so a
+ // user or repository it would not recognise is an answer, not a usage error.
+ if (authz_rule_validate(NULL, NULL, argv[2], NULL, 0,
+ msg, sizeof(msg)) != 0) {
+ fprintf(stderr, "%s: %s\n", progname, msg);
return 64;
}
+ level = authz_level_of(argv[2]);
if (rc != 0)
return deny_policy(path, rc);
authz_check(p, argv[0], argv[1], (level_t)level, argc > 3 ? argv[3] : NULL, &d);
@@ -209,7 +155,6 @@ int main(int argc, char **argv)
policy_t p;
hook_opts ho;
validate_opts vo;
- edit_opts eo;
int c;
int rc;
int status;
@@ -224,14 +169,6 @@ int main(int argc, char **argv)
memset(&ho, 0, sizeof(ho));
ho.use_git = 1;
memset(&vo, 0, sizeof(vo));
- vo.root = DEFAULT_ROOT;
- vo.hook_dir = DEFAULT_HOOKS;
- vo.gitconfig = DEFAULT_GITCONFIG;
- vo.sshd_config = DEFAULT_SSHD;
- vo.passwd = DEFAULT_PASSWD;
- vo.keys = DEFAULT_KEYS;
- vo.htpasswd = DEFAULT_HTPASSWD;
- memset(&eo, 0, sizeof(eo));
// Reached through the hook symlink: git's arguments are not this program's,
// so they are dropped, and the policy comes from the environment. The one
@@ -265,8 +202,7 @@ int main(int argc, char **argv)
usage(stdout);
return 0;
case ':':
- fprintf(stderr, "%s: --%s wants a value\n", progname,
- authz_optname(optopt, longopts));
+ authz_opt_need_value(longopts);
return 64;
default:
fprintf(stderr, "%s: unknown option before the command: %s\n",
@@ -294,18 +230,6 @@ int main(int argc, char **argv)
return 0;
}
- if (strcmp(cmd, "grant") == 0 || strcmp(cmd, "revoke") == 0) {
- int consumed = 0;
- int n = argc - 1;
-
- if (authz_parse_out(n, argv + 1, &eo, &consumed) != 0)
- return 64;
- n -= consumed; // what is left was compacted to the front
- if (strcmp(cmd, "grant") == 0)
- return cmd_grant(path, eo, n, argv + 1);
- return cmd_revoke(path, eo, n, argv + 1);
- }
-
policy_init(&p, path);
rc = policy_load(&p);
status = dispatch(&p, rc, cmd, argc, argv, path, ho, vo);
diff --git a/container/Dockerfile b/container/Dockerfile
index ac0d898..53b153e 100644
--- a/container/Dockerfile
+++ b/container/Dockerfile
@@ -78,7 +78,7 @@ RUN chmod 0755 \
&& git config --file /etc/gitconfig core.hooksPath /usr/local/share/git-host/hooks \
&& grep -q "^git:" /etc/passwd \
&& lighttpd -tt -f /etc/lighttpd/lighttpd.conf \
- && /usr/local/bin/git-authz validate --no-host
+ && /usr/local/bin/git-authz validate
EXPOSE 8080 2222
VOLUME ["/var/lib/git"]
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);
diff --git a/test/check.c b/test/check.c
index 4db4b9e..4b9a093 100644
--- a/test/check.c
+++ b/test/check.c
@@ -142,19 +142,6 @@ static void test_scope_edges(void)
expect_deny(&p, "bob", "web", LV_WRITE, "refs/heads/main", "but not on");
expect_deny(&p, "bob", "api", LV_WRITE, "refs/heads/dev", "no rule");
policy_free(&p);
-
- // reachable-by-nobody detection, which validate reports
- rc = load_text(&p, "bob team write\n");
- CHECK_INT(rc, 0);
- CHECK(authz_repo_reachable(&p, "team"));
- CHECK(authz_repo_reachable(&p, "team.git"));
- CHECK(!authz_repo_reachable(&p, "other"));
- policy_free(&p);
-
- rc = load_text(&p, "bob * write\n");
- CHECK_INT(rc, 0);
- CHECK(authz_repo_reachable(&p, "anything"));
- policy_free(&p);
}
int main(void)
diff --git a/test/run.sh b/test/run.sh
index a608468..d3268b0 100755
--- a/test/run.sh
+++ b/test/run.sh
@@ -154,7 +154,7 @@ alice * admin
bob team write refs/heads/feature/*
EOF
-out=$("$AUTHZ" --policy "$P" validate --no-host 2>&1)
+out=$("$AUTHZ" --policy "$P" validate 2>&1)
rc=$?
expect "the fixture policy is sound" 0 "is in force" "$rc" "$out"
@@ -546,323 +546,26 @@ chmod 644 "$TMP/secret.conf"
section "validate: the policy alone"
POLICY=$P
-out=$("$AUTHZ" --policy "$P" validate --no-host 2>&1); rc=$?
+out=$("$AUTHZ" --policy "$P" validate 2>&1); rc=$?
expect "the fixture passes" 0 "is in force" "$rc" "$out"
expect_not "with nothing to complain about" 0 "warn " "$rc" "$out"
-out=$("$AUTHZ" --policy "$TMP/broken.conf" validate --no-host 2>&1); rc=$?
+out=$("$AUTHZ" --policy "$TMP/broken.conf" validate 2>&1); rc=$?
expect "a broken policy is reported with its line number" 3 ":2: error" "$rc" "$out"
cat >"$TMP/noise.conf" <<'EOF'
@all web write
bob missing write refs/heads/*
EOF
-out=$("$AUTHZ" --policy "$TMP/noise.conf" validate --no-host 2>&1); rc=$?
+out=$("$AUTHZ" --policy "$TMP/noise.conf" validate 2>&1); rc=$?
expect "warnings do not fail a validate" 0 "warning(s)" "$rc" "$out"
contains "$out" "@all the whole of web" || bad "an unscoped @all should be warned about"
-out=$("$AUTHZ" --policy "$TMP/noise.conf" validate --no-host --strict 2>&1); rc=$?
+out=$("$AUTHZ" --policy "$TMP/noise.conf" validate --strict 2>&1); rc=$?
expect "--strict makes warnings fatal" 1 "warning" "$rc" "$out"
-out=$("$AUTHZ" --policy "$TMP/nope.conf" validate --no-host 2>&1); rc=$?
+out=$("$AUTHZ" --policy "$TMP/nope.conf" validate 2>&1); rc=$?
expect "a missing policy is not a valid policy" 3 "cannot be read" "$rc" "$out"
-section "validate: the host around it"
-
-mkhost() {
- H=$TMP/host
- rm -rf "$H"
- mkdir -p "$H/hooks" "$H/repos/team.git" "$H/repos/secrets.git" "$H/secrets"
- ln -sf "$AUTHZ_ABS" "$H/hooks/pre-receive" 2>/dev/null || cp "$AUTHZ" "$H/hooks/pre-receive"
- printf '[core]\n\thooksPath = %s/hooks\n' "$H" >"$H/gitconfig"
- printf 'PermitUserEnvironment yes\nForceCommand /usr/bin/git-shell\nAuthorizedKeysFile %s/secrets/authorized_keys\n' "$H" >"$H/sshd_config"
- printf 'git:x:1000:1000::/var/lib/git:/usr/bin/git-shell\n' >"$H/passwd"
- printf 'ssh-ed25519 AAAA bob\n' >"$H/secrets/authorized_keys"
- printf 'environment="GIT_HOST_USER=bob" ssh-ed25519 BBBB\n' >>"$H/secrets/authorized_keys"
- printf 'bob:$2y$05$hash\n' >"$H/secrets/htpasswd"
- cat >"$H/authz.conf" <<'POL'
-alice * admin
-bob team write refs/heads/feature/*
-POL
-}
-
-host_validate() {
- "$AUTHZ" --policy "$H/authz.conf" validate \
- --root "$H/repos" --hook-dir "$H/hooks" --gitconfig "$H/gitconfig" \
- --sshd-config "$H/sshd_config" --passwd "$H/passwd" \
- --keys "$H/secrets/authorized_keys" --htpasswd "$H/secrets/htpasswd" "$@" 2>&1
-}
-
-mkhost
-out=$(host_validate); rc=$?
-expect "a wired-up host passes" 0 "is in force" "$rc" "$out"
-expect "and says what it checked" 0 "ForceCommand" "$rc" "$out"
-
-sed "s|hooksPath = .*|hooksPath = /somewhere/else|" "$H/gitconfig" >"$H/gitconfig2"
-mv "$H/gitconfig2" "$H/gitconfig"
-out=$(host_validate); rc=$?
-expect "the wrong core.hooksPath is an error" 1 "checked by something else" "$rc" "$out"
-
-sed "s|hooksPath|hooks-path|" "$H/gitconfig" >"$H/gitconfig2"
-mv "$H/gitconfig2" "$H/gitconfig"
-out=$(host_validate); rc=$?
-expect "no core.hooksPath at all is an error" 1 "does not set core.hooksPath" "$rc" "$out"
-
-mkhost
-mv "$H/hooks/pre-receive" "$H/hooks/away"
-out=$(host_validate); rc=$?
-expect "a missing hook is an error" 1 "every push would be accepted unchecked" "$rc" "$out"
-mv "$H/hooks/away" "$H/hooks/pre-receive"
-
-mkhost
-sed "s/PermitUserEnvironment yes/PermitUserEnvironment no/" "$H/sshd_config" >"$H/s2"
-mv "$H/s2" "$H/sshd_config"
-out=$(host_validate); rc=$?
-expect "PermitUserEnvironment no means anonymous pushes" 1 "anonymous" "$rc" "$out"
-
-mkhost
-sed "s|^ForceCommand.*|# no force command|" "$H/sshd_config" >"$H/s2"
-mv "$H/s2" "$H/sshd_config"
-out=$(host_validate); rc=$?
-expect "no ForceCommand is an error" 1 "interactive shell" "$rc" "$out"
-
-mkhost
-sed "s|git-shell|/bin/sh|" "$H/passwd" >"$H/p2"
-mv "$H/p2" "$H/passwd"
-out=$(host_validate); rc=$?
-expect "a git account with a real shell is an error" 1 "not git-shell" "$rc" "$out"
-
-mkhost
-printf 'environment="GIT_HOST_USER=ghost" ssh-ed25519 CCCC\n' >"$H/secrets/authorized_keys"
-out=$(host_validate); rc=$?
-expect "a key identity with no rule is a warning" 0 "ghost" "$rc" "$out"
-out=$(host_validate --strict); rc=$?
-expect "and fatal under --strict" 1 "ghost" "$rc" "$out"
-
-mkhost
-printf 'ssh-ed25519 AAAAanonymouskey\n' >>"$H/secrets/authorized_keys"
-out=$(host_validate); rc=$?
-expect "an identity-less key is a warning" 0 "no GIT_HOST_USER" "$rc" "$out"
-
-mkhost
-printf 'mallory:$2y$05$hash\n' >>"$H/secrets/htpasswd"
-out=$(host_validate); rc=$?
-expect "an htpasswd user with no rule is a warning" 0 "change nothing" "$rc" "$out"
-
-mkhost
-printf 'bob team write\n' >"$H/authz.conf"
-mkdir -p "$H/repos/orphan.git"
-out=$(host_validate); rc=$?
-expect "a repository nobody can push to is a warning" 0 "orphan" "$rc" "$out"
-
-mkhost
-printf 'bob nowhere write\n' >>"$H/authz.conf"
-out=$(host_validate); rc=$?
-expect "a rule on a repository that is not there is a warning" 0 "nowhere" "$rc" "$out"
-
-mkhost
-printf 'alice web admin\nbob nowhere write refs/heads/*\n' >"$H/authz.conf"
-printf 'mallory:$2y$05$x\n' >"$H/secrets/htpasswd"
-mkdir -p "$H/repos/orphan.git"
-out=$(host_validate); rc=$?
-if contains "$out" "orphan" && contains "$out" "mallory" &&
- contains "$out" "nowhere" && contains "$out" "web"; then
- ok
-else
- bad "several warnings at once: said $out"
-fi
-expect "warnings alone still let the host serve" 0 "is in force" "$rc" "$out"
-
-# sshd reads a directive once and keeps the first answer it got, and "none" is
-# how a Match block turns a ForceCommand back off - both of those are the same
-# wide-open state as having no ForceCommand at all, so validate has to see them
-# that way too.
-
-mkhost
-printf 'PermitUserEnvironment yes\nForceCommand none\n' >"$H/sshd_config"
-out=$(host_validate); rc=$?
-expect "ForceCommand none is as open as no ForceCommand" 1 "interactive shell" "$rc" "$out"
-
-mkhost
-printf 'PermitUserEnvironment no\nPermitUserEnvironment yes\n' >"$H/sshd_config"
-out=$(host_validate); rc=$?
-expect "sshd keeps the first PermitUserEnvironment, not the last" 1 "anonymous" "$rc" "$out"
-
-mkhost
-printf 'PermitUserEnvironment yes\nForceCommand /usr/bin/git-shell\nForceCommand none\n' >"$H/sshd_config"
-out=$(host_validate); rc=$?
-expect "and the first ForceCommand is the one that counts" 0 "ForceCommand /usr/bin/git-shell" "$rc" "$out"
-
-# git spells section names case-insensitively, so [CORE] sets core.hooksPath just
-# as [core] does; refusing to see it reports a broken host that is not broken.
-mkhost
-printf '[CORE]\n\thooksPath = %s/hooks\n' "$H" >"$H/gitconfig"
-out=$(host_validate); rc=$?
-expect "a gitconfig section name is case-insensitive, like git's own" 0 "core.hooksPath" "$rc" "$out"
-
-# -------------------------------------------------------------- edit (grant)
-
-section "grant and revoke"
-
-cat >"$TMP/edit.conf" <<'EOF'
-# policy, kept in git and rolled out as a ConfigMap
-alice * admin # break glass
-bob team write refs/heads/feature/*
-EOF
-
-out=$("$AUTHZ" --policy "$TMP/edit.conf" grant carol team force refs/heads/feature/* 2>&1); rc=$?
-expect "grant appends" 0 "appended" "$rc" "$out"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" check carol team force refs/heads/feature/x 2>&1); rc=$?
-expect "and the grant is in force immediately" 0 "allow" "$rc" "$out"
-if grep -q '^# policy, kept in git' "$TMP/edit.conf" &&
- grep -q 'break glass' "$TMP/edit.conf"; then
- ok
-else
- bad "the comments survived neither"
-fi
-
-out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob team admin 2>&1); rc=$?
-expect "grant replaces the pair's rule" 0 "replaced line 3" "$rc" "$out"
-n=$(grep -c '^bob' "$TMP/edit.conf")
-[ "$n" = 1 ] && ok || bad "bob's rule is not collapsed into one line ($n lines)"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" check bob team admin refs/heads/main 2>&1); rc=$?
-expect "and the new level applies" 0 "allow" "$rc" "$out"
-
-out=$("$AUTHZ" --policy "$TMP/edit.conf" revoke bob team 2>&1); rc=$?
-expect "revoke removes the rule" 0 "revoked bob" "$rc" "$out"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" check bob team admin refs/heads/dev 2>&1); rc=$?
-expect "and the access is gone" 1 "no rule" "$rc" "$out"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" revoke bob team 2>&1); rc=$?
-expect "revoking nothing reports nothing" 1 "no rule for bob" "$rc" "$out"
-
-out=$("$AUTHZ" --policy "$TMP/edit.conf" grant frank team write refs/heads/main refs/tags/release/* 2>&1); rc=$?
-expect "grant can name several refs" 0 "granted frank" "$rc" "$out"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" check frank team write refs/heads/main 2>&1); rc=$?
-expect "and the list is in force" 0 "allow" "$rc" "$out"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" check frank team write refs/heads/dev 2>&1); rc=$?
-expect "and covers nothing else of his own" 1 "but not on refs/heads/dev" "$rc" "$out"
-if grep -q '^frank team write refs/heads/main refs/tags/release/\*$' "$TMP/edit.conf"; then
- ok
-else
- bad "the refs did not land on one line as written"
-fi
-
-out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob team nope 2>&1); rc=$?
-expect "grant rejects a level that is not one" 64 "unknown access level" "$rc" "$out"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob group/team write 2>&1); rc=$?
-expect "grant rejects a repository name with a slash in it" 64 "invalid repo pattern" "$rc" "$out"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob team write '!refs/heads/main' 2>&1); rc=$?
-expect "grant has no deny" 64 "no deny" "$rc" "$out"
-
-# A ref argument has to be one field. The line it lands on is split on whitespace
-# again when the file is read, and '#' starts a comment, so writing an argument
-# verbatim would either grant more than was asked for or leave a file that no
-# longer parses - and a policy that does not parse denies every push there is.
-cp "$TMP/edit.conf" "$TMP/edit.conf.before"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob team write 'refs/heads/a b' 2>&1); rc=$?
-expect "a ref argument with a space in it is refused" 64 "not a ref pattern" "$rc" "$out"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob team write 'refs/heads/a#b' 2>&1); rc=$?
-expect "a ref argument with a comment marker is refused" 64 "not a ref pattern" "$rc" "$out"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob team write "$(printf 'refs/heads/a\nb')" 2>&1); rc=$?
-expect "a ref argument with a newline in it is refused" 64 "not a ref pattern" "$rc" "$out"
-cmp -s "$TMP/edit.conf" "$TMP/edit.conf.before" ||
- bad "a refused grant still changed the file"
-
-# The pair's rule being the last line of the file is still a replacement: nothing
-# was appended, and the message says which line was taken over.
-cat >"$TMP/lastline.conf" <<'EOF'
-alice * admin
-bob team write refs/heads/feature/*
-EOF
-out=$("$AUTHZ" --policy "$TMP/lastline.conf" grant bob team admin 2>&1); rc=$?
-expect "replacing the last line says replaced, not appended" 0 "replaced line 2" "$rc" "$out"
-out=$("$AUTHZ" --policy "$TMP/lastline.conf" grant zed team admin 2>&1); rc=$?
-expect "and a rule with no pair yet is still an append" 0 "appended as line 3" "$rc" "$out"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" protect refs/heads/main 2>&1); rc=$?
-expect "protect is gone, and says it is unknown" 64 "unknown command" "$rc" "$out"
-printf 'set protected-refs refs/heads/main\n' >>"$TMP/edit.conf"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" check alice team admin refs/heads/main 2>&1); rc=$?
-expect "a policy that still carries protected-refs is refused, not ignored" 3 "no longer exists" "$rc" "$out"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" validate --no-host 2>&1); rc=$?
-expect "and validate says every push is denied" 3 "not used" "$rc" "$out"
-grep -v 'protected-refs' "$TMP/edit.conf" > "$TMP/edit.conf.clean" &&
- cp "$TMP/edit.conf.clean" "$TMP/edit.conf"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" validate --no-host 2>&1); rc=$?
-expect "cleaned, it validates again" 0 "is in force" "$rc" "$out"
-
-cp "$TMP/edit.conf" "$TMP/edit.conf.orig"
-out=$("$AUTHZ" --policy "$TMP/broken.conf" grant bob team write 2>&1); rc=$?
-expect "editing a policy that does not parse is refused" 3 "does not parse" "$rc" "$out"
-cmp -s "$TMP/edit.conf" "$TMP/edit.conf.orig" || bad "a refused edit still changed the file"
-
-section "edit: where the result goes"
-
-out=$("$AUTHZ" --policy "$TMP/edit.conf" grant erin secrets admin --out "$TMP/other.conf" 2>&1); rc=$?
-expect "--out writes elsewhere" 0 "$TMP/other.conf" "$rc" "$out"
-out=$("$AUTHZ" --policy "$TMP/other.conf" check erin secrets admin refs/heads/main 2>&1); rc=$?
-expect "and the new file works" 0 "allow" "$rc" "$out"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" check erin secrets admin refs/heads/main 2>&1); rc=$?
-expect "and the original does not have it" 1 "no rule" "$rc" "$out"
-
-out=$("$AUTHZ" --policy "$TMP/edit.conf" grant erin secrets admin --stdout 2>&1 >/dev/null); rc=$?
-expect "--stdout reports on stderr" 0 "untouched" "$rc" "$out"
-cp "$TMP/edit.conf" "$TMP/edit.conf.bak"
-out=$("$AUTHZ" --policy "$TMP/edit.conf" revoke carol team --stdout 2>/dev/null); rc=$?
-expect "--stdout prints the new file" 0 "alice * admin" "$rc" "$out"
-cmp -s "$TMP/edit.conf" "$TMP/edit.conf.bak" && ok || bad "--stdout modified the policy"
-if contains "$out" "alice * admin" && contains "$out" "break glass"; then
- ok
-else
- bad "--stdout lost the comments or the settings"
-fi
-
-# It is the directory that decides: a read-only mount is a read-only directory,
-# while a 0444 file in a writable directory is still replaceable.
-
-cp "$TMP/edit.conf" "$TMP/ro.conf"
-chmod 444 "$TMP/ro.conf"
-out=$("$AUTHZ" --policy "$TMP/ro.conf" grant bob team admin 2>&1); rc=$?
-if [ "$(id -u)" = 0 ]; then
- skip_it "an unwritable directory is refused" "running as root, who defeats chmod"
- expect "the mode of a 0444 policy survives a write" 0 "granted" "$rc" "$out"
- mode=$(ls -l "$TMP/ro.conf" | cut -c2-10)
- [ "$mode" = "r--r--r--" ] && ok || bad "a 0444 policy came back as $mode"
-else
- mkdir -p "$TMP/rodir"
- cp "$TMP/edit.conf" "$TMP/rodir/policy.conf"
- chmod 555 "$TMP/rodir"
- out=$("$AUTHZ" --policy "$TMP/rodir/policy.conf" grant bob team admin 2>&1); rc=$?
- expect "a policy in a read-only directory cannot be replaced" 1 "cannot write beside" "$rc" "$out"
- chmod 755 "$TMP/rodir"
-fi
-out=$("$AUTHZ" --policy "$TMP/ro.conf" grant bob team admin --out - 2>/dev/null); rc=$?
-expect "--out works whatever the input's mode is" 0 "bob team admin" "$rc" "$out"
-
-section "edit: leaving the file alone"
-
-cat >"$TMP/mode.conf" <<'EOF'
-alice * admin
-EOF
-chmod 640 "$TMP/mode.conf"
-out=$("$AUTHZ" --policy "$TMP/mode.conf" grant bob team write 2>&1); rc=$?
-expect "a grant succeeds" 0 "granted" "$rc" "$out"
-mode=$(ls -l "$TMP/mode.conf" | cut -c2-10)
-[ "$mode" = "rw-r-----" ] && ok || bad "the mode changed to $mode"
-left=$(ls -a "$TMP" | grep -c '^\.authz')
-[ "$left" = 0 ] && ok || bad "$left temporary file(s) left behind"
-
-printf 'alice * admin' >"$TMP/nonl.conf" # no final newline
-out=$("$AUTHZ" --policy "$TMP/nonl.conf" grant bob team write 2>&1); rc=$?
-expect "a file without a trailing newline is editable" 0 "granted" "$rc" "$out"
-[ "$(tail -c 1 "$TMP/nonl.conf" | wc -l)" = 1 ] && ok || bad "the last line still has no newline"
-
-out=$("$AUTHZ" --policy "$TMP/fresh.conf" grant alice '*' admin 2>&1); rc=$?
-expect "grant can create the first rule of a new policy" 0 "granted" "$rc" "$out"
-out=$("$AUTHZ" --policy "$TMP/fresh.conf" check alice anything admin refs/heads/main 2>&1); rc=$?
-expect "and the new policy works" 0 "allow" "$rc" "$out"
-
-out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob team write --out "$TMP/nodir/x.conf" 2>&1); rc=$?
-expect "writing where the directory is not there fails" 1 "cannot write beside" "$rc" "$out"
-
# ------------------------------------------------------------------ install
section "install"
@@ -918,7 +621,7 @@ fi
if [ -f authz.conf ]; then
section "the example policy that ships with it"
- out=$("$AUTHZ" --policy authz.conf validate --no-host 2>&1); rc=$?
+ out=$("$AUTHZ" --policy authz.conf validate 2>&1); rc=$?
expect "it validates" 0 "is in force" "$rc" "$out"
out=$("$AUTHZ" --policy authz.conf check alice anything admin refs/heads/main 2>&1); rc=$?
expect "alice can do anything" 0 "allow" "$rc" "$out"