summaryrefslogtreecommitdiffstats
path: root/cmd
diff options
context:
space:
mode:
Diffstat (limited to 'cmd')
-rw-r--r--cmd/main.c108
1 files changed, 16 insertions, 92 deletions
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);