Blame SOURCES/rsyslog-8.24.0-rhbz1431616-pmrfc3164sd-backport.patch

b65758
diff -up ./configure.ac.pmrfc3164sd ./configure.ac
b65758
--- ./configure.ac.pmrfc3164sd	2017-01-10 10:01:24.000000000 +0100
b65758
+++ ./configure.ac	2017-04-03 10:10:40.912388923 +0200
b65758
@@ -1559,6 +1559,18 @@ AC_ARG_ENABLE(pmpanngfw,
b65758
 )
b65758
 AM_CONDITIONAL(ENABLE_PMPANNGFW, test x$enable_pmpanngfw = xyes)
b65758
 
b65758
+# settings for pmrfc3164sd
b65758
+AC_ARG_ENABLE(pmrfc3164sd,
b65758
+        [AS_HELP_STRING([--enable-pmrfc3164sd],[Compiles rfc3164sd parser module @<:@default=no@:>@])],
b65758
+        [case "${enableval}" in
b65758
+         yes) enable_pmrfc3164sd="yes" ;;
b65758
+          no) enable_pmrfc3164sd="no" ;;
b65758
+           *) AC_MSG_ERROR(bad value ${enableval} for --enable-pmrfc3164sd) ;;
b65758
+         esac],
b65758
+        [enable_pmrfc3164sd=no]
b65758
+)
b65758
+AM_CONDITIONAL(ENABLE_PMRFC3164SD, test x$enable_pmrfc3164sd = xyes)
b65758
+
b65758
 
b65758
 # settings for omruleset
