nibkeep-cli 0.1.0

nib, the generic command-line tool for NibKeep
Documentation
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//! What commands need from the world: randomness, the time, passphrases, keystores and the
//! clipboard.

use std::io::{self, IsTerminal, Write};
use std::path::{Path, PathBuf};
use std::sync::mpsc;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use getrandom::SysRng;
use nibkeep_crypto::rand_core::{CryptoRng, UnwrapErr};
use nibkeep_node::at_login::{self, LoginEntry};
use nibkeep_node::{NodeError, PassphraseParams};
use nibkeep_passwords::{Board, ClipboardError};
use zeroize::Zeroizing;

/// The person's account on this computer, which names the user of a new library.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Account {
	/// The account's name, as `whoami` prints it, if the system names one.
	pub name: Option<String>,
	/// The person's full name, if the system knows it.
	pub full_name: Option<String>,
}

/// What commands need from the world.
pub trait Environment {
	/// The kind of random number generator.
	type Rng: CryptoRng;

	/// A cryptographically secure random number generator.
	fn rng(&mut self) -> &mut Self::Rng;

	/// The wall clock time, in milliseconds since 1970 (UTC).
	fn now(&self) -> u64;

	/// The device's offset from UTC, in minutes, at the current time.
	fn offset_minutes(&self) -> i32;

	/// The device's time zone, by which times are read and shown: a fixed offset from UTC,
	/// [`Environment::offset_minutes`], unless the environment knows the zone's rules.
	fn time_zone(&self) -> jiff::tz::TimeZone {
		jiff::tz::Offset::from_seconds(self.offset_minutes() * 60)
			.map_or(jiff::tz::TimeZone::UTC, jiff::tz::TimeZone::fixed)
	}

	/// Asks the person for a passphrase.
	///
	/// # Errors
	///
	/// Returns an error if the passphrase cannot be read.
	fn passphrase(&mut self, prompt: &str) -> io::Result<Zeroizing<String>>;

	/// How much work deriving a key from a new passphrase takes.
	fn passphrase_params(&self) -> PassphraseParams;

	/// The computer's name, the default name of a device.
	fn computer_name(&self) -> String;

	/// The person's account on this computer, after which a new library's user is named.
	fn account(&self) -> Account;

	/// The current folder: `nib init` makes it a library, and the other commands look for
	/// their library in it and in the folders it is in.
	///
	/// # Errors
	///
	/// Returns an error if the current folder cannot be found, such as one deleted since.
	fn current_dir(&self) -> io::Result<PathBuf>;

	/// The folder of the library that commands work in outside every library's folder, if
	/// the person names one: `NIBKEEP_LIBRARY`.
	fn default_library(&self) -> Option<PathBuf>;

	/// A folder in which, and above which, commands do not look for their library, as
	/// Git's `GIT_CEILING_DIRECTORIES` says: tests keep to their own folders with it, and the
	/// end-to-end tests name it in `NIBKEEP_CEILING`.
	fn ceiling(&self) -> Option<PathBuf> {
		None
	}

	/// Whether the platform has a keystore to keep a library's keys in, connecting to it
	/// the first time.
	fn keystore(&mut self) -> bool;

	/// The languages the system is set to, first the one it uses, as the platform names
	/// them, such as `en_US.UTF-8` or `en-US`.
	fn languages(&self) -> Vec<String>;

	/// This device's downloads folder, as the platform names it, if it has one.
	fn downloads(&self) -> Option<PathBuf> {
		None
	}

	/// Tells the person something beside what the command writes: on standard error, so
	/// that output that other programs read stays as it is.
	fn note(&mut self, line: &str) {
		eprintln!("nib: {}", crate::printable(line));
	}

	/// Asks the person a question on the terminal, and returns the answer, or `None` if
	/// there is no terminal to ask on, as for a script or an AI agent.
	///
	/// # Errors
	///
	/// Returns an error if the answer cannot be read.
	fn ask(&mut self, question: &str) -> io::Result<Option<String>> {
		let _ = question;
		Ok(None)
	}

