Add BOOT-button entry to the setup portal and gate the Wi-Fi card behind login
Hold BOOT (GPIO0) for 10s while running to reboot into the captive portal without needing the current media login (physical access is the proof). This also covers the "forgotten login" case. button.c (new) - button_task polls GPIO0 (pull-up, 50 ms), ignores a press held since boot so download-mode entry is unaffected, and calls wifi_request_portal_physical() after 10s. - Started from app_main() right after wifi_start_provisioning_if_needed(), only when not provisioning, so it also works while the station is retrying an unreachable network. wifi.c - wifi_request_portal_physical(): stores a one-shot "portal requested by button" flag in NVS (namespace "portal") and restarts. NVS is used instead of RTC_DATA_ATTR because the flag was lost across the reset on this board. - wifi_start_provisioning_if_needed(): reads and erases the flag at boot (portal_flag_take) and sets s_physical for that portal session only. Logs the reset reason. - Portal without the button (30 failed reconnects, wifi_request_portal()) stays non-physical and asks for the current login. - /save (Wi-Fi card) now requires cam_auth_verify() unless s_physical. - /auth uses cam_auth_set() when s_physical, else cam_auth_change(). - Shared lockout: 5 wrong logins block /save and /auth until restart (not applied when s_physical). Failed attempts are delayed by 1 s. - Physical portal times out after 180 s (normal portal: 300 s). - Root page gets "var PHYS=0/1" injected before its script via chunked send; with PHYS=1 the "current login" fields are hidden and a banner is shown. - html: added current-login fields (wu/wp) to the Wi-Fi form, wrapped current-login fields in .cur, /save errors are shown to the user. - read_body()/send_text() moved above the handlers. cam_auth.c - New cam_auth_set(): validates and stores a new login with no check of the current one. cam_auth_change() now = cam_auth_verify() + cam_auth_set(). Requirements - cam_auth_load() must run before wifi_start_provisioning_if_needed(), otherwise cam_auth_verify() treats auth as disabled and accepts any login. - cam_auth.h: declare cam_auth_set(); declare wifi_request_portal_physical() where button.c can see it. Tested on board: long press reboots into the AP "ESP32_Setup" instead of reconnecting to the stored network. Not yet tested: Wi-Fi card login gating, lockout, 180 s timeout, portal page rendering on a phone.
This commit is contained in:
+130
-11
@@ -3,6 +3,17 @@
|
||||
* - tiny self-contained MD5 (no mbedTLS dependency, works on every ESP-IDF version)
|
||||
* - RTSP Digest authentication (RFC 2069 style, no qop: what ffmpeg, VLC and go2rtc speak)
|
||||
* - HTTP check: "Authorization: Basic" header or "?key=" query parameter
|
||||
* - login stored in NVS as user name + MD5 hash (no plain password in flash),
|
||||
* changeable at runtime:
|
||||
* cam_auth_change() after verifying the current login
|
||||
* cam_auth_set() WITHOUT verifying it (only for the BOOT-button portal, where
|
||||
* physical access to the device is the proof)
|
||||
*
|
||||
* cam_auth.h must declare (add the last one):
|
||||
* bool cam_auth_verify(const char *user, const char *pass);
|
||||
* cam_auth_result_t cam_auth_change(const char *old_user, const char *old_pass,
|
||||
* const char *new_user, const char *new_pass);
|
||||
* cam_auth_result_t cam_auth_set(const char *new_user, const char *new_pass);
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
@@ -13,14 +24,17 @@
|
||||
#include "freertos/task.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_random.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "nvs.h"
|
||||
#include "cam_auth.h"
|
||||
|
||||
static const char *TAG = "AUTH";
|
||||
|
||||
static bool s_enabled = false;
|
||||
static char s_user[48];
|
||||
static char s_pass[64];
|
||||
static char s_ha1[33]; /* md5(user:realm:pass), computed once */
|
||||
static char s_ha1[33]; /* md5(user:realm:pass); the password itself is never kept */
|
||||
|
||||
#define AUTH_NS "cam_auth" /* NVS namespace: keys "user" and "ha1" */
|
||||
|
||||
/* ================================================================== */
|
||||
/* MD5 (RFC 1321) */
|
||||
@@ -177,6 +191,13 @@ static size_t b64_decode(const char *in, uint8_t *out, size_t outmax)
|
||||
/* ================================================================== */
|
||||
/* Public API */
|
||||
/* ================================================================== */
|
||||
static void make_ha1(const char *user, const char *pass, char out[33])
|
||||
{
|
||||
char tmp[200];
|
||||
snprintf(tmp, sizeof(tmp), "%s:%s:%s", user, CAM_AUTH_REALM, pass);
|
||||
md5_hex(tmp, out);
|
||||
}
|
||||
|
||||
void cam_auth_init(const char *user, const char *pass)
|
||||
{
|
||||
s_enabled = (user && *user);
|
||||
@@ -185,11 +206,7 @@ void cam_auth_init(const char *user, const char *pass)
|
||||
return;
|
||||
}
|
||||
snprintf(s_user, sizeof(s_user), "%s", user);
|
||||
snprintf(s_pass, sizeof(s_pass), "%s", pass ? pass : "");
|
||||
|
||||
char tmp[160];
|
||||
snprintf(tmp, sizeof(tmp), "%s:%s:%s", s_user, CAM_AUTH_REALM, s_pass);
|
||||
md5_hex(tmp, s_ha1);
|
||||
make_ha1(s_user, pass ? pass : "", s_ha1);
|
||||
ESP_LOGI(TAG, "Authentication enabled for user \"%s\"", s_user);
|
||||
}
|
||||
|
||||
@@ -198,6 +215,106 @@ bool cam_auth_enabled(void)
|
||||
return s_enabled;
|
||||
}
|
||||
|
||||
bool cam_auth_verify(const char *user, const char *pass)
|
||||
{
|
||||
if (!s_enabled) return true;
|
||||
if (!user || !pass) return false;
|
||||
char ha1[33];
|
||||
make_ha1(user, pass, ha1);
|
||||
bool u_ok = ct_eq(user, s_user); /* evaluate both: no early exit */
|
||||
bool p_ok = ct_eq(ha1, s_ha1);
|
||||
return u_ok && p_ok;
|
||||
}
|
||||
|
||||
/* ---------------- Stored login (NVS) ---------------- */
|
||||
static bool valid_user(const char *u) /* 3-31 chars: letters, digits . _ - */
|
||||
{
|
||||
size_t n = strlen(u);
|
||||
if (n < 3 || n > 31) return false;
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
char c = u[i];
|
||||
bool ok = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') ||
|
||||
c == '.' || c == '_' || c == '-';
|
||||
if (!ok) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool valid_pass(const char *p) /* 8-32 chars: letters and digits */
|
||||
{
|
||||
size_t n = strlen(p);
|
||||
if (n < 8 || n > 32) return false;
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
char c = p[i];
|
||||
bool ok = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9');
|
||||
if (!ok) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool store_save(const char *user, const char *ha1)
|
||||
{
|
||||
nvs_handle_t h;
|
||||
if (nvs_open(AUTH_NS, NVS_READWRITE, &h) != ESP_OK) return false;
|
||||
bool ok = nvs_set_str(h, "user", user) == ESP_OK &&
|
||||
nvs_set_str(h, "ha1", ha1) == ESP_OK &&
|
||||
nvs_commit(h) == ESP_OK;
|
||||
nvs_close(h);
|
||||
return ok;
|
||||
}
|
||||
|
||||
void cam_auth_load(const char *default_user, const char *default_pass)
|
||||
{
|
||||
esp_err_t e = nvs_flash_init(); /* harmless if already initialised */
|
||||
if (e == ESP_ERR_NVS_NO_FREE_PAGES || e == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
nvs_flash_erase();
|
||||
nvs_flash_init();
|
||||
}
|
||||
|
||||
nvs_handle_t h;
|
||||
if (nvs_open(AUTH_NS, NVS_READONLY, &h) == ESP_OK) {
|
||||
char u[48] = {0}, ha[33] = {0};
|
||||
size_t ul = sizeof(u), hl = sizeof(ha);
|
||||
bool ok = nvs_get_str(h, "user", u, &ul) == ESP_OK &&
|
||||
nvs_get_str(h, "ha1", ha, &hl) == ESP_OK &&
|
||||
u[0] != 0 && strlen(ha) == 32;
|
||||
nvs_close(h);
|
||||
if (ok) {
|
||||
s_enabled = true;
|
||||
snprintf(s_user, sizeof(s_user), "%s", u);
|
||||
memcpy(s_ha1, ha, sizeof(s_ha1));
|
||||
ESP_LOGI(TAG, "Loaded stored login for user \"%s\"", s_user);
|
||||
return;
|
||||
}
|
||||
}
|
||||
cam_auth_init(default_user, default_pass);
|
||||
ESP_LOGW(TAG, "Using the built-in default login: change it on the setup page");
|
||||
}
|
||||
|
||||
/* Validates the new login, stores it and makes it active. No check of the current login. */
|
||||
cam_auth_result_t cam_auth_set(const char *new_user, const char *new_pass)
|
||||
{
|
||||
if (!new_user || !new_pass || !valid_user(new_user) || !valid_pass(new_pass))
|
||||
return CAM_AUTH_BAD_NEW;
|
||||
|
||||
char ha1[33];
|
||||
make_ha1(new_user, new_pass, ha1);
|
||||
if (!store_save(new_user, ha1)) return CAM_AUTH_STORE_FAILED;
|
||||
|
||||
s_enabled = true;
|
||||
snprintf(s_user, sizeof(s_user), "%s", new_user);
|
||||
memcpy(s_ha1, ha1, sizeof(s_ha1));
|
||||
ESP_LOGI(TAG, "Login changed, new user \"%s\"", s_user);
|
||||
return CAM_AUTH_OK;
|
||||
}
|
||||
|
||||
cam_auth_result_t cam_auth_change(const char *old_user, const char *old_pass,
|
||||
const char *new_user, const char *new_pass)
|
||||
{
|
||||
if (!cam_auth_verify(old_user, old_pass)) return CAM_AUTH_BAD_CURRENT;
|
||||
return cam_auth_set(new_user, new_pass);
|
||||
}
|
||||
|
||||
/* ---------------- HTTP ---------------- */
|
||||
bool cam_http_check(httpd_req_t *req)
|
||||
{
|
||||
@@ -215,9 +332,11 @@ bool cam_http_check(httpd_req_t *req)
|
||||
size_t dl = b64_decode(h + 6, dec, sizeof(dec) - 1);
|
||||
if (dl > 0) {
|
||||
dec[dl] = 0;
|
||||
char want[120];
|
||||
snprintf(want, sizeof(want), "%s:%s", s_user, s_pass);
|
||||
ok = ct_eq((const char *)dec, want);
|
||||
char *colon = strchr((char *)dec, ':');
|
||||
if (colon) {
|
||||
*colon = 0;
|
||||
ok = cam_auth_verify((const char *)dec, colon + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -227,7 +346,7 @@ bool cam_http_check(httpd_req_t *req)
|
||||
char q[160], v[80];
|
||||
if (httpd_req_get_url_query_str(req, q, sizeof(q)) == ESP_OK &&
|
||||
httpd_query_key_value(q, "key", v, sizeof(v)) == ESP_OK) {
|
||||
ok = ct_eq(v, s_pass);
|
||||
ok = cam_auth_verify(s_user, v);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user