b65758
 AC_ARG_ENABLE(omruleset,
b65758
@@ -1953,6 +1965,7 @@ AC_CONFIG_FILES([Makefile \
b65758
 		plugins/mmexternal/Makefile \
b65758
 		plugins/omstdout/Makefile \
b65758
 		plugins/omjournal/Makefile \
b65758
+		plugins/pmrfc3164sd/Makefile \
b65758
 		plugins/pmciscoios/Makefile \
b65758
 		plugins/pmnull/Makefile \
b65758
 		plugins/omruleset/Makefile \
b65758
@@ -2057,6 +2070,7 @@ echo "    omamqp1 module will be compile
b65758
 echo "    omtcl module will be compiled:            $enable_omtcl"
b65758
 echo
b65758
 echo "---{ parser modules }---"
b65758
+echo "    pmrfc3164sd module will be compiled:      $enable_pmrfc3164sd"
b65758
 echo "    pmlastmsg module will be compiled:        $enable_pmlastmsg"
b65758
 echo "    pmcisconames module will be compiled:     $enable_pmcisconames"
b65758
 echo "    pmciscoios module will be compiled:       $enable_pmciscoios"
b65758
diff -up ./Makefile.am.pmrfc3164sd ./Makefile.am
b65758
--- ./Makefile.am.pmrfc3164sd	2017-01-10 10:00:04.000000000 +0100
b65758
+++ ./Makefile.am	2017-04-03 10:10:40.912388923 +0200
b65758
@@ -115,6 +115,10 @@ if ENABLE_PMLASTMSG
b65758
 SUBDIRS += plugins/pmlastmsg
b65758
 endif
b65758
 
b65758
+if ENABLE_PMRFC3164SD
b65758
+SUBDIRS += plugins/pmrfc3164sd
b65758
+endif
b65758
+
b65758
 if ENABLE_OMRULESET
b65758
 SUBDIRS += plugins/omruleset
b65758
 endif
b65758
diff -up ./plugins/pmrfc3164sd/Makefile.am.pmrfc3164sd ./plugins/pmrfc3164sd/Makefile.am
b65758
--- ./plugins/pmrfc3164sd/Makefile.am.pmrfc3164sd	2017-04-03 10:10:40.912388923 +0200
b65758
+++ ./plugins/pmrfc3164sd/Makefile.am	2017-04-03 10:10:40.912388923 +0200
b65758
@@ -0,0 +1,8 @@
b65758
+pkglib_LTLIBRARIES = pmrfc3164sd.la
b65758
+
b65758
+pmrfc3164sd_la_SOURCES = pmrfc3164sd.c
b65758
+pmrfc3164sd_la_CPPFLAGS =  $(RSRT_CFLAGS) $(PTHREADS_CFLAGS) -I ../../tools
b65758
+pmrfc3164sd_la_LDFLAGS = -module -avoid-version
b65758
+pmrfc3164sd_la_LIBADD = 
b65758
+
b65758
+EXTRA_DIST = 
b65758
diff -up ./plugins/pmrfc3164sd/pmrfc3164sd.c.pmrfc3164sd ./plugins/pmrfc3164sd/pmrfc3164sd.c
b65758
--- ./plugins/pmrfc3164sd/pmrfc3164sd.c.pmrfc3164sd	2017-04-03 10:10:40.913388923 +0200
b65758
+++ ./plugins/pmrfc3164sd/pmrfc3164sd.c	2017-04-03 10:26:52.012341658 +0200
b65758
@@ -0,0 +1,345 @@
b65758
+/* pmrfc3164sd.c
b65758
+ * This is a parser module for RFC3164(legacy syslog)-formatted messages.
b65758
+ *
b65758
+ * NOTE: read comments in module-template.h to understand how this file
b65758
+ *       works!
b65758
+ *
b65758
+ * File begun on 2009-11-04 by RGerhards
b65758
+ *
b65758
+ * Copyright 2007, 2009 Rainer Gerhards and Adiscon GmbH.
b65758
+ *
b65758
+ * This file is part of rsyslog.
b65758
+ *
b65758
+ * Rsyslog is free software: you can redistribute it and/or modify
b65758
+ * it under the terms of the GNU General Public License as published by
b65758
+ * the Free Software Foundation, either version 3 of the License, or
b65758
+ * (at your option) any later version.
b65758
+ *
b65758
+ * Rsyslog is distributed in the hope that it will be useful,
b65758
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
b65758
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
b65758
+ * GNU General Public License for more details.
b65758
+ *
b65758
+ * You should have received a copy of the GNU General Public License
b65758
+ * along with Rsyslog.  If not, see <http://www.gnu.org/licenses/>.
b65758
+ *
b65758
+ * A copy of the GPL can be found in the file "COPYING" in this distribution.
b65758
+ */
b65758
+#include "config.h"
b65758
+#include "rsyslog.h"
b65758
+#include <stdlib.h>
b65758
+#include <string.h>
b65758
+#include <assert.h>
b65758
+#include <errno.h>
b65758
+#include <ctype.h>
b65758
+#include "syslogd.h"
b65758
+#include "conf.h"
b65758
+#include "syslogd-types.h"
b65758
+#include "template.h"
b65758
+#include "msg.h"
b65758
+#include "module-template.h"
b65758
+#include "glbl.h"
b65758
+#include "errmsg.h"
b65758
+#include "parser.h"
b65758
+#include "datetime.h"
b65758
+#include "unicode-helper.h"
b65758
+
b65758
+MODULE_TYPE_PARSER
b65758
+MODULE_TYPE_NOKEEP
b65758
+MODULE_CNFNAME("pmrfc3164sd")
b65758
+PARSER_NAME("contrib.rfc3164sd")
b65758
+
b65758
+/* internal structures
b65758
+ */
b65758
+DEF_PMOD_STATIC_DATA
b65758
+DEFobjCurrIf(errmsg)
b65758
+DEFobjCurrIf(glbl)
b65758
+DEFobjCurrIf(parser)
b65758
+DEFobjCurrIf(datetime)
b65758
+
b65758
+
b65758
+/* static data */
b65758
+static int bParseHOSTNAMEandTAG;	/* cache for the equally-named global param - performance enhancement */
b65758
+
b65758
+
b65758
+BEGINisCompatibleWithFeature
b65758
+CODESTARTisCompatibleWithFeature
b65758
+	if(eFeat == sFEATUREAutomaticSanitazion)
b65758
+		iRet = RS_RET_OK;
b65758
+	if(eFeat == sFEATUREAutomaticPRIParsing)
b65758
+		iRet = RS_RET_OK;
b65758
+ENDisCompatibleWithFeature
b65758
+
b65758
+/* Helper to parseRFCSyslogMsg. This function parses the structured
b65758
+ * data field of a message. It does NOT parse inside structured data,
b65758
+ * just gets the field as whole. Parsing the single entities is left
b65758
+ * to other functions. The parsepointer is advanced
b65758
+ * to after the terminating SP. The caller must ensure that the 
b65758
+ * provided buffer is large enough to hold the to be extracted value.
b65758
+ * Returns 0 if everything is fine or 1 if either the field is not
b65758
+ * SP-terminated or any other error occurs. -- rger, 2005-11-24
b65758
+ * The function now receives the size of the string and makes sure
b65758
+ * that it does not process more than that. The *pLenStr counter is
b65758
+ * updated on exit. -- rgerhards, 2009-09-23
b65758
+ */
b65758
+static int parseRFCStructuredData(uchar **pp2parse, uchar *pResult, int *pLenStr)
b65758
+{
b65758
+	uchar *p2parse;
b65758
+	int bCont = 1;
b65758
+	int iRet = 0;
b65758
+	int lenStr;
b65758
+
b65758
+	assert(pp2parse != NULL);
b65758
+	assert(*pp2parse != NULL);
b65758
+	assert(pResult != NULL);
b65758
+
b65758
+	p2parse = *pp2parse;
b65758
+	lenStr = *pLenStr;
b65758
+
b65758
+	/* this is the actual parsing loop
b65758
+	 * Remeber: structured data starts with [ and includes any characters
b65758
+	 * until the first ] followed by a SP. There may be spaces inside
b65758
+	 * structured data. There may also be \] inside the structured data, which
b65758
+	 * do NOT terminate an element.
b65758
+	 */
b65758
+	 
b65758
+	/* trim */
b65758
+	while(lenStr > 0 && *p2parse == ' ') {
b65758
+		++p2parse; /* eat SP, but only if not at end of string */
b65758
+		--lenStr;
b65758
+	}
b65758
+		 
b65758
+	if(lenStr == 0 || *p2parse != '[')
b65758
+		return 1; /* this is NOT structured data! */
b65758
+
b65758
+	if(*p2parse == '-') { /* empty structured data? */
b65758
+		*pResult++ = '-';
b65758
+		++p2parse;
b65758
+		--lenStr;
b65758
+	} else {
b65758
+		while(bCont) {
b65758
+			if(lenStr < 2) {
b65758
+				/* we now need to check if we have only structured data */
b65758
+				if(lenStr > 0 && *p2parse == ']') {
b65758
+					*pResult++ = *p2parse;
b65758
+					p2parse++;
b65758
+					lenStr--;
b65758
+					bCont = 0;
b65758
+				} else {
b65758
+					iRet = 1; /* this is not valid! */
b65758
+					bCont = 0;
b65758
+				}
b65758
+			} else if(*p2parse == '\\' && *(p2parse+1) == ']') {
b65758
+				/* this is escaped, need to copy both */
b65758
+				*pResult++ = *p2parse++;
b65758
+				*pResult++ = *p2parse++;
b65758
+				lenStr -= 2;
b65758
+			} else if(*p2parse == ']' && *(p2parse+1) == ' ') {
b65758
+				/* found end, just need to copy the ] and eat the SP */
b65758
+				*pResult++ = *p2parse;
b65758
+				p2parse += 2;
b65758
+				lenStr -= 2;
b65758
+				bCont = 0;
b65758
+			} else {
b65758
+				*pResult++ = *p2parse++;
b65758
+				--lenStr;
b65758
+			}
b65758
+		}
b65758
+	}
b65758
+
b65758
+	if(lenStr > 0 && *p2parse == ' ') {
b65758
+		++p2parse; /* eat SP, but only if not at end of string */
b65758
+		--lenStr;
b65758
+	} else {
b65758
+		iRet = 1; /* there MUST be an SP! */
b65758
+	}
b65758
+	*pResult = '\0';
b65758
+
b65758
+	/* set the new parse pointer */
b65758
+	*pp2parse = p2parse;
b65758
+	*pLenStr = lenStr;
b65758
+	return 0;
b65758
+}
b65758
+
b65758
+/* parse a legay-formatted syslog message.
b65758
+ */
b65758
+BEGINparse
b65758
+	uchar *p2parse;
b65758
+	int lenMsg;
b65758
+	int bTAGCharDetected;
b65758
+	int i;	/* general index for parsing */
b65758
+	uchar bufParseTAG[CONF_TAG_MAXSIZE];
b65758
+	uchar bufParseHOSTNAME[CONF_HOSTNAME_MAXSIZE];
b65758
+	uchar *pBuf = NULL;
b65758
+CODESTARTparse
b65758
+	dbgprintf("Message will now be parsed by the legacy syslog parser with structured-data support.\n");
b65758
+	assert(pMsg != NULL);
b65758
+	assert(pMsg->pszRawMsg != NULL);
b65758
+	lenMsg = pMsg->iLenRawMsg - pMsg->offAfterPRI; /* note: offAfterPRI is already the number of PRI chars (do not add one!) */
b65758
+	p2parse = pMsg->pszRawMsg + pMsg->offAfterPRI; /* point to start of text, after PRI */
b65758
+	setProtocolVersion(pMsg, MSG_LEGACY_PROTOCOL);
b65758
+
b65758
+	/* Check to see if msg contains a timestamp. We start by assuming
b65758
+	 * that the message timestamp is the time of reception (which we 
b65758
+	 * generated ourselfs and then try to actually find one inside the
b65758
+	 * message. There we go from high-to low precison and are done
b65758
+	 * when we find a matching one. -- rgerhards, 2008-09-16
b65758
+	 */
b65758
+	if(datetime.ParseTIMESTAMP3339(&(pMsg->tTIMESTAMP), &p2parse, &lenMsg) == RS_RET_OK) {
b65758
+		/* we are done - parse pointer is moved by ParseTIMESTAMP3339 */;
b65758
+	} else if(datetime.ParseTIMESTAMP3164(&(pMsg->tTIMESTAMP), &p2parse, &lenMsg, NO_PARSE3164_TZSTRING, NO_PERMIT_YEAR_AFTER_TIME) == RS_RET_OK) {
b65758
+		/* we are done - parse pointer is moved by ParseTIMESTAMP3164 */;
b65758
+	} else if(*p2parse == ' ' && lenMsg > 1) { /* try to see if it is slighly malformed - HP procurve seems to do that sometimes */
b65758
+		++p2parse;	/* move over space */
b65758
+		--lenMsg;
b65758
+		if(datetime.ParseTIMESTAMP3164(&(pMsg->tTIMESTAMP), &p2parse, &lenMsg, NO_PARSE3164_TZSTRING, NO_PERMIT_YEAR_AFTER_TIME) == RS_RET_OK) {
b65758
+			/* indeed, we got it! */
b65758
+			/* we are done - parse pointer is moved by ParseTIMESTAMP3164 */;
b65758
+		} else {/* parse pointer needs to be restored, as we moved it off-by-one
b65758
+			 * for this try.
b65758
+			 */
b65758
+			--p2parse;
b65758
+			++lenMsg;
b65758
+		}
b65758
+	}
b65758
+
b65758
+	if(pMsg->msgFlags & IGNDATE) {
b65758
+		/* we need to ignore the msg data, so simply copy over reception date */
b65758
+		memcpy(&pMsg->tTIMESTAMP, &pMsg->tRcvdAt, sizeof(struct syslogTime));
b65758
+	}
b65758
+
b65758
+	/* rgerhards, 2006-03-13: next, we parse the hostname and tag. But we 
b65758
+	 * do this only when the user has not forbidden this. I now introduce some
b65758
+	 * code that allows a user to configure rsyslogd to treat the rest of the
b65758
+	 * message as MSG part completely. In this case, the hostname will be the
b65758
+	 * machine that we received the message from and the tag will be empty. This
b65758
+	 * is meant to be an interim solution, but for now it is in the code.
b65758
+	 */
b65758
+	if(bParseHOSTNAMEandTAG && !(pMsg->msgFlags & INTERNAL_MSG)) {
b65758
+		/* parse HOSTNAME - but only if this is network-received!
b65758
+		 * rger, 2005-11-14: we still have a problem with BSD messages. These messages
b65758
+		 * do NOT include a host name. In most cases, this leads to the TAG to be treated
b65758
+		 * as hostname and the first word of the message as the TAG. Clearly, this is not
b65758
+		 * of advantage ;) I think I have now found a way to handle this situation: there
b65758
+		 * are certain characters which are frequently used in TAG (e.g. ':'), which are
b65758
+		 * *invalid* in host names. So while parsing the hostname, I check for these characters.
b65758
+		 * If I find them, I set a simple flag but continue. After parsing, I check the flag.
b65758
+		 * If it was set, then we most probably do not have a hostname but a TAG. Thus, I change
b65758
+		 * the fields. I think this logic shall work with any type of syslog message.
b65758
+		 * rgerhards, 2009-06-23: and I now have extended this logic to every character
b65758
+		 * that is not a valid hostname.
b65758
+		 */
b65758
+		bTAGCharDetected = 0;
b65758
+		if(lenMsg > 0 && pMsg->msgFlags & PARSE_HOSTNAME) {
b65758
+			i = 0;
b65758
+			while(i < lenMsg && (isalnum(p2parse[i]) || p2parse[i] == '.' || p2parse[i] == '.'
b65758
+				|| p2parse[i] == '_' || p2parse[i] == '-') && i < (CONF_HOSTNAME_MAXSIZE - 1)) {
b65758
+				bufParseHOSTNAME[i] = p2parse[i];
b65758
+				++i;
b65758
+			}
b65758
+
b65758
+			if(i == lenMsg) {
b65758
+				/* we have a message that is empty immediately after the hostname,
b65758
+				* but the hostname thus is valid! -- rgerhards, 2010-02-22
b65758
+				*/
b65758
+				p2parse += i;
b65758
+				lenMsg -= i;
b65758
+				bufParseHOSTNAME[i] = '\0';
b65758
+				MsgSetHOSTNAME(pMsg, bufParseHOSTNAME, i);
b65758
+			} else if(i > 0 && p2parse[i] == ' ' && isalnum(p2parse[i-1])) {
b65758
+				/* we got a hostname! */
b65758
+				p2parse += i + 1; /* "eat" it (including SP delimiter) */
b65758
+				lenMsg -= i + 1;
b65758
+				bufParseHOSTNAME[i] = '\0';
b65758
+				MsgSetHOSTNAME(pMsg, bufParseHOSTNAME, i);
b65758
+			}
b65758
+		}
b65758
+
b65758
+		/* now parse TAG - that should be present in message from all sources.
b65758
+		 * This code is somewhat not compliant with RFC 3164. As of 3164,
b65758
+		 * the TAG field is ended by any non-alphanumeric character. In
b65758
+		 * practice, however, the TAG often contains dashes and other things,
b65758
+		 * which would end the TAG. So it is not desirable. As such, we only
b65758
+		 * accept colon and SP to be terminators. Even there is a slight difference:
b65758
+		 * a colon is PART of the TAG, while a SP is NOT part of the tag
b65758
+		 * (it is CONTENT). Starting 2008-04-04, we have removed the 32 character
b65758
+		 * size limit (from RFC3164) on the tag. This had bad effects on existing
b65758
+		 * envrionments, as sysklogd didn't obey it either (probably another bug
b65758
+		 * in RFC3164...). We now receive the full size, but will modify the
b65758
+		 * outputs so that only 32 characters max are used by default.
b65758
+		 */
b65758
+		i = 0;
b65758
+		while(lenMsg > 0 && *p2parse != ':' && *p2parse != ' ' && i < CONF_TAG_MAXSIZE) {
b65758
+			bufParseTAG[i++] = *p2parse++;
b65758
+			--lenMsg;
b65758
+		}
b65758
+		if(lenMsg > 0 && *p2parse == ':') {
b65758
+			++p2parse; 
b65758
+			--lenMsg;
b65758
+			bufParseTAG[i++] = ':';
b65758
+		}
b65758
+
b65758
+		/* no TAG can only be detected if the message immediatly ends, in which case an empty TAG
b65758
+		 * is considered OK. So we do not need to check for empty TAG. -- rgerhards, 2009-06-23
b65758
+		 */
b65758
+		bufParseTAG[i] = '\0';	/* terminate string */
b65758
+		MsgSetTAG(pMsg, bufParseTAG, i);
b65758
+	} else {/* we enter this code area when the user has instructed rsyslog NOT
b65758
+		 * to parse HOSTNAME and TAG - rgerhards, 2006-03-13
b65758
+		 */
b65758
+		if(!(pMsg->msgFlags & INTERNAL_MSG)) {
b65758
+			DBGPRINTF("HOSTNAME and TAG not parsed by user configuraton.\n");
b65758
+		}
b65758
+	}
b65758
+
b65758
+	CHKmalloc(pBuf = MALLOC(sizeof(uchar) * (lenMsg + 1)));
b65758
+
b65758
+	/* STRUCTURED-DATA */
b65758
+	if (parseRFCStructuredData(&p2parse, pBuf, &lenMsg) == 0)
b65758
+		MsgSetStructuredData(pMsg, (char*)pBuf);
b65758
+	else
b65758
+		MsgSetStructuredData(pMsg, "-");
b65758
+
b65758
+	/* The rest is the actual MSG */
b65758
+	MsgSetMSGoffs(pMsg, p2parse - pMsg->pszRawMsg);
b65758
+
b65758
+finalize_it:
b65758
+	if(pBuf != NULL)
b65758
+		free(pBuf);
b65758
+ENDparse
b65758
+
b65758
+
b65758
+BEGINmodExit
b65758
+CODESTARTmodExit
b65758
+	/* release what we no longer need */
b65758
+	objRelease(errmsg, CORE_COMPONENT);
b65758
+	objRelease(glbl, CORE_COMPONENT);
b65758
+	objRelease(parser, CORE_COMPONENT);
b65758
+	objRelease(datetime, CORE_COMPONENT);
b65758
+ENDmodExit
b65758
+
b65758
+
b65758
+BEGINqueryEtryPt
b65758
+CODESTARTqueryEtryPt
b65758
+CODEqueryEtryPt_STD_PMOD_QUERIES
b65758
+CODEqueryEtryPt_IsCompatibleWithFeature_IF_OMOD_QUERIES
b65758
+ENDqueryEtryPt
b65758
+
b65758
+
b65758
+BEGINmodInit()
b65758
+CODESTARTmodInit
b65758
+	*ipIFVersProvided = CURR_MOD_IF_VERSION; /* we only support the current interface specification */
b65758
+CODEmodInit_QueryRegCFSLineHdlr
b65758
+	CHKiRet(objUse(glbl, CORE_COMPONENT));
b65758
+	CHKiRet(objUse(errmsg, CORE_COMPONENT));
b65758
+	CHKiRet(objUse(parser, CORE_COMPONENT));
b65758
+	CHKiRet(objUse(datetime, CORE_COMPONENT));
b65758
+
b65758
+	dbgprintf("rfc3164sd parser init called\n");
b65758
+ 	bParseHOSTNAMEandTAG = glbl.GetParseHOSTNAMEandTAG(); /* cache value, is set only during rsyslogd option processing */
b65758
+
b65758
+
b65758
+ENDmodInit
b65758
+
b65758
+/* vim:set ai:
b65758
+ */