Files
d0nizam 96e9e1e9cd SSH.koplugin: add force‑close toggle for stop (#15338)
* Adds an optional setting to kill all dropbear processes when stopping the server, giving users control over whether active sessions are terminated.

* Modify signal sending to use pkill and kill
2026-07-08 19:11:50 +02:00

347 lines
12 KiB
Lua

local BD = require("ui/bidi")
local DataStorage = require("datastorage")
local Device = require("device")
local Dispatcher = require("dispatcher")
local InfoMessage = require("ui/widget/infomessage") -- luacheck:ignore
local InputDialog = require("ui/widget/inputdialog")
local UIManager = require("ui/uimanager")
local WidgetContainer = require("ui/widget/container/widgetcontainer")
local ffiutil = require("ffi/util")
local logger = require("logger")
local util = require("util")
local _ = require("gettext")
local T = ffiutil.template
-- This plugin uses a patched dropbear that adds two things:
-- the -n option to bypass password checks
-- reads the authorized_keys file from the relative path: settings/SSH/authorized_keys
local path = DataStorage:getFullDataDir()
if not util.pathExists("dropbear") then
return { disabled = true, }
end
local SSH = WidgetContainer:extend{
name = "SSH",
is_doc_only = false,
}
function SSH:init()
self.SSH_port = G_reader_settings:readSetting("SSH_port") or "2222"
self.allow_no_password = G_reader_settings:isTrue("SSH_allow_no_password")
self.key_only_auth = G_reader_settings:isTrue("SSH_key_only_auth")
self.autostart = G_reader_settings:isTrue("SSH_autostart")
-- force close all connections on stop
self.force_kill_clients = G_reader_settings:isTrue("SSH_force_kill_clients")
if self.autostart then
self:start()
end
self.ui.menu:registerToMainMenu(self)
self:onDispatcherRegisterActions()
end
function SSH:start()
if self:isRunning() then
logger.dbg("[Network] Not starting SSH server, already running.")
return
end
local cmd = string.format("%s %s %s %s%s %s",
"./dropbear",
"-E",
"-R",
"-p", self.SSH_port,
"-P /tmp/dropbear_koreader.pid")
-- prevent login without public key (discourage allow_no_password option)
if self.key_only_auth then
cmd = string.format("%s %s", cmd, "-s")
elseif self.allow_no_password then
cmd = string.format("%s %s", cmd, "-n")
end
-- Make a hole in the Kindle's firewall
if Device:isKindle() then
os.execute(string.format("%s %s %s",
"iptables -A INPUT -p tcp --dport", self.SSH_port,
"-m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT"))
os.execute(string.format("%s %s %s",
"iptables -A OUTPUT -p tcp --sport", self.SSH_port,
"-m conntrack --ctstate ESTABLISHED -j ACCEPT"))
end
-- An SSH/telnet server of course needs to be able to manipulate pseudoterminals...
-- Kobo's init scripts fail to set this up...
if Device:isKobo() then
os.execute([[if [ ! -d "/dev/pts" ] ; then
mkdir -p /dev/pts
mount -t devpts devpts /dev/pts
fi]])
end
if not util.pathExists(path.."/settings/SSH/") then
os.execute("mkdir "..path.."/settings/SSH")
end
logger.dbg("[Network] Launching SSH server : ", cmd)
if os.execute(cmd) == 0 then
local info = InfoMessage:new{
timeout = 10,
-- @translators: %1 is the SSH port, %2 is the network info.
text = T(_("SSH server started.\n\nSSH port: %1\n%2"),
self.SSH_port,
Device.retrieveNetworkInfo and Device:retrieveNetworkInfo() or _("Could not retrieve network info.")),
}
UIManager:show(info)
else
local info = InfoMessage:new{
icon = "notice-warning",
text = _("Failed to start SSH server."),
}
UIManager:show(info)
end
end
function SSH:isRunning()
return util.pathExists("/tmp/dropbear_koreader.pid")
end
--- Stops the Dropbear process started by this plugin.
--- @param force boolean If true, forces the process to stop if it doesn't exit gracefully.
--- @return boolean Success, string|nil Error
function SSH:stopPlugin(force)
if not self:isRunning() then
return true
end
local pid_path = "/tmp/dropbear_koreader.pid"
local function readPID()
local f = io.open(pid_path, "r")
if not f then return nil end
local s = f:read("*l")
f:close()
return s and tonumber(s) or nil
end
local pid = readPID()
local function isProcAlive(p)
return p and util.pathExists("/proc/" .. p)
end
local function send(sig, p)
return os.execute(string.format("pkill -%s -P %d && kill -%s %d", sig, p, sig, p)) == 0
end
send("TERM", pid)
for _ = 1, 20 do
if not isProcAlive(pid) then break end
ffiutil.sleep(0.1)
end
if isProcAlive(pid) and force then
send("KILL", pid)
for _ = 1, 10 do
if not isProcAlive(pid) then break end
ffiutil.sleep(0.1)
end
end
-- Plug the hole in the Kindle's firewall
if Device:isKindle() then
os.execute(string.format("%s %s %s",
"iptables -D INPUT -p tcp --dport", self.SSH_port,
"-m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT"))
os.execute(string.format("%s %s %s",
"iptables -D OUTPUT -p tcp --sport", self.SSH_port,
"-m conntrack --ctstate ESTABLISHED -j ACCEPT"))
end
if not isProcAlive(pid) then
os.remove(pid_path)
return true
end
return false, "dropbear process did not exit"
end
function SSH:stop()
local ok, err = self:stopPlugin(false)
if not ok then
logger.warn("SSH: graceful stop failed:", err)
-- If the user chose to force-close all connections, try a forced stop first.
if self.force_kill_clients then
ok, err = self:stopPlugin(true)
if not ok then
-- Last resort: kill every dropbear process to close all sessions.
logger.warn("SSH: force-stop also failed, using killall:", err)
os.execute("killall -9 dropbear 2>/dev/null")
os.remove("/tmp/dropbear_koreader.pid")
UIManager:show(InfoMessage:new{
text = _("SSH server forcefully stopped."),
timeout = 2,
})
return
end
else
-- Without force-kill, active connections may remain.
UIManager:show(InfoMessage:new{
icon = "notice-warning",
text = _("SSH server is still shutting down… Active connections may remain until they are closed."),
timeout = 3,
})
return
end
end
if ok then
UIManager:show(InfoMessage:new{
text = _("SSH server stopped."),
timeout = 2,
})
end
end
function SSH:onToggleSSHServer()
if self:isRunning() then
self:stop()
else
self:start()
end
end
function SSH:show_port_dialog(touchmenu_instance)
self.port_dialog = InputDialog:new{
title = _("Choose SSH port"),
input = self.SSH_port,
input_type = "number",
input_hint = self.SSH_port,
buttons = {
{
{
text = _("Cancel"),
id = "close",
callback = function()
UIManager:close(self.port_dialog)
end,
},
{
text = _("Save"),
is_enter_default = true,
callback = function()
local value = tonumber(self.port_dialog:getInputText())
if value and value >= 0 then
self.SSH_port = value
G_reader_settings:saveSetting("SSH_port", self.SSH_port)
UIManager:close(self.port_dialog)
touchmenu_instance:updateItems()
end
end,
},
},
},
}
UIManager:show(self.port_dialog)
self.port_dialog:onShowKeyboard()
end
function SSH:addToMainMenu(menu_items)
menu_items.ssh = {
text = _("SSH server"),
checked_func = function() return self:isRunning() end,
hold_callback = function(touchmenu_instance)
self:onToggleSSHServer()
ffiutil.sleep(1)
touchmenu_instance:updateItems()
end,
sub_item_table = {
{
text = _("SSH server"),
checked_func = function() return self:isRunning() end,
check_callback_updates_menu = true,
callback = function(touchmenu_instance)
self:onToggleSSHServer()
-- sleeping might not be needed, but it gives the feeling
-- something has been done and feedback is accurate
ffiutil.sleep(1)
touchmenu_instance:updateItems()
end,
},
{
text_func = function()
return T(_("SSH port: %1"), self.SSH_port)
end,
keep_menu_open = true,
enabled_func = function() return not self:isRunning() end,
callback = function(touchmenu_instance)
self:show_port_dialog(touchmenu_instance)
end,
},
{
text = _("SSH public key"),
keep_menu_open = true,
enabled_func = function() return not self:isRunning() end,
callback = function()
local info = InfoMessage:new{
timeout = 60,
text = T(_("Put your public SSH keys in\n%1"), BD.filepath(path.."/settings/SSH/authorized_keys")),
}
UIManager:show(info)
end,
},
{
text_func = function()
return _("Login with key only (SECURE, public key required)")
end,
checked_func = function() return self.key_only_auth end,
enabled_func = function() return not self:isRunning() end,
callback = function()
self.key_only_auth = not self.key_only_auth
if self.key_only_auth and self.allow_no_password then
self.allow_no_password = false
G_reader_settings:flipNilOrFalse("SSH_allow_no_password")
end
G_reader_settings:flipNilOrFalse("SSH_key_only_auth")
end,
},
{
text = _("Login without password (DANGEROUS)"),
checked_func = function() return self.allow_no_password end,
enabled_func = function() return not self:isRunning() end,
callback = function()
self.allow_no_password = not self.allow_no_password
if self.allow_no_password and self.key_only_auth then
self.key_only_auth = false
G_reader_settings:flipNilOrFalse("SSH_key_only_auth")
end
G_reader_settings:flipNilOrFalse("SSH_allow_no_password")
end,
},
{
text = _("Start SSH server with KOReader"),
checked_func = function() return self.autostart end,
enabled_func = function() return not self:isRunning() end,
callback = function()
self.autostart = not self.autostart
G_reader_settings:flipNilOrFalse("SSH_autostart")
end,
},
-- force-close all connections on stop
{
text = _("Force close all connections on stop"),
help_text = _("When enabled, all active SSH sessions will be terminated immediately when stopping the server. When disabled, active sessions may remain open until they are closed by the client."),
checked_func = function() return self.force_kill_clients end,
callback = function()
self.force_kill_clients = not self.force_kill_clients
G_reader_settings:flipNilOrFalse("SSH_force_kill_clients")
end,
separator = true,
},
}
}
end
function SSH:onDispatcherRegisterActions()
Dispatcher:registerAction("toggle_ssh_server", { category = "none", event = "ToggleSSHServer", title = _("Toggle SSH server"), general=true})
end
return SSH