| 1 |
/* parser.c |
| 2 |
* This module contains functions for message parsers. It still needs to be |
| 3 |
* converted into an object (and much extended). |
| 4 |
* |
| 5 |
* Module begun 2008-10-09 by Rainer Gerhards (based on previous code from syslogd.c) |
| 6 |
* |
| 7 |
* Copyright 2008-2021 Rainer Gerhards and Adiscon GmbH. |
| 8 |
* |
| 9 |
* This file is part of the rsyslog runtime library. |
| 10 |
* |
| 11 |
* The rsyslog runtime library is free software: you can redistribute it and/or modify |
| 12 |
* it under the terms of the GNU Lesser General Public License as published by |
| 13 |
* the Free Software Foundation, either version 3 of the License, or |
| 14 |
* (at your option) any later version. |
| 15 |
* |
| 16 |
* The rsyslog runtime library is distributed in the hope that it will be useful, |
| 17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
* GNU Lesser General Public License for more details. |
| 20 |
* |
| 21 |
* You should have received a copy of the GNU Lesser General Public License |
| 22 |
* along with the rsyslog runtime library. If not, see <http://www.gnu.org/licenses/>. |
| 23 |
* |
| 24 |
* A copy of the GPL can be found in the file "COPYING" in this distribution. |
| 25 |
* A copy of the LGPL can be found in the file "COPYING.LESSER" in this distribution. |
| 26 |
*/ |
| 27 |
#include "config.h" |
| 28 |
#include <stdlib.h> |
| 29 |
#include <ctype.h> |
| 30 |
#include <string.h> |
| 31 |
#include <assert.h> |
| 32 |
#include <zlib.h> |
| 33 |
|
| 34 |
#include "rsyslog.h" |
| 35 |
#include "dirty.h" |
| 36 |
#include "msg.h" |
| 37 |
#include "obj.h" |
| 38 |
#include "datetime.h" |
| 39 |
#include "errmsg.h" |
| 40 |
#include "parser.h" |
| 41 |
#include "ruleset.h" |
| 42 |
#include "unicode-helper.h" |
| 43 |
#include "dirty.h" |
| 44 |
#include "cfsysline.h" |
| 45 |
|
| 46 |
/* some defines */ |
| 47 |
#define DEFUPRI (LOG_USER | LOG_NOTICE) |
| 48 |
|
| 49 |
/* definitions for objects we access */ |
| 50 |
DEFobjStaticHelpers; |
| 51 |
DEFobjCurrIf(glbl) DEFobjCurrIf(datetime) DEFobjCurrIf(ruleset) |
| 52 |
|
| 53 |
/* static data */ |
| 54 |
|
| 55 |
static char hexdigit[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; |
| 56 |
|
| 57 |
BEGINobjDestruct(parser) /* be sure to specify the object type also in END and CODESTART macros! */ |
| 58 |
CODESTARTobjDestruct(parser); |
| 59 |
DBGPRINTF("destructing parser '%s'\n", pThis->pName); |
| 60 |
if (pThis->pInst != NULL) { |
| 61 |
pThis->pModule->mod.pm.freeParserInst(pThis->pInst); |
| 62 |
} |
| 63 |
free(pThis->pName); |
| 64 |
ENDobjDestruct(parser) |
| 65 |
|
| 66 |
/* destruct a parser list. The list elements are destroyed, but the parser objects |
| 67 |
* themselves are not modified. (That is done at a late stage during rsyslogd |
| 68 |
* shutdown and need not be considered here.) |
| 69 |
*/ |
| 70 |
static rsRetVal DestructParserList(parserList_t **ppListRoot) { |
| 71 |
parserList_t *pParsLst; |
| 72 |
parserList_t *pParsLstDel; |
| 73 |
|
| 74 |
pParsLst = *ppListRoot; |
| 75 |
while (pParsLst != NULL) { |
| 76 |
pParsLstDel = pParsLst; |
| 77 |
pParsLst = pParsLst->pNext; |
| 78 |
free(pParsLstDel); |
| 79 |
} |
| 80 |
*ppListRoot = NULL; |
| 81 |
return RS_RET_OK; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
/* Add a parser to the list. We use a VERY simple and ineffcient algorithm, |
| 86 |
* but it is employed only for a few milliseconds during config processing. So |
| 87 |
* I prefer to keep it very simple and with simple data structures. Unfortunately, |
| 88 |
* we need to preserve the order, but I don't like to add a tail pointer as that |
| 89 |
* would require a container object. So I do the extra work to skip to the tail |
| 90 |
* when adding elements... |
| 91 |
* rgerhards, 2009-11-03 |
| 92 |
*/ |
| 93 |
static rsRetVal AddParserToList(parserList_t **ppListRoot, parser_t *pParser) { |
| 94 |
parserList_t *pThis; |
| 95 |
parserList_t *pTail; |
| 96 |
DEFiRet; |
| 97 |
|
| 98 |
CHKmalloc(pThis = malloc(sizeof(parserList_t))); |
| 99 |
pThis->pParser = pParser; |
| 100 |
pThis->pNext = NULL; |
| 101 |
|
| 102 |
if (*ppListRoot == NULL) { |
| 103 |
pThis->pNext = *ppListRoot; |
| 104 |
*ppListRoot = pThis; |
| 105 |
} else { |
| 106 |
/* find tail first */ |
| 107 |
for (pTail = *ppListRoot; pTail->pNext != NULL; pTail = pTail->pNext) /* just search, do nothing else */ |
| 108 |
; |
| 109 |
/* add at tail */ |
| 110 |
pTail->pNext = pThis; |
| 111 |
} |
| 112 |
DBGPRINTF("DDDDD: added parser '%s' to list %p\n", pParser->pName, ppListRoot); |
| 113 |
finalize_it: |
| 114 |
RETiRet; |
| 115 |
} |
| 116 |
|
| 117 |
void printParserList(parserList_t *pList) { |
| 118 |
while (pList != NULL) { |
| 119 |
dbgprintf("parser: %s\n", pList->pParser->pName); |
| 120 |
pList = pList->pNext; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
/* find a parser based on the provided name */ |
| 125 |
static rsRetVal FindParser(parserList_t *pParserListRoot, parser_t **ppParser, uchar *pName) { |
| 126 |
parserList_t *pThis; |
| 127 |
DEFiRet; |
| 128 |
|
| 129 |
for (pThis = pParserListRoot; pThis != NULL; pThis = pThis->pNext) { |
| 130 |
if (ustrcmp(pThis->pParser->pName, pName) == 0) { |
| 131 |
*ppParser = pThis->pParser; |
| 132 |
FINALIZE; /* found it, iRet still eq. OK! */ |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
iRet = RS_RET_PARSER_NOT_FOUND; |
| 137 |
|
| 138 |
finalize_it: |
| 139 |
RETiRet; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
/* --- END helper functions for parser list handling --- */ |
| 144 |
|
| 145 |
/* Add a an already existing parser to the default list. As usual, order |
| 146 |
* of calls is important (most importantly, that means the legacy parser, |
| 147 |
* which can process everything, MUST be added last!). |
| 148 |
* rgerhards, 2009-11-04 |
| 149 |
*/ |
| 150 |
static rsRetVal AddDfltParser(uchar *pName) { |
| 151 |
parser_t *pParser; |
| 152 |
DEFiRet; |
| 153 |
|
| 154 |
CHKiRet(FindParser(loadConf->parsers.pParsLstRoot, &pParser, pName)); |
| 155 |
CHKiRet(AddParserToList(&loadConf->parsers.pDfltParsLst, pParser)); |
| 156 |
DBGPRINTF("Parser '%s' added to default parser set.\n", pName); |
| 157 |
|
| 158 |
finalize_it: |
| 159 |
RETiRet; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
/* set the parser name - string is copied over, call can continue to use it, |
| 164 |
* but must free it if desired. |
| 165 |
*/ |
| 166 |
static rsRetVal SetName(parser_t *pThis, uchar *name) { |
| 167 |
DEFiRet; |
| 168 |
|
| 169 |
ISOBJ_TYPE_assert(pThis, parser); |
| 170 |
assert(name != NULL); |
| 171 |
|
| 172 |
if (pThis->pName != NULL) { |
| 173 |
free(pThis->pName); |
| 174 |
pThis->pName = NULL; |
| 175 |
} |
| 176 |
|
| 177 |
CHKmalloc(pThis->pName = ustrdup(name)); |
| 178 |
|
| 179 |
finalize_it: |
| 180 |
RETiRet; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
/* set a pointer to "our" module. Note that no module |
| 185 |
* pointer must already be set. |
| 186 |
*/ |
| 187 |
static rsRetVal SetModPtr(parser_t *pThis, modInfo_t *pMod) { |
| 188 |
ISOBJ_TYPE_assert(pThis, parser); |
| 189 |
assert(pMod != NULL); |
| 190 |
assert(pThis->pModule == NULL); |
| 191 |
pThis->pModule = pMod; |
| 192 |
return RS_RET_OK; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
/* Specify if we should do standard PRI parsing before we pass the data |
| 197 |
* down to the parser module. |
| 198 |
*/ |
| 199 |
static rsRetVal SetDoPRIParsing(parser_t *pThis, int bDoIt) { |
| 200 |
ISOBJ_TYPE_assert(pThis, parser); |
| 201 |
pThis->bDoPRIParsing = bDoIt; |
| 202 |
return RS_RET_OK; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
BEGINobjConstruct(parser) /* be sure to specify the object type also in END macro! */ |
| 207 |
ENDobjConstruct(parser) |
| 208 |
|
| 209 |
/* ConstructionFinalizer. The most important chore is to add the parser object |
| 210 |
* to our global list of available parsers. |
| 211 |
* rgerhards, 2009-11-03 |
| 212 |
*/ |
| 213 |
static rsRetVal parserConstructFinalize(parser_t *pThis) { |
| 214 |
DEFiRet; |
| 215 |
|
| 216 |
ISOBJ_TYPE_assert(pThis, parser); |
| 217 |
CHKiRet(AddParserToList(&loadConf->parsers.pParsLstRoot, pThis)); |
| 218 |
DBGPRINTF("Parser '%s' added to list of available parsers.\n", pThis->pName); |
| 219 |
|
| 220 |
finalize_it: |
| 221 |
RETiRet; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
/* construct a parser object via a pointer to the parser module |
| 226 |
* and the name. This is a separate function because we need it |
| 227 |
* in multiple spots inside the code. |
| 228 |
*/ |
| 229 |
rsRetVal parserConstructViaModAndName(modInfo_t *__restrict__ pMod, uchar *const __restrict__ pName, void *pInst) { |
| 230 |
rsRetVal localRet; |
| 231 |
parser_t *pParser = NULL; |
| 232 |
DEFiRet; |
| 233 |
|
| 234 |
if (pInst == NULL && pMod->mod.pm.newParserInst != NULL) { |
| 235 |
/* this happens for the default instance on ModLoad time */ |
| 236 |
CHKiRet(pMod->mod.pm.newParserInst(NULL, &pInst)); |
| 237 |
} |
| 238 |
CHKiRet(parserConstruct(&pParser)); |
| 239 |
/* check some features */ |
| 240 |
localRet = pMod->isCompatibleWithFeature(sFEATUREAutomaticSanitazion); |
| 241 |
if (localRet == RS_RET_OK) { |
| 242 |
pParser->bDoSanitazion = RSTRUE; |
| 243 |
} |
| 244 |
localRet = pMod->isCompatibleWithFeature(sFEATUREAutomaticPRIParsing); |
| 245 |
if (localRet == RS_RET_OK) { |
| 246 |
CHKiRet(SetDoPRIParsing(pParser, RSTRUE)); |
| 247 |
} |
| 248 |
|
| 249 |
CHKiRet(SetName(pParser, pName)); |
| 250 |
CHKiRet(SetModPtr(pParser, pMod)); |
| 251 |
pParser->pInst = pInst; |
| 252 |
CHKiRet(parserConstructFinalize(pParser)); |
| 253 |
finalize_it: |
| 254 |
if (iRet != RS_RET_OK) free(pParser); |
| 255 |
RETiRet; |
| 256 |
} |
| 257 |
|
| 258 |
/* uncompress a received message if it is compressed. |
| 259 |
* pMsg->pszRawMsg buffer is updated. |
| 260 |
* rgerhards, 2008-10-09 |
| 261 |
*/ |
| 262 |
static rsRetVal uncompressMessage(smsg_t *pMsg) { |
| 263 |
DEFiRet; |
| 264 |
uchar *deflateBuf = NULL; |
| 265 |
uLongf iLenDefBuf; |
| 266 |
uchar *pszMsg; |
| 267 |
size_t lenMsg; |
| 268 |
|
| 269 |
assert(pMsg != NULL); |
| 270 |
pszMsg = pMsg->pszRawMsg; |
| 271 |
lenMsg = pMsg->iLenRawMsg; |
| 272 |
|
| 273 |
/* we first need to check if we have a compressed record. If so, |
| 274 |
* we must decompress it. |
| 275 |
*/ |
| 276 |
if (lenMsg > 0 && *pszMsg == 'z' && runConf->globals.bSupportCompressionExtension) { /* compressed data present? */ |
| 277 |
/* we have compressed data, so let's deflate it. We support a maximum |
| 278 |
* message size of iMaxLine. If it is larger, an error message is logged |
| 279 |
* and the message is dropped. We do NOT try to decompress larger messages |
| 280 |
* as such might be used for denial of service. It might happen to later |
| 281 |
* builds that such functionality be added as an optional, operator-configurable |
| 282 |
* feature. |
| 283 |
*/ |
| 284 |
int ret; |
| 285 |
iLenDefBuf = glbl.GetMaxLine(runConf); |
| 286 |
CHKmalloc(deflateBuf = malloc(iLenDefBuf + 1)); |
| 287 |
ret = uncompress((uchar *)deflateBuf, &iLenDefBuf, (uchar *)pszMsg + 1, lenMsg - 1); |
| 288 |
DBGPRINTF("Compressed message uncompressed with status %d, length: new %ld, old %d.\n", ret, (long)iLenDefBuf, |
| 289 |
(int)(lenMsg - 1)); |
| 290 |
/* Now check if the uncompression worked. If not, there is not much we can do. In |
| 291 |
* that case, we log an error message but ignore the message itself. Storing the |
| 292 |
* compressed text is dangerous, as it contains control characters. So we do |
| 293 |
* not do this. If someone would like to have a copy, this code here could be |
| 294 |
* modified to do a hex-dump of the buffer in question. We do not include |
| 295 |
* this functionality right now. |
| 296 |
* rgerhards, 2006-12-07 |
| 297 |
*/ |
| 298 |
if (ret != Z_OK) { |
| 299 |
LogError(0, NO_ERRCODE, |
| 300 |
"Uncompression of a message failed with return code %d " |
| 301 |
"- enable debug logging if you need further information. " |
| 302 |
"Message ignored.", |
| 303 |
ret); |
| 304 |
FINALIZE; /* unconditional exit, nothing left to do... */ |
| 305 |
} |
| 306 |
MsgSetRawMsg(pMsg, (char *)deflateBuf, iLenDefBuf); |
| 307 |
} |
| 308 |
finalize_it: |
| 309 |
if (deflateBuf != NULL) free(deflateBuf); |
| 310 |
|
| 311 |
RETiRet; |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
/* sanitize a received message |
| 316 |
* if a message gets to large during sanitization, it is truncated. This is |
| 317 |
* as specified in the upcoming syslog RFC series. |
| 318 |
* rgerhards, 2008-10-09 |
| 319 |
* We check if we have a NUL character at the very end of the |
| 320 |
* message. This seems to be a frequent problem with a number of senders. |
| 321 |
* So I have now decided to drop these NULs. However, if they are intentional, |
| 322 |
* that may cause us some problems, e.g. with syslog-sign. On the other hand, |
| 323 |
* current code always has problems with intentional NULs (as it needs to escape |
| 324 |
* them to prevent problems with the C string libraries), so that does not |
| 325 |
* really matter. Just to be on the save side, we'll log destruction of such |
| 326 |
* NULs in the debug log. |
| 327 |
* rgerhards, 2007-09-14 |
| 328 |
*/ |
| 329 |
static rsRetVal SanitizeMsg(smsg_t *pMsg) { |
| 330 |
DEFiRet; |
| 331 |
uchar *pszMsg; |
| 332 |
uchar *pDst; /* destination for copy job */ |
| 333 |
size_t lenMsg; |
| 334 |
size_t iSrc; |
| 335 |
size_t iDst; |
| 336 |
size_t iMaxLine; |
| 337 |
size_t maxDest; |
| 338 |
uchar pc; |
| 339 |
sbool bUpdatedLen = RSFALSE; |
| 340 |
uchar szSanBuf[32 * 1024]; /* buffer used for sanitizing a string */ |
| 341 |
|
| 342 |
assert(pMsg != NULL); |
| 343 |
assert(pMsg->iLenRawMsg > 0); |
| 344 |
|
| 345 |
pszMsg = pMsg->pszRawMsg; |
| 346 |
lenMsg = pMsg->iLenRawMsg; |
| 347 |
|
| 348 |
/* remove NUL character at end of message (see comment in function header) |
| 349 |
* Note that we do not need to add a NUL character in this case, because it |
| 350 |
* is already present ;) |
| 351 |
*/ |
| 352 |
if (pszMsg[lenMsg - 1] == '\0') { |
| 353 |
DBGPRINTF("dropped NUL at very end of message\n"); |
| 354 |
bUpdatedLen = RSTRUE; |
| 355 |
lenMsg--; |
| 356 |
} |
| 357 |
|
| 358 |
/* then we check if we need to drop trailing LFs, which often make |
| 359 |
* their way into syslog messages unintentionally. In order to remain |
| 360 |
* compatible to recent IETF developments, we allow the user to |
| 361 |
* turn on/off this handling. rgerhards, 2007-07-23 |
| 362 |
*/ |
| 363 |
if (glbl.GetParserDropTrailingLFOnReception(runConf) && lenMsg > 0 && pszMsg[lenMsg - 1] == '\n') { |
| 364 |
DBGPRINTF("dropped LF at very end of message (DropTrailingLF is set)\n"); |
| 365 |
lenMsg--; |
| 366 |
pszMsg[lenMsg] = '\0'; |
| 367 |
bUpdatedLen = RSTRUE; |
| 368 |
} |
| 369 |
|
| 370 |
/* it is much quicker to sweep over the message and see if it actually |
| 371 |
* needs sanitation than to do the sanitation in any case. So we first do |
| 372 |
* this and terminate when it is not needed - which is expectedly the case |
| 373 |
* for the vast majority of messages. -- rgerhards, 2009-06-15 |
| 374 |
* Note that we do NOT check here if tab characters are to be escaped or |
| 375 |
* not. I expect this functionality to be seldomly used and thus I do not |
| 376 |
* like to pay the performance penalty. So the penalty is only with those |
| 377 |
* that actually use it, because we may call the sanitizer without actual |
| 378 |
* need below (but it then still will work perfectly well!). -- rgerhards, 2009-11-27 |
| 379 |
*/ |
| 380 |
int bNeedSanitize = 0; |
| 381 |
for (iSrc = 0; iSrc < lenMsg; iSrc++) { |
| 382 |
if (pszMsg[iSrc] < 32) { |
| 383 |
if (glbl.GetParserSpaceLFOnReceive(runConf) && pszMsg[iSrc] == '\n') { |
| 384 |
pszMsg[iSrc] = ' '; |
| 385 |
} else if (pszMsg[iSrc] == '\0' || glbl.GetParserEscapeControlCharactersOnReceive(runConf)) { |
| 386 |
bNeedSanitize = 1; |
| 387 |
if (!glbl.GetParserSpaceLFOnReceive(runConf)) { |
| 388 |
break; |
| 389 |
} |
| 390 |
} |
| 391 |
} else if (pszMsg[iSrc] > 127 && glbl.GetParserEscape8BitCharactersOnReceive(runConf)) { |
| 392 |
bNeedSanitize = 1; |
| 393 |
break; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
if (!bNeedSanitize) { |
| 398 |
if (bUpdatedLen == RSTRUE) MsgSetRawMsgSize(pMsg, lenMsg); |
| 399 |
FINALIZE; |
| 400 |
} |
| 401 |
|
| 402 |
/* now copy over the message and sanitize it. Note that up to iSrc-1 there was |
| 403 |
* obviously no need to sanitize, so we can go over that quickly... |
| 404 |
*/ |
| 405 |
iMaxLine = glbl.GetMaxLine(runConf); |
| 406 |
maxDest = lenMsg * 4; /* message can grow at most four-fold */ |
| 407 |
|
| 408 |
if (maxDest > iMaxLine) maxDest = iMaxLine; /* but not more than the max size! */ |
| 409 |
if (maxDest < sizeof(szSanBuf)) |
| 410 |
pDst = szSanBuf; |
| 411 |
else |
| 412 |
CHKmalloc(pDst = malloc(maxDest + 1)); |
| 413 |
if (iSrc > 0) { |
| 414 |
iSrc--; /* go back to where everything is OK */ |
| 415 |
if (iSrc > maxDest) { |
| 416 |
DBGPRINTF( |
| 417 |
"parser.Sanitize: have oversize index %zd, " |
| 418 |
"max %zd - corrected, but should not happen\n", |
| 419 |
iSrc, maxDest); |
| 420 |
iSrc = maxDest; |
| 421 |
} |
| 422 |
memcpy(pDst, pszMsg, iSrc); /* fast copy known good */ |
| 423 |
} |
| 424 |
iDst = iSrc; |
| 425 |
while (iSrc < lenMsg && iDst < maxDest - 3) { /* leave some space if last char must be escaped */ |
| 426 |
if ((pszMsg[iSrc] < 32) && (pszMsg[iSrc] != '\t' || glbl.GetParserEscapeControlCharacterTab(runConf))) { |
| 427 |
/* note: \0 must always be escaped, the rest of the code currently |
| 428 |
* can not handle it! -- rgerhards, 2009-08-26 |
| 429 |
*/ |
| 430 |
if (pszMsg[iSrc] == '\0' || glbl.GetParserEscapeControlCharactersOnReceive(runConf)) { |
| 431 |
/* we are configured to escape control characters. Please note |
| 432 |
* that this most probably break non-western character sets like |
| 433 |
* Japanese, Korean or Chinese. rgerhards, 2007-07-17 |
| 434 |
*/ |
| 435 |
if (glbl.GetParserEscapeControlCharactersCStyle(runConf)) { |
| 436 |
pDst[iDst++] = '\\'; |
| 437 |
|
| 438 |
switch (pszMsg[iSrc]) { |
| 439 |
case '\0': |
| 440 |
pDst[iDst++] = '0'; |
| 441 |
break; |
| 442 |
case '\a': |
| 443 |
pDst[iDst++] = 'a'; |
| 444 |
break; |
| 445 |
case '\b': |
| 446 |
pDst[iDst++] = 'b'; |
| 447 |
break; |
| 448 |
case '\x1b': /* equivalent to '\e' which is not accepted by XLC */ |
| 449 |
pDst[iDst++] = 'e'; |
| 450 |
break; |
| 451 |
case '\f': |
| 452 |
pDst[iDst++] = 'f'; |
| 453 |
break; |
| 454 |
case '\n': |
| 455 |
pDst[iDst++] = 'n'; |
| 456 |
break; |
| 457 |
case '\r': |
| 458 |
pDst[iDst++] = 'r'; |
| 459 |
break; |
| 460 |
case '\t': |
| 461 |
pDst[iDst++] = 't'; |
| 462 |
break; |
| 463 |
case '\v': |
| 464 |
pDst[iDst++] = 'v'; |
| 465 |
break; |
| 466 |
default: |
| 467 |
pDst[iDst++] = 'x'; |
| 468 |
|
| 469 |
pc = pszMsg[iSrc]; |
| 470 |
pDst[iDst++] = hexdigit[(pc & 0xF0) >> 4]; |
| 471 |
pDst[iDst++] = hexdigit[pc & 0xF]; |
| 472 |
|
| 473 |
break; |
| 474 |
} |
| 475 |
|
| 476 |
} else { |
| 477 |
pDst[iDst++] = glbl.GetParserControlCharacterEscapePrefix(runConf); |
| 478 |
pDst[iDst++] = '0' + ((pszMsg[iSrc] & 0300) >> 6); |
| 479 |
pDst[iDst++] = '0' + ((pszMsg[iSrc] & 0070) >> 3); |
| 480 |
pDst[iDst++] = '0' + ((pszMsg[iSrc] & 0007)); |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
} else if (pszMsg[iSrc] > 127 && glbl.GetParserEscape8BitCharactersOnReceive(runConf)) { |
| 485 |
if (glbl.GetParserEscapeControlCharactersCStyle(runConf)) { |
| 486 |
pDst[iDst++] = '\\'; |
| 487 |
pDst[iDst++] = 'x'; |
| 488 |
|
| 489 |
pc = pszMsg[iSrc]; |
| 490 |
pDst[iDst++] = hexdigit[(pc & 0xF0) >> 4]; |
| 491 |
pDst[iDst++] = hexdigit[pc & 0xF]; |
| 492 |
|
| 493 |
} else { |
| 494 |
/* In this case, we also do the conversion. Note that this most |
| 495 |
* probably breaks European languages. -- rgerhards, 2010-01-27 |
| 496 |
*/ |
| 497 |
pDst[iDst++] = glbl.GetParserControlCharacterEscapePrefix(runConf); |
| 498 |
pDst[iDst++] = '0' + ((pszMsg[iSrc] & 0300) >> 6); |
| 499 |
pDst[iDst++] = '0' + ((pszMsg[iSrc] & 0070) >> 3); |
| 500 |
pDst[iDst++] = '0' + ((pszMsg[iSrc] & 0007)); |
| 501 |
} |
| 502 |
} else { |
| 503 |
pDst[iDst++] = pszMsg[iSrc]; |
| 504 |
} |
| 505 |
++iSrc; |
| 506 |
} |
| 507 |
pDst[iDst] = '\0'; |
| 508 |
|
| 509 |
MsgSetRawMsg(pMsg, (char *)pDst, iDst); /* save sanitized string */ |
| 510 |
|
| 511 |
if (pDst != szSanBuf) free(pDst); |
| 512 |
|
| 513 |
finalize_it: |
| 514 |
RETiRet; |
| 515 |
} |
| 516 |
|
| 517 |
/* A standard parser to parse out the PRI. This is made available in |
| 518 |
* this module as it is expected that allmost all parsers will need |
| 519 |
* that functionality and so they do not need to implement it themsleves. |
| 520 |
*/ |
| 521 |
static rsRetVal ParsePRI(smsg_t *pMsg) { |
| 522 |
syslog_pri_t pri; |
| 523 |
uchar *msg; |
| 524 |
int lenMsg; |
| 525 |
DEFiRet; |
| 526 |
|
| 527 |
/* pull PRI */ |
| 528 |
lenMsg = pMsg->iLenRawMsg; |
| 529 |
msg = pMsg->pszRawMsg; |
| 530 |
pri = DEFUPRI; |
| 531 |
if (pMsg->msgFlags & NO_PRI_IN_RAW) { |
| 532 |
/* In this case, simply do so as if the pri would be right at top */ |
| 533 |
MsgSetAfterPRIOffs(pMsg, 0); |
| 534 |
} else { |
| 535 |
if (*msg == '<') { |
| 536 |
pri = 0; |
| 537 |
while (--lenMsg > 0 && isdigit((int)*++msg) && pri <= LOG_MAXPRI) { |
| 538 |
pri = 10 * pri + (*msg - '0'); |
| 539 |
} |
| 540 |
if (*msg == '>') { |
| 541 |
++msg; |
| 542 |
} else { |
| 543 |
pri = LOG_PRI_INVLD; |
| 544 |
} |
| 545 |
if (pri > LOG_MAXPRI) pri = LOG_PRI_INVLD; |
| 546 |
} |
| 547 |
msgSetPRI(pMsg, pri); |
| 548 |
MsgSetAfterPRIOffs(pMsg, (pri == LOG_PRI_INVLD) ? 0 : msg - pMsg->pszRawMsg); |
| 549 |
} |
| 550 |
RETiRet; |
| 551 |
} |
| 552 |
|
| 553 |
|
| 554 |
/* Parse a received message. The object's rawmsg property is taken and |
| 555 |
* parsed according to the relevant standards. This can later be |
| 556 |
* extended to support configured parsers. |
| 557 |
* rgerhards, 2008-10-09 |
| 558 |
*/ |
| 559 |
static rsRetVal ParseMsg(smsg_t *pMsg) { |
| 560 |
rsRetVal localRet = RS_RET_ERR; |
| 561 |
parserList_t *pParserList; |
| 562 |
parser_t *pParser; |
| 563 |
sbool bIsSanitized; |
| 564 |
sbool bPRIisParsed; |
| 565 |
static int iErrMsgRateLimiter = 0; |
| 566 |
DEFiRet; |
| 567 |
|
| 568 |
if (pMsg->iLenRawMsg == 0) ABORT_FINALIZE(RS_RET_EMPTY_MSG); |
| 569 |
|
| 570 |
CHKiRet(uncompressMessage(pMsg)); |
| 571 |
|
| 572 |
/* we take the risk to print a non-sanitized string, because this is the best we can get |
| 573 |
* (and that functionality is too important for debugging to drop it...). |
| 574 |
*/ |
| 575 |
DBGPRINTF("msg parser: flags %x, from '%s', msg '%.60s'\n", pMsg->msgFlags, |
| 576 |
(pMsg->msgFlags & NEEDS_DNSRESOL) ? UCHAR_CONSTANT("~NOTRESOLVED~") : getRcvFrom(pMsg), pMsg->pszRawMsg); |
| 577 |
|
| 578 |
/* we now need to go through our list of parsers and see which one is capable of |
| 579 |
* parsing the message. Note that the first parser that requires message sanitization |
| 580 |
* will cause it to happen. After that, access to the unsanitized message is no |
| 581 |
* loger possible. |
| 582 |
*/ |
| 583 |
pParserList = ruleset.GetParserList(runConf, pMsg); |
| 584 |
if (pParserList == NULL) { |
| 585 |
pParserList = runConf->parsers.pDfltParsLst; |
| 586 |
} |
| 587 |
DBGPRINTF("parse using parser list %p%s.\n", pParserList, |
| 588 |
(pParserList == runConf->parsers.pDfltParsLst) ? " (the default list)" : ""); |
| 589 |
|
| 590 |
bIsSanitized = RSFALSE; |
| 591 |
bPRIisParsed = RSFALSE; |
| 592 |
while (pParserList != NULL) { |
| 593 |
pParser = pParserList->pParser; |
| 594 |
if (pParser->bDoSanitazion && bIsSanitized == RSFALSE) { |
| 595 |
CHKiRet(SanitizeMsg(pMsg)); |
| 596 |
if (pParser->bDoPRIParsing && bPRIisParsed == RSFALSE) { |
| 597 |
CHKiRet(ParsePRI(pMsg)); |
| 598 |
bPRIisParsed = RSTRUE; |
| 599 |
} |
| 600 |
bIsSanitized = RSTRUE; |
| 601 |
} |
| 602 |
if (pParser->pModule->mod.pm.parse2 == NULL) |
| 603 |
localRet = pParser->pModule->mod.pm.parse(pMsg); |
| 604 |
else |
| 605 |
localRet = pParser->pModule->mod.pm.parse2(pParser->pInst, pMsg); |
| 606 |
DBGPRINTF("Parser '%s' returned %d\n", pParser->pName, localRet); |
| 607 |
if (localRet != RS_RET_COULD_NOT_PARSE) break; |
| 608 |
pParserList = pParserList->pNext; |
| 609 |
} |
| 610 |
|
| 611 |
/* We need to log a warning message and drop the message if we did not find a parser. |
| 612 |
* Note that we log at most the first 1000 message, as this may very well be a problem |
| 613 |
* that causes a message generation loop. We do not synchronize that counter, it doesn't |
| 614 |
* matter if we log a handful messages more than we should... |
| 615 |
*/ |
| 616 |
if (localRet != RS_RET_OK) { |
| 617 |
if (++iErrMsgRateLimiter < 1000) { |
| 618 |
LogError(0, localRet, |
| 619 |
"Error: one message could not be processed by " |
| 620 |
"any parser, message is being discarded (start of raw msg: '%.60s')", |
| 621 |
pMsg->pszRawMsg); |
| 622 |
} |
| 623 |
DBGPRINTF("No parser could process the message (state %d), we need to discard it.\n", localRet); |
| 624 |
ABORT_FINALIZE(localRet); |
| 625 |
} |
| 626 |
|
| 627 |
/* "finalize" message object */ |
| 628 |
pMsg->msgFlags &= ~NEEDS_PARSING; /* this message is now parsed */ |
| 629 |
|
| 630 |
finalize_it: |
| 631 |
RETiRet; |
| 632 |
} |
| 633 |
|
| 634 |
/* This destroys the master parserlist and all of its parser entries. |
| 635 |
* Parser modules are NOT unloaded, rsyslog does that at a later stage |
| 636 |
* for all dynamically loaded modules. |
| 637 |
*/ |
| 638 |
static rsRetVal destroyMasterParserList(parserList_t *pParserListRoot) { |
| 639 |
DEFiRet; |
| 640 |
parserList_t *pParsLst; |
| 641 |
parserList_t *pParsLstDel; |
| 642 |
|
| 643 |
pParsLst = pParserListRoot; |
| 644 |
while (pParsLst != NULL) { |
| 645 |
parserDestruct(&pParsLst->pParser); |
| 646 |
pParsLstDel = pParsLst; |
| 647 |
pParsLst = pParsLst->pNext; |
| 648 |
free(pParsLstDel); |
| 649 |
} |
| 650 |
RETiRet; |
| 651 |
} |
| 652 |
|
| 653 |
/* queryInterface function-- rgerhards, 2009-11-03 |
| 654 |
*/ |
| 655 |
BEGINobjQueryInterface(parser) |
| 656 |
CODESTARTobjQueryInterface(parser); |
| 657 |
if (pIf->ifVersion != parserCURR_IF_VERSION) { /* check for current version, increment on each change */ |
| 658 |
ABORT_FINALIZE(RS_RET_INTERFACE_NOT_SUPPORTED); |
| 659 |
} |
| 660 |
|
| 661 |
/* ok, we have the right interface, so let's fill it |
| 662 |
* Please note that we may also do some backwards-compatibility |
| 663 |
* work here (if we can support an older interface version - that, |
| 664 |
* of course, also affects the "if" above). |
| 665 |
*/ |
| 666 |
pIf->Construct = parserConstruct; |
| 667 |
pIf->ConstructFinalize = parserConstructFinalize; |
| 668 |
pIf->Destruct = parserDestruct; |
| 669 |
pIf->SetName = SetName; |
| 670 |
pIf->SetModPtr = SetModPtr; |
| 671 |
pIf->SetDoPRIParsing = SetDoPRIParsing; |
| 672 |
pIf->ParseMsg = ParseMsg; |
| 673 |
pIf->SanitizeMsg = SanitizeMsg; |
| 674 |
pIf->DestructParserList = DestructParserList; |
| 675 |
pIf->AddParserToList = AddParserToList; |
| 676 |
pIf->AddDfltParser = AddDfltParser; |
| 677 |
pIf->FindParser = FindParser; |
| 678 |
pIf->destroyMasterParserList = destroyMasterParserList; |
| 679 |
finalize_it: |
| 680 |
ENDobjQueryInterface(parser) |
| 681 |
|
| 682 |
/* Exit our class. |
| 683 |
* rgerhards, 2009-11-04 |
| 684 |
*/ |
| 685 |
BEGINObjClassExit(parser, OBJ_IS_CORE_MODULE) /* class, version */ |
| 686 |
objRelease(glbl, CORE_COMPONENT); |
| 687 |
objRelease(datetime, CORE_COMPONENT); |
| 688 |
objRelease(ruleset, CORE_COMPONENT); |
| 689 |
ENDObjClassExit(parser) |
| 690 |
|
| 691 |
|
| 692 |
/* Initialize the parser class. Must be called as the very first method |
| 693 |
* before anything else is called inside this class. |
| 694 |
* rgerhards, 2009-11-02 |
| 695 |
*/ |
| 696 |
BEGINObjClassInit(parser, 1, OBJ_IS_CORE_MODULE) /* class, version */ |
| 697 |
/* request objects we use */ |
| 698 |
CHKiRet(objUse(glbl, CORE_COMPONENT)); |
| 699 |
CHKiRet(objUse(datetime, CORE_COMPONENT)); |
| 700 |
CHKiRet(objUse(ruleset, CORE_COMPONENT)); |
| 701 |
ENDObjClassInit(parser) |