Blame SOURCES/rsyslog-7.4.7-rhbz1223566-imrelp-multi-ruleset.patch

14695b
From c661a2917d065339e00d7588320a653ba29792ea Mon Sep 17 00:00:00 2001
14695b
From: Tomas Heinrich <theinric@redhat.com>
14695b
Date: Mon, 27 Jun 2016 20:10:31 +0200
14695b
Subject: [PATCH] Allow multiple rulesets in imrelp
14695b
14695b
Disable legacy module config directives if new style was used
14695b
---
14695b
 plugins/imrelp/imrelp.c | 140 ++++++++++++++++++++++++++++++++++++++----------
14695b
 1 file changed, 113 insertions(+), 27 deletions(-)
14695b
14695b
diff --git a/plugins/imrelp/imrelp.c b/plugins/imrelp/imrelp.c
14695b
index 5e0ae55..203e7cb 100644
14695b
--- a/plugins/imrelp/imrelp.c
14695b
+++ b/plugins/imrelp/imrelp.c
14695b
@@ -75,6 +75,8 @@ static struct configSettings_s {
14695b
 struct instanceConf_s {
14695b
 	uchar *pszBindPort;		/* port to bind to */
14695b
 	struct instanceConf_s *next;
14695b
+	uchar *pszBindRuleset;          /* name of ruleset to bind to */
14695b
+	ruleset_t *pBindRuleset;        /* ruleset to bind listener to */
14695b
 };
14695b
 
14695b
 
14695b
@@ -88,9 +90,20 @@ struct modConfData_s {
14695b
 static modConfData_t *loadModConf = NULL;/* modConf ptr to use for the current load process */
14695b
 static modConfData_t *runModConf = NULL;/* modConf ptr to use for the current load process */
14695b
 
14695b
+/* module-global parameters */
14695b
+static struct cnfparamdescr modpdescr[] = {
14695b
+       { "ruleset", eCmdHdlrGetWord, 0 },
14695b
+};
14695b
+static struct cnfparamblk modpblk =
14695b
+       { CNFPARAMBLK_VERSION,
14695b
+         sizeof(modpdescr)/sizeof(struct cnfparamdescr),
14695b
+         modpdescr
14695b
+       };
14695b
+
14695b
 /* input instance parameters */
14695b
 static struct cnfparamdescr inppdescr[] = {
14695b
-	{ "port", eCmdHdlrString, CNFPARAM_REQUIRED }
14695b
+	{ "port", eCmdHdlrString, CNFPARAM_REQUIRED },
14695b
+	{ "ruleset", eCmdHdlrGetWord, 0 }
14695b
 };
14695b
 static struct cnfparamblk inppblk =
14695b
 	{ CNFPARAMBLK_VERSION,
14695b
@@ -99,6 +112,9 @@ static struct cnfparamblk inppblk =
14695b
 	};
14695b
 
14695b
 
14695b
+#include "im-helper.h" /* must be included AFTER the type definitions! */
14695b
+static int bLegacyCnfModGlobalsPermitted;/* are legacy module-global config parameters permitted? */
14695b
+
14695b
 
14695b
 /* ------------------------------ callbacks ------------------------------ */
14695b
 
14695b
@@ -113,17 +129,18 @@ static struct cnfparamblk inppblk =
14695b
  * we will only see the hostname (twice). -- rgerhards, 2009-10-14
14695b
  */
14695b
 static relpRetVal
14695b
-onSyslogRcv(uchar *pHostname, uchar *pIP, uchar *msg, size_t lenMsg)
14695b
+onSyslogRcv(void *pUsr, uchar *pHostname, uchar *pIP, uchar *msg, size_t lenMsg)
14695b
 {
14695b
 	prop_t *pProp = NULL;
14695b
 	msg_t *pMsg;
14695b
+	instanceConf_t *inst = (instanceConf_t*) pUsr;
14695b
 	DEFiRet;
14695b
 
14695b
 	CHKiRet(msgConstruct(&pMsg));
14695b
 	MsgSetInputName(pMsg, pInputName);
14695b
 	MsgSetRawMsg(pMsg, (char*)msg, lenMsg);
14695b
 	MsgSetFlowControlType(pMsg, eFLOWCTL_LIGHT_DELAY);
14695b
-	MsgSetRuleset(pMsg, runModConf->pBindRuleset);
14695b
+	MsgSetRuleset(pMsg, inst->pBindRuleset);
14695b
 	pMsg->msgFlags  = PARSE_HOSTNAME | NEEDS_PARSING;
14695b
 
14695b
 	/* TODO: optimize this, we can store it inside the session, requires
14695b
@@ -155,6 +172,8 @@ createInstance(instanceConf_t **pinst)
14695b
 	inst->next = NULL;
14695b
 
14695b
 	inst->pszBindPort = NULL;
14695b
+	inst->pszBindRuleset = NULL;
14695b
+	inst->pBindRuleset = NULL;
14695b
 
14695b
 	/* node created, let's add to config */
14695b
 	if(loadModConf->tail == NULL) {
14695b
@@ -174,8 +193,10 @@ finalize_it:
14695b
 static inline void
14695b
 std_checkRuleset_genErrMsg(modConfData_t *modConf, __attribute__((unused)) instanceConf_t *inst)
14695b
 {
14695b
-	errmsg.LogError(0, NO_ERRCODE, "imrelp: ruleset '%s' not found - "
14695b
-			"using default ruleset instead", modConf->pszBindRuleset);
14695b
+	errmsg.LogError(0, NO_ERRCODE, "imrelp[%s]: ruleset '%s' not found - "
14695b
+			"using default ruleset instead",
14695b
+			inst->pszBindPort, inst->pszBindRuleset);
14695b
+
14695b
 }
14695b
 
14695b
 
14695b
@@ -196,6 +217,14 @@ static rsRetVal addInstance(void __attribute__((unused)) *pVal, uchar *pNewVal)
14695b
 	}
14695b
 	inst->pszBindPort = pNewVal;
14695b
 
14695b
+	if((cs.pszBindRuleset == NULL) || (cs.pszBindRuleset[0] == '\0')) {
14695b
+		inst->pszBindRuleset = NULL;
14695b
+	} else {
14695b
+		CHKmalloc(inst->pszBindRuleset = ustrdup(cs.pszBindRuleset));
14695b
+	}
14695b
+	inst->pBindRuleset = NULL;
14695b
+
14695b
+
14695b
 finalize_it:
14695b
 	RETiRet;
14695b
 }
14695b
@@ -205,18 +234,33 @@ static rsRetVal
14695b
 addListner(modConfData_t __attribute__((unused)) *modConf, instanceConf_t *inst)
14695b
 {
14695b
 	DEFiRet;
14695b
+	relpSrv_t *pSrv;
14695b
+	int relpRet;
14695b
+
14695b
 	if(pRelpEngine == NULL) {
14695b
 		CHKiRet(relpEngineConstruct(&pRelpEngine));
14695b
 		CHKiRet(relpEngineSetDbgprint(pRelpEngine, dbgprintf));
14695b
 		CHKiRet(relpEngineSetFamily(pRelpEngine, glbl.GetDefPFFamily()));
14695b
 		CHKiRet(relpEngineSetEnableCmd(pRelpEngine, (uchar*) "syslog", eRelpCmdState_Required));
14695b
-		CHKiRet(relpEngineSetSyslogRcv(pRelpEngine, onSyslogRcv));
14695b
+		CHKiRet(relpEngineSetSyslogRcv2(pRelpEngine, onSyslogRcv));
14695b
 		if (!glbl.GetDisableDNS()) {
14695b
 			CHKiRet(relpEngineSetDnsLookupMode(pRelpEngine, 1));
14695b
 		}
14695b
 	}
14695b
 
14695b
-	CHKiRet(relpEngineAddListner(pRelpEngine, inst->pszBindPort));
14695b
+	CHKiRet(relpEngineListnerConstruct(pRelpEngine, &pSrv));
14695b
+	CHKiRet(relpSrvSetLstnPort(pSrv, inst->pszBindPort));
14695b
+
14695b
+	relpSrvSetUsrPtr(pSrv, inst);
14695b
+
14695b
+	relpRet = relpEngineListnerConstructFinalize(pRelpEngine, pSrv);
14695b
+	if(relpRet != RELP_RET_OK) {
14695b
+		errmsg.LogError(0, RS_RET_RELP_ERR,
14695b
+				"imrelp: could not activate relp listner, code %d", relpRet);
14695b
+		ABORT_FINALIZE(RS_RET_RELP_ERR);
14695b
+	}
14695b
+
14695b
+	resetConfigVariables(NULL,NULL);
14695b
 
14695b
 finalize_it:
14695b
 	RETiRet;
14695b
@@ -249,6 +293,8 @@ CODESTARTnewInpInst
14695b
 			continue;
14695b
 		if(!strcmp(inppblk.descr[i].name, "port")) {
14695b
 			inst->pszBindPort = (uchar*)es_str2cstr(pvals[i].val.d.estr, NULL);
14695b
+		} else if(!strcmp(inppblk.descr[i].name, "ruleset")) {
14695b
+			inst->pszBindRuleset = (uchar*)es_str2cstr(pvals[i].val.d.estr, NULL);
14695b
 		} else {
14695b
 			dbgprintf("imrelp: program error, non-handled "
14695b
 			  "param '%s'\n", inppblk.descr[i].name);
14695b
@@ -264,41 +310,76 @@ BEGINbeginCnfLoad
14695b
 CODESTARTbeginCnfLoad
14695b
 	loadModConf = pModConf;
14695b
 	pModConf->pConf = pConf;
14695b
+	pModConf->pszBindRuleset = NULL;
14695b
 	/* init legacy config variables */
14695b
 	cs.pszBindRuleset = NULL;
14695b
+	bLegacyCnfModGlobalsPermitted = 1;
14695b
 ENDbeginCnfLoad
14695b
 
14695b
+BEGINsetModCnf
14695b
+	struct cnfparamvals *pvals = NULL;
14695b
+	int i;
14695b
+CODESTARTsetModCnf
14695b
+	pvals = nvlstGetParams(lst, &modpblk, NULL);
14695b
+	if(pvals == NULL) {
14695b
+		errmsg.LogError(0, RS_RET_MISSING_CNFPARAMS, "error processing module "
14695b
+				"config parameters [module(...)]");
14695b
+		ABORT_FINALIZE(RS_RET_MISSING_CNFPARAMS);
14695b
+	}
14695b
+
14695b
+	if(Debug) {
14695b
+		dbgprintf("module (global) param blk for imrelp:\n");
14695b
+		cnfparamsPrint(&modpblk, pvals);
14695b
+	}
14695b
+
14695b
+	for(i = 0 ; i < modpblk.nParams ; ++i) {
14695b
+		if(!pvals[i].bUsed)
14695b
+			continue;
14695b
+		if(!strcmp(modpblk.descr[i].name, "ruleset")) {
14695b
+			loadModConf->pszBindRuleset = (uchar*)es_str2cstr(pvals[i].val.d.estr, NULL);
14695b
+		} else {
14695b
+			dbgprintf("imrelp: program error, non-handled "
14695b
+			  "param '%s' in beginCnfLoad\n", modpblk.descr[i].name);
14695b
+		}
14695b
+	}
14695b
+	/* remove all of our legacy module handlers, as they can not used in addition
14695b
+	 * the the new-style config method.
14695b
+	 */
14695b
+	bLegacyCnfModGlobalsPermitted = 0;
14695b
+finalize_it:
14695b
+	if(pvals != NULL)
14695b
+		cnfparamvalsDestruct(pvals, &modpblk);
14695b
+ENDsetModCnf
14695b
 
14695b
 BEGINendCnfLoad
14695b
 CODESTARTendCnfLoad
14695b
-	if((cs.pszBindRuleset == NULL) || (cs.pszBindRuleset[0] == '\0')) {
14695b
-		loadModConf->pszBindRuleset = NULL;
14695b
+	if(loadModConf->pszBindRuleset == NULL) {
14695b
+		if((cs.pszBindRuleset == NULL) || (cs.pszBindRuleset[0] == '\0')) {
14695b
+			loadModConf->pszBindRuleset = NULL;
14695b
+		} else {
14695b
+			CHKmalloc(loadModConf->pszBindRuleset = ustrdup(cs.pszBindRuleset));
14695b
+		}
14695b
 	} else {
14695b
-		CHKmalloc(loadModConf->pszBindRuleset = ustrdup(cs.pszBindRuleset));
14695b
+		if((cs.pszBindRuleset != NULL) && (cs.pszBindRuleset[0] != '\0')) {
14695b
+			errmsg.LogError(0, RS_RET_DUP_PARAM, "imrelp: warning: ruleset "
14695b
+				       "set via legacy directive ignored");
14695b
+		}
14695b
 	}
14695b
-	loadModConf->pBindRuleset = NULL;
14695b
 finalize_it:
14695b
 	free(cs.pszBindRuleset);
14695b
+	cs.pszBindRuleset = NULL;
14695b
 	loadModConf = NULL; /* done loading */
14695b
 ENDendCnfLoad
14695b
 
14695b
 
14695b
 BEGINcheckCnf
14695b
-	rsRetVal localRet;
14695b
-	ruleset_t *pRuleset;
14695b
+	instanceConf_t *inst;
14695b
 CODESTARTcheckCnf
14695b
-	/* we emulate the standard "ruleset query" code provided by the framework
14695b
-	 * for *instances* (which we can currently not support due to librelp).
14695b
-	 */
14695b
-	if(pModConf->pszBindRuleset == NULL) {
14695b
-		pModConf->pBindRuleset = NULL;
14695b
-	} else {
14695b
-		localRet = ruleset.GetRuleset(pModConf->pConf, &pRuleset, pModConf->pszBindRuleset);
14695b
-		if(localRet == RS_RET_NOT_FOUND) {
14695b
-			std_checkRuleset_genErrMsg(pModConf, NULL);
14695b
+	for(inst = pModConf->root ; inst != NULL ; inst = inst->next) {
14695b
+		if(inst->pszBindRuleset == NULL && pModConf->pszBindRuleset != NULL) {
14695b
+			CHKmalloc(inst->pszBindRuleset = ustrdup(pModConf->pszBindRuleset));
14695b
 		}
14695b
-		CHKiRet(localRet);
14695b
-		pModConf->pBindRuleset = pRuleset;
14695b
+		std_checkRuleset(pModConf, inst);
14695b
 	}
14695b
 finalize_it:
14695b
 ENDcheckCnf
14695b
@@ -311,8 +392,10 @@ CODESTARTactivateCnfPrePrivDrop
14695b
 	for(inst = runModConf->root ; inst != NULL ; inst = inst->next) {
14695b
 		addListner(pModConf, inst);
14695b
 	}
14695b
-	if(pRelpEngine == NULL)
14695b
+	if(pRelpEngine == NULL) {
14695b
+		errmsg.LogError(0, RS_RET_NO_LSTN_DEFINED, "imrelp: no RELP listener defined, module ca  n not run.");
14695b
 		ABORT_FINALIZE(RS_RET_NO_RUN);
14695b
+	}
14695b
 finalize_it:
14695b
 ENDactivateCnfPrePrivDrop
14695b
 
14695b
@@ -326,10 +409,12 @@ BEGINfreeCnf
14695b
 CODESTARTfreeCnf
14695b
 	for(inst = pModConf->root ; inst != NULL ; ) {
14695b
 		free(inst->pszBindPort);
14695b
+		free(inst->pszBindRuleset);
14695b
 		del = inst;
14695b
 		inst = inst->next;
14695b
 		free(del);
14695b
 	}
14695b
+	free(pModConf->pszBindRuleset);
14695b
 ENDfreeCnf
14695b
 
14695b
 /* This is used to terminate the plugin. Note that the signal handler blocks
14695b
@@ -420,6 +505,7 @@ CODEqueryEtryPt_STD_IMOD_QUERIES
14695b
 CODEqueryEtryPt_STD_CONF2_QUERIES
14695b
 CODEqueryEtryPt_STD_CONF2_PREPRIVDROP_QUERIES
14695b
 CODEqueryEtryPt_STD_CONF2_IMOD_QUERIES
14695b
+CODEqueryEtryPt_STD_CONF2_setModCnf_QUERIES
14695b
 CODEqueryEtryPt_IsCompatibleWithFeature_IF_OMOD_QUERIES
14695b
 ENDqueryEtryPt
14695b
 
14695b
@@ -437,8 +523,8 @@ CODEmodInit_QueryRegCFSLineHdlr
14695b
 	CHKiRet(objUse(ruleset, CORE_COMPONENT));
14695b
 
14695b
 	/* register config file handlers */
14695b
-	CHKiRet(omsdRegCFSLineHdlr((uchar *)"inputrelpserverbindruleset", 0, eCmdHdlrGetWord,
14695b
-		NULL, &cs.pszBindRuleset, STD_LOADABLE_MODULE_ID));
14695b
+	CHKiRet(regCfSysLineHdlr2((uchar*)"inputrelpserverbindruleset", 0, eCmdHdlrGetWord,
14695b
+				   NULL, &cs.pszBindRuleset, STD_LOADABLE_MODULE_ID, &bLegacyCnfModGlobalsPermitted));
14695b
 	CHKiRet(omsdRegCFSLineHdlr((uchar *)"inputrelpserverrun", 0, eCmdHdlrGetWord,
14695b
 				   addInstance, NULL, STD_LOADABLE_MODULE_ID));
14695b
 	CHKiRet(omsdRegCFSLineHdlr((uchar *)"resetconfigvariables", 1, eCmdHdlrCustomHandler,
14695b
-- 
14695b
2.5.5
14695b