admin pw now lives in a separate config block. BREAKING CHANGE! will wipe old settings :(

This commit is contained in:
2017-09-22 00:22:46 +02:00
parent 781cf8825b
commit 43241c2679
6 changed files with 47 additions and 19 deletions
+1 -3
View File
@@ -13,9 +13,7 @@ Cgi/template routines for configuring non-wifi settings
static bool ICACHE_FLASH_ATTR
verify_admin_pw(const char *pw)
{
// This is not really for security, but to prevent someone who
// shouldn't touch those settings from fucking it up.
return streq(pw, STR(ADMIN_PASSWORD)); // the PW comes from the makefile
return streq(pw, persist.admin.pw);
}
httpd_cgi_state ICACHE_FLASH_ATTR
+25 -7
View File
@@ -97,19 +97,27 @@ compute_checksum(AppConfigBundle *bundle)
return calculateCRC32((uint8_t *) bundle, sizeof(AppConfigBundle) - 4);
}
static void ICACHE_FLASH_ATTR
set_admin_block_defaults(void)
{
persist_info("[Persist] Initing admin config block");
strcpy(persist.admin.pw, STR(ADMIN_PASSWORD));
persist.admin.version = ADMINCONF_VERSION;
}
/**
* Load, verify and apply persistent config
*/
void ICACHE_FLASH_ATTR
persist_load(void)
{
persist_info("[Persist] Loading stored settings from FLASH...");
persist_info("[Persist] Loading settings from FLASH...");
persist_dbg("AppConfigBundle memory map:");
persist_dbg("> WiFiConfigBundle at %4d (error %2d)", wconf_at, wconf_at - 0);
persist_dbg("> SystemConfigBundle at %4d (error %2d)", sconf_at, sconf_at - WIFICONF_SIZE);
persist_dbg("> TerminalConfigBundle at %4d (error %2d)", tconf_at, tconf_at - WIFICONF_SIZE - SYSCONF_SIZE);
persist_dbg("> Checksum at %4d (error %2d)", cksum_at, cksum_at - (APPCONF_SIZE - 4));
persist_dbg("Persist memory map:");
persist_dbg("> wifi at %4d (error %2d)", wconf_at, wconf_at - 0);
persist_dbg("> sys at %4d (error %2d)", sconf_at, sconf_at - WIFICONF_SIZE);
persist_dbg("> term at %4d (error %2d)", tconf_at, tconf_at - WIFICONF_SIZE - SYSCONF_SIZE);
persist_dbg("> cksum at %4d (error %2d)", cksum_at, cksum_at - (APPCONF_SIZE - 4));
persist_dbg("> Total size = %d bytes (error %d)", sizeof(AppConfigBundle), APPCONF_SIZE - sizeof(AppConfigBundle));
bool hard_reset = false;
@@ -120,7 +128,8 @@ persist_load(void)
// Verify checksums
if (hard_reset ||
(compute_checksum(&persist.defaults) != persist.defaults.checksum) ||
(compute_checksum(&persist.current) != persist.current.checksum)) {
(compute_checksum(&persist.current) != persist.current.checksum) ||
(persist.admin.version != 0 && (calculateCRC32((uint8_t *) &persist.admin, sizeof(AdminConfigBlock) - 4) != persist.admin.checksum))) {
error("[Persist] Checksum verification: FAILED");
hard_reset = true;
} else {
@@ -135,10 +144,18 @@ persist_load(void)
// write them also as defaults
memcpy(&persist.defaults, &persist.current, sizeof(AppConfigBundle));
// reset admin pw
set_admin_block_defaults();
persist_store();
// this also stores them to flash and applies to modules
} else {
if (persist.admin.version == 0) {
set_admin_block_defaults();
persist_store();
}
apply_live_settings();
}
@@ -153,6 +170,7 @@ persist_store(void)
// Update checksums before write
persist.current.checksum = compute_checksum(&persist.current);
persist.defaults.checksum = compute_checksum(&persist.defaults);
persist.admin.checksum = calculateCRC32((uint8_t *) &persist.admin, sizeof(AdminConfigBlock) - 4);
if (!system_param_save_with_protect(PERSIST_SECTOR_ID, &persist, sizeof(PersistBlock))) {
error("[Persist] Store to flash failed!");
+14 -3
View File
@@ -16,9 +16,9 @@
// Changing this could be used to force-erase the config area
// after a firmware upgrade
#define CHECKSUM_SALT 3
#define CHECKSUM_SALT 4
#define APPCONF_SIZE 2048
#define APPCONF_SIZE 1900
/** Struct for current or default settings */
typedef struct { // the entire block should be 1024 bytes long (for compatibility across upgrades)
@@ -41,7 +41,7 @@ typedef struct { // the entire block should be 1024 bytes long (for compatibilit
// it grew to a different memory area.
uint8_t _filler_end[
APPCONF_SIZE
- sizeof(uint32_t) // checksum
- 4 // checksum
- WIFICONF_SIZE
- SYSCONF_SIZE
- TERMCONF_SIZE
@@ -50,10 +50,21 @@ typedef struct { // the entire block should be 1024 bytes long (for compatibilit
uint32_t checksum; // computed before write and tested on load. If it doesn't match, values are reset to hard defaults.
} AppConfigBundle;
#define ADMINCONF_VERSION 1
#define ADMINCONF_SIZE 256
typedef struct {
u8 version;
char pw[64];
uint8_t _filler[ADMINCONF_SIZE-64-4];
uint32_t checksum;
} AdminConfigBlock;
/** This is the entire data block stored in FLASH */
typedef struct {
AppConfigBundle defaults; // defaults are stored here
AppConfigBundle current; // active settings adjusted by the user
AdminConfigBlock admin;
} PersistBlock;
// Persist holds the data currently loaded from the flash
+4 -5
View File
@@ -4,8 +4,7 @@
#include "ansi_parser.h"
#include "syscfg.h"
#define LOGBUF_SIZE 512
static char logbuf[LOGBUF_SIZE];
static char logbuf[DEBUG_LOGBUF_SIZE];
static u32 lb_nw = 1;
static u32 lb_ls = 0;
static ETSTimer flushLogTimer;
@@ -14,7 +13,7 @@ static void buf_putc(char c)
{
if (lb_ls != lb_nw) {
logbuf[lb_nw++] = c;
if (lb_nw >= LOGBUF_SIZE) lb_nw = 0;
if (lb_nw >= DEBUG_LOGBUF_SIZE) lb_nw = 0;
}
}
@@ -25,11 +24,11 @@ buf_pop(void *unused)
u32 old_ls;
while (quantity > 0) {
// stop when done
if ((lb_ls == lb_nw-1) || (lb_ls == LOGBUF_SIZE-1 && lb_nw == 0)) break;
if ((lb_ls == lb_nw-1) || (lb_ls == DEBUG_LOGBUF_SIZE-1 && lb_nw == 0)) break;
old_ls = lb_ls;
lb_ls++;
if (lb_ls >= LOGBUF_SIZE) lb_ls = 0;
if (lb_ls >= DEBUG_LOGBUF_SIZE) lb_ls = 0;
if (OK == UART_WriteCharCRLF(UART1, logbuf[lb_ls], 5)) {
quantity--;