1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// Copyright (c) 2026 Erik Lening (hollowpointer) and Contributors
//
// This file is part of Zond Engine, licensed under the GNU Affero General
// Public License, version 3 or later. See the LICENSE file for details, or
// <https://www.gnu.org/licenses/agpl-3.0.html>.
//
// SPDX-License-Identifier: AGPL-3.0-or-later
//! # Where a scan's journal lives
//!
//! The sibling of `import::settings::paths` — which is behind the
//! `import-settings` feature, so this is not linked — and deliberately not the
//! same directory.
//!
//! ## State is not configuration
//!
//! A settings file is hand-written: somebody opens it, edits it, and expects to
//! find it where every other command-line tool keeps one. That is the whole
//! argument the settings module makes for putting it under `~/.config` even on
//! macOS, where the platform convention says otherwise.
//!
//! **That argument does not extend to here, and inverts.** Nobody hand-edits a
//! checkpoint bitmap. A journal is machine-written, machine-read, disposable
//! after a scan completes, and of no interest to a human except through
//! whatever lists them. The specification has a directory for exactly that, and
//! it is not the configuration one.
//!
//! | | Journal root |
//! |---|---|
//! | Unix (incl. macOS) | `$XDG_STATE_HOME/zond/journals`, else `$HOME/.local/state/zond/journals` |
//! | Windows | `%LOCALAPPDATA%\zond\journals` |
//!
//! `%LOCALAPPDATA%` rather than the `%APPDATA%` the settings module uses, and
//! the difference is the point: `%APPDATA%` roams between machines on a domain
//! profile. A journal holds the addresses an engagement was pointed at, and a
//! roaming profile carrying those to another workstation is a data-handling
//! incident rather than a convenience.
//!
//! ## `sudo` is the case this module exists for
//!
//! Every raw strategy needs root, so most scans are run under `sudo`, and under
//! `sudo` `$HOME` is root's. Left alone, journals would be written to
//! `/root/.local/state/zond/journals`, while anything that lists them — which needs
//! no privilege, and so is not normally run under `sudo` — reads the invoking
//! user's directory and finds nothing. The feature would appear broken to most of
//! its users on first contact, and the data would be sitting somewhere they did
//! not look.
//!
//! So when this process is running elevated *and* the environment names the user
//! who invoked it, [`root`] resolves that user's home rather than root's, and
//! [`invoking_user`] reports the ownership a caller should then write with. The
//! caller does the `chown`; this module only says who.
//!
//! ## What is pure and what is not
//!
//! Every function here is pure computation over the environment, with one
//! documented exception: [`invoking_user`] consults the password database to
//! turn a uid into a home directory. That is a lookup, not a filesystem
//! traversal — it opens no path, tests no path for existence, and creates
//! nothing, which are the properties the settings module's purity rule is
//! actually protecting. Constructing `/home/<name>` by hand instead would be
//! pure and wrong: it is not where macOS puts homes, and it is not where a
//! directory-service or relocated home lives on either platform.
//!
//! Nothing here creates a directory. A caller that means to write asks
//! [`root`] where, and creates it with the modes the journal requires.
use PathBuf;
/// The directory this crate's state lives under, within whatever state root
/// applies. Shared with the settings module by name and not by location: one
/// vendor directory, two roots, so `zond/` means the same thing in both.
const DIRECTORY: &str = "zond";
/// The subdirectory holding one journal per scan.
///
/// Named rather than implied because the state root is going to acquire
/// neighbours — a fingerprint submission queue is the obvious next one — and
/// journals that had claimed the root would have to move when it did.
///
/// `journals` rather than `scans`, so that everything from this module to
/// whatever a front end calls its subcommand uses one word for one thing. A
/// directory of `scans` beside a `Journal` type invites the reader to wonder
/// what the difference is.
const JOURNALS: &str = "journals";
/// Who invoked a process that is now running elevated.
///
/// Returned by [`invoking_user`], and carried as a whole rather than as three
/// loose values because a caller that uses the home directory must also apply
/// the ownership: a journal written into somebody's home and left owned by root
/// is a directory they cannot prune, which is worse than not having written it
/// there.
/// The root directory holding one subdirectory per scan.
///
/// `None` when the environment names no home at all, which happens in a
/// container or a daemon with a cleared environment. A caller getting `None`
/// should carry on without a journal rather than invent a location — a scan
/// that cannot be resumed is a smaller failure than a scan that writes an
/// engagement's targets somewhere nobody chose.
///
/// Under `sudo`, this is the *invoking* user's directory. See the module
/// documentation.
/// Where one scan's directory would be, given its id.
///
/// The id is joined as a single component and is expected to be one: a ULID, as
/// the journal writes. This does not validate that, because a path is not the
/// place to enforce it — the caller that mints or parses an id does.
/// The state root this crate's directory sits under, before `zond/` is joined.
///
/// Split out from [`root`] so the platform rules and the `sudo` rule are one
/// expression each rather than one nested expression.
/// Picks the state root from the three places it can come from.
///
/// Pure, so the precedence can be tested without a process's environment — which
/// is shared, and which a test cannot change without changing it for every other
/// test running beside it.
///
/// **`XDG_STATE_HOME` leads, including under `sudo`.** It reaches an elevated
/// process only if somebody preserved it deliberately, and honouring it is what
/// makes an elevated scan and an unelevated listing agree. Reading the invoking
/// user first meant a scan run with `sudo` wrote under `~/.local/state` while a
/// listing read `$XDG_STATE_HOME`, and the listing reported no scans at all.
///
/// After that the invoking user comes before this process's own `HOME`, which
/// under `sudo` is root's and is not who asked.
/// The user who invoked this process, when it is running elevated on their
/// behalf and they can be identified.
///
/// `None` — which is the ordinary case — when any of the following holds, and
/// each is a reason to use this process's own environment instead:
///
/// - the process is not running as root, so nothing was elevated;
/// - `SUDO_UID` is absent, unparseable, or names root itself, so either this is
/// not a `sudo` invocation or root invoked it directly;
/// - the password database has no entry for that uid, or the entry names no
/// home directory, or names a relative one.
///
/// ## On trusting `SUDO_UID`
///
/// `sudo` sets it after clearing the environment, so under the invocation this
/// is written for it is `sudo`'s own value rather than the caller's. It is
/// still only consulted to *narrow* privilege — the worst a wrong value can do
/// is put the journal in the wrong user's home, never widen what the scan may
/// do — and the home directory itself comes from the password database rather
/// than from the environment, so a `SUDO_HOME` pointing anywhere is not
/// consulted at all.
/// Windows has no `sudo`: an elevated process keeps the invoking user's
/// profile, so there is never a different user to resolve.
/// The home directory and primary gid the password database records for `uid`.
///
/// The one impure function in this module; see the module documentation for why
/// the alternative is worse.
// ╔════════════════════════════════════════════╗
// ║ ████████╗███████╗███████╗████████╗███████╗ ║
// ║ ╚══██╔══╝██╔════╝██╔════╝╚══██╔══╝██╔════╝ ║
// ║ ██║ █████╗ ███████╗ ██║ ███████╗ ║
// ║ ██║ ██╔══╝ ╚════██║ ██║ ╚════██║ ║
// ║ ██║ ███████╗███████║ ██║ ███████║ ║
// ║ ╚═╝ ╚══════╝╚══════╝ ╚═╝ ╚══════╝ ║
// ╚════════════════════════════════════════════╝