summaryrefslogtreecommitdiffstats
path: root/src/authz.h
blob: 794c12cc8730c4ff3019156173074d074d6a6356 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* authz.h - who may move which refs.
 *
 * One policy file, one program, three modes:
 *
 *   check      answer one question            (an operator, or a script)
 *   authorize  answer the questions a push asks, the way git's receive hook
 *              asks them: identity in the environment, one "old new ref" line
 *              per ref on stdin
 *   validate   say whether the policy, and the host around it, make sense
 *   grant/revoke/protect  edit the policy file (it is a file; that is the
 *              whole point - but see the read-only rules in edit.c)
 *
 * The verdict travels in the exit status, because the caller is a hook:
 *
 *   0  allowed
 *   1  denied
 *   3  the policy could not be read, or does not parse - fail closed
 *   64 usage
 *
 * Everything here is C99 plus libc.  There is deliberately no regex library,
 * no fnmatch, no YAML, no SQLite: the policy grammar is words and three glob
 * metacharacters, and a hook must not depend on anything that can be missing
 * at 3am when the push happens.
 */
#ifndef AUTHZ_H
#define AUTHZ_H

#include <stddef.h>

#define MAX_FIELDS 64        /* one rule line, split on whitespace */

typedef enum {
    LV_NONE = 0,
    LV_WRITE = 1,            /* create and fast-forward */
    LV_FORCE = 2,            /* + rewrite history and delete */
    LV_ADMIN = 3             /* + protected refs, odd refs, the default branch */
} level_t;

typedef struct {
    char *user;              /* pattern, as written */
    char *repo;              /* pattern, normalised (no .git) */
    level_t level;
    char **refs;             /* normalised patterns; NULL/0 = whole repository */
    size_t nrefs;
    int line;                /* 1-based, for messages */
} rule_t;

typedef struct {
    rule_t *rules;
    size_t nrules, rcap;

    int errors;
    int unreadable;          /* the file itself could not be opened */
    char *path;              /* what messages call the file */
} policy_t;

typedef struct {
    int allowed;
    level_t need;            /* what it cost in the end */
    char why[512];           /* the sentence the person pushing reads */
} decision_t;

/* ---------------------------------------------------------------- glob.c */

/* Glob matching: '*' is any run of characters (including '/'), '?' is one
 * character, '[abc]' and '[a-z]' are classes, '!' or '^' after the bracket
 * negates.  Everything else is literal, including '.'.
 *
 * Written out rather than reached for with fnmatch(3) or regex(3): both exist
 * under different names and with different escapes on the systems this runs
 * on, and neither is worth a dependency for three metacharacters. */
int glob_match(const char *pattern, const char *s);

/* -------------------------------------------------------------- policy.c */

void policy_init(policy_t *p, const char *path);
void policy_free(policy_t *p);

/* Read and parse.  Returns 0 when the file is usable, -1 when it produced
 * errors, -2 when it could not be read at all.  Errors are reported as they are
 * found, on stderr, with the line number. */
int policy_load(policy_t *p);

/* Refs and repositories have one spelling each internally.  A bare name is a
 * branch, which is what makes "main" and "refs/heads/main" the same thing and
 * keeps a branch from ever meaning a tag of the same name.  Both return
 * malloc'd strings. */
char *authz_norm_ref(const char *in);

/* Whitespace helpers, shared by the parser and the editor.  authz_trim edits in
 * place and returns the first non-blank byte. */
char *authz_trim(char *s);
/* Splits in place, returning the field count; more than 'max' returns max+1 so
 * the caller can complain rather than read past the array. */
int authz_split_ws(char *line, char **fields, int max);

/* The shapes a user and a repository name may take.  Repository names are flat:
 * two levels of namespace with a glob over each is all this model has, and the
 * check that enforces it is the documentation for it. */
int authz_user_pattern_ok(const char *s);
int authz_repo_pattern_ok(const char *s);
char *authz_norm_repo(const char *in);

const char *authz_level_name(level_t l);
/* -1 for anything that is not a level. */
int authz_level_of(const char *name);

/* True if some rule other than an '@all' one names this user: '@all' means
 * "everybody this policy already knows about", and never invents a name. */
