Skip to content

Commit

Permalink
Fix denying wills on authentication failure
Browse files Browse the repository at this point in the history
  • Loading branch information
halfgaar committed Oct 16, 2023
1 parent 4b8f4fe commit 87353f7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
14 changes: 10 additions & 4 deletions client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -883,11 +883,16 @@ void Client::setClientProperties(bool connectPacketSeen, uint16_t keepalive, uin
this->supportsRetained = supportsRetained;
}

void Client::setWill(WillPublish &&willPublish)
void Client::stageWill(WillPublish &&willPublish)
{
this->willPublish = std::make_shared<WillPublish>(std::move(willPublish));
this->willPublish->client_id = this->clientid;
this->willPublish->username = this->username;
this->stagedWillPublish = std::make_shared<WillPublish>(std::move(willPublish));
this->stagedWillPublish->client_id = this->clientid;
this->stagedWillPublish->username = this->username;
}

void Client::setWillFromStaged()
{
this->willPublish = std::move(stagedWillPublish);
}

void Client::assignSession(std::shared_ptr<Session> &session)
Expand Down Expand Up @@ -942,6 +947,7 @@ std::chrono::seconds Client::getSecondsTillKeepAliveAction() const
void Client::clearWill()
{
willPublish.reset();
stagedWillPublish.reset();

if (session)
session->clearWill();
Expand Down
5 changes: 4 additions & 1 deletion client.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class Client
uint16_t keepalive = 10;
bool clean_start = false;

std::shared_ptr<WillPublish> stagedWillPublish;
std::shared_ptr<WillPublish> willPublish;

const int epoll_fd;
Expand Down Expand Up @@ -139,7 +140,8 @@ class Client
uint32_t maxOutgoingPacketSize, uint16_t maxOutgoingTopicAliasValue);
void setClientProperties(bool connectPacketSeen, uint16_t keepalive, uint32_t maxOutgoingPacketSize, uint16_t maxOutgoingTopicAliasValue, bool supportsRetained);
void setWill(const std::string &topic, const std::string &payload, bool retain, uint8_t qos);
void setWill(WillPublish &&willPublish);
void stageWill(WillPublish &&willPublish);
void setWillFromStaged();
void clearWill();
void setAuthenticated(bool value) { authenticated = value;}
bool getAuthenticated() { return authenticated; }
Expand All @@ -148,6 +150,7 @@ class Client
const std::string &getUsername() const { return this->username; }
std::string &getMutableUsername();
std::shared_ptr<WillPublish> &getWill() { return this->willPublish; }
const std::shared_ptr<WillPublish> &getStagedWill() { return this->stagedWillPublish; }
void assignSession(std::shared_ptr<Session> &session);
std::shared_ptr<Session> getSession();
void setDisconnectReason(const std::string &reason);
Expand Down
5 changes: 1 addition & 4 deletions mqttpacket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -875,10 +875,7 @@ void MqttPacket::handleConnect()

if (settings.willsEnabled && connectData.will_flag)
{
if (authentication.aclCheck(connectData.willpublish, connectData.willpublish.payload, AclAccess::register_will) == AuthResult::success)
{
sender->setWill(std::move(connectData.willpublish));
}
sender->stageWill(std::move(connectData.willpublish));
}

// Stage connack, for immediate or delayed use when auth succeeds.
Expand Down
7 changes: 7 additions & 0 deletions threaddata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ void ThreadData::continuationOfAuthentication(std::shared_ptr<Client> &client, A
if (!returnData.empty())
client->addAuthReturnDataToStagedConnAck(returnData);

const std::shared_ptr<WillPublish> will = client->getStagedWill();

if (will && authentication.aclCheck(*will, will->payload, AclAccess::register_will) == AuthResult::success)
{
client->setWillFromStaged();
}

client->sendConnackSuccess();
subscriptionStore->registerClientAndKickExistingOne(client);
}
Expand Down

0 comments on commit 87353f7

Please sign in to comment.