Files
meshcore-tth/src/helpers/ClientACL.h
T

95 lines
3.1 KiB
C++

#pragma once
#include <Arduino.h> // needed for PlatformIO
#include <Mesh.h>
#include <helpers/IdentityStore.h>
#define PERM_ACL_ROLE_MASK 3 // lower 2 bits
#define PERM_ACL_GUEST 0
#define PERM_ACL_READ_ONLY 1
#define PERM_ACL_READ_WRITE 2
#define PERM_ACL_ADMIN 3
#define OUT_PATH_UNKNOWN 0xFF
struct ClientInfo {
mesh::Identity id;
uint8_t permissions;
uint8_t out_path_len;
uint8_t out_path[MAX_PATH_SIZE];
uint8_t shared_secret[PUB_KEY_SIZE];
uint32_t last_timestamp; // by THEIR clock (restored from /s_acl_ts* on boot)
uint32_t saved_timestamp; // last_timestamp as last written to /s_acl_ts*
uint32_t last_activity; // by OUR clock (transient)
union {
struct {
uint32_t sync_since; // sync messages SINCE this timestamp (by OUR clock)
uint32_t pending_ack;
uint32_t push_post_timestamp;
unsigned long ack_timeout;
uint8_t push_failures;
} room;
struct {
uint32_t expiry_timestamp; // epoch seconds
uint32_t push_tag;
uint16_t scope_region_id; // scope to use when sending telemetry to this client/subscriber
uint8_t min_deltas_len;
uint8_t min_deltas[14]; // LPP encoded
uint8_t prev_telem[14]; // LPP encoded
} sensor;
} extra;
bool isAdmin() const { return (permissions & PERM_ACL_ROLE_MASK) == PERM_ACL_ADMIN; }
};
#ifndef MAX_CLIENTS
#define MAX_CLIENTS 32
#endif
// CUSTOM (TeTeHacko): a timestamp further ahead of our clock than this is accepted
// but not persisted, so a companion with a runaway RTC cannot lock its admin out
// across reboots.
#ifndef ACL_TS_MAX_AHEAD
#define ACL_TS_MAX_AHEAD (24 * 3600)
#endif
class ClientACL {
FILESYSTEM* _fs;
ClientInfo clients[MAX_CLIENTS];
int num_clients;
uint8_t _ts_slot;
uint32_t _ts_seq;
struct TsOrphan { uint8_t prefix[8]; uint32_t ts; };
TsOrphan _ts_orphans[MAX_CLIENTS]; // floors of clients not (or no longer) in clients[]
int _num_ts_orphans;
void loadTimestamps();
void saveTimestamps();
void orphanTimestamp(const ClientInfo* c);
public:
ClientACL() {
memset(clients, 0, sizeof(clients));
num_clients = 0;
_ts_slot = 0;
_ts_seq = 0;
_num_ts_orphans = 0;
}
void load(FILESYSTEM* _fs, const mesh::LocalIdentity& self_id);
void save(FILESYSTEM* _fs, bool (*filter)(ClientInfo*)=NULL);
bool clear();
// CUSTOM (TeTeHacko): replay protection that survives a reboot. last_timestamp
// used to live only in RAM, so after any reboot a captured command (a `reboot`
// among them) was accepted again -- forever. Call this BEFORE acting on an
// authenticated packet; it writes the new high-water mark synchronously.
void persistTimestamp(ClientInfo* c, uint32_t sender_timestamp, uint32_t now);
ClientInfo* getClient(const uint8_t* pubkey, int key_len);
ClientInfo* putClient(const mesh::Identity& id, uint8_t init_perms);
bool applyPermissions(const mesh::LocalIdentity& self_id, const uint8_t* pubkey, int key_len, uint8_t perms);
int getNumClients() const { return num_clients; }
ClientInfo* getClientByIdx(int idx) { return &clients[idx]; }
};