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.

confighelp.c 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Copyright 2023 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 <ucl.h>
  17. #include "config.h"
  18. #include "rspamadm.h"
  19. #include "cfg_file.h"
  20. #include "cfg_rcl.h"
  21. #include "rspamd.h"
  22. #include "lua/lua_common.h"
  23. static gboolean json = FALSE;
  24. static gboolean compact = FALSE;
  25. static gboolean keyword = FALSE;
  26. static const char *plugins_path = RSPAMD_PLUGINSDIR;
  27. extern struct rspamd_main *rspamd_main;
  28. /* Defined in modules.c */
  29. extern module_t *modules[];
  30. extern worker_t *workers[];
  31. static void rspamadm_confighelp(int argc, char **argv,
  32. const struct rspamadm_command *cmd);
  33. static const char *rspamadm_confighelp_help(gboolean full_help,
  34. const struct rspamadm_command *cmd);
  35. struct rspamadm_command confighelp_command = {
  36. .name = "confighelp",
  37. .flags = 0,
  38. .help = rspamadm_confighelp_help,
  39. .run = rspamadm_confighelp,
  40. .lua_subrs = NULL,
  41. };
  42. static GOptionEntry entries[] = {
  43. {"json", 'j', 0, G_OPTION_ARG_NONE, &json,
  44. "Output json", NULL},
  45. {"compact", 'c', 0, G_OPTION_ARG_NONE, &compact,
  46. "Output compacted", NULL},
  47. {"keyword", 'k', 0, G_OPTION_ARG_NONE, &keyword,
  48. "Search by keyword", NULL},
  49. {"plugins", 'P', 0, G_OPTION_ARG_STRING, &plugins_path,
  50. "Use the following plugin path (" RSPAMD_PLUGINSDIR ")", NULL},
  51. {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}};
  52. static const char *
  53. rspamadm_confighelp_help(gboolean full_help, const struct rspamadm_command *cmd)
  54. {
  55. const char *help_str;
  56. if (full_help) {
  57. help_str = "Shows help for the specified configuration options\n\n"
  58. "Usage: rspamadm confighelp [option[, option...]]\n"
  59. "Where options are:\n\n"
  60. "-c: output compacted JSON\n"
  61. "-j: output pretty formatted JSON\n"
  62. "-k: search by keyword in doc string\n"
  63. "-P: use specific Lua plugins path\n"
  64. "--no-color: disable coloured output\n"
  65. "--short: show only option names\n"
  66. "--no-examples: do not show examples (implied by --short)\n"
  67. "--help: shows available options and commands";
  68. }
  69. else {
  70. help_str = "Shows help for configuration options";
  71. }
  72. return help_str;
  73. }
  74. static void
  75. rspamadm_confighelp_show(struct rspamd_config *cfg, int argc, char **argv,
  76. const char *key, const ucl_object_t *obj)
  77. {
  78. rspamd_fstring_t *out;
  79. rspamd_lua_set_path(cfg->lua_state, NULL, ucl_vars);
  80. out = rspamd_fstring_new();
  81. if (json) {
  82. rspamd_ucl_emit_fstring(obj, UCL_EMIT_JSON, &out);
  83. }
  84. else if (compact) {
  85. rspamd_ucl_emit_fstring(obj, UCL_EMIT_JSON_COMPACT, &out);
  86. }
  87. else {
  88. /* TODO: add lua helper for output */
  89. if (key) {
  90. rspamd_fprintf(stdout, "Showing help for %s%s:\n",
  91. keyword ? "keyword " : "", key);
  92. }
  93. else {
  94. rspamd_fprintf(stdout, "Showing help for all options:\n");
  95. }
  96. rspamadm_execute_lua_ucl_subr(argc,
  97. argv,
  98. obj,
  99. "confighelp",
  100. TRUE);
  101. rspamd_fstring_free(out);
  102. return;
  103. }
  104. rspamd_fprintf(stdout, "%V", out);
  105. rspamd_fprintf(stdout, "\n");
  106. rspamd_fstring_free(out);
  107. }
  108. static void
  109. rspamadm_confighelp_search_word_step(const ucl_object_t *obj,
  110. ucl_object_t *res,
  111. const char *str,
  112. gsize len,
  113. GString *path)
  114. {
  115. ucl_object_iter_t it = NULL;
  116. const ucl_object_t *cur, *elt;
  117. const char *dot_pos;
  118. while ((cur = ucl_object_iterate(obj, &it, true)) != NULL) {
  119. if (cur->keylen > 0) {
  120. rspamd_printf_gstring(path, ".%*s", (int) cur->keylen, cur->key);
  121. if (rspamd_substring_search_caseless(cur->key,
  122. cur->keylen,
  123. str,
  124. len) != -1) {
  125. ucl_object_insert_key(res, ucl_object_ref(cur),
  126. path->str, path->len, true);
  127. goto fin;
  128. }
  129. }
  130. if (ucl_object_type(cur) == UCL_OBJECT) {
  131. elt = ucl_object_lookup(cur, "data");
  132. if (elt != NULL && ucl_object_type(elt) == UCL_STRING) {
  133. if (rspamd_substring_search_caseless(elt->value.sv,
  134. elt->len,
  135. str,
  136. len) != -1) {
  137. ucl_object_insert_key(res, ucl_object_ref(cur),
  138. path->str, path->len, true);
  139. goto fin;
  140. }
  141. }
  142. rspamadm_confighelp_search_word_step(cur, res, str, len, path);
  143. }
  144. fin:
  145. /* Remove the last component of the path */
  146. dot_pos = strrchr(path->str, '.');
  147. if (dot_pos) {
  148. g_string_erase(path, dot_pos - path->str,
  149. path->len - (dot_pos - path->str));
  150. }
  151. }
  152. }
  153. static ucl_object_t *
  154. rspamadm_confighelp_search_word(const ucl_object_t *obj, const char *str)
  155. {
  156. gsize len = strlen(str);
  157. GString *path = g_string_new("");
  158. ucl_object_t *res;
  159. res = ucl_object_typed_new(UCL_OBJECT);
  160. rspamadm_confighelp_search_word_step(obj, res, str, len, path);
  161. return res;
  162. }
  163. __attribute__((noreturn)) static void
  164. rspamadm_confighelp(int argc, char **argv, const struct rspamadm_command *cmd)
  165. {
  166. struct rspamd_config *cfg;
  167. ucl_object_t *doc_obj;
  168. const ucl_object_t *elt;
  169. GOptionContext *context;
  170. GError *error = NULL;
  171. module_t *mod, **pmod;
  172. worker_t **pworker;
  173. struct module_ctx *mod_ctx;
  174. int i, ret = 0, processed_args = 0;
  175. context = g_option_context_new(
  176. "confighelp - displays help for the configuration options");
  177. g_option_context_set_summary(context,
  178. "Summary:\n Rspamd administration utility version " RVERSION
  179. "\n Release id: " RID);
  180. g_option_context_add_main_entries(context, entries, NULL);
  181. g_option_context_set_ignore_unknown_options(context, TRUE);
  182. if (!g_option_context_parse(context, &argc, &argv, &error)) {
  183. rspamd_fprintf(stderr, "option parsing failed: %s\n", error->message);
  184. g_error_free(error);
  185. g_option_context_free(context);
  186. exit(EXIT_FAILURE);
  187. }
  188. g_option_context_free(context);
  189. pworker = &workers[0];
  190. while (*pworker) {
  191. /* Init string quarks */
  192. (void) g_quark_from_static_string((*pworker)->name);
  193. pworker++;
  194. }
  195. cfg = rspamd_config_new(RSPAMD_CONFIG_INIT_SKIP_LUA);
  196. cfg->lua_state = rspamd_main->cfg->lua_state;
  197. cfg->compiled_modules = modules;
  198. cfg->compiled_workers = workers;
  199. rspamd_rcl_config_init(cfg, NULL);
  200. lua_pushboolean(cfg->lua_state, true);
  201. lua_setglobal(cfg->lua_state, "confighelp");
  202. rspamd_rcl_add_lua_plugins_path(cfg->rcl_top_section, cfg, plugins_path, FALSE, NULL);
  203. /* Init modules to get documentation strings */
  204. i = 0;
  205. for (pmod = cfg->compiled_modules; pmod != NULL && *pmod != NULL; pmod++) {
  206. mod = *pmod;
  207. mod_ctx = g_malloc0(sizeof(struct module_ctx));
  208. if (mod->module_init_func(cfg, &mod_ctx) == 0) {
  209. g_ptr_array_add(cfg->c_modules, mod_ctx);
  210. mod_ctx->mod = mod;
  211. mod->ctx_offset = i++;
  212. mod_ctx->mod = mod;
  213. }
  214. }
  215. /* Also init all workers */
  216. for (pworker = cfg->compiled_workers; *pworker != NULL; pworker++) {
  217. (*pworker)->worker_init_func(cfg);
  218. }
  219. /* Init lua modules */
  220. rspamd_lua_set_path(cfg->lua_state, cfg->cfg_ucl_obj, ucl_vars);
  221. rspamd_init_lua_filters(cfg, true, false);
  222. if (argc > 1) {
  223. for (i = 1; i < argc; i++) {
  224. if (argv[i][0] != '-') {
  225. if (keyword) {
  226. doc_obj = rspamadm_confighelp_search_word(cfg->doc_strings,
  227. argv[i]);
  228. }
  229. else {
  230. doc_obj = ucl_object_typed_new(UCL_OBJECT);
  231. elt = ucl_object_lookup_path(cfg->doc_strings, argv[i]);
  232. if (elt) {
  233. ucl_object_insert_key(doc_obj, ucl_object_ref(elt),
  234. argv[i], 0, false);
  235. }
  236. }
  237. if (doc_obj != NULL) {
  238. rspamadm_confighelp_show(cfg, argc, argv, argv[i], doc_obj);
  239. ucl_object_unref(doc_obj);
  240. }
  241. else {
  242. rspamd_fprintf(stderr,
  243. "Cannot find help for %s\n",
  244. argv[i]);
  245. ret = EXIT_FAILURE;
  246. }
  247. processed_args++;
  248. }
  249. }
  250. }
  251. if (processed_args == 0) {
  252. /* Show all documentation strings */
  253. rspamadm_confighelp_show(cfg, argc, argv, NULL, cfg->doc_strings);
  254. }
  255. rspamd_config_free(cfg);
  256. exit(ret);
  257. }