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 7.9KB

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