| 1 |
/* msg.h |
| 2 |
* Header file for all msg-related functions. |
| 3 |
* |
| 4 |
* File begun on 2007-07-13 by RGerhards (extracted from syslogd.c) |
| 5 |
* |
| 6 |
* Copyright 2007-2025 Rainer Gerhards and Adiscon GmbH. |
| 7 |
* |
| 8 |
* This file is part of the rsyslog runtime library. |
| 9 |
* |
| 10 |
* The rsyslog runtime library is free software: you can redistribute it and/or modify |
| 11 |
* it under the terms of the GNU Lesser General Public License as published by |
| 12 |
* the Free Software Foundation, either version 3 of the License, or |
| 13 |
* (at your option) any later version. |
| 14 |
* |
| 15 |
* The rsyslog runtime library is distributed in the hope that it will be useful, |
| 16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
* GNU Lesser General Public License for more details. |
| 19 |
* |
| 20 |
* You should have received a copy of the GNU Lesser General Public License |
| 21 |
* along with the rsyslog runtime library. If not, see <http://www.gnu.org/licenses/>. |
| 22 |
* |
| 23 |
* A copy of the GPL can be found in the file "COPYING" in this distribution. |
| 24 |
* A copy of the LGPL can be found in the file "COPYING.LESSER" in this distribution. |
| 25 |
*/ |
| 26 |
#include "template.h" /* this is a quirk, but these two are too interdependant... */ |
| 27 |
|
| 28 |
#ifndef MSG_H_INCLUDED |
| 29 |
#define MSG_H_INCLUDED 1 |
| 30 |
|
| 31 |
#include <pthread.h> |
| 32 |
#include <libestr.h> |
| 33 |
#include <stdint.h> |
| 34 |
#include <json.h> |
| 35 |
#include "obj.h" |
| 36 |
#include "syslogd-types.h" |
| 37 |
#include "template.h" |
| 38 |
#include "atomic.h" |
| 39 |
|
| 40 |
/* rgerhards 2004-11-08: The following structure represents a |
| 41 |
* syslog message. |
| 42 |
* |
| 43 |
* Important Note: |
| 44 |
* The message object is used for multiple purposes (once it |
| 45 |
* has been created). Once created, it actully is a read-only |
| 46 |
* object (though we do not specifically express this). In order |
| 47 |
* to avoid multiple copies of the same object, we use a |
| 48 |
* reference counter. This counter is set to 1 by the constructer |
| 49 |
* and increased by 1 with a call to MsgAddRef(). The destructor |
| 50 |
* checks the reference count. If it is more than 1, only the counter |
| 51 |
* will be decremented. If it is 1, however, the object is actually |
| 52 |
* destroyed. To make this work, it is vital that MsgAddRef() is |
| 53 |
* called each time a "copy" is stored somewhere. |
| 54 |
* |
| 55 |
* WARNING: this structure is not calloc()ed, so be careful when |
| 56 |
* adding new fields. You need to initialize them in |
| 57 |
* msgBaseConstruct(). That function header comment also describes |
| 58 |
* why this is the case. |
| 59 |
*/ |
| 60 |
struct msg { |
| 61 |
BEGINobjInstance |
| 62 |
; /* Data to implement generic object - MUST be the first data element! */ |
| 63 |
flowControl_t flowCtlType; |
| 64 |
/**< type of flow control we can apply, for enqueueing, needs not to be persisted because |
| 65 |
once data has entered the queue, this property is no longer needed. */ |
| 66 |
pthread_mutex_t mut; |
| 67 |
int iRefCount; /* reference counter (0 = unused) */ |
| 68 |
sbool bParseSuccess; /* set to reflect state of last executed higher level parser */ |
| 69 |
unsigned short iSeverity; /* the severity */ |
| 70 |
unsigned short iFacility; /* Facility code */ |
| 71 |
int offAfterPRI; /* offset, at which raw message WITHOUT PRI part starts in pszRawMsg */ |
| 72 |
int offMSG; /* offset at which the MSG part starts in pszRawMsg */ |
| 73 |
short iProtocolVersion; /* protocol version of message received 0 - legacy, 1 syslog-protocol) */ |
| 74 |
int msgFlags; /* flags associated with this message */ |
| 75 |
int iLenRawMsg; /* length of raw message */ |
| 76 |
int iLenMSG; /* Length of the MSG part */ |
| 77 |
int iLenTAG; /* Length of the TAG part */ |
| 78 |
int iLenHOSTNAME; /* Length of HOSTNAME */ |
| 79 |
int iLenPROGNAME; /* Length of PROGNAME (-1 = not yet set) */ |
| 80 |
uchar *pszRawMsg; /* message as it was received on the wire. This is important in case we |
| 81 |
* need to preserve cryptographic verifiers. */ |
| 82 |
uchar *pszHOSTNAME; /* HOSTNAME from syslog message */ |
| 83 |
char *pszRcvdAt3164; /* time as RFC3164 formatted string (always 15 characters) */ |
| 84 |
char *pszRcvdAt3339; /* time as RFC3164 formatted string (32 characters at most) */ |
| 85 |
char *pszRcvdAt_MySQL; /* rcvdAt as MySQL formatted string (always 14 characters) */ |
| 86 |
char *pszRcvdAt_PgSQL; /* rcvdAt as PgSQL formatted string (always 21 characters) */ |
| 87 |
char *pszTIMESTAMP3164; /* TIMESTAMP as RFC3164 formatted string (always 15 characters) */ |
| 88 |
char *pszTIMESTAMP3339; /* TIMESTAMP as RFC3339 formatted string (32 characters at most) */ |
| 89 |
char *pszTIMESTAMP_MySQL; /* TIMESTAMP as MySQL formatted string (always 14 characters) */ |
| 90 |
char *pszTIMESTAMP_PgSQL; /* TIMESTAMP as PgSQL formatted string (always 21 characters) */ |
| 91 |
uchar *pszStrucData; /* STRUCTURED-DATA */ |
| 92 |
uint16_t lenStrucData; /* (cached) length of STRUCTURED-DATA */ |
| 93 |
cstr_t *pCSAPPNAME; /* APP-NAME */ |
| 94 |
cstr_t *pCSPROCID; /* PROCID */ |
| 95 |
cstr_t *pCSMSGID; /* MSGID */ |
| 96 |
prop_t *pInputName; /* input name property */ |
| 97 |
prop_t *pRcvFromIP; /* IP of system message was received from */ |
| 98 |
prop_t *pRcvFromPort; /* port of system message was received from */ |
| 99 |
union { |
| 100 |
prop_t *pRcvFrom; /* name of system message was received from */ |
| 101 |
struct sockaddr_storage *pfrominet; /* unresolved name */ |
| 102 |
} rcvFrom; |
| 103 |
|
| 104 |
ruleset_t *pRuleset; /* ruleset to be used for processing this message */ |
| 105 |
time_t ttGenTime; /* time msg object was generated, same as tRcvdAt, but a Unix timestamp. |
| 106 |
While this field looks redundant, it is required because a Unix timestamp |
| 107 |
is used at later processing stages (namely in the output arena). Thanks to |
| 108 |
the subleties of how time is defined, there is no reliable way to reconstruct |
| 109 |
the Unix timestamp from the syslogTime fields (in practice, we may be close |
| 110 |
enough to reliable, but I prefer to leave the subtle things to the OS, where |
| 111 |
it obviously is solved in way or another...). */ |
| 112 |
struct syslogTime tRcvdAt; /* time the message entered this program */ |
| 113 |
struct syslogTime tTIMESTAMP; /* (parsed) value of the timestamp */ |
| 114 |
struct json_object *json; |
| 115 |
struct json_object *localvars; |
| 116 |
/* some fixed-size buffers to save malloc()/free() for frequently used fields (from the default templates) */ |
| 117 |
uchar szRawMsg[CONF_RAWMSG_BUFSIZE]; |
| 118 |
/* most messages are small, and these are stored here (without malloc/free!) */ |
| 119 |
uchar szHOSTNAME[CONF_HOSTNAME_BUFSIZE]; |
| 120 |
union { |
| 121 |
uchar *ptr; /* pointer to progname value */ |
| 122 |
uchar szBuf[CONF_PROGNAME_BUFSIZE]; |
| 123 |
} PROGNAME; |
| 124 |
union { |
| 125 |
uchar *pszTAG; /* pointer to tag value */ |
| 126 |
uchar szBuf[CONF_TAG_BUFSIZE]; |
| 127 |
} TAG; |
| 128 |
char pszTimestamp3164[CONST_LEN_TIMESTAMP_3164 + 1]; |
| 129 |
char pszTimestamp3339[CONST_LEN_TIMESTAMP_3339 + 1]; |
| 130 |
char pszTIMESTAMP_SecFrac[7]; |
| 131 |
/* Note: a pointer is 64 bits/8 char, so this is actually fewer than a pointer! */ |
| 132 |
char pszRcvdAt_SecFrac[7]; |
| 133 |
/* same as above. Both are fractional seconds for their respective timestamp */ |
| 134 |
char pszTIMESTAMP_Unix[12]; /* almost as small as a pointer! */ |
| 135 |
char pszRcvdAt_Unix[12]; |
| 136 |
char dfltTZ[8]; /* 7 chars max, less overhead than ptr! */ |
| 137 |
uchar *pszUUID; /* The message's UUID */ |
| 138 |
}; |
| 139 |
|
| 140 |
|
| 141 |
/* message flags (msgFlags), not an enum for historical reasons */ |
| 142 |
#define NOFLAG 0x000 |
| 143 |
/* no flag is set (to be used when a flag must be specified and none is required) */ |
| 144 |
#define INTERNAL_MSG 0x001 |
| 145 |
/* msg generated by logmsgInternal() --> special handling */ |
| 146 |
/* 0x002 not used because it was previously a known value - rgerhards, 2008-10-09 */ |
| 147 |
#define IGNDATE 0x004 |
| 148 |
/* ignore, if given, date in message and use date of reception as msg date */ |
| 149 |
#define MARK 0x008 |
| 150 |
/* this message is a mark */ |
| 151 |
#define NEEDS_PARSING 0x010 |
| 152 |
/* raw message, must be parsed before processing can be done */ |
| 153 |
#define PARSE_HOSTNAME 0x020 |
| 154 |
/* parse the hostname during message parsing */ |
| 155 |
#define NEEDS_DNSRESOL 0x040 |
| 156 |
/* fromhost address is unresolved and must be locked up via DNS reverse lookup first */ |
| 157 |
#define NEEDS_ACLCHK_U 0x080 |
| 158 |
/* check UDP ACLs after DNS resolution has been done in main queue consumer */ |
| 159 |
#define NO_PRI_IN_RAW 0x100 |
| 160 |
/* rawmsg does not include a PRI (Solaris!), but PRI is already set correctly in the msg object */ |
| 161 |
#define PRESERVE_CASE 0x200 |
| 162 |
/* preserve case in fromhost */ |
| 163 |
|
| 164 |
/* (syslog) protocol types */ |
| 165 |
#define MSG_LEGACY_PROTOCOL 0 |
| 166 |
#define MSG_RFC5424_PROTOCOL 1 |
| 167 |
|
| 168 |
#define MAX_VARIABLE_NAME_LEN 1024 |
| 169 |
|
| 170 |
/* function prototypes |
| 171 |
*/ |
| 172 |
PROTOTYPEObjClassInit(msg); |
| 173 |
rsRetVal msgConstruct(smsg_t **ppThis); |
| 174 |
rsRetVal msgConstructWithTime(smsg_t **ppThis, const struct syslogTime *stTime, const time_t ttGenTime); |
| 175 |
rsRetVal msgConstructForDeserializer(smsg_t **ppThis); |
| 176 |
rsRetVal msgConstructFinalizer(smsg_t *pThis); |
| 177 |
rsRetVal msgDestruct(smsg_t **ppM); |
| 178 |
smsg_t *MsgDup(smsg_t *pOld); |
| 179 |
smsg_t *MsgAddRef(smsg_t *pM); |
| 180 |
void setProtocolVersion(smsg_t *pM, int iNewVersion); |
| 181 |
void MsgSetInputName(smsg_t *pMsg, prop_t *); |
| 182 |
void MsgSetDfltTZ(smsg_t *pThis, char *tz); |
| 183 |
rsRetVal MsgSetAPPNAME(smsg_t *pMsg, const char *pszAPPNAME); |
| 184 |
rsRetVal MsgSetPROCID(smsg_t *pMsg, const char *pszPROCID); |
| 185 |
rsRetVal MsgSetMSGID(smsg_t *pMsg, const char *pszMSGID); |
| 186 |
void MsgSetParseSuccess(smsg_t *pMsg, int bSuccess); |
| 187 |
void MsgSetTAG(smsg_t *pMsg, const uchar *pszBuf, const size_t lenBuf); |
| 188 |
void MsgSetRuleset(smsg_t *pMsg, ruleset_t *); |
| 189 |
rsRetVal MsgSetFlowControlType(smsg_t *pMsg, flowControl_t eFlowCtl); |
| 190 |
rsRetVal MsgSetStructuredData(smsg_t *const pMsg, const char *pszStrucData); |
| 191 |
rsRetVal MsgAddToStructuredData(smsg_t *pMsg, uchar *toadd, rs_size_t len); |
| 192 |
void MsgGetStructuredData(smsg_t *pM, uchar **pBuf, rs_size_t *len); |
| 193 |
rsRetVal msgSetFromSockinfo(smsg_t *pThis, struct sockaddr_storage *sa); |
| 194 |
void MsgSetRcvFrom(smsg_t *pMsg, prop_t *); |
| 195 |
void MsgSetRcvFromStr(smsg_t *const pMsg, const uchar *pszRcvFrom, const int, prop_t **); |
| 196 |
rsRetVal MsgSetRcvFromIP(smsg_t *pMsg, prop_t *); |
| 197 |
rsRetVal MsgSetRcvFromIPStr(smsg_t *const pThis, const uchar *psz, const int len, prop_t **ppProp); |
| 198 |
rsRetVal MsgSetRcvFromPort(smsg_t *pMsg, prop_t *); |
| 199 |
rsRetVal MsgSetRcvFromPortStr(smsg_t *const pThis, const uchar *psz, const int len, prop_t **ppProp); |
| 200 |
void MsgSetHOSTNAME(smsg_t *pMsg, const uchar *pszHOSTNAME, const int lenHOSTNAME); |
| 201 |
rsRetVal MsgSetAfterPRIOffs(smsg_t *pMsg, int offs); |
| 202 |
void MsgSetMSGoffs(smsg_t *pMsg, int offs); |
| 203 |
void MsgSetRawMsgWOSize(smsg_t *pMsg, char *pszRawMsg); |
| 204 |
void ATTR_NONNULL() MsgSetRawMsg(smsg_t *const pThis, const char *const pszRawMsg, const size_t lenMsg); |
| 205 |
rsRetVal MsgReplaceMSG(smsg_t *pThis, const uchar *pszMSG, int lenMSG); |
| 206 |
uchar *MsgGetProp(smsg_t *pMsg, |
| 207 |
struct templateEntry *pTpe, |
| 208 |
msgPropDescr_t *pProp, |
| 209 |
rs_size_t *pPropLen, |
| 210 |
unsigned short *pbMustBeFreed, |
| 211 |
struct syslogTime *ttNow); |
| 212 |
void getTAG(smsg_t *pM, uchar **ppBuf, int *piLen, sbool); |
| 213 |
const char *getTimeReported(smsg_t *pM, enum tplFormatTypes eFmt); |
| 214 |
const char *getPRI(smsg_t *pMsg); |
| 215 |
int getPRIi(const smsg_t *const pM); |
| 216 |
int ATTR_NONNULL() getRawMsgLen(const smsg_t *const pMsg); |
| 217 |
void getRawMsg(const smsg_t *pM, uchar **pBuf, int *piLen); |
| 218 |
void ATTR_NONNULL() MsgTruncateToMaxSize(smsg_t *const pThis); |
| 219 |
rsRetVal msgAddJSON(smsg_t *pM, uchar *name, struct json_object *json, int force_reset, int sharedReference); |
| 220 |
rsRetVal msgAddMetadata(smsg_t *msg, uchar *metaname, uchar *metaval); |
| 221 |
rsRetVal msgAddMultiMetadata(smsg_t *msg, const uchar **metaname, const uchar **metaval, const int count); |
| 222 |
rsRetVal MsgGetSeverity(smsg_t *pThis, int *piSeverity); |
| 223 |
rsRetVal MsgDeserialize(smsg_t *pMsg, strm_t *pStrm); |
| 224 |
rsRetVal MsgSetPropsViaJSON(smsg_t *__restrict__ const pMsg, const uchar *__restrict__ const json); |
| 225 |
rsRetVal MsgSetPropsViaJSON_Object(smsg_t *__restrict__ const pMsg, struct json_object *json); |
| 226 |
const uchar *msgGetJSONMESG(smsg_t *__restrict__ const pMsg); |
| 227 |
|
| 228 |
/** |
| 229 |
* @brief Acquire the message mutex. |
| 230 |
* |
| 231 |
* @param pMsg Message instance to lock. |
| 232 |
*/ |
| 233 |
void MsgLock(smsg_t *pMsg); |
| 234 |
|
| 235 |
/** |
| 236 |
* @brief Release the message mutex. |
| 237 |
* |
| 238 |
* @param pMsg Message instance to unlock. |
| 239 |
*/ |
| 240 |
void MsgUnlock(smsg_t *pMsg); |
| 241 |
|
| 242 |
uchar *getMSG(smsg_t *pM); |
| 243 |
const char *getHOSTNAME(smsg_t *pM); |
| 244 |
char *getPROCID(smsg_t *pM, sbool bLockMutex); |
| 245 |
char *getAPPNAME(smsg_t *pM, sbool bLockMutex); |
| 246 |
void setMSGLen(smsg_t *pM, int lenMsg); |
| 247 |
int getMSGLen(smsg_t *pM); |
| 248 |
void getInputName(const smsg_t *const pM, uchar **ppsz, int *const plen); |
| 249 |
|
| 250 |
int getHOSTNAMELen(smsg_t *pM); |
| 251 |
uchar *getProgramName(smsg_t *pM, sbool bLockMutex); |
| 252 |
uchar *getRcvFrom(smsg_t *pM); |
| 253 |
rsRetVal propNameToID(const uchar *pName, propid_t *pPropID); |
| 254 |
uchar *propIDToName(propid_t propID); |
| 255 |
rsRetVal ATTR_NONNULL() msgCheckVarExists(smsg_t *const pMsg, msgPropDescr_t *pProp); |
| 256 |
rsRetVal msgGetJSONPropJSON(smsg_t *pMsg, msgPropDescr_t *pProp, struct json_object **pjson); |
| 257 |
rsRetVal msgGetJSONPropJSONorString(smsg_t *const pMsg, |
| 258 |
msgPropDescr_t *pProp, |
| 259 |
struct json_object **pjson, |
| 260 |
uchar **pcstr); |
| 261 |
rsRetVal getJSONPropVal( |
| 262 |
smsg_t *pMsg, msgPropDescr_t *pProp, uchar **pRes, rs_size_t *buflen, unsigned short *pbMustBeFreed); |
| 263 |
rsRetVal msgSetJSONFromVar(smsg_t *pMsg, uchar *varname, struct svar *var, int force_reset); |
| 264 |
rsRetVal msgDelJSON(smsg_t *pMsg, uchar *varname); |
| 265 |
rsRetVal jsonFind(smsg_t *const pMsg, msgPropDescr_t *pProp, struct json_object **jsonres); |
| 266 |
struct json_object *jsonDeepCopy(struct json_object *src); |
| 267 |
|
| 268 |
rsRetVal msgPropDescrFill(msgPropDescr_t *pProp, uchar *name, int nameLen); |
| 269 |
void msgPropDescrDestruct(msgPropDescr_t *pProp); |
| 270 |
void msgSetPRI(smsg_t *const __restrict__ pMsg, syslog_pri_t pri); |
| 271 |
|
| 272 |
#define msgGetProtocolVersion(pM) ((pM)->iProtocolVersion) |
| 273 |
|
| 274 |
/* returns non-zero if the message has structured data, 0 otherwise */ |
| 275 |
#define MsgHasStructuredData(pM) (((pM)->pszStrucData == NULL) ? 0 : 1) |
| 276 |
|
| 277 |
/* ------------------------------ some inline functions ------------------------------ */ |
| 278 |
|
| 279 |
/* add Metadata to the message. This is stored in a special JSON |
| 280 |
* container. Note that only string types are currently supported, |
| 281 |
* what should pose absolutely no problem with the string-ish nature |
| 282 |
* of rsyslog metadata. |
| 283 |
* added 2015-01-09 rgerhards |
| 284 |
*/ |
| 285 |
/* set raw message size. This is needed in some cases where a trunctation is necessary |
| 286 |
* but the raw message must not be newly set. The most important (and currently only) |
| 287 |
* use case is if we remove trailing LF or NUL characters. Note that the size can NOT |
| 288 |
* be extended, only shrunk! |
| 289 |
* rgerhards, 2009-08-26 |
| 290 |
*/ |
| 291 |
static inline void __attribute__((unused)) MsgSetRawMsgSize(smsg_t *const __restrict__ pMsg, const size_t newLen) { |
| 292 |
assert(newLen <= (size_t)pMsg->iLenRawMsg); |
| 293 |
pMsg->iLenRawMsg = newLen; |
| 294 |
pMsg->pszRawMsg[newLen] = '\0'; |
| 295 |
} |
| 296 |
|
| 297 |
/* get the ruleset that is associated with the ruleset. |
| 298 |
* May be NULL. -- rgerhards, 2009-10-27 |
| 299 |
*/ |
| 300 |
#define MsgGetRuleset(pMsg) ((pMsg)->pRuleset) |
| 301 |
|
| 302 |
#endif /* #ifndef MSG_H_INCLUDED */ |