The controller checks every route against the signed-in user's role, with levels none, view, control and edit per area. Guests get the permissions of a built-in guest role. Auth stays off until the first user exists; LED_ADMIN_USER and LED_ADMIN_PASSWORD create the first admin. Sessions are cookies, passwords are PBKDF2-SHA256, and logins lock per username after five failures. The simulator's WebSocket checks the view permission at handshake and per frame; /ws needs devices control. The web UI gets a sign-in button, a sign-in prompt on refused changes, and a users and roles screen. Also: the device build gets the bridge include path and forward type it was missing, Dockerfile.sim runs non-root, and Dockerfile.prod with docker-compose.prod.yml runs the controller on an internal network behind Caddy for HTTPS. Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
221 lines
5.8 KiB
C
221 lines
5.8 KiB
C
#include "store.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "esp_check.h"
|
|
#include "esp_heap_caps.h"
|
|
#include "esp_littlefs.h"
|
|
#include "esp_log.h"
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/semphr.h"
|
|
|
|
static const char *TAG = "store";
|
|
|
|
// Where the collections live: the littlefs partition, or on the host build a directory
|
|
#if CONFIG_IDF_TARGET_LINUX
|
|
#include <stdlib.h>
|
|
static const char *db_root(void)
|
|
{
|
|
const char *d = getenv("LED_SIM_DB");
|
|
return d && d[0] ? d : "/db";
|
|
}
|
|
#define PATH_LEN 256
|
|
#else
|
|
#define db_root() "/db"
|
|
#define PATH_LEN 48
|
|
#endif
|
|
|
|
static const char *const NAMES[] = {
|
|
"device", "zone", "preset", "profile", "palette", "pattern",
|
|
"sequence", "scene", "group", "output", "settings", "user", "role",
|
|
};
|
|
#define N_COLL (sizeof(NAMES) / sizeof(NAMES[0]))
|
|
|
|
static cJSON *s_coll[N_COLL];
|
|
static SemaphoreHandle_t s_lock;
|
|
|
|
static int index_of(const char *name)
|
|
{
|
|
for (size_t i = 0; i < N_COLL; i++) {
|
|
if (strcmp(NAMES[i], name) == 0) {
|
|
return (int)i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
static esp_err_t mount(const char *label, const char *base)
|
|
{
|
|
esp_vfs_littlefs_conf_t conf = {
|
|
.base_path = base,
|
|
.partition_label = label,
|
|
.format_if_mount_failed = true,
|
|
.grow_on_mount = true, // the db partition spans the rest of the flash; an older, smaller filesystem is extended in place
|
|
};
|
|
return esp_vfs_littlefs_register(&conf);
|
|
}
|
|
|
|
static cJSON *load_file(const char *name)
|
|
{
|
|
char path[PATH_LEN];
|
|
snprintf(path, sizeof(path), "%s/%s.json", db_root(), name);
|
|
FILE *f = fopen(path, "rb");
|
|
if (!f) {
|
|
// store_save removes the old file before renaming the finished .tmp, so a restart between the two leaves only .tmp
|
|
char tmp[PATH_LEN];
|
|
snprintf(tmp, sizeof(tmp), "%s/%s.tmp", db_root(), name);
|
|
if (rename(tmp, path) == 0) {
|
|
ESP_LOGW(TAG, "%s: recovered from %s", path, tmp);
|
|
f = fopen(path, "rb");
|
|
}
|
|
}
|
|
if (!f) {
|
|
return cJSON_CreateObject();
|
|
}
|
|
fseek(f, 0, SEEK_END);
|
|
long n = ftell(f);
|
|
fseek(f, 0, SEEK_SET);
|
|
char *buf = malloc(n + 1);
|
|
cJSON *obj = NULL;
|
|
if (buf) {
|
|
buf[fread(buf, 1, n, f)] = 0;
|
|
obj = cJSON_Parse(buf);
|
|
free(buf);
|
|
}
|
|
fclose(f);
|
|
if (!cJSON_IsObject(obj)) {
|
|
ESP_LOGW(TAG, "%s: unreadable, starting empty", path);
|
|
cJSON_Delete(obj);
|
|
obj = cJSON_CreateObject();
|
|
}
|
|
return obj;
|
|
}
|
|
|
|
// JSON nodes are small, so plain malloc would put the whole store in internal RAM, which the mic,
|
|
// Wi-Fi and lwIP need. They go to PSRAM instead (free() releases either kind).
|
|
static void *json_malloc(size_t size)
|
|
{
|
|
void *p = heap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
|
return p ? p : malloc(size);
|
|
}
|
|
|
|
esp_err_t store_init(void)
|
|
{
|
|
cJSON_Hooks hooks = { .malloc_fn = json_malloc, .free_fn = free };
|
|
cJSON_InitHooks(&hooks);
|
|
s_lock = xSemaphoreCreateRecursiveMutex();
|
|
ESP_RETURN_ON_FALSE(s_lock, ESP_ERR_NO_MEM, TAG, "mutex");
|
|
ESP_RETURN_ON_ERROR(mount("db", db_root()), TAG, "mount db");
|
|
for (size_t i = 0; i < N_COLL; i++) {
|
|
s_coll[i] = load_file(NAMES[i]);
|
|
ESP_LOGI(TAG, "%s: %d records", NAMES[i], cJSON_GetArraySize(s_coll[i]));
|
|
}
|
|
return ESP_OK;
|
|
}
|
|
|
|
bool store_known(const char *name)
|
|
{
|
|
return index_of(name) >= 0;
|
|
}
|
|
|
|
cJSON *store_coll(const char *name)
|
|
{
|
|
int i = index_of(name);
|
|
return i < 0 ? NULL : s_coll[i];
|
|
}
|
|
|
|
esp_err_t store_save(const char *name)
|
|
{
|
|
int i = index_of(name);
|
|
ESP_RETURN_ON_FALSE(i >= 0, ESP_ERR_NOT_FOUND, TAG, "unknown collection");
|
|
char *json = cJSON_PrintUnformatted(s_coll[i]);
|
|
ESP_RETURN_ON_FALSE(json, ESP_ERR_NO_MEM, TAG, "print");
|
|
|
|
char tmp[PATH_LEN], path[PATH_LEN];
|
|
snprintf(tmp, sizeof(tmp), "%s/%s.tmp", db_root(), name);
|
|
snprintf(path, sizeof(path), "%s/%s.json", db_root(), name);
|
|
FILE *f = fopen(tmp, "wb");
|
|
esp_err_t ret = ESP_FAIL;
|
|
if (f) {
|
|
size_t len = strlen(json);
|
|
bool ok = fwrite(json, 1, len, f) == len;
|
|
ok = fclose(f) == 0 && ok;
|
|
if (ok) {
|
|
remove(path); // littlefs rename-over is not guaranteed on every version
|
|
ok = rename(tmp, path) == 0;
|
|
}
|
|
ret = ok ? ESP_OK : ESP_FAIL;
|
|
}
|
|
free(json);
|
|
if (ret != ESP_OK) {
|
|
ESP_LOGE(TAG, "saving %s failed", name);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
esp_err_t store_replace(const char *name, cJSON *obj)
|
|
{
|
|
int i = index_of(name);
|
|
if (i < 0 || !cJSON_IsObject(obj)) {
|
|
cJSON_Delete(obj);
|
|
return ESP_ERR_INVALID_ARG;
|
|
}
|
|
cJSON_Delete(s_coll[i]);
|
|
s_coll[i] = obj;
|
|
return store_save(name);
|
|
}
|
|
|
|
void store_next_id(const char *name, char *buf, size_t len)
|
|
{
|
|
long max = 0;
|
|
cJSON *c = store_coll(name), *it;
|
|
cJSON_ArrayForEach(it, c)
|
|
{
|
|
char *end;
|
|
long v = strtol(it->string, &end, 10);
|
|
if (*end == 0 && v > max) {
|
|
max = v;
|
|
}
|
|
}
|
|
snprintf(buf, len, "%ld", max + 1);
|
|
}
|
|
|
|
cJSON *store_settings(void)
|
|
{
|
|
return store_coll("settings");
|
|
}
|
|
|
|
size_t store_bridges(char addrs[][80], size_t max)
|
|
{
|
|
size_t n = 0;
|
|
cJSON *settings = store_settings();
|
|
cJSON *list = cJSON_GetObjectItem(settings, "bridges");
|
|
if (cJSON_IsArray(list)) {
|
|
cJSON *item = NULL;
|
|
cJSON_ArrayForEach(item, list) {
|
|
if (n < max && cJSON_IsString(item) && item->valuestring[0]) {
|
|
snprintf(addrs[n], 80, "%s", item->valuestring);
|
|
n++;
|
|
}
|
|
}
|
|
return n;
|
|
}
|
|
cJSON *one = cJSON_GetObjectItem(settings, "bridge");
|
|
if (max && cJSON_IsString(one) && one->valuestring[0]) {
|
|
snprintf(addrs[0], 80, "%s", one->valuestring);
|
|
n = 1;
|
|
}
|
|
return n;
|
|
}
|
|
|
|
void store_lock(void)
|
|
{
|
|
xSemaphoreTakeRecursive(s_lock, portMAX_DELAY);
|
|
}
|
|
|
|
void store_unlock(void)
|
|
{
|
|
xSemaphoreGiveRecursive(s_lock);
|
|
}
|