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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
|
// Run from the entrypoint before anything is served, and from CI.
//
// An error means a push would be authorised wrongly; a warning means a human is
// about to file a ticket. Missing files are skipped rather than warned about: a
// validator that cries about a developer machine gets ignored, which is worse
// than not checking.
#include "authz.h"
#include <ctype.h>
#include <dirent.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
// One validate per process, so file scope is enough.
static int g_errors;
static int g_warnings;
static void verr(const char *fmt, ...)
{
va_list ap;
g_errors++;
fputs("err ", stderr);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fputc('\n', stderr);
}
static void vwarn(const char *fmt, ...)
{
va_list ap;
g_warnings++;
fputs("warn ", stderr);
va_start(ap, fmt);
vfprintf(stderr, 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);
va_end(ap);
fputc('\n', stdout);
}
static int ci_equal(const char *a, const char *b)
{
for (; *a != '\0' && *b != '\0'; a++, b++) {
if (tolower((unsigned char)*a) != tolower((unsigned char)*b))
return 0;
}
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;
while (*s != '\0' && isspace((unsigned char)*s))
s++;
end = s + strlen(s);
while (end > s && isspace((unsigned char)end[-1]))
end--;
*end = '\0';
return s;
}
// [section] key = value, last one winning - which is the mistake worth
// reporting in a gitconfig.
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;
char here[64];
int in = 0;
int found = 0;
snprintf(here, sizeof(here), "[%s]", section);
f = fopen(path, "r");
if (f == NULL)
return -1;
while ((len = getline(&line, &cap, f)) != -1) {
char *t = strip(line);
char *eq;
if (*t == '\0' || *t == '#' || *t == ';')
continue;
if (*t == '[') {
in = ci_prefix(t, here);
continue;
}
if (!in)
continue;
eq = strchr(t, '=');
if (eq == NULL)
continue;
*eq = '\0';
if (!ci_equal(strip(t), key))
continue;
snprintf(out, outsz, "%s", strip(eq + 1));
{ // gitconfig allows quoting
size_t n = strlen(out);
if (n >= 2 && out[0] == '"' && out[n - 1] == '"') {
memmove(out, out + 1, n - 2);
out[n - 2] = '\0';
}
}
found = 1;
}
free(line);
fclose(f);
return found;
}
// 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;
f = fopen(path, "r");
if (f == NULL)
return -1;
while ((len = getline(&line, &cap, f)) != -1) {
char *t = strip(line);
char *sp;
if (*t == '\0' || *t == '#')
continue;
sp = t;
while (*sp != '\0' && !isspace((unsigned char)*sp))
sp++;
if (*sp == '\0')
continue;
*sp = '\0';
if (!ci_equal(t, name))
continue;
snprintf(out, outsz, "%s", strip(sp + 1));
free(line);
fclose(f);
return 1;
}
free(line);
fclose(f);
return 0;
}
static void check_repos(const policy_t *p, const char *root)
{
DIR *d;
struct dirent *ent;
int ondisk = 0;
size_t i;
d = opendir(root);
if (d == NULL) {
vok("no repository root at %s (nothing to cross-check)", root);
return;
}
while ((ent = readdir(d)) != NULL) {
size_t n = strlen(ent->d_name);
char path[4096];
struct stat st;
if (n <= 4 || strcmp(ent->d_name + n - 4, ".git") != 0)
continue;
snprintf(path, sizeof(path), "%s/%s", root, ent->d_name);
if (stat(path, &st) != 0 || !S_ISDIR(st.st_mode))
continue;
ondisk++;
{
char *name = authz_norm_repo(ent->d_name);
if (!authz_repo_reachable(p, name))
vwarn("%s is readable and nobody can push to it", name);
free(name);
}
}
closedir(d);
// A rule naming a repository that is not there is a renamed repository,
// and the rule still looks fine.
for (i = 0; i < p->nrules; i++) {
const char *r = p->rules[i].repo;
if (r == NULL || strpbrk(r, "*?[") != NULL || strcmp(r, "*") == 0)
continue;
{
char path[4096];
struct stat st;
snprintf(path, sizeof(path), "%s/%s.git", root, r);
if (stat(path, &st) != 0)
vwarn("line %d gives %s access to %s, which is not in %s",
p->rules[i].line, p->rules[i].user, r, root);
}
}
if (ondisk > 0)
vok("%d repository/repositories under %s", ondisk, root);
}
static void check_hook(const policy_t *p, validate_opts o)
{
char path[4096];
struct stat st;
char hooks[4096];
int rc;
(void)p;
snprintf(path, sizeof(path), "%s/pre-receive", o.hook_dir);
if (stat(path, &st) != 0) {
verr("no hook at %s: every push would be accepted unchecked", path);
return;
}
if (access(path, X_OK) != 0)
verr("%s is not executable: git would skip it", path);
else
vok("hook %s", path);
rc = ini_value(o.gitconfig, "core", "hooksPath", hooks, sizeof(hooks));
if (rc == -1) {
vok("no gitconfig at %s (nothing to cross-check)", o.gitconfig);
return;
}
if (rc == 0)
verr("%s does not set core.hooksPath: %s is never run", o.gitconfig, path);
else if (strcmp(hooks, o.hook_dir) != 0)
verr("core.hooksPath is %s, not %s: pushes are checked by something else",
hooks, o.hook_dir);
else
vok("core.hooksPath -> %s", hooks);
}
static void check_sshd(const policy_t *p, validate_opts o)
{
char value[4096];
int rc;
rc = directive_value(o.sshd_config, "PermitUserEnvironment", value, sizeof(value));
if (rc == -1) {
vok("no sshd config at %s (nothing to cross-check)", o.sshd_config);
return;
}
if (rc == 0 || !ci_equal(value, "yes"))
verr("PermitUserEnvironment is not yes in %s: key identities are dropped, "
"and every push arrives anonymous", o.sshd_config);
else
vok("PermitUserEnvironment yes");
rc = directive_value(o.sshd_config, "ForceCommand", value, sizeof(value));
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);
rc = directive_value(o.sshd_config, "AuthorizedKeysFile", value, sizeof(value));
if (rc == 1 && o.keys != NULL && strstr(value, o.keys) == NULL)
vwarn("AuthorizedKeysFile is \"%s\", which does not include %s", value, o.keys);
(void)p;
}
static void check_account(const policy_t *p, validate_opts o)
{
FILE *f;
char *line = NULL;
size_t cap = 0;
ssize_t len;
int found = 0;
(void)p;
f = fopen(o.passwd, "r");
if (f == NULL) {
vok("no %s (nothing to cross-check)", o.passwd);
return;
}
while ((len = getline(&line, &cap, f)) != -1) {
char *fields[8];
int nf = 0;
char *q = line;
// 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.
while (*q != '\0') {
char *start = q;
while (*q != '\0' && *q != ':')
q++;
if (*q == '\0') {
fields[nf++] = start;
break;
}
*q++ = '\0';
fields[nf++] = start;
if (nf == 7)
break;
}
if (nf >= 7 && strcmp(fields[0], "git") == 0) {
char *shell = strip(fields[6]);
found = 1;
if (strstr(shell, "git-shell") == NULL)
verr("the git account's shell is %s, not git-shell: ssh keys "
"would land in a real shell", shell);
else
vok("git account shell %s", shell);
break;
}
}
free(line);
fclose(f);
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;
int n = 0;
f = fopen(o.keys, "r");
if (f == NULL) {
vok("no authorized_keys at %s (nothing to cross-check)", o.keys);
return;
}
while ((len = getline(&line, &cap, f)) != -1) {
char *t = strip(line);
char *at;
if (*t == '\0' || *t == '#')
continue;
n++;
at = strstr(t, "GIT_HOST_USER=");
if (at == NULL) {
char *nl = strchr(t, '\n');
char *sp;
if (nl != NULL)
*nl = '\0';
sp = strchr(t, ' ');
if (sp != NULL)
*sp = '\0'; // leave the key type, e.g. ssh-ed25519
vwarn("a %s key carries no GIT_HOST_USER: pushes made with it are "
"anonymous and will be denied", t);
continue;
}
at += strlen("GIT_HOST_USER=");
{
char name[256];
size_t i = 0;
while (at[i] != '\0' && at[i] != '"' && at[i] != ',' && at[i] != ' ' &&
i + 1 < sizeof(name)) {
name[i] = at[i];
i++;
}
name[i] = '\0';
if (i == 0)
vwarn("an empty GIT_HOST_USER: pushes made with that key are denied");
else if (!authz_user_known(p, name))
vwarn("key identity \"%s\" has no rule in %s: the key works and "
"every push is denied", name, p->path);
}
}
free(line);
fclose(f);
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;
int n = 0;
f = fopen(o.htpasswd, "r");
if (f == NULL) {
vok("no htpasswd at %s (nothing to cross-check)", o.htpasswd);
return;
}
while ((len = getline(&line, &cap, f)) != -1) {
char *t = strip(line);
char *colon;
if (*t == '\0' || *t == '#')
continue;
n++;
colon = strchr(t, ':');
if (colon == NULL) {
vwarn("%s: a line with no \":\" is not an htpasswd entry", o.htpasswd);
continue;
}
*colon = '\0';
if (!authz_user_known(p, t))
vwarn("htpasswd user \"%s\" has no rule in %s: they can authenticate "
"and change nothing", t, p->path);
}
free(line);
fclose(f);
if (n > 0)
vok("%d http user(s) in %s", n, o.htpasswd);
}
int cmd_validate(const policy_t *p, validate_opts o)
{
size_t i;
struct stat st;
g_errors = 0;
g_warnings = 0;
// The parser already printed the line numbers. A policy with errors is not
// used at all, so nothing else matters until it is fixed.
if (p->unreadable) {
verr("%s cannot be read: the hook fails closed on this, so every push is "
"denied until it is mounted", p->path);
return 3;
}
if (p->errors > 0) {
verr("%s: %d error(s): the policy is not used, and every push is denied",
p->path, p->errors);
return 3;
}
vok("%s: %zu rule(s)", p->path, p->nrules);
if (stat(p->path, &st) == 0 && (st.st_mode & 0004) == 0)
vwarn("%s is not readable by anyone outside its owner and group: the hook "
"runs as the git account and fails closed when it cannot read this",
p->path);
for (i = 0; i < p->nrules; i++) {
if (strcmp(p->rules[i].user, "@all") == 0 && p->rules[i].nrefs == 0)
vwarn("line %zu gives @all the whole of %s, which is not a default "
"anybody chose", p->rules[i].line, p->rules[i].repo);
}
if (o.root != NULL)
check_repos(p, o.root);
if (o.hook_dir != NULL)
check_hook(p, o);
if (o.sshd_config != NULL)
check_sshd(p, o);
if (o.passwd != NULL)
check_account(p, o);
if (o.keys != NULL)
check_keys(p, o);
if (o.htpasswd != NULL)
check_htpasswd(p, o);
if (g_errors > 0 || (o.strict && g_warnings > 0)) {
fprintf(stderr, "%s: validate: %d error(s), %d warning(s)\n",
progname, g_errors, g_warnings);
return 1;
}
printf("validate: %s is in force, %d rule(s), %d warning(s)\n",
p->path, (int)p->nrules, g_warnings);
return 0;
}
|