Blob Blame History Raw
From 4d68698ecaa4b13927352ce6bff74a0b6767abca Mon Sep 17 00:00:00 2001
From: Tomas Heinrich <theinric@redhat.com>
Date: Fri, 24 Apr 2015 23:51:20 +0200
Subject: [PATCH] imjournal: enable setting a default message tag

---
 plugins/imjournal/imjournal.c | 62 ++++++++++++++++++++++++++++++-------------
 1 file changed, 43 insertions(+), 19 deletions(-)

diff --git a/plugins/imjournal/imjournal.c b/plugins/imjournal/imjournal.c
index b6910e8..cd22c3a 100755
--- a/plugins/imjournal/imjournal.c
+++ b/plugins/imjournal/imjournal.c
@@ -72,6 +72,7 @@ static struct configSettings_s {
 	int bIgnorePrevious;
 	int iDfltSeverity;
 	int iDfltFacility;
+	char *dfltTag;
 } cs;
 
 static rsRetVal facilityHdlr(uchar **pp, void *pVal);
@@ -84,7 +85,8 @@ static struct cnfparamdescr modpdescr[] = {
 	{ "persiststateinterval", eCmdHdlrInt, 0 },
 	{ "ignorepreviousmessages", eCmdHdlrBinary, 0 },
 	{ "defaultseverity", eCmdHdlrSeverity, 0 },
-	{ "defaultfacility", eCmdHdlrString, 0 }
+	{ "defaultfacility", eCmdHdlrString, 0 },
+	{ "defaulttag", eCmdHdlrGetWord, 0 },
 };
 static struct cnfparamblk modpblk =
 	{ CNFPARAMBLK_VERSION,
@@ -95,6 +97,7 @@ static struct cnfparamblk modpblk =
 #define DFLT_persiststateinterval 10
 #define DFLT_SEVERITY LOG_PRI(LOG_NOTICE)
 #define DFLT_FACILITY LOG_FAC(LOG_USER)
+#define DFLT_TAG "journal"
 
 static int bLegacyCnfModGlobalsPermitted = 1;/* are legacy module-global config parameters permitted? */
 
@@ -183,8 +186,13 @@ enqMsg(uchar *msg, uchar *pszTag, int iFacility, int iSeverity, struct timeval *
 	}
 	MsgSetFlowControlType(pMsg, eFLOWCTL_LIGHT_DELAY);
 	MsgSetInputName(pMsg, pInputName);
+	/* Recalculating the message length shouldn't cause problems as all
+	 * potential zero-bytes have been escaped in sanitizeValue(). */
 	len = strlen((char*)msg);
 	MsgSetRawMsg(pMsg, (char*)msg, len);
+	/* NB: SanitizeMsg() only touches the raw message and its
+	 * length which only contain the msg part. Thus the TAG and
+	 * other fields are not sanitized. */
 	if(len > 0)
 		parser.SanitizeMsg(pMsg);
 	MsgSetMSGoffs(pMsg, 0);	/* we do not have a header... */
@@ -221,7 +229,7 @@ readjournal() {
 
 	/* Information from messages */
 	char *message = NULL;
-	char *sys_iden;
+	char *sys_iden = NULL;
 	char *sys_iden_help = NULL;
 
 	const void *get;
@@ -283,27 +291,37 @@ readjournal() {
 	/* Get message identifier, client pid and add ':' */
 	if (sd_journal_get_data(j, "SYSLOG_IDENTIFIER", &get, &length) >= 0) {
 		CHKiRet(sanitizeValue(((const char *)get) + 18, length - 18, &sys_iden));
-	} else {
-		CHKmalloc(sys_iden = strdup("journal"));
 	}
 
-	if (sd_journal_get_data(j, "SYSLOG_PID", &pidget, &pidlength) >= 0) {
-		char *sys_pid;
+	if (sys_iden == NULL && !cs.dfltTag[0]) {
+		/* This is a special case: if no tag was obtained from
+		 * the message and the user has set the default tag to
+		 * an empty string, nothing is inserted.
+		 */
+		CHKmalloc(sys_iden_help = calloc(1, 1));
+	} else {
+		if (sys_iden == NULL) {
+			/* Use a predefined tag if it can't be obtained from the message */
+			CHKmalloc(sys_iden = strdup(cs.dfltTag));
+		}
+		if (sd_journal_get_data(j, "SYSLOG_PID", &pidget, &pidlength) >= 0) {
+			char *sys_pid;
 
-		CHKiRet_Hdlr(sanitizeValue(((const char *)pidget) + 11, pidlength - 11, &sys_pid)) {
-			free (sys_iden);
-			FINALIZE;
+			CHKiRet_Hdlr(sanitizeValue(((const char *)pidget) + 11, pidlength - 11, &sys_pid)) {
+				free (sys_iden);
+				FINALIZE;
+			}
+			r = asprintf(&sys_iden_help, "%s[%s]:", sys_iden, sys_pid);
+			free (sys_pid);
+		} else {
+			r = asprintf(&sys_iden_help, "%s:", sys_iden);
 		}
-		r = asprintf(&sys_iden_help, "%s[%s]:", sys_iden, sys_pid);
-		free (sys_pid);
-	} else {
-		r = asprintf(&sys_iden_help, "%s:", sys_iden);
-	}
 
-	free (sys_iden);
+		free (sys_iden);
 
-	if (-1 == r) {
-		ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY);
+		if (-1 == r) {
+			ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY);
+		}
 	}
 
 	json = json_object_new_object();
@@ -628,11 +646,14 @@ CODESTARTbeginCnfLoad
 	cs.ratelimitInterval = 600;
 	cs.iDfltSeverity = DFLT_SEVERITY;
 	cs.iDfltFacility = DFLT_FACILITY;
+	cs.dfltTag = NULL;
 ENDbeginCnfLoad
 
 
 BEGINendCnfLoad
 CODESTARTendCnfLoad
+	if (cs.dfltTag == NULL)
+		cs.dfltTag = strdup(DFLT_TAG);
 ENDendCnfLoad
 
 
@@ -648,6 +669,7 @@ ENDactivateCnf
 
 BEGINfreeCnf
 CODESTARTfreeCnf
+	free(cs.dfltTag);
 ENDfreeCnf
 
 /* open journal */
@@ -728,6 +750,8 @@ CODESTARTsetModCnf
 			fac = p = es_str2cstr(pvals[i].val.d.estr, NULL);
 			facilityHdlr((uchar **) &p, (void *) &cs.iDfltFacility);
 			free(fac);
+		} else if (!strcmp(modpblk.descr[i].name, "defaulttag")) {
+			cs.dfltTag = (char *)es_str2cstr(pvals[i].val.d.estr, NULL);
 		} else {
 			dbgprintf("imjournal: program error, non-handled "
 				"param '%s' in beginCnfLoad\n", modpblk.descr[i].name);
@@ -786,8 +810,8 @@ CODEmodInit_QueryRegCFSLineHdlr
 		NULL, &cs.iDfltSeverity, STD_LOADABLE_MODULE_ID));
 	CHKiRet(omsdRegCFSLineHdlr((uchar *)"imjournaldefaultfacility", 0, eCmdHdlrCustomHandler,
 		facilityHdlr, &cs.iDfltFacility, STD_LOADABLE_MODULE_ID));
-
-
+	CHKiRet(omsdRegCFSLineHdlr((uchar *)"imjournaldefaulttag", 0, eCmdHdlrGetWord,
+		NULL, &cs.dfltTag, STD_LOADABLE_MODULE_ID));
 ENDmodInit
 /* vim:set ai:
  */
-- 
1.9.3