Skip to content

Commit f65e317

Browse files
authored
Merge 9e341df into 1211c56
2 parents 1211c56 + 9e341df commit f65e317

File tree

10 files changed

+242
-83
lines changed

10 files changed

+242
-83
lines changed

NEWS.adoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ https://github.com/networkupstools/nut/milestone/12
327327

328328
- `upsd` data server updates:
329329
* Sometimes "Data for UPS [X] is stale" and "UPS [X] data is no longer
330-
stale" messages were logged in the same second, especially no busy
330+
stale" messages were logged in the same second, especially on busy
331331
systems. Now we allow one more second on top of `MAXAGE` setting to
332332
declare the device dead, just in case fractional/whole second rounding
333333
comes into play and breaks things. [issue #661]
@@ -502,6 +502,8 @@ several `FSD` notifications into one executed action. [PR #3097]
502502
* Added an option to (primarily) `--disable-threading` for systems with
503503
detected but broken `libpthread` support, or to test alternate code
504504
paths during development or in CI. [#3300]
505+
* Adjusted C++ header search path on Termux when `-isystem` is involved,
506+
as noted with NSS builds. [issues #1599, #1711, PR #3353]
505507

506508
- Recipes, CI and helper script updates not classified above:
507509
* Fixed CI recipes for PyPI publication of PyNUT(Client) module to also

clients/Makefile.am

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ CLEANFILES =
1616
# optionally includes "common.h" with the NUT build setup - and this option
1717
# was never triggered in fact, not until pushed through command line like this:
1818
AM_CXXFLAGS = -DHAVE_NUTCOMMON=1 -I$(top_builddir)/include -I$(top_srcdir)/include
19-
if WITH_SSL
20-
AM_CXXFLAGS += $(LIBSSL_CFLAGS)
21-
endif WITH_SSL
19+
if WITH_SSL_CXX
20+
AM_CXXFLAGS += $(LIBSSL_CXXFLAGS)
21+
endif WITH_SSL_CXX
2222

2323
# Make sure out-of-dir dependencies exist (especially when dev-building parts):
2424
$(top_builddir)/include/nut_version.h \
@@ -309,9 +309,9 @@ if HAVE_WINDOWS
309309
# Many versions of MingW seem to fail to build non-static DLL without this
310310
libnutclient_la_LDFLAGS += -no-undefined
311311
endif HAVE_WINDOWS
312-
if WITH_SSL
312+
if WITH_SSL_CXX
313313
libnutclient_la_LIBADD += $(LIBSSL_LDFLAGS_RPATH) $(LIBSSL_LIBS)
314-
endif WITH_SSL
314+
endif WITH_SSL_CXX
315315
else !HAVE_CXX11
316316
EXTRA_DIST += nutclient.h nutclient.cpp
317317
endif !HAVE_CXX11

clients/nutclient.cpp

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,13 @@ class Socket
258258

259259
private:
260260
SOCKET _sock;
261-
#ifdef WITH_OPENSSL
261+
#ifdef WITH_SSL_CXX
262+
# ifdef WITH_OPENSSL
262263
SSL* _ssl;
263264
static SSL_CTX* _ssl_ctx;
264-
#elif defined(WITH_NSS)
265+
# elif defined(WITH_NSS)
265266
PRFileDesc* _ssl;
267+
# endif
266268
#endif
267269
bool _debugConnect;
268270
struct timeval _tv;
@@ -271,7 +273,8 @@ class Socket
271273
uint16_t _port;
272274
int _ssl_configured;
273275
int _force_ssl; /* Always known, so even non-SSL builds can fail if security is required */
274-
#if defined(WITH_OPENSSL) || defined(WITH_NSS)
276+
#ifdef WITH_SSL_CXX
277+
# if defined(WITH_OPENSSL) || defined(WITH_NSS)
275278
int _certverify;
276279
/* OpenSSL specific */
277280
std::string _ca_path;
@@ -284,14 +287,14 @@ class Socket
284287
std::string _certstore_prefix;
285288
std::string _certident_name;
286289
std::string _certhost_name;
287-
#endif
290+
# endif
288291

289-
#if defined(WITH_OPENSSL)
292+
# if defined(WITH_OPENSSL)
290293
/* Callbacks, syntax dictated by OpenSSL */
291294
static int openssl_password_callback(char *buf, int size, int rwflag, void *userdata); /* pem_passwd_cb, 1.1.0+ */
292-
#endif
295+
# endif
293296

294-
#if defined(WITH_NSS)
297+
# if defined(WITH_NSS)
295298
/* Callbacks, syntax dictated by NSS */
296299
static char *nss_password_callback(PK11SlotInfo *slot, PRBool retry, void *arg);
297300
static SECStatus AuthCertificate(CERTCertDBHandle *arg, PRFileDesc *fd,
@@ -303,10 +306,12 @@ class Socket
303306
CERTDistNames *caNames, CERTCertificate **pRetCert,
304307
SECKEYPrivateKey **pRetKey);
305308
static void HandshakeCallback(PRFileDesc *fd, void *arg);
306-
#endif
309+
# endif
310+
#endif /* WITH_SSL_CXX */
307311
};
308312

309-
#ifdef WITH_OPENSSL
313+
#ifdef WITH_SSL_CXX
314+
# ifdef WITH_OPENSSL
310315
SSL_CTX* Socket::_ssl_ctx = nullptr;
311316

312317
/*static*/ int Socket::openssl_password_callback(char *buf, int size, int rwflag, void *userdata) /* pem_passwd_cb, 1.1.0+ */
@@ -322,9 +327,9 @@ SSL_CTX* Socket::_ssl_ctx = nullptr;
322327
buf[size - 1] = '\0';
323328
return static_cast<int>(strlen(buf));
324329
}
325-
#endif
330+
# endif /* WITH_OPENSSL */
326331

327-
#ifdef WITH_NSS
332+
# ifdef WITH_NSS
328333
static void nss_error(const char* funcname)
329334
{
330335
char buffer[256];
@@ -423,20 +428,25 @@ static void nss_error(const char* funcname)
423428
std::cerr << "SSL handshake done successfully with server " << sock->_host << std::endl;
424429
}
425430
}
426-
#endif /* WITH_NSS */
431+
# endif /* WITH_NSS */
432+
#endif /* WITH_SSL_CXX */
427433

428434
Socket::Socket():
429435
_sock(INVALID_SOCKET),
430-
#if defined(WITH_OPENSSL) || defined(WITH_NSS)
436+
#ifdef WITH_SSL_CXX
437+
# if defined(WITH_OPENSSL) || defined(WITH_NSS)
431438
_ssl(nullptr),
439+
# endif
432440
#endif
433441
_debugConnect(false),
434442
_tv(),
435443
_port(NUT_PORT),
436444
_force_ssl(0)
437-
#if defined(WITH_OPENSSL) || defined(WITH_NSS)
445+
#ifdef WITH_SSL_CXX
446+
# if defined(WITH_OPENSSL) || defined(WITH_NSS)
438447
,_certverify(-1)
439-
#endif
448+
# endif
449+
#endif /* WITH_SSL_CXX */
440450
{
441451
_tv.tv_sec = -1;
442452
_tv.tv_usec = 0;
@@ -761,17 +771,20 @@ void Socket::connect(const std::string& host, uint16_t port)
761771

762772
void Socket::disconnect()
763773
{
764-
#if defined(WITH_OPENSSL) || defined(WITH_NSS)
774+
#ifdef WITH_SSL_CXX
775+
# if defined(WITH_OPENSSL) || defined(WITH_NSS)
765776
if (_ssl) {
766-
# ifdef WITH_OPENSSL
777+
# ifdef WITH_OPENSSL
767778
SSL_shutdown(_ssl);
768779
SSL_free(_ssl);
769-
# elif defined(WITH_NSS)
780+
# elif defined(WITH_NSS)
770781
PR_Close(_ssl);
771-
# endif
782+
# endif
772783
_ssl = nullptr;
773784
}
774-
#endif
785+
# endif
786+
#endif /* WITH_SSL_CXX */
787+
775788
if(_sock != INVALID_SOCKET)
776789
{
777790
::closesocket(_sock);
@@ -782,7 +795,7 @@ void Socket::disconnect()
782795

783796
bool Socket::isSSL()const
784797
{
785-
#if defined(WITH_OPENSSL) || defined(WITH_NSS)
798+
#if defined (WITH_SSL_CXX) && (defined(WITH_OPENSSL) || defined(WITH_NSS))
786799
return _ssl != nullptr;
787800
#else
788801
return false;
@@ -793,7 +806,7 @@ void Socket::setSSLConfig_OpenSSL(bool force_ssl, int certverify, const std::str
793806
{
794807
_force_ssl = force_ssl;
795808

796-
#if defined(WITH_OPENSSL)
809+
#if defined(WITH_SSL_CXX) && defined(WITH_OPENSSL)
797810
_certverify = certverify;
798811
/* These need to be saved at least to handle callbacks
799812
* (to see if errors are fatal or ignorable)
@@ -821,7 +834,7 @@ void Socket::setSSLConfig_NSS(bool force_ssl, int certverify, const std::string&
821834
{
822835
_force_ssl = force_ssl;
823836

824-
#if defined(WITH_NSS)
837+
#if defined(WITH_SSL_CXX) && defined(WITH_NSS)
825838
_certverify = certverify;
826839
/* These need to be saved at least to handle NSS callbacks
827840
* (to see if errors are fatal or ignorable)
@@ -851,7 +864,8 @@ void Socket::startTLS()
851864
throw nut::NotConnectedException();
852865
}
853866

854-
#ifdef WITH_OPENSSL
867+
#ifdef WITH_SSL_CXX
868+
# ifdef WITH_OPENSSL
855869
if (!(_ssl_configured & UPSCLI_SSL_CAPS_OPENSSL)) {
856870
if (_debugConnect) std::cerr <<
857871
"[D2] Socket::startTLS(): Not configured for OpenSSL" <<
@@ -863,7 +877,7 @@ void Socket::startTLS()
863877
}
864878
return;
865879
}
866-
#elif defined(WITH_NSS)
880+
# elif defined(WITH_NSS)
867881
if (!(_ssl_configured & UPSCLI_SSL_CAPS_NSS)) {
868882
if (_debugConnect) std::cerr <<
869883
"[D2] Socket::startTLS(): Not configured for NSS" <<
@@ -875,9 +889,9 @@ void Socket::startTLS()
875889
}
876890
return;
877891
}
878-
#endif /* WITH_OPENSSL || WITH_NSS */
892+
# endif /* WITH_OPENSSL || WITH_NSS */
879893

880-
#if defined(WITH_OPENSSL) || defined(WITH_NSS)
894+
# if defined(WITH_OPENSSL) || defined(WITH_NSS)
881895
write("STARTTLS");
882896
std::string res = read();
883897
if (res.substr(0, 11) != "OK STARTTLS") {
@@ -887,17 +901,17 @@ void Socket::startTLS()
887901
}
888902
return;
889903
}
890-
#endif /* WITH_OPENSSL || WITH_NSS */
904+
# endif /* WITH_OPENSSL || WITH_NSS */
891905

892-
#ifdef WITH_OPENSSL
906+
# ifdef WITH_OPENSSL
893907
if (!_ssl_ctx) {
894-
# if OPENSSL_VERSION_NUMBER < 0x10100000L
908+
# if OPENSSL_VERSION_NUMBER < 0x10100000L
895909
SSL_load_error_strings();
896910
SSL_library_init();
897911
_ssl_ctx = SSL_CTX_new(SSLv23_client_method());
898-
# else
912+
# else
899913
_ssl_ctx = SSL_CTX_new(TLS_client_method());
900-
# endif
914+
# endif
901915
if (!_ssl_ctx) {
902916
throw nut::SSLException_OpenSSL("Cannot create SSL context");
903917
}
@@ -916,17 +930,17 @@ void Socket::startTLS()
916930
throw nut::SSLException_OpenSSL("Failed to load client certificate file");
917931
}
918932
if (!_key_pass.empty()) {
919-
# if OPENSSL_VERSION_NUMBER < 0x10100000L
933+
# if OPENSSL_VERSION_NUMBER < 0x10100000L
920934
throw nut::SSLException_OpenSSL("Private key password support not implemented for OpenSSL < 1.1 yet");
921-
# else
935+
# else
922936
/* OpenSSL 1.1.0+
923937
* https://docs.openssl.org/3.5/man3/SSL_CTX_set_default_passwd_cb/#return-values
924938
*/
925939
/* 1. Set the callback function */
926940
SSL_CTX_set_default_passwd_cb(_ssl_ctx, openssl_password_callback);
927941
/* 2. Set the userdata to the password string */
928942
SSL_CTX_set_default_passwd_cb_userdata(_ssl_ctx, const_cast<void *>(static_cast<const void *>(_key_pass.c_str())));
929-
# endif
943+
# endif
930944
}
931945
if (SSL_CTX_use_PrivateKey_file(_ssl_ctx, _key_file.empty() ? _cert_file.c_str() : _key_file.c_str(), SSL_FILETYPE_PEM) != 1) {
932946
throw nut::SSLException_OpenSSL("Failed to load client private key file");
@@ -948,7 +962,7 @@ void Socket::startTLS()
948962
throw nut::SSLException_OpenSSL(std::string("SSL connection failed: ") + errbuf);
949963
}
950964

951-
#elif defined(WITH_NSS)
965+
# elif defined(WITH_NSS)
952966
/* NSS implementation following upsclient.c logic */
953967
static bool nss_initialized = false;
954968

@@ -1025,6 +1039,7 @@ void Socket::startTLS()
10251039
disconnect();
10261040
throw nut::SSLException_NSS("Handshake failed");
10271041
}
1042+
# endif /* WITH_NSS */
10281043
#else
10291044
if (_debugConnect) std::cerr <<
10301045
"[D2] Socket::startTLS(): SSL support not compiled in" <<
@@ -1034,7 +1049,7 @@ void Socket::startTLS()
10341049
disconnect();
10351050
throw nut::SSLException("SSL support not compiled in");
10361051
}
1037-
#endif
1052+
#endif /* WITH_SSL_CXX */
10381053
}
10391054

10401055
bool Socket::isConnected()const
@@ -1061,7 +1076,7 @@ size_t Socket::read(void* buf, size_t sz)
10611076
}
10621077

10631078
ssize_t res;
1064-
#if defined(WITH_OPENSSL) || defined(WITH_NSS)
1079+
#if defined(WITH_SSL_CXX) && (defined(WITH_OPENSSL) || defined(WITH_NSS))
10651080
if (_ssl) {
10661081
# ifdef WITH_OPENSSL
10671082
res = SSL_read(_ssl, buf, static_cast<int>(sz));
@@ -1102,7 +1117,7 @@ size_t Socket::write(const void* buf, size_t sz)
11021117
}
11031118

11041119
ssize_t res;
1105-
#if defined(WITH_OPENSSL) || defined(WITH_NSS)
1120+
#if defined(WITH_SSL_CXX) && (defined(WITH_OPENSSL) || defined(WITH_NSS))
11061121
if (_ssl) {
11071122
# ifdef WITH_OPENSSL
11081123
res = SSL_write(_ssl, buf, static_cast<int>(sz));
@@ -1320,14 +1335,14 @@ TcpClient::~TcpClient()
13201335
{
13211336
int ret = UPSCLI_SSL_CAPS_NONE;
13221337

1323-
#ifdef WITH_SSL
1338+
#ifdef WITH_SSL_CXX
13241339
# ifdef WITH_OPENSSL
13251340
ret |= UPSCLI_SSL_CAPS_OPENSSL;
13261341
# endif
13271342
# ifdef WITH_NSS
13281343
ret |= UPSCLI_SSL_CAPS_NSS;
13291344
# endif
1330-
#endif /* WITH_SSL */
1345+
#endif /* WITH_SSL_CXX */
13311346

13321347
return ret;
13331348
}

clients/nutclient.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,25 @@
2929
/* Begin of C++ nutclient library declaration */
3030
#ifdef __cplusplus
3131

32-
#ifdef WITH_OPENSSL
32+
#ifdef WITH_SSL_CXX
33+
# ifdef WITH_OPENSSL
3334
# include <openssl/err.h>
3435
# include <openssl/ssl.h>
35-
#elif defined(WITH_NSS) /* not WITH_OPENSSL */
36+
# elif defined(WITH_NSS) /* not WITH_OPENSSL */
3637
# include <nss.h>
3738
# include <ssl.h>
38-
#endif /* WITH_OPENSSL | WITH_NSS */
39+
# endif /* WITH_OPENSSL | WITH_NSS */
40+
/*
41+
// This should not be needed if macros in code are all in the right places:
42+
#else
43+
# ifdef WITH_OPENSSL
44+
# undefine WITH_OPENSSL
45+
# endif
46+
# ifdef WITH_NSS
47+
# undefine WITH_NSS
48+
# endif
49+
*/
50+
#endif /* WITH_SSL_CXX */
3951

4052
#include <string>
4153
#include <vector>

conf/ups.conf.sample

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ maxretry = 3
131131
# user, group: OPTIONAL. Overrides the compiled-in (also global-section,
132132
# when used in driver section) default unprivileged user/group
133133
# name for NUT device driver. Impacts access rights used for
134-
# the socket file access (group) and communication ports (user).
134+
# the socket file access (group) and communication ports (user
135+
# and its default group; you may want to add that user in the
136+
# operating system to `dialout` group to access serial ports).
135137
#
136138
# synchronous: OPTIONAL. The driver work by default in asynchronous
137139
# mode (like *no*) with fallback to synchronous if sending

0 commit comments

Comments
 (0)