im
Clone
https://git.tahinli.com/Tahinli/im.git
0 watching·0 forks·Rust·AGPL-3.0·542.8 KiB·b9f0726·Tahinli·27 days ago·34 commits
| 1 | # im |
| 2 | |
| 3 | `im` is the family's sign-in provider. It is an OpenID Connect provider (authorization code with PKCE, |
| 4 | EdDSA-signed ID tokens, back-channel logout), and it keeps the people directory, the photos, and the list of |
| 5 | family services. The family's apps (`in`, `iz`) sign people in through `im` and never see a password. |
| 6 | |
| 7 | It is one Rust binary, `im`, with a SQLite-format database (Turso), no external services, and no runtime |
| 8 | dependencies beyond an SMTP server if you want mail to be sent. |
| 9 | |
| 10 | ## Build |
| 11 | |
| 12 | ```sh |
| 13 | cargo build --release --workspace |
| 14 | install -m 0755 target/release/im /usr/local/bin/im |
| 15 | ``` |
| 16 | |
| 17 | Run the checks the same way the project does: |
| 18 | |
| 19 | ```sh |
| 20 | cargo fmt --all --check |
| 21 | cargo clippy --workspace --all-targets -- -D warnings |
| 22 | cargo test --workspace |
| 23 | ``` |
| 24 | |
| 25 | ## Configuration |
| 26 | |
| 27 | The 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 | |
| 31 | The 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 | |
| 41 | Secrets never go in the file. Client secrets are shown once and stored as hashes. The SMTP password is read |
| 42 | from a file named in the configuration, and the instance key is created on first run. |
| 43 | |
| 44 | The `[policy]` section sets the sign-in policy. The admin UI can change these values at run time; a value |
| 45 | saved there overrides the file, and the settings page shows where each value comes from. |
| 46 | |
| 47 | ## First run |
| 48 | |
| 49 | 1. Start the server: `im --config /etc/im/im.toml serve`, or run the systemd unit (below). |
| 50 | 2. 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. |
| 52 | 3. 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 | |
| 57 | Each service that signs people in is an OpenID client. Register it from the shell on the server. The CLI |
| 58 | opens the database itself, so run it while `im serve` is stopped (or use the admin UI under Services): |
| 59 | |
| 60 | ```sh |
| 61 | im --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 | |
| 68 | im --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 | |
| 84 | The same clients can be managed in the admin UI under Services, where the secret is also shown once, and |
| 85 | where a secret can be rotated (the old one stops working at once) or a client disabled. |
| 86 | |
| 87 | Clients are listed with `im client list`, and disabled with `im client disable <client_id>`. |
| 88 | |
| 89 | |
| 90 | |
| 91 | Mail is always queued in the outbox and sent through `[smtp]`. With `host` empty no attempt is made, and mail |
| 92 | waits in the outbox. Set `host`, `port`, `tls` (`implicit` for port 465, `starttls` for 587, `none` only for a relay on |
| 93 | loopback), `from`, |
| 94 | `from_name`, and, when the relay needs a login, `username` and `password_file` (first line only, not readable |
| 95 | by group or others). |
| 96 | |
| 97 | The admin pages show the SMTP settings (without the password), the outbox by state, and let an administrator |
| 98 | retry 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 | |
| 102 | Set `[backup] dir` to a directory to turn scheduled backups on. Each run writes a set named `set-<stamp>`: |
| 103 | a consistent snapshot of the database (`VACUUM INTO`), the photos, and a `manifest.json` with the SHA-256 of |
| 104 | every file. `interval_hours` sets the schedule and `keep` how many sets are kept; older sets are removed |
| 105 | only when they are real sets with a manifest. An optional `healthcheck_url` is pinged after each run, with |
| 106 | the failure URL on error (a dead-man check). |
| 107 | |
| 108 | While the server runs, an administrator can start a backup from the admin Health page with the "Back up now" |
| 109 | button (it appears when `[backup] dir` is set). The running backup worker writes the set in the background, and |
| 110 | the page shows the last success or error once the run ends. Only one backup runs at a time: a press while one is |
| 111 | pending 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 |
| 114 | needs the data directory's lock. |
| 115 | |
| 116 | ```sh |
| 117 | im --config /etc/im/im.toml backup now # write a set now (server stopped only) |
| 118 | im --config /etc/im/im.toml fsck # check the database and the signing keys |
| 119 | im --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 |
| 123 | the snapshot's integrity and schema. A tampered set is refused. A verified set replaces the database and the |
| 124 | photos; the previous database and photos are moved into a `restore-<stamp>` directory inside `data_dir`, so |
| 125 | nothing is lost. `restore` refuses to run while the server (or another `im` command) holds the data directory, |
| 126 | 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. |
| 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 |
| 131 | before it signs, and a retired key stays published for seven days so that tokens it signed still verify. |
| 132 | Keys are maintained automatically. |
| 133 | |
| 134 | To rotate now, after a suspected leak of a key file: |
| 135 | |
| 136 | ```sh |
| 137 | im --config /etc/im/im.toml keys rotate |
| 138 | ``` |
| 139 | |
| 140 | A manual rotation is immediate. The new key signs from that moment. The previous signing key, any key still |
| 141 | waiting to sign, and any retired key still published are withdrawn at once: they leave the published keys, |
| 142 | their rows are deleted, and they never sign or verify again. Treat them as compromised. Deleted bytes can |
| 143 | linger 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 |
| 145 | exactly one published key. The admin Keys page offers the same rotation. Both are audited (`keys.rotated`, |
| 146 | listing the withdrawn key ids). |
| 147 | |
| 148 | Relying parties (`in`, `iz`) cache the JWKS for 10 minutes and refetch at most once a minute when a token names |
| 149 | an unknown `kid`. So new tokens work within about a minute, and tokens signed by a withdrawn key stop verifying |
| 150 | within 10 minutes (`im` itself stops accepting them at once). People who are signing in at that moment may need |
| 151 | to retry once. |
| 152 | |
| 153 | Rotation is the emergency action for a leaked signing key. If the whole server leaked (`im.key` and the |
| 154 | database), rotation is not enough: restore a backup onto a fresh machine and reset passwords. |
| 155 | |
| 156 | ## Command line |
| 157 | |
| 158 | ```text |
| 159 | im [--config <path>] [serve] |
| 160 | im [--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 ...>"] |
| 162 | im [--config <path>] client list |
| 163 | im [--config <path>] client disable <client_id> |
| 164 | im [--config <path>] backup now |
| 165 | im [--config <path>] restore --from <set> |
| 166 | im [--config <path>] fsck |
| 167 | im [--config <path>] keys rotate |
| 168 | ``` |
| 169 | |
| 170 | The commands that open the database hold the data directory's lock, so they refuse to run beside a running |
| 171 | server. |
| 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 |
| 176 | only to `/var/lib/im`. Create the user and the directories, copy the binary and the configuration, then: |
| 177 | |
| 178 | ```sh |
| 179 | useradd --system --home /var/lib/im --shell /usr/sbin/nologin im |
| 180 | install -d -o im -g im -m 0700 /var/lib/im |
| 181 | install -d -o root -g im -m 0750 /etc/im |
| 182 | install -o root -g im -m 0640 config/im.toml /etc/im/im.toml |
| 183 | install -m 0644 deploy/im.service /etc/systemd/system/im.service |
| 184 | systemctl daemon-reload |
| 185 | systemctl enable --now im |
| 186 | ``` |
| 187 | |
| 188 | Put a TLS-terminating reverse proxy in front of it. `deploy/ferron.conf` is an example Ferron site that |
| 189 | forwards 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 |
| 199 | user manager. As root: |
| 200 | |
| 201 | ```sh |
| 202 | useradd -m apps |
| 203 | loginctl enable-linger apps |
| 204 | ``` |
| 205 | |
| 206 | Then, in a session of that user (`sudo machinectl shell apps@`): |
| 207 | |
| 208 | ```sh |
| 209 | install -d ~/bin ~/.config/im ~/.config/systemd/user |
| 210 | install -m 0755 <built im binary> ~/bin/im |
| 211 | install -m 0600 config/im.toml ~/.config/im/im.toml |
| 212 | install -m 0644 deploy/user/im.service ~/.config/systemd/user/ |
| 213 | systemctl --user daemon-reload |
| 214 | systemctl --user enable --now im.service |
| 215 | ``` |
| 216 | |
| 217 | Set `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 |
| 219 | updated 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. |