3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "cpp_api/s_base.h"
26 #define CHECK_SECURE_PATH_INTERNAL(L, path, write_required, ptr) \
27 if (!ScriptApiSecurity::checkPath(L, path, write_required, ptr)) { \
28 throw LuaError(std::string("Mod security: Blocked attempted ") + \
29 (write_required ? "write to " : "read from ") + path); \
31 #define CHECK_SECURE_PATH(L, path, write_required) \
32 if (ScriptApiSecurity::isSecure(L)) { \
33 CHECK_SECURE_PATH_INTERNAL(L, path, write_required, NULL); \
35 #define CHECK_SECURE_PATH_POSSIBLE_WRITE(L, path, ptr) \
36 if (ScriptApiSecurity::isSecure(L)) { \
37 CHECK_SECURE_PATH_INTERNAL(L, path, false, ptr); \
41 class ScriptApiSecurity : virtual public ScriptApiBase
44 int backupGlobals(lua_State *L);
45 // Sets up security on the ScriptApi's Lua state
46 void initializeSecurity();
47 void initializeSecurityClient();
48 // Checks if the Lua state has been secured
49 static bool isSecure(lua_State *L);
50 // Loads a file as Lua code safely (doesn't allow bytecode).
51 static bool safeLoadFile(lua_State *L, const char *path);
52 // Checks if mods are allowed to read (and optionally write) to the path
53 static bool checkPath(lua_State *L, const char *path, bool write_required,
54 bool *write_allowed=NULL);
57 // Syntax: "sl_" <Library name or 'g' (global)> '_' <Function name>
58 // (sl stands for Secure Lua)
60 static int sl_g_dofile(lua_State *L);
61 static int sl_g_load(lua_State *L);
62 static int sl_g_loadfile(lua_State *L);
63 static int sl_g_loadstring(lua_State *L);
64 static int sl_g_require(lua_State *L);
66 static int sl_io_open(lua_State *L);
67 static int sl_io_input(lua_State *L);
68 static int sl_io_output(lua_State *L);
69 static int sl_io_lines(lua_State *L);
71 static int sl_os_rename(lua_State *L);
72 static int sl_os_remove(lua_State *L);