l_settings.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. Minetest
  3. Copyright (C) 2013 PilzAdam <pilzadam@minetest.net>
  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 "lua_api/l_settings.h"
  17. #include "lua_api/l_internal.h"
  18. #include "cpp_api/s_security.h"
  19. #include "util/string.h" // FlagDesc
  20. #include "settings.h"
  21. #include "noise.h"
  22. #include "log.h"
  23. #define SET_SECURITY_CHECK(L, name) \
  24. if (o->m_settings == g_settings && ScriptApiSecurity::isSecure(L) && \
  25. name.compare(0, 7, "secure.") == 0) { \
  26. throw LuaError("Attempt to set secure setting."); \
  27. }
  28. LuaSettings::LuaSettings(Settings *settings, const std::string &filename) :
  29. m_settings(settings),
  30. m_filename(filename)
  31. {
  32. }
  33. LuaSettings::LuaSettings(const std::string &filename, bool write_allowed) :
  34. m_filename(filename),
  35. m_is_own_settings(true),
  36. m_write_allowed(write_allowed)
  37. {
  38. m_settings = new Settings();
  39. m_settings->readConfigFile(filename.c_str());
  40. }
  41. LuaSettings::~LuaSettings()
  42. {
  43. if (m_is_own_settings)
  44. delete m_settings;
  45. }
  46. void LuaSettings::create(lua_State *L, Settings *settings,
  47. const std::string &filename)
  48. {
  49. LuaSettings *o = new LuaSettings(settings, filename);
  50. *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
  51. luaL_getmetatable(L, className);
  52. lua_setmetatable(L, -2);
  53. }
  54. // garbage collector
  55. int LuaSettings::gc_object(lua_State* L)
  56. {
  57. LuaSettings* o = *(LuaSettings **)(lua_touserdata(L, 1));
  58. delete o;
  59. return 0;
  60. }
  61. // get(self, key) -> value
  62. int LuaSettings::l_get(lua_State* L)
  63. {
  64. NO_MAP_LOCK_REQUIRED;
  65. LuaSettings* o = checkobject(L, 1);
  66. std::string key = std::string(luaL_checkstring(L, 2));
  67. if (o->m_settings->exists(key)) {
  68. std::string value = o->m_settings->get(key);
  69. lua_pushstring(L, value.c_str());
  70. } else {
  71. lua_pushnil(L);
  72. }
  73. return 1;
  74. }
  75. // get_bool(self, key) -> boolean
  76. int LuaSettings::l_get_bool(lua_State* L)
  77. {
  78. NO_MAP_LOCK_REQUIRED;
  79. LuaSettings* o = checkobject(L, 1);
  80. std::string key = std::string(luaL_checkstring(L, 2));
  81. if (o->m_settings->exists(key)) {
  82. bool value = o->m_settings->getBool(key);
  83. lua_pushboolean(L, value);
  84. } else {
  85. // Push default value
  86. if (lua_isboolean(L, 3))
  87. lua_pushboolean(L, readParam<bool>(L, 3));
  88. else
  89. lua_pushnil(L);
  90. }
  91. return 1;
  92. }
  93. // get_np_group(self, key) -> value
  94. int LuaSettings::l_get_np_group(lua_State *L)
  95. {
  96. NO_MAP_LOCK_REQUIRED;
  97. LuaSettings *o = checkobject(L, 1);
  98. std::string key = std::string(luaL_checkstring(L, 2));
  99. if (o->m_settings->exists(key)) {
  100. NoiseParams np;
  101. o->m_settings->getNoiseParams(key, np);
  102. push_noiseparams(L, &np);
  103. } else {
  104. lua_pushnil(L);
  105. }
  106. return 1;
  107. }
  108. int LuaSettings::l_get_flags(lua_State *L)
  109. {
  110. NO_MAP_LOCK_REQUIRED;
  111. LuaSettings *o = checkobject(L, 1);
  112. std::string key = std::string(luaL_checkstring(L, 2));
  113. u32 flags = 0;
  114. auto flagdesc = o->m_settings->getFlagDescFallback(key);
  115. if (o->m_settings->getFlagStrNoEx(key, flags, flagdesc)) {
  116. lua_newtable(L);
  117. int table = lua_gettop(L);
  118. for (size_t i = 0; flagdesc[i].name; ++i) {
  119. lua_pushboolean(L, flags & flagdesc[i].flag);
  120. lua_setfield(L, table, flagdesc[i].name);
  121. }
  122. lua_pushvalue(L, table);
  123. } else {
  124. lua_pushnil(L);
  125. }
  126. return 1;
  127. }
  128. // set(self, key, value)
  129. int LuaSettings::l_set(lua_State* L)
  130. {
  131. NO_MAP_LOCK_REQUIRED;
  132. LuaSettings* o = checkobject(L, 1);
  133. std::string key = std::string(luaL_checkstring(L, 2));
  134. const char* value = luaL_checkstring(L, 3);
  135. SET_SECURITY_CHECK(L, key);
  136. if (!o->m_settings->set(key, value))
  137. throw LuaError("Invalid sequence found in setting parameters");
  138. return 0;
  139. }
  140. // set_bool(self, key, value)
  141. int LuaSettings::l_set_bool(lua_State* L)
  142. {
  143. NO_MAP_LOCK_REQUIRED;
  144. LuaSettings* o = checkobject(L, 1);
  145. std::string key = std::string(luaL_checkstring(L, 2));
  146. bool value = readParam<bool>(L, 3);
  147. SET_SECURITY_CHECK(L, key);
  148. o->m_settings->setBool(key, value);
  149. return 1;
  150. }
  151. // set(self, key, value)
  152. int LuaSettings::l_set_np_group(lua_State *L)
  153. {
  154. NO_MAP_LOCK_REQUIRED;
  155. LuaSettings *o = checkobject(L, 1);
  156. std::string key = std::string(luaL_checkstring(L, 2));
  157. NoiseParams value;
  158. read_noiseparams(L, 3, &value);
  159. SET_SECURITY_CHECK(L, key);
  160. o->m_settings->setNoiseParams(key, value);
  161. return 0;
  162. }
  163. // remove(self, key) -> success
  164. int LuaSettings::l_remove(lua_State* L)
  165. {
  166. NO_MAP_LOCK_REQUIRED;
  167. LuaSettings* o = checkobject(L, 1);
  168. std::string key = std::string(luaL_checkstring(L, 2));
  169. SET_SECURITY_CHECK(L, key);
  170. bool success = o->m_settings->remove(key);
  171. lua_pushboolean(L, success);
  172. return 1;
  173. }
  174. // get_names(self) -> {key1, ...}
  175. int LuaSettings::l_get_names(lua_State* L)
  176. {
  177. NO_MAP_LOCK_REQUIRED;
  178. LuaSettings* o = checkobject(L, 1);
  179. std::vector<std::string> keys = o->m_settings->getNames();
  180. lua_newtable(L);
  181. for (unsigned int i=0; i < keys.size(); i++)
  182. {
  183. lua_pushstring(L, keys[i].c_str());
  184. lua_rawseti(L, -2, i + 1);
  185. }
  186. return 1;
  187. }
  188. // write(self) -> success
  189. int LuaSettings::l_write(lua_State* L)
  190. {
  191. NO_MAP_LOCK_REQUIRED;
  192. LuaSettings* o = checkobject(L, 1);
  193. if (!o->m_write_allowed) {
  194. throw LuaError("Settings: writing " + o->m_filename +
  195. " not allowed with mod security on.");
  196. }
  197. bool success = o->m_settings->updateConfigFile(o->m_filename.c_str());
  198. lua_pushboolean(L, success);
  199. return 1;
  200. }
  201. // to_table(self) -> {[key1]=value1,...}
  202. int LuaSettings::l_to_table(lua_State* L)
  203. {
  204. NO_MAP_LOCK_REQUIRED;
  205. LuaSettings* o = checkobject(L, 1);
  206. std::vector<std::string> keys = o->m_settings->getNames();
  207. lua_newtable(L);
  208. for (const std::string &key : keys) {
  209. lua_pushstring(L, o->m_settings->get(key).c_str());
  210. lua_setfield(L, -2, key.c_str());
  211. }
  212. return 1;
  213. }
  214. void LuaSettings::Register(lua_State* L)
  215. {
  216. lua_newtable(L);
  217. int methodtable = lua_gettop(L);
  218. luaL_newmetatable(L, className);
  219. int metatable = lua_gettop(L);
  220. lua_pushliteral(L, "__metatable");
  221. lua_pushvalue(L, methodtable);
  222. lua_settable(L, metatable); // hide metatable from Lua getmetatable()
  223. lua_pushliteral(L, "__index");
  224. lua_pushvalue(L, methodtable);
  225. lua_settable(L, metatable);
  226. lua_pushliteral(L, "__gc");
  227. lua_pushcfunction(L, gc_object);
  228. lua_settable(L, metatable);
  229. lua_pop(L, 1); // drop metatable
  230. luaL_openlib(L, 0, methods, 0); // fill methodtable
  231. lua_pop(L, 1); // drop methodtable
  232. // Can be created from Lua (Settings(filename))
  233. lua_register(L, className, create_object);
  234. }
  235. // LuaSettings(filename)
  236. // Creates a LuaSettings and leaves it on top of the stack
  237. int LuaSettings::create_object(lua_State* L)
  238. {
  239. NO_MAP_LOCK_REQUIRED;
  240. bool write_allowed = true;
  241. const char* filename = luaL_checkstring(L, 1);
  242. CHECK_SECURE_PATH_POSSIBLE_WRITE(L, filename, &write_allowed);
  243. LuaSettings* o = new LuaSettings(filename, write_allowed);
  244. *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
  245. luaL_getmetatable(L, className);
  246. lua_setmetatable(L, -2);
  247. return 1;
  248. }
  249. LuaSettings* LuaSettings::checkobject(lua_State* L, int narg)
  250. {
  251. NO_MAP_LOCK_REQUIRED;
  252. luaL_checktype(L, narg, LUA_TUSERDATA);
  253. void *ud = luaL_checkudata(L, narg, className);
  254. if (!ud)
  255. luaL_typerror(L, narg, className);
  256. return *(LuaSettings**) ud; // unbox pointer
  257. }
  258. const char LuaSettings::className[] = "Settings";
  259. const luaL_Reg LuaSettings::methods[] = {
  260. luamethod(LuaSettings, get),
  261. luamethod(LuaSettings, get_bool),
  262. luamethod(LuaSettings, get_np_group),
  263. luamethod(LuaSettings, get_flags),
  264. luamethod(LuaSettings, set),
  265. luamethod(LuaSettings, set_bool),
  266. luamethod(LuaSettings, set_np_group),
  267. luamethod(LuaSettings, remove),
  268. luamethod(LuaSettings, get_names),
  269. luamethod(LuaSettings, write),
  270. luamethod(LuaSettings, to_table),
  271. {0,0}
  272. };