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
|
// Allocation that stops rather than limps: a hook that cannot allocate is in no
// position to decide anything, so these exit 3 instead of returning NULL
// somewhere a caller forgot to look.
#include "authz.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
const char *progname = "git-authz";
static void oom(void)
{
fprintf(stderr, "%s: out of memory\n", progname);
exit(3);
}
void sb_init(strbuf_t *b)
{
b->cap = 64;
b->len = 0;
b->s = xmalloc(b->cap);
b->s[0] = '\0';
}
static void sb_room(strbuf_t *b, size_t extra)
{
if (b->len + extra + 1 <= b->cap)
return;
while (b->len + extra + 1 > b->cap)
b->cap *= 2;
b->s = xrealloc(b->s, b->cap);
}
void sb_add(strbuf_t *b, const char *s)
{
size_t n;
if (s == NULL)
return;
n = strlen(s);
sb_room(b, n);
memcpy(b->s + b->len, s, n + 1);
b->len += n;
}
void sb_addf(strbuf_t *b, const char *fmt, ...)
{
va_list ap;
int n;
char *out;
va_start(ap, fmt);
n = vsnprintf(NULL, 0, fmt, ap);
va_end(ap);
if (n < 0)
return;
sb_room(b, (size_t)n);
va_start(ap, fmt);
out = b->s + b->len;
n = vsnprintf(out, b->cap - b->len, fmt, ap);
va_end(ap);
if (n < 0) {
b->s[b->len] = '\0';
return;
}
b->len += (size_t)n;
}
void sb_free(strbuf_t *b)
{
free(b->s);
b->s = NULL;
b->len = b->cap = 0;
}
void *xmalloc(size_t n)
{
void *p = malloc(n != 0 ? n : 1);
if (p == NULL)
oom();
return p;
}
void *xrealloc(void *ptr, size_t n)
{
void *p = realloc(ptr, n != 0 ? n : 1);
if (p == NULL)
oom();
return p;
}
char *xstrdup(const char *s)
{
size_t n;
char *p;
if (s == NULL)
return NULL;
n = strlen(s) + 1;
p = xmalloc(n);
memcpy(p, s, n);
return p;
}
char *xsubstr(const char *s, size_t len)
{
char *p = xmalloc(len + 1);
memcpy(p, s, len);
p[len] = '\0';
return p;
}
// The two things every caller of getopt_long otherwise writes by hand.
const char *authz_optname(int val, const struct option *lo)
{
for (; lo->name != NULL; lo++) {
if (lo->val == val)
return lo->name;
}
return "?";
}
// optind = 1 is how a second getopt_long call is told to begin again; opterr = 0
// is so the messages come out in this program's words and not glibc's.
void authz_opt_reset(void)
{
optind = 1;
opterr = 0;
}
// The two complaints every getopt_long loop otherwise writes by hand. Neither
// returns anything: the caller knows whether its own failure is 64 or -1.
void authz_opt_need_value(const struct option *lo)
{
fprintf(stderr, "%s: --%s wants a value\n", progname, authz_optname(optopt, lo));
}
void authz_opt_unknown(const char *cmd, const char *arg)
{
fprintf(stderr, "%s: %s: unknown argument %s\n", progname, cmd, arg);
}
// ----------------------------------------------------------------- lines
void lines_init(lines_t *l)
{
l->v = NULL;
l->n = 0;
l->cap = 0;
}
void lines_push(lines_t *l, char *s) // takes ownership
{
if (l->n == l->cap) {
l->cap = l->cap != 0 ? l->cap * 2 : 64;
l->v = xrealloc(l->v, l->cap * sizeof(*l->v));
}
l->v[l->n++] = s;
}
void lines_free(lines_t *l)
{
size_t i;
for (i = 0; i < l->n; i++)
free(l->v[i]);
free(l->v);
lines_init(l);
}
// Every reader in this program used to write this loop, its teardown and its
// "skip the blank and comment lines" preamble again by hand, and a reader that
// forgot one of the three leaked quietly.
int authz_read_lines(const char *path, lines_t *l)
{
FILE *f;
char *line = NULL;
size_t cap = 0;
ssize_t len;
lines_init(l);
f = fopen(path, "r");
if (f == NULL)
return -1;
while ((len = getline(&line, &cap, f)) != -1) {
size_t n = strlen(line);
if (n == 0 || line[n - 1] == '\n') {
lines_push(l, xstrdup(line));
continue;
}
{ // a last line without its newline
char *own = xmalloc(n + 2);
memcpy(own, line, n);
own[n] = '\n';
own[n + 1] = '\0';
lines_push(l, own);
}
}
free(line);
if (ferror(f)) {
fclose(f);
return -2;
}
fclose(f);
return 0;
}
|