2
* Implementation of the configuration system line object.
4
* File begun on 2007-07-30 by RGerhards
6
* Copyright 2007 Rainer Gerhards and Adiscon GmbH.
8
* This program is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU General Public License
10
* as published by the Free Software Foundation; either version 2
11
* of the License, or (at your option) any later version.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program; if not, write to the Free Software
20
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
* A copy of the GPL can be found in the file "COPYING" in this distribution.
36
#include "syslogd.h" /* TODO: when the module interface & library design is done, this should be able to go away */
37
#include "cfsysline.h"
42
linkedList_t llCmdList; /* this is NOT a pointer - no typo here ;) */
44
/* --------------- START functions for handling canned syntaxes --------------- */
47
/* parse a character from the config line
48
* added 2007-07-17 by rgerhards
49
* TODO: enhance this function to handle different classes of characters
50
* HINT: check if char is ' and, if so, use 'c' where c may also be things
53
static rsRetVal doGetChar(uchar **pp, rsRetVal (*pSetHdlr)(void*, uid_t), void *pVal)
60
skipWhiteSpace(pp); /* skip over any whitespace */
62
/* if we are not at a '\0', we have our new char - no validity checks here... */
64
logerror("No character available");
65
iRet = RS_RET_NOT_FOUND;
67
if(pSetHdlr == NULL) {
68
/* we should set value directly to var */
69
*((uchar*)pVal) = **pp;
71
/* we set value via a set function */
72
CHKiRet(pSetHdlr(pVal, **pp));
74
++(*pp); /* eat processed char */
82
/* Parse a number from the configuration line. This is more or less
83
* a shell to call the custom handler.
84
* rgerhards, 2007-07-31
86
static rsRetVal doCustomHdlr(uchar **pp, rsRetVal (*pSetHdlr)(uchar**, void*), void *pVal)
93
CHKiRet(pSetHdlr(pp, pVal));
100
/* Parse a number from the configuration line.
101
* rgerhards, 2007-07-31
103
static rsRetVal doGetInt(uchar **pp, rsRetVal (*pSetHdlr)(void*, uid_t), void *pVal)
112
skipWhiteSpace(pp); /* skip over any whitespace */
115
if(!isdigit((int) *p)) {
117
logerror("invalid number");
118
ABORT_FINALIZE(RS_RET_INVALID_INT);
122
for(i = 0 ; *p && isdigit((int) *p) ; ++p)
123
i = i * 10 + *p - '0';
125
if(pSetHdlr == NULL) {
126
/* we should set value directly to var */
129
/* we set value via a set function */
130
CHKiRet(pSetHdlr(pVal, i));
140
/* Parse and interpet a $FileCreateMode and $umask line. This function
141
* pulls the creation mode and, if successful, stores it
142
* into the global variable so that the rest of rsyslogd
143
* opens files with that mode. Any previous value will be
145
* HINT: if we store the creation mode in selector_t, we
146
* can even specify multiple modes simply be virtue of
147
* being placed in the right section of rsyslog.conf
148
* rgerhards, 2007-07-4 (happy independence day to my US friends!)
149
* Parameter **pp has a pointer to the current config line.
150
* On exit, it will be updated to the processed position.
152
static rsRetVal doFileCreateMode(uchar **pp, rsRetVal (*pSetHdlr)(void*, uid_t), void *pVal)
156
uchar errMsg[128]; /* for dynamic error messages */
162
skipWhiteSpace(pp); /* skip over any whitespace */
165
/* for now, we parse and accept only octal numbers
166
* Sequence of tests is important, we are using boolean shortcuts
167
* to avoid addressing invalid memory!
170
&& (*(p+1) && *(p+1) >= '0' && *(p+1) <= '7')
171
&& (*(p+2) && *(p+2) >= '0' && *(p+2) <= '7')
172
&& (*(p+3) && *(p+3) >= '0' && *(p+3) <= '7') ) ) {
173
snprintf((char*) errMsg, sizeof(errMsg)/sizeof(uchar),
174
"value must be octal (e.g 0644).");
176
logerror((char*) errMsg);
177
ABORT_FINALIZE(RS_RET_INVALID_VALUE);
180
/* we reach this code only if the octal number is ok - so we can now
183
iVal = (*(p+1)-'0') * 64 + (*(p+2)-'0') * 8 + (*(p+3)-'0');
185
if(pSetHdlr == NULL) {
186
/* we should set value directly to var */
187
*((int*)pVal) = iVal;
189
/* we set value via a set function */
190
CHKiRet(pSetHdlr(pVal, iVal));
193
p += 4; /* eat the octal number */
201
/* Parse and interpret an on/off inside a config file line. This is most
202
* often used for boolean options, but of course it may also be used
203
* for other things. The passed-in pointer is updated to point to
204
* the first unparsed character on exit. Function emits error messages
205
* if the value is neither on or off. It returns 0 if the option is off,
206
* 1 if it is on and another value if there was an error.
207
* rgerhards, 2007-07-15
209
static int doParseOnOffOption(uchar **pp)
218
skipWhiteSpace(pp); /* skip over any whitespace */
220
if(getSubString(pp, (char*) szOpt, sizeof(szOpt) / sizeof(uchar), ' ') != 0) {
221
logerror("Invalid $-configline - could not extract on/off option");
225
if(!strcmp((char*)szOpt, "on")) {
227
} else if(!strcmp((char*)szOpt, "off")) {
230
logerrorSz("Option value must be on or off, but is '%s'", (char*)pOptStart);
236
/* extract a groupname and return its gid.
237
* rgerhards, 2007-07-17
239
static rsRetVal doGetGID(uchar **pp, rsRetVal (*pSetHdlr)(void*, uid_t), void *pVal)
245
char stringBuf[2048]; /* I hope this is large enough... */
250
if(getSubString(pp, (char*) szName, sizeof(szName) / sizeof(uchar), ' ') != 0) {
251
logerror("could not extract group name");
252
ABORT_FINALIZE(RS_RET_NOT_FOUND);
255
getgrnam_r((char*)szName, &gBuf, stringBuf, sizeof(stringBuf), &pgBuf);
258
logerrorSz("ID for group '%s' could not be found or error", (char*)szName);
259
iRet = RS_RET_NOT_FOUND;
261
if(pSetHdlr == NULL) {
262
/* we should set value directly to var */
263
*((gid_t*)pVal) = pgBuf->gr_gid;
265
/* we set value via a set function */
266
CHKiRet(pSetHdlr(pVal, pgBuf->gr_gid));
268
dbgprintf("gid %d obtained for group '%s'\n", pgBuf->gr_gid, szName);
271
skipWhiteSpace(pp); /* skip over any whitespace */
278
/* extract a username and return its uid.
279
* rgerhards, 2007-07-17
281
static rsRetVal doGetUID(uchar **pp, rsRetVal (*pSetHdlr)(void*, uid_t), void *pVal)
283
struct passwd *ppwBuf;
287
char stringBuf[2048]; /* I hope this is large enough... */
292
if(getSubString(pp, (char*) szName, sizeof(szName) / sizeof(uchar), ' ') != 0) {
293
logerror("could not extract user name");
294
ABORT_FINALIZE(RS_RET_NOT_FOUND);
297
getpwnam_r((char*)szName, &pwBuf, stringBuf, sizeof(stringBuf), &ppwBuf);
300
logerrorSz("ID for user '%s' could not be found or error", (char*)szName);
301
iRet = RS_RET_NOT_FOUND;
303
if(pSetHdlr == NULL) {
304
/* we should set value directly to var */
305
*((uid_t*)pVal) = ppwBuf->pw_uid;
307
/* we set value via a set function */
308
CHKiRet(pSetHdlr(pVal, ppwBuf->pw_uid));
310
dbgprintf("uid %d obtained for user '%s'\n", ppwBuf->pw_uid, szName);
313
skipWhiteSpace(pp); /* skip over any whitespace */
320
/* Parse and process an binary cofig option. pVal must be
321
* a pointer to an integer which is to receive the option
323
* rgerhards, 2007-07-15
325
static rsRetVal doBinaryOptionLine(uchar **pp, rsRetVal (*pSetHdlr)(void*, int), void *pVal)
333
if((iOption = doParseOnOffOption(pp)) == -1)
334
return RS_RET_ERR; /* nothing left to do */
336
if(pSetHdlr == NULL) {
337
/* we should set value directly to var */
338
*((int*)pVal) = iOption;
340
/* we set value via a set function */
341
CHKiRet(pSetHdlr(pVal, iOption));
344
skipWhiteSpace(pp); /* skip over any whitespace */
351
/* Parse and a word config line option. A word is a consequitive
352
* sequence of non-whitespace characters. pVal must be
353
* a pointer to a string which is to receive the option
354
* value. The returned string must be freed by the caller.
355
* rgerhards, 2007-09-07
357
static rsRetVal doGetWord(uchar **pp, rsRetVal (*pSetHdlr)(void*, uchar*), void *pVal)
367
if((pStrB = rsCStrConstruct()) == NULL)
368
return RS_RET_OUT_OF_MEMORY;
370
/* parse out the word */
373
while(*p && !isspace((int) *p)) {
374
CHKiRet(rsCStrAppendChar(pStrB, *p++));
376
CHKiRet(rsCStrFinish(pStrB));
378
CHKiRet(rsCStrConvSzStrAndDestruct(pStrB, &pNewVal, 0));
380
/* we got the word, now set it */
381
if(pSetHdlr == NULL) {
382
/* we should set value directly to var */
383
*((uchar**)pVal) = pNewVal;
385
/* we set value via a set function */
386
CHKiRet(pSetHdlr(pVal, pNewVal));
390
skipWhiteSpace(pp); /* skip over any whitespace */
393
if(iRet != RS_RET_OK) {
395
rsCStrDestruct(pStrB);
402
/* --------------- END functions for handling canned syntaxes --------------- */
404
/* destructor for cslCmdHdlr
405
* pThis is actually a cslCmdHdlr_t, but we do not cast it as all we currently
406
* need to do is free it.
408
static rsRetVal cslchDestruct(void *pThis)
410
assert(pThis != NULL);
417
/* constructor for cslCmdHdlr
419
static rsRetVal cslchConstruct(cslCmdHdlr_t **ppThis)
424
assert(ppThis != NULL);
425
if((pThis = calloc(1, sizeof(cslCmdHdlr_t))) == NULL) {
426
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY);
434
/* set data members for this object
436
rsRetVal cslchSetEntry(cslCmdHdlr_t *pThis, ecslCmdHdrlType eType, rsRetVal (*pHdlr)(), void *pData)
438
assert(pThis != NULL);
439
assert(eType != eCmdHdlrInvalid);
441
pThis->eType = eType;
442
pThis->cslCmdHdlr = pHdlr;
443
pThis->pData = pData;
449
/* call the specified handler
451
static rsRetVal cslchCallHdlr(cslCmdHdlr_t *pThis, uchar **ppConfLine)
454
rsRetVal (*pHdlr)() = NULL;
455
assert(pThis != NULL);
456
assert(ppConfLine != NULL);
458
switch(pThis->eType) {
459
case eCmdHdlrCustomHandler:
460
pHdlr = doCustomHdlr;
469
pHdlr = doBinaryOptionLine;
471
case eCmdHdlrFileCreateMode:
472
pHdlr = doFileCreateMode;
477
case eCmdHdlrGetChar:
480
case eCmdHdlrGetWord:
484
iRet = RS_RET_NOT_IMPLEMENTED;
488
/* we got a pointer to the handler, so let's call it */
489
assert(pHdlr != NULL);
490
CHKiRet(pHdlr(ppConfLine, pThis->cslCmdHdlr, pThis->pData));
497
/* ---------------------------------------------------------------------- *
498
* now come the handlers for cslCmd_t
499
* ---------------------------------------------------------------------- */
501
/* destructor for a cslCmd list key (a string as of now)
503
static rsRetVal cslcKeyDestruct(void *pData)
505
free(pData); /* we do not need to cast as all we do is free it anyway... */
509
/* destructor for cslCmd
511
static rsRetVal cslcDestruct(void *pData)
513
cslCmd_t *pThis = (cslCmd_t*) pData;
515
assert(pThis != NULL);
517
llDestroy(&pThis->llCmdHdlrs);
524
/* constructor for cslCmd
526
static rsRetVal cslcConstruct(cslCmd_t **ppThis, int bChainingPermitted)
531
assert(ppThis != NULL);
532
if((pThis = calloc(1, sizeof(cslCmd_t))) == NULL) {
533
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY);
536
pThis->bChainingPermitted = bChainingPermitted;
538
CHKiRet(llInit(&pThis->llCmdHdlrs, cslchDestruct, NULL, NULL));
546
/* add a handler entry to a known command
548
static rsRetVal cslcAddHdlr(cslCmd_t *pThis, ecslCmdHdrlType eType, rsRetVal (*pHdlr)(), void *pData)
551
cslCmdHdlr_t *pCmdHdlr = NULL;
553
assert(pThis != NULL);
555
CHKiRet(cslchConstruct(&pCmdHdlr));
556
CHKiRet(cslchSetEntry(pCmdHdlr, eType, pHdlr, pData));
557
CHKiRet(llAppend(&pThis->llCmdHdlrs, NULL, pCmdHdlr));
560
if(iRet != RS_RET_OK) {
562
cslchDestruct(pCmdHdlr);
569
/* function that initializes this module here. This is primarily a hook
572
rsRetVal cfsyslineInit(void)
576
CHKiRet(llInit(&llCmdList, cslcDestruct, cslcKeyDestruct, strcasecmp));
583
/* function that registers cfsysline handlers.
584
* The supplied pCmdName is copied and a new buffer is allocated. This
585
* buffer is automatically destroyed when the element is freed, the
586
* caller does not need to take care of that. The caller must, however,
587
* free pCmdName if he allocated it dynamically! -- rgerhards, 2007-08-09
589
rsRetVal regCfSysLineHdlr(uchar *pCmdName, int bChainingPermitted, ecslCmdHdrlType eType, rsRetVal (*pHdlr)(), void *pData)
595
iRet = llFind(&llCmdList, (void *) pCmdName, (void*) &pThis);
596
if(iRet == RS_RET_NOT_FOUND) {
598
CHKiRet(cslcConstruct(&pThis, bChainingPermitted));
599
CHKiRet_Hdlr(cslcAddHdlr(pThis, eType, pHdlr, pData)) {
603
/* important: add to list, AFTER everything else is OK. Else
604
* we mess up things in the error case.
606
if((pMyCmdName = (uchar*) strdup((char*)pCmdName)) == NULL) {
608
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY);
610
CHKiRet_Hdlr(llAppend(&llCmdList, pMyCmdName, (void*) pThis)) {
615
/* command already exists, are we allowed to chain? */
616
if(pThis->bChainingPermitted == 0 || bChainingPermitted == 0) {
617
ABORT_FINALIZE(RS_RET_CHAIN_NOT_PERMITTED);
619
CHKiRet_Hdlr(cslcAddHdlr(pThis, eType, pHdlr, pData)) {
630
rsRetVal unregCfSysLineHdlrs(void)
632
return llDestroy(&llCmdList);
636
/* process a cfsysline command (based on handler structure)
637
* param "p" is a pointer to the command line after the command. Should be
640
rsRetVal processCfSysLineCommand(uchar *pCmdName, uchar **p)
643
rsRetVal iRetLL; /* for linked list handling */
645
cslCmdHdlr_t *pCmdHdlr;
646
linkedListCookie_t llCookieCmdHdlr;
647
uchar *pHdlrP; /* the handler's private p (else we could only call one handler) */
648
int bWasOnceOK; /* was the result of an handler at least once RS_RET_OK? */
649
uchar *pOKp = NULL; /* returned conf line pointer when it was OK */
651
iRet = llFind(&llCmdList, (void *) pCmdName, (void*) &pCmd);
653
if(iRet == RS_RET_NOT_FOUND) {
654
logerror("invalid or yet-unknown config file command - have you forgotten to load a module?");
657
if(iRet != RS_RET_OK)
660
llCookieCmdHdlr = NULL;
662
while((iRetLL = llGetNextElt(&pCmd->llCmdHdlrs, &llCookieCmdHdlr, (void*)&pCmdHdlr)) == RS_RET_OK) {
663
/* for the time being, we ignore errors during handlers. The
664
* reason is that handlers are independent. An error in one
665
* handler does not necessarily mean that another one will
666
* fail, too. Later, we might add a config variable to control
667
* this behaviour (but I am not sure if that is rally
668
* necessary). -- rgerhards, 2007-07-31
671
if((iRet = cslchCallHdlr(pCmdHdlr, &pHdlrP)) == RS_RET_OK) {
677
if(bWasOnceOK == 1) {
682
if(iRetLL != RS_RET_END_OF_LINKEDLIST)
690
/* debug print the command handler structure
692
void dbgPrintCfSysLineHandlers(void)
697
cslCmdHdlr_t *pCmdHdlr;
698
linkedListCookie_t llCookieCmd;
699
linkedListCookie_t llCookieCmdHdlr;
702
printf("\nSytem Line Configuration Commands:\n");
704
while((iRet = llGetNextElt(&llCmdList, &llCookieCmd, (void*)&pCmd)) == RS_RET_OK) {
705
llGetKey(llCookieCmd, (void*) &pKey); /* TODO: using the cookie is NOT clean! */
706
printf("\tCommand '%s':\n", pKey);
707
llCookieCmdHdlr = NULL;
708
while((iRet = llGetNextElt(&pCmd->llCmdHdlrs, &llCookieCmdHdlr, (void*)&pCmdHdlr)) == RS_RET_OK) {
709
printf("\t\ttype : %d\n", pCmdHdlr->eType);
710
printf("\t\tpData: 0x%x\n", (unsigned) pCmdHdlr->pData);
711
printf("\t\tHdlr : 0x%x\n", (unsigned) pCmdHdlr->cslCmdHdlr);