2015-05-12 09:59:17 +02:00
|
|
|
-- Minetest: builtin/auth.lua
|
2012-04-01 11:37:41 +02:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Authentication handler
|
|
|
|
--
|
|
|
|
|
2015-11-01 09:01:13 +01:00
|
|
|
function core.string_to_privs(str, delim)
|
|
|
|
assert(type(str) == "string")
|
2012-04-01 11:37:41 +02:00
|
|
|
delim = delim or ','
|
2014-06-05 18:40:34 +02:00
|
|
|
local privs = {}
|
2012-04-01 11:37:41 +02:00
|
|
|
for _, priv in pairs(string.split(str, delim)) do
|
|
|
|
privs[priv:trim()] = true
|
|
|
|
end
|
|
|
|
return privs
|
|
|
|
end
|
|
|
|
|
2015-11-01 09:01:13 +01:00
|
|
|
function core.privs_to_string(privs, delim)
|
2012-04-01 11:37:41 +02:00
|
|
|
assert(type(privs) == "table")
|
|
|
|
delim = delim or ','
|
2014-06-05 18:40:34 +02:00
|
|
|
local list = {}
|
2012-04-01 11:37:41 +02:00
|
|
|
for priv, bool in pairs(privs) do
|
|
|
|
if bool then
|
|
|
|
table.insert(list, priv)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return table.concat(list, delim)
|
|
|
|
end
|
|
|
|
|
2015-11-01 09:01:13 +01:00
|
|
|
assert(core.string_to_privs("a,b").b == true)
|
|
|
|
assert(core.privs_to_string({a=true,b=true}) == "a,b")
|
2012-04-01 11:37:41 +02:00
|
|
|
|
2015-11-01 09:01:13 +01:00
|
|
|
core.auth_file_path = core.get_worldpath().."/auth.txt"
|
|
|
|
core.auth_table = {}
|
2015-05-12 09:59:17 +02:00
|
|
|
|
2012-04-01 11:37:41 +02:00
|
|
|
local function read_auth_file()
|
|
|
|
local newtable = {}
|
2015-11-01 09:01:13 +01:00
|
|
|
local file, errmsg = io.open(core.auth_file_path, 'rb')
|
2012-04-01 11:37:41 +02:00
|
|
|
if not file then
|
2015-11-01 09:01:13 +01:00
|
|
|
core.log("info", core.auth_file_path.." could not be opened for reading ("..errmsg.."); assuming new world")
|
2012-04-01 11:37:41 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
for line in file:lines() do
|
|
|
|
if line ~= "" then
|
2014-10-09 00:11:44 +02:00
|
|
|
local fields = line:split(":", true)
|
|
|
|
local name, password, privilege_string, last_login = unpack(fields)
|
|
|
|
last_login = tonumber(last_login)
|
|
|
|
if not (name and password and privilege_string) then
|
2012-04-01 11:37:41 +02:00
|
|
|
error("Invalid line in auth.txt: "..dump(line))
|
|
|
|
end
|
2015-11-01 09:01:13 +01:00
|
|
|
local privileges = core.string_to_privs(privilege_string)
|
|
|
|
newtable[name] = {password=password, privileges=privileges, last_login=last_login}
|
2012-04-01 11:37:41 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
io.close(file)
|
2015-11-01 09:01:13 +01:00
|
|
|
core.auth_table = newtable
|
|
|
|
core.notify_authentication_modified()
|
2012-04-01 11:37:41 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function save_auth_file()
|
|
|
|
local newtable = {}
|
|
|
|
-- Check table for validness before attempting to save
|
2015-11-01 09:01:13 +01:00
|
|
|
for name, stuff in pairs(core.auth_table) do
|
2012-04-01 11:37:41 +02:00
|
|
|
assert(type(name) == "string")
|
|
|
|
assert(name ~= "")
|
|
|
|
assert(type(stuff) == "table")
|
|
|
|
assert(type(stuff.password) == "string")
|
|
|
|
assert(type(stuff.privileges) == "table")
|
2014-10-09 00:11:44 +02:00
|
|
|
assert(stuff.last_login == nil or type(stuff.last_login) == "number")
|
2012-04-01 11:37:41 +02:00
|
|
|
end
|
2015-11-01 09:01:13 +01:00
|
|
|
local file, errmsg = io.open(core.auth_file_path, 'w+b')
|
2012-04-01 11:37:41 +02:00
|
|
|
if not file then
|
2015-11-01 09:01:13 +01:00
|
|
|
error(core.auth_file_path.." could not be opened for writing: "..errmsg)
|
2012-04-01 11:37:41 +02:00
|
|
|
end
|
2015-11-01 09:01:13 +01:00
|
|
|
for name, stuff in pairs(core.auth_table) do
|
|
|
|
local priv_string = core.privs_to_string(stuff.privileges)
|
|
|
|
local parts = {name, stuff.password, priv_string, stuff.last_login or ""}
|
2014-10-09 00:11:44 +02:00
|
|
|
file:write(table.concat(parts, ":").."\n")
|
2012-04-01 11:37:41 +02:00
|
|
|
end
|
|
|
|
io.close(file)
|
|
|
|
end
|
|
|
|
|
|
|
|
read_auth_file()
|
|
|
|
|
2015-11-01 09:01:13 +01:00
|
|
|
core.builtin_auth_handler = {
|
2012-04-01 11:37:41 +02:00
|
|
|
get_auth = function(name)
|
|
|
|
assert(type(name) == "string")
|
|
|
|
-- Figure out what password to use for a new player (singleplayer
|
|
|
|
-- always has an empty password, otherwise use default, which is
|
|
|
|
-- usually empty too)
|
|
|
|
local new_password_hash = ""
|
2012-04-09 10:24:35 +02:00
|
|
|
-- If not in authentication table, return nil
|
2015-11-01 09:01:13 +01:00
|
|
|
if not core.auth_table[name] then
|
2012-04-09 10:24:35 +02:00
|
|
|
return nil
|
2012-04-01 11:37:41 +02:00
|
|
|
end
|
|
|
|
-- Figure out what privileges the player should have.
|
|
|
|
-- Take a copy of the privilege table
|
|
|
|
local privileges = {}
|
2015-11-01 09:01:13 +01:00
|
|
|
for priv, _ in pairs(core.auth_table[name].privileges) do
|
2012-04-01 11:37:41 +02:00
|
|
|
privileges[priv] = true
|
|
|
|
end
|
|
|
|
-- If singleplayer, give all privileges except those marked as give_to_singleplayer = false
|
2015-11-01 09:01:13 +01:00
|
|
|
if core.is_singleplayer() then
|
|
|
|
for priv, def in pairs(core.registered_privileges) do
|
2012-04-01 11:37:41 +02:00
|
|
|
if def.give_to_singleplayer then
|
|
|
|
privileges[priv] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- For the admin, give everything
|
2015-11-01 09:01:13 +01:00
|
|
|
elseif name == core.setting_get("name") then
|
|
|
|
for priv, def in pairs(core.registered_privileges) do
|
2012-04-01 11:37:41 +02:00
|
|
|
privileges[priv] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- All done
|
|
|
|
return {
|
2015-11-01 09:01:13 +01:00
|
|
|
password = core.auth_table[name].password,
|
2012-04-01 11:37:41 +02:00
|
|
|
privileges = privileges,
|
2014-10-09 00:11:44 +02:00
|
|
|
-- Is set to nil if unknown
|
2015-11-01 09:01:13 +01:00
|
|
|
last_login = core.auth_table[name].last_login,
|
2012-04-01 11:37:41 +02:00
|
|
|
}
|
|
|
|
end,
|
|
|
|
create_auth = function(name, password)
|
|
|
|
assert(type(name) == "string")
|
|
|
|
assert(type(password) == "string")
|
2015-11-01 09:01:13 +01:00
|
|
|
core.log('info', "Built-in authentication handler adding player '"..name.."'")
|
|
|
|
core.auth_table[name] = {
|
2012-04-01 11:37:41 +02:00
|
|
|
password = password,
|
2015-11-01 09:01:13 +01:00
|
|
|
privileges = core.string_to_privs(core.setting_get("default_privs")),
|
2014-10-09 00:11:44 +02:00
|
|
|
last_login = os.time(),
|
2012-04-01 11:37:41 +02:00
|
|
|
}
|
|
|
|
save_auth_file()
|
|
|
|
end,
|
|
|
|
set_password = function(name, password)
|
|
|
|
assert(type(name) == "string")
|
|
|
|
assert(type(password) == "string")
|
2015-11-01 09:01:13 +01:00
|
|
|
if not core.auth_table[name] then
|
|
|
|
core.builtin_auth_handler.create_auth(name, password)
|
2012-04-01 11:37:41 +02:00
|
|
|
else
|
2015-11-01 09:01:13 +01:00
|
|
|
core.log('info', "Built-in authentication handler setting password of player '"..name.."'")
|
|
|
|
core.auth_table[name].password = password
|
2012-04-01 11:37:41 +02:00
|
|
|
save_auth_file()
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end,
|
|
|
|
set_privileges = function(name, privileges)
|
|
|
|
assert(type(name) == "string")
|
|
|
|
assert(type(privileges) == "table")
|
2015-11-01 09:01:13 +01:00
|
|
|
if not core.auth_table[name] then
|
|
|
|
core.builtin_auth_handler.create_auth(name,
|
|
|
|
core.get_password_hash(name,
|
|
|
|
core.setting_get("default_password")))
|
2012-04-01 11:37:41 +02:00
|
|
|
end
|
2015-11-01 09:01:13 +01:00
|
|
|
core.auth_table[name].privileges = privileges
|
|
|
|
core.notify_authentication_modified(name)
|
2012-04-01 11:37:41 +02:00
|
|
|
save_auth_file()
|
|
|
|
end,
|
|
|
|
reload = function()
|
|
|
|
read_auth_file()
|
|
|
|
return true
|
|
|
|
end,
|
2014-10-09 00:11:44 +02:00
|
|
|
record_login = function(name)
|
|
|
|
assert(type(name) == "string")
|
2015-11-01 09:01:13 +01:00
|
|
|
assert(core.auth_table[name]).last_login = os.time()
|
2014-10-09 00:11:44 +02:00
|
|
|
save_auth_file()
|
|
|
|
end,
|
2012-04-01 11:37:41 +02:00
|
|
|
}
|
|
|
|
|
2015-11-01 09:01:13 +01:00
|
|
|
function core.register_authentication_handler(handler)
|
|
|
|
if core.registered_auth_handler then
|
|
|
|
error("Add-on authentication handler already registered by "..core.registered_auth_handler_modname)
|
2012-04-01 11:37:41 +02:00
|
|
|
end
|
2015-11-01 09:01:13 +01:00
|
|
|
core.registered_auth_handler = handler
|
|
|
|
core.registered_auth_handler_modname = core.get_current_modname()
|
2015-11-10 12:49:24 +01:00
|
|
|
handler.mod_origin = core.registered_auth_handler_modname
|
2012-04-01 11:37:41 +02:00
|
|
|
end
|
|
|
|
|
2015-11-01 09:01:13 +01:00
|
|
|
function core.get_auth_handler()
|
|
|
|
return core.registered_auth_handler or core.builtin_auth_handler
|
2012-04-01 11:37:41 +02:00
|
|
|
end
|
|
|
|
|
2014-10-09 00:11:44 +02:00
|
|
|
local function auth_pass(name)
|
|
|
|
return function(...)
|
2015-11-01 09:01:13 +01:00
|
|
|
local auth_handler = core.get_auth_handler()
|
2014-10-09 00:11:44 +02:00
|
|
|
if auth_handler[name] then
|
|
|
|
return auth_handler[name](...)
|
|
|
|
end
|
|
|
|
return false
|
2012-04-01 11:37:41 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-01 09:01:13 +01:00
|
|
|
core.set_player_password = auth_pass("set_password")
|
|
|
|
core.set_player_privs = auth_pass("set_privileges")
|
|
|
|
core.auth_reload = auth_pass("reload")
|
2012-04-01 11:37:41 +02:00
|
|
|
|
|
|
|
|
2014-10-09 00:11:44 +02:00
|
|
|
local record_login = auth_pass("record_login")
|
|
|
|
|
2015-11-01 09:01:13 +01:00
|
|
|
core.register_on_joinplayer(function(player)
|
2014-10-09 00:11:44 +02:00
|
|
|
record_login(player:get_player_name())
|
|
|
|
end)
|
2012-04-01 11:37:41 +02:00
|
|
|
|