summaryrefslogtreecommitdiffstats
path: root/template.c
diff options
context:
space:
mode:
Diffstat (limited to 'template.c')
-rw-r--r--template.c126
1 files changed, 106 insertions, 20 deletions
diff --git a/template.c b/template.c
index a5331d57..2038c6c1 100644
--- a/template.c
+++ b/template.c
@@ -36,22 +36,29 @@
#include "dirty.h"
#include "obj.h"
#include "errmsg.h"
+#include "strgen.h"
+#include "unicode-helper.h"
/* static data */
DEFobjCurrIf(obj)
DEFobjCurrIf(errmsg)
-DEFobjCurrIf(regexp)
+DEFobjCurrIf(strgen)
+#ifdef FEATURE_REGEXP
+DEFobjCurrIf(regexp)
static int bFirstRegexpErrmsg = 1; /**< did we already do a "can't load regexp" error message? */
+#endif
+
static struct template *tplRoot = NULL; /* the root of the template list */
static struct template *tplLast = NULL; /* points to the last element of the template list */
static struct template *tplLastStatic = NULL; /* last static element of the template list */
-/* helper to tplToString, extends buffer */
+/* helper to tplToString and strgen's, extends buffer */
#define ALLOC_INC 128
-static inline rsRetVal ExtendBuf(uchar **pBuf, size_t *pLenBuf, size_t iMinSize)
+rsRetVal
+ExtendBuf(uchar **pBuf, size_t *pLenBuf, size_t iMinSize)
{
uchar *pNewBuf;
size_t iNewSize;
@@ -81,7 +88,7 @@ rsRetVal tplToString(struct template *pTpl, msg_t *pMsg, uchar **ppBuf, size_t *
{
DEFiRet;
struct templateEntry *pTpe;
- int iBuf;
+ size_t iBuf;
unsigned short bMustBeFreed = 0;
uchar *pVal;
size_t iLenVal = 0;
@@ -91,6 +98,11 @@ rsRetVal tplToString(struct template *pTpl, msg_t *pMsg, uchar **ppBuf, size_t *
assert(ppBuf != NULL);
assert(pLenBuf != NULL);
+ if(pTpl->pStrgen != NULL) {
+ CHKiRet(pTpl->pStrgen(pMsg, ppBuf, pLenBuf));
+ FINALIZE;
+ }
+
/* loop through the template. We obtain one value
* and copy it over to our dynamic string buffer. Then, we
* free the obtained value (if requested). We continue this
@@ -117,10 +129,11 @@ rsRetVal tplToString(struct template *pTpl, msg_t *pMsg, uchar **ppBuf, size_t *
doSQLEscape(&pVal, &iLenVal, &bMustBeFreed, 0);
}
/* got source, now copy over */
- if(iBuf + iLenVal + 1 >= *pLenBuf) /* we reserve one char for the final \0! */
- CHKiRet(ExtendBuf(ppBuf, pLenBuf, iBuf + iLenVal + 1));
-
if(iLenVal > 0) { /* may be zero depending on property */
+ /* first, make sure buffer fits */
+ if(iBuf + iLenVal >= *pLenBuf) /* we reserve one char for the final \0! */
+ CHKiRet(ExtendBuf(ppBuf, pLenBuf, iBuf + iLenVal + 1));
+
memcpy(*ppBuf + iBuf, pVal, iLenVal);
iBuf += iLenVal;
}
@@ -131,7 +144,15 @@ rsRetVal tplToString(struct template *pTpl, msg_t *pMsg, uchar **ppBuf, size_t *
pTpe = pTpe->pNext;
}
- (*ppBuf)[iBuf] = '\0'; /* space was reserved above (see copy) */
+ if(iBuf == *pLenBuf) {
+ /* in the weired case of an *empty* template, this can happen.
+ * it is debatable if we should really fix it here or simply
+ * forbid that case. However, performance toll is minimal, so
+ * I tend to permit it. -- 201011-05 rgerhards
+ */
+ CHKiRet(ExtendBuf(ppBuf, pLenBuf, iBuf + 1));
+ }
+ (*ppBuf)[iBuf] = '\0';
finalize_it:
RETiRet;
@@ -531,10 +552,9 @@ static int do_Parameter(unsigned char **pp, struct template *pTpl)
cstr_t *pStrB;
struct templateEntry *pTpe;
int iNum; /* to compute numbers */
- rsRetVal iRetLocal;
-
#ifdef FEATURE_REGEXP
/* APR: variables for regex */
+ rsRetVal iRetLocal;
int longitud;
unsigned char *regex_char;
unsigned char *regex_end;
@@ -741,7 +761,7 @@ static int do_Parameter(unsigned char **pp, struct template *pTpl)
/* We get here ONLY if the regex end was found */
longitud = regex_end - p;
/* Malloc for the regex string */
- regex_char = (unsigned char *) malloc(longitud + 1);
+ regex_char = (unsigned char *) MALLOC(longitud + 1);
if(regex_char == NULL) {
dbgprintf("Could not allocate memory for template parameter!\n");
pTpe->data.field.has_regex = 0;
@@ -829,16 +849,63 @@ static int do_Parameter(unsigned char **pp, struct template *pTpl)
}
+/* Add a new entry for a template module.
+ * returns pointer to new object if it succeeds, NULL otherwise.
+ * rgerhards, 2010-05-31
+ */
+static rsRetVal
+tplAddTplMod(struct template *pTpl, uchar** ppRestOfConfLine)
+{
+ uchar *pSrc, *pDst;
+ uchar szMod[2048];
+ unsigned lenMod;
+ strgen_t *pStrgen;
+ DEFiRet;
+
+ pSrc = *ppRestOfConfLine;
+ pDst = szMod;
+ lenMod = 0;
+ while(*pSrc && !isspace(*pSrc) && lenMod < sizeof(szMod) - 1) {
+ szMod[lenMod] = *pSrc++;
+ lenMod++;
+
+ }
+ szMod[lenMod] = '\0';
+ *ppRestOfConfLine = pSrc;
+ CHKiRet(strgen.FindStrgen(&pStrgen, szMod));
+ pTpl->pStrgen = pStrgen->pModule->mod.sm.strgen;
+ DBGPRINTF("template bound to strgen '%s'\n", szMod);
+ /* check if the name potentially contains some well-known options
+ * Note: we have opted to let the name contain all options. This sounds
+ * useful, because the strgen MUST actually implement a specific set
+ * of options. Doing this via the name looks to the enduser as if the
+ * regular syntax were used, and it make sure the strgen postively
+ * acknowledged implementing the option. -- rgerhards, 2011-03-21
+ */
+ if(lenMod > 6 && !strcasecmp((char*) szMod + lenMod - 7, ",stdsql")) {
+ pTpl->optFormatForSQL = 2;
+ DBGPRINTF("strgen suports the stdsql option\n");
+ } else if(lenMod > 3 && !strcasecmp((char*) szMod+ lenMod - 4, ",sql")) {
+ pTpl->optFormatForSQL = 1;
+ DBGPRINTF("strgen suports the sql option\n");
+ }
+
+finalize_it:
+ RETiRet;
+}
+
+
/* Add a new template line
* returns pointer to new object if it succeeds, NULL otherwise.
*/
-struct template *tplAddLine(char* pName, unsigned char** ppRestOfConfLine)
+struct template *tplAddLine(char* pName, uchar** ppRestOfConfLine)
{
struct template *pTpl;
unsigned char *p;
int bDone;
char optBuf[128]; /* buffer for options - should be more than enough... */
size_t i;
+ rsRetVal localRet;
assert(pName != NULL);
assert(ppRestOfConfLine != NULL);
@@ -847,7 +914,7 @@ struct template *tplAddLine(char* pName, unsigned char** ppRestOfConfLine)
return NULL;
pTpl->iLenName = strlen(pName);
- pTpl->pszName = (char*) malloc(sizeof(char) * (pTpl->iLenName + 1));
+ pTpl->pszName = (char*) MALLOC(sizeof(char) * (pTpl->iLenName + 1));
if(pTpl->pszName == NULL) {
dbgprintf("tplAddLine could not alloc memory for template name!");
pTpl->iLenName = 0;
@@ -866,13 +933,30 @@ struct template *tplAddLine(char* pName, unsigned char** ppRestOfConfLine)
while(isspace((int)*p))/* skip whitespace */
++p;
- if(*p != '"') {
+ switch(*p) {
+ case '"': /* just continue */
+ break;
+ case '=':
+ *ppRestOfConfLine = p + 1;
+ localRet = tplAddTplMod(pTpl, ppRestOfConfLine);
+ if(localRet != RS_RET_OK) {
+ errmsg.LogError(0, localRet, "Template '%s': error %d defining template via strgen module",
+ pTpl->pszName, localRet);
+ /* we simply make the template defunct in this case by setting
+ * its name to a zero-string. We do not free it, as this would
+ * require additional code and causes only a very small memory
+ * consumption. Memory is freed, however, in normal operation
+ * and most importantly by HUPing syslogd.
+ */
+ *pTpl->pszName = '\0';
+ }
+ return NULL;
+ default:
dbgprintf("Template '%s' invalid, does not start with '\"'!\n", pTpl->pszName);
/* we simply make the template defunct in this case by setting
* its name to a zero-string. We do not free it, as this would
* require additional code and causes only a very small memory
- * consumption. Memory is freed, however, in normal operation
- * and most importantly by HUPing syslogd.
+ * consumption.
*/
*pTpl->pszName = '\0';
return NULL;
@@ -942,6 +1026,7 @@ struct template *tplAddLine(char* pName, unsigned char** ppRestOfConfLine)
}
*ppRestOfConfLine = p;
+
return(pTpl);
}
@@ -1001,11 +1086,13 @@ void tplDeleteAll(void)
break;
case FIELD:
/* check if we have a regexp and, if so, delete it */
+#ifdef FEATURE_REGEXP
if(pTpeDel->data.field.has_regex != 0) {
if(objUse(regexp, LM_REGEXP_FILENAME) == RS_RET_OK) {
regexp.regfree(&(pTpeDel->data.field.re));
}
}
+#endif
break;
}
/*dbgprintf("\n");*/
@@ -1054,12 +1141,14 @@ void tplDeleteNew(void)
free(pTpeDel->data.constant.pConstant);
break;
case FIELD:
+#ifdef FEATURE_REGEXP
/* check if we have a regexp and, if so, delete it */
if(pTpeDel->data.field.has_regex != 0) {
if(objUse(regexp, LM_REGEXP_FILENAME) == RS_RET_OK) {
regexp.regfree(&(pTpeDel->data.field.re));
}
}
+#endif
break;
}
/*dbgprintf("\n");*/
@@ -1192,11 +1281,8 @@ rsRetVal templateInit()
DEFiRet;
CHKiRet(objGetObjInterface(&obj));
CHKiRet(objUse(errmsg, CORE_COMPONENT));
+ CHKiRet(objUse(strgen, CORE_COMPONENT));
finalize_it:
RETiRet;
}
-
-/*
- * vi:set ai:
- */