Default repeater guest login off and expose guest sessions

This commit is contained in:
mikecarper committed 2026-10-21 11:11:07 -07:00
1 parent a587a391a5
commit c433b40e51
26 files changed
+1529 -5

No files matched your search

+16
View File
@@ -895,6 +895,22 @@ bool ClientACL::clear() {
return true;
}
int ClientACL::removeGuestClients() {
int retained = 0;
const int previous_count = num_clients;
for (int i = 0; i < previous_count; ++i) {
if ((clients[i].permissions & PERM_ACL_ROLE_MASK) == PERM_ACL_GUEST) continue;
if (retained != i) clients[retained] = clients[i];
++retained;
}
const int removed = previous_count - retained;
if (removed != 0) {
memset(clients + retained, 0, sizeof(ClientInfo) * size_t(removed));
num_clients = retained;
}
return removed;
}
ClientInfo* ClientACL::getClient(const uint8_t* pubkey, int key_len) {
if (pubkey == NULL || key_len <= 0 || key_len > PUB_KEY_SIZE) return NULL;
for (int i = 0; i < num_clients; i++) {
+1
View File
@@ -124,6 +124,7 @@ public:
void protectExplicitPermissions(bool protect = true) { protect_explicit_permissions = protect; }
bool save(FILESYSTEM* _fs, bool (*filter)(ClientInfo*)=NULL);
bool clear();
int removeGuestClients(); // transient sessions only; never writes flash
// Authenticate the sender/password first, then call this before allocating
// or mutating a client. A true result means any required replay high-water
+11 -1
View File
@@ -232,7 +232,7 @@ bool rowKey(const char* key, size_t length, const char* family) {
}
enum class CommandKind : uint8_t {
Filter, Row, Advert, Schedule, SecondarySchedule, Radio, RadioStatus, Cross, Setting
Filter, Row, Advert, Schedule, SecondarySchedule, Radio, RadioStatus, Cross, Guest, Guests, Setting
};
struct CommandFamily {
@@ -266,6 +266,8 @@ const CommandFamily* commandFamily(const char* key, size_t length) {
{"radio2.status", CommandKind::RadioStatus},
{"radio2.timing", CommandKind::RadioStatus},
{"radio2.cross", CommandKind::Cross},
{"guest", CommandKind::Guest},
{"guests", CommandKind::Guests},
{"flood.channel.data", CommandKind::Setting},
{"flood.channel.data.hops", CommandKind::Setting},
{"flood.max", CommandKind::Setting},
@@ -592,6 +594,14 @@ bool FleetCommand::commandAllowed(const char* command) {
return true;
}
if (clear) return false;
if (kind == CommandKind::Guest) {
return get ? !*arguments : set && (!strcmp(arguments, "on") || !strcmp(arguments, "off"));
}
if (kind == CommandKind::Guests) {
uint32_t page = 0;
return get && (!*arguments || (arguments[0] >= '1' && arguments[0] <= '9'
&& boundedIntegerToken(arguments, strlen(arguments), 65535, page)));
}
const bool scheduled_secondary = kind == CommandKind::SecondarySchedule;
if (kind == CommandKind::Schedule || scheduled_secondary) {
+84
View File
@@ -0,0 +1,84 @@
#pragma once
#include "ClientACL.h"
#include <Utils.h>
#include <stdio.h>
#include <string.h>
namespace mesh {
namespace cli {
inline bool isLiveGuest(const ClientInfo& client) {
if ((client.permissions & PERM_ACL_ROLE_MASK) != PERM_ACL_GUEST) return false;
uint8_t key_bits = 0, secret_bits = 0;
for (size_t i = 0; i < PUB_KEY_SIZE; ++i) {
key_bits |= client.id.pub_key[i];
secret_bits |= client.shared_secret[i];
}
return key_bits != 0 && secret_bits != 0;
}
// Call after the repeater's admin authorization check. One complete public
// key per page leaves room for activity information in a radio CLI reply.
inline bool handleGuestsGet(const ClientACL& acl, const char* command,
char* reply, size_t capacity) {
static const char prefix[] = "get guests";
const size_t prefix_len = sizeof(prefix) - 1;
if (command == nullptr || strncmp(command, prefix, prefix_len) != 0) return false;
const char* arg = command + prefix_len;
if (*arg && *arg != ' ' && *arg != '\t' && *arg != '\r' && *arg != '\n') return false;
if (reply == nullptr || capacity == 0) return true;
while (*arg == ' ' || *arg == '\t') ++arg;
unsigned page = 1;
if (*arg >= '0' && *arg <= '9') {
page = 0;
do {
page = page * 10 + unsigned(*arg++ - '0');
if (page > 65535) break;
} while (*arg >= '0' && *arg <= '9');
}
while (*arg == ' ' || *arg == '\t' || *arg == '\r' || *arg == '\n') ++arg;
if (*arg || page == 0 || page > 65535) {
snprintf(reply, capacity, "Err - usage: get guests [page]");
return true;
}
unsigned total = 0;
for (int i = 0; i < acl.getNumClients(); ++i) {
if (isLiveGuest(*acl.getClientByIdx(i))) ++total;
}
if (total == 0 && page == 1) {
snprintf(reply, capacity, "Guests: empty");
return true;
}
const unsigned pages = total == 0 ? 1 : total;
if (page > pages) {
snprintf(reply, capacity, "Err - guest page range: 1-%u", pages);
return true;
}
unsigned index = 0;
for (int i = 0; i < acl.getNumClients(); ++i) {
const auto* client = acl.getClientByIdx(i);
if (!isLiveGuest(*client) || ++index != page) continue;
char key[PUB_KEY_SIZE * 2 + 1];
Utils::toHex(key, client->id.pub_key, PUB_KEY_SIZE);
const int needed = snprintf(nullptr, 0, "Guests %u/%u (%u)\n%s last=%lu",
page, pages, total, key,
static_cast<unsigned long>(client->last_activity));
// A small caller buffer must never turn a complete key into a prefix.
if (needed < 0 || size_t(needed) >= capacity) {
snprintf(reply, capacity, "Err - guest reply too small");
} else {
snprintf(reply, capacity, "Guests %u/%u (%u)\n%s last=%lu",
page, pages, total, key,
static_cast<unsigned long>(client->last_activity));
}
return true;
}
return true;
}
} // namespace cli
} // namespace mesh
+83
View File
@@ -0,0 +1,83 @@
#include "GuestLoginSettings.h"
#include "CheckedSettingsFile.h"
#include "FilePresence.h"
#include "PersistentStoreFormat.h"
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
#include "AtomicFileWriter.h"
#else
#include "ContactFileTransaction.h"
#endif
namespace mesh {
namespace {
constexpr char Path[] = "/guest_login";
constexpr size_t ImageSize = 12;
void encode(uint8_t (&image)[ImageSize], bool enabled) {
memset(image, 0, sizeof(image));
memcpy(image, "GLG1", 4);
image[4] = uint8_t(enabled);
storage::writeLE32(image + 8, storage::updateCRC32(0xffffffff, image, 8));
}
bool valid(const uint8_t (&image)[ImageSize]) {
return memcmp(image, "GLG1", 4) == 0 && image[4] <= 1
&& image[5] == 0 && image[6] == 0 && image[7] == 0
&& storage::readLE32(image + 8)
== storage::updateCRC32(0xffffffff, image, 8);
}
}
bool GuestLoginSettings::begin(FILESYSTEM* fs) {
fs_ = fs;
enabled_ = false;
healthy_ = false;
if (!fs_) return false;
#if defined(ESP32_PLATFORM) || defined(RP2040_PLATFORM)
if (!ContactFileTransaction::recover(fs_, Path, filePresence<FILESYSTEM>)) return false;
#endif
bool present = false;
if (!filePresence(fs_, Path, present)) return false;
if (!present) {
healthy_ = true;
return true;
}
uint8_t image[ImageSize];
const auto result = storage::readFixedSettingsFileResult(fs_, Path, image, sizeof(image));
if (result == storage::FixedSettingsRead::Unreadable) return false;
// An explicit administrator setting may replace a complete malformed record;
// an I/O error must first be reconciled by a successful reload.
healthy_ = true;
if (result != storage::FixedSettingsRead::Complete || !valid(image)) return false;
enabled_ = image[4] != 0;
return true;
}
bool GuestLoginSettings::setEnabled(bool enabled) {
if (!healthy_ || !fs_) return false;
uint8_t image[ImageSize];
encode(image, enabled);
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
AtomicFileWriter writer(fs_, Path);
const bool wrote = writer && writer.write(image, sizeof(image)) == sizeof(image);
const bool saved = writer.commit(wrote);
if (writer.outcome() == AtomicFileWriter::Outcome::Indeterminate) healthy_ = false;
if (!saved) return false;
#else
ContactFileTransaction writer(fs_, Path, filePresence<FILESYSTEM>);
if (!writer || writer.write(image, sizeof(image)) != sizeof(image)) {
if (writer.outcome() == ContactFileTransaction::Outcome::Indeterminate) healthy_ = false;
return false;
}
writer.commit();
if (writer.outcome() == ContactFileTransaction::Outcome::NotCommitted) return false;
#endif
uint8_t verified[ImageSize];
healthy_ = storage::readFixedSettingsFile(fs_, Path, verified, sizeof(verified))
&& memcmp(image, verified, sizeof(image)) == 0;
if (!healthy_) return false;
enabled_ = enabled;
return true;
}
} // namespace mesh
+22
View File
@@ -0,0 +1,22 @@
#pragma once
#include <helpers/IdentityStore.h>
namespace mesh {
// Stored separately so upgrading existing repeaters leaves guest access off.
class GuestLoginSettings {
FILESYSTEM* fs_ = nullptr;
bool enabled_ = false;
bool healthy_ = false;
public:
// Missing settings are a valid, unwritten default. Invalid records fail closed.
bool begin(FILESYSTEM* fs);
bool enabled() const { return healthy_ && enabled_; }
bool healthy() const { return healthy_; }
// Changes take effect only after the published record is read back.
bool setEnabled(bool enabled);
};
} // namespace mesh