From c9322f5759d64975b3fe2965a9e5483e70fb09e0 Mon Sep 17 00:00:00 2001 From: agessaman Date: Wed, 2 Dec 2026 07:42:24 -0800 Subject: [PATCH 1/2] Add a read-only password for repeaters and sensors ro.password grants the read-only role at login. These logins are never saved, cannot evict saved ACL entries, and on a sensor get no location. Sensors also honor guest.password now (blank disables guest login). --- docs/cli_commands.md | 16 ++++++++ examples/simple_repeater/MyMesh.cpp | 13 +++++-- examples/simple_sensor/SensorMesh.cpp | 53 +++++++++++++++++++++++---- examples/simple_sensor/SensorMesh.h | 2 +- src/helpers/ClientACL.cpp | 8 ++-- src/helpers/ClientACL.h | 4 +- src/helpers/CommonCLI.cpp | 38 +++++++++++++++---- src/helpers/CommonCLI.h | 3 ++ 8 files changed, 113 insertions(+), 24 deletions(-) diff --git a/docs/cli_commands.md b/docs/cli_commands.md index 4c88ee329..5a4b3c0b4 100644 --- a/docs/cli_commands.md +++ b/docs/cli_commands.md @@ -436,6 +436,22 @@ Refer to the node's manual for the correct settings to use. --- +#### View or change this node's read-only password +**Usage:** +- `get ro.password` +- `set ro.password ` + +**Parameters:** +- `password`: Read-only password, must differ from the admin and guest passwords + +**Default:** `` (read-only logins disabled) + +**Note:** Repeater and Sensor only. Logins with this password get the read-only role (full telemetry, subscriptions) but are not saved to the ACL, so changing it revokes access after the next reboot. On a sensor, location is withheld from these logins. + +**Note:** On a sensor, a blank guest password disables guest logins. + +--- + #### View or change this node's owner info **Usage:** - `get owner.info` diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index 3d29caf96..a7889970d 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -114,6 +114,8 @@ uint8_t MyMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* secr uint8_t perms; if (strcmp((char *)data, _prefs.password) == 0) { // check for valid admin password perms = PERM_ACL_ADMIN; + } else if (_prefs.ro_password[0] && strcmp((char *)data, _prefs.ro_password) == 0) { // check read-only password + perms = PERM_ACL_READ_ONLY | PERM_ACL_TRANSIENT; } else if (strcmp((char *)data, _prefs.guest_password) == 0) { // check guest password perms = PERM_ACL_GUEST; } else { @@ -123,7 +125,8 @@ uint8_t MyMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* secr return 0; } - client = acl.putClient(sender, 0); // add to contacts (if not already known) + bool shared_pw = perms != PERM_ACL_ADMIN; + client = acl.putClient(sender, 0, shared_pw); // add to contacts (if not already known) if (client == NULL) return 0; if (sender_timestamp <= client->last_timestamp) { MESH_DEBUG_PRINTLN("Possible login replay attack!"); @@ -132,11 +135,13 @@ uint8_t MyMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* secr MESH_DEBUG_PRINTLN("Login success!"); client->last_timestamp = sender_timestamp; - client->permissions &= ~0x03; - client->permissions |= perms; + if (!shared_pw || client->permissions == 0 || (client->permissions & PERM_ACL_TRANSIENT)) { // shared passwords don't change saved entries + client->permissions &= ~(PERM_ACL_ROLE_MASK | PERM_ACL_TRANSIENT); + client->permissions |= perms; + } memcpy(client->shared_secret, secret, PUB_KEY_SIZE); - if (perms != PERM_ACL_GUEST) { // keep number of FS writes to a minimum + if (!shared_pw) { // keep number of FS writes to a minimum dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY); } } diff --git a/examples/simple_sensor/SensorMesh.cpp b/examples/simple_sensor/SensorMesh.cpp index adb2cd9af..b621efe42 100644 --- a/examples/simple_sensor/SensorMesh.cpp +++ b/examples/simple_sensor/SensorMesh.cpp @@ -96,6 +96,21 @@ static const uint8_t* findTelemValueRaw(const uint8_t* buf, uint8_t size, uint8_ return NULL; // not found } +static uint8_t copyTelemWithoutGPS(uint8_t* dest, const uint8_t* buf, uint8_t size) { + uint8_t i = 0, n = 0; + + while (i + 2 < size) { + uint8_t sz = 2 + LPPData::getDataSize(buf[i + 1]); // channel, type, data + if (i + sz > size) break; // truncated record + if (buf[i + 1] != LPP_GPS) { + memcpy(&dest[n], &buf[i], sz); + n += sz; + } + i += sz; + } + return n; +} + static float findTelemValue(const uint8_t* buf, uint8_t size, uint8_t channel, uint8_t type, float def_value) { uint8_t i = 0; @@ -191,7 +206,12 @@ uint8_t SensorMesh::handleRequest(ClientInfo* from, uint32_t sender_timestamp, u telemetry.reset(); telemetry.addVoltage(TELEM_CHANNEL_SELF, (float)board.getBattMilliVolts() / 1000.0f); // query other sensors -- target specific - sensors.querySensors(0xFF & perm_mask, telemetry); // allow all telemetry permissions for admin or guest + if ((perms & PERM_ACL_ROLE_MASK) == PERM_ACL_GUEST) { + perm_mask = 0x00; // just base telemetry allowed + } else if (!from->isAdmin() && (from->permissions & PERM_ACL_TRANSIENT)) { + perm_mask &= ~TELEM_PERM_LOCATION; // shared read-only password, no location + } + sensors.querySensors(perm_mask, telemetry); // TODO: let requester know permissions they have: telemetry.addPresence(TELEM_CHANNEL_SELF, perms); float temperature = board.getMCUTemperature(); if (!isnan(temperature)) { // Supported boards with built-in temperature sensor. ESP32-C3 may return NAN @@ -415,14 +435,22 @@ uint8_t SensorMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* return 0; } } else { - if (strcmp((char *) data, _prefs.password) != 0) { // check for valid admin password + uint8_t perms; + if (strcmp((char *) data, _prefs.password) == 0) { // check for valid admin password + perms = PERM_ACL_ADMIN; + } else if (_prefs.ro_password[0] && strcmp((char *) data, _prefs.ro_password) == 0) { // check read-only password + perms = PERM_ACL_READ_ONLY | PERM_ACL_TRANSIENT; + } else if (_prefs.guest_password[0] && strcmp((char *) data, _prefs.guest_password) == 0) { // check guest password (blank = disabled) + perms = PERM_ACL_GUEST; + } else { #if MESH_DEBUG MESH_DEBUG_PRINTLN("Invalid password: %s", &data[0]); #endif return 0; } - client = acl.putClient(sender, PERM_RECV_ALERTS_HI | PERM_RECV_ALERTS_LO); // add to contacts (if not already known) + bool shared_pw = perms != PERM_ACL_ADMIN; + client = acl.putClient(sender, shared_pw ? 0 : PERM_RECV_ALERTS_HI | PERM_RECV_ALERTS_LO, shared_pw); // add to contacts (if not already known) if (client == NULL) return 0; if (sender_timestamp <= client->last_timestamp) { MESH_DEBUG_PRINTLN("Possible login replay attack!"); @@ -431,10 +459,14 @@ uint8_t SensorMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* MESH_DEBUG_PRINTLN("Login success!"); client->last_timestamp = sender_timestamp; - client->permissions |= PERM_ACL_ADMIN; + if (!shared_pw) { + client->permissions &= ~PERM_ACL_TRANSIENT; + client->permissions |= PERM_ACL_ADMIN; + dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY); + } else if (client->permissions == 0 || (client->permissions & PERM_ACL_TRANSIENT)) { // shared passwords don't change saved entries + client->permissions = perms; + } memcpy(client->shared_secret, secret, PUB_KEY_SIZE); - - dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY); } client->last_activity = getRTCClock()->getCurrentTime(); @@ -1097,11 +1129,16 @@ void SensorMesh::loop() { if (telemHasChanged(c)) { TransportKey scope; if (region_map.getTransportKeysFor(*r, &scope, 1) > 0) { - uint8_t tlen = telemetry.getSize(); + uint8_t tlen; memcpy(reply_data, &c->extra.sensor.push_tag, 4); uint32_t timestamp = getRTCClock()->getCurrentTimeUnique(); memcpy(&reply_data[4], ×tamp, 4); - memcpy(&reply_data[8], telemetry.getBuffer(), tlen); + if (c->permissions & PERM_ACL_TRANSIENT) { + tlen = copyTelemWithoutGPS(&reply_data[8], telemetry.getBuffer(), telemetry.getSize()); // shared read-only password, no location + } else { + tlen = telemetry.getSize(); + memcpy(&reply_data[8], telemetry.getBuffer(), tlen); + } mesh::Packet* reply = createDatagram(PAYLOAD_TYPE_RESPONSE, c->id, c->shared_secret, reply_data, 8 + tlen); if (reply) { diff --git a/examples/simple_sensor/SensorMesh.h b/examples/simple_sensor/SensorMesh.h index 9cac1e2db..213a07ddb 100644 --- a/examples/simple_sensor/SensorMesh.h +++ b/examples/simple_sensor/SensorMesh.h @@ -27,7 +27,7 @@ #include #include -#define PERM_RESERVED1 (1 << 2) +// (1 << 2) is PERM_ACL_TRANSIENT #define PERM_RESERVED2 (1 << 3) #define PERM_RESERVED3 (1 << 4) #define PERM_RESERVED4 (1 << 5) diff --git a/src/helpers/ClientACL.cpp b/src/helpers/ClientACL.cpp index 9e9255234..59ae92782 100644 --- a/src/helpers/ClientACL.cpp +++ b/src/helpers/ClientACL.cpp @@ -61,7 +61,7 @@ void ClientACL::save(FILESYSTEM* fs, bool (*filter)(ClientInfo*)) { for (int i = 0; i < num_clients; i++) { auto c = &clients[i]; - if (c->permissions == 0 || (filter && !filter(c))) continue; // skip deleted entries, or by filter function + if (c->permissions == 0 || (c->permissions & PERM_ACL_TRANSIENT) || (filter && !filter(c))) continue; // skip deleted/transient entries, or by filter function bool success = (file.write(c->id.pub_key, 32) == 32); success = success && (file.write((uint8_t *) &c->permissions, 1) == 1); @@ -94,11 +94,13 @@ ClientInfo* ClientACL::getClient(const uint8_t* pubkey, int key_len) { return NULL; // not found } -ClientInfo* ClientACL::putClient(const mesh::Identity& id, uint8_t init_perms) { +ClientInfo* ClientACL::putClient(const mesh::Identity& id, uint8_t init_perms, bool shared_pw) { uint32_t min_time = 0xFFFFFFFF; ClientInfo* oldest = NULL; for (int i = 0; i < num_clients; i++) { if (id.matches(clients[i].id)) return &clients[i]; // already known + bool unsaved = clients[i].permissions == 0 || (clients[i].permissions & PERM_ACL_TRANSIENT); + if (shared_pw && !unsaved) continue; // shared password logins can only evict other unsaved entries if (!clients[i].isAdmin() && clients[i].last_activity < min_time) { oldest = &clients[i]; min_time = oldest->last_activity; @@ -109,7 +111,7 @@ ClientInfo* ClientACL::putClient(const mesh::Identity& id, uint8_t init_perms) { if (num_clients < MAX_CLIENTS) { c = &clients[num_clients++]; } else { - if (oldest == NULL) return NULL; // every entry is an admin, so don't evict + if (oldest == NULL) return NULL; // every entry is an admin (or saved), so don't evict c = oldest; // evict least active contact } memset(c, 0, sizeof(*c)); diff --git a/src/helpers/ClientACL.h b/src/helpers/ClientACL.h index 6c8d90512..0c6055f96 100644 --- a/src/helpers/ClientACL.h +++ b/src/helpers/ClientACL.h @@ -10,6 +10,8 @@ #define PERM_ACL_READ_WRITE 2 #define PERM_ACL_ADMIN 3 +#define PERM_ACL_TRANSIENT (1 << 2) // granted by a shared password, never saved + #define OUT_PATH_UNKNOWN 0xFF struct ClientInfo { @@ -61,7 +63,7 @@ public: bool clear(); ClientInfo* getClient(const uint8_t* pubkey, int key_len); - ClientInfo* putClient(const mesh::Identity& id, uint8_t init_perms); // returns NULL if table is full and all entries are admins + ClientInfo* putClient(const mesh::Identity& id, uint8_t init_perms, bool shared_pw=false); // returns NULL if table is full and all entries are admins (or saved, if shared_pw) bool applyPermissions(const mesh::LocalIdentity& self_id, const uint8_t* pubkey, int key_len, uint8_t perms); int getNumClients() const { return num_clients; } diff --git a/src/helpers/CommonCLI.cpp b/src/helpers/CommonCLI.cpp index 64fed99b3..cffc3de94 100644 --- a/src/helpers/CommonCLI.cpp +++ b/src/helpers/CommonCLI.cpp @@ -268,10 +268,16 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re } } else if (memcmp(command, "password ", 9) == 0) { // change admin password - StrHelper::strncpy(_prefs->password, &command[9], sizeof(_prefs->password)); - savePrefs(); - sprintf(reply, "password now: "); - StrHelper::strncpy(&reply[14], _prefs->password, 160-15); // echo back just to let admin know for sure!! + char pw[sizeof(_prefs->password)]; + StrHelper::strncpy(pw, &command[9], sizeof(pw)); + if (_prefs->ro_password[0] && strcmp(pw, _prefs->ro_password) == 0) { + strcpy(reply, "Error, same as ro.password"); + } else { + strcpy(_prefs->password, pw); + savePrefs(); + sprintf(reply, "password now: "); + StrHelper::strncpy(&reply[14], _prefs->password, 160-15); // echo back just to let admin know for sure!! + } } else if (memcmp(command, "clear stats", 11) == 0) { _callbacks->clearStats(); strcpy(reply, "(OK - stats reset)"); @@ -484,9 +490,25 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep strcpy(reply, "Error, invalid policy"); } } else if (memcmp(config, "guest.password ", 15) == 0) { - StrHelper::strncpy(_prefs->guest_password, &config[15], sizeof(_prefs->guest_password)); - savePrefs(); - strcpy(reply, "OK"); + char pw[sizeof(_prefs->guest_password)]; + StrHelper::strncpy(pw, &config[15], sizeof(pw)); + if (pw[0] && strcmp(pw, _prefs->ro_password) == 0) { + strcpy(reply, "Error, same as ro.password"); + } else { + strcpy(_prefs->guest_password, pw); + savePrefs(); + strcpy(reply, "OK"); + } + } else if (memcmp(config, "ro.password ", 12) == 0) { + char pw[sizeof(_prefs->ro_password)]; + StrHelper::strncpy(pw, &config[12], sizeof(pw)); + if (pw[0] && (strcmp(pw, _prefs->password) == 0 || strcmp(pw, _prefs->guest_password) == 0)) { + strcpy(reply, "Error, must differ from admin and guest passwords"); + } else { + strcpy(_prefs->ro_password, pw); // blank disables read-only logins + savePrefs(); + strcpy(reply, "OK"); + } } else if (memcmp(config, "prv.key ", 8) == 0) { uint8_t prv_key[PRV_KEY_SIZE]; bool success = mesh::Utils::fromHex(prv_key, PRV_KEY_SIZE, &config[8]); @@ -697,6 +719,8 @@ void CommonCLI::handleGetCmd(uint32_t sender_timestamp, char* command, char* rep } } else if (memcmp(config, "guest.password", 14) == 0) { sprintf(reply, "> %s", _prefs->guest_password); + } else if (memcmp(config, "ro.password", 11) == 0) { + sprintf(reply, "> %s", _prefs->ro_password); } else if (sender_timestamp == 0 && memcmp(config, "prv.key", 7) == 0) { // from serial command line only uint8_t prv_key[PRV_KEY_SIZE]; int len = _callbacks->getSelfId().writeTo(prv_key, PRV_KEY_SIZE); diff --git a/src/helpers/CommonCLI.h b/src/helpers/CommonCLI.h index 24b1fe26b..d05b12ac0 100644 --- a/src/helpers/CommonCLI.h +++ b/src/helpers/CommonCLI.h @@ -38,6 +38,7 @@ public: float rx_delay_base = 0; float tx_delay_factor = 0; char guest_password[16]; + char ro_password[16]; float direct_tx_delay_factor = 0; uint32_t guard; uint8_t sf = 0; @@ -247,6 +248,7 @@ protected: def("name", node_name, sizeof(node_name)); def("pass", password, sizeof(password)); def("guest", guest_password, sizeof(guest_password)); + def("ro_pass", ro_password, sizeof(ro_password)); def("owner", owner_info, sizeof(owner_info)); def("adv_int", advert_interval); def("f_adv_int", flood_advert_interval); @@ -266,6 +268,7 @@ public: node_name[0] = 0; password[0] = 0; guest_password[0] = 0; + ro_password[0] = 0; bridge_secret[0] = 0; owner_info[0] = 0; } From 3513ddfc01a684f0dbdd01c5d6886064c0f65db2 Mon Sep 17 00:00:00 2001 From: agessaman Date: Wed, 2 Dec 2026 08:14:05 -0800 Subject: [PATCH 2/2] Track shared-password logins in RAM, tighten eviction and passwords Mark guest and read-only logins with a RAM-only flag instead of an ACL permission bit, so saved permission bytes keep their meaning. Inserts evict unsaved entries before saved ones. Sensors refuse GPS movement deltas from shared-password subscribers and give promoted admins the default alert flags. The admin, guest and read-only passwords must all differ, and set ro.password with no value clears it. --- docs/cli_commands.md | 1 + examples/simple_repeater/MyMesh.cpp | 7 ++++--- examples/simple_sensor/SensorMesh.cpp | 23 +++++++++++++++++------ examples/simple_sensor/SensorMesh.h | 2 +- src/helpers/ClientACL.cpp | 22 ++++++++++++++-------- src/helpers/ClientACL.h | 5 ++--- src/helpers/CommonCLI.cpp | 12 ++++++------ 7 files changed, 45 insertions(+), 27 deletions(-) diff --git a/docs/cli_commands.md b/docs/cli_commands.md index 5a4b3c0b4..a9d047d42 100644 --- a/docs/cli_commands.md +++ b/docs/cli_commands.md @@ -440,6 +440,7 @@ Refer to the node's manual for the correct settings to use. **Usage:** - `get ro.password` - `set ro.password ` +- `set ro.password` (clears it) **Parameters:** - `password`: Read-only password, must differ from the admin and guest passwords diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index a7889970d..be7d68e35 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -115,7 +115,7 @@ uint8_t MyMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* secr if (strcmp((char *)data, _prefs.password) == 0) { // check for valid admin password perms = PERM_ACL_ADMIN; } else if (_prefs.ro_password[0] && strcmp((char *)data, _prefs.ro_password) == 0) { // check read-only password - perms = PERM_ACL_READ_ONLY | PERM_ACL_TRANSIENT; + perms = PERM_ACL_READ_ONLY; } else if (strcmp((char *)data, _prefs.guest_password) == 0) { // check guest password perms = PERM_ACL_GUEST; } else { @@ -135,9 +135,10 @@ uint8_t MyMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* secr MESH_DEBUG_PRINTLN("Login success!"); client->last_timestamp = sender_timestamp; - if (!shared_pw || client->permissions == 0 || (client->permissions & PERM_ACL_TRANSIENT)) { // shared passwords don't change saved entries - client->permissions &= ~(PERM_ACL_ROLE_MASK | PERM_ACL_TRANSIENT); + if (!shared_pw || client->permissions == 0 || client->shared_login) { // shared passwords don't change saved entries + client->permissions &= ~PERM_ACL_ROLE_MASK; client->permissions |= perms; + client->shared_login = shared_pw; } memcpy(client->shared_secret, secret, PUB_KEY_SIZE); diff --git a/examples/simple_sensor/SensorMesh.cpp b/examples/simple_sensor/SensorMesh.cpp index b621efe42..4759100bc 100644 --- a/examples/simple_sensor/SensorMesh.cpp +++ b/examples/simple_sensor/SensorMesh.cpp @@ -111,6 +111,13 @@ static uint8_t copyTelemWithoutGPS(uint8_t* dest, const uint8_t* buf, uint8_t si return n; } +static bool hasGPSDelta(const uint8_t* deltas, uint8_t len) { + for (uint8_t i = 0; i + 2 < len; i += 2 + LPPData::getDataSize(deltas[i + 1])) { + if (deltas[i + 1] == LPP_GPS) return true; + } + return false; +} + static float findTelemValue(const uint8_t* buf, uint8_t size, uint8_t channel, uint8_t type, float def_value) { uint8_t i = 0; @@ -208,7 +215,7 @@ uint8_t SensorMesh::handleRequest(ClientInfo* from, uint32_t sender_timestamp, u // query other sensors -- target specific if ((perms & PERM_ACL_ROLE_MASK) == PERM_ACL_GUEST) { perm_mask = 0x00; // just base telemetry allowed - } else if (!from->isAdmin() && (from->permissions & PERM_ACL_TRANSIENT)) { + } else if (from->shared_login) { perm_mask &= ~TELEM_PERM_LOCATION; // shared read-only password, no location } sensors.querySensors(perm_mask, telemetry); @@ -283,7 +290,8 @@ uint8_t SensorMesh::handleRequest(ClientInfo* from, uint32_t sender_timestamp, u r = region_map.getDefaultRegion(); } uint8_t reply_len; - if (r && min_deltas_len >= 3 && min_deltas_len <= sizeof(from->extra.sensor.min_deltas)) { + if (r && min_deltas_len >= 3 && min_deltas_len <= sizeof(from->extra.sensor.min_deltas) + && !(from->shared_login && hasGPSDelta(&payload[8], min_deltas_len))) { // no movement alerts without location access from->extra.sensor.scope_region_id = r->id; from->extra.sensor.expiry_timestamp = getRTCClock()->getCurrentTime() + timeout_secs; from->extra.sensor.min_deltas_len = min_deltas_len; @@ -439,7 +447,7 @@ uint8_t SensorMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* if (strcmp((char *) data, _prefs.password) == 0) { // check for valid admin password perms = PERM_ACL_ADMIN; } else if (_prefs.ro_password[0] && strcmp((char *) data, _prefs.ro_password) == 0) { // check read-only password - perms = PERM_ACL_READ_ONLY | PERM_ACL_TRANSIENT; + perms = PERM_ACL_READ_ONLY; } else if (_prefs.guest_password[0] && strcmp((char *) data, _prefs.guest_password) == 0) { // check guest password (blank = disabled) perms = PERM_ACL_GUEST; } else { @@ -459,12 +467,15 @@ uint8_t SensorMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* MESH_DEBUG_PRINTLN("Login success!"); client->last_timestamp = sender_timestamp; + bool saved = client->permissions != 0 && !client->shared_login; if (!shared_pw) { - client->permissions &= ~PERM_ACL_TRANSIENT; + if (!saved) client->permissions |= PERM_RECV_ALERTS_HI | PERM_RECV_ALERTS_LO; // same defaults as a new admin client->permissions |= PERM_ACL_ADMIN; + client->shared_login = false; dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY); - } else if (client->permissions == 0 || (client->permissions & PERM_ACL_TRANSIENT)) { // shared passwords don't change saved entries + } else if (!saved) { // shared passwords don't change saved entries client->permissions = perms; + client->shared_login = true; } memcpy(client->shared_secret, secret, PUB_KEY_SIZE); } @@ -1133,7 +1144,7 @@ void SensorMesh::loop() { memcpy(reply_data, &c->extra.sensor.push_tag, 4); uint32_t timestamp = getRTCClock()->getCurrentTimeUnique(); memcpy(&reply_data[4], ×tamp, 4); - if (c->permissions & PERM_ACL_TRANSIENT) { + if (c->shared_login) { tlen = copyTelemWithoutGPS(&reply_data[8], telemetry.getBuffer(), telemetry.getSize()); // shared read-only password, no location } else { tlen = telemetry.getSize(); diff --git a/examples/simple_sensor/SensorMesh.h b/examples/simple_sensor/SensorMesh.h index 213a07ddb..9cac1e2db 100644 --- a/examples/simple_sensor/SensorMesh.h +++ b/examples/simple_sensor/SensorMesh.h @@ -27,7 +27,7 @@ #include #include -// (1 << 2) is PERM_ACL_TRANSIENT +#define PERM_RESERVED1 (1 << 2) #define PERM_RESERVED2 (1 << 3) #define PERM_RESERVED3 (1 << 4) #define PERM_RESERVED4 (1 << 5) diff --git a/src/helpers/ClientACL.cpp b/src/helpers/ClientACL.cpp index 59ae92782..8a5596270 100644 --- a/src/helpers/ClientACL.cpp +++ b/src/helpers/ClientACL.cpp @@ -61,7 +61,7 @@ void ClientACL::save(FILESYSTEM* fs, bool (*filter)(ClientInfo*)) { for (int i = 0; i < num_clients; i++) { auto c = &clients[i]; - if (c->permissions == 0 || (c->permissions & PERM_ACL_TRANSIENT) || (filter && !filter(c))) continue; // skip deleted/transient entries, or by filter function + if (c->permissions == 0 || c->shared_login || (filter && !filter(c))) continue; // skip deleted/transient entries, or by filter function bool success = (file.write(c->id.pub_key, 32) == 32); success = success && (file.write((uint8_t *) &c->permissions, 1) == 1); @@ -94,14 +94,18 @@ ClientInfo* ClientACL::getClient(const uint8_t* pubkey, int key_len) { return NULL; // not found } -ClientInfo* ClientACL::putClient(const mesh::Identity& id, uint8_t init_perms, bool shared_pw) { - uint32_t min_time = 0xFFFFFFFF; +ClientInfo* ClientACL::putClient(const mesh::Identity& id, uint8_t init_perms, bool shared_login) { + uint32_t min_time = 0xFFFFFFFF, min_unsaved_time = 0xFFFFFFFF; ClientInfo* oldest = NULL; + ClientInfo* oldest_unsaved = NULL; for (int i = 0; i < num_clients; i++) { if (id.matches(clients[i].id)) return &clients[i]; // already known - bool unsaved = clients[i].permissions == 0 || (clients[i].permissions & PERM_ACL_TRANSIENT); - if (shared_pw && !unsaved) continue; // shared password logins can only evict other unsaved entries - if (!clients[i].isAdmin() && clients[i].last_activity < min_time) { + if (clients[i].isAdmin()) continue; + if ((clients[i].permissions == 0 || clients[i].shared_login) && clients[i].last_activity < min_unsaved_time) { + oldest_unsaved = &clients[i]; + min_unsaved_time = oldest_unsaved->last_activity; + } + if (clients[i].last_activity < min_time) { oldest = &clients[i]; min_time = oldest->last_activity; } @@ -111,11 +115,12 @@ ClientInfo* ClientACL::putClient(const mesh::Identity& id, uint8_t init_perms, b if (num_clients < MAX_CLIENTS) { c = &clients[num_clients++]; } else { - if (oldest == NULL) return NULL; // every entry is an admin (or saved), so don't evict - c = oldest; // evict least active contact + c = oldest_unsaved ? oldest_unsaved : (shared_login ? NULL : oldest); // evict unsaved entries first, shared logins never evict saved ones + if (c == NULL) return NULL; // every entry is an admin (or saved), so don't evict } memset(c, 0, sizeof(*c)); c->permissions = init_perms; + c->shared_login = shared_login; c->id = id; c->out_path_len = OUT_PATH_UNKNOWN; return c; @@ -141,6 +146,7 @@ bool ClientACL::applyPermissions(const mesh::LocalIdentity& self_id, const uint8 if (c == NULL) return false; // couldn't put client c->permissions = perms; // update their permissions + c->shared_login = false; self_id.calcSharedSecret(c->shared_secret, pubkey); } return true; diff --git a/src/helpers/ClientACL.h b/src/helpers/ClientACL.h index 0c6055f96..7abea4d86 100644 --- a/src/helpers/ClientACL.h +++ b/src/helpers/ClientACL.h @@ -10,8 +10,6 @@ #define PERM_ACL_READ_WRITE 2 #define PERM_ACL_ADMIN 3 -#define PERM_ACL_TRANSIENT (1 << 2) // granted by a shared password, never saved - #define OUT_PATH_UNKNOWN 0xFF struct ClientInfo { @@ -23,6 +21,7 @@ struct ClientInfo { uint32_t last_timestamp; // by THEIR clock (transient) uint32_t last_req_timestamp; uint32_t last_activity; // by OUR clock (transient) + bool shared_login; // logged in with a guest or read-only password, never saved (transient) union { struct { uint32_t sync_since; // sync messages SINCE this timestamp (by OUR clock) @@ -63,7 +62,7 @@ public: bool clear(); ClientInfo* getClient(const uint8_t* pubkey, int key_len); - ClientInfo* putClient(const mesh::Identity& id, uint8_t init_perms, bool shared_pw=false); // returns NULL if table is full and all entries are admins (or saved, if shared_pw) + ClientInfo* putClient(const mesh::Identity& id, uint8_t init_perms, bool shared_login=false); // returns NULL if table is full and all entries are admins (or saved, if shared_login) bool applyPermissions(const mesh::LocalIdentity& self_id, const uint8_t* pubkey, int key_len, uint8_t perms); int getNumClients() const { return num_clients; } diff --git a/src/helpers/CommonCLI.cpp b/src/helpers/CommonCLI.cpp index cffc3de94..436dc5c94 100644 --- a/src/helpers/CommonCLI.cpp +++ b/src/helpers/CommonCLI.cpp @@ -270,8 +270,8 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re // change admin password char pw[sizeof(_prefs->password)]; StrHelper::strncpy(pw, &command[9], sizeof(pw)); - if (_prefs->ro_password[0] && strcmp(pw, _prefs->ro_password) == 0) { - strcpy(reply, "Error, same as ro.password"); + if (strcmp(pw, _prefs->guest_password) == 0 || (_prefs->ro_password[0] && strcmp(pw, _prefs->ro_password) == 0)) { + strcpy(reply, "Error, must differ from guest and ro passwords"); } else { strcpy(_prefs->password, pw); savePrefs(); @@ -492,16 +492,16 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep } else if (memcmp(config, "guest.password ", 15) == 0) { char pw[sizeof(_prefs->guest_password)]; StrHelper::strncpy(pw, &config[15], sizeof(pw)); - if (pw[0] && strcmp(pw, _prefs->ro_password) == 0) { - strcpy(reply, "Error, same as ro.password"); + if (pw[0] && (strcmp(pw, _prefs->password) == 0 || strcmp(pw, _prefs->ro_password) == 0)) { + strcpy(reply, "Error, must differ from admin and ro passwords"); } else { strcpy(_prefs->guest_password, pw); savePrefs(); strcpy(reply, "OK"); } - } else if (memcmp(config, "ro.password ", 12) == 0) { + } else if (memcmp(config, "ro.password", 11) == 0 && (config[11] == 0 || config[11] == ' ')) { char pw[sizeof(_prefs->ro_password)]; - StrHelper::strncpy(pw, &config[12], sizeof(pw)); + StrHelper::strncpy(pw, config[11] ? &config[12] : "", sizeof(pw)); // no value clears it if (pw[0] && (strcmp(pw, _prefs->password) == 0 || strcmp(pw, _prefs->guest_password) == 0)) { strcpy(reply, "Error, must differ from admin and guest passwords"); } else {