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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340
  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 *pubkey = 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, &pubkey,
  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. rspamc_symbol_human_output(FILE *out, const ucl_object_t *obj)
  731. {
  732. auto first = true;
  733. auto score = 0.0;
  734. const char *desc = nullptr;
  735. const auto *key = ucl_object_key(obj);
  736. const auto *val = ucl_object_lookup(obj, "score");
  737. if (val != nullptr) {
  738. score = ucl_object_todouble(val);
  739. }
  740. val = ucl_object_lookup(obj, "description");
  741. if (val != nullptr) {
  742. desc = ucl_object_tostring(val);
  743. }
  744. auto line = fmt::format("{:>4.1f} {:<22} ", score, key);
  745. if (desc != nullptr) {
  746. line += desc;
  747. }
  748. val = ucl_object_lookup(obj, "options");
  749. if (val != nullptr && ucl_object_type(val) == UCL_ARRAY) {
  750. ucl_object_iter_t it = nullptr;
  751. const ucl_object_t *cur;
  752. line += fmt::format("{}[", desc == nullptr ? "" : " ");
  753. while ((cur = ucl_object_iterate (val, &it, true)) != nullptr) {
  754. if (first) {
  755. line += fmt::format("{}", ucl_object_tostring(cur));
  756. first = false;
  757. }
  758. else {
  759. line += fmt::format(",{}", ucl_object_tostring(cur));
  760. }
  761. }
  762. line += ']';
  763. }
  764. else if (desc == nullptr) {
  765. line += '\n';
  766. }
  767. auto print_indented_line = [&](size_t maxlen, size_t indent) {
  768. if (maxlen < 1 || maxlen < indent) {
  769. return;
  770. }
  771. for (size_t pos = 0; pos < line.size(); ) {
  772. auto s = line.substr(pos, pos ? (maxlen-indent) : maxlen);
  773. if (indent && pos) {
  774. fmt::print(out, "{:>{}}", " ", indent);
  775. }
  776. fmt::print(out, "{}\n", s);
  777. pos += s.size();
  778. }
  779. };
  780. print_indented_line(78, 28);
  781. }
  782. static void
  783. rspamc_symbol_output(FILE *out, const ucl_object_t *obj)
  784. {
  785. auto first = true;
  786. fmt::print(out, "Symbol: {} ", ucl_object_key(obj));
  787. const auto *val = ucl_object_lookup(obj, "score");
  788. if (val != nullptr) {
  789. fmt::print(out, "({:.2f})", ucl_object_todouble(val));
  790. }
  791. val = ucl_object_lookup(obj, "options");
  792. if (val != nullptr && ucl_object_type(val) == UCL_ARRAY) {
  793. ucl_object_iter_t it = nullptr;
  794. const ucl_object_t *cur;
  795. fmt::print(out, "[");
  796. while ((cur = ucl_object_iterate (val, &it, true)) != nullptr) {
  797. if (first) {
  798. fmt::print(out, "{}", ucl_object_tostring(cur));
  799. first = false;
  800. }
  801. else {
  802. fmt::print(out, ", {}", ucl_object_tostring(cur));
  803. }
  804. }
  805. fmt::print(out, "]");
  806. }
  807. fmt::print(out, "\n");
  808. }
  809. static void
  810. rspamc_metric_output(FILE *out, const ucl_object_t *obj)
  811. {
  812. int got_scores = 0;
  813. bool is_spam = false, is_skipped = false;
  814. double score = 0, required_score = 0, greylist_score =0, addheader_score = 0;
  815. auto print_protocol_string = [&](const char *ucl_name, const char *output_message) {
  816. auto *elt = ucl_object_lookup(obj, ucl_name);
  817. if (elt) {
  818. if (humanreport) {
  819. fmt::print(out, ",{}={}", output_message, emphasis_argument(ucl_object_tostring(elt)));
  820. }
  821. else {
  822. fmt::print(out, "{}: {}\n", output_message, emphasis_argument(ucl_object_tostring(elt)));
  823. }
  824. }
  825. };
  826. if (!humanreport) {
  827. fmt::print(out, "[Metric: default]\n");
  828. }
  829. const auto *elt = ucl_object_lookup(obj, "required_score");
  830. if (elt) {
  831. required_score = ucl_object_todouble(elt);
  832. got_scores++;
  833. }
  834. elt = ucl_object_lookup(obj, "score");
  835. if (elt) {
  836. score = ucl_object_todouble(elt);
  837. got_scores++;
  838. }
  839. /* XXX: greylist_score is not yet in checkv2 */
  840. elt = ucl_object_lookup(obj, "greylist_score");
  841. if (elt) {
  842. greylist_score = ucl_object_todouble(elt);
  843. }
  844. /* XXX: addheader_score is not yet in checkv2 */
  845. elt = ucl_object_lookup(obj, "addheader_score");
  846. if (elt) {
  847. addheader_score = ucl_object_todouble(elt);
  848. }
  849. if (humanreport) {
  850. fmt::print(out,
  851. "{}/{}/{}/{}",
  852. emphasis_argument(score, 2),
  853. emphasis_argument(greylist_score, 2),
  854. emphasis_argument(addheader_score, 2),
  855. emphasis_argument(required_score, 2));
  856. }
  857. elt = ucl_object_lookup(obj, "action");
  858. if (elt) {
  859. auto act = rspamd_action_from_str_rspamc(ucl_object_tostring(elt));
  860. if (act.has_value()) {
  861. if (!tty) {
  862. if (humanreport) {
  863. fmt::print(out, ",action={}:{}", act.value(), ucl_object_tostring(elt));
  864. }
  865. else {
  866. print_protocol_string("action", "Action");
  867. }
  868. }
  869. else {
  870. /* Colorize action type */
  871. std::string colorized_action;
  872. switch (act.value()) {
  873. case METRIC_ACTION_REJECT:
  874. colorized_action = fmt::format(fmt::fg(fmt::color::red), "reject");
  875. break;
  876. case METRIC_ACTION_NOACTION:
  877. colorized_action = fmt::format(fmt::fg(fmt::color::green), "no action");
  878. break;
  879. case METRIC_ACTION_ADD_HEADER:
  880. case METRIC_ACTION_REWRITE_SUBJECT:
  881. colorized_action = fmt::format(fmt::fg(fmt::color::orange), ucl_object_tostring(elt));
  882. break;
  883. case METRIC_ACTION_GREYLIST:
  884. case METRIC_ACTION_SOFT_REJECT:
  885. colorized_action = fmt::format(fmt::fg(fmt::color::gray), ucl_object_tostring(elt));
  886. break;
  887. default:
  888. colorized_action = fmt::format(fmt::emphasis::bold, ucl_object_tostring(elt));
  889. break;
  890. }
  891. if (humanreport) {
  892. fmt::print(out, ",action={}:{}", act.value(), colorized_action);
  893. }
  894. else {
  895. fmt::print(out, "Action: {}\n", colorized_action);
  896. }
  897. }
  898. is_spam = act.value() < METRIC_ACTION_GREYLIST ? true : false;
  899. if (!humanreport) {
  900. fmt::print(out, "Spam: {}\n", is_spam ? "true" : "false");
  901. }
  902. }
  903. else {
  904. if (humanreport) {
  905. fmt::print(out, ",action={}:{}", METRIC_ACTION_NOACTION, ucl_object_tostring(elt));
  906. }
  907. else {
  908. print_protocol_string("action", "Action");
  909. }
  910. }
  911. }
  912. if (!humanreport) {
  913. print_protocol_string("subject", "Subject");
  914. }
  915. if (humanreport) {
  916. /* XXX: why checkv2 does not provide "is_spam"? */
  917. elt = ucl_object_lookup(obj, "is_spam");
  918. if (elt) {
  919. is_spam = ucl_object_toboolean(elt);
  920. }
  921. elt = ucl_object_lookup(obj, "is_skipped");
  922. if (elt) {
  923. is_skipped = ucl_object_toboolean(elt);
  924. }
  925. fmt::print(out, ",spam={},skipped={}\n", is_spam ? 1 : 0, is_skipped ? 1 : 0);
  926. }
  927. else if (got_scores == 2) {
  928. fmt::print(out,
  929. "Score: {} / {}\n",
  930. emphasis_argument(score, 2),
  931. emphasis_argument(required_score, 2));
  932. }
  933. if (humanreport) {
  934. fmt::print(out, "Content analysis details: ({} points, {} required)\n\n",
  935. emphasis_argument(score, 2),
  936. emphasis_argument(required_score, 2));
  937. fmt::print(out, " pts rule name description\n");
  938. fmt::print(out, "---- ---------------------- --------------------------------------------------\n");
  939. }
  940. elt = ucl_object_lookup(obj, "symbols");
  941. if (elt) {
  942. std::vector<const ucl_object_t *> symbols;
  943. ucl_object_iter_t it = nullptr;
  944. const ucl_object_t *cur;
  945. while ((cur = ucl_object_iterate (elt, &it, true)) != nullptr) {
  946. symbols.push_back(cur);
  947. }
  948. sort_ucl_container_with_default(symbols, "name");
  949. for (const auto *sym_obj : symbols) {
  950. humanreport ? rspamc_symbol_human_output(out, sym_obj) : rspamc_symbol_output(out, sym_obj);
  951. }
  952. }
  953. if (humanreport) {
  954. fmt::print(out, "\n");
  955. }
  956. }
  957. static void
  958. rspamc_profile_output(FILE *out, const ucl_object_t *obj)
  959. {
  960. ucl_object_iter_t it = nullptr;
  961. const ucl_object_t *cur;
  962. std::vector<const ucl_object_t *> ar;
  963. while ((cur = ucl_object_iterate (obj, &it, true)) != nullptr) {
  964. ar.push_back(cur);
  965. }
  966. std::stable_sort(std::begin(ar), std::end(ar),
  967. [](const ucl_object_t *u1, const ucl_object_t *u2) -> int {
  968. return ucl_object_compare(u1, u2);
  969. });
  970. for (const auto *cur_elt : ar) {
  971. fmt::print(out, "\t{}: {:3} usec\n",
  972. ucl_object_key(cur_elt), ucl_object_todouble(cur_elt));
  973. }
  974. }
  975. static void
  976. rspamc_symbols_output(FILE *out, ucl_object_t *obj)
  977. {
  978. rspamc_metric_output(out, obj);
  979. auto print_protocol_string = [&](const char *ucl_name, const char *output_message) {
  980. auto *elt = ucl_object_lookup(obj, ucl_name);
  981. if (elt) {
  982. fmt::print(out, "{}: {}\n", output_message, ucl_object_tostring(elt));
  983. }
  984. };
  985. if (!humanreport) {
  986. print_protocol_string("message-id", "Message-ID");
  987. print_protocol_string("queue-id", "Queue-ID");
  988. }
  989. const auto *elt = ucl_object_lookup(obj, "urls");
  990. if (elt) {
  991. char *emitted;
  992. if (!extended_urls || compact) {
  993. emitted = (char *)ucl_object_emit(elt, UCL_EMIT_JSON_COMPACT);
  994. }
  995. else {
  996. emitted = (char *)ucl_object_emit(elt, UCL_EMIT_JSON);
  997. }
  998. if (humanreport) {
  999. if (emitted && strcmp(emitted, "[]") != 0) {
  1000. auto folded_line = rspamd_header_value_fold("Domains found: ", sizeof("Domains found: ") - 1,
  1001. emitted, strlen(emitted), 78,
  1002. RSPAMD_TASK_NEWLINES_LF, nullptr);
  1003. fmt::print("Domains found: {}\n", folded_line->str);
  1004. g_string_free(folded_line, true);
  1005. }
  1006. }
  1007. else {
  1008. fmt::print(out, "Urls: {}\n", emitted);
  1009. }
  1010. free(emitted);
  1011. }
  1012. elt = ucl_object_lookup(obj, "emails");
  1013. if (elt) {
  1014. char *emitted;
  1015. if (!extended_urls || compact) {
  1016. emitted = (char *)ucl_object_emit(elt, UCL_EMIT_JSON_COMPACT);
  1017. }
  1018. else {
  1019. emitted = (char *)ucl_object_emit(elt, UCL_EMIT_JSON);
  1020. }
  1021. if (humanreport) {
  1022. if (emitted && strcmp(emitted, "[]") != 0) {
  1023. auto folded_line = rspamd_header_value_fold("Emails found: ", sizeof("Emails found: ") - 1,
  1024. emitted, strlen(emitted), 78,
  1025. RSPAMD_TASK_NEWLINES_LF, nullptr);
  1026. fmt::print("Emails found: {}\n", folded_line->str);
  1027. g_string_free(folded_line, true);
  1028. }
  1029. }
  1030. else {
  1031. fmt::print(out, "Emails: {}\n", emitted);
  1032. }
  1033. free(emitted);
  1034. }
  1035. print_protocol_string("error", "Scan error");
  1036. if (humanreport) {
  1037. return;
  1038. }
  1039. elt = ucl_object_lookup(obj, "messages");
  1040. if (elt && elt->type == UCL_OBJECT) {
  1041. ucl_object_iter_t mit = nullptr;
  1042. const ucl_object_t *cmesg;
  1043. while ((cmesg = ucl_object_iterate (elt, &mit, true)) != nullptr) {
  1044. if (ucl_object_type(cmesg) == UCL_STRING) {
  1045. fmt::print(out, "Message - {}: {}\n",
  1046. ucl_object_key(cmesg), ucl_object_tostring(cmesg));
  1047. } else {
  1048. char *rendered_message;
  1049. rendered_message = (char *)ucl_object_emit(cmesg, UCL_EMIT_JSON_COMPACT);
  1050. fmt::print(out, "Message - {}: {:.60}\n",
  1051. ucl_object_key(cmesg), rendered_message);
  1052. free(rendered_message);
  1053. }
  1054. }
  1055. }
  1056. elt = ucl_object_lookup(obj, "dkim-signature");
  1057. if (elt && elt->type == UCL_STRING) {
  1058. fmt::print(out, "DKIM-Signature: {}\n", ucl_object_tostring(elt));
  1059. }
  1060. else if (elt && elt->type == UCL_ARRAY) {
  1061. ucl_object_iter_t it = nullptr;
  1062. const ucl_object_t *cur;
  1063. while ((cur = ucl_object_iterate (elt, &it, true)) != nullptr) {
  1064. fmt::print(out, "DKIM-Signature: {}\n", ucl_object_tostring(cur));
  1065. }
  1066. }
  1067. elt = ucl_object_lookup(obj, "profile");
  1068. if (elt) {
  1069. fmt::print(out, "Profile data:\n");
  1070. rspamc_profile_output(out, elt);
  1071. }
  1072. }
  1073. static void
  1074. rspamc_uptime_output(FILE *out, ucl_object_t *obj)
  1075. {
  1076. int64_t seconds, days, hours, minutes;
  1077. const auto *elt = ucl_object_lookup(obj, "version");
  1078. if (elt != nullptr) {
  1079. fmt::print(out, "Rspamd version: %s\n", ucl_object_tostring(
  1080. elt));
  1081. }
  1082. elt = ucl_object_lookup(obj, "uptime");
  1083. if (elt != nullptr) {
  1084. fmt::print("Uptime: ");
  1085. seconds = ucl_object_toint(elt);
  1086. if (seconds >= 2 * 3600) {
  1087. days = seconds / 86400;
  1088. hours = seconds / 3600 - days * 24;
  1089. minutes = seconds / 60 - hours * 60 - days * 1440;
  1090. fmt::print("{} day{} {} hour{} {} minute{}\n", days,
  1091. days > 1 ? "s" : "", hours, hours > 1 ? "s" : "",
  1092. minutes, minutes > 1 ? "s" : "");
  1093. }
  1094. /* If uptime is less than 1 minute print only seconds */
  1095. else if (seconds / 60 == 0) {
  1096. fmt::print("{} second%s\n", seconds,
  1097. (gint) seconds > 1 ? "s" : "");
  1098. }
  1099. /* Else print the minutes and seconds. */
  1100. else {
  1101. hours = seconds / 3600;
  1102. minutes = seconds / 60 - hours * 60;
  1103. seconds -= hours * 3600 + minutes * 60;
  1104. fmt::print("{} hour {} minute{} {} second{}\n", hours,
  1105. minutes, minutes > 1 ? "s" : "",
  1106. seconds, seconds > 1 ? "s" : "");
  1107. }
  1108. }
  1109. }
  1110. static void
  1111. rspamc_counters_output(FILE *out, ucl_object_t *obj)
  1112. {
  1113. if (obj->type != UCL_ARRAY) {
  1114. fmt::print(out, "Bad output\n");
  1115. return;
  1116. }
  1117. std::vector<const ucl_object_t *> counters_vec;
  1118. auto max_len = sizeof("Symbol") - 1;
  1119. {
  1120. ucl_object_iter_t iter = nullptr;
  1121. const ucl_object_t *cur;
  1122. while ((cur = ucl_object_iterate (obj, &iter, true)) != nullptr) {
  1123. const auto *sym = ucl_object_lookup(cur, "symbol");
  1124. if (sym != nullptr) {
  1125. if (sym->len > max_len) {
  1126. max_len = sym->len;
  1127. }
  1128. }
  1129. counters_vec.push_back(cur);
  1130. }
  1131. }
  1132. sort_ucl_container_with_default(counters_vec, "name");
  1133. char dash_buf[82], sym_buf[82];
  1134. const int dashes = 44;
  1135. max_len = MIN (sizeof(dash_buf) - dashes - 1, max_len);
  1136. memset(dash_buf, '-', dashes + max_len);
  1137. dash_buf[dashes + max_len] = '\0';
  1138. fmt::print(out, "Symbols cache\n");
  1139. fmt::print(out, " {} \n", emphasis_argument(dash_buf));
  1140. fmt::print(out,
  1141. "| {:<4} | {:<{}} | {:^7} | {:^13} | {:^7} |\n",
  1142. "Pri",
  1143. "Symbol",
  1144. max_len,
  1145. "Weight",
  1146. "Frequency",
  1147. "Hits");
  1148. fmt::print(out, " {} \n", emphasis_argument(dash_buf));
  1149. fmt::print(out, "| {:<4} | {:<{}} | {:^7} | {:^13} | {:^7} |\n", "",
  1150. "", max_len,
  1151. "", "hits/min", "");
  1152. for (const auto [i, cur] : rspamd::enumerate(counters_vec)) {
  1153. fmt::print(out, " {} \n", dash_buf);
  1154. const auto *sym = ucl_object_lookup(cur, "symbol");
  1155. const auto *weight = ucl_object_lookup(cur, "weight");
  1156. const auto *freq = ucl_object_lookup(cur, "frequency");
  1157. const auto *freq_dev = ucl_object_lookup(cur, "frequency_stddev");
  1158. const auto *nhits = ucl_object_lookup(cur, "hits");
  1159. if (sym && weight && freq && nhits) {
  1160. const char *sym_name;
  1161. if (sym->len > max_len) {
  1162. rspamd_snprintf(sym_buf, sizeof(sym_buf), "%*s...",
  1163. (max_len - 3), ucl_object_tostring(sym));
  1164. sym_name = sym_buf;
  1165. }
  1166. else {
  1167. sym_name = ucl_object_tostring(sym);
  1168. }
  1169. fmt::print(out, "| {:<4} | {:<{}} | {:^7.1f} | {:^6.3f}({:^5.3f}) | {:^7} |\n", i,
  1170. sym_name,
  1171. max_len,
  1172. ucl_object_todouble(weight),
  1173. ucl_object_todouble(freq) * 60.0,
  1174. ucl_object_todouble(freq_dev) * 60.0,
  1175. (std::uintmax_t)ucl_object_toint(nhits));
  1176. }
  1177. }
  1178. fmt::print(out, " {} \n", dash_buf);
  1179. }
  1180. static void
  1181. rspamc_stat_actions(ucl_object_t *obj, std::string &out, std::int64_t scanned)
  1182. {
  1183. const ucl_object_t *actions = ucl_object_lookup(obj, "actions"), *cur;
  1184. ucl_object_iter_t iter = nullptr;
  1185. if (scanned > 0) {
  1186. if (actions && ucl_object_type(actions) == UCL_OBJECT) {
  1187. while ((cur = ucl_object_iterate (actions, &iter, true)) != nullptr) {
  1188. auto cnt = ucl_object_toint(cur);
  1189. fmt::format_to(std::back_inserter(out), "Messages with action {}: {}, {:.2f}%\n",
  1190. ucl_object_key(cur), emphasis_argument(cnt),
  1191. ((double) cnt / (double) scanned) * 100.);
  1192. }
  1193. }
  1194. auto spam = ucl_object_toint(ucl_object_lookup(obj, "spam_count"));
  1195. auto ham = ucl_object_toint(ucl_object_lookup(obj, "ham_count"));
  1196. fmt::format_to(std::back_inserter(out), "Messages treated as spam: {}, {:.2f}%\n",
  1197. emphasis_argument(spam),
  1198. ((double) spam / (double) scanned) * 100.);
  1199. fmt::format_to(std::back_inserter(out), "Messages treated as ham: {}, {:.2f}%\n",
  1200. emphasis_argument(ham),
  1201. ((double) ham / (double) scanned) * 100.);
  1202. }
  1203. }
  1204. static void
  1205. rspamc_stat_statfile(const ucl_object_t *obj, std::string &out)
  1206. {
  1207. auto version = ucl_object_toint(ucl_object_lookup(obj, "revision"));
  1208. auto size = ucl_object_toint(ucl_object_lookup(obj, "size"));
  1209. auto blocks = ucl_object_toint(ucl_object_lookup(obj, "total"));
  1210. auto used_blocks = ucl_object_toint(ucl_object_lookup(obj, "used"));
  1211. auto label = ucl_object_tostring(ucl_object_lookup(obj, "label"));
  1212. auto symbol = ucl_object_tostring(ucl_object_lookup(obj, "symbol"));
  1213. auto type = ucl_object_tostring(ucl_object_lookup(obj, "type"));
  1214. auto nlanguages = ucl_object_toint(ucl_object_lookup(obj, "languages"));
  1215. auto nusers = ucl_object_toint(ucl_object_lookup(obj, "users"));
  1216. if (label) {
  1217. fmt::format_to(std::back_inserter(out), "Statfile: {} <{}> type: {}; ", symbol,
  1218. label, type);
  1219. }
  1220. else {
  1221. fmt::format_to(std::back_inserter(out), "Statfile: {} type: {}; ", symbol, type);
  1222. }
  1223. fmt::format_to(std::back_inserter(out), "length: {}; free blocks: {}; total blocks: {}; "
  1224. "free: {:.2f}%; learned: {}; users: {}; languages: {}\n",
  1225. size,
  1226. blocks - used_blocks, blocks,
  1227. blocks > 0 ? (blocks - used_blocks) * 100.0 / (double) blocks : 0,
  1228. version,
  1229. nusers, nlanguages);
  1230. }
  1231. static void
  1232. rspamc_stat_output(FILE *out, ucl_object_t *obj)
  1233. {
  1234. std::string out_str;
  1235. out_str.reserve(8192);
  1236. auto scanned = ucl_object_toint(ucl_object_lookup(obj, "scanned"));
  1237. fmt::format_to(std::back_inserter(out_str), "Messages scanned: {}\n",
  1238. emphasis_argument(scanned));
  1239. rspamc_stat_actions(obj, out_str, scanned);
  1240. fmt::format_to(std::back_inserter(out_str), "Messages learned: {}\n",
  1241. emphasis_argument(ucl_object_toint(ucl_object_lookup(obj, "learned"))));
  1242. fmt::format_to(std::back_inserter(out_str), "Connections count: {}\n",
  1243. emphasis_argument(ucl_object_toint(ucl_object_lookup(obj, "connections"))));
  1244. fmt::format_to(std::back_inserter(out_str), "Control connections count: {}\n",
  1245. emphasis_argument(ucl_object_toint(ucl_object_lookup(obj, "control_connections"))));
  1246. const auto *avg_time_obj = ucl_object_lookup(obj, "scan_times");
  1247. if (avg_time_obj && ucl_object_type(avg_time_obj) == UCL_ARRAY) {
  1248. ucl_object_iter_t iter = nullptr;
  1249. const ucl_object_t *cur;
  1250. std::vector<float> nums;
  1251. while ((cur = ucl_object_iterate (avg_time_obj, &iter, true)) != nullptr) {
  1252. if (ucl_object_type(cur) == UCL_FLOAT || ucl_object_type(cur) == UCL_INT) {
  1253. nums.push_back(ucl_object_todouble(cur));
  1254. }
  1255. }
  1256. auto cnt = nums.size();
  1257. if (cnt > 0) {
  1258. auto sum = rspamd_sum_floats(nums.data(), &cnt);
  1259. fmt::format_to(std::back_inserter(out_str),
  1260. "Average scan time: {} sec\n",
  1261. emphasis_argument(sum / cnt, 3));
  1262. }
  1263. }
  1264. /* Pools */
  1265. fmt::format_to(std::back_inserter(out_str), "Pools allocated: {}\n",
  1266. ucl_object_toint(ucl_object_lookup(obj, "pools_allocated")));
  1267. fmt::format_to(std::back_inserter(out_str), "Pools freed: {}\n",
  1268. ucl_object_toint(ucl_object_lookup(obj, "pools_freed")));
  1269. fmt::format_to(std::back_inserter(out_str), "Bytes allocated: {}\n",
  1270. ucl_object_toint(ucl_object_lookup(obj, "bytes_allocated")));
  1271. fmt::format_to(std::back_inserter(out_str), "Memory chunks allocated: {}\n",
  1272. ucl_object_toint(ucl_object_lookup(obj, "chunks_allocated")));
  1273. fmt::format_to(std::back_inserter(out_str), "Shared chunks allocated: {}\n",
  1274. ucl_object_toint(ucl_object_lookup(obj, "shared_chunks_allocated")));
  1275. fmt::format_to(std::back_inserter(out_str), "Chunks freed: {}\n",
  1276. ucl_object_toint(ucl_object_lookup(obj, "chunks_freed")));
  1277. fmt::format_to(std::back_inserter(out_str), "Oversized chunks: {}\n",
  1278. ucl_object_toint(ucl_object_lookup(obj, "chunks_oversized")));
  1279. /* Fuzzy */
  1280. const auto *st = ucl_object_lookup(obj, "fuzzy_hashes");
  1281. if (st) {
  1282. ucl_object_iter_t it = nullptr;
  1283. const ucl_object_t *cur;
  1284. std::uint64_t stored = 0;
  1285. while ((cur = ucl_iterate_object (st, &it, true)) != nullptr) {
  1286. auto num = ucl_object_toint(cur);
  1287. fmt::format_to(std::back_inserter(out_str), "Fuzzy hashes in storage \"{}\": {}\n",
  1288. ucl_object_key(cur),
  1289. num);
  1290. stored += num;
  1291. }
  1292. fmt::format_to(std::back_inserter(out_str), "Fuzzy hashes stored: {}\n",
  1293. stored);
  1294. }
  1295. st = ucl_object_lookup(obj, "fuzzy_checked");
  1296. if (st != nullptr && ucl_object_type(st) == UCL_ARRAY) {
  1297. ucl_object_iter_t iter = nullptr;
  1298. const ucl_object_t *cur;
  1299. out_str += "Fuzzy hashes checked: ";
  1300. while ((cur = ucl_object_iterate (st, &iter, true)) != nullptr) {
  1301. fmt::format_to(std::back_inserter(out_str), "{} ", ucl_object_toint(cur));
  1302. }
  1303. out_str.push_back('\n');
  1304. }
  1305. st = ucl_object_lookup(obj, "fuzzy_found");
  1306. if (st != nullptr && ucl_object_type(st) == UCL_ARRAY) {
  1307. ucl_object_iter_t iter = nullptr;
  1308. const ucl_object_t *cur;
  1309. out_str += "Fuzzy hashes found: ";
  1310. while ((cur = ucl_object_iterate (st, &iter, true)) != nullptr) {
  1311. fmt::format_to(std::back_inserter(out_str), "{} ", ucl_object_toint(cur));
  1312. }
  1313. out_str.push_back('\n');
  1314. }
  1315. st = ucl_object_lookup(obj, "statfiles");
  1316. if (st != nullptr && ucl_object_type(st) == UCL_ARRAY) {
  1317. ucl_object_iter_t iter = nullptr;
  1318. const ucl_object_t *cur;
  1319. while ((cur = ucl_object_iterate (st, &iter, true)) != nullptr) {
  1320. rspamc_stat_statfile(cur, out_str);
  1321. }
  1322. }
  1323. fmt::format_to(std::back_inserter(out_str), "Total learns: {}\n",
  1324. ucl_object_toint(ucl_object_lookup(obj, "total_learns")));
  1325. fmt::print(out, "{}", out_str.c_str());
  1326. }
  1327. static void
  1328. rspamc_output_headers(FILE *out, struct rspamd_http_message *msg)
  1329. {
  1330. struct rspamd_http_header *h;
  1331. kh_foreach_value (msg->headers, h, {
  1332. fmt::print(out, "{}: {}\n", std::string_view{h->name.begin, h->name.len},
  1333. std::string_view{h->value.begin, h->value.len});
  1334. });
  1335. fmt::print(out, "\n");
  1336. }
  1337. static void
  1338. rspamc_mime_output(FILE *out, ucl_object_t *result, GString *input,
  1339. gdouble time, GError *err)
  1340. {
  1341. const gchar *action = "no action", *line_end = "\r\n", *p;
  1342. gdouble score = 0.0, required_score = 0.0;
  1343. gboolean is_spam = FALSE;
  1344. auto nl_type = RSPAMD_TASK_NEWLINES_CRLF;
  1345. auto headers_pos = rspamd_string_find_eoh(input, nullptr);
  1346. if (headers_pos == -1) {
  1347. fmt::print(stderr, "cannot find end of headers position");
  1348. return;
  1349. }
  1350. p = input->str + headers_pos;
  1351. if (headers_pos > 1 && *(p - 1) == '\n') {
  1352. if (headers_pos > 2 && *(p - 2) == '\r') {
  1353. line_end = "\r\n";
  1354. nl_type = RSPAMD_TASK_NEWLINES_CRLF;
  1355. }
  1356. else {
  1357. line_end = "\n";
  1358. nl_type = RSPAMD_TASK_NEWLINES_LF;
  1359. }
  1360. }
  1361. else if (headers_pos > 1 && *(p - 1) == '\r') {
  1362. line_end = "\r";
  1363. nl_type = RSPAMD_TASK_NEWLINES_CR;
  1364. }
  1365. std::string added_headers;
  1366. if (result) {
  1367. const auto *res = ucl_object_lookup(result, "action");
  1368. if (res) {
  1369. action = ucl_object_tostring(res);
  1370. }
  1371. res = ucl_object_lookup(result, "score");
  1372. if (res) {
  1373. score = ucl_object_todouble(res);
  1374. }
  1375. res = ucl_object_lookup(result, "required_score");
  1376. if (res) {
  1377. required_score = ucl_object_todouble(res);
  1378. }
  1379. auto act = rspamd_action_from_str_rspamc(action);
  1380. if (act.has_value() && act.value() < METRIC_ACTION_GREYLIST) {
  1381. is_spam = TRUE;
  1382. }
  1383. fmt::format_to(std::back_inserter(added_headers), "X-Spam-Scanner: {}{}",
  1384. "rspamc " RVERSION, line_end);
  1385. fmt::format_to(std::back_inserter(added_headers), "X-Spam-Scan-Time: {:.3}{}",
  1386. time, line_end);
  1387. /*
  1388. * TODO: add milter_headers support here
  1389. */
  1390. if (is_spam) {
  1391. fmt::format_to(std::back_inserter(added_headers), "X-Spam: yes{}", line_end);
  1392. }
  1393. fmt::format_to(std::back_inserter(added_headers),"X-Spam-Action: {}{}",
  1394. action, line_end);
  1395. fmt::format_to(std::back_inserter(added_headers), "X-Spam-Score: {:.2f} / {:.2f}{}",
  1396. score, required_score, line_end);
  1397. /* SA style stars header */
  1398. std::string scorebuf;
  1399. auto adjusted_score = std::min(score, 32.0);
  1400. while(adjusted_score > 0) {
  1401. scorebuf.push_back('*');
  1402. adjusted_score -= 1.0;
  1403. }
  1404. fmt::format_to(std::back_inserter(added_headers), "X-Spam-Level: {}{}",
  1405. scorebuf, line_end);
  1406. /* Short description of all symbols */
  1407. std::string symbuf;
  1408. const ucl_object_t *cur;
  1409. ucl_object_iter_t it = nullptr;
  1410. const auto *syms = ucl_object_lookup(result, "symbols");
  1411. while (syms && (cur = ucl_object_iterate (syms, &it, true)) != nullptr) {
  1412. if (ucl_object_type(cur) == UCL_OBJECT) {
  1413. fmt::format_to(std::back_inserter(symbuf), "{},", ucl_object_key(cur));
  1414. }
  1415. }
  1416. /* Trim the last comma */
  1417. if (symbuf.back() == ',') {
  1418. symbuf.pop_back();
  1419. }
  1420. auto *folded_symbuf = rspamd_header_value_fold("X-Spam-Symbols", strlen("X-Spam-Symbols"),
  1421. symbuf.data(), symbuf.size(),
  1422. 0, nl_type, ",");
  1423. fmt::format_to(std::back_inserter(added_headers), "X-Spam-Symbols: {}{}",
  1424. folded_symbuf->str, line_end);
  1425. g_string_free(folded_symbuf, TRUE);
  1426. res = ucl_object_lookup(result, "dkim-signature");
  1427. if (res && res->type == UCL_STRING) {
  1428. fmt::format_to(std::back_inserter(added_headers), "DKIM-Signature: {}{}",
  1429. ucl_object_tostring(res), line_end);
  1430. }
  1431. else if (res && res->type == UCL_ARRAY) {
  1432. it = nullptr;
  1433. while ((cur = ucl_object_iterate (res, &it, true)) != nullptr) {
  1434. fmt::format_to(std::back_inserter(added_headers), "DKIM-Signature: {}{}",
  1435. ucl_object_tostring(cur), line_end);
  1436. }
  1437. }
  1438. if (json || ucl_reply || compact) {
  1439. unsigned char *json_header;
  1440. /* We also append json data as a specific header */
  1441. if (json) {
  1442. json_header = ucl_object_emit(result,
  1443. compact ? UCL_EMIT_JSON_COMPACT : UCL_EMIT_JSON);
  1444. }
  1445. else {
  1446. json_header = ucl_object_emit(result,
  1447. compact ? UCL_EMIT_JSON_COMPACT : UCL_EMIT_CONFIG);
  1448. }
  1449. auto *json_header_encoded = rspamd_encode_base64_fold(json_header,
  1450. strlen((char *)json_header), 60, nullptr, nl_type);
  1451. free(json_header);
  1452. fmt::format_to(std::back_inserter(added_headers),
  1453. "X-Spam-Result: {}{}",
  1454. json_header_encoded, line_end);
  1455. g_free(json_header_encoded);
  1456. }
  1457. ucl_object_unref(result);
  1458. }
  1459. else {
  1460. fmt::format_to(std::back_inserter(added_headers), "X-Spam-Scanner: {}{}",
  1461. "rspamc " RVERSION, line_end);
  1462. fmt::format_to(std::back_inserter(added_headers), "X-Spam-Scan-Time: {:.3f}{}",
  1463. time, line_end);
  1464. fmt::format_to(std::back_inserter(added_headers), "X-Spam-Error: {}{}",
  1465. err->message, line_end);
  1466. }
  1467. /* Write message */
  1468. /* Original headers */
  1469. fmt::print(out, "{}", std::string_view{input->str, (std::size_t)headers_pos});
  1470. /* Added headers */
  1471. fmt::print(out, "{}", added_headers);
  1472. /* Message body */
  1473. fmt::print(out, "{}", input->str + headers_pos);
  1474. }
  1475. static void
  1476. rspamc_client_execute_cmd(const struct rspamc_command &cmd, ucl_object_t *result,
  1477. GString *input, gdouble time, GError *err)
  1478. {
  1479. gchar **eargv;
  1480. gint eargc, infd, outfd, errfd;
  1481. GError *exec_err = nullptr;
  1482. GPid cld;
  1483. if (!g_shell_parse_argv(execute, &eargc, &eargv, &err)) {
  1484. fmt::print(stderr, "Cannot execute {}: {}", execute, err->message);
  1485. g_error_free(err);
  1486. return;
  1487. }
  1488. if (!g_spawn_async_with_pipes(nullptr, eargv, nullptr,
  1489. static_cast<GSpawnFlags>(G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD), nullptr, nullptr, &cld,
  1490. &infd, &outfd, &errfd, &exec_err)) {
  1491. fmt::print(stderr, "Cannot execute {}: {}", execute, exec_err->message);
  1492. g_error_free(exec_err);
  1493. exit(EXIT_FAILURE);
  1494. }
  1495. else {
  1496. children.push_back(cld);
  1497. auto *out = fdopen(infd, "w");
  1498. if (cmd.cmd == RSPAMC_COMMAND_SYMBOLS && mime_output && input) {
  1499. rspamc_mime_output(out, result, input, time, err);
  1500. }
  1501. else if (result) {
  1502. if (ucl_reply || cmd.command_output_func == nullptr) {
  1503. char *ucl_out;
  1504. if (json) {
  1505. ucl_out = (char *)ucl_object_emit(result,
  1506. compact ? UCL_EMIT_JSON_COMPACT : UCL_EMIT_JSON);
  1507. }
  1508. else {
  1509. ucl_out = (char *)ucl_object_emit(result,
  1510. compact ? UCL_EMIT_JSON_COMPACT : UCL_EMIT_CONFIG);
  1511. }
  1512. fmt::print(out, "{}", ucl_out);
  1513. free(ucl_out);
  1514. }
  1515. else {
  1516. cmd.command_output_func(out, result);
  1517. }
  1518. ucl_object_unref(result);
  1519. }
  1520. else {
  1521. fmt::print(out, "{}\n", err->message);
  1522. }
  1523. fflush(out);
  1524. fclose(out);
  1525. }
  1526. g_strfreev(eargv);
  1527. }
  1528. static void
  1529. rspamc_client_cb(struct rspamd_client_connection *conn,
  1530. struct rspamd_http_message *msg,
  1531. const char *name, ucl_object_t *result, GString *input,
  1532. gpointer ud, gdouble start_time, gdouble send_time,
  1533. const char *body, gsize bodylen,
  1534. GError *err)
  1535. {
  1536. struct rspamc_callback_data *cbdata = (struct rspamc_callback_data *) ud;
  1537. FILE *out = stdout;
  1538. gdouble finish = rspamd_get_ticks(FALSE), diff;
  1539. auto &cmd = cbdata->cmd;
  1540. if (send_time > 0) {
  1541. diff = finish - send_time;
  1542. }
  1543. else {
  1544. diff = finish - start_time;
  1545. }
  1546. if (execute) {
  1547. /* Pass all to the external command */
  1548. rspamc_client_execute_cmd(cmd, result, input, diff, err);
  1549. }
  1550. else {
  1551. if (cmd.cmd == RSPAMC_COMMAND_SYMBOLS && mime_output && input) {
  1552. if (body) {
  1553. GString tmp;
  1554. tmp.str = (char *) body;
  1555. tmp.len = bodylen;
  1556. rspamc_mime_output(out, result, &tmp, diff, err);
  1557. }
  1558. else {
  1559. rspamc_mime_output(out, result, input, diff, err);
  1560. }
  1561. }
  1562. else {
  1563. if (cmd.need_input && !json) {
  1564. if (!compact && !humanreport) {
  1565. fmt::print(out, "Results for file: {} ({:.3} seconds)\n",
  1566. emphasis_argument(cbdata->filename), diff);
  1567. }
  1568. }
  1569. else {
  1570. if (!compact && !json && !humanreport) {
  1571. fmt::print(out, "Results for command: {} ({:.3} seconds)\n",
  1572. emphasis_argument(cmd.name), diff);
  1573. }
  1574. }
  1575. if (result != nullptr) {
  1576. if (headers && msg != nullptr) {
  1577. rspamc_output_headers(out, msg);
  1578. }
  1579. if (ucl_reply || cmd.command_output_func == nullptr) {
  1580. if (cmd.need_input) {
  1581. ucl_object_insert_key(result,
  1582. ucl_object_fromstring(cbdata->filename.c_str()),
  1583. "filename", 0,
  1584. false);
  1585. }
  1586. ucl_object_insert_key(result,
  1587. ucl_object_fromdouble(diff),
  1588. "scan_time", 0,
  1589. false);
  1590. char *ucl_out;
  1591. if (json) {
  1592. ucl_out = (char *)ucl_object_emit(result,
  1593. compact ? UCL_EMIT_JSON_COMPACT : UCL_EMIT_JSON);
  1594. }
  1595. else {
  1596. ucl_out = (char *)ucl_object_emit(result,
  1597. compact ? UCL_EMIT_JSON_COMPACT : UCL_EMIT_CONFIG);
  1598. }
  1599. fmt::print(out, "{}", ucl_out);
  1600. free(ucl_out);
  1601. }
  1602. else {
  1603. cmd.command_output_func(out, result);
  1604. }
  1605. if (body) {
  1606. fmt::print(out, "\nNew body:\n{}\n",
  1607. std::string_view{body, bodylen});
  1608. }
  1609. ucl_object_unref(result);
  1610. }
  1611. else if (err != nullptr) {
  1612. fmt::print(out, "{}\n", err->message);
  1613. if (json && msg != nullptr) {
  1614. gsize rawlen;
  1615. auto *raw_body = rspamd_http_message_get_body(msg, &rawlen);
  1616. if (raw_body) {
  1617. /* We can also output the resulting json */
  1618. fmt::print(out, "{}\n", std::string_view{raw_body, (std::size_t)(rawlen - bodylen)});
  1619. }
  1620. }
  1621. }
  1622. fmt::print(out, "\n");
  1623. }
  1624. fflush(out);
  1625. }
  1626. rspamd_client_destroy(conn);
  1627. delete cbdata;
  1628. if (err) {
  1629. retcode = EXIT_FAILURE;
  1630. }
  1631. }
  1632. static void
  1633. rspamc_process_input(struct ev_loop *ev_base, const struct rspamc_command &cmd,
  1634. FILE *in, const std::string &name, GQueue *attrs)
  1635. {
  1636. struct rspamd_client_connection *conn;
  1637. const char *p;
  1638. guint16 port;
  1639. GError *err = nullptr;
  1640. std::string hostbuf;
  1641. if (connect_str[0] == '[') {
  1642. p = strrchr(connect_str, ']');
  1643. if (p != nullptr) {
  1644. hostbuf.assign(connect_str + 1, (std::size_t)(p - connect_str - 1));
  1645. p++;
  1646. }
  1647. else {
  1648. p = connect_str;
  1649. }
  1650. }
  1651. else {
  1652. p = connect_str;
  1653. }
  1654. p = strrchr(p, ':');
  1655. if (hostbuf.empty()) {
  1656. if (p != nullptr) {
  1657. hostbuf.assign(connect_str, (std::size_t)(p - connect_str));
  1658. }
  1659. else {
  1660. hostbuf.assign(connect_str);
  1661. }
  1662. }
  1663. if (p != nullptr) {
  1664. port = strtoul(p + 1, nullptr, 10);
  1665. }
  1666. else {
  1667. /*
  1668. * If we connect to localhost, 127.0.0.1 or ::1, then try controller
  1669. * port first
  1670. */
  1671. if (hostbuf == "localhost" ||
  1672. hostbuf == "127.0.0.1"||
  1673. hostbuf == "::1" ||
  1674. hostbuf == "[::1]") {
  1675. port = DEFAULT_CONTROL_PORT;
  1676. }
  1677. else {
  1678. port = cmd.is_controller ? DEFAULT_CONTROL_PORT : DEFAULT_PORT;
  1679. }
  1680. }
  1681. conn = rspamd_client_init(http_ctx, ev_base, hostbuf.c_str(), port, timeout, pubkey);
  1682. if (conn != nullptr) {
  1683. auto *cbdata = new rspamc_callback_data;
  1684. cbdata->cmd = cmd;
  1685. cbdata->filename = name;
  1686. if (cmd.need_input) {
  1687. rspamd_client_command(conn, cmd.path, attrs, in, rspamc_client_cb,
  1688. cbdata, compressed, dictionary, cbdata->filename.c_str(), &err);
  1689. }
  1690. else {
  1691. rspamd_client_command(conn,
  1692. cmd.path,
  1693. attrs,
  1694. nullptr,
  1695. rspamc_client_cb,
  1696. cbdata,
  1697. compressed,
  1698. dictionary,
  1699. cbdata->filename.c_str(),
  1700. &err);
  1701. }
  1702. }
  1703. else {
  1704. fmt::print(stderr, "cannot connect to {}: {}\n", connect_str,
  1705. strerror(errno));
  1706. exit(EXIT_FAILURE);
  1707. }
  1708. }
  1709. static gsize
  1710. rspamd_dirent_size(DIR *dirp)
  1711. {
  1712. goffset name_max;
  1713. gsize name_end;
  1714. #if defined(HAVE_FPATHCONF) && defined(HAVE_DIRFD) \
  1715. && defined(_PC_NAME_MAX)
  1716. name_max = fpathconf(dirfd(dirp), _PC_NAME_MAX);
  1717. # if defined(NAME_MAX)
  1718. if (name_max == -1) {
  1719. name_max = (NAME_MAX > 255) ? NAME_MAX : 255;
  1720. }
  1721. # else
  1722. if (name_max == -1) {
  1723. return (size_t)(-1);
  1724. }
  1725. # endif
  1726. #else
  1727. # if defined(NAME_MAX)
  1728. name_max = (NAME_MAX > 255) ? NAME_MAX : 255;
  1729. # else
  1730. # error "buffer size for readdir_r cannot be determined"
  1731. # endif
  1732. #endif
  1733. name_end = G_STRUCT_OFFSET (struct dirent, d_name) + name_max + 1;
  1734. return (name_end > sizeof(struct dirent) ? name_end : sizeof(struct dirent));
  1735. }
  1736. static void
  1737. rspamc_process_dir(struct ev_loop *ev_base, const struct rspamc_command &cmd,
  1738. const std::string &name, GQueue *attrs)
  1739. {
  1740. static auto cur_req = 0;
  1741. auto *d = opendir(name.c_str());
  1742. if (d != nullptr) {
  1743. struct dirent *pentry;
  1744. std::string fpath;
  1745. fpath.reserve(PATH_MAX);
  1746. while ((pentry = readdir(d)) != nullptr) {
  1747. if (pentry->d_name[0] == '.') {
  1748. continue;
  1749. }
  1750. fpath.clear();
  1751. fmt::format_to(std::back_inserter(fpath), "{}{}{}",
  1752. name, G_DIR_SEPARATOR,
  1753. pentry->d_name);
  1754. /* Check exclude */
  1755. auto **ex = exclude_compiled;
  1756. auto skip = false;
  1757. while (ex != nullptr && *ex != nullptr) {
  1758. #if GLIB_MAJOR_VERSION >= 2 && GLIB_MINOR_VERSION >= 70
  1759. if (g_pattern_spec_match(*ex, fpath.size(), fpath.c_str(), nullptr)) {
  1760. #else
  1761. if (g_pattern_match(*ex, fpath.size(), fpath.c_str(), nullptr)) {
  1762. #endif
  1763. skip = true;
  1764. break;
  1765. }
  1766. ex++;
  1767. }
  1768. if (skip) {
  1769. continue;
  1770. }
  1771. auto is_reg = false;
  1772. auto is_dir = false;
  1773. struct stat st;
  1774. #if (defined(_DIRENT_HAVE_D_TYPE) || defined(__APPLE__)) && defined(DT_UNKNOWN)
  1775. if (pentry->d_type == DT_UNKNOWN) {
  1776. /* Fallback to lstat */
  1777. if (lstat(fpath.c_str(), &st) == -1) {
  1778. fmt::print(stderr, "cannot stat file {}: {}\n",
  1779. fpath, strerror(errno));
  1780. continue;
  1781. }
  1782. is_dir = S_ISDIR(st.st_mode);
  1783. is_reg = S_ISREG(st.st_mode);
  1784. }
  1785. else {
  1786. if (pentry->d_type == DT_REG) {
  1787. is_reg = true;
  1788. }
  1789. else if (pentry->d_type == DT_DIR) {
  1790. is_dir = true;
  1791. }
  1792. }
  1793. #else
  1794. if (lstat(fpath.c_str(), &st) == -1) {
  1795. fmt::print(stderr, "cannot stat file {}: {}\n",
  1796. fpath, strerror (errno));
  1797. continue;
  1798. }
  1799. is_dir = S_ISDIR(st.st_mode);
  1800. is_reg = S_ISREG(st.st_mode);
  1801. #endif
  1802. if (is_dir) {
  1803. rspamc_process_dir(ev_base, cmd, fpath, attrs);
  1804. continue;
  1805. }
  1806. else if (is_reg) {
  1807. auto *in = fopen(fpath.c_str(), "r");
  1808. if (in == nullptr) {
  1809. fmt::print(stderr, "cannot open file {}: {}\n",
  1810. fpath, strerror(errno));
  1811. continue;
  1812. }
  1813. rspamc_process_input(ev_base, cmd, in, fpath, attrs);
  1814. cur_req++;
  1815. fclose(in);
  1816. if (cur_req >= max_requests) {
  1817. cur_req = 0;
  1818. /* Wait for completion */
  1819. ev_loop(ev_base, 0);
  1820. }
  1821. }
  1822. }
  1823. }
  1824. else {
  1825. fmt::print(stderr, "cannot open directory {}: {}\n", name, strerror(errno));
  1826. exit(EXIT_FAILURE);
  1827. }
  1828. closedir(d);
  1829. ev_loop(ev_base, 0);
  1830. }
  1831. static void
  1832. rspamc_kwattr_free(gpointer p)
  1833. {
  1834. struct rspamd_http_client_header *h = (struct rspamd_http_client_header *) p;
  1835. g_free(h->value);
  1836. g_free(h->name);
  1837. g_free(h);
  1838. }
  1839. int
  1840. main(int argc, char **argv, char **env)
  1841. {
  1842. auto *kwattrs = g_queue_new();
  1843. read_cmd_line(&argc, &argv);
  1844. tty = isatty(STDOUT_FILENO);
  1845. if (print_commands) {
  1846. print_commands_list();
  1847. exit(EXIT_SUCCESS);
  1848. }
  1849. /* Deal with exclude patterns */
  1850. auto **exclude_pattern = exclude_patterns;
  1851. auto npatterns = 0;
  1852. while (exclude_pattern && *exclude_pattern) {
  1853. exclude_pattern++;
  1854. npatterns++;
  1855. }
  1856. if (npatterns > 0) {
  1857. exclude_compiled = g_new0(GPatternSpec *, (npatterns + 1));
  1858. for (auto i = 0; i < npatterns; i++) {
  1859. exclude_compiled[i] = g_pattern_spec_new(exclude_patterns[i]);
  1860. if (exclude_compiled[i] == nullptr) {
  1861. fmt::print(stderr, "Invalid glob pattern: {}\n",
  1862. exclude_patterns[i]);
  1863. exit(EXIT_FAILURE);
  1864. }
  1865. }
  1866. }
  1867. auto *libs = rspamd_init_libs();
  1868. auto *event_loop = ev_loop_new(EVBACKEND_ALL);
  1869. struct rspamd_http_context_cfg http_config;
  1870. memset(&http_config, 0, sizeof(http_config));
  1871. http_config.kp_cache_size_client = 32;
  1872. http_config.kp_cache_size_server = 0;
  1873. http_config.user_agent = user_agent;
  1874. http_ctx = rspamd_http_context_create_config(&http_config,
  1875. event_loop, nullptr);
  1876. /* Ignore sigpipe */
  1877. struct sigaction sigpipe_act;
  1878. sigemptyset (&sigpipe_act.sa_mask);
  1879. sigaddset (&sigpipe_act.sa_mask, SIGPIPE);
  1880. sigpipe_act.sa_handler = SIG_IGN;
  1881. sigpipe_act.sa_flags = 0;
  1882. sigaction(SIGPIPE, &sigpipe_act, nullptr);
  1883. /* Now read other args from argc and argv */
  1884. FILE *in = nullptr;
  1885. std::optional<rspamc_command> maybe_cmd;
  1886. auto start_argc = 0;
  1887. if (argc == 1) {
  1888. start_argc = argc;
  1889. in = stdin;
  1890. maybe_cmd = check_rspamc_command("symbols");
  1891. }
  1892. else if (argc == 2) {
  1893. /* One argument is whether command or filename */
  1894. maybe_cmd = check_rspamc_command(argv[1]);
  1895. if (maybe_cmd.has_value()) {
  1896. start_argc = argc;
  1897. in = stdin;
  1898. }
  1899. else {
  1900. maybe_cmd = check_rspamc_command("symbols"); /* Symbols command */
  1901. start_argc = 1;
  1902. }
  1903. }
  1904. else {
  1905. maybe_cmd = check_rspamc_command(argv[1]);
  1906. if (maybe_cmd.has_value()) {
  1907. auto &cmd = maybe_cmd.value();
  1908. /* In case of command read arguments starting from 2 */
  1909. if (cmd.cmd == RSPAMC_COMMAND_ADD_SYMBOL || cmd.cmd == RSPAMC_COMMAND_ADD_ACTION) {
  1910. if (argc < 4 || argc > 5) {
  1911. fmt::print(stderr, "invalid arguments\n");
  1912. exit(EXIT_FAILURE);
  1913. }
  1914. if (argc == 5) {
  1915. add_client_header(kwattrs, "metric", argv[2]);
  1916. add_client_header(kwattrs, "name", argv[3]);
  1917. add_client_header(kwattrs, "value", argv[4]);
  1918. }
  1919. else {
  1920. add_client_header(kwattrs, "name", argv[2]);
  1921. add_client_header(kwattrs, "value", argv[3]);
  1922. }
  1923. start_argc = argc;
  1924. }
  1925. else {
  1926. start_argc = 2;
  1927. }
  1928. }
  1929. else {
  1930. maybe_cmd = check_rspamc_command("symbols");
  1931. start_argc = 1;
  1932. }
  1933. }
  1934. if (!maybe_cmd.has_value()) {
  1935. fmt::print(stderr, "invalid command\n");
  1936. exit(EXIT_FAILURE);
  1937. }
  1938. add_options(kwattrs);
  1939. auto cmd = maybe_cmd.value();
  1940. if (start_argc == argc) {
  1941. /* Do command without input or with stdin */
  1942. if (empty_input) {
  1943. rspamc_process_input(event_loop, cmd, nullptr, "empty", kwattrs);
  1944. }
  1945. else {
  1946. rspamc_process_input(event_loop, cmd, in, "stdin", kwattrs);
  1947. }
  1948. }
  1949. else {
  1950. auto cur_req = 0;
  1951. for (auto i = start_argc; i < argc; i++) {
  1952. if (cmd.cmd == RSPAMC_COMMAND_FUZZY_DELHASH) {
  1953. add_client_header(kwattrs, "Hash", argv[i]);
  1954. }
  1955. else {
  1956. struct stat st;
  1957. if (stat(argv[i], &st) == -1) {
  1958. fmt::print(stderr, "cannot stat file {}\n", argv[i]);
  1959. exit(EXIT_FAILURE);
  1960. }
  1961. if (S_ISDIR (st.st_mode)) {
  1962. /* Directories are processed with a separate limit */
  1963. rspamc_process_dir(event_loop, cmd, argv[i], kwattrs);
  1964. cur_req = 0;
  1965. }
  1966. else {
  1967. in = fopen(argv[i], "r");
  1968. if (in == nullptr) {
  1969. fmt::print(stderr, "cannot open file {}\n", argv[i]);
  1970. exit(EXIT_FAILURE);
  1971. }
  1972. rspamc_process_input(event_loop, cmd, in, argv[i], kwattrs);
  1973. cur_req++;
  1974. fclose(in);
  1975. }
  1976. if (cur_req >= max_requests) {
  1977. cur_req = 0;
  1978. /* Wait for completion */
  1979. ev_loop(event_loop, 0);
  1980. }
  1981. }
  1982. }
  1983. if (cmd.cmd == RSPAMC_COMMAND_FUZZY_DELHASH) {
  1984. rspamc_process_input(event_loop, cmd, nullptr, "hashes", kwattrs);
  1985. }
  1986. }
  1987. ev_loop(event_loop, 0);
  1988. g_queue_free_full(kwattrs, rspamc_kwattr_free);
  1989. /* Wait for children processes */
  1990. auto ret = 0;
  1991. for (auto cld : children) {
  1992. auto res = 0;
  1993. if (waitpid(cld, &res, 0) == -1) {
  1994. fmt::print(stderr, "Cannot wait for {}: {}", cld,
  1995. strerror(errno));
  1996. ret = errno;
  1997. }
  1998. if (ret == 0) {
  1999. /* Check return code */
  2000. if (WIFSIGNALED (res)) {
  2001. ret = WTERMSIG (res);
  2002. }
  2003. else if (WIFEXITED (res)) {
  2004. ret = WEXITSTATUS (res);
  2005. }
  2006. }
  2007. }
  2008. for (auto i = 0; i < npatterns; i++) {
  2009. g_pattern_spec_free(exclude_compiled[i]);
  2010. }
  2011. g_free(exclude_compiled);
  2012. rspamd_deinit_libs(libs);
  2013. /* Mix retcode (return from Rspamd side) and ret (return from subprocess) */
  2014. return ret | retcode;
  2015. }