im
Clone
0 watching0 forksRustAGPL-3.0542.8 KiBb9f0726Tahinli27 days ago34 commits
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. Behind Ferron on the same host, ["127.0.0.1/32"]; empty only when nothing is in front of im, otherwise every visitor shares the proxy's address and one visitor's failed sign-ins throttle everyone. The proxy must append the client address to X-Forwarded-For, never pass a client's own header through unchanged. |
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
- Start the server:
im --config /etc/im/im.toml serve, or run the systemd unit (below). - Open
base_urlin a browser. With no account yet,imshows the setup page. Create the first administrator there. Setup is closed once an account exists. - 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. The CLI
opens the database itself, so run it while im serve is stopped (or use the admin UI under Services):
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 directory.read"
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 directory.read files.service"
- The redirect URI is
{base}/auth/callbackof 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 keyimis reserved. - The command prints the
client_idand theclient_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 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, none only for a relay on
loopback), 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).
While the server runs, an administrator can start a backup from the admin Health page with the "Back up now"
button (it appears when [backup] dir is set). The running backup worker writes the set in the background, and
the page shows the last success or error once the run ends. Only one backup runs at a time: a press while one is
pending or running starts nothing. The button is audited.
im backup now writes a set from the command line, but only while the server is stopped, because the command
needs the data directory's lock.
im --config /etc/im/im.toml backup now # write a set now (server stopped only)
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. A set written before the manifest MAC was added has no manifest.mac and is refused; take a new backup instead.
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, after a suspected leak of a key file:
im --config /etc/im/im.toml keys rotate
A manual rotation is immediate. The new key signs from that moment. The previous signing key, any key still
waiting to sign, and any retired key still published are withdrawn at once: they leave the published keys,
their rows are deleted, and they never sign or verify again. Treat them as compromised. Deleted bytes can
linger in free database pages until they are reused, so a copy of the whole data directory (database and
im.key) may still yield a withdrawn seed; no relying party trusts it any more. The result is
exactly one published key. The admin Keys page offers the same rotation. Both are audited (keys.rotated,
listing the withdrawn key ids).
Relying parties (in, iz) cache the JWKS for 10 minutes and refetch at most once a minute when a token names
an unknown kid. So new tokens work within about a minute, and tokens signed by a withdrawn key stop verifying
within 10 minutes (im itself stops accepting them at once). People who are signing in at that moment may need
to retry once.
Rotation is the emergency action for a leaked signing key. If the whole server leaked (im.key and the
database), rotation is not enough: restore a backup onto a fresh machine and reset passwords.
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 -o root -g im -m 0750 /etc/im
install -o root -g im -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.conf is an example Ferron site that
forwards to 127.0.0.1:7650; base_url must be the same public origin. Three things matter in the proxy:
- Redirect plain HTTP to HTTPS.
imsends HSTS itself whenbase_urlishttps://. - Do not buffer responses: the live update streams depend on it.
- Keep secrets out of the access log. Reset, invitation and email-change links carry a one-time token in
the path (
/reset/…,/invite/…,/email/…), so log without the path for those prefixes, or not at all.
As a user service (with kur)
deploy/user/im.service runs im without root, as an ordinary account (here apps) under its own systemd
user manager. As root:
useradd -m apps
loginctl enable-linger apps
Then, in a session of that user (sudo machinectl shell apps@):
install -d ~/bin ~/.config/im ~/.config/systemd/user
install -m 0755 <built im binary> ~/bin/im
install -m 0600 config/im.toml ~/.config/im/im.toml
install -m 0644 deploy/user/im.service ~/.config/systemd/user/
systemctl --user daemon-reload
systemctl --user enable --now im.service
Set database, data_dir and [backup] dir in ~/.config/im/im.toml to absolute paths under
~/.local/state/im, because im does not expand ~. kur keeps it
updated from im's v* releases: give it an app with path = "~/bin/im", unit = "im.service", and
health_url set to listen with /healthz appended (for example http://127.0.0.1:7650/healthz).
Layout
crates/im-core: the domain, the store and its migrations, the configuration, keys, secrets, backups.crates/im-web: theimbinary: 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.