TNT/include/bootstrap.h
m1ngsama b5f9a17290 refactor: extract bootstrap module (PR2-M4)
Move the per-connection SSH bootstrap pipeline -- key exchange, auth,
channel open + PTY/shell-or-exec request, and the hand-off into a
client_t -- out of ssh_server.c into a dedicated module.

Migrated to bootstrap.{c,h}:
- session_context_t (now private to bootstrap.c)
- accepted_session_t (declared in bootstrap.h, the IPC envelope from
  the accept loop into the bootstrap thread)
- TNT_ACCESS_TOKEN handling: g_access_token + bootstrap_init()
- constant_time_strcmp (auth-only utility)
- bootstrap_peer_ip (peer IP read from libssh fd)
- auth_password / auth_none / auth_pubkey
- destroy_session_context, cleanup_failed_session
- channel_open_request_session, channel_pty_request,
  channel_pty_window_change, channel_shell_request, channel_exec_request
- setup_session_channel_callbacks
- bootstrap_run (formerly bootstrap_client_session, the pthread entry)

Stayed in ssh_server.c:
- accept loop in ssh_server_start (now calls bootstrap_peer_ip and
  pthread_create(bootstrap_run))
- ssh_server_init (now calls ratelimit_init() + bootstrap_init() +
  reads only g_idle_timeout / TNT_BIND_ADDR / TNT_SSH_LOG_LEVEL)
- client_send/printf/addref/release, notify_mentions
- client_channel_window_change/eof/close (post-bootstrap, target client_t)
- client_install_channel_callbacks (renamed from
  install_client_channel_callbacks, now non-static and exposed via
  ssh_server.h so bootstrap.c can install them on the new client_t)
- read_username, handle_key, client_handle_session (will move to
  input.c in PR2-M5)
- setup_host_key, ssh_server_start_time

Two helpers also lifted: sanitize_terminal_size moved to common.c (used
by the bootstrap PTY callback and the post-bootstrap window-change
callback), and is_valid_username already lived there from M2.

ssh_server.c shrinks from 1513 to 1026 lines (-487).
Behaviour is preserved: implementations are byte-for-byte the same.
2026-05-17 09:47:28 +08:00

39 lines
1.6 KiB
C

#ifndef BOOTSTRAP_H
#define BOOTSTRAP_H
#include "ssh_server.h" /* for client_t and the libssh / arpa includes */
/* Hand-off envelope between the accept loop and the bootstrap thread.
* The accept loop allocates one of these per accepted session, fills it,
* and pthread_create()s a detached bootstrap_run() with this pointer.
* bootstrap_run() owns the struct and the embedded ssh_session, and frees
* both before returning. */
typedef struct {
ssh_session session;
char client_ip[INET6_ADDRSTRLEN];
} accepted_session_t;
/* Read TNT_ACCESS_TOKEN from the environment. Idempotent. Call once
* during startup, before bootstrap_run() can fire on any accepted
* session. */
void bootstrap_init(void);
/* Read the peer IP off an accepted ssh_session into ip_buf. Sets ip_buf
* to "unknown" when the address family is unrecognised or getpeername()
* fails. ip_buf must be at least INET6_ADDRSTRLEN bytes. */
void bootstrap_peer_ip(ssh_session session, char *ip_buf, size_t buf_size);
/* pthread entry point for the per-connection bootstrap thread.
*
* Steps performed before handing control to client_handle_session():
* 1. SSH key exchange
* 2. auth (password / none / pubkey, with rate-limit feedback)
* 3. channel open + PTY/shell-or-exec request
* 4. construct a client_t and install its lifetime channel callbacks
*
* On any failure path the connection is torn down and ratelimit /
* connection counters are released; client_handle_session() is never
* invoked. Always returns NULL. */
void *bootstrap_run(void *arg);
#endif /* BOOTSTRAP_H */