2013-08-11 04:09:45 +02:00
|
|
|
/*
|
|
|
|
Minetest
|
|
|
|
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cpp_api/s_server.h"
|
|
|
|
#include "cpp_api/s_internal.h"
|
|
|
|
#include "common/c_converter.h"
|
|
|
|
|
|
|
|
bool ScriptApiServer::getAuth(const std::string &playername,
|
|
|
|
std::string *dst_password,
|
|
|
|
std::set<std::string> *dst_privs)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
2013-11-05 18:06:15 +01:00
|
|
|
lua_pushcfunction(L, script_error_handler);
|
|
|
|
int errorhandler = lua_gettop(L);
|
|
|
|
|
2013-08-11 04:09:45 +02:00
|
|
|
getAuthHandler();
|
|
|
|
lua_getfield(L, -1, "get_auth");
|
|
|
|
if(lua_type(L, -1) != LUA_TFUNCTION)
|
2014-03-15 21:01:06 +01:00
|
|
|
throw LuaError(NULL, "Authentication handler missing get_auth");
|
2013-08-11 04:09:45 +02:00
|
|
|
lua_pushstring(L, playername.c_str());
|
2013-11-05 18:06:15 +01:00
|
|
|
if(lua_pcall(L, 1, 1, errorhandler))
|
|
|
|
scriptError();
|
|
|
|
lua_remove(L, -2); // Remove auth handler
|
|
|
|
lua_remove(L, errorhandler); // Remove error handler
|
2013-08-11 04:09:45 +02:00
|
|
|
|
|
|
|
// nil = login not allowed
|
|
|
|
if(lua_isnil(L, -1))
|
|
|
|
return false;
|
|
|
|
luaL_checktype(L, -1, LUA_TTABLE);
|
|
|
|
|
|
|
|
std::string password;
|
|
|
|
bool found = getstringfield(L, -1, "password", password);
|
|
|
|
if(!found)
|
2014-03-15 21:01:06 +01:00
|
|
|
throw LuaError(NULL, "Authentication handler didn't return password");
|
2013-08-11 04:09:45 +02:00
|
|
|
if(dst_password)
|
|
|
|
*dst_password = password;
|
|
|
|
|
|
|
|
lua_getfield(L, -1, "privileges");
|
|
|
|
if(!lua_istable(L, -1))
|
2014-03-15 21:01:06 +01:00
|
|
|
throw LuaError(NULL, "Authentication handler didn't return privilege table");
|
2013-08-11 04:09:45 +02:00
|
|
|
if(dst_privs)
|
|
|
|
readPrivileges(-1, *dst_privs);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptApiServer::getAuthHandler()
|
|
|
|
{
|
|
|
|
lua_State *L = getStack();
|
|
|
|
|
|
|
|
lua_getglobal(L, "minetest");
|
|
|
|
lua_getfield(L, -1, "registered_auth_handler");
|
|
|
|
if(lua_isnil(L, -1)){
|
|
|
|
lua_pop(L, 1);
|
|
|
|
lua_getfield(L, -1, "builtin_auth_handler");
|
|
|
|
}
|
2013-11-05 18:06:15 +01:00
|
|
|
lua_remove(L, -2); // Remove minetest
|
2013-08-11 04:09:45 +02:00
|
|
|
if(lua_type(L, -1) != LUA_TTABLE)
|
2014-03-15 21:01:06 +01:00
|
|
|
throw LuaError(NULL, "Authentication handler table not valid");
|
2013-08-11 04:09:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptApiServer::readPrivileges(int index, std::set<std::string> &result)
|
|
|
|
{
|
|
|
|
lua_State *L = getStack();
|
|
|
|
|
|
|
|
result.clear();
|
|
|
|
lua_pushnil(L);
|
|
|
|
if(index < 0)
|
|
|
|
index -= 1;
|
|
|
|
while(lua_next(L, index) != 0){
|
|
|
|
// key at index -2 and value at index -1
|
|
|
|
std::string key = luaL_checkstring(L, -2);
|
|
|
|
bool value = lua_toboolean(L, -1);
|
|
|
|
if(value)
|
|
|
|
result.insert(key);
|
|
|
|
// removes value, keeps key for next iteration
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptApiServer::createAuth(const std::string &playername,
|
|
|
|
const std::string &password)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
2013-11-05 18:06:15 +01:00
|
|
|
lua_pushcfunction(L, script_error_handler);
|
|
|
|
int errorhandler = lua_gettop(L);
|
|
|
|
|
2013-08-11 04:09:45 +02:00
|
|
|
getAuthHandler();
|
|
|
|
lua_getfield(L, -1, "create_auth");
|
2013-11-05 18:06:15 +01:00
|
|
|
lua_remove(L, -2); // Remove auth handler
|
2013-08-11 04:09:45 +02:00
|
|
|
if(lua_type(L, -1) != LUA_TFUNCTION)
|
2014-03-15 21:01:06 +01:00
|
|
|
throw LuaError(NULL, "Authentication handler missing create_auth");
|
2013-08-11 04:09:45 +02:00
|
|
|
lua_pushstring(L, playername.c_str());
|
|
|
|
lua_pushstring(L, password.c_str());
|
2013-11-05 18:06:15 +01:00
|
|
|
if(lua_pcall(L, 2, 0, errorhandler))
|
|
|
|
scriptError();
|
|
|
|
lua_pop(L, 1); // Pop error handler
|
2013-08-11 04:09:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ScriptApiServer::setPassword(const std::string &playername,
|
|
|
|
const std::string &password)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
2013-11-05 18:06:15 +01:00
|
|
|
lua_pushcfunction(L, script_error_handler);
|
|
|
|
int errorhandler = lua_gettop(L);
|
|
|
|
|
2013-08-11 04:09:45 +02:00
|
|
|
getAuthHandler();
|
|
|
|
lua_getfield(L, -1, "set_password");
|
2013-11-05 18:06:15 +01:00
|
|
|
lua_remove(L, -2); // Remove auth handler
|
2013-08-11 04:09:45 +02:00
|
|
|
if(lua_type(L, -1) != LUA_TFUNCTION)
|
2014-03-15 21:01:06 +01:00
|
|
|
throw LuaError(NULL, "Authentication handler missing set_password");
|
2013-08-11 04:09:45 +02:00
|
|
|
lua_pushstring(L, playername.c_str());
|
|
|
|
lua_pushstring(L, password.c_str());
|
2013-11-05 18:06:15 +01:00
|
|
|
if(lua_pcall(L, 2, 1, errorhandler))
|
|
|
|
scriptError();
|
|
|
|
lua_remove(L, -2); // Remove error handler
|
2013-08-11 04:09:45 +02:00
|
|
|
return lua_toboolean(L, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScriptApiServer::on_chat_message(const std::string &name,
|
|
|
|
const std::string &message)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
|
|
|
// Get minetest.registered_on_chat_messages
|
|
|
|
lua_getglobal(L, "minetest");
|
|
|
|
lua_getfield(L, -1, "registered_on_chat_messages");
|
|
|
|
// Call callbacks
|
|
|
|
lua_pushstring(L, name.c_str());
|
|
|
|
lua_pushstring(L, message.c_str());
|
|
|
|
script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_OR_SC);
|
|
|
|
bool ate = lua_toboolean(L, -1);
|
|
|
|
return ate;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptApiServer::on_shutdown()
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
|
|
|
// Get registered shutdown hooks
|
|
|
|
lua_getglobal(L, "minetest");
|
|
|
|
lua_getfield(L, -1, "registered_on_shutdown");
|
|
|
|
// Call callbacks
|
|
|
|
script_run_callbacks(L, 0, RUN_CALLBACKS_MODE_FIRST);
|
|
|
|
}
|
|
|
|
|