/* What pic asks of the machine it runs on, apart from files a tool touches
* and commands it runs (pic_executor, in pic.h): clocks, entropy, the
* journal's single-writer lock, file inputs, and the SQLite VFS for pic's
* databases. The host's is the only one today; a build where pic is the
* whole guest of a microVM supplies its own with pic_platform_set(). */
#ifndef PIC_PLATFORM_H
#define PIC_PLATFORM_H
#include <stddef.h>
#include <stdint.h>
/* What cred.c asks a platform's secret_key for. */
typedef struct pic_secret_ask {
const char *source; /* the store's recorded source */
const char *id; /* the store: its database's path */
int fresh; /* the store has no key yet */
int fd; /* a passphrase: read a line from this descriptor (--passphrase-fd), or -1 */
const char *command; /* a passphrase: the first line this shell command prints, or NULL */
} pic_secret_ask;
/* How the secret came: a raw 32-byte key; a passphrase to stretch; a
* passphrase a person typed at a prompt just now (an interface may offer to
* stop asking for it). */
enum { PIC_SECRET_KEY, PIC_SECRET_PASSPHRASE, PIC_SECRET_TYPED };
/* What pic remote install asks of a platform's login_agent. */
enum { PIC_AGENT_STATE, PIC_AGENT_INSTALL, PIC_AGENT_REMOVE };
typedef struct pic_login_agent {
const char *const *argv; /* INSTALL: the program (an absolute path) and its arguments, NULL-ended */
const char *log; /* INSTALL: the file its output is appended to */
char kind[16]; /* out: the mechanism ("launchd", "systemd", "cron", a no-OS build's own) */
char where[1024]; /* out: what it keeps (a file, or "crontab") */
int started; /* out, INSTALL: the system has started it now as well */
} pic_login_agent;
typedef struct pic_platform {
/* Wall-clock time in ns since the Unix epoch, or -1 when unknown. */
int64_t (*wall_ns)(void);
/* A clock that never steps back, in ns from an arbitrary origin. */
uint64_t (*mono_ns)(void);
/* Fills buf with n (at most 256) random bytes: 0, or -1 with errno. */
int (*entropy)(void *buf, size_t n);
/* Takes the lock named by path without waiting: shared with other
* shared holders when SHARED, else exclusive. A handle (>= 0) on
* success; -1 with errno when the lock cannot be opened; -2 with errno
* (EWOULDBLOCK: someone else holds it) when it is not taken. The lock
* lasts until unlock() or until this process and the children it forks
* have all exited. Lock files are never removed. */
int (*lock)(const char *path, int shared);
void (*unlock)(int handle);
/* pic_input_read (input.h) dispatches here. */
char *(*input_read)(const char *path, size_t max, int flags, size_t *len, int *truncated);
/* Calls fn(ctx, name) for each entry of the directory DIR (not . or ..)
* until it returns nonzero: 0, or -1 with errno when DIR cannot be
* opened. pic_input_read reads what it names. */
int (*input_list)(const char *dir, int (*fn)(void *ctx, const char *name), void *ctx);
/* The VFS pic's journal and ~/.pic/pic.sqlite are opened with; NULL is
* SQLite's default. */
const char *vfs;
/* The credential store's master key (cred.c), asked only when a stored
* key is needed. The store records the name of the source its key came
* from: "passphrase" (asked, and stretched by cred.c with the store's
* KDF), or a source that keeps a raw 32-byte key ("keychain" and "file"
* on a host; a no-OS build names its own). secret_key fetches it into buf
* (at most cap bytes) and its length into *len, with *how one of
* PIC_SECRET_*; ASK->fresh: the store has no key yet (a typed passphrase
* is asked twice). secret_keep keeps KEY, a new raw key, in the source
* WANT (NULL: this platform's choice for a new store; "": its choice of
* a source that keeps a key, never a passphrase) and names the source
* used in SOURCE: 0; or 1 when it keeps nothing and cred.c must fetch
* the key from SOURCE instead (a passphrase, a fixed boot key). cred.c
* treats every source but "passphrase" as opaque, so a platform adds a
* source here alone: the host's portable "file" works on any POSIX
* system and the keychain is used when present; a platform with no
* filesystem keeps the key as a blob in a key database of its own
* beside pic.sqlite, through its SQLite VFS.
* secret_drop forgets what SOURCE keeps for the store, after the store
* moved away from it. Each 0, or -1 with err. NULL: this build has no
* source, and stored keys cannot be used. */
int (*secret_key)(const struct pic_secret_ask *ask, unsigned char *buf, size_t cap, size_t *len, int *how, char *err,
size_t errcap);
int (*secret_keep)(const char *want, const char *id, const unsigned char *key, size_t n, char *source, size_t scap,
char *err, size_t errcap);
void (*secret_drop)(const char *source, const char *id);
/* A descriptor that becomes readable when the file PATH is written
* (kqueue's EVFILT_VNODE, inotify), or -1 when this platform cannot
* watch it; the caller then stats it on a timer. watch_clear consumes
* what made it readable; watch_end ends it. NULL: no watch. */
int (*watch)(const char *path);
void (*watch_clear)(int fd);
void (*watch_end)(int fd);
/* The one login agent pic keeps: the system runs AGENT->argv whenever this user logs in (or
* the machine starts, where that is all it offers), and again when it fails, with its output
* appended to AGENT->log. OP: PIC_AGENT_INSTALL replaces any earlier one and starts it now
* where the mechanism can; PIC_AGENT_REMOVE removes it and stops what the system started;
* PIC_AGENT_STATE only looks. AGENT->kind and ->where are filled in each case. 1 when one is
* installed (STATE, INSTALL) or was removed (REMOVE), 0 when none, or -1 with err. NULL: this
* platform has no way to start a program at login. */
int (*login_agent)(int op, pic_login_agent *agent, char *err, size_t errcap);
/* The absolute path of the program this process runs, as it was started (macOS's
* _NSGetExecutablePath, Linux's /proc/self/exe): 0, or -1. NULL: this platform cannot tell. */
int (*self_path)(char *out, size_t cap);
/* A word for the file now at PATH that changes when it is replaced or rewritten (its device,
* inode, size and modification time): 0, or -1 when there is none. A long-running pic compares
* it with its own at start to learn that a newer pic was installed over it. NULL: no files. */
int (*file_stamp)(const char *path, char *out, size_t cap);
/* The processes that have the file PATH open, any process this user may see (lsof, where the
* system has it): up to CAP pids in PIDS with their command names in NAMES; how many, or -1
* when this cannot tell. pic doctor finds a pic that does not say it holds a journal so. NULL:
* never. */
int (*file_users)(const char *path, long *pids, char (*names)[32], int cap);
/* A key from a command (cred.h, "keys from a command"): runs COMMAND through the shell, as
* this user, with nothing on stdin, and gives the first line of its output in OUT. 0; -1 with
* err (the first line of what it wrote to stderr, or why: its exit status, no output, ten seconds
* without an answer). NULL: this platform starts no processes, and such a key is refused. */
int (*key_command)(const char *command, char *out, size_t cap, char *err, size_t errcap);
} pic_platform;
/* This machine's: POSIX clocks, getentropy, flock, the filesystem. */
const pic_platform *pic_platform_host(void);
/* The one in use, the host's unless another was set. */
const pic_platform *pic_platform_get(void);
void pic_platform_set(const pic_platform *p);
/* Host only: reads a line from the controlling terminal with echo off,
* after writing PROMPT to it; the newline is dropped. Its length, or -1 with
* err (no terminal, too long, or interrupted). */
long pic_tty_secret(const char *prompt, char *buf, size_t cap, char *err, size_t errcap);
/* Host only: reads a line from the controlling terminal with echo on, after
* writing PROMPT to it: its length, or -1 (no terminal). */
long pic_tty_answer(const char *prompt, char *buf, size_t cap);
/* Host only: where the key source SOURCE keeps the key, in words ("your
* Keychain", "your keyring", "~/.pic/master.key", "a passphrase"); NULL: the
* place a key moved off a passphrase goes (the keychain, else the file). */
const char *pic_host_where(const char *source);
/* Host only: opens TARGET, an http, https, file or mailto address, in the
* program the system gives it (open on macOS, xdg-open elsewhere), by its
* absolute path and never through a shell, detached and with its output
* dropped. 0 when the opener was started; -1 when there is none or TARGET is
* refused. Dev builds run PIC_TEST_BROWSER instead when it is set. */
int pic_host_open(const char *target);
/* Host only: 1 when HOME is the account's own home directory (the password database's), else 0. */
int pic_host_own_home(void);
/* The err pic_tty_secret gives when there is no terminal to ask on. */
#define PIC_NO_TERMINAL "no terminal to ask on"
/* From now on this process never asks on the terminal (RPC mode, whose client owns the terminal
* if there is one): pic_tty_secret fails with PIC_NO_TERMINAL. */
void pic_tty_forbid(void);
/* input.c's reader and lister of host files, the host platform's input_read and input_list. */
char *pic_input_read_host(const char *path, size_t max, int flags, size_t *len, int *truncated);
int pic_input_list_host(const char *dir, int (*fn)(void *ctx, const char *name), void *ctx);
#endif