ClientACL.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #pragma once
  2. #include <Arduino.h> // needed for PlatformIO
  3. #include <Mesh.h>
  4. #include <helpers/IdentityStore.h>
  5. #define PERM_ACL_ROLE_MASK 3 // lower 2 bits
  6. #define PERM_ACL_GUEST 0
  7. #define PERM_ACL_READ_ONLY 1
  8. #define PERM_ACL_READ_WRITE 2
  9. #define PERM_ACL_ADMIN 3
  10. #define OUT_PATH_UNKNOWN 0xFF
  11. struct ClientInfo {
  12. mesh::Identity id;
  13. uint8_t permissions;
  14. uint8_t out_path_len;
  15. uint8_t out_path[MAX_PATH_SIZE];
  16. uint8_t shared_secret[PUB_KEY_SIZE];
  17. uint32_t last_timestamp; // by THEIR clock (transient)
  18. uint32_t last_activity; // by OUR clock (transient)
  19. union {
  20. struct {
  21. uint32_t sync_since; // sync messages SINCE this timestamp (by OUR clock)
  22. uint32_t pending_ack;
  23. uint32_t push_post_timestamp;
  24. unsigned long ack_timeout;
  25. uint8_t push_failures;
  26. } room;
  27. } extra;
  28. bool isAdmin() const { return (permissions & PERM_ACL_ROLE_MASK) == PERM_ACL_ADMIN; }
  29. };
  30. #ifndef MAX_CLIENTS
  31. #define MAX_CLIENTS 20
  32. #endif
  33. class ClientACL {
  34. FILESYSTEM* _fs;
  35. ClientInfo clients[MAX_CLIENTS];
  36. int num_clients;
  37. public:
  38. ClientACL() {
  39. memset(clients, 0, sizeof(clients));
  40. num_clients = 0;
  41. }
  42. void load(FILESYSTEM* _fs, const mesh::LocalIdentity& self_id);
  43. void save(FILESYSTEM* _fs, bool (*filter)(ClientInfo*)=NULL);
  44. bool clear();
  45. ClientInfo* getClient(const uint8_t* pubkey, int key_len);
  46. ClientInfo* putClient(const mesh::Identity& id, uint8_t init_perms);
  47. bool applyPermissions(const mesh::LocalIdentity& self_id, const uint8_t* pubkey, int key_len, uint8_t perms);
  48. int getNumClients() const { return num_clients; }
  49. ClientInfo* getClientByIdx(int idx) { return &clients[idx]; }
  50. };