	/// Whether [`Environment::ask`] has someone to ask, as before showing a code that would
	/// be spent without an answer.
	fn can_ask(&self) -> bool {
		false
	}

	/// The clipboard, to copy secrets to: none, unless the environment has one.
	///
	/// # Errors
	///
	/// Returns an error if there is no clipboard, such as on a computer without a display.
	fn clipboard(&mut self) -> Result<Box<dyn Board>, ClipboardError> {
		Err(ClipboardError::new("there is none here"))
	}

	/// Makes an interruption, as with Ctrl+C, end the next wait rather than `nib`, from now
	/// on: before `nib` copies a secret, so that an interruption never leaves it on the
	/// clipboard.
	fn interruptible(&mut self) {}

	/// Waits for `duration`, or until the person interrupts `nib`, as with Ctrl+C, whichever
	/// comes first. Returns whether the person interrupted it.
	fn wait(&mut self, duration: Duration) -> bool {
		std::thread::sleep(duration);
		false
	}

	/// Makes an interruption, as with Ctrl+C, set `stop` rather than end `nib`: for a
	/// command that runs until stopped, and lets go of what it holds as it ends.
	fn stop_on_interrupt(&mut self, stop: std::sync::Arc<std::sync::atomic::AtomicBool>) {
		let _ = stop;
	}

	/// This computer's folder of NibKeep's state, where the list of libraries synced from
	/// login is ([technical design](../../../docs/Technical-design.md#106-syncing-from-login)): `None`
	/// where there is none.
	fn state_folder(&self) -> Option<PathBuf> {
		None
	}

	/// The copy of `nibkeep-sync` installed beside `nib`, if there is one.
	fn sync_program(&self) -> Option<PathBuf> {
		None
	}

	/// The program of `nib` itself, which AI clients run as a library's server.
	fn nib_program(&self) -> Option<PathBuf> {
		None
	}

	/// A command of the AI client's program `program`, such as `claude`, if it is installed
	/// here, to tell the client of a server or take it away: none unless the environment
	/// finds it.
	fn ai_client(&self, program: &str) -> Option<std::process::Command> {
		let _ = program;
		None
	}

	/// The person's own Git, with their settings and credentials, as a command to give
	/// arguments to ([technical design](../../../docs/Technical-design.md#15-trees)).
	fn git(&self) -> std::process::Command {
		let mut git = std::process::Command::new("git");
		clear_git_variables(&mut git);
		git
	}

	/// The login entry that starts `nibkeep-sync`, where the platform has one.
	fn login_entry(&self) -> Option<LoginEntry> {
		None
	}

	/// Lets the person's other devices reach this computer through its firewall, where
	/// `nib` does that, as on Windows, asking for the administrator's consent.
	fn open_firewall(&mut self) -> Firewall {
		Firewall::NotHere
	}

	/// The engine's version that a copy of `nibkeep-sync` tells, or `None` if it is gone.
	fn sync_version(&self, program: &Path) -> Option<String> {
		at_login::service_version(program)
	}

	/// Starts a copy of `nibkeep-sync`, apart from `nib`, so that it runs on when `nib`
	/// ends.
	///
	/// # Errors
	///
	/// Returns an error if it cannot be started.
	fn start_sync(&mut self, program: &Path) -> Result<(), NodeError> {
		at_login::start_service(program)
	}
}

/// What `nib` did about the firewall, so that the person's other devices reach `nibkeep-sync`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Firewall {
	/// Nothing: `nib` changes no firewall here, as on Linux and macOS.
	NotHere,
	/// Nothing: the rule is there already.
	Open,
	/// It added the rule, with the administrator's consent.
	Opened,
	/// The rule is not there: the person refused, or could not consent.
	Refused,
}

/// The real world: the operating system's randomness, clock and keystore, and the
/// terminal.
pub struct System {
	rng: UnwrapErr<SysRng>,
	/// Whether there is a keystore, once a command asked.
	keystore: Option<bool>,
	/// The interruptions, once `nib` takes them instead of ending.
	interruptions: Option<mpsc::Receiver<()>>,
}

