forge
Explore
Log in

Don't you dare go Hollow

im

Clone
★ 0

0 watching0 forksRustAGPL-3.0542.8 KiBb9f0726Tahinli27 days ago34 commits

README.md227 lines, 11.2 KiBHistoryRawDownload
1# im
2
3`im` is the family's sign-in provider. It is an OpenID Connect provider (authorization code with PKCE,
4EdDSA-signed ID tokens, back-channel logout), and it keeps the people directory, the photos, and the list of
5family services. The family's apps (`in`, `iz`) sign people in through `im` and never see a password.
6
7It is one Rust binary, `im`, with a SQLite-format database (Turso), no external services, and no runtime
8dependencies beyond an SMTP server if you want mail to be sent.
9
10## Build
11
12```sh
13cargo build --release --workspace
14install -m 0755 target/release/im /usr/local/bin/im
15```
16
17Run the checks the same way the project does:
18
19```sh
20cargo fmt --all --check
21cargo clippy --workspace --all-targets -- -D warnings
22cargo test --workspace
23```
24
25## Configuration
26
27The configuration is a TOML file. The default path is `config/im.toml`; set `--config <path>` or the
28`IM_CONFIG` variable to use another. When the default file is missing, `im` creates it from
29`config/im.example.toml`.
30
31The settings that matter first:
32
33| Setting | Meaning |
34| --- | --- |
35| `database` | The database file. Keep it inside `data_dir`. |
36| `data_dir` | Holds the instance key `im.key`, the photos, and the backups. Created with mode 0700. |
37| `listen` | The loopback address `im` binds to, for example `127.0.0.1:7650`. |
38| `base_url` | The public origin, with no trailing slash. This is the OpenID issuer and the origin of every link in mail. |
39| `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. |
40
41Secrets never go in the file. Client secrets are shown once and stored as hashes. The SMTP password is read
42from a file named in the configuration, and the instance key is created on first run.
43
44The `[policy]` section sets the sign-in policy. The admin UI can change these values at run time; a value
45saved there overrides the file, and the settings page shows where each value comes from.
46
47## First run
48
491. Start the server: `im --config /etc/im/im.toml serve`, or run the systemd unit (below).
502. Open `base_url` in a browser. With no account yet, `im` shows the setup page. Create the first
51 administrator there. Setup is closed once an account exists.
523. Sign in and turn on two-factor sign-in for the administrator account. The policy requires it for
53 administrators by default.
54
55## Registering the family services
56
57Each service that signs people in is an OpenID client. Register it from the shell on the server. The CLI
58opens the database itself, so run it while `im serve` is stopped (or use the admin UI under Services):
59
60```sh
61im --config /etc/im/im.toml client add --name "in" \
62 --redirect-uri https://in.example.com/auth/callback \
63 --post-logout-uri https://in.example.com/ \
64 --backchannel-uri https://in.example.com/auth/backchannel-logout \
65 --family-key in --public-url https://in.example.com \
66 --scopes "openid profile email directory.read"
67
68im --config /etc/im/im.toml client add --name "iz" \
69 --redirect-uri https://iz.example.com/auth/callback \
70 --post-logout-uri https://iz.example.com/ \
71 --backchannel-uri https://iz.example.com/auth/backchannel-logout \
72 --family-key iz --public-url https://iz.example.com \
73 --scopes "openid profile email directory.read files.service"
74```
75
76- The redirect URI is `{base}/auth/callback` of the service.
77- The post-logout URI is `{base}/`.
78- The back-channel logout URI is `{base}/auth/backchannel-logout`.
79- The family key (`in`, `iz`) names the service in the switcher. A family key needs `--public-url`, the
80 origin the switcher links to. The key `im` is reserved.
81- The command prints the `client_id` and the `client_secret`. The secret is shown once; store it in the
82 service's configuration now.
83
84The same clients can be managed in the admin UI under Services, where the secret is also shown once, and
85where a secret can be rotated (the old one stops working at once) or a client disabled.
86
87Clients are listed with `im client list`, and disabled with `im client disable <client_id>`.
88
89## Mail
90
91Mail is always queued in the outbox and sent through `[smtp]`. With `host` empty no attempt is made, and mail
92waits in the outbox. Set `host`, `port`, `tls` (`implicit` for port 465, `starttls` for 587, `none` only for a relay on
93loopback), `from`,
94`from_name`, and, when the relay needs a login, `username` and `password_file` (first line only, not readable
95by group or others).
96
97The admin pages show the SMTP settings (without the password), the outbox by state, and let an administrator
98retry a failed mail, give up on one that is not yet sent, and send a test mail to their own address.
99
100## Backups and restore
101
102Set `[backup] dir` to a directory to turn scheduled backups on. Each run writes a set named `set-<stamp>`:
103a consistent snapshot of the database (`VACUUM INTO`), the photos, and a `manifest.json` with the SHA-256 of
104every file. `interval_hours` sets the schedule and `keep` how many sets are kept; older sets are removed
105only when they are real sets with a manifest. An optional `healthcheck_url` is pinged after each run, with
106the failure URL on error (a dead-man check).
107
108While the server runs, an administrator can start a backup from the admin Health page with the "Back up now"
109button (it appears when `[backup] dir` is set). The running backup worker writes the set in the background, and
110the page shows the last success or error once the run ends. Only one backup runs at a time: a press while one is
111pending or running starts nothing. The button is audited.
112
113`im backup now` writes a set from the command line, but only while the server is stopped, because the command
114needs the data directory's lock.
115
116```sh
117im --config /etc/im/im.toml backup now # write a set now (server stopped only)
118im --config /etc/im/im.toml fsck # check the database and the signing keys
119im --config /etc/im/im.toml restore --from <set> # verify, then restore
120```
121
122`restore` verifies the set before it changes anything: every file's hash, the manifest's list of files, and
123the snapshot's integrity and schema. A tampered set is refused. A verified set replaces the database and the
124photos; the previous database and photos are moved into a `restore-<stamp>` directory inside `data_dir`, so
125nothing is lost. `restore` refuses to run while the server (or another `im` command) holds the data directory,
126so 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.
127
128## Signing keys
129
130`im` signs tokens with Ed25519 keys. A key signs for 90 days; its successor is created and published a day
131before it signs, and a retired key stays published for seven days so that tokens it signed still verify.
132Keys are maintained automatically.
133
134To rotate now, after a suspected leak of a key file:
135
136```sh
137im --config /etc/im/im.toml keys rotate
138```
139
140A manual rotation is immediate. The new key signs from that moment. The previous signing key, any key still
141waiting to sign, and any retired key still published are withdrawn at once: they leave the published keys,
142their rows are deleted, and they never sign or verify again. Treat them as compromised. Deleted bytes can
143linger in free database pages until they are reused, so a copy of the whole data directory (database and
144`im.key`) may still yield a withdrawn seed; no relying party trusts it any more. The result is
145exactly one published key. The admin Keys page offers the same rotation. Both are audited (`keys.rotated`,
146listing the withdrawn key ids).
147
148Relying parties (`in`, `iz`) cache the JWKS for 10 minutes and refetch at most once a minute when a token names
149an unknown `kid`. So new tokens work within about a minute, and tokens signed by a withdrawn key stop verifying
150within 10 minutes (`im` itself stops accepting them at once). People who are signing in at that moment may need
151to retry once.
152
153Rotation is the emergency action for a leaked signing key. If the whole server leaked (`im.key` and the
154database), rotation is not enough: restore a backup onto a fresh machine and reset passwords.
155
156## Command line
157
158```text
159im [--config <path>] [serve]
160im [--config <path>] client add --name <name> [--redirect-uri <uri>]... [--post-logout-uri <uri>]...
161 [--backchannel-uri <uri>] [--family-key <key> --public-url <origin>] [--scopes "<scope ...>"]
162im [--config <path>] client list
163im [--config <path>] client disable <client_id>
164im [--config <path>] backup now
165im [--config <path>] restore --from <set>
166im [--config <path>] fsck
167im [--config <path>] keys rotate
168```
169
170The commands that open the database hold the data directory's lock, so they refuse to run beside a running
171server.
172
173## Running as a service
174
175`deploy/im.service` is a hardened systemd unit. It runs as the `im` user with `/etc/im/im.toml` and writes
176only to `/var/lib/im`. Create the user and the directories, copy the binary and the configuration, then:
177
178```sh
179useradd --system --home /var/lib/im --shell /usr/sbin/nologin im
180install -d -o im -g im -m 0700 /var/lib/im
181install -d -o root -g im -m 0750 /etc/im
182install -o root -g im -m 0640 config/im.toml /etc/im/im.toml
183install -m 0644 deploy/im.service /etc/systemd/system/im.service
184systemctl daemon-reload
185systemctl enable --now im
186```
187
188Put a TLS-terminating reverse proxy in front of it. `deploy/ferron.conf` is an example Ferron site that
189forwards to `127.0.0.1:7650`; `base_url` must be the same public origin. Three things matter in the proxy:
190
191- Redirect plain HTTP to HTTPS. `im` sends HSTS itself when `base_url` is `https://`.
192- Do not buffer responses: the live update streams depend on it.
193- Keep secrets out of the access log. Reset, invitation and email-change links carry a one-time token in
194 the path (`/reset/…`, `/invite/…`, `/email/…`), so log without the path for those prefixes, or not at all.
195
196### As a user service (with kur)
197
198`deploy/user/im.service` runs `im` without root, as an ordinary account (here `apps`) under its own systemd
199user manager. As root:
200
201```sh
202useradd -m apps
203loginctl enable-linger apps
204```
205
206Then, in a session of that user (`sudo machinectl shell apps@`):
207
208```sh
209install -d ~/bin ~/.config/im ~/.config/systemd/user
210install -m 0755 <built im binary> ~/bin/im
211install -m 0600 config/im.toml ~/.config/im/im.toml
212install -m 0644 deploy/user/im.service ~/.config/systemd/user/
213systemctl --user daemon-reload
214systemctl --user enable --now im.service
215```
216
217Set `database`, `data_dir` and `[backup] dir` in `~/.config/im/im.toml` to absolute paths under
218`~/.local/state/im`, because `im` does not expand `~`. [kur]https://git.tahinli.com/Tahinli/kur keeps it
219updated from im's `v*` releases: give it an app with `path = "~/bin/im"`, `unit = "im.service"`, and
220`health_url` set to `listen` with `/healthz` appended (for example `http://127.0.0.1:7650/healthz`).
221
222## Layout
223
224- `crates/im-core`: the domain, the store and its migrations, the configuration, keys, secrets, backups.
225- `crates/im-web`: the `im` binary: pages, OpenID Connect endpoints, the admin area, live updates, the mail
226 and back-channel workers, and the command line.
227- `docs/`: the architecture, the contracts with the family's apps, the conventions, and the attack plan.