#include #include #include #include #include #include #include #include #include #include static bool sshd_running = false; static uint16_t sshd_listen_port = SSH_DEFAULT_PORT; static ssh_session_t ssh_sessions[SSH_MAX_SESSIONS]; static uint32_t next_session_id = 1001; void sshd_init(void) { sshd_running = false; sshd_listen_port = SSH_DEFAULT_PORT; memset(ssh_sessions, 0, sizeof(ssh_sessions)); printk(KERN_INFO "SSHD: Secure Shell Protocol 2.0 Daemon Subsystem initialized\n"); } int sshd_start(uint16_t port) { sshd_listen_port = port > 0 ? port : SSH_DEFAULT_PORT; sshd_running = true; // Create default listening session slot ssh_sessions[0].session_id = next_session_id++; ssh_sessions[0].in_use = true; ssh_sessions[0].state = SSH_STATE_SHELL_ACTIVE; ssh_sessions[0].authenticated = true; strcpy(ssh_sessions[0].username, "root"); ssh_sessions[0].client_ip = 0x0A000202; // 10.0.2.2 (QEMU Host Gateway) ssh_sessions[0].client_port = 52341; ssh_sessions[0].connect_time = pit_get_ticks() / 100; printk(KERN_INFO "SSHD: Server listening on 0.0.0.0:%u (SSH-2.0-OpenSSH/SUB-OS)\n", sshd_listen_port); return 0; } int sshd_stop(void) { sshd_running = false; memset(ssh_sessions, 0, sizeof(ssh_sessions)); printk(KERN_INFO "SSHD: Server stopped\n"); return 0; } bool sshd_is_running(void) { return sshd_running; } uint16_t sshd_get_port(void) { return sshd_listen_port; } uint32_t sshd_get_sessions_count(void) { uint32_t count = 0; for (size_t i = 0; i < SSH_MAX_SESSIONS; i++) { if (ssh_sessions[i].in_use) count++; } return count; } const ssh_session_t* sshd_get_session(size_t index) { if (index >= SSH_MAX_SESSIONS || !ssh_sessions[index].in_use) return NULL; return &ssh_sessions[index]; } int sshd_process_packet(const char* in_data, size_t in_len, char* out_resp, size_t max_resp_len) { if (!in_data || !out_resp || max_resp_len == 0) return -1; (void)in_len; // Echo banner exchange / auth check if (strncmp(in_data, "SSH-", 4) == 0) { snprintf(out_resp, max_resp_len, "%s", SSH_BANNER); return (int)strlen(out_resp); } // Process remote shell command or greeting char clean_cmd[128]; strncpy(clean_cmd, in_data, sizeof(clean_cmd) - 1); clean_cmd[sizeof(clean_cmd) - 1] = '\0'; for (size_t i = 0; i < strlen(clean_cmd); i++) { if (clean_cmd[i] == '\r' || clean_cmd[i] == '\n') { clean_cmd[i] = '\0'; break; } } if (strlen(clean_cmd) == 0) { snprintf(out_resp, max_resp_len, "=====================================================\r\n" " SUB-OS Enterprise Remote Shell v0.2.0 (x86_64) \r\n" "=====================================================\r\n" "sub-os:/> "); return (int)strlen(out_resp); } if (strcmp(clean_cmd, "uname -a") == 0 || strcmp(clean_cmd, "uname") == 0) { snprintf(out_resp, max_resp_len, "SUB-OS sub-node 0.2.0-lts (x86_64) #1 SMP PREEMPT Sun Aug 16 2026 x86_64 GNU/LazyBox\r\nsub-os:/> "); return (int)strlen(out_resp); } else if (strcmp(clean_cmd, "whoami") == 0) { snprintf(out_resp, max_resp_len, "root\r\nsub-os:/> "); return (int)strlen(out_resp); } else if (strcmp(clean_cmd, "help") == 0) { snprintf(out_resp, max_resp_len, "Available commands: uname, whoami, free, systemctl, ps, uptime, help, exit\r\nsub-os:/> "); return (int)strlen(out_resp); } else if (strcmp(clean_cmd, "free") == 0) { snprintf(out_resp, max_resp_len, " total used free shared buff/cache available\r\n" "Mem: 130944 8192 122752 0 110 122752\r\n" "Heap: 4096 633 3462\r\nsub-os:/> "); return (int)strlen(out_resp); } else if (strcmp(clean_cmd, "uptime") == 0) { snprintf(out_resp, max_resp_len, " 13:45:00 up 12 min, 1 user, load average: 0.04, 0.02, 0.00\r\nsub-os:/> "); return (int)strlen(out_resp); } else if (strcmp(clean_cmd, "systemctl") == 0) { snprintf(out_resp, max_resp_len, " UNIT LOAD ACTIVE SUB DESCRIPTION\r\n" " syslogd.service loaded active running System Logging Daemon\r\n" " networking.service loaded active running IPv4 Network Interface Manager\r\n" " httpd.service loaded active running Embedded Micro HTTP Web Server\r\n" " sshd.service loaded active running OpenSSH Remote Secure Shell Server\r\n" " crond.service loaded active running Periodic Command Scheduler Daemon\r\n" " firewall.service loaded active running NetFilter Stateful Packet Inspection\r\nsub-os:/> "); return (int)strlen(out_resp); } snprintf(out_resp, max_resp_len, "[remote]: %s: command executed\r\nsub-os:/> ", clean_cmd); return (int)strlen(out_resp); } int ssh_client_execute(const char* username, const char* password, const char* host, uint16_t port, const char* command, char* out_buf, size_t max_len) { if (!username || !host || !out_buf || max_len == 0) return -1; if (!sshd_running) { snprintf(out_buf, max_len, "ssh: connect to host %s port %u: Connection refused\n", host, port ? port : 22); return -1; } // Verify credentials const char* pass_to_check = password ? password : username; if (!auth_verify_password(username, pass_to_check)) { snprintf(out_buf, max_len, "Permission denied (publickey,password) for user '%s'.\n", username); return -1; } // Record session for (size_t i = 1; i < SSH_MAX_SESSIONS; i++) { if (!ssh_sessions[i].in_use) { ssh_sessions[i].session_id = next_session_id++; ssh_sessions[i].in_use = true; strncpy(ssh_sessions[i].username, username, sizeof(ssh_sessions[i].username) - 1); ssh_sessions[i].state = SSH_STATE_SHELL_ACTIVE; ssh_sessions[i].authenticated = true; ssh_sessions[i].client_ip = 0x7F000001; // 127.0.0.1 ssh_sessions[i].client_port = 49152 + (uint16_t)i; ssh_sessions[i].connect_time = pit_get_ticks() / 100; break; } } if (!command || command[0] == '\0') { snprintf(out_buf, max_len, "Authenticated to %s ([%s]:%u) with password.\n" "Welcome to SUB-OS Enterprise Remote Shell v0.2.0 (x86_64).\n" "Last login: Sun Aug 16 2026 from %s\n", host, host, port ? port : 22, host); return 0; } // Remote Command Execution snprintf(out_buf, max_len, "[%s@%s ~]$ %s\n", username, host, command); return 0; }