From d6110f91cbf5a6cdc95fdf620a2f856a15ae93dc Mon Sep 17 00:00:00 2001 From: Audric Date: Sun, 17 May 2026 00:49:43 +0200 Subject: [PATCH] feat(satellite): parse per-id SECRET_= entries in [SATELLITE] --- src/svxlink/reflector/Reflector.cpp | 63 ++++++++++++++++++++++++++--- src/svxlink/reflector/Reflector.h | 23 ++++++++++- 2 files changed, 79 insertions(+), 7 deletions(-) diff --git a/src/svxlink/reflector/Reflector.cpp b/src/svxlink/reflector/Reflector.cpp index fc8bfa9..49a699d 100644 --- a/src/svxlink/reflector/Reflector.cpp +++ b/src/svxlink/reflector/Reflector.cpp @@ -2688,22 +2688,73 @@ void Reflector::trunkPendingTimeout(Async::Timer* t) void Reflector::initSatelliteServer(void) { std::string sat_port; - std::string sat_secret; - if (!m_cfg->getValue("SATELLITE", "LISTEN_PORT", sat_port) || - !m_cfg->getValue("SATELLITE", "SECRET", sat_secret) || - sat_secret.empty()) + if (!m_cfg->getValue("SATELLITE", "LISTEN_PORT", sat_port)) { return; // No [SATELLITE] section — not accepting satellites } - m_satellite_secret = sat_secret; + // Fallback (optional). Empty/absent means "no fallback". + m_satellite_secret.clear(); + std::string sat_secret; + if (m_cfg->getValue("SATELLITE", "SECRET", sat_secret)) + { + m_satellite_secret = sat_secret; + } + + // Per-id secrets: enumerate every tag in [SATELLITE] starting with + // "SECRET_" and validate the suffix against [A-Za-z0-9-]+. Malformed + // entries are logged and ignored. + m_satellite_secrets.clear(); + auto isValidIdChar = [](char c) { + return (c >= 'A' && c <= 'Z') || + (c >= 'a' && c <= 'z') || + (c >= '0' && c <= '9') || + c == '-'; + }; + for (const std::string& tag : m_cfg->listSection("SATELLITE")) + { + static const std::string kPrefix = "SECRET_"; + if (tag.compare(0, kPrefix.size(), kPrefix) != 0) continue; + std::string id = tag.substr(kPrefix.size()); + bool valid = !id.empty(); + for (char c : id) + { + if (!isValidIdChar(c)) { valid = false; break; } + } + if (!valid) + { + geulog::warn("satellite", "ignoring malformed [SATELLITE] secret " + "key '", tag, "' — id must be [A-Za-z0-9-]+"); + continue; + } + std::string value; + m_cfg->getValue("SATELLITE", tag, value); + if (value.empty()) + { + geulog::warn("satellite", "ignoring empty [SATELLITE] secret value " + "for key '", tag, "'"); + continue; + } + m_satellite_secrets[id] = value; + } + + if (m_satellite_secret.empty() && m_satellite_secrets.empty()) + { + geulog::warn("satellite", + "[SATELLITE] has neither SECRET= nor any SECRET_= — " + "all satellite connections will be rejected"); + } + m_sat_srv = new TcpServer(sat_port); m_sat_srv->clientConnected.connect( sigc::mem_fun(*this, &Reflector::satelliteConnected)); m_sat_srv->clientDisconnected.connect( sigc::mem_fun(*this, &Reflector::satelliteDisconnected)); - geulog::info("core", "Satellite server listening on port ", sat_port); + geulog::info("core", "Satellite server listening on port ", sat_port, + " (per-id secrets: ", m_satellite_secrets.size(), + ", fallback: ", (m_satellite_secret.empty() ? "no" : "yes"), + ")"); } /* Reflector::initSatelliteServer */ diff --git a/src/svxlink/reflector/Reflector.h b/src/svxlink/reflector/Reflector.h index 56e3dca..981bc79 100644 --- a/src/svxlink/reflector/Reflector.h +++ b/src/svxlink/reflector/Reflector.h @@ -317,6 +317,26 @@ class Reflector : public sigc::trackable const std::vector& audio); void forwardFlushToSatellitesExcept(SatelliteLink* except, uint32_t tg); + /** + * Resolve the HMAC secret to verify a satellite's MsgPeerHello. + * + * Lookup rule: + * 1. If [SATELLITE].SECRET_ is configured, return that value + * and set pinned=true. Caller MUST reject the connection on + * HMAC mismatch — there is no fallback once an id is pinned. + * 2. Otherwise, if [SATELLITE].SECRET is configured, return it + * with pinned=false. + * 3. Otherwise return false (no secret available — reject). + * + * @param id SATELLITE_ID from the incoming hello. + * @param out_secret On true return, the secret to verify against. + * @param out_pinned On true return, whether the per-id path was used. + * @return true if a secret was resolved. + */ + bool resolveSatelliteSecret(const std::string& id, + std::string& out_secret, + bool& out_pinned) const; + void publishRxUpdate(ReflectorClient* client); // Push the rich per-client status blob (rx, monitoredTGs, qth, ...) // to Redis. Cheap no-op if Redis is not configured. @@ -455,7 +475,8 @@ class Reflector : public sigc::trackable bool m_is_satellite = false; SatelliteClient* m_satellite_client = nullptr; FramedTcpServer* m_sat_srv = nullptr; - std::string m_satellite_secret; + std::string m_satellite_secret; // fallback (optional) + std::map m_satellite_secrets; // per-id (optional) std::map m_satellite_con_map; std::vector m_sat_cleanup_pending; Async::Timer m_sat_cleanup_timer;