rightkit_daemon/lib.rs
1//! rightkit-daemon: the one discovery and authentication convention for local
2//! Right Suite services (the shared speech service, the in-app control server,
3//! `right-ctl`).
4//!
5//! * **Discovery** - `<suite>/runtime/<service>/service.json`, written
6//! atomically after the endpoint is listening, removed on clean exit. A
7//! record whose endpoint does not answer is stale and ignored.
8//! * **Ranking and takeover** - one host per service per suite. A would-be host
9//! that outranks the incumbent (lower rank wins) writes `takeover.json`; the
10//! incumbent stops admitting work, finishes or hands off in-flight requests
11//! with the retryable [`ERROR_HOST_HANDOFF`] at their next boundary, and
12//! releases. Clients re-read discovery and resubmit. The incumbent looks for a
13//! request from another process once a second (a stat of `takeover.json`, no
14//! thread polls faster), so a takeover starts up to about a second after the
15//! request is written.
16//! * **Authentication** - no TCP and no bearer tokens. Unix: a socket in a 0700
17//! directory, mode 0600, and the kernel-reported peer uid (`getpeereid` on macOS
18//! and the BSDs, `SO_PEERCRED` on Linux) checked on every accept and by every
19//! client ([`verify_peer_is_current_user`]); a platform that cannot check refuses
20//! every peer. Windows: a named pipe whose DACL grants only the current user's SID
21//! ([`owner_only_security_attributes`]), remote clients rejected, first-instance
22//! flag set against name squatting, a bounded instance count
23//! ([`HostOptions::max_pipe_instances`]), the client's process-token SID checked on
24//! every accept, and clients opening with `SECURITY_IDENTIFICATION` and checking
25//! the server's SID. See the README threat model.
26//! * **Verifying the host** - the transport proves a host runs as the current user, not which
27//! program it is. [`Client::connect_expecting`] takes a [`HostExpectation`] (the executable the
28//! host may be, for example a sibling inside the client's own app bundle), asks the kernel who
29//! is on the listening end of the connected socket (macOS `LOCAL_PEERPID` and `proc_pidpath`,
30//! Linux `SO_PEERCRED` and `/proc/<pid>/exe`), and refuses anyone else with a typed
31//! [`HostIdentityError`] before the `hello` or any request is written. [`Client::connect`] stays
32//! unauthenticated-host. [`ClientConfig::ignore_env_overrides`] makes the suite root come only
33//! from the account database, so a launcher's environment cannot redirect a release client.
34//! See the README, "Verifying the host".
35//! * **Per-app request allowlist** - each client names its app in `hello`; an
36//! app may call only the methods listed for it (the generalisation of
37//! HeardRight's `SERVICE_ALLOWED_SCHEMAS`). The app label is an allowlist key,
38//! not a security boundary against code already running as the same user.
39//! * **Fail-closed roots** - [`Suite::resolve`] errors when no absolute
40//! app-data root can be determined; it never falls back to the working
41//! directory. [`Suite::per_run_temp`] is the explicit opt-in alternative.
42//!
43//! Wire format: bounded JSON lines (`rightkit-sidecar`), the same frames
44//! as the `rightkit-job-queue` engine contract:
45//! `hello`, `request{id,method,params}`, `event{id,event}`, `response{id,ok,result|error}`.
46
47mod app_root;
48pub mod chunk;
49mod client;
50pub mod contract;
51mod discovery;
52mod error;
53mod fail_limit;
54mod host_identity;
55#[cfg(feature = "link")]
56pub mod link;
57pub mod mode;
58mod peer;
59pub mod private;
60mod record;
61#[cfg(feature = "relay")]
62pub mod relay;
63/// Daemon host (`remote` feature) and device client (`link` feature); see the module docs.
64#[cfg(any(feature = "remote", feature = "link"))]
65pub mod remote;
66mod server;
67mod service;
68pub mod stop;
69mod suite;
70pub mod supervisor;
71mod takeover_watch;
72pub mod ticker;
73mod transport;
74
75pub use client::{Client, ClientConfig, RetryPolicy, DEFAULT_DEADLINE};
76pub use discovery::{
77 adopt_or_spawn, discover_apps_in, discover_apps_in_with, discover_macos, discover_macos_with,
78 discover_windows, discover_windows_with, parse_plist_string, parse_reg_query, plan_host,
79 poll_until, publish_takeover, record_process_matches, resolve_install_dir, resolve_rank,
80 should_request_takeover, spawn_detached, takeover_obliges_yield, try_adopt_or_spawn,
81 try_plan_host, wait_for_claim_winner, AdoptOrSpawn, Completeness, DiscoverOptions, HostPlan,
82 InstalledApp, SpawnClaim, SpawnClaimError, UninstallEntry,
83};
84pub use private::{
85 exit_when_stdin_closes, watch_reader_eof, watch_stdin_eof, PrivateChild, PrivateSecret,
86 SecretError, SECRET_BYTES, SECRET_HEX_LEN,
87};
88// Deprecated items stay exported for existing callers; their deprecation is reported at the
89// caller's own use site, not on this re-export.
90pub use app_root::{
91 valid_app_name, AppRoot, AppRootError, MigrationOutcome, WriterLock, MAX_APP_NAME_LEN,
92 MIGRATION_MARKER,
93};
94#[allow(deprecated)]
95pub use discovery::{discover_apps, KNOWN_APPS};
96pub use error::{
97 ConnectError, ServiceError, ERROR_BAD_CHUNK, ERROR_CONNECTION_CLOSED, ERROR_FORBIDDEN,
98 ERROR_HOST_HANDOFF, ERROR_HOST_IDENTITY, ERROR_NOT_ALLOWED, ERROR_REQUEST_TOO_LARGE,
99 ERROR_RESPONSE_TOO_LARGE, ERROR_REVISION_CONFLICT, ERROR_SUITE_UNRESOLVED, ERROR_TIMEOUT,
100 ERROR_UNAUTHENTICATED, ERROR_UNAVAILABLE,
101};
102pub use host_identity::{HostExpectation, HostIdentityError, HostIdentityReason, HostProcess};
103#[cfg(unix)]
104pub use peer::verify_peer_is_current_user;
105#[cfg(windows)]
106pub use peer::{
107 owner_only_security_attributes, verify_pipe_peer_is_current_user,
108 verify_pipe_server_is_current_user, OwnerOnlySecurityAttributes,
109};
110pub use peer::{PeerError, PeerIdentity};
111pub use record::{
112 clear_takeover_if_owner, read_live_record, read_record, read_takeover, record_path,
113 takeover_path, try_read_live_record, try_read_record, try_read_takeover, write_atomic,
114 write_takeover, Allowlist, ServiceRecord, TakeoverRequest, PROTOCOL_MAJOR, PROTOCOL_MINOR,
115 RANK_UNKNOWN,
116};
117pub use server::{
118 host, host_with_options, CallContext, Emit, Handler, HostError, HostOptions, Hosted,
119 ServiceConfig, ServiceEvent, ServiceStatus, DEFAULT_FRAME_LIMIT, DEFAULT_MAX_CONNECTIONS,
120 DEFAULT_WRITE_TIMEOUT,
121};
122pub use service::{BuildError, Service, ServiceBuilder};
123pub use suite::{
124 refuse_build_tool_real_data, valid_service_name, Suite, SuiteError, ENV_SUITE_ROOT,
125 MAX_SERVICE_NAME_LEN, SHARED_DIR_NAME, SUITE_DIR_NAME,
126};
127pub use transport::{
128 endpoint_candidates, endpoint_for, find_live_endpoint, open_stream, open_stream_expecting,
129 pid_alive, probe_endpoint, try_endpoint_for, Accepted, Conn, EndpointError, Listener, Stream,
130 DEFAULT_MAX_PIPE_INSTANCES, MAX_UNIX_SOCKET_PATH,
131};