diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/edit.c | 28 | ||||
| -rw-r--r-- | src/hook.c | 42 | ||||
| -rw-r--r-- | src/policy.c | 15 | ||||
| -rw-r--r-- | src/validate.c | 32 |
4 files changed, 96 insertions, 21 deletions
@@ -194,6 +194,26 @@ static int is_rule_for(const char *line, const char *user, const char *repo) return same; } +// A ref pattern reaches the file as one field. Whitespace would split it into +// two patterns when the file is read back - granting a ref nobody asked for - and +// '#' starts a comment, so the pattern would be quietly cut short. An argument +// carrying either means something other than what was typed. + +static int ref_field_ok(const char *s) +{ + size_t i; + + if (s[0] == '\0') + return 0; + for (i = 0; s[i] != '\0'; i++) { + unsigned char c = (unsigned char)s[i]; + + if (isspace(c) || c == '#') + return 0; + } + return 1; +} + static char *rule_text(const char *user, const char *repo, const char *level, int nrefs, char **refs) { @@ -315,6 +335,7 @@ int cmd_grant(const char *path, edit_opts o, int argc, char **argv) int i; int first = -1; int replaced = -1; + int appended = 0; int dropped = 0; int created = 0; @@ -343,6 +364,10 @@ int cmd_grant(const char *path, edit_opts o, int argc, char **argv) progname, argv[i]); return 64; } + if (!ref_field_ok(argv[i])) { + fprintf(stderr, "%s: not a ref pattern: %s\n", progname, argv[i]); + return 64; + } } if (authz_reject_options(argc, argv) != 0) @@ -382,6 +407,7 @@ int cmd_grant(const char *path, edit_opts o, int argc, char **argv) if (first < 0) { lines_push(&lines, text); // the buffer belongs to the file now replaced = (int)lines.n; + appended = 1; } else { size_t w; @@ -404,7 +430,7 @@ int cmd_grant(const char *path, edit_opts o, int argc, char **argv) else if (dropped > 0) snprintf(what, sizeof(what), "granted %s %s %s (line %d, %d duplicate(s) removed)", argv[0], argv[1], argv[2], replaced, dropped); - else if ((int)lines.n == replaced) + else if (appended) snprintf(what, sizeof(what), "granted %s %s %s (appended as line %d)", argv[0], argv[1], argv[2], replaced); else @@ -225,14 +225,28 @@ static const char *identity(hook_opts o) return NULL; } -static int flag(const char *arg, const char *name, const char **slot) +// "--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". + +static int option(int argc, char **argv, int *i, const char *name, + const char **slot) { size_t n = strlen(name); - if (strncmp(arg, name, n) != 0 || arg[n] != '=') - return 0; - *slot = arg + n + 1; - return 1; + 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; } int cmd_authorize(const policy_t *p, hook_opts o, int argc, char **argv) @@ -253,13 +267,17 @@ int cmd_authorize(const policy_t *p, hook_opts o, int argc, char **argv) o.head = "auto"; for (i = 0; i < argc; i++) { - if (flag(argv[i], "--user", &o.user)) - continue; - if (flag(argv[i], "--repo", &o.repo)) - continue; - if (flag(argv[i], "--ff", &o.ff)) - continue; - if (flag(argv[i], "--head", &o.head)) + 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; diff --git a/src/policy.c b/src/policy.c index aae99a1..47a64f1 100644 --- a/src/policy.c +++ b/src/policy.c @@ -53,13 +53,13 @@ int authz_split_ws(char *line, char **f, int max) p++; if (*p == '\0') break; + if (n == max) + return n + 1; // "too many"; the caller decides f[n++] = p; while (*p != '\0' && !isspace((unsigned char)*p)) p++; if (*p != '\0') *p++ = '\0'; - if (n == max) - return n + 1; // "too many"; the caller decides } return n; } @@ -323,6 +323,17 @@ int policy_load(policy_t *p) 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); return p->errors > 0 ? -1 : 0; } diff --git a/src/validate.c b/src/validate.c index 7004731..91be26a 100644 --- a/src/validate.c +++ b/src/validate.c @@ -65,6 +65,18 @@ static int ci_equal(const char *a, const char *b) return *a == '\0' && *b == '\0'; } +// git reads a section name without regard to case, so [CORE] opens core. +static int ci_prefix(const char *line, const char *want) +{ + while (*want != '\0') { + if (tolower((unsigned char)*line) != tolower((unsigned char)*want)) + return 0; + line++; + want++; + } + return 1; +} + static char *strip(char *s) { char *end; @@ -103,7 +115,7 @@ static int ini_value(const char *path, const char *section, const char *key, if (*t == '\0' || *t == '#' || *t == ';') continue; if (*t == '[') { - in = strncmp(t, here, strlen(here)) == 0; + in = ci_prefix(t, here); continue; } if (!in) @@ -130,14 +142,17 @@ static int ini_value(const char *path, const char *section, const char *key, return found; } -// sshd_config's "Directive value", last one winning, case-insensitive. +// sshd_config's "Directive value", case-insensitive. sshd keeps the first value +// it reads for a directive, not the last, so the first match is the answer and +// every later line is one sshd never looked at - which is the difference between +// a host that accepts key identities and one that silently drops them. + 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; - int found = 0; f = fopen(path, "r"); if (f == NULL) @@ -157,11 +172,13 @@ static int directive_value(const char *path, const char *name, char *out, size_t if (!ci_equal(t, name)) continue; snprintf(out, outsz, "%s", strip(sp + 1)); - found = 1; + free(line); + fclose(f); + return 1; } free(line); fclose(f); - return found; + return 0; } static void check_repos(const policy_t *p, const char *root) @@ -268,9 +285,12 @@ static void check_sshd(const policy_t *p, validate_opts o) vok("PermitUserEnvironment yes"); rc = directive_value(o.sshd_config, "ForceCommand", value, sizeof(value)); - if (rc == 0) + if (rc != 1) verr("%s sets no ForceCommand: a key can run an interactive shell", o.sshd_config); + else if (ci_equal(value, "none")) + verr("ForceCommand is none in %s: that is how a Match block turns it back " + "off, and a key can run an interactive shell", o.sshd_config); else vok("ForceCommand %s", value); |
