Skip to content

Commit 621cb5d

Browse files
committed
Add 'drop_on_absent_certificate' listener option
1 parent 6c0e795 commit 621cb5d

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

configfileparser.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ ConfigFileParser::ConfigFileParser(const std::string &path) :
250250
validListenKeys.insert("minimum_tls_version");
251251
validListenKeys.insert("overload_mode");
252252
validListenKeys.insert("acme_redirect_url");
253+
validListenKeys.insert("drop_on_absent_certificate");
253254

254255
validBridgeKeys.insert("local_username");
255256
validBridgeKeys.insert("remote_username");
@@ -449,7 +450,20 @@ void ConfigFileParser::loadFile(bool test)
449450
if (curParseLevel == ConfigParseLevel::Listen)
450451
{
451452
curListener->isValid();
452-
tmpSettings.listeners.push_back(curListener);
453+
454+
if (curListener->dropListener())
455+
{
456+
if (test)
457+
{
458+
Logger::getInstance()->log(LOG_NOTICE)
459+
<< "Approved missing certificates: dropping " << curListener->getProtocolName()
460+
<< " listener, port " << curListener->port;
461+
}
462+
}
463+
else
464+
{
465+
tmpSettings.listeners.push_back(curListener);
466+
}
453467
curListener.reset();
454468
}
455469
else if (curParseLevel == ConfigParseLevel::Bridge)
@@ -495,12 +509,10 @@ void ConfigFileParser::loadFile(bool test)
495509
}
496510
else if (testKeyValidity(key, "fullchain", validListenKeys))
497511
{
498-
checkFileExistsAndReadable("SSL fullchain", value, 1024*1024);
499512
curListener->sslFullchain = value;
500513
}
501514
if (testKeyValidity(key, "privkey", validListenKeys))
502515
{
503-
checkFileExistsAndReadable("SSL privkey", value, 1024*1024);
504516
curListener->sslPrivkey = value;
505517
}
506518
if (testKeyValidity(key, "inet_protocol", validListenKeys))
@@ -578,6 +590,10 @@ void ConfigFileParser::loadFile(bool test)
578590
{
579591
curListener->acmeRedirectURL = valueTrimmed;
580592
}
593+
if (testKeyValidity(key, "drop_on_absent_certificate", validListenKeys))
594+
{
595+
curListener->dropOnAbsentCertificates = stringTruthiness(value);
596+
}
581597

582598
testCorrectNumberOfValues(key, number_of_expected_values, values);
583599
continue;

configfileparser.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ class ConfigFileParser
4444

4545
void static testCorrectNumberOfValues(const std::string &key, size_t expected_values, const std::vector<std::string> &values);
4646
bool testKeyValidity(const std::string &key, const std::string &matchKey, const std::set<std::string> &validKeys) const;
47+
48+
public:
4749
void static checkFileExistsAndReadable(const std::string &key, const std::string &pathToCheck, ssize_t max_size = std::numeric_limits<ssize_t>::max());
4850
void static checkFileOrItsDirWritable(const std::string &filepath);
4951
void static checkDirExists(const std::string &key, const std::string &dir);
5052

51-
public:
5253
ConfigFileParser(const std::string &path);
5354
void loadFile(bool test);
5455
std::list<std::string> readFileRecursively(const std::string &path) const;

listener.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ See LICENSE for license details.
1515
#include "utils.h"
1616
#include "exceptions.h"
1717
#include "logger.h"
18+
#include "configfileparser.h"
1819

1920
void Listener::isValid()
2021
{
@@ -33,7 +34,13 @@ void Listener::isValid()
3334
throw ConfigFileException("An SSL listener can't have an acme_redirect_url.");
3435
}
3536

36-
testSsl(sslFullchain, sslPrivkey);
37+
if (!dropListener())
38+
{
39+
ConfigFileParser::checkFileExistsAndReadable("SSL fullchain", sslFullchain, 1024*1024);
40+
ConfigFileParser::checkFileExistsAndReadable("SSL privkey", sslPrivkey, 1024*1024);
41+
testSsl(sslFullchain, sslPrivkey);
42+
}
43+
3744
testSslVerifyLocations(clientVerificationCaFile, clientVerificationCaDir, "Loading client_verification_ca_dir/client_verification_ca_file failed.");
3845
}
3946
else
@@ -45,6 +52,11 @@ void Listener::isValid()
4552
else
4653
port = 1883;
4754
}
55+
56+
if (dropOnAbsentCertificates)
57+
{
58+
throw ConfigFileException("Using drop_on_absent_certificate is only valid on SSL listeners; define privkey and fullchain.");
59+
}
4860
}
4961

5062
if ((!clientVerificationCaDir.empty() || !clientVerificationCaFile.empty()) && !isSsl())
@@ -168,6 +180,14 @@ X509ClientVerification Listener::getX509ClientVerficationMode() const
168180
return result;
169181
}
170182

183+
bool Listener::dropListener() const
184+
{
185+
if (!dropOnAbsentCertificates || !isSsl())
186+
return false;
187+
188+
return access(sslPrivkey.c_str(), R_OK) != 0 && access(sslFullchain.c_str(), R_OK) != 0;
189+
}
190+
171191
std::string Listener::getBindAddress(ListenerProtocol p)
172192
{
173193
if (p == ListenerProtocol::IPv4)

listener.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ struct Listener
5050
std::optional<std::string> acmeRedirectURL;
5151
TLSVersion minimumTlsVersion = TLSVersion::TLSv1_1;
5252
std::optional<OverloadMode> overloadMode;
53+
bool dropOnAbsentCertificates = false;
5354

5455
void isValid();
5556
bool isSsl() const;
@@ -58,6 +59,7 @@ struct Listener
5859
std::string getProtocolName() const;
5960
void loadCertAndKeyFromConfig();
6061
X509ClientVerification getX509ClientVerficationMode() const;
62+
bool dropListener() const;
6163

6264
std::string getBindAddress(ListenerProtocol p);
6365
};

0 commit comments

Comments
 (0)