Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ The version lives in `CERBERUS_VERSION` in `src/cerberus.c` (`MAJOR.MINOR.PATCH`
published only as the [`latest`](https://github.com/OpenIPC/cerberus/releases/latest) release
(see the README); this file is the change history, newest first.

## 0.3.4 — 2026-11-13

- The coordinator's `NightModeOn` / `NightModeOff` commands switch majestic into night or day mode
through its own `/night/on` and `/night/off`, on the same address the snapshot comes from — so a
majestic with a password is reached with the one already in `cerberus_snapshot_url`, and the log
names the path, never that address. Like every command there is no answer to the cloud; if
majestic's automatic mode is on, its light sensor may switch back.

## 0.3.3 — 2026-11-12

- The coordinator's `EnableMotion` command switches majestic's own motion detector on
Expand Down
67 changes: 63 additions & 4 deletions src/cerberus.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@

/* The version of every build, release builds included (#32); `make VERSION=...` overrides it for a test build. */
#ifndef CERBERUS_VERSION
#define CERBERUS_VERSION "0.3.3"
#define CERBERUS_VERSION "0.3.4"
#endif

/* Where the errors are decoded. Printed after every fatal one: the reader is on a camera
Expand Down Expand Up @@ -901,13 +901,15 @@ static int conn_send(struct conn *c, const char *buf, size_t len)
return 0;
}

#define HTTP_TIMEOUT_MS 10000

static int conn_recv(struct conn *c, unsigned char *buf, size_t len)
{
int n;
do {
n = c->tls
? mbedtls_ssl_read(&c->ssl, buf, len)
: mbedtls_net_recv(&c->net, buf, len);
: mbedtls_net_recv_timeout(&c->net, buf, len, HTTP_TIMEOUT_MS);
} while (n == MBEDTLS_ERR_SSL_WANT_READ || n == MBEDTLS_ERR_SSL_WANT_WRITE);
return n;
}
Expand Down Expand Up @@ -1082,9 +1084,10 @@ static long http_do(const struct http_req *rq, unsigned char *resp, size_t resp_
}

mbedtls_ssl_conf_rng(&c.conf, mbedtls_ctr_drbg_random, rng);
mbedtls_ssl_conf_read_timeout(&c.conf, HTTP_TIMEOUT_MS);
if (mbedtls_ssl_setup(&c.ssl, &c.conf) != 0) die("ssl setup failed");
if (mbedtls_ssl_set_hostname(&c.ssl, host) != 0) die("ssl SNI failed");
mbedtls_ssl_set_bio(&c.ssl, &c.net, mbedtls_net_send, mbedtls_net_recv, NULL);
mbedtls_ssl_set_bio(&c.ssl, &c.net, mbedtls_net_send, mbedtls_net_recv, mbedtls_net_recv_timeout);

while ((rc = mbedtls_ssl_handshake(&c.ssl)) != 0) {
if (rc == MBEDTLS_ERR_SSL_WANT_READ || rc == MBEDTLS_ERR_SSL_WANT_WRITE) continue;
Expand Down Expand Up @@ -1126,7 +1129,15 @@ static long http_do(const struct http_req *rq, unsigned char *resp, size_t resp_
for (;;) {
n = conn_recv(&c, buf, sizeof buf);
if (n == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY || n <= 0) break;
if (total + (size_t)n + 1 > resp_sz) { overflow = 1; break; }
if (total + (size_t)n + 1 > resp_sz) {
size_t room = resp_sz > total + 1 ? resp_sz - 1 - total : 0;
if (room) {
memcpy(resp + total, buf, room);
total += room;
}
overflow = 1;
break;
}
memcpy(resp + total, buf, (size_t)n);
total += (size_t)n;
}
Expand Down Expand Up @@ -2135,6 +2146,50 @@ static void enable_motion(void)
g_cfg.events ? "" : " (motion events are off in this agent, so nothing will reach the cloud)");
}

/*
* NightModeOn / NightModeOff: majestic's own /night/on and /night/off — the switch its web interface
* flips. Sent to the same origin the snapshot comes from (cerberus_snapshot_url), so a majestic with
* a password is reached with the password already given for frames, and no second address has to be
* configured. There is no answer to the cloud either way — the directive channel has none — so what
* happened goes to the log; and if majestic's automatic mode is on, its light sensor may switch back.
*/
static void night_mode(int on)
{
const char *what = on ? "NightModeOn" : "NightModeOff";
const char *s = g_cfg.snapshot_url, *scheme = s ? strstr(s, "://") : NULL, *path;
struct http_req rq = { "GET", NULL, NULL, NULL, 0, NULL };
unsigned char resp[512];
char url[512];
size_t origin;
int status = 0;
long n;

if (!url_ok(s) || !scheme) {
warn_("%s: CERBERUS_SNAPSHOT_URL is not an http(s) URL, so there is no majestic to ask", what);
return;
}
path = strchr(scheme + 3, '/');
origin = path ? (size_t)(path - s) : strlen(s);
if (origin + sizeof "/night/off" > sizeof url) {
warn_("%s: CERBERUS_SNAPSHOT_URL is too long", what);
return;
}
memcpy(url, s, origin);
strcpy(url + origin, on ? "/night/on" : "/night/off");
rq.url = url;

n = http_do(&rq, resp, sizeof resp, &status);
/* The address may carry majestic's password: say the path, never the URL. */
if (n == -1)
warn_("%s: majestic did not answer %s", what, on ? "/night/on" : "/night/off");
else if (status == 401 || status == 403)
warn_("%s: majestic wants a password — put it in cerberus_snapshot_url, as for frames", what);
else if (status != 200)
warn_("%s: majestic answered HTTP %d to %s", what, status, on ? "/night/on" : "/night/off");
else
note("%s mode switched on in majestic", on ? "night" : "day");
}

/*
* Применяет NodeDirective из ответа на heartbeat.
*
Expand Down Expand Up @@ -2196,6 +2251,10 @@ static void apply_directive(const char *json)
self_restart();
else if (!strcmp(command, "EnableMotion"))
enable_motion();
else if (!strcmp(command, "NightModeOn"))
night_mode(1);
else if (!strcmp(command, "NightModeOff"))
night_mode(0);
else if (strcmp(command, "None") != 0)
warn_("unknown command '%s' from the coordinator — ignoring", command);
}
Expand Down
18 changes: 17 additions & 1 deletion tests/fake_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def heartbeat(self, node):


class Majestic(BaseHTTPRequestHandler):
"""majestic's /image.jpg: Basic auth root:12345, a JPEG-looking body, sent chunked."""
"""majestic's /image.jpg (a JPEG-looking body, sent chunked) and /night/on|off, behind Basic auth root:12345."""
protocol_version = "HTTP/1.1"
FRAME = b"\xff\xd8\xff\xe0" + bytes(range(256)) * 40 + b"\xff\xd9"

Expand All @@ -242,6 +242,22 @@ def do_GET(self):
with LOCK:
STATE["requests"].append({"path": "majestic:" + self.path,
"authorized": self.headers.get("Authorization") == want})
if self.path in ("/night/on", "/night/off"):
if self.headers.get("Authorization") != want:
self.send_response(401)
self.send_header("WWW-Authenticate", 'Basic realm="majestic"')
self.send_header("Content-Length", "0")
self.send_header("Connection", "close")
self.end_headers()
return
out = b"ok" if self.path == "/night/on" else b"ok\n" + b"x" * 600
self.send_response(200)
self.send_header("Content-Type", "text/plain")
self.send_header("Content-Length", str(len(out)))
self.send_header("Connection", "close")
self.end_headers()
self.wfile.write(out)
return
if not self.path.startswith("/image.jpg"):
self.send_response(404)
self.send_header("Content-Length", "0")
Expand Down
1 change: 1 addition & 0 deletions tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ MSYS_NO_PATHCONV=1 exec docker run --rm --privileged \
--add-host coordinator.test:127.0.0.1 \
-v "$(host_path .):/src:ro" \
-e REQUIRE_WG="${REQUIRE_WG:-}" \
-e GITHUB_ACTIONS="${GITHUB_ACTIONS:-}" \
"$IMAGE" sh /src/tests/suite.sh
30 changes: 26 additions & 4 deletions tests/suite.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ check() { # check <label> <actual> <expected>
PASS=$((PASS + 1))
else
printf ' FAIL %-52s got [%s], want [%s]\n' "$1" "$2" "$3"
if [ "${GITHUB_ACTIONS:-}" = true ]; then
printf '::error::%s — got [%s], want [%s]\n' "$1" "$2" "$3"
fi
FAIL=$((FAIL + 1))
fi
}
Expand Down Expand Up @@ -317,7 +320,7 @@ check "the key file does not outlive the call" "$(yes_if test -e $T/.wg.key)" "n
# that has everything else it needs.
FULL=$T/full-uboot.env
grep -v '^cerberus_device_pubkey=' enrolled.env >$FULL
FAKE_UBOOT_FULL=cerberus_device_pubkey uboot $FULL $A up >full.log 2>&1
uboot $FULL env FAKE_UBOOT_FULL=cerberus_device_pubkey $A up >full.log 2>&1
check "a full U-Boot env: up still succeeds" "$?" "0"
check "and programs the interface" "$(yes_if grep -qF "dry-run: wg set cbr0 private-key" full.log)" "yes"
check "and says what it could not keep" "$(yes_if grep -q 'fw_setenv cerberus_device_pubkey failed' full.log)" "yes"
Expand Down Expand Up @@ -365,6 +368,21 @@ check "EnableMotion: both majestic keys through cli" "$(tr '
check "and majestic is told to re-read them" "$(cat $T/killall.calls)" "-1 majestic"
check "and says so" "$(yes_if grep -q 'motion detection switched on in majestic' motion-on.log)" "yes"

# Day/night goes to majestic on the snapshot's origin, with the snapshot's password.
NIGHT_SNAP=CERBERUS_SNAPSHOT_URL=http://root:12345@127.0.0.1:8081/image.jpg
ctl command '{"command":"NightModeOn"}'
runfor 7 night-on.log $NIGHT_SNAP
check "NightModeOn: majestic's /night/on, with the snapshot's password" "$(q "print([r['authorized'] for r in R if r['path'] == 'majestic:/night/on'])")" "[True]"
check "and says so, without printing the address it carries a password in" "$(yes_if grep -q 'night mode switched on in majestic' night-on.log)$(yes_if grep -q '12345' night-on.log)" "yesno"
ctl command '{"command":"NightModeOff"}'
runfor 7 night-off.log $NIGHT_SNAP
check "NightModeOff: majestic's /night/off" "$(q "print(sum(r['path'] == 'majestic:/night/off' for r in R))")" "1"
check "even when majestic's answer outgrows the status buffer" \
"$(yes_if grep -q 'day mode switched on in majestic' night-off.log)" "yes"
ctl command '{"command":"NightModeOn"}'
runfor 7 night-nopass.log CERBERUS_SNAPSHOT_URL=http://127.0.0.1:8081/image.jpg
check "without majestic's password: says where to put it" "$(yes_if grep -q 'put it in cerberus_snapshot_url' night-nopass.log)" "yes"

ctl command '{"command":"SelfDestruct"}'
runfor 7 unknown.log
check "an unknown command is ignored, loudly" "$(yes_if grep -q "unknown command 'SelfDestruct'" unknown.log)" "yes"
Expand Down Expand Up @@ -414,19 +432,22 @@ wg_ok() {
&& ip netns add hub 2>/dev/null
}
if wg_ok; then
# Under /etc/wireguard so a host AppArmor profile for /usr/bin/wg (Ubuntu 25.04+) lets wg read both keys.
mkdir -p /etc/wireguard
cp $T/hub.key /etc/wireguard/hub.key
ip link add veth-cam type veth peer name veth-hub
ip link set veth-hub netns hub
ip addr add 192.168.77.1/24 dev veth-cam && ip link set veth-cam up
ip -n hub addr add 192.168.77.2/24 dev veth-hub
ip -n hub link set veth-hub up && ip -n hub link set lo up
ip netns exec hub ip link add wg-hub type wireguard
ip netns exec hub wg set wg-hub private-key $T/hub.key listen-port 51820 \
ip netns exec hub wg set wg-hub private-key /etc/wireguard/hub.key listen-port 51820 \
peer "$PUB" allowed-ips $OVERLAY/32
ip -n hub addr add $HUB_IP/32 dev wg-hub && ip -n hub link set wg-hub up
ip -n hub route add $SUBNET dev wg-hub

cp enrolled.env agent.env
export CERBERUS_DRY_RUN=0 CERBERUS_IFACE=wg0
export CERBERUS_DRY_RUN=0 CERBERUS_IFACE=wg0 CERBERUS_KEY_TMP=/etc/wireguard/.wg.key
$A up >dp-up.log 2>&1
check "wg0 comes up with the overlay address" \
"$(yes_if sh -c "ip -4 -o addr show wg0 | grep -q ' $OVERLAY/24 '")" "yes"
Expand Down Expand Up @@ -458,7 +479,8 @@ if wg_ok; then
check "route restored" "$(yes_if sh -c "ip route show $HUB_IP | grep -q 'dev wg0'")" "yes"

ip link del wg0 2>/dev/null
export CERBERUS_DRY_RUN=1 CERBERUS_IFACE=cbr0
rm -rf /etc/wireguard
export CERBERUS_DRY_RUN=1 CERBERUS_IFACE=cbr0 CERBERUS_KEY_TMP=$T/.wg.key
else
if [ "${REQUIRE_WG:-}" = 1 ]; then
check "kernel WireGuard and netns available" "no" "yes"
Expand Down
Loading