impl System {
	/// The real world. The platform's keystore is connected to only when a command needs
	/// it.
	#[must_use]
	pub fn new() -> Self {
		Self {
			rng: UnwrapErr(SysRng),
			keystore: None,
			interruptions: None,
		}
	}
}

impl Default for System {
	fn default() -> Self {
		Self::new()
	}
}

impl Environment for System {
	type Rng = UnwrapErr<SysRng>;

	fn rng(&mut self) -> &mut Self::Rng {
		&mut self.rng
	}

	fn now(&self) -> u64 {
		SystemTime::now()
			.duration_since(UNIX_EPOCH)
			.map_or(0, |since| {
				u64::try_from(since.as_millis()).unwrap_or(u64::MAX)
			})
	}

	fn offset_minutes(&self) -> i32 {
		jiff::tz::TimeZone::system()
			.to_offset(jiff::Timestamp::now())
			.seconds()
			/ 60
	}

	fn time_zone(&self) -> jiff::tz::TimeZone {
		jiff::tz::TimeZone::system()
	}

	fn passphrase(&mut self, prompt: &str) -> io::Result<Zeroizing<String>> {
		rpassword::prompt_password(prompt).map(Zeroizing::new)
	}

	fn passphrase_params(&self) -> PassphraseParams {
		// Every command derives the key again, so the parameters for little memory.
		PassphraseParams::LOW_MEMORY
	}

	fn computer_name(&self) -> String {
		gethostname::gethostname().to_string_lossy().into_owned()
	}

	fn account(&self) -> Account {
		// Read as the system gives them, so that a name in another encoding still names the
		// user.
		let lossy = |name: std::ffi::OsString| name.to_string_lossy().into_owned();
		Account {
			name: whoami::username_os().ok().map(lossy),
			full_name: whoami::realname_os().ok().map(lossy),
		}
	}

	fn current_dir(&self) -> io::Result<PathBuf> {
		std::env::current_dir()
	}

	fn default_library(&self) -> Option<PathBuf> {
		std::env::var_os("NIBKEEP_LIBRARY")
			.filter(|folder| !folder.is_empty())
			.map(PathBuf::from)
	}

	fn ceiling(&self) -> Option<PathBuf> {
		// A full path, through no link, as the current folder's are, which it is compared
		// with.
		std::env::var_os("NIBKEEP_CEILING")
			.map(PathBuf::from)
			.filter(|folder| folder.is_absolute())
			.map(|folder| nibkeep_node::resolved_path(&folder).unwrap_or(folder))
	}

	fn keystore(&mut self) -> bool {
		// Tests keep nib from the computer's keystore, as they keep the apps: with
		// `NIBKEEP_NO_KEYSTORE`, there is none.
		*self
			.keystore
			.get_or_insert_with(|| !at_login::app::kept_away() && platform_keystore())
	}

	fn downloads(&self) -> Option<PathBuf> {
		directories::UserDirs::new()
			.and_then(|directories| directories.download_dir().map(PathBuf::from))
	}

	fn state_folder(&self) -> Option<PathBuf> {
		#[cfg(not(any(target_os = "android", target_os = "ios")))]
		return at_login::settings_folder();
		#[cfg(any(target_os = "android", target_os = "ios"))]
		None
	}

	fn login_entry(&self) -> Option<LoginEntry> {
		LoginEntry::platform()
	}

	fn open_firewall(&mut self) -> Firewall {
		#[cfg(windows)]
		{
			use nibkeep_node::at_login::firewall;
			if firewall::has_rule() {
				Firewall::Open
			} else if firewall::add_rule() {
				Firewall::Opened
			} else {
				Firewall::Refused
			}
		}
		#[cfg(not(windows))]
		Firewall::NotHere
	}

	fn sync_program(&self) -> Option<PathBuf> {
		let name = format!(
			"{}{}",
			at_login::SERVICE_PROGRAM,
			std::env::consts::EXE_SUFFIX
		);
		let program = std::env::current_exe().ok()?.with_file_name(name);
		program.is_file().then_some(program)
	}

	fn nib_program(&self) -> Option<PathBuf> {
		std::env::current_exe().ok()
	}

