forge
Explore
Log in

Don't you dare go Hollow

im

Clone
★ 0

0 watching0 forksRustAGPL-3.0542.8 KiBadfb39fTahinli1 month ago11 commits

README.md

im

im is the family's sign-in provider. It is an OpenID Connect provider (authorization code with PKCE, EdDSA-signed ID tokens, back-channel logout), and it keeps the people directory, the photos, and the list of family services. The family's apps (in, iz) sign people in through im and never see a password.

It is one Rust binary, im, with a SQLite-format database (Turso), no external services, and no runtime dependencies beyond an SMTP server if you want mail to be sent.

Build

cargo build --release --workspace
install -m 0755 target/release/im /usr/local/bin/im

Run the checks the same way the project does:

cargo fmt --all --check
cargo clippy --workspace --all-targets -- -D warnings
cargo test --workspace

Configuration

The configuration is a TOML file. The default path is config/im.toml; set --config <path> or the IM_CONFIG variable to use another. When the default file is missing, im creates it from config/im.example.toml.

The settings that matter first:

Setting Meaning
database The database file. Keep it inside data_dir.
data_dir Holds the instance key im.key, the photos, and the backups. Created with mode 0700.
listen The loopback address im binds to, for example 127.0.0.1:7650.
base_url The public origin, with no trailing slash. This is the OpenID issuer and the origin of every link in mail.
trusted_proxies The proxies whose X-Forwarded-For is believed. Leave empty when nothing is in front of im.

Secrets never go in the file. Client secrets are shown once and stored as hashes. The SMTP password is read from a file named in the configuration, and the instance key is created on first run.

The [policy] section sets the sign-in policy. The admin UI can change these values at run time; a value saved there overrides the file, and the settings page shows where each value comes from.

First run

  1. Start the server: im --config /etc/im/im.toml serve, or run the systemd unit (below).
  2. Open base_url in a browser. With no account yet, im shows the setup page. Create the first administrator there. Setup is closed once an account exists.
  3. Sign in and turn on two-factor sign-in for the administrator account. The policy requires it for administrators by default.

Registering the family services

Each service that signs people in is an OpenID client. Register it from the shell on the server:

im --config /etc/im/im.toml client add --name "in" \
  --redirect-uri https://in.example.com/auth/callback \
  --post-logout-uri https://in.example.com/ \
  --backchannel-uri https://in.example.com/auth/backchannel-logout \
  --family-key in --public-url https://in.example.com \
  --scopes "openid profile email"

im --config /etc/im/im.toml client add --name "iz" \
  --redirect-uri https://iz.example.com/auth/callback \
  --post-logout-uri https://iz.example.com/ \
  --backchannel-uri https://iz.example.com/auth/backchannel-logout \
  --family-key iz --public-url https://iz.example.com \
  --scopes "openid profile email"
  • The redirect URI is {base}/auth/callback of the service.
  • The post-logout URI is {base}/.
  • The back-channel logout URI is {base}/auth/backchannel-logout.
  • The family key (in, iz) names the service in the switcher. A family key needs --public-url, the origin the switcher links to. The key im is reserved.
  • The command prints the client_id and the client_secret. The secret is shown once; store it in the service's configuration now.

The same clients can be managed in the admin UI under Services, where the secret is also shown once, and where a secret can be rotated (the old one stops working at once) or a client disabled.

Clients are listed with im client list, and disabled with im client disable <client_id>.

Mail

Mail is always queued in the outbox and sent through [smtp]. With host empty no attempt is made, and mail waits in the outbox. Set host, port, tls (implicit for port 465, starttls for 587), from, from_name, and, when the relay needs a login, username and password_file (first line only, not readable by group or others).

The admin pages show the SMTP settings (without the password), the outbox by state, and let an administrator retry a failed mail, give up on one that is not yet sent, and send a test mail to their own address.

Backups and restore

Set [backup] dir to a directory to turn scheduled backups on. Each run writes a set named set-<stamp>: a consistent snapshot of the database (VACUUM INTO), the photos, and a manifest.json with the SHA-256 of every file. interval_hours sets the schedule and keep how many sets are kept; older sets are removed only when they are real sets with a manifest. An optional healthcheck_url is pinged after each run, with the failure URL on error (a dead-man check).

im --config /etc/im/im.toml backup now            # write a set now
im --config /etc/im/im.toml fsck                   # check the database and the signing keys
im --config /etc/im/im.toml restore --from <set>   # verify, then restore

restore verifies the set before it changes anything: every file's hash, the manifest's list of files, and the snapshot's integrity and schema. A tampered set is refused. A verified set replaces the database and the photos; the previous database and photos are moved into a restore-<stamp> directory inside data_dir, so nothing is lost. restore refuses to run while the server (or another im command) holds the data directory, so stop the service first.

Signing keys

im signs tokens with Ed25519 keys. A key signs for 90 days; its successor is created and published a day before it signs, and a retired key stays published for seven days so that tokens it signed still verify. Keys are maintained automatically.

To rotate now (for example after a suspected leak of a key file):

im --config /etc/im/im.toml keys rotate

The new key signs at once and the previous key stops signing at once; it stays published for the overlap. The admin Keys page shows every key, its state, and the same rotation.

Command line

im [--config <path>] [serve]
im [--config <path>] client add --name <name> [--redirect-uri <uri>]... [--post-logout-uri <uri>]...
                         [--backchannel-uri <uri>] [--family-key <key> --public-url <origin>] [--scopes "<scope ...>"]
im [--config <path>] client list
im [--config <path>] client disable <client_id>
im [--config <path>] backup now
im [--config <path>] restore --from <set>
im [--config <path>] fsck
im [--config <path>] keys rotate

The commands that open the database hold the data directory's lock, so they refuse to run beside a running server.

Running as a service

deploy/im.service is a hardened systemd unit. It runs as the im user with /etc/im/im.toml and writes only to /var/lib/im. Create the user and the directories, copy the binary and the configuration, then:

useradd --system --home /var/lib/im --shell /usr/sbin/nologin im
install -d -o im -g im -m 0700 /var/lib/im
install -d -m 0750 /etc/im
install -m 0640 config/im.toml /etc/im/im.toml
install -m 0644 deploy/im.service /etc/systemd/system/im.service
systemctl daemon-reload
systemctl enable --now im

Put a TLS-terminating reverse proxy in front of it. deploy/ferron.kdl is an example Ferron site that forwards to 127.0.0.1:7650; base_url must be the same public origin.

Layout

  • crates/im-core: the domain, the store and its migrations, the configuration, keys, secrets, backups.
  • crates/im-web: the im binary: pages, OpenID Connect endpoints, the admin area, live updates, the mail and back-channel workers, and the command line.
  • docs/: the architecture, the contracts with the family's apps, the conventions, and the attack plan.