Merge pull request #4014 from agessaman/feat/ro-password
Add a read-only password for repeaters and sensors
This commit is contained in:
commit
05fa237ef3
7 files changed
+134
-27
No files matched your search
@@ -96,6 +96,28 @@ 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 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;
|
||||
|
||||
@@ -191,7 +213,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->shared_login) {
|
||||
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
|
||||
@@ -263,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;
|
||||
@@ -415,14 +443,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;
|
||||
} 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 +467,17 @@ 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;
|
||||
bool saved = client->permissions != 0 && !client->shared_login;
|
||||
if (!shared_pw) {
|
||||
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 (!saved) { // shared passwords don't change saved entries
|
||||
client->permissions = perms;
|
||||
client->shared_login = true;
|
||||
}
|
||||
memcpy(client->shared_secret, secret, PUB_KEY_SIZE);
|
||||
|
||||
dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY);
|
||||
}
|
||||
client->last_activity = getRTCClock()->getCurrentTime();
|
||||
|
||||
@@ -1102,11 +1145,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->shared_login) {
|
||||
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) {
|
||||
|
||||
Reference in new issue
Block a user