Debian Codegraph @

runtime/cfsysline.c

raw
1 /* cfsysline.c
2 * Implementation of the configuration system line object.
3 *
4 * File begun on 2007-07-30 by RGerhards
5 *
6 * Copyright (C) 2007-2020 Adiscon GmbH.
7 *
8 * This file is part of rsyslog.
9 *
10 * This file is part of the rsyslog runtime library.
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 * -or-
18 * see COPYING.ASL20 in the source distribution
19 *
20 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS,
22 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the License for the specific language governing permissions and
24 * limitations under the License.
25 */
26 #include "config.h"
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <assert.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <ctype.h>
34 #include <pwd.h>
35 #include <grp.h>
36
37 #include "rsyslog.h"
38 #include "cfsysline.h"
39 #include "obj.h"
40 #include "conf.h"
41 #include "errmsg.h"
42 #include "srUtils.h"
43 #include "unicode-helper.h"
44 #include "rsconf.h"
45 #include "parserif.h"
46
47
48 /* static data */
49 DEFobjCurrIf(obj)
50
51 linkedList_t llCmdList; /* this is NOT a pointer - no typo here ;) */
52
53 /* --------------- START functions for handling canned syntaxes --------------- */
54
55
56 /* parse a character from the config line
57 * added 2007-07-17 by rgerhards
58 * TODO: enhance this function to handle different classes of characters
59 * HINT: check if char is ' and, if so, use 'c' where c may also be things
60 * like \t etc.
61 */
62 static rsRetVal doGetChar(uchar **pp, rsRetVal (*pSetHdlr)(void *, uchar *), void *pVal) {
63 DEFiRet;
64
65 assert(pp != NULL);
66 assert(*pp != NULL);
67
68 skipWhiteSpace(pp); /* skip over any whitespace */
69
70 /* if we are not at a '\0', we have our new char - no validity checks here... */
71 if (**pp == '\0') {
72 LogError(0, RS_RET_NOT_FOUND, "No character available");
73 iRet = RS_RET_NOT_FOUND;
74 } else {
75 if (pSetHdlr == NULL) {
76 /* we should set value directly to var */
77 *((uchar *)pVal) = **pp;
78 } else {
79 /* we set value via a set function */
80 CHKiRet(pSetHdlr(pVal, *pp));
81 }
82 ++(*pp); /* eat processed char */
83 }
84
85 finalize_it:
86 RETiRet;
87 }
88
89
90 /* Parse a number from the configuration line. This is more or less
91 * a shell to call the custom handler.
92 * rgerhards, 2007-07-31
93 */
94 static rsRetVal doCustomHdlr(uchar **pp, rsRetVal (*pSetHdlr)(uchar **, void *), void *pVal) {
95 DEFiRet;
96
97 assert(pp != NULL);
98 assert(*pp != NULL);
99
100 CHKiRet(pSetHdlr(pp, pVal));
101
102 finalize_it:
103 RETiRet;
104 }
105
106
107 /* Parse a number from the configuration line. This functions just parses
108 * the number and does NOT call any handlers or set any values. It is just
109 * for INTERNAL USE by other parse functions!
110 * rgerhards, 2008-01-08
111 */
112 static rsRetVal parseIntVal(uchar **pp, int64 *pVal) {
113 DEFiRet;
114 uchar *p;
115 int64 i;
116 int bWasNegative;
117
118 assert(pp != NULL);
119 assert(*pp != NULL);
120 assert(pVal != NULL);
121
122 skipWhiteSpace(pp); /* skip over any whitespace */
123 p = *pp;
124
125 if (*p == '-') {
126 bWasNegative = 1;
127 ++p; /* eat it */
128 } else {
129 bWasNegative = 0;
130 }
131
132 if (!isdigit((int)*p)) {
133 errno = 0;
134 LogError(0, RS_RET_INVALID_INT, "invalid number");
135 ABORT_FINALIZE(RS_RET_INVALID_INT);
136 }
137
138 /* pull value */
139 for (i = 0; *p && (isdigit((int)*p) || *p == '.' || *p == ','); ++p) {
140 if (isdigit((int)*p)) {
141 i = i * 10 + *p - '0';
142 }
143 }
144
145 if (bWasNegative) i *= -1;
146
147 *pVal = i;
148 *pp = p;
149
150 finalize_it:
151 RETiRet;
152 }
153
154
155 /* Parse a size from the configuration line. This is basically an integer
156 * syntax, but modifiers may be added after the integer (e.g. 1k to mean
157 * 1024). The size must immediately follow the number. Note that the
158 * param value must be int64!
159 * rgerhards, 2008-01-09
160 */
161 static rsRetVal doGetSize(uchar **pp, rsRetVal (*pSetHdlr)(void *, int64), void *pVal) {
162 DEFiRet;
163 int64 i;
164
165 assert(pp != NULL);
166 assert(*pp != NULL);
167
168 CHKiRet(parseIntVal(pp, &i));
169
170 /* we now check if the next character is one of our known modifiers.
171 * If so, we accept it as such. If not, we leave it alone. tera and
172 * above does not make any sense as that is above a 32-bit int value.
173 */
174 switch (**pp) {
175 /* traditional binary-based definitions */
176 case 'k':
177 i *= 1024;
178 ++(*pp);
179 break;
180 case 'm':
181 i *= 1024 * 1024;
182 ++(*pp);
183 break;
184 case 'g':
185 i *= 1024 * 1024 * 1024;
186 ++(*pp);
187 break;
188 case 't':
189 i *= (int64)1024 * 1024 * 1024 * 1024;
190 ++(*pp);
191 break; /* tera */
192 case 'p':
193 i *= (int64)1024 * 1024 * 1024 * 1024 * 1024;
194 ++(*pp);
195 break; /* peta */
196 case 'e':
197 i *= (int64)1024 * 1024 * 1024 * 1024 * 1024 * 1024;
198 ++(*pp);
199 break; /* exa */
200 /* and now the "new" 1000-based definitions */
201 case 'K':
202 i *= 1000;
203 ++(*pp);
204 break;
205 case 'M':
206 i *= 1000000;
207 ++(*pp);
208 break;
209 case 'G':
210 i *= 1000000000;
211 ++(*pp);
212 break;
213 /* we need to use the multiplication below because otherwise
214 * the compiler gets an error during constant parsing */
215 case 'T':
216 i *= (int64)1000 * 1000000000;
217 ++(*pp);
218 break; /* tera */
219 case 'P':
220 i *= (int64)1000000 * 1000000000;
221 ++(*pp);
222 break; /* peta */
223 case 'E':
224 i *= (int64)1000000000 * 1000000000;
225 ++(*pp);
226 break; /* exa */
227 default:
228 // No action needed for other cases
229 break;
230 }
231
232 /* done */
233 if (pSetHdlr == NULL) {
234 /* we should set value directly to var */
235 *((int64 *)pVal) = i;
236 } else {
237 /* we set value via a set function */
238 CHKiRet(pSetHdlr(pVal, i));
239 }
240
241 finalize_it:
242 RETiRet;
243 }
244
245
246 /* Parse a number from the configuration line.
247 * rgerhards, 2007-07-31
248 */
249 static rsRetVal doGetInt(uchar **pp, rsRetVal (*pSetHdlr)(void *, uid_t), void *pVal) {
250 uchar *p;
251 DEFiRet;
252 int64 i;
253
254 assert(pp != NULL);
255 assert(*pp != NULL);
256
257 CHKiRet(doGetSize(pp, NULL, &i));
258 p = *pp;
259 if (i > 2147483648ll) { /*2^31*/
260 LogError(0, RS_RET_INVALID_VALUE, "value %lld too large for integer argument.", i);
261 ABORT_FINALIZE(RS_RET_INVALID_VALUE);
262 }
263
264 if (pSetHdlr == NULL) {
265 /* we should set value directly to var */
266 *((int *)pVal) = (int)i;
267 } else {
268 /* we set value via a set function */
269 CHKiRet(pSetHdlr(pVal, (int)i));
270 }
271
272 *pp = p;
273
274 finalize_it:
275 RETiRet;
276 }
277
278
279 static rsRetVal doGetNonNegInt(uchar **pp, rsRetVal (*pSetHdlr)(void *, uid_t), void *pVal) {
280 int val;
281 DEFiRet;
282
283 CHKiRet(doGetInt(pp, NULL, &val));
284 if (val < 0) {
285 LogError(0, RS_RET_INVALID_VALUE, "value %d must not be less than zero.", val);
286 ABORT_FINALIZE(RS_RET_INVALID_VALUE);
287 }
288
289 if (pSetHdlr == NULL) {
290 *((int *)pVal) = val;
291 } else {
292 CHKiRet(pSetHdlr(pVal, val));
293 }
294
295 finalize_it:
296 RETiRet;
297 }
298
299
300 static rsRetVal doGetPositiveInt(uchar **pp, rsRetVal (*pSetHdlr)(void *, uid_t), void *pVal) {
301 int val;
302 DEFiRet;
303
304 CHKiRet(doGetInt(pp, NULL, &val));
305 if (val < 1) {
306 LogError(0, RS_RET_INVALID_VALUE, "value %d must be greater than zero.", val);
307 ABORT_FINALIZE(RS_RET_INVALID_VALUE);
308 }
309
310 if (pSetHdlr == NULL) {
311 *((int *)pVal) = val;
312 } else {
313 CHKiRet(pSetHdlr(pVal, val));
314 }
315
316 finalize_it:
317 RETiRet;
318 }
319
320
321 /* Parse and interpret a $FileCreateMode and $umask line. This function
322 * pulls the creation mode and, if successful, stores it
323 * into the global variable so that the rest of rsyslogd
324 * opens files with that mode. Any previous value will be
325 * overwritten.
326 * HINT: if we store the creation mode in selector_t, we
327 * can even specify multiple modes simply be virtue of
328 * being placed in the right section of rsyslog.conf
329 * rgerhards, 2007-07-4 (happy independence day to my US friends!)
330 * Parameter **pp has a pointer to the current config line.
331 * On exit, it will be updated to the processed position.
332 */
333 static rsRetVal doFileCreateMode(uchar **pp, rsRetVal (*pSetHdlr)(void *, uid_t), void *pVal) {
334 uchar *p;
335 DEFiRet;
336 int iVal;
337
338 assert(pp != NULL);
339 assert(*pp != NULL);
340
341 skipWhiteSpace(pp); /* skip over any whitespace */
342 p = *pp;
343
344 /* for now, we parse and accept only octal numbers
345 * Sequence of tests is important, we are using boolean shortcuts
346 * to avoid addressing invalid memory!
347 */
348 if (!((*p == '0') && (*(p + 1) && *(p + 1) >= '0' && *(p + 1) <= '7') &&
349 (*(p + 2) && *(p + 2) >= '0' && *(p + 2) <= '7') && (*(p + 3) && *(p + 3) >= '0' && *(p + 3) <= '7'))) {
350 LogError(0, RS_RET_INVALID_VALUE, "value must be octal (e.g 0644).");
351 ABORT_FINALIZE(RS_RET_INVALID_VALUE);
352 }
353
354 /* we reach this code only if the octal number is ok - so we can now
355 * compute the value.
356 */
357 iVal = (*(p + 1) - '0') * 64 + (*(p + 2) - '0') * 8 + (*(p + 3) - '0');
358
359 if (pSetHdlr == NULL) {
360 /* we should set value directly to var */
361 *((int *)pVal) = iVal;
362 } else {
363 /* we set value via a set function */
364 CHKiRet(pSetHdlr(pVal, iVal));
365 }
366
367 p += 4; /* eat the octal number */
368 *pp = p;
369
370 finalize_it:
371 RETiRet;
372 }
373
374
375 /* Parse and interpret an on/off inside a config file line. This is most
376 * often used for boolean options, but of course it may also be used
377 * for other things. The passed-in pointer is updated to point to
378 * the first unparsed character on exit. Function emits error messages
379 * if the value is neither on or off. It returns 0 if the option is off,
380 * 1 if it is on and another value if there was an error.
381 * rgerhards, 2007-07-15
382 */
383 static int doParseOnOffOption(uchar **pp) {
384 uchar *pOptStart;
385 uchar szOpt[32];
386
387 assert(pp != NULL);
388 assert(*pp != NULL);
389
390 pOptStart = *pp;
391 skipWhiteSpace(pp); /* skip over any whitespace */
392
393 if (getSubString(pp, (char *)szOpt, sizeof(szOpt), ' ') != 0) {
394 LogError(0, NO_ERRCODE, "Invalid $-configline - could not extract on/off option");
395 return -1;
396 }
397
398 if (!strcmp((char *)szOpt, "on")) {
399 return 1;
400 } else if (!strcmp((char *)szOpt, "off")) {
401 return 0;
402 } else {
403 LogError(0, NO_ERRCODE, "Option value must be on or off, but is '%s'", (char *)pOptStart);
404 return -1;
405 }
406 }
407
408
409 /* extract a groupname and return its gid.
410 * rgerhards, 2007-07-17
411 */
412 static rsRetVal doGetGID(uchar **pp, rsRetVal (*pSetHdlr)(void *, uid_t), void *pVal) {
413 struct group *pgBuf = NULL;
414 struct group gBuf;
415 DEFiRet;
416 uchar szName[256];
417 int bufSize = 1024;
418 char *stringBuf = NULL;
419 int err;
420
421 assert(pp != NULL);
422 assert(*pp != NULL);
423
424 if (getSubString(pp, (char *)szName, sizeof(szName), ' ') != 0) {
425 if (loadConf->globals.abortOnIDResolutionFail) {
426 fprintf(stderr, "could not extract group name: %s\n", (char *)szName);
427 exit(1); /* good exit */
428 } else {
429 LogError(0, RS_RET_NOT_FOUND, "could not extract group name");
430 ABORT_FINALIZE(RS_RET_NOT_FOUND);
431 }
432 }
433
434 do {
435 char *p;
436
437 /* Increase bufsize and try again.*/
438 bufSize *= 2;
439 CHKmalloc(p = realloc(stringBuf, bufSize));
440 stringBuf = p;
441 err = getgrnam_r((char *)szName, &gBuf, stringBuf, bufSize, &pgBuf);
442 } while ((pgBuf == NULL) && (err == ERANGE));
443
444 if (pgBuf == NULL) {
445 if (err != 0) {
446 LogError(err, RS_RET_NOT_FOUND, "Query for group '%s' resulted in an error", szName);
447 } else {
448 LogError(0, RS_RET_NOT_FOUND, "ID for group '%s' could not be found", szName);
449 }
450 iRet = RS_RET_NOT_FOUND;
451 if (loadConf->globals.abortOnIDResolutionFail) {
452 fprintf(stderr, "ID for group '%s' could not be found or error\n", szName);
453 exit(1); /* good exit */
454 }
455 } else {
456 if (pSetHdlr == NULL) {
457 /* we should set value directly to var */
458 *((gid_t *)pVal) = pgBuf->gr_gid;
459 } else {
460 /* we set value via a set function */
461 CHKiRet(pSetHdlr(pVal, pgBuf->gr_gid));
462 }
463 dbgprintf("gid %d obtained for group '%s'\n", (int)pgBuf->gr_gid, szName);
464 }
465
466 skipWhiteSpace(pp); /* skip over any whitespace */
467
468 finalize_it:
469 free(stringBuf);
470 RETiRet;
471 }
472
473
474 /* extract a username and return its uid.
475 * rgerhards, 2007-07-17
476 */
477 static rsRetVal doGetUID(uchar **pp, rsRetVal (*pSetHdlr)(void *, uid_t), void *pVal) {
478 struct passwd *ppwBuf;
479 struct passwd pwBuf;
480 DEFiRet;
481 uchar szName[256];
482 char stringBuf[2048]; /* I hope this is large enough... */
483
484 assert(pp != NULL);
485 assert(*pp != NULL);
486
487 if (getSubString(pp, (char *)szName, sizeof(szName), ' ') != 0) {
488 if (loadConf->globals.abortOnIDResolutionFail) {
489 fprintf(stderr, "could not extract user name: %s\n", (char *)szName);
490 exit(1); /* good exit */
491 } else {
492 LogError(0, RS_RET_NOT_FOUND, "could not extract user name");
493 ABORT_FINALIZE(RS_RET_NOT_FOUND);
494 }
495 }
496
497 getpwnam_r((char *)szName, &pwBuf, stringBuf, sizeof(stringBuf), &ppwBuf);
498
499 if (ppwBuf == NULL) {
500 if (loadConf->globals.abortOnIDResolutionFail) {
501 fprintf(stderr, "ID for user '%s' could not be found or error\n", (char *)szName);
502 exit(1); /* good exit */
503 } else {
504 LogError(0, RS_RET_NOT_FOUND, "ID for user '%s' could not be found or error", (char *)szName);
505 iRet = RS_RET_NOT_FOUND;
506 }
507 } else {
508 if (pSetHdlr == NULL) {
509 /* we should set value directly to var */
510 *((uid_t *)pVal) = ppwBuf->pw_uid;
511 } else {
512 /* we set value via a set function */
513 CHKiRet(pSetHdlr(pVal, ppwBuf->pw_uid));
514 }
515 dbgprintf("uid %d obtained for user '%s'\n", (int)ppwBuf->pw_uid, szName);
516 }
517
518 skipWhiteSpace(pp); /* skip over any whitespace */
519
520 finalize_it:
521 RETiRet;
522 }
523
524
525 /* Parse and process an binary cofig option. pVal must be
526 * a pointer to an integer which is to receive the option
527 * value.
528 * rgerhards, 2007-07-15
529 */
530 static rsRetVal doBinaryOptionLine(uchar **pp, rsRetVal (*pSetHdlr)(void *, int), void *pVal) {
531 int iOption;
532 DEFiRet;
533
534 assert(pp != NULL);
535 assert(*pp != NULL);
536
537 if ((iOption = doParseOnOffOption(pp)) == -1) return RS_RET_ERR; /* nothing left to do */
538
539 if (pSetHdlr == NULL) {
540 /* we should set value directly to var */
541 *((int *)pVal) = iOption;
542 } else {
543 /* we set value via a set function */
544 CHKiRet(pSetHdlr(pVal, iOption));
545 }
546
547 skipWhiteSpace(pp); /* skip over any whitespace */
548
549 finalize_it:
550 RETiRet;
551 }
552
553
554 /* parse a whitespace-delimited word from the provided string. This is a
555 * helper function for a number of syntaxes. The parsed value is returned
556 * in ppStrB (which must be provided by caller).
557 * rgerhards, 2008-02-14
558 */
559 static rsRetVal getWord(uchar **pp, cstr_t **ppStrB) {
560 DEFiRet;
561 uchar *p;
562
563 assert(pp != NULL);
564 assert(*pp != NULL);
565 assert(ppStrB != NULL);
566
567 CHKiRet(cstrConstruct(ppStrB));
568
569 skipWhiteSpace(pp); /* skip over any whitespace */
570
571 /* parse out the word */
572 p = *pp;
573
574 while (*p && !isspace((int)*p)) {
575 CHKiRet(cstrAppendChar(*ppStrB, *p++));
576 }
577 cstrFinalize(*ppStrB);
578
579 *pp = p;
580
581 finalize_it:
582 RETiRet;
583 }
584
585
586 /* Parse and a word config line option. A word is a consequtive
587 * sequence of non-whitespace characters. pVal must be
588 * a pointer to a string which is to receive the option
589 * value. The returned string must be freed by the caller.
590 * rgerhards, 2007-09-07
591 * To facilitate multiple instances of the same command line
592 * directive, doGetWord() now checks if pVal is already a
593 * non-NULL pointer. If so, we assume it was created by a previous
594 * incarnation and is automatically freed. This happens only when
595 * no custom handler is defined. If it is, the customer handler
596 * must do the cleanup. I have checked and this was al also memory
597 * leak with some code. Obviously, not a large one. -- rgerhards, 2007-12-20
598 * Just to clarify: if pVal is parsed to a custom handler, this handler
599 * is responsible for freeing pVal. -- rgerhards, 2008-03-20
600 */
601 static rsRetVal doGetWord(uchar **pp, rsRetVal (*pSetHdlr)(void *, uchar *), void *pVal) {
602 DEFiRet;
603 cstr_t *pStrB = NULL;
604 uchar *pNewVal;
605
606 assert(pp != NULL);
607 assert(*pp != NULL);
608
609 CHKiRet(getWord(pp, &pStrB));
610 CHKiRet(cstrConvSzStrAndDestruct(&pStrB, &pNewVal, 0));
611
612 DBGPRINTF("doGetWord: get newval '%s' (len %d), hdlr %p\n", pNewVal, (int)ustrlen(pNewVal), pSetHdlr);
613 /* we got the word, now set it */
614 if (pSetHdlr == NULL) {
615 /* we should set value directly to var */
616 if (*((uchar **)pVal) != NULL) free(*((uchar **)pVal)); /* free previous entry */
617 *((uchar **)pVal) = pNewVal; /* set new one */
618 } else {
619 /* we set value via a set function */
620 CHKiRet(pSetHdlr(pVal, pNewVal));
621 }
622
623 skipWhiteSpace(pp); /* skip over any whitespace */
624
625 finalize_it:
626 if (iRet != RS_RET_OK) {
627 if (pStrB != NULL) cstrDestruct(&pStrB);
628 }
629
630 RETiRet;
631 }
632
633
634 /* parse a syslog name from the string. This is the generic code that is
635 * called by the facility/severity functions. Note that we do not check the
636 * validity of numerical values, something that should probably change over
637 * time (TODO). -- rgerhards, 2008-02-14
638 */
639 static rsRetVal doSyslogName(uchar **pp, rsRetVal (*pSetHdlr)(void *, int), void *pVal, syslogName_t *pNameTable) {
640 DEFiRet;
641 cstr_t *pStrB;
642 int iNewVal;
643
644 assert(pp != NULL);
645 assert(*pp != NULL);
646
647 CHKiRet(getWord(pp, &pStrB)); /* get word */
648 iNewVal = decodeSyslogName(cstrGetSzStrNoNULL(pStrB), pNameTable);
649
650 if (pSetHdlr == NULL) {
651 /* we should set value directly to var */
652 *((int *)pVal) = iNewVal; /* set new one */
653 } else {
654 /* we set value via a set function */
655 CHKiRet(pSetHdlr(pVal, iNewVal));
656 }
657
658 skipWhiteSpace(pp); /* skip over any whitespace */
659
660 finalize_it:
661 if (pStrB != NULL) rsCStrDestruct(&pStrB);
662
663 RETiRet;
664 }
665
666
667 /* Implements the facility syntax.
668 * rgerhards, 2008-02-14
669 */
670 static rsRetVal doFacility(uchar **pp, rsRetVal (*pSetHdlr)(void *, int), void *pVal) {
671 DEFiRet;
672 iRet = doSyslogName(pp, pSetHdlr, pVal, syslogFacNames);
673 RETiRet;
674 }
675
676
677 static rsRetVal doGoneAway(__attribute__((unused)) uchar **pp,
678 __attribute__((unused)) rsRetVal (*pSetHdlr)(void *, int),
679 __attribute__((unused)) void *pVal) {
680 parser_warnmsg("config directive is no longer supported -- ignored");
681 return RS_RET_CMD_GONE_AWAY;
682 }
683
684 /* Implements the severity syntax.
685 * rgerhards, 2008-02-14
686 */
687 static rsRetVal doSeverity(uchar **pp, rsRetVal (*pSetHdlr)(void *, int), void *pVal) {
688 DEFiRet;
689 iRet = doSyslogName(pp, pSetHdlr, pVal, syslogPriNames);
690 RETiRet;
691 }
692
693
694 /* --------------- END functions for handling canned syntaxes --------------- */
695
696 /* destructor for cslCmdHdlr
697 * pThis is actually a cslCmdHdlr_t, but we do not cast it as all we currently
698 * need to do is free it.
699 */
700 static rsRetVal cslchDestruct(void *pThis) {
701 assert(pThis != NULL);
702 free(pThis);
703
704 return RS_RET_OK;
705 }
706
707
708 /* constructor for cslCmdHdlr
709 */
710 static rsRetVal cslchConstruct(cslCmdHdlr_t **ppThis) {
711 cslCmdHdlr_t *pThis;
712 DEFiRet;
713
714 assert(ppThis != NULL);
715 if ((pThis = calloc(1, sizeof(cslCmdHdlr_t))) == NULL) {
716 ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY);
717 }
718
719 finalize_it:
720 *ppThis = pThis;
721 RETiRet;
722 }
723
724 /* destructor for linked list keys. As we do not use any dynamic memory,
725 * we simply return. However, this entry point must be defined for the
726 * linkedList class to make sure we have not forgotten a destructor.
727 * rgerhards, 2007-11-21
728 */
729 static rsRetVal cslchKeyDestruct(void __attribute__((unused)) * pData) {
730 return RS_RET_OK;
731 }
732
733
734 /* Key compare operation for linked list class. This compares two
735 * owner cookies (void *).
736 * rgerhards, 2007-11-21
737 */
738 static int cslchKeyCompare(void *pKey1, void *pKey2) {
739 if (pKey1 == pKey2)
740 return 0;
741 else if (pKey1 < pKey2)
742 return -1;
743 else
744 return 1;
745 }
746
747
748 /* set data members for this object
749 */
750 static rsRetVal cslchSetEntry(
751 cslCmdHdlr_t *pThis, ecslCmdHdrlType eType, rsRetVal (*pHdlr)(), void *pData, int *permitted) {
752 assert(pThis != NULL);
753 assert(eType != eCmdHdlrInvalid);
754
755 pThis->eType = eType;
756 pThis->cslCmdHdlr = pHdlr;
757 pThis->pData = pData;
758 pThis->permitted = permitted;
759
760 return RS_RET_OK;
761 }
762
763
764 /* call the specified handler
765 */
766 static rsRetVal cslchCallHdlr(cslCmdHdlr_t *pThis, uchar **ppConfLine) {
767 DEFiRet;
768 assert(pThis != NULL);
769 assert(ppConfLine != NULL);
770
771 switch (pThis->eType) {
772 case eCmdHdlrCustomHandler:
773 CHKiRet(doCustomHdlr(ppConfLine, pThis->cslCmdHdlr, pThis->pData));
774 break;
775 case eCmdHdlrUID:
776 CHKiRet(doGetUID(ppConfLine, pThis->cslCmdHdlr, pThis->pData));
777 break;
778 case eCmdHdlrGID:
779 CHKiRet(doGetGID(ppConfLine, pThis->cslCmdHdlr, pThis->pData));
780 break;
781 case eCmdHdlrBinary:
782 CHKiRet(doBinaryOptionLine(ppConfLine, pThis->cslCmdHdlr, pThis->pData));
783 break;
784 case eCmdHdlrFileCreateMode:
785 CHKiRet(doFileCreateMode(ppConfLine, pThis->cslCmdHdlr, pThis->pData));
786 break;
787 case eCmdHdlrInt:
788 CHKiRet(doGetInt(ppConfLine, pThis->cslCmdHdlr, pThis->pData));
789 break;
790 case eCmdHdlrNonNegInt:
791 CHKiRet(doGetNonNegInt(ppConfLine, pThis->cslCmdHdlr, pThis->pData));
792 break;
793 case eCmdHdlrPositiveInt:
794 CHKiRet(doGetPositiveInt(ppConfLine, pThis->cslCmdHdlr, pThis->pData));
795 break;
796 case eCmdHdlrSize:
797 CHKiRet(doGetSize(ppConfLine, pThis->cslCmdHdlr, pThis->pData));
798 break;
799 case eCmdHdlrGetChar:
800 CHKiRet(doGetChar(ppConfLine, pThis->cslCmdHdlr, pThis->pData));
801 break;
802 case eCmdHdlrFacility:
803 CHKiRet(doFacility(ppConfLine, pThis->cslCmdHdlr, pThis->pData));
804 break;
805 case eCmdHdlrSeverity:
806 CHKiRet(doSeverity(ppConfLine, pThis->cslCmdHdlr, pThis->pData));
807 break;
808 case eCmdHdlrGetWord:
809 CHKiRet(doGetWord(ppConfLine, pThis->cslCmdHdlr, pThis->pData));
810 break;
811 case eCmdHdlrGoneAway:
812 CHKiRet(doGoneAway(ppConfLine, pThis->cslCmdHdlr, pThis->pData));
813 break;
814 /* some non-legacy handler (used in v6+ solely) */
815 case eCmdHdlrInvalid:
816 case eCmdHdlrString:
817 case eCmdHdlrArray:
818 case eCmdHdlrQueueType:
819 default:
820 dbgprintf("error: command handler type %d not implemented in legacy system\n", pThis->eType);
821 iRet = RS_RET_NOT_IMPLEMENTED;
822 goto finalize_it;
823 }
824
825 finalize_it:
826 RETiRet;
827 }
828
829
830 /* ---------------------------------------------------------------------- *
831 * now come the handlers for cslCmd_t
832 * ---------------------------------------------------------------------- */
833
834 /* destructor for a cslCmd list key (a string as of now)
835 */
836 static rsRetVal cslcKeyDestruct(void *pData) {
837 free(pData); /* we do not need to cast as all we do is free it anyway... */
838 return RS_RET_OK;
839 }
840
841 /* destructor for cslCmd
842 */
843 static rsRetVal cslcDestruct(void *pData) {
844 cslCmd_t *pThis = (cslCmd_t *)pData;
845
846 assert(pThis != NULL);
847
848 llDestroy(&pThis->llCmdHdlrs);
849 free(pThis);
850
851 return RS_RET_OK;
852 }
853
854
855 /* constructor for cslCmd
856 */
857 static rsRetVal cslcConstruct(cslCmd_t **ppThis, int bChainingPermitted) {
858 cslCmd_t *pThis;
859 DEFiRet;
860
861 assert(ppThis != NULL);
862 if ((pThis = calloc(1, sizeof(cslCmd_t))) == NULL) {
863 ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY);
864 }
865
866 pThis->bChainingPermitted = bChainingPermitted;
867
868 CHKiRet(llInit(&pThis->llCmdHdlrs, cslchDestruct, cslchKeyDestruct, (int (*)(void *, void *))cslchKeyCompare));
869
870 finalize_it:
871 *ppThis = pThis;
872 RETiRet;
873 }
874
875
876 /* add a handler entry to a known command
877 */
878 static rsRetVal cslcAddHdlr(
879 cslCmd_t *pThis, ecslCmdHdrlType eType, rsRetVal (*pHdlr)(), void *pData, void *pOwnerCookie, int *permitted) {
880 DEFiRet;
881 cslCmdHdlr_t *pCmdHdlr = NULL;
882
883 assert(pThis != NULL);
884
885 CHKiRet(cslchConstruct(&pCmdHdlr));
886 CHKiRet(cslchSetEntry(pCmdHdlr, eType, pHdlr, pData, permitted));
887 CHKiRet(llAppend(&pThis->llCmdHdlrs, pOwnerCookie, pCmdHdlr));
888
889 finalize_it:
890 if (iRet != RS_RET_OK) {
891 if (pHdlr != NULL) cslchDestruct(pCmdHdlr);
892 }
893
894 RETiRet;
895 }
896
897
898 /* function that registers cfsysline handlers.
899 * The supplied pCmdName is copied and a new buffer is allocated. This
900 * buffer is automatically destroyed when the element is freed, the
901 * caller does not need to take care of that. The caller must, however,
902 * free pCmdName if he allocated it dynamically! -- rgerhards, 2007-08-09
903 * Parameter permitted has been added to support the v2 config system. With it,
904 * we can tell the legacy system (us here!) to check if a config directive is
905 * still permitted. For example, the v2 system will disable module global
906 * parameters if the are supplied via the native v2 callbacks. In order not
907 * to break exisiting modules, we have renamed the rgCfSysLinHdlr routine to
908 * version 2 and added a new one with the original name. It just calls the
909 * v2 function and supplies a "don't care (NULL)" pointer as this argument.
910 * rgerhards, 2012-06-26
911 */
912 rsRetVal regCfSysLineHdlr2(const uchar *pCmdName,
913 int bChainingPermitted,
914 ecslCmdHdrlType eType,
915 rsRetVal (*pHdlr)(),
916 void *pData,
917 void *pOwnerCookie,
918 int *permitted) {
919 DEFiRet;
920 cslCmd_t *pThis;
921 uchar *pMyCmdName;
922
923 iRet = llFind(&llCmdList, (void *)pCmdName, (void *)&pThis);
924 if (iRet == RS_RET_NOT_FOUND) {
925 /* new command */
926 CHKiRet(cslcConstruct(&pThis, bChainingPermitted));
927 CHKiRet_Hdlr(cslcAddHdlr(pThis, eType, pHdlr, pData, pOwnerCookie, permitted)) {
928 cslcDestruct(pThis);
929 FINALIZE;
930 }
931 /* important: add to list, AFTER everything else is OK. Else
932 * we mess up things in the error case.
933 */
934 if ((pMyCmdName = (uchar *)strdup((char *)pCmdName)) == NULL) {
935 cslcDestruct(pThis);
936 ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY);
937 }
938 CHKiRet_Hdlr(llAppend(&llCmdList, pMyCmdName, (void *)pThis)) {
939 cslcDestruct(pThis);
940 FINALIZE;
941 }
942 } else {
943 /* command already exists, are we allowed to chain? */
944 if (pThis->bChainingPermitted == 0 || bChainingPermitted == 0) {
945 ABORT_FINALIZE(RS_RET_CHAIN_NOT_PERMITTED);
946 }
947 CHKiRet_Hdlr(cslcAddHdlr(pThis, eType, pHdlr, pData, pOwnerCookie, permitted)) {
948 cslcDestruct(pThis);
949 FINALIZE;
950 }
951 }
952
953 finalize_it:
954 RETiRet;
955 }
956
957 rsRetVal regCfSysLineHdlr(const uchar *pCmdName,
958 int bChainingPermitted,
959 ecslCmdHdrlType eType,
960 rsRetVal (*pHdlr)(),
961 void *pData,
962 void *pOwnerCookie) {
963 DEFiRet;
964 iRet = regCfSysLineHdlr2(pCmdName, bChainingPermitted, eType, pHdlr, pData, pOwnerCookie, NULL);
965 RETiRet;
966 }
967
968
969 rsRetVal unregCfSysLineHdlrs(void) {
970 return llDestroy(&llCmdList);
971 }
972
973
974 /* helper function for unregCfSysLineHdlrs4Owner(). This is used to see if there is
975 * a handler of this owner inside the element and, if so, remove it. Please note that
976 * it keeps track of a pointer to the last linked list entry, as this is needed to
977 * remove an entry from the list.
978 * rgerhards, 2007-11-21
979 */
980 DEFFUNC_llExecFunc(unregHdlrsHeadExec) {
981 DEFiRet;
982 cslCmd_t *pListHdr = (cslCmd_t *)pData;
983 int iNumElts;
984
985 /* first find element */
986 CHKiRet(llFindAndDelete(&(pListHdr->llCmdHdlrs), pParam));
987
988 /* now go back and check how many elements are left */
989 CHKiRet(llGetNumElts(&(pListHdr->llCmdHdlrs), &iNumElts));
990
991 if (iNumElts == 0) {
992 /* nothing left in header, so request to delete it */
993 iRet = RS_RET_OK_DELETE_LISTENTRY;
994 }
995
996 finalize_it:
997 RETiRet;
998 }
999 /* unregister and destroy cfSysLineHandlers for a specific owner. This method is
1000 * most importantly used before unloading a loadable module providing some handlers.
1001 * The full list of handlers is searched. If the to-be removed handler was the only
1002 * handler for a directive name, the directive header, too, is deleted.
1003 * rgerhards, 2007-11-21
1004 */
1005 rsRetVal unregCfSysLineHdlrs4Owner(void *pOwnerCookie) {
1006 DEFiRet;
1007 /* we need to walk through all directive names, as the linked list
1008 * class does not provide a way to just search the lower-level handlers.
1009 */
1010 iRet = llExecFunc(&llCmdList, unregHdlrsHeadExec, pOwnerCookie);
1011 if (iRet == RS_RET_NOT_FOUND) {
1012 /* It is not considered an error if a module had no
1013 hanlers registered. */
1014 iRet = RS_RET_OK;
1015 }
1016
1017 RETiRet;
1018 }
1019
1020
1021 /* process a cfsysline command (based on handler structure)
1022 * param "p" is a pointer to the command line after the command. Should be
1023 * updated.
1024 */
1025 rsRetVal processCfSysLineCommand(uchar *pCmdName, uchar **p) {
1026 DEFiRet;
1027 rsRetVal iRetLL; /* for linked list handling */
1028 cslCmd_t *pCmd;
1029 cslCmdHdlr_t *pCmdHdlr;
1030 linkedListCookie_t llCookieCmdHdlr;
1031 uchar *pHdlrP; /* the handler's private p (else we could only call one handler) */
1032 int bWasOnceOK; /* was the result of an handler at least once RS_RET_OK? */
1033 uchar *pOKp = NULL; /* returned conf line pointer when it was OK */
1034
1035 iRet = llFind(&llCmdList, (void *)pCmdName, (void *)&pCmd);
1036
1037 if (iRet == RS_RET_NOT_FOUND) {
1038 LogError(0, RS_RET_NOT_FOUND,
1039 "invalid or yet-unknown config file command '%s' - "
1040 "have you forgotten to load a module?",
1041 pCmdName);
1042 }
1043
1044 if (iRet != RS_RET_OK) goto finalize_it;
1045
1046 llCookieCmdHdlr = NULL;
1047 bWasOnceOK = 0;
1048 while ((iRetLL = llGetNextElt(&pCmd->llCmdHdlrs, &llCookieCmdHdlr, (void *)&pCmdHdlr)) == RS_RET_OK) {
1049 /* for the time being, we ignore errors during handlers. The
1050 * reason is that handlers are independent. An error in one
1051 * handler does not necessarily mean that another one will
1052 * fail, too. Later, we might add a config variable to control
1053 * this behaviour (but I am not sure if that is really
1054 * necessary). -- rgerhards, 2007-07-31
1055 */
1056 pHdlrP = *p;
1057 if (pCmdHdlr->permitted != NULL && !*(pCmdHdlr->permitted)) {
1058 LogError(0, RS_RET_PARAM_NOT_PERMITTED,
1059 "command '%s' is currently not "
1060 "permitted - did you already set it via a RainerScript command (v6+ config)?",
1061 pCmdName);
1062 ABORT_FINALIZE(RS_RET_PARAM_NOT_PERMITTED);
1063 } else if ((iRet = cslchCallHdlr(pCmdHdlr, &pHdlrP)) == RS_RET_OK) {
1064 bWasOnceOK = 1;
1065 pOKp = pHdlrP;
1066 }
1067 }
1068
1069 if (bWasOnceOK == 1) {
1070 *p = pOKp;
1071 iRet = RS_RET_OK;
1072 }
1073
1074 if (iRetLL != RS_RET_END_OF_LINKEDLIST) iRet = iRetLL;
1075
1076 finalize_it:
1077 RETiRet;
1078 }
1079
1080
1081 /* debug print the command handler structure
1082 */
1083 void dbgPrintCfSysLineHandlers(void) {
1084 cslCmd_t *pCmd;
1085 cslCmdHdlr_t *pCmdHdlr;
1086 linkedListCookie_t llCookieCmd;
1087 linkedListCookie_t llCookieCmdHdlr;
1088 uchar *pKey;
1089
1090 dbgprintf("System Line Configuration Commands:\n");
1091 llCookieCmd = NULL;
1092 while (llGetNextElt(&llCmdList, &llCookieCmd, (void *)&pCmd) == RS_RET_OK) {
1093 llGetKey(llCookieCmd, (void *)&pKey); /* TODO: using the cookie is NOT clean! */
1094 dbgprintf("\tCommand '%s':\n", pKey);
1095 llCookieCmdHdlr = NULL;
1096 while (llGetNextElt(&pCmd->llCmdHdlrs, &llCookieCmdHdlr, (void *)&pCmdHdlr) == RS_RET_OK) {
1097 dbgprintf("\t\ttype : %d\n", pCmdHdlr->eType);
1098 dbgprintf("\t\tpData: 0x%lx\n", (unsigned long)pCmdHdlr->pData);
1099 dbgprintf("\t\tHdlr : 0x%lx\n", (unsigned long)pCmdHdlr->cslCmdHdlr);
1100 dbgprintf("\t\tOwner: 0x%lx\n", (unsigned long)llCookieCmdHdlr->pKey);
1101 dbgprintf("\n");
1102 }
1103 }
1104 dbgprintf("\n");
1105 }
1106
1107
1108 rsRetVal cfsyslineInit(void) {
1109 DEFiRet;
1110 CHKiRet(objGetObjInterface(&obj));
1111
1112 CHKiRet(llInit(&llCmdList, cslcDestruct, cslcKeyDestruct, (int (*)(void *, void *))strcasecmp));
1113
1114 finalize_it:
1115 RETiRet;
1116 }