	fn ai_client(&self, program: &str) -> Option<std::process::Command> {
		let name = format!("{program}{}", std::env::consts::EXE_SUFFIX);
		let found = std::env::split_paths(&std::env::var_os("PATH")?)
			.map(|folder| folder.join(&name))
			.find(|path| path.is_file())?;
		Some(std::process::Command::new(found))
	}

	fn can_ask(&self) -> bool {
		io::stdin().is_terminal() && io::stderr().is_terminal()
	}

	fn ask(&mut self, question: &str) -> io::Result<Option<String>> {
		// Only a person at a terminal answers; a program that runs nib has no say.
		if !self.can_ask() {
			return Ok(None);
		}
		let mut stderr = io::stderr();
		write!(stderr, "{}", crate::printable(question))?;
		stderr.flush()?;
		let mut answer = String::new();
		if io::stdin().read_line(&mut answer)? == 0 {
			return Ok(None);
		}
		Ok(Some(answer.trim().to_owned()))
	}

	fn clipboard(&mut self) -> Result<Box<dyn Board>, ClipboardError> {
		#[cfg(not(any(target_os = "android", target_os = "ios")))]
		return Ok(Box::new(nibkeep_passwords::SystemBoard::new()?));
		#[cfg(any(target_os = "android", target_os = "ios"))]
		Err(ClipboardError::new("nib has no clipboard on phones"))
	}

	fn interruptible(&mut self) {
		// Ctrl+C, and a terminal closed on Linux and macOS, end the wait instead of nib: on
		// Windows and macOS what nib copied would otherwise stay on the clipboard. Windows
		// ends a program whose console window is closed without waiting for it, so there the
		// copy stays until something else is copied.
		if self.interruptions.is_none() {
			let (interrupted, interruptions) = mpsc::channel();
			if ctrlc::set_handler(move || {
				let _ = interrupted.send(());
			})
			.is_ok()
			{
				self.interruptions = Some(interruptions);
			}
		}
	}

	fn stop_on_interrupt(&mut self, stop: std::sync::Arc<std::sync::atomic::AtomicBool>) {
		if self.interruptions.is_none() {
			let _ = ctrlc::set_handler(move || {
				stop.store(true, std::sync::atomic::Ordering::SeqCst);
			});
		}
	}

	fn wait(&mut self, duration: Duration) -> bool {
		self.interruptible();
		if let Some(interruptions) = &self.interruptions {
			interruptions.recv_timeout(duration).is_ok()
		} else {
			std::thread::sleep(duration);
			false
		}
	}

	fn languages(&self) -> Vec<String> {
		// GNU systems rank the languages of messages in LANGUAGE, as `en_US:ru`.
		#[cfg(unix)]
		if let Ok(ranked) = std::env::var("LANGUAGE")
			&& !ranked.is_empty()
		{
			return ranked.split(':').map(str::to_owned).collect();
		}
		sys_locale::get_locales().collect()
	}
}

/// Makes the platform's keystore the one that libraries use. Returns whether there is one.
#[must_use]
pub fn platform_keystore() -> bool {
	#[cfg(target_os = "macos")]
	let store = apple_native_keyring_store::keychain::Store::new();
	#[cfg(target_os = "windows")]
	let store = windows_native_keyring_store::Store::new();
	#[cfg(all(
		unix,
		not(any(target_os = "macos", target_os = "ios", target_os = "android"))
	))]
	let store = zbus_secret_service_keyring_store::Store::new();
	#[cfg(any(
		target_os = "macos",
		target_os = "windows",
		all(
			unix,
			not(any(target_os = "macos", target_os = "ios", target_os = "android"))
		)
	))]
	{
		match store {
			Ok(store) => {
				keyring_core::set_default_store(store);
				true
			}
			Err(_) => false,
		}
	}
	#[cfg(not(any(
		target_os = "macos",
		target_os = "windows",
		all(
			unix,
			not(any(target_os = "macos", target_os = "ios", target_os = "android"))
		)
	)))]
	false
}

/// Clears from a command of Git the variables that would make it work on another repository
/// than the folder it is given, as a hook or an alias of Git that runs `nib` sets them.
pub use nibkeep_git::clear_variables as clear_git_variables;