int authz_user_known(const policy_t *p, const char *user);

/* What a ref really costs beyond what the rule said.  Anything that is not a
 * branch or a tag is for maintainers, whatever a rule about it appears to
 * grant: refs/notes, refs/merge-requests, whatever a tool invents next, are not
 * part of the namespace a policy was written with in mind, and the safe reading
 * of "not mentioned" is "not granted".  When the answer is higher than 'have',
 * the reason is written to raised, which must hold at least 64 bytes. */
level_t authz_need_for(const char *ref, level_t have, char *raised,
                       size_t raisedsz);

/* The decision.  'ref' may be NULL, which asks "may this user do this
 * somewhere in the repository" rather than about one ref. */
void authz_check(const policy_t *p, const char *user, const char *repo,
                 level_t need, const char *ref, decision_t *out);

/* True if any rule reaches this repository at all: an unreadable-by-nobody
 * repository is readable and unpushable, which is usually a name that stopped
 * matching rather than a decision. */
int authz_repo_reachable(const policy_t *p, const char *repo);

/* --------------------------------------------------------------- hook.c */

/* authorize mode: identity from the environment, ref lines from stdin, the
 * shape git hands a receive hook.  Returns the exit status.
 *
 * Whether an update is a fast-forward, and which ref HEAD points at, are facts
 * about the object store that only git knows.  They are asked of git unless
 * asked not to be, and an answer that cannot be had is the conservative one:
 * treat the update as a rewrite, and treat any deleted ref as the default
 * branch. */
typedef struct {
    const char *user;        /* overrides the environment (tests, debugging) */
    const char *repo;        /* ditto */
    const char *ff;          /* "auto" (ask git), "yes", "no" */
    const char *head;        /* "auto" (ask git), "none", or a refname */
    int use_git;             /* 0 never runs git, whatever 'ff'/'head' say */
    int explain;             /* print every verdict, not only the denials */
} hook_opts;

int cmd_authorize(const policy_t *p, hook_opts o, int argc, char **argv);

/* ------------------------------------------------------------ validate.c */

typedef struct {
    const char *root;        /* where the repositories live */
    const char *hook_dir;
    const char *gitconfig;
    const char *sshd_config;
    const char *passwd;
    const char *keys;
    const char *htpasswd;
    int strict;              /* warnings are errors */
} validate_opts;

int cmd_validate(const policy_t *p, validate_opts o);

/* ---------------------------------------------------------------- edit.c */

typedef struct {
    const char *out;         /* NULL edits in place; "-" is stdout */
} edit_opts;

/* Shared by the two editing commands: pulls --out and --stdout out of the
 * argument list wherever they sit (an option at the end reads better in a
 * shell history) and compacts what is left into argv, returning the count in
 * *consumed.  Returns -1 on a missing value. */
int authz_parse_out(int argc, char **argv, edit_opts *o, int *consumed);

/* A ref pattern or name that starts with "--" is a mistyped option, not
 * something to grant.  Prints and returns 1 when it finds one. */
int authz_reject_options(int argc, char **argv);

int cmd_grant(const char *path, edit_opts o, int argc, char **argv);
int cmd_revoke(const char *path, edit_opts o, int argc, char **argv);

/* ---------------------------------------------------------------- main.c
 * Shared little helpers, so that no file invents its own way to fail. */

/* A growing string.  The policy is assembled and explained in pieces, and
 * strncat with hand-computed room is how buffer bugs are born. */
typedef struct {
    char *s;              /* never NULL once sb_init has run */
    size_t len;
    size_t cap;
} strbuf_t;

void sb_init(strbuf_t *b);
void sb_add(strbuf_t *b, const char *s);
void sb_addf(strbuf_t *b, const char *fmt, ...);
void sb_free(strbuf_t *b);

void *xmalloc(size_t n);
void *xrealloc(void *p, size_t n);
char *xstrdup(const char *s);
char *xsubstr(const char *s, size_t len);

/* The name of this program as it should appear in messages: 'pre-receive' when
 * it was invoked through the hook symlink, 'git-authz' otherwise. */
extern const char *progname;

#endif /* AUTHZ_H */