2012-04-01 11:37:41 +02:00
|
|
|
-- Minetest: builtin/auth.lua
|
|
|
|
|
|
|
|
--
|
2017-11-27 17:37:41 +01:00
|
|
|
-- Builtin authentication handler
|
2012-04-01 11:37:41 +02:00
|
|
|
--
|
|
|
|
|
2018-08-05 13:13:38 +02:00
|
|
|
-- Make the auth object private, deny access to mods
|
|
|
|
local core_auth = core.auth
|
|
|
|
core.auth = nil
|
2012-04-01 11:37:41 +02:00
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
core.builtin_auth_handler = {
|
2012-04-01 11:37:41 +02:00
|
|
|
get_auth = function(name)
|
|
|
|
assert(type(name) == "string")
|
2018-08-05 13:13:38 +02:00
|
|
|
local auth_entry = core_auth.read(name)
|
|
|
|
-- If no such auth found, return nil
|
|
|
|
if not auth_entry 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 = {}
|
2018-08-05 13:13:38 +02:00
|
|
|
for priv, _ in pairs(auth_entry.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
|
2014-04-28 03:02:48 +02: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
|
2014-12-12 20:49:19 +01:00
|
|
|
elseif name == core.settings:get("name") then
|
2014-04-28 03:02:48 +02:00
|
|
|
for priv, def in pairs(core.registered_privileges) do
|
2017-10-07 15:12:09 +02:00
|
|
|
if def.give_to_admin then
|
|
|
|
privileges[priv] = true
|
|
|
|
end
|
2012-04-01 11:37:41 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
-- All done
|
|
|
|
return {
|
2018-08-05 13:13:38 +02:00
|
|
|
password = auth_entry.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
|
2018-08-05 13:13:38 +02:00
|
|
|
last_login = auth_entry.last_login,
|
2012-04-01 11:37:41 +02:00
|
|
|
}
|
|
|
|
end,
|
|
|
|
create_auth = function(name, password)
|
|
|
|
assert(type(name) == "string")
|
|
|
|
assert(type(password) == "string")
|
2014-04-28 03:02:48 +02:00
|
|
|
core.log('info', "Built-in authentication handler adding player '"..name.."'")
|
2018-08-05 13:13:38 +02:00
|
|
|
return core_auth.create({
|
|
|
|
name = name,
|
2012-04-01 11:37:41 +02:00
|
|
|
password = password,
|
2014-12-12 20:49:19 +01:00
|
|
|
privileges = core.string_to_privs(core.settings:get("default_privs")),
|
2014-10-09 00:11:44 +02:00
|
|
|
last_login = os.time(),
|
2018-08-05 13:13:38 +02:00
|
|
|
})
|
2012-04-01 11:37:41 +02:00
|
|
|
end,
|
2017-12-06 17:32:49 +01:00
|
|
|
delete_auth = function(name)
|
|
|
|
assert(type(name) == "string")
|
2018-08-05 13:13:38 +02:00
|
|
|
local auth_entry = core_auth.read(name)
|
|
|
|
if not auth_entry then
|
2017-12-06 17:32:49 +01:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
core.log('info', "Built-in authentication handler deleting player '"..name.."'")
|
2018-08-05 13:13:38 +02:00
|
|
|
return core_auth.delete(name)
|
2017-12-06 17:32:49 +01:00
|
|
|
end,
|
2012-04-01 11:37:41 +02:00
|
|
|
set_password = function(name, password)
|
|
|
|
assert(type(name) == "string")
|
|
|
|
assert(type(password) == "string")
|
2018-08-05 13:13:38 +02:00
|
|
|
local auth_entry = core_auth.read(name)
|
|
|
|
if not auth_entry then
|
2014-04-28 03:02:48 +02:00
|
|
|
core.builtin_auth_handler.create_auth(name, password)
|
2012-04-01 11:37:41 +02:00
|
|
|
else
|
2014-04-28 03:02:48 +02:00
|
|
|
core.log('info', "Built-in authentication handler setting password of player '"..name.."'")
|
2018-08-05 13:13:38 +02:00
|
|
|
auth_entry.password = password
|
|
|
|
core_auth.save(auth_entry)
|
2012-04-01 11:37:41 +02:00
|
|
|
end
|
|
|
|
return true
|
|
|
|
end,
|
|
|
|
set_privileges = function(name, privileges)
|
|
|
|
assert(type(name) == "string")
|
|
|
|
assert(type(privileges) == "table")
|
2018-08-05 13:13:38 +02:00
|
|
|
local auth_entry = core_auth.read(name)
|
|
|
|
if not auth_entry then
|
|
|
|
auth_entry = core.builtin_auth_handler.create_auth(name,
|
2014-10-09 00:11:44 +02:00
|
|
|
core.get_password_hash(name,
|
2014-12-12 20:49:19 +01:00
|
|
|
core.settings:get("default_password")))
|
2012-04-01 11:37:41 +02:00
|
|
|
end
|
2017-08-26 08:17:05 +02:00
|
|
|
|
|
|
|
-- Run grant callbacks
|
|
|
|
for priv, _ in pairs(privileges) do
|
2018-08-05 13:13:38 +02:00
|
|
|
if not auth_entry.privileges[priv] then
|
2017-08-26 08:17:05 +02:00
|
|
|
core.run_priv_callbacks(name, priv, nil, "grant")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Run revoke callbacks
|
2018-08-05 13:13:38 +02:00
|
|
|
for priv, _ in pairs(auth_entry.privileges) do
|
2017-08-26 08:17:05 +02:00
|
|
|
if not privileges[priv] then
|
|
|
|
core.run_priv_callbacks(name, priv, nil, "revoke")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-05 13:13:38 +02:00
|
|
|
auth_entry.privileges = privileges
|
|
|
|
core_auth.save(auth_entry)
|
2014-04-28 03:02:48 +02:00
|
|
|
core.notify_authentication_modified(name)
|
2012-04-01 11:37:41 +02:00
|
|
|
end,
|
|
|
|
reload = function()
|
2018-08-05 13:13:38 +02:00
|
|
|
core_auth.reload()
|
2012-04-01 11:37:41 +02:00
|
|
|
return true
|
|
|
|
end,
|
2014-10-09 00:11:44 +02:00
|
|
|
record_login = function(name)
|
|
|
|
assert(type(name) == "string")
|
2018-08-05 13:13:38 +02:00
|
|
|
local auth_entry = core_auth.read(name)
|
|
|
|
assert(auth_entry)
|
|
|
|
auth_entry.last_login = os.time()
|
|
|
|
core_auth.save(auth_entry)
|
2014-10-09 00:11:44 +02:00
|
|
|
end,
|
2017-12-06 17:32:49 +01:00
|
|
|
iterate = function()
|
|
|
|
local names = {}
|
2018-08-05 13:13:38 +02:00
|
|
|
local nameslist = core_auth.list_names()
|
|
|
|
for k,v in pairs(nameslist) do
|
|
|
|
names[v] = true
|
2017-12-06 17:32:49 +01:00
|
|
|
end
|
|
|
|
return pairs(names)
|
|
|
|
end,
|
2012-04-01 11:37:41 +02:00
|
|
|
}
|
|
|
|
|
2017-11-27 17:37:41 +01:00
|
|
|
core.register_on_prejoinplayer(function(name, ip)
|
|
|
|
if core.registered_auth_handler ~= nil then
|
|
|
|
return -- Don't do anything if custom auth handler registered
|
|
|
|
end
|
2018-08-05 13:13:38 +02:00
|
|
|
local auth_entry = core_auth.read(name)
|
|
|
|
if auth_entry ~= nil then
|
2017-11-27 17:37:41 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local name_lower = name:lower()
|
2018-08-05 13:13:38 +02:00
|
|
|
for k in core.builtin_auth_handler.iterate() do
|
2017-11-27 17:37:41 +01:00
|
|
|
if k:lower() == name_lower then
|
|
|
|
return string.format("\nCannot create new player called '%s'. "..
|
|
|
|
"Another account called '%s' is already registered. "..
|
|
|
|
"Please check the spelling if it's your account "..
|
|
|
|
"or use a different nickname.", name, k)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Authentication API
|
|
|
|
--
|
|
|
|
|
2014-04-28 03:02:48 +02: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
|
2014-04-28 03:02:48 +02:00
|
|
|
core.registered_auth_handler = handler
|
|
|
|
core.registered_auth_handler_modname = core.get_current_modname()
|
2015-08-12 04:27:54 +02:00
|
|
|
handler.mod_origin = core.registered_auth_handler_modname
|
2012-04-01 11:37:41 +02:00
|
|
|
end
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
function core.get_auth_handler()
|
2014-10-09 00:11:44 +02:00
|
|
|
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(...)
|
|
|
|
local auth_handler = core.get_auth_handler()
|
|
|
|
if auth_handler[name] then
|
|
|
|
return auth_handler[name](...)
|
|
|
|
end
|
|
|
|
return false
|
2012-04-01 11:37:41 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-09 00:11:44 +02:00
|
|
|
core.set_player_password = auth_pass("set_password")
|
|
|
|
core.set_player_privs = auth_pass("set_privileges")
|
2017-12-06 17:32:49 +01:00
|
|
|
core.remove_player_auth = auth_pass("delete_auth")
|
2014-10-09 00:11:44 +02:00
|
|
|
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")
|
|
|
|
core.register_on_joinplayer(function(player)
|
|
|
|
record_login(player:get_player_name())
|
|
|
|
end)
|