13 Commits
Author SHA1 Message Date
agessaman 20788733f5 fix(acl): authenticate by registered type and keep stored grants from blank logins
A room config without a "type" key authenticated as a repeater, so the
empty-guest-password rule admitted blank logins to a room with
allow_read_only off. The login callback now passes the registered type.

A blank-password newcomer on a full table could evict a stored read-only or
read-write grant and delete its row. It now evicts only entries that are not
stored, and is refused when none are left.
2026-10-02 16:07:16 -07:00
agessaman 28e0f87a7e fix(acl): let an admin grant evict the least recently seen admin
A table full of admins refused every newcomer, and now that entries outlive
a restart that lockout was permanent; the upgrade placeholder made it likely
with max_clients: 1. The placeholder is now 32 (firmware MAX_CLIENTS), and
an admin password login or setperm to an admin role evicts the admin seen
least recently, by its stored last login after a restart. Blank-password and
guest newcomers are still refused, so they cannot strip admins one by one.

An eviction now waits for the newcomer: a refused login (a replay) puts the
evicted entry back, and its stored row is deleted only once the newcomer's
grant is stored, or once that grant's next write succeeds after a failure.
Evicted clients' replay watermarks are kept (capped, in RAM) so an evicted
admin's captured login cannot be replayed when it comes back.

unregister_identity also detaches an ACL listed only by name, after a hash
collision displaced it from the hash index.
2026-10-02 10:57:24 +01:00
agessaman 0a1d53f77f fix(acl): keep an evicted entry when the grant that evicted it fails
A setperm on a full ACL deleted the evicted client's stored entry before
trying to store the newcomer, so a failed write reported an error but still
revoked someone. The eviction is now deleted from the store only once the
grant is stored, and put back in the table if it isn't.

Deleting a room whose rename was not hot-reloaded looked up its live ACL by
the new name only, so the ACL listed under the old name stayed attached and
the next grant wrote the deleted room's rows back. Fall back to the store
key, as move_room_acl does.
2026-10-02 10:57:24 +01:00
agessaman 34c307ee33 feat(acl): keep an ACL entry's last login across restarts
Stored entries now record when they last logged in, so the web UI can
show whether a provisioned grant is still in use after a restart.
Firmware keeps this in RAM only; last_activity still starts at 0 on
load, so "not active since restart" keeps its meaning.

- acl_entries gains a nullable last_login column, added beside the
  table's CREATE so a failed earlier migration cannot lock stored
  admins out of a load that reads it.
- A login by a stored entry writes its time, best-effort, and not when
  the stored second already matches it.
- A guest promoted by setperm keeps its login time; a row taken over
  from another identity's label does not inherit that identity's.
- Key changes, renames and label adoption carry the time; a retried
  load keeps the later of the live and stored times and writes a login
  made while the store was unreadable.
2026-10-02 10:57:24 +01:00
agessaman a04a44089d fix(acl): a room never takes over rows already stored under its name
Codex confirmation round: after a delete whose ACL cleanup failed, a room
created on that key and renamed to the deleted room's name took over its
leftover rows. Names are unique, so no live room holds the label a
rename takes or a new room gets; a rename (before taking the label) and
create_identity now drop every row already stored under it.
2026-10-02 10:57:24 +01:00
agessaman 58ec227297 fix(acl): sweep a room's leftover entries whenever it is renamed or rekeyed
Codex confirmation round: rows a rekey's failed cleanup left at an old key
survived a later rename under the old label, which the rename did not
touch and the delete (by the new label) did not reach, so a room given
the old name and key could load them. A room owns only the rows under
its label at its current key; a rename, a rekey and a delete now drop
every other row under the labels involved. A failed sweep is retried by
the next one. Loading does not sweep: a room whose key was edited by hand
would otherwise lose rows that setting the key back recovers.
2026-10-02 10:57:24 +01:00
agessaman 43ead04a07 fix(acl): delete a room's stored entries at every key it used
Codex confirmation round: deleting a room removed only its rows at its
current key. Rows a rekey's failed cleanup left at an old key kept its
label, so a room re-created with that name on that key loaded them. A
delete now drops every row under the room's label; names are unique, so
no other room holds it.
2026-10-02 10:57:24 +01:00
agessaman 44b856024e fix(acl): retire a room's old key and handlers; rows belong to key and label
Codex round 5 (after the usage limit reset):
- A rekeyed or deleted room's old key kept answering logins, messages and
  requests until a restart: its login, text and protocol handlers stayed
  registered. The daemon now unregisters an identity from every packet
  helper (_unregister_identity_everywhere), and update_identity and
  delete_identity call it for the retired identity. Each helper removes
  only its own handler, never another identity's on the same hash byte.
