Debian Codegraph @

runtime/conf.c

raw
1 /* The config file handler (not yet a real object)
2 *
3 * This file is based on an excerpt from syslogd.c, which dates back
4 * much later. I began the file on 2008-02-19 as part of the modularization
5 * effort. Over time, a clean abstration will become even more important
6 * because the config file handler will by dynamically be loaded and be
7 * kept in memory only as long as the config file is actually being
8 * processed. Thereafter, it shall be unloaded. -- rgerhards
9 * Please note that the original syslogd.c source was under BSD license
10 * at the time of the rsyslog fork from sysklogd.
11 *
12 * Copyright 2008-2025 Rainer Gerhards and Adiscon GmbH.
13 *
14 * This file is part of rsyslog.
15 *
16 * Licensed under the Apache License, Version 2.0 (the "License");
17 * you may not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 * -or-
22 * see COPYING.ASL20 in the source distribution
23 *
24 * Unless required by applicable law or agreed to in writing, software
25 * distributed under the License is distributed on an "AS IS" BASIS,
26 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 * See the License for the specific language governing permissions and
28 * limitations under the License.
29 */
30 #define CFGLNSIZ 64 * 1024 /* the maximum size of a configuration file line, after re-combination */
31 #include "config.h"
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <stddef.h>
35 #include <string.h>
36 #include <dlfcn.h>
37 #include <sys/stat.h>
38 #include <errno.h>
39 #include <ctype.h>
40 #include <assert.h>
41 #include <dirent.h>
42 #include <glob.h>
43 #include <sys/types.h>
44 #ifdef HAVE_LIBGEN_H
45 #ifndef OS_SOLARIS
46 #include <libgen.h>
47 #endif
48 #endif
49
50 #include "rsyslog.h"
51 #include "dirty.h"
52 #include "parse.h"
53 #include "action.h"
54 #include "template.h"
55 #include "cfsysline.h"
56 #include "modules.h"
57 #include "outchannel.h"
58 #include "stringbuf.h"
59 #include "conf.h"
60 #include "stringbuf.h"
61 #include "srUtils.h"
62 #include "errmsg.h"
63 #include "net.h"
64 #include "ruleset.h"
65 #include "rsconf.h"
66 #include "unicode-helper.h"
67 #include "rainerscript.h"
68
69 #ifdef OS_SOLARIS
70 #define NAME_MAX MAXNAMELEN
71 #endif
72
73 /* forward definitions */
74
75
76 /* static data */
77 DEFobjStaticHelpers;
78 DEFobjCurrIf(module) DEFobjCurrIf(net) DEFobjCurrIf(ruleset)
79
80 int bConfStrictScoping = 0; /* force strict scoping during config processing? */
81
82
83 /* The following module-global variables are used for building
84 * tag and host selector lines during startup and config reload.
85 * This is stored as a global variable pool because of its ease. It is
86 * also fairly compatible with multi-threading as the stratup code must
87 * be run in a single thread anyways. So there can be no race conditions.
88 * rgerhards 2005-10-18
89 */
90 EHostnameCmpMode eDfltHostnameCmpMode = HN_NO_COMP;
91 cstr_t *pDfltHostnameCmp = NULL;
92 cstr_t *pDfltProgNameCmp = NULL;
93
94
95 /* process a $ModLoad config line. */
96 static rsRetVal doModLoad(uchar **pp, __attribute__((unused)) void *pVal) {
97 DEFiRet;
98 uchar szName[512];
99 uchar *pModName;
100
101 assert(pp != NULL);
102 assert(*pp != NULL);
103
104 skipWhiteSpace(pp); /* skip over any whitespace */
105 if (getSubString(pp, (char *)szName, sizeof(szName), ' ') != 0) {
106 LogError(0, RS_RET_NOT_FOUND, "could not extract module name");
107 ABORT_FINALIZE(RS_RET_NOT_FOUND);
108 }
109 skipWhiteSpace(pp); /* skip over any whitespace */
110
111 /* this below is a quick and dirty hack to provide compatibility with the
112 * $ModLoad MySQL forward compatibility statement. This needs to be supported
113 * for legacy format.
114 */
115 if (!strcmp((char *)szName, "MySQL"))
116 pModName = (uchar *)"ommysql.so";
117 else
118 pModName = szName;
119
120 CHKiRet(module.Load(pModName, 1, NULL));
121
122 finalize_it:
123 RETiRet;
124 }
125
126
127 /* remove leading spaces from name; this "fixes" some anomalies in
128 * getSubString(), but I was not brave enough to fix the former as
129 * it has many other callers... -- rgerhards, 2013-05-27
130 */
131 static void ltrim(char *src) {
132 char *dst = src;
133 while (isspace(*src)) ++src; /*SKIP*/
134 ;
135 if (dst != src) {
136 while (*src != '\0') *dst++ = *src++;
137 *dst = '\0';
138 }
139 }
140
141 /* parse and interpret a $-config line that starts with
142 * a name (this is common code). It is parsed to the name
143 * and then the proper sub-function is called to handle
144 * the actual directive.
145 * rgerhards 2004-11-17
146 * rgerhards 2005-06-21: previously only for templates, now
147 * generalized.
148 */
149 static rsRetVal doNameLine(uchar **pp, void *pVal) {
150 DEFiRet;
151 uchar *p;
152 enum eDirective eDir;
153 char szName[128];
154
155 assert(pp != NULL);
156 p = *pp;
157 assert(p != NULL);
158
159 PRAGMA_DIAGNOSTIC_PUSH
160 PRAGMA_IGNORE_Wvoid_pointer_to_enum_cast;
161 /* this time, pVal actually is NOT a pointer! It is save to case, as
162 * the enum was written to it, so there can be no loss of bits (ptr is larger).
163 */
164 eDir = (enum eDirective)pVal;
165 PRAGMA_DIAGNOSTIC_POP
166
167 if (getSubString(&p, szName, sizeof(szName), ',') != 0) {
168 LogError(0, RS_RET_NOT_FOUND, "Invalid config line: could not extract name - line ignored");
169 ABORT_FINALIZE(RS_RET_NOT_FOUND);
170 }
171 ltrim(szName);
172 if (*p == ',') ++p; /* comma was eaten */
173
174 /* we got the name - now we pass name & the rest of the string
175 * to the subfunction. It makes no sense to do further
176 * parsing here, as this is in close interaction with the
177 * respective subsystem. rgerhards 2004-11-17
178 */
179
180 switch (eDir) {
181 case DIR_TEMPLATE:
182 tplAddLine(loadConf, szName, &p);
183 break;
184 case DIR_OUTCHANNEL:
185 ochAddLine(szName, &p);
186 break;
187 case DIR_ALLOWEDSENDER:
188 net.addAllowedSenderLine(szName, &p);
189 break;
190 default: /* we do this to avoid compiler warning - not all
191 * enum values call this function, so an incomplete list
192 * is quite ok (but then we should not run into this code,
193 * so at least we log a debug warning).
194 */
195 dbgprintf("INTERNAL ERROR: doNameLine() called with invalid eDir %d.\n", eDir);
196 break;
197 }
198
199 *pp = p;
200
201 finalize_it:
202 RETiRet;
203 }
204
205
206 /* Parse and interpret a system-directive in the config line
207 * A system directive is one that starts with a "$" sign. It offers
208 * extended configuration parameters.
209 * 2004-11-17 rgerhards
210 */
211 static rsRetVal cfsysline(uchar *p) {
212 DEFiRet;
213 uchar szCmd[64];
214
215 assert(p != NULL);
216 errno = 0;
217 if (getSubString(&p, (char *)szCmd, sizeof(szCmd), ' ') != 0) {
218 LogError(0, RS_RET_NOT_FOUND,
219 "Invalid $-configline "
220 "- could not extract command - line ignored\n");
221 ABORT_FINALIZE(RS_RET_NOT_FOUND);
222 }
223
224 /* we now try and see if we can find the command in the registered
225 * list of cfsysline handlers. -- rgerhards, 2007-07-31
226 */
227 CHKiRet(processCfSysLineCommand(szCmd, &p));
228
229 /* now check if we have some extra characters left on the line - that
230 * should not be the case. Whitespace is OK, but everything else should
231 * trigger a warning (that may be an indication of undesired behaviour).
232 * An exception, of course, are comments (starting with '#').
233 * rgerhards, 2007-07-04
234 */
235 skipWhiteSpace(&p);
236
237 if (*p && *p != '#') { /* we have a non-whitespace, so let's complain */
238 LogError(0, NO_ERRCODE, "error: extra characters in config line ignored: '%s'", p);
239 }
240
241 finalize_it:
242 RETiRet;
243 }
244
245
246 /* Helper to cfline() and its helpers. Parses a template name
247 * from an "action" line. Must be called with the Line pointer
248 * pointing to the first character after the semicolon.
249 * rgerhards 2004-11-19
250 * changed function to work with OMSR. -- rgerhards, 2007-07-27
251 * the default template is to be used when no template is specified.
252 */
253 rsRetVal cflineParseTemplateName(uchar **pp, omodStringRequest_t *pOMSR, int iEntry, int iTplOpts, uchar *dfltTplName) {
254 uchar *p;
255 uchar *tplName = NULL;
256 cstr_t *pStrB = NULL;
257 DEFiRet;
258
259 assert(pp != NULL);
260 assert(*pp != NULL);
261 assert(pOMSR != NULL);
262
263 p = *pp;
264 /* a template must follow - search it and complain, if not found */
265 skipWhiteSpace(&p);
266 if (*p == ';')
267 ++p; /* eat it */
268 else if (*p != '\0' && *p != '#') {
269 LogError(0, RS_RET_ERR, "invalid character in selector line - ';template' expected");
270 ABORT_FINALIZE(RS_RET_ERR);
271 }
272
273 skipWhiteSpace(&p); /* go to begin of template name */
274
275 if (*p == '\0' || *p == '#') {
276 /* no template specified, use the default */
277 CHKmalloc(tplName = (uchar *)strdup((char *)dfltTplName));
278 } else {
279 /* template specified, pick it up */
280 CHKiRet(cstrConstruct(&pStrB));
281
282 /* now copy the string */
283 while (*p && *p != '#' && !isspace((int)*p)) {
284 CHKiRet(cstrAppendChar(pStrB, *p));
285 ++p;
286 }
287 cstrFinalize(pStrB);
288 CHKiRet(cstrConvSzStrAndDestruct(&pStrB, &tplName, 0));
289 }
290
291 CHKiRet(OMSRsetEntry(pOMSR, iEntry, tplName, iTplOpts));
292
293 finalize_it:
294 if (iRet != RS_RET_OK) {
295 free(tplName);
296 if (pStrB != NULL) cstrDestruct(&pStrB);
297 }
298
299 *pp = p;
300
301 RETiRet;
302 }
303
304 /* Helper to cfline(). Parses a file name up until the first
305 * comma and then looks for the template specifier. Tries
306 * to find that template.
307 * rgerhards 2004-11-18
308 * parameter pFileName must point to a buffer large enough
309 * to hold the largest possible filename.
310 * rgerhards, 2007-07-25
311 * updated to include OMSR pointer -- rgerhards, 2007-07-27
312 * updated to include template name -- rgerhards, 2008-03-28
313 * rgerhards, 2010-01-19: file names end at the first space
314 */
315 rsRetVal cflineParseFileName(
316 uchar *p, uchar *pFileName, omodStringRequest_t *pOMSR, int iEntry, int iTplOpts, uchar *pszTpl) {
317 register uchar *pName;
318 int i;
319 DEFiRet;
320
321 assert(pOMSR != NULL);
322
323 pName = pFileName;
324 i = 1; /* we start at 1 so that we reseve space for the '\0'! */
325 while (*p && *p != ';' && *p != ' ' && i < MAXFNAME) {
326 *pName++ = *p++;
327 ++i;
328 }
329 *pName = '\0';
330
331 iRet = cflineParseTemplateName(&p, pOMSR, iEntry, iTplOpts, pszTpl);
332
333 RETiRet;
334 }
335
336
337 /* Decode a traditional PRI filter */
338 /* GPLv3 - stems back to sysklogd */
339 rsRetVal DecodePRIFilter(uchar *pline, uchar pmask[]) {
340 uchar *p;
341 register uchar *q;
342 register int i, i2;
343 uchar *bp;
344 int pri; /* this MUST be int, as -1 is used to convey an error state */
345 int singlpri = 0;
346 int ignorepri = 0;
347 uchar buf[2048]; /* buffer for facility and priority names */
348 uchar xbuf[200];
349 DEFiRet;
350
351 assert(pline != NULL);
352
353 dbgprintf("Decoding traditional PRI filter '%s'\n", pline);
354
355 for (i = 0; i <= LOG_NFACILITIES; i++) {
356 pmask[i] = TABLE_NOPRI;
357 }
358
359 /* scan through the list of selectors */
360 for (p = pline; *p && *p != '\t' && *p != ' ';) {
361 /* find the end of this facility name list */
362 for (q = p; *q && *q != '\t' && *q++ != '.';) continue;
363
364 /* collect priority name */
365 for (bp = buf; *q && !strchr("\t ,;", *q) && bp < buf + sizeof(buf) - 1;) *bp++ = *q++;
366 *bp = '\0';
367
368 /* skip cruft */
369 if (*q) {
370 while (strchr(",;", *q)) q++;
371 }
372
373 /* decode priority name */
374 if (*buf == '!') {
375 ignorepri = 1;
376 /* copy below is ok, we can NOT go off the allocated area */
377 for (bp = buf; *(bp + 1); bp++) *bp = *(bp + 1);
378 *bp = '\0';
379 } else {
380 ignorepri = 0;
381 }
382 if (*buf == '=') {
383 singlpri = 1;
384 pri = decodeSyslogName(&buf[1], syslogPriNames);
385 } else {
386 singlpri = 0;
387 pri = decodeSyslogName(buf, syslogPriNames);
388 }
389
390 if (pri < 0) {
391 snprintf((char *)xbuf, sizeof(xbuf), "unknown priority name \"%s\"", buf);
392 LogError(0, RS_RET_ERR, "%s", xbuf);
393 return RS_RET_ERR;
394 }
395
396 /* scan facilities */
397 while (*p && !strchr("\t .;", *p)) {
398 for (bp = buf; *p && !strchr("\t ,;.", *p) && bp < buf + sizeof(buf) - 1;) *bp++ = *p++;
399 *bp = '\0';
400 if (*buf == '*') {
401 for (i = 0; i <= LOG_NFACILITIES; i++) {
402 if (pri == INTERNAL_NOPRI) {
403 if (ignorepri)
404 pmask[i] = TABLE_ALLPRI;
405 else
406 pmask[i] = TABLE_NOPRI;
407 } else if (singlpri) {
408 if (ignorepri)
409 pmask[i] &= ~(1 << pri);
410 else
411 pmask[i] |= (1 << pri);
412 } else {
413 if (pri == TABLE_ALLPRI) {
414 if (ignorepri)
415 pmask[i] = TABLE_NOPRI;
416 else
417 pmask[i] = TABLE_ALLPRI;
418 } else {
419 if (ignorepri)
420 for (i2 = 0; i2 <= pri; ++i2) pmask[i] &= ~(1 << i2);
421 else
422 for (i2 = 0; i2 <= pri; ++i2) pmask[i] |= (1 << i2);
423 }
424 }
425 }
426 } else {
427 i = decodeSyslogName(buf, syslogFacNames);
428 if (i < 0) {
429 snprintf((char *)xbuf, sizeof(xbuf), "unknown facility name \"%s\"", buf);
430 LogError(0, RS_RET_ERR, "%s", xbuf);
431 return RS_RET_ERR;
432 }
433
434 if (pri == INTERNAL_NOPRI) {
435 if (ignorepri)
436 pmask[i >> 3] = TABLE_ALLPRI;
437 else
438 pmask[i >> 3] = TABLE_NOPRI;
439 } else if (singlpri) {
440 if (ignorepri)
441 pmask[i >> 3] &= ~(1 << pri);
442 else
443 pmask[i >> 3] |= (1 << pri);
444 } else {
445 if (pri == TABLE_ALLPRI) {
446 if (ignorepri)
447 pmask[i >> 3] = TABLE_NOPRI;
448 else
449 pmask[i >> 3] = TABLE_ALLPRI;
450 } else {
451 if (ignorepri)
452 for (i2 = 0; i2 <= pri; ++i2) pmask[i >> 3] &= ~(1 << i2);
453 else
454 for (i2 = 0; i2 <= pri; ++i2) pmask[i >> 3] |= (1 << i2);
455 }
456 }
457 }
458 while (*p == ',' || *p == ' ') p++;
459 }
460
461 p = q;
462 }
463
464 RETiRet;
465 }
466
467
468 /* process the action part of a selector line
469 * rgerhards, 2007-08-01
470 */
471 rsRetVal cflineDoAction(rsconf_t *conf, uchar **p, action_t **ppAction) {
472 modInfo_t *pMod;
473 cfgmodules_etry_t *node;
474 omodStringRequest_t *pOMSR;
475 int bHadWarning = 0;
476 action_t *pAction = NULL;
477 void *pModData;
478 DEFiRet;
479
480 assert(p != NULL);
481 assert(ppAction != NULL);
482
483 /* loop through all modules and see if one picks up the line */
484 node = module.GetNxtCnfType(conf, NULL, eMOD_OUT);
485 /* Note: clang static analyzer reports that node maybe == NULL. However, this is
486 * not possible, because we have the built-in output modules which are always
487 * present. Anyhow, we guard this by an assert. -- rgerhards, 2010-12-16
488 */
489 assert(node != NULL);
490 while (node != NULL) {
491 pOMSR = NULL;
492 pMod = node->pMod;
493 iRet = pMod->mod.om.parseSelectorAct(p, &pModData, &pOMSR);
494 dbgprintf("tried selector action for %s: %d\n", module.GetName(pMod), iRet);
495 if (iRet == RS_RET_OK_WARN) {
496 bHadWarning = 1;
497 iRet = RS_RET_OK;
498 }
499 if (iRet == RS_RET_OK) {
500 if ((iRet = addAction(&pAction, pMod, pModData, pOMSR, NULL, NULL)) == RS_RET_OK) {
501 /* here check if the module is compatible with select features
502 * (currently, we have no such features!) */
503 conf->actions.nbrActions++; /* one more active action! */
504 }
505 break;
506 } else if (iRet != RS_RET_CONFLINE_UNPROCESSED) {
507 /* In this case, the module would have handled the config
508 * line, but some error occurred while doing so. This error should
509 * already by reported by the module. We do not try any other
510 * modules on this line, because we found the right one.
511 * rgerhards, 2007-07-24
512 */
513 dbgprintf("error %d parsing config line\n", (int)iRet);
514 break;
515 }
516 node = module.GetNxtCnfType(conf, node, eMOD_OUT);
517 }
518
519 *ppAction = pAction;
520 if (iRet == RS_RET_OK && bHadWarning) iRet = RS_RET_OK_WARN;
521 RETiRet;
522 }
523
524
525 /* return the current number of active actions
526 * rgerhards, 2008-07-28
527 */
528 static rsRetVal GetNbrActActions(rsconf_t *conf, int *piNbrActions) {
529 DEFiRet;
530 assert(piNbrActions != NULL);
531 *piNbrActions = conf->actions.nbrActions;
532 RETiRet;
533 }
534
535
536 /* queryInterface function
537 * rgerhards, 2008-02-29
538 */
539 BEGINobjQueryInterface(conf)
540 CODESTARTobjQueryInterface(conf);
541 if (pIf->ifVersion != confCURR_IF_VERSION) { /* check for current version, increment on each change */
542 ABORT_FINALIZE(RS_RET_INTERFACE_NOT_SUPPORTED);
543 }
544
545 /* ok, we have the right interface, so let's fill it
546 * Please note that we may also do some backwards-compatibility
547 * work here (if we can support an older interface version - that,
548 * of course, also affects the "if" above).
549 */
550 pIf->doNameLine = doNameLine;
551 pIf->cfsysline = cfsysline;
552 pIf->doModLoad = doModLoad;
553 pIf->GetNbrActActions = GetNbrActActions;
554
555 finalize_it:
556 ENDobjQueryInterface(conf)
557
558
559 /* Reset config variables to default values.
560 * rgerhards, 2010-07-23
561 */
562 static rsRetVal resetConfigVariables(uchar __attribute__((unused)) * pp, void __attribute__((unused)) * pVal) {
563 bConfStrictScoping = 0;
564 return RS_RET_OK;
565 }
566
567
568 /* exit our class
569 * rgerhards, 2008-03-11
570 */
571 BEGINObjClassExit(conf, OBJ_IS_CORE_MODULE) /* CHANGE class also in END MACRO! */
572 CODESTARTObjClassExit(conf);
573 /* free no-longer needed module-global variables */
574 if (pDfltHostnameCmp != NULL) {
575 rsCStrDestruct(&pDfltHostnameCmp);
576 }
577
578 if (pDfltProgNameCmp != NULL) {
579 rsCStrDestruct(&pDfltProgNameCmp);
580 }
581
582 /* release objects we no longer need */
583 objRelease(module, CORE_COMPONENT);
584 objRelease(net, LM_NET_FILENAME);
585 objRelease(ruleset, CORE_COMPONENT);
586 ENDObjClassExit(conf)
587
588
589 /* Initialize our class. Must be called as the very first method
590 * before anything else is called inside this class.
591 * rgerhards, 2008-02-29
592 */
593 BEGINAbstractObjClassInit(conf, 1, OBJ_IS_CORE_MODULE) /* class, version - CHANGE class also in END MACRO! */
594 /* request objects we use */
595 CHKiRet(objUse(module, CORE_COMPONENT));
596 CHKiRet(objUse(net, LM_NET_FILENAME));
597 CHKiRet(objUse(ruleset, CORE_COMPONENT));
598
599 /* These commands will NOT be supported -- the new v6.3 config system provides
600 * far better methods. We will remove the related code soon. -- rgerhards, 2012-01-09
601 */
602 CHKiRet(
603 regCfSysLineHdlr((uchar *)"resetconfigvariables", 1, eCmdHdlrCustomHandler, resetConfigVariables, NULL, NULL));
604 ENDObjClassInit(conf)