| 1 |
/* nsd_ptcp.c |
| 2 |
* |
| 3 |
* An implementation of the nsd interface for plain tcp sockets. |
| 4 |
* |
| 5 |
* Copyright 2007-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 |
|
| 27 |
#include <stdio.h> |
| 28 |
#include <stdarg.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <assert.h> |
| 31 |
#include <errno.h> |
| 32 |
#include <string.h> |
| 33 |
#include <signal.h> |
| 34 |
#include <ctype.h> |
| 35 |
#include <netdb.h> |
| 36 |
#include <fnmatch.h> |
| 37 |
#include <fcntl.h> |
| 38 |
#include <unistd.h> |
| 39 |
#include <netinet/tcp.h> |
| 40 |
#include <sys/time.h> |
| 41 |
|
| 42 |
#include "rsyslog.h" |
| 43 |
#include "syslogd-types.h" |
| 44 |
#include "module-template.h" |
| 45 |
#include "parse.h" |
| 46 |
#include "srUtils.h" |
| 47 |
#include "obj.h" |
| 48 |
#include "errmsg.h" |
| 49 |
#include "net.h" |
| 50 |
#include "netstrms.h" |
| 51 |
#include "netstrm.h" |
| 52 |
#include "nsd_ptcp.h" |
| 53 |
#include "prop.h" |
| 54 |
#include "dnscache.h" |
| 55 |
#include "rsconf.h" |
| 56 |
|
| 57 |
MODULE_TYPE_LIB |
| 58 |
MODULE_TYPE_NOKEEP; |
| 59 |
|
| 60 |
/* static data */ |
| 61 |
DEFobjStaticHelpers; |
| 62 |
DEFobjCurrIf(glbl) DEFobjCurrIf(net) DEFobjCurrIf(netstrms) DEFobjCurrIf(netstrm) DEFobjCurrIf(prop) |
| 63 |
|
| 64 |
|
| 65 |
/* a few deinit helpers */ |
| 66 |
|
| 67 |
/* close socket if open (may always be called) */ |
| 68 |
static void sockClose(int *pSock) { |
| 69 |
if (*pSock >= 0) { |
| 70 |
close(*pSock); |
| 71 |
*pSock = -1; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/* Standard-Constructor |
| 76 |
*/ |
| 77 |
BEGINobjConstruct(nsd_ptcp) /* be sure to specify the object type also in END macro! */ |
| 78 |
pThis->sock = -1; |
| 79 |
ENDobjConstruct(nsd_ptcp) |
| 80 |
|
| 81 |
|
| 82 |
/* destructor for the nsd_ptcp object */ |
| 83 |
BEGINobjDestruct(nsd_ptcp) /* be sure to specify the object type also in END and CODESTART macros! */ |
| 84 |
CODESTARTobjDestruct(nsd_ptcp); |
| 85 |
sockClose(&pThis->sock); |
| 86 |
if (pThis->remoteIP != NULL) prop.Destruct(&pThis->remoteIP); |
| 87 |
free(pThis->pRemHostName); |
| 88 |
ENDobjDestruct(nsd_ptcp) |
| 89 |
|
| 90 |
|
| 91 |
/* Provide access to the sockaddr_storage of the remote peer. This |
| 92 |
* is needed by the legacy ACL system. --- gerhards, 2008-12-01 |
| 93 |
*/ |
| 94 |
static rsRetVal GetRemAddr(nsd_t *pNsd, struct sockaddr_storage **ppAddr) { |
| 95 |
nsd_ptcp_t *pThis = (nsd_ptcp_t *)pNsd; |
| 96 |
DEFiRet; |
| 97 |
|
| 98 |
ISOBJ_TYPE_assert((pThis), nsd_ptcp); |
| 99 |
assert(ppAddr != NULL); |
| 100 |
|
| 101 |
*ppAddr = &(pThis->remAddr); |
| 102 |
|
| 103 |
RETiRet; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
/* Provide access to the underlying OS socket. This is primarily |
| 108 |
* useful for other drivers (like nsd_gtls) who utilize ourselfs |
| 109 |
* for some of their functionality. -- rgerhards, 2008-04-18 |
| 110 |
*/ |
| 111 |
static rsRetVal GetSock(nsd_t *pNsd, int *pSock) { |
| 112 |
nsd_ptcp_t *pThis = (nsd_ptcp_t *)pNsd; |
| 113 |
DEFiRet; |
| 114 |
|
| 115 |
NULL_CHECK(pSock); |
| 116 |
|
| 117 |
*pSock = pThis->sock; |
| 118 |
|
| 119 |
finalize_it: |
| 120 |
RETiRet; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
/* Set the driver mode. We support no different modes, but allow mode |
| 125 |
* 0 to be set to be compatible with config file defaults and the other |
| 126 |
* drivers. |
| 127 |
* rgerhards, 2008-04-28 |
| 128 |
*/ |
| 129 |
static rsRetVal SetMode(nsd_t __attribute__((unused)) * pNsd, int mode) { |
| 130 |
DEFiRet; |
| 131 |
if (mode != 0) { |
| 132 |
LogError(0, RS_RET_INVALID_DRVR_MODE, |
| 133 |
"error: driver mode %d not supported by " |
| 134 |
"ptcp netstream driver", |
| 135 |
mode); |
| 136 |
ABORT_FINALIZE(RS_RET_INVALID_DRVR_MODE); |
| 137 |
} |
| 138 |
finalize_it: |
| 139 |
RETiRet; |
| 140 |
} |
| 141 |
|
| 142 |
/* Set the driver cert extended key usage check setting, not supported in ptcp. |
| 143 |
* jvymazal, 2019-08-16 |
| 144 |
*/ |
| 145 |
static rsRetVal SetCheckExtendedKeyUsage(nsd_t __attribute__((unused)) * pNsd, int ChkExtendedKeyUsage) { |
| 146 |
DEFiRet; |
| 147 |
if (ChkExtendedKeyUsage != 0) { |
| 148 |
LogError(0, RS_RET_VALUE_NOT_SUPPORTED, |
| 149 |
"error: driver ChkExtendedKeyUsage %d " |
| 150 |
"not supported by ptcp netstream driver", |
| 151 |
ChkExtendedKeyUsage); |
| 152 |
ABORT_FINALIZE(RS_RET_VALUE_NOT_SUPPORTED); |
| 153 |
} |
| 154 |
finalize_it: |
| 155 |
RETiRet; |
| 156 |
} |
| 157 |
|
| 158 |
/* Set the driver name checking strictness, not supported in ptcp. |
| 159 |
* jvymazal, 2019-08-16 |
| 160 |
*/ |
| 161 |
static rsRetVal SetPrioritizeSAN(nsd_t __attribute__((unused)) * pNsd, int prioritizeSan) { |
| 162 |
DEFiRet; |
| 163 |
if (prioritizeSan != 0) { |
| 164 |
LogError(0, RS_RET_VALUE_NOT_SUPPORTED, |
| 165 |
"error: driver prioritizeSan %d " |
| 166 |
"not supported by ptcp netstream driver", |
| 167 |
prioritizeSan); |
| 168 |
ABORT_FINALIZE(RS_RET_VALUE_NOT_SUPPORTED); |
| 169 |
} |
| 170 |
finalize_it: |
| 171 |
RETiRet; |
| 172 |
} |
| 173 |
|
| 174 |
/* Set the tls verify depth, not supported in ptcp. |
| 175 |
* alorbach, 2019-12-20 |
| 176 |
*/ |
| 177 |
static rsRetVal SetTlsVerifyDepth(nsd_t __attribute__((unused)) * pNsd, int verifyDepth) { |
| 178 |
nsd_ptcp_t __attribute__((unused)) *pThis = (nsd_ptcp_t *)pNsd; |
| 179 |
DEFiRet; |
| 180 |
ISOBJ_TYPE_assert((pThis), nsd_ptcp); |
| 181 |
if (verifyDepth == 0) { |
| 182 |
FINALIZE; |
| 183 |
} |
| 184 |
finalize_it: |
| 185 |
RETiRet; |
| 186 |
} |
| 187 |
|
| 188 |
/* Set the authentication mode. For us, the following is supported: |
| 189 |
* anon - no certificate checks whatsoever (discouraged, but supported) |
| 190 |
* mode == NULL is valid and defaults to anon |
| 191 |
* Actually, we do not even record the mode right now, because we can |
| 192 |
* always work in anon mode, only. So there is no point in recording |
| 193 |
* something if that's the only choice. What the function does is |
| 194 |
* return an error if something is requested that we can not support. |
| 195 |
* rgerhards, 2008-05-17 |
| 196 |
*/ |
| 197 |
static rsRetVal SetAuthMode(nsd_t __attribute__((unused)) * pNsd, uchar *mode) { |
| 198 |
DEFiRet; |
| 199 |
if (mode != NULL && strcasecmp((char *)mode, "anon")) { |
| 200 |
LogError(0, RS_RET_VALUE_NOT_SUPPORTED, |
| 201 |
"error: authentication mode '%s' not supported by " |
| 202 |
"ptcp netstream driver", |
| 203 |
mode); |
| 204 |
ABORT_FINALIZE(RS_RET_VALUE_NOT_SUPPORTED); |
| 205 |
} |
| 206 |
|
| 207 |
finalize_it: |
| 208 |
RETiRet; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
/* Set the PermitExpiredCerts mode. not supported in ptcp |
| 213 |
* alorbach, 2018-12-20 |
| 214 |
*/ |
| 215 |
static rsRetVal SetPermitExpiredCerts(nsd_t __attribute__((unused)) * pNsd, uchar *mode) { |
| 216 |
DEFiRet; |
| 217 |
if (mode != NULL) { |
| 218 |
LogError(0, RS_RET_VALUE_NOT_SUPPORTED, |
| 219 |
"error: permitexpiredcerts setting not supported by " |
| 220 |
"ptcp netstream driver"); |
| 221 |
ABORT_FINALIZE(RS_RET_VALUE_NOT_SUPPORTED); |
| 222 |
} |
| 223 |
|
| 224 |
finalize_it: |
| 225 |
RETiRet; |
| 226 |
} |
| 227 |
|
| 228 |
static rsRetVal SetTlsCAFile(nsd_t __attribute__((unused)) * pNsd, const uchar *const pszFile) { |
| 229 |
DEFiRet; |
| 230 |
if (pszFile != NULL) { |
| 231 |
LogError(0, RS_RET_VALUE_NOT_SUPPORTED, |
| 232 |
"error: CA File setting not supported by " |
| 233 |
"ptcp netstream driver - value %s", |
| 234 |
pszFile); |
| 235 |
ABORT_FINALIZE(RS_RET_VALUE_NOT_SUPPORTED); |
| 236 |
} |
| 237 |
finalize_it: |
| 238 |
RETiRet; |
| 239 |
} |
| 240 |
|
| 241 |
static rsRetVal SetTlsCRLFile(nsd_t __attribute__((unused)) * pNsd, const uchar *const pszFile) { |
| 242 |
DEFiRet; |
| 243 |
if (pszFile != NULL) { |
| 244 |
LogError(0, RS_RET_VALUE_NOT_SUPPORTED, |
| 245 |
"error: CRL File setting not supported by " |
| 246 |
"ptcp netstream driver - value %s", |
| 247 |
pszFile); |
| 248 |
ABORT_FINALIZE(RS_RET_VALUE_NOT_SUPPORTED); |
| 249 |
} |
| 250 |
finalize_it: |
| 251 |
RETiRet; |
| 252 |
} |
| 253 |
|
| 254 |
static rsRetVal SetTlsKeyFile(nsd_t __attribute__((unused)) * pNsd, const uchar *const pszFile) { |
| 255 |
DEFiRet; |
| 256 |
if (pszFile != NULL) { |
| 257 |
LogError(0, RS_RET_VALUE_NOT_SUPPORTED, |
| 258 |
"error: TLS Key File setting not supported by " |
| 259 |
"ptcp netstream driver"); |
| 260 |
ABORT_FINALIZE(RS_RET_VALUE_NOT_SUPPORTED); |
| 261 |
} |
| 262 |
finalize_it: |
| 263 |
RETiRet; |
| 264 |
} |
| 265 |
|
| 266 |
static rsRetVal SetTlsCertFile(nsd_t __attribute__((unused)) * pNsd, const uchar *const pszFile) { |
| 267 |
DEFiRet; |
| 268 |
if (pszFile != NULL) { |
| 269 |
LogError(0, RS_RET_VALUE_NOT_SUPPORTED, |
| 270 |
"error: TLS Cert File setting not supported by " |
| 271 |
"ptcp netstream driver"); |
| 272 |
ABORT_FINALIZE(RS_RET_VALUE_NOT_SUPPORTED); |
| 273 |
} |
| 274 |
|
| 275 |
finalize_it: |
| 276 |
RETiRet; |
| 277 |
} |
| 278 |
|
| 279 |
static rsRetVal SetRemoteSNI(nsd_t __attribute__((unused)) * pNsd, uchar *pszRemoteSNI) { |
| 280 |
DEFiRet; |
| 281 |
if (pszRemoteSNI != NULL) { |
| 282 |
LogError(0, RS_RET_VALUE_NOT_SUPPORTED, |
| 283 |
"error: remote SNI setting not supported by " |
| 284 |
"ptcp netstream driver"); |
| 285 |
ABORT_FINALIZE(RS_RET_VALUE_NOT_SUPPORTED); |
| 286 |
} |
| 287 |
|
| 288 |
finalize_it: |
| 289 |
RETiRet; |
| 290 |
} |
| 291 |
|
| 292 |
static rsRetVal SetTlsRevocationCheck(nsd_t __attribute__((unused)) * pNsd, int enabled) { |
| 293 |
DEFiRet; |
| 294 |
if (enabled) { |
| 295 |
LogError(0, RS_RET_VALUE_NOT_SUPPORTED, |
| 296 |
"error: TLS revocation checking not supported by " |
| 297 |
"ptcp netstream driver (plain TCP has no TLS)"); |
| 298 |
ABORT_FINALIZE(RS_RET_VALUE_NOT_SUPPORTED); |
| 299 |
} |
| 300 |
|
| 301 |
finalize_it: |
| 302 |
RETiRet; |
| 303 |
} |
| 304 |
|
| 305 |
/* Set priorityString |
| 306 |
* PascalWithopf 2017-08-18 */ |
| 307 |
static rsRetVal SetGnutlsPriorityString(nsd_t __attribute__((unused)) * pNsd, uchar *iVal) { |
| 308 |
DEFiRet; |
| 309 |
if (iVal != NULL) { |
| 310 |
LogError(0, RS_RET_VALUE_NOT_SUPPORTED, |
| 311 |
"error: " |
| 312 |
"gnutlsPriorityString '%s' not supported by ptcp netstream " |
| 313 |
"driver", |
| 314 |
iVal); |
| 315 |
ABORT_FINALIZE(RS_RET_VALUE_NOT_SUPPORTED); |
| 316 |
} |
| 317 |
finalize_it: |
| 318 |
RETiRet; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
/* Set the permitted peers. This is a dummy, always returning an |
| 323 |
* error because we do not support fingerprint authentication. |
| 324 |
* rgerhards, 2008-05-17 |
| 325 |
*/ |
| 326 |
static rsRetVal SetPermPeers(nsd_t __attribute__((unused)) * pNsd, |
| 327 |
permittedPeers_t __attribute__((unused)) * pPermPeers) { |
| 328 |
DEFiRet; |
| 329 |
|
| 330 |
if (pPermPeers != NULL) { |
| 331 |
LogError(0, RS_RET_VALUE_NOT_IN_THIS_MODE, "authentication not supported by ptcp netstream driver"); |
| 332 |
ABORT_FINALIZE(RS_RET_VALUE_NOT_IN_THIS_MODE); |
| 333 |
} |
| 334 |
|
| 335 |
finalize_it: |
| 336 |
RETiRet; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
/* Provide access to the underlying OS socket. This is primarily |
| 341 |
* useful for other drivers (like nsd_gtls) who utilize ourselfs |
| 342 |
* for some of their functionality. |
| 343 |
* This function sets the socket -- rgerhards, 2008-04-25 |
| 344 |
*/ |
| 345 |
static rsRetVal SetSock(nsd_t *pNsd, int sock) { |
| 346 |
nsd_ptcp_t *pThis = (nsd_ptcp_t *)pNsd; |
| 347 |
DEFiRet; |
| 348 |
|
| 349 |
ISOBJ_TYPE_assert((pThis), nsd_ptcp); |
| 350 |
assert(sock >= 0); |
| 351 |
|
| 352 |
pThis->sock = sock; |
| 353 |
|
| 354 |
RETiRet; |
| 355 |
} |
| 356 |
|
| 357 |
/* Keep Alive Options |
| 358 |
*/ |
| 359 |
static rsRetVal SetKeepAliveIntvl(nsd_t *pNsd, int keepAliveIntvl) { |
| 360 |
nsd_ptcp_t *pThis = (nsd_ptcp_t *)pNsd; |
| 361 |
DEFiRet; |
| 362 |
|
| 363 |
ISOBJ_TYPE_assert((pThis), nsd_ptcp); |
| 364 |
|
| 365 |
pThis->iKeepAliveIntvl = keepAliveIntvl; |
| 366 |
|
| 367 |
RETiRet; |
| 368 |
} |
| 369 |
|
| 370 |
/* Keep Alive Options |
| 371 |
*/ |
| 372 |
static rsRetVal SetKeepAliveProbes(nsd_t *pNsd, int keepAliveProbes) { |
| 373 |
nsd_ptcp_t *pThis = (nsd_ptcp_t *)pNsd; |
| 374 |
DEFiRet; |
| 375 |
|
| 376 |
ISOBJ_TYPE_assert((pThis), nsd_ptcp); |
| 377 |
|
| 378 |
pThis->iKeepAliveProbes = keepAliveProbes; |
| 379 |
|
| 380 |
RETiRet; |
| 381 |
} |
| 382 |
|
| 383 |
/* Keep Alive Options |
| 384 |
*/ |
| 385 |
static rsRetVal SetKeepAliveTime(nsd_t *pNsd, int keepAliveTime) { |
| 386 |
nsd_ptcp_t *pThis = (nsd_ptcp_t *)pNsd; |
| 387 |
DEFiRet; |
| 388 |
|
| 389 |
ISOBJ_TYPE_assert((pThis), nsd_ptcp); |
| 390 |
|
| 391 |
pThis->iKeepAliveTime = keepAliveTime; |
| 392 |
|
| 393 |
RETiRet; |
| 394 |
} |
| 395 |
|
| 396 |
/* abort a connection. This is meant to be called immediately |
| 397 |
* before the Destruct call. -- rgerhards, 2008-03-24 |
| 398 |
*/ |
| 399 |
static rsRetVal Abort(nsd_t *pNsd) { |
| 400 |
struct linger ling; |
| 401 |
nsd_ptcp_t *pThis = (nsd_ptcp_t *)pNsd; |
| 402 |
|
| 403 |
DEFiRet; |
| 404 |
ISOBJ_TYPE_assert((pThis), nsd_ptcp); |
| 405 |
|
| 406 |
if ((pThis)->sock != -1) { |
| 407 |
ling.l_onoff = 1; |
| 408 |
ling.l_linger = 0; |
| 409 |
if (setsockopt((pThis)->sock, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling)) < 0) { |
| 410 |
dbgprintf("could not set SO_LINGER, errno %d\n", errno); |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
RETiRet; |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
/* Set pRemHost based on the address provided. This is to be called upon accept()ing |
| 419 |
* a connection request. It must be provided by the socket we received the |
| 420 |
* message on as well as a NI_MAXHOST size large character buffer for the FQDN. |
| 421 |
* Please see http://www.hmug.org/man/3/getnameinfo.php (under Caveats) |
| 422 |
* for some explanation of the code found below. If we detect a malicious |
| 423 |
* hostname, we return RS_RET_MALICIOUS_HNAME and let the caller decide |
| 424 |
* on how to deal with that. |
| 425 |
* rgerhards, 2008-03-31 |
| 426 |
*/ |
| 427 |
static rsRetVal FillRemHost(nsd_ptcp_t *pThis, struct sockaddr_storage *pAddr) { |
| 428 |
prop_t *fqdn; |
| 429 |
|
| 430 |
DEFiRet; |
| 431 |
ISOBJ_TYPE_assert(pThis, nsd_ptcp); |
| 432 |
assert(pAddr != NULL); |
| 433 |
|
| 434 |
CHKiRet(dnscacheLookup(pAddr, &fqdn, NULL, NULL, &pThis->remoteIP)); |
| 435 |
|
| 436 |
/* We now have the names, so now let's allocate memory and store them permanently. |
| 437 |
* (side note: we may hold on to these values for quite a while, thus we trim their |
| 438 |
* memory consumption) |
| 439 |
*/ |
| 440 |
if ((pThis->pRemHostName = malloc(prop.GetStringLen(fqdn) + 1)) == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 441 |
memcpy(pThis->pRemHostName, propGetSzStr(fqdn), prop.GetStringLen(fqdn) + 1); |
| 442 |
prop.Destruct(&fqdn); |
| 443 |
|
| 444 |
finalize_it: |
| 445 |
RETiRet; |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
/* obtain connection info as soon as we are connected */ |
| 450 |
static void get_socket_info(const int sockfd, char *const connInfo) { |
| 451 |
char local_ip_str[INET_ADDRSTRLEN]; // Buffer to hold the IP address string |
| 452 |
int local_port = -1; |
| 453 |
char local_port_str[8]; |
| 454 |
char remote_ip_str[INET_ADDRSTRLEN]; // Buffer to hold the IP address string |
| 455 |
int remote_port = -1; |
| 456 |
char remote_port_str[8]; |
| 457 |
struct sockaddr_in local_addr; |
| 458 |
socklen_t local_addr_len = sizeof(local_addr); |
| 459 |
|
| 460 |
struct sockaddr_in remote_addr; |
| 461 |
socklen_t remote_addr_len = sizeof(remote_addr); |
| 462 |
|
| 463 |
/* local system info */ |
| 464 |
local_addr.sin_port = 0; /* just to keep clang static analyzer happy */ |
| 465 |
if (getsockname(sockfd, (struct sockaddr *)&local_addr, &local_addr_len) == -1) { |
| 466 |
RS_COPY_LITERAL(local_ip_str, "?"); |
| 467 |
} else { |
| 468 |
if (inet_ntop(AF_INET, &local_addr.sin_addr, local_ip_str, INET_ADDRSTRLEN) == NULL) { |
| 469 |
RS_COPY_LITERAL(local_ip_str, "?"); |
| 470 |
} |
| 471 |
local_port = ntohs(local_addr.sin_port); |
| 472 |
} |
| 473 |
|
| 474 |
/* remote system info */ |
| 475 |
remote_addr.sin_port = 0; /* just to keep clang static analyzer happy */ |
| 476 |
if (getpeername(sockfd, (struct sockaddr *)&remote_addr, &remote_addr_len) == -1) { |
| 477 |
RS_COPY_LITERAL(remote_ip_str, "?"); |
| 478 |
} else { |
| 479 |
if (inet_ntop(AF_INET, &remote_addr.sin_addr, remote_ip_str, INET_ADDRSTRLEN) == NULL) { |
| 480 |
RS_COPY_LITERAL(remote_ip_str, "?"); |
| 481 |
} |
| 482 |
remote_port = ntohs(remote_addr.sin_port); |
| 483 |
} |
| 484 |
|
| 485 |
if (local_port == -1) { |
| 486 |
RS_COPY_LITERAL(local_port_str, "?"); |
| 487 |
} else { |
| 488 |
snprintf(local_port_str, 7, "%d", local_port); |
| 489 |
local_port_str[7] = '\0'; /* be on safe side */ |
| 490 |
} |
| 491 |
if (remote_port == -1) { |
| 492 |
RS_COPY_LITERAL(remote_port_str, "?"); |
| 493 |
} else { |
| 494 |
snprintf(remote_port_str, 7, "%d", remote_port); |
| 495 |
remote_port_str[7] = '\0'; /* be on safe side */ |
| 496 |
} |
| 497 |
snprintf(connInfo, TCPSRV_CONNINFO_SIZE, "from %s:%s to %s:%s", remote_ip_str, remote_port_str, local_ip_str, |
| 498 |
local_port_str); |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
/* accept an incoming connection request |
| 503 |
* rgerhards, 2008-04-22 |
| 504 |
*/ |
| 505 |
static rsRetVal AcceptConnReq(nsd_t *pNsd, nsd_t **ppNew, char *const connInfo) { |
| 506 |
int sockflags; |
| 507 |
nsd_ptcp_t *pThis = (nsd_ptcp_t *)pNsd; |
| 508 |
struct sockaddr_storage addr; |
| 509 |
socklen_t addrlen = sizeof(addr); |
| 510 |
nsd_ptcp_t *pNew = NULL; |
| 511 |
int iNewSock = -1; |
| 512 |
|
| 513 |
DEFiRet; |
| 514 |
assert(ppNew != NULL); |
| 515 |
ISOBJ_TYPE_assert(pThis, nsd_ptcp); |
| 516 |
|
| 517 |
iNewSock = accept(pThis->sock, (struct sockaddr *)&addr, &addrlen); |
| 518 |
|
| 519 |
if (iNewSock < 0) { |
| 520 |
if (errno == EINTR || errno == EAGAIN) { |
| 521 |
ABORT_FINALIZE(RS_RET_NO_MORE_DATA); |
| 522 |
} |
| 523 |
|
| 524 |
LogMsg(errno, RS_RET_ACCEPT_ERR, LOG_WARNING, "nds_ptcp: error accepting connection on socket %d", pThis->sock); |
| 525 |
ABORT_FINALIZE(RS_RET_ACCEPT_ERR); |
| 526 |
} |
| 527 |
|
| 528 |
get_socket_info(iNewSock, connInfo); |
| 529 |
|
| 530 |
/* construct our object so that we can use it... */ |
| 531 |
CHKiRet(nsd_ptcpConstruct(&pNew)); |
| 532 |
|
| 533 |
/* for the legacy ACL code, we need to preserve addr. While this is far from |
| 534 |
* begin perfect (from an abstract design perspective), we need this to prevent |
| 535 |
* breaking everything. |
| 536 |
*/ |
| 537 |
memcpy(&pNew->remAddr, &addr, sizeof(struct sockaddr_storage)); |
| 538 |
CHKiRet(FillRemHost(pNew, &addr)); |
| 539 |
|
| 540 |
/* set the new socket to non-blocking IO -TODO:do we really need to do this here? Do we always want it? */ |
| 541 |
if ((sockflags = fcntl(iNewSock, F_GETFL)) != -1) { |
| 542 |
sockflags |= O_NONBLOCK; |
| 543 |
/* SETFL could fail too, so get it caught by the subsequent |
| 544 |
* error check. |
| 545 |
*/ |
| 546 |
sockflags = fcntl(iNewSock, F_SETFL, sockflags); |
| 547 |
} |
| 548 |
if (sockflags == -1) { |
| 549 |
dbgprintf("error %d setting fcntl(O_NONBLOCK) on tcp socket %d", errno, iNewSock); |
| 550 |
ABORT_FINALIZE(RS_RET_IO_ERROR); |
| 551 |
} |
| 552 |
|
| 553 |
pNew->sock = iNewSock; |
| 554 |
*ppNew = (nsd_t *)pNew; |
| 555 |
|
| 556 |
finalize_it: |
| 557 |
if (iRet != RS_RET_OK) { |
| 558 |
if (pNew != NULL) nsd_ptcpDestruct(&pNew); |
| 559 |
/* the close may be redundant, but that doesn't hurt... */ |
| 560 |
sockClose(&iNewSock); |
| 561 |
} |
| 562 |
|
| 563 |
RETiRet; |
| 564 |
} |
| 565 |
|
| 566 |
|
| 567 |
/* initialize tcp sockets for a listner. The initialized sockets are passed to the |
| 568 |
* app-level caller via a callback. |
| 569 |
* pLstnPort must point to a port name or number. NULL is NOT permitted. pLstnIP |
| 570 |
* points to the port to listen to (NULL means "all"), iMaxSess has the maximum |
| 571 |
* number of sessions permitted. |
| 572 |
* rgerhards, 2008-04-22 |
| 573 |
*/ |
| 574 |
static rsRetVal ATTR_NONNULL(1, 3, 5) LstnInit(netstrms_t *const pNS, |
| 575 |
void *pUsr, |
| 576 |
rsRetVal (*fAddLstn)(void *, netstrm_t *), |
| 577 |
const int iSessMax, |
| 578 |
const tcpLstnParams_t *const cnf_params) { |
| 579 |
DEFiRet; |
| 580 |
netstrm_t *pNewStrm = NULL; |
| 581 |
nsd_t *pNewNsd = NULL; |
| 582 |
int error, maxs, on = 1; |
| 583 |
int isIPv6 = 0; |
| 584 |
int sock = -1; |
| 585 |
int numSocks; |
| 586 |
int sockflags; |
| 587 |
int port_override = 0; /* if dyn port (0): use this for actually bound port */ |
| 588 |
struct addrinfo hints, *res = NULL, *r; |
| 589 |
union { |
| 590 |
struct sockaddr *sa; |
| 591 |
struct sockaddr_in *ipv4; |
| 592 |
struct sockaddr_in6 *ipv6; |
| 593 |
} savecast; |
| 594 |
|
| 595 |
ISOBJ_TYPE_assert(pNS, netstrms); |
| 596 |
assert(fAddLstn != NULL); |
| 597 |
assert(cnf_params->pszPort != NULL); |
| 598 |
assert(iSessMax >= 0); |
| 599 |
|
| 600 |
dbgprintf("creating tcp listen socket on port %s\n", cnf_params->pszPort); |
| 601 |
|
| 602 |
memset(&hints, 0, sizeof(hints)); |
| 603 |
hints.ai_flags = AI_PASSIVE; |
| 604 |
hints.ai_family = glbl.GetDefPFFamily(runConf); |
| 605 |
hints.ai_socktype = SOCK_STREAM; |
| 606 |
|
| 607 |
error = getaddrinfo((const char *)cnf_params->pszAddr, (const char *)cnf_params->pszPort, &hints, &res); |
| 608 |
if (error) { |
| 609 |
LogError(0, RS_RET_INVALID_PORT, "error querying port '%s': %s", |
| 610 |
(cnf_params->pszAddr == NULL) ? "**UNSPECIFIED**" : (const char *)cnf_params->pszAddr, |
| 611 |
gai_strerror(error)); |
| 612 |
ABORT_FINALIZE(RS_RET_INVALID_PORT); |
| 613 |
} |
| 614 |
|
| 615 |
/* Count max number of sockets we may open */ |
| 616 |
for (maxs = 0, r = res; r != NULL; r = r->ai_next, maxs++) /* EMPTY */ |
| 617 |
; |
| 618 |
|
| 619 |
numSocks = 0; /* num of sockets counter at start of array */ |
| 620 |
for (r = res; r != NULL; r = r->ai_next) { |
| 621 |
if (port_override != 0) { |
| 622 |
savecast.sa = (struct sockaddr *)r->ai_addr; |
| 623 |
if (r->ai_family == AF_INET6) { |
| 624 |
savecast.ipv6->sin6_port = port_override; |
| 625 |
} else { |
| 626 |
savecast.ipv4->sin_port = port_override; |
| 627 |
} |
| 628 |
} |
| 629 |
sock = socket(r->ai_family, r->ai_socktype, r->ai_protocol); |
| 630 |
if (sock < 0) { |
| 631 |
if (!(r->ai_family == PF_INET6 && errno == EAFNOSUPPORT)) { |
| 632 |
dbgprintf("error %d creating tcp listen socket", errno); |
| 633 |
/* it is debatable if PF_INET with EAFNOSUPPORT should |
| 634 |
* also be ignored... |
| 635 |
*/ |
| 636 |
} |
| 637 |
continue; |
| 638 |
} |
| 639 |
|
| 640 |
#ifdef IPV6_V6ONLY |
| 641 |
if (r->ai_family == AF_INET6) { |
| 642 |
isIPv6 = 1; |
| 643 |
int iOn = 1; |
| 644 |
if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&iOn, sizeof(iOn)) < 0) { |
| 645 |
close(sock); |
| 646 |
sock = -1; |
| 647 |
continue; |
| 648 |
} |
| 649 |
} |
| 650 |
#endif |
| 651 |
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0) { |
| 652 |
dbgprintf("error %d setting tcp socket option\n", errno); |
| 653 |
close(sock); |
| 654 |
sock = -1; |
| 655 |
continue; |
| 656 |
} |
| 657 |
|
| 658 |
/* We use non-blocking IO! */ |
| 659 |
if ((sockflags = fcntl(sock, F_GETFL)) != -1) { |
| 660 |
sockflags |= O_NONBLOCK; |
| 661 |
/* SETFL could fail too, so get it caught by the subsequent |
| 662 |
* error check. |
| 663 |
*/ |
| 664 |
sockflags = fcntl(sock, F_SETFL, sockflags); |
| 665 |
} |
| 666 |
if (sockflags == -1) { |
| 667 |
dbgprintf("error %d setting fcntl(O_NONBLOCK) on tcp socket", errno); |
| 668 |
close(sock); |
| 669 |
sock = -1; |
| 670 |
continue; |
| 671 |
} |
| 672 |
|
| 673 |
/* We need to enable BSD compatibility. Otherwise an attacker |
| 674 |
* could flood our log files by sending us tons of ICMP errors. |
| 675 |
*/ |
| 676 |
#if !defined(_AIX) && !defined(BSD) |
| 677 |
if (net.should_use_so_bsdcompat()) { |
| 678 |
if (setsockopt(sock, SOL_SOCKET, SO_BSDCOMPAT, (char *)&on, sizeof(on)) < 0) { |
| 679 |
LogError(errno, NO_ERRCODE, "TCP setsockopt(BSDCOMPAT)"); |
| 680 |
close(sock); |
| 681 |
sock = -1; |
| 682 |
continue; |
| 683 |
} |
| 684 |
} |
| 685 |
#endif |
| 686 |
|
| 687 |
if ((bind(sock, r->ai_addr, r->ai_addrlen) < 0) |
| 688 |
#ifndef IPV6_V6ONLY |
| 689 |
&& (errno != EADDRINUSE) |
| 690 |
#endif |
| 691 |
) { |
| 692 |
/* TODO: check if *we* bound the socket - else we *have* an error! */ |
| 693 |
LogError(errno, NO_ERRCODE, "Error while binding tcp socket"); |
| 694 |
close(sock); |
| 695 |
sock = -1; |
| 696 |
continue; |
| 697 |
} |
| 698 |
|
| 699 |
/* if we bind to dynamic port (port 0 given), we will do so consistently. Thus |
| 700 |
* once we got a dynamic port, we will keep it and use it for other protocols |
| 701 |
* as well. As of my understanding, this should always work as the OS does not |
| 702 |
* pick a port that is used by some protocol (well, at least this looks very |
| 703 |
* unlikely...). If our asusmption is wrong, we should iterate until we find a |
| 704 |
* combination that works - it is very unusual to have the same service listen |
| 705 |
* on differnt ports on IPv4 and IPv6. |
| 706 |
*/ |
| 707 |
savecast.sa = (struct sockaddr *)r->ai_addr; |
| 708 |
const int currport = (isIPv6) ? savecast.ipv6->sin6_port : savecast.ipv4->sin_port; |
| 709 |
if (currport == 0) { |
| 710 |
socklen_t socklen_r = r->ai_addrlen; |
| 711 |
if (getsockname(sock, r->ai_addr, &socklen_r) == -1) { |
| 712 |
LogError(errno, NO_ERRCODE, |
| 713 |
"nsd_ptcp: ListenPortFileName: getsockname:" |
| 714 |
"error while trying to get socket"); |
| 715 |
} |
| 716 |
r->ai_addrlen = socklen_r; |
| 717 |
savecast.sa = (struct sockaddr *)r->ai_addr; |
| 718 |
port_override = (isIPv6) ? savecast.ipv6->sin6_port : savecast.ipv4->sin_port; |
| 719 |
if (cnf_params->pszLstnPortFileName != NULL) { |
| 720 |
FILE *fp; |
| 721 |
if ((fp = fopen((const char *)cnf_params->pszLstnPortFileName, "w+")) == NULL) { |
| 722 |
LogError(errno, RS_RET_IO_ERROR, |
| 723 |
"nsd_ptcp: ListenPortFileName: " |
| 724 |
"error while trying to open file"); |
| 725 |
ABORT_FINALIZE(RS_RET_IO_ERROR); |
| 726 |
} |
| 727 |
if (isIPv6) { |
| 728 |
fprintf(fp, "%d", ntohs(savecast.ipv6->sin6_port)); |
| 729 |
} else { |
| 730 |
fprintf(fp, "%d", ntohs(savecast.ipv4->sin_port)); |
| 731 |
} |
| 732 |
fclose(fp); |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
const int iSynBacklog = (pNS->iSynBacklog == 0) ? iSessMax / 10 + 5 : pNS->iSynBacklog; |
| 737 |
if (listen(sock, iSynBacklog) < 0) { |
| 738 |
/* If the listen fails, it most probably fails because we ask |
| 739 |
* for a too-large backlog. So in this case we first set back |
| 740 |
* to a fixed, reasonable, limit that should work. Only if |
| 741 |
* that fails, too, we give up. |
| 742 |
*/ |
| 743 |
dbgprintf("listen with a backlog of %d failed - retrying with default of 32.\n", iSessMax / 10 + 5); |
| 744 |
if (listen(sock, 32) < 0) { |
| 745 |
dbgprintf("tcp listen error %d, suspending\n", errno); |
| 746 |
close(sock); |
| 747 |
sock = -1; |
| 748 |
continue; |
| 749 |
} |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
/* if we reach this point, we were able to obtain a valid socket, so we can |
| 754 |
* construct a new netstrm obj and hand it over to the upper layers for inclusion |
| 755 |
* into their socket array. -- rgerhards, 2008-04-23 |
| 756 |
*/ |
| 757 |
CHKiRet(pNS->Drvr.Construct(&pNewNsd)); |
| 758 |
CHKiRet(pNS->Drvr.SetSock(pNewNsd, sock)); |
| 759 |
CHKiRet(pNS->Drvr.SetMode(pNewNsd, netstrms.GetDrvrMode(pNS))); |
| 760 |
CHKiRet(pNS->Drvr.SetCheckExtendedKeyUsage(pNewNsd, netstrms.GetDrvrCheckExtendedKeyUsage(pNS))); |
| 761 |
CHKiRet(pNS->Drvr.SetPrioritizeSAN(pNewNsd, netstrms.GetDrvrPrioritizeSAN(pNS))); |
| 762 |
CHKiRet(pNS->Drvr.SetTlsCAFile(pNewNsd, netstrms.GetDrvrTlsCAFile(pNS))); |
| 763 |
CHKiRet(pNS->Drvr.SetTlsCRLFile(pNewNsd, netstrms.GetDrvrTlsCRLFile(pNS))); |
| 764 |
CHKiRet(pNS->Drvr.SetTlsKeyFile(pNewNsd, netstrms.GetDrvrTlsKeyFile(pNS))); |
| 765 |
CHKiRet(pNS->Drvr.SetTlsCertFile(pNewNsd, netstrms.GetDrvrTlsCertFile(pNS))); |
| 766 |
CHKiRet(pNS->Drvr.SetTlsVerifyDepth(pNewNsd, netstrms.GetDrvrTlsVerifyDepth(pNS))); |
| 767 |
CHKiRet(pNS->Drvr.SetTlsRevocationCheck(pNewNsd, netstrms.GetDrvrTlsRevocationCheck(pNS))); |
| 768 |
CHKiRet(pNS->Drvr.SetAuthMode(pNewNsd, netstrms.GetDrvrAuthMode(pNS))); |
| 769 |
CHKiRet(pNS->Drvr.SetPermitExpiredCerts(pNewNsd, netstrms.GetDrvrPermitExpiredCerts(pNS))); |
| 770 |
CHKiRet(pNS->Drvr.SetPermPeers(pNewNsd, netstrms.GetDrvrPermPeers(pNS))); |
| 771 |
CHKiRet(pNS->Drvr.SetGnutlsPriorityString(pNewNsd, netstrms.GetDrvrGnutlsPriorityString(pNS))); |
| 772 |
|
| 773 |
CHKiRet(netstrms.CreateStrm(pNS, &pNewStrm)); |
| 774 |
pNewStrm->pDrvrData = (nsd_t *)pNewNsd; |
| 775 |
if (pNS->fLstnInitDrvr != NULL) { |
| 776 |
CHKiRet(pNS->fLstnInitDrvr(pNewStrm)); |
| 777 |
} |
| 778 |
CHKiRet(fAddLstn(pUsr, pNewStrm)); |
| 779 |
pNewStrm = NULL; |
| 780 |
/* sock has been handed over by SetSock() above, so invalidate it here |
| 781 |
* coverity scan falsely identifies this as ressource leak |
| 782 |
*/ |
| 783 |
sock = -1; |
| 784 |
++numSocks; |
| 785 |
} |
| 786 |
|
| 787 |
if (numSocks != maxs) |
| 788 |
dbgprintf( |
| 789 |
"We could initialize %d TCP listen sockets out of %d we received " |
| 790 |
"- this may or may not be an error indication.\n", |
| 791 |
numSocks, maxs); |
| 792 |
|
| 793 |
if (numSocks == 0) { |
| 794 |
dbgprintf("No TCP listen sockets could successfully be initialized\n"); |
| 795 |
ABORT_FINALIZE(RS_RET_COULD_NOT_BIND); |
| 796 |
} |
| 797 |
|
| 798 |
finalize_it: |
| 799 |
if (sock != -1) { |
| 800 |
close(sock); |
| 801 |
} |
| 802 |
if (res != NULL) freeaddrinfo(res); |
| 803 |
|
| 804 |
if (iRet != RS_RET_OK) { |
| 805 |
if (pNewStrm != NULL) |
| 806 |
netstrm.Destruct(&pNewStrm); |
| 807 |
else if (pNewNsd != NULL) |
| 808 |
pNS->Drvr.Destruct(&pNewNsd); |
| 809 |
} |
| 810 |
|
| 811 |
RETiRet; |
| 812 |
} |
| 813 |
|
| 814 |
/* receive data from a tcp socket |
| 815 |
* The lenBuf parameter must contain the max buffer size on entry and contains |
| 816 |
* the number of octets read (or -1 in case of error) on exit. This function |
| 817 |
* never blocks, not even when called on a blocking socket. That is important |
| 818 |
* for client sockets, which are set to block during send, but should not |
| 819 |
* block when trying to read data. If *pLenBuf is -1, an error occurred and |
| 820 |
* oserr holds the exact error cause. |
| 821 |
* rgerhards, 2008-03-17 |
| 822 |
*/ |
| 823 |
static rsRetVal Rcv(nsd_t *pNsd, uchar *pRcvBuf, ssize_t *pLenBuf, int *const oserr, unsigned *const nextIODirection) { |
| 824 |
char errStr[1024]; |
| 825 |
DEFiRet; |
| 826 |
nsd_ptcp_t *pThis = (nsd_ptcp_t *)pNsd; |
| 827 |
ISOBJ_TYPE_assert(pThis, nsd_ptcp); |
| 828 |
assert(oserr != NULL); |
| 829 |
/* AIXPORT : MSG_DONTWAIT not supported */ |
| 830 |
#if defined(_AIX) |
| 831 |
#define MSG_DONTWAIT MSG_NONBLOCK |
| 832 |
#endif |
| 833 |
|
| 834 |
*pLenBuf = recv(pThis->sock, pRcvBuf, *pLenBuf, MSG_DONTWAIT); |
| 835 |
*oserr = errno; |
| 836 |
*nextIODirection = NSDSEL_RD; |
| 837 |
|
| 838 |
if (*pLenBuf == 0) { |
| 839 |
ABORT_FINALIZE(RS_RET_CLOSED); |
| 840 |
} else if (*pLenBuf < 0) { |
| 841 |
if (*oserr == EINTR || *oserr == EAGAIN) { |
| 842 |
ABORT_FINALIZE(RS_RET_RETRY); |
| 843 |
} else { |
| 844 |
rs_strerror_r(errno, errStr, sizeof(errStr)); |
| 845 |
dbgprintf("error during recv on NSD %p: %s\n", pNsd, errStr); |
| 846 |
ABORT_FINALIZE(RS_RET_RCV_ERR); |
| 847 |
} |
| 848 |
} |
| 849 |
|
| 850 |
finalize_it: |
| 851 |
RETiRet; |
| 852 |
} |
| 853 |
|
| 854 |
|
| 855 |
/* send a buffer. On entry, pLenBuf contains the number of octets to |
| 856 |
* write. On exit, it contains the number of octets actually written. |
| 857 |
* If this number is lower than on entry, only a partial buffer has |
| 858 |
* been written. |
| 859 |
* rgerhards, 2008-03-19 |
| 860 |
*/ |
| 861 |
static rsRetVal Send(nsd_t *pNsd, uchar *pBuf, ssize_t *pLenBuf) { |
| 862 |
nsd_ptcp_t *pThis = (nsd_ptcp_t *)pNsd; |
| 863 |
ssize_t written; |
| 864 |
DEFiRet; |
| 865 |
ISOBJ_TYPE_assert(pThis, nsd_ptcp); |
| 866 |
|
| 867 |
written = send(pThis->sock, pBuf, *pLenBuf, 0); |
| 868 |
|
| 869 |
if (written == -1) { |
| 870 |
switch (errno) { |
| 871 |
case EAGAIN: |
| 872 |
case EINTR: |
| 873 |
/* this is fine, just retry... */ |
| 874 |
written = 0; |
| 875 |
break; |
| 876 |
default: |
| 877 |
ABORT_FINALIZE(RS_RET_IO_ERROR); |
| 878 |
break; |
| 879 |
} |
| 880 |
} |
| 881 |
|
| 882 |
*pLenBuf = written; |
| 883 |
finalize_it: |
| 884 |
RETiRet; |
| 885 |
} |
| 886 |
|
| 887 |
|
| 888 |
/* Enable KEEPALIVE handling on the socket. |
| 889 |
* rgerhards, 2009-06-02 |
| 890 |
*/ |
| 891 |
static rsRetVal EnableKeepAlive(nsd_t *pNsd) { |
| 892 |
nsd_ptcp_t *pThis = (nsd_ptcp_t *)pNsd; |
| 893 |
int ret; |
| 894 |
int optval; |
| 895 |
socklen_t optlen; |
| 896 |
DEFiRet; |
| 897 |
ISOBJ_TYPE_assert(pThis, nsd_ptcp); |
| 898 |
|
| 899 |
optval = 1; |
| 900 |
optlen = sizeof(optval); |
| 901 |
ret = setsockopt(pThis->sock, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen); |
| 902 |
if (ret < 0) { |
| 903 |
dbgprintf("EnableKeepAlive socket call returns error %d\n", ret); |
| 904 |
ABORT_FINALIZE(RS_RET_ERR); |
| 905 |
} |
| 906 |
|
| 907 |
#if defined(IPPROTO_TCP) && defined(TCP_KEEPCNT) |
| 908 |
if (pThis->iKeepAliveProbes > 0) { |
| 909 |
optval = pThis->iKeepAliveProbes; |
| 910 |
optlen = sizeof(optval); |
| 911 |
ret = setsockopt(pThis->sock, IPPROTO_TCP, TCP_KEEPCNT, &optval, optlen); |
| 912 |
} else { |
| 913 |
ret = 0; |
| 914 |
} |
| 915 |
#else |
| 916 |
ret = -1; |
| 917 |
#endif |
| 918 |
if (ret < 0) { |
| 919 |
LogError(ret, NO_ERRCODE, "nsd_ptcp cannot set keepalive probes - ignored"); |
| 920 |
} |
| 921 |
|
| 922 |
#if defined(IPPROTO_TCP) && defined(TCP_KEEPIDLE) |
| 923 |
if (pThis->iKeepAliveTime > 0) { |
| 924 |
optval = pThis->iKeepAliveTime; |
| 925 |
optlen = sizeof(optval); |
| 926 |
ret = setsockopt(pThis->sock, IPPROTO_TCP, TCP_KEEPIDLE, &optval, optlen); |
| 927 |
} else { |
| 928 |
ret = 0; |
| 929 |
} |
| 930 |
#else |
| 931 |
ret = -1; |
| 932 |
#endif |
| 933 |
if (ret < 0) { |
| 934 |
LogError(ret, NO_ERRCODE, "nsd_ptcp cannot set keepalive time - ignored"); |
| 935 |
} |
| 936 |
|
| 937 |
#if defined(IPPROTO_TCP) && defined(TCP_KEEPCNT) |
| 938 |
if (pThis->iKeepAliveIntvl > 0) { |
| 939 |
optval = pThis->iKeepAliveIntvl; |
| 940 |
optlen = sizeof(optval); |
| 941 |
ret = setsockopt(pThis->sock, IPPROTO_TCP, TCP_KEEPINTVL, &optval, optlen); |
| 942 |
} else { |
| 943 |
ret = 0; |
| 944 |
} |
| 945 |
#else |
| 946 |
ret = -1; |
| 947 |
#endif |
| 948 |
if (ret < 0) { |
| 949 |
LogError(errno, NO_ERRCODE, "nsd_ptcp cannot set keepalive intvl - ignored"); |
| 950 |
} |
| 951 |
|
| 952 |
dbgprintf("KEEPALIVE enabled for socket %d\n", pThis->sock); |
| 953 |
|
| 954 |
finalize_it: |
| 955 |
RETiRet; |
| 956 |
} |
| 957 |
|
| 958 |
|
| 959 |
/* open a connection to a remote host (server). |
| 960 |
* rgerhards, 2008-03-19 |
| 961 |
*/ |
| 962 |
static rsRetVal Connect(nsd_t *pNsd, int family, uchar *port, uchar *host, char *device) { |
| 963 |
nsd_ptcp_t *pThis = (nsd_ptcp_t *)pNsd; |
| 964 |
struct addrinfo *res = NULL; |
| 965 |
struct addrinfo *currAddr; |
| 966 |
struct addrinfo hints; |
| 967 |
int saved_errno = 0; |
| 968 |
struct timeval start, end; |
| 969 |
long seconds = 0, useconds = 0; |
| 970 |
int gai_rc; |
| 971 |
|
| 972 |
DEFiRet; |
| 973 |
ISOBJ_TYPE_assert(pThis, nsd_ptcp); |
| 974 |
assert(port != NULL); |
| 975 |
assert(host != NULL); |
| 976 |
assert(pThis->sock == -1); |
| 977 |
|
| 978 |
memset(&hints, 0, sizeof(hints)); |
| 979 |
hints.ai_family = family; |
| 980 |
hints.ai_socktype = SOCK_STREAM; |
| 981 |
gai_rc = getaddrinfo((char *)host, (char *)port, &hints, &res); |
| 982 |
if (gai_rc != 0) { |
| 983 |
LogError(0, RS_RET_IO_ERROR, "cannot resolve hostname '%s': %s", host, gai_strerror(gai_rc)); |
| 984 |
ABORT_FINALIZE(RS_RET_IO_ERROR); |
| 985 |
} |
| 986 |
|
| 987 |
/* We need to copy Remote Hostname here for error logging purposes */ |
| 988 |
if ((pThis->pRemHostName = malloc(strlen((char *)host) + 1)) == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 989 |
memcpy(pThis->pRemHostName, host, strlen((char *)host) + 1); |
| 990 |
|
| 991 |
if (device != NULL) { |
| 992 |
#if !defined(SO_BINDTODEVICE) |
| 993 |
LogError(0, RS_RET_VALUE_NOT_SUPPORTED, |
| 994 |
"cannot use device '%s': SO_BINDTODEVICE is not supported on this platform", device); |
| 995 |
ABORT_FINALIZE(RS_RET_VALUE_NOT_SUPPORTED); |
| 996 |
#endif |
| 997 |
} |
| 998 |
|
| 999 |
gettimeofday(&start, NULL); |
| 1000 |
for (currAddr = res; currAddr != NULL; currAddr = currAddr->ai_next) { |
| 1001 |
pThis->sock = socket(currAddr->ai_family, currAddr->ai_socktype, currAddr->ai_protocol); |
| 1002 |
if (pThis->sock == -1) { |
| 1003 |
saved_errno = errno; |
| 1004 |
continue; |
| 1005 |
} |
| 1006 |
|
| 1007 |
if (device) { |
| 1008 |
#if defined(SO_BINDTODEVICE) |
| 1009 |
if (setsockopt(pThis->sock, SOL_SOCKET, SO_BINDTODEVICE, device, strlen(device) + 1) < 0) { |
| 1010 |
saved_errno = errno; |
| 1011 |
dbgprintf("setsockopt(SO_BINDTODEVICE) failed\n"); |
| 1012 |
sockClose(&pThis->sock); |
| 1013 |
continue; |
| 1014 |
} |
| 1015 |
#endif |
| 1016 |
} |
| 1017 |
|
| 1018 |
if (connect(pThis->sock, currAddr->ai_addr, currAddr->ai_addrlen) == 0) { |
| 1019 |
break; |
| 1020 |
} |
| 1021 |
saved_errno = errno; |
| 1022 |
sockClose(&pThis->sock); |
| 1023 |
} |
| 1024 |
|
| 1025 |
if (currAddr == NULL) { |
| 1026 |
gettimeofday(&end, NULL); |
| 1027 |
seconds = end.tv_sec - start.tv_sec; |
| 1028 |
useconds = end.tv_usec - start.tv_usec; |
| 1029 |
if (useconds < 0) { |
| 1030 |
useconds += 1000000; |
| 1031 |
seconds -= 1; |
| 1032 |
} |
| 1033 |
if (seconds < 0) { |
| 1034 |
seconds = 0; |
| 1035 |
useconds = 0; |
| 1036 |
} |
| 1037 |
LogError(saved_errno, RS_RET_IO_ERROR, "cannot connect to %s:%s (took %ld.%02ld seconds)", host, port, seconds, |
| 1038 |
useconds / 10000); |
| 1039 |
ABORT_FINALIZE(RS_RET_IO_ERROR); |
| 1040 |
} |
| 1041 |
|
| 1042 |
finalize_it: |
| 1043 |
if (res != NULL) freeaddrinfo(res); |
| 1044 |
|
| 1045 |
if (iRet != RS_RET_OK) { |
| 1046 |
sockClose(&pThis->sock); |
| 1047 |
} |
| 1048 |
|
| 1049 |
RETiRet; |
| 1050 |
} |
| 1051 |
|
| 1052 |
|
| 1053 |
/* get the remote hostname. The returned hostname must be freed by the |
| 1054 |
* caller. |
| 1055 |
* rgerhards, 2008-04-24 |
| 1056 |
*/ |
| 1057 |
static rsRetVal GetRemoteHName(nsd_t *pNsd, uchar **ppszHName) { |
| 1058 |
DEFiRet; |
| 1059 |
nsd_ptcp_t *pThis = (nsd_ptcp_t *)pNsd; |
| 1060 |
ISOBJ_TYPE_assert(pThis, nsd_ptcp); |
| 1061 |
assert(ppszHName != NULL); |
| 1062 |
|
| 1063 |
// TODO: how can the RemHost be empty? |
| 1064 |
CHKmalloc(*ppszHName = (uchar *)strdup(pThis->pRemHostName == NULL ? "" : (char *)pThis->pRemHostName)); |
| 1065 |
|
| 1066 |
finalize_it: |
| 1067 |
RETiRet; |
| 1068 |
} |
| 1069 |
|
| 1070 |
|
| 1071 |
/** |
| 1072 |
* getRemotePort - return peer TCP port number for improved diagnostics |
| 1073 |
* @pThis: nsd_t object representing the network stream driver. |
| 1074 |
* @port: Pointer to an integer where the port number will be stored. |
| 1075 |
* It will be set to -1 on error or if no port information is available. |
| 1076 |
* @return: standard rsRetVal error code |
| 1077 |
*/ |
| 1078 |
static rsRetVal GetRemotePort(nsd_t *pNsd, int *port) { |
| 1079 |
DEFiRet; |
| 1080 |
nsd_ptcp_t *pThis = (nsd_ptcp_t *)pNsd; |
| 1081 |
struct sockaddr_storage addr; |
| 1082 |
socklen_t addrlen = sizeof(addr); |
| 1083 |
|
| 1084 |
assert(pThis != NULL); |
| 1085 |
assert(port != NULL); |
| 1086 |
|
| 1087 |
*port = -1; /* Initialize port to -1, indicating no port found initially or on error */ |
| 1088 |
memset(&addr, 0, sizeof(addr)); /* avoid potential garbage values */ |
| 1089 |
|
| 1090 |
if (getpeername(pThis->sock, (struct sockaddr *)&addr, &addrlen) == 0) { |
| 1091 |
if (addr.ss_family == AF_INET6) { |
| 1092 |
*port = ntohs(((struct sockaddr_in6 *)&addr)->sin6_port); |
| 1093 |
} else if (addr.ss_family == AF_INET) { |
| 1094 |
*port = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
| 1095 |
} else { |
| 1096 |
/* Unsupported address family */ |
| 1097 |
ABORT_FINALIZE(RS_RET_UNSUPP_SOCK_AF); |
| 1098 |
} |
| 1099 |
} else { |
| 1100 |
/* getpeername failed */ |
| 1101 |
ABORT_FINALIZE(RS_RET_IO_ERROR); |
| 1102 |
} |
| 1103 |
|
| 1104 |
finalize_it: |
| 1105 |
RETiRet; |
| 1106 |
} |
| 1107 |
|
| 1108 |
|
| 1109 |
static rsRetVal FmtRemotePortStr(const int port, uchar *const buf, const size_t len) { |
| 1110 |
int r; |
| 1111 |
|
| 1112 |
if (len == 0) { |
| 1113 |
return RS_RET_INVALID_PARAMS; /* can't do anything with zero-sized buffer */ |
| 1114 |
} |
| 1115 |
|
| 1116 |
if (port == -1) { |
| 1117 |
r = snprintf((char *)buf, len, "?"); |
| 1118 |
} else { |
| 1119 |
r = snprintf((char *)buf, len, "%d", port); |
| 1120 |
} |
| 1121 |
|
| 1122 |
if (r < 0) { |
| 1123 |
/* An encoding error occurred. */ |
| 1124 |
return RS_RET_ERR; |
| 1125 |
} else if ((size_t)r >= len) { |
| 1126 |
/* The buffer was too small, truncation occurred. */ |
| 1127 |
return RS_RET_PROVIDED_BUFFER_TOO_SMALL; |
| 1128 |
} |
| 1129 |
|
| 1130 |
/* Ensure null termination, though snprintf usually does this unless truncated. */ |
| 1131 |
buf[len - 1] = '\0'; |
| 1132 |
return RS_RET_OK; |
| 1133 |
} |
| 1134 |
|
| 1135 |
|
| 1136 |
/* This function checks if the connection is still alive - well, kind of... It |
| 1137 |
* is primarily being used for plain TCP syslog and it is quite a hack. However, |
| 1138 |
* as it seems to work, it is worth supporting it. The bottom line is that it |
| 1139 |
* should not be called by anything else but a plain tcp syslog sender. |
| 1140 |
* In order for it to work, it must be called *immediately* *before* the send() |
| 1141 |
* call. For details about what is done, see here: |
| 1142 |
* http://blog.gerhards.net/2008/06/getting-bit-more-reliability-from-plain.html |
| 1143 |
* rgerhards, 2008-06-09 |
| 1144 |
*/ |
| 1145 |
static rsRetVal CheckConnection(nsd_t *pNsd) { |
| 1146 |
DEFiRet; |
| 1147 |
int rc; |
| 1148 |
char msgbuf[1]; /* dummy */ |
| 1149 |
nsd_ptcp_t *pThis = (nsd_ptcp_t *)pNsd; |
| 1150 |
ISOBJ_TYPE_assert(pThis, nsd_ptcp); |
| 1151 |
|
| 1152 |
rc = recv(pThis->sock, msgbuf, 1, MSG_DONTWAIT | MSG_PEEK); |
| 1153 |
|
| 1154 |
if (rc == 0) { |
| 1155 |
LogMsg(0, RS_RET_IO_ERROR, LOG_INFO, |
| 1156 |
"ptcp network driver: CheckConnection detected that peer closed connection."); |
| 1157 |
sockClose(&pThis->sock); |
| 1158 |
ABORT_FINALIZE(RS_RET_PEER_CLOSED_CONN); |
| 1159 |
} else if (rc < 0) { |
| 1160 |
if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK) { |
| 1161 |
LogMsg(errno, RS_RET_IO_ERROR, LOG_ERR, "ptcp network driver: CheckConnection detected broken connection"); |
| 1162 |
sockClose(&pThis->sock); |
| 1163 |
ABORT_FINALIZE(RS_RET_IO_ERROR); |
| 1164 |
} |
| 1165 |
} |
| 1166 |
finalize_it: |
| 1167 |
RETiRet; |
| 1168 |
} |
| 1169 |
|
| 1170 |
|
| 1171 |
/* get the remote host's IP address. Caller must Destruct the object. |
| 1172 |
*/ |
| 1173 |
static rsRetVal GetRemoteIP(nsd_t *pNsd, prop_t **ip) { |
| 1174 |
DEFiRet; |
| 1175 |
nsd_ptcp_t *pThis = (nsd_ptcp_t *)pNsd; |
| 1176 |
ISOBJ_TYPE_assert(pThis, nsd_ptcp); |
| 1177 |
prop.AddRef(pThis->remoteIP); |
| 1178 |
*ip = pThis->remoteIP; |
| 1179 |
RETiRet; |
| 1180 |
} |
| 1181 |
|
| 1182 |
|
| 1183 |
/* queryInterface function */ |
| 1184 |
BEGINobjQueryInterface(nsd_ptcp) |
| 1185 |
CODESTARTobjQueryInterface(nsd_ptcp); |
| 1186 |
if (pIf->ifVersion != nsdCURR_IF_VERSION) { /* check for current version, increment on each change */ |
| 1187 |
ABORT_FINALIZE(RS_RET_INTERFACE_NOT_SUPPORTED); |
| 1188 |
} |
| 1189 |
|
| 1190 |
/* ok, we have the right interface, so let's fill it |
| 1191 |
* Please note that we may also do some backwards-compatibility |
| 1192 |
* work here (if we can support an older interface version - that, |
| 1193 |
* of course, also affects the "if" above). |
| 1194 |
*/ |
| 1195 |
pIf->Construct = (rsRetVal(*)(nsd_t **))nsd_ptcpConstruct; |
| 1196 |
pIf->Destruct = (rsRetVal(*)(nsd_t **))nsd_ptcpDestruct; |
| 1197 |
pIf->Abort = Abort; |
| 1198 |
pIf->GetRemAddr = GetRemAddr; |
| 1199 |
pIf->GetSock = GetSock; |
| 1200 |
pIf->SetSock = SetSock; |
| 1201 |
pIf->SetMode = SetMode; |
| 1202 |
pIf->SetAuthMode = SetAuthMode; |
| 1203 |
pIf->SetPermitExpiredCerts = SetPermitExpiredCerts; |
| 1204 |
pIf->SetGnutlsPriorityString = SetGnutlsPriorityString; |
| 1205 |
pIf->SetPermPeers = SetPermPeers; |
| 1206 |
pIf->Rcv = Rcv; |
| 1207 |
pIf->Send = Send; |
| 1208 |
pIf->LstnInit = LstnInit; |
| 1209 |
pIf->AcceptConnReq = AcceptConnReq; |
| 1210 |
pIf->Connect = Connect; |
| 1211 |
pIf->GetRemoteHName = GetRemoteHName; |
| 1212 |
pIf->GetRemoteIP = GetRemoteIP; |
| 1213 |
pIf->GetRemotePort = GetRemotePort; |
| 1214 |
pIf->FmtRemotePortStr = FmtRemotePortStr; |
| 1215 |
pIf->CheckConnection = CheckConnection; |
| 1216 |
pIf->EnableKeepAlive = EnableKeepAlive; |
| 1217 |
pIf->SetKeepAliveIntvl = SetKeepAliveIntvl; |
| 1218 |
pIf->SetKeepAliveProbes = SetKeepAliveProbes; |
| 1219 |
pIf->SetKeepAliveTime = SetKeepAliveTime; |
| 1220 |
pIf->SetCheckExtendedKeyUsage = SetCheckExtendedKeyUsage; |
| 1221 |
pIf->SetPrioritizeSAN = SetPrioritizeSAN; |
| 1222 |
pIf->SetTlsVerifyDepth = SetTlsVerifyDepth; |
| 1223 |
pIf->SetTlsCAFile = SetTlsCAFile; |
| 1224 |
pIf->SetTlsCRLFile = SetTlsCRLFile; |
| 1225 |
pIf->SetTlsKeyFile = SetTlsKeyFile; |
| 1226 |
pIf->SetTlsCertFile = SetTlsCertFile; |
| 1227 |
pIf->SetRemoteSNI = SetRemoteSNI; |
| 1228 |
pIf->SetTlsRevocationCheck = SetTlsRevocationCheck; |
| 1229 |
finalize_it: |
| 1230 |
ENDobjQueryInterface(nsd_ptcp) |
| 1231 |
|
| 1232 |
|
| 1233 |
/* exit our class |
| 1234 |
*/ |
| 1235 |
BEGINObjClassExit(nsd_ptcp, OBJ_IS_LOADABLE_MODULE) /* CHANGE class also in END MACRO! */ |
| 1236 |
CODESTARTObjClassExit(nsd_ptcp); |
| 1237 |
/* release objects we no longer need */ |
| 1238 |
objRelease(net, CORE_COMPONENT); |
| 1239 |
objRelease(glbl, CORE_COMPONENT); |
| 1240 |
objRelease(prop, CORE_COMPONENT); |
| 1241 |
objRelease(netstrm, DONT_LOAD_LIB); |
| 1242 |
objRelease(netstrms, LM_NETSTRMS_FILENAME); |
| 1243 |
ENDObjClassExit(nsd_ptcp) |
| 1244 |
|
| 1245 |
|
| 1246 |
/* Initialize the nsd_ptcp class. Must be called as the very first method |
| 1247 |
* before anything else is called inside this class. |
| 1248 |
* rgerhards, 2008-02-19 |
| 1249 |
*/ |
| 1250 |
BEGINObjClassInit(nsd_ptcp, 1, OBJ_IS_LOADABLE_MODULE) /* class, version */ |
| 1251 |
/* request objects we use */ |
| 1252 |
CHKiRet(objUse(glbl, CORE_COMPONENT)); |
| 1253 |
CHKiRet(objUse(prop, CORE_COMPONENT)); |
| 1254 |
CHKiRet(objUse(net, CORE_COMPONENT)); |
| 1255 |
CHKiRet(objUse(netstrms, LM_NETSTRMS_FILENAME)); |
| 1256 |
CHKiRet(objUse(netstrm, DONT_LOAD_LIB)); |
| 1257 |
|
| 1258 |
/* set our own handlers */ |
| 1259 |
ENDObjClassInit(nsd_ptcp) |
| 1260 |
|
| 1261 |
|
| 1262 |
/* --------------- here now comes the plumbing that makes as a library module --------------- */ |
| 1263 |
|
| 1264 |
|
| 1265 |
BEGINmodExit |
| 1266 |
CODESTARTmodExit; |
| 1267 |
nsd_ptcpClassExit(); |
| 1268 |
ENDmodExit |
| 1269 |
|
| 1270 |
|
| 1271 |
BEGINqueryEtryPt |
| 1272 |
CODESTARTqueryEtryPt; |
| 1273 |
CODEqueryEtryPt_STD_LIB_QUERIES; |
| 1274 |
ENDqueryEtryPt |
| 1275 |
|
| 1276 |
|
| 1277 |
BEGINmodInit() |
| 1278 |
CODESTARTmodInit; |
| 1279 |
*ipIFVersProvided = CURR_MOD_IF_VERSION; /* we only support the current interface specification */ |
| 1280 |
|
| 1281 |
/* Initialize all classes that are in our module - this includes ourselfs */ |
| 1282 |
CHKiRet(nsd_ptcpClassInit(pModInfo)); /* must be done after tcps_sess, as we use it */ |
| 1283 |
ENDmodInit |