auth.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. -- Minetest: builtin/auth.lua
  2. --
  3. -- Authentication handler
  4. --
  5. function core.string_to_privs(str, delim)
  6. assert(type(str) == "string")
  7. delim = delim or ','
  8. local privs = {}
  9. for _, priv in pairs(string.split(str, delim)) do
  10. privs[priv:trim()] = true
  11. end
  12. return privs
  13. end
  14. function core.privs_to_string(privs, delim)
  15. assert(type(privs) == "table")
  16. delim = delim or ','
  17. local list = {}
  18. for priv, bool in pairs(privs) do
  19. if bool then
  20. table.insert(list, priv)
  21. end
  22. end
  23. return table.concat(list, delim)
  24. end
  25. assert(core.string_to_privs("a,b").b == true)
  26. assert(core.privs_to_string({a=true,b=true}) == "a,b")
  27. core.auth_file_path = core.get_worldpath().."/auth.txt"
  28. core.auth_table = {}
  29. local function read_auth_file()
  30. local newtable = {}
  31. local file, errmsg = io.open(core.auth_file_path, 'rb')
  32. if not file then
  33. core.log("info", core.auth_file_path.." could not be opened for reading ("..errmsg.."); assuming new world")
  34. return
  35. end
  36. for line in file:lines() do
  37. if line ~= "" then
  38. local fields = line:split(":", true)
  39. local name, password, privilege_string, last_login = unpack(fields)
  40. last_login = tonumber(last_login)
  41. if not (name and password and privilege_string) then
  42. error("Invalid line in auth.txt: "..dump(line))
  43. end
  44. local privileges = core.string_to_privs(privilege_string)
  45. newtable[name] = {password=password, privileges=privileges, last_login=last_login}
  46. end
  47. end
  48. io.close(file)
  49. core.auth_table = newtable
  50. core.notify_authentication_modified()
  51. end
  52. local function save_auth_file()
  53. local newtable = {}
  54. -- Check table for validness before attempting to save
  55. for name, stuff in pairs(core.auth_table) do
  56. assert(type(name) == "string")
  57. assert(name ~= "")
  58. assert(type(stuff) == "table")
  59. assert(type(stuff.password) == "string")
  60. assert(type(stuff.privileges) == "table")
  61. assert(stuff.last_login == nil or type(stuff.last_login) == "number")
  62. end
  63. local file, errmsg = io.open(core.auth_file_path, 'w+b')
  64. if not file then
  65. error(core.auth_file_path.." could not be opened for writing: "..errmsg)
  66. end
  67. for name, stuff in pairs(core.auth_table) do
  68. local priv_string = core.privs_to_string(stuff.privileges)
  69. local parts = {name, stuff.password, priv_string, stuff.last_login or ""}
  70. file:write(table.concat(parts, ":").."\n")
  71. end
  72. io.close(file)
  73. end
  74. read_auth_file()
  75. core.builtin_auth_handler = {
  76. get_auth = function(name)
  77. assert(type(name) == "string")
  78. -- Figure out what password to use for a new player (singleplayer
  79. -- always has an empty password, otherwise use default, which is
  80. -- usually empty too)
  81. local new_password_hash = ""
  82. -- If not in authentication table, return nil
  83. if not core.auth_table[name] then
  84. return nil
  85. end
  86. -- Figure out what privileges the player should have.
  87. -- Take a copy of the privilege table
  88. local privileges = {}
  89. for priv, _ in pairs(core.auth_table[name].privileges) do
  90. privileges[priv] = true
  91. end
  92. -- If singleplayer, give all privileges except those marked as give_to_singleplayer = false
  93. if core.is_singleplayer() then
  94. for priv, def in pairs(core.registered_privileges) do
  95. if def.give_to_singleplayer then
  96. privileges[priv] = true
  97. end
  98. end
  99. -- For the admin, give everything
  100. elseif name == core.setting_get("name") then
  101. for priv, def in pairs(core.registered_privileges) do
  102. privileges[priv] = true
  103. end
  104. end
  105. -- All done
  106. return {
  107. password = core.auth_table[name].password,
  108. privileges = privileges,
  109. -- Is set to nil if unknown
  110. last_login = core.auth_table[name].last_login,
  111. }
  112. end,
  113. create_auth = function(name, password)
  114. assert(type(name) == "string")
  115. assert(type(password) == "string")
  116. core.log('info', "Built-in authentication handler adding player '"..name.."'")
  117. core.auth_table[name] = {
  118. password = password,
  119. privileges = core.string_to_privs(core.setting_get("default_privs")),
  120. last_login = os.time(),
  121. }
  122. save_auth_file()
  123. end,
  124. set_password = function(name, password)
  125. assert(type(name) == "string")
  126. assert(type(password) == "string")
  127. if not core.auth_table[name] then
  128. core.builtin_auth_handler.create_auth(name, password)
  129. else
  130. core.log('info', "Built-in authentication handler setting password of player '"..name.."'")
  131. core.auth_table[name].password = password
  132. save_auth_file()
  133. end
  134. return true
  135. end,
  136. set_privileges = function(name, privileges)
  137. assert(type(name) == "string")
  138. assert(type(privileges) == "table")
  139. if not core.auth_table[name] then
  140. core.builtin_auth_handler.create_auth(name,
  141. core.get_password_hash(name,
  142. core.setting_get("default_password")))
  143. end
  144. core.auth_table[name].privileges = privileges
  145. core.notify_authentication_modified(name)
  146. save_auth_file()
  147. end,
  148. reload = function()
  149. read_auth_file()
  150. return true
  151. end,
  152. record_login = function(name)
  153. assert(type(name) == "string")
  154. assert(core.auth_table[name]).last_login = os.time()
  155. save_auth_file()
  156. end,
  157. }
  158. function core.register_authentication_handler(handler)
  159. if core.registered_auth_handler then
  160. error("Add-on authentication handler already registered by "..core.registered_auth_handler_modname)
  161. end
  162. core.registered_auth_handler = handler
  163. core.registered_auth_handler_modname = core.get_current_modname()
  164. handler.mod_origin = core.registered_auth_handler_modname
  165. end
  166. function core.get_auth_handler()
  167. return core.registered_auth_handler or core.builtin_auth_handler
  168. end
  169. local function auth_pass(name)
  170. return function(...)
  171. local auth_handler = core.get_auth_handler()
  172. if auth_handler[name] then
  173. return auth_handler[name](...)
  174. end
  175. return false
  176. end
  177. end
  178. core.set_player_password = auth_pass("set_password")
  179. core.set_player_privs = auth_pass("set_privileges")
  180. core.auth_reload = auth_pass("reload")
  181. local record_login = auth_pass("record_login")
  182. core.register_on_joinplayer(function(player)
  183. record_login(player:get_player_name())
  184. end)