-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.l
517 lines (413 loc) · 11.4 KB
/
parser.l
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
507
508
509
510
511
512
513
514
515
516
517
%{
/* ################################################################### */
/* Copyright 2022, Pierre Gentile ([email protected]) */
/* */
/* This Source Code Form is subject to the terms of the Mozilla Public */
/* License, v. 2.0. If a copy of the MPL was not distributed with this */
/* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
/* ################################################################### */
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "comp.h"
#include "bst.h"
#include "list.h"
#include "types.h"
#include "xmalloc.h"
#include "log.h"
#include "strarray.h"
#include "conf.h"
#include "utils.h"
static char *cur_content;
static char sep = ':';
static ll_t *seen_param_list;
extern bst_node_t *rule_tree;
extern bst_node_t *var_tree;
var_t *var = NULL;
rule_t *rule = NULL;
param_t *param;
static void
manage_eol(void);
void
var_check_dependencies(void *node, void *arg);
void
expand_var_ref(char **str);
static void
process_old_var(void);
static void
merge_old_params(void);
static void
process_old_rule(void);
static param_t *
new_param(char *yytext, char sep);
%}
%s IN_RULE
%s IN_PARAM
%s IN_VAR
NAME [a-zA-Z_][a-zA-Z0-9_]*
CR [\n]
SPACE [ \t]
PREFIX [ \t]+
PARAM1 "uid"|"gid"|"paths"|"users"|"groups"|"netgroups"
PARAM2 "environment"|"password"|"help"|"umask"|"owners"|"disabled"
PARAM ({PARAM1}|{PARAM2})
CMD "cmd"
%%
<INITIAL>^{SPACE}*{CR} ; /* Ignore blank lines when in INITIAL state. */
^{SPACE}*#.*{CR} ; /* Ignores lines with a # as first non-blank character. */
/* Variable declaration. */
/* """"""""""""""""""""" */
<INITIAL,IN_VAR,IN_RULE>^("global"{SPACE}*)?"@"{NAME}{SPACE}*:.* {
char *equal_addr;
char *start;
manage_eol();
if (param)
merge_old_params();
process_old_var();
process_old_rule();
BEGIN(IN_VAR);
var = xmalloc(sizeof(var_t));
var->name = NULL;
var->value = NULL;
if (strncmp(yytext, "global ", 7) == 0 || strncmp(yytext, "global\t", 7) == 0)
{
start = yytext + 7;
var->global = 1;
}
else
{
start = yytext;
var->global = 0;
}
if ((equal_addr = strchr(start, ':')) == NULL)
trace(LOG_DATA,
"[%s] \"%s\": invalid variable definition.",
rule->tag,
yytext);
else
{
*equal_addr = '\0';
var->name = xstrdup(start + 1);
var->value = xstrdup(equal_addr + 1);
}
}
/* Continuation line in variables */
/* """""""""""""""""""""""""""""" */
<IN_VAR>^{PREFIX}>.* {
char * start;
manage_eol();
start = strchr(yytext, '>');
if (start == NULL)
trace(LOG_DATA, "[%s] malformed continuation line.", var->name);
if (var->value != NULL)
{
size_t l = strlen(var->value);
if (l > 0 && var->value[l - 1] == '\\')
{
var->value[l - 1] = '\0';
var->value = strappend(var->value, start + 1, NULL);
}
else
var->value = strappend(var->value, "\n", start + 1, NULL);
}
else
trace(LOG_DATA, "[%s] unexpected continuation line.", var->name);
}
/* Continuation line in commands. */
/* """""""""""""""""""""""""""""" */
<IN_RULE>^{PREFIX}>.* {
char *start;
manage_eol();
start = strchr(yytext, '>');
if (start == NULL)
trace(LOG_DATA, "[%s] malformed continuation line.", rule->tag);
if (cur_content != NULL)
{
size_t l = strlen(cur_content);
if (l > 0 && cur_content[l - 1] == '\\')
{
cur_content[l - 1] = '\0';
cur_content = strappend(cur_content, start + 1, NULL);
}
else
cur_content = strappend(cur_content, "\n", start + 1, NULL);
}
else
trace(LOG_DATA, "[%s] unexpected continuation line.", rule->tag);
}
/* Command declaration. */
/* """""""""""""""""""" */
<INITIAL,IN_VAR,IN_RULE>^({NAME}|\+[1-9][0-9]*){SPACE}* {
size_t tag_len;
manage_eol();
if (param)
merge_old_params();
process_old_var();
process_old_rule();
BEGIN(IN_RULE);
free(cur_content);
cur_content = NULL;
ll_free(seen_param_list, free);
rule = xmalloc(sizeof(rule_t));
tag_len = strcspn(yytext, " \t"); /* Remove trailing blanks in tag. */
yytext[tag_len] = '\0';
rule->tag = xstrdup(yytext);
rule->is_valid = 1;
rule->command = NULL;
rule->executable = NULL;
rule->param_list = ll_new();
}
/* Executable declaration in command. */
/* """""""""""""""""""""""""""""""""" */
<IN_RULE>^{PREFIX}{CMD}{SPACE}*:.* {
manage_eol();
if (param)
merge_old_params();
sep = ':';
param = new_param(yytext, sep);
}
/* Parameter declaration in command. */
/* """"""""""""""""""""""""""""""""" */
<IN_RULE>^{PREFIX}!?{PARAM}{SPACE}*:.* {
manage_eol();
if (param)
merge_old_params();
sep = ':';
param = new_param(yytext, sep);
if (param)
ll_append(rule->param_list, param);
}
/* Parameter declaration in command. */
/* """"""""""""""""""""""""""""""""" */
<IN_RULE>^{PREFIX}%{NAME}{SPACE}*:.* {
manage_eol();
if (param)
merge_old_params();
sep = ':';
param = new_param(yytext, sep);
if (param)
ll_append(rule->param_list, param);
}
/* Positional argument restriction in command. */
/* """"""""""""""""""""""""""""""""""""""""""" */
<IN_RULE>^{PREFIX}!?"$"([0-9.?]|([*+,;]?))[1-9]?{SPACE}*:.* {
manage_eol();
if (param)
merge_old_params();
sep = ':';
param = new_param(yytext, sep);
if (param)
ll_append(rule->param_list, param);
}
/* Environment variable affectation in command. */
/* """""""""""""""""""""""""""""""""""""""""""" */
<IN_RULE>^{PREFIX}!?"$"[^\n:]+{SPACE}*:.* {
manage_eol();
if (param)
merge_old_params();
sep = ':';
param = new_param(yytext, sep);
if (param)
ll_append(rule->param_list, param);
}
/* Command ending detection. */
/* """"""""""""""""""""""""" */
<IN_RULE>^{SPACE}* {
if (param)
merge_old_params();
cur_content = NULL;
BEGIN(INITIAL);
}
/* Variable ending detection. */
/* """""""""""""""""""""""""" */
<IN_VAR>^{SPACE}*{CR} {
process_old_var();
cur_content=NULL;
unput(yytext[yyleng - 1]);
BEGIN(INITIAL);
}
{CR} ; /* ignore spurious \n */
/* Default rule -> Error. */
/* """""""""""""""""""""" */
.* {
if (param)
merge_old_params();
if (rule)
{
if (!rule->is_valid)
trace(LOG_DATA,"[%s] \%s\": invalid rule, see previous message, "
"please contact your sys administrator.",
rule->tag, yytext);
else
{
trace(LOG_DATA,"[%s] \"%s\": syntax error, "
"please contact your sys administrator.",
rule->tag, yytext);
rule->is_valid = 0;
}
}
else
trace(LOG_DATA,"[%s] Configuration syntax error, "
"please contact your sys administrator.",
yytext);
cur_content=NULL;
BEGIN(INITIAL);
}
%%
/* ============================================================== */
/* Function automatically launched at the end of each parsed file.*/
/* ============================================================== */
int
yywrap(void)
{
if (param)
merge_old_params();
free(cur_content);
cur_content = NULL;
return 1;
}
void
manage_eol(void)
{
int c;
c = input();
if (c > 0 && c != '\n')
unput(c);
}
/* ===================== */
/* parser main function. */
/* ===================== */
void
parse(FILE *file)
{
seen_param_list = ll_new();
yyin = file;
yylex();
process_old_var();
process_old_rule();
ll_destroy(seen_param_list, free);
}
/* ================================================================== */
/* Finalize a new variable by expanding embedded variables references */
/* and add it in the variables tree. */
/* ================================================================== */
static void
process_old_var(void)
{
if (var != NULL)
{
size_t l = strlen(var->value);
if (l > 0 && var->value[l - 1] == '\\')
var->value[l - 1] = '\0';
expand_var_ref(&var->value);
var_tree = bst_insert(var_tree, var, item_comp, NULL);
var = NULL;
}
}
/* ================================================================= */
/* Build a parameter by merging its initial declaration will all its */
/* continuation lines. */
/* ================================================================= */
static void
merge_old_params(void)
{
char **array;
int n, nb;
if (cur_content)
{
size_t l;
l = strlen(cur_content);
if (l > 0 && cur_content[l - 1] == '\\')
cur_content[l - 1] = '\0';
if (strcmp(param->name, "cmd") == 0)
{
rule->command = xstrdup(cur_content);
l = strcspn(cur_content, " \t");
rule->executable = xstrndup(cur_content, l);
}
else
{
strarray(cur_content, &array, &nb, ",;", MERGESAMESEPS | KEEPQUOTES);
for (n = 0; n < nb; n++)
ll_append(param->val_list, array[n]);
}
}
}
/* ============================================================== */
/* Finalize a new rule by expanding embedded variables references */
/* and add it in the rule tree. */
/* ============================================================== */
static void
process_old_rule(void)
{
param_t *param;
char *val;
if (rule)
{
ll_node_t *param_node = rule->param_list->head;
if (!rule->is_valid || rule->command == NULL || rule->executable == NULL)
{
/* And add the special invalid parameter to invalid rules. */
/* """"""""""""""""""""""""""""""""""""""""""""""""""""""" */
param = xmalloc(sizeof(param_t));
param->name = xstrdup("invalid");
param->val_list = ll_new();
ll_append(rule->param_list, param);
}
else
/* Normal treatment for valid rules. */
/* """"""""""""""""""""""""""""""""" */
while (param_node)
{
ll_node_t *val_node;
param = (param_t *)(param_node->data);
val_node = param->val_list->head;
while (val_node)
{
val = (char *)(val_node->data);
expand_var_ref(&val);
val_node->data = val;
val_node = val_node->next;
}
param_node = param_node->next;
}
rule_tree = bst_insert(rule_tree, rule, item_comp, NULL);
rule = NULL;
}
}
/* ========================================== */
/* Initiates the creation of a new parameter. */
/* ========================================== */
static param_t *
new_param(char *yytext, char sep)
{
char *end;
char *name;
ltrim(yytext, " \t");
end = strchr(yytext, sep);
if (!rule->is_valid)
return NULL;
name = xstrndup(yytext, end - yytext);
/* Check if the parameter with the same name has already been seen */
/* for this rule. */
/* """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
if (!ll_find(seen_param_list, name, str_comp))
{
ll_prepend(seen_param_list, xstrdup(name));
param = xmalloc(sizeof(param_t));
param->name = name;
param->val_list = ll_new();
free(cur_content);
cur_content = NULL;
cur_content = xstrdup(end + 1);
return param;
}
else
{
trace(LOG_DATA, "[%s] \"%s\": duplicate entry.", rule->tag, name);
free(name);
rule->is_valid = 0;
return NULL;
}
}