/
libmavis_groups.c
480 lines (444 loc) · 10.2 KB
/
libmavis_groups.c
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
/*
* libmavis_groups.c
* (C)2011-2022 by Marc Huber <Marc.Huber@web.de>
* All rights reserved.
*
* $Id$
*
*/
#define MAVIS_name "groups"
#include "misc/sysconf.h"
#include <stdio.h>
#include <grp.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <grp.h>
#include <errno.h>
#include <unistd.h>
#include <dlfcn.h>
#include "misc/strops.h"
#include "misc/io.h"
#include "groups.h"
#include "misc/memops.h"
#include "debug.h"
#include "log.h"
#ifdef WITH_PCRE2
#include <pcre2.h>
#endif
#include <regex.h>
static const char rcsid[] __attribute__((used)) = "$Id$";
struct regex_list;
#define MAVIS_CTX_PRIVATE int resolve_gid; \
int resolve_gids; \
int resolve_gid_attr; \
int resolve_gids_attr; \
struct regex_list *group_regex; \
struct regex_list *groups_regex; \
struct regex_list *memberof_regex; \
struct gid_list *gid; \
struct gid_list *gids;
#include "mavis.h"
struct regex_list {
struct regex_list *next;
int negate;
enum token type;
void *p;
};
struct gid_list {
struct gid_list *next;
int negate;
gid_t gid_start;
gid_t gid_end;
};
static void parse_filter_regex(struct sym *sym, struct regex_list **l)
{
int negate = 0;
if (sym->code == S_not) {
negate = 1;
sym_get(sym);
}
do {
int errcode = 0;
while (*l)
l = &(*l)->next;
*l = Xcalloc(1, sizeof(struct regex_list));
(*l)->negate = negate;
if (sym->code == S_slash) {
#ifdef WITH_PCRE2
PCRE2_SIZE erroffset;
(*l)->type = S_slash;
(*l)->p = (void *)
pcre2_compile((PCRE2_SPTR8) sym->buf, PCRE2_ZERO_TERMINATED, PCRE2_MULTILINE | common_data.regex_pcre_flags, &errcode, &erroffset, NULL);
if (!(*l)->p) {
PCRE2_UCHAR buffer[256];
pcre2_get_error_message(errcode, buffer, sizeof(buffer));
parse_error(sym, "In PCRE2 expression /%s/ at offset %d: %s", sym->buf, erroffset, buffer);
}
#else
parse_error(sym, "You're using PCRE2 syntax, but this binary wasn't compiled with PCRE2 support.");
#endif
} else {
(*l)->type = S_regex;
(*l)->p = Xcalloc(1, sizeof(regex_t));
errcode = regcomp((regex_t *) (*l)->p, sym->buf, REG_EXTENDED | REG_NOSUB | REG_NEWLINE | common_data.regex_posix_flags);
if (errcode) {
char e[160];
regerror(errcode, (regex_t *) (*l)->p, e, sizeof(e));
parse_error(sym, "In regular expression '%s': %s", sym->buf, e);
}
}
sym_get(sym);
} while (parse_comma(sym));
}
static void parse_gid(struct sym *sym, struct gid_list **l)
{
int negate = 0;
if (sym->code == S_not) {
negate = 1;
sym_get(sym);
}
do {
u_int gs, ge;
while (*l)
l = &(*l)->next;
*l = Xcalloc(1, sizeof(struct gid_list));
(*l)->negate = negate;
switch (sscanf(sym->buf, "%u-%u", &gs, &ge)) {
case 1:
ge = gs;
case 2:
break;
default:
parse_error(sym, "Expected numeric GID or GID range, but got \"%s\"", sym->buf);
}
(*l)->gid_start = gs;
(*l)->gid_end = ge;
sym_get(sym);
} while (parse_comma(sym));
}
#define HAVE_mavis_parse_in
static int mavis_parse_in(mavis_ctx * mcx, struct sym *sym)
{
mcx->resolve_gid_attr = AV_A_GID;
mcx->resolve_gids_attr = AV_A_GIDS;
while (1) {
switch (sym->code) {
case S_script:
mavis_script_parse(mcx, sym);
continue;
case S_resolve:
// resolve gid = (yes|no)
// resolve gid attribute = <attr>
sym_get(sym);
switch (sym->code) {
case S_gid:
sym_get(sym);
switch (sym->code) {
case S_attr:
sym_get(sym);
parse(sym, S_equal);
mcx->resolve_gid_attr = av_attribute_to_i(sym->buf);
if (mcx->resolve_gid_attr < 0)
parse_error(sym, "'%s' is not a recognized attribute", sym->buf);
sym_get(sym);
continue;
case S_equal:
parse(sym, S_equal);
mcx->resolve_gid = parse_bool(sym);
continue;
default:
parse_error_expect(sym, S_attr, S_equal, S_unknown);
}
case S_gids:
// resolve gids = (yes|no)
// resolve gids attribute = <attr>
sym_get(sym);
switch (sym->code) {
case S_attr:
sym_get(sym);
parse(sym, S_equal);
mcx->resolve_gids_attr = av_attribute_to_i(sym->buf);
if (mcx->resolve_gids_attr < 0)
parse_error(sym, "'%s' is not a recognized attribute", sym->buf);
sym_get(sym);
continue;
case S_equal:
parse(sym, S_equal);
mcx->resolve_gids = parse_bool(sym);
continue;
default:
parse_error_expect(sym, S_attr, S_equal, S_unknown);
}
default:
parse_error_expect(sym, S_gid, S_gids, S_unknown);
}
case S_group:
sym_get(sym);
parse(sym, S_filter);
sym->flag_parse_pcre = 1;
parse(sym, S_equal);
// group filter = [not] regex ...
parse_filter_regex(sym, &mcx->group_regex);
sym->flag_parse_pcre = 0;
continue;
case S_groups:
sym_get(sym);
parse(sym, S_filter);
sym->flag_parse_pcre = 1;
parse(sym, S_equal);
// groups filter = [not] regex ...
parse_filter_regex(sym, &mcx->groups_regex);
sym->flag_parse_pcre = 0;
continue;
case S_memberof:
sym_get(sym);
parse(sym, S_filter);
sym->flag_parse_pcre = 1;
parse(sym, S_equal);
// memberof filter = [not] regex ...
parse_filter_regex(sym, &mcx->memberof_regex);
sym->flag_parse_pcre = 0;
continue;
case S_gid:
sym_get(sym);
parse(sym, S_filter);
parse(sym, S_equal);
// gid filter = [not] <gid>[-<gid>][,<gid>[-<gid>]]+
parse_gid(sym, &mcx->gid);
continue;
case S_gids:
sym_get(sym);
parse(sym, S_filter);
parse(sym, S_equal);
// gids filter = [not] <gid>[-<gid>][,<gid>[-<gid>]]+
parse_gid(sym, &mcx->gids);
continue;
case S_eof:
case S_closebra:
return MAVIS_CONF_OK;
case S_action:
mavis_module_parse_action(mcx, sym);
continue;
default:
parse_error_expect(sym, S_resolve, S_script, S_group, S_groups, S_memberof, S_gid, S_gids, S_action, S_unknown);
}
}
}
static int good_gid(struct gid_list *l, u_long gid)
{
if (!l)
return -1;
while (l) {
int match = l->gid_start <= (gid_t) gid && l->gid_end >= (gid_t) gid;
if (l->negate)
match = !match;
if (match)
return match;
l = l->next;
}
return 0;
}
static int rxmatch(void *v, char *s, enum token token)
{
#ifdef WITH_PCRE2
int pcre_res = 0;
pcre2_match_data *match_data = NULL;
#endif
switch (token) {
case S_slash:
#ifdef WITH_PCRE2
match_data = pcre2_match_data_create_from_pattern((pcre2_code *) v, NULL);
pcre_res = pcre2_match((pcre2_code *) v, (PCRE2_SPTR8) s, PCRE2_ZERO_TERMINATED, 0, 0, match_data, NULL);
if (pcre_res < 0 && pcre_res != PCRE2_ERROR_NOMATCH) {
report_cfg_error(LOG_INFO, ~0, "PCRE2 matching error: %d", pcre_res);
}
pcre2_match_data_free(match_data);
return -1 < pcre_res;
#endif
default:
return !regexec((regex_t *) v, s, 0, NULL, 0);
}
}
static int good_name(struct regex_list *l, char *s)
{
if (!l)
return -1;
while (l) {
int match = rxmatch(l->p, s, l->type);
if (l->negate)
match = !match;
if (match)
return match;
l = l->next;
}
return 0;
}
#define HAVE_mavis_recv_out
static int mavis_recv_out(mavis_ctx * mcx, av_ctx ** ac)
{
char *s = NULL;
if (mcx->resolve_gid) {
s = av_get(*ac, AV_A_GID);
if (s) {
u_long u = strtoul(s, NULL, 10);
if (u || errno != EINVAL) {
struct group *g;
if (!good_gid(mcx->gid, u)) {
av_unset(*ac, AV_A_GID);
} else {
g = getgrgid((gid_t) u);
if (g && good_name(mcx->group_regex, g->gr_name))
av_set(*ac, mcx->resolve_gid_attr, g->gr_name);
else
av_unset(*ac, mcx->resolve_gid_attr);
}
} else
av_unset(*ac, mcx->resolve_gid_attr);
}
}
if (mcx->resolve_gids) {
s = av_get(*ac, AV_A_GIDS);
if (s) {
char b[8192];
char *p = b;
*p = 0;
while (*s)
if (isdigit((int) *s)) {
u_long u = strtoul(s, &s, 10);
if (u || errno != EINVAL) {
struct group *g;
if (!good_gid(mcx->gids, u))
continue;
g = getgrgid((gid_t) u);
if (g && good_name(mcx->groups_regex, g->gr_name)) {
ssize_t l;
l = strlen(g->gr_name);
if (b + sizeof(b) - p - 2 > l) {
if (b[0])
*p++ = ',';
strcpy(p, g->gr_name);
p += l;
}
}
}
} else
s++;
if (b[0])
av_set(*ac, mcx->resolve_gids_attr, b);
else
av_unset(*ac, mcx->resolve_gids_attr);
}
}
s = av_get(*ac, AV_A_TACMEMBER);
if (s) {
size_t len = strlen(s) + 1;
char *v = alloca(len);
char *b = alloca(len);
char *p = b;
*p = 0;
memcpy(v, s, len);
while (*v) {
char *e;
int quoted = (*v == '"');
if (quoted) {
v++;
for (e = v; *e && *e != '"'; e++);
*e++ = 0;
if (*e == ',')
e++;
else if (*e)
break;
} else {
for (e = v; *e && *e != ','; e++);
if (*e)
*e++ = 0;
}
if (good_name(mcx->groups_regex, v)) {
if (*b)
*p++ = ',';
if (quoted)
*p++ = '"';
len = strlen(v);
memcpy(p, v, len);
p += len;
if (quoted)
*p++ = '"';
*p = 0;
}
v = e;
}
if (b[0])
av_set(*ac, AV_A_TACMEMBER, b);
else
av_unset(*ac, AV_A_TACMEMBER);
}
s = av_get(*ac, AV_A_MEMBEROF);
if (s) {
size_t len = strlen(s) + 1;
char *v = alloca(len);
char *b = alloca(len);
char *p = b;
*p = 0;
memcpy(v, s, len);
while (*v) {
char *e;
if (*v != '"')
break;
v++;
for (e = v; *e && *e != '"'; e++);
if (*e != '"')
break;
*e++ = 0;
if (*e == ',')
e++;
if (good_name(mcx->memberof_regex, v)) {
if (*b)
*p++ = ',';
*p++ = '"';
len = strlen(v);
memcpy(p, v, len);
p += len;
*p++ = '"';
*p = 0;
}
v = e;
}
if (b[0])
av_set(*ac, AV_A_MEMBEROF, b);
else
av_unset(*ac, AV_A_MEMBEROF);
}
return MAVIS_FINAL;
}
static void drop_gr(struct regex_list *r)
{
while (r) {
struct regex_list *l = r->next;
#ifdef WITH_PCRE2
if (r->type == S_slash)
pcre2_code_free(r->p);
else
#endif
regfree(r->p);
free(r);
r = l;
}
}
static void drop_gl(struct gid_list *r)
{
while (r) {
struct gid_list *l = r->next;
free(r);
r = l;
}
}
#define HAVE_mavis_drop_in
static void mavis_drop_in(mavis_ctx * mcx)
{
drop_gr(mcx->group_regex);
drop_gr(mcx->groups_regex);
drop_gl(mcx->gid);
drop_gl(mcx->gids);
}
#include "mavis_glue.c"