Debian Codegraph @

runtime/netstrm.c

raw
1 /* netstrm.c
2 *
3 * This class implements a generic netstrmwork stream class. It supports
4 * sending and receiving data streams over a netstrmwork. The class abstracts
5 * the transport, though it is a safe assumption that TCP is being used.
6 * The class has a number of properties, among which are also ones to
7 * select privacy settings, eg by enabling TLS and/or GSSAPI. In the
8 * long run, this class shall provide all stream-oriented netstrmwork
9 * functionality inside rsyslog.
10 *
11 * It is a high-level class, which uses a number of helper objects
12 * to carry out its work (including, and most importantly, transport
13 * drivers).
14 *
15 * Note on processing:
16 * - Initiating a listener may be driver-specific, but in regard to TLS/non-TLS
17 * it actually is not. This is because TLS is negotiated after a connection
18 * has been established. So it is the "acceptConnReq" driver entry where TLS
19 * params need to be applied.
20 *
21 * Work on this module begun 2008-04-17 by Rainer Gerhards. This code
22 * borrows from librelp's tcp.c/.h code. librelp is dual licensed and
23 * Rainer Gerhards and Adiscon GmbH have agreed to permit using the code
24 * under the terms of the GNU Lesser General Public License.
25 *
26 * Copyright 2007-2020 Rainer Gerhards and Adiscon GmbH.
27 *
28 * This file is part of the rsyslog runtime library.
29 *
30 * The rsyslog runtime library is free software: you can redistribute it and/or modify
31 * it under the terms of the GNU Lesser General Public License as published by
32 * the Free Software Foundation, either version 3 of the License, or
33 * (at your option) any later version.
34 *
35 * The rsyslog runtime library is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU Lesser General Public License for more details.
39 *
40 * You should have received a copy of the GNU Lesser General Public License
41 * along with the rsyslog runtime library. If not, see <http://www.gnu.org/licenses/>.
42 *
43 * A copy of the GPL can be found in the file "COPYING" in this distribution.
44 * A copy of the LGPL can be found in the file "COPYING.LESSER" in this distribution.
45 */
46 #include "config.h"
47 #include <stdlib.h>
48 #include <assert.h>
49 #include <string.h>
50
51 #include "rsyslog.h"
52 #include "net.h"
53 #include "module-template.h"
54 #include "obj.h"
55 #include "errmsg.h"
56 #include "netstrms.h"
57 #include "netstrm.h"
58
59 /* static data */
60 DEFobjStaticHelpers;
61 DEFobjCurrIf(net) DEFobjCurrIf(netstrms)
62
63
64 /* Standard-Constructor */
65 BEGINobjConstruct(netstrm) /* be sure to specify the object type also in END macro! */
66 ENDobjConstruct
67 (netstrm)
68
69
70 /* destructor for the netstrm object */
71 BEGINobjDestruct(netstrm) /* be sure to specify the object type also in END and CODESTART macros! */
72 CODESTARTobjDestruct(netstrm);
73 if (pThis->pDrvrData != NULL) iRet = pThis->Drvr.Destruct(&pThis->pDrvrData);
74 ENDobjDestruct
75 (netstrm)
76
77
78 /* ConstructionFinalizer */
79 static rsRetVal netstrmConstructFinalize(netstrm_t *pThis) {
80 DEFiRet;
81 NULL_CHECK(pThis);
82 iRet = pThis->Drvr.Construct(&pThis->pDrvrData);
83 finalize_it:
84 RETiRet;
85 }
86
87 /* abort a connection. This is much like Destruct(), but tries
88 * to discard any unsent data. -- rgerhards, 2008-03-24
89 */
90 static rsRetVal AbortDestruct(netstrm_t **ppThis) {
91 DEFiRet;
92 NULL_CHECK(ppThis);
93 NULL_CHECK(*ppThis);
94
95 /* we do NOT exit on error, because that would make things worse */
96 (*ppThis)->Drvr.Abort((*ppThis)->pDrvrData);
97 iRet = netstrmDestruct(ppThis);
98
99
100 finalize_it:
101 RETiRet;
102 }
103
104
105 /* accept an incoming connection request
106 * The netstrm instance that had the incoming request must be provided. If
107 * the connection request succeeds, a new netstrm object is created and
108 * passed back to the caller. The caller is responsible for destructing it.
109 * pReq is the nsd_t obj that has the accept request.
110 * rgerhards, 2008-04-21
111 */
112 static rsRetVal AcceptConnReq(netstrm_t *pThis, netstrm_t **ppNew, char *const connInfo) {
113 nsd_t *pNewNsd = NULL;
114 DEFiRet;
115
116 NULL_CHECK(pThis);
117 assert(ppNew != NULL);
118
119 /* accept the new connection */
120 CHKiRet(pThis->Drvr.AcceptConnReq(pThis->pDrvrData, &pNewNsd, connInfo));
121 /* construct our object so that we can use it... */
122 CHKiRet(objUse(netstrms, DONT_LOAD_LIB)); /* use netstrms obj if not already done so */
123 CHKiRet(netstrms.CreateStrm(pThis->pNS, ppNew));
124 (*ppNew)->pDrvrData = pNewNsd;
125
126 finalize_it:
127 if (iRet != RS_RET_OK) {
128 /* the close may be redundant, but that doesn't hurt... */
129 if (pNewNsd != NULL) pThis->Drvr.Destruct(&pNewNsd);
130 }
131
132 RETiRet;
133 }
134
135
136 /* make the netstrm listen to specified port and IP.
137 * pLstnIP points to the port to listen to (NULL means "all"),
138 * iMaxSess has the maximum number of sessions permitted (this ist just a hint).
139 * pLstnPort must point to a port name or number. NULL is NOT permitted.
140 * rgerhards, 2008-04-22
141 */
142 static rsRetVal ATTR_NONNULL(1, 3, 5) LstnInit(netstrms_t *pNS,
143 void *pUsr,
144 rsRetVal (*fAddLstn)(void *, netstrm_t *),
145 const int iSessMax,
146 const tcpLstnParams_t *const cnf_params) {
147 DEFiRet;
148 const char *ns = cnf_params->pszNetworkNamespace;
149 int netns_fd = -1;
150
151 ISOBJ_TYPE_assert(pNS, netstrms);
152 assert(fAddLstn != NULL);
153 assert(cnf_params->pszPort != NULL);
154
155 #ifdef HAVE_SETNS
156 if (ns) {
157 CHKiRet(net.netns_save(&netns_fd));
158 CHKiRet(net.netns_switch(ns));
159 }
160 #endif // ndef HAVE_SETNS
161
162 CHKiRet(pNS->Drvr.LstnInit(pNS, pUsr, fAddLstn, iSessMax, cnf_params));
163
164 finalize_it:
165 #ifdef HAVE_SETNS
166 if (ns) {
167 // netns_restore will log a message on failure
168 // but there's really nothing we can do about it
169 (void)net.netns_restore(&netns_fd);
170 }
171 #endif // ndef HAVE_SETNS
172
173 RETiRet;
174 }
175
176
177 /* receive data from a tcp socket
178 * The lenBuf parameter must contain the max buffer size on entry and contains
179 * the number of octets read (or -1 in case of error) on exit. This function
180 * never blocks, not even when called on a blocking socket. That is important
181 * for client sockets, which are set to block during send, but should not
182 * block when trying to read data. If *pLenBuf is -1, an error occurred and
183 * oserr holds the exact error cause.
184 * rgerhards, 2008-03-17
185 */
186 static rsRetVal Rcv(netstrm_t *pThis, uchar *pBuf, ssize_t *pLenBuf, int *const oserr, unsigned *nextIODirection) {
187 DEFiRet;
188 NULL_CHECK(pThis);
189 iRet = pThis->Drvr.Rcv(pThis->pDrvrData, pBuf, pLenBuf, oserr, nextIODirection);
190
191 finalize_it:
192 RETiRet;
193 }
194
195 /* here follows a number of methods that shuffle authentication settings down
196 * to the drivers. Drivers not supporting these settings may return an error
197 * state.
198 * -------------------------------------------------------------------------- */
199
200 /* set the driver mode
201 * rgerhards, 2008-04-28
202 */
203 static rsRetVal SetDrvrMode(netstrm_t *pThis, int iMode) {
204 DEFiRet;
205 NULL_CHECK(pThis);
206 iRet = pThis->Drvr.SetMode(pThis->pDrvrData, iMode);
207
208 finalize_it:
209 RETiRet;
210 }
211
212
213 /* set the driver authentication mode -- rgerhards, 2008-05-16
214 */
215 static rsRetVal SetDrvrAuthMode(netstrm_t *pThis, uchar *mode) {
216 DEFiRet;
217 NULL_CHECK(pThis);
218 iRet = pThis->Drvr.SetAuthMode(pThis->pDrvrData, mode);
219
220 finalize_it:
221 RETiRet;
222 }
223
224
225 /* set the driver permitexpiredcerts mode -- alorbach, 2018-12-20
226 */
227 static rsRetVal SetDrvrPermitExpiredCerts(netstrm_t *pThis, uchar *mode) {
228 DEFiRet;
229 NULL_CHECK(pThis);
230 iRet = pThis->Drvr.SetPermitExpiredCerts(pThis->pDrvrData, mode);
231
232 finalize_it:
233 RETiRet;
234 }
235
236 /* set the driver's permitted peers -- rgerhards, 2008-05-19 */
237 static rsRetVal SetDrvrPermPeers(netstrm_t *pThis, permittedPeers_t *pPermPeers) {
238 DEFiRet;
239 NULL_CHECK(pThis);
240 iRet = pThis->Drvr.SetPermPeers(pThis->pDrvrData, pPermPeers);
241
242 finalize_it:
243 RETiRet;
244 }
245
246 /* Mandate also verification of Extended key usage / purpose field */
247 static rsRetVal SetDrvrCheckExtendedKeyUsage(netstrm_t *pThis, int ChkExtendedKeyUsage) {
248 DEFiRet;
249 NULL_CHECK(pThis);
250 iRet = pThis->Drvr.SetCheckExtendedKeyUsage(pThis->pDrvrData, ChkExtendedKeyUsage);
251
252 finalize_it:
253 RETiRet;
254 }
255
256 /* Mandate stricter name checking per RFC 6125 - ignoce CN if any SAN present */
257 static rsRetVal SetDrvrPrioritizeSAN(netstrm_t *pThis, int prioritizeSan) {
258 DEFiRet;
259 NULL_CHECK(pThis);
260 iRet = pThis->Drvr.SetPrioritizeSAN(pThis->pDrvrData, prioritizeSan);
261
262 finalize_it:
263 RETiRet;
264 }
265
266 /* tls verify depth */
267 static rsRetVal SetDrvrTlsRevocationCheck(netstrm_t *pThis, int enabled) {
268 DEFiRet;
269 iRet = pThis->Drvr.SetTlsRevocationCheck(pThis->pDrvrData, enabled);
270 RETiRet;
271 }
272
273 static rsRetVal SetDrvrTlsVerifyDepth(netstrm_t *pThis, int verifyDepth) {
274 DEFiRet;
275 NULL_CHECK(pThis);
276 iRet = pThis->Drvr.SetTlsVerifyDepth(pThis->pDrvrData, verifyDepth);
277
278 finalize_it:
279 RETiRet;
280 }
281
282 static rsRetVal SetDrvrTlsCAFile(netstrm_t *const pThis, const uchar *const file) {
283 DEFiRet;
284 NULL_CHECK(pThis);
285 iRet = pThis->Drvr.SetTlsCAFile(pThis->pDrvrData, file);
286
287 finalize_it:
288 RETiRet;
289 }
290
291 static rsRetVal SetDrvrTlsCRLFile(netstrm_t *const pThis, const uchar *const file) {
292 DEFiRet;
293 NULL_CHECK(pThis);
294 iRet = pThis->Drvr.SetTlsCRLFile(pThis->pDrvrData, file);
295
296 finalize_it:
297 RETiRet;
298 }
299
300 static rsRetVal SetDrvrTlsKeyFile(netstrm_t *const pThis, const uchar *const file) {
301 DEFiRet;
302 NULL_CHECK(pThis);
303 iRet = pThis->Drvr.SetTlsKeyFile(pThis->pDrvrData, file);
304
305 finalize_it:
306 RETiRet;
307 }
308
309 static rsRetVal SetDrvrTlsCertFile(netstrm_t *const pThis, const uchar *const file) {
310 DEFiRet;
311 NULL_CHECK(pThis);
312 iRet = pThis->Drvr.SetTlsCertFile(pThis->pDrvrData, file);
313
314 finalize_it:
315 RETiRet;
316 }
317
318 /* Set remote host's TLS SNI */
319 static rsRetVal SetDrvrRemoteSNI(netstrm_t *pThis, uchar *remoteSNI) {
320 DEFiRet;
321 NULL_CHECK(pThis);
322 iRet = pThis->Drvr.SetRemoteSNI(pThis->pDrvrData, remoteSNI);
323
324 finalize_it:
325 RETiRet;
326 }
327
328 /* End of methods to shuffle autentication settings to the driver.
329 * -------------------------------------------------------------------------- */
330
331
332 /* send a buffer. On entry, pLenBuf contains the number of octets to
333 * write. On exit, it contains the number of octets actually written.
334 * If this number is lower than on entry, only a partial buffer has
335 * been written.
336 * rgerhards, 2008-03-19
337 */
338 static rsRetVal Send(netstrm_t *pThis, uchar *pBuf, ssize_t *pLenBuf) {
339 DEFiRet;
340 NULL_CHECK(pThis);
341 iRet = pThis->Drvr.Send(pThis->pDrvrData, pBuf, pLenBuf);
342
343 finalize_it:
344 RETiRet;
345 }
346
347 /* Enable Keep-Alive handling for those drivers that support it.
348 * rgerhards, 2009-06-02
349 */
350 static rsRetVal EnableKeepAlive(netstrm_t *pThis) {
351 DEFiRet;
352 NULL_CHECK(pThis);
353 iRet = pThis->Drvr.EnableKeepAlive(pThis->pDrvrData);
354
355 finalize_it:
356 RETiRet;
357 }
358
359 /* Keep-Alive options
360 */
361 static rsRetVal SetKeepAliveProbes(netstrm_t *pThis, int keepAliveProbes) {
362 DEFiRet;
363 NULL_CHECK(pThis);
364 iRet = pThis->Drvr.SetKeepAliveProbes(pThis->pDrvrData, keepAliveProbes);
365
366 finalize_it:
367 RETiRet;
368 }
369
370 /* Keep-Alive options
371 */
372 static rsRetVal SetKeepAliveTime(netstrm_t *pThis, int keepAliveTime) {
373 DEFiRet;
374 NULL_CHECK(pThis);
375 iRet = pThis->Drvr.SetKeepAliveTime(pThis->pDrvrData, keepAliveTime);
376
377 finalize_it:
378 RETiRet;
379 }
380
381 /* Keep-Alive options
382 */
383 static rsRetVal SetKeepAliveIntvl(netstrm_t *pThis, int keepAliveIntvl) {
384 DEFiRet;
385 NULL_CHECK(pThis);
386 iRet = pThis->Drvr.SetKeepAliveIntvl(pThis->pDrvrData, keepAliveIntvl);
387
388 finalize_it:
389 RETiRet;
390 }
391
392 /* gnutls priority string */
393 static rsRetVal SetGnutlsPriorityString(netstrm_t *pThis, uchar *gnutlsPriorityString) {
394 DEFiRet;
395 NULL_CHECK(pThis);
396 iRet = pThis->Drvr.SetGnutlsPriorityString(pThis->pDrvrData, gnutlsPriorityString);
397
398 finalize_it:
399 RETiRet;
400 }
401
402 /* check connection - slim wrapper for NSD driver function */
403 static rsRetVal CheckConnection(netstrm_t *pThis) {
404 DEFiRet;
405 NULL_CHECK(pThis);
406 iRet = pThis->Drvr.CheckConnection(pThis->pDrvrData);
407 finalize_it:
408 RETiRet;
409 }
410
411
412 /* get remote hname - slim wrapper for NSD driver function */
413 static rsRetVal GetRemoteHName(netstrm_t *pThis, uchar **ppsz) {
414 DEFiRet;
415 NULL_CHECK(pThis);
416 iRet = pThis->Drvr.GetRemoteHName(pThis->pDrvrData, ppsz);
417
418 finalize_it:
419 RETiRet;
420 }
421
422
423 /* get remote IP - slim wrapper for NSD driver function */
424 static rsRetVal GetRemoteIP(netstrm_t *pThis, prop_t **ip) {
425 DEFiRet;
426 NULL_CHECK(pThis);
427 iRet = pThis->Drvr.GetRemoteIP(pThis->pDrvrData, ip);
428
429 finalize_it:
430 RETiRet;
431 }
432
433
434 /* get remote addr - slim wrapper for NSD driver function */
435 static rsRetVal GetRemAddr(netstrm_t *pThis, struct sockaddr_storage **ppAddr) {
436 DEFiRet;
437 NULL_CHECK(pThis);
438 iRet = pThis->Drvr.GetRemAddr(pThis->pDrvrData, ppAddr);
439
440 finalize_it:
441 RETiRet;
442 }
443
444
445 /* open a connection to a remote host (server).
446 * rgerhards, 2008-03-19
447 */
448 static rsRetVal Connect(netstrm_t *pThis, int family, uchar *port, uchar *host, char *device) {
449 DEFiRet;
450 NULL_CHECK(pThis);
451 assert(port != NULL);
452 assert(host != NULL);
453 iRet = pThis->Drvr.Connect(pThis->pDrvrData, family, port, host, device);
454
455 finalize_it:
456 RETiRet;
457 }
458
459
460 /* Provide access to the underlying OS socket. This is dirty
461 * and scheduled to be removed. Does not work with all nsd drivers.
462 * See comment in netstrm interface for details.
463 * rgerhards, 2008-05-05
464 */
465 static rsRetVal GetSock(netstrm_t *pThis, int *pSock) {
466 DEFiRet;
467 NULL_CHECK(pThis);
468 NULL_CHECK(pSock);
469 iRet = pThis->Drvr.GetSock(pThis->pDrvrData, pSock);
470
471 finalize_it:
472 RETiRet;
473 }
474
475
476 /* queryInterface function
477 */
478 BEGINobjQueryInterface(netstrm)
479 CODESTARTobjQueryInterface(netstrm);
480 if (pIf->ifVersion != netstrmCURR_IF_VERSION) { /* check for current version, increment on each change */
481 ABORT_FINALIZE(RS_RET_INTERFACE_NOT_SUPPORTED);
482 }
483
484 /* ok, we have the right interface, so let's fill it
485 * Please note that we may also do some backwards-compatibility
486 * work here (if we can support an older interface version - that,
487 * of course, also affects the "if" above).
488 */
489 pIf->Construct = netstrmConstruct;
490 pIf->ConstructFinalize = netstrmConstructFinalize;
491 pIf->Destruct = netstrmDestruct;
492 pIf->AbortDestruct = AbortDestruct;
493 pIf->Rcv = Rcv;
494 pIf->Send = Send;
495 pIf->Connect = Connect;
496 pIf->LstnInit = LstnInit;
497 pIf->AcceptConnReq = AcceptConnReq;
498 pIf->GetRemoteHName = GetRemoteHName;
499 pIf->GetRemoteIP = GetRemoteIP;
500 pIf->GetRemAddr = GetRemAddr;
501 pIf->SetDrvrMode = SetDrvrMode;
502 pIf->SetDrvrAuthMode = SetDrvrAuthMode;
503 pIf->SetDrvrPermitExpiredCerts = SetDrvrPermitExpiredCerts;
504 pIf->SetDrvrPermPeers = SetDrvrPermPeers;
505 pIf->CheckConnection = CheckConnection;
506 pIf->GetSock = GetSock;
507 pIf->EnableKeepAlive = EnableKeepAlive;
508 pIf->SetKeepAliveProbes = SetKeepAliveProbes;
509 pIf->SetKeepAliveTime = SetKeepAliveTime;
510 pIf->SetKeepAliveIntvl = SetKeepAliveIntvl;
511 pIf->SetGnutlsPriorityString = SetGnutlsPriorityString;
512 pIf->SetDrvrCheckExtendedKeyUsage = SetDrvrCheckExtendedKeyUsage;
513 pIf->SetDrvrPrioritizeSAN = SetDrvrPrioritizeSAN;
514 pIf->SetDrvrTlsVerifyDepth = SetDrvrTlsVerifyDepth;
515 pIf->SetDrvrTlsRevocationCheck = SetDrvrTlsRevocationCheck;
516 pIf->SetDrvrTlsCAFile = SetDrvrTlsCAFile;
517 pIf->SetDrvrTlsCRLFile = SetDrvrTlsCRLFile;
518 pIf->SetDrvrTlsKeyFile = SetDrvrTlsKeyFile;
519 pIf->SetDrvrTlsCertFile = SetDrvrTlsCertFile;
520 pIf->SetDrvrRemoteSNI = SetDrvrRemoteSNI;
521 finalize_it:
522 ENDobjQueryInterface(netstrm)
523
524
525 /* exit our class
526 */
527 BEGINObjClassExit(netstrm, OBJ_IS_LOADABLE_MODULE) /* CHANGE class also in END MACRO! */
528 CODESTARTObjClassExit(netstrm);
529 /* release objects we no longer need */
530 objRelease(netstrms, DONT_LOAD_LIB);
531 objRelease(net, LM_NET_FILENAME);
532 ENDObjClassExit(netstrm)
533
534
535 /* Initialize the netstrm class. Must be called as the very first method
536 * before anything else is called inside this class.
537 * rgerhards, 2008-02-19
538 */
539 BEGINAbstractObjClassInit(netstrm, 1, OBJ_IS_CORE_MODULE) /* class, version */
540 /* request objects we use */
541 CHKiRet(objUse(net, LM_NET_FILENAME));
542
543 /* set our own handlers */
544 ENDObjClassInit(netstrm)