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.

rspamc.cxx 63KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170
  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 "config.h"
  17. #include "libutil/util.h"
  18. #include "libserver/http/http_connection.h"
  19. #include "libserver/http/http_private.h"
  20. #include "libserver/cfg_file.h"
  21. #include "rspamdclient.h"
  22. #include "unix-std.h"
  23. #include <vector>
  24. #include <string>
  25. #include <optional>
  26. #include <algorithm>
  27. #include <functional>
  28. #include <cstdint>
  29. #include <cstdio>
  30. #include <cmath>
  31. #include "frozen/string.h"
  32. #include "frozen/unordered_map.h"
  33. #include "fmt/format.h"
  34. #include "fmt/color.h"
  35. #include "libutil/cxx/locked_file.hxx"
  36. #include "libutil/cxx/util.hxx"
  37. #ifdef HAVE_SYS_WAIT_H
  38. #include <sys/wait.h>
  39. #endif
  40. #define DEFAULT_PORT 11333
  41. #define DEFAULT_CONTROL_PORT 11334
  42. static const char *connect_str = "localhost";
  43. static const char *password = nullptr;
  44. static const char *ip = nullptr;
  45. static const char *from = nullptr;
  46. static const char *deliver_to = nullptr;
  47. static const char **rcpts = nullptr;
  48. static const char *user = nullptr;
  49. static const char *helo = nullptr;
  50. static const char *hostname = nullptr;
  51. static const char *classifier = nullptr;
  52. static const char *local_addr = nullptr;
  53. static const char *execute = nullptr;
  54. static const char *sort = nullptr;
  55. static const char **http_headers = nullptr;
  56. static const char **exclude_patterns = nullptr;
  57. static int weight = 0;
  58. static int flag = 0;
  59. static const char *fuzzy_symbol = nullptr;
  60. static const char *dictionary = nullptr;
  61. static int max_requests = 8;
  62. static double timeout = 10.0;
  63. static gboolean pass_all;
  64. static gboolean tty = FALSE;
  65. static gboolean verbose = FALSE;
  66. static gboolean print_commands = FALSE;
  67. static gboolean json = FALSE;
  68. static gboolean compact = FALSE;
  69. static gboolean headers = FALSE;
  70. static gboolean raw = FALSE;
  71. static gboolean ucl_reply = FALSE;
  72. static gboolean extended_urls = FALSE;
  73. static gboolean mime_output = FALSE;
  74. static gboolean empty_input = FALSE;
  75. static gboolean compressed = FALSE;
  76. static gboolean profile = FALSE;
  77. static gboolean skip_images = FALSE;
  78. static gboolean skip_attachments = FALSE;
  79. static const char *key = nullptr;
  80. static const char *user_agent = "rspamc";
  81. std::vector<GPid> children;
  82. static GPatternSpec **exclude_compiled = nullptr;
  83. static struct rspamd_http_context *http_ctx;
  84. static gint retcode = EXIT_SUCCESS;
  85. static gboolean rspamc_password_callback(const gchar *option_name,
  86. const gchar *value,
  87. gpointer data,
  88. GError **error);
  89. static GOptionEntry entries[] =
  90. {
  91. {"connect", 'h', 0, G_OPTION_ARG_STRING, &connect_str,
  92. "Specify host and port", nullptr},
  93. {"password", 'P', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
  94. (void *) &rspamc_password_callback, "Specify control password", nullptr},
  95. {"classifier", 'c', 0, G_OPTION_ARG_STRING, &classifier,
  96. "Classifier to learn spam or ham", nullptr},
  97. {"weight", 'w', 0, G_OPTION_ARG_INT, &weight,
  98. "Weight for fuzzy operations", nullptr},
  99. {"flag", 'f', 0, G_OPTION_ARG_INT, &flag, "Flag for fuzzy operations",
  100. nullptr},
  101. {"pass-all", 'p', 0, G_OPTION_ARG_NONE, &pass_all, "Pass all filters",
  102. nullptr},
  103. {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "More verbose output",
  104. nullptr},
  105. {"ip", 'i', 0, G_OPTION_ARG_STRING, &ip,
  106. "Emulate that message was received from specified ip address",
  107. nullptr},
  108. {"user", 'u', 0, G_OPTION_ARG_STRING, &user,
  109. "Emulate that message was received from specified authenticated user", nullptr},
  110. {"deliver", 'd', 0, G_OPTION_ARG_STRING, &deliver_to,
  111. "Emulate that message is delivered to specified user (for LDA/statistics)", nullptr},
  112. {"from", 'F', 0, G_OPTION_ARG_STRING, &from,
  113. "Emulate that message has specified SMTP FROM address", nullptr},
  114. {"rcpt", 'r', 0, G_OPTION_ARG_STRING_ARRAY, &rcpts,
  115. "Emulate that message has specified SMTP RCPT address", nullptr},
  116. {"helo", 0, 0, G_OPTION_ARG_STRING, &helo,
  117. "Imitate SMTP HELO passing from MTA", nullptr},
  118. {"hostname", 0, 0, G_OPTION_ARG_STRING, &hostname,
  119. "Imitate hostname passing from MTA", nullptr},
  120. {"timeout", 't', 0, G_OPTION_ARG_DOUBLE, &timeout,
  121. "Time in seconds to wait for a reply", nullptr},
  122. {"bind", 'b', 0, G_OPTION_ARG_STRING, &local_addr,
  123. "Bind to specified ip address", nullptr},
  124. {"commands", 0, 0, G_OPTION_ARG_NONE, &print_commands,
  125. "List available commands", nullptr},
  126. {"json", 'j', 0, G_OPTION_ARG_NONE, &json, "Output json reply", nullptr},
  127. {"compact", '\0', 0, G_OPTION_ARG_NONE, &compact, "Output compact json reply", nullptr},
  128. {"headers", 0, 0, G_OPTION_ARG_NONE, &headers, "Output HTTP headers",
  129. nullptr},
  130. {"raw", 0, 0, G_OPTION_ARG_NONE, &raw, "Input is a raw file, not an email file",
  131. nullptr},
  132. {"ucl", 0, 0, G_OPTION_ARG_NONE, &ucl_reply, "Output ucl reply from rspamd",
  133. nullptr},
  134. {"max-requests", 'n', 0, G_OPTION_ARG_INT, &max_requests,
  135. "Maximum count of parallel requests to rspamd", nullptr},
  136. {"extended-urls", 0, 0, G_OPTION_ARG_NONE, &extended_urls,
  137. "Output urls in extended format", nullptr},
  138. {"key", 0, 0, G_OPTION_ARG_STRING, &key,
  139. "Use specified pubkey to encrypt request", nullptr},
  140. {"exec", 'e', 0, G_OPTION_ARG_STRING, &execute,
  141. "Execute the specified command and pass output to it", nullptr},
  142. {"mime", 'm', 0, G_OPTION_ARG_NONE, &mime_output,
  143. "Write mime body of message with headers instead of just a scan's result", nullptr},
  144. {"header", 0, 0, G_OPTION_ARG_STRING_ARRAY, &http_headers,
  145. "Add custom HTTP header to query (can be repeated)", nullptr},
  146. {"exclude", 0, 0, G_OPTION_ARG_STRING_ARRAY, &exclude_patterns,
  147. "Exclude specific glob patterns in file names (can be repeated)", nullptr},
  148. {"sort", 0, 0, G_OPTION_ARG_STRING, &sort,
  149. "Sort output in a specific order (name, weight, frequency, hits)", nullptr},
  150. {"empty", 'E', 0, G_OPTION_ARG_NONE, &empty_input,
  151. "Allow empty input instead of reading from stdin", nullptr},
  152. {"fuzzy-symbol", 'S', 0, G_OPTION_ARG_STRING, &fuzzy_symbol,
  153. "Learn the specified fuzzy symbol", nullptr},
  154. {"compressed", 'z', 0, G_OPTION_ARG_NONE, &compressed,
  155. "Enable zstd compression", nullptr},
  156. {"profile", '\0', 0, G_OPTION_ARG_NONE, &profile,
  157. "Profile symbols execution time", nullptr},
  158. {"dictionary", 'D', 0, G_OPTION_ARG_FILENAME, &dictionary,
  159. "Use dictionary to compress data", nullptr},
  160. {"skip-images", '\0', 0, G_OPTION_ARG_NONE, &skip_images,
  161. "Skip images when learning/unlearning fuzzy", nullptr},
  162. {"skip-attachments", '\0', 0, G_OPTION_ARG_NONE, &skip_attachments,
  163. "Skip attachments when learning/unlearning fuzzy", nullptr},
  164. {"user-agent", 'U', 0, G_OPTION_ARG_STRING, &user_agent,
  165. "Use specific User-Agent instead of \"rspamc\"", nullptr},
  166. {nullptr, 0, 0, G_OPTION_ARG_NONE, nullptr, nullptr, nullptr}
  167. };
  168. static void rspamc_symbols_output(FILE *out, ucl_object_t *obj);
  169. static void rspamc_uptime_output(FILE *out, ucl_object_t *obj);
  170. static void rspamc_counters_output(FILE *out, ucl_object_t *obj);
  171. static void rspamc_stat_output(FILE *out, ucl_object_t *obj);
  172. enum rspamc_command_type {
  173. RSPAMC_COMMAND_UNKNOWN = 0,
  174. RSPAMC_COMMAND_CHECK,
  175. RSPAMC_COMMAND_SYMBOLS,
  176. RSPAMC_COMMAND_LEARN_SPAM,
  177. RSPAMC_COMMAND_LEARN_HAM,
  178. RSPAMC_COMMAND_FUZZY_ADD,
  179. RSPAMC_COMMAND_FUZZY_DEL,
  180. RSPAMC_COMMAND_FUZZY_DELHASH,
  181. RSPAMC_COMMAND_STAT,
  182. RSPAMC_COMMAND_STAT_RESET,
  183. RSPAMC_COMMAND_COUNTERS,
  184. RSPAMC_COMMAND_UPTIME,
  185. RSPAMC_COMMAND_ADD_SYMBOL,
  186. RSPAMC_COMMAND_ADD_ACTION
  187. };
  188. struct rspamc_command {
  189. enum rspamc_command_type cmd;
  190. const char *name;
  191. const char *path;
  192. const char *description;
  193. gboolean is_controller;
  194. gboolean is_privileged;
  195. gboolean need_input;
  196. void (*command_output_func)(FILE *, ucl_object_t *obj);
  197. };
  198. static const constexpr auto rspamc_commands = rspamd::array_of(
  199. rspamc_command{
  200. .cmd = RSPAMC_COMMAND_SYMBOLS,
  201. .name = "symbols",
  202. .path = "checkv2",
  203. .description = "scan message and show symbols (default command)",
  204. .is_controller = FALSE,
  205. .is_privileged = FALSE,
  206. .need_input = TRUE,
  207. .command_output_func = rspamc_symbols_output
  208. },
  209. rspamc_command{
  210. .cmd = RSPAMC_COMMAND_LEARN_SPAM,
  211. .name = "learn_spam",
  212. .path = "learnspam",
  213. .description = "learn message as spam",
  214. .is_controller = TRUE,
  215. .is_privileged = TRUE,
  216. .need_input = TRUE,
  217. .command_output_func = nullptr
  218. },
  219. rspamc_command{
  220. .cmd = RSPAMC_COMMAND_LEARN_HAM,
  221. .name = "learn_ham",
  222. .path = "learnham",
  223. .description = "learn message as ham",
  224. .is_controller = TRUE,
  225. .is_privileged = TRUE,
  226. .need_input = TRUE,
  227. .command_output_func = nullptr
  228. },
  229. rspamc_command{
  230. .cmd = RSPAMC_COMMAND_FUZZY_ADD,
  231. .name = "fuzzy_add",
  232. .path = "fuzzyadd",
  233. .description =
  234. "add hashes from a message to the fuzzy storage (check -f and -w options for this command)",
  235. .is_controller = TRUE,
  236. .is_privileged = TRUE,
  237. .need_input = TRUE,
  238. .command_output_func = nullptr
  239. },
  240. rspamc_command{
  241. .cmd = RSPAMC_COMMAND_FUZZY_DEL,
  242. .name = "fuzzy_del",
  243. .path = "fuzzydel",
  244. .description =
  245. "delete hashes from a message from the fuzzy storage (check -f option for this command)",
  246. .is_controller = TRUE,
  247. .is_privileged = TRUE,
  248. .need_input = TRUE,
  249. .command_output_func = nullptr
  250. },
  251. rspamc_command{
  252. .cmd = RSPAMC_COMMAND_FUZZY_DELHASH,
  253. .name = "fuzzy_delhash",
  254. .path = "fuzzydelhash",
  255. .description =
  256. "delete a hash from fuzzy storage (check -f option for this command)",
  257. .is_controller = TRUE,
  258. .is_privileged = TRUE,
  259. .need_input = FALSE,
  260. .command_output_func = nullptr
  261. },
  262. rspamc_command{
  263. .cmd = RSPAMC_COMMAND_STAT,
  264. .name = "stat",
  265. .path = "stat",
  266. .description = "show rspamd statistics",
  267. .is_controller = TRUE,
  268. .is_privileged = FALSE,
  269. .need_input = FALSE,
  270. .command_output_func = rspamc_stat_output,
  271. },
  272. rspamc_command{
  273. .cmd = RSPAMC_COMMAND_STAT_RESET,
  274. .name = "stat_reset",
  275. .path = "statreset",
  276. .description = "show and reset rspamd statistics (useful for graphs)",
  277. .is_controller = TRUE,
  278. .is_privileged = TRUE,
  279. .need_input = FALSE,
  280. .command_output_func = rspamc_stat_output
  281. },
  282. rspamc_command{
  283. .cmd = RSPAMC_COMMAND_COUNTERS,
  284. .name = "counters",
  285. .path = "counters",
  286. .description = "display rspamd symbols statistics",
  287. .is_controller = TRUE,
  288. .is_privileged = FALSE,
  289. .need_input = FALSE,
  290. .command_output_func = rspamc_counters_output
  291. },
  292. rspamc_command{
  293. .cmd = RSPAMC_COMMAND_UPTIME,
  294. .name = "uptime",
  295. .path = "auth",
  296. .description = "show rspamd uptime",
  297. .is_controller = TRUE,
  298. .is_privileged = FALSE,
  299. .need_input = FALSE,
  300. .command_output_func = rspamc_uptime_output
  301. },
  302. rspamc_command{
  303. .cmd = RSPAMC_COMMAND_ADD_SYMBOL,
  304. .name = "add_symbol",
  305. .path = "addsymbol",
  306. .description = "add or modify symbol settings in rspamd",
  307. .is_controller = TRUE,
  308. .is_privileged = TRUE,
  309. .need_input = FALSE,
  310. .command_output_func = nullptr
  311. },
  312. rspamc_command{
  313. .cmd = RSPAMC_COMMAND_ADD_ACTION,
  314. .name = "add_action",
  315. .path = "addaction",
  316. .description = "add or modify action settings",
  317. .is_controller = TRUE,
  318. .is_privileged = TRUE,
  319. .need_input = FALSE,
  320. .command_output_func = nullptr
  321. }
  322. );
  323. struct rspamc_callback_data {
  324. struct rspamc_command cmd;
  325. std::string filename;
  326. };
  327. template<typename T>
  328. static constexpr auto emphasis_argument(const T &arg) -> auto {
  329. if (tty) {
  330. return fmt::format(fmt::emphasis::bold, "{}", arg);
  331. }
  332. return fmt::format("{}", arg);
  333. }
  334. template<typename T, typename std::enable_if_t<std::is_floating_point_v<T>, bool> = false>
  335. static constexpr auto emphasis_argument(const T &arg, int precision) -> auto {
  336. if (tty) {
  337. return fmt::format(fmt::emphasis::bold, "{:.{}f}", arg, precision);
  338. }
  339. return fmt::format("{:.{}f}", arg, precision);
  340. }
  341. using sort_lambda = std::function<int(const ucl_object_t *, const ucl_object_t *)>;
  342. static const auto sort_map = frozen::make_unordered_map<frozen::string, sort_lambda>({
  343. {"name", [](const ucl_object_t *o1, const ucl_object_t *o2) -> int {
  344. const auto *elt1 = ucl_object_lookup(o1, "symbol");
  345. const auto *elt2 = ucl_object_lookup(o2, "symbol");
  346. if (elt1 && elt2) {
  347. return strcmp(ucl_object_tostring(elt1),
  348. ucl_object_tostring(elt2));
  349. }
  350. else if (ucl_object_key(o1) != nullptr && ucl_object_key(o2) != nullptr) {
  351. return strcmp(ucl_object_key(o1),
  352. ucl_object_key(o2));
  353. }
  354. return 0;
  355. }},
  356. {"weight", [](const ucl_object_t *o1, const ucl_object_t *o2) -> int {
  357. const auto *elt1 = ucl_object_lookup(o1, "weight");
  358. const auto *elt2 = ucl_object_lookup(o2, "weight");
  359. if (elt1 && elt2) {
  360. return ucl_object_todouble(elt2) * 1000.0 - ucl_object_todouble(elt1) * 1000.0;
  361. }
  362. return 0;
  363. }},
  364. {"score", [](const ucl_object_t *o1, const ucl_object_t *o2) -> int {
  365. const auto *elt1 = ucl_object_lookup(o1, "score");
  366. const auto *elt2 = ucl_object_lookup(o2, "score");
  367. if (elt1 && elt2) {
  368. return std::fabs(ucl_object_todouble(elt2)) * 1000.0 -
  369. std::fabs(ucl_object_todouble(elt1)) * 1000.0;
  370. }
  371. return 0;
  372. }},
  373. {"time", [](const ucl_object_t *o1, const ucl_object_t *o2) -> int {
  374. const auto *elt1 = ucl_object_lookup(o1, "time");
  375. const auto *elt2 = ucl_object_lookup(o2, "time");
  376. if (elt1 && elt2) {
  377. return ucl_object_todouble(elt2) * 1000.0 - ucl_object_todouble(elt1) * 1000.0;
  378. }
  379. return 0;
  380. }},
  381. {"frequency", [](const ucl_object_t *o1, const ucl_object_t *o2) -> int {
  382. const auto *elt1 = ucl_object_lookup(o1, "frequency");
  383. const auto *elt2 = ucl_object_lookup(o2, "frequency");
  384. if (elt1 && elt2) {
  385. return ucl_object_todouble(elt2) * 1000.0 - ucl_object_todouble(elt1) * 1000.0;
  386. }
  387. return 0;
  388. }},
  389. {"hits", [](const ucl_object_t *o1, const ucl_object_t *o2) -> int {
  390. const auto *elt1 = ucl_object_lookup(o1, "hits");
  391. const auto *elt2 = ucl_object_lookup(o2, "hits");
  392. if (elt1 && elt2) {
  393. return ucl_object_toint(elt2) - ucl_object_toint(elt1);
  394. }
  395. return 0;
  396. }},
  397. });
  398. /* TODO: remove once migrate to C++20 standard */
  399. static constexpr auto
  400. sv_ends_with(std::string_view inp, std::string_view suffix) -> bool {
  401. return inp.size() >= suffix.size() && inp.compare(inp.size() - suffix.size(), std::string_view::npos, suffix) == 0;
  402. }
  403. template<typename T>
  404. auto sort_ucl_container_with_default(T &cont, const char *default_sort,
  405. typename std::enable_if<std::is_same_v<typename T::value_type, const ucl_object_t *>>::type* = 0) -> void
  406. {
  407. auto real_sort = sort ? sort : default_sort;
  408. if (real_sort) {
  409. auto sort_view = std::string_view{real_sort};
  410. auto inverse = false;
  411. if (sv_ends_with(sort_view, ":asc")) {
  412. inverse = true;
  413. sort_view = std::string_view{sort, strlen(sort) - sizeof(":asc") + 1};
  414. }
  415. const auto sort_functor = sort_map.find(sort_view);
  416. if (sort_functor != sort_map.end()) {
  417. std::stable_sort(std::begin(cont), std::end(cont),
  418. [&](const ucl_object_t *o1, const ucl_object_t *o2) -> int {
  419. auto order = sort_functor->second(o1, o2);
  420. return inverse ? order > 0 : order < 0;
  421. });
  422. }
  423. }
  424. }
  425. static gboolean
  426. rspamc_password_callback(const gchar *option_name,
  427. const gchar *value,
  428. gpointer data,
  429. GError **error)
  430. {
  431. // Some efforts to keep password erased
  432. static std::vector<char, rspamd::secure_mem_allocator<char>> processed_passwd;
  433. processed_passwd.clear();
  434. if (value != nullptr) {
  435. std::string_view value_view{value};
  436. if (value_view[0] == '/' || value_view[0] == '.') {
  437. /* Try to open file */
  438. auto locked_mmap = rspamd::util::raii_mmaped_locked_file::mmap_shared(value, O_RDONLY, PROT_READ);
  439. if (!locked_mmap.has_value() || locked_mmap.value().get_size() == 0) {
  440. /* Just use it as a string */
  441. processed_passwd.assign(std::begin(value_view), std::end(value_view));
  442. processed_passwd.push_back('\0');
  443. }
  444. else {
  445. /* Strip trailing spaces */
  446. auto *map = (char *) locked_mmap.value().get_map();
  447. auto *end = map + locked_mmap.value().get_size() - 1;
  448. while (g_ascii_isspace(*end) && end > map) {
  449. end--;
  450. }
  451. end++;
  452. value_view = std::string_view{map, static_cast<std::size_t>(end - map + 1)};
  453. processed_passwd.assign(std::begin(value_view), std::end(value_view));
  454. processed_passwd.push_back('\0');
  455. }
  456. }
  457. else {
  458. processed_passwd.assign(std::begin(value_view), std::end(value_view));
  459. processed_passwd.push_back('\0');
  460. }
  461. }
  462. else {
  463. /* Read password from console */
  464. auto plen = 8192;
  465. processed_passwd.resize(plen, '\0');
  466. plen = rspamd_read_passphrase(processed_passwd.data(), plen, 0, nullptr);
  467. if (plen == 0) {
  468. fmt::print(stderr, "Invalid password\n");
  469. exit(EXIT_FAILURE);
  470. }
  471. processed_passwd.resize(plen);
  472. processed_passwd.push_back('\0');
  473. }
  474. password = processed_passwd.data();
  475. return TRUE;
  476. }
  477. /*
  478. * Parse command line
  479. */
  480. static void
  481. read_cmd_line(gint *argc, gchar ***argv)
  482. {
  483. GError *error = nullptr;
  484. GOptionContext *context;
  485. /* Prepare parser */
  486. context = g_option_context_new("- run rspamc client");
  487. g_option_context_set_summary(context,
  488. "Summary:\n Rspamd client version " RVERSION "\n Release id: " RID);
  489. g_option_context_add_main_entries(context, entries, nullptr);
  490. /* Parse options */
  491. if (!g_option_context_parse(context, argc, argv, &error)) {
  492. fmt::print(stderr, "option parsing failed: {}\n", error->message);
  493. g_option_context_free(context);
  494. exit(EXIT_FAILURE);
  495. }
  496. if (json || compact) {
  497. ucl_reply = TRUE;
  498. }
  499. /* Argc and argv are shifted after this function */
  500. g_option_context_free(context);
  501. }
  502. static auto
  503. add_client_header(GQueue *opts, const char *hn, const char *hv) -> void
  504. {
  505. g_assert(hn != nullptr);
  506. g_assert(hv != nullptr);
  507. auto *nhdr = g_new(rspamd_http_client_header, 1);
  508. nhdr->name = g_strdup(hn);
  509. nhdr->value = g_strdup(hv);
  510. g_queue_push_tail(opts, (void *) nhdr);
  511. }
  512. static auto
  513. add_client_header(GQueue *opts, std::string_view hn, std::string_view hv) -> void
  514. {
  515. auto *nhdr = g_new(rspamd_http_client_header, 1);
  516. nhdr->name = g_new(char, hn.size() + 1);
  517. rspamd_strlcpy(nhdr->name, hn.data(), hn.size() + 1);
  518. nhdr->value = g_new(char, hv.size() + 1);
  519. rspamd_strlcpy(nhdr->value, hv.data(), hv.size() + 1);
  520. g_queue_push_tail(opts, (void *) nhdr);
  521. }
  522. static auto
  523. rspamd_string_tolower(const char *inp) -> std::string
  524. {
  525. std::string s{inp};
  526. std::transform(std::begin(s), std::end(s), std::begin(s),
  527. [](unsigned char c) { return std::tolower(c); });
  528. return s;
  529. }
  530. static auto
  531. rspamd_action_from_str_rspamc(const char *data) -> std::optional<int>
  532. {
  533. static constexpr const auto str_map = frozen::make_unordered_map<frozen::string, int>({
  534. {"reject", METRIC_ACTION_REJECT},
  535. {"greylist", METRIC_ACTION_GREYLIST},
  536. {"add_header", METRIC_ACTION_ADD_HEADER},
  537. {"add header", METRIC_ACTION_ADD_HEADER},
  538. {"rewrite_subject", METRIC_ACTION_REWRITE_SUBJECT},
  539. {"rewrite subject", METRIC_ACTION_REWRITE_SUBJECT},
  540. {"soft_reject", METRIC_ACTION_SOFT_REJECT},
  541. {"soft reject", METRIC_ACTION_SOFT_REJECT},
  542. {"no_action", METRIC_ACTION_NOACTION},
  543. {"no action", METRIC_ACTION_NOACTION},
  544. });
  545. auto st_lower = rspamd_string_tolower(data);
  546. return rspamd::find_map(str_map, std::string_view{st_lower});
  547. }
  548. /*
  549. * Check rspamc command from string (used for arguments parsing)
  550. */
  551. static auto
  552. check_rspamc_command(const char *cmd) -> std::optional<rspamc_command>
  553. {
  554. static constexpr const auto str_map = frozen::make_unordered_map<frozen::string, int>({
  555. {"symbols", RSPAMC_COMMAND_SYMBOLS},
  556. {"check", RSPAMC_COMMAND_SYMBOLS},
  557. {"report", RSPAMC_COMMAND_SYMBOLS},
  558. {"learn_spam", RSPAMC_COMMAND_LEARN_SPAM},
  559. {"learn_ham", RSPAMC_COMMAND_LEARN_HAM},
  560. {"fuzzy_add", RSPAMC_COMMAND_FUZZY_ADD},
  561. {"fuzzy_del", RSPAMC_COMMAND_FUZZY_DEL},
  562. {"fuzzy_delhash", RSPAMC_COMMAND_FUZZY_DELHASH},
  563. {"stat", RSPAMC_COMMAND_STAT},
  564. {"stat_reset", RSPAMC_COMMAND_STAT_RESET},
  565. {"counters", RSPAMC_COMMAND_COUNTERS},
  566. {"uptime", RSPAMC_COMMAND_UPTIME},
  567. });
  568. std::string cmd_lc = rspamd_string_tolower(cmd);
  569. auto ct = rspamd::find_map(str_map, std::string_view{cmd_lc});
  570. auto elt_it = std::find_if(rspamc_commands.begin(), rspamc_commands.end(), [&](const auto &item) {
  571. return item.cmd == ct;
  572. });
  573. if (elt_it != std::end(rspamc_commands)) {
  574. return *elt_it;
  575. }
  576. return std::nullopt;
  577. }
  578. static void
  579. print_commands_list()
  580. {
  581. guint cmd_len = 0;
  582. fmt::print(stdout, "Rspamc commands summary:\n");
  583. for (const auto &cmd: rspamc_commands) {
  584. auto clen = strlen(cmd.name);
  585. if (clen > cmd_len) {
  586. cmd_len = clen;
  587. }
  588. }
  589. for (const auto &cmd: rspamc_commands) {
  590. fmt::print(stdout,
  591. " {:>{}} ({:7}{:1})\t{}\n",
  592. cmd.name,
  593. cmd_len,
  594. cmd.is_controller ? "control" : "normal",
  595. cmd.is_privileged ? "*" : "",
  596. cmd.description);
  597. }
  598. fmt::print(stdout,
  599. "\n* is for privileged commands that may need password (see -P option)\n");
  600. fmt::print(stdout,
  601. "control commands use port 11334 while normal use 11333 by default (see -h option)\n");
  602. }
  603. static void
  604. add_options(GQueue *opts)
  605. {
  606. std::string flagbuf;
  607. if (ip != nullptr) {
  608. rspamd_inet_addr_t *addr = nullptr;
  609. if (!rspamd_parse_inet_address(&addr, ip, strlen(ip),
  610. RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) {
  611. /* Try to resolve */
  612. struct addrinfo hints, *res, *cur;
  613. int r;
  614. memset(&hints, 0, sizeof(hints));
  615. hints.ai_socktype = SOCK_STREAM; /* Type of the socket */
  616. #ifdef AI_IDN
  617. hints.ai_flags = AI_NUMERICSERV|AI_IDN;
  618. #else
  619. hints.ai_flags = AI_NUMERICSERV;
  620. #endif
  621. hints.ai_family = AF_UNSPEC;
  622. if ((r = getaddrinfo(ip, "25", &hints, &res)) == 0) {
  623. cur = res;
  624. while (cur) {
  625. addr = rspamd_inet_address_from_sa(cur->ai_addr,
  626. cur->ai_addrlen);
  627. if (addr != nullptr) {
  628. ip = g_strdup(rspamd_inet_address_to_string(addr));
  629. rspamd_inet_address_free(addr);
  630. break;
  631. }
  632. cur = cur->ai_next;
  633. }
  634. freeaddrinfo(res);
  635. }
  636. else {
  637. fmt::print(stderr, "address resolution for {} failed: {}\n",
  638. ip,
  639. gai_strerror(r));
  640. }
  641. }
  642. else {
  643. rspamd_inet_address_free(addr);
  644. }
  645. add_client_header(opts, "Ip", ip);
  646. }
  647. if (from != nullptr) {
  648. add_client_header(opts, "From", from);
  649. }
  650. if (user != nullptr) {
  651. add_client_header(opts, "User", user);
  652. }
  653. if (rcpts != nullptr) {
  654. for (auto *rcpt = rcpts; *rcpt != nullptr; rcpt++) {
  655. add_client_header(opts, "Rcpt", *rcpt);
  656. }
  657. }
  658. if (deliver_to != nullptr) {
  659. add_client_header(opts, "Deliver-To", deliver_to);
  660. }
  661. if (helo != nullptr) {
  662. add_client_header(opts, "Helo", helo);
  663. }
  664. if (hostname != nullptr) {
  665. add_client_header(opts, "Hostname", hostname);
  666. }
  667. if (password != nullptr) {
  668. add_client_header(opts, "Password", password);
  669. }
  670. if (pass_all) {
  671. flagbuf += "pass_all,";
  672. }
  673. if (raw) {
  674. add_client_header(opts, "Raw", "yes");
  675. }
  676. if (classifier) {
  677. add_client_header(opts, "Classifier", classifier);
  678. }
  679. if (weight != 0) {
  680. auto nstr = fmt::format("{}", weight);
  681. add_client_header(opts, "Weight", nstr.c_str());
  682. }
  683. if (fuzzy_symbol != nullptr) {
  684. add_client_header(opts, "Symbol", fuzzy_symbol);
  685. }
  686. if (flag != 0) {
  687. auto nstr = fmt::format("{}", flag);
  688. add_client_header(opts, "Flag", nstr.c_str());
  689. }
  690. if (extended_urls) {
  691. add_client_header(opts, "URL-Format", "extended");
  692. }
  693. if (profile) {
  694. flagbuf += "profile,";
  695. }
  696. flagbuf += "body_block,";
  697. if (skip_images) {
  698. add_client_header(opts, "Skip-Images", "true");
  699. }
  700. if (skip_attachments) {
  701. add_client_header(opts, "Skip-Attachments", "true");
  702. }
  703. auto hdr = http_headers;
  704. while (hdr != nullptr && *hdr != nullptr) {
  705. std::string_view hdr_view{*hdr};
  706. auto delim_pos = std::find_if(std::begin(hdr_view), std::end(hdr_view), [](auto c) {
  707. return c == ':' || c == '=';
  708. });
  709. if (delim_pos == std::end(hdr_view)) {
  710. /* Just a header name with no value */
  711. add_client_header(opts, *hdr, "");
  712. }
  713. else {
  714. add_client_header(opts,
  715. hdr_view.substr(0, std::distance(std::begin(hdr_view), delim_pos)),
  716. hdr_view.substr(std::distance(std::begin(hdr_view), delim_pos) + 1));
  717. }
  718. hdr++;
  719. }
  720. if (!flagbuf.empty()) {
  721. if (flagbuf.back() == ',') {
  722. flagbuf.pop_back();
  723. }
  724. add_client_header(opts, "Flags", flagbuf.c_str());
  725. }
  726. }
  727. static void
  728. rspamc_symbol_output(FILE *out, const ucl_object_t *obj)
  729. {
  730. auto first = true;
  731. fmt::print(out, "Symbol: {} ", ucl_object_key(obj));
  732. const auto *val = ucl_object_lookup(obj, "score");
  733. if (val != nullptr) {
  734. fmt::print(out, "({:.2f})", ucl_object_todouble(val));
  735. }
  736. val = ucl_object_lookup(obj, "options");
  737. if (val != nullptr && val->type == UCL_ARRAY) {
  738. ucl_object_iter_t it = nullptr;
  739. const ucl_object_t *cur;
  740. fmt::print(out, "[");
  741. while ((cur = ucl_object_iterate (val, &it, true)) != nullptr) {
  742. if (first) {
  743. fmt::print(out, "{}", ucl_object_tostring(cur));
  744. first = false;
  745. }
  746. else {
  747. fmt::print(out, ", {}", ucl_object_tostring(cur));
  748. }
  749. }
  750. fmt::print(out, "]");
  751. }
  752. fmt::print(out, "\n");
  753. }
  754. static void
  755. rspamc_metric_output(FILE *out, const ucl_object_t *obj)
  756. {
  757. double score = 0, required_score = 0;
  758. int got_scores = 0;
  759. auto print_protocol_string = [&](const char *ucl_name, const char *output_message) {
  760. auto *elt = ucl_object_lookup(obj, ucl_name);
  761. if (elt) {
  762. fmt::print(out, "{}: {}\n", output_message,
  763. emphasis_argument(ucl_object_tostring(elt)));
  764. }
  765. };
  766. fmt::print(out, "[Metric: default]\n");
  767. const auto *elt = ucl_object_lookup(obj, "required_score");
  768. if (elt) {
  769. required_score = ucl_object_todouble(elt);
  770. got_scores++;
  771. }
  772. elt = ucl_object_lookup(obj, "score");
  773. if (elt) {
  774. score = ucl_object_todouble(elt);
  775. got_scores++;
  776. }
  777. elt = ucl_object_lookup(obj, "action");
  778. if (elt) {
  779. auto act = rspamd_action_from_str_rspamc(ucl_object_tostring(elt));
  780. if (act.has_value()) {
  781. if (!tty) {
  782. print_protocol_string("action", "Action");
  783. }
  784. else {
  785. /* Colorize action type */
  786. std::string colorized_action;
  787. switch (act.value()) {
  788. case METRIC_ACTION_REJECT:
  789. colorized_action = fmt::format(fmt::fg(fmt::color::red), "reject");
  790. break;
  791. case METRIC_ACTION_NOACTION:
  792. colorized_action = fmt::format(fmt::fg(fmt::color::green), "no action");
  793. break;
  794. case METRIC_ACTION_ADD_HEADER:
  795. case METRIC_ACTION_REWRITE_SUBJECT:
  796. colorized_action = fmt::format(fmt::fg(fmt::color::orange), ucl_object_tostring(elt));
  797. break;
  798. case METRIC_ACTION_GREYLIST:
  799. case METRIC_ACTION_SOFT_REJECT:
  800. colorized_action = fmt::format(fmt::fg(fmt::color::gray), ucl_object_tostring(elt));
  801. break;
  802. default:
  803. colorized_action = fmt::format(fmt::emphasis::bold, ucl_object_tostring(elt));
  804. break;
  805. }
  806. fmt::print(out, "Action: {}\n", colorized_action);
  807. }
  808. fmt::print(out, "Spam: {}\n", emphasis_argument(act.value() < METRIC_ACTION_GREYLIST ?
  809. "true" : "false"));
  810. }
  811. else {
  812. print_protocol_string("action", "Action");
  813. }
  814. }
  815. print_protocol_string("subject", "Subject");
  816. if (got_scores == 2) {
  817. fmt::print(out,
  818. "Score: {} / {}\n",
  819. emphasis_argument(score, 2),
  820. emphasis_argument(required_score, 2));
  821. }
  822. elt = ucl_object_lookup(obj, "symbols");
  823. if (elt) {
  824. std::vector<const ucl_object_t *> symbols;
  825. ucl_object_iter_t it = nullptr;
  826. const ucl_object_t *cur;
  827. while ((cur = ucl_object_iterate (elt, &it, true)) != nullptr) {
  828. symbols.push_back(cur);
  829. }
  830. sort_ucl_container_with_default(symbols, "name");
  831. for (const auto *sym_obj : symbols) {
  832. rspamc_symbol_output(out, sym_obj);
  833. }
  834. }
  835. }
  836. static void
  837. rspamc_profile_output(FILE *out, const ucl_object_t *obj)
  838. {
  839. ucl_object_iter_t it = nullptr;
  840. const ucl_object_t *cur;
  841. std::vector<const ucl_object_t *> ar;
  842. while ((cur = ucl_object_iterate (obj, &it, true)) != nullptr) {
  843. ar.push_back(cur);
  844. }
  845. std::stable_sort(std::begin(ar), std::end(ar),
  846. [](const ucl_object_t *u1, const ucl_object_t *u2) -> int {
  847. return ucl_object_compare(u1, u2);
  848. });
  849. for (const auto *cur_elt : ar) {
  850. fmt::print(out, "\t{}: {:3} usec\n",
  851. ucl_object_key(cur_elt), ucl_object_todouble(cur_elt));
  852. }
  853. }
  854. static void
  855. rspamc_symbols_output(FILE *out, ucl_object_t *obj)
  856. {
  857. rspamc_metric_output(out, obj);
  858. auto print_protocol_string = [&](const char *ucl_name, const char *output_message) {
  859. auto *elt = ucl_object_lookup(obj, ucl_name);
  860. if (elt) {
  861. fmt::print(out, "{}: {}\n", output_message, ucl_object_tostring(elt));
  862. }
  863. };
  864. print_protocol_string("message-id", "Message-ID");
  865. print_protocol_string("queue-id", "Queue-ID");
  866. const auto *elt = ucl_object_lookup(obj, "urls");
  867. if (elt) {
  868. char *emitted;
  869. if (!extended_urls || compact) {
  870. emitted = (char *)ucl_object_emit(elt, UCL_EMIT_JSON_COMPACT);
  871. }
  872. else {
  873. emitted = (char *)ucl_object_emit(elt, UCL_EMIT_JSON);
  874. }
  875. fmt::print(out, "Urls: {}\n", emitted);
  876. free(emitted);
  877. }
  878. elt = ucl_object_lookup(obj, "emails");
  879. if (elt) {
  880. char *emitted;
  881. if (!extended_urls || compact) {
  882. emitted = (char *)ucl_object_emit(elt, UCL_EMIT_JSON_COMPACT);
  883. }
  884. else {
  885. emitted = (char *)ucl_object_emit(elt, UCL_EMIT_JSON);
  886. }
  887. fmt::print(out, "Emails: {}\n", emitted);
  888. free(emitted);
  889. }
  890. print_protocol_string("error", "Scan error");
  891. elt = ucl_object_lookup(obj, "messages");
  892. if (elt && elt->type == UCL_OBJECT) {
  893. ucl_object_iter_t mit = nullptr;
  894. const ucl_object_t *cmesg;
  895. while ((cmesg = ucl_object_iterate (elt, &mit, true)) != nullptr) {
  896. fmt::print(out, "Message - {}: {}\n",
  897. ucl_object_key(cmesg), ucl_object_tostring(cmesg));
  898. }
  899. }
  900. elt = ucl_object_lookup(obj, "dkim-signature");
  901. if (elt && elt->type == UCL_STRING) {
  902. fmt::print(out, "DKIM-Signature: {}\n", ucl_object_tostring(elt));
  903. }
  904. else if (elt && elt->type == UCL_ARRAY) {
  905. ucl_object_iter_t it = nullptr;
  906. const ucl_object_t *cur;
  907. while ((cur = ucl_object_iterate (elt, &it, true)) != nullptr) {
  908. fmt::print(out, "DKIM-Signature: {}\n", ucl_object_tostring(cur));
  909. }
  910. }
  911. elt = ucl_object_lookup(obj, "profile");
  912. if (elt) {
  913. fmt::print(out, "Profile data:\n");
  914. rspamc_profile_output(out, elt);
  915. }
  916. }
  917. static void
  918. rspamc_uptime_output(FILE *out, ucl_object_t *obj)
  919. {
  920. int64_t seconds, days, hours, minutes;
  921. const auto *elt = ucl_object_lookup(obj, "version");
  922. if (elt != nullptr) {
  923. fmt::print(out, "Rspamd version: %s\n", ucl_object_tostring(
  924. elt));
  925. }
  926. elt = ucl_object_lookup(obj, "uptime");
  927. if (elt != nullptr) {
  928. fmt::print("Uptime: ");
  929. seconds = ucl_object_toint(elt);
  930. if (seconds >= 2 * 3600) {
  931. days = seconds / 86400;
  932. hours = seconds / 3600 - days * 24;
  933. minutes = seconds / 60 - hours * 60 - days * 1440;
  934. fmt::print("{} day{} {} hour{} {} minute{}\n", days,
  935. days > 1 ? "s" : "", hours, hours > 1 ? "s" : "",
  936. minutes, minutes > 1 ? "s" : "");
  937. }
  938. /* If uptime is less than 1 minute print only seconds */
  939. else if (seconds / 60 == 0) {
  940. fmt::print("{} second%s\n", seconds,
  941. (gint) seconds > 1 ? "s" : "");
  942. }
  943. /* Else print the minutes and seconds. */
  944. else {
  945. hours = seconds / 3600;
  946. minutes = seconds / 60 - hours * 60;
  947. seconds -= hours * 3600 + minutes * 60;
  948. fmt::print("{} hour {} minute{} {} second{}\n", hours,
  949. minutes, minutes > 1 ? "s" : "",
  950. seconds, seconds > 1 ? "s" : "");
  951. }
  952. }
  953. }
  954. static void
  955. rspamc_counters_output(FILE *out, ucl_object_t *obj)
  956. {
  957. if (obj->type != UCL_ARRAY) {
  958. fmt::print(out, "Bad output\n");
  959. return;
  960. }
  961. std::vector<const ucl_object_t *> counters_vec;
  962. auto max_len = sizeof("Symbol") - 1;
  963. {
  964. ucl_object_iter_t iter = nullptr;
  965. const ucl_object_t *cur;
  966. while ((cur = ucl_object_iterate (obj, &iter, true)) != nullptr) {
  967. const auto *sym = ucl_object_lookup(cur, "symbol");
  968. if (sym != nullptr) {
  969. if (sym->len > max_len) {
  970. max_len = sym->len;
  971. }
  972. }
  973. counters_vec.push_back(cur);
  974. }
  975. }
  976. sort_ucl_container_with_default(counters_vec, "name");
  977. char dash_buf[82], sym_buf[82];
  978. const int dashes = 44;
  979. max_len = MIN (sizeof(dash_buf) - dashes - 1, max_len);
  980. memset(dash_buf, '-', dashes + max_len);
  981. dash_buf[dashes + max_len] = '\0';
  982. fmt::print(out, "Symbols cache\n");
  983. fmt::print(out, " {} \n", emphasis_argument(dash_buf));
  984. fmt::print(out,
  985. "| {:<4} | {:<{}} | {:^7} | {:^13} | {:^7} |\n",
  986. "Pri",
  987. "Symbol",
  988. max_len,
  989. "Weight",
  990. "Frequency",
  991. "Hits");
  992. fmt::print(out, " {} \n", emphasis_argument(dash_buf));
  993. fmt::print(out, "| {:<4} | {:<{}} | {:^7} | {:^13} | {:^7} |\n", "",
  994. "", max_len,
  995. "", "hits/min", "");
  996. for (const auto [i, cur] : rspamd::enumerate(counters_vec)) {
  997. fmt::print(out, " {} \n", dash_buf);
  998. const auto *sym = ucl_object_lookup(cur, "symbol");
  999. const auto *weight = ucl_object_lookup(cur, "weight");
  1000. const auto *freq = ucl_object_lookup(cur, "frequency");
  1001. const auto *freq_dev = ucl_object_lookup(cur, "frequency_stddev");
  1002. const auto *nhits = ucl_object_lookup(cur, "hits");
  1003. if (sym && weight && freq && nhits) {
  1004. const char *sym_name;
  1005. if (sym->len > max_len) {
  1006. rspamd_snprintf(sym_buf, sizeof(sym_buf), "%*s...",
  1007. (max_len - 3), ucl_object_tostring(sym));
  1008. sym_name = sym_buf;
  1009. }
  1010. else {
  1011. sym_name = ucl_object_tostring(sym);
  1012. }
  1013. fmt::print(out, "| {:<4} | {:<{}} | {:^7.1f} | {:^6.3f}({:^5.3f}) | {:^7} |\n", i,
  1014. sym_name,
  1015. max_len,
  1016. ucl_object_todouble(weight),
  1017. ucl_object_todouble(freq) * 60.0,
  1018. ucl_object_todouble(freq_dev) * 60.0,
  1019. (std::uintmax_t)ucl_object_toint(nhits));
  1020. }
  1021. }
  1022. fmt::print(out, " {} \n", dash_buf);
  1023. }
  1024. static void
  1025. rspamc_stat_actions(ucl_object_t *obj, std::string &out, std::int64_t scanned)
  1026. {
  1027. const ucl_object_t *actions = ucl_object_lookup(obj, "actions"), *cur;
  1028. ucl_object_iter_t iter = nullptr;
  1029. if (scanned > 0) {
  1030. if (actions && ucl_object_type(actions) == UCL_OBJECT) {
  1031. while ((cur = ucl_object_iterate (actions, &iter, true)) != nullptr) {
  1032. auto cnt = ucl_object_toint(cur);
  1033. fmt::format_to(std::back_inserter(out), "Messages with action {}: {}, {:.2f}%\n",
  1034. ucl_object_key(cur), emphasis_argument(cnt),
  1035. ((double) cnt / (double) scanned) * 100.);
  1036. }
  1037. }
  1038. auto spam = ucl_object_toint(ucl_object_lookup(obj, "spam_count"));
  1039. auto ham = ucl_object_toint(ucl_object_lookup(obj, "ham_count"));
  1040. fmt::format_to(std::back_inserter(out), "Messages treated as spam: {}, {:.2f}%\n",
  1041. emphasis_argument(spam),
  1042. ((double) spam / (double) scanned) * 100.);
  1043. fmt::format_to(std::back_inserter(out), "Messages treated as ham: {}, {:.2f}%\n",
  1044. emphasis_argument(ham),
  1045. ((double) ham / (double) scanned) * 100.);
  1046. }
  1047. }
  1048. static void
  1049. rspamc_stat_statfile(const ucl_object_t *obj, std::string &out)
  1050. {
  1051. auto version = ucl_object_toint(ucl_object_lookup(obj, "revision"));
  1052. auto size = ucl_object_toint(ucl_object_lookup(obj, "size"));
  1053. auto blocks = ucl_object_toint(ucl_object_lookup(obj, "total"));
  1054. auto used_blocks = ucl_object_toint(ucl_object_lookup(obj, "used"));
  1055. auto label = ucl_object_tostring(ucl_object_lookup(obj, "label"));
  1056. auto symbol = ucl_object_tostring(ucl_object_lookup(obj, "symbol"));
  1057. auto type = ucl_object_tostring(ucl_object_lookup(obj, "type"));
  1058. auto nlanguages = ucl_object_toint(ucl_object_lookup(obj, "languages"));
  1059. auto nusers = ucl_object_toint(ucl_object_lookup(obj, "users"));
  1060. if (label) {
  1061. fmt::format_to(std::back_inserter(out), "Statfile: {} <{}> type: {}; ", symbol,
  1062. label, type);
  1063. }
  1064. else {
  1065. fmt::format_to(std::back_inserter(out), "Statfile: {} type: {}; ", symbol, type);
  1066. }
  1067. fmt::format_to(std::back_inserter(out), "length: {}; free blocks: {}; total blocks: {}; "
  1068. "free: {:.2f}%; learned: {}; users: {}; languages: {}\n",
  1069. size,
  1070. blocks - used_blocks, blocks,
  1071. blocks > 0 ? (blocks - used_blocks) * 100.0 / (double) blocks : 0,
  1072. version,
  1073. nusers, nlanguages);
  1074. }
  1075. static void
  1076. rspamc_stat_output(FILE *out, ucl_object_t *obj)
  1077. {
  1078. std::string out_str;
  1079. out_str.reserve(8192);
  1080. auto scanned = ucl_object_toint(ucl_object_lookup(obj, "scanned"));
  1081. fmt::format_to(std::back_inserter(out_str), "Messages scanned: {}\n",
  1082. emphasis_argument(scanned));
  1083. rspamc_stat_actions(obj, out_str, scanned);
  1084. fmt::format_to(std::back_inserter(out_str), "Messages learned: {}\n",
  1085. emphasis_argument(ucl_object_toint(ucl_object_lookup(obj, "learned"))));
  1086. fmt::format_to(std::back_inserter(out_str), "Connections count: {}\n",
  1087. emphasis_argument(ucl_object_toint(ucl_object_lookup(obj, "connections"))));
  1088. fmt::format_to(std::back_inserter(out_str), "Control connections count: {}\n",
  1089. emphasis_argument(ucl_object_toint(ucl_object_lookup(obj, "control_connections"))));
  1090. const auto *avg_time_obj = ucl_object_lookup(obj, "scan_times");
  1091. if (avg_time_obj && ucl_object_type(avg_time_obj) == UCL_ARRAY) {
  1092. ucl_object_iter_t iter = nullptr;
  1093. const ucl_object_t *cur;
  1094. std::vector<float> nums;
  1095. while ((cur = ucl_object_iterate (avg_time_obj, &iter, true)) != nullptr) {
  1096. if (ucl_object_type(cur) == UCL_FLOAT || ucl_object_type(cur) == UCL_INT) {
  1097. nums.push_back(ucl_object_todouble(cur));
  1098. }
  1099. }
  1100. auto cnt = nums.size();
  1101. if (cnt > 0) {
  1102. auto sum = rspamd_sum_floats(nums.data(), &cnt);
  1103. fmt::format_to(std::back_inserter(out_str),
  1104. "Average scan time: {} sec\n",
  1105. emphasis_argument(sum / cnt, 3));
  1106. }
  1107. }
  1108. /* Pools */
  1109. fmt::format_to(std::back_inserter(out_str), "Pools allocated: {}\n",
  1110. ucl_object_toint(ucl_object_lookup(obj, "pools_allocated")));
  1111. fmt::format_to(std::back_inserter(out_str), "Pools freed: {}\n",
  1112. ucl_object_toint(ucl_object_lookup(obj, "pools_freed")));
  1113. fmt::format_to(std::back_inserter(out_str), "Bytes allocated: {}\n",
  1114. ucl_object_toint(ucl_object_lookup(obj, "bytes_allocated")));
  1115. fmt::format_to(std::back_inserter(out_str), "Memory chunks allocated: {}\n",
  1116. ucl_object_toint(ucl_object_lookup(obj, "chunks_allocated")));
  1117. fmt::format_to(std::back_inserter(out_str), "Shared chunks allocated: {}\n",
  1118. ucl_object_toint(ucl_object_lookup(obj, "shared_chunks_allocated")));
  1119. fmt::format_to(std::back_inserter(out_str), "Chunks freed: {}\n",
  1120. ucl_object_toint(ucl_object_lookup(obj, "chunks_freed")));
  1121. fmt::format_to(std::back_inserter(out_str), "Oversized chunks: {}\n",
  1122. ucl_object_toint(ucl_object_lookup(obj, "chunks_oversized")));
  1123. /* Fuzzy */
  1124. const auto *st = ucl_object_lookup(obj, "fuzzy_hashes");
  1125. if (st) {
  1126. ucl_object_iter_t it = nullptr;
  1127. const ucl_object_t *cur;
  1128. std::uint64_t stored = 0;
  1129. while ((cur = ucl_iterate_object (st, &it, true)) != nullptr) {
  1130. auto num = ucl_object_toint(cur);
  1131. fmt::format_to(std::back_inserter(out_str), "Fuzzy hashes in storage \"{}\": {}\n",
  1132. ucl_object_key(cur),
  1133. num);
  1134. stored += num;
  1135. }
  1136. fmt::format_to(std::back_inserter(out_str), "Fuzzy hashes stored: {}\n",
  1137. stored);
  1138. }
  1139. st = ucl_object_lookup(obj, "fuzzy_checked");
  1140. if (st != nullptr && ucl_object_type(st) == UCL_ARRAY) {
  1141. ucl_object_iter_t iter = nullptr;
  1142. const ucl_object_t *cur;
  1143. out_str += "Fuzzy hashes checked: ";
  1144. while ((cur = ucl_object_iterate (st, &iter, true)) != nullptr) {
  1145. fmt::format_to(std::back_inserter(out_str), "{} ", ucl_object_toint(cur));
  1146. }
  1147. out_str.push_back('\n');
  1148. }
  1149. st = ucl_object_lookup(obj, "fuzzy_found");
  1150. if (st != nullptr && ucl_object_type(st) == UCL_ARRAY) {
  1151. ucl_object_iter_t iter = nullptr;
  1152. const ucl_object_t *cur;
  1153. out_str += "Fuzzy hashes found: ";
  1154. while ((cur = ucl_object_iterate (st, &iter, true)) != nullptr) {
  1155. fmt::format_to(std::back_inserter(out_str), "{} ", ucl_object_toint(cur));
  1156. }
  1157. out_str.push_back('\n');
  1158. }
  1159. st = ucl_object_lookup(obj, "statfiles");
  1160. if (st != nullptr && ucl_object_type(st) == UCL_ARRAY) {
  1161. ucl_object_iter_t iter = nullptr;
  1162. const ucl_object_t *cur;
  1163. while ((cur = ucl_object_iterate (st, &iter, true)) != nullptr) {
  1164. rspamc_stat_statfile(cur, out_str);
  1165. }
  1166. }
  1167. fmt::format_to(std::back_inserter(out_str), "Total learns: {}\n",
  1168. ucl_object_toint(ucl_object_lookup(obj, "total_learns")));
  1169. fmt::print(out, "{}", out_str.c_str());
  1170. }
  1171. static void
  1172. rspamc_output_headers(FILE *out, struct rspamd_http_message *msg)
  1173. {
  1174. struct rspamd_http_header *h;
  1175. kh_foreach_value (msg->headers, h, {
  1176. fmt::print(out, "{}: {}\n", std::string_view{h->name.begin, h->name.len},
  1177. std::string_view{h->value.begin, h->value.len});
  1178. });
  1179. fmt::print(out, "\n");
  1180. }
  1181. static void
  1182. rspamc_mime_output(FILE *out, ucl_object_t *result, GString *input,
  1183. gdouble time, GError *err)
  1184. {
  1185. const gchar *action = "no action", *line_end = "\r\n", *p;
  1186. gdouble score = 0.0, required_score = 0.0;
  1187. gboolean is_spam = FALSE;
  1188. auto nl_type = RSPAMD_TASK_NEWLINES_CRLF;
  1189. auto headers_pos = rspamd_string_find_eoh(input, nullptr);
  1190. if (headers_pos == -1) {
  1191. fmt::print(stderr, "cannot find end of headers position");
  1192. return;
  1193. }
  1194. p = input->str + headers_pos;
  1195. if (headers_pos > 1 && *(p - 1) == '\n') {
  1196. if (headers_pos > 2 && *(p - 2) == '\r') {
  1197. line_end = "\r\n";
  1198. nl_type = RSPAMD_TASK_NEWLINES_CRLF;
  1199. }
  1200. else {
  1201. line_end = "\n";
  1202. nl_type = RSPAMD_TASK_NEWLINES_LF;
  1203. }
  1204. }
  1205. else if (headers_pos > 1 && *(p - 1) == '\r') {
  1206. line_end = "\r";
  1207. nl_type = RSPAMD_TASK_NEWLINES_CR;
  1208. }
  1209. std::string added_headers;
  1210. if (result) {
  1211. const auto *res = ucl_object_lookup(result, "action");
  1212. if (res) {
  1213. action = ucl_object_tostring(res);
  1214. }
  1215. res = ucl_object_lookup(result, "score");
  1216. if (res) {
  1217. score = ucl_object_todouble(res);
  1218. }
  1219. res = ucl_object_lookup(result, "required_score");
  1220. if (res) {
  1221. required_score = ucl_object_todouble(res);
  1222. }
  1223. auto act = rspamd_action_from_str_rspamc(action);
  1224. if (act.has_value() && act.value() < METRIC_ACTION_GREYLIST) {
  1225. is_spam = TRUE;
  1226. }
  1227. fmt::format_to(std::back_inserter(added_headers), "X-Spam-Scanner: {}{}",
  1228. "rspamc " RVERSION, line_end);
  1229. fmt::format_to(std::back_inserter(added_headers), "X-Spam-Scan-Time: {:.3}{}",
  1230. time, line_end);
  1231. /*
  1232. * TODO: add milter_headers support here
  1233. */
  1234. if (is_spam) {
  1235. fmt::format_to(std::back_inserter(added_headers), "X-Spam: yes{}", line_end);
  1236. }
  1237. fmt::format_to(std::back_inserter(added_headers),"X-Spam-Action: {}{}",
  1238. action, line_end);
  1239. fmt::format_to(std::back_inserter(added_headers), "X-Spam-Score: {:.2f} / {:.2f}{}",
  1240. score, required_score, line_end);
  1241. /* SA style stars header */
  1242. std::string scorebuf;
  1243. auto adjusted_score = std::min(score, 32.0);
  1244. while(adjusted_score > 0) {
  1245. scorebuf.push_back('*');
  1246. adjusted_score -= 1.0;
  1247. }
  1248. fmt::format_to(std::back_inserter(added_headers), "X-Spam-Level: {}{}",
  1249. scorebuf, line_end);
  1250. /* Short description of all symbols */
  1251. std::string symbuf;
  1252. const ucl_object_t *cur;
  1253. ucl_object_iter_t it = nullptr;
  1254. const auto *syms = ucl_object_lookup(result, "symbols");
  1255. while (syms && (cur = ucl_object_iterate (syms, &it, true)) != nullptr) {
  1256. if (ucl_object_type(cur) == UCL_OBJECT) {
  1257. fmt::format_to(std::back_inserter(symbuf), "{},", ucl_object_key(cur));
  1258. }
  1259. }
  1260. /* Trim the last comma */
  1261. if (symbuf.back() == ',') {
  1262. symbuf.pop_back();
  1263. }
  1264. auto *folded_symbuf = rspamd_header_value_fold("X-Spam-Symbols", strlen("X-Spam-Symbols"),
  1265. symbuf.data(), symbuf.size(),
  1266. 0, nl_type, ",");
  1267. fmt::format_to(std::back_inserter(added_headers), "X-Spam-Symbols: {}{}",
  1268. folded_symbuf->str, line_end);
  1269. g_string_free(folded_symbuf, TRUE);
  1270. res = ucl_object_lookup(result, "dkim-signature");
  1271. if (res && res->type == UCL_STRING) {
  1272. fmt::format_to(std::back_inserter(added_headers), "DKIM-Signature: {}{}",
  1273. ucl_object_tostring(res), line_end);
  1274. }
  1275. else if (res && res->type == UCL_ARRAY) {
  1276. it = nullptr;
  1277. while ((cur = ucl_object_iterate (res, &it, true)) != nullptr) {
  1278. fmt::format_to(std::back_inserter(added_headers), "DKIM-Signature: {}{}",
  1279. ucl_object_tostring(cur), line_end);
  1280. }
  1281. }
  1282. if (json || ucl_reply || compact) {
  1283. unsigned char *json_header;
  1284. /* We also append json data as a specific header */
  1285. if (json) {
  1286. json_header = ucl_object_emit(result,
  1287. compact ? UCL_EMIT_JSON_COMPACT : UCL_EMIT_JSON);
  1288. }
  1289. else {
  1290. json_header = ucl_object_emit(result,
  1291. compact ? UCL_EMIT_JSON_COMPACT : UCL_EMIT_CONFIG);
  1292. }
  1293. auto *json_header_encoded = rspamd_encode_base64_fold(json_header,
  1294. strlen((char *)json_header), 60, nullptr, nl_type);
  1295. free(json_header);
  1296. fmt::format_to(std::back_inserter(added_headers),
  1297. "X-Spam-Result: {}{}",
  1298. json_header_encoded, line_end);
  1299. g_free(json_header_encoded);
  1300. }
  1301. ucl_object_unref(result);
  1302. }
  1303. else {
  1304. fmt::format_to(std::back_inserter(added_headers), "X-Spam-Scanner: {}{}",
  1305. "rspamc " RVERSION, line_end);
  1306. fmt::format_to(std::back_inserter(added_headers), "X-Spam-Scan-Time: {:.3f}{}",
  1307. time, line_end);
  1308. fmt::format_to(std::back_inserter(added_headers), "X-Spam-Error: {}{}",
  1309. err->message, line_end);
  1310. }
  1311. /* Write message */
  1312. /* Original headers */
  1313. fmt::print(out, "{}", std::string_view{input->str, (std::size_t)headers_pos});
  1314. /* Added headers */
  1315. fmt::print(out, "{}", added_headers);
  1316. /* Message body */
  1317. fmt::print(out, "{}", input->str + headers_pos);
  1318. }
  1319. static void
  1320. rspamc_client_execute_cmd(const struct rspamc_command &cmd, ucl_object_t *result,
  1321. GString *input, gdouble time, GError *err)
  1322. {
  1323. gchar **eargv;
  1324. gint eargc, infd, outfd, errfd;
  1325. GError *exec_err = nullptr;
  1326. GPid cld;
  1327. if (!g_shell_parse_argv(execute, &eargc, &eargv, &err)) {
  1328. fmt::print(stderr, "Cannot execute {}: {}", execute, err->message);
  1329. g_error_free(err);
  1330. return;
  1331. }
  1332. if (!g_spawn_async_with_pipes(nullptr, eargv, nullptr,
  1333. static_cast<GSpawnFlags>(G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD), nullptr, nullptr, &cld,
  1334. &infd, &outfd, &errfd, &exec_err)) {
  1335. fmt::print(stderr, "Cannot execute {}: {}", execute, exec_err->message);
  1336. g_error_free(exec_err);
  1337. exit(EXIT_FAILURE);
  1338. }
  1339. else {
  1340. children.push_back(cld);
  1341. auto *out = fdopen(infd, "w");
  1342. if (cmd.cmd == RSPAMC_COMMAND_SYMBOLS && mime_output && input) {
  1343. rspamc_mime_output(out, result, input, time, err);
  1344. }
  1345. else if (result) {
  1346. if (ucl_reply || cmd.command_output_func == nullptr) {
  1347. char *ucl_out;
  1348. if (json) {
  1349. ucl_out = (char *)ucl_object_emit(result,
  1350. compact ? UCL_EMIT_JSON_COMPACT : UCL_EMIT_JSON);
  1351. }
  1352. else {
  1353. ucl_out = (char *)ucl_object_emit(result,
  1354. compact ? UCL_EMIT_JSON_COMPACT : UCL_EMIT_CONFIG);
  1355. }
  1356. fmt::print(out, "{}", ucl_out);
  1357. free(ucl_out);
  1358. }
  1359. else {
  1360. cmd.command_output_func(out, result);
  1361. }
  1362. ucl_object_unref(result);
  1363. }
  1364. else {
  1365. fmt::print(out, "{}\n", err->message);
  1366. }
  1367. fflush(out);
  1368. fclose(out);
  1369. }
  1370. g_strfreev(eargv);
  1371. }
  1372. static void
  1373. rspamc_client_cb(struct rspamd_client_connection *conn,
  1374. struct rspamd_http_message *msg,
  1375. const char *name, ucl_object_t *result, GString *input,
  1376. gpointer ud, gdouble start_time, gdouble send_time,
  1377. const char *body, gsize bodylen,
  1378. GError *err)
  1379. {
  1380. struct rspamc_callback_data *cbdata = (struct rspamc_callback_data *) ud;
  1381. FILE *out = stdout;
  1382. gdouble finish = rspamd_get_ticks(FALSE), diff;
  1383. auto &cmd = cbdata->cmd;
  1384. if (send_time > 0) {
  1385. diff = finish - send_time;
  1386. }
  1387. else {
  1388. diff = finish - start_time;
  1389. }
  1390. if (execute) {
  1391. /* Pass all to the external command */
  1392. rspamc_client_execute_cmd(cmd, result, input, diff, err);
  1393. }
  1394. else {
  1395. if (cmd.cmd == RSPAMC_COMMAND_SYMBOLS && mime_output && input) {
  1396. if (body) {
  1397. GString tmp;
  1398. tmp.str = (char *) body;
  1399. tmp.len = bodylen;
  1400. rspamc_mime_output(out, result, &tmp, diff, err);
  1401. }
  1402. else {
  1403. rspamc_mime_output(out, result, input, diff, err);
  1404. }
  1405. }
  1406. else {
  1407. if (cmd.need_input && !json) {
  1408. if (!compact) {
  1409. fmt::print(out, "Results for file: {} ({:.3} seconds)\n",
  1410. emphasis_argument(cbdata->filename), diff);
  1411. }
  1412. }
  1413. else {
  1414. if (!compact && !json) {
  1415. fmt::print(out, "Results for command: {} ({:.3} seconds)\n",
  1416. emphasis_argument(cmd.name), diff);
  1417. }
  1418. }
  1419. if (result != nullptr) {
  1420. if (headers && msg != nullptr) {
  1421. rspamc_output_headers(out, msg);
  1422. }
  1423. if (ucl_reply || cmd.command_output_func == nullptr) {
  1424. if (cmd.need_input) {
  1425. ucl_object_insert_key(result,
  1426. ucl_object_fromstring(cbdata->filename.c_str()),
  1427. "filename", 0,
  1428. false);
  1429. }
  1430. ucl_object_insert_key(result,
  1431. ucl_object_fromdouble(diff),
  1432. "scan_time", 0,
  1433. false);
  1434. char *ucl_out;
  1435. if (json) {
  1436. ucl_out = (char *)ucl_object_emit(result,
  1437. compact ? UCL_EMIT_JSON_COMPACT : UCL_EMIT_JSON);
  1438. }
  1439. else {
  1440. ucl_out = (char *)ucl_object_emit(result,
  1441. compact ? UCL_EMIT_JSON_COMPACT : UCL_EMIT_CONFIG);
  1442. }
  1443. fmt::print(out, "{}", ucl_out);
  1444. free(ucl_out);
  1445. }
  1446. else {
  1447. cmd.command_output_func(out, result);
  1448. }
  1449. if (body) {
  1450. fmt::print(out, "\nNew body:\n{}\n",
  1451. std::string_view{body, bodylen});
  1452. }
  1453. ucl_object_unref(result);
  1454. }
  1455. else if (err != nullptr) {
  1456. fmt::print(out, "{}\n", err->message);
  1457. if (json && msg != nullptr) {
  1458. gsize rawlen;
  1459. auto *raw_body = rspamd_http_message_get_body(msg, &rawlen);
  1460. if (raw_body) {
  1461. /* We can also output the resulting json */
  1462. fmt::print(out, "{}\n", std::string_view{raw_body, (std::size_t)(rawlen - bodylen)});
  1463. }
  1464. }
  1465. }
  1466. fmt::print(out, "\n");
  1467. }
  1468. fflush(out);
  1469. }
  1470. rspamd_client_destroy(conn);
  1471. delete cbdata;
  1472. if (err) {
  1473. retcode = EXIT_FAILURE;
  1474. }
  1475. }
  1476. static void
  1477. rspamc_process_input(struct ev_loop *ev_base, const struct rspamc_command &cmd,
  1478. FILE *in, const std::string &name, GQueue *attrs)
  1479. {
  1480. struct rspamd_client_connection *conn;
  1481. const char *p;
  1482. guint16 port;
  1483. GError *err = nullptr;
  1484. std::string hostbuf;
  1485. if (connect_str[0] == '[') {
  1486. p = strrchr(connect_str, ']');
  1487. if (p != nullptr) {
  1488. hostbuf.assign(connect_str + 1, (std::size_t)(p - connect_str - 1));
  1489. p++;
  1490. }
  1491. else {
  1492. p = connect_str;
  1493. }
  1494. }
  1495. else {
  1496. p = connect_str;
  1497. }
  1498. p = strrchr(p, ':');
  1499. if (hostbuf.empty()) {
  1500. if (p != nullptr) {
  1501. hostbuf.assign(connect_str, (std::size_t)(p - connect_str));
  1502. }
  1503. else {
  1504. hostbuf.assign(connect_str);
  1505. }
  1506. }
  1507. if (p != nullptr) {
  1508. port = strtoul(p + 1, nullptr, 10);
  1509. }
  1510. else {
  1511. /*
  1512. * If we connect to localhost, 127.0.0.1 or ::1, then try controller
  1513. * port first
  1514. */
  1515. if (hostbuf == "localhost" ||
  1516. hostbuf == "127.0.0.1"||
  1517. hostbuf == "::1" ||
  1518. hostbuf == "[::1]") {
  1519. port = DEFAULT_CONTROL_PORT;
  1520. }
  1521. else {
  1522. port = cmd.is_controller ? DEFAULT_CONTROL_PORT : DEFAULT_PORT;
  1523. }
  1524. }
  1525. conn = rspamd_client_init(http_ctx, ev_base, hostbuf.c_str(), port, timeout, key);
  1526. if (conn != nullptr) {
  1527. auto *cbdata = new rspamc_callback_data;
  1528. cbdata->cmd = cmd;
  1529. cbdata->filename = name;
  1530. if (cmd.need_input) {
  1531. rspamd_client_command(conn, cmd.path, attrs, in, rspamc_client_cb,
  1532. cbdata, compressed, dictionary, cbdata->filename.c_str(), &err);
  1533. }
  1534. else {
  1535. rspamd_client_command(conn,
  1536. cmd.path,
  1537. attrs,
  1538. nullptr,
  1539. rspamc_client_cb,
  1540. cbdata,
  1541. compressed,
  1542. dictionary,
  1543. cbdata->filename.c_str(),
  1544. &err);
  1545. }
  1546. }
  1547. else {
  1548. fmt::print(stderr, "cannot connect to {}: {}\n", connect_str,
  1549. strerror(errno));
  1550. exit(EXIT_FAILURE);
  1551. }
  1552. }
  1553. static gsize
  1554. rspamd_dirent_size(DIR *dirp)
  1555. {
  1556. goffset name_max;
  1557. gsize name_end;
  1558. #if defined(HAVE_FPATHCONF) && defined(HAVE_DIRFD) \
  1559. && defined(_PC_NAME_MAX)
  1560. name_max = fpathconf(dirfd(dirp), _PC_NAME_MAX);
  1561. # if defined(NAME_MAX)
  1562. if (name_max == -1) {
  1563. name_max = (NAME_MAX > 255) ? NAME_MAX : 255;
  1564. }
  1565. # else
  1566. if (name_max == -1) {
  1567. return (size_t)(-1);
  1568. }
  1569. # endif
  1570. #else
  1571. # if defined(NAME_MAX)
  1572. name_max = (NAME_MAX > 255) ? NAME_MAX : 255;
  1573. # else
  1574. # error "buffer size for readdir_r cannot be determined"
  1575. # endif
  1576. #endif
  1577. name_end = G_STRUCT_OFFSET (struct dirent, d_name) + name_max + 1;
  1578. return (name_end > sizeof(struct dirent) ? name_end : sizeof(struct dirent));
  1579. }
  1580. static void
  1581. rspamc_process_dir(struct ev_loop *ev_base, const struct rspamc_command &cmd,
  1582. const std::string &name, GQueue *attrs)
  1583. {
  1584. static auto cur_req = 0;
  1585. auto *d = opendir(name.c_str());
  1586. if (d != nullptr) {
  1587. struct dirent *pentry;
  1588. std::string fpath;
  1589. fpath.reserve(PATH_MAX);
  1590. while ((pentry = readdir(d)) != nullptr) {
  1591. if (pentry->d_name[0] == '.') {
  1592. continue;
  1593. }
  1594. fpath.clear();
  1595. fmt::format_to(std::back_inserter(fpath), "{}{}{}",
  1596. name, G_DIR_SEPARATOR,
  1597. pentry->d_name);
  1598. /* Check exclude */
  1599. auto **ex = exclude_compiled;
  1600. auto skip = false;
  1601. while (ex != nullptr && *ex != nullptr) {
  1602. #if GLIB_MAJOR_VERSION >= 2 && GLIB_MINOR_VERSION >= 70
  1603. if (g_pattern_spec_match(*ex, fpath.size(), fpath.c_str(), nullptr)) {
  1604. #else
  1605. if (g_pattern_match(*ex, fpath.size(), fpath.c_str(), nullptr)) {
  1606. #endif
  1607. skip = true;
  1608. break;
  1609. }
  1610. ex++;
  1611. }
  1612. if (skip) {
  1613. continue;
  1614. }
  1615. auto is_reg = false;
  1616. auto is_dir = false;
  1617. struct stat st;
  1618. #if (defined(_DIRENT_HAVE_D_TYPE) || defined(__APPLE__)) && defined(DT_UNKNOWN)
  1619. if (pentry->d_type == DT_UNKNOWN) {
  1620. /* Fallback to lstat */
  1621. if (lstat(fpath.c_str(), &st) == -1) {
  1622. fmt::print(stderr, "cannot stat file {}: {}\n",
  1623. fpath, strerror(errno));
  1624. continue;
  1625. }
  1626. is_dir = S_ISDIR(st.st_mode);
  1627. is_reg = S_ISREG(st.st_mode);
  1628. }
  1629. else {
  1630. if (pentry->d_type == DT_REG) {
  1631. is_reg = true;
  1632. }
  1633. else if (pentry->d_type == DT_DIR) {
  1634. is_dir = true;
  1635. }
  1636. }
  1637. #else
  1638. if (lstat(fpath.c_str(), &st) == -1) {
  1639. fmt::print(stderr, "cannot stat file {}: {}\n",
  1640. fpath, strerror (errno));
  1641. continue;
  1642. }
  1643. is_dir = S_ISDIR(st.st_mode);
  1644. is_reg = S_ISREG(st.st_mode);
  1645. #endif
  1646. if (is_dir) {
  1647. rspamc_process_dir(ev_base, cmd, fpath, attrs);
  1648. continue;
  1649. }
  1650. else if (is_reg) {
  1651. auto *in = fopen(fpath.c_str(), "r");
  1652. if (in == nullptr) {
  1653. fmt::print(stderr, "cannot open file {}: {}\n",
  1654. fpath, strerror(errno));
  1655. continue;
  1656. }
  1657. rspamc_process_input(ev_base, cmd, in, fpath, attrs);
  1658. cur_req++;
  1659. fclose(in);
  1660. if (cur_req >= max_requests) {
  1661. cur_req = 0;
  1662. /* Wait for completion */
  1663. ev_loop(ev_base, 0);
  1664. }
  1665. }
  1666. }
  1667. }
  1668. else {
  1669. fmt::print(stderr, "cannot open directory {}: {}\n", name, strerror(errno));
  1670. exit(EXIT_FAILURE);
  1671. }
  1672. closedir(d);
  1673. ev_loop(ev_base, 0);
  1674. }
  1675. static void
  1676. rspamc_kwattr_free(gpointer p)
  1677. {
  1678. struct rspamd_http_client_header *h = (struct rspamd_http_client_header *) p;
  1679. g_free(h->value);
  1680. g_free(h->name);
  1681. g_free(h);
  1682. }
  1683. int
  1684. main(int argc, char **argv, char **env)
  1685. {
  1686. auto *kwattrs = g_queue_new();
  1687. read_cmd_line(&argc, &argv);
  1688. tty = isatty(STDOUT_FILENO);
  1689. if (print_commands) {
  1690. print_commands_list();
  1691. exit(EXIT_SUCCESS);
  1692. }
  1693. /* Deal with exclude patterns */
  1694. auto **exclude_pattern = exclude_patterns;
  1695. auto npatterns = 0;
  1696. while (exclude_pattern && *exclude_pattern) {
  1697. exclude_pattern++;
  1698. npatterns++;
  1699. }
  1700. if (npatterns > 0) {
  1701. exclude_compiled = g_new0(GPatternSpec *, (npatterns + 1));
  1702. for (auto i = 0; i < npatterns; i++) {
  1703. exclude_compiled[i] = g_pattern_spec_new(exclude_patterns[i]);
  1704. if (exclude_compiled[i] == nullptr) {
  1705. fmt::print(stderr, "Invalid glob pattern: {}\n",
  1706. exclude_patterns[i]);
  1707. exit(EXIT_FAILURE);
  1708. }
  1709. }
  1710. }
  1711. auto *libs = rspamd_init_libs();
  1712. auto *event_loop = ev_loop_new(EVBACKEND_ALL);
  1713. struct rspamd_http_context_cfg http_config;
  1714. memset(&http_config, 0, sizeof(http_config));
  1715. http_config.kp_cache_size_client = 32;
  1716. http_config.kp_cache_size_server = 0;
  1717. http_config.user_agent = user_agent;
  1718. http_ctx = rspamd_http_context_create_config(&http_config,
  1719. event_loop, nullptr);
  1720. /* Ignore sigpipe */
  1721. struct sigaction sigpipe_act;
  1722. sigemptyset (&sigpipe_act.sa_mask);
  1723. sigaddset (&sigpipe_act.sa_mask, SIGPIPE);
  1724. sigpipe_act.sa_handler = SIG_IGN;
  1725. sigpipe_act.sa_flags = 0;
  1726. sigaction(SIGPIPE, &sigpipe_act, nullptr);
  1727. /* Now read other args from argc and argv */
  1728. FILE *in = nullptr;
  1729. std::optional<rspamc_command> maybe_cmd;
  1730. auto start_argc = 0;
  1731. if (argc == 1) {
  1732. start_argc = argc;
  1733. in = stdin;
  1734. maybe_cmd = check_rspamc_command("symbols");
  1735. }
  1736. else if (argc == 2) {
  1737. /* One argument is whether command or filename */
  1738. maybe_cmd = check_rspamc_command(argv[1]);
  1739. if (maybe_cmd.has_value()) {
  1740. start_argc = argc;
  1741. in = stdin;
  1742. }
  1743. else {
  1744. maybe_cmd = check_rspamc_command("symbols"); /* Symbols command */
  1745. start_argc = 1;
  1746. }
  1747. }
  1748. else {
  1749. maybe_cmd = check_rspamc_command(argv[1]);
  1750. if (maybe_cmd.has_value()) {
  1751. auto &cmd = maybe_cmd.value();
  1752. /* In case of command read arguments starting from 2 */
  1753. if (cmd.cmd == RSPAMC_COMMAND_ADD_SYMBOL || cmd.cmd == RSPAMC_COMMAND_ADD_ACTION) {
  1754. if (argc < 4 || argc > 5) {
  1755. fmt::print(stderr, "invalid arguments\n");
  1756. exit(EXIT_FAILURE);
  1757. }
  1758. if (argc == 5) {
  1759. add_client_header(kwattrs, "metric", argv[2]);
  1760. add_client_header(kwattrs, "name", argv[3]);
  1761. add_client_header(kwattrs, "value", argv[4]);
  1762. }
  1763. else {
  1764. add_client_header(kwattrs, "name", argv[2]);
  1765. add_client_header(kwattrs, "value", argv[3]);
  1766. }
  1767. start_argc = argc;
  1768. }
  1769. else {
  1770. start_argc = 2;
  1771. }
  1772. }
  1773. else {
  1774. maybe_cmd = check_rspamc_command("symbols");
  1775. start_argc = 1;
  1776. }
  1777. }
  1778. if (!maybe_cmd.has_value()) {
  1779. fmt::print(stderr, "invalid command\n");
  1780. exit(EXIT_FAILURE);
  1781. }
  1782. add_options(kwattrs);
  1783. auto cmd = maybe_cmd.value();
  1784. if (start_argc == argc) {
  1785. /* Do command without input or with stdin */
  1786. if (empty_input) {
  1787. rspamc_process_input(event_loop, cmd, nullptr, "empty", kwattrs);
  1788. }
  1789. else {
  1790. rspamc_process_input(event_loop, cmd, in, "stdin", kwattrs);
  1791. }
  1792. }
  1793. else {
  1794. auto cur_req = 0;
  1795. for (auto i = start_argc; i < argc; i++) {
  1796. if (cmd.cmd == RSPAMC_COMMAND_FUZZY_DELHASH) {
  1797. add_client_header(kwattrs, "Hash", argv[i]);
  1798. }
  1799. else {
  1800. struct stat st;
  1801. if (stat(argv[i], &st) == -1) {
  1802. fmt::print(stderr, "cannot stat file {}\n", argv[i]);
  1803. exit(EXIT_FAILURE);
  1804. }
  1805. if (S_ISDIR (st.st_mode)) {
  1806. /* Directories are processed with a separate limit */
  1807. rspamc_process_dir(event_loop, cmd, argv[i], kwattrs);
  1808. cur_req = 0;
  1809. }
  1810. else {
  1811. in = fopen(argv[i], "r");
  1812. if (in == nullptr) {
  1813. fmt::print(stderr, "cannot open file {}\n", argv[i]);
  1814. exit(EXIT_FAILURE);
  1815. }
  1816. rspamc_process_input(event_loop, cmd, in, argv[i], kwattrs);
  1817. cur_req++;
  1818. fclose(in);
  1819. }
  1820. if (cur_req >= max_requests) {
  1821. cur_req = 0;
  1822. /* Wait for completion */
  1823. ev_loop(event_loop, 0);
  1824. }
  1825. }
  1826. }
  1827. if (cmd.cmd == RSPAMC_COMMAND_FUZZY_DELHASH) {
  1828. rspamc_process_input(event_loop, cmd, nullptr, "hashes", kwattrs);
  1829. }
  1830. }
  1831. ev_loop(event_loop, 0);
  1832. g_queue_free_full(kwattrs, rspamc_kwattr_free);
  1833. /* Wait for children processes */
  1834. auto ret = 0;
  1835. for (auto cld : children) {
  1836. auto res = 0;
  1837. if (waitpid(cld, &res, 0) == -1) {
  1838. fmt::print(stderr, "Cannot wait for {}: {}", cld,
  1839. strerror(errno));
  1840. ret = errno;
  1841. }
  1842. if (ret == 0) {
  1843. /* Check return code */
  1844. if (WIFSIGNALED (res)) {
  1845. ret = WTERMSIG (res);
  1846. }
  1847. else if (WIFEXITED (res)) {
  1848. ret = WEXITSTATUS (res);
  1849. }
  1850. }
  1851. }
  1852. for (auto i = 0; i < npatterns; i++) {
  1853. g_pattern_spec_free(exclude_compiled[i]);
  1854. }
  1855. g_free(exclude_compiled);
  1856. rspamd_deinit_libs(libs);
  1857. /* Mix retcode (return from Rspamd side) and ret (return from subprocess) */
  1858. return ret | retcode;
  1859. }