diff options
| author | dev <dev@example> | 2026-11-14 17:42:44 +0000 |
|---|---|---|
| committer | dev <dev@example> | 2026-11-14 17:42:44 +0000 |
| commit | 12dcb09794c48a333bb850d353540c688c503b76 (patch) | |
| tree | f1538ec1b1c794790b16b2a4f3b6f1cd4cb99231 | |
| parent | a2a702cb81eaf73a8989cc31c682592c8acb769a (diff) | |
| download | access-control-12dcb09794c48a333bb850d353540c688c503b76.tar.gz access-control-12dcb09794c48a333bb850d353540c688c503b76.zip | |
Options: getopt_long instead of three hand-rolled parsers
option() in hook.c and one_option() in cmd/main.c were the same function
under two names, and validate's seven options were a chain of seven calls
to one of them. getopt_long does all of it - both --opt value and
--opt=value, the lot - so the parsers are gone and what is left is a table
and what each entry sets.
Two small helpers carry what getopt_long cannot: the name behind a long
option's val, for the 'wants a value' message, and the optind reset that
lets a run parse options twice on two different argv slices.
A command now gets argv[0] as its own name, which is where getopt_long
expects argv[0]; the commands that take names step over it.
strip() in validate.c was authz_trim() again, and cmd_authorize freed the
same three pointers on four exit paths.
One behaviour change: 'authorize --' is now the end-of-options marker
rather than an unknown argument. Anything after it is still refused.
| -rw-r--r-- | cmd/main.c | 201 | ||||
| -rw-r--r-- | src/authz.h | 14 | ||||
| -rw-r--r-- | src/hook.c | 116 | ||||
| -rw-r--r-- | src/util.c | 21 | ||||
| -rw-r--r-- | src/validate.c | 29 |
5 files changed, 221 insertions, 160 deletions
@@ -19,6 +19,8 @@ #define DEFAULT_KEYS "/run/git-host/secrets/authorized_keys" #define DEFAULT_HTPASSWD "/etc/git-host/secrets/htpasswd" +enum { OPT_POLICY = 128, OPT_HELP }; + static void usage(FILE *f) { fprintf(f, @@ -66,25 +68,6 @@ DEFAULT_ROOT, DEFAULT_HOOKS, DEFAULT_GITCONFIG, DEFAULT_SSHD, DEFAULT_PASSWD, DEFAULT_KEYS, DEFAULT_HTPASSWD); } -static int one_option(int argc, char **argv, int *i, const char *name, const char **slot) -{ - size_t n = strlen(name); - - if (strcmp(argv[*i], name) == 0) { - if (*i + 1 >= argc) { - fprintf(stderr, "%s: %s wants a value\n", progname, name); - return -1; - } - *slot = argv[++*i]; - return 1; - } - if (strncmp(argv[*i], name, n) == 0 && argv[*i][n] == '=') { - *slot = argv[*i] + n + 1; - return 1; - } - return 0; -} - // 3 and not 1: a push refused because the host is broken has to sound different // from one refused because the person is not allowed. @@ -99,53 +82,80 @@ static int deny_policy(const char *path, int rc) // process can leave the heap to the kernel, but then the sanitizer reports // 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 +}; + +// 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 }, + { NULL, 0, NULL, 0 } + }; + int c; + + 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 ':': + fprintf(stderr, "%s: --%s wants a value\n", progname, + authz_optname(optopt, longopts)); + return -1; + default: + fprintf(stderr, "%s: validate: unknown argument %s\n", progname, + argv[optind - 1]); + return -1; + } + } + if (optind < argc) { + fprintf(stderr, "%s: validate: unknown argument %s\n", progname, + argv[optind]); + return -1; + } + return 0; +} + static int dispatch(policy_t *p, int rc, const char *cmd, int argc, char **argv, const char *path, hook_opts ho, validate_opts vo) { + // argv[0] is the command name, which is where getopt_long expects argv[0] + // to be. A command's own positional arguments therefore start at argv[1]. + if (strcmp(cmd, "validate") == 0) { - int j; - - for (j = 0; j < argc; j++) { - int got; - - got = one_option(argc, argv, &j, "--root", &vo.root); - if (got == 0) - got = one_option(argc, argv, &j, "--hook-dir", &vo.hook_dir); - if (got == 0) - got = one_option(argc, argv, &j, "--gitconfig", &vo.gitconfig); - if (got == 0) - got = one_option(argc, argv, &j, "--sshd-config", &vo.sshd_config); - if (got == 0) - got = one_option(argc, argv, &j, "--passwd", &vo.passwd); - if (got == 0) - got = one_option(argc, argv, &j, "--keys", &vo.keys); - if (got == 0) - got = one_option(argc, argv, &j, "--htpasswd", &vo.htpasswd); - if (got < 0) - return 64; - if (got == 0 && strcmp(argv[j], "--strict") == 0) { - vo.strict = 1; - continue; - } - // A host without a git account yet - a container being built - - // still wants its policy read. - - if (got == 0 && strcmp(argv[j], "--no-host") == 0) { - vo.root = NULL; - vo.hook_dir = NULL; - vo.gitconfig = NULL; - vo.sshd_config = NULL; - vo.passwd = NULL; - vo.keys = NULL; - vo.htpasswd = NULL; - continue; - } - if (got == 0) { - fprintf(stderr, "%s: validate: unknown argument %s\n", progname, - argv[j]); - return 64; - } - } + if (parse_validate(argc, argv, &vo) != 0) + return 64; // A policy that does not parse is the finding, so it is passed down // rather than exiting 3 here. @@ -162,6 +172,8 @@ static int dispatch(policy_t *p, int rc, const char *cmd, int argc, char **argv, decision_t d; int level; + argc--; + argv++; if (argc < 3 || argc > 4) { fprintf(stderr, "%s: check wants <user> <repo> <level> [ref]\n", progname); return 64; @@ -186,6 +198,11 @@ static int dispatch(policy_t *p, int rc, const char *cmd, int argc, char **argv, int main(int argc, char **argv) { + static const struct option longopts[] = { + { "policy", required_argument, NULL, OPT_POLICY }, + { "help", no_argument, NULL, OPT_HELP }, + { NULL, 0, NULL, 0 } + }; const char *path; const char *base; const char *cmd; @@ -193,7 +210,7 @@ int main(int argc, char **argv) hook_opts ho; validate_opts vo; edit_opts eo; - int i; + int c; int rc; int status; @@ -217,45 +234,59 @@ int main(int argc, char **argv) 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. + // so they are dropped, and the policy comes from the environment. The one + // element is the command-name slot getopt_long expects at argv[0]. if (strcmp(progname, "pre-receive") == 0) { + char *hook_argv[] = { (char *)"pre-receive", NULL }; + policy_init(&p, path); rc = policy_load(&p); if (rc != 0) { policy_free(&p); return deny_policy(path, rc); } - status = cmd_authorize(&p, ho, 0, NULL); + status = cmd_authorize(&p, ho, 1, hook_argv); policy_free(&p); return status; } - i = 1; - while (i < argc && argv[i][0] == '-' && argv[i][1] != '\0') { - int got = one_option(argc, argv, &i, "--policy", &path); + // Options before the command are the program's own. The '+' in the optstring + // is what stops getopt at the command name rather than permuting past it. - if (got < 0) - return 64; - if (got == 0 && (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)) { + authz_opt_reset(); + while ((c = getopt_long(argc, argv, "+:h", longopts, NULL)) != -1) { + switch (c) { + case OPT_POLICY: + path = optarg; + break; + case OPT_HELP: + case 'h': usage(stdout); return 0; - } - if (got == 0) { - fprintf(stderr, "%s: unknown option before the command: %s\n", progname, - argv[i]); + case ':': + fprintf(stderr, "%s: --%s wants a value\n", progname, + authz_optname(optopt, longopts)); + return 64; + default: + fprintf(stderr, "%s: unknown option before the command: %s\n", + progname, argv[optind - 1]); usage(stderr); return 64; } - i++; } - if (i >= argc) { + if (optind >= argc) { usage(stderr); return 64; } - cmd = argv[i++]; - argc -= i; - argv += i; + + // argv[0] is the command from here on - which is where getopt_long expects + // argv[0] to be, so validate and authorize parse it as it stands, and the + // commands that take names step over it. + + cmd = argv[optind]; + argc -= optind; + argv += optind; if (strcmp(cmd, "help") == 0 || strcmp(cmd, "-h") == 0 || strcmp(cmd, "--help") == 0) { @@ -263,16 +294,16 @@ 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(argc, argv, &eo, &consumed) != 0) + if (authz_parse_out(n, argv + 1, &eo, &consumed) != 0) return 64; - argc -= consumed; // what is left was compacted to the front + n -= consumed; // what is left was compacted to the front if (strcmp(cmd, "grant") == 0) - return cmd_grant(path, eo, argc, argv); - return cmd_revoke(path, eo, argc, argv); + return cmd_grant(path, eo, n, argv + 1); + return cmd_revoke(path, eo, n, argv + 1); } policy_init(&p, path); diff --git a/src/authz.h b/src/authz.h index 671b45f..8e6c2bb 100644 --- a/src/authz.h +++ b/src/authz.h @@ -7,9 +7,10 @@ #ifndef AUTHZ_H #define AUTHZ_H +#include <getopt.h> #include <stddef.h> -#define MAX_FIELDS 64 // one rule line, split on whitespace +#define MAX_FIELDS 64 // one rule line, split on whitespace // one rule line, split on whitespace typedef enum { LV_NONE = 0, @@ -172,4 +173,15 @@ char *xsubstr(const char *s, size_t len); // 'pre-receive' when invoked through the hook symlink, 'git-authz' otherwise. extern const char *progname; +// getopt_long hands back an option's val, not the name that was typed, and for +// a long-only option the val is a number nobody wrote down. The table is the +// only place that still knows the pair. + +const char *authz_optname(int val, const struct option *lo); + +// A run parses options twice - the global ones, then the command's own - each on +// its own argv slice, so getopt_long has to be told to start again. + +void authz_opt_reset(void); + #endif /* AUTHZ_H */ @@ -227,24 +227,56 @@ static const char *identity(hook_opts o) // "--ff=yes" and "--ff yes" mean the same thing. The usage text writes the // two-word form and validate accepts both, so authorize accepts both: an option -// the help offers must not come back as "unknown argument". +// the help offers must not come back as "unknown argument". getopt_long does +// both spellings, so there is nothing here but the table and what each one sets. -static int option(int argc, char **argv, int *i, const char *name, - const char **slot) -{ - size_t n = strlen(name); +enum { + OPT_USER = 128, OPT_REPO, OPT_FF, OPT_HEAD, OPT_NOGIT, OPT_EXPLAIN +}; - if (strcmp(argv[*i], name) == 0) { - if (*i + 1 >= argc) { - fprintf(stderr, "%s: %s wants a value\n", progname, name); +static int parse_options(int argc, char **argv, hook_opts *o) +{ + static const struct option longopts[] = { + { "user", required_argument, NULL, OPT_USER }, + { "repo", required_argument, NULL, OPT_REPO }, + { "ff", required_argument, NULL, OPT_FF }, + { "head", required_argument, NULL, OPT_HEAD }, + { "no-git", no_argument, NULL, OPT_NOGIT }, + { "explain", no_argument, NULL, OPT_EXPLAIN }, + { NULL, 0, NULL, 0 } + }; + int c; + + // The leading '+' stops at the first non-option: authorize takes no + // positional arguments, so anything that is not an option is a mistake + // either way, and this way it is reported as the one the person typed + // rather than as everything that followed it. + + authz_opt_reset(); + while ((c = getopt_long(argc, argv, "+:", longopts, NULL)) != -1) { + switch (c) { + case OPT_USER: o->user = optarg; break; + case OPT_REPO: o->repo = optarg; break; + case OPT_FF: o->ff = optarg; break; + case OPT_HEAD: o->head = optarg; break; + 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)); + 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]); return -1; } - *slot = argv[++*i]; - return 1; } - if (strncmp(argv[*i], name, n) == 0 && argv[*i][n] == '=') { - *slot = argv[*i] + n + 1; - return 1; + if (optind < argc) { + fprintf(stderr, "%s: authorize: unknown argument %s\n", progname, + argv[optind]); + return -1; } return 0; } @@ -256,40 +288,21 @@ int cmd_authorize(const policy_t *p, hook_opts o, int argc, char **argv) ssize_t len; char *repo; const char *user; - const char *head; + char *head; int head_unknown; int seen = 0; - int i; + int status = 0; if (o.ff == NULL) o.ff = "auto"; if (o.head == NULL) o.head = "auto"; - for (i = 0; i < argc; i++) { - int got = option(argc, argv, &i, "--user", &o.user); - - if (got == 0) - got = option(argc, argv, &i, "--repo", &o.repo); - if (got == 0) - got = option(argc, argv, &i, "--ff", &o.ff); - if (got == 0) - got = option(argc, argv, &i, "--head", &o.head); - if (got < 0) - return 64; - if (got > 0) - continue; - if (strcmp(argv[i], "--no-git") == 0) { - o.use_git = 0; - continue; - } - if (strcmp(argv[i], "--explain") == 0) { - o.explain = 1; - continue; - } - fprintf(stderr, "%s: authorize: unknown argument %s\n", progname, argv[i]); + // argv[0] is the command name, which is where getopt_long expects it; the + // options are what follows. + + if (parse_options(argc, argv, &o) != 0) return 64; - } if (strcmp(o.ff, "auto") != 0 && strcmp(o.ff, "yes") != 0 && strcmp(o.ff, "no") != 0) { fprintf(stderr, "%s: --ff wants auto, yes or no\n", progname); @@ -334,10 +347,8 @@ int cmd_authorize(const policy_t *p, hook_opts o, int argc, char **argv) if (nf != 3) { fprintf(stderr, "%s: deny: expected \"old new ref\", got %d field(s)\n", progname, nf); - free(line); - free(repo); - free((char *)head); - return 1; + status = 1; + break; } old = fields[0]; @@ -349,10 +360,8 @@ int cmd_authorize(const policy_t *p, hook_opts o, int argc, char **argv) strncmp(ref, "refs/", 5) != 0) { fprintf(stderr, "%s: deny: \"%s %s %s\" is not an update git would send\n", progname, old, new, ref); - free(line); - free(repo); - free((char *)head); - return 1; + status = 1; + break; } if (is_zero(new)) { @@ -371,19 +380,20 @@ int cmd_authorize(const policy_t *p, hook_opts o, int argc, char **argv) authz_check(p, user, repo, need, ref, &d); if (!d.allowed) { fprintf(stderr, "%s: deny: %s\n", progname, d.why); - free(line); - free(repo); - free((char *)head); - return 1; + status = 1; + break; } if (o.explain) printf("%s: allow: %s\n", progname, d.why); } + + // One exit path, so nothing added later can forget one of these. + free(line); free(repo); - free((char *)head); + free(head); - if (seen == 0 && o.explain) + if (seen == 0 && o.explain && status == 0) printf("%s: nothing to check\n", progname); - return 0; + return status; } @@ -114,3 +114,24 @@ char *xsubstr(const char *s, size_t len) p[len] = '\0'; return p; } + +// The two things every caller of getopt_long otherwise writes by hand. + +const char *authz_optname(int val, const struct option *lo) +{ + for (; lo->name != NULL; lo++) { + if (lo->val == val) + return lo->name; + } + return "?"; +} + +// optind = 1 is how a second getopt_long call is told to begin again; opterr = 0 +// is so the messages come out in this program's words and not glibc's. + +void authz_opt_reset(void) +{ + optind = 1; + opterr = 0; +} + diff --git a/src/validate.c b/src/validate.c index 91be26a..c5d05ab 100644 --- a/src/validate.c +++ b/src/validate.c @@ -77,19 +77,6 @@ static int ci_prefix(const char *line, const char *want) return 1; } -static char *strip(char *s) -{ - char *end; - - while (*s != '\0' && isspace((unsigned char)*s)) - s++; - end = s + strlen(s); - while (end > s && isspace((unsigned char)end[-1])) - end--; - *end = '\0'; - return s; -} - // [section] key = value, last one winning - which is the mistake worth // reporting in a gitconfig. @@ -109,7 +96,7 @@ static int ini_value(const char *path, const char *section, const char *key, if (f == NULL) return -1; while ((len = getline(&line, &cap, f)) != -1) { - char *t = strip(line); + char *t = authz_trim(line); char *eq; if (*t == '\0' || *t == '#' || *t == ';') @@ -124,9 +111,9 @@ static int ini_value(const char *path, const char *section, const char *key, if (eq == NULL) continue; *eq = '\0'; - if (!ci_equal(strip(t), key)) + if (!ci_equal(authz_trim(t), key)) continue; - snprintf(out, outsz, "%s", strip(eq + 1)); + snprintf(out, outsz, "%s", authz_trim(eq + 1)); { // gitconfig allows quoting size_t n = strlen(out); @@ -158,7 +145,7 @@ static int directive_value(const char *path, const char *name, char *out, size_t if (f == NULL) return -1; while ((len = getline(&line, &cap, f)) != -1) { - char *t = strip(line); + char *t = authz_trim(line); char *sp; if (*t == '\0' || *t == '#') @@ -171,7 +158,7 @@ static int directive_value(const char *path, const char *name, char *out, size_t *sp = '\0'; if (!ci_equal(t, name)) continue; - snprintf(out, outsz, "%s", strip(sp + 1)); + snprintf(out, outsz, "%s", authz_trim(sp + 1)); free(line); fclose(f); return 1; @@ -337,7 +324,7 @@ static void check_account(const policy_t *p, validate_opts o) break; } if (nf >= 7 && strcmp(fields[0], "git") == 0) { - char *shell = strip(fields[6]); + char *shell = authz_trim(fields[6]); found = 1; if (strstr(shell, "git-shell") == NULL) @@ -368,7 +355,7 @@ static void check_keys(const policy_t *p, validate_opts o) return; } while ((len = getline(&line, &cap, f)) != -1) { - char *t = strip(line); + char *t = authz_trim(line); char *at; if (*t == '\0' || *t == '#') @@ -426,7 +413,7 @@ static void check_htpasswd(const policy_t *p, validate_opts o) return; } while ((len = getline(&line, &cap, f)) != -1) { - char *t = strip(line); + char *t = authz_trim(line); char *colon; if (*t == '\0' || *t == '#') |