- Rows left at a key by a failed cleanup could grant their admins to a
  later room on that key. A room loads only rows under its own label and
  logs any others; moves copy, relabel and delete only the room's own
  label's rows. A same-key rename relabels before the config is saved
  and puts the label back if the save fails, so a room's own rows follow
  its name. delete_identity reports an ACL cleanup that failed.
- Two overlapping hot reloads could start a superseded replacement
  RoomServer whose sync loop then ran untracked. A swap starts its room
  only if it is still the registered one.
2026-10-02 10:57:24 +01:00
agessaman d5541aca3d fix(acl): let a room's hot reload apply; make an interrupted key change retryable
Fourth review round (opencode; codex hit its usage limit):
- update_identity's hot reload was always refused: the identity manager
  rejects a name or hash already registered, which the running room's own
  entry always is. The ACL name index then lagged the config, so a renamed
  room was unmanageable by name, and a later rekey left a second live,
  writable ACL. The old entry is now unregistered first and restored if
  the new registration is refused (IdentityManager.unregister_identity),
  and a move falls back to the store key when the name misses.
- A key change whose save and cleanup both failed left a copy at the new
  key that blocked every retry. A copy under the room's own label is
  replaced on retry; another identity's entries still refuse the move.
- A registration now detaches the ACL it replaces before loading, warns
  when it displaces another identity sharing its hash byte, and the no-store
  guard for a key change lives in LoginHelper.move_room_acl.
- /acl_stats is documented as it has always answered; store_error and
  store_errors say they clear once a later read succeeds; the example
  config says room servers keep their own security settings.
2026-10-02 10:48:10 +01:00
agessaman 583b438bdb fix(acl): move room ACLs around the config save; keep one ACL per identity
Third review round (codex, opencode):
- A failed save whose rollback also failed left the ACL under a key the
  config did not name. A key change now copies the entries, saves the
  config, then drops the old ones, all under the ACL's lock: any failure
  leaves the key the saved config names holding its entries.
- Moves and deletes found the live ACL by scanning for its store key, and a
  rekey left two ACL objects for one identity, so a second move or a delete
  could act on the stale one. LoginHelper finds ACLs by name, and a new ACL
  detaches and drops the one it replaces.
- A room deleted and re-added with the same key reused the detached ACL and
  never stored again. A detached ACL is never reused.
- A failed read at startup was never retried; the next login or change
  retries it, and acl_clients reports it as store_errors.
- A key change with no store anywhere was treated as moved. It is refused.
- The owner lookup fell back to the 1-byte hash for a room with no ACL,
  attributing another identity's entries to it. acl_stats used the hash too.
- A refused update_identity left its merged settings in the live config;
  every refusal now restores the entry. A key that yields no public key is
  refused rather than skipping the shared-key check.
- Removing from every ACL stopped at the first failed write; it now reports
  which failed and which succeeded. Room eviction carries on past one.
- Settings on a reused ACL are applied under its lock.
2026-10-02 10:48:10 +01:00
agessaman faaf7f2213 fix(acl): make room ACL moves atomic, refuse shared keys, surface load errors
Second review round (codex, opencode):
- Moving a room's rows and pointing its live ACL at them happened in two
  steps, so a grant could land under the old key in between.
  ACL.move_store now does both under the ACL's lock.
