]> git.lizzy.rs Git - minetest.git/blob - src/script/cpp_api/s_security.h
Add First Person Attachments (#10360)
[minetest.git] / src / script / cpp_api / s_security.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #pragma once
21
22 #include "cpp_api/s_base.h"
23
24
25 #define CHECK_SECURE_PATH_INTERNAL(L, path, write_required, ptr) \
26         if (!ScriptApiSecurity::checkPath(L, path, write_required, ptr)) { \
27                 throw LuaError(std::string("Mod security: Blocked attempted ") + \
28                                 (write_required ? "write to " : "read from ") + path); \
29         }
30 #define CHECK_SECURE_PATH(L, path, write_required) \
31         if (ScriptApiSecurity::isSecure(L)) { \
32                 CHECK_SECURE_PATH_INTERNAL(L, path, write_required, NULL); \
33         }
34 #define CHECK_SECURE_PATH_POSSIBLE_WRITE(L, path, ptr) \
35         if (ScriptApiSecurity::isSecure(L)) { \
36                 CHECK_SECURE_PATH_INTERNAL(L, path, false, ptr); \
37         }
38
39
40 class ScriptApiSecurity : virtual public ScriptApiBase
41 {
42 public:
43         int getThread(lua_State *L);
44         // creates an empty Lua environment
45         void createEmptyEnv(lua_State *L);
46         // sets the enviroment to the table thats on top of the stack
47         void setLuaEnv(lua_State *L, int thread);
48         // Sets up security on the ScriptApi's Lua state
49         void initializeSecurity();
50         void initializeSecurityClient();
51         // Checks if the Lua state has been secured
52         static bool isSecure(lua_State *L);
53         // Loads a string as Lua code safely (doesn't allow bytecode).
54         static bool safeLoadString(lua_State *L, const std::string &code, const char *chunk_name);
55         // Loads a file as Lua code safely (doesn't allow bytecode).
56         static bool safeLoadFile(lua_State *L, const char *path, const char *display_name = NULL);
57         // Checks if mods are allowed to read (and optionally write) to the path
58         static bool checkPath(lua_State *L, const char *path, bool write_required,
59                         bool *write_allowed=NULL);
60
61 private:
62         // Syntax: "sl_" <Library name or 'g' (global)> '_' <Function name>
63         // (sl stands for Secure Lua)
64
65         static int sl_g_dofile(lua_State *L);
66         static int sl_g_load(lua_State *L);
67         static int sl_g_loadfile(lua_State *L);
68         static int sl_g_loadstring(lua_State *L);
69         static int sl_g_require(lua_State *L);
70
71         static int sl_io_open(lua_State *L);
72         static int sl_io_input(lua_State *L);
73         static int sl_io_output(lua_State *L);
74         static int sl_io_lines(lua_State *L);
75
76         static int sl_os_rename(lua_State *L);
77         static int sl_os_remove(lua_State *L);
78 };