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

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