- update_identity accepted a key another identity already uses, and the
  move merged the two ACLs. It now refuses such a key before changing
  anything, and the store refuses to move onto a key that has entries.
- update_identity saved the new key before moving the ACL, so a failed move
  left a saved key with an empty ACL. The move now runs first, and a failed
  save moves it back. It also runs when the daemon has no login helper.
- A failed read at startup looked like an empty ACL. The ACL records the
  error and /api/acl_info reports it as store_error.
- A deleted room's handlers stay live until a restart, and a login there
  wrote its entries back. Deleting now detaches the live ACL from storage.
- /api/acl_info looked ACLs up by the 1-byte hash, so identities sharing one
  could show each other's counts. It uses the name lookup acl_clients does.
- The acl_clients filter is an object in the spec, as returned.
2026-10-02 10:48:10 +01:00
agessaman e0e7cbd9e3 fix(acl): address review of persistent ACLs; add ACL management API
Review (codex, opencode) found:
- A room renamed and rekeyed in one update lost its ACL, and a room later
  created under the old name could adopt it. Room servers are no longer
  adopted by label; update_identity moves their rows to the new key and
  name in one transaction and points the live ACL at them. Only the
  repeater, which is unique, still adopts by label after a key change.
- A removal racing a grant could leave the grant stored but not listed.
  Each ACL change and its write now happen under one lock.
- A failed write was reported as OK. The store now raises; setperm answers
  "Err - failed to save" and leaves the table as it was, and the API
  returns an error. A login still succeeds, as its session is valid.
- Re-registering an identity (a rename) built a new ACL, zeroing every
  session's activity and replay watermark, so the room sync loop stopped
  pushing to logged-in clients. The live ACL is now reused.
- The repeater's max_clients default rises to firmware's 32: admins are
  never evicted and now persist, so 5 or 10 filled permanently. Stored
  entries past max_clients all load, with a warning.
- atoi takes ASCII digits only; non-repeater identity types get their own
  label; the sync-loop skip logs at debug; `get acl` has help.

API for the web UI:
- POST /api/acl_set_permissions sets an entry by identity name.
- /api/acl_clients lists entries by identity name rather than the 1-byte
  hash two identities can share, with permissions_value, persisted and
  identity_pubkey. /api/acl_info adds acl_entries and stored_entries, and
  authenticated_clients counts entries that have been active.
- /api/acl_remove_client reads client_pubkey, as its schema always said
  (the UI sent it, and the endpoint read only public_key), and takes
  identity_name.
2026-10-02 10:48:10 +01:00
agessaman 2050f1b71e feat(acl): keep ACL entries across restarts and implement setperm
Firmware keeps its ACL in /s_contacts, so a key provisioned over serial with
`setperm <pubkey> 3` logs in with a blank password forever after. Ours was
RAM-only and setperm was a stub, so that workflow could not work.

- New acl_entries table, keyed by the local identity's full 32-byte public
  key rather than its 1-byte hash, which collides between identities. An
  identity_label ("repeater", "room_server:<name>") lets an identity adopt
  its rows after a key change, as firmware's ACL survives a new private key.
  Shared secrets are derived on load, never stored.
- The repeater stores every entry with permissions, a room server admins
  only (firmware saveFilter). A write happens only when an entry's stored
  permissions change, so a returning admin costs no I/O.
- setperm follows ClientACL::applyPermissions and firmware's replies: atoi
  permissions stored as the whole byte, a guest role deletes by key prefix,
  other roles need the full key. It refuses an empty prefix and a key that
  is not a valid ed25519 point, both of which firmware accepts.
- `get acl` works from the web CLI (the serial stand-in, passed local=True)
  and still errors remotely. It was unreachable: "get " matched it first.
- A full table evicts the least active non-admin, as firmware putClient,
  instead of refusing; it still refuses when every entry is an admin.
- Room servers keep a stored admin through inactivity eviction and do not
  push to entries that have not logged in since load, as firmware skips
  clients with no activity.
- Deleting a room server drops its stored entries.
2026-10-02 10:48:10 +01:00