Debian Codegraph @

runtime/netstrms.c

raw
1 /* netstrms.c
2 *
3 * Work on this module begun 2008-04-23 by Rainer Gerhards.
4 *
5 * Copyright 2008-2025 Rainer Gerhards and Adiscon GmbH.
6 *
7 * This file is part of the rsyslog runtime library.
8 *
9 * The rsyslog runtime library is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * The rsyslog runtime library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with the rsyslog runtime library. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * A copy of the GPL can be found in the file "COPYING" in this distribution.
23 * A copy of the LGPL can be found in the file "COPYING.LESSER" in this distribution.
24 */
25 #include "config.h"
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <assert.h>
30 #include <errno.h>
31
32 #include "rsyslog.h"
33 #include "module-template.h"
34 #include "obj.h"
35 #include "nsd.h"
36 #include "netstrm.h"
37 #include "netstrms.h"
38 #include "rsconf.h"
39
40 MODULE_TYPE_LIB
41 MODULE_TYPE_NOKEEP;
42
43 /* static data */
44 DEFobjStaticHelpers;
45 DEFobjCurrIf(glbl) DEFobjCurrIf(netstrm)
46
47
48 /* load our low-level driver. This must be done before any
49 * driver-specific functions (allmost all...) can be carried
50 * out. Note that the driver's .ifIsLoaded is correctly
51 * initialized by calloc() and we depend on that.
52 */
53 static rsRetVal loadDrvr(netstrms_t *pThis) {
54 DEFiRet;
55 uchar *pBaseDrvrName;
56 uchar szDrvrName[48]; /* 48 shall be large enough */
57
58 pBaseDrvrName = pThis->pBaseDrvrName;
59 if (pBaseDrvrName == NULL) /* if no drvr name is set, use system default */
60 pBaseDrvrName = glbl.GetDfltNetstrmDrvr(runConf);
61 if (snprintf((char *)szDrvrName, sizeof(szDrvrName), "lmnsd_%s", pBaseDrvrName) == sizeof(szDrvrName))
62 ABORT_FINALIZE(RS_RET_DRVRNAME_TOO_LONG);
63 CHKmalloc(pThis->pDrvrName = (uchar *)strdup((char *)szDrvrName));
64
65 pThis->Drvr.ifVersion = nsdCURR_IF_VERSION;
66 /* The pDrvrName+2 below is a hack to obtain the object name. It
67 * safes us to have yet another variable with the name without "lm" in
68 * front of it. If we change the module load interface, we may re-think
69 * about this hack, but for the time being it is efficient and clean
70 * enough. -- rgerhards, 2008-04-18
71 */
72 CHKiRet(obj.UseObj(__FILE__, szDrvrName + 2, szDrvrName, (void *)&pThis->Drvr));
73
74 finalize_it:
75 if (iRet != RS_RET_OK) {
76 if (pThis->pDrvrName != NULL) {
77 free(pThis->pDrvrName);
78 pThis->pDrvrName = NULL;
79 }
80 }
81 RETiRet;
82 }
83
84
85 /* Standard-Constructor */
86 BEGINobjConstruct(netstrms) /* be sure to specify the object type also in END macro! */
87 ENDobjConstruct(netstrms)
88
89
90 /* destructor for the netstrms object */
91 BEGINobjDestruct(netstrms) /* be sure to specify the object type also in END and CODESTART macros! */
92 CODESTARTobjDestruct(netstrms);
93 /* and now we must release our driver, if we got one. We use the presence of
94 * a driver name string as load indicator (because we also need that string
95 * to release the driver
96 */
97 if (pThis->pDrvrName != NULL) {
98 obj.ReleaseObj(__FILE__, pThis->pDrvrName + 2, pThis->pDrvrName, (void *)&pThis->Drvr);
99 free(pThis->pDrvrName);
100 }
101 if (pThis->pszDrvrAuthMode != NULL) {
102 free(pThis->pszDrvrAuthMode);
103 pThis->pszDrvrAuthMode = NULL;
104 }
105 if (pThis->pszDrvrPermitExpiredCerts != NULL) {
106 free(pThis->pszDrvrPermitExpiredCerts);
107 pThis->pszDrvrPermitExpiredCerts = NULL;
108 }
109 free((void *)pThis->pszDrvrCAFile);
110 pThis->pszDrvrCAFile = NULL;
111 free((void *)pThis->pszDrvrCRLFile);
112 pThis->pszDrvrCRLFile = NULL;
113 free((void *)pThis->pszDrvrKeyFile);
114 pThis->pszDrvrKeyFile = NULL;
115 free((void *)pThis->pszDrvrCertFile);
116 pThis->pszDrvrCertFile = NULL;
117 if (pThis->pBaseDrvrName != NULL) {
118 free(pThis->pBaseDrvrName);
119 pThis->pBaseDrvrName = NULL;
120 }
121 if (pThis->gnutlsPriorityString != NULL) {
122 free(pThis->gnutlsPriorityString);
123 pThis->gnutlsPriorityString = NULL;
124 }
125 if (pThis->remoteSNI != NULL) {
126 free(pThis->remoteSNI);
127 pThis->remoteSNI = NULL;
128 }
129 ENDobjDestruct(netstrms)
130
131
132 /* ConstructionFinalizer */
133 static rsRetVal netstrmsConstructFinalize(netstrms_t *pThis) {
134 DEFiRet;
135 ISOBJ_TYPE_assert(pThis, netstrms);
136 iRet = loadDrvr(pThis);
137 RETiRet;
138 }
139
140
141 static rsRetVal SetSynBacklog(netstrms_t *pThis, const int iSynBacklog) {
142 DEFiRet;
143 ISOBJ_TYPE_assert(pThis, netstrms);
144 pThis->iSynBacklog = iSynBacklog;
145 RETiRet;
146 }
147
148
149 /* set the base driver name. If the driver name
150 * is set to NULL, the previously set name is deleted but
151 * no name set again (which results in the system default being
152 * used)-- rgerhards, 2008-05-05
153 */
154 static rsRetVal SetDrvrName(netstrms_t *pThis, uchar *pszName) {
155 DEFiRet;
156 ISOBJ_TYPE_assert(pThis, netstrms);
157 if (pThis->pBaseDrvrName != NULL) {
158 free(pThis->pBaseDrvrName);
159 pThis->pBaseDrvrName = NULL;
160 }
161
162 if (pszName != NULL) {
163 CHKmalloc(pThis->pBaseDrvrName = (uchar *)strdup((char *)pszName));
164 }
165 finalize_it:
166 RETiRet;
167 }
168
169
170 /* set the driver's permitted peers -- rgerhards, 2008-05-19 */
171 static rsRetVal SetDrvrPermPeers(netstrms_t *pThis, permittedPeers_t *pPermPeers) {
172 DEFiRet;
173 ISOBJ_TYPE_assert(pThis, netstrms);
174 pThis->pPermPeers = pPermPeers;
175 RETiRet;
176 }
177 /* return the driver's permitted peers
178 * We use non-standard calling conventions because it makes an awful lot
179 * of sense here.
180 * rgerhards, 2008-05-19
181 */
182 static permittedPeers_t *GetDrvrPermPeers(netstrms_t *pThis) {
183 ISOBJ_TYPE_assert(pThis, netstrms);
184 return pThis->pPermPeers;
185 }
186
187
188 /* set the driver auth mode -- rgerhards, 2008-05-19 */
189 static rsRetVal SetDrvrAuthMode(netstrms_t *pThis, uchar *mode) {
190 DEFiRet;
191 ISOBJ_TYPE_assert(pThis, netstrms);
192 CHKmalloc(pThis->pszDrvrAuthMode = (uchar *)strdup((char *)mode));
193 finalize_it:
194 RETiRet;
195 }
196
197
198 /* return the driver auth mode
199 * We use non-standard calling conventions because it makes an awful lot
200 * of sense here.
201 * rgerhards, 2008-05-19
202 */
203 static uchar *GetDrvrAuthMode(netstrms_t *pThis) {
204 ISOBJ_TYPE_assert(pThis, netstrms);
205 return pThis->pszDrvrAuthMode;
206 }
207
208
209 /* return the driver permitexpiredcerts mode
210 * We use non-standard calling conventions because it makes an awful lot
211 * of sense here.
212 * alorbach, 2018-12-21
213 */
214 static uchar *GetDrvrPermitExpiredCerts(netstrms_t *pThis) {
215 ISOBJ_TYPE_assert(pThis, netstrms);
216 return pThis->pszDrvrPermitExpiredCerts;
217 }
218
219 /* set the driver permitexpiredcerts mode -- alorbach, 2018-12-20
220 */
221 static rsRetVal SetDrvrPermitExpiredCerts(netstrms_t *pThis, uchar *mode) {
222 DEFiRet;
223 ISOBJ_TYPE_assert(pThis, netstrms);
224 if (mode != NULL) {
225 CHKmalloc(pThis->pszDrvrPermitExpiredCerts = (uchar *)strdup((char *)mode));
226 }
227 finalize_it:
228 RETiRet;
229 }
230
231 static rsRetVal SetDrvrTlsCAFile(netstrms_t *pThis, const uchar *mode) {
232 DEFiRet;
233 ISOBJ_TYPE_assert(pThis, netstrms);
234 if (mode != NULL) {
235 CHKmalloc(pThis->pszDrvrCAFile = (uchar *)strdup((char *)mode));
236 }
237 finalize_it:
238 RETiRet;
239 }
240
241 static rsRetVal SetDrvrTlsCRLFile(netstrms_t *pThis, const uchar *mode) {
242 DEFiRet;
243 ISOBJ_TYPE_assert(pThis, netstrms);
244 if (mode != NULL) {
245 CHKmalloc(pThis->pszDrvrCRLFile = (uchar *)strdup((char *)mode));
246 }
247 finalize_it:
248 RETiRet;
249 }
250
251 static rsRetVal SetDrvrTlsKeyFile(netstrms_t *pThis, const uchar *mode) {
252 DEFiRet;
253 ISOBJ_TYPE_assert(pThis, netstrms);
254 if (mode != NULL) {
255 CHKmalloc(pThis->pszDrvrKeyFile = (uchar *)strdup((char *)mode));
256 }
257 finalize_it:
258 RETiRet;
259 }
260
261 static rsRetVal SetDrvrTlsCertFile(netstrms_t *pThis, const uchar *mode) {
262 DEFiRet;
263 ISOBJ_TYPE_assert(pThis, netstrms);
264 if (mode != NULL) {
265 CHKmalloc(pThis->pszDrvrCertFile = (uchar *)strdup((char *)mode));
266 }
267 finalize_it:
268 RETiRet;
269 }
270
271
272 /* Set the priorityString
273 * PascalWithopf 2017-08-16
274 */
275 static rsRetVal SetDrvrGnutlsPriorityString(netstrms_t *pThis, uchar *iVal) {
276 DEFiRet;
277 ISOBJ_TYPE_assert(pThis, netstrms);
278 CHKmalloc(pThis->gnutlsPriorityString = (uchar *)strdup((char *)iVal));
279 finalize_it:
280 RETiRet;
281 }
282
283
284 /* return the priorityString
285 * PascalWithopf, 2017-08-16
286 */
287 static uchar *GetDrvrGnutlsPriorityString(netstrms_t *pThis) {
288 ISOBJ_TYPE_assert(pThis, netstrms);
289 return pThis->gnutlsPriorityString;
290 }
291
292
293 /* set the driver mode -- rgerhards, 2008-04-30 */
294 static rsRetVal SetDrvrMode(netstrms_t *pThis, int iMode) {
295 DEFiRet;
296 ISOBJ_TYPE_assert(pThis, netstrms);
297 pThis->iDrvrMode = iMode;
298 RETiRet;
299 }
300
301
302 /* return the driver mode
303 * We use non-standard calling conventions because it makes an awful lot
304 * of sense here.
305 * rgerhards, 2008-04-30
306 */
307 static int GetDrvrMode(netstrms_t *pThis) {
308 ISOBJ_TYPE_assert(pThis, netstrms);
309 return pThis->iDrvrMode;
310 }
311
312
313 /* set the driver cert extended key usage check setting -- jvymazal, 2019-08-16 */
314 static rsRetVal SetDrvrCheckExtendedKeyUsage(netstrms_t *pThis, int ChkExtendedKeyUsage) {
315 DEFiRet;
316 ISOBJ_TYPE_assert(pThis, netstrms);
317 pThis->DrvrChkExtendedKeyUsage = ChkExtendedKeyUsage;
318 RETiRet;
319 }
320
321
322 /* return the driver cert extended key usage check setting
323 * jvymazal, 2019-08-16
324 */
325 static int GetDrvrCheckExtendedKeyUsage(netstrms_t *pThis) {
326 ISOBJ_TYPE_assert(pThis, netstrms);
327 return pThis->DrvrChkExtendedKeyUsage;
328 }
329
330
331 /* set the driver name checking policy -- jvymazal, 2019-08-16 */
332 static rsRetVal SetDrvrPrioritizeSAN(netstrms_t *pThis, int prioritizeSan) {
333 DEFiRet;
334 ISOBJ_TYPE_assert(pThis, netstrms);
335 pThis->DrvrPrioritizeSan = prioritizeSan;
336 RETiRet;
337 }
338
339
340 /* return the driver name checking policy
341 * jvymazal, 2019-08-16
342 */
343 static int GetDrvrPrioritizeSAN(netstrms_t *pThis) {
344 ISOBJ_TYPE_assert(pThis, netstrms);
345 return pThis->DrvrPrioritizeSan;
346 }
347
348 /* set the driver TlsVerifyDepth -- alorbach, 2019-12-20 */
349 /* set TLS revocation check */
350 static rsRetVal SetDrvrTlsRevocationCheck(netstrms_t *pThis, int enabled) {
351 DEFiRet;
352 ISOBJ_TYPE_assert(pThis, netstrms);
353 pThis->DrvrTlsRevocationCheck = (enabled != 0) ? 1 : 0;
354 RETiRet;
355 }
356
357 /* return TLS revocation check setting */
358 static int GetDrvrTlsRevocationCheck(netstrms_t *pThis) {
359 ISOBJ_TYPE_assert(pThis, netstrms);
360 return pThis->DrvrTlsRevocationCheck;
361 }
362
363 static rsRetVal SetDrvrTlsVerifyDepth(netstrms_t *pThis, int verifyDepth) {
364 DEFiRet;
365 ISOBJ_TYPE_assert(pThis, netstrms);
366 pThis->DrvrVerifyDepth = verifyDepth;
367 RETiRet;
368 }
369
370 /* return the driver TlsVerifyDepth
371 * alorbach, 2019-12-20
372 */
373 static int GetDrvrTlsVerifyDepth(netstrms_t *pThis) {
374 ISOBJ_TYPE_assert(pThis, netstrms);
375 return pThis->DrvrVerifyDepth;
376 }
377
378 static const uchar *GetDrvrTlsCAFile(netstrms_t *pThis) {
379 ISOBJ_TYPE_assert(pThis, netstrms);
380 return pThis->pszDrvrCAFile;
381 }
382 static const uchar *GetDrvrTlsCRLFile(netstrms_t *pThis) {
383 ISOBJ_TYPE_assert(pThis, netstrms);
384 return pThis->pszDrvrCRLFile;
385 }
386 static const uchar *GetDrvrTlsKeyFile(netstrms_t *pThis) {
387 ISOBJ_TYPE_assert(pThis, netstrms);
388 return pThis->pszDrvrKeyFile;
389 }
390 static const uchar *GetDrvrTlsCertFile(netstrms_t *pThis) {
391 ISOBJ_TYPE_assert(pThis, netstrms);
392 return pThis->pszDrvrCertFile;
393 }
394
395 static rsRetVal SetDrvrRemoteSNI(netstrms_t *pThis, uchar *remoteSNI) {
396 DEFiRet;
397 ISOBJ_TYPE_assert(pThis, netstrms);
398 CHKmalloc(pThis->remoteSNI = (uchar *)strdup((char *)remoteSNI));
399 finalize_it:
400 RETiRet;
401 }
402
403 /* Return the remote server SNI */
404 static uchar *GetDrvrRemoteSNI(netstrms_t *pThis) {
405 ISOBJ_TYPE_assert(pThis, netstrms);
406 return pThis->remoteSNI;
407 }
408
409 /* create an instance of a netstrm object. It is initialized with default
410 * values. The current driver is used. The caller may set netstrm properties
411 * and must call ConstructFinalize().
412 */
413 static rsRetVal CreateStrm(netstrms_t *pThis, netstrm_t **ppStrm) {
414 netstrm_t *pStrm = NULL;
415 DEFiRet;
416
417 CHKiRet(objUse(netstrm, DONT_LOAD_LIB));
418 CHKiRet(netstrm.Construct(&pStrm));
419 /* we copy over our driver structure. We could provide a pointer to
420 * ourselves, but that costs some performance on each driver invocation.
421 * As we already have hefty indirection (and thus performance toll), I
422 * prefer to copy over the function pointers here. -- rgerhards, 2008-04-23
423 */
424 memcpy(&pStrm->Drvr, &pThis->Drvr, sizeof(pThis->Drvr));
425 pStrm->pNS = pThis;
426
427 *ppStrm = pStrm;
428
429 finalize_it:
430 if (iRet != RS_RET_OK) {
431 if (pStrm != NULL) netstrm.Destruct(&pStrm);
432 }
433 RETiRet;
434 }
435
436
437 /* queryInterface function */
438 BEGINobjQueryInterface(netstrms)
439 CODESTARTobjQueryInterface(netstrms);
440 if (pIf->ifVersion != netstrmsCURR_IF_VERSION) { /* check for current version, increment on each change */
441 ABORT_FINALIZE(RS_RET_INTERFACE_NOT_SUPPORTED);
442 }
443
444 /* ok, we have the right interface, so let's fill it
445 * Please note that we may also do some backwards-compatibility
446 * work here (if we can support an older interface version - that,
447 * of course, also affects the "if" above).
448 */
449 pIf->Construct = netstrmsConstruct;
450 pIf->ConstructFinalize = netstrmsConstructFinalize;
451 pIf->Destruct = netstrmsDestruct;
452 pIf->CreateStrm = CreateStrm;
453 pIf->SetSynBacklog = SetSynBacklog;
454 pIf->SetDrvrName = SetDrvrName;
455 pIf->SetDrvrMode = SetDrvrMode;
456 pIf->GetDrvrMode = GetDrvrMode;
457 pIf->SetDrvrAuthMode = SetDrvrAuthMode;
458 pIf->GetDrvrAuthMode = GetDrvrAuthMode;
459 pIf->SetDrvrPermitExpiredCerts = SetDrvrPermitExpiredCerts;
460 pIf->GetDrvrPermitExpiredCerts = GetDrvrPermitExpiredCerts;
461 pIf->SetDrvrGnutlsPriorityString = SetDrvrGnutlsPriorityString;
462 pIf->GetDrvrGnutlsPriorityString = GetDrvrGnutlsPriorityString;
463 pIf->SetDrvrPermPeers = SetDrvrPermPeers;
464 pIf->GetDrvrPermPeers = GetDrvrPermPeers;
465 pIf->SetDrvrCheckExtendedKeyUsage = SetDrvrCheckExtendedKeyUsage;
466 pIf->GetDrvrCheckExtendedKeyUsage = GetDrvrCheckExtendedKeyUsage;
467 pIf->SetDrvrPrioritizeSAN = SetDrvrPrioritizeSAN;
468 pIf->GetDrvrPrioritizeSAN = GetDrvrPrioritizeSAN;
469 pIf->SetDrvrTlsVerifyDepth = SetDrvrTlsVerifyDepth;
470 pIf->SetDrvrTlsRevocationCheck = SetDrvrTlsRevocationCheck;
471 pIf->GetDrvrTlsVerifyDepth = GetDrvrTlsVerifyDepth;
472 pIf->GetDrvrTlsRevocationCheck = GetDrvrTlsRevocationCheck;
473 pIf->SetDrvrRemoteSNI = SetDrvrRemoteSNI;
474 pIf->GetDrvrRemoteSNI = GetDrvrRemoteSNI;
475 pIf->GetDrvrTlsCAFile = GetDrvrTlsCAFile;
476 pIf->GetDrvrTlsCRLFile = GetDrvrTlsCRLFile;
477 pIf->GetDrvrTlsKeyFile = GetDrvrTlsKeyFile;
478 pIf->GetDrvrTlsCertFile = GetDrvrTlsCertFile;
479 pIf->SetDrvrTlsCAFile = SetDrvrTlsCAFile;
480 pIf->SetDrvrTlsCRLFile = SetDrvrTlsCRLFile;
481 pIf->SetDrvrTlsKeyFile = SetDrvrTlsKeyFile;
482 pIf->SetDrvrTlsCertFile = SetDrvrTlsCertFile;
483 finalize_it:
484 ENDobjQueryInterface(netstrms)
485
486
487 /* exit our class */
488 BEGINObjClassExit(netstrms, OBJ_IS_LOADABLE_MODULE) /* CHANGE class also in END MACRO! */
489 CODESTARTObjClassExit(netstrms);
490 /* release objects we no longer need */
491 objRelease(glbl, CORE_COMPONENT);
492 objRelease(netstrm, DONT_LOAD_LIB);
493 ENDObjClassExit(netstrms)
494
495
496 /* Initialize the netstrms class. Must be called as the very first method
497 * before anything else is called inside this class.
498 * rgerhards, 2008-02-19
499 */
500 BEGINAbstractObjClassInit(netstrms, 1, OBJ_IS_CORE_MODULE) /* class, version */
501 /* request objects we use */
502 CHKiRet(objUse(glbl, CORE_COMPONENT));
503
504 /* set our own handlers */
505 ENDObjClassInit(netstrms)
506
507
508 /* --------------- here now comes the plumbing that makes as a library module --------------- */
509
510
511 BEGINmodExit
512 CODESTARTmodExit;
513 netstrmsClassExit();
514 netstrmClassExit(); /* we use this object, so we must exit it after we are finished */
515 ENDmodExit
516
517
518 BEGINqueryEtryPt
519 CODESTARTqueryEtryPt;
520 CODEqueryEtryPt_STD_LIB_QUERIES;
521 ENDqueryEtryPt
522
523
524 BEGINmodInit()
525 CODESTARTmodInit;
526 *ipIFVersProvided = CURR_MOD_IF_VERSION; /* we only support the current interface specification */
527
528 /* Initialize all classes that are in our module - this includes ourselfs */
529 CHKiRet(netstrmClassInit(pModInfo));
530 CHKiRet(netstrmsClassInit(pModInfo));
531 ENDmodInit