diff options
| author | dev <dev@example> | 2026-11-14 17:46:44 +0000 |
|---|---|---|
| committer | dev <dev@example> | 2026-11-14 17:46:44 +0000 |
| commit | 11c976ec713b975568594cf57d9118f58ba8c511 (patch) | |
| tree | 840912cd000f50fbd4c045a3bceaa2b55261f3fe | |
| parent | 12dcb09794c48a333bb850d353540c688c503b76 (diff) | |
| download | access-control-11c976ec713b975568594cf57d9118f58ba8c511.tar.gz access-control-11c976ec713b975568594cf57d9118f58ba8c511.zip | |
One reader for every file, one voice for every finding
Seven places opened a file, ran a getline loop, freed the line buffer and
closed the handle, and five of them also rewrote the same skip-the-blank-
and-comment preamble. authz_read_lines() does it once, and keeps the
distinction policy_load needed: a file that would not give an answer is
not an empty file.
lines_t moves to util.c with it, so edit.c's private copy goes too.
verr, vwarn and vok were one function with three tags.
cmd_authorize now frees what it read on one path instead of four.
| -rw-r--r-- | src/authz.h | 18 | ||||
| -rw-r--r-- | src/edit.c | 74 | ||||
| -rw-r--r-- | src/policy.c | 40 | ||||
| -rw-r--r-- | src/util.c | 69 | ||||
| -rw-r--r-- | src/validate.c | 112 |
5 files changed, 160 insertions, 153 deletions
diff --git a/src/authz.h b/src/authz.h index 8e6c2bb..d107fd8 100644 --- a/src/authz.h +++ b/src/authz.h @@ -160,6 +160,24 @@ typedef struct { size_t cap; } strbuf_t; +// A file, one entry per line. The newline is kept, and a last line without one +// is given one, so the lines may be written straight back out again. +typedef struct { + char **v; + size_t n; + size_t cap; +} lines_t; + +void lines_init(lines_t *l); +void lines_push(lines_t *l, char *s); // takes ownership +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. +int authz_read_lines(const char *path, lines_t *l); + void sb_init(strbuf_t *b); void sb_add(strbuf_t *b, const char *s); void sb_addf(strbuf_t *b, const char *fmt, ...); @@ -21,73 +21,6 @@ #define HIDDEN_PREFIX "/.authz" #define TEMPLATE_SUFFIX ".XXXXXX" -typedef struct { - char **v; - size_t n; - size_t cap; -} lines_t; - -static void lines_init(lines_t *l) -{ - l->v = NULL; - l->n = 0; - l->cap = 0; -} - -static void lines_push(lines_t *l, char *s) // takes ownership -{ - if (l->n == l->cap) { - l->cap = l->cap != 0 ? l->cap * 2 : 64; - l->v = xrealloc(l->v, l->cap * sizeof(*l->v)); - } - l->v[l->n++] = s; -} - -static void lines_free(lines_t *l) -{ - size_t i; - - for (i = 0; i < l->n; i++) - free(l->v[i]); - free(l->v); - lines_init(l); -} - -// Every line, newline included, exactly as it was. -static int read_lines(const char *path, lines_t *l) -{ - FILE *f; - char *line = NULL; - size_t cap = 0; - ssize_t len; - - lines_init(l); - f = fopen(path, "r"); - if (f == NULL) { - fprintf(stderr, "%s: cannot read %s: %s\n", progname, path, strerror(errno)); - return -1; - } - while ((len = getline(&line, &cap, f)) != -1) { - size_t n = strlen(line); - - if (n == 0 || line[n - 1] == '\n') { - lines_push(l, xstrdup(line)); - continue; - } - { // a last line without its newline - char *own = xmalloc(n + 2); - - memcpy(own, line, n); - own[n] = '\n'; - own[n + 1] = '\0'; - lines_push(l, own); - } - } - free(line); - fclose(f); - return 0; -} - // 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. @@ -385,8 +318,11 @@ int cmd_grant(const char *path, edit_opts o, int argc, char **argv) "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 (read_lines(path, &lines) != 0) + } 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. @@ -455,7 +391,7 @@ int cmd_revoke(const char *path, edit_opts o, int argc, char **argv) return 64; if (refuse_broken(path, NULL) != 0) return 3; - if (read_lines(path, &lines) != 0) { + 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; diff --git a/src/policy.c b/src/policy.c index 47a64f1..657e0c9 100644 --- a/src/policy.c +++ b/src/policy.c @@ -10,6 +10,7 @@ #include "authz.h" #include <ctype.h> +#include <errno.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> @@ -290,25 +291,30 @@ void policy_free(policy_t *p) int policy_load(policy_t *p) { - FILE *f; - char *line = NULL; - size_t cap = 0; - ssize_t len; + lines_t lines; + size_t i; int lineno = 0; + int rc; - f = fopen(p->path, "r"); - if (f == NULL) { + // A read that stopped because the file would not give an answer is not an + // empty policy: a directory, an I/O error halfway, a mount pulled out. The + // lines read so far are not the file, so this is the broken-host answer (3), + // not the empty-policy answer (0). + + rc = authz_read_lines(p->path, &lines); + if (rc != 0) { p->unreadable = 1; - fprintf(stderr, "%s: cannot read the policy: ", p->path); - perror(""); + fprintf(stderr, "%s: cannot read the policy: %s\n", p->path, + strerror(errno)); + lines_free(&lines); return -2; } - while ((len = getline(&line, &cap, f)) != -1) { + for (i = 0; i < lines.n; i++) { char *hash; char *text; lineno++; - text = authz_trim(line); + text = authz_trim(lines.v[i]); hash = strchr(text, '#'); if (hash != NULL) { *hash = '\0'; @@ -322,19 +328,7 @@ int policy_load(policy_t *p) else parse_rule(p, text, lineno); } - free(line); - // A read that stopped because the file would not give an answer is not an - // empty policy: a directory, an I/O error halfway, a mount pulled out. The - // lines read so far are not the file, so this is the broken-host answer (3), - // not the empty-policy answer (0). - if (ferror(f)) { - fclose(f); - p->unreadable = 1; - fprintf(stderr, "%s: cannot read the policy: ", p->path); - perror(""); - return -2; - } - fclose(f); + lines_free(&lines); return p->errors > 0 ? -1 : 0; } @@ -7,6 +7,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <sys/types.h> const char *progname = "git-authz"; @@ -135,3 +136,71 @@ void authz_opt_reset(void) opterr = 0; } +// ----------------------------------------------------------------- lines + +void lines_init(lines_t *l) +{ + l->v = NULL; + l->n = 0; + l->cap = 0; +} + +void lines_push(lines_t *l, char *s) // takes ownership +{ + if (l->n == l->cap) { + l->cap = l->cap != 0 ? l->cap * 2 : 64; + l->v = xrealloc(l->v, l->cap * sizeof(*l->v)); + } + l->v[l->n++] = s; +} + +void lines_free(lines_t *l) +{ + size_t i; + + for (i = 0; i < l->n; i++) + free(l->v[i]); + free(l->v); + lines_init(l); +} + +// Every reader in this program used to write this loop, its teardown and its +// "skip the blank and comment lines" preamble again by hand, and a reader that +// forgot one of the three leaked quietly. + +int authz_read_lines(const char *path, lines_t *l) +{ + FILE *f; + char *line = NULL; + size_t cap = 0; + ssize_t len; + + lines_init(l); + f = fopen(path, "r"); + if (f == NULL) + return -1; + while ((len = getline(&line, &cap, f)) != -1) { + size_t n = strlen(line); + + if (n == 0 || line[n - 1] == '\n') { + lines_push(l, xstrdup(line)); + continue; + } + { // a last line without its newline + char *own = xmalloc(n + 2); + + memcpy(own, line, n); + own[n] = '\n'; + own[n + 1] = '\0'; + lines_push(l, own); + } + } + free(line); + if (ferror(f)) { + fclose(f); + return -2; + } + fclose(f); + return 0; +} + diff --git a/src/validate.c b/src/validate.c index c5d05ab..6c2b19a 100644 --- a/src/validate.c +++ b/src/validate.c @@ -21,16 +21,24 @@ 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. + +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++; - fputs("err ", stderr); va_start(ap, fmt); - vfprintf(stderr, fmt, ap); + vmsg(stderr, "err ", fmt, ap); va_end(ap); - fputc('\n', stderr); } static void vwarn(const char *fmt, ...) @@ -38,22 +46,18 @@ static void vwarn(const char *fmt, ...) va_list ap; g_warnings++; - fputs("warn ", stderr); va_start(ap, fmt); - vfprintf(stderr, fmt, ap); + vmsg(stderr, "warn ", fmt, ap); va_end(ap); - fputc('\n', stderr); } static void vok(const char *fmt, ...) { va_list ap; - fputs("ok ", stdout); va_start(ap, fmt); - vfprintf(stdout, fmt, ap); + vmsg(stdout, "ok ", fmt, ap); va_end(ap); - fputc('\n', stdout); } static int ci_equal(const char *a, const char *b) @@ -83,20 +87,19 @@ static int ci_prefix(const char *line, const char *want) static int ini_value(const char *path, const char *section, const char *key, char *out, size_t outsz) { - FILE *f; - char *line = NULL; - size_t cap = 0; - ssize_t len; + lines_t lines; + size_t i; char here[64]; int in = 0; int found = 0; snprintf(here, sizeof(here), "[%s]", section); - f = fopen(path, "r"); - if (f == NULL) + if (authz_read_lines(path, &lines) != 0) { + lines_free(&lines); return -1; - while ((len = getline(&line, &cap, f)) != -1) { - char *t = authz_trim(line); + } + for (i = 0; i < lines.n; i++) { + char *t = authz_trim(lines.v[i]); char *eq; if (*t == '\0' || *t == '#' || *t == ';') @@ -124,8 +127,7 @@ static int ini_value(const char *path, const char *section, const char *key, } found = 1; } - free(line); - fclose(f); + lines_free(&lines); return found; } @@ -136,16 +138,15 @@ static int ini_value(const char *path, const char *section, const char *key, static int directive_value(const char *path, const char *name, char *out, size_t outsz) { - FILE *f; - char *line = NULL; - size_t cap = 0; - ssize_t len; + lines_t lines; + size_t i; - f = fopen(path, "r"); - if (f == NULL) + if (authz_read_lines(path, &lines) != 0) { + lines_free(&lines); return -1; - while ((len = getline(&line, &cap, f)) != -1) { - char *t = authz_trim(line); + } + for (i = 0; i < lines.n; i++) { + char *t = authz_trim(lines.v[i]); char *sp; if (*t == '\0' || *t == '#') @@ -159,12 +160,10 @@ static int directive_value(const char *path, const char *name, char *out, size_t if (!ci_equal(t, name)) continue; snprintf(out, outsz, "%s", authz_trim(sp + 1)); - free(line); - fclose(f); + lines_free(&lines); return 1; } - free(line); - fclose(f); + lines_free(&lines); return 0; } @@ -289,22 +288,20 @@ static void check_sshd(const policy_t *p, validate_opts o) static void check_account(const policy_t *p, validate_opts o) { - FILE *f; - char *line = NULL; - size_t cap = 0; - ssize_t len; + lines_t lines; + size_t i; int found = 0; (void)p; - f = fopen(o.passwd, "r"); - if (f == NULL) { + if (authz_read_lines(o.passwd, &lines) != 0) { + lines_free(&lines); vok("no %s (nothing to cross-check)", o.passwd); return; } - while ((len = getline(&line, &cap, f)) != -1) { + for (i = 0; i < lines.n; i++) { char *fields[8]; int nf = 0; - char *q = line; + 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. @@ -335,27 +332,24 @@ static void check_account(const policy_t *p, validate_opts o) break; } } - free(line); - fclose(f); + lines_free(&lines); if (!found) vwarn("no git account in %s", o.passwd); } static void check_keys(const policy_t *p, validate_opts o) { - FILE *f; - char *line = NULL; - size_t cap = 0; - ssize_t len; + lines_t lines; + size_t i; int n = 0; - f = fopen(o.keys, "r"); - if (f == NULL) { + if (authz_read_lines(o.keys, &lines) != 0) { + lines_free(&lines); vok("no authorized_keys at %s (nothing to cross-check)", o.keys); return; } - while ((len = getline(&line, &cap, f)) != -1) { - char *t = authz_trim(line); + for (i = 0; i < lines.n; i++) { + char *t = authz_trim(lines.v[i]); char *at; if (*t == '\0' || *t == '#') @@ -393,27 +387,24 @@ static void check_keys(const policy_t *p, validate_opts o) "every push is denied", name, p->path); } } - free(line); - fclose(f); + 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) { - FILE *f; - char *line = NULL; - size_t cap = 0; - ssize_t len; + lines_t lines; + size_t i; int n = 0; - f = fopen(o.htpasswd, "r"); - if (f == NULL) { + if (authz_read_lines(o.htpasswd, &lines) != 0) { + lines_free(&lines); vok("no htpasswd at %s (nothing to cross-check)", o.htpasswd); return; } - while ((len = getline(&line, &cap, f)) != -1) { - char *t = authz_trim(line); + for (i = 0; i < lines.n; i++) { + char *t = authz_trim(lines.v[i]); char *colon; if (*t == '\0' || *t == '#') @@ -429,8 +420,7 @@ static void check_htpasswd(const policy_t *p, validate_opts o) vwarn("htpasswd user \"%s\" has no rule in %s: they can authenticate " "and change nothing", t, p->path); } - free(line); - fclose(f); + lines_free(&lines); if (n > 0) vok("%d http user(s) in %s", n, o.htpasswd); } |
