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.

fuzzy_convert.c 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. /*-
  17. * Copyright (c) 2016, Andrew Lewis <nerf@judo.za.org>
  18. * Copyright (c) 2022, Vsevolod Stakhov <vsevolod@rspamd.com>
  19. *
  20. * Licensed under the Apache License, Version 2.0 (the "License");
  21. * you may not use this file except in compliance with the License.
  22. * You may obtain a copy of the License at
  23. *
  24. * http://www.apache.org/licenses/LICENSE-2.0
  25. *
  26. * Unless required by applicable law or agreed to in writing, software
  27. * distributed under the License is distributed on an "AS IS" BASIS,
  28. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  29. * See the License for the specific language governing permissions and
  30. * limitations under the License.
  31. */
  32. #include "config.h"
  33. #include "rspamadm.h"
  34. #include "lua/lua_common.h"
  35. static char *source_db = NULL;
  36. static char *redis_host = NULL;
  37. static char *redis_db = NULL;
  38. static char *redis_username = NULL;
  39. static char *redis_password = NULL;
  40. static int64_t fuzzy_expiry = 0;
  41. static void rspamadm_fuzzyconvert(int argc, char **argv,
  42. const struct rspamadm_command *cmd);
  43. static const char *rspamadm_fuzzyconvert_help(gboolean full_help,
  44. const struct rspamadm_command *cmd);
  45. struct rspamadm_command fuzzyconvert_command = {
  46. .name = "fuzzyconvert",
  47. .flags = 0,
  48. .help = rspamadm_fuzzyconvert_help,
  49. .run = rspamadm_fuzzyconvert,
  50. .lua_subrs = NULL,
  51. };
  52. static GOptionEntry entries[] = {
  53. {"database", 'd', 0, G_OPTION_ARG_FILENAME, &source_db,
  54. "Input sqlite", NULL},
  55. {"expiry", 'e', 0, G_OPTION_ARG_INT, &fuzzy_expiry,
  56. "Time in seconds after which hashes should be expired", NULL},
  57. {"host", 'h', 0, G_OPTION_ARG_STRING, &redis_host,
  58. "Output redis ip (in format ip:port)", NULL},
  59. {"dbname", 'D', 0, G_OPTION_ARG_STRING, &redis_db,
  60. "Database in redis (should be numeric)", NULL},
  61. {"username", 'u', 0, G_OPTION_ARG_STRING, &redis_username,
  62. "Username to connect to redis", NULL},
  63. {"password", 'p', 0, G_OPTION_ARG_STRING, &redis_password,
  64. "Password to connect to redis", NULL},
  65. {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}};
  66. static const char *
  67. rspamadm_fuzzyconvert_help(gboolean full_help, const struct rspamadm_command *cmd)
  68. {
  69. const char *help_str;
  70. if (full_help) {
  71. help_str = "Convert fuzzy hashes from sqlite3 to redis\n\n"
  72. "Usage: rspamadm fuzzyconvert -d <sqlite_db> -h <redis_ip>\n"
  73. "Where options are:\n\n"
  74. "-d: input sqlite\n"
  75. "-h: output redis ip (in format ip:port)\n"
  76. "-D: output redis database\n"
  77. "-u: redis username\n"
  78. "-p: redis password\n";
  79. }
  80. else {
  81. help_str = "Convert fuzzy hashes from sqlite3 to redis";
  82. }
  83. return help_str;
  84. }
  85. static void
  86. rspamadm_fuzzyconvert(int argc, char **argv, const struct rspamadm_command *cmd)
  87. {
  88. GOptionContext *context;
  89. GError *error = NULL;
  90. ucl_object_t *obj;
  91. context = g_option_context_new(
  92. "fuzzyconvert - converts fuzzy hashes from sqlite3 to redis");
  93. g_option_context_set_summary(context,
  94. "Summary:\n Rspamd administration utility version " RVERSION
  95. "\n Release id: " RID);
  96. g_option_context_add_main_entries(context, entries, NULL);
  97. g_option_context_set_ignore_unknown_options(context, TRUE);
  98. if (!g_option_context_parse(context, &argc, &argv, &error)) {
  99. rspamd_fprintf(stderr, "option parsing failed: %s\n", error->message);
  100. g_error_free(error);
  101. g_option_context_free(context);
  102. exit(EXIT_FAILURE);
  103. }
  104. g_option_context_free(context);
  105. if (!source_db) {
  106. rspamd_fprintf(stderr, "source db is missing\n");
  107. exit(EXIT_FAILURE);
  108. }
  109. if (!redis_host) {
  110. rspamd_fprintf(stderr, "redis host is missing\n");
  111. exit(EXIT_FAILURE);
  112. }
  113. if (!fuzzy_expiry) {
  114. rspamd_fprintf(stderr, "expiry is missing\n");
  115. exit(EXIT_FAILURE);
  116. }
  117. obj = ucl_object_typed_new(UCL_OBJECT);
  118. ucl_object_insert_key(obj, ucl_object_fromstring(source_db),
  119. "source_db", 0, false);
  120. ucl_object_insert_key(obj, ucl_object_fromstring(redis_host),
  121. "redis_host", 0, false);
  122. ucl_object_insert_key(obj, ucl_object_fromint(fuzzy_expiry),
  123. "expiry", 0, false);
  124. if (redis_username) {
  125. ucl_object_insert_key(obj, ucl_object_fromstring(redis_username),
  126. "redis_username", 0, false);
  127. }
  128. if (redis_password) {
  129. ucl_object_insert_key(obj, ucl_object_fromstring(redis_password),
  130. "redis_password", 0, false);
  131. }
  132. if (redis_db) {
  133. ucl_object_insert_key(obj, ucl_object_fromstring(redis_db),
  134. "redis_db", 0, false);
  135. }
  136. rspamadm_execute_lua_ucl_subr(argc,
  137. argv,
  138. obj,
  139. "fuzzy_convert",
  140. TRUE);
  141. ucl_object_unref(obj);
  142. }