summaryrefslogtreecommitdiffstats
path: root/src/hook.c
diff options
context:
space:
mode:
authordev <dev@example>2026-11-14 17:42:44 +0000
committerdev <dev@example>2026-11-14 17:42:44 +0000
commit12dcb09794c48a333bb850d353540c688c503b76 (patch)
treef1538ec1b1c794790b16b2a4f3b6f1cd4cb99231 /src/hook.c
parenta2a702cb81eaf73a8989cc31c682592c8acb769a (diff)
downloadaccess-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.
Diffstat (limited to 'src/hook.c')
-rw-r--r--src/hook.c116
1 files changed, 63 insertions, 53 deletions
diff --git a/src/hook.c b/src/hook.c
index b7d12a5..c43f1e3 100644
--- a/src/hook.c
+++ b/src/hook.c
@@ -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;
}