You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

170 lines
5.0 KiB

  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "cpp_api/s_server.h"
  17. #include "cpp_api/s_internal.h"
  18. #include "common/c_converter.h"
  19. bool ScriptApiServer::getAuth(const std::string &playername,
  20. std::string *dst_password,
  21. std::set<std::string> *dst_privs)
  22. {
  23. SCRIPTAPI_PRECHECKHEADER
  24. int error_handler = PUSH_ERROR_HANDLER(L);
  25. getAuthHandler();
  26. lua_getfield(L, -1, "get_auth");
  27. if (lua_type(L, -1) != LUA_TFUNCTION)
  28. throw LuaError("Authentication handler missing get_auth");
  29. lua_pushstring(L, playername.c_str());
  30. PCALL_RES(lua_pcall(L, 1, 1, error_handler));
  31. lua_remove(L, -2); // Remove auth handler
  32. lua_remove(L, error_handler);
  33. // nil = login not allowed
  34. if (lua_isnil(L, -1))
  35. return false;
  36. luaL_checktype(L, -1, LUA_TTABLE);
  37. std::string password;
  38. bool found = getstringfield(L, -1, "password", password);
  39. if (!found)
  40. throw LuaError("Authentication handler didn't return password");
  41. if (dst_password)
  42. *dst_password = password;
  43. lua_getfield(L, -1, "privileges");
  44. if (!lua_istable(L, -1))
  45. throw LuaError("Authentication handler didn't return privilege table");
  46. if (dst_privs)
  47. readPrivileges(-1, *dst_privs);
  48. lua_pop(L, 1);
  49. return true;
  50. }
  51. void ScriptApiServer::getAuthHandler()
  52. {
  53. lua_State *L = getStack();
  54. lua_getglobal(L, "core");
  55. lua_getfield(L, -1, "registered_auth_handler");
  56. if (lua_isnil(L, -1)){
  57. lua_pop(L, 1);
  58. lua_getfield(L, -1, "builtin_auth_handler");
  59. }
  60. setOriginFromTable(-1);
  61. lua_remove(L, -2); // Remove core
  62. if (lua_type(L, -1) != LUA_TTABLE)
  63. throw LuaError("Authentication handler table not valid");
  64. }
  65. void ScriptApiServer::readPrivileges(int index, std::set<std::string> &result)
  66. {
  67. lua_State *L = getStack();
  68. result.clear();
  69. lua_pushnil(L);
  70. if (index < 0)
  71. index -= 1;
  72. while (lua_next(L, index) != 0) {
  73. // key at index -2 and value at index -1
  74. std::string key = luaL_checkstring(L, -2);
  75. bool value = readParam<bool>(L, -1);
  76. if (value)
  77. result.insert(key);
  78. // removes value, keeps key for next iteration
  79. lua_pop(L, 1);
  80. }
  81. }
  82. void ScriptApiServer::createAuth(const std::string &playername,
  83. const std::string &password)
  84. {
  85. SCRIPTAPI_PRECHECKHEADER
  86. int error_handler = PUSH_ERROR_HANDLER(L);
  87. getAuthHandler();
  88. lua_getfield(L, -1, "create_auth");
  89. lua_remove(L, -2); // Remove auth handler
  90. if (lua_type(L, -1) != LUA_TFUNCTION)
  91. throw LuaError("Authentication handler missing create_auth");
  92. lua_pushstring(L, playername.c_str());
  93. lua_pushstring(L, password.c_str());
  94. PCALL_RES(lua_pcall(L, 2, 0, error_handler));
  95. lua_pop(L, 1); // Pop error handler
  96. }
  97. bool ScriptApiServer::setPassword(const std::string &playername,
  98. const std::string &password)
  99. {
  100. SCRIPTAPI_PRECHECKHEADER
  101. int error_handler = PUSH_ERROR_HANDLER(L);
  102. getAuthHandler();
  103. lua_getfield(L, -1, "set_password");
  104. lua_remove(L, -2); // Remove auth handler
  105. if (lua_type(L, -1) != LUA_TFUNCTION)
  106. throw LuaError("Authentication handler missing set_password");
  107. lua_pushstring(L, playername.c_str());
  108. lua_pushstring(L, password.c_str());
  109. PCALL_RES(lua_pcall(L, 2, 1, error_handler));
  110. lua_remove(L, error_handler);
  111. return lua_toboolean(L, -1);
  112. }
  113. bool ScriptApiServer::on_chat_message(const std::string &name,
  114. const std::string &message)
  115. {
  116. SCRIPTAPI_PRECHECKHEADER
  117. // Get core.registered_on_chat_messages
  118. lua_getglobal(L, "core");
  119. lua_getfield(L, -1, "registered_on_chat_messages");
  120. // Call callbacks
  121. lua_pushstring(L, name.c_str());
  122. lua_pushstring(L, message.c_str());
  123. runCallbacks(2, RUN_CALLBACKS_MODE_OR_SC);
  124. return readParam<bool>(L, -1);
  125. }
  126. void ScriptApiServer::on_mods_loaded()
  127. {
  128. SCRIPTAPI_PRECHECKHEADER
  129. // Get registered shutdown hooks
  130. lua_getglobal(L, "core");
  131. lua_getfield(L, -1, "registered_on_mods_loaded");
  132. // Call callbacks
  133. runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
  134. }
  135. void ScriptApiServer::on_shutdown()
  136. {
  137. SCRIPTAPI_PRECHECKHEADER
  138. // Get registered shutdown hooks
  139. lua_getglobal(L, "core");
  140. lua_getfield(L, -1, "registered_on_shutdown");
  141. // Call callbacks
  142. runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
  143. }