BlockColor-Engine/builtin/game/auth.lua

222 lines
6.7 KiB
Lua
Raw Normal View History

-- Minetest: builtin/auth.lua
2012-04-01 11:37:41 +02:00
--
-- Authentication handler
--
2015-07-05 14:23:28 +02:00
function multicraft.string_to_privs(str, delim)
if type(str) ~= "string" then return end
2012-04-01 11:37:41 +02:00
delim = delim or ','
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-07-05 14:23:28 +02:00
function multicraft.privs_to_string(privs, delim)
2012-04-01 11:37:41 +02:00
assert(type(privs) == "table")
delim = delim or ','
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-07-05 14:23:28 +02:00
assert(multicraft.string_to_privs("a,b").b == true)
assert(multicraft.privs_to_string({a=true,b=true}) == "a,b")
2012-04-01 11:37:41 +02:00
2015-07-05 14:23:28 +02:00
multicraft.auth_file_path = multicraft.get_worldpath().."/auth.txt"
multicraft.auth_table = {}
2012-04-01 11:37:41 +02:00
local hex={}
for i=0,255 do
hex[string.format("%0x",i)]=string.char(i)
hex[string.format("%0X",i)]=string.char(i)
end
local function uri_decode(str)
str = string.gsub (str, "+", " ")
return (str:gsub('%%(%x%x)',hex))
end
function uri_encode (str)
str = string.gsub (str, "([^0-9a-zA-Z_ -])", function (c) return string.format ("%%%02X", string.byte(c)) end)
str = string.gsub (str, " ", "+")
return str
end
2012-04-01 11:37:41 +02:00
local function read_auth_file()
local newtable = {}
2015-07-05 14:23:28 +02:00
local file, errmsg = io.open(multicraft.auth_file_path, 'rb')
2012-04-01 11:37:41 +02:00
if not file then
2015-07-05 14:23:28 +02:00
multicraft.log("info", multicraft.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
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-07-05 14:23:28 +02:00
local privileges = multicraft.string_to_privs(privilege_string)
newtable[uri_decode(name)] = {password=password, privileges=privileges, last_login=last_login}
2012-04-01 11:37:41 +02:00
end
end
io.close(file)
2015-07-05 14:23:28 +02:00
multicraft.auth_table = newtable
multicraft.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-07-05 14:23:28 +02:00
for name, stuff in pairs(multicraft.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")
assert(stuff.last_login == nil or type(stuff.last_login) == "number")
2012-04-01 11:37:41 +02:00
end
2015-07-05 14:23:28 +02:00
local file, errmsg = io.open(multicraft.auth_file_path, 'w+b')
2012-04-01 11:37:41 +02:00
if not file then
2015-07-05 14:23:28 +02:00
error(multicraft.auth_file_path.." could not be opened for writing: "..errmsg)
2012-04-01 11:37:41 +02:00
end
2015-07-05 14:23:28 +02:00
for name, stuff in pairs(multicraft.auth_table) do
local priv_string = multicraft.privs_to_string(stuff.privileges)
local parts = {uri_encode(name), stuff.password, priv_string, stuff.last_login or ""}
file:write(table.concat(parts, ":").."\n")
2012-04-01 11:37:41 +02:00
end
io.close(file)
end
read_auth_file()
2015-07-05 14:23:28 +02:00
multicraft.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 = ""
-- If not in authentication table, return nil
2015-07-05 14:23:28 +02:00
if not multicraft.auth_table[name] then
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-07-05 14:23:28 +02:00
for priv, _ in pairs(multicraft.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-07-05 14:23:28 +02:00
if multicraft.is_singleplayer() then
for priv, def in pairs(multicraft.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-07-05 14:23:28 +02:00
elseif name == multicraft.setting_get("name") then
for priv, def in pairs(multicraft.registered_privileges) do
2012-04-01 11:37:41 +02:00
privileges[priv] = true
end
end
-- All done
return {
2015-07-05 14:23:28 +02:00
password = multicraft.auth_table[name].password,
2012-04-01 11:37:41 +02:00
privileges = privileges,
-- Is set to nil if unknown
2015-07-05 14:23:28 +02:00
last_login = multicraft.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-07-05 14:23:28 +02:00
multicraft.log('info', "Built-in authentication handler adding player '"..name.."'")
local privs = multicraft.setting_get("default_privs")
if multicraft.setting_getbool("creative_mode") and multicraft.setting_get("default_privs_creative") then
privs = multicraft.setting_get("default_privs_creative")
end
2015-07-05 14:23:28 +02:00
multicraft.auth_table[name] = {
2012-04-01 11:37:41 +02:00
password = password,
2015-07-05 14:23:28 +02:00
privileges = multicraft.string_to_privs(privs),
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-07-05 14:23:28 +02:00
if not multicraft.auth_table[name] then
multicraft.builtin_auth_handler.create_auth(name, password)
2012-04-01 11:37:41 +02:00
else
2015-07-05 14:23:28 +02:00
multicraft.log('info', "Built-in authentication handler setting password of player '"..name.."'")
multicraft.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-07-05 14:23:28 +02:00
if not multicraft.auth_table[name] then
multicraft.builtin_auth_handler.create_auth(name,
multicraft.get_password_hash(name,
multicraft.setting_get("default_password")))
2012-04-01 11:37:41 +02:00
end
2015-07-05 14:23:28 +02:00
multicraft.auth_table[name].privileges = privileges
multicraft.notify_authentication_modified(name)
2012-04-01 11:37:41 +02:00
save_auth_file()
end,
reload = function()
read_auth_file()
return true
end,
record_login = function(name)
assert(type(name) == "string")
2015-07-05 14:23:28 +02:00
assert(multicraft.auth_table[name]).last_login = os.time()
save_auth_file()
end,
2012-04-01 11:37:41 +02:00
}
2015-07-05 14:23:28 +02:00
function multicraft.register_authentication_handler(handler)
if multicraft.registered_auth_handler then
error("Add-on authentication handler already registered by "..multicraft.registered_auth_handler_modname)
2012-04-01 11:37:41 +02:00
end
2015-07-05 14:23:28 +02:00
multicraft.registered_auth_handler = handler
multicraft.registered_auth_handler_modname = multicraft.get_current_modname()
2012-04-01 11:37:41 +02:00
end
2015-07-05 14:23:28 +02:00
function multicraft.get_auth_handler()
return multicraft.registered_auth_handler or multicraft.builtin_auth_handler
2012-04-01 11:37:41 +02:00
end
local function auth_pass(name)
return function(...)
2015-07-05 14:23:28 +02:00
local auth_handler = multicraft.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
2015-07-05 14:23:28 +02:00
multicraft.set_player_password = auth_pass("set_password")
multicraft.set_player_privs = auth_pass("set_privileges")
multicraft.auth_reload = auth_pass("reload")
2012-04-01 11:37:41 +02:00
local record_login = auth_pass("record_login")
2015-07-05 14:23:28 +02:00
multicraft.register_on_joinplayer(function(player)
record_login(player:get_player_name())
end)
2012-04-01 11:37:41 +02:00