Skip to content

Commit d15b374

Browse files
author
Scott Powell
committed
* Sensor permission levels renamed. Misc sensor fixes.
1 parent f74819f commit d15b374

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

examples/simple_sensor/SensorMesh.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ static uint8_t putFloat(uint8_t * dest, float value, uint8_t size, uint32_t mult
243243
uint8_t SensorMesh::handleRequest(uint8_t perms, uint32_t sender_timestamp, uint8_t req_type, uint8_t* payload, size_t payload_len) {
244244
memcpy(reply_data, &sender_timestamp, 4); // reflect sender_timestamp back in response packet (kind of like a 'tag')
245245

246-
if (req_type == REQ_TYPE_GET_TELEMETRY_DATA && (perms & PERM_GET_TELEMETRY) != 0) {
246+
if (req_type == REQ_TYPE_GET_TELEMETRY_DATA) { // allow all
247247
telemetry.reset();
248248
telemetry.addVoltage(TELEM_CHANNEL_SELF, (float)board.getBattMilliVolts() / 1000.0f);
249249
// query other sensors -- target specific
@@ -254,7 +254,7 @@ uint8_t SensorMesh::handleRequest(uint8_t perms, uint32_t sender_timestamp, uint
254254
memcpy(&reply_data[4], telemetry.getBuffer(), tlen);
255255
return 4 + tlen; // reply_len
256256
}
257-
if (req_type == REQ_TYPE_GET_AVG_MIN_MAX && (perms & PERM_GET_OTHER_STATS) != 0) {
257+
if (req_type == REQ_TYPE_GET_AVG_MIN_MAX && (perms & PERM_ACL_ROLE_MASK) >= PERM_ACL_READ_ONLY) {
258258
uint32_t start_secs_ago, end_secs_ago;
259259
memcpy(&start_secs_ago, &payload[0], 4);
260260
memcpy(&end_secs_ago, &payload[4], 4);
@@ -288,13 +288,14 @@ uint8_t SensorMesh::handleRequest(uint8_t perms, uint32_t sender_timestamp, uint
288288
}
289289
return ofs;
290290
}
291-
if (req_type == REQ_TYPE_GET_ACCESS_LIST && (perms & PERM_ACL_ROLE_MASK) == PERM_ACL_LEVEL3) {
291+
if (req_type == REQ_TYPE_GET_ACCESS_LIST && (perms & PERM_ACL_ROLE_MASK) == PERM_ACL_ADMIN) {
292292
uint8_t res1 = payload[0]; // reserved for future (extra query params)
293293
uint8_t res2 = payload[1];
294294
if (res1 == 0 && res2 == 0) {
295295
uint8_t ofs = 4;
296296
for (int i = 0; i < num_contacts && ofs + 7 <= sizeof(reply_data) - 4; i++) {
297297
auto c = &contacts[i];
298+
if (c->permissions == 0) continue; // skip deleted entries
298299
memcpy(&reply_data[ofs], c->id.pub_key, 6); ofs += 6; // just 6-byte pub_key prefix
299300
reply_data[ofs++] = c->permissions;
300301
}
@@ -315,7 +316,7 @@ mesh::Packet* SensorMesh::createSelfAdvert() {
315316
return createAdvert(self_id, app_data, app_data_len);
316317
}
317318

318-
ContactInfo* SensorMesh::putContact(const mesh::Identity& id) {
319+
ContactInfo* SensorMesh::putContact(const mesh::Identity& id, uint8_t init_perms) {
319320
uint32_t min_time = 0xFFFFFFFF;
320321
ContactInfo* oldest = &contacts[MAX_CONTACTS - 1];
321322
for (int i = 0; i < num_contacts; i++) {
@@ -333,14 +334,15 @@ ContactInfo* SensorMesh::putContact(const mesh::Identity& id) {
333334
c = oldest; // evict least active contact
334335
}
335336
memset(c, 0, sizeof(*c));
337+
c->permissions = init_perms;
336338
c->id = id;
337339
c->out_path_len = -1; // initially out_path is unknown
338340
return c;
339341
}
340342

341343
void SensorMesh::applyContactPermissions(const uint8_t* pubkey, uint8_t perms) {
342344
mesh::Identity id(pubkey);
343-
auto c = putContact(id);
345+
auto c = putContact(id, 0);
344346

345347
if ((perms & PERM_ACL_ROLE_MASK) == PERM_ACL_GUEST) { // guest role is not persisted in contacts
346348
memset(c, 0, sizeof(*c));
@@ -441,7 +443,7 @@ uint8_t SensorMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t*
441443
return 0;
442444
}
443445

444-
auto client = putContact(sender); // add to contacts (if not already known)
446+
auto client = putContact(sender, PERM_RECV_ALERTS_HI | PERM_RECV_ALERTS_LO); // add to contacts (if not already known)
445447
if (sender_timestamp <= client->last_timestamp) {
446448
MESH_DEBUG_PRINTLN("Possible login replay attack!");
447449
return 0; // FATAL: client table is full -OR- replay attack
@@ -450,7 +452,7 @@ uint8_t SensorMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t*
450452
MESH_DEBUG_PRINTLN("Login success!");
451453
client->last_timestamp = sender_timestamp;
452454
client->last_activity = getRTCClock()->getCurrentTime();
453-
client->permissions = PERM_ACL_LEVEL3 | PERM_RECV_ALERTS_HI | PERM_RECV_ALERTS_LO; // initially opt-in to receive alerts (can opt out)
455+
client->permissions |= PERM_ACL_ADMIN;
454456
memcpy(client->shared_secret, secret, PUB_KEY_SIZE);
455457

456458
dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY);
@@ -502,6 +504,7 @@ void SensorMesh::handleCommand(uint32_t sender_timestamp, char* command, char* r
502504
Serial.println("ACL:");
503505
for (int i = 0; i < num_contacts; i++) {
504506
auto c = &contacts[i];
507+
if (c->permissions == 0) continue; // skip deleted entries
505508

506509
Serial.printf("%02X ", c->permissions);
507510
mesh::Utils::printHex(Serial, c->id.pub_key, PUB_KEY_SIZE);
@@ -569,7 +572,7 @@ void SensorMesh::onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_i
569572
memcpy(&timestamp, data, 4);
570573

571574
if (timestamp > from.last_timestamp) { // prevent replay attacks
572-
uint8_t reply_len = handleRequest(from.isAdmin() ? 0xFFFF : from.permissions, timestamp, data[4], &data[5], len - 5);
575+
uint8_t reply_len = handleRequest(from.isAdmin() ? 0xFF : from.permissions, timestamp, data[4], &data[5], len - 5);
573576
if (reply_len == 0) return; // invalid command
574577

575578
from.last_timestamp = timestamp;

examples/simple_sensor/SensorMesh.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525

2626
#define PERM_ACL_ROLE_MASK 3 // lower 2 bits
2727
#define PERM_ACL_GUEST 0
28-
#define PERM_ACL_LEVEL1 1
29-
#define PERM_ACL_LEVEL2 2
30-
#define PERM_ACL_LEVEL3 3 // admin
31-
32-
#define PERM_GET_TELEMETRY (1 << 2)
33-
#define PERM_GET_OTHER_STATS (1 << 3)
34-
#define PERM_RESERVED1 (1 << 4)
35-
#define PERM_RESERVED2 (1 << 5)
28+
#define PERM_ACL_READ_ONLY 1
29+
#define PERM_ACL_READ_WRITE 2
30+
#define PERM_ACL_ADMIN 3
31+
32+
#define PERM_RESERVED1 (1 << 2)
33+
#define PERM_RESERVED2 (1 << 3)
34+
#define PERM_RESERVED3 (1 << 4)
35+
#define PERM_RESERVED4 (1 << 5)
3636
#define PERM_RECV_ALERTS_LO (1 << 6) // low priority alerts
3737
#define PERM_RECV_ALERTS_HI (1 << 7) // high priority alerts
3838

@@ -45,7 +45,7 @@ struct ContactInfo {
4545
uint32_t last_timestamp; // by THEIR clock (transient)
4646
uint32_t last_activity; // by OUR clock (transient)
4747

48-
bool isAdmin() const { return (permissions & PERM_ACL_ROLE_MASK) == PERM_ACL_LEVEL3; }
48+
bool isAdmin() const { return (permissions & PERM_ACL_ROLE_MASK) == PERM_ACL_ADMIN; }
4949
};
5050

5151
#ifndef FIRMWARE_BUILD_DATE
@@ -160,7 +160,7 @@ class SensorMesh : public mesh::Mesh, public CommonCLICallbacks {
160160
uint8_t handleLoginReq(const mesh::Identity& sender, const uint8_t* secret, uint32_t sender_timestamp, const uint8_t* data);
161161
uint8_t handleRequest(uint8_t perms, uint32_t sender_timestamp, uint8_t req_type, uint8_t* payload, size_t payload_len);
162162
mesh::Packet* createSelfAdvert();
163-
ContactInfo* putContact(const mesh::Identity& id);
163+
ContactInfo* putContact(const mesh::Identity& id, uint8_t init_perms);
164164
void applyContactPermissions(const uint8_t* pubkey, uint8_t perms);
165165

166166
void sendAlert(ContactInfo* c, Trigger* t);

0 commit comments

Comments
 (0)