summaryrefslogtreecommitdiffstats
path: root/src/policy.c
diff options
context:
space:
mode:
authordev <dev@example>2026-11-14 17:46:44 +0000
committerdev <dev@example>2026-11-14 17:46:44 +0000
commit11c976ec713b975568594cf57d9118f58ba8c511 (patch)
tree840912cd000f50fbd4c045a3bceaa2b55261f3fe /src/policy.c
parent12dcb09794c48a333bb850d353540c688c503b76 (diff)
downloadaccess-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.
Diffstat (limited to 'src/policy.c')
-rw-r--r--src/policy.c40
1 files changed, 17 insertions, 23 deletions
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;
}