/* scorpiox-scorpioplayer-ssh-transport-server — SSH-2 server transport in pure C. * * KEX curve25519-sha256 (+@libssh.org), diffie-hellman-group14-sha256, -group16-sha512; * host key ssh-ed25519 (+ rsa-sha2-256/512 when the caller supplies an RSA key); * ciphers chacha20-poly1305@openssh.com, aes{128,256}-gcm@openssh.com, aes{128,256}-ctr * with hmac-sha2-256(-etm@openssh.com); strict KEX (Terrapin fix); ext-info server-sig-algs. * Auth: none (guest mode), password, publickey (ed25519, rsa-sha2-*). * Connection: "session" channels only; "subsystem" handed to a registered callback; * "exec" opt-in against a per-user allow-list, never via a shell. Everything else refused. * * One server per process (singleton API). All functions return 0 on success and a * negative value on error unless documented otherwise; spsshs_last_error() explains it. * Returned const char* point to static storage valid until the next call on this thread's * API use; copy them immediately. See SPEC.md. */ #ifndef SPSSHS_H #define SPSSHS_H #ifdef __cplusplus extern "C" { #endif #if defined(SPSSHS_STATIC) # define SPSSHS_API #elif defined(_WIN32) # ifdef SPSSHS_BUILD # define SPSSHS_API __declspec(dllexport) # else # define SPSSHS_API __declspec(dllimport) # endif #else # define SPSSHS_API __attribute__((visibility("default"))) #endif /* ---- error codes ---- */ #define SPSSHS_OK 0 #define SPSSHS_E_ARG -1 /* bad argument */ #define SPSSHS_E_STATE -2 /* wrong state (already running / not running) */ #define SPSSHS_E_IO -3 /* socket / file error */ #define SPSSHS_E_KEY -4 /* host key / authorized key could not be parsed */ #define SPSSHS_E_LIMIT -5 /* table full */ #define SPSSHS_E_NOTFOUND -6 #define SPSSHS_E_CRYPTO -7 /* ---- subsystem callbacks (row 40 sftp-file-server plugs in here) ---- * open : called when a client requests "subsystem " on an authenticated session * channel. `chan` is an opaque token for spsshs_channel_write/close, valid until * `close` is called. Return a non-NULL per-channel context, or NULL to refuse. * data : bytes the client sent on the channel. Return 0 to continue, <0 to close. * Called once with data == NULL, len == 0 when the client sends EOF; the channel * is then closed (exit-status 0 unless set by spsshs_channel_close) after all * queued output has been sent. * close: channel is gone (client EOF/close, disconnect or server stop). Free `sub`. * All three are called on the connection's thread. */ typedef void *(*spsshs_sub_open_fn)(const char *user, void *chan, void *ud); typedef int (*spsshs_sub_data_fn)(void *sub, const unsigned char *data, int len, void *ud); typedef void (*spsshs_sub_close_fn)(void *sub, void *ud); /* ---- lifecycle ---- */ /* bind_ip: "127.0.0.1", "::1", "0.0.0.0" ...; port 0 = pick an ephemeral port (spsshs_port). * hostkey_ed25519_path: OpenSSH "openssh-key-v1" unencrypted private key; generated with * mode 0600 if missing. rsa_key_path_or_null: optional OpenSSH-format RSA private key * (>= 2048 bit) — enables rsa-sha2-512/256 host key algorithms. */ SPSSHS_API int spsshs_start(const char *bind_ip, int port, const char *hostkey_ed25519_path, const char *rsa_key_path_or_null); SPSSHS_API void spsshs_stop(void); SPSSHS_API int spsshs_is_running(void); SPSSHS_API int spsshs_port(void); SPSSHS_API const char *spsshs_host_fingerprint(void); /* "SHA256:..." of the ed25519 key */ SPSSHS_API const char *spsshs_host_fingerprint_rsa(void); /* "" when no RSA key */ SPSSHS_API const char *spsshs_host_pubkey_line(void); /* "ssh-ed25519 AAAA... spsshs" (known_hosts) */ SPSSHS_API int spsshs_session_count(void); /* live TCP connections */ SPSSHS_API const char *spsshs_last_error(void); SPSSHS_API const char *spsshs_version(void); /* ---- credentials (never OS accounts) ---- */ SPSSHS_API int spsshs_add_user_password(const char *user, const char *pass); SPSSHS_API int spsshs_add_user_key(const char *user, const char *authorized_keys_line); SPSSHS_API int spsshs_remove_user(const char *user); SPSSHS_API int spsshs_clear_users(void); SPSSHS_API int spsshs_set_guest(int on); /* "none" auth for user guest/anonymous → user "guest" */ /* ---- network policy ---- */ SPSSHS_API int spsshs_allow_cidr(const char *cidr); /* default list: 127.0.0.0/8 and ::1/128 */ SPSSHS_API int spsshs_clear_cidr(void); /* back to the loopback-only default */ SPSSHS_API int spsshs_set_limits(int max_connections, int idle_timeout_s, int login_grace_s); SPSSHS_API int spsshs_set_rekey_limits(long long bytes, int seconds); /* ---- share roots (used by exec path arguments; same shape as rows 30/32/34/36) ---- */ SPSSHS_API int spsshs_add_root(const char *name, const char *path); /* folder or single file */ SPSSHS_API int spsshs_remove_root(const char *name); SPSSHS_API int spsshs_root_count(void); /* ---- subsystems ---- */ SPSSHS_API int spsshs_register_subsystem(const char *name, spsshs_sub_open_fn open, spsshs_sub_data_fn data, spsshs_sub_close_fn close, void *ud); SPSSHS_API int spsshs_unregister_subsystem(const char *name); /* queue bytes to the client (respects the SSH window; buffered up to 16 MiB per channel). * Returns bytes accepted or <0. Call only from inside a callback or the connection thread. */ SPSSHS_API int spsshs_channel_write(void *chan, const unsigned char *data, int len); SPSSHS_API int spsshs_channel_close(void *chan, int exit_status); /* ---- exec (off by default) ---- */ SPSSHS_API int spsshs_exec_enable(int on); /* user "*" = every authenticated user. argv0_path: absolute path of the program. The client * may name it by that path or by its basename. argv_pattern: space-separated allowed argument * tokens: literals (e.g. "-show_streams -of json") and classes @path (a path inside a share * root, given as "/"), @word ([A-Za-z0-9_.,:=+-], no leading '/', no ".."), * @int. Every client argument must match one of them. "" = no arguments allowed. */ SPSSHS_API int spsshs_exec_allow(const char *user, const char *argv0_path, const char *argv_pattern); SPSSHS_API int spsshs_exec_clear(void); SPSSHS_API int spsshs_exec_limits(int timeout_ms, int max_output_bytes); /* Dry-run the policy: returns 0 if `user` may run `command`, otherwise 126. The resolved * argv (one per line) is available via spsshs_last_error() on failure (reason) or * spsshs_exec_last_argv() on success. */ SPSSHS_API int spsshs_exec_check(const char *user, const char *command); SPSSHS_API const char *spsshs_exec_last_argv(void); /* ---- log (every auth / exec / refusal); ring of the last 256 lines ---- */ SPSSHS_API int spsshs_log_count(void); SPSSHS_API const char *spsshs_log_line(int i); /* 0 = oldest */ SPSSHS_API int spsshs_log_to_stderr(int on); /* ---- self-test of the vendored + in-tree crypto (KATs); 0 = all pass ---- */ SPSSHS_API int spsshs_selftest(void); #ifdef __cplusplus } #endif #endif