-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathconfparse.c
820 lines (788 loc) · 23.6 KB
/
confparse.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
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
/************************************************************************
* Bahamut IRCd, src/confparse.c
* Copyright (C) 2004, Aaron Wiebe
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 1, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "struct.h"
#include "common.h"
#include "sys.h"
#include "h.h"
#include "userban.h"
#define CONF_TABS
#include "confparse.h"
#ifndef LINE_MAX
#define LINE_MAX 256
#endif
/* notes on confparse.c
* While this initial revision requires a fair bit of trimming down,
* my primary goal right now was to build an extendable system that will
* allow for fairly easy changes to the config file.
* Heres a few notes on how to go about that.
*
* The parser works on two primary depths - blocks and tokens:
*
* block {
* token value;
* token "string value";
* token 123; # int value
* token; # nonvar token
* };
*
* It can also parse non-token blocks:
*
* block (
* "string string string";
* "string blah";
* };
*
* Blocks are defined by tconftab (in confparse.h)
* Tokens are defined by sconftab (^^^^^^^^^^^^^^)
*
* Each block must have a function that takes the values collected
* and checks them against the requirements. These functions are also
* handy for getting variables out of the array that they are stored in.
*
* The array variables are placed in (an array of cVar structs) contains
* all values for the the block, and the corrisponding sconftab item.
*
* I feel the need to rewrite large sections of this still, but I'll just
* be happy to have it working for now.
*
* Feb 24/04
* -epi
*/
extern int forked;
extern char *set_classes(void);
extern aPort *new_ports;
extern Conf_Me *new_MeLine;
/* free_vars()
* clear our temp variable array used by parse_block and children
*/
static void
free_vars(cVar *vars[])
{
int i = 0;
while(vars[i])
{
MyFree(vars[i]->value);
MyFree(vars[i]);
i++;
}
}
/* error handler */
static char *current_file = "unknown";
void
confparse_error(char *problem, int line)
{
if(!forked)
printf("ERROR: %s near line %d of %s\n", problem, line, current_file);
else
{
sendto_realops("Conf Error: %s near line %d of %s", problem, line,
current_file);
#ifdef USE_SYSLOG
syslog(LOG_ERR, "Conf Error: %s near line %d of %s", problem, line,
current_file);
#endif
}
return;
}
/* check_quote
* this routine skips over any ignored items inside our file
*/
static int quote = 0;
static char *
check_quote(char *cur)
{
if(quote)
{
while((cur = strchr(cur, '*')))
if(*(++cur) == '/')
{
cur++;
quote = 0;
break;
}
if(!cur)
return cur;
}
while((*cur == ' ') || (*cur == '\t'))
cur++;
/* now we've hit something .. check for single line quotes */
if (!*cur || *cur == '#' || *cur == '\n' ||
(*cur == '/' && *(cur+1) == '/'))
return NULL;
/* check for multiple line quotes */
if((*cur == '/') && (*(cur+1) == '*'))
{
cur += 2;
quote = 1;
while((cur = strchr(cur, '*')))
if(*(++cur) == '/')
{
cur++;
quote = 0;
break;
}
if(!cur)
return cur;
else
return check_quote(cur);
}
return cur;
}
#define MAX_VALUES 128 /* maximum values per block */
static char *
parse_block(tConf *block, char *cur, FILE *file, int *lnum)
{
char *tok, *var, *var2;
char line[LINE_MAX];
tConf *b2 = NULL;
sConf *item = NULL;
sConf *sconftab = block->subtok;
cVar *vars[MAX_VALUES] = { 0 };
int vnum = 0, tlnum = 0, clear = 0, done = 0, skip = 0;
if((sconftab) && (sconftab->flag == SCONFF_STRING))
{
/* this subtype only takes freeform variables
* dont bother looking for tokens
*/
int i = 0;
while(!BadPtr(cur) || ((fgets(line, LINE_MAX, file) != NULL) &&
(*lnum)++ && (cur = line)))
{
cur = check_quote(cur);
if(BadPtr(cur))
continue;
if(clear)
{
if(*cur != ';')
{
confparse_error("Missing semicolon", *lnum);
free_vars(vars);
return NULL;
}
else
cur++;
clear = 0;
cur = check_quote(cur);
if(BadPtr(cur))
continue;
}
if(done)
{
if(*cur != ';')
{
confparse_error("Missing block end semicolon", *lnum);
free_vars(vars);
return NULL;
}
else
cur++;
if(((*block->func) (vars, *lnum)) == -1)
{
free_vars(vars);
return NULL;
}
if(BadPtr(cur))
*cur = '#'; /* we cant return a bad pointer because
* that will pull us out of the conf read
* so this will just get ignored
* kludgy, but effective */
free_vars(vars);
return cur;
}
cur = check_quote(cur);
if(BadPtr(cur))
continue;
if(*cur == '}')
{
done = 1;
cur++;
cur = check_quote(cur);
if(BadPtr(cur))
continue;
if(*cur != ';')
{
confparse_error("Missing block end semicolon", *lnum);
free_vars(vars);
return NULL;
}
else
cur++;
if(((*block->func) (vars, *lnum)) == -1)
{
free_vars(vars);
return NULL;
}
if(BadPtr(cur))
*cur = '#'; /* we cant return a bad pointer because
* that will pull us out of the conf read
* so this will just get ignored
* kludgy, but effective */
free_vars(vars);
return cur;
}
vars[vnum] = (cVar *) MyMalloc(sizeof(cVar));
memset((char *) vars[vnum], '\0', sizeof(cVar));
vars[vnum]->loaded = 1;
vars[vnum]->type = NULL;
tok = cur;
if(*cur == '"')
{
i = 1;
cur++;
}
var = cur;
if(i == 1)
{
while(!BadPtr(cur) && (*cur != '"'))
cur++;
if(BadPtr(cur))
{
confparse_error("Cant find closequote", *lnum);
free_vars(vars);
return NULL;
}
*cur = '\0';
cur++;
while(!BadPtr(cur) && (*cur != ';'))
cur++;
}
else
{
while(!BadPtr(cur) && (*cur != ';'))
{
if(*cur == ' ')
{
*cur = '\0';
if(vars[vnum]->loaded == 1)
{
DupString(vars[vnum]->value, var);
vars[vnum]->loaded = 2;
}
}
else if(vars[vnum]->loaded == 2)
{
confparse_error("Junk after value", *lnum);
free_vars(vars);
return NULL;
}
cur++;
}
}
tlnum = *lnum;
if(BadPtr(cur))
{
clear = 1;
continue;
}
*cur = '\0';
cur++;
if(vars[vnum]->loaded == 1)
DupString(vars[vnum]->value, var);
vars[vnum]->loaded = 3;
vnum++;
}
confparse_error("Unexpected EOF: Syntax Error", tlnum);
free_vars(vars);
return NULL;
}
while(!BadPtr(cur) || ((fgets(line, LINE_MAX, file) != NULL) && (*lnum)++
&& (cur = line)))
{
cur = check_quote(cur);
if(BadPtr(cur))
continue;
if(clear)
{
/* if we're looking for a closing semicolon, check for it first
* if we cant find it, ignore it and hope for the best
*/
if(*cur != ';')
{
confparse_error("Missing semicolon ", *lnum);
free_vars(vars);
return NULL;
}
else
cur++;
clear = 0;
if(vars[vnum])
{
vars[vnum]->loaded = 3;
vnum++;
}
item = NULL;
cur = check_quote(cur);
if(BadPtr(cur))
continue;
}
if(done)
{
/* we've found the end of our block, now we're looking for the
* closing semicolon. if we cant find it, ignore it and
* hope for the best
*/
if(*cur != ';')
{
confparse_error("Missing block end semicolon", *lnum);
free_vars(vars);
return NULL;
}
else
cur++;
if(((*block->func) (vars, *lnum)) == -1)
{
free_vars(vars);
return NULL;
}
if(BadPtr(cur))
*cur = '#'; /* we cant return a bad pointer because
* that will pull us out of the conf read
* so this will just get ignored
* kludgy, but effective */
free_vars(vars);
return cur;
}
if(b2 && b2->tok)
{
/* we've identified a nested block in a previous loop.
* we didnt get an openquote yet, so look for that.
* we must find this. keep looking til we do.
*/
if(*cur != '{')
{
confparse_error("Junk after nested block token", *lnum);
free_vars(vars);
return NULL;
}
cur++;
cur = check_quote(cur);
cur = parse_block(b2, cur, file, lnum);
b2 = NULL;
continue;
}
if(!item || !item->tok)
{
/* if we dont already have a specific token we're working on
* find one here.
*/
cur = check_quote(cur);
if(BadPtr(cur))
continue;
tok = cur;
tlnum = *lnum;
if(*cur == '}')
{
/* if we've got a closebracket, then we've hit the end
* of our block.
*/
done = 1;
cur++;
cur = check_quote(cur);
if(BadPtr(cur))
continue;
if(*cur != ';')
{
confparse_error("Missing block end semicolon", *lnum);
free_vars(vars);
return NULL;
}
else
cur++;
if(((*block->func) (vars, *lnum)) == -1)
{
free_vars(vars);
return NULL;
}
if(BadPtr(cur))
*cur = '#'; /* we cant return a bad pointer because
* that will pull us out of the conf read
* so this will just get ignored
* kludgy, but effective */
free_vars(vars);
return cur;
}
/* our token ends where whitespace or a semicolon begins */
while(!BadPtr(cur) && ((*cur != ' ') && (*cur != ';') &&
(*cur != '\t') && (*cur != '\n')))
cur++;
if(BadPtr(cur))
{
confparse_error("Unterminated token", *lnum);
free_vars(vars);
return NULL;
}
else
{
if(*cur == ';')
skip = 1;
*cur = '\0';
}
cur++;
if(block->nest)
{
/* we allow nested stuff inside here, so check for it. */
for(b2 = tconftab; b2->tok; b2++)
if(!mycmp(b2->tok, tok))
break;
if(b2 && b2->tok)
if(!(block->nest & b2->flag))
b2 = NULL;
if(b2 && b2->tok)
{
/* recurse through the block we found */
tlnum = *lnum;
cur = check_quote(cur);
if(BadPtr(cur))
continue;
if(*cur != '{')
{
confparse_error("Junk after nested block name", *lnum);
free_vars(vars);
return NULL;
}
cur++;
cur = check_quote(cur);
cur = parse_block(b2, cur, file, lnum);
if(!cur)
{
free_vars(vars);
return NULL;
}
b2 = NULL;
continue;
}
}
/* find our token */
for(item = sconftab; item && item->tok; item++)
if(!mycmp(item->tok, tok))
break;
if(!item->tok)
{
confparse_error("Unknown token", *lnum);
free_vars(vars);
return NULL;
}
/* create our variable */
vars[vnum] = (cVar *) MyMalloc(sizeof(cVar));
memset((char *) vars[vnum], '\0', sizeof(cVar));
vars[vnum]->type = item;
vars[vnum]->loaded = 1;
}
if(item->var & VARTYPE_NONE)
{
/* we dont need to grab a variable for this type
* just look for the closing semicolon, and move on */
vars[vnum]->loaded = 2;
if(!skip)
{
/* we've already gotten our semicolon back
* at the end of our token. dont look for it. */
cur = check_quote(cur);
while(!BadPtr(cur) && (*cur != ';'))
cur++;
if(BadPtr(cur))
{
clear = 1;
continue;
}
cur++;
}
skip = 0;
vars[vnum]->loaded = 3;
vnum++;
item = NULL;
continue;
}
if(item->var & VARTYPE_STRING)
{
/* we're looking for a string here, so we require
* quotes around the string...
*/
cur = check_quote(cur);
while(!BadPtr(cur) && (*cur != '"'))
cur++;
if(BadPtr(cur))
continue;
cur++;
var = cur;
while(!BadPtr(cur) && (*cur != '"'))
cur++;
if(BadPtr(cur))
{
confparse_error("Unterminated quote", *lnum);
free_vars(vars);
return NULL;
}
*cur = '\0';
cur++;
DupString(vars[vnum]->value, var);
vars[vnum]->loaded = 2;
while(!BadPtr(cur) && (*cur != ';'))
cur++;
if(BadPtr(cur))
{
clear = 1;
continue;
}
cur++;
vars[vnum]->loaded = 3;
vnum++;
item = NULL;
continue;
}
if(item->var & VARTYPE_INT)
{
cur = check_quote(cur);
var = cur;
while(!BadPtr(cur) && ((*cur != ';') && (*cur != '\t') &&
(*cur != '\n') && (*cur != ' ')))
cur++;
if(BadPtr(cur))
{
clear = 1;
continue;
}
if(*cur != ';')
clear = 1;
*cur = '\0';
cur++;
var2 = var;
while(*var)
{
if(IsDigit(*var))
var++;
else
{
confparse_error("Expecting integer value", *lnum);
free_vars(vars);
return NULL;
}
}
if(!item)
continue;
var = var2;
DupString(vars[vnum]->value, var);
vars[vnum]->loaded = 3;
vnum++;
item = NULL;
continue;
}
if(item->var & VARTYPE_NAME)
{
cur = check_quote(cur);
if(!BadPtr(cur) && (*cur == '"'))
cur++;
var = cur;
while(!BadPtr(cur) && (*cur != ';'))
{
if((*cur == ' ') || (*cur == '"') || (*cur == '\t'))
{
*cur = '\0';
if(vars[vnum]->loaded == 1)
{
DupString(vars[vnum]->value, var);
vars[vnum]->loaded = 2;
}
}
cur++;
}
if(BadPtr(cur))
{
clear = 1;
continue;
}
*cur = '\0';
cur++;
if(vars[vnum]->loaded == 1)
DupString(vars[vnum]->value, var);
vars[vnum]->loaded = 3;
vnum++;
item = NULL;
continue;
}
confparse_error("Unexpected EOF: Syntax Error", tlnum);
free_vars(vars);
return NULL;
}
confparse_error("Unexpected EOF: Syntax Error", tlnum);
free_vars(vars);
return NULL;
}
int
initconf(char *filename)
{
int lnum = 0, blnum = 0, clear = 0;
char line[LINE_MAX];
char *cur = NULL;
char *tok;
tConf *block = NULL;
FILE *file;
int including = 0;
current_file = filename;
if(!(file = fopen(filename, "r")))
{
if(forked)
{
sendto_realops("Unable to open config file %s", filename);
#ifdef USE_SYSLOG
syslog(LOG_ERR, "Unable to open config file %s", filename);
#endif
}
else
printf("Unable to open config file %s\n", filename);
return -1;
}
while(!BadPtr(cur) || ((fgets(line, LINE_MAX, file) != NULL) && ++lnum
&& (cur = line)))
{
cur = check_quote(cur);
if(BadPtr(cur))
continue;
if (including)
{
if (including == 1)
{
jmp_including:
if (*cur == '"' || *cur == '<')
cur++;
tok = cur;
while (*cur && *cur != ' ' && *cur != '\t' && *cur != '"'
&& *cur != '>' && *cur != ';' && *cur != '\n')
cur++;
if (*cur == ';')
including = 0;
else
including++;
*cur++ = 0;
if (!*tok)
{
confparse_error("Bad include filename", lnum);
fclose(file);
return -1;
}
/* parse new file */
if(initconf(tok) == -1)
{
current_file = filename;
confparse_error("while processing include directive",lnum);
fclose(file);
return -1;
}
/* reset */
current_file = filename;
cur = check_quote(cur);
if (BadPtr(cur))
continue;
}
if (including == 2)
{
if (*cur != ';')
{
confparse_error("Missing semicolon", lnum);
fclose(file);
return -1;
}
including = 0;
cur++;
cur = check_quote(cur);
if (BadPtr(cur))
continue;
}
}
/* now, we should be ok to get that token.. */
if(!block)
{
tok = cur;
while((*cur != ' ') && (*cur != '\n') && (*cur != '{'))
cur++; /* find the whitespace following the token */
if(*cur == '{')
clear = 1;
*cur = '\0';
cur++;
if (!mycmp("INCLUDE", tok))
{
if(clear)
{
confparse_error("Unexpected opening bracket", lnum);
fclose(file);
return -1;
}
including++;
cur = check_quote(cur);
if (BadPtr(cur))
continue;
goto jmp_including; /* XXX */
}
for(block = tconftab; block->tok; block++)
if(!mycmp(block->tok, tok))
break;
if(!block->tok)
{
confparse_error("Unknown block type", lnum);
fclose(file);
return -1;
}
blnum = lnum;
}
cur = check_quote(cur);
if(BadPtr(cur))
continue;
if((*cur == '{') || clear)
cur++;
else
{
confparse_error("Junk after block name", lnum);
fclose(file);
return -1;
}
if((cur = parse_block(block, cur, file, &lnum)) == NULL)
{
fclose(file);
return -1;
}
clear = 0;
block = NULL;
continue;
}
if(clear)
{
confparse_error("Unexpected EOF: Syntax error", blnum);
fclose(file);
return -1;
}
fclose(file);
return 1;
}
inline char *
finishconf(void)
{
static char buf[256];
char *ret;
if (!new_MeLine || !new_MeLine->servername)
return "Missing global block";
if ((ret = set_classes()))
{
ircsnprintf(buf, sizeof(buf), "Missing class block for referenced "
"class '%s'", ret);
return buf;
}
if (!new_ports)
return "No ports defined";
return NULL;
}