summaryrefslogtreecommitdiffstats
path: root/src/validate.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/validate.c')
-rw-r--r--src/validate.c112
1 files changed, 51 insertions, 61 deletions
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);
}