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.

commands.c 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "rspamadm.h"
  17. #include "libutil/util.h"
  18. #include "libserver/logger.h"
  19. #include "lua/lua_common.h"
  20. #include "lua/lua_thread_pool.h"
  21. extern struct rspamadm_command pw_command;
  22. extern struct rspamadm_command configtest_command;
  23. extern struct rspamadm_command configdump_command;
  24. extern struct rspamadm_command control_command;
  25. extern struct rspamadm_command confighelp_command;
  26. extern struct rspamadm_command statconvert_command;
  27. extern struct rspamadm_command fuzzyconvert_command;
  28. extern struct rspamadm_command signtool_command;
  29. extern struct rspamadm_command lua_command;
  30. const struct rspamadm_command *commands[] = {
  31. &help_command,
  32. &pw_command,
  33. &configtest_command,
  34. &configdump_command,
  35. &control_command,
  36. &confighelp_command,
  37. &statconvert_command,
  38. &fuzzyconvert_command,
  39. &signtool_command,
  40. &lua_command,
  41. NULL};
  42. const struct rspamadm_command *
  43. rspamadm_search_command(const char *name, GPtrArray *all_commands)
  44. {
  45. const struct rspamadm_command *ret = NULL, *cmd;
  46. const char *alias;
  47. unsigned int i, j;
  48. if (name == NULL) {
  49. name = "help";
  50. }
  51. PTR_ARRAY_FOREACH(all_commands, i, cmd)
  52. {
  53. if (strcmp(name, cmd->name) == 0) {
  54. ret = cmd;
  55. break;
  56. }
  57. PTR_ARRAY_FOREACH(cmd->aliases, j, alias)
  58. {
  59. if (strcmp(name, alias) == 0) {
  60. ret = cmd;
  61. break;
  62. }
  63. }
  64. }
  65. return ret;
  66. }
  67. void rspamadm_fill_internal_commands(GPtrArray *dest)
  68. {
  69. unsigned int i;
  70. for (i = 0; i < G_N_ELEMENTS(commands); i++) {
  71. if (commands[i]) {
  72. g_ptr_array_add(dest, (gpointer) commands[i]);
  73. }
  74. }
  75. }
  76. static void
  77. lua_thread_str_error_cb(struct thread_entry *thread, int ret, const char *msg)
  78. {
  79. msg_err("call to rspamadm lua script failed (%d): %s",
  80. ret, msg);
  81. }
  82. static void
  83. rspamadm_lua_command_run(int argc, char **argv,
  84. const struct rspamadm_command *cmd)
  85. {
  86. struct thread_entry *thread = lua_thread_pool_get_for_config(rspamd_main->cfg);
  87. lua_State *L = thread->lua_state;
  88. int table_idx = GPOINTER_TO_INT(cmd->command_data);
  89. int i;
  90. /* Function */
  91. lua_rawgeti(L, LUA_REGISTRYINDEX, table_idx);
  92. lua_pushstring(L, "handler");
  93. lua_gettable(L, -2);
  94. /* Args */
  95. lua_createtable(L, argc + 1, 0);
  96. for (i = 0; i < argc; i++) {
  97. lua_pushstring(L, argv[i]);
  98. lua_rawseti(L, -2, i); /* Starting from zero ! */
  99. }
  100. if (lua_repl_thread_call(thread, 1, (void *) cmd, lua_thread_str_error_cb) != 0) {
  101. exit(EXIT_FAILURE);
  102. }
  103. lua_settop(L, 0);
  104. }
  105. static const char *
  106. rspamadm_lua_command_help(gboolean full_help,
  107. const struct rspamadm_command *cmd)
  108. {
  109. int table_idx = GPOINTER_TO_INT(cmd->command_data);
  110. if (full_help) {
  111. struct thread_entry *thread = lua_thread_pool_get_for_config(rspamd_main->cfg);
  112. lua_State *L = thread->lua_state;
  113. lua_rawgeti(L, LUA_REGISTRYINDEX, table_idx);
  114. /* Function */
  115. lua_pushstring(L, "handler");
  116. lua_gettable(L, -2);
  117. /* Args */
  118. lua_createtable(L, 2, 0);
  119. lua_pushstring(L, cmd->name);
  120. lua_rawseti(L, -2, 0); /* Starting from zero ! */
  121. lua_pushstring(L, "--help");
  122. lua_rawseti(L, -2, 1);
  123. if (lua_repl_thread_call(thread, 1, (void *) cmd, lua_thread_str_error_cb) != 0) {
  124. exit(EXIT_FAILURE);
  125. }
  126. lua_settop(L, 0);
  127. }
  128. else {
  129. lua_State *L = rspamd_main->cfg->lua_state;
  130. lua_rawgeti(L, LUA_REGISTRYINDEX, table_idx);
  131. lua_pushstring(L, "description");
  132. lua_gettable(L, -2);
  133. if (lua_isstring(L, -1)) {
  134. printf(" %-18s %-60s\n", cmd->name, lua_tostring(L, -1));
  135. }
  136. else {
  137. printf(" %-18s %-60s\n", cmd->name, "no description available");
  138. }
  139. lua_settop(L, 0);
  140. }
  141. return NULL; /* Must be handled in rspamadm itself */
  142. }
  143. void rspamadm_fill_lua_commands(lua_State *L, GPtrArray *dest)
  144. {
  145. int i;
  146. GPtrArray *lua_paths;
  147. GError *err = NULL;
  148. const char *lualibdir = RSPAMD_LUALIBDIR, *path;
  149. struct rspamadm_command *lua_cmd;
  150. char search_dir[PATH_MAX];
  151. if (g_hash_table_lookup(ucl_vars, "LUALIBDIR")) {
  152. lualibdir = g_hash_table_lookup(ucl_vars, "LUALIBDIR");
  153. }
  154. rspamd_snprintf(search_dir, sizeof(search_dir), "%s%crspamadm%c",
  155. lualibdir, G_DIR_SEPARATOR, G_DIR_SEPARATOR);
  156. if ((lua_paths = rspamd_glob_path(search_dir, "*.lua", FALSE, &err)) == NULL) {
  157. msg_err("cannot glob files in %s/*.lua: %e", search_dir, err);
  158. g_error_free(err);
  159. return;
  160. }
  161. PTR_ARRAY_FOREACH(lua_paths, i, path)
  162. {
  163. if (luaL_dofile(L, path) != 0) {
  164. msg_err("cannot execute lua script %s: %s",
  165. path, lua_tostring(L, -1));
  166. lua_settop(L, 0);
  167. continue;
  168. }
  169. else {
  170. if (lua_type(L, -1) == LUA_TTABLE) {
  171. lua_pushstring(L, "handler");
  172. lua_gettable(L, -2);
  173. }
  174. else {
  175. continue; /* Something goes wrong, huh */
  176. }
  177. if (lua_type(L, -1) != LUA_TFUNCTION) {
  178. msg_err("rspamadm script %s does not have 'handler' field with type "
  179. "function",
  180. path);
  181. continue;
  182. }
  183. /* Pop handler */
  184. lua_pop(L, 1);
  185. lua_cmd = g_malloc0(sizeof(*lua_cmd));
  186. lua_pushstring(L, "name");
  187. lua_gettable(L, -2);
  188. if (lua_type(L, -1) == LUA_TSTRING) {
  189. lua_cmd->name = g_strdup(lua_tostring(L, -1));
  190. }
  191. else {
  192. goffset ext_pos;
  193. char *name;
  194. name = g_path_get_basename(path);
  195. /* Remove .lua */
  196. ext_pos = rspamd_substring_search(path, strlen(path), ".lua", 4);
  197. if (ext_pos != -1) {
  198. name[ext_pos] = '\0';
  199. }
  200. lua_cmd->name = name;
  201. }
  202. lua_pop(L, 1);
  203. lua_pushstring(L, "aliases");
  204. lua_gettable(L, -2);
  205. if (lua_type(L, -1) == LUA_TTABLE) {
  206. lua_cmd->aliases = g_ptr_array_new_full(
  207. rspamd_lua_table_size(L, -1),
  208. g_free);
  209. for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) {
  210. if (lua_isstring(L, -1)) {
  211. g_ptr_array_add(lua_cmd->aliases,
  212. g_strdup(lua_tostring(L, -1)));
  213. }
  214. }
  215. }
  216. lua_pop(L, 1);
  217. lua_pushvalue(L, -1);
  218. /* Reference table itself */
  219. lua_cmd->command_data = GINT_TO_POINTER(luaL_ref(L, LUA_REGISTRYINDEX));
  220. lua_cmd->flags |= RSPAMADM_FLAG_LUA | RSPAMADM_FLAG_DYNAMIC;
  221. lua_cmd->run = rspamadm_lua_command_run;
  222. lua_cmd->help = rspamadm_lua_command_help;
  223. g_ptr_array_add(dest, lua_cmd);
  224. }
  225. lua_settop(L, 0);
  226. }
  227. g_ptr_array_free(lua_paths, TRUE);
  228. }