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
|
// No regex, no fnmatch, no libgit2: a hook must not depend on anything that can
// be missing at 3am.
//
// The exit status is the interface: 0 allowed, 1 denied, 3 no usable policy,
// 64 usage.
#ifndef AUTHZ_H
#define AUTHZ_H
#include <getopt.h>
#include <stddef.h>
#define MAX_FIELDS 64 // one rule line, split on whitespace
#define AUTHZ_MSGSZ 512 // one complaint, in the words the caller prints
#define DEFAULT_ROOT "/var/lib/git" // where the repositories live; the hook
// derives a repo name from this too
typedef enum {
LV_NONE = 0,
LV_WRITE = 1, // create, fast-forward
LV_FORCE = 2, // + rewrite history, delete
LV_ADMIN = 3 // + the default branch, refs that are neither
} 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 --------------------------------------------------------------------
// '*' is any run including '/', '?' one character, [a-z] classes with ! or ^
// negating, everything else literal. fnmatch and regex mean different things on
// different systems; this does not.
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);
// 0 usable, -1 errors, -2 unreadable. Errors print as they are found.
int policy_load(policy_t *p);
// A bare name is a branch: "main" and "refs/heads/main" are one thing, and a
// branch never means a same-named tag. malloc'd.
char *authz_norm_ref(const char *in);
char *authz_trim(char *s);
// A policy line's body: trimmed, comment and all after it gone. Edits the line.
char *authz_line_body(char *line);
// More than 'max' fields returns max+1, so the caller complains rather than
// reading past the array.
int authz_split_ws(char *line, char **fields, int max);
// Repository names are flat: two levels with a glob over each is the whole model.
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);
int authz_level_of(const char *name); // -1 for anything that is not one
// '@all' means everybody this policy already names; it never invents a name.
int authz_user_known(const policy_t *p, const char *user);
// The grammar, in one place: check and the file parser must refuse the same
// lines for the same reason, because a rule the parser rejects denies every push
// in the repository. Fills msg with the complaint in the words the caller
// prints; returns 0 when the fields parse. A NULL user or repo is a field the
// caller is not asking about.
int authz_rule_validate(const char *user, const char *repo, const char *level,
char *const *refs, int nrefs, char *msg, size_t msgs);
// What a ref costs beyond what the rule said. Neither a branch nor a tag is
// admin whatever a rule grants - refs/notes, refs/merge-requests, whatever a tool
// invents next. The reason goes in raised (>= 64 bytes).
level_t authz_need_for(const char *ref, level_t have, char *raised,
size_t raisedsz);
// 'ref' may be NULL, which asks "somewhere in this 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);
// hook.c --------------------------------------------------------------------
// authorize mode: identity from the environment, "old new ref" on stdin, the
// shape git hands a receive hook. Returns the exit status.
//
// Is old an ancestor of new, and which ref is HEAD, are asked of git. No answer
// is the conservative answer: a rewrite, and any deleted ref 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 {
int strict; // warnings are errors
} validate_opts;
int cmd_validate(const policy_t *p, validate_opts o);
// util.c --------------------------------------------------------------------
// A growing string. 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;
// 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,
// nothing at all for a file that is optional here.
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, ...);
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);
// 'pre-receive' when invoked through the hook symlink, 'git-authz' otherwise.
extern const char *progname;
// getopt_long hands back an option's val, not the name that was typed, and for
// a long-only option the val is a number nobody wrote down. The table is the
// only place that still knows the pair.
const char *authz_optname(int val, const struct option *lo);
// A run parses options twice - the global ones, then the command's own - each on
// its own argv slice, so getopt_long has to be told to start again.
void authz_opt_reset(void);
// The two complaints every getopt_long loop would otherwise write by hand.
void authz_opt_need_value(const struct option *lo);
void authz_opt_unknown(const char *cmd, const char *arg);
#endif /* AUTHZ_H */
|